xref: /freebsd/sys/dev/bge/if_bge.c (revision d9fc28e40d77eb85d407ce4b01298dd9b4ffd46a)
1098ca2bdSWarner Losh /*-
295d67482SBill Paul  * Copyright (c) 2001 Wind River Systems
395d67482SBill Paul  * Copyright (c) 1997, 1998, 1999, 2001
495d67482SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
595d67482SBill Paul  *
695d67482SBill Paul  * Redistribution and use in source and binary forms, with or without
795d67482SBill Paul  * modification, are permitted provided that the following conditions
895d67482SBill Paul  * are met:
995d67482SBill Paul  * 1. Redistributions of source code must retain the above copyright
1095d67482SBill Paul  *    notice, this list of conditions and the following disclaimer.
1195d67482SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1295d67482SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1395d67482SBill Paul  *    documentation and/or other materials provided with the distribution.
1495d67482SBill Paul  * 3. All advertising materials mentioning features or use of this software
1595d67482SBill Paul  *    must display the following acknowledgement:
1695d67482SBill Paul  *	This product includes software developed by Bill Paul.
1795d67482SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1895d67482SBill Paul  *    may be used to endorse or promote products derived from this software
1995d67482SBill Paul  *    without specific prior written permission.
2095d67482SBill Paul  *
2195d67482SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2295d67482SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2395d67482SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2495d67482SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2595d67482SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2695d67482SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2795d67482SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2895d67482SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2995d67482SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3095d67482SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3195d67482SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3295d67482SBill Paul  */
3395d67482SBill Paul 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
3795d67482SBill Paul /*
3895d67482SBill Paul  * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
3995d67482SBill Paul  *
4095d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
4195d67482SBill Paul  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
4222a4ecedSMarius Strobl  * MAC chips. The BCM5700, sometimes referred to as the Tigon III, has
4395d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4495d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4595d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
4695d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
4795d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
4895d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
4995d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
5095d67482SBill Paul  * into the driver.
5195d67482SBill Paul  *
5295d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5395d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5495d67482SBill Paul  *
5595d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
5698b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
5795d67482SBill Paul  * does not support external SSRAM.
5895d67482SBill Paul  *
5995d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
6095d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
6195d67482SBill Paul  *
6295d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6395d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6495d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6595d67482SBill Paul  * result, this driver does not implement any support for the mini RX
6695d67482SBill Paul  * ring.
6795d67482SBill Paul  */
6895d67482SBill Paul 
6975719184SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
7075719184SGleb Smirnoff #include "opt_device_polling.h"
7175719184SGleb Smirnoff #endif
7275719184SGleb Smirnoff 
7395d67482SBill Paul #include <sys/param.h>
74f41ac2beSBill Paul #include <sys/endian.h>
7595d67482SBill Paul #include <sys/systm.h>
7695d67482SBill Paul #include <sys/sockio.h>
7795d67482SBill Paul #include <sys/mbuf.h>
7895d67482SBill Paul #include <sys/malloc.h>
7995d67482SBill Paul #include <sys/kernel.h>
80fe12f24bSPoul-Henning Kamp #include <sys/module.h>
8195d67482SBill Paul #include <sys/socket.h>
82f1a7e6d5SScott Long #include <sys/sysctl.h>
83dfe0df9aSPyun YongHyeon #include <sys/taskqueue.h>
8495d67482SBill Paul 
8595d67482SBill Paul #include <net/if.h>
8695d67482SBill Paul #include <net/if_arp.h>
8795d67482SBill Paul #include <net/ethernet.h>
8895d67482SBill Paul #include <net/if_dl.h>
8995d67482SBill Paul #include <net/if_media.h>
9095d67482SBill Paul 
9195d67482SBill Paul #include <net/bpf.h>
9295d67482SBill Paul 
9395d67482SBill Paul #include <net/if_types.h>
9495d67482SBill Paul #include <net/if_vlan_var.h>
9595d67482SBill Paul 
9695d67482SBill Paul #include <netinet/in_systm.h>
9795d67482SBill Paul #include <netinet/in.h>
9895d67482SBill Paul #include <netinet/ip.h>
99ca3f1187SPyun YongHyeon #include <netinet/tcp.h>
10095d67482SBill Paul 
10195d67482SBill Paul #include <machine/bus.h>
10295d67482SBill Paul #include <machine/resource.h>
10395d67482SBill Paul #include <sys/bus.h>
10495d67482SBill Paul #include <sys/rman.h>
10595d67482SBill Paul 
10695d67482SBill Paul #include <dev/mii/mii.h>
10795d67482SBill Paul #include <dev/mii/miivar.h>
1082d3ce713SDavid E. O'Brien #include "miidevs.h"
10995d67482SBill Paul #include <dev/mii/brgphyreg.h>
11095d67482SBill Paul 
11108013fd3SMarius Strobl #ifdef __sparc64__
11208013fd3SMarius Strobl #include <dev/ofw/ofw_bus.h>
11308013fd3SMarius Strobl #include <dev/ofw/openfirm.h>
11408013fd3SMarius Strobl #include <machine/ofw_machdep.h>
11508013fd3SMarius Strobl #include <machine/ver.h>
11608013fd3SMarius Strobl #endif
11708013fd3SMarius Strobl 
1184fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1194fbd232cSWarner Losh #include <dev/pci/pcivar.h>
12095d67482SBill Paul 
12195d67482SBill Paul #include <dev/bge/if_bgereg.h>
12295d67482SBill Paul 
12335f945cdSPyun YongHyeon #define	BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP)
124d375e524SGleb Smirnoff #define	ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
12595d67482SBill Paul 
126f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
127f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
12895d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12995d67482SBill Paul 
1307b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
13195d67482SBill Paul #include "miibus_if.h"
13295d67482SBill Paul 
13395d67482SBill Paul /*
13495d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
13595d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
13695d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13795d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13895d67482SBill Paul  */
139852c67f9SMarius Strobl static const struct bge_type {
1404c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1414c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
142978f2704SMarius Strobl } const bge_devs[] = {
1434c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5700 },
1444c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5701 },
14595d67482SBill Paul 
1464c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1000 },
1474c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1002 },
1484c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC9100 },
1494c0da0ffSGleb Smirnoff 
1504c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	APPLE_DEVICE_BCM5701 },
1514c0da0ffSGleb Smirnoff 
1524c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5700 },
1534c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5701 },
1544c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702 },
1554c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702_ALT },
1564c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702X },
1574c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703 },
1584c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703_ALT },
1594c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703X },
1604c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704C },
1614c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S },
1624c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S_ALT },
1634c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705 },
1644c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705F },
1654c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705K },
1664c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M },
1674c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M_ALT },
1684c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714C },
1694c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714S },
1704c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715 },
1714c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715S },
1721108273aSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5717 },
1731108273aSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5718 },
174bbe2ca75SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5719 },
1754c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1764c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
177effef978SRemko Lodder 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5722 },
178a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5723 },
1794c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1804c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1814c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1824c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1834c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1844c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1854c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1864c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1874c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1884c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1899e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1909e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1919e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1929e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
193f7d1b2ebSXin LI 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5756 },
194a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761 },
195a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761E },
196a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761S },
197a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761SE },
198a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5764 },
1994c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
2004c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
2014c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
2024c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
203a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5784 },
204a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785F },
205a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785G },
2069e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
2079e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
208a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787F },
2099e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
2104c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
2114c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
2124c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
2134c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
2144c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
21538cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906 },
21638cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906M },
217a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57760 },
218b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57761 },
219b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57765 },
220a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57780 },
221b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57781 },
222b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57785 },
223a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57788 },
224a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57790 },
225b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57791 },
226b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57795 },
2274c0da0ffSGleb Smirnoff 
2284c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
2294c0da0ffSGleb Smirnoff 
2304c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
2314c0da0ffSGleb Smirnoff 
232a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE4 },
233a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE5 },
234a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PP250450 },
235a5779553SStanislav Sedov 
2364c0da0ffSGleb Smirnoff 	{ 0, 0 }
23795d67482SBill Paul };
23895d67482SBill Paul 
2394c0da0ffSGleb Smirnoff static const struct bge_vendor {
2404c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2414c0da0ffSGleb Smirnoff 	const char	*v_name;
242978f2704SMarius Strobl } const bge_vendors[] = {
2434c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2444c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2454c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2464c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2474c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2484c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
249a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	"Fujitsu" },
2504c0da0ffSGleb Smirnoff 
2514c0da0ffSGleb Smirnoff 	{ 0, NULL }
2524c0da0ffSGleb Smirnoff };
2534c0da0ffSGleb Smirnoff 
2544c0da0ffSGleb Smirnoff static const struct bge_revision {
2554c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2564c0da0ffSGleb Smirnoff 	const char	*br_name;
257978f2704SMarius Strobl } const bge_revisions[] = {
2584c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2594c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2604c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2614c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2624c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2634c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2644c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2654c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2664c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2674c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2684c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2694c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2704c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2714c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2724c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2734c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2749e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2754c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2764c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2774c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2784c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2794c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2804c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2814c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2824c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2834c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2844c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2854c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2864c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2874c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2884c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2894c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2904c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
29142787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2924c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2934c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2944c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
2954c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
2964c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
2974c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
2984c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
2994c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
3000c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5715_A3,	"BCM5715 A3" },
3011108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_A0,	"BCM5717 A0" },
3021108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_B0,	"BCM5717 B0" },
303bbe2ca75SPyun YongHyeon 	{ BGE_CHIPID_BCM5719_A0,	"BCM5719 A0" },
30450515680SPyun YongHyeon 	{ BGE_CHIPID_BCM5720_A0,	"BCM5720 A0" },
3050c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A0,	"BCM5755 A0" },
3060c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A1,	"BCM5755 A1" },
3070c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A2,	"BCM5755 A2" },
308bcc20328SJohn Baldwin 	{ BGE_CHIPID_BCM5722_A0,	"BCM5722 A0" },
309a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A0,	"BCM5761 A0" },
310a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A1,	"BCM5761 A1" },
311a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A0,	"BCM5784 A0" },
312a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A1,	"BCM5784 A1" },
31381179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3146f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
3156f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
3166f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
31738cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A1,	"BCM5906 A1" },
31838cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A2,	"BCM5906 A2" },
319b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_A0,	"BCM57765 A0" },
320b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_B0,	"BCM57765 B0" },
321a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A0,	"BCM57780 A0" },
322a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A1,	"BCM57780 A1" },
3234c0da0ffSGleb Smirnoff 
3244c0da0ffSGleb Smirnoff 	{ 0, NULL }
3254c0da0ffSGleb Smirnoff };
3264c0da0ffSGleb Smirnoff 
3274c0da0ffSGleb Smirnoff /*
3284c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
3294c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
3304c0da0ffSGleb Smirnoff  */
331978f2704SMarius Strobl static const struct bge_revision const bge_majorrevs[] = {
3329e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
3339e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
3349e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
3359e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
3369e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
3379e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
3389e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
3399e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
3409e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
3419e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
3429e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
343a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5761,		"unknown BCM5761" },
344a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5784,		"unknown BCM5784" },
345a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5785,		"unknown BCM5785" },
34681179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3476f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
34838cc658fSJohn Baldwin 	{ BGE_ASICREV_BCM5906,		"unknown BCM5906" },
349b4a256acSPyun YongHyeon 	{ BGE_ASICREV_BCM57765,		"unknown BCM57765" },
350a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM57780,		"unknown BCM57780" },
3511108273aSPyun YongHyeon 	{ BGE_ASICREV_BCM5717,		"unknown BCM5717" },
352bbe2ca75SPyun YongHyeon 	{ BGE_ASICREV_BCM5719,		"unknown BCM5719" },
35350515680SPyun YongHyeon 	{ BGE_ASICREV_BCM5720,		"unknown BCM5720" },
3544c0da0ffSGleb Smirnoff 
3554c0da0ffSGleb Smirnoff 	{ 0, NULL }
3564c0da0ffSGleb Smirnoff };
3574c0da0ffSGleb Smirnoff 
3580c8aa4eaSJung-uk Kim #define	BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
3590c8aa4eaSJung-uk Kim #define	BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
3600c8aa4eaSJung-uk Kim #define	BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
3610c8aa4eaSJung-uk Kim #define	BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
3620c8aa4eaSJung-uk Kim #define	BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
363a5779553SStanislav Sedov #define	BGE_IS_5755_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5755_PLUS)
3641108273aSPyun YongHyeon #define	BGE_IS_5717_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5717_PLUS)
3654c0da0ffSGleb Smirnoff 
3664c0da0ffSGleb Smirnoff const struct bge_revision * bge_lookup_rev(uint32_t);
3674c0da0ffSGleb Smirnoff const struct bge_vendor * bge_lookup_vendor(uint16_t);
36838cc658fSJohn Baldwin 
36938cc658fSJohn Baldwin typedef int	(*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
37038cc658fSJohn Baldwin 
371e51a25f8SAlfred Perlstein static int bge_probe(device_t);
372e51a25f8SAlfred Perlstein static int bge_attach(device_t);
373e51a25f8SAlfred Perlstein static int bge_detach(device_t);
37414afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
37514afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3763f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
377f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
3785b610048SPyun YongHyeon static int bge_dma_alloc(struct bge_softc *);
379f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
3805b610048SPyun YongHyeon static int bge_dma_ring_alloc(struct bge_softc *, bus_size_t, bus_size_t,
3815b610048SPyun YongHyeon     bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
382f41ac2beSBill Paul 
3835fea260fSMarius Strobl static int bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[]);
38438cc658fSJohn Baldwin static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
38538cc658fSJohn Baldwin static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
38638cc658fSJohn Baldwin static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
38738cc658fSJohn Baldwin static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
38838cc658fSJohn Baldwin 
389b9c05fa5SPyun YongHyeon static void bge_txeof(struct bge_softc *, uint16_t);
3901108273aSPyun YongHyeon static void bge_rxcsum(struct bge_softc *, struct bge_rx_bd *, struct mbuf *);
391dfe0df9aSPyun YongHyeon static int bge_rxeof(struct bge_softc *, uint16_t, int);
39295d67482SBill Paul 
3938cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
394e51a25f8SAlfred Perlstein static void bge_tick(void *);
3952280c16bSPyun YongHyeon static void bge_stats_clear_regs(struct bge_softc *);
396e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
3973f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
398d598b626SPyun YongHyeon static struct mbuf *bge_check_short_dma(struct mbuf *);
3992e1d4df4SPyun YongHyeon static struct mbuf *bge_setup_tso(struct bge_softc *, struct mbuf *,
4001108273aSPyun YongHyeon     uint16_t *, uint16_t *);
401676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
40295d67482SBill Paul 
403e51a25f8SAlfred Perlstein static void bge_intr(void *);
404dfe0df9aSPyun YongHyeon static int bge_msi_intr(void *);
405dfe0df9aSPyun YongHyeon static void bge_intr_task(void *, int);
4060f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
407e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
408e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
4090f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
410e51a25f8SAlfred Perlstein static void bge_init(void *);
4115a147ba6SPyun YongHyeon static void bge_stop_block(struct bge_softc *, bus_size_t, uint32_t);
412e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
413b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
414b6c974e8SWarner Losh static int bge_shutdown(device_t);
41567d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
416e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
417e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
41895d67482SBill Paul 
41938cc658fSJohn Baldwin static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
42038cc658fSJohn Baldwin static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
42138cc658fSJohn Baldwin 
4223f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
423e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
42495d67482SBill Paul 
4253e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
426e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
427cb2eacc7SYaroslav Tykhiy static void bge_setvlan(struct bge_softc *);
42895d67482SBill Paul 
429e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_std(struct bge_softc *, int);
430e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_jumbo(struct bge_softc *, int);
431943787f3SPyun YongHyeon static int bge_newbuf_std(struct bge_softc *, int);
432943787f3SPyun YongHyeon static int bge_newbuf_jumbo(struct bge_softc *, int);
433e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
434e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
435e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
436e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
437e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
438e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
43995d67482SBill Paul 
440e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
441e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
44250515680SPyun YongHyeon static uint32_t bge_dma_swap_options(struct bge_softc *);
44395d67482SBill Paul 
4445fea260fSMarius Strobl static int bge_has_eaddr(struct bge_softc *);
4453f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
446e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
44738cc658fSJohn Baldwin static void bge_writembx(struct bge_softc *, int, int);
44895d67482SBill Paul #ifdef notdef
4493f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
45095d67482SBill Paul #endif
4519ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
452e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
45395d67482SBill Paul 
454e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
455e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
456e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
45775719184SGleb Smirnoff #ifdef DEVICE_POLLING
4581abcdbd1SAttilio Rao static int bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
45975719184SGleb Smirnoff #endif
46095d67482SBill Paul 
4618cb1383cSDoug Ambrisko #define	BGE_RESET_START 1
4628cb1383cSDoug Ambrisko #define	BGE_RESET_STOP  2
4638cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
4648cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
4658cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
466797ab05eSPyun YongHyeon static void bge_stop_fw(struct bge_softc *);
4678cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
468dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
46995d67482SBill Paul 
4706f8718a3SScott Long /*
4716f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
4726f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
4736f8718a3SScott Long  * traps on certain architectures.
4746f8718a3SScott Long  */
4756f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
4766f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
4776f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
4786f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
4796f8718a3SScott Long #endif
4806f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
4812280c16bSPyun YongHyeon static void bge_add_sysctl_stats_regs(struct bge_softc *,
4822280c16bSPyun YongHyeon     struct sysctl_ctx_list *, struct sysctl_oid_list *);
4832280c16bSPyun YongHyeon static void bge_add_sysctl_stats(struct bge_softc *, struct sysctl_ctx_list *,
4842280c16bSPyun YongHyeon     struct sysctl_oid_list *);
485763757b2SScott Long static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
4866f8718a3SScott Long 
48795d67482SBill Paul static device_method_t bge_methods[] = {
48895d67482SBill Paul 	/* Device interface */
48995d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
49095d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
49195d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
49295d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
49314afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
49414afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
49595d67482SBill Paul 
49695d67482SBill Paul 	/* MII interface */
49795d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
49895d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
49995d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
50095d67482SBill Paul 
5014b7ec270SMarius Strobl 	DEVMETHOD_END
50295d67482SBill Paul };
50395d67482SBill Paul 
50495d67482SBill Paul static driver_t bge_driver = {
50595d67482SBill Paul 	"bge",
50695d67482SBill Paul 	bge_methods,
50795d67482SBill Paul 	sizeof(struct bge_softc)
50895d67482SBill Paul };
50995d67482SBill Paul 
51095d67482SBill Paul static devclass_t bge_devclass;
51195d67482SBill Paul 
512f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
51395d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
51495d67482SBill Paul 
515f1a7e6d5SScott Long static int bge_allow_asf = 1;
516f1a7e6d5SScott Long 
517f1a7e6d5SScott Long TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
518f1a7e6d5SScott Long 
5196472ac3dSEd Schouten static SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
520f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
521f1a7e6d5SScott Long 	"Allow ASF mode if available");
522c4529f41SMichael Reifenberger 
52308013fd3SMarius Strobl #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
52408013fd3SMarius Strobl #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
52508013fd3SMarius Strobl #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
52608013fd3SMarius Strobl #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
52708013fd3SMarius Strobl #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
52808013fd3SMarius Strobl 
52908013fd3SMarius Strobl static int
5305fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
53108013fd3SMarius Strobl {
53208013fd3SMarius Strobl #ifdef __sparc64__
53308013fd3SMarius Strobl 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
53408013fd3SMarius Strobl 	device_t dev;
53508013fd3SMarius Strobl 	uint32_t subvendor;
53608013fd3SMarius Strobl 
53708013fd3SMarius Strobl 	dev = sc->bge_dev;
53808013fd3SMarius Strobl 
53908013fd3SMarius Strobl 	/*
54008013fd3SMarius Strobl 	 * The on-board BGEs found in sun4u machines aren't fitted with
54108013fd3SMarius Strobl 	 * an EEPROM which means that we have to obtain the MAC address
54208013fd3SMarius Strobl 	 * via OFW and that some tests will always fail.  We distinguish
54308013fd3SMarius Strobl 	 * such BGEs by the subvendor ID, which also has to be obtained
54408013fd3SMarius Strobl 	 * from OFW instead of the PCI configuration space as the latter
54508013fd3SMarius Strobl 	 * indicates Broadcom as the subvendor of the netboot interface.
54608013fd3SMarius Strobl 	 * For early Blade 1500 and 2500 we even have to check the OFW
54708013fd3SMarius Strobl 	 * device path as the subvendor ID always defaults to Broadcom
54808013fd3SMarius Strobl 	 * there.
54908013fd3SMarius Strobl 	 */
55008013fd3SMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
55108013fd3SMarius Strobl 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
5522d857b9bSMarius Strobl 	    (subvendor == FJTSU_VENDORID || subvendor == SUN_VENDORID))
55308013fd3SMarius Strobl 		return (0);
55408013fd3SMarius Strobl 	memset(buf, 0, sizeof(buf));
55508013fd3SMarius Strobl 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
55608013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
55708013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
55808013fd3SMarius Strobl 			return (0);
55908013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
56008013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
56108013fd3SMarius Strobl 			return (0);
56208013fd3SMarius Strobl 	}
56308013fd3SMarius Strobl #endif
56408013fd3SMarius Strobl 	return (1);
56508013fd3SMarius Strobl }
56608013fd3SMarius Strobl 
5673f74909aSGleb Smirnoff static uint32_t
5683f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
56995d67482SBill Paul {
57095d67482SBill Paul 	device_t dev;
5716f8718a3SScott Long 	uint32_t val;
57295d67482SBill Paul 
573a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
574a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
575a4431ebaSPyun YongHyeon 		return (0);
576a4431ebaSPyun YongHyeon 
57795d67482SBill Paul 	dev = sc->bge_dev;
57895d67482SBill Paul 
57995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
5806f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
5816f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
5826f8718a3SScott Long 	return (val);
58395d67482SBill Paul }
58495d67482SBill Paul 
58595d67482SBill Paul static void
5863f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
58795d67482SBill Paul {
58895d67482SBill Paul 	device_t dev;
58995d67482SBill Paul 
590a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
591a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
592a4431ebaSPyun YongHyeon 		return;
593a4431ebaSPyun YongHyeon 
59495d67482SBill Paul 	dev = sc->bge_dev;
59595d67482SBill Paul 
59695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
59795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
5986f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
59995d67482SBill Paul }
60095d67482SBill Paul 
60195d67482SBill Paul #ifdef notdef
6023f74909aSGleb Smirnoff static uint32_t
6033f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
60495d67482SBill Paul {
60595d67482SBill Paul 	device_t dev;
60695d67482SBill Paul 
60795d67482SBill Paul 	dev = sc->bge_dev;
60895d67482SBill Paul 
60995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
61095d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
61195d67482SBill Paul }
61295d67482SBill Paul #endif
61395d67482SBill Paul 
61495d67482SBill Paul static void
6153f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
61695d67482SBill Paul {
61795d67482SBill Paul 	device_t dev;
61895d67482SBill Paul 
61995d67482SBill Paul 	dev = sc->bge_dev;
62095d67482SBill Paul 
62195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
62295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
62395d67482SBill Paul }
62495d67482SBill Paul 
6256f8718a3SScott Long static void
6266f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
6276f8718a3SScott Long {
6286f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6296f8718a3SScott Long }
6306f8718a3SScott Long 
63138cc658fSJohn Baldwin static void
63238cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
63338cc658fSJohn Baldwin {
63438cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
63538cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
63638cc658fSJohn Baldwin 
63738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
63838cc658fSJohn Baldwin }
63938cc658fSJohn Baldwin 
640f41ac2beSBill Paul /*
641f41ac2beSBill Paul  * Map a single buffer address.
642f41ac2beSBill Paul  */
643f41ac2beSBill Paul 
644f41ac2beSBill Paul static void
6453f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
646f41ac2beSBill Paul {
647f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
648f41ac2beSBill Paul 
649f41ac2beSBill Paul 	if (error)
650f41ac2beSBill Paul 		return;
651f41ac2beSBill Paul 
6525b610048SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
6535b610048SPyun YongHyeon 
654f41ac2beSBill Paul 	ctx = arg;
655f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
656f41ac2beSBill Paul }
657f41ac2beSBill Paul 
65838cc658fSJohn Baldwin static uint8_t
65938cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
66038cc658fSJohn Baldwin {
66138cc658fSJohn Baldwin 	uint32_t access, byte = 0;
66238cc658fSJohn Baldwin 	int i;
66338cc658fSJohn Baldwin 
66438cc658fSJohn Baldwin 	/* Lock. */
66538cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
66638cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
66738cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
66838cc658fSJohn Baldwin 			break;
66938cc658fSJohn Baldwin 		DELAY(20);
67038cc658fSJohn Baldwin 	}
67138cc658fSJohn Baldwin 	if (i == 8000)
67238cc658fSJohn Baldwin 		return (1);
67338cc658fSJohn Baldwin 
67438cc658fSJohn Baldwin 	/* Enable access. */
67538cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
67638cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
67738cc658fSJohn Baldwin 
67838cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
67938cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
68038cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
68138cc658fSJohn Baldwin 		DELAY(10);
68238cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
68338cc658fSJohn Baldwin 			DELAY(10);
68438cc658fSJohn Baldwin 			break;
68538cc658fSJohn Baldwin 		}
68638cc658fSJohn Baldwin 	}
68738cc658fSJohn Baldwin 
68838cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
68938cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
69038cc658fSJohn Baldwin 		return (1);
69138cc658fSJohn Baldwin 	}
69238cc658fSJohn Baldwin 
69338cc658fSJohn Baldwin 	/* Get result. */
69438cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
69538cc658fSJohn Baldwin 
69638cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
69738cc658fSJohn Baldwin 
69838cc658fSJohn Baldwin 	/* Disable access. */
69938cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
70038cc658fSJohn Baldwin 
70138cc658fSJohn Baldwin 	/* Unlock. */
70238cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
70338cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
70438cc658fSJohn Baldwin 
70538cc658fSJohn Baldwin 	return (0);
70638cc658fSJohn Baldwin }
70738cc658fSJohn Baldwin 
70838cc658fSJohn Baldwin /*
70938cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
71038cc658fSJohn Baldwin  */
71138cc658fSJohn Baldwin static int
71238cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
71338cc658fSJohn Baldwin {
71438cc658fSJohn Baldwin 	int err = 0, i;
71538cc658fSJohn Baldwin 	uint8_t byte = 0;
71638cc658fSJohn Baldwin 
71738cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
71838cc658fSJohn Baldwin 		return (1);
71938cc658fSJohn Baldwin 
72038cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
72138cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
72238cc658fSJohn Baldwin 		if (err)
72338cc658fSJohn Baldwin 			break;
72438cc658fSJohn Baldwin 		*(dest + i) = byte;
72538cc658fSJohn Baldwin 	}
72638cc658fSJohn Baldwin 
72738cc658fSJohn Baldwin 	return (err ? 1 : 0);
72838cc658fSJohn Baldwin }
72938cc658fSJohn Baldwin 
73095d67482SBill Paul /*
73195d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
73295d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
73395d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
73495d67482SBill Paul  * access method.
73595d67482SBill Paul  */
7363f74909aSGleb Smirnoff static uint8_t
7373f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
73895d67482SBill Paul {
73995d67482SBill Paul 	int i;
7403f74909aSGleb Smirnoff 	uint32_t byte = 0;
74195d67482SBill Paul 
74295d67482SBill Paul 	/*
74395d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
74495d67482SBill Paul 	 * having to use the bitbang method.
74595d67482SBill Paul 	 */
74695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
74795d67482SBill Paul 
74895d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
74995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
75095d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
75195d67482SBill Paul 	DELAY(20);
75295d67482SBill Paul 
75395d67482SBill Paul 	/* Issue the read EEPROM command. */
75495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
75595d67482SBill Paul 
75695d67482SBill Paul 	/* Wait for completion */
75795d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
75895d67482SBill Paul 		DELAY(10);
75995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
76095d67482SBill Paul 			break;
76195d67482SBill Paul 	}
76295d67482SBill Paul 
763d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
764fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
765f6789fbaSPyun YongHyeon 		return (1);
76695d67482SBill Paul 	}
76795d67482SBill Paul 
76895d67482SBill Paul 	/* Get result. */
76995d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
77095d67482SBill Paul 
7710c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
77295d67482SBill Paul 
77395d67482SBill Paul 	return (0);
77495d67482SBill Paul }
77595d67482SBill Paul 
77695d67482SBill Paul /*
77795d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
77895d67482SBill Paul  */
77995d67482SBill Paul static int
7803f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
78195d67482SBill Paul {
7823f74909aSGleb Smirnoff 	int i, error = 0;
7833f74909aSGleb Smirnoff 	uint8_t byte = 0;
78495d67482SBill Paul 
78595d67482SBill Paul 	for (i = 0; i < cnt; i++) {
7863f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
7873f74909aSGleb Smirnoff 		if (error)
78895d67482SBill Paul 			break;
78995d67482SBill Paul 		*(dest + i) = byte;
79095d67482SBill Paul 	}
79195d67482SBill Paul 
7923f74909aSGleb Smirnoff 	return (error ? 1 : 0);
79395d67482SBill Paul }
79495d67482SBill Paul 
79595d67482SBill Paul static int
7963f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
79795d67482SBill Paul {
79895d67482SBill Paul 	struct bge_softc *sc;
799a813ed78SPyun YongHyeon 	uint32_t val;
80095d67482SBill Paul 	int i;
80195d67482SBill Paul 
80295d67482SBill Paul 	sc = device_get_softc(dev);
80395d67482SBill Paul 
804a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
805a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
806a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
807a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
808a813ed78SPyun YongHyeon 		DELAY(80);
80937ceeb4dSPaul Saab 	}
81037ceeb4dSPaul Saab 
81195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
81295d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
81395d67482SBill Paul 
814a813ed78SPyun YongHyeon 	/* Poll for the PHY register access to complete. */
81595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
816d5d23857SJung-uk Kim 		DELAY(10);
81795d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
818a813ed78SPyun YongHyeon 		if ((val & BGE_MICOMM_BUSY) == 0) {
819a813ed78SPyun YongHyeon 			DELAY(5);
820a813ed78SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_MI_COMM);
82195d67482SBill Paul 			break;
82295d67482SBill Paul 		}
823a813ed78SPyun YongHyeon 	}
82495d67482SBill Paul 
82595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
8265fea260fSMarius Strobl 		device_printf(sc->bge_dev,
8275fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
8285fea260fSMarius Strobl 		    phy, reg, val);
82937ceeb4dSPaul Saab 		val = 0;
83095d67482SBill Paul 	}
83195d67482SBill Paul 
832a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
833a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
834a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
835a813ed78SPyun YongHyeon 		DELAY(80);
83637ceeb4dSPaul Saab 	}
83737ceeb4dSPaul Saab 
83895d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
83995d67482SBill Paul 		return (0);
84095d67482SBill Paul 
8410c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
84295d67482SBill Paul }
84395d67482SBill Paul 
84495d67482SBill Paul static int
8453f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
84695d67482SBill Paul {
84795d67482SBill Paul 	struct bge_softc *sc;
84895d67482SBill Paul 	int i;
84995d67482SBill Paul 
85095d67482SBill Paul 	sc = device_get_softc(dev);
85195d67482SBill Paul 
85238cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
85338cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
85438cc658fSJohn Baldwin 		return (0);
85538cc658fSJohn Baldwin 
856a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
857a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
858a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
859a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
860a813ed78SPyun YongHyeon 		DELAY(80);
86137ceeb4dSPaul Saab 	}
86237ceeb4dSPaul Saab 
86395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
86495d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
86595d67482SBill Paul 
86695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
867d5d23857SJung-uk Kim 		DELAY(10);
86838cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
86938cc658fSJohn Baldwin 			DELAY(5);
87038cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
87195d67482SBill Paul 			break;
872d5d23857SJung-uk Kim 		}
87338cc658fSJohn Baldwin 	}
874d5d23857SJung-uk Kim 
875a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
876a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
877a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
878a813ed78SPyun YongHyeon 		DELAY(80);
879a813ed78SPyun YongHyeon 	}
880a813ed78SPyun YongHyeon 
881a813ed78SPyun YongHyeon 	if (i == BGE_TIMEOUT)
88238cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
88338cc658fSJohn Baldwin 		    "PHY write timed out (phy %d, reg %d, val %d)\n",
88438cc658fSJohn Baldwin 		    phy, reg, val);
88537ceeb4dSPaul Saab 
88695d67482SBill Paul 	return (0);
88795d67482SBill Paul }
88895d67482SBill Paul 
88995d67482SBill Paul static void
8903f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
89195d67482SBill Paul {
89295d67482SBill Paul 	struct bge_softc *sc;
89395d67482SBill Paul 	struct mii_data *mii;
89495d67482SBill Paul 	sc = device_get_softc(dev);
89595d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
89695d67482SBill Paul 
897d4f5240aSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
898d4f5240aSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
899d4f5240aSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
900d4f5240aSPyun YongHyeon 		case IFM_10_T:
901d4f5240aSPyun YongHyeon 		case IFM_100_TX:
902d4f5240aSPyun YongHyeon 			sc->bge_link = 1;
903d4f5240aSPyun YongHyeon 			break;
904d4f5240aSPyun YongHyeon 		case IFM_1000_T:
905d4f5240aSPyun YongHyeon 		case IFM_1000_SX:
906d4f5240aSPyun YongHyeon 		case IFM_2500_SX:
907d4f5240aSPyun YongHyeon 			if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
908d4f5240aSPyun YongHyeon 				sc->bge_link = 1;
909d4f5240aSPyun YongHyeon 			else
910d4f5240aSPyun YongHyeon 				sc->bge_link = 0;
911d4f5240aSPyun YongHyeon 			break;
912d4f5240aSPyun YongHyeon 		default:
913d4f5240aSPyun YongHyeon 			sc->bge_link = 0;
914d4f5240aSPyun YongHyeon 			break;
915d4f5240aSPyun YongHyeon 		}
916d4f5240aSPyun YongHyeon 	} else
917d4f5240aSPyun YongHyeon 		sc->bge_link = 0;
918d4f5240aSPyun YongHyeon 	if (sc->bge_link == 0)
919d4f5240aSPyun YongHyeon 		return;
92095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
921ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
922ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
92395d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
9243f74909aSGleb Smirnoff 	else
92595d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
92695d67482SBill Paul 
9276854be25SPyun YongHyeon 	if (IFM_OPTIONS(mii->mii_media_active & IFM_FDX) != 0) {
92895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
929efd4fc3fSMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
930efd4fc3fSMarius Strobl 		    IFM_ETH_TXPAUSE) != 0)
9316854be25SPyun YongHyeon 			BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
9323f74909aSGleb Smirnoff 		else
9336854be25SPyun YongHyeon 			BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
934efd4fc3fSMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
935efd4fc3fSMarius Strobl 		    IFM_ETH_RXPAUSE) != 0)
9366854be25SPyun YongHyeon 			BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
9376854be25SPyun YongHyeon 		else
9386854be25SPyun YongHyeon 			BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
9396854be25SPyun YongHyeon 	} else {
94095d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
9416854be25SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
9426854be25SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
9436854be25SPyun YongHyeon 	}
94495d67482SBill Paul }
94595d67482SBill Paul 
94695d67482SBill Paul /*
94795d67482SBill Paul  * Intialize a standard receive ring descriptor.
94895d67482SBill Paul  */
94995d67482SBill Paul static int
950943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
95195d67482SBill Paul {
952943787f3SPyun YongHyeon 	struct mbuf *m;
95395d67482SBill Paul 	struct bge_rx_bd *r;
954a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
955943787f3SPyun YongHyeon 	bus_dmamap_t map;
956a23634a1SPyun YongHyeon 	int error, nsegs;
95795d67482SBill Paul 
958f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD &&
959f5459d4cSPyun YongHyeon 	    (sc->bge_ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
960f5459d4cSPyun YongHyeon 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))) {
961f5459d4cSPyun YongHyeon 		m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
962f5459d4cSPyun YongHyeon 		if (m == NULL)
963f5459d4cSPyun YongHyeon 			return (ENOBUFS);
964f5459d4cSPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MJUM9BYTES;
965f5459d4cSPyun YongHyeon 	} else {
966943787f3SPyun YongHyeon 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
967943787f3SPyun YongHyeon 		if (m == NULL)
96895d67482SBill Paul 			return (ENOBUFS);
969943787f3SPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MCLBYTES;
970f5459d4cSPyun YongHyeon 	}
971652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
972943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
973943787f3SPyun YongHyeon 
9740ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
975943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
976a23634a1SPyun YongHyeon 	if (error != 0) {
977943787f3SPyun YongHyeon 		m_freem(m);
978a23634a1SPyun YongHyeon 		return (error);
979f41ac2beSBill Paul 	}
980943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
981943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
982943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
983943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
984943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
985943787f3SPyun YongHyeon 	}
986943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
987943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
988943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
989943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
990e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
991943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
992a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
993a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
994e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
995a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
996e907febfSPyun YongHyeon 	r->bge_idx = i;
997f41ac2beSBill Paul 
9980ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
999943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
100095d67482SBill Paul 
100195d67482SBill Paul 	return (0);
100295d67482SBill Paul }
100395d67482SBill Paul 
100495d67482SBill Paul /*
100595d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
100695d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
100795d67482SBill Paul  */
100895d67482SBill Paul static int
1009943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
101095d67482SBill Paul {
10111be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
1012943787f3SPyun YongHyeon 	bus_dmamap_t map;
10131be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
1014943787f3SPyun YongHyeon 	struct mbuf *m;
1015943787f3SPyun YongHyeon 	int error, nsegs;
101695d67482SBill Paul 
1017943787f3SPyun YongHyeon 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1018943787f3SPyun YongHyeon 	if (m == NULL)
101995d67482SBill Paul 		return (ENOBUFS);
102095d67482SBill Paul 
1021943787f3SPyun YongHyeon 	m_cljget(m, M_DONTWAIT, MJUM9BYTES);
1022943787f3SPyun YongHyeon 	if (!(m->m_flags & M_EXT)) {
1023943787f3SPyun YongHyeon 		m_freem(m);
102495d67482SBill Paul 		return (ENOBUFS);
102595d67482SBill Paul 	}
1026943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1027652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1028943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
10291be6acb7SGleb Smirnoff 
10301be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1031943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1032943787f3SPyun YongHyeon 	if (error != 0) {
1033943787f3SPyun YongHyeon 		m_freem(m);
10341be6acb7SGleb Smirnoff 		return (error);
1035f7cea149SGleb Smirnoff 	}
10361be6acb7SGleb Smirnoff 
1037aa8cbdbfSMarius Strobl 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1038943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1039943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1040943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1041943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1042943787f3SPyun YongHyeon 	}
1043943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1044943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1045943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1046943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1047943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1048e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1049e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1050e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1051e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1052e0b7b101SPyun YongHyeon 
10531be6acb7SGleb Smirnoff 	/*
10541be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
10551be6acb7SGleb Smirnoff 	 */
1056943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
10574e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
10584e7ba1abSGleb Smirnoff 	r->bge_idx = i;
10594e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
10604e7ba1abSGleb Smirnoff 	switch (nsegs) {
10614e7ba1abSGleb Smirnoff 	case 4:
10624e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
10634e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
10644e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1065e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
10664e7ba1abSGleb Smirnoff 	case 3:
1067e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1068e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1069e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1070e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
10714e7ba1abSGleb Smirnoff 	case 2:
10724e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
10734e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
10744e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1075e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
10764e7ba1abSGleb Smirnoff 	case 1:
10774e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
10784e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
10794e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1080e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
10814e7ba1abSGleb Smirnoff 		break;
10824e7ba1abSGleb Smirnoff 	default:
10834e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
10844e7ba1abSGleb Smirnoff 	}
1085f41ac2beSBill Paul 
1086a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1087943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
108895d67482SBill Paul 
108995d67482SBill Paul 	return (0);
109095d67482SBill Paul }
109195d67482SBill Paul 
109295d67482SBill Paul static int
10933f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
109495d67482SBill Paul {
10953ee5d7daSPyun YongHyeon 	int error, i;
109695d67482SBill Paul 
1097e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
109803e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1099e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1100943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
11013ee5d7daSPyun YongHyeon 			return (error);
110203e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
11031888f324SPyun YongHyeon 	}
110495d67482SBill Paul 
1105f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1106d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1107f41ac2beSBill Paul 
1108e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1109e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
111095d67482SBill Paul 
111195d67482SBill Paul 	return (0);
111295d67482SBill Paul }
111395d67482SBill Paul 
111495d67482SBill Paul static void
11153f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
111695d67482SBill Paul {
111795d67482SBill Paul 	int i;
111895d67482SBill Paul 
111995d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
112095d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
11210ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1122e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1123e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
11240ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1125f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1126e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1127e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
112895d67482SBill Paul 		}
1129f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
113095d67482SBill Paul 		    sizeof(struct bge_rx_bd));
113195d67482SBill Paul 	}
113295d67482SBill Paul }
113395d67482SBill Paul 
113495d67482SBill Paul static int
11353f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
113695d67482SBill Paul {
113795d67482SBill Paul 	struct bge_rcb *rcb;
11383ee5d7daSPyun YongHyeon 	int error, i;
113995d67482SBill Paul 
1140e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
114103e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
114295d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1143943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
11443ee5d7daSPyun YongHyeon 			return (error);
114503e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
11461888f324SPyun YongHyeon 	}
114795d67482SBill Paul 
1148f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1149d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1150f41ac2beSBill Paul 
1151e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
115295d67482SBill Paul 
11538a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1154f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
11558a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
11568a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
115767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
115895d67482SBill Paul 
1159e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
116095d67482SBill Paul 
116195d67482SBill Paul 	return (0);
116295d67482SBill Paul }
116395d67482SBill Paul 
116495d67482SBill Paul static void
11653f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
116695d67482SBill Paul {
116795d67482SBill Paul 	int i;
116895d67482SBill Paul 
116995d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
117095d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1171e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1172e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1173e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1174f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1175f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1176e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1177e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
117895d67482SBill Paul 		}
1179f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
11801be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
118195d67482SBill Paul 	}
118295d67482SBill Paul }
118395d67482SBill Paul 
118495d67482SBill Paul static void
11853f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
118695d67482SBill Paul {
118795d67482SBill Paul 	int i;
118895d67482SBill Paul 
1189f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
119095d67482SBill Paul 		return;
119195d67482SBill Paul 
119295d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
119395d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
11940ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1195e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1196e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
11970ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1198f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1199e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1200e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
120195d67482SBill Paul 		}
1202f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
120395d67482SBill Paul 		    sizeof(struct bge_tx_bd));
120495d67482SBill Paul 	}
120595d67482SBill Paul }
120695d67482SBill Paul 
120795d67482SBill Paul static int
12083f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
120995d67482SBill Paul {
121095d67482SBill Paul 	sc->bge_txcnt = 0;
121195d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
12123927098fSPaul Saab 
1213e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1214e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
12155c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1216e6bf277eSPyun YongHyeon 
121714bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
121814bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
121938cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
122014bbd30fSGleb Smirnoff 
12213927098fSPaul Saab 	/* 5700 b2 errata */
1222e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
122338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
12243927098fSPaul Saab 
122514bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
122638cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
12273927098fSPaul Saab 	/* 5700 b2 errata */
1228e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
122938cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
123095d67482SBill Paul 
123195d67482SBill Paul 	return (0);
123295d67482SBill Paul }
123395d67482SBill Paul 
123495d67482SBill Paul static void
12353e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
12363e9b1bcaSJung-uk Kim {
12373e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
12383e9b1bcaSJung-uk Kim 
12393e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
12403e9b1bcaSJung-uk Kim 
12413e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
12423e9b1bcaSJung-uk Kim 
124345ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
12443e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
124545ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
12463e9b1bcaSJung-uk Kim 	else
124745ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
12483e9b1bcaSJung-uk Kim }
12493e9b1bcaSJung-uk Kim 
12503e9b1bcaSJung-uk Kim static void
12513f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
125295d67482SBill Paul {
125395d67482SBill Paul 	struct ifnet *ifp;
125495d67482SBill Paul 	struct ifmultiaddr *ifma;
12553f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
125695d67482SBill Paul 	int h, i;
125795d67482SBill Paul 
12580f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
12590f9bd73bSSam Leffler 
1260fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
126195d67482SBill Paul 
126295d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
126395d67482SBill Paul 		for (i = 0; i < 4; i++)
12640c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
126595d67482SBill Paul 		return;
126695d67482SBill Paul 	}
126795d67482SBill Paul 
126895d67482SBill Paul 	/* First, zot all the existing filters. */
126995d67482SBill Paul 	for (i = 0; i < 4; i++)
127095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
127195d67482SBill Paul 
127295d67482SBill Paul 	/* Now program new ones. */
1273eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
127495d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
127595d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
127695d67482SBill Paul 			continue;
12770e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
12780c8aa4eaSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
12790c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
128095d67482SBill Paul 	}
1281eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
128295d67482SBill Paul 
128395d67482SBill Paul 	for (i = 0; i < 4; i++)
128495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
128595d67482SBill Paul }
128695d67482SBill Paul 
12878cb1383cSDoug Ambrisko static void
1288cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1289cb2eacc7SYaroslav Tykhiy {
1290cb2eacc7SYaroslav Tykhiy 	struct ifnet *ifp;
1291cb2eacc7SYaroslav Tykhiy 
1292cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1293cb2eacc7SYaroslav Tykhiy 
1294cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1295cb2eacc7SYaroslav Tykhiy 
1296cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1297cb2eacc7SYaroslav Tykhiy 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1298cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1299cb2eacc7SYaroslav Tykhiy 	else
1300cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1301cb2eacc7SYaroslav Tykhiy }
1302cb2eacc7SYaroslav Tykhiy 
1303cb2eacc7SYaroslav Tykhiy static void
1304797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
13058cb1383cSDoug Ambrisko {
1306797ab05eSPyun YongHyeon 
13078cb1383cSDoug Ambrisko 	/*
13088cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
13098cb1383cSDoug Ambrisko 	 */
13108cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
1311888b47f0SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
13128cb1383cSDoug Ambrisko 
13138cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
13148cb1383cSDoug Ambrisko 		switch (type) {
13158cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1316224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1317224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
13188cb1383cSDoug Ambrisko 			break;
13198cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
1320224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1321224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
13228cb1383cSDoug Ambrisko 			break;
13238cb1383cSDoug Ambrisko 		}
13248cb1383cSDoug Ambrisko 	}
13258cb1383cSDoug Ambrisko }
13268cb1383cSDoug Ambrisko 
13278cb1383cSDoug Ambrisko static void
1328797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
13298cb1383cSDoug Ambrisko {
1330797ab05eSPyun YongHyeon 
13318cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
13328cb1383cSDoug Ambrisko 		switch (type) {
13338cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1334224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1335224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START_DONE);
13368cb1383cSDoug Ambrisko 			/* START DONE */
13378cb1383cSDoug Ambrisko 			break;
13388cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
1339224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1340224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD_DONE);
13418cb1383cSDoug Ambrisko 			break;
13428cb1383cSDoug Ambrisko 		}
13438cb1383cSDoug Ambrisko 	}
13448cb1383cSDoug Ambrisko }
13458cb1383cSDoug Ambrisko 
13468cb1383cSDoug Ambrisko static void
1347797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
13488cb1383cSDoug Ambrisko {
1349797ab05eSPyun YongHyeon 
13508cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
13518cb1383cSDoug Ambrisko 		switch (type) {
13528cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1353224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1354224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
13558cb1383cSDoug Ambrisko 			break;
13568cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
1357224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1358224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
13598cb1383cSDoug Ambrisko 			break;
13608cb1383cSDoug Ambrisko 		}
13618cb1383cSDoug Ambrisko 	}
13628cb1383cSDoug Ambrisko }
13638cb1383cSDoug Ambrisko 
1364797ab05eSPyun YongHyeon static void
1365797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
13668cb1383cSDoug Ambrisko {
13678cb1383cSDoug Ambrisko 	int i;
13688cb1383cSDoug Ambrisko 
13698cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
13703c201200SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB, BGE_FW_CMD_PAUSE);
13713fed2d5dSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
13729931ba85SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RX_CPU_EVENT) | BGE_RX_CPU_DRV_EVENT);
13738cb1383cSDoug Ambrisko 
13748cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
13759931ba85SPyun YongHyeon 			if (!(CSR_READ_4(sc, BGE_RX_CPU_EVENT) &
13769931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT))
13778cb1383cSDoug Ambrisko 				break;
13788cb1383cSDoug Ambrisko 			DELAY(10);
13798cb1383cSDoug Ambrisko 		}
13808cb1383cSDoug Ambrisko 	}
13818cb1383cSDoug Ambrisko }
13828cb1383cSDoug Ambrisko 
138350515680SPyun YongHyeon static uint32_t
138450515680SPyun YongHyeon bge_dma_swap_options(struct bge_softc *sc)
138550515680SPyun YongHyeon {
138650515680SPyun YongHyeon 	uint32_t dma_options;
138750515680SPyun YongHyeon 
138850515680SPyun YongHyeon 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
138950515680SPyun YongHyeon 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
139050515680SPyun YongHyeon #if BYTE_ORDER == BIG_ENDIAN
139150515680SPyun YongHyeon 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
139250515680SPyun YongHyeon #endif
139350515680SPyun YongHyeon 	if ((sc)->bge_asicrev == BGE_ASICREV_BCM5720)
139450515680SPyun YongHyeon 		dma_options |= BGE_MODECTL_BYTESWAP_B2HRX_DATA |
139550515680SPyun YongHyeon 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA | BGE_MODECTL_B2HRX_ENABLE |
139650515680SPyun YongHyeon 		    BGE_MODECTL_HTX2B_ENABLE;
139750515680SPyun YongHyeon 
139850515680SPyun YongHyeon 	return (dma_options);
139950515680SPyun YongHyeon }
140050515680SPyun YongHyeon 
140195d67482SBill Paul /*
1402c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
140395d67482SBill Paul  */
140495d67482SBill Paul static int
14053f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
140695d67482SBill Paul {
140750515680SPyun YongHyeon 	uint32_t dma_rw_ctl, misc_ctl, mode_ctl;
1408fbc374afSPyun YongHyeon 	uint16_t val;
140995d67482SBill Paul 	int i;
141095d67482SBill Paul 
14118cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
14121108273aSPyun YongHyeon 	misc_ctl = BGE_INIT;
14131108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
14141108273aSPyun YongHyeon 		misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
14151108273aSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
141695d67482SBill Paul 
141795d67482SBill Paul 	/* Clear the MAC control register */
141895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
141995d67482SBill Paul 
142095d67482SBill Paul 	/*
142195d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
142295d67482SBill Paul 	 * internal memory.
142395d67482SBill Paul 	 */
142495d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
14253f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
142695d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
142795d67482SBill Paul 
142895d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
14293f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
143095d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
143195d67482SBill Paul 
1432fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1433fbc374afSPyun YongHyeon 		/*
1434d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1435fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1436fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1437fbc374afSPyun YongHyeon 		 */
1438fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1439fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1440fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1441fbc374afSPyun YongHyeon 	}
1442fbc374afSPyun YongHyeon 
1443186f842bSJung-uk Kim 	/*
1444186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1445186f842bSJung-uk Kim 	 */
1446186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1447186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1448652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
1449186f842bSJung-uk Kim 		/* Read watermark not used, 128 bytes for write. */
1450186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1451652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
14524c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1453186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1454186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1455186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1456186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1457186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1458186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1459cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1460cbb2b2feSPyun YongHyeon 			/*
1461cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1462cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1463cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1464cbb2b2feSPyun YongHyeon 			 * register.
1465cbb2b2feSPyun YongHyeon 			 */
1466cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1467cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1468186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1469186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1470186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1471186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1472186f842bSJung-uk Kim 		} else {
1473186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1474186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1475186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
14760c8aa4eaSJung-uk Kim 			    0x0F;
1477186f842bSJung-uk Kim 		}
1478e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1479e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
14803f74909aSGleb Smirnoff 			uint32_t tmp;
14815cba12d3SPaul Saab 
1482186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
14830c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1484186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1485186f842bSJung-uk Kim 				dma_rw_ctl |=
1486186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
14875cba12d3SPaul Saab 
1488186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1489186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1490186f842bSJung-uk Kim 		}
1491186f842bSJung-uk Kim 	} else {
1492186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1493186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1494186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1495186f842bSJung-uk Kim 
1496186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1497186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1498186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1499186f842bSJung-uk Kim 	}
1500186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1501186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1502186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1503186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1504e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1505186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
15065cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1507b4a256acSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
15081108273aSPyun YongHyeon 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1509b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
1510b4a256acSPyun YongHyeon 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1511bbe2ca75SPyun YongHyeon 		/*
1512bbe2ca75SPyun YongHyeon 		 * Enable HW workaround for controllers that misinterpret
1513bbe2ca75SPyun YongHyeon 		 * a status tag update and leave interrupts permanently
1514bbe2ca75SPyun YongHyeon 		 * disabled.
1515bbe2ca75SPyun YongHyeon 		 */
1516bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
1517bbe2ca75SPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM57765)
1518bbe2ca75SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1519b4a256acSPyun YongHyeon 	}
15205cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
152195d67482SBill Paul 
152295d67482SBill Paul 	/*
152395d67482SBill Paul 	 * Set up general mode register.
152495d67482SBill Paul 	 */
152550515680SPyun YongHyeon 	mode_ctl = bge_dma_swap_options(sc) | BGE_MODECTL_MAC_ATTN_INTR |
152650515680SPyun YongHyeon 	    BGE_MODECTL_HOST_SEND_BDS | BGE_MODECTL_TX_NO_PHDR_CSUM;
152795d67482SBill Paul 
152895d67482SBill Paul 	/*
152990447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
153090447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
153190447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
153290447aadSMarius Strobl 	 * certain bridges.
153390447aadSMarius Strobl 	 */
153490447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
153590447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
153650515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_FORCE_PCI32;
153790447aadSMarius Strobl 
153890447aadSMarius Strobl 	/*
15398cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
15408cb1383cSDoug Ambrisko 	 */
15418cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
154250515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_STACKUP;
154350515680SPyun YongHyeon 
154450515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
15458cb1383cSDoug Ambrisko 
15468cb1383cSDoug Ambrisko 	/*
1547ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1548c9ffd9f0SMarius Strobl 	 * properly by these devices.  Also ensure that INTx isn't disabled,
1549c9ffd9f0SMarius Strobl 	 * as these chips need it even when using MSI.
155095d67482SBill Paul 	 */
1551c9ffd9f0SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD,
1552c9ffd9f0SMarius Strobl 	    PCIM_CMD_INTxDIS | PCIM_CMD_MWIEN, 4);
155395d67482SBill Paul 
155495d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
15550c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
155695d67482SBill Paul 
155738cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
155838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
155938cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
156038cc658fSJohn Baldwin 
156138cc658fSJohn Baldwin 		/* Put PHY into ready state */
156238cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
156338cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
156438cc658fSJohn Baldwin 		DELAY(40);
156538cc658fSJohn Baldwin 	}
156638cc658fSJohn Baldwin 
156795d67482SBill Paul 	return (0);
156895d67482SBill Paul }
156995d67482SBill Paul 
157095d67482SBill Paul static int
15713f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
157295d67482SBill Paul {
157395d67482SBill Paul 	struct bge_rcb *rcb;
1574e907febfSPyun YongHyeon 	bus_size_t vrcb;
1575e907febfSPyun YongHyeon 	bge_hostaddr taddr;
1576bbe2ca75SPyun YongHyeon 	uint32_t dmactl, val;
15778a315a6dSPyun YongHyeon 	int i, limit;
157895d67482SBill Paul 
157995d67482SBill Paul 	/*
158095d67482SBill Paul 	 * Initialize the memory window pointer register so that
158195d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
158295d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
158395d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
158495d67482SBill Paul 	 */
158595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
158695d67482SBill Paul 
1587822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1588822f63fcSBill Paul 
15897ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
159095d67482SBill Paul 		/* Configure mbuf memory pool */
15910dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1592822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1593822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1594822f63fcSBill Paul 		else
159595d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
159695d67482SBill Paul 
159795d67482SBill Paul 		/* Configure DMA resource pool */
15980434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
15990434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
160095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
16010434d1b8SBill Paul 	}
160295d67482SBill Paul 
160395d67482SBill Paul 	/* Configure mbuf pool watermarks */
160450515680SPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
16051108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
16061108273aSPyun YongHyeon 		if (sc->bge_ifp->if_mtu > ETHERMTU) {
16071108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
16081108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
16091108273aSPyun YongHyeon 		} else {
16101108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
16111108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
16121108273aSPyun YongHyeon 		}
16131108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc)) {
1614fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1615fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1616fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
161738cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
161838cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
161938cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
162038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
162138cc658fSJohn Baldwin 	} else {
162238cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
162338cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
162438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
162538cc658fSJohn Baldwin 	}
162695d67482SBill Paul 
162795d67482SBill Paul 	/* Configure DMA resource watermarks */
162895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
162995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
163095d67482SBill Paul 
163195d67482SBill Paul 	/* Enable buffer manager */
1632bbe2ca75SPyun YongHyeon 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
1633bbe2ca75SPyun YongHyeon 	/*
1634bbe2ca75SPyun YongHyeon 	 * Change the arbitration algorithm of TXMBUF read request to
1635bbe2ca75SPyun YongHyeon 	 * round-robin instead of priority based for BCM5719.  When
1636bbe2ca75SPyun YongHyeon 	 * TXFIFO is almost empty, RDMA will hold its request until
1637bbe2ca75SPyun YongHyeon 	 * TXFIFO is not almost empty.
1638bbe2ca75SPyun YongHyeon 	 */
1639bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
1640bbe2ca75SPyun YongHyeon 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
1641bbe2ca75SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
164295d67482SBill Paul 
164395d67482SBill Paul 	/* Poll for buffer manager start indication */
164495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1645d5d23857SJung-uk Kim 		DELAY(10);
16460c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
164795d67482SBill Paul 			break;
164895d67482SBill Paul 	}
164995d67482SBill Paul 
165095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
16515a147ba6SPyun YongHyeon 		device_printf(sc->bge_dev, "buffer manager failed to start\n");
165295d67482SBill Paul 		return (ENXIO);
165395d67482SBill Paul 	}
165495d67482SBill Paul 
165595d67482SBill Paul 	/* Enable flow-through queues */
16560c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
165795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
165895d67482SBill Paul 
165995d67482SBill Paul 	/* Wait until queue initialization is complete */
166095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1661d5d23857SJung-uk Kim 		DELAY(10);
166295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
166395d67482SBill Paul 			break;
166495d67482SBill Paul 	}
166595d67482SBill Paul 
166695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1667fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
166895d67482SBill Paul 		return (ENXIO);
166995d67482SBill Paul 	}
167095d67482SBill Paul 
16718a315a6dSPyun YongHyeon 	/*
16728a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
16738a315a6dSPyun YongHyeon 	 *
16748a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
16758a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
16768a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
16778a315a6dSPyun YongHyeon 	 *
16788a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
16798a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
16808a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
16818a315a6dSPyun YongHyeon 	 *   to the controller.
16828a315a6dSPyun YongHyeon 	 *
16838a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
16848a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
16858a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
16868a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
16878a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
16888a315a6dSPyun YongHyeon 	 *   be disabled.
16898a315a6dSPyun YongHyeon 	 *
16908a315a6dSPyun YongHyeon 	 * Receive Return Ring
16918a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
16928a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
16938a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
16948a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
16958a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
16968a315a6dSPyun YongHyeon 	 *
16978a315a6dSPyun YongHyeon 	 * Send Ring
16988a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
16998a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
17008a315a6dSPyun YongHyeon 	 */
17018a315a6dSPyun YongHyeon 
17028a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
1703f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1704f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1705f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1706f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1707f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1708f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1709f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
17101108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
17111108273aSPyun YongHyeon 		/*
17121108273aSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
17131108273aSPyun YongHyeon 		 * Bits 15-2 : Maximum RX frame size
17141108273aSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
17151108273aSPyun YongHyeon 		 * Bit 0     : Reserved
17161108273aSPyun YongHyeon 		 */
17171108273aSPyun YongHyeon 		rcb->bge_maxlen_flags =
17181108273aSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
17191108273aSPyun YongHyeon 	} else if (BGE_IS_5705_PLUS(sc)) {
17208a315a6dSPyun YongHyeon 		/*
17218a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
17228a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
17238a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
17248a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
17258a315a6dSPyun YongHyeon 		 */
17260434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
17278a315a6dSPyun YongHyeon 	} else {
17288a315a6dSPyun YongHyeon 		/*
17298a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
17308a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
17318a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
17328a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
17338a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
17348a315a6dSPyun YongHyeon 		 */
17350434d1b8SBill Paul 		rcb->bge_maxlen_flags =
17360434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
17378a315a6dSPyun YongHyeon 	}
1738bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
173950515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
174050515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
17411108273aSPyun YongHyeon 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
17421108273aSPyun YongHyeon 	else
174395d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
17448a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
17450c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
17460c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
174767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
174867111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
174995d67482SBill Paul 
17508a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
17518a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
17528a315a6dSPyun YongHyeon 
175395d67482SBill Paul 	/*
17548a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
17558a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
17568a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
175795d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
175895d67482SBill Paul 	 * high enough to require it).
175995d67482SBill Paul 	 */
17604c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1761f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
17628a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
1763f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1764f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1765f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1766f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1767f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1768f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1769f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
17701be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
17711be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
1772bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
177350515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
177450515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
17751108273aSPyun YongHyeon 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
17761108273aSPyun YongHyeon 		else
177795d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
177867111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
177967111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
178067111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
178167111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
17828a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
17830434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
17840434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
178567111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
17868a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
17878a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
17888a315a6dSPyun YongHyeon 	}
178995d67482SBill Paul 
17908a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
17915e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
1792f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
179367111612SJohn Polstra 		rcb->bge_maxlen_flags =
179467111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
17950434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
17960434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
17978a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
17988a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
17990434d1b8SBill Paul 	}
180095d67482SBill Paul 
1801ca4f8986SPyun YongHyeon 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
1802ca4f8986SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
1803427d3f33SPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
1804427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
1805427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
18068d5f7181SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
18078d5f7181SPyun YongHyeon 			    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
1808ca4f8986SPyun YongHyeon 	}
180995d67482SBill Paul 	/*
18108a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
18118a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
18128a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
18138a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
18148a315a6dSPyun YongHyeon 	 *
181595d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
181695d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
181795d67482SBill Paul 	 * each ring.
18189ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
18199ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
18209ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
18219ba784dbSScott Long 	 * are reports that it might not need to be so strict.
182238cc658fSJohn Baldwin 	 *
182338cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
182438cc658fSJohn Baldwin 	 * well.
182595d67482SBill Paul 	 */
18265345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
18276f8718a3SScott Long 		val = 8;
18286f8718a3SScott Long 	else
18296f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
18306f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
18312a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
18322a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
18332a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
18341108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
18351108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
18361108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
18371108273aSPyun YongHyeon 	}
183895d67482SBill Paul 
183995d67482SBill Paul 	/*
18408a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
18418a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
18428a315a6dSPyun YongHyeon 	 * located in NIC memory.
184395d67482SBill Paul 	 */
18448a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
18458a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
18468a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
18478a315a6dSPyun YongHyeon 	else
18488a315a6dSPyun YongHyeon 		limit = 1;
1849e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
18508a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
1851e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1852e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1853e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1854e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
185595d67482SBill Paul 	}
185695d67482SBill Paul 
18578a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
1858e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1859e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1860e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1861e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1862bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
186350515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
186450515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
18651108273aSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
18661108273aSPyun YongHyeon 	else
1867e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1868e907febfSPyun YongHyeon 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
1869e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1870e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
187195d67482SBill Paul 
18728a315a6dSPyun YongHyeon 	/*
18738a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
18748a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
18758a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
18768a315a6dSPyun YongHyeon 	 */
1877bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
187850515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
187950515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
18801108273aSPyun YongHyeon 		/* Should be 17, use 16 until we get an SRAM map. */
18811108273aSPyun YongHyeon 		limit = 16;
18821108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc))
18838a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
1884b4a256acSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
1885b4a256acSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57765)
18868a315a6dSPyun YongHyeon 		limit = 4;
18878a315a6dSPyun YongHyeon 	else
18888a315a6dSPyun YongHyeon 		limit = 1;
18898a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
1890e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
18918a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
1892e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1893e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1894e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
18958a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
1896e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
189738cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
18983f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1899e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
190095d67482SBill Paul 	}
190195d67482SBill Paul 
190295d67482SBill Paul 	/*
19038a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
19048a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
19058a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
190695d67482SBill Paul 	 */
1907e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1908e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1909e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1910e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
19118a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1912e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1913e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
191495d67482SBill Paul 
191595d67482SBill Paul 	/* Set random backoff seed for TX */
191695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
19174a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
19184a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
19194a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
192095d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
192195d67482SBill Paul 
192295d67482SBill Paul 	/* Set inter-packet gap */
192350515680SPyun YongHyeon 	val = 0x2620;
192450515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
192550515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
192650515680SPyun YongHyeon 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
192750515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
192895d67482SBill Paul 
192995d67482SBill Paul 	/*
193095d67482SBill Paul 	 * Specify which ring to use for packets that don't match
193195d67482SBill Paul 	 * any RX rules.
193295d67482SBill Paul 	 */
193395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
193495d67482SBill Paul 
193595d67482SBill Paul 	/*
193695d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
193795d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
193895d67482SBill Paul 	 */
193995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
194095d67482SBill Paul 
194195d67482SBill Paul 	/* Inialize RX list placement stats mask. */
19420c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
194395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
194495d67482SBill Paul 
194595d67482SBill Paul 	/* Disable host coalescing until we get it set up */
194695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
194795d67482SBill Paul 
194895d67482SBill Paul 	/* Poll to make sure it's shut down. */
194995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1950d5d23857SJung-uk Kim 		DELAY(10);
195195d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
195295d67482SBill Paul 			break;
195395d67482SBill Paul 	}
195495d67482SBill Paul 
195595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1956fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1957fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
195895d67482SBill Paul 		return (ENXIO);
195995d67482SBill Paul 	}
196095d67482SBill Paul 
196195d67482SBill Paul 	/* Set up host coalescing defaults */
196295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
196395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
196495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
196595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
19667ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
196795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
196895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
19690434d1b8SBill Paul 	}
1970b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
1971b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
197295d67482SBill Paul 
197395d67482SBill Paul 	/* Set up address of statistics block */
19747ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
1975f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1976f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
197795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1978f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
19790434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
198095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
19810434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
19820434d1b8SBill Paul 	}
19830434d1b8SBill Paul 
19840434d1b8SBill Paul 	/* Set up address of status block */
1985f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1986f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
198795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1988f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
198995d67482SBill Paul 
199030f57f61SPyun YongHyeon 	/* Set up status block size. */
199130f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
1992864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
199330f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
1994864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
1995864104feSPyun YongHyeon 	} else {
199630f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
1997864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
1998864104feSPyun YongHyeon 	}
1999864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2000864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
2001864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
200230f57f61SPyun YongHyeon 
200395d67482SBill Paul 	/* Turn on host coalescing state machine */
200430f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
200595d67482SBill Paul 
200695d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
200795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
200895d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
200995d67482SBill Paul 
201095d67482SBill Paul 	/* Turn on RX list placement state machine */
201195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
201295d67482SBill Paul 
201395d67482SBill Paul 	/* Turn on RX list selector state machine. */
20147ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
201595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
201695d67482SBill Paul 
2017ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2018ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2019ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2020ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
2021ea3b4127SPyun YongHyeon 
2022ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
2023ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
2024ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
2025ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
2026ea3b4127SPyun YongHyeon 	else
2027ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
2028ea3b4127SPyun YongHyeon 
202995d67482SBill Paul 	/* Turn on DMA, clear stats */
2030ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
203195d67482SBill Paul 
203295d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
203395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
203495d67482SBill Paul 
203595d67482SBill Paul #ifdef notdef
203695d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
203795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
203895d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
203995d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
204095d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
204195d67482SBill Paul #endif
204295d67482SBill Paul 
204395d67482SBill Paul 	/* Turn on DMA completion state machine */
20447ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
204595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
204695d67482SBill Paul 
20476f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
20486f8718a3SScott Long 
20496f8718a3SScott Long 	/* Enable host coalescing bug fix. */
2050a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
20513889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
20526f8718a3SScott Long 
20537aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
20547aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
20557aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
20567aa4b937SPyun YongHyeon 
205795d67482SBill Paul 	/* Turn on write DMA state machine */
20586f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
20594f09c4c7SMarius Strobl 	DELAY(40);
206095d67482SBill Paul 
206195d67482SBill Paul 	/* Turn on read DMA state machine */
20624f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
20631108273aSPyun YongHyeon 
20641108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
20651108273aSPyun YongHyeon 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
20661108273aSPyun YongHyeon 
2067a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2068a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2069a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2070a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2071a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2072a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
20734f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
20744f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
20751108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2076ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
20771108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_TSO3 ||
20781108273aSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
207955a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
208055a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
208155a24a05SPyun YongHyeon 	}
208250515680SPyun YongHyeon 
2083e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
208450515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
208550515680SPyun YongHyeon 			BGE_RDMAMODE_H2BNC_VLAN_DET;
2086e3215f76SPyun YongHyeon 		/*
2087e3215f76SPyun YongHyeon 		 * Allow multiple outstanding read requests from
2088e3215f76SPyun YongHyeon 		 * non-LSO read DMA engine.
2089e3215f76SPyun YongHyeon 		 */
2090e3215f76SPyun YongHyeon 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
2091e3215f76SPyun YongHyeon 	}
209250515680SPyun YongHyeon 
2093d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2094d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2095d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
20961108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
20971108273aSPyun YongHyeon 	    BGE_IS_5717_PLUS(sc)) {
2098bbe2ca75SPyun YongHyeon 		dmactl = CSR_READ_4(sc, BGE_RDMA_RSRVCTRL);
2099bbe2ca75SPyun YongHyeon 		/*
2100bbe2ca75SPyun YongHyeon 		 * Adjust tx margin to prevent TX data corruption and
2101bbe2ca75SPyun YongHyeon 		 * fix internal FIFO overflow.
2102bbe2ca75SPyun YongHyeon 		 */
210350515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
210450515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2105bbe2ca75SPyun YongHyeon 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
2106bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
2107bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
2108bbe2ca75SPyun YongHyeon 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
2109bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
2110bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
2111bbe2ca75SPyun YongHyeon 		}
2112d255f2a9SPyun YongHyeon 		/*
2113d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
2114d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
2115d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
2116d255f2a9SPyun YongHyeon 		 */
2117bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_RSRVCTRL, dmactl |
2118d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2119d255f2a9SPyun YongHyeon 	}
2120bbe2ca75SPyun YongHyeon 
2121e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719) {
2122bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2123bbe2ca75SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2124bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2125bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2126e3215f76SPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2127e3215f76SPyun YongHyeon 		/*
2128e3215f76SPyun YongHyeon 		 * Allow 4KB burst length reads for non-LSO frames.
2129e3215f76SPyun YongHyeon 		 * Enable 512B burst length reads for buffer descriptors.
2130e3215f76SPyun YongHyeon 		 */
2131e3215f76SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2132e3215f76SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2133e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
2134e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2135bbe2ca75SPyun YongHyeon 	}
2136bbe2ca75SPyun YongHyeon 
21374f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
21384f09c4c7SMarius Strobl 	DELAY(40);
213995d67482SBill Paul 
214095d67482SBill Paul 	/* Turn on RX data completion state machine */
214195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
214295d67482SBill Paul 
214395d67482SBill Paul 	/* Turn on RX BD initiator state machine */
214495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
214595d67482SBill Paul 
214695d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
214795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
214895d67482SBill Paul 
214995d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
21507ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
215195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
215295d67482SBill Paul 
215395d67482SBill Paul 	/* Turn on send BD completion state machine */
215495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
215595d67482SBill Paul 
215695d67482SBill Paul 	/* Turn on send data completion state machine */
2157a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2158a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2159a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2160a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
216195d67482SBill Paul 
216295d67482SBill Paul 	/* Turn on send data initiator state machine */
21631108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
21641108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
21651108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2166ca3f1187SPyun YongHyeon 	else
216795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
216895d67482SBill Paul 
216995d67482SBill Paul 	/* Turn on send BD initiator state machine */
217095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
217195d67482SBill Paul 
217295d67482SBill Paul 	/* Turn on send BD selector state machine */
217395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
217495d67482SBill Paul 
21750c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
217695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
217795d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
217895d67482SBill Paul 
217995d67482SBill Paul 	/* ack/clear link change events */
218095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
21810434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
21820434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2183f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
218495d67482SBill Paul 
21856ede2cfaSPyun YongHyeon 	/*
21866ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
21876ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
21886ede2cfaSPyun YongHyeon 	 */
2189652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
219095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2191a1d52896SBill Paul 	} else {
21927ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
21937ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
21947ed3f0f0SPyun YongHyeon 			DELAY(80);
21957ed3f0f0SPyun YongHyeon 		}
21961f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
21974c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2198a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2199a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2200a1d52896SBill Paul 	}
220195d67482SBill Paul 
22021f313773SOleg Bulyzhin 	/*
22031f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
22041f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
22051f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
22061f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
22071f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
22081f313773SOleg Bulyzhin 	 */
22091f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
22101f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
22111f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
22121f313773SOleg Bulyzhin 
221395d67482SBill Paul 	/* Enable link state change attentions. */
221495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
221595d67482SBill Paul 
221695d67482SBill Paul 	return (0);
221795d67482SBill Paul }
221895d67482SBill Paul 
22194c0da0ffSGleb Smirnoff const struct bge_revision *
22204c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
22214c0da0ffSGleb Smirnoff {
22224c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
22234c0da0ffSGleb Smirnoff 
22244c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
22254c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
22264c0da0ffSGleb Smirnoff 			return (br);
22274c0da0ffSGleb Smirnoff 	}
22284c0da0ffSGleb Smirnoff 
22294c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
22304c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
22314c0da0ffSGleb Smirnoff 			return (br);
22324c0da0ffSGleb Smirnoff 	}
22334c0da0ffSGleb Smirnoff 
22344c0da0ffSGleb Smirnoff 	return (NULL);
22354c0da0ffSGleb Smirnoff }
22364c0da0ffSGleb Smirnoff 
22374c0da0ffSGleb Smirnoff const struct bge_vendor *
22384c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
22394c0da0ffSGleb Smirnoff {
22404c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
22414c0da0ffSGleb Smirnoff 
22424c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
22434c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
22444c0da0ffSGleb Smirnoff 			return (v);
22454c0da0ffSGleb Smirnoff 
22464c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
22474c0da0ffSGleb Smirnoff 	return (NULL);
22484c0da0ffSGleb Smirnoff }
22494c0da0ffSGleb Smirnoff 
225095d67482SBill Paul /*
225195d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
22524c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
22534c0da0ffSGleb Smirnoff  *
22544c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
22557c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
22567c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
22577c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
22587c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
225995d67482SBill Paul  */
226095d67482SBill Paul static int
22613f74909aSGleb Smirnoff bge_probe(device_t dev)
226295d67482SBill Paul {
2263978f2704SMarius Strobl 	char buf[96];
2264978f2704SMarius Strobl 	char model[64];
2265978f2704SMarius Strobl 	const struct bge_revision *br;
2266978f2704SMarius Strobl 	const char *pname;
22674c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
2268978f2704SMarius Strobl 	const struct bge_type *t = bge_devs;
2269978f2704SMarius Strobl 	const struct bge_vendor *v;
2270978f2704SMarius Strobl 	uint32_t id;
2271978f2704SMarius Strobl 	uint16_t did, vid;
227295d67482SBill Paul 
227395d67482SBill Paul 	sc->bge_dev = dev;
22747c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
22757c929cf9SJung-uk Kim 	did = pci_get_device(dev);
22764c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
22777c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2278a5779553SStanislav Sedov 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2279a5779553SStanislav Sedov 			    BGE_PCIMISCCTL_ASICREV_SHIFT;
22801108273aSPyun YongHyeon 			if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
22811108273aSPyun YongHyeon 				/*
22821108273aSPyun YongHyeon 				 * Find the ASCI revision.  Different chips
22831108273aSPyun YongHyeon 				 * use different registers.
22841108273aSPyun YongHyeon 				 */
22851108273aSPyun YongHyeon 				switch (pci_get_device(dev)) {
22861108273aSPyun YongHyeon 				case BCOM_DEVICEID_BCM5717:
22871108273aSPyun YongHyeon 				case BCOM_DEVICEID_BCM5718:
2288bbe2ca75SPyun YongHyeon 				case BCOM_DEVICEID_BCM5719:
228950515680SPyun YongHyeon 				case BCOM_DEVICEID_BCM5720:
22901108273aSPyun YongHyeon 					id = pci_read_config(dev,
22911108273aSPyun YongHyeon 					    BGE_PCI_GEN2_PRODID_ASICREV, 4);
22921108273aSPyun YongHyeon 					break;
2293b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57761:
2294b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57765:
2295b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57781:
2296b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57785:
2297b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57791:
2298b4a256acSPyun YongHyeon 				case BCOM_DEVICEID_BCM57795:
2299b4a256acSPyun YongHyeon 					id = pci_read_config(dev,
2300b4a256acSPyun YongHyeon 					    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2301b4a256acSPyun YongHyeon 					break;
23021108273aSPyun YongHyeon 				default:
2303a5779553SStanislav Sedov 					id = pci_read_config(dev,
2304a5779553SStanislav Sedov 					    BGE_PCI_PRODID_ASICREV, 4);
23051108273aSPyun YongHyeon 				}
23061108273aSPyun YongHyeon 			}
23074c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
23087c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
2309852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2310852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
23114e35d186SJung-uk Kim 				snprintf(model, 64, "%s", pname);
23124e35d186SJung-uk Kim 			else
2313978f2704SMarius Strobl 				snprintf(model, 64, "%s %s", v->v_name,
23147c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
23157c929cf9SJung-uk Kim 				    "NetXtreme Ethernet Controller");
2316a5779553SStanislav Sedov 			snprintf(buf, 96, "%s, %sASIC rev. %#08x", model,
2317a5779553SStanislav Sedov 			    br != NULL ? "" : "unknown ", id);
23184c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
231995d67482SBill Paul 			return (0);
232095d67482SBill Paul 		}
232195d67482SBill Paul 		t++;
232295d67482SBill Paul 	}
232395d67482SBill Paul 
232495d67482SBill Paul 	return (ENXIO);
232595d67482SBill Paul }
232695d67482SBill Paul 
2327f41ac2beSBill Paul static void
23283f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2329f41ac2beSBill Paul {
2330f41ac2beSBill Paul 	int i;
2331f41ac2beSBill Paul 
23323f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2333f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2334f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
23350ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2336f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2337f41ac2beSBill Paul 	}
2338943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2339943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2340943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2341f41ac2beSBill Paul 
23423f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2343f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2344f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2345f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2346f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2347f41ac2beSBill Paul 	}
2348943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2349943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2350943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2351f41ac2beSBill Paul 
23523f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2353f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2354f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
23550ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2356f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2357f41ac2beSBill Paul 	}
2358f41ac2beSBill Paul 
23590ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
23600ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2361c0220d81SPyun YongHyeon 	if (sc->bge_cdata.bge_mtag_jumbo)
2362c0220d81SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
23630ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
23640ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2365f41ac2beSBill Paul 
2366f41ac2beSBill Paul 
23673f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2368e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
2369e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2370e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2371e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
2372f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2373f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2374f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2375f41ac2beSBill Paul 
2376f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2377f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2378f41ac2beSBill Paul 
23793f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2380e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
2381e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2382e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2383e65bed95SPyun YongHyeon 
2384e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
2385e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
2386f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2387f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2388f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2389f41ac2beSBill Paul 
2390f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2391f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2392f41ac2beSBill Paul 
23933f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2394e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
2395e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2396e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2397e65bed95SPyun YongHyeon 
2398e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
2399e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
2400f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2401f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2402f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2403f41ac2beSBill Paul 
2404f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2405f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2406f41ac2beSBill Paul 
24073f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2408e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
2409e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2410e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2411e65bed95SPyun YongHyeon 
2412e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2413f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2414f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2415f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2416f41ac2beSBill Paul 
2417f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2418f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2419f41ac2beSBill Paul 
24203f74909aSGleb Smirnoff 	/* Destroy status block. */
2421e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
2422e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2423e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2424e65bed95SPyun YongHyeon 
2425e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2426f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2427f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2428f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2429f41ac2beSBill Paul 
2430f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2431f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2432f41ac2beSBill Paul 
24333f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2434e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
2435e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2436e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2437e65bed95SPyun YongHyeon 
2438e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2439f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2440f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2441f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2442f41ac2beSBill Paul 
2443f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2444f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2445f41ac2beSBill Paul 
24465b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
24475b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
24485b610048SPyun YongHyeon 
24493f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2450f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2451f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2452f41ac2beSBill Paul }
2453f41ac2beSBill Paul 
2454f41ac2beSBill Paul static int
24555b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
24565b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
24575b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2458f41ac2beSBill Paul {
24593f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
2460f681b29aSPyun YongHyeon 	bus_addr_t lowaddr;
24615b610048SPyun YongHyeon 	bus_size_t ring_end;
24625b610048SPyun YongHyeon 	int error;
2463f41ac2beSBill Paul 
24645b610048SPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
24655b610048SPyun YongHyeon again:
24665b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
24675b610048SPyun YongHyeon 	    alignment, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
24685b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
24695b610048SPyun YongHyeon 	if (error != 0) {
24705b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
24715b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
24725b610048SPyun YongHyeon 		return (ENOMEM);
24735b610048SPyun YongHyeon 	}
24745b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
24755b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
24765b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
24775b610048SPyun YongHyeon 	if (error != 0) {
24785b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
24795b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
24805b610048SPyun YongHyeon 		return (ENOMEM);
24815b610048SPyun YongHyeon 	}
24825b610048SPyun YongHyeon 	/* Load the address of the ring. */
24835b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
24845b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
24855b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
24865b610048SPyun YongHyeon 	if (error != 0) {
24875b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
24885b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
24895b610048SPyun YongHyeon 		return (ENOMEM);
24905b610048SPyun YongHyeon 	}
24915b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
24925b610048SPyun YongHyeon 	ring_end = *paddr + maxsize;
24935b610048SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0 &&
24945b610048SPyun YongHyeon 	    BGE_ADDR_HI(*paddr) != BGE_ADDR_HI(ring_end)) {
24955b610048SPyun YongHyeon 		/*
24965b610048SPyun YongHyeon 		 * 4GB boundary crossed.  Limit maximum allowable DMA
24975b610048SPyun YongHyeon 		 * address space to 32bit and try again.
24985b610048SPyun YongHyeon 		 */
24995b610048SPyun YongHyeon 		bus_dmamap_unload(*tag, *map);
25005b610048SPyun YongHyeon 		bus_dmamem_free(*tag, *ring, *map);
25015b610048SPyun YongHyeon 		bus_dma_tag_destroy(*tag);
25025b610048SPyun YongHyeon 		if (bootverbose)
25035b610048SPyun YongHyeon 			device_printf(sc->bge_dev, "4GB boundary crossed, "
25045b610048SPyun YongHyeon 			    "limit DMA address space to 32bit for %s\n", msg);
25055b610048SPyun YongHyeon 		*ring = NULL;
25065b610048SPyun YongHyeon 		*tag = NULL;
25075b610048SPyun YongHyeon 		*map = NULL;
25085b610048SPyun YongHyeon 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
25095b610048SPyun YongHyeon 		goto again;
25105b610048SPyun YongHyeon 	}
25115b610048SPyun YongHyeon 	return (0);
25125b610048SPyun YongHyeon }
25135b610048SPyun YongHyeon 
25145b610048SPyun YongHyeon static int
25155b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
25165b610048SPyun YongHyeon {
25175b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2518f5459d4cSPyun YongHyeon 	bus_size_t boundary, sbsz, rxmaxsegsz, txsegsz, txmaxsegsz;
25195b610048SPyun YongHyeon 	int i, error;
2520f41ac2beSBill Paul 
2521f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2522f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2523f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2524f41ac2beSBill Paul 	/*
2525f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2526f41ac2beSBill Paul 	 */
25274eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2528f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
25294eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
25304eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2531e65bed95SPyun YongHyeon 	if (error != 0) {
2532fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2533fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2534e65bed95SPyun YongHyeon 		return (ENOMEM);
2535e65bed95SPyun YongHyeon 	}
2536e65bed95SPyun YongHyeon 
25375b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
25385b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
25395b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
25405b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
25415b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
25425b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
25435b610048SPyun YongHyeon 	if (error)
25445b610048SPyun YongHyeon 		return (error);
25455b610048SPyun YongHyeon 
25465b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
25475b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
25485b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
25495b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
25505b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
25515b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
25525b610048SPyun YongHyeon 	if (error)
25535b610048SPyun YongHyeon 		return (error);
25545b610048SPyun YongHyeon 
25555b610048SPyun YongHyeon 	/* Create tag for TX ring. */
25565b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
25575b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
25585b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
25595b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
25605b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
25615b610048SPyun YongHyeon 	if (error)
25625b610048SPyun YongHyeon 		return (error);
25635b610048SPyun YongHyeon 
2564f41ac2beSBill Paul 	/*
25655b610048SPyun YongHyeon 	 * Create tag for status block.
25665b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
25675b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
25685b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
25695b610048SPyun YongHyeon 	 * of configured number of ring.
2570f41ac2beSBill Paul 	 */
25715b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
25725b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
25735b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
25745b610048SPyun YongHyeon 	else
25755b610048SPyun YongHyeon 		sbsz = 32;
25765b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
25775b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
25785b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
25795b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
25805b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
25815b610048SPyun YongHyeon 	if (error)
25825b610048SPyun YongHyeon 		return (error);
25835b610048SPyun YongHyeon 
258412c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
258512c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
258612c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
258712c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
258812c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
258912c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
259012c65daeSPyun YongHyeon 	if (error)
259112c65daeSPyun YongHyeon 		return (error);
259212c65daeSPyun YongHyeon 
25935b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
25945b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
25955b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
25965b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
25975b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
25985b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
25995b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
26005b610048SPyun YongHyeon 		if (error)
26015b610048SPyun YongHyeon 			return (error);
26025b610048SPyun YongHyeon 	}
26035b610048SPyun YongHyeon 
26045b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
26055b610048SPyun YongHyeon 	boundary = 0;
2606d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
260738cc6151SPyun YongHyeon 		boundary = BGE_DMA_BNDRY;
2608d2ffe15aSPyun YongHyeon 		/*
2609d2ffe15aSPyun YongHyeon 		 * XXX
2610d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
2611d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
2612d2ffe15aSPyun YongHyeon 		 * Limiting DMA address space to 32bits seems to address
2613d2ffe15aSPyun YongHyeon 		 * it.
2614d2ffe15aSPyun YongHyeon 		 */
2615d2ffe15aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_PCIX)
2616d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
2617d2ffe15aSPyun YongHyeon 	}
26185b610048SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
261992c0b021SPyun YongHyeon 	    1, boundary, lowaddr, BUS_SPACE_MAXADDR, NULL,
26205b610048SPyun YongHyeon 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
26215b610048SPyun YongHyeon 	    0, NULL, NULL, &sc->bge_cdata.bge_buffer_tag);
26225b610048SPyun YongHyeon 	if (error != 0) {
26235b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
26245b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
26255b610048SPyun YongHyeon 		return (ENOMEM);
26265b610048SPyun YongHyeon 	}
26275b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
26281108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2629ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
2630ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
2631ca3f1187SPyun YongHyeon 	} else {
2632ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
2633ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
2634ca3f1187SPyun YongHyeon 	}
26355b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
2636ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
2637ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
2638ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
2639f41ac2beSBill Paul 
2640f41ac2beSBill Paul 	if (error) {
26410ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
26420ac56796SPyun YongHyeon 		return (ENOMEM);
26430ac56796SPyun YongHyeon 	}
26440ac56796SPyun YongHyeon 
26455b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
2646f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
2647f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
2648f5459d4cSPyun YongHyeon 	else
2649f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
26505b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
2651f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
2652f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
26530ac56796SPyun YongHyeon 
26540ac56796SPyun YongHyeon 	if (error) {
26550ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
2656f41ac2beSBill Paul 		return (ENOMEM);
2657f41ac2beSBill Paul 	}
2658f41ac2beSBill Paul 
26593f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
2660943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2661943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
2662943787f3SPyun YongHyeon 	if (error) {
2663943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
2664943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
2665943787f3SPyun YongHyeon 		return (ENOMEM);
2666943787f3SPyun YongHyeon 	}
2667f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
26680ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2669f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
2670f41ac2beSBill Paul 		if (error) {
2671fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
2672fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
2673f41ac2beSBill Paul 			return (ENOMEM);
2674f41ac2beSBill Paul 		}
2675f41ac2beSBill Paul 	}
2676f41ac2beSBill Paul 
26773f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
2678f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
26790ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
2680f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
2681f41ac2beSBill Paul 		if (error) {
2682fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
26830ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
2684f41ac2beSBill Paul 			return (ENOMEM);
2685f41ac2beSBill Paul 		}
2686f41ac2beSBill Paul 	}
2687f41ac2beSBill Paul 
26885b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
26894c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
26905b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
26918a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
26921be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
26931be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
2694f41ac2beSBill Paul 		if (error) {
2695fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
26963f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
2697f41ac2beSBill Paul 			return (ENOMEM);
2698f41ac2beSBill Paul 		}
26993f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
2700943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2701943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
2702943787f3SPyun YongHyeon 		if (error) {
2703943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
27041b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
2705943787f3SPyun YongHyeon 			return (ENOMEM);
2706943787f3SPyun YongHyeon 		}
2707f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2708f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2709f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2710f41ac2beSBill Paul 			if (error) {
2711fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
27123f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
2713f41ac2beSBill Paul 				return (ENOMEM);
2714f41ac2beSBill Paul 			}
2715f41ac2beSBill Paul 		}
2716f41ac2beSBill Paul 	}
2717f41ac2beSBill Paul 
2718f41ac2beSBill Paul 	return (0);
2719f41ac2beSBill Paul }
2720f41ac2beSBill Paul 
2721bf6ef57aSJohn Polstra /*
2722bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
2723bf6ef57aSJohn Polstra  */
2724bf6ef57aSJohn Polstra static int
2725bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
2726bf6ef57aSJohn Polstra {
2727bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
272855aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
2729bf6ef57aSJohn Polstra 
273055aaf894SMarius Strobl 	d = pci_get_domain(dev);
2731bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
2732bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
2733bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
2734bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
273555aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
2736bf6ef57aSJohn Polstra 			return (1);
2737bf6ef57aSJohn Polstra 	return (0);
2738bf6ef57aSJohn Polstra }
2739bf6ef57aSJohn Polstra 
2740bf6ef57aSJohn Polstra /*
2741bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
2742bf6ef57aSJohn Polstra  */
2743bf6ef57aSJohn Polstra static int
2744bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
2745bf6ef57aSJohn Polstra {
2746bf6ef57aSJohn Polstra 	int can_use_msi = 0;
2747bf6ef57aSJohn Polstra 
2748*d9fc28e4SPyun YongHyeon 	if (sc->bge_msi == 0)
27495c952e8dSPyun YongHyeon 		return (0);
27505c952e8dSPyun YongHyeon 
27511108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
27521108273aSPyun YongHyeon #ifdef DEVICE_POLLING
27531108273aSPyun YongHyeon 	return (0);
27541108273aSPyun YongHyeon #endif
2755bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
2756a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
2757bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
2758bf6ef57aSJohn Polstra 		/*
2759a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
2760a8376f70SMarius Strobl 		 * configured in single-port mode.
2761bf6ef57aSJohn Polstra 		 */
2762bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
2763bf6ef57aSJohn Polstra 			can_use_msi = 1;
2764bf6ef57aSJohn Polstra 		break;
2765bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
2766bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
2767bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
2768bf6ef57aSJohn Polstra 			can_use_msi = 1;
2769bf6ef57aSJohn Polstra 		break;
2770a8376f70SMarius Strobl 	default:
2771a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
2772bf6ef57aSJohn Polstra 			can_use_msi = 1;
2773bf6ef57aSJohn Polstra 	}
2774bf6ef57aSJohn Polstra 	return (can_use_msi);
2775bf6ef57aSJohn Polstra }
2776bf6ef57aSJohn Polstra 
277795d67482SBill Paul static int
27783f74909aSGleb Smirnoff bge_attach(device_t dev)
277995d67482SBill Paul {
278095d67482SBill Paul 	struct ifnet *ifp;
278195d67482SBill Paul 	struct bge_softc *sc;
27824f0794ffSBjoern A. Zeeb 	uint32_t hwcfg = 0, misccfg;
278308013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
2784fb772a6cSMarius Strobl 	int capmask, error, f, msicount, phy_addr, reg, rid, trys;
278595d67482SBill Paul 
278695d67482SBill Paul 	sc = device_get_softc(dev);
278795d67482SBill Paul 	sc->bge_dev = dev;
278895d67482SBill Paul 
2789dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
2790dfe0df9aSPyun YongHyeon 
279195d67482SBill Paul 	/*
279295d67482SBill Paul 	 * Map control/status registers.
279395d67482SBill Paul 	 */
279495d67482SBill Paul 	pci_enable_busmaster(dev);
279595d67482SBill Paul 
2796736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
27975f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
279844f8f2fcSMarius Strobl 	    RF_ACTIVE);
279995d67482SBill Paul 
280095d67482SBill Paul 	if (sc->bge_res == NULL) {
2801fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
280295d67482SBill Paul 		error = ENXIO;
280395d67482SBill Paul 		goto fail;
280495d67482SBill Paul 	}
280595d67482SBill Paul 
28064f09c4c7SMarius Strobl 	/* Save various chip information. */
2807e53d81eeSPaul Saab 	sc->bge_chipid =
2808a5779553SStanislav Sedov 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2809a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
28101108273aSPyun YongHyeon 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_USE_PRODID_REG) {
28111108273aSPyun YongHyeon 		/*
28121108273aSPyun YongHyeon 		 * Find the ASCI revision.  Different chips use different
28131108273aSPyun YongHyeon 		 * registers.
28141108273aSPyun YongHyeon 		 */
28151108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
28161108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5717:
28171108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
2818bbe2ca75SPyun YongHyeon 		case BCOM_DEVICEID_BCM5719:
281950515680SPyun YongHyeon 		case BCOM_DEVICEID_BCM5720:
28201108273aSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
28211108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
28221108273aSPyun YongHyeon 			break;
2823b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57761:
2824b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57765:
2825b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57781:
2826b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57785:
2827b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57791:
2828b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57795:
2829b4a256acSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
2830b4a256acSPyun YongHyeon 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2831b4a256acSPyun YongHyeon 			break;
28321108273aSPyun YongHyeon 		default:
28331108273aSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
28341108273aSPyun YongHyeon 			    BGE_PCI_PRODID_ASICREV, 4);
28351108273aSPyun YongHyeon 		}
28361108273aSPyun YongHyeon 	}
2837e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2838e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2839e53d81eeSPaul Saab 
2840a813ed78SPyun YongHyeon 	/* Set default PHY address. */
28418e5d93dbSMarius Strobl 	phy_addr = 1;
28421108273aSPyun YongHyeon 	 /*
28431108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
28441108273aSPyun YongHyeon 	  *
28451108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
28461108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
28471108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
28481108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
28491108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
2850bbe2ca75SPyun YongHyeon 	  * BCM5719  |   1   |   8   |   2   |   9   |
285150515680SPyun YongHyeon 	  * BCM5720  |   1   |   8   |   2   |   9   |
28521108273aSPyun YongHyeon 	  *
28531108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
28541108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
28551108273aSPyun YongHyeon 	  */
2856bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
285750515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
285850515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
28591108273aSPyun YongHyeon 		f = pci_get_function(dev);
28601108273aSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5717_A0) {
28611108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
28621108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
28631108273aSPyun YongHyeon 				phy_addr = f + 8;
28641108273aSPyun YongHyeon 			else
28651108273aSPyun YongHyeon 				phy_addr = f + 1;
2866bbe2ca75SPyun YongHyeon 		} else {
28671108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
28681108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
28691108273aSPyun YongHyeon 				phy_addr = f + 8;
28701108273aSPyun YongHyeon 			else
28711108273aSPyun YongHyeon 				phy_addr = f + 1;
28721108273aSPyun YongHyeon 		}
28731108273aSPyun YongHyeon 	}
2874a813ed78SPyun YongHyeon 
287586543395SJung-uk Kim 	/*
287638cc658fSJohn Baldwin 	 * Don't enable Ethernet@WireSpeed for the 5700, 5906, or the
287786543395SJung-uk Kim 	 * 5705 A0 and A1 chips.
287886543395SJung-uk Kim 	 */
2879cb777a07SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
2880cb777a07SPyun YongHyeon 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
2881cb777a07SPyun YongHyeon 	    (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
2882cb777a07SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)) ||
2883cb777a07SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5906)
2884cb777a07SPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
288586543395SJung-uk Kim 
28865fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
28875fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
288808013fd3SMarius Strobl 
28890dae9719SJung-uk Kim 	/* Save chipset family. */
28900dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
28911108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
2892bbe2ca75SPyun YongHyeon 	case BGE_ASICREV_BCM5719:
289350515680SPyun YongHyeon 	case BGE_ASICREV_BCM5720:
2894b4a256acSPyun YongHyeon 	case BGE_ASICREV_BCM57765:
28951108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
28961108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
2897b4a256acSPyun YongHyeon 		    BGE_FLAG_JUMBO_FRAME;
2898bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
2899bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
2900bbe2ca75SPyun YongHyeon 			/* Jumbo frame on BCM5719 A0 does not work. */
2901463a7e27SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_JUMBO;
2902bbe2ca75SPyun YongHyeon 		}
29031108273aSPyun YongHyeon 		break;
2904a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
2905a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
2906a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
2907a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
2908a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
2909a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
2910a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
2911a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
2912a5779553SStanislav Sedov 		break;
29130dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
29140dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
29150dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
29160dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
29177ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
29180dae9719SJung-uk Kim 		break;
29190dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
29200dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
29210dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
2922f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
29239fe569d8SXin LI 		/* FALLTHROUGH */
29240dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
29250dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
292638cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
29270dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
29289fe569d8SXin LI 		/* FALLTHROUGH */
29290dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
29300dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
29310dae9719SJung-uk Kim 		break;
29320dae9719SJung-uk Kim 	}
29330dae9719SJung-uk Kim 
2934757402fbSPyun YongHyeon 	/* Set various PHY bug flags. */
29351ec4c3a8SJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
29361ec4c3a8SJung-uk Kim 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
2937757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
29385ee49a3aSJung-uk Kim 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
29395ee49a3aSJung-uk Kim 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
2940757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
29415ee49a3aSJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
2942757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
29434150ce6fSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
2944757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
2945eea8956aSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
2946eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
29471108273aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
2948bbe2ca75SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5719 &&
294950515680SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5720 &&
2950eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
2951b4a256acSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57765 &&
2952eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780) {
29535ee49a3aSJung-uk Kim 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
2954a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2955a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
29564fcf220bSJohn Baldwin 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
2957f7d1b2ebSXin LI 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
2958f7d1b2ebSXin LI 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
2959757402fbSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
2960eea8956aSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
2961eea8956aSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
2962eea8956aSPyun YongHyeon 		} else
2963757402fbSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
29645ee49a3aSJung-uk Kim 	}
29655ee49a3aSJung-uk Kim 
2966a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
29671108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
29681108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2969a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2970a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2971a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2972a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
2973a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
2974a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
2975a813ed78SPyun YongHyeon 	else
2976a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
29777ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
29787ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
29797ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
2980a813ed78SPyun YongHyeon 
2981f681b29aSPyun YongHyeon 	/*
2982d4622124SPyun YongHyeon 	 * All Broadcom controllers have 4GB boundary DMA bug.
2983f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
2984f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
2985f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
2986f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
2987f681b29aSPyun YongHyeon 	 */
2988f681b29aSPyun YongHyeon 	sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
29894f0794ffSBjoern A. Zeeb 
2990d9820cd8SPyun YongHyeon 	/* BCM5755 or higher and BCM5906 have short DMA bug. */
2991d9820cd8SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
2992d9820cd8SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
2993d9820cd8SPyun YongHyeon 
2994a7fcfcf3SPyun YongHyeon 	/*
2995a7fcfcf3SPyun YongHyeon 	 * BCM5719 cannot handle DMA requests for DMA segments that
2996a7fcfcf3SPyun YongHyeon 	 * have larger than 4KB in size.  However the maximum DMA
2997a7fcfcf3SPyun YongHyeon 	 * segment size created in DMA tag is 4KB for TSO, so we
2998a7fcfcf3SPyun YongHyeon 	 * wouldn't encounter the issue here.
2999a7fcfcf3SPyun YongHyeon 	 */
3000a7fcfcf3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3001a7fcfcf3SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3002a7fcfcf3SPyun YongHyeon 
30034f0794ffSBjoern A. Zeeb 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID;
3004fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
30054f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
30064f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
30074f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
300884ac96f8SPyun YongHyeon 	}
30094f0794ffSBjoern A. Zeeb 
3010fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
3011fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3012fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
3013fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3014fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
3015fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3016fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3017fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3018fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
3019fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3020fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3021fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3022fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3023fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3024fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
3025fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
3026fb772a6cSMarius Strobl 	}
3027fb772a6cSMarius Strobl 
3028e53d81eeSPaul Saab 	/*
3029ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
3030ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
3031ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
3032ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
3033ca3f1187SPyun YongHyeon 	 * known bug which can't handle TSO if ethernet header + IP/TCP
3034ca3f1187SPyun YongHyeon 	 * header is greater than 80 bytes. The workaround for the TSO
3035ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
3036ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
3037ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
3038ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
3039ca3f1187SPyun YongHyeon 	 */
30401108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
30411108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
30421108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
3043bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3044bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3045bbe2ca75SPyun YongHyeon 			/* TSO on BCM5719 A0 does not work. */
3046bbe2ca75SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_TSO3;
3047bbe2ca75SPyun YongHyeon 		}
30481108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
30494f4a16e1SPyun YongHyeon 		/*
30504f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
30514f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
3052be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
30534f4a16e1SPyun YongHyeon 		 */
30544f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3055be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3056be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3057ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
30584f4a16e1SPyun YongHyeon 	}
3059ca3f1187SPyun YongHyeon 
3060ca3f1187SPyun YongHyeon 	/*
30616f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
3062e53d81eeSPaul Saab 	 */
30633b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
30644c0da0ffSGleb Smirnoff 		/*
30656f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
30666f8718a3SScott Long 		 * must be a PCI Express device.
30676f8718a3SScott Long 		 */
30686f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
30690aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
307050515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
307150515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
3072bbe2ca75SPyun YongHyeon 			pci_set_max_read_req(dev, 2048);
3073bbe2ca75SPyun YongHyeon 		else if (pci_get_max_read_req(dev) != 4096)
3074d2b6e9a0SPyun YongHyeon 			pci_set_max_read_req(dev, 4096);
30756f8718a3SScott Long 	} else {
30766f8718a3SScott Long 		/*
30776f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
30786f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
30794c0da0ffSGleb Smirnoff 		 */
30803b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
30810aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
308290447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
30834c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
3084652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
30856f8718a3SScott Long 	}
30864c0da0ffSGleb Smirnoff 
3087bf6ef57aSJohn Polstra 	/*
3088fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3089fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
3090fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3091fd4d32feSPyun YongHyeon 	 */
3092fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3093fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3094fd4d32feSPyun YongHyeon 	/*
3095bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
3096bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
3097bf6ef57aSJohn Polstra 	 * normal operation.
3098bf6ef57aSJohn Polstra 	 */
30990aaf1057SPyun YongHyeon 	rid = 0;
31003b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
31010aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
3102bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
3103bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
3104bf6ef57aSJohn Polstra 			if (msicount > 1)
3105bf6ef57aSJohn Polstra 				msicount = 1;
3106bf6ef57aSJohn Polstra 		} else
3107bf6ef57aSJohn Polstra 			msicount = 0;
3108bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
3109bf6ef57aSJohn Polstra 			rid = 1;
3110bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
31110aaf1057SPyun YongHyeon 		}
31120aaf1057SPyun YongHyeon 	}
3113bf6ef57aSJohn Polstra 
31141108273aSPyun YongHyeon 	/*
31151108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
31161108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
31171108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
31181108273aSPyun YongHyeon 	 */
31191108273aSPyun YongHyeon #ifndef DEVICE_POLLING
31201108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
31211108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
31221108273aSPyun YongHyeon #endif
31231108273aSPyun YongHyeon 
3124bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3125bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
3126bf6ef57aSJohn Polstra 
3127bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
3128bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
3129bf6ef57aSJohn Polstra 		error = ENXIO;
3130bf6ef57aSJohn Polstra 		goto fail;
3131bf6ef57aSJohn Polstra 	}
3132bf6ef57aSJohn Polstra 
31334f09c4c7SMarius Strobl 	device_printf(dev,
31344f09c4c7SMarius Strobl 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; %s\n",
31354f09c4c7SMarius Strobl 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev,
31364f09c4c7SMarius Strobl 	    (sc->bge_flags & BGE_FLAG_PCIX) ? "PCI-X" :
31374f09c4c7SMarius Strobl 	    ((sc->bge_flags & BGE_FLAG_PCIE) ? "PCI-E" : "PCI"));
31384f09c4c7SMarius Strobl 
3139bf6ef57aSJohn Polstra 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
3140bf6ef57aSJohn Polstra 
314195d67482SBill Paul 	/* Try to reset the chip. */
31428cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
31438cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
31448cb1383cSDoug Ambrisko 		error = ENXIO;
31458cb1383cSDoug Ambrisko 		goto fail;
31468cb1383cSDoug Ambrisko 	}
31478cb1383cSDoug Ambrisko 
31488cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
3149888b47f0SPyun YongHyeon 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3150888b47f0SPyun YongHyeon 	    BGE_SRAM_DATA_SIG_MAGIC)) {
3151888b47f0SPyun YongHyeon 		if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG)
31528cb1383cSDoug Ambrisko 		    & BGE_HWCFG_ASF) {
31538cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_ENABLE;
31548cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_STACKUP;
3155d67eba2fSPyun YongHyeon 			if (BGE_IS_575X_PLUS(sc))
31568cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
31578cb1383cSDoug Ambrisko 		}
31588cb1383cSDoug Ambrisko 	}
31598cb1383cSDoug Ambrisko 
31608cb1383cSDoug Ambrisko 	/* Try to reset the chip again the nice way. */
31618cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
31628cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
31638cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
31648cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
31658cb1383cSDoug Ambrisko 		error = ENXIO;
31668cb1383cSDoug Ambrisko 		goto fail;
31678cb1383cSDoug Ambrisko 	}
31688cb1383cSDoug Ambrisko 
31698cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
31708cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
317195d67482SBill Paul 
317295d67482SBill Paul 	if (bge_chipinit(sc)) {
3173fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
317495d67482SBill Paul 		error = ENXIO;
317595d67482SBill Paul 		goto fail;
317695d67482SBill Paul 	}
317795d67482SBill Paul 
317838cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
317938cc658fSJohn Baldwin 	if (error) {
318008013fd3SMarius Strobl 		device_printf(sc->bge_dev,
318108013fd3SMarius Strobl 		    "failed to read station address\n");
318295d67482SBill Paul 		error = ENXIO;
318395d67482SBill Paul 		goto fail;
318495d67482SBill Paul 	}
318595d67482SBill Paul 
3186f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
31871108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
31881108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
31891108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3190f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3191f41ac2beSBill Paul 	else
3192f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3193f41ac2beSBill Paul 
31945b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3195fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3196fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3197f41ac2beSBill Paul 		error = ENXIO;
3198f41ac2beSBill Paul 		goto fail;
3199f41ac2beSBill Paul 	}
3200f41ac2beSBill Paul 
320135f945cdSPyun YongHyeon 	bge_add_sysctls(sc);
320235f945cdSPyun YongHyeon 
320395d67482SBill Paul 	/* Set default tuneable values. */
320495d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
320595d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
320695d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
32076f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
32086f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
320995d67482SBill Paul 
321035f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
321135f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
321235f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
321335f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
321435f945cdSPyun YongHyeon 
321595d67482SBill Paul 	/* Set up ifnet structure */
3216fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3217fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3218fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3219fc74a9f9SBrooks Davis 		error = ENXIO;
3220fc74a9f9SBrooks Davis 		goto fail;
3221fc74a9f9SBrooks Davis 	}
322295d67482SBill Paul 	ifp->if_softc = sc;
32239bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
322495d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
322595d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
322695d67482SBill Paul 	ifp->if_start = bge_start;
322795d67482SBill Paul 	ifp->if_init = bge_init;
32284d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
32294d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
32304d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
323135f945cdSPyun YongHyeon 	ifp->if_hwassist = sc->bge_csum_features;
3232d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
32334e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
32341108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3235ca3f1187SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
323604bde852SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_VLAN_HWTSO;
3237ca3f1187SPyun YongHyeon 	}
32384e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
32394e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
32404e35d186SJung-uk Kim #endif
324195d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
324275719184SGleb Smirnoff #ifdef DEVICE_POLLING
324375719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
324475719184SGleb Smirnoff #endif
324595d67482SBill Paul 
3246a1d52896SBill Paul 	/*
3247d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3248d375e524SGleb Smirnoff 	 * to hardware bugs.
3249d375e524SGleb Smirnoff 	 */
3250d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3251d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
32524d3a629cSPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_HWCSUM;
3253d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
3254d375e524SGleb Smirnoff 	}
3255d375e524SGleb Smirnoff 
3256d375e524SGleb Smirnoff 	/*
3257a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
325841abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
325941abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
326041abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
326141abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
326241abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
326341abcc1bSPaul Saab 	 * SK-9D41.
3264a1d52896SBill Paul 	 */
3265888b47f0SPyun YongHyeon 	if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3266888b47f0SPyun YongHyeon 		hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
32675fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
32685fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3269f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3270f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3271fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3272f6789fbaSPyun YongHyeon 			error = ENXIO;
3273f6789fbaSPyun YongHyeon 			goto fail;
3274f6789fbaSPyun YongHyeon 		}
327541abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
327641abcc1bSPaul Saab 	}
327741abcc1bSPaul Saab 
327895d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3279ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3280ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
3281ea3b4127SPyun YongHyeon 		if (BGE_IS_5714_FAMILY(sc))
3282ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
3283ea3b4127SPyun YongHyeon 		else
3284652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3285ea3b4127SPyun YongHyeon 	}
328695d67482SBill Paul 
3287652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
32880c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
32890c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
32900c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
32916098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
32926098821cSJung-uk Kim 		    0, NULL);
329395d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
329495d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3295da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
329695d67482SBill Paul 	} else {
329795d67482SBill Paul 		/*
32988cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
32998cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
33008cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
33018cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
33028cb1383cSDoug Ambrisko 		 * the PHY.
330395d67482SBill Paul 		 */
33044012d104SMarius Strobl 		trys = 0;
33058cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
33068cb1383cSDoug Ambrisko again:
33078cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
33088cb1383cSDoug Ambrisko 
3309fb772a6cSMarius Strobl 		error = mii_attach(dev, &sc->bge_miibus, ifp, bge_ifmedia_upd,
3310fb772a6cSMarius Strobl 		    bge_ifmedia_sts, capmask, phy_addr, MII_OFFSET_ANY,
3311fb772a6cSMarius Strobl 		    MIIF_DOPAUSE);
33128e5d93dbSMarius Strobl 		if (error != 0) {
33138cb1383cSDoug Ambrisko 			if (trys++ < 4) {
33148cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
33154e35d186SJung-uk Kim 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR,
33164e35d186SJung-uk Kim 				    BMCR_RESET);
33178cb1383cSDoug Ambrisko 				goto again;
33188cb1383cSDoug Ambrisko 			}
33198e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
332095d67482SBill Paul 			goto fail;
332195d67482SBill Paul 		}
33228cb1383cSDoug Ambrisko 
33238cb1383cSDoug Ambrisko 		/*
33248cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
33258cb1383cSDoug Ambrisko 		 */
33268cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
33278cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
332895d67482SBill Paul 	}
332995d67482SBill Paul 
333095d67482SBill Paul 	/*
3331e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3332e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3333e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3334e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3335e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3336e255b776SJohn Polstra 	 * payloads by copying the received packets.
3337e255b776SJohn Polstra 	 */
3338652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3339652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3340652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3341e255b776SJohn Polstra 
3342e255b776SJohn Polstra 	/*
334395d67482SBill Paul 	 * Call MI attach routine.
334495d67482SBill Paul 	 */
3345fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
3346b74e67fbSGleb Smirnoff 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
33470f9bd73bSSam Leffler 
334861ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
334961ccb9daSPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
335061ccb9daSPyun YongHyeon 
33510f9bd73bSSam Leffler 	/*
33520f9bd73bSSam Leffler 	 * Hookup IRQ last.
33530f9bd73bSSam Leffler 	 */
3354dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3355dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
33567e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
33577e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3358dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3359dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3360dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3361dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3362dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3363dfe0df9aSPyun YongHyeon 			error = ENXIO;
3364dfe0df9aSPyun YongHyeon 			goto fail;
3365dfe0df9aSPyun YongHyeon 		}
3366dfe0df9aSPyun YongHyeon 		taskqueue_start_threads(&sc->bge_tq, 1, PI_NET, "%s taskq",
3367dfe0df9aSPyun YongHyeon 		    device_get_nameunit(sc->bge_dev));
3368dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3369dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3370dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3371dfe0df9aSPyun YongHyeon 		if (error)
3372dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3373dfe0df9aSPyun YongHyeon 	} else
3374dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3375dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3376dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
33770f9bd73bSSam Leffler 
33780f9bd73bSSam Leffler 	if (error) {
3379fc74a9f9SBrooks Davis 		bge_detach(dev);
3380fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
33810f9bd73bSSam Leffler 	}
338295d67482SBill Paul 
338308013fd3SMarius Strobl 	return (0);
338408013fd3SMarius Strobl 
338595d67482SBill Paul fail:
338608013fd3SMarius Strobl 	bge_release_resources(sc);
338708013fd3SMarius Strobl 
338895d67482SBill Paul 	return (error);
338995d67482SBill Paul }
339095d67482SBill Paul 
339195d67482SBill Paul static int
33923f74909aSGleb Smirnoff bge_detach(device_t dev)
339395d67482SBill Paul {
339495d67482SBill Paul 	struct bge_softc *sc;
339595d67482SBill Paul 	struct ifnet *ifp;
339695d67482SBill Paul 
339795d67482SBill Paul 	sc = device_get_softc(dev);
3398fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
339995d67482SBill Paul 
340075719184SGleb Smirnoff #ifdef DEVICE_POLLING
340175719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
340275719184SGleb Smirnoff 		ether_poll_deregister(ifp);
340375719184SGleb Smirnoff #endif
340475719184SGleb Smirnoff 
34050f9bd73bSSam Leffler 	BGE_LOCK(sc);
340695d67482SBill Paul 	bge_stop(sc);
340795d67482SBill Paul 	bge_reset(sc);
34080f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
34090f9bd73bSSam Leffler 
34105dda8085SOleg Bulyzhin 	callout_drain(&sc->bge_stat_ch);
34115dda8085SOleg Bulyzhin 
3412dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3413dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
34140f9bd73bSSam Leffler 	ether_ifdetach(ifp);
341595d67482SBill Paul 
3416652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
341795d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
341895d67482SBill Paul 	} else {
341995d67482SBill Paul 		bus_generic_detach(dev);
342095d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
342195d67482SBill Paul 	}
342295d67482SBill Paul 
342395d67482SBill Paul 	bge_release_resources(sc);
342495d67482SBill Paul 
342595d67482SBill Paul 	return (0);
342695d67482SBill Paul }
342795d67482SBill Paul 
342895d67482SBill Paul static void
34293f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
343095d67482SBill Paul {
343195d67482SBill Paul 	device_t dev;
343295d67482SBill Paul 
343395d67482SBill Paul 	dev = sc->bge_dev;
343495d67482SBill Paul 
3435dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3436dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3437dfe0df9aSPyun YongHyeon 
343895d67482SBill Paul 	if (sc->bge_intrhand != NULL)
343995d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
344095d67482SBill Paul 
344195d67482SBill Paul 	if (sc->bge_irq != NULL)
3442724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3443724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
3444724bd939SJohn Polstra 
3445724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
3446724bd939SJohn Polstra 		pci_release_msi(dev);
344795d67482SBill Paul 
344895d67482SBill Paul 	if (sc->bge_res != NULL)
344995d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
3450736b9319SPyun YongHyeon 		    PCIR_BAR(0), sc->bge_res);
345195d67482SBill Paul 
3452ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
3453ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
3454ad61f896SRuslan Ermilov 
3455f41ac2beSBill Paul 	bge_dma_free(sc);
345695d67482SBill Paul 
34570f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
34580f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
345995d67482SBill Paul }
346095d67482SBill Paul 
34618cb1383cSDoug Ambrisko static int
34623f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
346395d67482SBill Paul {
346495d67482SBill Paul 	device_t dev;
34655fea260fSMarius Strobl 	uint32_t cachesize, command, pcistate, reset, val;
34666f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
34670aaf1057SPyun YongHyeon 	uint16_t devctl;
34685fea260fSMarius Strobl 	int i;
346995d67482SBill Paul 
347095d67482SBill Paul 	dev = sc->bge_dev;
347195d67482SBill Paul 
347238cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
347338cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
34746f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
34756f8718a3SScott Long 			write_op = bge_writemem_direct;
34766f8718a3SScott Long 		else
34776f8718a3SScott Long 			write_op = bge_writemem_ind;
34789ba784dbSScott Long 	} else
34796f8718a3SScott Long 		write_op = bge_writereg_ind;
34806f8718a3SScott Long 
348195d67482SBill Paul 	/* Save some important PCI state. */
348295d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
348395d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
348495d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
348595d67482SBill Paul 
348695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
348795d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3488e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
348995d67482SBill Paul 
34906f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
34916f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
3492a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
34936f8718a3SScott Long 		if (bootverbose)
3494333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
34956f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
34966f8718a3SScott Long 	}
34976f8718a3SScott Long 
34986f8718a3SScott Long 	/*
34996f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
35006f8718a3SScott Long 	 * When firmware finishes its initialization it will
3501888b47f0SPyun YongHyeon 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
35026f8718a3SScott Long 	 */
3503888b47f0SPyun YongHyeon 	bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
35046f8718a3SScott Long 
35050c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
3506e53d81eeSPaul Saab 
3507e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3508652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
35090c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
35100c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, 0x7E2C, 0x20);
3511e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
3512e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
35130c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
35140c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
3515e53d81eeSPaul Saab 		}
3516e53d81eeSPaul Saab 	}
3517e53d81eeSPaul Saab 
351821c9e407SDavid Christensen 	/*
35196f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
35206f8718a3SScott Long 	 * powered up in D0 uninitialized.
35216f8718a3SScott Long 	 */
35225512ca01SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc) &&
35235512ca01SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
3524caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
35256f8718a3SScott Long 
352695d67482SBill Paul 	/* Issue global reset */
35276f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
352895d67482SBill Paul 
352938cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
35305fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
353138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
35325fea260fSMarius Strobl 		    val | BGE_VCPU_STATUS_DRV_RESET);
35335fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
353438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
35355fea260fSMarius Strobl 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
353638cc658fSJohn Baldwin 	}
353738cc658fSJohn Baldwin 
353895d67482SBill Paul 	DELAY(1000);
353995d67482SBill Paul 
3540e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3541652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
3542e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
3543e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
35445fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
35455fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
3546e53d81eeSPaul Saab 		}
35470aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
35480aaf1057SPyun YongHyeon 		    sc->bge_expcap + PCIR_EXPRESS_DEVICE_CTL, 2);
35490aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
35509a6e301dSPyun YongHyeon 		devctl &= ~(PCIM_EXP_CTL_RELAXED_ORD_ENABLE |
35519a6e301dSPyun YongHyeon 		    PCIM_EXP_CTL_NOSNOOP_ENABLE);
35520aaf1057SPyun YongHyeon 		/* Set PCIE max payload size to 128. */
35530aaf1057SPyun YongHyeon 		devctl &= ~PCIM_EXP_CTL_MAX_PAYLOAD;
35540aaf1057SPyun YongHyeon 		pci_write_config(dev, sc->bge_expcap + PCIR_EXPRESS_DEVICE_CTL,
35550aaf1057SPyun YongHyeon 		    devctl, 2);
35560aaf1057SPyun YongHyeon 		/* Clear error status. */
35570aaf1057SPyun YongHyeon 		pci_write_config(dev, sc->bge_expcap + PCIR_EXPRESS_DEVICE_STA,
35589a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_CORRECTABLE_ERROR |
35599a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_NON_FATAL_ERROR | PCIM_EXP_STA_FATAL_ERROR |
35609a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_UNSUPPORTED_REQ, 2);
3561e53d81eeSPaul Saab 	}
3562e53d81eeSPaul Saab 
35633f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
356495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
356595d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3566e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
356795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
356895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
35690c8aa4eaSJung-uk Kim 	write_op(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
3570cbb2b2feSPyun YongHyeon 	/*
3571cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
3572fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
3573cbb2b2feSPyun YongHyeon 	 * read stale status block.
3574cbb2b2feSPyun YongHyeon 	 */
3575cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
3576cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
3577cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
3578cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
3579cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
3580cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
3581cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
3582cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3583cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
3584cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
3585cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
3586cbb2b2feSPyun YongHyeon 		}
3587cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
3588cbb2b2feSPyun YongHyeon 		    devctl, 2);
3589cbb2b2feSPyun YongHyeon 	}
359022a4ecedSMarius Strobl 	/* Re-enable MSI, if necessary, and enable the memory arbiter. */
35914c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
3592bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
3593bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
35940aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
35950aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
35960aaf1057SPyun YongHyeon 			pci_write_config(dev,
35970aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
3598bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
3599bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
3600bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
3601bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
3602bf6ef57aSJohn Polstra 		}
36034c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
36044c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
36054c0da0ffSGleb Smirnoff 	} else
3606a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
3607a7b0c314SPaul Saab 
360838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
360938cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
361038cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
361138cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
361238cc658fSJohn Baldwin 				break;
361338cc658fSJohn Baldwin 			DELAY(100);
361438cc658fSJohn Baldwin 		}
361538cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
3616333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
361738cc658fSJohn Baldwin 			return (1);
361838cc658fSJohn Baldwin 		}
361938cc658fSJohn Baldwin 	} else {
362095d67482SBill Paul 		/*
36216f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
362208013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
36235fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
36245fea260fSMarius Strobl 		 * address is fitted though.
362595d67482SBill Paul 		 */
362695d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
3627d5d23857SJung-uk Kim 			DELAY(10);
3628888b47f0SPyun YongHyeon 			val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
3629888b47f0SPyun YongHyeon 			if (val == ~BGE_SRAM_FW_MB_MAGIC)
363095d67482SBill Paul 				break;
363195d67482SBill Paul 		}
363295d67482SBill Paul 
36335fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
3634333704a3SPyun YongHyeon 			device_printf(dev,
3635333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
3636333704a3SPyun YongHyeon 			    val);
3637b4a256acSPyun YongHyeon 		/* BCM57765 A0 needs additional time before accessing. */
3638b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
3639b4a256acSPyun YongHyeon 			DELAY(10 * 1000);	/* XXX */
364038cc658fSJohn Baldwin 	}
364195d67482SBill Paul 
364295d67482SBill Paul 	/*
364395d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
364495d67482SBill Paul 	 * return to its original pre-reset state. This is a
364595d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
364695d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
364795d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
364895d67482SBill Paul 	 * results.
364995d67482SBill Paul 	 */
365095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
365195d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
365295d67482SBill Paul 			break;
365395d67482SBill Paul 		DELAY(10);
365495d67482SBill Paul 	}
365595d67482SBill Paul 
36563f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
365750515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
365895d67482SBill Paul 
36598cb1383cSDoug Ambrisko 	/* Tell the ASF firmware we are up */
36608cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
36618cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
36628cb1383cSDoug Ambrisko 
366395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
366495d67482SBill Paul 
3665da3003f0SBill Paul 	/*
3666da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
3667da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
3668da3003f0SBill Paul 	 * to 1.2V.
3669da3003f0SBill Paul 	 */
3670652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
3671652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
36725fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
36735fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
36745fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
3675da3003f0SBill Paul 	}
3676da3003f0SBill Paul 
3677e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3678652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
3679b4a256acSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc) &&
3680a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
3681a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
3682a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
36835fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
36845fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
3685e53d81eeSPaul Saab 	}
368695d67482SBill Paul 	DELAY(10000);
36878cb1383cSDoug Ambrisko 
368850515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
368950515680SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
369050515680SPyun YongHyeon 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
369150515680SPyun YongHyeon 
36928cb1383cSDoug Ambrisko 	return (0);
369395d67482SBill Paul }
369495d67482SBill Paul 
3695e0b7b101SPyun YongHyeon static __inline void
3696e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
3697e0b7b101SPyun YongHyeon {
3698e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
3699e0b7b101SPyun YongHyeon 
3700e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
3701e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
3702e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
3703e0b7b101SPyun YongHyeon 	r->bge_idx = i;
3704e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
3705e0b7b101SPyun YongHyeon }
3706e0b7b101SPyun YongHyeon 
3707e0b7b101SPyun YongHyeon static __inline void
3708e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
3709e0b7b101SPyun YongHyeon {
3710e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
3711e0b7b101SPyun YongHyeon 
3712e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
3713e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
3714e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
3715e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
3716e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
3717e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
3718e0b7b101SPyun YongHyeon 	r->bge_idx = i;
3719e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
3720e0b7b101SPyun YongHyeon }
3721e0b7b101SPyun YongHyeon 
372295d67482SBill Paul /*
372395d67482SBill Paul  * Frame reception handling. This is called if there's a frame
372495d67482SBill Paul  * on the receive return list.
372595d67482SBill Paul  *
372695d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
37271be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
372895d67482SBill Paul  * 2) the frame is from the standard receive ring
372995d67482SBill Paul  */
373095d67482SBill Paul 
37311abcdbd1SAttilio Rao static int
3732dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
373395d67482SBill Paul {
373495d67482SBill Paul 	struct ifnet *ifp;
37351abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
3736b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
373795d67482SBill Paul 
37387f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
37390f9bd73bSSam Leffler 
37403f74909aSGleb Smirnoff 	/* Nothing to do. */
37417f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
37421abcdbd1SAttilio Rao 		return (rx_npkts);
3743cfcb5025SOleg Bulyzhin 
3744fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
374595d67482SBill Paul 
3746f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
3747e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
3748f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
374915eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
3750f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
3751f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
3752c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN))
3753f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
375415eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
3755f41ac2beSBill Paul 
37567f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
375795d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
37583f74909aSGleb Smirnoff 		uint32_t		rxidx;
375995d67482SBill Paul 		struct mbuf		*m = NULL;
37603f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
376195d67482SBill Paul 		int			have_tag = 0;
376295d67482SBill Paul 
376375719184SGleb Smirnoff #ifdef DEVICE_POLLING
376475719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
376575719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
376675719184SGleb Smirnoff 				break;
376775719184SGleb Smirnoff 			sc->rxcycles--;
376875719184SGleb Smirnoff 		}
376975719184SGleb Smirnoff #endif
377075719184SGleb Smirnoff 
37717f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
377295d67482SBill Paul 
377395d67482SBill Paul 		rxidx = cur_rx->bge_idx;
37747f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
377595d67482SBill Paul 
3776cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
3777cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
377895d67482SBill Paul 			have_tag = 1;
377995d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
378095d67482SBill Paul 		}
378195d67482SBill Paul 
378295d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
378395d67482SBill Paul 			jumbocnt++;
3784943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
378595d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3786e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
378795d67482SBill Paul 				continue;
378895d67482SBill Paul 			}
3789943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
3790e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
3791943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
379295d67482SBill Paul 				continue;
379395d67482SBill Paul 			}
379403e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
379595d67482SBill Paul 		} else {
379695d67482SBill Paul 			stdcnt++;
3797e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
379895d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3799e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
380095d67482SBill Paul 				continue;
380195d67482SBill Paul 			}
3802943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
3803e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
3804943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
380595d67482SBill Paul 				continue;
380695d67482SBill Paul 			}
380703e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
380895d67482SBill Paul 		}
380995d67482SBill Paul 
381095d67482SBill Paul 		ifp->if_ipackets++;
3811e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
3812e255b776SJohn Polstra 		/*
3813e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
3814e65bed95SPyun YongHyeon 		 * the payload is aligned.
3815e255b776SJohn Polstra 		 */
3816652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
3817e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
3818e255b776SJohn Polstra 			    cur_rx->bge_len);
3819e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
3820e255b776SJohn Polstra 		}
3821e255b776SJohn Polstra #endif
3822473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
382395d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
382495d67482SBill Paul 
38251108273aSPyun YongHyeon 		if (ifp->if_capenable & IFCAP_RXCSUM)
38261108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
382795d67482SBill Paul 
382895d67482SBill Paul 		/*
3829673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
3830673d9191SSam Leffler 		 * attach that information to the packet.
383195d67482SBill Paul 		 */
3832d147662cSGleb Smirnoff 		if (have_tag) {
383378ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
383478ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
3835d147662cSGleb Smirnoff 		}
383695d67482SBill Paul 
3837dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
38380f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
3839673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
38400f9bd73bSSam Leffler 			BGE_LOCK(sc);
3841dfe0df9aSPyun YongHyeon 		} else
3842dfe0df9aSPyun YongHyeon 			(*ifp->if_input)(ifp, m);
3843d4da719cSAttilio Rao 		rx_npkts++;
384425e13e68SXin LI 
384525e13e68SXin LI 		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
38468cf7d13dSAttilio Rao 			return (rx_npkts);
384795d67482SBill Paul 	}
384895d67482SBill Paul 
384915eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
385015eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
3851e65bed95SPyun YongHyeon 	if (stdcnt > 0)
3852f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
3853e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
38544c0da0ffSGleb Smirnoff 
3855c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
3856f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
38574c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
3858f41ac2beSBill Paul 
38597f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
386038cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
386195d67482SBill Paul 	if (stdcnt)
3862767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
3863767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
386495d67482SBill Paul 	if (jumbocnt)
3865767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
3866767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
3867f5a034f9SPyun YongHyeon #ifdef notyet
3868f5a034f9SPyun YongHyeon 	/*
3869f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
3870f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
3871f5a034f9SPyun YongHyeon 	 */
3872f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
3873f5a034f9SPyun YongHyeon 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
3874f5a034f9SPyun YongHyeon #endif
38751abcdbd1SAttilio Rao 	return (rx_npkts);
387695d67482SBill Paul }
387795d67482SBill Paul 
387895d67482SBill Paul static void
38791108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
38801108273aSPyun YongHyeon {
38811108273aSPyun YongHyeon 
38821108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
38831108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
38841108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
38851108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
38861108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
38871108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
38881108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
38891108273aSPyun YongHyeon 			}
38901108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
38911108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
38921108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
38931108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
38941108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
38951108273aSPyun YongHyeon 			}
38961108273aSPyun YongHyeon 		}
38971108273aSPyun YongHyeon 	} else {
38981108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
38991108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
39001108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
39011108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
39021108273aSPyun YongHyeon 		}
39031108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
39041108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
39051108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
39061108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
39071108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
39081108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
39091108273aSPyun YongHyeon 		}
39101108273aSPyun YongHyeon 	}
39111108273aSPyun YongHyeon }
39121108273aSPyun YongHyeon 
39131108273aSPyun YongHyeon static void
3914b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
391595d67482SBill Paul {
391695a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
391795d67482SBill Paul 	struct ifnet *ifp;
391895d67482SBill Paul 
39190f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
39200f9bd73bSSam Leffler 
39213f74909aSGleb Smirnoff 	/* Nothing to do. */
3922b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
3923cfcb5025SOleg Bulyzhin 		return;
3924cfcb5025SOleg Bulyzhin 
3925fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
392695d67482SBill Paul 
3927e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
39285c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
392995d67482SBill Paul 	/*
393095d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
393195d67482SBill Paul 	 * frames that have been sent.
393295d67482SBill Paul 	 */
3933b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
393495a0a340SPyun YongHyeon 		uint32_t		idx;
393595d67482SBill Paul 
393695d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
3937f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
393895d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
393995d67482SBill Paul 			ifp->if_opackets++;
394095d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
39410ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
3942e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
3943e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
39440ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
3945f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
3946e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
3947e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
394895d67482SBill Paul 		}
394995d67482SBill Paul 		sc->bge_txcnt--;
395095d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
395195d67482SBill Paul 	}
395295d67482SBill Paul 
395313f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
39545b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
39555b01e77cSBruce Evans 		sc->bge_timer = 0;
395695d67482SBill Paul }
395795d67482SBill Paul 
395875719184SGleb Smirnoff #ifdef DEVICE_POLLING
39591abcdbd1SAttilio Rao static int
396075719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
396175719184SGleb Smirnoff {
396275719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
3963b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
3964366454f2SOleg Bulyzhin 	uint32_t statusword;
39651abcdbd1SAttilio Rao 	int rx_npkts = 0;
396675719184SGleb Smirnoff 
39673f74909aSGleb Smirnoff 	BGE_LOCK(sc);
39683f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
39693f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
39701abcdbd1SAttilio Rao 		return (rx_npkts);
39713f74909aSGleb Smirnoff 	}
397275719184SGleb Smirnoff 
3973dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3974b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3975b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3976b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
3977b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
3978dab5cd05SOleg Bulyzhin 
3979175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
3980175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
3981dab5cd05SOleg Bulyzhin 
3982dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3983b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3984b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3985366454f2SOleg Bulyzhin 
39860c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
3987366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
3988366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
3989366454f2SOleg Bulyzhin 
3990366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
3991366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
39924c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3993652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
3994366454f2SOleg Bulyzhin 			bge_link_upd(sc);
3995366454f2SOleg Bulyzhin 
3996366454f2SOleg Bulyzhin 	sc->rxcycles = count;
3997dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
399825e13e68SXin LI 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
399925e13e68SXin LI 		BGE_UNLOCK(sc);
40008cf7d13dSAttilio Rao 		return (rx_npkts);
400125e13e68SXin LI 	}
4002b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
4003366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4004366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
40053f74909aSGleb Smirnoff 
40063f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
40071abcdbd1SAttilio Rao 	return (rx_npkts);
400875719184SGleb Smirnoff }
400975719184SGleb Smirnoff #endif /* DEVICE_POLLING */
401075719184SGleb Smirnoff 
4011dfe0df9aSPyun YongHyeon static int
4012dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
4013dfe0df9aSPyun YongHyeon {
4014dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4015dfe0df9aSPyun YongHyeon 
4016dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4017dfe0df9aSPyun YongHyeon 	/*
4018dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
4019dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
4020dfe0df9aSPyun YongHyeon 	 */
4021dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4022dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
4023dfe0df9aSPyun YongHyeon }
4024dfe0df9aSPyun YongHyeon 
4025dfe0df9aSPyun YongHyeon static void
4026dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
4027dfe0df9aSPyun YongHyeon {
4028dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4029dfe0df9aSPyun YongHyeon 	struct ifnet *ifp;
40301108273aSPyun YongHyeon 	uint32_t status, status_tag;
4031dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4032dfe0df9aSPyun YongHyeon 
4033dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4034dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
4035dfe0df9aSPyun YongHyeon 
403666151edfSPyun YongHyeon 	BGE_LOCK(sc);
403766151edfSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
403866151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4039dfe0df9aSPyun YongHyeon 		return;
404066151edfSPyun YongHyeon 	}
4041dfe0df9aSPyun YongHyeon 
4042dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
4043dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4044dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4045dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4046dfe0df9aSPyun YongHyeon 
4047dfe0df9aSPyun YongHyeon 	/* Save producer/consumer indexess. */
4048dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4049dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4050dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
40511108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
4052dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4053dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4054dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4055dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
40561108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
40571108273aSPyun YongHyeon 		status_tag = 0;
405866151edfSPyun YongHyeon 
405966151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
406066151edfSPyun YongHyeon 		bge_link_upd(sc);
406166151edfSPyun YongHyeon 
4062dfe0df9aSPyun YongHyeon 	/* Let controller work. */
40631108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4064dfe0df9aSPyun YongHyeon 
406566151edfSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
406666151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
4067dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
406866151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4069dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
407066151edfSPyun YongHyeon 		BGE_LOCK(sc);
4071dfe0df9aSPyun YongHyeon 	}
4072dfe0df9aSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4073dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
4074dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
4075dfe0df9aSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4076dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
4077dfe0df9aSPyun YongHyeon 	}
407866151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
4079dfe0df9aSPyun YongHyeon }
4080dfe0df9aSPyun YongHyeon 
408195d67482SBill Paul static void
40823f74909aSGleb Smirnoff bge_intr(void *xsc)
408395d67482SBill Paul {
408495d67482SBill Paul 	struct bge_softc *sc;
408595d67482SBill Paul 	struct ifnet *ifp;
4086dab5cd05SOleg Bulyzhin 	uint32_t statusword;
4087b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
408895d67482SBill Paul 
408995d67482SBill Paul 	sc = xsc;
4090f41ac2beSBill Paul 
40910f9bd73bSSam Leffler 	BGE_LOCK(sc);
40920f9bd73bSSam Leffler 
4093dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
4094dab5cd05SOleg Bulyzhin 
409575719184SGleb Smirnoff #ifdef DEVICE_POLLING
409675719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
409775719184SGleb Smirnoff 		BGE_UNLOCK(sc);
409875719184SGleb Smirnoff 		return;
409975719184SGleb Smirnoff 	}
410075719184SGleb Smirnoff #endif
410175719184SGleb Smirnoff 
4102f30cbfc6SScott Long 	/*
4103b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
4104b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
4105b848e032SBruce Evans 	 * our current organization this just gives complications and
4106b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
4107b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
4108b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
4109b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
4110b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
4111b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
4112b848e032SBruce Evans 	 *
4113b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
4114b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
4115b848e032SBruce Evans 	 * changing later because it is more efficient to get another
4116b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
4117b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
4118b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
4119b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
4120b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
4121b848e032SBruce Evans 	 */
412238cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4123b848e032SBruce Evans 
4124f584dfd1SPyun YongHyeon 	/*
4125f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
4126f584dfd1SPyun YongHyeon 	 */
4127f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4128f584dfd1SPyun YongHyeon 
4129f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
4130f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4131f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4132f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4133f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4134f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4135f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4136f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4137f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4138f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4139f584dfd1SPyun YongHyeon 
41401f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
41414c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4142f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
4143dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
414495d67482SBill Paul 
414513f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
41463f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
4147dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
414825e13e68SXin LI 	}
414995d67482SBill Paul 
415025e13e68SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
41513f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
4152b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
415395d67482SBill Paul 	}
415495d67482SBill Paul 
415513f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
415613f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
41570f9bd73bSSam Leffler 		bge_start_locked(ifp);
41580f9bd73bSSam Leffler 
41590f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
416095d67482SBill Paul }
416195d67482SBill Paul 
416295d67482SBill Paul static void
41638cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
41648cb1383cSDoug Ambrisko {
41658cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
41668cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
41678cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
41688cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
41698cb1383cSDoug Ambrisko 		else {
4170899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
4171888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
41723c201200SPyun YongHyeon 			    BGE_FW_CMD_DRV_ALIVE);
4173888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4174941a6e13SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4175941a6e13SPyun YongHyeon 			    BGE_FW_HB_TIMEOUT_SEC);
41763fed2d5dSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
41779931ba85SPyun YongHyeon 			    CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
41789931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT);
41798cb1383cSDoug Ambrisko 		}
41808cb1383cSDoug Ambrisko 	}
41818cb1383cSDoug Ambrisko }
41828cb1383cSDoug Ambrisko 
41838cb1383cSDoug Ambrisko static void
4184b74e67fbSGleb Smirnoff bge_tick(void *xsc)
41850f9bd73bSSam Leffler {
4186b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
418795d67482SBill Paul 	struct mii_data *mii = NULL;
418895d67482SBill Paul 
41890f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
419095d67482SBill Paul 
41915dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
41925dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
41935dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
41945dda8085SOleg Bulyzhin 		return;
41955dda8085SOleg Bulyzhin 
41967ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
41970434d1b8SBill Paul 		bge_stats_update_regs(sc);
41980434d1b8SBill Paul 	else
419995d67482SBill Paul 		bge_stats_update(sc);
420095d67482SBill Paul 
4201652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
420295d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
420382b67c01SOleg Bulyzhin 		/*
420482b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
420582b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
420682b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
420782b67c01SOleg Bulyzhin 		 */
420882b67c01SOleg Bulyzhin 		if (!sc->bge_link)
420995d67482SBill Paul 			mii_tick(mii);
42107b97099dSOleg Bulyzhin 	} else {
42117b97099dSOleg Bulyzhin 		/*
42127b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
42137b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
42147b97099dSOleg Bulyzhin 		 * and trigger interrupt.
42157b97099dSOleg Bulyzhin 		 */
42167b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
42173f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
42187b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
42197b97099dSOleg Bulyzhin #endif
42207b97099dSOleg Bulyzhin 		{
42217b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
42224f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
42234f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
42247b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
42254f0794ffSBjoern A. Zeeb 		else
42264f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
42277b97099dSOleg Bulyzhin 		}
4228dab5cd05SOleg Bulyzhin 	}
422995d67482SBill Paul 
42308cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4231b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
42328cb1383cSDoug Ambrisko 
4233dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
423495d67482SBill Paul }
423595d67482SBill Paul 
423695d67482SBill Paul static void
42373f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
42380434d1b8SBill Paul {
42393f74909aSGleb Smirnoff 	struct ifnet *ifp;
42402280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
42410434d1b8SBill Paul 
4242fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
42432280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
42440434d1b8SBill Paul 
42452280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
42462280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
42472280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
42482280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
42492280c16bSPyun YongHyeon 	stats->outXonSent +=
42502280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
42512280c16bSPyun YongHyeon 	stats->outXoffSent +=
42522280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
42532280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
42542280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
42552280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
42562280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
42572280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
42582280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
42592280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
42602280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
42612280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
42622280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
42632280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
42642280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
42652280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
42662280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
42672280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
42682280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
42692280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
42702280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
42717e6e2507SJung-uk Kim 
42722280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
42732280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
42742280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
42752280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
42762280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
42772280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
42782280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
42792280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
42802280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
42812280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
42822280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
42832280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
42842280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
42852280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
42862280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
42872280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
42882280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
42892280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
42902280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
42912280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
42922280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
42932280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
42942280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
42952280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
42962280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
42972280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
42982280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
42992280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
43002280c16bSPyun YongHyeon 
43012280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
43022280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
43032280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
43042280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
43052280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
43062280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
43072280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
43082280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4309f78094a5SPyun YongHyeon 	/*
4310f78094a5SPyun YongHyeon 	 * XXX
4311f78094a5SPyun YongHyeon 	 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4312f78094a5SPyun YongHyeon 	 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4313f78094a5SPyun YongHyeon 	 * includes number of unwanted multicast frames.  This comes
4314f78094a5SPyun YongHyeon 	 * from silicon bug and known workaround to get rough(not
4315f78094a5SPyun YongHyeon 	 * exact) counter is to enable interrupt on MBUF low water
4316f78094a5SPyun YongHyeon 	 * attention.  This can be accomplished by setting
4317f78094a5SPyun YongHyeon 	 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4318f78094a5SPyun YongHyeon 	 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4319f78094a5SPyun YongHyeon 	 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4320f78094a5SPyun YongHyeon 	 * However that change would generate more interrupts and
4321f78094a5SPyun YongHyeon 	 * there are still possibilities of losing multiple frames
4322f78094a5SPyun YongHyeon 	 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4323f78094a5SPyun YongHyeon 	 * Given that the workaround still would not get correct
4324f78094a5SPyun YongHyeon 	 * counter I don't think it's worth to implement it.  So
4325f78094a5SPyun YongHyeon 	 * ignore reading the counter on controllers that have the
4326f78094a5SPyun YongHyeon 	 * silicon bug.
4327f78094a5SPyun YongHyeon 	 */
4328f78094a5SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4329f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4330f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
43312280c16bSPyun YongHyeon 		stats->InputDiscards +=
43322280c16bSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
43332280c16bSPyun YongHyeon 	stats->InputErrors +=
43342280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
43352280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
43362280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
43372280c16bSPyun YongHyeon 
43382280c16bSPyun YongHyeon 	ifp->if_collisions = (u_long)stats->etherStatsCollisions;
43392280c16bSPyun YongHyeon 	ifp->if_ierrors = (u_long)(stats->NoMoreRxBDs + stats->InputDiscards +
43402280c16bSPyun YongHyeon 	    stats->InputErrors);
43412280c16bSPyun YongHyeon }
43422280c16bSPyun YongHyeon 
43432280c16bSPyun YongHyeon static void
43442280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
43452280c16bSPyun YongHyeon {
43462280c16bSPyun YongHyeon 
43472280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
43482280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
43492280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
43502280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
43512280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
43522280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
43532280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
43542280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
43552280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
43562280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
43572280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
43582280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
43592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
43602280c16bSPyun YongHyeon 
43612280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
43622280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
43632280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
43642280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
43652280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
43662280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
43672280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
43682280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
43692280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
43702280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
43712280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
43722280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
43732280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
43742280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
43752280c16bSPyun YongHyeon 
43762280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
43772280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
43782280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
43792280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
43802280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
43812280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
43822280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
43830434d1b8SBill Paul }
43840434d1b8SBill Paul 
43850434d1b8SBill Paul static void
43863f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
438795d67482SBill Paul {
438895d67482SBill Paul 	struct ifnet *ifp;
4389e907febfSPyun YongHyeon 	bus_size_t stats;
43907e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
439195d67482SBill Paul 
4392fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
439395d67482SBill Paul 
4394e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
4395e907febfSPyun YongHyeon 
4396e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
4397e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
439895d67482SBill Paul 
43998634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
44006b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
44016fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
44026fb34dd2SOleg Bulyzhin 
44036fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
44046b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
44056fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
44066fb34dd2SOleg Bulyzhin 
44076fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
44086b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
44096fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
441095d67482SBill Paul 
4411e907febfSPyun YongHyeon #undef	READ_STAT
441295d67482SBill Paul }
441395d67482SBill Paul 
441495d67482SBill Paul /*
4415d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
4416d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
4417d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
4418d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
4419d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
4420d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
4421d375e524SGleb Smirnoff  */
4422d375e524SGleb Smirnoff static __inline int
4423d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
4424d375e524SGleb Smirnoff {
4425d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
4426d375e524SGleb Smirnoff 	struct mbuf *last;
4427d375e524SGleb Smirnoff 
4428d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
4429d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
4430d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
4431d375e524SGleb Smirnoff 		last = m;
4432d375e524SGleb Smirnoff 	} else {
4433d375e524SGleb Smirnoff 		/*
4434d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
4435d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
4436d375e524SGleb Smirnoff 		 */
4437d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
4438d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
4439d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
4440d375e524SGleb Smirnoff 			struct mbuf *n;
4441d375e524SGleb Smirnoff 
4442d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
4443d375e524SGleb Smirnoff 			if (n == NULL)
4444d375e524SGleb Smirnoff 				return (ENOBUFS);
4445d375e524SGleb Smirnoff 			n->m_len = 0;
4446d375e524SGleb Smirnoff 			last->m_next = n;
4447d375e524SGleb Smirnoff 			last = n;
4448d375e524SGleb Smirnoff 		}
4449d375e524SGleb Smirnoff 	}
4450d375e524SGleb Smirnoff 
4451d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
4452d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
4453d375e524SGleb Smirnoff 	last->m_len += padlen;
4454d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
4455d375e524SGleb Smirnoff 
4456d375e524SGleb Smirnoff 	return (0);
4457d375e524SGleb Smirnoff }
4458d375e524SGleb Smirnoff 
4459ca3f1187SPyun YongHyeon static struct mbuf *
4460d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
4461d598b626SPyun YongHyeon {
4462d598b626SPyun YongHyeon 	struct mbuf *n;
4463d598b626SPyun YongHyeon 	int found;
4464d598b626SPyun YongHyeon 
4465d598b626SPyun YongHyeon 	/*
4466d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
4467d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
4468d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
4469d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
4470d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
4471d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
4472d598b626SPyun YongHyeon 	 */
4473d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
4474d598b626SPyun YongHyeon 		if (n->m_len < 8) {
4475d598b626SPyun YongHyeon 			found++;
4476d598b626SPyun YongHyeon 			if (found > 1)
4477d598b626SPyun YongHyeon 				break;
4478d598b626SPyun YongHyeon 			continue;
4479d598b626SPyun YongHyeon 		}
4480d598b626SPyun YongHyeon 		found = 0;
4481d598b626SPyun YongHyeon 	}
4482d598b626SPyun YongHyeon 
4483d598b626SPyun YongHyeon 	if (found > 1) {
4484d598b626SPyun YongHyeon 		n = m_defrag(m, M_DONTWAIT);
4485d598b626SPyun YongHyeon 		if (n == NULL)
4486d598b626SPyun YongHyeon 			m_freem(m);
4487d598b626SPyun YongHyeon 	} else
4488d598b626SPyun YongHyeon 		n = m;
4489d598b626SPyun YongHyeon 	return (n);
4490d598b626SPyun YongHyeon }
4491d598b626SPyun YongHyeon 
4492d598b626SPyun YongHyeon static struct mbuf *
44931108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
44941108273aSPyun YongHyeon     uint16_t *flags)
4495ca3f1187SPyun YongHyeon {
4496ca3f1187SPyun YongHyeon 	struct ip *ip;
4497ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
4498ca3f1187SPyun YongHyeon 	struct mbuf *n;
4499ca3f1187SPyun YongHyeon 	uint16_t hlen;
45005b355c4fSPyun YongHyeon 	uint32_t poff;
4501ca3f1187SPyun YongHyeon 
4502ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
4503ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
4504ca3f1187SPyun YongHyeon 		n = m_dup(m, M_DONTWAIT);
4505ca3f1187SPyun YongHyeon 		m_freem(m);
4506ca3f1187SPyun YongHyeon 		if (n == NULL)
4507ca3f1187SPyun YongHyeon 			return (NULL);
4508ca3f1187SPyun YongHyeon 		m = n;
4509ca3f1187SPyun YongHyeon 	}
45105b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
4511ca3f1187SPyun YongHyeon 	if (m == NULL)
4512ca3f1187SPyun YongHyeon 		return (NULL);
45135b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
45145b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
4515ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
4516ca3f1187SPyun YongHyeon 	if (m == NULL)
4517ca3f1187SPyun YongHyeon 		return (NULL);
4518ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
45195b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
4520ca3f1187SPyun YongHyeon 	if (m == NULL)
4521ca3f1187SPyun YongHyeon 		return (NULL);
4522ca3f1187SPyun YongHyeon 	/*
4523ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
4524ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
4525ca3f1187SPyun YongHyeon 	 */
4526ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
452796486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
4528ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
4529ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
4530ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
453196486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
4532ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
4533ca3f1187SPyun YongHyeon 	/*
4534ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
4535ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
4536ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
45371108273aSPyun YongHyeon 	 * we only support hardware based TSO.
4538ca3f1187SPyun YongHyeon 	 */
45391108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
4540ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
45411108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
45421108273aSPyun YongHyeon 		/*
45431108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
45441108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
45451108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
45461108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
45471108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
45481108273aSPyun YongHyeon 		 * frames are supported.
45491108273aSPyun YongHyeon 		 */
45501108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
45511108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
45521108273aSPyun YongHyeon 	} else {
45531108273aSPyun YongHyeon 		/*
45541108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
45551108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
45561108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
45571108273aSPyun YongHyeon 		 * supported.
45581108273aSPyun YongHyeon 		 */
4559ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
45601108273aSPyun YongHyeon 	}
4561ca3f1187SPyun YongHyeon 	return (m);
4562ca3f1187SPyun YongHyeon }
4563ca3f1187SPyun YongHyeon 
4564d375e524SGleb Smirnoff /*
456595d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
456695d67482SBill Paul  * pointers to descriptors.
456795d67482SBill Paul  */
456895d67482SBill Paul static int
4569676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
457095d67482SBill Paul {
45717e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
4572f41ac2beSBill Paul 	bus_dmamap_t		map;
4573676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
4574676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
45757e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
4576ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
45777e27542aSGleb Smirnoff 	int			nsegs, i, error;
457895d67482SBill Paul 
45796909dc43SGleb Smirnoff 	csum_flags = 0;
4580ca3f1187SPyun YongHyeon 	mss = 0;
4581ca3f1187SPyun YongHyeon 	vlan_tag = 0;
4582d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
4583d598b626SPyun YongHyeon 	    m->m_next != NULL) {
4584d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
4585d598b626SPyun YongHyeon 		if (*m_head == NULL)
4586d598b626SPyun YongHyeon 			return (ENOBUFS);
4587d598b626SPyun YongHyeon 		m = *m_head;
4588d598b626SPyun YongHyeon 	}
4589ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
45901108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
4591ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
4592ca3f1187SPyun YongHyeon 			return (ENOBUFS);
4593ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
4594ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
459535f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
45966909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
45976909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
45986909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
45996909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
46006909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
46016909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
46026909dc43SGleb Smirnoff 				m_freem(m);
46036909dc43SGleb Smirnoff 				*m_head = NULL;
46046909dc43SGleb Smirnoff 				return (error);
46056909dc43SGleb Smirnoff 			}
46066909dc43SGleb Smirnoff 		}
46076909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
46086909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
46096909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
46106909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
46116909dc43SGleb Smirnoff 	}
46126909dc43SGleb Smirnoff 
46131108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
46141108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
46151108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
46161108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
46171108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
4618beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
4619d94f2b85SPyun YongHyeon 			/*
4620d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
4621d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
4622d94f2b85SPyun YongHyeon 			 * DMA read operation.
4623d94f2b85SPyun YongHyeon 			 */
4624beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
4625d94f2b85SPyun YongHyeon 				m = m_defrag(m, M_DONTWAIT);
4626d94f2b85SPyun YongHyeon 			else
46271108273aSPyun YongHyeon 				m = m_collapse(m, M_DONTWAIT,
46281108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
4629261f04d6SPyun YongHyeon 			if (m == NULL)
4630261f04d6SPyun YongHyeon 				m = *m_head;
4631d94f2b85SPyun YongHyeon 			*m_head = m;
4632d94f2b85SPyun YongHyeon 		}
46331108273aSPyun YongHyeon 	}
4634d94f2b85SPyun YongHyeon 
46357e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
46360ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
4637676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
46387e27542aSGleb Smirnoff 	if (error == EFBIG) {
46394eee14cbSMarius Strobl 		m = m_collapse(m, M_DONTWAIT, BGE_NSEG_NEW);
4640676ad2c9SGleb Smirnoff 		if (m == NULL) {
4641676ad2c9SGleb Smirnoff 			m_freem(*m_head);
4642676ad2c9SGleb Smirnoff 			*m_head = NULL;
46437e27542aSGleb Smirnoff 			return (ENOBUFS);
46447e27542aSGleb Smirnoff 		}
4645676ad2c9SGleb Smirnoff 		*m_head = m;
46460ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
46470ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
4648676ad2c9SGleb Smirnoff 		if (error) {
4649676ad2c9SGleb Smirnoff 			m_freem(m);
4650676ad2c9SGleb Smirnoff 			*m_head = NULL;
46517e27542aSGleb Smirnoff 			return (error);
46527e27542aSGleb Smirnoff 		}
4653676ad2c9SGleb Smirnoff 	} else if (error != 0)
4654676ad2c9SGleb Smirnoff 		return (error);
46557e27542aSGleb Smirnoff 
4656167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
4657167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
46580ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
465995d67482SBill Paul 		return (ENOBUFS);
46607e27542aSGleb Smirnoff 	}
46617e27542aSGleb Smirnoff 
46620ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
4663e65bed95SPyun YongHyeon 
4664ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
4665ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
4666ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
4667ca3f1187SPyun YongHyeon 	}
46687e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
46697e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
46707e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
46717e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
46727e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
46737e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
4674ca3f1187SPyun YongHyeon 		d->bge_vlan_tag = vlan_tag;
4675ca3f1187SPyun YongHyeon 		d->bge_mss = mss;
46767e27542aSGleb Smirnoff 		if (i == nsegs - 1)
46777e27542aSGleb Smirnoff 			break;
46787e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
46797e27542aSGleb Smirnoff 	}
46807e27542aSGleb Smirnoff 
46817e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
46827e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
4683676ad2c9SGleb Smirnoff 
4684f41ac2beSBill Paul 	/*
4685f41ac2beSBill Paul 	 * Insure that the map for this transmission
4686f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
4687f41ac2beSBill Paul 	 * in this chain.
4688f41ac2beSBill Paul 	 */
46897e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
46907e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
4691676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
46927e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
469395d67482SBill Paul 
46947e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
46957e27542aSGleb Smirnoff 	*txidx = idx;
469695d67482SBill Paul 
469795d67482SBill Paul 	return (0);
469895d67482SBill Paul }
469995d67482SBill Paul 
470095d67482SBill Paul /*
470195d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
470295d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
470395d67482SBill Paul  */
470495d67482SBill Paul static void
47053f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
470695d67482SBill Paul {
470795d67482SBill Paul 	struct bge_softc *sc;
4708167fdb62SPyun YongHyeon 	struct mbuf *m_head;
470914bbd30fSGleb Smirnoff 	uint32_t prodidx;
4710167fdb62SPyun YongHyeon 	int count;
471195d67482SBill Paul 
471295d67482SBill Paul 	sc = ifp->if_softc;
4713167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
471495d67482SBill Paul 
4715167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
4716167fdb62SPyun YongHyeon 	    (ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
4717167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
471895d67482SBill Paul 		return;
471995d67482SBill Paul 
472014bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
472195d67482SBill Paul 
4722167fdb62SPyun YongHyeon 	for (count = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
4723167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
4724167fdb62SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
4725167fdb62SPyun YongHyeon 			break;
4726167fdb62SPyun YongHyeon 		}
47274d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
472895d67482SBill Paul 		if (m_head == NULL)
472995d67482SBill Paul 			break;
473095d67482SBill Paul 
473195d67482SBill Paul 		/*
473295d67482SBill Paul 		 * XXX
4733b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
4734b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
4735b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
4736b874fdd4SYaroslav Tykhiy 		 *
4737b874fdd4SYaroslav Tykhiy 		 * XXX
473895d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
473995d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
474095d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
474195d67482SBill Paul 		 * chain at once.
474295d67482SBill Paul 		 * (paranoia -- may not actually be needed)
474395d67482SBill Paul 		 */
474495d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
474595d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
474695d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
474795d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
47484d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
474913f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
475095d67482SBill Paul 				break;
475195d67482SBill Paul 			}
475295d67482SBill Paul 		}
475395d67482SBill Paul 
475495d67482SBill Paul 		/*
475595d67482SBill Paul 		 * Pack the data into the transmit ring. If we
475695d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
475795d67482SBill Paul 		 * for the NIC to drain the ring.
475895d67482SBill Paul 		 */
4759676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
4760676ad2c9SGleb Smirnoff 			if (m_head == NULL)
4761676ad2c9SGleb Smirnoff 				break;
47624d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
476313f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
476495d67482SBill Paul 			break;
476595d67482SBill Paul 		}
4766303a718cSDag-Erling Smørgrav 		++count;
476795d67482SBill Paul 
476895d67482SBill Paul 		/*
476995d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
477095d67482SBill Paul 		 * to him.
477195d67482SBill Paul 		 */
47724e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
477345ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
47744e35d186SJung-uk Kim #else
47754e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
47764e35d186SJung-uk Kim #endif
477795d67482SBill Paul 	}
477895d67482SBill Paul 
4779167fdb62SPyun YongHyeon 	if (count > 0) {
4780aa94f333SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
47815c1da2faSPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
47823f74909aSGleb Smirnoff 		/* Transmit. */
478338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
47843927098fSPaul Saab 		/* 5700 b2 errata */
4785e0ced696SPaul Saab 		if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
478638cc658fSJohn Baldwin 			bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
478795d67482SBill Paul 
478814bbd30fSGleb Smirnoff 		sc->bge_tx_prodidx = prodidx;
478914bbd30fSGleb Smirnoff 
479095d67482SBill Paul 		/*
479195d67482SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
479295d67482SBill Paul 		 */
4793b74e67fbSGleb Smirnoff 		sc->bge_timer = 5;
479495d67482SBill Paul 	}
4795167fdb62SPyun YongHyeon }
479695d67482SBill Paul 
47970f9bd73bSSam Leffler /*
47980f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
47990f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
48000f9bd73bSSam Leffler  */
480195d67482SBill Paul static void
48023f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
480395d67482SBill Paul {
48040f9bd73bSSam Leffler 	struct bge_softc *sc;
48050f9bd73bSSam Leffler 
48060f9bd73bSSam Leffler 	sc = ifp->if_softc;
48070f9bd73bSSam Leffler 	BGE_LOCK(sc);
48080f9bd73bSSam Leffler 	bge_start_locked(ifp);
48090f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
48100f9bd73bSSam Leffler }
48110f9bd73bSSam Leffler 
48120f9bd73bSSam Leffler static void
48133f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
48140f9bd73bSSam Leffler {
481595d67482SBill Paul 	struct ifnet *ifp;
48163f74909aSGleb Smirnoff 	uint16_t *m;
4817f6a65488SPyun YongHyeon 	uint32_t mode;
481895d67482SBill Paul 
48190f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
482095d67482SBill Paul 
4821fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
482295d67482SBill Paul 
482313f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
482495d67482SBill Paul 		return;
482595d67482SBill Paul 
482695d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
482795d67482SBill Paul 	bge_stop(sc);
48288cb1383cSDoug Ambrisko 
48298cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
48308cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
483195d67482SBill Paul 	bge_reset(sc);
48328cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
48338cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
48348cb1383cSDoug Ambrisko 
483595d67482SBill Paul 	bge_chipinit(sc);
483695d67482SBill Paul 
483795d67482SBill Paul 	/*
483895d67482SBill Paul 	 * Init the various state machines, ring
483995d67482SBill Paul 	 * control blocks and firmware.
484095d67482SBill Paul 	 */
484195d67482SBill Paul 	if (bge_blockinit(sc)) {
4842fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
484395d67482SBill Paul 		return;
484495d67482SBill Paul 	}
484595d67482SBill Paul 
4846fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
484795d67482SBill Paul 
484895d67482SBill Paul 	/* Specify MTU. */
484995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
4850cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
4851cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
485295d67482SBill Paul 
485395d67482SBill Paul 	/* Load our MAC address. */
48543f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
485595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
485695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
485795d67482SBill Paul 
48583e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
48593e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
486095d67482SBill Paul 
486195d67482SBill Paul 	/* Program multicast filter. */
486295d67482SBill Paul 	bge_setmulti(sc);
486395d67482SBill Paul 
4864cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
4865cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
4866cb2eacc7SYaroslav Tykhiy 
486735f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
486835f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
486935f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
487035f945cdSPyun YongHyeon 	else
487135f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
487235f945cdSPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_TXCSUM &&
487335f945cdSPyun YongHyeon 	    ifp->if_capenable & IFCAP_TXCSUM) {
487435f945cdSPyun YongHyeon 		ifp->if_hwassist &= ~(BGE_CSUM_FEATURES | CSUM_UDP);
487535f945cdSPyun YongHyeon 		ifp->if_hwassist |= sc->bge_csum_features;
487635f945cdSPyun YongHyeon 	}
487735f945cdSPyun YongHyeon 
487895d67482SBill Paul 	/* Init RX ring. */
48793ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
48803ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
48813ee5d7daSPyun YongHyeon 		bge_stop(sc);
48823ee5d7daSPyun YongHyeon 		return;
48833ee5d7daSPyun YongHyeon 	}
488495d67482SBill Paul 
48850434d1b8SBill Paul 	/*
48860434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
48870434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
48880434d1b8SBill Paul 	 * entry of the ring.
48890434d1b8SBill Paul 	 */
48900434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
48913f74909aSGleb Smirnoff 		uint32_t		v, i;
48920434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
48930434d1b8SBill Paul 			DELAY(20);
48940434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
48950434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
48960434d1b8SBill Paul 				break;
48970434d1b8SBill Paul 		}
48980434d1b8SBill Paul 		if (i == 10)
4899fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
4900fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
49010434d1b8SBill Paul 	}
49020434d1b8SBill Paul 
490395d67482SBill Paul 	/* Init jumbo RX ring. */
4904f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
4905f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
4906c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN)) {
49073ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
4908333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
4909b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
49103ee5d7daSPyun YongHyeon 			bge_stop(sc);
49113ee5d7daSPyun YongHyeon 			return;
49123ee5d7daSPyun YongHyeon 		}
49133ee5d7daSPyun YongHyeon 	}
491495d67482SBill Paul 
49153f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
491695d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
491795d67482SBill Paul 
49187e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
49197e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
49207e6e2507SJung-uk Kim 
492195d67482SBill Paul 	/* Init TX ring. */
492295d67482SBill Paul 	bge_init_tx_ring(sc);
492395d67482SBill Paul 
4924f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
4925f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
4926f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
4927f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
492850515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
492950515680SPyun YongHyeon 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
493050515680SPyun YongHyeon 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
493150515680SPyun YongHyeon 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
493250515680SPyun YongHyeon 	}
49333f74909aSGleb Smirnoff 	/* Turn on transmitter. */
4934f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
493595d67482SBill Paul 
49363f74909aSGleb Smirnoff 	/* Turn on receiver. */
493795d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
493895d67482SBill Paul 
4939dedcdf57SPyun YongHyeon 	/*
4940dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
4941dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
4942dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
4943dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
4944dedcdf57SPyun YongHyeon 	 */
4945b4a256acSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM57765)
4946b4a256acSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
4947b4a256acSPyun YongHyeon 	else
4948dedcdf57SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
4949dedcdf57SPyun YongHyeon 
49502280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
49512280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
49522280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
49532280c16bSPyun YongHyeon 
495495d67482SBill Paul 	/* Tell firmware we're alive. */
495595d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
495695d67482SBill Paul 
495775719184SGleb Smirnoff #ifdef DEVICE_POLLING
495875719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
495975719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
496075719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
496175719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
496238cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
496375719184SGleb Smirnoff 	} else
496475719184SGleb Smirnoff #endif
496575719184SGleb Smirnoff 
496695d67482SBill Paul 	/* Enable host interrupts. */
496775719184SGleb Smirnoff 	{
496895d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
496995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
497038cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
497175719184SGleb Smirnoff 	}
497295d67482SBill Paul 
497367d5e043SOleg Bulyzhin 	bge_ifmedia_upd_locked(ifp);
497495d67482SBill Paul 
497513f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
497613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
497795d67482SBill Paul 
49780f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
49790f9bd73bSSam Leffler }
49800f9bd73bSSam Leffler 
49810f9bd73bSSam Leffler static void
49823f74909aSGleb Smirnoff bge_init(void *xsc)
49830f9bd73bSSam Leffler {
49840f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
49850f9bd73bSSam Leffler 
49860f9bd73bSSam Leffler 	BGE_LOCK(sc);
49870f9bd73bSSam Leffler 	bge_init_locked(sc);
49880f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
498995d67482SBill Paul }
499095d67482SBill Paul 
499195d67482SBill Paul /*
499295d67482SBill Paul  * Set media options.
499395d67482SBill Paul  */
499495d67482SBill Paul static int
49953f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
499695d67482SBill Paul {
499767d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
499867d5e043SOleg Bulyzhin 	int res;
499967d5e043SOleg Bulyzhin 
500067d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
500167d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
500267d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
500367d5e043SOleg Bulyzhin 
500467d5e043SOleg Bulyzhin 	return (res);
500567d5e043SOleg Bulyzhin }
500667d5e043SOleg Bulyzhin 
500767d5e043SOleg Bulyzhin static int
500867d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
500967d5e043SOleg Bulyzhin {
501067d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
501195d67482SBill Paul 	struct mii_data *mii;
50124f09c4c7SMarius Strobl 	struct mii_softc *miisc;
501395d67482SBill Paul 	struct ifmedia *ifm;
501495d67482SBill Paul 
501567d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
501667d5e043SOleg Bulyzhin 
501795d67482SBill Paul 	ifm = &sc->bge_ifmedia;
501895d67482SBill Paul 
501995d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
5020652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
502195d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
502295d67482SBill Paul 			return (EINVAL);
502395d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
502495d67482SBill Paul 		case IFM_AUTO:
5025ff50922bSDoug White 			/*
5026ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
5027ff50922bSDoug White 			 * mechanism for programming the autoneg
5028ff50922bSDoug White 			 * advertisement registers in TBI mode.
5029ff50922bSDoug White 			 */
50300f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5031ff50922bSDoug White 				uint32_t sgdig;
50320f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
50330f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
5034ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5035ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5036ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
5037ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
5038ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
5039ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5040ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
5041ff50922bSDoug White 					DELAY(5);
5042ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5043ff50922bSDoug White 				}
50440f89fde2SJung-uk Kim 			}
504595d67482SBill Paul 			break;
504695d67482SBill Paul 		case IFM_1000_SX:
504795d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
504895d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
504995d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
505095d67482SBill Paul 			} else {
505195d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
505295d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
505395d67482SBill Paul 			}
505495d67482SBill Paul 			break;
505595d67482SBill Paul 		default:
505695d67482SBill Paul 			return (EINVAL);
505795d67482SBill Paul 		}
505895d67482SBill Paul 		return (0);
505995d67482SBill Paul 	}
506095d67482SBill Paul 
50611493e883SOleg Bulyzhin 	sc->bge_link_evt++;
506295d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
50634f09c4c7SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
50643fcb7a53SMarius Strobl 		PHY_RESET(miisc);
506595d67482SBill Paul 	mii_mediachg(mii);
506695d67482SBill Paul 
5067902827f6SBjoern A. Zeeb 	/*
5068902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
5069902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
5070902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
5071902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
5072902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
5073902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
5074902827f6SBjoern A. Zeeb 	 * get an RX intr.
5075902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
5076902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
5077902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
5078902827f6SBjoern A. Zeeb 	 */
50794f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
50804f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
5081902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
50824f0794ffSBjoern A. Zeeb 	else
508363ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5084902827f6SBjoern A. Zeeb 
508595d67482SBill Paul 	return (0);
508695d67482SBill Paul }
508795d67482SBill Paul 
508895d67482SBill Paul /*
508995d67482SBill Paul  * Report current media status.
509095d67482SBill Paul  */
509195d67482SBill Paul static void
50923f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
509395d67482SBill Paul {
509467d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
509595d67482SBill Paul 	struct mii_data *mii;
509695d67482SBill Paul 
509767d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
509895d67482SBill Paul 
5099652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
510095d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
510195d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
510295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
510395d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
510495d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
51054c0da0ffSGleb Smirnoff 		else {
51064c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
510767d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
51084c0da0ffSGleb Smirnoff 			return;
51094c0da0ffSGleb Smirnoff 		}
511095d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
511195d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
511295d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
511395d67482SBill Paul 		else
511495d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
511567d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
511695d67482SBill Paul 		return;
511795d67482SBill Paul 	}
511895d67482SBill Paul 
511995d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
512095d67482SBill Paul 	mii_pollstat(mii);
512195d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
512295d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
512367d5e043SOleg Bulyzhin 
512467d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
512595d67482SBill Paul }
512695d67482SBill Paul 
512795d67482SBill Paul static int
51283f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
512995d67482SBill Paul {
513095d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
513195d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
513295d67482SBill Paul 	struct mii_data *mii;
5133f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
513495d67482SBill Paul 
513595d67482SBill Paul 	switch (command) {
513695d67482SBill Paul 	case SIOCSIFMTU:
5137f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
5138f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
51394c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
5140f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
514195d67482SBill Paul 				error = EINVAL;
5142f5459d4cSPyun YongHyeon 				break;
5143f5459d4cSPyun YongHyeon 			}
5144f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5145f5459d4cSPyun YongHyeon 			error = EINVAL;
5146f5459d4cSPyun YongHyeon 			break;
5147f5459d4cSPyun YongHyeon 		}
5148f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
5149f5459d4cSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
515095d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
51513a429c8fSPyun YongHyeon 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
515213f4c340SRobert Watson 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
51533a429c8fSPyun YongHyeon 				bge_init_locked(sc);
515495d67482SBill Paul 			}
51553a429c8fSPyun YongHyeon 		}
51563a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
515795d67482SBill Paul 		break;
515895d67482SBill Paul 	case SIOCSIFFLAGS:
51590f9bd73bSSam Leffler 		BGE_LOCK(sc);
516095d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
516195d67482SBill Paul 			/*
516295d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
516395d67482SBill Paul 			 * then just use the 'set promisc mode' command
516495d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
516595d67482SBill Paul 			 * a full re-init means reloading the firmware and
516695d67482SBill Paul 			 * waiting for it to start up, which may take a
5167d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
516895d67482SBill Paul 			 */
5169f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
5170f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
51713e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
51723e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
5173f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
5174d183af7fSRuslan Ermilov 					bge_setmulti(sc);
517595d67482SBill Paul 			} else
51760f9bd73bSSam Leffler 				bge_init_locked(sc);
517795d67482SBill Paul 		} else {
517813f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
517995d67482SBill Paul 				bge_stop(sc);
518095d67482SBill Paul 			}
518195d67482SBill Paul 		}
518295d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
51830f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
518495d67482SBill Paul 		error = 0;
518595d67482SBill Paul 		break;
518695d67482SBill Paul 	case SIOCADDMULTI:
518795d67482SBill Paul 	case SIOCDELMULTI:
518813f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
51890f9bd73bSSam Leffler 			BGE_LOCK(sc);
519095d67482SBill Paul 			bge_setmulti(sc);
51910f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
519295d67482SBill Paul 			error = 0;
519395d67482SBill Paul 		}
519495d67482SBill Paul 		break;
519595d67482SBill Paul 	case SIOCSIFMEDIA:
519695d67482SBill Paul 	case SIOCGIFMEDIA:
5197652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
519895d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
519995d67482SBill Paul 			    &sc->bge_ifmedia, command);
520095d67482SBill Paul 		} else {
520195d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
520295d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
520395d67482SBill Paul 			    &mii->mii_media, command);
520495d67482SBill Paul 		}
520595d67482SBill Paul 		break;
520695d67482SBill Paul 	case SIOCSIFCAP:
520795d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
520875719184SGleb Smirnoff #ifdef DEVICE_POLLING
520975719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
521075719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
521175719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
521275719184SGleb Smirnoff 				if (error)
521375719184SGleb Smirnoff 					return (error);
521475719184SGleb Smirnoff 				BGE_LOCK(sc);
521575719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
521675719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
521738cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
521875719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
521975719184SGleb Smirnoff 				BGE_UNLOCK(sc);
522075719184SGleb Smirnoff 			} else {
522175719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
522275719184SGleb Smirnoff 				/* Enable interrupt even in error case */
522375719184SGleb Smirnoff 				BGE_LOCK(sc);
522475719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
522575719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
522638cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
522775719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
522875719184SGleb Smirnoff 				BGE_UNLOCK(sc);
522975719184SGleb Smirnoff 			}
523075719184SGleb Smirnoff 		}
523175719184SGleb Smirnoff #endif
5232d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5233d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
5234d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
5235d8b57f98SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
523635f945cdSPyun YongHyeon 				ifp->if_hwassist |= sc->bge_csum_features;
523795d67482SBill Paul 			else
523835f945cdSPyun YongHyeon 				ifp->if_hwassist &= ~sc->bge_csum_features;
523995d67482SBill Paul 		}
5240cb2eacc7SYaroslav Tykhiy 
5241d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5242d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
5243d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
5244d8b57f98SPyun YongHyeon 
5245ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5246ca3f1187SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
5247ca3f1187SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
5248ca3f1187SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
5249ca3f1187SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
5250ca3f1187SPyun YongHyeon 			else
5251ca3f1187SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
5252ca3f1187SPyun YongHyeon 		}
5253ca3f1187SPyun YongHyeon 
5254cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5255cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
5256cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5257cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5258cb2eacc7SYaroslav Tykhiy 		}
5259cb2eacc7SYaroslav Tykhiy 
526004bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
526104bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
526204bde852SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
526304bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
526404bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
5265cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
526604bde852SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
526704bde852SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
5268cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5269cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5270cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
527104bde852SPyun YongHyeon 		}
5272cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5273cb2eacc7SYaroslav Tykhiy 		VLAN_CAPABILITIES(ifp);
5274cb2eacc7SYaroslav Tykhiy #endif
527595d67482SBill Paul 		break;
527695d67482SBill Paul 	default:
5277673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
527895d67482SBill Paul 		break;
527995d67482SBill Paul 	}
528095d67482SBill Paul 
528195d67482SBill Paul 	return (error);
528295d67482SBill Paul }
528395d67482SBill Paul 
528495d67482SBill Paul static void
5285b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
528695d67482SBill Paul {
5287b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
528895d67482SBill Paul 
5289b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5290b74e67fbSGleb Smirnoff 
5291b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5292b74e67fbSGleb Smirnoff 		return;
5293b74e67fbSGleb Smirnoff 
5294b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
529595d67482SBill Paul 
5296fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
529795d67482SBill Paul 
529813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5299426742bfSGleb Smirnoff 	bge_init_locked(sc);
530095d67482SBill Paul 
530195d67482SBill Paul 	ifp->if_oerrors++;
530295d67482SBill Paul }
530395d67482SBill Paul 
53045a147ba6SPyun YongHyeon static void
53055a147ba6SPyun YongHyeon bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
53065a147ba6SPyun YongHyeon {
53075a147ba6SPyun YongHyeon 	int i;
53085a147ba6SPyun YongHyeon 
53095a147ba6SPyun YongHyeon 	BGE_CLRBIT(sc, reg, bit);
53105a147ba6SPyun YongHyeon 
53115a147ba6SPyun YongHyeon 	for (i = 0; i < BGE_TIMEOUT; i++) {
53125a147ba6SPyun YongHyeon 		if ((CSR_READ_4(sc, reg) & bit) == 0)
53135a147ba6SPyun YongHyeon 			return;
53145a147ba6SPyun YongHyeon 		DELAY(100);
53155a147ba6SPyun YongHyeon         }
53165a147ba6SPyun YongHyeon }
53175a147ba6SPyun YongHyeon 
531895d67482SBill Paul /*
531995d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
532095d67482SBill Paul  * RX and TX lists.
532195d67482SBill Paul  */
532295d67482SBill Paul static void
53233f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
532495d67482SBill Paul {
532595d67482SBill Paul 	struct ifnet *ifp;
532695d67482SBill Paul 
53270f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
53280f9bd73bSSam Leffler 
5329fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
533095d67482SBill Paul 
53310f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
533295d67482SBill Paul 
533344b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
533444b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
533544b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
533644b63691SBjoern A. Zeeb 
533744b63691SBjoern A. Zeeb 	/*
533844b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
533944b63691SBjoern A. Zeeb 	 */
534044b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
534144b63691SBjoern A. Zeeb 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
534244b63691SBjoern A. Zeeb 
534395d67482SBill Paul 	/*
53443f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
534595d67482SBill Paul 	 */
53465a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
53475a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
53485a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
53495a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
53505a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
53515a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
53525a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
53535a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
535495d67482SBill Paul 
535595d67482SBill Paul 	/*
53563f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
535795d67482SBill Paul 	 */
53585a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
53595a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
53605a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
53615a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
53625a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
53635a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
53645a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
53655a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
536695d67482SBill Paul 
536795d67482SBill Paul 	/*
536895d67482SBill Paul 	 * Shut down all of the memory managers and related
536995d67482SBill Paul 	 * state machines.
537095d67482SBill Paul 	 */
53715a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
53725a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
53735a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
53745a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
53755a147ba6SPyun YongHyeon 
53760c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
537795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
53787ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
537995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
538095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
53810434d1b8SBill Paul 	}
53822280c16bSPyun YongHyeon 	/* Update MAC statistics. */
53832280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
53842280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
538595d67482SBill Paul 
53868cb1383cSDoug Ambrisko 	bge_reset(sc);
53878cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
53888cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
53898cb1383cSDoug Ambrisko 
53908cb1383cSDoug Ambrisko 	/*
53918cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
53928cb1383cSDoug Ambrisko 	 */
53938cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
53948cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
53958cb1383cSDoug Ambrisko 	else
539695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
539795d67482SBill Paul 
539895d67482SBill Paul 	/* Free the RX lists. */
539995d67482SBill Paul 	bge_free_rx_ring_std(sc);
540095d67482SBill Paul 
540195d67482SBill Paul 	/* Free jumbo RX list. */
54024c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
540395d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
540495d67482SBill Paul 
540595d67482SBill Paul 	/* Free TX buffers. */
540695d67482SBill Paul 	bge_free_tx_ring(sc);
540795d67482SBill Paul 
540895d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
540995d67482SBill Paul 
54105dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
54111493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
54121493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
54131493e883SOleg Bulyzhin 	sc->bge_link = 0;
541495d67482SBill Paul 
54151493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
541695d67482SBill Paul }
541795d67482SBill Paul 
541895d67482SBill Paul /*
541995d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
542095d67482SBill Paul  * get confused by errant DMAs when rebooting.
542195d67482SBill Paul  */
5422b6c974e8SWarner Losh static int
54233f74909aSGleb Smirnoff bge_shutdown(device_t dev)
542495d67482SBill Paul {
542595d67482SBill Paul 	struct bge_softc *sc;
542695d67482SBill Paul 
542795d67482SBill Paul 	sc = device_get_softc(dev);
54280f9bd73bSSam Leffler 	BGE_LOCK(sc);
542995d67482SBill Paul 	bge_stop(sc);
543095d67482SBill Paul 	bge_reset(sc);
54310f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
5432b6c974e8SWarner Losh 
5433b6c974e8SWarner Losh 	return (0);
543495d67482SBill Paul }
543514afefa3SPawel Jakub Dawidek 
543614afefa3SPawel Jakub Dawidek static int
543714afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
543814afefa3SPawel Jakub Dawidek {
543914afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
544014afefa3SPawel Jakub Dawidek 
544114afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
544214afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
544314afefa3SPawel Jakub Dawidek 	bge_stop(sc);
544414afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
544514afefa3SPawel Jakub Dawidek 
544614afefa3SPawel Jakub Dawidek 	return (0);
544714afefa3SPawel Jakub Dawidek }
544814afefa3SPawel Jakub Dawidek 
544914afefa3SPawel Jakub Dawidek static int
545014afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
545114afefa3SPawel Jakub Dawidek {
545214afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
545314afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
545414afefa3SPawel Jakub Dawidek 
545514afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
545614afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
545714afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
545814afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
545914afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
546014afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
546114afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
546214afefa3SPawel Jakub Dawidek 	}
546314afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
546414afefa3SPawel Jakub Dawidek 
546514afefa3SPawel Jakub Dawidek 	return (0);
546614afefa3SPawel Jakub Dawidek }
5467dab5cd05SOleg Bulyzhin 
5468dab5cd05SOleg Bulyzhin static void
54693f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
5470dab5cd05SOleg Bulyzhin {
54711f313773SOleg Bulyzhin 	struct mii_data *mii;
54721f313773SOleg Bulyzhin 	uint32_t link, status;
5473dab5cd05SOleg Bulyzhin 
5474dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
54751f313773SOleg Bulyzhin 
54763f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
54777b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
54787b97099dSOleg Bulyzhin 
5479dab5cd05SOleg Bulyzhin 	/*
5480dab5cd05SOleg Bulyzhin 	 * Process link state changes.
5481dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
5482dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
5483dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
5484dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
5485dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
5486dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
5487dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
5488dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
54891f313773SOleg Bulyzhin 	 *
54901f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
54914c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
5492dab5cd05SOleg Bulyzhin 	 */
5493dab5cd05SOleg Bulyzhin 
54941f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
54954c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
5496dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
5497dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
54981f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
54995dda8085SOleg Bulyzhin 			mii_pollstat(mii);
55001f313773SOleg Bulyzhin 			if (!sc->bge_link &&
55011f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
55021f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
55031f313773SOleg Bulyzhin 				sc->bge_link++;
55041f313773SOleg Bulyzhin 				if (bootverbose)
55051f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
55061f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
55071f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
55081f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
55091f313773SOleg Bulyzhin 				sc->bge_link = 0;
55101f313773SOleg Bulyzhin 				if (bootverbose)
55111f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
55121f313773SOleg Bulyzhin 			}
55131f313773SOleg Bulyzhin 
55143f74909aSGleb Smirnoff 			/* Clear the interrupt. */
5515dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
5516dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
5517dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
5518dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
5519dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
5520dab5cd05SOleg Bulyzhin 		}
5521dab5cd05SOleg Bulyzhin 		return;
5522dab5cd05SOleg Bulyzhin 	}
5523dab5cd05SOleg Bulyzhin 
5524652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
55251f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
55267b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
55277b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
55281f313773SOleg Bulyzhin 				sc->bge_link++;
55291f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
55301f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
55311f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
55320c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
55331f313773SOleg Bulyzhin 				if (bootverbose)
55341f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
55353f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
55363f74909aSGleb Smirnoff 				    LINK_STATE_UP);
55377b97099dSOleg Bulyzhin 			}
55381f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
5539dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
55401f313773SOleg Bulyzhin 			if (bootverbose)
55411f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
55427b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
55431f313773SOleg Bulyzhin 		}
55446ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
55451f313773SOleg Bulyzhin 		/*
55460c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
55470c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
55480c8aa4eaSJung-uk Kim 		 * PHY link status directly.
55491f313773SOleg Bulyzhin 		 */
55501f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
55511f313773SOleg Bulyzhin 
55521f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
55531f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
55541f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
55555dda8085SOleg Bulyzhin 			mii_pollstat(mii);
55561f313773SOleg Bulyzhin 			if (!sc->bge_link &&
55571f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
55581f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
55591f313773SOleg Bulyzhin 				sc->bge_link++;
55601f313773SOleg Bulyzhin 				if (bootverbose)
55611f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
55621f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
55631f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
55641f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
55651f313773SOleg Bulyzhin 				sc->bge_link = 0;
55661f313773SOleg Bulyzhin 				if (bootverbose)
55671f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
55681f313773SOleg Bulyzhin 			}
55691f313773SOleg Bulyzhin 		}
55700c8aa4eaSJung-uk Kim 	} else {
55710c8aa4eaSJung-uk Kim 		/*
55726ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
55736ede2cfaSPyun YongHyeon 		 * link status.
55740c8aa4eaSJung-uk Kim 		 */
55756ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
55766ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
55776ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
5578dab5cd05SOleg Bulyzhin 	}
5579dab5cd05SOleg Bulyzhin 
55803f74909aSGleb Smirnoff 	/* Clear the attention. */
5581dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
5582dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
5583dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
5584dab5cd05SOleg Bulyzhin }
55856f8718a3SScott Long 
55866f8718a3SScott Long static void
55876f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
55886f8718a3SScott Long {
55896f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
55902280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
55917e32f79aSPyun YongHyeon 	char tn[32];
55927e32f79aSPyun YongHyeon 	int unit;
55936f8718a3SScott Long 
55946f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
55956f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
55966f8718a3SScott Long 
55976f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
55986f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
55996f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
56006f8718a3SScott Long 	    "Debug Information");
56016f8718a3SScott Long 
56026f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
56036f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
56046f8718a3SScott Long 	    "Register Read");
56056f8718a3SScott Long 
56066f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
56076f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
56086f8718a3SScott Long 	    "Memory Read");
56096f8718a3SScott Long 
56106f8718a3SScott Long #endif
5611763757b2SScott Long 
56127e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
5613beaa2ae1SPyun YongHyeon 	/*
5614beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
5615beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
5616beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
5617beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
5618beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
5619beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
5620beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
5621beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
5622beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
5623beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
5624beaa2ae1SPyun YongHyeon 	 */
56257e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
56267e32f79aSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.forced_collapse", unit);
56277e32f79aSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_collapse);
5628beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
5629beaa2ae1SPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_collapse, 0,
5630beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
5631beaa2ae1SPyun YongHyeon 	    "forced collapsing");
5632beaa2ae1SPyun YongHyeon 
56332ae7f64bSPyun YongHyeon 	sc->bge_msi = 1;
56342ae7f64bSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.msi", unit);
56352ae7f64bSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_msi);
56362ae7f64bSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
56372ae7f64bSPyun YongHyeon 	    CTLFLAG_RD, &sc->bge_msi, 0, "Enable MSI");
56385c952e8dSPyun YongHyeon 
563935f945cdSPyun YongHyeon 	/*
564035f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
564135f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
564235f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
564335f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
564435f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
564535f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
564635f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
564735f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
564835f945cdSPyun YongHyeon 	 */
564935f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
565035f945cdSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.bge_forced_udpcsum", unit);
565135f945cdSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_udpcsum);
565235f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
565335f945cdSPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_udpcsum, 0,
565435f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
565535f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
565635f945cdSPyun YongHyeon 
5657d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
56582280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
56592280c16bSPyun YongHyeon 	else
56602280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
56612280c16bSPyun YongHyeon }
5662d949071dSJung-uk Kim 
56632280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
56642280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
56652280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
56662280c16bSPyun YongHyeon 	    desc)
56672280c16bSPyun YongHyeon 
56682280c16bSPyun YongHyeon static void
56692280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
56702280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
56712280c16bSPyun YongHyeon {
56722280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
56732280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
56742280c16bSPyun YongHyeon 
56752280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
5676763757b2SScott Long 	    NULL, "BGE Statistics");
5677763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
5678763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
5679763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
5680763757b2SScott Long 	    "FramesDroppedDueToFilters");
5681763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
5682763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
5683763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
5684763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
5685763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
5686763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
568706e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
568806e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
568906e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
569006e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
5691763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
5692763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
5693763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
5694763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
5695763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
5696763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
5697763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
5698763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
5699763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
5700763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
5701763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
5702763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
5703763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
5704763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
5705763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
5706763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
5707763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
5708763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
5709763757b2SScott Long 
5710763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
5711763757b2SScott Long 	    NULL, "BGE RX Statistics");
5712763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
5713763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
57141cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
5715763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
5716763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
5717763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
57181cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
5719763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
5720763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
5721763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
5722763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
5723763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
5724763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
5725763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
5726763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
5727763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
5728763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
5729763757b2SScott Long 	    "xoffPauseFramesReceived");
5730763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
5731763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
5732763757b2SScott Long 	    "ControlFramesReceived");
5733763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
5734763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
5735763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
5736763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
5737763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
5738763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
5739763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
5740763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
5741763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
574206e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
5743763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
574406e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
5745763757b2SScott Long 
5746763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
5747763757b2SScott Long 	    NULL, "BGE TX Statistics");
5748763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
5749763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
57501cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
5751763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
5752763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
5753763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
5754763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
5755763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
5756763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
5757763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
5758763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
5759763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
5760763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
5761763757b2SScott Long 	    "InternalMacTransmitErrors");
5762763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
5763763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
5764763757b2SScott Long 	    "SingleCollisionFrames");
5765763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
5766763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
5767763757b2SScott Long 	    "MultipleCollisionFrames");
5768763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
5769763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
5770763757b2SScott Long 	    "DeferredTransmissions");
5771763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
5772763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
5773763757b2SScott Long 	    "ExcessiveCollisions");
5774763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
577506e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
577606e83c7eSScott Long 	    "LateCollisions");
5777763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
57781cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
5779763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
5780763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
5781763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
5782763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
5783763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
5784763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
5785763757b2SScott Long 	    "CarrierSenseErrors");
5786763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
5787763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
5788763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
5789763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
5790763757b2SScott Long }
5791763757b2SScott Long 
57922280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
57932280c16bSPyun YongHyeon 
57942280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
57956dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
57962280c16bSPyun YongHyeon 
57972280c16bSPyun YongHyeon static void
57982280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
57992280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
58002280c16bSPyun YongHyeon {
58012280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
58022280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
58032280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
58042280c16bSPyun YongHyeon 
58052280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
58062280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
58072280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
58082280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
58092280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
58102280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
58112280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
58122280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
58132280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
58142280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
58152280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
58162280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
58172280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
58182280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
58192280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
58202280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
58212280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
58222280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
58232280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
58242280c16bSPyun YongHyeon 
58252280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
58262280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
58272280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
58282280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
58292280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
58302280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
58312280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
58321cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
58332280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
58342280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
58352280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
58362280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
58372280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
58382280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
58392280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
58402280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
58412280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
58422280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
58432280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
58442280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
58452280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
58462280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
58472280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
58482280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
58492280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
58502280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
58512280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
58522280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
58532280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
58542280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
58552280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
58562280c16bSPyun YongHyeon 
58572280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
58582280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
58592280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
58601cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
58612280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
58622280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
58632280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
58642280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
58652280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
58662280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
58672280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
58682280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
58692280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
58702280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
58712280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
58722280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
58732280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
58742280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
58752280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
58762280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
58772280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
58782280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
58792280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
58802280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
58812280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
58821cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
58832280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
58841cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
58852280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
58861cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
58872280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
58882280c16bSPyun YongHyeon }
58892280c16bSPyun YongHyeon 
58902280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
58912280c16bSPyun YongHyeon 
5892763757b2SScott Long static int
5893763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
5894763757b2SScott Long {
5895763757b2SScott Long 	struct bge_softc *sc;
589606e83c7eSScott Long 	uint32_t result;
5897d949071dSJung-uk Kim 	int offset;
5898763757b2SScott Long 
5899763757b2SScott Long 	sc = (struct bge_softc *)arg1;
5900763757b2SScott Long 	offset = arg2;
5901d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
5902d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
5903041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
59046f8718a3SScott Long }
59056f8718a3SScott Long 
59066f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
59076f8718a3SScott Long static int
59086f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
59096f8718a3SScott Long {
59106f8718a3SScott Long 	struct bge_softc *sc;
59116f8718a3SScott Long 	uint16_t *sbdata;
591228276ad6SPyun YongHyeon 	int error, result, sbsz;
59136f8718a3SScott Long 	int i, j;
59146f8718a3SScott Long 
59156f8718a3SScott Long 	result = -1;
59166f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
59176f8718a3SScott Long 	if (error || (req->newptr == NULL))
59186f8718a3SScott Long 		return (error);
59196f8718a3SScott Long 
59206f8718a3SScott Long 	if (result == 1) {
59216f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
59226f8718a3SScott Long 
592328276ad6SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
592428276ad6SPyun YongHyeon 		    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
592528276ad6SPyun YongHyeon 			sbsz = BGE_STATUS_BLK_SZ;
592628276ad6SPyun YongHyeon 		else
592728276ad6SPyun YongHyeon 			sbsz = 32;
59286f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
59296f8718a3SScott Long 		printf("Status Block:\n");
593028276ad6SPyun YongHyeon 		BGE_LOCK(sc);
593128276ad6SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
593228276ad6SPyun YongHyeon 		    sc->bge_cdata.bge_status_map,
593328276ad6SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
593428276ad6SPyun YongHyeon 		for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
59356f8718a3SScott Long 			printf("%06x:", i);
593628276ad6SPyun YongHyeon 			for (j = 0; j < 8; j++)
593728276ad6SPyun YongHyeon 				printf(" %04x", sbdata[i++]);
59386f8718a3SScott Long 			printf("\n");
59396f8718a3SScott Long 		}
59406f8718a3SScott Long 
59416f8718a3SScott Long 		printf("Registers:\n");
59420c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
59436f8718a3SScott Long 			printf("%06x:", i);
59446f8718a3SScott Long 			for (j = 0; j < 8; j++) {
59456f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
59466f8718a3SScott Long 				i += 4;
59476f8718a3SScott Long 			}
59486f8718a3SScott Long 			printf("\n");
59496f8718a3SScott Long 		}
595028276ad6SPyun YongHyeon 		BGE_UNLOCK(sc);
59516f8718a3SScott Long 
59526f8718a3SScott Long 		printf("Hardware Flags:\n");
595328276ad6SPyun YongHyeon 		if (BGE_IS_5717_PLUS(sc))
595428276ad6SPyun YongHyeon 			printf(" - 5717 Plus\n");
5955a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
5956a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
59575345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
59586f8718a3SScott Long 			printf(" - 575X Plus\n");
59595345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
59606f8718a3SScott Long 			printf(" - 5705 Plus\n");
59615345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
59625345bad0SScott Long 			printf(" - 5714 Family\n");
59635345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
59645345bad0SScott Long 			printf(" - 5700 Family\n");
59656f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
59666f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
59676f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
59686f8718a3SScott Long 			printf(" - PCI-X Bus\n");
59696f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
59706f8718a3SScott Long 			printf(" - PCI Express Bus\n");
59717d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
59726f8718a3SScott Long 			printf(" - No 3 LEDs\n");
59736f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
59746f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
59756f8718a3SScott Long 	}
59766f8718a3SScott Long 
59776f8718a3SScott Long 	return (error);
59786f8718a3SScott Long }
59796f8718a3SScott Long 
59806f8718a3SScott Long static int
59816f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
59826f8718a3SScott Long {
59836f8718a3SScott Long 	struct bge_softc *sc;
59846f8718a3SScott Long 	int error;
59856f8718a3SScott Long 	uint16_t result;
59866f8718a3SScott Long 	uint32_t val;
59876f8718a3SScott Long 
59886f8718a3SScott Long 	result = -1;
59896f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
59906f8718a3SScott Long 	if (error || (req->newptr == NULL))
59916f8718a3SScott Long 		return (error);
59926f8718a3SScott Long 
59936f8718a3SScott Long 	if (result < 0x8000) {
59946f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
59956f8718a3SScott Long 		val = CSR_READ_4(sc, result);
59966f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
59976f8718a3SScott Long 	}
59986f8718a3SScott Long 
59996f8718a3SScott Long 	return (error);
60006f8718a3SScott Long }
60016f8718a3SScott Long 
60026f8718a3SScott Long static int
60036f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
60046f8718a3SScott Long {
60056f8718a3SScott Long 	struct bge_softc *sc;
60066f8718a3SScott Long 	int error;
60076f8718a3SScott Long 	uint16_t result;
60086f8718a3SScott Long 	uint32_t val;
60096f8718a3SScott Long 
60106f8718a3SScott Long 	result = -1;
60116f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
60126f8718a3SScott Long 	if (error || (req->newptr == NULL))
60136f8718a3SScott Long 		return (error);
60146f8718a3SScott Long 
60156f8718a3SScott Long 	if (result < 0x8000) {
60166f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
60176f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
60186f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
60196f8718a3SScott Long 	}
60206f8718a3SScott Long 
60216f8718a3SScott Long 	return (error);
60226f8718a3SScott Long }
60236f8718a3SScott Long #endif
602438cc658fSJohn Baldwin 
602538cc658fSJohn Baldwin static int
60265fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
60275fea260fSMarius Strobl {
60285fea260fSMarius Strobl 
60295fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
60305fea260fSMarius Strobl 		return (1);
60315fea260fSMarius Strobl 
60325fea260fSMarius Strobl #ifdef __sparc64__
60335fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
60345fea260fSMarius Strobl 	return (0);
60355fea260fSMarius Strobl #endif
60365fea260fSMarius Strobl 	return (1);
60375fea260fSMarius Strobl }
60385fea260fSMarius Strobl 
60395fea260fSMarius Strobl static int
604038cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
604138cc658fSJohn Baldwin {
604238cc658fSJohn Baldwin 	uint32_t mac_addr;
604338cc658fSJohn Baldwin 
604473635418SPyun YongHyeon 	mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
604538cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
604638cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
604738cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
604873635418SPyun YongHyeon 		mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
604938cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
605038cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
605138cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
605238cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
60535fea260fSMarius Strobl 		return (0);
605438cc658fSJohn Baldwin 	}
60555fea260fSMarius Strobl 	return (1);
605638cc658fSJohn Baldwin }
605738cc658fSJohn Baldwin 
605838cc658fSJohn Baldwin static int
605938cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
606038cc658fSJohn Baldwin {
606138cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
606238cc658fSJohn Baldwin 
606338cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
606438cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
606538cc658fSJohn Baldwin 
60665fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
60675fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
606838cc658fSJohn Baldwin }
606938cc658fSJohn Baldwin 
607038cc658fSJohn Baldwin static int
607138cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
607238cc658fSJohn Baldwin {
607338cc658fSJohn Baldwin 
60745fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
60755fea260fSMarius Strobl 		return (1);
60765fea260fSMarius Strobl 
60775fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
60785fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
607938cc658fSJohn Baldwin }
608038cc658fSJohn Baldwin 
608138cc658fSJohn Baldwin static int
608238cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
608338cc658fSJohn Baldwin {
608438cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
608538cc658fSJohn Baldwin 		/* NOTE: Order is critical */
60865fea260fSMarius Strobl 		bge_get_eaddr_fw,
608738cc658fSJohn Baldwin 		bge_get_eaddr_mem,
608838cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
608938cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
609038cc658fSJohn Baldwin 		NULL
609138cc658fSJohn Baldwin 	};
609238cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
609338cc658fSJohn Baldwin 
609438cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
609538cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
609638cc658fSJohn Baldwin 			break;
609738cc658fSJohn Baldwin 	}
609838cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
609938cc658fSJohn Baldwin }
6100