xref: /freebsd/sys/dev/bge/if_bge.c (revision 6ede2cfa12ab33cfd46d3c75415ba4382e927888)
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;
771a813ed78SPyun YongHyeon 	uint32_t val;
77295d67482SBill Paul 	int i;
77395d67482SBill Paul 
77495d67482SBill Paul 	sc = device_get_softc(dev);
77595d67482SBill Paul 
776a813ed78SPyun YongHyeon 	/* Prevent the probe from finding incorrect devices. */
777a813ed78SPyun YongHyeon 	if (phy != sc->bge_phy_addr)
77898b28ee5SBill Paul 		return (0);
77998b28ee5SBill Paul 
780a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
781a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
782a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
783a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
784a813ed78SPyun YongHyeon 		DELAY(80);
78537ceeb4dSPaul Saab 	}
78637ceeb4dSPaul Saab 
78795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
78895d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
78995d67482SBill Paul 
790a813ed78SPyun YongHyeon 	/* Poll for the PHY register access to complete. */
79195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
792d5d23857SJung-uk Kim 		DELAY(10);
79395d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
794a813ed78SPyun YongHyeon 		if ((val & BGE_MICOMM_BUSY) == 0) {
795a813ed78SPyun YongHyeon 			DELAY(5);
796a813ed78SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_MI_COMM);
79795d67482SBill Paul 			break;
79895d67482SBill Paul 		}
799a813ed78SPyun YongHyeon 	}
80095d67482SBill Paul 
80195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
8025fea260fSMarius Strobl 		device_printf(sc->bge_dev,
8035fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
8045fea260fSMarius Strobl 		    phy, reg, val);
80537ceeb4dSPaul Saab 		val = 0;
80695d67482SBill Paul 	}
80795d67482SBill Paul 
808a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
809a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
810a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
811a813ed78SPyun YongHyeon 		DELAY(80);
81237ceeb4dSPaul Saab 	}
81337ceeb4dSPaul Saab 
81495d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
81595d67482SBill Paul 		return (0);
81695d67482SBill Paul 
8170c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
81895d67482SBill Paul }
81995d67482SBill Paul 
82095d67482SBill Paul static int
8213f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
82295d67482SBill Paul {
82395d67482SBill Paul 	struct bge_softc *sc;
82495d67482SBill Paul 	int i;
82595d67482SBill Paul 
82695d67482SBill Paul 	sc = device_get_softc(dev);
82795d67482SBill Paul 
82838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
82938cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
83038cc658fSJohn Baldwin 		return (0);
83138cc658fSJohn Baldwin 
832a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
833a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
834a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
835a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
836a813ed78SPyun YongHyeon 		DELAY(80);
83737ceeb4dSPaul Saab 	}
83837ceeb4dSPaul Saab 
83995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
84095d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
84195d67482SBill Paul 
84295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
843d5d23857SJung-uk Kim 		DELAY(10);
84438cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
84538cc658fSJohn Baldwin 			DELAY(5);
84638cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
84795d67482SBill Paul 			break;
848d5d23857SJung-uk Kim 		}
84938cc658fSJohn Baldwin 	}
850d5d23857SJung-uk Kim 
851a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
852a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
853a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
854a813ed78SPyun YongHyeon 		DELAY(80);
855a813ed78SPyun YongHyeon 	}
856a813ed78SPyun YongHyeon 
857a813ed78SPyun YongHyeon 	if (i == BGE_TIMEOUT)
85838cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
85938cc658fSJohn Baldwin 		    "PHY write timed out (phy %d, reg %d, val %d)\n",
86038cc658fSJohn Baldwin 		    phy, reg, val);
86137ceeb4dSPaul Saab 
86295d67482SBill Paul 	return (0);
86395d67482SBill Paul }
86495d67482SBill Paul 
86595d67482SBill Paul static void
8663f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
86795d67482SBill Paul {
86895d67482SBill Paul 	struct bge_softc *sc;
86995d67482SBill Paul 	struct mii_data *mii;
87095d67482SBill Paul 	sc = device_get_softc(dev);
87195d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
87295d67482SBill Paul 
87395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
874ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
875ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
87695d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
8773f74909aSGleb Smirnoff 	else
87895d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
87995d67482SBill Paul 
8806854be25SPyun YongHyeon 	if (IFM_OPTIONS(mii->mii_media_active & IFM_FDX) != 0) {
88195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
8826854be25SPyun YongHyeon 		if (IFM_OPTIONS(mii->mii_media_active) & IFM_FLAG1)
8836854be25SPyun YongHyeon 			BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
8843f74909aSGleb Smirnoff 		else
8856854be25SPyun YongHyeon 			BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
8866854be25SPyun YongHyeon 		if (IFM_OPTIONS(mii->mii_media_active) & IFM_FLAG0)
8876854be25SPyun YongHyeon 			BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
8886854be25SPyun YongHyeon 		else
8896854be25SPyun YongHyeon 			BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
8906854be25SPyun YongHyeon 	} else {
89195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
8926854be25SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
8936854be25SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
8946854be25SPyun YongHyeon 	}
89595d67482SBill Paul }
89695d67482SBill Paul 
89795d67482SBill Paul /*
89895d67482SBill Paul  * Intialize a standard receive ring descriptor.
89995d67482SBill Paul  */
90095d67482SBill Paul static int
901943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
90295d67482SBill Paul {
903943787f3SPyun YongHyeon 	struct mbuf *m;
90495d67482SBill Paul 	struct bge_rx_bd *r;
905a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
906943787f3SPyun YongHyeon 	bus_dmamap_t map;
907a23634a1SPyun YongHyeon 	int error, nsegs;
90895d67482SBill Paul 
909943787f3SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
910943787f3SPyun YongHyeon 	if (m == NULL)
91195d67482SBill Paul 		return (ENOBUFS);
912943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
913652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
914943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
915943787f3SPyun YongHyeon 
9160ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
917943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
918a23634a1SPyun YongHyeon 	if (error != 0) {
919943787f3SPyun YongHyeon 		m_freem(m);
920a23634a1SPyun YongHyeon 		return (error);
921f41ac2beSBill Paul 	}
922943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
923943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
924943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
925943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
926943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
927943787f3SPyun YongHyeon 	}
928943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
929943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
930943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
931943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
932e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
933943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
934a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
935a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
936e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
937a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
938e907febfSPyun YongHyeon 	r->bge_idx = i;
939f41ac2beSBill Paul 
9400ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
941943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
94295d67482SBill Paul 
94395d67482SBill Paul 	return (0);
94495d67482SBill Paul }
94595d67482SBill Paul 
94695d67482SBill Paul /*
94795d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
94895d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
94995d67482SBill Paul  */
95095d67482SBill Paul static int
951943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
95295d67482SBill Paul {
9531be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
954943787f3SPyun YongHyeon 	bus_dmamap_t map;
9551be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
956943787f3SPyun YongHyeon 	struct mbuf *m;
957943787f3SPyun YongHyeon 	int error, nsegs;
95895d67482SBill Paul 
959943787f3SPyun YongHyeon 	MGETHDR(m, M_DONTWAIT, MT_DATA);
960943787f3SPyun YongHyeon 	if (m == NULL)
96195d67482SBill Paul 		return (ENOBUFS);
96295d67482SBill Paul 
963943787f3SPyun YongHyeon 	m_cljget(m, M_DONTWAIT, MJUM9BYTES);
964943787f3SPyun YongHyeon 	if (!(m->m_flags & M_EXT)) {
965943787f3SPyun YongHyeon 		m_freem(m);
96695d67482SBill Paul 		return (ENOBUFS);
96795d67482SBill Paul 	}
968943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
969652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
970943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
9711be6acb7SGleb Smirnoff 
9721be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
973943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
974943787f3SPyun YongHyeon 	if (error != 0) {
975943787f3SPyun YongHyeon 		m_freem(m);
9761be6acb7SGleb Smirnoff 		return (error);
977f7cea149SGleb Smirnoff 	}
9781be6acb7SGleb Smirnoff 
979943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] == NULL) {
980943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
981943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
982943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
983943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
984943787f3SPyun YongHyeon 	}
985943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
986943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
987943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
988943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
989943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
990e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
991e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
992e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
993e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
994e0b7b101SPyun YongHyeon 
9951be6acb7SGleb Smirnoff 	/*
9961be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
9971be6acb7SGleb Smirnoff 	 */
998943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
9994e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
10004e7ba1abSGleb Smirnoff 	r->bge_idx = i;
10014e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
10024e7ba1abSGleb Smirnoff 	switch (nsegs) {
10034e7ba1abSGleb Smirnoff 	case 4:
10044e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
10054e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
10064e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1007e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
10084e7ba1abSGleb Smirnoff 	case 3:
1009e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1010e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1011e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1012e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
10134e7ba1abSGleb Smirnoff 	case 2:
10144e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
10154e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
10164e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1017e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
10184e7ba1abSGleb Smirnoff 	case 1:
10194e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
10204e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
10214e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1022e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
10234e7ba1abSGleb Smirnoff 		break;
10244e7ba1abSGleb Smirnoff 	default:
10254e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
10264e7ba1abSGleb Smirnoff 	}
1027f41ac2beSBill Paul 
1028a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1029943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
103095d67482SBill Paul 
103195d67482SBill Paul 	return (0);
103295d67482SBill Paul }
103395d67482SBill Paul 
103495d67482SBill Paul static int
10353f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
103695d67482SBill Paul {
10373ee5d7daSPyun YongHyeon 	int error, i;
103895d67482SBill Paul 
1039e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
104003e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1041e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1042943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
10433ee5d7daSPyun YongHyeon 			return (error);
104403e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
10451888f324SPyun YongHyeon 	}
104695d67482SBill Paul 
1047f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1048d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1049f41ac2beSBill Paul 
1050e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1051e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
105295d67482SBill Paul 
105395d67482SBill Paul 	return (0);
105495d67482SBill Paul }
105595d67482SBill Paul 
105695d67482SBill Paul static void
10573f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
105895d67482SBill Paul {
105995d67482SBill Paul 	int i;
106095d67482SBill Paul 
106195d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
106295d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
10630ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1064e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1065e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
10660ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1067f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1068e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1069e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
107095d67482SBill Paul 		}
1071f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
107295d67482SBill Paul 		    sizeof(struct bge_rx_bd));
107395d67482SBill Paul 	}
107495d67482SBill Paul }
107595d67482SBill Paul 
107695d67482SBill Paul static int
10773f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
107895d67482SBill Paul {
107995d67482SBill Paul 	struct bge_rcb *rcb;
10803ee5d7daSPyun YongHyeon 	int error, i;
108195d67482SBill Paul 
1082e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
108303e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
108495d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1085943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
10863ee5d7daSPyun YongHyeon 			return (error);
108703e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
10881888f324SPyun YongHyeon 	}
108995d67482SBill Paul 
1090f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1091d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1092f41ac2beSBill Paul 
1093e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
109495d67482SBill Paul 
10958a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1096f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
10978a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
10988a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
109967111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
110095d67482SBill Paul 
1101e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
110295d67482SBill Paul 
110395d67482SBill Paul 	return (0);
110495d67482SBill Paul }
110595d67482SBill Paul 
110695d67482SBill Paul static void
11073f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
110895d67482SBill Paul {
110995d67482SBill Paul 	int i;
111095d67482SBill Paul 
111195d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
111295d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1113e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1114e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1115e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1116f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1117f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1118e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1119e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
112095d67482SBill Paul 		}
1121f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
11221be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
112395d67482SBill Paul 	}
112495d67482SBill Paul }
112595d67482SBill Paul 
112695d67482SBill Paul static void
11273f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
112895d67482SBill Paul {
112995d67482SBill Paul 	int i;
113095d67482SBill Paul 
1131f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
113295d67482SBill Paul 		return;
113395d67482SBill Paul 
113495d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
113595d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
11360ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1137e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1138e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
11390ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1140f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1141e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1142e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
114395d67482SBill Paul 		}
1144f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
114595d67482SBill Paul 		    sizeof(struct bge_tx_bd));
114695d67482SBill Paul 	}
114795d67482SBill Paul }
114895d67482SBill Paul 
114995d67482SBill Paul static int
11503f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
115195d67482SBill Paul {
115295d67482SBill Paul 	sc->bge_txcnt = 0;
115395d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
11543927098fSPaul Saab 
1155e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1156e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
11575c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1158e6bf277eSPyun YongHyeon 
115914bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
116014bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
116138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
116214bbd30fSGleb Smirnoff 
11633927098fSPaul Saab 	/* 5700 b2 errata */
1164e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
116538cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
11663927098fSPaul Saab 
116714bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
116838cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
11693927098fSPaul Saab 	/* 5700 b2 errata */
1170e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
117138cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
117295d67482SBill Paul 
117395d67482SBill Paul 	return (0);
117495d67482SBill Paul }
117595d67482SBill Paul 
117695d67482SBill Paul static void
11773e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
11783e9b1bcaSJung-uk Kim {
11793e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
11803e9b1bcaSJung-uk Kim 
11813e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
11823e9b1bcaSJung-uk Kim 
11833e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
11843e9b1bcaSJung-uk Kim 
118545ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
11863e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
118745ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
11883e9b1bcaSJung-uk Kim 	else
118945ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
11903e9b1bcaSJung-uk Kim }
11913e9b1bcaSJung-uk Kim 
11923e9b1bcaSJung-uk Kim static void
11933f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
119495d67482SBill Paul {
119595d67482SBill Paul 	struct ifnet *ifp;
119695d67482SBill Paul 	struct ifmultiaddr *ifma;
11973f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
119895d67482SBill Paul 	int h, i;
119995d67482SBill Paul 
12000f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
12010f9bd73bSSam Leffler 
1202fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
120395d67482SBill Paul 
120495d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
120595d67482SBill Paul 		for (i = 0; i < 4; i++)
12060c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
120795d67482SBill Paul 		return;
120895d67482SBill Paul 	}
120995d67482SBill Paul 
121095d67482SBill Paul 	/* First, zot all the existing filters. */
121195d67482SBill Paul 	for (i = 0; i < 4; i++)
121295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
121395d67482SBill Paul 
121495d67482SBill Paul 	/* Now program new ones. */
1215eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
121695d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
121795d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
121895d67482SBill Paul 			continue;
12190e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
12200c8aa4eaSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
12210c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
122295d67482SBill Paul 	}
1223eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
122495d67482SBill Paul 
122595d67482SBill Paul 	for (i = 0; i < 4; i++)
122695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
122795d67482SBill Paul }
122895d67482SBill Paul 
12298cb1383cSDoug Ambrisko static void
1230cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1231cb2eacc7SYaroslav Tykhiy {
1232cb2eacc7SYaroslav Tykhiy 	struct ifnet *ifp;
1233cb2eacc7SYaroslav Tykhiy 
1234cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1235cb2eacc7SYaroslav Tykhiy 
1236cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1237cb2eacc7SYaroslav Tykhiy 
1238cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1239cb2eacc7SYaroslav Tykhiy 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1240cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1241cb2eacc7SYaroslav Tykhiy 	else
1242cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1243cb2eacc7SYaroslav Tykhiy }
1244cb2eacc7SYaroslav Tykhiy 
1245cb2eacc7SYaroslav Tykhiy static void
1246797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
12478cb1383cSDoug Ambrisko {
1248797ab05eSPyun YongHyeon 
12498cb1383cSDoug Ambrisko 	/*
12508cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
12518cb1383cSDoug Ambrisko 	 */
12528cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
12538cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
12548cb1383cSDoug Ambrisko 
12558cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
12568cb1383cSDoug Ambrisko 		switch (type) {
12578cb1383cSDoug Ambrisko 		case BGE_RESET_START:
12588cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
12598cb1383cSDoug Ambrisko 			break;
12608cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
12618cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
12628cb1383cSDoug Ambrisko 			break;
12638cb1383cSDoug Ambrisko 		}
12648cb1383cSDoug Ambrisko 	}
12658cb1383cSDoug Ambrisko }
12668cb1383cSDoug Ambrisko 
12678cb1383cSDoug Ambrisko static void
1268797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
12698cb1383cSDoug Ambrisko {
1270797ab05eSPyun YongHyeon 
12718cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
12728cb1383cSDoug Ambrisko 		switch (type) {
12738cb1383cSDoug Ambrisko 		case BGE_RESET_START:
12748cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000001);
12758cb1383cSDoug Ambrisko 			/* START DONE */
12768cb1383cSDoug Ambrisko 			break;
12778cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
12788cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000002);
12798cb1383cSDoug Ambrisko 			break;
12808cb1383cSDoug Ambrisko 		}
12818cb1383cSDoug Ambrisko 	}
12828cb1383cSDoug Ambrisko }
12838cb1383cSDoug Ambrisko 
12848cb1383cSDoug Ambrisko static void
1285797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
12868cb1383cSDoug Ambrisko {
1287797ab05eSPyun YongHyeon 
12888cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
12898cb1383cSDoug Ambrisko 		switch (type) {
12908cb1383cSDoug Ambrisko 		case BGE_RESET_START:
12918cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
12928cb1383cSDoug Ambrisko 			break;
12938cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
12948cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
12958cb1383cSDoug Ambrisko 			break;
12968cb1383cSDoug Ambrisko 		}
12978cb1383cSDoug Ambrisko 	}
12988cb1383cSDoug Ambrisko }
12998cb1383cSDoug Ambrisko 
1300797ab05eSPyun YongHyeon static void
1301797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
13028cb1383cSDoug Ambrisko {
13038cb1383cSDoug Ambrisko 	int i;
13048cb1383cSDoug Ambrisko 
13058cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
13068cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW, BGE_FW_PAUSE);
13078cb1383cSDoug Ambrisko 		CSR_WRITE_4(sc, BGE_CPU_EVENT,
130839153c5aSJung-uk Kim 		    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
13098cb1383cSDoug Ambrisko 
13108cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
13118cb1383cSDoug Ambrisko 			if (!(CSR_READ_4(sc, BGE_CPU_EVENT) & (1 << 14)))
13128cb1383cSDoug Ambrisko 				break;
13138cb1383cSDoug Ambrisko 			DELAY(10);
13148cb1383cSDoug Ambrisko 		}
13158cb1383cSDoug Ambrisko 	}
13168cb1383cSDoug Ambrisko }
13178cb1383cSDoug Ambrisko 
131895d67482SBill Paul /*
1319c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
132095d67482SBill Paul  */
132195d67482SBill Paul static int
13223f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
132395d67482SBill Paul {
13243f74909aSGleb Smirnoff 	uint32_t dma_rw_ctl;
1325fbc374afSPyun YongHyeon 	uint16_t val;
132695d67482SBill Paul 	int i;
132795d67482SBill Paul 
13288cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
1329e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
133095d67482SBill Paul 
133195d67482SBill Paul 	/* Clear the MAC control register */
133295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
133395d67482SBill Paul 
133495d67482SBill Paul 	/*
133595d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
133695d67482SBill Paul 	 * internal memory.
133795d67482SBill Paul 	 */
133895d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
13393f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
134095d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
134195d67482SBill Paul 
134295d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
13433f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
134495d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
134595d67482SBill Paul 
1346fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1347fbc374afSPyun YongHyeon 		/*
1348d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1349fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1350fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1351fbc374afSPyun YongHyeon 		 */
1352fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1353fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1354fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1355fbc374afSPyun YongHyeon 	}
1356fbc374afSPyun YongHyeon 
1357186f842bSJung-uk Kim 	/*
1358186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1359186f842bSJung-uk Kim 	 */
1360186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1361186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1362652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
1363186f842bSJung-uk Kim 		/* Read watermark not used, 128 bytes for write. */
1364186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1365652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
13664c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1367186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1368186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1369186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1370186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1371186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1372186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1373cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1374cbb2b2feSPyun YongHyeon 			/*
1375cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1376cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1377cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1378cbb2b2feSPyun YongHyeon 			 * register.
1379cbb2b2feSPyun YongHyeon 			 */
1380cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1381cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1382186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1383186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1384186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1385186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1386186f842bSJung-uk Kim 		} else {
1387186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1388186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1389186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
13900c8aa4eaSJung-uk Kim 			    0x0F;
1391186f842bSJung-uk Kim 		}
1392e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1393e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
13943f74909aSGleb Smirnoff 			uint32_t tmp;
13955cba12d3SPaul Saab 
1396186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
13970c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1398186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1399186f842bSJung-uk Kim 				dma_rw_ctl |=
1400186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
14015cba12d3SPaul Saab 
1402186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1403186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1404186f842bSJung-uk Kim 		}
1405186f842bSJung-uk Kim 	} else {
1406186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1407186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1408186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1409186f842bSJung-uk Kim 
1410186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1411186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1412186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1413186f842bSJung-uk Kim 	}
1414186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1415186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1416186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1417186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1418e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1419186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
14205cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
14215cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
142295d67482SBill Paul 
142395d67482SBill Paul 	/*
142495d67482SBill Paul 	 * Set up general mode register.
142595d67482SBill Paul 	 */
1426e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
142795d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1428ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
142995d67482SBill Paul 
143095d67482SBill Paul 	/*
143190447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
143290447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
143390447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
143490447aadSMarius Strobl 	 * certain bridges.
143590447aadSMarius Strobl 	 */
143690447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
143790447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
143890447aadSMarius Strobl 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_FORCE_PCI32);
143990447aadSMarius Strobl 
144090447aadSMarius Strobl 	/*
14418cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
14428cb1383cSDoug Ambrisko 	 */
14438cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
14448cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
14458cb1383cSDoug Ambrisko 
14468cb1383cSDoug Ambrisko 	/*
1447ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1448c9ffd9f0SMarius Strobl 	 * properly by these devices.  Also ensure that INTx isn't disabled,
1449c9ffd9f0SMarius Strobl 	 * as these chips need it even when using MSI.
145095d67482SBill Paul 	 */
1451c9ffd9f0SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD,
1452c9ffd9f0SMarius Strobl 	    PCIM_CMD_INTxDIS | PCIM_CMD_MWIEN, 4);
145395d67482SBill Paul 
145495d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
14550c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
145695d67482SBill Paul 
145738cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
145838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
145938cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
146038cc658fSJohn Baldwin 
146138cc658fSJohn Baldwin 		/* Put PHY into ready state */
146238cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
146338cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
146438cc658fSJohn Baldwin 		DELAY(40);
146538cc658fSJohn Baldwin 	}
146638cc658fSJohn Baldwin 
146795d67482SBill Paul 	return (0);
146895d67482SBill Paul }
146995d67482SBill Paul 
147095d67482SBill Paul static int
14713f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
147295d67482SBill Paul {
147395d67482SBill Paul 	struct bge_rcb *rcb;
1474e907febfSPyun YongHyeon 	bus_size_t vrcb;
1475e907febfSPyun YongHyeon 	bge_hostaddr taddr;
14766f8718a3SScott Long 	uint32_t val;
14778a315a6dSPyun YongHyeon 	int i, limit;
147895d67482SBill Paul 
147995d67482SBill Paul 	/*
148095d67482SBill Paul 	 * Initialize the memory window pointer register so that
148195d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
148295d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
148395d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
148495d67482SBill Paul 	 */
148595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
148695d67482SBill Paul 
1487822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1488822f63fcSBill Paul 
14897ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
149095d67482SBill Paul 		/* Configure mbuf memory pool */
14910dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1492822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1493822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1494822f63fcSBill Paul 		else
149595d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
149695d67482SBill Paul 
149795d67482SBill Paul 		/* Configure DMA resource pool */
14980434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
14990434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
150095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
15010434d1b8SBill Paul 	}
150295d67482SBill Paul 
150395d67482SBill Paul 	/* Configure mbuf pool watermarks */
150438cc658fSJohn Baldwin 	if (!BGE_IS_5705_PLUS(sc)) {
1505fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1506fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1507fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
150838cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
150938cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
151038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
151138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
151238cc658fSJohn Baldwin 	} else {
151338cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
151438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
151538cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
151638cc658fSJohn Baldwin 	}
151795d67482SBill Paul 
151895d67482SBill Paul 	/* Configure DMA resource watermarks */
151995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
152095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
152195d67482SBill Paul 
152295d67482SBill Paul 	/* Enable buffer manager */
15237ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
152495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
152595d67482SBill Paul 		    BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN);
152695d67482SBill Paul 
152795d67482SBill Paul 		/* Poll for buffer manager start indication */
152895d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
1529d5d23857SJung-uk Kim 			DELAY(10);
15300c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
153195d67482SBill Paul 				break;
153295d67482SBill Paul 		}
153395d67482SBill Paul 
153495d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1535fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1536fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
153795d67482SBill Paul 			return (ENXIO);
153895d67482SBill Paul 		}
15390434d1b8SBill Paul 	}
154095d67482SBill Paul 
154195d67482SBill Paul 	/* Enable flow-through queues */
15420c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
154395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
154495d67482SBill Paul 
154595d67482SBill Paul 	/* Wait until queue initialization is complete */
154695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1547d5d23857SJung-uk Kim 		DELAY(10);
154895d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
154995d67482SBill Paul 			break;
155095d67482SBill Paul 	}
155195d67482SBill Paul 
155295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1553fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
155495d67482SBill Paul 		return (ENXIO);
155595d67482SBill Paul 	}
155695d67482SBill Paul 
15578a315a6dSPyun YongHyeon 	/*
15588a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
15598a315a6dSPyun YongHyeon 	 *
15608a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
15618a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
15628a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
15638a315a6dSPyun YongHyeon 	 *
15648a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
15658a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
15668a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
15678a315a6dSPyun YongHyeon 	 *   to the controller.
15688a315a6dSPyun YongHyeon 	 *
15698a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
15708a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
15718a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
15728a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
15738a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
15748a315a6dSPyun YongHyeon 	 *   be disabled.
15758a315a6dSPyun YongHyeon 	 *
15768a315a6dSPyun YongHyeon 	 * Receive Return Ring
15778a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
15788a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
15798a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
15808a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
15818a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
15828a315a6dSPyun YongHyeon 	 *
15838a315a6dSPyun YongHyeon 	 * Send Ring
15848a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
15858a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
15868a315a6dSPyun YongHyeon 	 */
15878a315a6dSPyun YongHyeon 
15888a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
1589f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1590f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1591f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1592f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1593f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1594f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1595f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
15968a315a6dSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc)) {
15978a315a6dSPyun YongHyeon 		/*
15988a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
15998a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
16008a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
16018a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
16028a315a6dSPyun YongHyeon 		 */
16030434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
16048a315a6dSPyun YongHyeon 	} else {
16058a315a6dSPyun YongHyeon 		/*
16068a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
16078a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
16088a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
16098a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
16108a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
16118a315a6dSPyun YongHyeon 		 */
16120434d1b8SBill Paul 		rcb->bge_maxlen_flags =
16130434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
16148a315a6dSPyun YongHyeon 	}
161595d67482SBill Paul 	rcb->bge_nicaddr = BGE_STD_RX_RINGS;
16168a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
16170c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
16180c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
161967111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
162067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
162195d67482SBill Paul 
16228a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
16238a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
16248a315a6dSPyun YongHyeon 
162595d67482SBill Paul 	/*
16268a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
16278a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
16288a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
162995d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
163095d67482SBill Paul 	 * high enough to require it).
163195d67482SBill Paul 	 */
16324c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1633f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
16348a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
1635f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1636f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1637f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1638f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1639f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1640f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1641f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
16421be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
16431be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
164495d67482SBill Paul 		rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
164567111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
164667111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
164767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
164867111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
16498a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
16500434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
16510434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
165267111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
16538a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
16548a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
16558a315a6dSPyun YongHyeon 	}
165695d67482SBill Paul 
16578a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
16588a315a6dSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700) {
1659f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
166067111612SJohn Polstra 		rcb->bge_maxlen_flags =
166167111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
16620434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
16630434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
16648a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
16658a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
16660434d1b8SBill Paul 	}
166795d67482SBill Paul 
166895d67482SBill Paul 	/*
16698a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
16708a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
16718a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
16728a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
16738a315a6dSPyun YongHyeon 	 *
167495d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
167595d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
167695d67482SBill Paul 	 * each ring.
16779ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
16789ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
16799ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
16809ba784dbSScott Long 	 * are reports that it might not need to be so strict.
168138cc658fSJohn Baldwin 	 *
168238cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
168338cc658fSJohn Baldwin 	 * well.
168495d67482SBill Paul 	 */
16855345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
16866f8718a3SScott Long 		val = 8;
16876f8718a3SScott Long 	else
16886f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
16896f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
16902a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
16912a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
16922a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
169395d67482SBill Paul 
169495d67482SBill Paul 	/*
16958a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
16968a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
16978a315a6dSPyun YongHyeon 	 * located in NIC memory.
169895d67482SBill Paul 	 */
16998a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
17008a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
17018a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
17028a315a6dSPyun YongHyeon 	else
17038a315a6dSPyun YongHyeon 		limit = 1;
1704e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
17058a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
1706e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1707e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1708e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1709e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
171095d67482SBill Paul 	}
171195d67482SBill Paul 
17128a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
1713e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1714e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1715e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1716e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1717e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1718e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
1719e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1720e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
172195d67482SBill Paul 
17228a315a6dSPyun YongHyeon 	/*
17238a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
17248a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
17258a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
17268a315a6dSPyun YongHyeon 	 */
17278a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
17288a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
17298a315a6dSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755)
17308a315a6dSPyun YongHyeon 		limit = 4;
17318a315a6dSPyun YongHyeon 	else
17328a315a6dSPyun YongHyeon 		limit = 1;
17338a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
1734e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
17358a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
1736e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1737e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1738e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
17398a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
1740e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
174138cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
17423f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1743e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
174495d67482SBill Paul 	}
174595d67482SBill Paul 
174695d67482SBill Paul 	/*
17478a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
17488a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
17498a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
175095d67482SBill Paul 	 */
1751e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1752e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1753e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1754e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
17558a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1756e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1757e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
175895d67482SBill Paul 
175995d67482SBill Paul 	/* Set random backoff seed for TX */
176095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
17614a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
17624a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
17634a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
176495d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
176595d67482SBill Paul 
176695d67482SBill Paul 	/* Set inter-packet gap */
176795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
176895d67482SBill Paul 
176995d67482SBill Paul 	/*
177095d67482SBill Paul 	 * Specify which ring to use for packets that don't match
177195d67482SBill Paul 	 * any RX rules.
177295d67482SBill Paul 	 */
177395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
177495d67482SBill Paul 
177595d67482SBill Paul 	/*
177695d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
177795d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
177895d67482SBill Paul 	 */
177995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
178095d67482SBill Paul 
178195d67482SBill Paul 	/* Inialize RX list placement stats mask. */
17820c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
178395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
178495d67482SBill Paul 
178595d67482SBill Paul 	/* Disable host coalescing until we get it set up */
178695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
178795d67482SBill Paul 
178895d67482SBill Paul 	/* Poll to make sure it's shut down. */
178995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1790d5d23857SJung-uk Kim 		DELAY(10);
179195d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
179295d67482SBill Paul 			break;
179395d67482SBill Paul 	}
179495d67482SBill Paul 
179595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1796fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1797fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
179895d67482SBill Paul 		return (ENXIO);
179995d67482SBill Paul 	}
180095d67482SBill Paul 
180195d67482SBill Paul 	/* Set up host coalescing defaults */
180295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
180395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
180495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
180595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
18067ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
180795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
180895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
18090434d1b8SBill Paul 	}
1810b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
1811b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
181295d67482SBill Paul 
181395d67482SBill Paul 	/* Set up address of statistics block */
18147ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
1815f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1816f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
181795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1818f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
18190434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
182095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
18210434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
18220434d1b8SBill Paul 	}
18230434d1b8SBill Paul 
18240434d1b8SBill Paul 	/* Set up address of status block */
1825f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1826f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
182795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1828f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
182995d67482SBill Paul 
183030f57f61SPyun YongHyeon 	/* Set up status block size. */
183130f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
1832864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
183330f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
1834864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
1835864104feSPyun YongHyeon 	} else {
183630f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
1837864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
1838864104feSPyun YongHyeon 	}
1839864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
1840864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
1841864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
184230f57f61SPyun YongHyeon 
184395d67482SBill Paul 	/* Turn on host coalescing state machine */
184430f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
184595d67482SBill Paul 
184695d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
184795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
184895d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
184995d67482SBill Paul 
185095d67482SBill Paul 	/* Turn on RX list placement state machine */
185195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
185295d67482SBill Paul 
185395d67482SBill Paul 	/* Turn on RX list selector state machine. */
18547ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
185595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
185695d67482SBill Paul 
1857ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
1858ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
1859ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
1860ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
1861ea3b4127SPyun YongHyeon 
1862ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
1863ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
1864ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
1865ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
1866ea3b4127SPyun YongHyeon 	else
1867ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
1868ea3b4127SPyun YongHyeon 
186995d67482SBill Paul 	/* Turn on DMA, clear stats */
1870ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
187195d67482SBill Paul 
187295d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
187395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
187495d67482SBill Paul 
187595d67482SBill Paul #ifdef notdef
187695d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
187795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
187895d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
187995d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
188095d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
188195d67482SBill Paul #endif
188295d67482SBill Paul 
188395d67482SBill Paul 	/* Turn on DMA completion state machine */
18847ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
188595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
188695d67482SBill Paul 
18876f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
18886f8718a3SScott Long 
18896f8718a3SScott Long 	/* Enable host coalescing bug fix. */
1890a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
18913889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
18926f8718a3SScott Long 
18937aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
18947aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
18957aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
18967aa4b937SPyun YongHyeon 
189795d67482SBill Paul 	/* Turn on write DMA state machine */
18986f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
18994f09c4c7SMarius Strobl 	DELAY(40);
190095d67482SBill Paul 
190195d67482SBill Paul 	/* Turn on read DMA state machine */
19024f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
1903a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
1904a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
1905a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
1906a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
1907a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
1908a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
19094f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
19104f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
191155a24a05SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO) {
1912ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
191355a24a05SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
191455a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
191555a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
191655a24a05SPyun YongHyeon 	}
1917d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
1918d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
1919d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
1920d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780) {
1921d255f2a9SPyun YongHyeon 		/*
1922d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
1923d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
1924d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
1925d255f2a9SPyun YongHyeon 		 */
1926d255f2a9SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_RSRVCTRL,
1927d255f2a9SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_RSRVCTRL) |
1928d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
1929d255f2a9SPyun YongHyeon 	}
19304f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
19314f09c4c7SMarius Strobl 	DELAY(40);
193295d67482SBill Paul 
193395d67482SBill Paul 	/* Turn on RX data completion state machine */
193495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
193595d67482SBill Paul 
193695d67482SBill Paul 	/* Turn on RX BD initiator state machine */
193795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
193895d67482SBill Paul 
193995d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
194095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
194195d67482SBill Paul 
194295d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
19437ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
194495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
194595d67482SBill Paul 
194695d67482SBill Paul 	/* Turn on send BD completion state machine */
194795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
194895d67482SBill Paul 
194995d67482SBill Paul 	/* Turn on send data completion state machine */
1950a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
1951a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
1952a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
1953a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
195495d67482SBill Paul 
195595d67482SBill Paul 	/* Turn on send data initiator state machine */
1956ca3f1187SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO)
1957ca3f1187SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE | 0x08);
1958ca3f1187SPyun YongHyeon 	else
195995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
196095d67482SBill Paul 
196195d67482SBill Paul 	/* Turn on send BD initiator state machine */
196295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
196395d67482SBill Paul 
196495d67482SBill Paul 	/* Turn on send BD selector state machine */
196595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
196695d67482SBill Paul 
19670c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
196895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
196995d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
197095d67482SBill Paul 
197195d67482SBill Paul 	/* ack/clear link change events */
197295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
19730434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
19740434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1975f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
197695d67482SBill Paul 
1977*6ede2cfaSPyun YongHyeon 	/*
1978*6ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
1979*6ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
1980*6ede2cfaSPyun YongHyeon 	 */
1981652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
198295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1983a1d52896SBill Paul 	} else {
19841f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
19854c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
1986a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1987a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1988a1d52896SBill Paul 	}
198995d67482SBill Paul 
19901f313773SOleg Bulyzhin 	/*
19911f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
19921f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
19931f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
19941f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
19951f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
19961f313773SOleg Bulyzhin 	 */
19971f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
19981f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
19991f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
20001f313773SOleg Bulyzhin 
200195d67482SBill Paul 	/* Enable link state change attentions. */
200295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
200395d67482SBill Paul 
200495d67482SBill Paul 	return (0);
200595d67482SBill Paul }
200695d67482SBill Paul 
20074c0da0ffSGleb Smirnoff const struct bge_revision *
20084c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
20094c0da0ffSGleb Smirnoff {
20104c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
20114c0da0ffSGleb Smirnoff 
20124c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
20134c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
20144c0da0ffSGleb Smirnoff 			return (br);
20154c0da0ffSGleb Smirnoff 	}
20164c0da0ffSGleb Smirnoff 
20174c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
20184c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
20194c0da0ffSGleb Smirnoff 			return (br);
20204c0da0ffSGleb Smirnoff 	}
20214c0da0ffSGleb Smirnoff 
20224c0da0ffSGleb Smirnoff 	return (NULL);
20234c0da0ffSGleb Smirnoff }
20244c0da0ffSGleb Smirnoff 
20254c0da0ffSGleb Smirnoff const struct bge_vendor *
20264c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
20274c0da0ffSGleb Smirnoff {
20284c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
20294c0da0ffSGleb Smirnoff 
20304c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
20314c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
20324c0da0ffSGleb Smirnoff 			return (v);
20334c0da0ffSGleb Smirnoff 
20344c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
20354c0da0ffSGleb Smirnoff 	return (NULL);
20364c0da0ffSGleb Smirnoff }
20374c0da0ffSGleb Smirnoff 
203895d67482SBill Paul /*
203995d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
20404c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
20414c0da0ffSGleb Smirnoff  *
20424c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
20437c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
20447c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
20457c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
20467c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
204795d67482SBill Paul  */
204895d67482SBill Paul static int
20493f74909aSGleb Smirnoff bge_probe(device_t dev)
205095d67482SBill Paul {
2051852c67f9SMarius Strobl 	const struct bge_type *t = bge_devs;
20524c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
20537c929cf9SJung-uk Kim 	uint16_t vid, did;
205495d67482SBill Paul 
205595d67482SBill Paul 	sc->bge_dev = dev;
20567c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
20577c929cf9SJung-uk Kim 	did = pci_get_device(dev);
20584c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
20597c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
20607c929cf9SJung-uk Kim 			char model[64], buf[96];
20614c0da0ffSGleb Smirnoff 			const struct bge_revision *br;
20624c0da0ffSGleb Smirnoff 			const struct bge_vendor *v;
20634c0da0ffSGleb Smirnoff 			uint32_t id;
20644c0da0ffSGleb Smirnoff 
2065a5779553SStanislav Sedov 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2066a5779553SStanislav Sedov 			    BGE_PCIMISCCTL_ASICREV_SHIFT;
2067a5779553SStanislav Sedov 			if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG)
2068a5779553SStanislav Sedov 				id = pci_read_config(dev,
2069a5779553SStanislav Sedov 				    BGE_PCI_PRODID_ASICREV, 4);
20704c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
20717c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
20724e35d186SJung-uk Kim 			{
20734e35d186SJung-uk Kim #if __FreeBSD_version > 700024
20744e35d186SJung-uk Kim 				const char *pname;
20754e35d186SJung-uk Kim 
2076852c67f9SMarius Strobl 				if (bge_has_eaddr(sc) &&
2077852c67f9SMarius Strobl 				    pci_get_vpd_ident(dev, &pname) == 0)
20784e35d186SJung-uk Kim 					snprintf(model, 64, "%s", pname);
20794e35d186SJung-uk Kim 				else
20804e35d186SJung-uk Kim #endif
20817c929cf9SJung-uk Kim 					snprintf(model, 64, "%s %s",
20827c929cf9SJung-uk Kim 					    v->v_name,
20837c929cf9SJung-uk Kim 					    br != NULL ? br->br_name :
20847c929cf9SJung-uk Kim 					    "NetXtreme Ethernet Controller");
20854e35d186SJung-uk Kim 			}
2086a5779553SStanislav Sedov 			snprintf(buf, 96, "%s, %sASIC rev. %#08x", model,
2087a5779553SStanislav Sedov 			    br != NULL ? "" : "unknown ", id);
20884c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
208995d67482SBill Paul 			return (0);
209095d67482SBill Paul 		}
209195d67482SBill Paul 		t++;
209295d67482SBill Paul 	}
209395d67482SBill Paul 
209495d67482SBill Paul 	return (ENXIO);
209595d67482SBill Paul }
209695d67482SBill Paul 
2097f41ac2beSBill Paul static void
20983f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2099f41ac2beSBill Paul {
2100f41ac2beSBill Paul 	int i;
2101f41ac2beSBill Paul 
21023f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2103f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2104f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
21050ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2106f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2107f41ac2beSBill Paul 	}
2108943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2109943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2110943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2111f41ac2beSBill Paul 
21123f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2113f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2114f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2115f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2116f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2117f41ac2beSBill Paul 	}
2118943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2119943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2120943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2121f41ac2beSBill Paul 
21223f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2123f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2124f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
21250ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2126f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2127f41ac2beSBill Paul 	}
2128f41ac2beSBill Paul 
21290ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
21300ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
21310ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
21320ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2133f41ac2beSBill Paul 
2134f41ac2beSBill Paul 
21353f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2136e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
2137e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2138e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2139e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
2140f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2141f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2142f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2143f41ac2beSBill Paul 
2144f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2145f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2146f41ac2beSBill Paul 
21473f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2148e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
2149e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2150e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2151e65bed95SPyun YongHyeon 
2152e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
2153e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
2154f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2155f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2156f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2157f41ac2beSBill Paul 
2158f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2159f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2160f41ac2beSBill Paul 
21613f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2162e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
2163e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2164e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2165e65bed95SPyun YongHyeon 
2166e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
2167e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
2168f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2169f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2170f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2171f41ac2beSBill Paul 
2172f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2173f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2174f41ac2beSBill Paul 
21753f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2176e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
2177e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2178e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2179e65bed95SPyun YongHyeon 
2180e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2181f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2182f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2183f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2184f41ac2beSBill Paul 
2185f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2186f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2187f41ac2beSBill Paul 
21883f74909aSGleb Smirnoff 	/* Destroy status block. */
2189e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
2190e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2191e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2192e65bed95SPyun YongHyeon 
2193e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2194f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2195f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2196f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2197f41ac2beSBill Paul 
2198f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2199f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2200f41ac2beSBill Paul 
22013f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2202e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
2203e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2204e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2205e65bed95SPyun YongHyeon 
2206e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2207f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2208f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2209f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2210f41ac2beSBill Paul 
2211f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2212f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2213f41ac2beSBill Paul 
22145b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
22155b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
22165b610048SPyun YongHyeon 
22173f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2218f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2219f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2220f41ac2beSBill Paul }
2221f41ac2beSBill Paul 
2222f41ac2beSBill Paul static int
22235b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
22245b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
22255b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2226f41ac2beSBill Paul {
22273f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
2228f681b29aSPyun YongHyeon 	bus_addr_t lowaddr;
22295b610048SPyun YongHyeon 	bus_size_t ring_end;
22305b610048SPyun YongHyeon 	int error;
2231f41ac2beSBill Paul 
22325b610048SPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
22335b610048SPyun YongHyeon again:
22345b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
22355b610048SPyun YongHyeon 	    alignment, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
22365b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
22375b610048SPyun YongHyeon 	if (error != 0) {
22385b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
22395b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
22405b610048SPyun YongHyeon 		return (ENOMEM);
22415b610048SPyun YongHyeon 	}
22425b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
22435b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
22445b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
22455b610048SPyun YongHyeon 	if (error != 0) {
22465b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
22475b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
22485b610048SPyun YongHyeon 		return (ENOMEM);
22495b610048SPyun YongHyeon 	}
22505b610048SPyun YongHyeon 	/* Load the address of the ring. */
22515b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
22525b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
22535b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
22545b610048SPyun YongHyeon 	if (error != 0) {
22555b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
22565b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
22575b610048SPyun YongHyeon 		return (ENOMEM);
22585b610048SPyun YongHyeon 	}
22595b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
22605b610048SPyun YongHyeon 	ring_end = *paddr + maxsize;
22615b610048SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0 &&
22625b610048SPyun YongHyeon 	    BGE_ADDR_HI(*paddr) != BGE_ADDR_HI(ring_end)) {
22635b610048SPyun YongHyeon 		/*
22645b610048SPyun YongHyeon 		 * 4GB boundary crossed.  Limit maximum allowable DMA
22655b610048SPyun YongHyeon 		 * address space to 32bit and try again.
22665b610048SPyun YongHyeon 		 */
22675b610048SPyun YongHyeon 		bus_dmamap_unload(*tag, *map);
22685b610048SPyun YongHyeon 		bus_dmamem_free(*tag, *ring, *map);
22695b610048SPyun YongHyeon 		bus_dma_tag_destroy(*tag);
22705b610048SPyun YongHyeon 		if (bootverbose)
22715b610048SPyun YongHyeon 			device_printf(sc->bge_dev, "4GB boundary crossed, "
22725b610048SPyun YongHyeon 			    "limit DMA address space to 32bit for %s\n", msg);
22735b610048SPyun YongHyeon 		*ring = NULL;
22745b610048SPyun YongHyeon 		*tag = NULL;
22755b610048SPyun YongHyeon 		*map = NULL;
22765b610048SPyun YongHyeon 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
22775b610048SPyun YongHyeon 		goto again;
22785b610048SPyun YongHyeon 	}
22795b610048SPyun YongHyeon 	return (0);
22805b610048SPyun YongHyeon }
22815b610048SPyun YongHyeon 
22825b610048SPyun YongHyeon static int
22835b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
22845b610048SPyun YongHyeon {
22855b610048SPyun YongHyeon 	bus_addr_t lowaddr;
22865b610048SPyun YongHyeon 	bus_size_t boundary, sbsz, txsegsz, txmaxsegsz;
22875b610048SPyun YongHyeon 	int i, error;
2288f41ac2beSBill Paul 
2289f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2290f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2291f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2292f41ac2beSBill Paul 	/*
2293f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2294f41ac2beSBill Paul 	 */
22954eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2296f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
22974eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
22984eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2299e65bed95SPyun YongHyeon 	if (error != 0) {
2300fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2301fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2302e65bed95SPyun YongHyeon 		return (ENOMEM);
2303e65bed95SPyun YongHyeon 	}
2304e65bed95SPyun YongHyeon 
23055b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
23065b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
23075b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
23085b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
23095b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
23105b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
23115b610048SPyun YongHyeon 	if (error)
23125b610048SPyun YongHyeon 		return (error);
23135b610048SPyun YongHyeon 
23145b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
23155b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
23165b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
23175b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
23185b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
23195b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
23205b610048SPyun YongHyeon 	if (error)
23215b610048SPyun YongHyeon 		return (error);
23225b610048SPyun YongHyeon 
23235b610048SPyun YongHyeon 	/* Create tag for TX ring. */
23245b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
23255b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
23265b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
23275b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
23285b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
23295b610048SPyun YongHyeon 	if (error)
23305b610048SPyun YongHyeon 		return (error);
23315b610048SPyun YongHyeon 
2332f41ac2beSBill Paul 	/*
23335b610048SPyun YongHyeon 	 * Create tag for status block.
23345b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
23355b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
23365b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
23375b610048SPyun YongHyeon 	 * of configured number of ring.
2338f41ac2beSBill Paul 	 */
23395b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
23405b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
23415b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
23425b610048SPyun YongHyeon 	else
23435b610048SPyun YongHyeon 		sbsz = 32;
23445b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
23455b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
23465b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
23475b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
23485b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
23495b610048SPyun YongHyeon 	if (error)
23505b610048SPyun YongHyeon 		return (error);
23515b610048SPyun YongHyeon 
235212c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
235312c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
235412c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
235512c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
235612c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
235712c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
235812c65daeSPyun YongHyeon 	if (error)
235912c65daeSPyun YongHyeon 		return (error);
236012c65daeSPyun YongHyeon 
23615b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
23625b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
23635b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
23645b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
23655b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
23665b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
23675b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
23685b610048SPyun YongHyeon 		if (error)
23695b610048SPyun YongHyeon 			return (error);
23705b610048SPyun YongHyeon 	}
23715b610048SPyun YongHyeon 
23725b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
23735b610048SPyun YongHyeon 	boundary = 0;
23745b610048SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0)
237538cc6151SPyun YongHyeon 		boundary = BGE_DMA_BNDRY;
23765b610048SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
23775b610048SPyun YongHyeon 	    1, boundary, lowaddr, BUS_SPACE_MAXADDR, NULL,
23785b610048SPyun YongHyeon 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
23795b610048SPyun YongHyeon 	    0, NULL, NULL, &sc->bge_cdata.bge_buffer_tag);
23805b610048SPyun YongHyeon 	if (error != 0) {
23815b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
23825b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
23835b610048SPyun YongHyeon 		return (ENOMEM);
23845b610048SPyun YongHyeon 	}
23855b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
2386ca3f1187SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO) {
2387ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
2388ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
2389ca3f1187SPyun YongHyeon 	} else {
2390ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
2391ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
2392ca3f1187SPyun YongHyeon 	}
23935b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
2394ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
2395ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
2396ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
2397f41ac2beSBill Paul 
2398f41ac2beSBill Paul 	if (error) {
23990ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
24000ac56796SPyun YongHyeon 		return (ENOMEM);
24010ac56796SPyun YongHyeon 	}
24020ac56796SPyun YongHyeon 
24035b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
24045b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
24050ac56796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1,
2406ca3f1187SPyun YongHyeon 	    MCLBYTES, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
24070ac56796SPyun YongHyeon 
24080ac56796SPyun YongHyeon 	if (error) {
24090ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
2410f41ac2beSBill Paul 		return (ENOMEM);
2411f41ac2beSBill Paul 	}
2412f41ac2beSBill Paul 
24133f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
2414943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2415943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
2416943787f3SPyun YongHyeon 	if (error) {
2417943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
2418943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
2419943787f3SPyun YongHyeon 		return (ENOMEM);
2420943787f3SPyun YongHyeon 	}
2421f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
24220ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2423f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
2424f41ac2beSBill Paul 		if (error) {
2425fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
2426fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
2427f41ac2beSBill Paul 			return (ENOMEM);
2428f41ac2beSBill Paul 		}
2429f41ac2beSBill Paul 	}
2430f41ac2beSBill Paul 
24313f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
2432f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
24330ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
2434f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
2435f41ac2beSBill Paul 		if (error) {
2436fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
24370ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
2438f41ac2beSBill Paul 			return (ENOMEM);
2439f41ac2beSBill Paul 		}
2440f41ac2beSBill Paul 	}
2441f41ac2beSBill Paul 
24425b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
24434c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
24445b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
24458a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
24461be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
24471be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
2448f41ac2beSBill Paul 		if (error) {
2449fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
24503f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
2451f41ac2beSBill Paul 			return (ENOMEM);
2452f41ac2beSBill Paul 		}
24533f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
2454943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2455943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
2456943787f3SPyun YongHyeon 		if (error) {
2457943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
24581b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
2459943787f3SPyun YongHyeon 			return (ENOMEM);
2460943787f3SPyun YongHyeon 		}
2461f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2462f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2463f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2464f41ac2beSBill Paul 			if (error) {
2465fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
24663f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
2467f41ac2beSBill Paul 				return (ENOMEM);
2468f41ac2beSBill Paul 			}
2469f41ac2beSBill Paul 		}
2470f41ac2beSBill Paul 	}
2471f41ac2beSBill Paul 
2472f41ac2beSBill Paul 	return (0);
2473f41ac2beSBill Paul }
2474f41ac2beSBill Paul 
2475bf6ef57aSJohn Polstra /*
2476bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
2477bf6ef57aSJohn Polstra  */
2478bf6ef57aSJohn Polstra static int
2479bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
2480bf6ef57aSJohn Polstra {
2481bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
248255aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
2483bf6ef57aSJohn Polstra 
248455aaf894SMarius Strobl 	d = pci_get_domain(dev);
2485bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
2486bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
2487bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
2488bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
248955aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
2490bf6ef57aSJohn Polstra 			return (1);
2491bf6ef57aSJohn Polstra 	return (0);
2492bf6ef57aSJohn Polstra }
2493bf6ef57aSJohn Polstra 
2494bf6ef57aSJohn Polstra /*
2495bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
2496bf6ef57aSJohn Polstra  */
2497bf6ef57aSJohn Polstra static int
2498bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
2499bf6ef57aSJohn Polstra {
2500bf6ef57aSJohn Polstra 	int can_use_msi = 0;
2501bf6ef57aSJohn Polstra 
2502bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
2503a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
2504bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
2505bf6ef57aSJohn Polstra 		/*
2506a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
2507a8376f70SMarius Strobl 		 * configured in single-port mode.
2508bf6ef57aSJohn Polstra 		 */
2509bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
2510bf6ef57aSJohn Polstra 			can_use_msi = 1;
2511bf6ef57aSJohn Polstra 		break;
2512bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
2513bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
2514bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
2515bf6ef57aSJohn Polstra 			can_use_msi = 1;
2516bf6ef57aSJohn Polstra 		break;
2517a8376f70SMarius Strobl 	default:
2518a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
2519bf6ef57aSJohn Polstra 			can_use_msi = 1;
2520bf6ef57aSJohn Polstra 	}
2521bf6ef57aSJohn Polstra 	return (can_use_msi);
2522bf6ef57aSJohn Polstra }
2523bf6ef57aSJohn Polstra 
252495d67482SBill Paul static int
25253f74909aSGleb Smirnoff bge_attach(device_t dev)
252695d67482SBill Paul {
252795d67482SBill Paul 	struct ifnet *ifp;
252895d67482SBill Paul 	struct bge_softc *sc;
25294f0794ffSBjoern A. Zeeb 	uint32_t hwcfg = 0, misccfg;
253008013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
2531d648358bSPyun YongHyeon 	int error, msicount, reg, rid, trys;
253295d67482SBill Paul 
253395d67482SBill Paul 	sc = device_get_softc(dev);
253495d67482SBill Paul 	sc->bge_dev = dev;
253595d67482SBill Paul 
2536dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
2537dfe0df9aSPyun YongHyeon 
253895d67482SBill Paul 	/*
253995d67482SBill Paul 	 * Map control/status registers.
254095d67482SBill Paul 	 */
254195d67482SBill Paul 	pci_enable_busmaster(dev);
254295d67482SBill Paul 
2543736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
25445f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
254544f8f2fcSMarius Strobl 	    RF_ACTIVE);
254695d67482SBill Paul 
254795d67482SBill Paul 	if (sc->bge_res == NULL) {
2548fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
254995d67482SBill Paul 		error = ENXIO;
255095d67482SBill Paul 		goto fail;
255195d67482SBill Paul 	}
255295d67482SBill Paul 
25534f09c4c7SMarius Strobl 	/* Save various chip information. */
2554e53d81eeSPaul Saab 	sc->bge_chipid =
2555a5779553SStanislav Sedov 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2556a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
2557a5779553SStanislav Sedov 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_USE_PRODID_REG)
2558a5779553SStanislav Sedov 		sc->bge_chipid = pci_read_config(dev, BGE_PCI_PRODID_ASICREV,
2559a5779553SStanislav Sedov 		    4);
2560e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2561e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2562e53d81eeSPaul Saab 
2563a813ed78SPyun YongHyeon 	/* Set default PHY address. */
2564a813ed78SPyun YongHyeon 	sc->bge_phy_addr = 1;
2565a813ed78SPyun YongHyeon 
256686543395SJung-uk Kim 	/*
256738cc658fSJohn Baldwin 	 * Don't enable Ethernet@WireSpeed for the 5700, 5906, or the
256886543395SJung-uk Kim 	 * 5705 A0 and A1 chips.
256986543395SJung-uk Kim 	 */
257086543395SJung-uk Kim 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
257138cc658fSJohn Baldwin 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
257286543395SJung-uk Kim 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
257386543395SJung-uk Kim 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)
2574757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_WIRESPEED;
257586543395SJung-uk Kim 
25765fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
25775fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
257808013fd3SMarius Strobl 
25790dae9719SJung-uk Kim 	/* Save chipset family. */
25800dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
2581a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
2582a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
2583a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
2584a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
2585a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
2586a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
2587a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
2588a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
2589a5779553SStanislav Sedov 		break;
25900dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
25910dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
25920dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
25930dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
25947ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
25950dae9719SJung-uk Kim 		break;
25960dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
25970dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
25980dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
25997ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5714_FAMILY /* | BGE_FLAG_JUMBO */;
26009fe569d8SXin LI 		/* FALLTHROUGH */
26010dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
26020dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
260338cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
26040dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
26059fe569d8SXin LI 		/* FALLTHROUGH */
26060dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
26070dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
26080dae9719SJung-uk Kim 		break;
26090dae9719SJung-uk Kim 	}
26100dae9719SJung-uk Kim 
2611757402fbSPyun YongHyeon 	/* Set various PHY bug flags. */
26121ec4c3a8SJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
26131ec4c3a8SJung-uk Kim 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
2614757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
26155ee49a3aSJung-uk Kim 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
26165ee49a3aSJung-uk Kim 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
2617757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
26185ee49a3aSJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
2619757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
26204150ce6fSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
2621757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
2622eea8956aSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
2623eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
2624eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
2625eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780) {
26265ee49a3aSJung-uk Kim 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
2627a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2628a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
26294fcf220bSJohn Baldwin 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
2630f7d1b2ebSXin LI 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
2631f7d1b2ebSXin LI 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
2632757402fbSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
2633eea8956aSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
2634eea8956aSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
2635eea8956aSPyun YongHyeon 		} else
2636757402fbSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
26375ee49a3aSJung-uk Kim 	}
26385ee49a3aSJung-uk Kim 
2639a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
2640a813ed78SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2641a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2642a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2643a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2644a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
2645a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
2646a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
2647a813ed78SPyun YongHyeon 	else
2648a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
2649a813ed78SPyun YongHyeon 
2650f681b29aSPyun YongHyeon 	/*
2651f681b29aSPyun YongHyeon 	 * All controllers that are not 5755 or higher have 4GB
2652f681b29aSPyun YongHyeon 	 * boundary DMA bug.
2653f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
2654f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
2655f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
2656f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
2657f681b29aSPyun YongHyeon 	 */
2658f681b29aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) == 0)
2659f681b29aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
26604f0794ffSBjoern A. Zeeb 
266184ac96f8SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
26624f0794ffSBjoern A. Zeeb 		misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID;
26634f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
26644f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
26654f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
266684ac96f8SPyun YongHyeon 	}
26674f0794ffSBjoern A. Zeeb 
2668e53d81eeSPaul Saab 	/*
2669ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
2670ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
2671ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
2672ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
2673ca3f1187SPyun YongHyeon 	 * known bug which can't handle TSO if ethernet header + IP/TCP
2674ca3f1187SPyun YongHyeon 	 * header is greater than 80 bytes. The workaround for the TSO
2675ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
2676ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
2677ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
2678ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
2679ca3f1187SPyun YongHyeon 	 */
26804f4a16e1SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc)) {
26814f4a16e1SPyun YongHyeon 		/*
26824f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
26834f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
2684be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
26854f4a16e1SPyun YongHyeon 		 */
26864f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
2687be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
2688be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
2689ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
26904f4a16e1SPyun YongHyeon 	}
2691ca3f1187SPyun YongHyeon 
2692ca3f1187SPyun YongHyeon   	/*
26936f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
2694e53d81eeSPaul Saab   	 */
26956f8718a3SScott Long 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
26964c0da0ffSGleb Smirnoff 		/*
26976f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
26986f8718a3SScott Long 		 * must be a PCI Express device.
26996f8718a3SScott Long 		 */
27006f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
27010aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
2702d2b6e9a0SPyun YongHyeon 		if (pci_get_max_read_req(dev) != 4096)
2703d2b6e9a0SPyun YongHyeon 			pci_set_max_read_req(dev, 4096);
27046f8718a3SScott Long 	} else {
27056f8718a3SScott Long 		/*
27066f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
27076f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
27084c0da0ffSGleb Smirnoff 		 */
27090aaf1057SPyun YongHyeon 		if (pci_find_extcap(dev, PCIY_PCIX, &reg) == 0)
27100aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
271190447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
27124c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
2713652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
27146f8718a3SScott Long 	}
27154c0da0ffSGleb Smirnoff 
2716bf6ef57aSJohn Polstra 	/*
2717fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
2718fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
2719fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
2720fd4d32feSPyun YongHyeon 	 */
2721fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
2722fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
2723fd4d32feSPyun YongHyeon 	/*
2724bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
2725bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
2726bf6ef57aSJohn Polstra 	 * normal operation.
2727bf6ef57aSJohn Polstra 	 */
27280aaf1057SPyun YongHyeon 	rid = 0;
27296a15578dSPyun YongHyeon 	if (pci_find_extcap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
27300aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
2731bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
2732bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
2733bf6ef57aSJohn Polstra 			if (msicount > 1)
2734bf6ef57aSJohn Polstra 				msicount = 1;
2735bf6ef57aSJohn Polstra 		} else
2736bf6ef57aSJohn Polstra 			msicount = 0;
2737bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
2738bf6ef57aSJohn Polstra 			rid = 1;
2739bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
27400aaf1057SPyun YongHyeon 		}
27410aaf1057SPyun YongHyeon 	}
2742bf6ef57aSJohn Polstra 
2743bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2744bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
2745bf6ef57aSJohn Polstra 
2746bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
2747bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
2748bf6ef57aSJohn Polstra 		error = ENXIO;
2749bf6ef57aSJohn Polstra 		goto fail;
2750bf6ef57aSJohn Polstra 	}
2751bf6ef57aSJohn Polstra 
27524f09c4c7SMarius Strobl 	device_printf(dev,
27534f09c4c7SMarius Strobl 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; %s\n",
27544f09c4c7SMarius Strobl 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev,
27554f09c4c7SMarius Strobl 	    (sc->bge_flags & BGE_FLAG_PCIX) ? "PCI-X" :
27564f09c4c7SMarius Strobl 	    ((sc->bge_flags & BGE_FLAG_PCIE) ? "PCI-E" : "PCI"));
27574f09c4c7SMarius Strobl 
2758bf6ef57aSJohn Polstra 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
2759bf6ef57aSJohn Polstra 
276095d67482SBill Paul 	/* Try to reset the chip. */
27618cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
27628cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
27638cb1383cSDoug Ambrisko 		error = ENXIO;
27648cb1383cSDoug Ambrisko 		goto fail;
27658cb1383cSDoug Ambrisko 	}
27668cb1383cSDoug Ambrisko 
27678cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
2768f1a7e6d5SScott Long 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG)
2769f1a7e6d5SScott Long 	    == BGE_MAGIC_NUMBER)) {
27708cb1383cSDoug Ambrisko 		if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG)
27718cb1383cSDoug Ambrisko 		    & BGE_HWCFG_ASF) {
27728cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_ENABLE;
27738cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_STACKUP;
2774d67eba2fSPyun YongHyeon 			if (BGE_IS_575X_PLUS(sc))
27758cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
27768cb1383cSDoug Ambrisko 		}
27778cb1383cSDoug Ambrisko 	}
27788cb1383cSDoug Ambrisko 
27798cb1383cSDoug Ambrisko 	/* Try to reset the chip again the nice way. */
27808cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
27818cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
27828cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
27838cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
27848cb1383cSDoug Ambrisko 		error = ENXIO;
27858cb1383cSDoug Ambrisko 		goto fail;
27868cb1383cSDoug Ambrisko 	}
27878cb1383cSDoug Ambrisko 
27888cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
27898cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
279095d67482SBill Paul 
279195d67482SBill Paul 	if (bge_chipinit(sc)) {
2792fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
279395d67482SBill Paul 		error = ENXIO;
279495d67482SBill Paul 		goto fail;
279595d67482SBill Paul 	}
279695d67482SBill Paul 
279738cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
279838cc658fSJohn Baldwin 	if (error) {
279908013fd3SMarius Strobl 		device_printf(sc->bge_dev,
280008013fd3SMarius Strobl 		    "failed to read station address\n");
280195d67482SBill Paul 		error = ENXIO;
280295d67482SBill Paul 		goto fail;
280395d67482SBill Paul 	}
280495d67482SBill Paul 
2805f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
28067ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
2807f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2808f41ac2beSBill Paul 	else
2809f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2810f41ac2beSBill Paul 
28115b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
2812fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2813fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2814f41ac2beSBill Paul 		error = ENXIO;
2815f41ac2beSBill Paul 		goto fail;
2816f41ac2beSBill Paul 	}
2817f41ac2beSBill Paul 
281835f945cdSPyun YongHyeon 	bge_add_sysctls(sc);
281935f945cdSPyun YongHyeon 
282095d67482SBill Paul 	/* Set default tuneable values. */
282195d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
282295d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
282395d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
28246f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
28256f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
282695d67482SBill Paul 
282735f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
282835f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
282935f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
283035f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
283135f945cdSPyun YongHyeon 
283295d67482SBill Paul 	/* Set up ifnet structure */
2833fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2834fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2835fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2836fc74a9f9SBrooks Davis 		error = ENXIO;
2837fc74a9f9SBrooks Davis 		goto fail;
2838fc74a9f9SBrooks Davis 	}
283995d67482SBill Paul 	ifp->if_softc = sc;
28409bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
284195d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
284295d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
284395d67482SBill Paul 	ifp->if_start = bge_start;
284495d67482SBill Paul 	ifp->if_init = bge_init;
28454d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
28464d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
28474d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
284835f945cdSPyun YongHyeon 	ifp->if_hwassist = sc->bge_csum_features;
2849d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
28504e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
2851ca3f1187SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TSO) != 0) {
2852ca3f1187SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
285304bde852SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_VLAN_HWTSO;
2854ca3f1187SPyun YongHyeon 	}
28554e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
28564e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
28574e35d186SJung-uk Kim #endif
285895d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
285975719184SGleb Smirnoff #ifdef DEVICE_POLLING
286075719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
286175719184SGleb Smirnoff #endif
286295d67482SBill Paul 
2863a1d52896SBill Paul 	/*
2864d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2865d375e524SGleb Smirnoff 	 * to hardware bugs.
2866d375e524SGleb Smirnoff 	 */
2867d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2868d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
28694d3a629cSPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_HWCSUM;
2870d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2871d375e524SGleb Smirnoff 	}
2872d375e524SGleb Smirnoff 
2873d375e524SGleb Smirnoff 	/*
2874a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
287541abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
287641abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
287741abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
287841abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
287941abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
288041abcc1bSPaul Saab 	 * SK-9D41.
2881a1d52896SBill Paul 	 */
288241abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
288341abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
28845fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
28855fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
2886f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2887f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2888fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2889f6789fbaSPyun YongHyeon 			error = ENXIO;
2890f6789fbaSPyun YongHyeon 			goto fail;
2891f6789fbaSPyun YongHyeon 		}
289241abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
289341abcc1bSPaul Saab 	}
289441abcc1bSPaul Saab 
289595d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
2896ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
2897ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
2898ea3b4127SPyun YongHyeon 		if (BGE_IS_5714_FAMILY(sc))
2899ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
2900ea3b4127SPyun YongHyeon 		else
2901652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
2902ea3b4127SPyun YongHyeon 	}
290395d67482SBill Paul 
2904652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
29050c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
29060c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
29070c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
29086098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
29096098821cSJung-uk Kim 		    0, NULL);
291095d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
291195d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
2912da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
291395d67482SBill Paul 	} else {
291495d67482SBill Paul 		/*
29158cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
29168cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
29178cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
29188cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
29198cb1383cSDoug Ambrisko 		 * the PHY.
292095d67482SBill Paul 		 */
29214012d104SMarius Strobl 		trys = 0;
29228cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
29238cb1383cSDoug Ambrisko again:
29248cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
29258cb1383cSDoug Ambrisko 
292695d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
292795d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
29288cb1383cSDoug Ambrisko 			if (trys++ < 4) {
29298cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
29304e35d186SJung-uk Kim 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR,
29314e35d186SJung-uk Kim 				    BMCR_RESET);
29328cb1383cSDoug Ambrisko 				goto again;
29338cb1383cSDoug Ambrisko 			}
29348cb1383cSDoug Ambrisko 
2935fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
293695d67482SBill Paul 			error = ENXIO;
293795d67482SBill Paul 			goto fail;
293895d67482SBill Paul 		}
29398cb1383cSDoug Ambrisko 
29408cb1383cSDoug Ambrisko 		/*
29418cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
29428cb1383cSDoug Ambrisko 		 */
29438cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
29448cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
294595d67482SBill Paul 	}
294695d67482SBill Paul 
294795d67482SBill Paul 	/*
2948e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2949e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2950e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2951e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2952e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2953e255b776SJohn Polstra 	 * payloads by copying the received packets.
2954e255b776SJohn Polstra 	 */
2955652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
2956652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
2957652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
2958e255b776SJohn Polstra 
2959e255b776SJohn Polstra 	/*
296095d67482SBill Paul 	 * Call MI attach routine.
296195d67482SBill Paul 	 */
2962fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
2963b74e67fbSGleb Smirnoff 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
29640f9bd73bSSam Leffler 
296561ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
296661ccb9daSPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
296761ccb9daSPyun YongHyeon 
29680f9bd73bSSam Leffler 	/*
29690f9bd73bSSam Leffler 	 * Hookup IRQ last.
29700f9bd73bSSam Leffler 	 */
29714e35d186SJung-uk Kim #if __FreeBSD_version > 700030
2972dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
2973dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
29747e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
29757e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
2976dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
2977dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
2978dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
2979dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
2980dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
2981dfe0df9aSPyun YongHyeon 			error = ENXIO;
2982dfe0df9aSPyun YongHyeon 			goto fail;
2983dfe0df9aSPyun YongHyeon 		}
2984dfe0df9aSPyun YongHyeon 		taskqueue_start_threads(&sc->bge_tq, 1, PI_NET, "%s taskq",
2985dfe0df9aSPyun YongHyeon 		    device_get_nameunit(sc->bge_dev));
2986dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
2987dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
2988dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
2989dfe0df9aSPyun YongHyeon 		if (error)
2990dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
2991dfe0df9aSPyun YongHyeon 	} else
2992dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
2993dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
2994dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
29954e35d186SJung-uk Kim #else
29964e35d186SJung-uk Kim 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
29974e35d186SJung-uk Kim 	   bge_intr, sc, &sc->bge_intrhand);
29984e35d186SJung-uk Kim #endif
29990f9bd73bSSam Leffler 
30000f9bd73bSSam Leffler 	if (error) {
3001fc74a9f9SBrooks Davis 		bge_detach(dev);
3002fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
30030f9bd73bSSam Leffler 	}
300495d67482SBill Paul 
300508013fd3SMarius Strobl 	return (0);
300608013fd3SMarius Strobl 
300795d67482SBill Paul fail:
300808013fd3SMarius Strobl 	bge_release_resources(sc);
300908013fd3SMarius Strobl 
301095d67482SBill Paul 	return (error);
301195d67482SBill Paul }
301295d67482SBill Paul 
301395d67482SBill Paul static int
30143f74909aSGleb Smirnoff bge_detach(device_t dev)
301595d67482SBill Paul {
301695d67482SBill Paul 	struct bge_softc *sc;
301795d67482SBill Paul 	struct ifnet *ifp;
301895d67482SBill Paul 
301995d67482SBill Paul 	sc = device_get_softc(dev);
3020fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
302195d67482SBill Paul 
302275719184SGleb Smirnoff #ifdef DEVICE_POLLING
302375719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
302475719184SGleb Smirnoff 		ether_poll_deregister(ifp);
302575719184SGleb Smirnoff #endif
302675719184SGleb Smirnoff 
30270f9bd73bSSam Leffler 	BGE_LOCK(sc);
302895d67482SBill Paul 	bge_stop(sc);
302995d67482SBill Paul 	bge_reset(sc);
30300f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
30310f9bd73bSSam Leffler 
30325dda8085SOleg Bulyzhin 	callout_drain(&sc->bge_stat_ch);
30335dda8085SOleg Bulyzhin 
3034dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3035dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
30360f9bd73bSSam Leffler 	ether_ifdetach(ifp);
303795d67482SBill Paul 
3038652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
303995d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
304095d67482SBill Paul 	} else {
304195d67482SBill Paul 		bus_generic_detach(dev);
304295d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
304395d67482SBill Paul 	}
304495d67482SBill Paul 
304595d67482SBill Paul 	bge_release_resources(sc);
304695d67482SBill Paul 
304795d67482SBill Paul 	return (0);
304895d67482SBill Paul }
304995d67482SBill Paul 
305095d67482SBill Paul static void
30513f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
305295d67482SBill Paul {
305395d67482SBill Paul 	device_t dev;
305495d67482SBill Paul 
305595d67482SBill Paul 	dev = sc->bge_dev;
305695d67482SBill Paul 
3057dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3058dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3059dfe0df9aSPyun YongHyeon 
306095d67482SBill Paul 	if (sc->bge_intrhand != NULL)
306195d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
306295d67482SBill Paul 
306395d67482SBill Paul 	if (sc->bge_irq != NULL)
3064724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3065724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
3066724bd939SJohn Polstra 
3067724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
3068724bd939SJohn Polstra 		pci_release_msi(dev);
306995d67482SBill Paul 
307095d67482SBill Paul 	if (sc->bge_res != NULL)
307195d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
3072736b9319SPyun YongHyeon 		    PCIR_BAR(0), sc->bge_res);
307395d67482SBill Paul 
3074ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
3075ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
3076ad61f896SRuslan Ermilov 
3077f41ac2beSBill Paul 	bge_dma_free(sc);
307895d67482SBill Paul 
30790f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
30800f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
308195d67482SBill Paul }
308295d67482SBill Paul 
30838cb1383cSDoug Ambrisko static int
30843f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
308595d67482SBill Paul {
308695d67482SBill Paul 	device_t dev;
30875fea260fSMarius Strobl 	uint32_t cachesize, command, pcistate, reset, val;
30886f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
30890aaf1057SPyun YongHyeon 	uint16_t devctl;
30905fea260fSMarius Strobl 	int i;
309195d67482SBill Paul 
309295d67482SBill Paul 	dev = sc->bge_dev;
309395d67482SBill Paul 
309438cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
309538cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
30966f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
30976f8718a3SScott Long 			write_op = bge_writemem_direct;
30986f8718a3SScott Long 		else
30996f8718a3SScott Long 			write_op = bge_writemem_ind;
31009ba784dbSScott Long 	} else
31016f8718a3SScott Long 		write_op = bge_writereg_ind;
31026f8718a3SScott Long 
310395d67482SBill Paul 	/* Save some important PCI state. */
310495d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
310595d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
310695d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
310795d67482SBill Paul 
310895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
310995d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3110e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
311195d67482SBill Paul 
31126f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
31136f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
3114a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
31156f8718a3SScott Long 		if (bootverbose)
3116333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
31176f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
31186f8718a3SScott Long 	}
31196f8718a3SScott Long 
31206f8718a3SScott Long 	/*
31216f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
31226f8718a3SScott Long 	 * When firmware finishes its initialization it will
31236f8718a3SScott Long 	 * write ~BGE_MAGIC_NUMBER to the same location.
31246f8718a3SScott Long 	 */
31256f8718a3SScott Long 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
31266f8718a3SScott Long 
31270c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
3128e53d81eeSPaul Saab 
3129e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3130652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
31310c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
31320c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, 0x7E2C, 0x20);
3133e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
3134e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
31350c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
31360c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
3137e53d81eeSPaul Saab 		}
3138e53d81eeSPaul Saab 	}
3139e53d81eeSPaul Saab 
314021c9e407SDavid Christensen 	/*
31416f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
31426f8718a3SScott Long 	 * powered up in D0 uninitialized.
31436f8718a3SScott Long 	 */
31445345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
3145caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
31466f8718a3SScott Long 
314795d67482SBill Paul 	/* Issue global reset */
31486f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
314995d67482SBill Paul 
315038cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
31515fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
315238cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
31535fea260fSMarius Strobl 		    val | BGE_VCPU_STATUS_DRV_RESET);
31545fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
315538cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
31565fea260fSMarius Strobl 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
315738cc658fSJohn Baldwin 	}
315838cc658fSJohn Baldwin 
315995d67482SBill Paul 	DELAY(1000);
316095d67482SBill Paul 
3161e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3162652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
3163e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
3164e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
31655fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
31665fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
3167e53d81eeSPaul Saab 		}
31680aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
31690aaf1057SPyun YongHyeon 		    sc->bge_expcap + PCIR_EXPRESS_DEVICE_CTL, 2);
31700aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
31719a6e301dSPyun YongHyeon 		devctl &= ~(PCIM_EXP_CTL_RELAXED_ORD_ENABLE |
31729a6e301dSPyun YongHyeon 		    PCIM_EXP_CTL_NOSNOOP_ENABLE);
31730aaf1057SPyun YongHyeon 		/* Set PCIE max payload size to 128. */
31740aaf1057SPyun YongHyeon 		devctl &= ~PCIM_EXP_CTL_MAX_PAYLOAD;
31750aaf1057SPyun YongHyeon 		pci_write_config(dev, sc->bge_expcap + PCIR_EXPRESS_DEVICE_CTL,
31760aaf1057SPyun YongHyeon 		    devctl, 2);
31770aaf1057SPyun YongHyeon 		/* Clear error status. */
31780aaf1057SPyun YongHyeon 		pci_write_config(dev, sc->bge_expcap + PCIR_EXPRESS_DEVICE_STA,
31799a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_CORRECTABLE_ERROR |
31809a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_NON_FATAL_ERROR | PCIM_EXP_STA_FATAL_ERROR |
31819a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_UNSUPPORTED_REQ, 2);
3182e53d81eeSPaul Saab 	}
3183e53d81eeSPaul Saab 
31843f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
318595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
318695d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3187e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
318895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
318995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
31900c8aa4eaSJung-uk Kim 	write_op(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
3191cbb2b2feSPyun YongHyeon 	/*
3192cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
3193fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
3194cbb2b2feSPyun YongHyeon 	 * read stale status block.
3195cbb2b2feSPyun YongHyeon 	 */
3196cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
3197cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
3198cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
3199cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
3200cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
3201cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
3202cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
3203cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3204cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
3205cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
3206cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
3207cbb2b2feSPyun YongHyeon 		}
3208cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
3209cbb2b2feSPyun YongHyeon 		    devctl, 2);
3210cbb2b2feSPyun YongHyeon 	}
3211bf6ef57aSJohn Polstra 	/* Re-enable MSI, if neccesary, and enable the memory arbiter. */
32124c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
3213bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
3214bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
32150aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
32160aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
32170aaf1057SPyun YongHyeon 			pci_write_config(dev,
32180aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
3219bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
3220bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
3221bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
3222bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
3223bf6ef57aSJohn Polstra 		}
32244c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
32254c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
32264c0da0ffSGleb Smirnoff 	} else
3227a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
3228a7b0c314SPaul Saab 
322938cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
323038cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
323138cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
323238cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
323338cc658fSJohn Baldwin 				break;
323438cc658fSJohn Baldwin 			DELAY(100);
323538cc658fSJohn Baldwin 		}
323638cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
3237333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
323838cc658fSJohn Baldwin 			return (1);
323938cc658fSJohn Baldwin 		}
324038cc658fSJohn Baldwin 	} else {
324195d67482SBill Paul 		/*
32426f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
324308013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
32445fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
32455fea260fSMarius Strobl 		 * address is fitted though.
324695d67482SBill Paul 		 */
324795d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
3248d5d23857SJung-uk Kim 			DELAY(10);
324995d67482SBill Paul 			val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
325095d67482SBill Paul 			if (val == ~BGE_MAGIC_NUMBER)
325195d67482SBill Paul 				break;
325295d67482SBill Paul 		}
325395d67482SBill Paul 
32545fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
3255333704a3SPyun YongHyeon 			device_printf(dev,
3256333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
3257333704a3SPyun YongHyeon 			    val);
325838cc658fSJohn Baldwin 	}
325995d67482SBill Paul 
326095d67482SBill Paul 	/*
326195d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
326295d67482SBill Paul 	 * return to its original pre-reset state. This is a
326395d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
326495d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
326595d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
326695d67482SBill Paul 	 * results.
326795d67482SBill Paul 	 */
326895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
326995d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
327095d67482SBill Paul 			break;
327195d67482SBill Paul 		DELAY(10);
327295d67482SBill Paul 	}
327395d67482SBill Paul 
32743f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
3275e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
327695d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
327795d67482SBill Paul 
32788cb1383cSDoug Ambrisko 	/* Tell the ASF firmware we are up */
32798cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
32808cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
32818cb1383cSDoug Ambrisko 
328295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
328395d67482SBill Paul 
3284da3003f0SBill Paul 	/*
3285da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
3286da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
3287da3003f0SBill Paul 	 * to 1.2V.
3288da3003f0SBill Paul 	 */
3289652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
3290652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
32915fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
32925fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
32935fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
3294da3003f0SBill Paul 	}
3295da3003f0SBill Paul 
3296e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3297652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
3298a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
3299a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
3300a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
33015fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
33025fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
3303e53d81eeSPaul Saab 	}
330495d67482SBill Paul 	DELAY(10000);
33058cb1383cSDoug Ambrisko 
33068cb1383cSDoug Ambrisko 	return (0);
330795d67482SBill Paul }
330895d67482SBill Paul 
3309e0b7b101SPyun YongHyeon static __inline void
3310e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
3311e0b7b101SPyun YongHyeon {
3312e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
3313e0b7b101SPyun YongHyeon 
3314e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
3315e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
3316e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
3317e0b7b101SPyun YongHyeon 	r->bge_idx = i;
3318e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
3319e0b7b101SPyun YongHyeon }
3320e0b7b101SPyun YongHyeon 
3321e0b7b101SPyun YongHyeon static __inline void
3322e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
3323e0b7b101SPyun YongHyeon {
3324e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
3325e0b7b101SPyun YongHyeon 
3326e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
3327e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
3328e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
3329e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
3330e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
3331e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
3332e0b7b101SPyun YongHyeon 	r->bge_idx = i;
3333e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
3334e0b7b101SPyun YongHyeon }
3335e0b7b101SPyun YongHyeon 
333695d67482SBill Paul /*
333795d67482SBill Paul  * Frame reception handling. This is called if there's a frame
333895d67482SBill Paul  * on the receive return list.
333995d67482SBill Paul  *
334095d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
33411be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
334295d67482SBill Paul  * 2) the frame is from the standard receive ring
334395d67482SBill Paul  */
334495d67482SBill Paul 
33451abcdbd1SAttilio Rao static int
3346dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
334795d67482SBill Paul {
334895d67482SBill Paul 	struct ifnet *ifp;
33491abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
3350b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
335195d67482SBill Paul 
33527f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
33530f9bd73bSSam Leffler 
33543f74909aSGleb Smirnoff 	/* Nothing to do. */
33557f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
33561abcdbd1SAttilio Rao 		return (rx_npkts);
3357cfcb5025SOleg Bulyzhin 
3358fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
335995d67482SBill Paul 
3360f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
3361e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
3362f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
336315eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
3364c215fd77SPyun YongHyeon 	if (ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
3365c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN))
3366f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
336715eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
3368f41ac2beSBill Paul 
33697f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
337095d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
33713f74909aSGleb Smirnoff 		uint32_t		rxidx;
337295d67482SBill Paul 		struct mbuf		*m = NULL;
33733f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
337495d67482SBill Paul 		int			have_tag = 0;
337595d67482SBill Paul 
337675719184SGleb Smirnoff #ifdef DEVICE_POLLING
337775719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
337875719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
337975719184SGleb Smirnoff 				break;
338075719184SGleb Smirnoff 			sc->rxcycles--;
338175719184SGleb Smirnoff 		}
338275719184SGleb Smirnoff #endif
338375719184SGleb Smirnoff 
33847f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
338595d67482SBill Paul 
338695d67482SBill Paul 		rxidx = cur_rx->bge_idx;
33877f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
338895d67482SBill Paul 
3389cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
3390cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
339195d67482SBill Paul 			have_tag = 1;
339295d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
339395d67482SBill Paul 		}
339495d67482SBill Paul 
339595d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
339695d67482SBill Paul 			jumbocnt++;
3397943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
339895d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3399e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
340095d67482SBill Paul 				continue;
340195d67482SBill Paul 			}
3402943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
3403e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
3404943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
340595d67482SBill Paul 				continue;
340695d67482SBill Paul 			}
340703e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
340895d67482SBill Paul 		} else {
340995d67482SBill Paul 			stdcnt++;
3410e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
341195d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3412e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
341395d67482SBill Paul 				continue;
341495d67482SBill Paul 			}
3415943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
3416e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
3417943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
341895d67482SBill Paul 				continue;
341995d67482SBill Paul 			}
342003e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
342195d67482SBill Paul 		}
342295d67482SBill Paul 
342395d67482SBill Paul 		ifp->if_ipackets++;
3424e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
3425e255b776SJohn Polstra 		/*
3426e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
3427e65bed95SPyun YongHyeon 		 * the payload is aligned.
3428e255b776SJohn Polstra 		 */
3429652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
3430e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
3431e255b776SJohn Polstra 			    cur_rx->bge_len);
3432e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
3433e255b776SJohn Polstra 		}
3434e255b776SJohn Polstra #endif
3435473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
343695d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
343795d67482SBill Paul 
3438b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
343978178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
344095d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
34410c8aa4eaSJung-uk Kim 				if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
34420c8aa4eaSJung-uk Kim 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
344378178cd1SGleb Smirnoff 			}
3444d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
3445d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
344695d67482SBill Paul 				m->m_pkthdr.csum_data =
344795d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
3448ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
3449ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
345095d67482SBill Paul 			}
345195d67482SBill Paul 		}
345295d67482SBill Paul 
345395d67482SBill Paul 		/*
3454673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
3455673d9191SSam Leffler 		 * attach that information to the packet.
345695d67482SBill Paul 		 */
3457d147662cSGleb Smirnoff 		if (have_tag) {
34584e35d186SJung-uk Kim #if __FreeBSD_version > 700022
345978ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
346078ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
34614e35d186SJung-uk Kim #else
34624e35d186SJung-uk Kim 			VLAN_INPUT_TAG_NEW(ifp, m, vlan_tag);
34634e35d186SJung-uk Kim 			if (m == NULL)
34644e35d186SJung-uk Kim 				continue;
34654e35d186SJung-uk Kim #endif
3466d147662cSGleb Smirnoff 		}
346795d67482SBill Paul 
3468dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
34690f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
3470673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
34710f9bd73bSSam Leffler 			BGE_LOCK(sc);
3472dfe0df9aSPyun YongHyeon 		} else
3473dfe0df9aSPyun YongHyeon 			(*ifp->if_input)(ifp, m);
3474d4da719cSAttilio Rao 		rx_npkts++;
347525e13e68SXin LI 
347625e13e68SXin LI 		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
34778cf7d13dSAttilio Rao 			return (rx_npkts);
347895d67482SBill Paul 	}
347995d67482SBill Paul 
348015eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
348115eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
3482e65bed95SPyun YongHyeon 	if (stdcnt > 0)
3483f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
3484e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
34854c0da0ffSGleb Smirnoff 
3486c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
3487f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
34884c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
3489f41ac2beSBill Paul 
34907f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
349138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
349295d67482SBill Paul 	if (stdcnt)
3493767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
3494767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
349595d67482SBill Paul 	if (jumbocnt)
3496767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
3497767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
3498f5a034f9SPyun YongHyeon #ifdef notyet
3499f5a034f9SPyun YongHyeon 	/*
3500f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
3501f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
3502f5a034f9SPyun YongHyeon 	 */
3503f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
3504f5a034f9SPyun YongHyeon 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
3505f5a034f9SPyun YongHyeon #endif
35061abcdbd1SAttilio Rao 	return (rx_npkts);
350795d67482SBill Paul }
350895d67482SBill Paul 
350995d67482SBill Paul static void
3510b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
351195d67482SBill Paul {
351295a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
351395d67482SBill Paul 	struct ifnet *ifp;
351495d67482SBill Paul 
35150f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
35160f9bd73bSSam Leffler 
35173f74909aSGleb Smirnoff 	/* Nothing to do. */
3518b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
3519cfcb5025SOleg Bulyzhin 		return;
3520cfcb5025SOleg Bulyzhin 
3521fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
352295d67482SBill Paul 
3523e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
35245c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
352595d67482SBill Paul 	/*
352695d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
352795d67482SBill Paul 	 * frames that have been sent.
352895d67482SBill Paul 	 */
3529b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
353095a0a340SPyun YongHyeon 		uint32_t		idx;
353195d67482SBill Paul 
353295d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
3533f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
353495d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
353595d67482SBill Paul 			ifp->if_opackets++;
353695d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
35370ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
3538e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
3539e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
35400ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
3541f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
3542e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
3543e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
354495d67482SBill Paul 		}
354595d67482SBill Paul 		sc->bge_txcnt--;
354695d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
354795d67482SBill Paul 	}
354895d67482SBill Paul 
354913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
35505b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
35515b01e77cSBruce Evans 		sc->bge_timer = 0;
355295d67482SBill Paul }
355395d67482SBill Paul 
355475719184SGleb Smirnoff #ifdef DEVICE_POLLING
35551abcdbd1SAttilio Rao static int
355675719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
355775719184SGleb Smirnoff {
355875719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
3559b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
3560366454f2SOleg Bulyzhin 	uint32_t statusword;
35611abcdbd1SAttilio Rao 	int rx_npkts = 0;
356275719184SGleb Smirnoff 
35633f74909aSGleb Smirnoff 	BGE_LOCK(sc);
35643f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
35653f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
35661abcdbd1SAttilio Rao 		return (rx_npkts);
35673f74909aSGleb Smirnoff 	}
356875719184SGleb Smirnoff 
3569dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3570b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3571b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3572b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
3573b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
3574dab5cd05SOleg Bulyzhin 
3575175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
3576175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
3577dab5cd05SOleg Bulyzhin 
3578dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3579b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3580b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3581366454f2SOleg Bulyzhin 
35820c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
3583366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
3584366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
3585366454f2SOleg Bulyzhin 
3586366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
3587366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
35884c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3589652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
3590366454f2SOleg Bulyzhin 			bge_link_upd(sc);
3591366454f2SOleg Bulyzhin 
3592366454f2SOleg Bulyzhin 	sc->rxcycles = count;
3593dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
359425e13e68SXin LI 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
359525e13e68SXin LI 		BGE_UNLOCK(sc);
35968cf7d13dSAttilio Rao 		return (rx_npkts);
359725e13e68SXin LI 	}
3598b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
3599366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3600366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
36013f74909aSGleb Smirnoff 
36023f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
36031abcdbd1SAttilio Rao 	return (rx_npkts);
360475719184SGleb Smirnoff }
360575719184SGleb Smirnoff #endif /* DEVICE_POLLING */
360675719184SGleb Smirnoff 
3607dfe0df9aSPyun YongHyeon static int
3608dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
3609dfe0df9aSPyun YongHyeon {
3610dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
3611dfe0df9aSPyun YongHyeon 
3612dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
3613dfe0df9aSPyun YongHyeon 	/*
3614dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
3615dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
3616dfe0df9aSPyun YongHyeon 	 */
3617dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
3618dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
3619dfe0df9aSPyun YongHyeon }
3620dfe0df9aSPyun YongHyeon 
3621dfe0df9aSPyun YongHyeon static void
3622dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
3623dfe0df9aSPyun YongHyeon {
3624dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
3625dfe0df9aSPyun YongHyeon 	struct ifnet *ifp;
3626dfe0df9aSPyun YongHyeon 	uint32_t status;
3627dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
3628dfe0df9aSPyun YongHyeon 
3629dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
3630dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
3631dfe0df9aSPyun YongHyeon 
3632dfe0df9aSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
3633dfe0df9aSPyun YongHyeon 		return;
3634dfe0df9aSPyun YongHyeon 
3635dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
3636dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3637dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3638dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3639dfe0df9aSPyun YongHyeon 
3640dfe0df9aSPyun YongHyeon 	/* Save producer/consumer indexess. */
3641dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
3642dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
3643dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
3644dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
3645dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3646dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3647dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3648dfe0df9aSPyun YongHyeon 	/* Let controller work. */
3649dfe0df9aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
3650dfe0df9aSPyun YongHyeon 
3651dfe0df9aSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0) {
3652dfe0df9aSPyun YongHyeon 		BGE_LOCK(sc);
3653dfe0df9aSPyun YongHyeon 		bge_link_upd(sc);
3654dfe0df9aSPyun YongHyeon 		BGE_UNLOCK(sc);
3655dfe0df9aSPyun YongHyeon 	}
3656dfe0df9aSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3657dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
3658dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
3659dfe0df9aSPyun YongHyeon 	}
3660dfe0df9aSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3661dfe0df9aSPyun YongHyeon 		BGE_LOCK(sc);
3662dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
3663dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
3664dfe0df9aSPyun YongHyeon 	    	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3665dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
3666dfe0df9aSPyun YongHyeon 		BGE_UNLOCK(sc);
3667dfe0df9aSPyun YongHyeon 	}
3668dfe0df9aSPyun YongHyeon }
3669dfe0df9aSPyun YongHyeon 
367095d67482SBill Paul static void
36713f74909aSGleb Smirnoff bge_intr(void *xsc)
367295d67482SBill Paul {
367395d67482SBill Paul 	struct bge_softc *sc;
367495d67482SBill Paul 	struct ifnet *ifp;
3675dab5cd05SOleg Bulyzhin 	uint32_t statusword;
3676b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
367795d67482SBill Paul 
367895d67482SBill Paul 	sc = xsc;
3679f41ac2beSBill Paul 
36800f9bd73bSSam Leffler 	BGE_LOCK(sc);
36810f9bd73bSSam Leffler 
3682dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
3683dab5cd05SOleg Bulyzhin 
368475719184SGleb Smirnoff #ifdef DEVICE_POLLING
368575719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
368675719184SGleb Smirnoff 		BGE_UNLOCK(sc);
368775719184SGleb Smirnoff 		return;
368875719184SGleb Smirnoff 	}
368975719184SGleb Smirnoff #endif
369075719184SGleb Smirnoff 
3691f30cbfc6SScott Long 	/*
3692b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
3693b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
3694b848e032SBruce Evans 	 * our current organization this just gives complications and
3695b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
3696b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
3697b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
3698b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
3699b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
3700b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
3701b848e032SBruce Evans 	 *
3702b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
3703b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
3704b848e032SBruce Evans 	 * changing later because it is more efficient to get another
3705b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
3706b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
3707b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
3708b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
3709b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
3710b848e032SBruce Evans 	 */
371138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
3712b848e032SBruce Evans 
3713f584dfd1SPyun YongHyeon 	/*
3714f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
3715f584dfd1SPyun YongHyeon 	 */
3716f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
3717f584dfd1SPyun YongHyeon 
3718f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
3719f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3720f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3721f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3722f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
3723f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
3724f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
3725f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3726f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3727f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3728f584dfd1SPyun YongHyeon 
37291f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
37304c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3731f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
3732dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
373395d67482SBill Paul 
373413f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
37353f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
3736dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
373725e13e68SXin LI 	}
373895d67482SBill Paul 
373925e13e68SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
37403f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
3741b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
374295d67482SBill Paul 	}
374395d67482SBill Paul 
374413f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
374513f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
37460f9bd73bSSam Leffler 		bge_start_locked(ifp);
37470f9bd73bSSam Leffler 
37480f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
374995d67482SBill Paul }
375095d67482SBill Paul 
375195d67482SBill Paul static void
37528cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
37538cb1383cSDoug Ambrisko {
37548cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
37558cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
37568cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
37578cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
37588cb1383cSDoug Ambrisko 		else {
3759899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
37608cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW,
37618cb1383cSDoug Ambrisko 			    BGE_FW_DRV_ALIVE);
37628cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_LEN, 4);
37638cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_DATA, 3);
37648cb1383cSDoug Ambrisko 			CSR_WRITE_4(sc, BGE_CPU_EVENT,
376539153c5aSJung-uk Kim 			    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
37668cb1383cSDoug Ambrisko 		}
37678cb1383cSDoug Ambrisko 	}
37688cb1383cSDoug Ambrisko }
37698cb1383cSDoug Ambrisko 
37708cb1383cSDoug Ambrisko static void
3771b74e67fbSGleb Smirnoff bge_tick(void *xsc)
37720f9bd73bSSam Leffler {
3773b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
377495d67482SBill Paul 	struct mii_data *mii = NULL;
377595d67482SBill Paul 
37760f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
377795d67482SBill Paul 
37785dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
37795dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
37805dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
37815dda8085SOleg Bulyzhin 	    	return;
37825dda8085SOleg Bulyzhin 
37837ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
37840434d1b8SBill Paul 		bge_stats_update_regs(sc);
37850434d1b8SBill Paul 	else
378695d67482SBill Paul 		bge_stats_update(sc);
378795d67482SBill Paul 
3788652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
378995d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
379082b67c01SOleg Bulyzhin 		/*
379182b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
379282b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
379382b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
379482b67c01SOleg Bulyzhin 		 */
379582b67c01SOleg Bulyzhin 		if (!sc->bge_link)
379695d67482SBill Paul 			mii_tick(mii);
37977b97099dSOleg Bulyzhin 	} else {
37987b97099dSOleg Bulyzhin 		/*
37997b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
38007b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
38017b97099dSOleg Bulyzhin 		 * and trigger interrupt.
38027b97099dSOleg Bulyzhin 		 */
38037b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
38043f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
38057b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
38067b97099dSOleg Bulyzhin #endif
38077b97099dSOleg Bulyzhin 		{
38087b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
38094f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
38104f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
38117b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
38124f0794ffSBjoern A. Zeeb 		else
38134f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
38147b97099dSOleg Bulyzhin 		}
3815dab5cd05SOleg Bulyzhin 	}
381695d67482SBill Paul 
38178cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
3818b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
38198cb1383cSDoug Ambrisko 
3820dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
382195d67482SBill Paul }
382295d67482SBill Paul 
382395d67482SBill Paul static void
38243f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
38250434d1b8SBill Paul {
38263f74909aSGleb Smirnoff 	struct ifnet *ifp;
38272280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
38280434d1b8SBill Paul 
3829fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
38302280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
38310434d1b8SBill Paul 
38322280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
38332280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
38342280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
38352280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
38362280c16bSPyun YongHyeon 	stats->outXonSent +=
38372280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
38382280c16bSPyun YongHyeon 	stats->outXoffSent +=
38392280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
38402280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
38412280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
38422280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
38432280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
38442280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
38452280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
38462280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
38472280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
38482280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
38492280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
38502280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
38512280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
38522280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
38532280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
38542280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
38552280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
38562280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
38572280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
38587e6e2507SJung-uk Kim 
38592280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
38602280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
38612280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
38622280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
38632280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
38642280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
38652280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
38662280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
38672280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
38682280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
38692280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
38702280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
38712280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
38722280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
38732280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
38742280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
38752280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
38762280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
38772280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
38782280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
38792280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
38802280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
38812280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
38822280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
38832280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
38842280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
38852280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
38862280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
38872280c16bSPyun YongHyeon 
38882280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
38892280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
38902280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
38912280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
38922280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
38932280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
38942280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
38952280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
38962280c16bSPyun YongHyeon 	stats->InputDiscards +=
38972280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
38982280c16bSPyun YongHyeon 	stats->InputErrors +=
38992280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
39002280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
39012280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
39022280c16bSPyun YongHyeon 
39032280c16bSPyun YongHyeon 	ifp->if_collisions = (u_long)stats->etherStatsCollisions;
39042280c16bSPyun YongHyeon 	ifp->if_ierrors = (u_long)(stats->NoMoreRxBDs + stats->InputDiscards +
39052280c16bSPyun YongHyeon 	    stats->InputErrors);
39062280c16bSPyun YongHyeon }
39072280c16bSPyun YongHyeon 
39082280c16bSPyun YongHyeon static void
39092280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
39102280c16bSPyun YongHyeon {
39112280c16bSPyun YongHyeon 
39122280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
39132280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
39142280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
39152280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
39162280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
39172280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
39182280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
39192280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
39202280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
39212280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
39222280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
39232280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
39242280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
39252280c16bSPyun YongHyeon 
39262280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
39272280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
39282280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
39292280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
39302280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
39312280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
39322280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
39332280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
39342280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
39352280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
39362280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
39372280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
39382280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
39392280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
39402280c16bSPyun YongHyeon 
39412280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
39422280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
39432280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
39442280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
39452280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
39462280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
39472280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
39480434d1b8SBill Paul }
39490434d1b8SBill Paul 
39500434d1b8SBill Paul static void
39513f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
395295d67482SBill Paul {
395395d67482SBill Paul 	struct ifnet *ifp;
3954e907febfSPyun YongHyeon 	bus_size_t stats;
39557e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
395695d67482SBill Paul 
3957fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
395895d67482SBill Paul 
3959e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
3960e907febfSPyun YongHyeon 
3961e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
3962e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
396395d67482SBill Paul 
39648634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
39656b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
39666fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
39676fb34dd2SOleg Bulyzhin 
39686fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
39696b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
39706fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
39716fb34dd2SOleg Bulyzhin 
39726fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
39736b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
39746fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
397595d67482SBill Paul 
3976e907febfSPyun YongHyeon #undef	READ_STAT
397795d67482SBill Paul }
397895d67482SBill Paul 
397995d67482SBill Paul /*
3980d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
3981d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
3982d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
3983d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
3984d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
3985d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
3986d375e524SGleb Smirnoff  */
3987d375e524SGleb Smirnoff static __inline int
3988d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
3989d375e524SGleb Smirnoff {
3990d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
3991d375e524SGleb Smirnoff 	struct mbuf *last;
3992d375e524SGleb Smirnoff 
3993d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
3994d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
3995d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
3996d375e524SGleb Smirnoff 		last = m;
3997d375e524SGleb Smirnoff 	} else {
3998d375e524SGleb Smirnoff 		/*
3999d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
4000d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
4001d375e524SGleb Smirnoff 		 */
4002d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
4003d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
4004d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
4005d375e524SGleb Smirnoff 			struct mbuf *n;
4006d375e524SGleb Smirnoff 
4007d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
4008d375e524SGleb Smirnoff 			if (n == NULL)
4009d375e524SGleb Smirnoff 				return (ENOBUFS);
4010d375e524SGleb Smirnoff 			n->m_len = 0;
4011d375e524SGleb Smirnoff 			last->m_next = n;
4012d375e524SGleb Smirnoff 			last = n;
4013d375e524SGleb Smirnoff 		}
4014d375e524SGleb Smirnoff 	}
4015d375e524SGleb Smirnoff 
4016d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
4017d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
4018d375e524SGleb Smirnoff 	last->m_len += padlen;
4019d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
4020d375e524SGleb Smirnoff 
4021d375e524SGleb Smirnoff 	return (0);
4022d375e524SGleb Smirnoff }
4023d375e524SGleb Smirnoff 
4024ca3f1187SPyun YongHyeon static struct mbuf *
4025ca3f1187SPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss)
4026ca3f1187SPyun YongHyeon {
4027ca3f1187SPyun YongHyeon 	struct ip *ip;
4028ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
4029ca3f1187SPyun YongHyeon 	struct mbuf *n;
4030ca3f1187SPyun YongHyeon 	uint16_t hlen;
40315b355c4fSPyun YongHyeon 	uint32_t poff;
4032ca3f1187SPyun YongHyeon 
4033ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
4034ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
4035ca3f1187SPyun YongHyeon 		n = m_dup(m, M_DONTWAIT);
4036ca3f1187SPyun YongHyeon 		m_freem(m);
4037ca3f1187SPyun YongHyeon 		if (n == NULL)
4038ca3f1187SPyun YongHyeon 			return (NULL);
4039ca3f1187SPyun YongHyeon 		m = n;
4040ca3f1187SPyun YongHyeon 	}
40415b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
4042ca3f1187SPyun YongHyeon 	if (m == NULL)
4043ca3f1187SPyun YongHyeon 		return (NULL);
40445b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
40455b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
4046ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
4047ca3f1187SPyun YongHyeon 	if (m == NULL)
4048ca3f1187SPyun YongHyeon 		return (NULL);
4049ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
40505b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
4051ca3f1187SPyun YongHyeon 	if (m == NULL)
4052ca3f1187SPyun YongHyeon 		return (NULL);
4053ca3f1187SPyun YongHyeon 	/*
4054ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
4055ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
4056ca3f1187SPyun YongHyeon 	 */
4057ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
4058ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
4059ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
4060ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
4061ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
4062ca3f1187SPyun YongHyeon 	/*
4063ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
4064ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
4065ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
4066ca3f1187SPyun YongHyeon 	 * we only support hardware based TSO which is applicable for
4067ca3f1187SPyun YongHyeon 	 * BCM5755 or newer controllers. Hardware based TSO uses 11
4068ca3f1187SPyun YongHyeon 	 * bits to store MSS and upper 5 bits are used to store IP/TCP
4069ca3f1187SPyun YongHyeon 	 * header length(including IP/TCP options). The header length
4070ca3f1187SPyun YongHyeon 	 * is expressed as 32 bits unit.
4071ca3f1187SPyun YongHyeon 	 */
4072ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
4073ca3f1187SPyun YongHyeon 	*mss |= (hlen << 11);
4074ca3f1187SPyun YongHyeon 	return (m);
4075ca3f1187SPyun YongHyeon }
4076ca3f1187SPyun YongHyeon 
4077d375e524SGleb Smirnoff /*
407895d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
407995d67482SBill Paul  * pointers to descriptors.
408095d67482SBill Paul  */
408195d67482SBill Paul static int
4082676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
408395d67482SBill Paul {
40847e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
4085f41ac2beSBill Paul 	bus_dmamap_t		map;
4086676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
4087676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
40887e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
4089ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
40907e27542aSGleb Smirnoff 	int			nsegs, i, error;
409195d67482SBill Paul 
40926909dc43SGleb Smirnoff 	csum_flags = 0;
4093ca3f1187SPyun YongHyeon 	mss = 0;
4094ca3f1187SPyun YongHyeon 	vlan_tag = 0;
4095ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
4096ca3f1187SPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss);
4097ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
4098ca3f1187SPyun YongHyeon 			return (ENOBUFS);
4099ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
4100ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
410135f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
41026909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
41036909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
41046909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
41056909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
41066909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
41076909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
41086909dc43SGleb Smirnoff 				m_freem(m);
41096909dc43SGleb Smirnoff 				*m_head = NULL;
41106909dc43SGleb Smirnoff 				return (error);
41116909dc43SGleb Smirnoff 			}
41126909dc43SGleb Smirnoff 		}
41136909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
41146909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
41156909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
41166909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
41176909dc43SGleb Smirnoff 	}
41186909dc43SGleb Smirnoff 
4119d94f2b85SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0 &&
4120beaa2ae1SPyun YongHyeon 	    sc->bge_forced_collapse > 0 &&
4121beaa2ae1SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
4122d94f2b85SPyun YongHyeon 		/*
4123d94f2b85SPyun YongHyeon 		 * Forcedly collapse mbuf chains to overcome hardware
4124d94f2b85SPyun YongHyeon 		 * limitation which only support a single outstanding
4125d94f2b85SPyun YongHyeon 		 * DMA read operation.
4126d94f2b85SPyun YongHyeon 		 */
4127beaa2ae1SPyun YongHyeon 		if (sc->bge_forced_collapse == 1)
4128d94f2b85SPyun YongHyeon 			m = m_defrag(m, M_DONTWAIT);
4129d94f2b85SPyun YongHyeon 		else
4130beaa2ae1SPyun YongHyeon 			m = m_collapse(m, M_DONTWAIT, sc->bge_forced_collapse);
4131261f04d6SPyun YongHyeon 		if (m == NULL)
4132261f04d6SPyun YongHyeon 			m = *m_head;
4133d94f2b85SPyun YongHyeon 		*m_head = m;
4134d94f2b85SPyun YongHyeon 	}
4135d94f2b85SPyun YongHyeon 
41367e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
41370ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
4138676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
41397e27542aSGleb Smirnoff 	if (error == EFBIG) {
41404eee14cbSMarius Strobl 		m = m_collapse(m, M_DONTWAIT, BGE_NSEG_NEW);
4141676ad2c9SGleb Smirnoff 		if (m == NULL) {
4142676ad2c9SGleb Smirnoff 			m_freem(*m_head);
4143676ad2c9SGleb Smirnoff 			*m_head = NULL;
41447e27542aSGleb Smirnoff 			return (ENOBUFS);
41457e27542aSGleb Smirnoff 		}
4146676ad2c9SGleb Smirnoff 		*m_head = m;
41470ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
41480ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
4149676ad2c9SGleb Smirnoff 		if (error) {
4150676ad2c9SGleb Smirnoff 			m_freem(m);
4151676ad2c9SGleb Smirnoff 			*m_head = NULL;
41527e27542aSGleb Smirnoff 			return (error);
41537e27542aSGleb Smirnoff 		}
4154676ad2c9SGleb Smirnoff 	} else if (error != 0)
4155676ad2c9SGleb Smirnoff 		return (error);
41567e27542aSGleb Smirnoff 
4157167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
4158167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
41590ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
416095d67482SBill Paul 		return (ENOBUFS);
41617e27542aSGleb Smirnoff 	}
41627e27542aSGleb Smirnoff 
41630ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
4164e65bed95SPyun YongHyeon 
4165ca3f1187SPyun YongHyeon #if __FreeBSD_version > 700022
4166ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
4167ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
4168ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
4169ca3f1187SPyun YongHyeon 	}
4170ca3f1187SPyun YongHyeon #else
4171ca3f1187SPyun YongHyeon 	{
4172ca3f1187SPyun YongHyeon 		struct m_tag		*mtag;
4173ca3f1187SPyun YongHyeon 
4174ca3f1187SPyun YongHyeon 		if ((mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m)) != NULL) {
4175ca3f1187SPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
4176ca3f1187SPyun YongHyeon 			vlan_tag = VLAN_TAG_VALUE(mtag);
4177ca3f1187SPyun YongHyeon 		}
4178ca3f1187SPyun YongHyeon 	}
4179ca3f1187SPyun YongHyeon #endif
41807e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
41817e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
41827e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
41837e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
41847e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
41857e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
4186ca3f1187SPyun YongHyeon 		d->bge_vlan_tag = vlan_tag;
4187ca3f1187SPyun YongHyeon 		d->bge_mss = mss;
41887e27542aSGleb Smirnoff 		if (i == nsegs - 1)
41897e27542aSGleb Smirnoff 			break;
41907e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
41917e27542aSGleb Smirnoff 	}
41927e27542aSGleb Smirnoff 
41937e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
41947e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
4195676ad2c9SGleb Smirnoff 
4196f41ac2beSBill Paul 	/*
4197f41ac2beSBill Paul 	 * Insure that the map for this transmission
4198f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
4199f41ac2beSBill Paul 	 * in this chain.
4200f41ac2beSBill Paul 	 */
42017e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
42027e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
4203676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
42047e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
420595d67482SBill Paul 
42067e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
42077e27542aSGleb Smirnoff 	*txidx = idx;
420895d67482SBill Paul 
420995d67482SBill Paul 	return (0);
421095d67482SBill Paul }
421195d67482SBill Paul 
421295d67482SBill Paul /*
421395d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
421495d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
421595d67482SBill Paul  */
421695d67482SBill Paul static void
42173f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
421895d67482SBill Paul {
421995d67482SBill Paul 	struct bge_softc *sc;
4220167fdb62SPyun YongHyeon 	struct mbuf *m_head;
422114bbd30fSGleb Smirnoff 	uint32_t prodidx;
4222167fdb62SPyun YongHyeon 	int count;
422395d67482SBill Paul 
422495d67482SBill Paul 	sc = ifp->if_softc;
4225167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
422695d67482SBill Paul 
4227167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
4228167fdb62SPyun YongHyeon 	    (ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
4229167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
423095d67482SBill Paul 		return;
423195d67482SBill Paul 
423214bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
423395d67482SBill Paul 
4234167fdb62SPyun YongHyeon 	for (count = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
4235167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
4236167fdb62SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
4237167fdb62SPyun YongHyeon 			break;
4238167fdb62SPyun YongHyeon 		}
42394d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
424095d67482SBill Paul 		if (m_head == NULL)
424195d67482SBill Paul 			break;
424295d67482SBill Paul 
424395d67482SBill Paul 		/*
424495d67482SBill Paul 		 * XXX
4245b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
4246b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
4247b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
4248b874fdd4SYaroslav Tykhiy 		 *
4249b874fdd4SYaroslav Tykhiy 		 * XXX
425095d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
425195d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
425295d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
425395d67482SBill Paul 		 * chain at once.
425495d67482SBill Paul 		 * (paranoia -- may not actually be needed)
425595d67482SBill Paul 		 */
425695d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
425795d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
425895d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
425995d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
42604d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
426113f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
426295d67482SBill Paul 				break;
426395d67482SBill Paul 			}
426495d67482SBill Paul 		}
426595d67482SBill Paul 
426695d67482SBill Paul 		/*
426795d67482SBill Paul 		 * Pack the data into the transmit ring. If we
426895d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
426995d67482SBill Paul 		 * for the NIC to drain the ring.
427095d67482SBill Paul 		 */
4271676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
4272676ad2c9SGleb Smirnoff 			if (m_head == NULL)
4273676ad2c9SGleb Smirnoff 				break;
42744d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
427513f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
427695d67482SBill Paul 			break;
427795d67482SBill Paul 		}
4278303a718cSDag-Erling Smørgrav 		++count;
427995d67482SBill Paul 
428095d67482SBill Paul 		/*
428195d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
428295d67482SBill Paul 		 * to him.
428395d67482SBill Paul 		 */
42844e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
428545ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
42864e35d186SJung-uk Kim #else
42874e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
42884e35d186SJung-uk Kim #endif
428995d67482SBill Paul 	}
429095d67482SBill Paul 
4291167fdb62SPyun YongHyeon 	if (count > 0) {
4292aa94f333SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
42935c1da2faSPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
42943f74909aSGleb Smirnoff 		/* Transmit. */
429538cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
42963927098fSPaul Saab 		/* 5700 b2 errata */
4297e0ced696SPaul Saab 		if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
429838cc658fSJohn Baldwin 			bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
429995d67482SBill Paul 
430014bbd30fSGleb Smirnoff 		sc->bge_tx_prodidx = prodidx;
430114bbd30fSGleb Smirnoff 
430295d67482SBill Paul 		/*
430395d67482SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
430495d67482SBill Paul 		 */
4305b74e67fbSGleb Smirnoff 		sc->bge_timer = 5;
430695d67482SBill Paul 	}
4307167fdb62SPyun YongHyeon }
430895d67482SBill Paul 
43090f9bd73bSSam Leffler /*
43100f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
43110f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
43120f9bd73bSSam Leffler  */
431395d67482SBill Paul static void
43143f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
431595d67482SBill Paul {
43160f9bd73bSSam Leffler 	struct bge_softc *sc;
43170f9bd73bSSam Leffler 
43180f9bd73bSSam Leffler 	sc = ifp->if_softc;
43190f9bd73bSSam Leffler 	BGE_LOCK(sc);
43200f9bd73bSSam Leffler 	bge_start_locked(ifp);
43210f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
43220f9bd73bSSam Leffler }
43230f9bd73bSSam Leffler 
43240f9bd73bSSam Leffler static void
43253f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
43260f9bd73bSSam Leffler {
432795d67482SBill Paul 	struct ifnet *ifp;
43283f74909aSGleb Smirnoff 	uint16_t *m;
432995d67482SBill Paul 
43300f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
433195d67482SBill Paul 
4332fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
433395d67482SBill Paul 
433413f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
433595d67482SBill Paul 		return;
433695d67482SBill Paul 
433795d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
433895d67482SBill Paul 	bge_stop(sc);
43398cb1383cSDoug Ambrisko 
43408cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
43418cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
434295d67482SBill Paul 	bge_reset(sc);
43438cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
43448cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
43458cb1383cSDoug Ambrisko 
434695d67482SBill Paul 	bge_chipinit(sc);
434795d67482SBill Paul 
434895d67482SBill Paul 	/*
434995d67482SBill Paul 	 * Init the various state machines, ring
435095d67482SBill Paul 	 * control blocks and firmware.
435195d67482SBill Paul 	 */
435295d67482SBill Paul 	if (bge_blockinit(sc)) {
4353fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
435495d67482SBill Paul 		return;
435595d67482SBill Paul 	}
435695d67482SBill Paul 
4357fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
435895d67482SBill Paul 
435995d67482SBill Paul 	/* Specify MTU. */
436095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
4361cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
4362cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
436395d67482SBill Paul 
436495d67482SBill Paul 	/* Load our MAC address. */
43653f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
436695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
436795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
436895d67482SBill Paul 
43693e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
43703e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
437195d67482SBill Paul 
437295d67482SBill Paul 	/* Program multicast filter. */
437395d67482SBill Paul 	bge_setmulti(sc);
437495d67482SBill Paul 
4375cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
4376cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
4377cb2eacc7SYaroslav Tykhiy 
437835f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
437935f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
438035f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
438135f945cdSPyun YongHyeon 	else
438235f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
438335f945cdSPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_TXCSUM &&
438435f945cdSPyun YongHyeon 	    ifp->if_capenable & IFCAP_TXCSUM) {
438535f945cdSPyun YongHyeon 		ifp->if_hwassist &= ~(BGE_CSUM_FEATURES | CSUM_UDP);
438635f945cdSPyun YongHyeon 		ifp->if_hwassist |= sc->bge_csum_features;
438735f945cdSPyun YongHyeon 	}
438835f945cdSPyun YongHyeon 
438995d67482SBill Paul 	/* Init RX ring. */
43903ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
43913ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
43923ee5d7daSPyun YongHyeon 		bge_stop(sc);
43933ee5d7daSPyun YongHyeon 		return;
43943ee5d7daSPyun YongHyeon 	}
439595d67482SBill Paul 
43960434d1b8SBill Paul 	/*
43970434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
43980434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
43990434d1b8SBill Paul 	 * entry of the ring.
44000434d1b8SBill Paul 	 */
44010434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
44023f74909aSGleb Smirnoff 		uint32_t		v, i;
44030434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
44040434d1b8SBill Paul 			DELAY(20);
44050434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
44060434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
44070434d1b8SBill Paul 				break;
44080434d1b8SBill Paul 		}
44090434d1b8SBill Paul 		if (i == 10)
4410fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
4411fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
44120434d1b8SBill Paul 	}
44130434d1b8SBill Paul 
441495d67482SBill Paul 	/* Init jumbo RX ring. */
4415c215fd77SPyun YongHyeon 	if (ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
4416c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN)) {
44173ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
4418333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
4419b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
44203ee5d7daSPyun YongHyeon 			bge_stop(sc);
44213ee5d7daSPyun YongHyeon 			return;
44223ee5d7daSPyun YongHyeon 		}
44233ee5d7daSPyun YongHyeon 	}
442495d67482SBill Paul 
44253f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
442695d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
442795d67482SBill Paul 
44287e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
44297e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
44307e6e2507SJung-uk Kim 
443195d67482SBill Paul 	/* Init TX ring. */
443295d67482SBill Paul 	bge_init_tx_ring(sc);
443395d67482SBill Paul 
44343f74909aSGleb Smirnoff 	/* Turn on transmitter. */
443595d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
443695d67482SBill Paul 
44373f74909aSGleb Smirnoff 	/* Turn on receiver. */
443895d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
443995d67482SBill Paul 
4440dedcdf57SPyun YongHyeon 	/*
4441dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
4442dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
4443dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
4444dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
4445dedcdf57SPyun YongHyeon 	 */
4446dedcdf57SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
4447dedcdf57SPyun YongHyeon 
44482280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
44492280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
44502280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
44512280c16bSPyun YongHyeon 
445295d67482SBill Paul 	/* Tell firmware we're alive. */
445395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
445495d67482SBill Paul 
445575719184SGleb Smirnoff #ifdef DEVICE_POLLING
445675719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
445775719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
445875719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
445975719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
446038cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
446175719184SGleb Smirnoff 	} else
446275719184SGleb Smirnoff #endif
446375719184SGleb Smirnoff 
446495d67482SBill Paul 	/* Enable host interrupts. */
446575719184SGleb Smirnoff 	{
446695d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
446795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
446838cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
446975719184SGleb Smirnoff 	}
447095d67482SBill Paul 
447167d5e043SOleg Bulyzhin 	bge_ifmedia_upd_locked(ifp);
447295d67482SBill Paul 
447313f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
447413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
447595d67482SBill Paul 
44760f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
44770f9bd73bSSam Leffler }
44780f9bd73bSSam Leffler 
44790f9bd73bSSam Leffler static void
44803f74909aSGleb Smirnoff bge_init(void *xsc)
44810f9bd73bSSam Leffler {
44820f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
44830f9bd73bSSam Leffler 
44840f9bd73bSSam Leffler 	BGE_LOCK(sc);
44850f9bd73bSSam Leffler 	bge_init_locked(sc);
44860f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
448795d67482SBill Paul }
448895d67482SBill Paul 
448995d67482SBill Paul /*
449095d67482SBill Paul  * Set media options.
449195d67482SBill Paul  */
449295d67482SBill Paul static int
44933f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
449495d67482SBill Paul {
449567d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
449667d5e043SOleg Bulyzhin 	int res;
449767d5e043SOleg Bulyzhin 
449867d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
449967d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
450067d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
450167d5e043SOleg Bulyzhin 
450267d5e043SOleg Bulyzhin 	return (res);
450367d5e043SOleg Bulyzhin }
450467d5e043SOleg Bulyzhin 
450567d5e043SOleg Bulyzhin static int
450667d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
450767d5e043SOleg Bulyzhin {
450867d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
450995d67482SBill Paul 	struct mii_data *mii;
45104f09c4c7SMarius Strobl 	struct mii_softc *miisc;
451195d67482SBill Paul 	struct ifmedia *ifm;
451295d67482SBill Paul 
451367d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
451467d5e043SOleg Bulyzhin 
451595d67482SBill Paul 	ifm = &sc->bge_ifmedia;
451695d67482SBill Paul 
451795d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
4518652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
451995d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
452095d67482SBill Paul 			return (EINVAL);
452195d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
452295d67482SBill Paul 		case IFM_AUTO:
4523ff50922bSDoug White 			/*
4524ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
4525ff50922bSDoug White 			 * mechanism for programming the autoneg
4526ff50922bSDoug White 			 * advertisement registers in TBI mode.
4527ff50922bSDoug White 			 */
45280f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4529ff50922bSDoug White 				uint32_t sgdig;
45300f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
45310f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
4532ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
4533ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
4534ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
4535ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
4536ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
4537ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
4538ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
4539ff50922bSDoug White 					DELAY(5);
4540ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
4541ff50922bSDoug White 				}
45420f89fde2SJung-uk Kim 			}
454395d67482SBill Paul 			break;
454495d67482SBill Paul 		case IFM_1000_SX:
454595d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
454695d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
454795d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
454895d67482SBill Paul 			} else {
454995d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
455095d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
455195d67482SBill Paul 			}
455295d67482SBill Paul 			break;
455395d67482SBill Paul 		default:
455495d67482SBill Paul 			return (EINVAL);
455595d67482SBill Paul 		}
455695d67482SBill Paul 		return (0);
455795d67482SBill Paul 	}
455895d67482SBill Paul 
45591493e883SOleg Bulyzhin 	sc->bge_link_evt++;
456095d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
45614f09c4c7SMarius Strobl 	if (mii->mii_instance)
45624f09c4c7SMarius Strobl 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
456395d67482SBill Paul 			mii_phy_reset(miisc);
456495d67482SBill Paul 	mii_mediachg(mii);
456595d67482SBill Paul 
4566902827f6SBjoern A. Zeeb 	/*
4567902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
4568902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
4569902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
4570902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
4571902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
4572902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
4573902827f6SBjoern A. Zeeb 	 * get an RX intr.
4574902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
4575902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
4576902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
4577902827f6SBjoern A. Zeeb 	 */
45784f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
45794f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
4580902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
45814f0794ffSBjoern A. Zeeb 	else
458263ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
4583902827f6SBjoern A. Zeeb 
458495d67482SBill Paul 	return (0);
458595d67482SBill Paul }
458695d67482SBill Paul 
458795d67482SBill Paul /*
458895d67482SBill Paul  * Report current media status.
458995d67482SBill Paul  */
459095d67482SBill Paul static void
45913f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
459295d67482SBill Paul {
459367d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
459495d67482SBill Paul 	struct mii_data *mii;
459595d67482SBill Paul 
459667d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
459795d67482SBill Paul 
4598652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
459995d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
460095d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
460195d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
460295d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
460395d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
46044c0da0ffSGleb Smirnoff 		else {
46054c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
460667d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
46074c0da0ffSGleb Smirnoff 			return;
46084c0da0ffSGleb Smirnoff 		}
460995d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
461095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
461195d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
461295d67482SBill Paul 		else
461395d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
461467d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
461595d67482SBill Paul 		return;
461695d67482SBill Paul 	}
461795d67482SBill Paul 
461895d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
461995d67482SBill Paul 	mii_pollstat(mii);
462095d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
462195d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
462267d5e043SOleg Bulyzhin 
462367d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
462495d67482SBill Paul }
462595d67482SBill Paul 
462695d67482SBill Paul static int
46273f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
462895d67482SBill Paul {
462995d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
463095d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
463195d67482SBill Paul 	struct mii_data *mii;
4632f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
463395d67482SBill Paul 
463495d67482SBill Paul 	switch (command) {
463595d67482SBill Paul 	case SIOCSIFMTU:
46363a429c8fSPyun YongHyeon 		BGE_LOCK(sc);
46374c0da0ffSGleb Smirnoff 		if (ifr->ifr_mtu < ETHERMIN ||
46384c0da0ffSGleb Smirnoff 		    ((BGE_IS_JUMBO_CAPABLE(sc)) &&
46394c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > BGE_JUMBO_MTU) ||
46404c0da0ffSGleb Smirnoff 		    ((!BGE_IS_JUMBO_CAPABLE(sc)) &&
46414c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > ETHERMTU))
464295d67482SBill Paul 			error = EINVAL;
46434c0da0ffSGleb Smirnoff 		else if (ifp->if_mtu != ifr->ifr_mtu) {
464495d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
46453a429c8fSPyun YongHyeon 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
464613f4c340SRobert Watson 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
46473a429c8fSPyun YongHyeon 				bge_init_locked(sc);
464895d67482SBill Paul 			}
46493a429c8fSPyun YongHyeon 		}
46503a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
465195d67482SBill Paul 		break;
465295d67482SBill Paul 	case SIOCSIFFLAGS:
46530f9bd73bSSam Leffler 		BGE_LOCK(sc);
465495d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
465595d67482SBill Paul 			/*
465695d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
465795d67482SBill Paul 			 * then just use the 'set promisc mode' command
465895d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
465995d67482SBill Paul 			 * a full re-init means reloading the firmware and
466095d67482SBill Paul 			 * waiting for it to start up, which may take a
4661d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
466295d67482SBill Paul 			 */
4663f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4664f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
46653e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
46663e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
4667f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
4668d183af7fSRuslan Ermilov 					bge_setmulti(sc);
466995d67482SBill Paul 			} else
46700f9bd73bSSam Leffler 				bge_init_locked(sc);
467195d67482SBill Paul 		} else {
467213f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
467395d67482SBill Paul 				bge_stop(sc);
467495d67482SBill Paul 			}
467595d67482SBill Paul 		}
467695d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
46770f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
467895d67482SBill Paul 		error = 0;
467995d67482SBill Paul 		break;
468095d67482SBill Paul 	case SIOCADDMULTI:
468195d67482SBill Paul 	case SIOCDELMULTI:
468213f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
46830f9bd73bSSam Leffler 			BGE_LOCK(sc);
468495d67482SBill Paul 			bge_setmulti(sc);
46850f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
468695d67482SBill Paul 			error = 0;
468795d67482SBill Paul 		}
468895d67482SBill Paul 		break;
468995d67482SBill Paul 	case SIOCSIFMEDIA:
469095d67482SBill Paul 	case SIOCGIFMEDIA:
4691652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
469295d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
469395d67482SBill Paul 			    &sc->bge_ifmedia, command);
469495d67482SBill Paul 		} else {
469595d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
469695d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
469795d67482SBill Paul 			    &mii->mii_media, command);
469895d67482SBill Paul 		}
469995d67482SBill Paul 		break;
470095d67482SBill Paul 	case SIOCSIFCAP:
470195d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
470275719184SGleb Smirnoff #ifdef DEVICE_POLLING
470375719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
470475719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
470575719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
470675719184SGleb Smirnoff 				if (error)
470775719184SGleb Smirnoff 					return (error);
470875719184SGleb Smirnoff 				BGE_LOCK(sc);
470975719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
471075719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
471138cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
471275719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
471375719184SGleb Smirnoff 				BGE_UNLOCK(sc);
471475719184SGleb Smirnoff 			} else {
471575719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
471675719184SGleb Smirnoff 				/* Enable interrupt even in error case */
471775719184SGleb Smirnoff 				BGE_LOCK(sc);
471875719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
471975719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
472038cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
472175719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
472275719184SGleb Smirnoff 				BGE_UNLOCK(sc);
472375719184SGleb Smirnoff 			}
472475719184SGleb Smirnoff 		}
472575719184SGleb Smirnoff #endif
4726d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
4727d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
4728d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
4729d8b57f98SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
473035f945cdSPyun YongHyeon 				ifp->if_hwassist |= sc->bge_csum_features;
473195d67482SBill Paul 			else
473235f945cdSPyun YongHyeon 				ifp->if_hwassist &= ~sc->bge_csum_features;
473395d67482SBill Paul 		}
4734cb2eacc7SYaroslav Tykhiy 
4735d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
4736d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
4737d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
4738d8b57f98SPyun YongHyeon 
4739ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
4740ca3f1187SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
4741ca3f1187SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
4742ca3f1187SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
4743ca3f1187SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
4744ca3f1187SPyun YongHyeon 			else
4745ca3f1187SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
4746ca3f1187SPyun YongHyeon 		}
4747ca3f1187SPyun YongHyeon 
4748cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
4749cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
4750cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
4751cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
4752cb2eacc7SYaroslav Tykhiy 		}
4753cb2eacc7SYaroslav Tykhiy 
475404bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
475504bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
475604bde852SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
475704bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
475804bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
4759cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
476004bde852SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
476104bde852SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
4762cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
4763cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
4764cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
476504bde852SPyun YongHyeon 		}
4766cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
4767cb2eacc7SYaroslav Tykhiy 		VLAN_CAPABILITIES(ifp);
4768cb2eacc7SYaroslav Tykhiy #endif
476995d67482SBill Paul 		break;
477095d67482SBill Paul 	default:
4771673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
477295d67482SBill Paul 		break;
477395d67482SBill Paul 	}
477495d67482SBill Paul 
477595d67482SBill Paul 	return (error);
477695d67482SBill Paul }
477795d67482SBill Paul 
477895d67482SBill Paul static void
4779b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
478095d67482SBill Paul {
4781b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
478295d67482SBill Paul 
4783b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
4784b74e67fbSGleb Smirnoff 
4785b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
4786b74e67fbSGleb Smirnoff 		return;
4787b74e67fbSGleb Smirnoff 
4788b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
478995d67482SBill Paul 
4790fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
479195d67482SBill Paul 
479213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
4793426742bfSGleb Smirnoff 	bge_init_locked(sc);
479495d67482SBill Paul 
479595d67482SBill Paul 	ifp->if_oerrors++;
479695d67482SBill Paul }
479795d67482SBill Paul 
479895d67482SBill Paul /*
479995d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
480095d67482SBill Paul  * RX and TX lists.
480195d67482SBill Paul  */
480295d67482SBill Paul static void
48033f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
480495d67482SBill Paul {
480595d67482SBill Paul 	struct ifnet *ifp;
480695d67482SBill Paul 
48070f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
48080f9bd73bSSam Leffler 
4809fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
481095d67482SBill Paul 
48110f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
481295d67482SBill Paul 
481344b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
481444b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
481544b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
481644b63691SBjoern A. Zeeb 
481744b63691SBjoern A. Zeeb 	/*
481844b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
481944b63691SBjoern A. Zeeb 	 */
482044b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
482144b63691SBjoern A. Zeeb 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
482244b63691SBjoern A. Zeeb 
482395d67482SBill Paul 	/*
48243f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
482595d67482SBill Paul 	 */
482695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
482795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
482895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
48297ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
483095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
483195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
483295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
483395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
483495d67482SBill Paul 
483595d67482SBill Paul 	/*
48363f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
483795d67482SBill Paul 	 */
483895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
483995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
484095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
484195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
484295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
48437ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
484495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
484595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
484695d67482SBill Paul 
484795d67482SBill Paul 	/*
484895d67482SBill Paul 	 * Shut down all of the memory managers and related
484995d67482SBill Paul 	 * state machines.
485095d67482SBill Paul 	 */
485195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
485295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
48537ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
485495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
48550c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
485695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
48577ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
485895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
485995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
48600434d1b8SBill Paul 	}
48612280c16bSPyun YongHyeon 	/* Update MAC statistics. */
48622280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
48632280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
486495d67482SBill Paul 
48658cb1383cSDoug Ambrisko 	bge_reset(sc);
48668cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
48678cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
48688cb1383cSDoug Ambrisko 
48698cb1383cSDoug Ambrisko 	/*
48708cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
48718cb1383cSDoug Ambrisko 	 */
48728cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
48738cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
48748cb1383cSDoug Ambrisko 	else
487595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
487695d67482SBill Paul 
487795d67482SBill Paul 	/* Free the RX lists. */
487895d67482SBill Paul 	bge_free_rx_ring_std(sc);
487995d67482SBill Paul 
488095d67482SBill Paul 	/* Free jumbo RX list. */
48814c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
488295d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
488395d67482SBill Paul 
488495d67482SBill Paul 	/* Free TX buffers. */
488595d67482SBill Paul 	bge_free_tx_ring(sc);
488695d67482SBill Paul 
488795d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
488895d67482SBill Paul 
48895dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
48901493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
48911493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
48921493e883SOleg Bulyzhin 	sc->bge_link = 0;
489395d67482SBill Paul 
48941493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
489595d67482SBill Paul }
489695d67482SBill Paul 
489795d67482SBill Paul /*
489895d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
489995d67482SBill Paul  * get confused by errant DMAs when rebooting.
490095d67482SBill Paul  */
4901b6c974e8SWarner Losh static int
49023f74909aSGleb Smirnoff bge_shutdown(device_t dev)
490395d67482SBill Paul {
490495d67482SBill Paul 	struct bge_softc *sc;
490595d67482SBill Paul 
490695d67482SBill Paul 	sc = device_get_softc(dev);
49070f9bd73bSSam Leffler 	BGE_LOCK(sc);
490895d67482SBill Paul 	bge_stop(sc);
490995d67482SBill Paul 	bge_reset(sc);
49100f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
4911b6c974e8SWarner Losh 
4912b6c974e8SWarner Losh 	return (0);
491395d67482SBill Paul }
491414afefa3SPawel Jakub Dawidek 
491514afefa3SPawel Jakub Dawidek static int
491614afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
491714afefa3SPawel Jakub Dawidek {
491814afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
491914afefa3SPawel Jakub Dawidek 
492014afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
492114afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
492214afefa3SPawel Jakub Dawidek 	bge_stop(sc);
492314afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
492414afefa3SPawel Jakub Dawidek 
492514afefa3SPawel Jakub Dawidek 	return (0);
492614afefa3SPawel Jakub Dawidek }
492714afefa3SPawel Jakub Dawidek 
492814afefa3SPawel Jakub Dawidek static int
492914afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
493014afefa3SPawel Jakub Dawidek {
493114afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
493214afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
493314afefa3SPawel Jakub Dawidek 
493414afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
493514afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
493614afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
493714afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
493814afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
493914afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
494014afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
494114afefa3SPawel Jakub Dawidek 	}
494214afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
494314afefa3SPawel Jakub Dawidek 
494414afefa3SPawel Jakub Dawidek 	return (0);
494514afefa3SPawel Jakub Dawidek }
4946dab5cd05SOleg Bulyzhin 
4947dab5cd05SOleg Bulyzhin static void
49483f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
4949dab5cd05SOleg Bulyzhin {
49501f313773SOleg Bulyzhin 	struct mii_data *mii;
49511f313773SOleg Bulyzhin 	uint32_t link, status;
4952dab5cd05SOleg Bulyzhin 
4953dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
49541f313773SOleg Bulyzhin 
49553f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
49567b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
49577b97099dSOleg Bulyzhin 
4958dab5cd05SOleg Bulyzhin 	/*
4959dab5cd05SOleg Bulyzhin 	 * Process link state changes.
4960dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
4961dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
4962dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
4963dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
4964dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
4965dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
4966dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
4967dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
49681f313773SOleg Bulyzhin 	 *
49691f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
49704c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
4971dab5cd05SOleg Bulyzhin 	 */
4972dab5cd05SOleg Bulyzhin 
49731f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
49744c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
4975dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
4976dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
49771f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
49785dda8085SOleg Bulyzhin 			mii_pollstat(mii);
49791f313773SOleg Bulyzhin 			if (!sc->bge_link &&
49801f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
49811f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
49821f313773SOleg Bulyzhin 				sc->bge_link++;
49831f313773SOleg Bulyzhin 				if (bootverbose)
49841f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
49851f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
49861f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
49871f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
49881f313773SOleg Bulyzhin 				sc->bge_link = 0;
49891f313773SOleg Bulyzhin 				if (bootverbose)
49901f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
49911f313773SOleg Bulyzhin 			}
49921f313773SOleg Bulyzhin 
49933f74909aSGleb Smirnoff 			/* Clear the interrupt. */
4994dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
4995dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
4996dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
4997dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
4998dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
4999dab5cd05SOleg Bulyzhin 		}
5000dab5cd05SOleg Bulyzhin 		return;
5001dab5cd05SOleg Bulyzhin 	}
5002dab5cd05SOleg Bulyzhin 
5003652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
50041f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
50057b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
50067b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
50071f313773SOleg Bulyzhin 				sc->bge_link++;
50081f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
50091f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
50101f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
50110c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
50121f313773SOleg Bulyzhin 				if (bootverbose)
50131f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
50143f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
50153f74909aSGleb Smirnoff 				    LINK_STATE_UP);
50167b97099dSOleg Bulyzhin 			}
50171f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
5018dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
50191f313773SOleg Bulyzhin 			if (bootverbose)
50201f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
50217b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
50221f313773SOleg Bulyzhin 		}
5023*6ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
50241f313773SOleg Bulyzhin 		/*
50250c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
50260c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
50270c8aa4eaSJung-uk Kim 		 * PHY link status directly.
50281f313773SOleg Bulyzhin 		 */
50291f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
50301f313773SOleg Bulyzhin 
50311f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
50321f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
50331f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
50345dda8085SOleg Bulyzhin 			mii_pollstat(mii);
50351f313773SOleg Bulyzhin 			if (!sc->bge_link &&
50361f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
50371f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
50381f313773SOleg Bulyzhin 				sc->bge_link++;
50391f313773SOleg Bulyzhin 				if (bootverbose)
50401f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
50411f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
50421f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
50431f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
50441f313773SOleg Bulyzhin 				sc->bge_link = 0;
50451f313773SOleg Bulyzhin 				if (bootverbose)
50461f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
50471f313773SOleg Bulyzhin 			}
50481f313773SOleg Bulyzhin 		}
50490c8aa4eaSJung-uk Kim 	} else {
50500c8aa4eaSJung-uk Kim 		/*
5051*6ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
5052*6ede2cfaSPyun YongHyeon 		 * link status.
50530c8aa4eaSJung-uk Kim 		 */
5054*6ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
5055*6ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
5056*6ede2cfaSPyun YongHyeon 		if (!sc->bge_link && mii->mii_media_status & IFM_ACTIVE &&
5057*6ede2cfaSPyun YongHyeon 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
5058*6ede2cfaSPyun YongHyeon 			bge_miibus_statchg(sc->bge_dev);
5059*6ede2cfaSPyun YongHyeon 			sc->bge_link = 1;
5060*6ede2cfaSPyun YongHyeon 		} else
5061*6ede2cfaSPyun YongHyeon 			sc->bge_link = 0;
5062dab5cd05SOleg Bulyzhin 	}
5063dab5cd05SOleg Bulyzhin 
50643f74909aSGleb Smirnoff 	/* Clear the attention. */
5065dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
5066dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
5067dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
5068dab5cd05SOleg Bulyzhin }
50696f8718a3SScott Long 
50706f8718a3SScott Long static void
50716f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
50726f8718a3SScott Long {
50736f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
50742280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
50757e32f79aSPyun YongHyeon 	char tn[32];
50767e32f79aSPyun YongHyeon 	int unit;
50776f8718a3SScott Long 
50786f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
50796f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
50806f8718a3SScott Long 
50816f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
50826f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
50836f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
50846f8718a3SScott Long 	    "Debug Information");
50856f8718a3SScott Long 
50866f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
50876f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
50886f8718a3SScott Long 	    "Register Read");
50896f8718a3SScott Long 
50906f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
50916f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
50926f8718a3SScott Long 	    "Memory Read");
50936f8718a3SScott Long 
50946f8718a3SScott Long #endif
5095763757b2SScott Long 
50967e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
5097beaa2ae1SPyun YongHyeon 	/*
5098beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
5099beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
5100beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
5101beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
5102beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
5103beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
5104beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
5105beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
5106beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
5107beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
5108beaa2ae1SPyun YongHyeon 	 */
51097e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
51107e32f79aSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.forced_collapse", unit);
51117e32f79aSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_collapse);
5112beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
5113beaa2ae1SPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_collapse, 0,
5114beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
5115beaa2ae1SPyun YongHyeon 	    "forced collapsing");
5116beaa2ae1SPyun YongHyeon 
511735f945cdSPyun YongHyeon 	/*
511835f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
511935f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
512035f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
512135f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
512235f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
512335f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
512435f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
512535f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
512635f945cdSPyun YongHyeon 	 */
512735f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
512835f945cdSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.bge_forced_udpcsum", unit);
512935f945cdSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_udpcsum);
513035f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
513135f945cdSPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_udpcsum, 0,
513235f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
513335f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
513435f945cdSPyun YongHyeon 
5135d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
51362280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
51372280c16bSPyun YongHyeon 	else
51382280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
51392280c16bSPyun YongHyeon }
5140d949071dSJung-uk Kim 
51412280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
51422280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
51432280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
51442280c16bSPyun YongHyeon 	    desc)
51452280c16bSPyun YongHyeon 
51462280c16bSPyun YongHyeon static void
51472280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
51482280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
51492280c16bSPyun YongHyeon {
51502280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
51512280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
51522280c16bSPyun YongHyeon 
51532280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
5154763757b2SScott Long 	    NULL, "BGE Statistics");
5155763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
5156763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
5157763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
5158763757b2SScott Long 	    "FramesDroppedDueToFilters");
5159763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
5160763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
5161763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
5162763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
5163763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
5164763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
516506e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
516606e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
516706e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
516806e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
5169763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
5170763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
5171763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
5172763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
5173763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
5174763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
5175763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
5176763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
5177763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
5178763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
5179763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
5180763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
5181763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
5182763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
5183763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
5184763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
5185763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
5186763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
5187763757b2SScott Long 
5188763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
5189763757b2SScott Long 	    NULL, "BGE RX Statistics");
5190763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
5191763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
51921cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
5193763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
5194763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
5195763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
51961cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
5197763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
5198763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
5199763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
5200763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
5201763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
5202763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
5203763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
5204763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
5205763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
5206763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
5207763757b2SScott Long 	    "xoffPauseFramesReceived");
5208763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
5209763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
5210763757b2SScott Long 	    "ControlFramesReceived");
5211763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
5212763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
5213763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
5214763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
5215763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
5216763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
5217763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
5218763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
5219763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
522006e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
5221763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
522206e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
5223763757b2SScott Long 
5224763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
5225763757b2SScott Long 	    NULL, "BGE TX Statistics");
5226763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
5227763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
52281cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
5229763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
5230763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
5231763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
5232763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
5233763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
5234763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
5235763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
5236763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
5237763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
5238763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
5239763757b2SScott Long 	    "InternalMacTransmitErrors");
5240763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
5241763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
5242763757b2SScott Long 	    "SingleCollisionFrames");
5243763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
5244763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
5245763757b2SScott Long 	    "MultipleCollisionFrames");
5246763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
5247763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
5248763757b2SScott Long 	    "DeferredTransmissions");
5249763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
5250763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
5251763757b2SScott Long 	    "ExcessiveCollisions");
5252763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
525306e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
525406e83c7eSScott Long 	    "LateCollisions");
5255763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
52561cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
5257763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
5258763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
5259763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
5260763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
5261763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
5262763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
5263763757b2SScott Long 	    "CarrierSenseErrors");
5264763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
5265763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
5266763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
5267763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
5268763757b2SScott Long }
5269763757b2SScott Long 
52702280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
52712280c16bSPyun YongHyeon 
52722280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
52732280c16bSPyun YongHyeon 	    SYSCTL_ADD_QUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
52742280c16bSPyun YongHyeon 
52752280c16bSPyun YongHyeon static void
52762280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
52772280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
52782280c16bSPyun YongHyeon {
52792280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
52802280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
52812280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
52822280c16bSPyun YongHyeon 
52832280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
52842280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
52852280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
52862280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
52872280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
52882280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
52892280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
52902280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
52912280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
52922280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
52932280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
52942280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
52952280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
52962280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
52972280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
52982280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
52992280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
53002280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
53012280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
53022280c16bSPyun YongHyeon 
53032280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
53042280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
53052280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
53062280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
53072280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
53082280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
53092280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
53101cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
53112280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
53122280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
53132280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
53142280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
53152280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
53162280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
53172280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
53182280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
53192280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
53202280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
53212280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
53222280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
53232280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
53242280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
53252280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
53262280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
53272280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
53282280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
53292280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
53302280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
53312280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
53322280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
53332280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
53342280c16bSPyun YongHyeon 
53352280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
53362280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
53372280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
53381cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
53392280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
53402280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
53412280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
53422280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
53432280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
53442280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
53452280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
53462280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
53472280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
53482280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
53492280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
53502280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
53512280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
53522280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
53532280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
53542280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
53552280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
53562280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
53572280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
53582280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
53592280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
53601cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
53612280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
53621cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
53632280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
53641cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
53652280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
53662280c16bSPyun YongHyeon }
53672280c16bSPyun YongHyeon 
53682280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
53692280c16bSPyun YongHyeon 
5370763757b2SScott Long static int
5371763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
5372763757b2SScott Long {
5373763757b2SScott Long 	struct bge_softc *sc;
537406e83c7eSScott Long 	uint32_t result;
5375d949071dSJung-uk Kim 	int offset;
5376763757b2SScott Long 
5377763757b2SScott Long 	sc = (struct bge_softc *)arg1;
5378763757b2SScott Long 	offset = arg2;
5379d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
5380d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
5381041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
53826f8718a3SScott Long }
53836f8718a3SScott Long 
53846f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
53856f8718a3SScott Long static int
53866f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
53876f8718a3SScott Long {
53886f8718a3SScott Long 	struct bge_softc *sc;
53896f8718a3SScott Long 	uint16_t *sbdata;
53906f8718a3SScott Long 	int error;
53916f8718a3SScott Long 	int result;
53926f8718a3SScott Long 	int i, j;
53936f8718a3SScott Long 
53946f8718a3SScott Long 	result = -1;
53956f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
53966f8718a3SScott Long 	if (error || (req->newptr == NULL))
53976f8718a3SScott Long 		return (error);
53986f8718a3SScott Long 
53996f8718a3SScott Long 	if (result == 1) {
54006f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
54016f8718a3SScott Long 
54026f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
54036f8718a3SScott Long 		printf("Status Block:\n");
54046f8718a3SScott Long 		for (i = 0x0; i < (BGE_STATUS_BLK_SZ / 4); ) {
54056f8718a3SScott Long 			printf("%06x:", i);
54066f8718a3SScott Long 			for (j = 0; j < 8; j++) {
54076f8718a3SScott Long 				printf(" %04x", sbdata[i]);
54086f8718a3SScott Long 				i += 4;
54096f8718a3SScott Long 			}
54106f8718a3SScott Long 			printf("\n");
54116f8718a3SScott Long 		}
54126f8718a3SScott Long 
54136f8718a3SScott Long 		printf("Registers:\n");
54140c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
54156f8718a3SScott Long 			printf("%06x:", i);
54166f8718a3SScott Long 			for (j = 0; j < 8; j++) {
54176f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
54186f8718a3SScott Long 				i += 4;
54196f8718a3SScott Long 			}
54206f8718a3SScott Long 			printf("\n");
54216f8718a3SScott Long 		}
54226f8718a3SScott Long 
54236f8718a3SScott Long 		printf("Hardware Flags:\n");
5424a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
5425a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
54265345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
54276f8718a3SScott Long 			printf(" - 575X Plus\n");
54285345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
54296f8718a3SScott Long 			printf(" - 5705 Plus\n");
54305345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
54315345bad0SScott Long 			printf(" - 5714 Family\n");
54325345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
54335345bad0SScott Long 			printf(" - 5700 Family\n");
54346f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
54356f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
54366f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
54376f8718a3SScott Long 			printf(" - PCI-X Bus\n");
54386f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
54396f8718a3SScott Long 			printf(" - PCI Express Bus\n");
54407d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
54416f8718a3SScott Long 			printf(" - No 3 LEDs\n");
54426f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
54436f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
54446f8718a3SScott Long 	}
54456f8718a3SScott Long 
54466f8718a3SScott Long 	return (error);
54476f8718a3SScott Long }
54486f8718a3SScott Long 
54496f8718a3SScott Long static int
54506f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
54516f8718a3SScott Long {
54526f8718a3SScott Long 	struct bge_softc *sc;
54536f8718a3SScott Long 	int error;
54546f8718a3SScott Long 	uint16_t result;
54556f8718a3SScott Long 	uint32_t val;
54566f8718a3SScott Long 
54576f8718a3SScott Long 	result = -1;
54586f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
54596f8718a3SScott Long 	if (error || (req->newptr == NULL))
54606f8718a3SScott Long 		return (error);
54616f8718a3SScott Long 
54626f8718a3SScott Long 	if (result < 0x8000) {
54636f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
54646f8718a3SScott Long 		val = CSR_READ_4(sc, result);
54656f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
54666f8718a3SScott Long 	}
54676f8718a3SScott Long 
54686f8718a3SScott Long 	return (error);
54696f8718a3SScott Long }
54706f8718a3SScott Long 
54716f8718a3SScott Long static int
54726f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
54736f8718a3SScott Long {
54746f8718a3SScott Long 	struct bge_softc *sc;
54756f8718a3SScott Long 	int error;
54766f8718a3SScott Long 	uint16_t result;
54776f8718a3SScott Long 	uint32_t val;
54786f8718a3SScott Long 
54796f8718a3SScott Long 	result = -1;
54806f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
54816f8718a3SScott Long 	if (error || (req->newptr == NULL))
54826f8718a3SScott Long 		return (error);
54836f8718a3SScott Long 
54846f8718a3SScott Long 	if (result < 0x8000) {
54856f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
54866f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
54876f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
54886f8718a3SScott Long 	}
54896f8718a3SScott Long 
54906f8718a3SScott Long 	return (error);
54916f8718a3SScott Long }
54926f8718a3SScott Long #endif
549338cc658fSJohn Baldwin 
549438cc658fSJohn Baldwin static int
54955fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
54965fea260fSMarius Strobl {
54975fea260fSMarius Strobl 
54985fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
54995fea260fSMarius Strobl 		return (1);
55005fea260fSMarius Strobl 
55015fea260fSMarius Strobl #ifdef __sparc64__
55025fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
55035fea260fSMarius Strobl 	return (0);
55045fea260fSMarius Strobl #endif
55055fea260fSMarius Strobl 	return (1);
55065fea260fSMarius Strobl }
55075fea260fSMarius Strobl 
55085fea260fSMarius Strobl static int
550938cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
551038cc658fSJohn Baldwin {
551138cc658fSJohn Baldwin 	uint32_t mac_addr;
551238cc658fSJohn Baldwin 
551338cc658fSJohn Baldwin 	mac_addr = bge_readmem_ind(sc, 0x0c14);
551438cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
551538cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
551638cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
551738cc658fSJohn Baldwin 		mac_addr = bge_readmem_ind(sc, 0x0c18);
551838cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
551938cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
552038cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
552138cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
55225fea260fSMarius Strobl 		return (0);
552338cc658fSJohn Baldwin 	}
55245fea260fSMarius Strobl 	return (1);
552538cc658fSJohn Baldwin }
552638cc658fSJohn Baldwin 
552738cc658fSJohn Baldwin static int
552838cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
552938cc658fSJohn Baldwin {
553038cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
553138cc658fSJohn Baldwin 
553238cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
553338cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
553438cc658fSJohn Baldwin 
55355fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
55365fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
553738cc658fSJohn Baldwin }
553838cc658fSJohn Baldwin 
553938cc658fSJohn Baldwin static int
554038cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
554138cc658fSJohn Baldwin {
554238cc658fSJohn Baldwin 
55435fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
55445fea260fSMarius Strobl 		return (1);
55455fea260fSMarius Strobl 
55465fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
55475fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
554838cc658fSJohn Baldwin }
554938cc658fSJohn Baldwin 
555038cc658fSJohn Baldwin static int
555138cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
555238cc658fSJohn Baldwin {
555338cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
555438cc658fSJohn Baldwin 		/* NOTE: Order is critical */
55555fea260fSMarius Strobl 		bge_get_eaddr_fw,
555638cc658fSJohn Baldwin 		bge_get_eaddr_mem,
555738cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
555838cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
555938cc658fSJohn Baldwin 		NULL
556038cc658fSJohn Baldwin 	};
556138cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
556238cc658fSJohn Baldwin 
556338cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
556438cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
556538cc658fSJohn Baldwin 			break;
556638cc658fSJohn Baldwin 	}
556738cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
556838cc658fSJohn Baldwin }
5569