xref: /freebsd/sys/dev/bge/if_bge.c (revision f5459d4cad6bb85b1e5ac49456bc7d3a237b98e6)
1098ca2bdSWarner Losh /*-
295d67482SBill Paul  * Copyright (c) 2001 Wind River Systems
395d67482SBill Paul  * Copyright (c) 1997, 1998, 1999, 2001
495d67482SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
595d67482SBill Paul  *
695d67482SBill Paul  * Redistribution and use in source and binary forms, with or without
795d67482SBill Paul  * modification, are permitted provided that the following conditions
895d67482SBill Paul  * are met:
995d67482SBill Paul  * 1. Redistributions of source code must retain the above copyright
1095d67482SBill Paul  *    notice, this list of conditions and the following disclaimer.
1195d67482SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1295d67482SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1395d67482SBill Paul  *    documentation and/or other materials provided with the distribution.
1495d67482SBill Paul  * 3. All advertising materials mentioning features or use of this software
1595d67482SBill Paul  *    must display the following acknowledgement:
1695d67482SBill Paul  *	This product includes software developed by Bill Paul.
1795d67482SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1895d67482SBill Paul  *    may be used to endorse or promote products derived from this software
1995d67482SBill Paul  *    without specific prior written permission.
2095d67482SBill Paul  *
2195d67482SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2295d67482SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2395d67482SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2495d67482SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2595d67482SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2695d67482SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2795d67482SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2895d67482SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2995d67482SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3095d67482SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3195d67482SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3295d67482SBill Paul  */
3395d67482SBill Paul 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
3795d67482SBill Paul /*
3895d67482SBill Paul  * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
3995d67482SBill Paul  *
4095d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
4195d67482SBill Paul  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
4295d67482SBill Paul  * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
4395d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4495d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4595d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
4695d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
4795d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
4895d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
4995d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
5095d67482SBill Paul  * into the driver.
5195d67482SBill Paul  *
5295d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5395d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5495d67482SBill Paul  *
5595d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
5698b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
5795d67482SBill Paul  * does not support external SSRAM.
5895d67482SBill Paul  *
5995d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
6095d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
6195d67482SBill Paul  *
6295d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6395d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6495d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6595d67482SBill Paul  * result, this driver does not implement any support for the mini RX
6695d67482SBill Paul  * ring.
6795d67482SBill Paul  */
6895d67482SBill Paul 
6975719184SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
7075719184SGleb Smirnoff #include "opt_device_polling.h"
7175719184SGleb Smirnoff #endif
7275719184SGleb Smirnoff 
7395d67482SBill Paul #include <sys/param.h>
74f41ac2beSBill Paul #include <sys/endian.h>
7595d67482SBill Paul #include <sys/systm.h>
7695d67482SBill Paul #include <sys/sockio.h>
7795d67482SBill Paul #include <sys/mbuf.h>
7895d67482SBill Paul #include <sys/malloc.h>
7995d67482SBill Paul #include <sys/kernel.h>
80fe12f24bSPoul-Henning Kamp #include <sys/module.h>
8195d67482SBill Paul #include <sys/socket.h>
82f1a7e6d5SScott Long #include <sys/sysctl.h>
83dfe0df9aSPyun YongHyeon #include <sys/taskqueue.h>
8495d67482SBill Paul 
8595d67482SBill Paul #include <net/if.h>
8695d67482SBill Paul #include <net/if_arp.h>
8795d67482SBill Paul #include <net/ethernet.h>
8895d67482SBill Paul #include <net/if_dl.h>
8995d67482SBill Paul #include <net/if_media.h>
9095d67482SBill Paul 
9195d67482SBill Paul #include <net/bpf.h>
9295d67482SBill Paul 
9395d67482SBill Paul #include <net/if_types.h>
9495d67482SBill Paul #include <net/if_vlan_var.h>
9595d67482SBill Paul 
9695d67482SBill Paul #include <netinet/in_systm.h>
9795d67482SBill Paul #include <netinet/in.h>
9895d67482SBill Paul #include <netinet/ip.h>
99ca3f1187SPyun YongHyeon #include <netinet/tcp.h>
10095d67482SBill Paul 
10195d67482SBill Paul #include <machine/bus.h>
10295d67482SBill Paul #include <machine/resource.h>
10395d67482SBill Paul #include <sys/bus.h>
10495d67482SBill Paul #include <sys/rman.h>
10595d67482SBill Paul 
10695d67482SBill Paul #include <dev/mii/mii.h>
10795d67482SBill Paul #include <dev/mii/miivar.h>
1082d3ce713SDavid E. O'Brien #include "miidevs.h"
10995d67482SBill Paul #include <dev/mii/brgphyreg.h>
11095d67482SBill Paul 
11108013fd3SMarius Strobl #ifdef __sparc64__
11208013fd3SMarius Strobl #include <dev/ofw/ofw_bus.h>
11308013fd3SMarius Strobl #include <dev/ofw/openfirm.h>
11408013fd3SMarius Strobl #include <machine/ofw_machdep.h>
11508013fd3SMarius Strobl #include <machine/ver.h>
11608013fd3SMarius Strobl #endif
11708013fd3SMarius Strobl 
1184fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1194fbd232cSWarner Losh #include <dev/pci/pcivar.h>
12095d67482SBill Paul 
12195d67482SBill Paul #include <dev/bge/if_bgereg.h>
12295d67482SBill Paul 
12335f945cdSPyun YongHyeon #define	BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP)
124d375e524SGleb Smirnoff #define	ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
12595d67482SBill Paul 
126f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
127f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
12895d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12995d67482SBill Paul 
1307b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
13195d67482SBill Paul #include "miibus_if.h"
13295d67482SBill Paul 
13395d67482SBill Paul /*
13495d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
13595d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
13695d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13795d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13895d67482SBill Paul  */
139852c67f9SMarius Strobl static const struct bge_type {
1404c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1414c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
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 },
1744c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1754c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
176effef978SRemko Lodder 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5722 },
177a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5723 },
1784c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1794c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1804c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1814c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1824c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1834c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1844c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1854c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1864c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1874c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1889e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1899e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1909e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1919e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
192f7d1b2ebSXin LI 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5756 },
193a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761 },
194a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761E },
195a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761S },
196a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761SE },
197a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5764 },
1984c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
1994c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
2004c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
2014c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
202a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5784 },
203a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785F },
204a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785G },
2059e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
2069e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
207a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787F },
2089e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
2094c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
2104c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
2114c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
2124c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
2134c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
21438cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906 },
21538cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906M },
216a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57760 },
217a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57780 },
218a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57788 },
219a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57790 },
2204c0da0ffSGleb Smirnoff 
2214c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
2224c0da0ffSGleb Smirnoff 
2234c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
2244c0da0ffSGleb Smirnoff 
225a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE4 },
226a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE5 },
227a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PP250450 },
228a5779553SStanislav Sedov 
2294c0da0ffSGleb Smirnoff 	{ 0, 0 }
23095d67482SBill Paul };
23195d67482SBill Paul 
2324c0da0ffSGleb Smirnoff static const struct bge_vendor {
2334c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2344c0da0ffSGleb Smirnoff 	const char	*v_name;
235978f2704SMarius Strobl } const bge_vendors[] = {
2364c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2374c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2384c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2394c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2404c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2414c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
242a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	"Fujitsu" },
2434c0da0ffSGleb Smirnoff 
2444c0da0ffSGleb Smirnoff 	{ 0, NULL }
2454c0da0ffSGleb Smirnoff };
2464c0da0ffSGleb Smirnoff 
2474c0da0ffSGleb Smirnoff static const struct bge_revision {
2484c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2494c0da0ffSGleb Smirnoff 	const char	*br_name;
250978f2704SMarius Strobl } const bge_revisions[] = {
2514c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2524c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2534c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2544c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2554c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2564c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2574c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2584c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2594c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2604c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2614c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2624c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2634c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2644c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2654c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2664c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2679e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2684c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2694c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2704c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2714c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2724c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2734c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2744c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2754c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2764c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2774c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2784c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2794c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2804c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2814c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2824c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2834c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
28442787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2854c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2864c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2874c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
2884c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
2894c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
2904c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
2914c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
2924c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
2930c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5715_A3,	"BCM5715 A3" },
2941108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_A0,	"BCM5717 A0" },
2951108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_B0,	"BCM5717 B0" },
2960c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A0,	"BCM5755 A0" },
2970c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A1,	"BCM5755 A1" },
2980c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A2,	"BCM5755 A2" },
299bcc20328SJohn Baldwin 	{ BGE_CHIPID_BCM5722_A0,	"BCM5722 A0" },
300a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A0,	"BCM5761 A0" },
301a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A1,	"BCM5761 A1" },
302a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A0,	"BCM5784 A0" },
303a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A1,	"BCM5784 A1" },
30481179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3056f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
3066f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
3076f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
30838cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A1,	"BCM5906 A1" },
30938cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A2,	"BCM5906 A2" },
310a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A0,	"BCM57780 A0" },
311a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A1,	"BCM57780 A1" },
3124c0da0ffSGleb Smirnoff 
3134c0da0ffSGleb Smirnoff 	{ 0, NULL }
3144c0da0ffSGleb Smirnoff };
3154c0da0ffSGleb Smirnoff 
3164c0da0ffSGleb Smirnoff /*
3174c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
3184c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
3194c0da0ffSGleb Smirnoff  */
320978f2704SMarius Strobl static const struct bge_revision const bge_majorrevs[] = {
3219e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
3229e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
3239e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
3249e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
3259e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
3269e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
3279e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
3289e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
3299e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
3309e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
3319e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
332a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5761,		"unknown BCM5761" },
333a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5784,		"unknown BCM5784" },
334a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5785,		"unknown BCM5785" },
33581179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3366f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
33738cc658fSJohn Baldwin 	{ BGE_ASICREV_BCM5906,		"unknown BCM5906" },
338a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM57780,		"unknown BCM57780" },
3391108273aSPyun YongHyeon 	{ BGE_ASICREV_BCM5717,		"unknown BCM5717" },
3404c0da0ffSGleb Smirnoff 
3414c0da0ffSGleb Smirnoff 	{ 0, NULL }
3424c0da0ffSGleb Smirnoff };
3434c0da0ffSGleb Smirnoff 
3440c8aa4eaSJung-uk Kim #define	BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
3450c8aa4eaSJung-uk Kim #define	BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
3460c8aa4eaSJung-uk Kim #define	BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
3470c8aa4eaSJung-uk Kim #define	BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
3480c8aa4eaSJung-uk Kim #define	BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
349a5779553SStanislav Sedov #define	BGE_IS_5755_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5755_PLUS)
3501108273aSPyun YongHyeon #define	BGE_IS_5717_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5717_PLUS)
3514c0da0ffSGleb Smirnoff 
3524c0da0ffSGleb Smirnoff const struct bge_revision * bge_lookup_rev(uint32_t);
3534c0da0ffSGleb Smirnoff const struct bge_vendor * bge_lookup_vendor(uint16_t);
35438cc658fSJohn Baldwin 
35538cc658fSJohn Baldwin typedef int	(*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
35638cc658fSJohn Baldwin 
357e51a25f8SAlfred Perlstein static int bge_probe(device_t);
358e51a25f8SAlfred Perlstein static int bge_attach(device_t);
359e51a25f8SAlfred Perlstein static int bge_detach(device_t);
36014afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
36114afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3623f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
363f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
3645b610048SPyun YongHyeon static int bge_dma_alloc(struct bge_softc *);
365f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
3665b610048SPyun YongHyeon static int bge_dma_ring_alloc(struct bge_softc *, bus_size_t, bus_size_t,
3675b610048SPyun YongHyeon     bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
368f41ac2beSBill Paul 
3695fea260fSMarius Strobl static int bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[]);
37038cc658fSJohn Baldwin static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
37138cc658fSJohn Baldwin static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
37238cc658fSJohn Baldwin static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
37338cc658fSJohn Baldwin static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
37438cc658fSJohn Baldwin 
375b9c05fa5SPyun YongHyeon static void bge_txeof(struct bge_softc *, uint16_t);
3761108273aSPyun YongHyeon static void bge_rxcsum(struct bge_softc *, struct bge_rx_bd *, struct mbuf *);
377dfe0df9aSPyun YongHyeon static int bge_rxeof(struct bge_softc *, uint16_t, int);
37895d67482SBill Paul 
3798cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
380e51a25f8SAlfred Perlstein static void bge_tick(void *);
3812280c16bSPyun YongHyeon static void bge_stats_clear_regs(struct bge_softc *);
382e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
3833f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
384d598b626SPyun YongHyeon static struct mbuf *bge_check_short_dma(struct mbuf *);
3852e1d4df4SPyun YongHyeon static struct mbuf *bge_setup_tso(struct bge_softc *, struct mbuf *,
3861108273aSPyun YongHyeon     uint16_t *, uint16_t *);
387676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
38895d67482SBill Paul 
389e51a25f8SAlfred Perlstein static void bge_intr(void *);
390dfe0df9aSPyun YongHyeon static int bge_msi_intr(void *);
391dfe0df9aSPyun YongHyeon static void bge_intr_task(void *, int);
3920f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
393e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
394e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
3950f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
396e51a25f8SAlfred Perlstein static void bge_init(void *);
397e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
398b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
399b6c974e8SWarner Losh static int bge_shutdown(device_t);
40067d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
401e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
402e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
40395d67482SBill Paul 
40438cc658fSJohn Baldwin static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
40538cc658fSJohn Baldwin static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
40638cc658fSJohn Baldwin 
4073f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
408e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
40995d67482SBill Paul 
4103e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
411e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
412cb2eacc7SYaroslav Tykhiy static void bge_setvlan(struct bge_softc *);
41395d67482SBill Paul 
414e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_std(struct bge_softc *, int);
415e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_jumbo(struct bge_softc *, int);
416943787f3SPyun YongHyeon static int bge_newbuf_std(struct bge_softc *, int);
417943787f3SPyun YongHyeon static int bge_newbuf_jumbo(struct bge_softc *, int);
418e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
419e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
420e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
421e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
422e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
423e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
42495d67482SBill Paul 
425e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
426e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
42795d67482SBill Paul 
4285fea260fSMarius Strobl static int bge_has_eaddr(struct bge_softc *);
4293f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
430e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
43138cc658fSJohn Baldwin static void bge_writembx(struct bge_softc *, int, int);
43295d67482SBill Paul #ifdef notdef
4333f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
43495d67482SBill Paul #endif
4359ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
436e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
43795d67482SBill Paul 
438e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
439e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
440e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
44175719184SGleb Smirnoff #ifdef DEVICE_POLLING
4421abcdbd1SAttilio Rao static int bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
44375719184SGleb Smirnoff #endif
44495d67482SBill Paul 
4458cb1383cSDoug Ambrisko #define	BGE_RESET_START 1
4468cb1383cSDoug Ambrisko #define	BGE_RESET_STOP  2
4478cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
4488cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
4498cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
450797ab05eSPyun YongHyeon static void bge_stop_fw(struct bge_softc *);
4518cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
452dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
45395d67482SBill Paul 
4546f8718a3SScott Long /*
4556f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
4566f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
4576f8718a3SScott Long  * traps on certain architectures.
4586f8718a3SScott Long  */
4596f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
4606f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
4616f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
4626f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
4636f8718a3SScott Long #endif
4646f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
4652280c16bSPyun YongHyeon static void bge_add_sysctl_stats_regs(struct bge_softc *,
4662280c16bSPyun YongHyeon     struct sysctl_ctx_list *, struct sysctl_oid_list *);
4672280c16bSPyun YongHyeon static void bge_add_sysctl_stats(struct bge_softc *, struct sysctl_ctx_list *,
4682280c16bSPyun YongHyeon     struct sysctl_oid_list *);
469763757b2SScott Long static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
4706f8718a3SScott Long 
47195d67482SBill Paul static device_method_t bge_methods[] = {
47295d67482SBill Paul 	/* Device interface */
47395d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
47495d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
47595d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
47695d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
47714afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
47814afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
47995d67482SBill Paul 
48095d67482SBill Paul 	/* bus interface */
48195d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
48295d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
48395d67482SBill Paul 
48495d67482SBill Paul 	/* MII interface */
48595d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
48695d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
48795d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
48895d67482SBill Paul 
48995d67482SBill Paul 	{ 0, 0 }
49095d67482SBill Paul };
49195d67482SBill Paul 
49295d67482SBill Paul static driver_t bge_driver = {
49395d67482SBill Paul 	"bge",
49495d67482SBill Paul 	bge_methods,
49595d67482SBill Paul 	sizeof(struct bge_softc)
49695d67482SBill Paul };
49795d67482SBill Paul 
49895d67482SBill Paul static devclass_t bge_devclass;
49995d67482SBill Paul 
500f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
50195d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
50295d67482SBill Paul 
503f1a7e6d5SScott Long static int bge_allow_asf = 1;
504f1a7e6d5SScott Long 
505f1a7e6d5SScott Long TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
506f1a7e6d5SScott Long 
507f1a7e6d5SScott Long SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
508f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
509f1a7e6d5SScott Long 	"Allow ASF mode if available");
510c4529f41SMichael Reifenberger 
51108013fd3SMarius Strobl #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
51208013fd3SMarius Strobl #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
51308013fd3SMarius Strobl #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
51408013fd3SMarius Strobl #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
51508013fd3SMarius Strobl #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
51608013fd3SMarius Strobl 
51708013fd3SMarius Strobl static int
5185fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
51908013fd3SMarius Strobl {
52008013fd3SMarius Strobl #ifdef __sparc64__
52108013fd3SMarius Strobl 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
52208013fd3SMarius Strobl 	device_t dev;
52308013fd3SMarius Strobl 	uint32_t subvendor;
52408013fd3SMarius Strobl 
52508013fd3SMarius Strobl 	dev = sc->bge_dev;
52608013fd3SMarius Strobl 
52708013fd3SMarius Strobl 	/*
52808013fd3SMarius Strobl 	 * The on-board BGEs found in sun4u machines aren't fitted with
52908013fd3SMarius Strobl 	 * an EEPROM which means that we have to obtain the MAC address
53008013fd3SMarius Strobl 	 * via OFW and that some tests will always fail.  We distinguish
53108013fd3SMarius Strobl 	 * such BGEs by the subvendor ID, which also has to be obtained
53208013fd3SMarius Strobl 	 * from OFW instead of the PCI configuration space as the latter
53308013fd3SMarius Strobl 	 * indicates Broadcom as the subvendor of the netboot interface.
53408013fd3SMarius Strobl 	 * For early Blade 1500 and 2500 we even have to check the OFW
53508013fd3SMarius Strobl 	 * device path as the subvendor ID always defaults to Broadcom
53608013fd3SMarius Strobl 	 * there.
53708013fd3SMarius Strobl 	 */
53808013fd3SMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
53908013fd3SMarius Strobl 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
5402d857b9bSMarius Strobl 	    (subvendor == FJTSU_VENDORID || subvendor == SUN_VENDORID))
54108013fd3SMarius Strobl 		return (0);
54208013fd3SMarius Strobl 	memset(buf, 0, sizeof(buf));
54308013fd3SMarius Strobl 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
54408013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
54508013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
54608013fd3SMarius Strobl 			return (0);
54708013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
54808013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
54908013fd3SMarius Strobl 			return (0);
55008013fd3SMarius Strobl 	}
55108013fd3SMarius Strobl #endif
55208013fd3SMarius Strobl 	return (1);
55308013fd3SMarius Strobl }
55408013fd3SMarius Strobl 
5553f74909aSGleb Smirnoff static uint32_t
5563f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
55795d67482SBill Paul {
55895d67482SBill Paul 	device_t dev;
5596f8718a3SScott Long 	uint32_t val;
56095d67482SBill Paul 
561a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
562a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
563a4431ebaSPyun YongHyeon 		return (0);
564a4431ebaSPyun YongHyeon 
56595d67482SBill Paul 	dev = sc->bge_dev;
56695d67482SBill Paul 
56795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
5686f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
5696f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
5706f8718a3SScott Long 	return (val);
57195d67482SBill Paul }
57295d67482SBill Paul 
57395d67482SBill Paul static void
5743f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
57595d67482SBill Paul {
57695d67482SBill Paul 	device_t dev;
57795d67482SBill Paul 
578a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
579a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
580a4431ebaSPyun YongHyeon 		return;
581a4431ebaSPyun YongHyeon 
58295d67482SBill Paul 	dev = sc->bge_dev;
58395d67482SBill Paul 
58495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
58595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
5866f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
58795d67482SBill Paul }
58895d67482SBill Paul 
58995d67482SBill Paul #ifdef notdef
5903f74909aSGleb Smirnoff static uint32_t
5913f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
59295d67482SBill Paul {
59395d67482SBill Paul 	device_t dev;
59495d67482SBill Paul 
59595d67482SBill Paul 	dev = sc->bge_dev;
59695d67482SBill Paul 
59795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
59895d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
59995d67482SBill Paul }
60095d67482SBill Paul #endif
60195d67482SBill Paul 
60295d67482SBill Paul static void
6033f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
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 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
61195d67482SBill Paul }
61295d67482SBill Paul 
6136f8718a3SScott Long static void
6146f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
6156f8718a3SScott Long {
6166f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6176f8718a3SScott Long }
6186f8718a3SScott Long 
61938cc658fSJohn Baldwin static void
62038cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
62138cc658fSJohn Baldwin {
62238cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
62338cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
62438cc658fSJohn Baldwin 
62538cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
62638cc658fSJohn Baldwin }
62738cc658fSJohn Baldwin 
628f41ac2beSBill Paul /*
629f41ac2beSBill Paul  * Map a single buffer address.
630f41ac2beSBill Paul  */
631f41ac2beSBill Paul 
632f41ac2beSBill Paul static void
6333f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
634f41ac2beSBill Paul {
635f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
636f41ac2beSBill Paul 
637f41ac2beSBill Paul 	if (error)
638f41ac2beSBill Paul 		return;
639f41ac2beSBill Paul 
6405b610048SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
6415b610048SPyun YongHyeon 
642f41ac2beSBill Paul 	ctx = arg;
643f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
644f41ac2beSBill Paul }
645f41ac2beSBill Paul 
64638cc658fSJohn Baldwin static uint8_t
64738cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
64838cc658fSJohn Baldwin {
64938cc658fSJohn Baldwin 	uint32_t access, byte = 0;
65038cc658fSJohn Baldwin 	int i;
65138cc658fSJohn Baldwin 
65238cc658fSJohn Baldwin 	/* Lock. */
65338cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
65438cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
65538cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
65638cc658fSJohn Baldwin 			break;
65738cc658fSJohn Baldwin 		DELAY(20);
65838cc658fSJohn Baldwin 	}
65938cc658fSJohn Baldwin 	if (i == 8000)
66038cc658fSJohn Baldwin 		return (1);
66138cc658fSJohn Baldwin 
66238cc658fSJohn Baldwin 	/* Enable access. */
66338cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
66438cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
66538cc658fSJohn Baldwin 
66638cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
66738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
66838cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
66938cc658fSJohn Baldwin 		DELAY(10);
67038cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
67138cc658fSJohn Baldwin 			DELAY(10);
67238cc658fSJohn Baldwin 			break;
67338cc658fSJohn Baldwin 		}
67438cc658fSJohn Baldwin 	}
67538cc658fSJohn Baldwin 
67638cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
67738cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
67838cc658fSJohn Baldwin 		return (1);
67938cc658fSJohn Baldwin 	}
68038cc658fSJohn Baldwin 
68138cc658fSJohn Baldwin 	/* Get result. */
68238cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
68338cc658fSJohn Baldwin 
68438cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
68538cc658fSJohn Baldwin 
68638cc658fSJohn Baldwin 	/* Disable access. */
68738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
68838cc658fSJohn Baldwin 
68938cc658fSJohn Baldwin 	/* Unlock. */
69038cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
69138cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
69238cc658fSJohn Baldwin 
69338cc658fSJohn Baldwin 	return (0);
69438cc658fSJohn Baldwin }
69538cc658fSJohn Baldwin 
69638cc658fSJohn Baldwin /*
69738cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
69838cc658fSJohn Baldwin  */
69938cc658fSJohn Baldwin static int
70038cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
70138cc658fSJohn Baldwin {
70238cc658fSJohn Baldwin 	int err = 0, i;
70338cc658fSJohn Baldwin 	uint8_t byte = 0;
70438cc658fSJohn Baldwin 
70538cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
70638cc658fSJohn Baldwin 		return (1);
70738cc658fSJohn Baldwin 
70838cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
70938cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
71038cc658fSJohn Baldwin 		if (err)
71138cc658fSJohn Baldwin 			break;
71238cc658fSJohn Baldwin 		*(dest + i) = byte;
71338cc658fSJohn Baldwin 	}
71438cc658fSJohn Baldwin 
71538cc658fSJohn Baldwin 	return (err ? 1 : 0);
71638cc658fSJohn Baldwin }
71738cc658fSJohn Baldwin 
71895d67482SBill Paul /*
71995d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
72095d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
72195d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
72295d67482SBill Paul  * access method.
72395d67482SBill Paul  */
7243f74909aSGleb Smirnoff static uint8_t
7253f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
72695d67482SBill Paul {
72795d67482SBill Paul 	int i;
7283f74909aSGleb Smirnoff 	uint32_t byte = 0;
72995d67482SBill Paul 
73095d67482SBill Paul 	/*
73195d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
73295d67482SBill Paul 	 * having to use the bitbang method.
73395d67482SBill Paul 	 */
73495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
73595d67482SBill Paul 
73695d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
73795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
73895d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
73995d67482SBill Paul 	DELAY(20);
74095d67482SBill Paul 
74195d67482SBill Paul 	/* Issue the read EEPROM command. */
74295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
74395d67482SBill Paul 
74495d67482SBill Paul 	/* Wait for completion */
74595d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
74695d67482SBill Paul 		DELAY(10);
74795d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
74895d67482SBill Paul 			break;
74995d67482SBill Paul 	}
75095d67482SBill Paul 
751d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
752fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
753f6789fbaSPyun YongHyeon 		return (1);
75495d67482SBill Paul 	}
75595d67482SBill Paul 
75695d67482SBill Paul 	/* Get result. */
75795d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
75895d67482SBill Paul 
7590c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
76095d67482SBill Paul 
76195d67482SBill Paul 	return (0);
76295d67482SBill Paul }
76395d67482SBill Paul 
76495d67482SBill Paul /*
76595d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
76695d67482SBill Paul  */
76795d67482SBill Paul static int
7683f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
76995d67482SBill Paul {
7703f74909aSGleb Smirnoff 	int i, error = 0;
7713f74909aSGleb Smirnoff 	uint8_t byte = 0;
77295d67482SBill Paul 
77395d67482SBill Paul 	for (i = 0; i < cnt; i++) {
7743f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
7753f74909aSGleb Smirnoff 		if (error)
77695d67482SBill Paul 			break;
77795d67482SBill Paul 		*(dest + i) = byte;
77895d67482SBill Paul 	}
77995d67482SBill Paul 
7803f74909aSGleb Smirnoff 	return (error ? 1 : 0);
78195d67482SBill Paul }
78295d67482SBill Paul 
78395d67482SBill Paul static int
7843f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
78595d67482SBill Paul {
78695d67482SBill Paul 	struct bge_softc *sc;
787a813ed78SPyun YongHyeon 	uint32_t val;
78895d67482SBill Paul 	int i;
78995d67482SBill Paul 
79095d67482SBill Paul 	sc = device_get_softc(dev);
79195d67482SBill Paul 
792a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
793a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
794a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
795a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
796a813ed78SPyun YongHyeon 		DELAY(80);
79737ceeb4dSPaul Saab 	}
79837ceeb4dSPaul Saab 
79995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
80095d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
80195d67482SBill Paul 
802a813ed78SPyun YongHyeon 	/* Poll for the PHY register access to complete. */
80395d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
804d5d23857SJung-uk Kim 		DELAY(10);
80595d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
806a813ed78SPyun YongHyeon 		if ((val & BGE_MICOMM_BUSY) == 0) {
807a813ed78SPyun YongHyeon 			DELAY(5);
808a813ed78SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_MI_COMM);
80995d67482SBill Paul 			break;
81095d67482SBill Paul 		}
811a813ed78SPyun YongHyeon 	}
81295d67482SBill Paul 
81395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
8145fea260fSMarius Strobl 		device_printf(sc->bge_dev,
8155fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
8165fea260fSMarius Strobl 		    phy, reg, val);
81737ceeb4dSPaul Saab 		val = 0;
81895d67482SBill Paul 	}
81995d67482SBill Paul 
820a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
821a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
822a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
823a813ed78SPyun YongHyeon 		DELAY(80);
82437ceeb4dSPaul Saab 	}
82537ceeb4dSPaul Saab 
82695d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
82795d67482SBill Paul 		return (0);
82895d67482SBill Paul 
8290c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
83095d67482SBill Paul }
83195d67482SBill Paul 
83295d67482SBill Paul static int
8333f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
83495d67482SBill Paul {
83595d67482SBill Paul 	struct bge_softc *sc;
83695d67482SBill Paul 	int i;
83795d67482SBill Paul 
83895d67482SBill Paul 	sc = device_get_softc(dev);
83995d67482SBill Paul 
84038cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
84138cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
84238cc658fSJohn Baldwin 		return (0);
84338cc658fSJohn Baldwin 
844a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
845a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
846a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
847a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
848a813ed78SPyun YongHyeon 		DELAY(80);
84937ceeb4dSPaul Saab 	}
85037ceeb4dSPaul Saab 
85195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
85295d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
85395d67482SBill Paul 
85495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
855d5d23857SJung-uk Kim 		DELAY(10);
85638cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
85738cc658fSJohn Baldwin 			DELAY(5);
85838cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
85995d67482SBill Paul 			break;
860d5d23857SJung-uk Kim 		}
86138cc658fSJohn Baldwin 	}
862d5d23857SJung-uk Kim 
863a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
864a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
865a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
866a813ed78SPyun YongHyeon 		DELAY(80);
867a813ed78SPyun YongHyeon 	}
868a813ed78SPyun YongHyeon 
869a813ed78SPyun YongHyeon 	if (i == BGE_TIMEOUT)
87038cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
87138cc658fSJohn Baldwin 		    "PHY write timed out (phy %d, reg %d, val %d)\n",
87238cc658fSJohn Baldwin 		    phy, reg, val);
87337ceeb4dSPaul Saab 
87495d67482SBill Paul 	return (0);
87595d67482SBill Paul }
87695d67482SBill Paul 
87795d67482SBill Paul static void
8783f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
87995d67482SBill Paul {
88095d67482SBill Paul 	struct bge_softc *sc;
88195d67482SBill Paul 	struct mii_data *mii;
88295d67482SBill Paul 	sc = device_get_softc(dev);
88395d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
88495d67482SBill Paul 
885d4f5240aSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
886d4f5240aSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
887d4f5240aSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
888d4f5240aSPyun YongHyeon 		case IFM_10_T:
889d4f5240aSPyun YongHyeon 		case IFM_100_TX:
890d4f5240aSPyun YongHyeon 			sc->bge_link = 1;
891d4f5240aSPyun YongHyeon 			break;
892d4f5240aSPyun YongHyeon 		case IFM_1000_T:
893d4f5240aSPyun YongHyeon 		case IFM_1000_SX:
894d4f5240aSPyun YongHyeon 		case IFM_2500_SX:
895d4f5240aSPyun YongHyeon 			if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
896d4f5240aSPyun YongHyeon 				sc->bge_link = 1;
897d4f5240aSPyun YongHyeon 			else
898d4f5240aSPyun YongHyeon 				sc->bge_link = 0;
899d4f5240aSPyun YongHyeon 			break;
900d4f5240aSPyun YongHyeon 		default:
901d4f5240aSPyun YongHyeon 			sc->bge_link = 0;
902d4f5240aSPyun YongHyeon 			break;
903d4f5240aSPyun YongHyeon 		}
904d4f5240aSPyun YongHyeon 	} else
905d4f5240aSPyun YongHyeon 		sc->bge_link = 0;
906d4f5240aSPyun YongHyeon 	if (sc->bge_link == 0)
907d4f5240aSPyun YongHyeon 		return;
90895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
909ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
910ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
91195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
9123f74909aSGleb Smirnoff 	else
91395d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
91495d67482SBill Paul 
9156854be25SPyun YongHyeon 	if (IFM_OPTIONS(mii->mii_media_active & IFM_FDX) != 0) {
91695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
917efd4fc3fSMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
918efd4fc3fSMarius Strobl 		    IFM_ETH_TXPAUSE) != 0)
9196854be25SPyun YongHyeon 			BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
9203f74909aSGleb Smirnoff 		else
9216854be25SPyun YongHyeon 			BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
922efd4fc3fSMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
923efd4fc3fSMarius Strobl 		    IFM_ETH_RXPAUSE) != 0)
9246854be25SPyun YongHyeon 			BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
9256854be25SPyun YongHyeon 		else
9266854be25SPyun YongHyeon 			BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
9276854be25SPyun YongHyeon 	} else {
92895d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
9296854be25SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
9306854be25SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
9316854be25SPyun YongHyeon 	}
93295d67482SBill Paul }
93395d67482SBill Paul 
93495d67482SBill Paul /*
93595d67482SBill Paul  * Intialize a standard receive ring descriptor.
93695d67482SBill Paul  */
93795d67482SBill Paul static int
938943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
93995d67482SBill Paul {
940943787f3SPyun YongHyeon 	struct mbuf *m;
94195d67482SBill Paul 	struct bge_rx_bd *r;
942a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
943943787f3SPyun YongHyeon 	bus_dmamap_t map;
944a23634a1SPyun YongHyeon 	int error, nsegs;
94595d67482SBill Paul 
946*f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD &&
947*f5459d4cSPyun YongHyeon 	    (sc->bge_ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
948*f5459d4cSPyun YongHyeon 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))) {
949*f5459d4cSPyun YongHyeon 		m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
950*f5459d4cSPyun YongHyeon 		if (m == NULL)
951*f5459d4cSPyun YongHyeon 			return (ENOBUFS);
952*f5459d4cSPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MJUM9BYTES;
953*f5459d4cSPyun YongHyeon 	} else {
954943787f3SPyun YongHyeon 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
955943787f3SPyun YongHyeon 		if (m == NULL)
95695d67482SBill Paul 			return (ENOBUFS);
957943787f3SPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MCLBYTES;
958*f5459d4cSPyun YongHyeon 	}
959652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
960943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
961943787f3SPyun YongHyeon 
9620ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
963943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
964a23634a1SPyun YongHyeon 	if (error != 0) {
965943787f3SPyun YongHyeon 		m_freem(m);
966a23634a1SPyun YongHyeon 		return (error);
967f41ac2beSBill Paul 	}
968943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
969943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
970943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
971943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
972943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
973943787f3SPyun YongHyeon 	}
974943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
975943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
976943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
977943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
978e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
979943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
980a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
981a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
982e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
983a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
984e907febfSPyun YongHyeon 	r->bge_idx = i;
985f41ac2beSBill Paul 
9860ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
987943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
98895d67482SBill Paul 
98995d67482SBill Paul 	return (0);
99095d67482SBill Paul }
99195d67482SBill Paul 
99295d67482SBill Paul /*
99395d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
99495d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
99595d67482SBill Paul  */
99695d67482SBill Paul static int
997943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
99895d67482SBill Paul {
9991be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
1000943787f3SPyun YongHyeon 	bus_dmamap_t map;
10011be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
1002943787f3SPyun YongHyeon 	struct mbuf *m;
1003943787f3SPyun YongHyeon 	int error, nsegs;
100495d67482SBill Paul 
1005943787f3SPyun YongHyeon 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1006943787f3SPyun YongHyeon 	if (m == NULL)
100795d67482SBill Paul 		return (ENOBUFS);
100895d67482SBill Paul 
1009943787f3SPyun YongHyeon 	m_cljget(m, M_DONTWAIT, MJUM9BYTES);
1010943787f3SPyun YongHyeon 	if (!(m->m_flags & M_EXT)) {
1011943787f3SPyun YongHyeon 		m_freem(m);
101295d67482SBill Paul 		return (ENOBUFS);
101395d67482SBill Paul 	}
1014943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1015652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1016943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
10171be6acb7SGleb Smirnoff 
10181be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1019943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1020943787f3SPyun YongHyeon 	if (error != 0) {
1021943787f3SPyun YongHyeon 		m_freem(m);
10221be6acb7SGleb Smirnoff 		return (error);
1023f7cea149SGleb Smirnoff 	}
10241be6acb7SGleb Smirnoff 
1025943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] == NULL) {
1026943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1027943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1028943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1029943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1030943787f3SPyun YongHyeon 	}
1031943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1032943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1033943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1034943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1035943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1036e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1037e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1038e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1039e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1040e0b7b101SPyun YongHyeon 
10411be6acb7SGleb Smirnoff 	/*
10421be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
10431be6acb7SGleb Smirnoff 	 */
1044943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
10454e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
10464e7ba1abSGleb Smirnoff 	r->bge_idx = i;
10474e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
10484e7ba1abSGleb Smirnoff 	switch (nsegs) {
10494e7ba1abSGleb Smirnoff 	case 4:
10504e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
10514e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
10524e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1053e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
10544e7ba1abSGleb Smirnoff 	case 3:
1055e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1056e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1057e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1058e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
10594e7ba1abSGleb Smirnoff 	case 2:
10604e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
10614e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
10624e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1063e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
10644e7ba1abSGleb Smirnoff 	case 1:
10654e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
10664e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
10674e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1068e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
10694e7ba1abSGleb Smirnoff 		break;
10704e7ba1abSGleb Smirnoff 	default:
10714e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
10724e7ba1abSGleb Smirnoff 	}
1073f41ac2beSBill Paul 
1074a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1075943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
107695d67482SBill Paul 
107795d67482SBill Paul 	return (0);
107895d67482SBill Paul }
107995d67482SBill Paul 
108095d67482SBill Paul static int
10813f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
108295d67482SBill Paul {
10833ee5d7daSPyun YongHyeon 	int error, i;
108495d67482SBill Paul 
1085e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
108603e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1087e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1088943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
10893ee5d7daSPyun YongHyeon 			return (error);
109003e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
10911888f324SPyun YongHyeon 	}
109295d67482SBill Paul 
1093f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1094d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1095f41ac2beSBill Paul 
1096e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1097e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
109895d67482SBill Paul 
109995d67482SBill Paul 	return (0);
110095d67482SBill Paul }
110195d67482SBill Paul 
110295d67482SBill Paul static void
11033f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
110495d67482SBill Paul {
110595d67482SBill Paul 	int i;
110695d67482SBill Paul 
110795d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
110895d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
11090ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1110e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1111e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
11120ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1113f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1114e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1115e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
111695d67482SBill Paul 		}
1117f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
111895d67482SBill Paul 		    sizeof(struct bge_rx_bd));
111995d67482SBill Paul 	}
112095d67482SBill Paul }
112195d67482SBill Paul 
112295d67482SBill Paul static int
11233f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
112495d67482SBill Paul {
112595d67482SBill Paul 	struct bge_rcb *rcb;
11263ee5d7daSPyun YongHyeon 	int error, i;
112795d67482SBill Paul 
1128e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
112903e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
113095d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1131943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
11323ee5d7daSPyun YongHyeon 			return (error);
113303e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
11341888f324SPyun YongHyeon 	}
113595d67482SBill Paul 
1136f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1137d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1138f41ac2beSBill Paul 
1139e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
114095d67482SBill Paul 
11418a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1142f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
11438a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
11448a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
114567111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
114695d67482SBill Paul 
1147e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
114895d67482SBill Paul 
114995d67482SBill Paul 	return (0);
115095d67482SBill Paul }
115195d67482SBill Paul 
115295d67482SBill Paul static void
11533f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
115495d67482SBill Paul {
115595d67482SBill Paul 	int i;
115695d67482SBill Paul 
115795d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
115895d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1159e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1160e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1161e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1162f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1163f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1164e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1165e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
116695d67482SBill Paul 		}
1167f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
11681be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
116995d67482SBill Paul 	}
117095d67482SBill Paul }
117195d67482SBill Paul 
117295d67482SBill Paul static void
11733f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
117495d67482SBill Paul {
117595d67482SBill Paul 	int i;
117695d67482SBill Paul 
1177f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
117895d67482SBill Paul 		return;
117995d67482SBill Paul 
118095d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
118195d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
11820ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1183e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1184e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
11850ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1186f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1187e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1188e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
118995d67482SBill Paul 		}
1190f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
119195d67482SBill Paul 		    sizeof(struct bge_tx_bd));
119295d67482SBill Paul 	}
119395d67482SBill Paul }
119495d67482SBill Paul 
119595d67482SBill Paul static int
11963f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
119795d67482SBill Paul {
119895d67482SBill Paul 	sc->bge_txcnt = 0;
119995d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
12003927098fSPaul Saab 
1201e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1202e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
12035c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1204e6bf277eSPyun YongHyeon 
120514bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
120614bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
120738cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
120814bbd30fSGleb Smirnoff 
12093927098fSPaul Saab 	/* 5700 b2 errata */
1210e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
121138cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
12123927098fSPaul Saab 
121314bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
121438cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
12153927098fSPaul Saab 	/* 5700 b2 errata */
1216e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
121738cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
121895d67482SBill Paul 
121995d67482SBill Paul 	return (0);
122095d67482SBill Paul }
122195d67482SBill Paul 
122295d67482SBill Paul static void
12233e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
12243e9b1bcaSJung-uk Kim {
12253e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
12263e9b1bcaSJung-uk Kim 
12273e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
12283e9b1bcaSJung-uk Kim 
12293e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
12303e9b1bcaSJung-uk Kim 
123145ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
12323e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
123345ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
12343e9b1bcaSJung-uk Kim 	else
123545ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
12363e9b1bcaSJung-uk Kim }
12373e9b1bcaSJung-uk Kim 
12383e9b1bcaSJung-uk Kim static void
12393f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
124095d67482SBill Paul {
124195d67482SBill Paul 	struct ifnet *ifp;
124295d67482SBill Paul 	struct ifmultiaddr *ifma;
12433f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
124495d67482SBill Paul 	int h, i;
124595d67482SBill Paul 
12460f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
12470f9bd73bSSam Leffler 
1248fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
124995d67482SBill Paul 
125095d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
125195d67482SBill Paul 		for (i = 0; i < 4; i++)
12520c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
125395d67482SBill Paul 		return;
125495d67482SBill Paul 	}
125595d67482SBill Paul 
125695d67482SBill Paul 	/* First, zot all the existing filters. */
125795d67482SBill Paul 	for (i = 0; i < 4; i++)
125895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
125995d67482SBill Paul 
126095d67482SBill Paul 	/* Now program new ones. */
1261eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
126295d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
126395d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
126495d67482SBill Paul 			continue;
12650e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
12660c8aa4eaSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
12670c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
126895d67482SBill Paul 	}
1269eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
127095d67482SBill Paul 
127195d67482SBill Paul 	for (i = 0; i < 4; i++)
127295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
127395d67482SBill Paul }
127495d67482SBill Paul 
12758cb1383cSDoug Ambrisko static void
1276cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1277cb2eacc7SYaroslav Tykhiy {
1278cb2eacc7SYaroslav Tykhiy 	struct ifnet *ifp;
1279cb2eacc7SYaroslav Tykhiy 
1280cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1281cb2eacc7SYaroslav Tykhiy 
1282cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1283cb2eacc7SYaroslav Tykhiy 
1284cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1285cb2eacc7SYaroslav Tykhiy 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1286cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1287cb2eacc7SYaroslav Tykhiy 	else
1288cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1289cb2eacc7SYaroslav Tykhiy }
1290cb2eacc7SYaroslav Tykhiy 
1291cb2eacc7SYaroslav Tykhiy static void
1292797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
12938cb1383cSDoug Ambrisko {
1294797ab05eSPyun YongHyeon 
12958cb1383cSDoug Ambrisko 	/*
12968cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
12978cb1383cSDoug Ambrisko 	 */
12988cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
12998cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
13008cb1383cSDoug Ambrisko 
13018cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
13028cb1383cSDoug Ambrisko 		switch (type) {
13038cb1383cSDoug Ambrisko 		case BGE_RESET_START:
13048cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
13058cb1383cSDoug Ambrisko 			break;
13068cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
13078cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
13088cb1383cSDoug Ambrisko 			break;
13098cb1383cSDoug Ambrisko 		}
13108cb1383cSDoug Ambrisko 	}
13118cb1383cSDoug Ambrisko }
13128cb1383cSDoug Ambrisko 
13138cb1383cSDoug Ambrisko static void
1314797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
13158cb1383cSDoug Ambrisko {
1316797ab05eSPyun YongHyeon 
13178cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
13188cb1383cSDoug Ambrisko 		switch (type) {
13198cb1383cSDoug Ambrisko 		case BGE_RESET_START:
13208cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000001);
13218cb1383cSDoug Ambrisko 			/* START DONE */
13228cb1383cSDoug Ambrisko 			break;
13238cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
13248cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000002);
13258cb1383cSDoug Ambrisko 			break;
13268cb1383cSDoug Ambrisko 		}
13278cb1383cSDoug Ambrisko 	}
13288cb1383cSDoug Ambrisko }
13298cb1383cSDoug Ambrisko 
13308cb1383cSDoug Ambrisko static void
1331797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
13328cb1383cSDoug Ambrisko {
1333797ab05eSPyun YongHyeon 
13348cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
13358cb1383cSDoug Ambrisko 		switch (type) {
13368cb1383cSDoug Ambrisko 		case BGE_RESET_START:
13378cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
13388cb1383cSDoug Ambrisko 			break;
13398cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
13408cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
13418cb1383cSDoug Ambrisko 			break;
13428cb1383cSDoug Ambrisko 		}
13438cb1383cSDoug Ambrisko 	}
13448cb1383cSDoug Ambrisko }
13458cb1383cSDoug Ambrisko 
1346797ab05eSPyun YongHyeon static void
1347797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
13488cb1383cSDoug Ambrisko {
13498cb1383cSDoug Ambrisko 	int i;
13508cb1383cSDoug Ambrisko 
13518cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
13528cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW, BGE_FW_PAUSE);
13538cb1383cSDoug Ambrisko 		CSR_WRITE_4(sc, BGE_CPU_EVENT,
135439153c5aSJung-uk Kim 		    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
13558cb1383cSDoug Ambrisko 
13568cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
13578cb1383cSDoug Ambrisko 			if (!(CSR_READ_4(sc, BGE_CPU_EVENT) & (1 << 14)))
13588cb1383cSDoug Ambrisko 				break;
13598cb1383cSDoug Ambrisko 			DELAY(10);
13608cb1383cSDoug Ambrisko 		}
13618cb1383cSDoug Ambrisko 	}
13628cb1383cSDoug Ambrisko }
13638cb1383cSDoug Ambrisko 
136495d67482SBill Paul /*
1365c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
136695d67482SBill Paul  */
136795d67482SBill Paul static int
13683f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
136995d67482SBill Paul {
13701108273aSPyun YongHyeon 	uint32_t dma_rw_ctl, misc_ctl;
1371fbc374afSPyun YongHyeon 	uint16_t val;
137295d67482SBill Paul 	int i;
137395d67482SBill Paul 
13748cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
13751108273aSPyun YongHyeon 	misc_ctl = BGE_INIT;
13761108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
13771108273aSPyun YongHyeon 		misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
13781108273aSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
137995d67482SBill Paul 
138095d67482SBill Paul 	/* Clear the MAC control register */
138195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
138295d67482SBill Paul 
138395d67482SBill Paul 	/*
138495d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
138595d67482SBill Paul 	 * internal memory.
138695d67482SBill Paul 	 */
138795d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
13883f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
138995d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
139095d67482SBill Paul 
139195d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
13923f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
139395d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
139495d67482SBill Paul 
1395fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1396fbc374afSPyun YongHyeon 		/*
1397d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1398fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1399fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1400fbc374afSPyun YongHyeon 		 */
1401fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1402fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1403fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1404fbc374afSPyun YongHyeon 	}
1405fbc374afSPyun YongHyeon 
1406186f842bSJung-uk Kim 	/*
1407186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1408186f842bSJung-uk Kim 	 */
1409186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1410186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1411652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
1412186f842bSJung-uk Kim 		/* Read watermark not used, 128 bytes for write. */
1413186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1414652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
14154c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1416186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1417186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1418186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1419186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1420186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1421186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1422cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1423cbb2b2feSPyun YongHyeon 			/*
1424cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1425cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1426cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1427cbb2b2feSPyun YongHyeon 			 * register.
1428cbb2b2feSPyun YongHyeon 			 */
1429cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1430cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1431186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1432186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1433186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1434186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1435186f842bSJung-uk Kim 		} else {
1436186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1437186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1438186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
14390c8aa4eaSJung-uk Kim 			    0x0F;
1440186f842bSJung-uk Kim 		}
1441e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1442e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
14433f74909aSGleb Smirnoff 			uint32_t tmp;
14445cba12d3SPaul Saab 
1445186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
14460c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1447186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1448186f842bSJung-uk Kim 				dma_rw_ctl |=
1449186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
14505cba12d3SPaul Saab 
1451186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1452186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1453186f842bSJung-uk Kim 		}
1454186f842bSJung-uk Kim 	} else {
1455186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1456186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1457186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1458186f842bSJung-uk Kim 
1459186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1460186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1461186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1462186f842bSJung-uk Kim 	}
1463186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1464186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1465186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1466186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1467e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1468186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
14695cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
14701108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
14711108273aSPyun YongHyeon 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
14725cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
147395d67482SBill Paul 
147495d67482SBill Paul 	/*
147595d67482SBill Paul 	 * Set up general mode register.
147695d67482SBill Paul 	 */
1477e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
147895d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1479ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
148095d67482SBill Paul 
148195d67482SBill Paul 	/*
148290447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
148390447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
148490447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
148590447aadSMarius Strobl 	 * certain bridges.
148690447aadSMarius Strobl 	 */
148790447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
148890447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
148990447aadSMarius Strobl 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_FORCE_PCI32);
149090447aadSMarius Strobl 
149190447aadSMarius Strobl 	/*
14928cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
14938cb1383cSDoug Ambrisko 	 */
14948cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
14958cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
14968cb1383cSDoug Ambrisko 
14978cb1383cSDoug Ambrisko 	/*
1498ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1499c9ffd9f0SMarius Strobl 	 * properly by these devices.  Also ensure that INTx isn't disabled,
1500c9ffd9f0SMarius Strobl 	 * as these chips need it even when using MSI.
150195d67482SBill Paul 	 */
1502c9ffd9f0SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD,
1503c9ffd9f0SMarius Strobl 	    PCIM_CMD_INTxDIS | PCIM_CMD_MWIEN, 4);
150495d67482SBill Paul 
150595d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
15060c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
150795d67482SBill Paul 
150838cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
150938cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
151038cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
151138cc658fSJohn Baldwin 
151238cc658fSJohn Baldwin 		/* Put PHY into ready state */
151338cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
151438cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
151538cc658fSJohn Baldwin 		DELAY(40);
151638cc658fSJohn Baldwin 	}
151738cc658fSJohn Baldwin 
151895d67482SBill Paul 	return (0);
151995d67482SBill Paul }
152095d67482SBill Paul 
152195d67482SBill Paul static int
15223f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
152395d67482SBill Paul {
152495d67482SBill Paul 	struct bge_rcb *rcb;
1525e907febfSPyun YongHyeon 	bus_size_t vrcb;
1526e907febfSPyun YongHyeon 	bge_hostaddr taddr;
15276f8718a3SScott Long 	uint32_t val;
15288a315a6dSPyun YongHyeon 	int i, limit;
152995d67482SBill Paul 
153095d67482SBill Paul 	/*
153195d67482SBill Paul 	 * Initialize the memory window pointer register so that
153295d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
153395d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
153495d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
153595d67482SBill Paul 	 */
153695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
153795d67482SBill Paul 
1538822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1539822f63fcSBill Paul 
15407ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
154195d67482SBill Paul 		/* Configure mbuf memory pool */
15420dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1543822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1544822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1545822f63fcSBill Paul 		else
154695d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
154795d67482SBill Paul 
154895d67482SBill Paul 		/* Configure DMA resource pool */
15490434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
15500434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
155195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
15520434d1b8SBill Paul 	}
155395d67482SBill Paul 
155495d67482SBill Paul 	/* Configure mbuf pool watermarks */
15551108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717) {
15561108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
15571108273aSPyun YongHyeon 		if (sc->bge_ifp->if_mtu > ETHERMTU) {
15581108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
15591108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
15601108273aSPyun YongHyeon 		} else {
15611108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
15621108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
15631108273aSPyun YongHyeon 		}
15641108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc)) {
1565fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1566fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1567fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
156838cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
156938cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
157038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
157138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
157238cc658fSJohn Baldwin 	} else {
157338cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
157438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
157538cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
157638cc658fSJohn Baldwin 	}
157795d67482SBill Paul 
157895d67482SBill Paul 	/* Configure DMA resource watermarks */
157995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
158095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
158195d67482SBill Paul 
158295d67482SBill Paul 	/* Enable buffer manager */
15837ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
158495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
158595d67482SBill Paul 		    BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN);
158695d67482SBill Paul 
158795d67482SBill Paul 		/* Poll for buffer manager start indication */
158895d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
1589d5d23857SJung-uk Kim 			DELAY(10);
15900c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
159195d67482SBill Paul 				break;
159295d67482SBill Paul 		}
159395d67482SBill Paul 
159495d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1595fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1596fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
159795d67482SBill Paul 			return (ENXIO);
159895d67482SBill Paul 		}
15990434d1b8SBill Paul 	}
160095d67482SBill Paul 
160195d67482SBill Paul 	/* Enable flow-through queues */
16020c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
160395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
160495d67482SBill Paul 
160595d67482SBill Paul 	/* Wait until queue initialization is complete */
160695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1607d5d23857SJung-uk Kim 		DELAY(10);
160895d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
160995d67482SBill Paul 			break;
161095d67482SBill Paul 	}
161195d67482SBill Paul 
161295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1613fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
161495d67482SBill Paul 		return (ENXIO);
161595d67482SBill Paul 	}
161695d67482SBill Paul 
16178a315a6dSPyun YongHyeon 	/*
16188a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
16198a315a6dSPyun YongHyeon 	 *
16208a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
16218a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
16228a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
16238a315a6dSPyun YongHyeon 	 *
16248a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
16258a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
16268a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
16278a315a6dSPyun YongHyeon 	 *   to the controller.
16288a315a6dSPyun YongHyeon 	 *
16298a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
16308a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
16318a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
16328a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
16338a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
16348a315a6dSPyun YongHyeon 	 *   be disabled.
16358a315a6dSPyun YongHyeon 	 *
16368a315a6dSPyun YongHyeon 	 * Receive Return Ring
16378a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
16388a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
16398a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
16408a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
16418a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
16428a315a6dSPyun YongHyeon 	 *
16438a315a6dSPyun YongHyeon 	 * Send Ring
16448a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
16458a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
16468a315a6dSPyun YongHyeon 	 */
16478a315a6dSPyun YongHyeon 
16488a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
1649f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1650f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1651f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1652f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1653f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1654f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1655f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
16561108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
16571108273aSPyun YongHyeon 		/*
16581108273aSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
16591108273aSPyun YongHyeon 		 * Bits 15-2 : Maximum RX frame size
16601108273aSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
16611108273aSPyun YongHyeon 		 * Bit 0     : Reserved
16621108273aSPyun YongHyeon 		 */
16631108273aSPyun YongHyeon 		rcb->bge_maxlen_flags =
16641108273aSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
16651108273aSPyun YongHyeon 	} else if (BGE_IS_5705_PLUS(sc)) {
16668a315a6dSPyun YongHyeon 		/*
16678a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
16688a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
16698a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
16708a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
16718a315a6dSPyun YongHyeon 		 */
16720434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
16738a315a6dSPyun YongHyeon 	} else {
16748a315a6dSPyun YongHyeon 		/*
16758a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
16768a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
16778a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
16788a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
16798a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
16808a315a6dSPyun YongHyeon 		 */
16810434d1b8SBill Paul 		rcb->bge_maxlen_flags =
16820434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
16838a315a6dSPyun YongHyeon 	}
16841108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
16851108273aSPyun YongHyeon 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
16861108273aSPyun YongHyeon 	else
168795d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
16888a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
16890c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
16900c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
169167111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
169267111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
169395d67482SBill Paul 
16948a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
16958a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
16968a315a6dSPyun YongHyeon 
169795d67482SBill Paul 	/*
16988a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
16998a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
17008a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
170195d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
170295d67482SBill Paul 	 * high enough to require it).
170395d67482SBill Paul 	 */
17044c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1705f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
17068a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
1707f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1708f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1709f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1710f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1711f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1712f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1713f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
17141be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
17151be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
17161108273aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
17171108273aSPyun YongHyeon 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
17181108273aSPyun YongHyeon 		else
171995d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
172067111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
172167111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
172267111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
172367111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
17248a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
17250434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
17260434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
172767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
17288a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
17298a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
17308a315a6dSPyun YongHyeon 	}
173195d67482SBill Paul 
17328a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
17335e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
1734f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
173567111612SJohn Polstra 		rcb->bge_maxlen_flags =
173667111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
17370434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
17380434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
17398a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
17408a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
17410434d1b8SBill Paul 	}
174295d67482SBill Paul 
1743ca4f8986SPyun YongHyeon 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
1744ca4f8986SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
1745427d3f33SPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
1746427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
1747427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
17488d5f7181SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
17498d5f7181SPyun YongHyeon 			    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
1750ca4f8986SPyun YongHyeon 	}
175195d67482SBill Paul 	/*
17528a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
17538a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
17548a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
17558a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
17568a315a6dSPyun YongHyeon 	 *
175795d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
175895d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
175995d67482SBill Paul 	 * each ring.
17609ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
17619ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
17629ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
17639ba784dbSScott Long 	 * are reports that it might not need to be so strict.
176438cc658fSJohn Baldwin 	 *
176538cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
176638cc658fSJohn Baldwin 	 * well.
176795d67482SBill Paul 	 */
17685345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
17696f8718a3SScott Long 		val = 8;
17706f8718a3SScott Long 	else
17716f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
17726f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
17732a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
17742a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
17752a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
17761108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
17771108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
17781108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
17791108273aSPyun YongHyeon 	}
178095d67482SBill Paul 
178195d67482SBill Paul 	/*
17828a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
17838a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
17848a315a6dSPyun YongHyeon 	 * located in NIC memory.
178595d67482SBill Paul 	 */
17868a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
17878a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
17888a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
17898a315a6dSPyun YongHyeon 	else
17908a315a6dSPyun YongHyeon 		limit = 1;
1791e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
17928a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
1793e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1794e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1795e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1796e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
179795d67482SBill Paul 	}
179895d67482SBill Paul 
17998a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
1800e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1801e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1802e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1803e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
18041108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
18051108273aSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
18061108273aSPyun YongHyeon 	else
1807e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1808e907febfSPyun YongHyeon 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
1809e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1810e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
181195d67482SBill Paul 
18128a315a6dSPyun YongHyeon 	/*
18138a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
18148a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
18158a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
18168a315a6dSPyun YongHyeon 	 */
18171108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717) {
18181108273aSPyun YongHyeon 		/* Should be 17, use 16 until we get an SRAM map. */
18191108273aSPyun YongHyeon 		limit = 16;
18201108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc))
18218a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
18228a315a6dSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755)
18238a315a6dSPyun YongHyeon 		limit = 4;
18248a315a6dSPyun YongHyeon 	else
18258a315a6dSPyun YongHyeon 		limit = 1;
18268a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
1827e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
18288a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
1829e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1830e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1831e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
18328a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
1833e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
183438cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
18353f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1836e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
183795d67482SBill Paul 	}
183895d67482SBill Paul 
183995d67482SBill Paul 	/*
18408a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
18418a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
18428a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
184395d67482SBill Paul 	 */
1844e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1845e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1846e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1847e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
18488a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1849e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1850e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
185195d67482SBill Paul 
185295d67482SBill Paul 	/* Set random backoff seed for TX */
185395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
18544a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
18554a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
18564a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
185795d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
185895d67482SBill Paul 
185995d67482SBill Paul 	/* Set inter-packet gap */
186095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
186195d67482SBill Paul 
186295d67482SBill Paul 	/*
186395d67482SBill Paul 	 * Specify which ring to use for packets that don't match
186495d67482SBill Paul 	 * any RX rules.
186595d67482SBill Paul 	 */
186695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
186795d67482SBill Paul 
186895d67482SBill Paul 	/*
186995d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
187095d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
187195d67482SBill Paul 	 */
187295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
187395d67482SBill Paul 
187495d67482SBill Paul 	/* Inialize RX list placement stats mask. */
18750c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
187695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
187795d67482SBill Paul 
187895d67482SBill Paul 	/* Disable host coalescing until we get it set up */
187995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
188095d67482SBill Paul 
188195d67482SBill Paul 	/* Poll to make sure it's shut down. */
188295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1883d5d23857SJung-uk Kim 		DELAY(10);
188495d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
188595d67482SBill Paul 			break;
188695d67482SBill Paul 	}
188795d67482SBill Paul 
188895d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1889fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1890fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
189195d67482SBill Paul 		return (ENXIO);
189295d67482SBill Paul 	}
189395d67482SBill Paul 
189495d67482SBill Paul 	/* Set up host coalescing defaults */
189595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
189695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
189795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
189895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
18997ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
190095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
190195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
19020434d1b8SBill Paul 	}
1903b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
1904b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
190595d67482SBill Paul 
190695d67482SBill Paul 	/* Set up address of statistics block */
19077ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
1908f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1909f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
191095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1911f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
19120434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
191395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
19140434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
19150434d1b8SBill Paul 	}
19160434d1b8SBill Paul 
19170434d1b8SBill Paul 	/* Set up address of status block */
1918f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1919f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
192095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1921f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
192295d67482SBill Paul 
192330f57f61SPyun YongHyeon 	/* Set up status block size. */
192430f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
1925864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
192630f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
1927864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
1928864104feSPyun YongHyeon 	} else {
192930f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
1930864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
1931864104feSPyun YongHyeon 	}
1932864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
1933864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
1934864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
193530f57f61SPyun YongHyeon 
193695d67482SBill Paul 	/* Turn on host coalescing state machine */
193730f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
193895d67482SBill Paul 
193995d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
194095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
194195d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
194295d67482SBill Paul 
194395d67482SBill Paul 	/* Turn on RX list placement state machine */
194495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
194595d67482SBill Paul 
194695d67482SBill Paul 	/* Turn on RX list selector state machine. */
19477ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
194895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
194995d67482SBill Paul 
1950ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
1951ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
1952ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
1953ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
1954ea3b4127SPyun YongHyeon 
1955ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
1956ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
1957ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
1958ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
1959ea3b4127SPyun YongHyeon 	else
1960ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
1961ea3b4127SPyun YongHyeon 
196295d67482SBill Paul 	/* Turn on DMA, clear stats */
1963ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
196495d67482SBill Paul 
196595d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
196695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
196795d67482SBill Paul 
196895d67482SBill Paul #ifdef notdef
196995d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
197095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
197195d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
197295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
197395d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
197495d67482SBill Paul #endif
197595d67482SBill Paul 
197695d67482SBill Paul 	/* Turn on DMA completion state machine */
19777ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
197895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
197995d67482SBill Paul 
19806f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
19816f8718a3SScott Long 
19826f8718a3SScott Long 	/* Enable host coalescing bug fix. */
1983a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
19843889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
19856f8718a3SScott Long 
19867aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
19877aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
19887aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
19897aa4b937SPyun YongHyeon 
199095d67482SBill Paul 	/* Turn on write DMA state machine */
19916f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
19924f09c4c7SMarius Strobl 	DELAY(40);
199395d67482SBill Paul 
199495d67482SBill Paul 	/* Turn on read DMA state machine */
19954f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
19961108273aSPyun YongHyeon 
19971108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
19981108273aSPyun YongHyeon 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
19991108273aSPyun YongHyeon 
2000a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2001a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2002a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2003a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2004a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2005a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
20064f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
20074f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
20081108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2009ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
20101108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_TSO3 ||
20111108273aSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
201255a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
201355a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
201455a24a05SPyun YongHyeon 	}
2015d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2016d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2017d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
20181108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
20191108273aSPyun YongHyeon 	    BGE_IS_5717_PLUS(sc)) {
2020d255f2a9SPyun YongHyeon 		/*
2021d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
2022d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
2023d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
2024d255f2a9SPyun YongHyeon 		 */
2025d255f2a9SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_RSRVCTRL,
2026d255f2a9SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_RSRVCTRL) |
2027d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2028d255f2a9SPyun YongHyeon 	}
20294f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
20304f09c4c7SMarius Strobl 	DELAY(40);
203195d67482SBill Paul 
203295d67482SBill Paul 	/* Turn on RX data completion state machine */
203395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
203495d67482SBill Paul 
203595d67482SBill Paul 	/* Turn on RX BD initiator state machine */
203695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
203795d67482SBill Paul 
203895d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
203995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
204095d67482SBill Paul 
204195d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
20427ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
204395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
204495d67482SBill Paul 
204595d67482SBill Paul 	/* Turn on send BD completion state machine */
204695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
204795d67482SBill Paul 
204895d67482SBill Paul 	/* Turn on send data completion state machine */
2049a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2050a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2051a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2052a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
205395d67482SBill Paul 
205495d67482SBill Paul 	/* Turn on send data initiator state machine */
20551108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
20561108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
20571108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2058ca3f1187SPyun YongHyeon 	else
205995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
206095d67482SBill Paul 
206195d67482SBill Paul 	/* Turn on send BD initiator state machine */
206295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
206395d67482SBill Paul 
206495d67482SBill Paul 	/* Turn on send BD selector state machine */
206595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
206695d67482SBill Paul 
20670c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
206895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
206995d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
207095d67482SBill Paul 
207195d67482SBill Paul 	/* ack/clear link change events */
207295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
20730434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
20740434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2075f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
207695d67482SBill Paul 
20776ede2cfaSPyun YongHyeon 	/*
20786ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
20796ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
20806ede2cfaSPyun YongHyeon 	 */
2081652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
208295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2083a1d52896SBill Paul 	} else {
20847ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
20857ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
20867ed3f0f0SPyun YongHyeon 			DELAY(80);
20877ed3f0f0SPyun YongHyeon 		}
20881f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
20894c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2090a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2091a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2092a1d52896SBill Paul 	}
209395d67482SBill Paul 
20941f313773SOleg Bulyzhin 	/*
20951f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
20961f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
20971f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
20981f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
20991f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
21001f313773SOleg Bulyzhin 	 */
21011f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
21021f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
21031f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
21041f313773SOleg Bulyzhin 
210595d67482SBill Paul 	/* Enable link state change attentions. */
210695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
210795d67482SBill Paul 
210895d67482SBill Paul 	return (0);
210995d67482SBill Paul }
211095d67482SBill Paul 
21114c0da0ffSGleb Smirnoff const struct bge_revision *
21124c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
21134c0da0ffSGleb Smirnoff {
21144c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
21154c0da0ffSGleb Smirnoff 
21164c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
21174c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
21184c0da0ffSGleb Smirnoff 			return (br);
21194c0da0ffSGleb Smirnoff 	}
21204c0da0ffSGleb Smirnoff 
21214c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
21224c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
21234c0da0ffSGleb Smirnoff 			return (br);
21244c0da0ffSGleb Smirnoff 	}
21254c0da0ffSGleb Smirnoff 
21264c0da0ffSGleb Smirnoff 	return (NULL);
21274c0da0ffSGleb Smirnoff }
21284c0da0ffSGleb Smirnoff 
21294c0da0ffSGleb Smirnoff const struct bge_vendor *
21304c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
21314c0da0ffSGleb Smirnoff {
21324c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
21334c0da0ffSGleb Smirnoff 
21344c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
21354c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
21364c0da0ffSGleb Smirnoff 			return (v);
21374c0da0ffSGleb Smirnoff 
21384c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
21394c0da0ffSGleb Smirnoff 	return (NULL);
21404c0da0ffSGleb Smirnoff }
21414c0da0ffSGleb Smirnoff 
214295d67482SBill Paul /*
214395d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
21444c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
21454c0da0ffSGleb Smirnoff  *
21464c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
21477c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
21487c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
21497c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
21507c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
215195d67482SBill Paul  */
215295d67482SBill Paul static int
21533f74909aSGleb Smirnoff bge_probe(device_t dev)
215495d67482SBill Paul {
2155978f2704SMarius Strobl 	char buf[96];
2156978f2704SMarius Strobl 	char model[64];
2157978f2704SMarius Strobl 	const struct bge_revision *br;
2158978f2704SMarius Strobl 	const char *pname;
21594c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
2160978f2704SMarius Strobl 	const struct bge_type *t = bge_devs;
2161978f2704SMarius Strobl 	const struct bge_vendor *v;
2162978f2704SMarius Strobl 	uint32_t id;
2163978f2704SMarius Strobl 	uint16_t did, vid;
216495d67482SBill Paul 
216595d67482SBill Paul 	sc->bge_dev = dev;
21667c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
21677c929cf9SJung-uk Kim 	did = pci_get_device(dev);
21684c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
21697c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2170a5779553SStanislav Sedov 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2171a5779553SStanislav Sedov 			    BGE_PCIMISCCTL_ASICREV_SHIFT;
21721108273aSPyun YongHyeon 			if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
21731108273aSPyun YongHyeon 				/*
21741108273aSPyun YongHyeon 				 * Find the ASCI revision.  Different chips
21751108273aSPyun YongHyeon 				 * use different registers.
21761108273aSPyun YongHyeon 				 */
21771108273aSPyun YongHyeon 				switch (pci_get_device(dev)) {
21781108273aSPyun YongHyeon 				case BCOM_DEVICEID_BCM5717:
21791108273aSPyun YongHyeon 				case BCOM_DEVICEID_BCM5718:
21801108273aSPyun YongHyeon 					id = pci_read_config(dev,
21811108273aSPyun YongHyeon 					    BGE_PCI_GEN2_PRODID_ASICREV, 4);
21821108273aSPyun YongHyeon 					break;
21831108273aSPyun YongHyeon 				default:
2184a5779553SStanislav Sedov 					id = pci_read_config(dev,
2185a5779553SStanislav Sedov 					    BGE_PCI_PRODID_ASICREV, 4);
21861108273aSPyun YongHyeon 				}
21871108273aSPyun YongHyeon 			}
21884c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
21897c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
2190852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2191852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
21924e35d186SJung-uk Kim 				snprintf(model, 64, "%s", pname);
21934e35d186SJung-uk Kim 			else
2194978f2704SMarius Strobl 				snprintf(model, 64, "%s %s", v->v_name,
21957c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
21967c929cf9SJung-uk Kim 				    "NetXtreme Ethernet Controller");
2197a5779553SStanislav Sedov 			snprintf(buf, 96, "%s, %sASIC rev. %#08x", model,
2198a5779553SStanislav Sedov 			    br != NULL ? "" : "unknown ", id);
21994c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
220095d67482SBill Paul 			return (0);
220195d67482SBill Paul 		}
220295d67482SBill Paul 		t++;
220395d67482SBill Paul 	}
220495d67482SBill Paul 
220595d67482SBill Paul 	return (ENXIO);
220695d67482SBill Paul }
220795d67482SBill Paul 
2208f41ac2beSBill Paul static void
22093f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2210f41ac2beSBill Paul {
2211f41ac2beSBill Paul 	int i;
2212f41ac2beSBill Paul 
22133f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2214f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2215f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
22160ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2217f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2218f41ac2beSBill Paul 	}
2219943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2220943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2221943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2222f41ac2beSBill Paul 
22233f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2224f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2225f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2226f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2227f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2228f41ac2beSBill Paul 	}
2229943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2230943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2231943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2232f41ac2beSBill Paul 
22333f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2234f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2235f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
22360ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2237f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2238f41ac2beSBill Paul 	}
2239f41ac2beSBill Paul 
22400ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
22410ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
22420ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
22430ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2244f41ac2beSBill Paul 
2245f41ac2beSBill Paul 
22463f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2247e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
2248e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2249e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2250e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
2251f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2252f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2253f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2254f41ac2beSBill Paul 
2255f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2256f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2257f41ac2beSBill Paul 
22583f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2259e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
2260e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2261e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2262e65bed95SPyun YongHyeon 
2263e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
2264e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
2265f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2266f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2267f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2268f41ac2beSBill Paul 
2269f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2270f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2271f41ac2beSBill Paul 
22723f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2273e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
2274e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2275e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2276e65bed95SPyun YongHyeon 
2277e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
2278e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
2279f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2280f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2281f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2282f41ac2beSBill Paul 
2283f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2284f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2285f41ac2beSBill Paul 
22863f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2287e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
2288e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2289e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2290e65bed95SPyun YongHyeon 
2291e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2292f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2293f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2294f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2295f41ac2beSBill Paul 
2296f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2297f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2298f41ac2beSBill Paul 
22993f74909aSGleb Smirnoff 	/* Destroy status block. */
2300e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
2301e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2302e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2303e65bed95SPyun YongHyeon 
2304e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2305f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2306f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2307f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2308f41ac2beSBill Paul 
2309f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2310f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2311f41ac2beSBill Paul 
23123f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2313e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
2314e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2315e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2316e65bed95SPyun YongHyeon 
2317e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2318f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2319f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2320f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2321f41ac2beSBill Paul 
2322f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2323f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2324f41ac2beSBill Paul 
23255b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
23265b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
23275b610048SPyun YongHyeon 
23283f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2329f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2330f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2331f41ac2beSBill Paul }
2332f41ac2beSBill Paul 
2333f41ac2beSBill Paul static int
23345b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
23355b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
23365b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2337f41ac2beSBill Paul {
23383f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
2339f681b29aSPyun YongHyeon 	bus_addr_t lowaddr;
23405b610048SPyun YongHyeon 	bus_size_t ring_end;
23415b610048SPyun YongHyeon 	int error;
2342f41ac2beSBill Paul 
23435b610048SPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
23445b610048SPyun YongHyeon again:
23455b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
23465b610048SPyun YongHyeon 	    alignment, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
23475b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
23485b610048SPyun YongHyeon 	if (error != 0) {
23495b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
23505b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
23515b610048SPyun YongHyeon 		return (ENOMEM);
23525b610048SPyun YongHyeon 	}
23535b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
23545b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
23555b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
23565b610048SPyun YongHyeon 	if (error != 0) {
23575b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
23585b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
23595b610048SPyun YongHyeon 		return (ENOMEM);
23605b610048SPyun YongHyeon 	}
23615b610048SPyun YongHyeon 	/* Load the address of the ring. */
23625b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
23635b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
23645b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
23655b610048SPyun YongHyeon 	if (error != 0) {
23665b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
23675b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
23685b610048SPyun YongHyeon 		return (ENOMEM);
23695b610048SPyun YongHyeon 	}
23705b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
23715b610048SPyun YongHyeon 	ring_end = *paddr + maxsize;
23725b610048SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0 &&
23735b610048SPyun YongHyeon 	    BGE_ADDR_HI(*paddr) != BGE_ADDR_HI(ring_end)) {
23745b610048SPyun YongHyeon 		/*
23755b610048SPyun YongHyeon 		 * 4GB boundary crossed.  Limit maximum allowable DMA
23765b610048SPyun YongHyeon 		 * address space to 32bit and try again.
23775b610048SPyun YongHyeon 		 */
23785b610048SPyun YongHyeon 		bus_dmamap_unload(*tag, *map);
23795b610048SPyun YongHyeon 		bus_dmamem_free(*tag, *ring, *map);
23805b610048SPyun YongHyeon 		bus_dma_tag_destroy(*tag);
23815b610048SPyun YongHyeon 		if (bootverbose)
23825b610048SPyun YongHyeon 			device_printf(sc->bge_dev, "4GB boundary crossed, "
23835b610048SPyun YongHyeon 			    "limit DMA address space to 32bit for %s\n", msg);
23845b610048SPyun YongHyeon 		*ring = NULL;
23855b610048SPyun YongHyeon 		*tag = NULL;
23865b610048SPyun YongHyeon 		*map = NULL;
23875b610048SPyun YongHyeon 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
23885b610048SPyun YongHyeon 		goto again;
23895b610048SPyun YongHyeon 	}
23905b610048SPyun YongHyeon 	return (0);
23915b610048SPyun YongHyeon }
23925b610048SPyun YongHyeon 
23935b610048SPyun YongHyeon static int
23945b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
23955b610048SPyun YongHyeon {
23965b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2397*f5459d4cSPyun YongHyeon 	bus_size_t boundary, sbsz, rxmaxsegsz, txsegsz, txmaxsegsz;
23985b610048SPyun YongHyeon 	int i, error;
2399f41ac2beSBill Paul 
2400f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2401f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2402f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2403f41ac2beSBill Paul 	/*
2404f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2405f41ac2beSBill Paul 	 */
24064eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2407f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
24084eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
24094eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2410e65bed95SPyun YongHyeon 	if (error != 0) {
2411fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2412fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2413e65bed95SPyun YongHyeon 		return (ENOMEM);
2414e65bed95SPyun YongHyeon 	}
2415e65bed95SPyun YongHyeon 
24165b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
24175b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
24185b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
24195b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
24205b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
24215b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
24225b610048SPyun YongHyeon 	if (error)
24235b610048SPyun YongHyeon 		return (error);
24245b610048SPyun YongHyeon 
24255b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
24265b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
24275b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
24285b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
24295b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
24305b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
24315b610048SPyun YongHyeon 	if (error)
24325b610048SPyun YongHyeon 		return (error);
24335b610048SPyun YongHyeon 
24345b610048SPyun YongHyeon 	/* Create tag for TX ring. */
24355b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
24365b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
24375b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
24385b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
24395b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
24405b610048SPyun YongHyeon 	if (error)
24415b610048SPyun YongHyeon 		return (error);
24425b610048SPyun YongHyeon 
2443f41ac2beSBill Paul 	/*
24445b610048SPyun YongHyeon 	 * Create tag for status block.
24455b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
24465b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
24475b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
24485b610048SPyun YongHyeon 	 * of configured number of ring.
2449f41ac2beSBill Paul 	 */
24505b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
24515b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
24525b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
24535b610048SPyun YongHyeon 	else
24545b610048SPyun YongHyeon 		sbsz = 32;
24555b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
24565b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
24575b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
24585b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
24595b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
24605b610048SPyun YongHyeon 	if (error)
24615b610048SPyun YongHyeon 		return (error);
24625b610048SPyun YongHyeon 
246312c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
246412c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
246512c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
246612c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
246712c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
246812c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
246912c65daeSPyun YongHyeon 	if (error)
247012c65daeSPyun YongHyeon 		return (error);
247112c65daeSPyun YongHyeon 
24725b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
24735b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
24745b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
24755b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
24765b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
24775b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
24785b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
24795b610048SPyun YongHyeon 		if (error)
24805b610048SPyun YongHyeon 			return (error);
24815b610048SPyun YongHyeon 	}
24825b610048SPyun YongHyeon 
24835b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
24845b610048SPyun YongHyeon 	boundary = 0;
2485d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
248638cc6151SPyun YongHyeon 		boundary = BGE_DMA_BNDRY;
2487d2ffe15aSPyun YongHyeon 		/*
2488d2ffe15aSPyun YongHyeon 		 * XXX
2489d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
2490d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
2491d2ffe15aSPyun YongHyeon 		 * Limiting DMA address space to 32bits seems to address
2492d2ffe15aSPyun YongHyeon 		 * it.
2493d2ffe15aSPyun YongHyeon 		 */
2494d2ffe15aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_PCIX)
2495d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
2496d2ffe15aSPyun YongHyeon 	}
24975b610048SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
249892c0b021SPyun YongHyeon 	    1, boundary, lowaddr, BUS_SPACE_MAXADDR, NULL,
24995b610048SPyun YongHyeon 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
25005b610048SPyun YongHyeon 	    0, NULL, NULL, &sc->bge_cdata.bge_buffer_tag);
25015b610048SPyun YongHyeon 	if (error != 0) {
25025b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
25035b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
25045b610048SPyun YongHyeon 		return (ENOMEM);
25055b610048SPyun YongHyeon 	}
25065b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
25071108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2508ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
2509ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
2510ca3f1187SPyun YongHyeon 	} else {
2511ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
2512ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
2513ca3f1187SPyun YongHyeon 	}
25145b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
2515ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
2516ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
2517ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
2518f41ac2beSBill Paul 
2519f41ac2beSBill Paul 	if (error) {
25200ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
25210ac56796SPyun YongHyeon 		return (ENOMEM);
25220ac56796SPyun YongHyeon 	}
25230ac56796SPyun YongHyeon 
25245b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
2525*f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
2526*f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
2527*f5459d4cSPyun YongHyeon 	else
2528*f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
25295b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
2530*f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
2531*f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
25320ac56796SPyun YongHyeon 
25330ac56796SPyun YongHyeon 	if (error) {
25340ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
2535f41ac2beSBill Paul 		return (ENOMEM);
2536f41ac2beSBill Paul 	}
2537f41ac2beSBill Paul 
25383f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
2539943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2540943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
2541943787f3SPyun YongHyeon 	if (error) {
2542943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
2543943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
2544943787f3SPyun YongHyeon 		return (ENOMEM);
2545943787f3SPyun YongHyeon 	}
2546f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
25470ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2548f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
2549f41ac2beSBill Paul 		if (error) {
2550fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
2551fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
2552f41ac2beSBill Paul 			return (ENOMEM);
2553f41ac2beSBill Paul 		}
2554f41ac2beSBill Paul 	}
2555f41ac2beSBill Paul 
25563f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
2557f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
25580ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
2559f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
2560f41ac2beSBill Paul 		if (error) {
2561fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
25620ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
2563f41ac2beSBill Paul 			return (ENOMEM);
2564f41ac2beSBill Paul 		}
2565f41ac2beSBill Paul 	}
2566f41ac2beSBill Paul 
25675b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
25684c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
25695b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
25708a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
25711be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
25721be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
2573f41ac2beSBill Paul 		if (error) {
2574fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
25753f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
2576f41ac2beSBill Paul 			return (ENOMEM);
2577f41ac2beSBill Paul 		}
25783f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
2579943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2580943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
2581943787f3SPyun YongHyeon 		if (error) {
2582943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
25831b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
2584943787f3SPyun YongHyeon 			return (ENOMEM);
2585943787f3SPyun YongHyeon 		}
2586f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2587f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2588f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2589f41ac2beSBill Paul 			if (error) {
2590fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
25913f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
2592f41ac2beSBill Paul 				return (ENOMEM);
2593f41ac2beSBill Paul 			}
2594f41ac2beSBill Paul 		}
2595f41ac2beSBill Paul 	}
2596f41ac2beSBill Paul 
2597f41ac2beSBill Paul 	return (0);
2598f41ac2beSBill Paul }
2599f41ac2beSBill Paul 
2600bf6ef57aSJohn Polstra /*
2601bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
2602bf6ef57aSJohn Polstra  */
2603bf6ef57aSJohn Polstra static int
2604bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
2605bf6ef57aSJohn Polstra {
2606bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
260755aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
2608bf6ef57aSJohn Polstra 
260955aaf894SMarius Strobl 	d = pci_get_domain(dev);
2610bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
2611bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
2612bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
2613bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
261455aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
2615bf6ef57aSJohn Polstra 			return (1);
2616bf6ef57aSJohn Polstra 	return (0);
2617bf6ef57aSJohn Polstra }
2618bf6ef57aSJohn Polstra 
2619bf6ef57aSJohn Polstra /*
2620bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
2621bf6ef57aSJohn Polstra  */
2622bf6ef57aSJohn Polstra static int
2623bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
2624bf6ef57aSJohn Polstra {
2625bf6ef57aSJohn Polstra 	int can_use_msi = 0;
2626bf6ef57aSJohn Polstra 
26271108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
26281108273aSPyun YongHyeon #ifdef DEVICE_POLLING
26291108273aSPyun YongHyeon 	return (0);
26301108273aSPyun YongHyeon #endif
2631bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
2632a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
2633bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
2634bf6ef57aSJohn Polstra 		/*
2635a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
2636a8376f70SMarius Strobl 		 * configured in single-port mode.
2637bf6ef57aSJohn Polstra 		 */
2638bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
2639bf6ef57aSJohn Polstra 			can_use_msi = 1;
2640bf6ef57aSJohn Polstra 		break;
2641bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
2642bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
2643bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
2644bf6ef57aSJohn Polstra 			can_use_msi = 1;
2645bf6ef57aSJohn Polstra 		break;
2646a8376f70SMarius Strobl 	default:
2647a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
2648bf6ef57aSJohn Polstra 			can_use_msi = 1;
2649bf6ef57aSJohn Polstra 	}
2650bf6ef57aSJohn Polstra 	return (can_use_msi);
2651bf6ef57aSJohn Polstra }
2652bf6ef57aSJohn Polstra 
265395d67482SBill Paul static int
26543f74909aSGleb Smirnoff bge_attach(device_t dev)
265595d67482SBill Paul {
265695d67482SBill Paul 	struct ifnet *ifp;
265795d67482SBill Paul 	struct bge_softc *sc;
26584f0794ffSBjoern A. Zeeb 	uint32_t hwcfg = 0, misccfg;
265908013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
2660fb772a6cSMarius Strobl 	int capmask, error, f, msicount, phy_addr, reg, rid, trys;
266195d67482SBill Paul 
266295d67482SBill Paul 	sc = device_get_softc(dev);
266395d67482SBill Paul 	sc->bge_dev = dev;
266495d67482SBill Paul 
2665dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
2666dfe0df9aSPyun YongHyeon 
266795d67482SBill Paul 	/*
266895d67482SBill Paul 	 * Map control/status registers.
266995d67482SBill Paul 	 */
267095d67482SBill Paul 	pci_enable_busmaster(dev);
267195d67482SBill Paul 
2672736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
26735f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
267444f8f2fcSMarius Strobl 	    RF_ACTIVE);
267595d67482SBill Paul 
267695d67482SBill Paul 	if (sc->bge_res == NULL) {
2677fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
267895d67482SBill Paul 		error = ENXIO;
267995d67482SBill Paul 		goto fail;
268095d67482SBill Paul 	}
268195d67482SBill Paul 
26824f09c4c7SMarius Strobl 	/* Save various chip information. */
2683e53d81eeSPaul Saab 	sc->bge_chipid =
2684a5779553SStanislav Sedov 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2685a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
26861108273aSPyun YongHyeon 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_USE_PRODID_REG) {
26871108273aSPyun YongHyeon 		/*
26881108273aSPyun YongHyeon 		 * Find the ASCI revision.  Different chips use different
26891108273aSPyun YongHyeon 		 * registers.
26901108273aSPyun YongHyeon 		 */
26911108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
26921108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5717:
26931108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
26941108273aSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
26951108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
26961108273aSPyun YongHyeon 			break;
26971108273aSPyun YongHyeon 		default:
26981108273aSPyun YongHyeon 			sc->bge_chipid = pci_read_config(dev,
26991108273aSPyun YongHyeon 			    BGE_PCI_PRODID_ASICREV, 4);
27001108273aSPyun YongHyeon 		}
27011108273aSPyun YongHyeon 	}
2702e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2703e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2704e53d81eeSPaul Saab 
2705a813ed78SPyun YongHyeon 	/* Set default PHY address. */
27068e5d93dbSMarius Strobl 	phy_addr = 1;
27071108273aSPyun YongHyeon 	 /*
27081108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
27091108273aSPyun YongHyeon 	  *
27101108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
27111108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
27121108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
27131108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
27141108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
27151108273aSPyun YongHyeon 	  *
27161108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
27171108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
27181108273aSPyun YongHyeon 	  */
27191108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717) {
27201108273aSPyun YongHyeon 		f = pci_get_function(dev);
27211108273aSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5717_A0) {
27221108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
27231108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
27241108273aSPyun YongHyeon 				phy_addr = f + 8;
27251108273aSPyun YongHyeon 			else
27261108273aSPyun YongHyeon 				phy_addr = f + 1;
27271108273aSPyun YongHyeon 		} else if (sc->bge_chipid == BGE_CHIPID_BCM5717_B0) {
27281108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
27291108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
27301108273aSPyun YongHyeon 				phy_addr = f + 8;
27311108273aSPyun YongHyeon 			else
27321108273aSPyun YongHyeon 				phy_addr = f + 1;
27331108273aSPyun YongHyeon 		}
27341108273aSPyun YongHyeon 	}
2735a813ed78SPyun YongHyeon 
273686543395SJung-uk Kim 	/*
273738cc658fSJohn Baldwin 	 * Don't enable Ethernet@WireSpeed for the 5700, 5906, or the
273886543395SJung-uk Kim 	 * 5705 A0 and A1 chips.
273986543395SJung-uk Kim 	 */
274086543395SJung-uk Kim 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
274138cc658fSJohn Baldwin 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
274286543395SJung-uk Kim 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
27431108273aSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1 &&
27441108273aSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc))
2745757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_WIRESPEED;
274686543395SJung-uk Kim 
27475fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
27485fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
274908013fd3SMarius Strobl 
27500dae9719SJung-uk Kim 	/* Save chipset family. */
27510dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
27521108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
27531108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
27541108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
27551108273aSPyun YongHyeon 		    BGE_FLAG_SHORT_DMA_BUG | BGE_FLAG_JUMBO_FRAME;
27561108273aSPyun YongHyeon 		break;
2757a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
2758a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
2759a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
2760a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
2761a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
2762a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
2763a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
2764a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
2765a5779553SStanislav Sedov 		break;
27660dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
27670dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
27680dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
27690dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
27707ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
27710dae9719SJung-uk Kim 		break;
27720dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
27730dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
27740dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
2775*f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
27769fe569d8SXin LI 		/* FALLTHROUGH */
27770dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
27780dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
277938cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
27800dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
2781d598b626SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
2782d598b626SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
27839fe569d8SXin LI 		/* FALLTHROUGH */
27840dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
27850dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
27860dae9719SJung-uk Kim 		break;
27870dae9719SJung-uk Kim 	}
27880dae9719SJung-uk Kim 
2789757402fbSPyun YongHyeon 	/* Set various PHY bug flags. */
27901ec4c3a8SJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
27911ec4c3a8SJung-uk Kim 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
2792757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
27935ee49a3aSJung-uk Kim 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
27945ee49a3aSJung-uk Kim 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
2795757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
27965ee49a3aSJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
2797757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
27984150ce6fSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
2799757402fbSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
2800eea8956aSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
2801eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
28021108273aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
2803eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
2804eea8956aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780) {
28055ee49a3aSJung-uk Kim 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
2806a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2807a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
28084fcf220bSJohn Baldwin 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
2809f7d1b2ebSXin LI 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
2810f7d1b2ebSXin LI 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
2811757402fbSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
2812eea8956aSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
2813eea8956aSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
2814eea8956aSPyun YongHyeon 		} else
2815757402fbSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
28165ee49a3aSJung-uk Kim 	}
28175ee49a3aSJung-uk Kim 
2818a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
28191108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
28201108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2821a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2822a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2823a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2824a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
2825a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
2826a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
2827a813ed78SPyun YongHyeon 	else
2828a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
28297ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
28307ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
28317ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
2832a813ed78SPyun YongHyeon 
2833f681b29aSPyun YongHyeon 	/*
2834f681b29aSPyun YongHyeon 	 * All controllers that are not 5755 or higher have 4GB
2835f681b29aSPyun YongHyeon 	 * boundary DMA bug.
2836f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
2837f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
2838f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
2839f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
2840f681b29aSPyun YongHyeon 	 */
2841f681b29aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) == 0)
2842f681b29aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
28434f0794ffSBjoern A. Zeeb 
28444f0794ffSBjoern A. Zeeb 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID;
2845fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
28464f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
28474f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
28484f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
284984ac96f8SPyun YongHyeon 	}
28504f0794ffSBjoern A. Zeeb 
2851fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
2852fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
2853fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
2854fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
2855fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
2856fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
2857fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
2858fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
2859fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
2860fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
2861fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
2862fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
2863fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
2864fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2865fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
2866fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
2867fb772a6cSMarius Strobl 	}
2868fb772a6cSMarius Strobl 
2869e53d81eeSPaul Saab 	/*
2870ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
2871ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
2872ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
2873ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
2874ca3f1187SPyun YongHyeon 	 * known bug which can't handle TSO if ethernet header + IP/TCP
2875ca3f1187SPyun YongHyeon 	 * header is greater than 80 bytes. The workaround for the TSO
2876ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
2877ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
2878ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
2879ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
2880ca3f1187SPyun YongHyeon 	 */
28811108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
28821108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
28831108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
28841108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
28854f4a16e1SPyun YongHyeon 		/*
28864f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
28874f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
2888be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
28894f4a16e1SPyun YongHyeon 		 */
28904f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
2891be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
2892be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
2893ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
28944f4a16e1SPyun YongHyeon 	}
2895ca3f1187SPyun YongHyeon 
2896ca3f1187SPyun YongHyeon   	/*
28976f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
2898e53d81eeSPaul Saab   	 */
28993b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
29004c0da0ffSGleb Smirnoff 		/*
29016f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
29026f8718a3SScott Long 		 * must be a PCI Express device.
29036f8718a3SScott Long 		 */
29046f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
29050aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
2906d2b6e9a0SPyun YongHyeon 		if (pci_get_max_read_req(dev) != 4096)
2907d2b6e9a0SPyun YongHyeon 			pci_set_max_read_req(dev, 4096);
29086f8718a3SScott Long 	} else {
29096f8718a3SScott Long 		/*
29106f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
29116f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
29124c0da0ffSGleb Smirnoff 		 */
29133b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
29140aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
291590447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
29164c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
2917652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
29186f8718a3SScott Long 	}
29194c0da0ffSGleb Smirnoff 
2920bf6ef57aSJohn Polstra 	/*
2921fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
2922fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
2923fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
2924fd4d32feSPyun YongHyeon 	 */
2925fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
2926fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
2927fd4d32feSPyun YongHyeon 	/*
2928bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
2929bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
2930bf6ef57aSJohn Polstra 	 * normal operation.
2931bf6ef57aSJohn Polstra 	 */
29320aaf1057SPyun YongHyeon 	rid = 0;
29333b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
29340aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
2935bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
2936bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
2937bf6ef57aSJohn Polstra 			if (msicount > 1)
2938bf6ef57aSJohn Polstra 				msicount = 1;
2939bf6ef57aSJohn Polstra 		} else
2940bf6ef57aSJohn Polstra 			msicount = 0;
2941bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
2942bf6ef57aSJohn Polstra 			rid = 1;
2943bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
29440aaf1057SPyun YongHyeon 		}
29450aaf1057SPyun YongHyeon 	}
2946bf6ef57aSJohn Polstra 
29471108273aSPyun YongHyeon 	/*
29481108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
29491108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
29501108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
29511108273aSPyun YongHyeon 	 */
29521108273aSPyun YongHyeon #ifndef DEVICE_POLLING
29531108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
29541108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
29551108273aSPyun YongHyeon #endif
29561108273aSPyun YongHyeon 
2957bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2958bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
2959bf6ef57aSJohn Polstra 
2960bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
2961bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
2962bf6ef57aSJohn Polstra 		error = ENXIO;
2963bf6ef57aSJohn Polstra 		goto fail;
2964bf6ef57aSJohn Polstra 	}
2965bf6ef57aSJohn Polstra 
29664f09c4c7SMarius Strobl 	device_printf(dev,
29674f09c4c7SMarius Strobl 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; %s\n",
29684f09c4c7SMarius Strobl 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev,
29694f09c4c7SMarius Strobl 	    (sc->bge_flags & BGE_FLAG_PCIX) ? "PCI-X" :
29704f09c4c7SMarius Strobl 	    ((sc->bge_flags & BGE_FLAG_PCIE) ? "PCI-E" : "PCI"));
29714f09c4c7SMarius Strobl 
2972bf6ef57aSJohn Polstra 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
2973bf6ef57aSJohn Polstra 
297495d67482SBill Paul 	/* Try to reset the chip. */
29758cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
29768cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
29778cb1383cSDoug Ambrisko 		error = ENXIO;
29788cb1383cSDoug Ambrisko 		goto fail;
29798cb1383cSDoug Ambrisko 	}
29808cb1383cSDoug Ambrisko 
29818cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
2982f1a7e6d5SScott Long 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG)
2983f1a7e6d5SScott Long 	    == BGE_MAGIC_NUMBER)) {
29848cb1383cSDoug Ambrisko 		if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG)
29858cb1383cSDoug Ambrisko 		    & BGE_HWCFG_ASF) {
29868cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_ENABLE;
29878cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_STACKUP;
2988d67eba2fSPyun YongHyeon 			if (BGE_IS_575X_PLUS(sc))
29898cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
29908cb1383cSDoug Ambrisko 		}
29918cb1383cSDoug Ambrisko 	}
29928cb1383cSDoug Ambrisko 
29938cb1383cSDoug Ambrisko 	/* Try to reset the chip again the nice way. */
29948cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
29958cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
29968cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
29978cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
29988cb1383cSDoug Ambrisko 		error = ENXIO;
29998cb1383cSDoug Ambrisko 		goto fail;
30008cb1383cSDoug Ambrisko 	}
30018cb1383cSDoug Ambrisko 
30028cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
30038cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
300495d67482SBill Paul 
300595d67482SBill Paul 	if (bge_chipinit(sc)) {
3006fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
300795d67482SBill Paul 		error = ENXIO;
300895d67482SBill Paul 		goto fail;
300995d67482SBill Paul 	}
301095d67482SBill Paul 
301138cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
301238cc658fSJohn Baldwin 	if (error) {
301308013fd3SMarius Strobl 		device_printf(sc->bge_dev,
301408013fd3SMarius Strobl 		    "failed to read station address\n");
301595d67482SBill Paul 		error = ENXIO;
301695d67482SBill Paul 		goto fail;
301795d67482SBill Paul 	}
301895d67482SBill Paul 
3019f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
30201108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
30211108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
30221108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3023f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3024f41ac2beSBill Paul 	else
3025f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3026f41ac2beSBill Paul 
30275b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3028fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3029fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3030f41ac2beSBill Paul 		error = ENXIO;
3031f41ac2beSBill Paul 		goto fail;
3032f41ac2beSBill Paul 	}
3033f41ac2beSBill Paul 
303435f945cdSPyun YongHyeon 	bge_add_sysctls(sc);
303535f945cdSPyun YongHyeon 
303695d67482SBill Paul 	/* Set default tuneable values. */
303795d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
303895d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
303995d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
30406f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
30416f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
304295d67482SBill Paul 
304335f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
304435f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
304535f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
304635f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
304735f945cdSPyun YongHyeon 
304895d67482SBill Paul 	/* Set up ifnet structure */
3049fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3050fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3051fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3052fc74a9f9SBrooks Davis 		error = ENXIO;
3053fc74a9f9SBrooks Davis 		goto fail;
3054fc74a9f9SBrooks Davis 	}
305595d67482SBill Paul 	ifp->if_softc = sc;
30569bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
305795d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
305895d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
305995d67482SBill Paul 	ifp->if_start = bge_start;
306095d67482SBill Paul 	ifp->if_init = bge_init;
30614d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
30624d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
30634d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
306435f945cdSPyun YongHyeon 	ifp->if_hwassist = sc->bge_csum_features;
3065d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
30664e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
30671108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3068ca3f1187SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
306904bde852SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_VLAN_HWTSO;
3070ca3f1187SPyun YongHyeon 	}
30714e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
30724e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
30734e35d186SJung-uk Kim #endif
307495d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
307575719184SGleb Smirnoff #ifdef DEVICE_POLLING
307675719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
307775719184SGleb Smirnoff #endif
307895d67482SBill Paul 
3079a1d52896SBill Paul 	/*
3080d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3081d375e524SGleb Smirnoff 	 * to hardware bugs.
3082d375e524SGleb Smirnoff 	 */
3083d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3084d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
30854d3a629cSPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_HWCSUM;
3086d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
3087d375e524SGleb Smirnoff 	}
3088d375e524SGleb Smirnoff 
3089d375e524SGleb Smirnoff 	/*
3090a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
309141abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
309241abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
309341abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
309441abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
309541abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
309641abcc1bSPaul Saab 	 * SK-9D41.
3097a1d52896SBill Paul 	 */
309841abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
309941abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
31005fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
31015fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3102f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3103f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3104fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3105f6789fbaSPyun YongHyeon 			error = ENXIO;
3106f6789fbaSPyun YongHyeon 			goto fail;
3107f6789fbaSPyun YongHyeon 		}
310841abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
310941abcc1bSPaul Saab 	}
311041abcc1bSPaul Saab 
311195d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3112ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3113ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
3114ea3b4127SPyun YongHyeon 		if (BGE_IS_5714_FAMILY(sc))
3115ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
3116ea3b4127SPyun YongHyeon 		else
3117652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3118ea3b4127SPyun YongHyeon 	}
311995d67482SBill Paul 
3120652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
31210c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
31220c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
31230c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
31246098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
31256098821cSJung-uk Kim 		    0, NULL);
312695d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
312795d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3128da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
312995d67482SBill Paul 	} else {
313095d67482SBill Paul 		/*
31318cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
31328cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
31338cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
31348cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
31358cb1383cSDoug Ambrisko 		 * the PHY.
313695d67482SBill Paul 		 */
31374012d104SMarius Strobl 		trys = 0;
31388cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
31398cb1383cSDoug Ambrisko again:
31408cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
31418cb1383cSDoug Ambrisko 
3142fb772a6cSMarius Strobl 		error = mii_attach(dev, &sc->bge_miibus, ifp, bge_ifmedia_upd,
3143fb772a6cSMarius Strobl 		    bge_ifmedia_sts, capmask, phy_addr, MII_OFFSET_ANY,
3144fb772a6cSMarius Strobl 		    MIIF_DOPAUSE);
31458e5d93dbSMarius Strobl 		if (error != 0) {
31468cb1383cSDoug Ambrisko 			if (trys++ < 4) {
31478cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
31484e35d186SJung-uk Kim 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR,
31494e35d186SJung-uk Kim 				    BMCR_RESET);
31508cb1383cSDoug Ambrisko 				goto again;
31518cb1383cSDoug Ambrisko 			}
31528e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
315395d67482SBill Paul 			goto fail;
315495d67482SBill Paul 		}
31558cb1383cSDoug Ambrisko 
31568cb1383cSDoug Ambrisko 		/*
31578cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
31588cb1383cSDoug Ambrisko 		 */
31598cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
31608cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
316195d67482SBill Paul 	}
316295d67482SBill Paul 
316395d67482SBill Paul 	/*
3164e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3165e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3166e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3167e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3168e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3169e255b776SJohn Polstra 	 * payloads by copying the received packets.
3170e255b776SJohn Polstra 	 */
3171652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3172652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3173652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3174e255b776SJohn Polstra 
3175e255b776SJohn Polstra 	/*
317695d67482SBill Paul 	 * Call MI attach routine.
317795d67482SBill Paul 	 */
3178fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
3179b74e67fbSGleb Smirnoff 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
31800f9bd73bSSam Leffler 
318161ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
318261ccb9daSPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
318361ccb9daSPyun YongHyeon 
31840f9bd73bSSam Leffler 	/*
31850f9bd73bSSam Leffler 	 * Hookup IRQ last.
31860f9bd73bSSam Leffler 	 */
3187dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3188dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
31897e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
31907e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3191dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3192dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3193dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3194dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3195dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3196dfe0df9aSPyun YongHyeon 			error = ENXIO;
3197dfe0df9aSPyun YongHyeon 			goto fail;
3198dfe0df9aSPyun YongHyeon 		}
3199dfe0df9aSPyun YongHyeon 		taskqueue_start_threads(&sc->bge_tq, 1, PI_NET, "%s taskq",
3200dfe0df9aSPyun YongHyeon 		    device_get_nameunit(sc->bge_dev));
3201dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3202dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3203dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3204dfe0df9aSPyun YongHyeon 		if (error)
3205dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3206dfe0df9aSPyun YongHyeon 	} else
3207dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3208dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3209dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
32100f9bd73bSSam Leffler 
32110f9bd73bSSam Leffler 	if (error) {
3212fc74a9f9SBrooks Davis 		bge_detach(dev);
3213fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
32140f9bd73bSSam Leffler 	}
321595d67482SBill Paul 
321608013fd3SMarius Strobl 	return (0);
321708013fd3SMarius Strobl 
321895d67482SBill Paul fail:
321908013fd3SMarius Strobl 	bge_release_resources(sc);
322008013fd3SMarius Strobl 
322195d67482SBill Paul 	return (error);
322295d67482SBill Paul }
322395d67482SBill Paul 
322495d67482SBill Paul static int
32253f74909aSGleb Smirnoff bge_detach(device_t dev)
322695d67482SBill Paul {
322795d67482SBill Paul 	struct bge_softc *sc;
322895d67482SBill Paul 	struct ifnet *ifp;
322995d67482SBill Paul 
323095d67482SBill Paul 	sc = device_get_softc(dev);
3231fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
323295d67482SBill Paul 
323375719184SGleb Smirnoff #ifdef DEVICE_POLLING
323475719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
323575719184SGleb Smirnoff 		ether_poll_deregister(ifp);
323675719184SGleb Smirnoff #endif
323775719184SGleb Smirnoff 
32380f9bd73bSSam Leffler 	BGE_LOCK(sc);
323995d67482SBill Paul 	bge_stop(sc);
324095d67482SBill Paul 	bge_reset(sc);
32410f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
32420f9bd73bSSam Leffler 
32435dda8085SOleg Bulyzhin 	callout_drain(&sc->bge_stat_ch);
32445dda8085SOleg Bulyzhin 
3245dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3246dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
32470f9bd73bSSam Leffler 	ether_ifdetach(ifp);
324895d67482SBill Paul 
3249652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
325095d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
325195d67482SBill Paul 	} else {
325295d67482SBill Paul 		bus_generic_detach(dev);
325395d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
325495d67482SBill Paul 	}
325595d67482SBill Paul 
325695d67482SBill Paul 	bge_release_resources(sc);
325795d67482SBill Paul 
325895d67482SBill Paul 	return (0);
325995d67482SBill Paul }
326095d67482SBill Paul 
326195d67482SBill Paul static void
32623f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
326395d67482SBill Paul {
326495d67482SBill Paul 	device_t dev;
326595d67482SBill Paul 
326695d67482SBill Paul 	dev = sc->bge_dev;
326795d67482SBill Paul 
3268dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3269dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3270dfe0df9aSPyun YongHyeon 
327195d67482SBill Paul 	if (sc->bge_intrhand != NULL)
327295d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
327395d67482SBill Paul 
327495d67482SBill Paul 	if (sc->bge_irq != NULL)
3275724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3276724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
3277724bd939SJohn Polstra 
3278724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
3279724bd939SJohn Polstra 		pci_release_msi(dev);
328095d67482SBill Paul 
328195d67482SBill Paul 	if (sc->bge_res != NULL)
328295d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
3283736b9319SPyun YongHyeon 		    PCIR_BAR(0), sc->bge_res);
328495d67482SBill Paul 
3285ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
3286ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
3287ad61f896SRuslan Ermilov 
3288f41ac2beSBill Paul 	bge_dma_free(sc);
328995d67482SBill Paul 
32900f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
32910f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
329295d67482SBill Paul }
329395d67482SBill Paul 
32948cb1383cSDoug Ambrisko static int
32953f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
329695d67482SBill Paul {
329795d67482SBill Paul 	device_t dev;
32985fea260fSMarius Strobl 	uint32_t cachesize, command, pcistate, reset, val;
32996f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
33000aaf1057SPyun YongHyeon 	uint16_t devctl;
33015fea260fSMarius Strobl 	int i;
330295d67482SBill Paul 
330395d67482SBill Paul 	dev = sc->bge_dev;
330495d67482SBill Paul 
330538cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
330638cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
33076f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
33086f8718a3SScott Long 			write_op = bge_writemem_direct;
33096f8718a3SScott Long 		else
33106f8718a3SScott Long 			write_op = bge_writemem_ind;
33119ba784dbSScott Long 	} else
33126f8718a3SScott Long 		write_op = bge_writereg_ind;
33136f8718a3SScott Long 
331495d67482SBill Paul 	/* Save some important PCI state. */
331595d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
331695d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
331795d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
331895d67482SBill Paul 
331995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
332095d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3321e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
332295d67482SBill Paul 
33236f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
33246f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
3325a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
33266f8718a3SScott Long 		if (bootverbose)
3327333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
33286f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
33296f8718a3SScott Long 	}
33306f8718a3SScott Long 
33316f8718a3SScott Long 	/*
33326f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
33336f8718a3SScott Long 	 * When firmware finishes its initialization it will
33346f8718a3SScott Long 	 * write ~BGE_MAGIC_NUMBER to the same location.
33356f8718a3SScott Long 	 */
33366f8718a3SScott Long 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
33376f8718a3SScott Long 
33380c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
3339e53d81eeSPaul Saab 
3340e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3341652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
33420c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
33430c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, 0x7E2C, 0x20);
3344e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
3345e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
33460c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
33470c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
3348e53d81eeSPaul Saab 		}
3349e53d81eeSPaul Saab 	}
3350e53d81eeSPaul Saab 
335121c9e407SDavid Christensen 	/*
33526f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
33536f8718a3SScott Long 	 * powered up in D0 uninitialized.
33546f8718a3SScott Long 	 */
33555345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
3356caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
33576f8718a3SScott Long 
335895d67482SBill Paul 	/* Issue global reset */
33596f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
336095d67482SBill Paul 
336138cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
33625fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
336338cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
33645fea260fSMarius Strobl 		    val | BGE_VCPU_STATUS_DRV_RESET);
33655fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
336638cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
33675fea260fSMarius Strobl 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
336838cc658fSJohn Baldwin 	}
336938cc658fSJohn Baldwin 
337095d67482SBill Paul 	DELAY(1000);
337195d67482SBill Paul 
3372e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3373652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
3374e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
3375e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
33765fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
33775fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
3378e53d81eeSPaul Saab 		}
33790aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
33800aaf1057SPyun YongHyeon 		    sc->bge_expcap + PCIR_EXPRESS_DEVICE_CTL, 2);
33810aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
33829a6e301dSPyun YongHyeon 		devctl &= ~(PCIM_EXP_CTL_RELAXED_ORD_ENABLE |
33839a6e301dSPyun YongHyeon 		    PCIM_EXP_CTL_NOSNOOP_ENABLE);
33840aaf1057SPyun YongHyeon 		/* Set PCIE max payload size to 128. */
33850aaf1057SPyun YongHyeon 		devctl &= ~PCIM_EXP_CTL_MAX_PAYLOAD;
33860aaf1057SPyun YongHyeon 		pci_write_config(dev, sc->bge_expcap + PCIR_EXPRESS_DEVICE_CTL,
33870aaf1057SPyun YongHyeon 		    devctl, 2);
33880aaf1057SPyun YongHyeon 		/* Clear error status. */
33890aaf1057SPyun YongHyeon 		pci_write_config(dev, sc->bge_expcap + PCIR_EXPRESS_DEVICE_STA,
33909a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_CORRECTABLE_ERROR |
33919a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_NON_FATAL_ERROR | PCIM_EXP_STA_FATAL_ERROR |
33929a6e301dSPyun YongHyeon 		    PCIM_EXP_STA_UNSUPPORTED_REQ, 2);
3393e53d81eeSPaul Saab 	}
3394e53d81eeSPaul Saab 
33953f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
339695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
339795d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3398e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
339995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
340095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
34010c8aa4eaSJung-uk Kim 	write_op(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
3402cbb2b2feSPyun YongHyeon 	/*
3403cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
3404fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
3405cbb2b2feSPyun YongHyeon 	 * read stale status block.
3406cbb2b2feSPyun YongHyeon 	 */
3407cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
3408cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
3409cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
3410cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
3411cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
3412cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
3413cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
3414cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3415cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
3416cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
3417cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
3418cbb2b2feSPyun YongHyeon 		}
3419cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
3420cbb2b2feSPyun YongHyeon 		    devctl, 2);
3421cbb2b2feSPyun YongHyeon 	}
3422bf6ef57aSJohn Polstra 	/* Re-enable MSI, if neccesary, and enable the memory arbiter. */
34234c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
3424bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
3425bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
34260aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
34270aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
34280aaf1057SPyun YongHyeon 			pci_write_config(dev,
34290aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
3430bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
3431bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
3432bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
3433bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
3434bf6ef57aSJohn Polstra 		}
34354c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
34364c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
34374c0da0ffSGleb Smirnoff 	} else
3438a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
3439a7b0c314SPaul Saab 
344038cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
344138cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
344238cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
344338cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
344438cc658fSJohn Baldwin 				break;
344538cc658fSJohn Baldwin 			DELAY(100);
344638cc658fSJohn Baldwin 		}
344738cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
3448333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
344938cc658fSJohn Baldwin 			return (1);
345038cc658fSJohn Baldwin 		}
345138cc658fSJohn Baldwin 	} else {
345295d67482SBill Paul 		/*
34536f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
345408013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
34555fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
34565fea260fSMarius Strobl 		 * address is fitted though.
345795d67482SBill Paul 		 */
345895d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
3459d5d23857SJung-uk Kim 			DELAY(10);
346095d67482SBill Paul 			val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
346195d67482SBill Paul 			if (val == ~BGE_MAGIC_NUMBER)
346295d67482SBill Paul 				break;
346395d67482SBill Paul 		}
346495d67482SBill Paul 
34655fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
3466333704a3SPyun YongHyeon 			device_printf(dev,
3467333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
3468333704a3SPyun YongHyeon 			    val);
346938cc658fSJohn Baldwin 	}
347095d67482SBill Paul 
347195d67482SBill Paul 	/*
347295d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
347395d67482SBill Paul 	 * return to its original pre-reset state. This is a
347495d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
347595d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
347695d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
347795d67482SBill Paul 	 * results.
347895d67482SBill Paul 	 */
347995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
348095d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
348195d67482SBill Paul 			break;
348295d67482SBill Paul 		DELAY(10);
348395d67482SBill Paul 	}
348495d67482SBill Paul 
34853f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
3486e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
348795d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
348895d67482SBill Paul 
34898cb1383cSDoug Ambrisko 	/* Tell the ASF firmware we are up */
34908cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
34918cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
34928cb1383cSDoug Ambrisko 
349395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
349495d67482SBill Paul 
3495da3003f0SBill Paul 	/*
3496da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
3497da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
3498da3003f0SBill Paul 	 * to 1.2V.
3499da3003f0SBill Paul 	 */
3500652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
3501652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
35025fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
35035fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
35045fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
3505da3003f0SBill Paul 	}
3506da3003f0SBill Paul 
3507e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3508652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
35091108273aSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
3510a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
3511a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
3512a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
35135fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
35145fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
3515e53d81eeSPaul Saab 	}
351695d67482SBill Paul 	DELAY(10000);
35178cb1383cSDoug Ambrisko 
35188cb1383cSDoug Ambrisko 	return (0);
351995d67482SBill Paul }
352095d67482SBill Paul 
3521e0b7b101SPyun YongHyeon static __inline void
3522e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
3523e0b7b101SPyun YongHyeon {
3524e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
3525e0b7b101SPyun YongHyeon 
3526e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
3527e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
3528e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
3529e0b7b101SPyun YongHyeon 	r->bge_idx = i;
3530e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
3531e0b7b101SPyun YongHyeon }
3532e0b7b101SPyun YongHyeon 
3533e0b7b101SPyun YongHyeon static __inline void
3534e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
3535e0b7b101SPyun YongHyeon {
3536e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
3537e0b7b101SPyun YongHyeon 
3538e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
3539e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
3540e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
3541e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
3542e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
3543e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
3544e0b7b101SPyun YongHyeon 	r->bge_idx = i;
3545e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
3546e0b7b101SPyun YongHyeon }
3547e0b7b101SPyun YongHyeon 
354895d67482SBill Paul /*
354995d67482SBill Paul  * Frame reception handling. This is called if there's a frame
355095d67482SBill Paul  * on the receive return list.
355195d67482SBill Paul  *
355295d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
35531be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
355495d67482SBill Paul  * 2) the frame is from the standard receive ring
355595d67482SBill Paul  */
355695d67482SBill Paul 
35571abcdbd1SAttilio Rao static int
3558dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
355995d67482SBill Paul {
356095d67482SBill Paul 	struct ifnet *ifp;
35611abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
3562b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
356395d67482SBill Paul 
35647f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
35650f9bd73bSSam Leffler 
35663f74909aSGleb Smirnoff 	/* Nothing to do. */
35677f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
35681abcdbd1SAttilio Rao 		return (rx_npkts);
3569cfcb5025SOleg Bulyzhin 
3570fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
357195d67482SBill Paul 
3572f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
3573e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
3574f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
357515eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
3576*f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
3577*f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
3578c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN))
3579f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
358015eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
3581f41ac2beSBill Paul 
35827f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
358395d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
35843f74909aSGleb Smirnoff 		uint32_t		rxidx;
358595d67482SBill Paul 		struct mbuf		*m = NULL;
35863f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
358795d67482SBill Paul 		int			have_tag = 0;
358895d67482SBill Paul 
358975719184SGleb Smirnoff #ifdef DEVICE_POLLING
359075719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
359175719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
359275719184SGleb Smirnoff 				break;
359375719184SGleb Smirnoff 			sc->rxcycles--;
359475719184SGleb Smirnoff 		}
359575719184SGleb Smirnoff #endif
359675719184SGleb Smirnoff 
35977f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
359895d67482SBill Paul 
359995d67482SBill Paul 		rxidx = cur_rx->bge_idx;
36007f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
360195d67482SBill Paul 
3602cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
3603cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
360495d67482SBill Paul 			have_tag = 1;
360595d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
360695d67482SBill Paul 		}
360795d67482SBill Paul 
360895d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
360995d67482SBill Paul 			jumbocnt++;
3610943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
361195d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3612e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
361395d67482SBill Paul 				continue;
361495d67482SBill Paul 			}
3615943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
3616e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
3617943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
361895d67482SBill Paul 				continue;
361995d67482SBill Paul 			}
362003e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
362195d67482SBill Paul 		} else {
362295d67482SBill Paul 			stdcnt++;
3623e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
362495d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3625e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
362695d67482SBill Paul 				continue;
362795d67482SBill Paul 			}
3628943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
3629e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
3630943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
363195d67482SBill Paul 				continue;
363295d67482SBill Paul 			}
363303e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
363495d67482SBill Paul 		}
363595d67482SBill Paul 
363695d67482SBill Paul 		ifp->if_ipackets++;
3637e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
3638e255b776SJohn Polstra 		/*
3639e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
3640e65bed95SPyun YongHyeon 		 * the payload is aligned.
3641e255b776SJohn Polstra 		 */
3642652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
3643e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
3644e255b776SJohn Polstra 			    cur_rx->bge_len);
3645e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
3646e255b776SJohn Polstra 		}
3647e255b776SJohn Polstra #endif
3648473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
364995d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
365095d67482SBill Paul 
36511108273aSPyun YongHyeon 		if (ifp->if_capenable & IFCAP_RXCSUM)
36521108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
365395d67482SBill Paul 
365495d67482SBill Paul 		/*
3655673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
3656673d9191SSam Leffler 		 * attach that information to the packet.
365795d67482SBill Paul 		 */
3658d147662cSGleb Smirnoff 		if (have_tag) {
365978ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
366078ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
3661d147662cSGleb Smirnoff 		}
366295d67482SBill Paul 
3663dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
36640f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
3665673d9191SSam Leffler 			(*ifp->if_input)(ifp, m);
36660f9bd73bSSam Leffler 			BGE_LOCK(sc);
3667dfe0df9aSPyun YongHyeon 		} else
3668dfe0df9aSPyun YongHyeon 			(*ifp->if_input)(ifp, m);
3669d4da719cSAttilio Rao 		rx_npkts++;
367025e13e68SXin LI 
367125e13e68SXin LI 		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
36728cf7d13dSAttilio Rao 			return (rx_npkts);
367395d67482SBill Paul 	}
367495d67482SBill Paul 
367515eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
367615eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
3677e65bed95SPyun YongHyeon 	if (stdcnt > 0)
3678f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
3679e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
36804c0da0ffSGleb Smirnoff 
3681c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
3682f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
36834c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
3684f41ac2beSBill Paul 
36857f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
368638cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
368795d67482SBill Paul 	if (stdcnt)
3688767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
3689767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
369095d67482SBill Paul 	if (jumbocnt)
3691767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
3692767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
3693f5a034f9SPyun YongHyeon #ifdef notyet
3694f5a034f9SPyun YongHyeon 	/*
3695f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
3696f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
3697f5a034f9SPyun YongHyeon 	 */
3698f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
3699f5a034f9SPyun YongHyeon 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
3700f5a034f9SPyun YongHyeon #endif
37011abcdbd1SAttilio Rao 	return (rx_npkts);
370295d67482SBill Paul }
370395d67482SBill Paul 
370495d67482SBill Paul static void
37051108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
37061108273aSPyun YongHyeon {
37071108273aSPyun YongHyeon 
37081108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
37091108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
37101108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
37111108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
37121108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
37131108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
37141108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
37151108273aSPyun YongHyeon 			}
37161108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
37171108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
37181108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
37191108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
37201108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
37211108273aSPyun YongHyeon 			}
37221108273aSPyun YongHyeon 		}
37231108273aSPyun YongHyeon 	} else {
37241108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
37251108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
37261108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
37271108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
37281108273aSPyun YongHyeon 		}
37291108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
37301108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
37311108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
37321108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
37331108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
37341108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
37351108273aSPyun YongHyeon 		}
37361108273aSPyun YongHyeon 	}
37371108273aSPyun YongHyeon }
37381108273aSPyun YongHyeon 
37391108273aSPyun YongHyeon static void
3740b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
374195d67482SBill Paul {
374295a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
374395d67482SBill Paul 	struct ifnet *ifp;
374495d67482SBill Paul 
37450f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
37460f9bd73bSSam Leffler 
37473f74909aSGleb Smirnoff 	/* Nothing to do. */
3748b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
3749cfcb5025SOleg Bulyzhin 		return;
3750cfcb5025SOleg Bulyzhin 
3751fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
375295d67482SBill Paul 
3753e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
37545c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
375595d67482SBill Paul 	/*
375695d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
375795d67482SBill Paul 	 * frames that have been sent.
375895d67482SBill Paul 	 */
3759b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
376095a0a340SPyun YongHyeon 		uint32_t		idx;
376195d67482SBill Paul 
376295d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
3763f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
376495d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
376595d67482SBill Paul 			ifp->if_opackets++;
376695d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
37670ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
3768e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
3769e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
37700ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
3771f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
3772e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
3773e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
377495d67482SBill Paul 		}
377595d67482SBill Paul 		sc->bge_txcnt--;
377695d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
377795d67482SBill Paul 	}
377895d67482SBill Paul 
377913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
37805b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
37815b01e77cSBruce Evans 		sc->bge_timer = 0;
378295d67482SBill Paul }
378395d67482SBill Paul 
378475719184SGleb Smirnoff #ifdef DEVICE_POLLING
37851abcdbd1SAttilio Rao static int
378675719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
378775719184SGleb Smirnoff {
378875719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
3789b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
3790366454f2SOleg Bulyzhin 	uint32_t statusword;
37911abcdbd1SAttilio Rao 	int rx_npkts = 0;
379275719184SGleb Smirnoff 
37933f74909aSGleb Smirnoff 	BGE_LOCK(sc);
37943f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
37953f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
37961abcdbd1SAttilio Rao 		return (rx_npkts);
37973f74909aSGleb Smirnoff 	}
379875719184SGleb Smirnoff 
3799dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3800b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3801b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3802b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
3803b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
3804dab5cd05SOleg Bulyzhin 
3805175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
3806175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
3807dab5cd05SOleg Bulyzhin 
3808dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3809b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3810b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3811366454f2SOleg Bulyzhin 
38120c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
3813366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
3814366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
3815366454f2SOleg Bulyzhin 
3816366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
3817366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
38184c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3819652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
3820366454f2SOleg Bulyzhin 			bge_link_upd(sc);
3821366454f2SOleg Bulyzhin 
3822366454f2SOleg Bulyzhin 	sc->rxcycles = count;
3823dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
382425e13e68SXin LI 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
382525e13e68SXin LI 		BGE_UNLOCK(sc);
38268cf7d13dSAttilio Rao 		return (rx_npkts);
382725e13e68SXin LI 	}
3828b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
3829366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3830366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
38313f74909aSGleb Smirnoff 
38323f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
38331abcdbd1SAttilio Rao 	return (rx_npkts);
383475719184SGleb Smirnoff }
383575719184SGleb Smirnoff #endif /* DEVICE_POLLING */
383675719184SGleb Smirnoff 
3837dfe0df9aSPyun YongHyeon static int
3838dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
3839dfe0df9aSPyun YongHyeon {
3840dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
3841dfe0df9aSPyun YongHyeon 
3842dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
3843dfe0df9aSPyun YongHyeon 	/*
3844dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
3845dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
3846dfe0df9aSPyun YongHyeon 	 */
3847dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
3848dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
3849dfe0df9aSPyun YongHyeon }
3850dfe0df9aSPyun YongHyeon 
3851dfe0df9aSPyun YongHyeon static void
3852dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
3853dfe0df9aSPyun YongHyeon {
3854dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
3855dfe0df9aSPyun YongHyeon 	struct ifnet *ifp;
38561108273aSPyun YongHyeon 	uint32_t status, status_tag;
3857dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
3858dfe0df9aSPyun YongHyeon 
3859dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
3860dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
3861dfe0df9aSPyun YongHyeon 
386266151edfSPyun YongHyeon 	BGE_LOCK(sc);
386366151edfSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
386466151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
3865dfe0df9aSPyun YongHyeon 		return;
386666151edfSPyun YongHyeon 	}
3867dfe0df9aSPyun YongHyeon 
3868dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
3869dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3870dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3871dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3872dfe0df9aSPyun YongHyeon 
3873dfe0df9aSPyun YongHyeon 	/* Save producer/consumer indexess. */
3874dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
3875dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
3876dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
38771108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
3878dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
3879dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3880dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3881dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
38821108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
38831108273aSPyun YongHyeon 		status_tag = 0;
388466151edfSPyun YongHyeon 
388566151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
388666151edfSPyun YongHyeon 		bge_link_upd(sc);
388766151edfSPyun YongHyeon 
3888dfe0df9aSPyun YongHyeon 	/* Let controller work. */
38891108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
3890dfe0df9aSPyun YongHyeon 
389166151edfSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
389266151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
3893dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
389466151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
3895dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
389666151edfSPyun YongHyeon 		BGE_LOCK(sc);
3897dfe0df9aSPyun YongHyeon 	}
3898dfe0df9aSPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3899dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
3900dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
3901dfe0df9aSPyun YongHyeon 	    	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3902dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
3903dfe0df9aSPyun YongHyeon 	}
390466151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
3905dfe0df9aSPyun YongHyeon }
3906dfe0df9aSPyun YongHyeon 
390795d67482SBill Paul static void
39083f74909aSGleb Smirnoff bge_intr(void *xsc)
390995d67482SBill Paul {
391095d67482SBill Paul 	struct bge_softc *sc;
391195d67482SBill Paul 	struct ifnet *ifp;
3912dab5cd05SOleg Bulyzhin 	uint32_t statusword;
3913b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
391495d67482SBill Paul 
391595d67482SBill Paul 	sc = xsc;
3916f41ac2beSBill Paul 
39170f9bd73bSSam Leffler 	BGE_LOCK(sc);
39180f9bd73bSSam Leffler 
3919dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
3920dab5cd05SOleg Bulyzhin 
392175719184SGleb Smirnoff #ifdef DEVICE_POLLING
392275719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
392375719184SGleb Smirnoff 		BGE_UNLOCK(sc);
392475719184SGleb Smirnoff 		return;
392575719184SGleb Smirnoff 	}
392675719184SGleb Smirnoff #endif
392775719184SGleb Smirnoff 
3928f30cbfc6SScott Long 	/*
3929b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
3930b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
3931b848e032SBruce Evans 	 * our current organization this just gives complications and
3932b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
3933b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
3934b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
3935b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
3936b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
3937b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
3938b848e032SBruce Evans 	 *
3939b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
3940b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
3941b848e032SBruce Evans 	 * changing later because it is more efficient to get another
3942b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
3943b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
3944b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
3945b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
3946b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
3947b848e032SBruce Evans 	 */
394838cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
3949b848e032SBruce Evans 
3950f584dfd1SPyun YongHyeon 	/*
3951f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
3952f584dfd1SPyun YongHyeon 	 */
3953f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
3954f584dfd1SPyun YongHyeon 
3955f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
3956f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3957f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3958f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3959f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
3960f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
3961f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
3962f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3963f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
3964f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3965f584dfd1SPyun YongHyeon 
39661f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
39674c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3968f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
3969dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
397095d67482SBill Paul 
397113f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
39723f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
3973dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
397425e13e68SXin LI 	}
397595d67482SBill Paul 
397625e13e68SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
39773f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
3978b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
397995d67482SBill Paul 	}
398095d67482SBill Paul 
398113f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
398213f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
39830f9bd73bSSam Leffler 		bge_start_locked(ifp);
39840f9bd73bSSam Leffler 
39850f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
398695d67482SBill Paul }
398795d67482SBill Paul 
398895d67482SBill Paul static void
39898cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
39908cb1383cSDoug Ambrisko {
39918cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
39928cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
39938cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
39948cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
39958cb1383cSDoug Ambrisko 		else {
3996899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
39978cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW,
39988cb1383cSDoug Ambrisko 			    BGE_FW_DRV_ALIVE);
39998cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_LEN, 4);
40008cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_DATA, 3);
40018cb1383cSDoug Ambrisko 			CSR_WRITE_4(sc, BGE_CPU_EVENT,
400239153c5aSJung-uk Kim 			    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
40038cb1383cSDoug Ambrisko 		}
40048cb1383cSDoug Ambrisko 	}
40058cb1383cSDoug Ambrisko }
40068cb1383cSDoug Ambrisko 
40078cb1383cSDoug Ambrisko static void
4008b74e67fbSGleb Smirnoff bge_tick(void *xsc)
40090f9bd73bSSam Leffler {
4010b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
401195d67482SBill Paul 	struct mii_data *mii = NULL;
401295d67482SBill Paul 
40130f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
401495d67482SBill Paul 
40155dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
40165dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
40175dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
40185dda8085SOleg Bulyzhin 	    	return;
40195dda8085SOleg Bulyzhin 
40207ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
40210434d1b8SBill Paul 		bge_stats_update_regs(sc);
40220434d1b8SBill Paul 	else
402395d67482SBill Paul 		bge_stats_update(sc);
402495d67482SBill Paul 
4025652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
402695d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
402782b67c01SOleg Bulyzhin 		/*
402882b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
402982b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
403082b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
403182b67c01SOleg Bulyzhin 		 */
403282b67c01SOleg Bulyzhin 		if (!sc->bge_link)
403395d67482SBill Paul 			mii_tick(mii);
40347b97099dSOleg Bulyzhin 	} else {
40357b97099dSOleg Bulyzhin 		/*
40367b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
40377b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
40387b97099dSOleg Bulyzhin 		 * and trigger interrupt.
40397b97099dSOleg Bulyzhin 		 */
40407b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
40413f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
40427b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
40437b97099dSOleg Bulyzhin #endif
40447b97099dSOleg Bulyzhin 		{
40457b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
40464f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
40474f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
40487b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
40494f0794ffSBjoern A. Zeeb 		else
40504f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
40517b97099dSOleg Bulyzhin 		}
4052dab5cd05SOleg Bulyzhin 	}
405395d67482SBill Paul 
40548cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4055b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
40568cb1383cSDoug Ambrisko 
4057dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
405895d67482SBill Paul }
405995d67482SBill Paul 
406095d67482SBill Paul static void
40613f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
40620434d1b8SBill Paul {
40633f74909aSGleb Smirnoff 	struct ifnet *ifp;
40642280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
40650434d1b8SBill Paul 
4066fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
40672280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
40680434d1b8SBill Paul 
40692280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
40702280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
40712280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
40722280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
40732280c16bSPyun YongHyeon 	stats->outXonSent +=
40742280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
40752280c16bSPyun YongHyeon 	stats->outXoffSent +=
40762280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
40772280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
40782280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
40792280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
40802280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
40812280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
40822280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
40832280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
40842280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
40852280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
40862280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
40872280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
40882280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
40892280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
40902280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
40912280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
40922280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
40932280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
40942280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
40957e6e2507SJung-uk Kim 
40962280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
40972280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
40982280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
40992280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
41002280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
41012280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
41022280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
41032280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
41042280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
41052280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
41062280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
41072280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
41082280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
41092280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
41102280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
41112280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
41122280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
41132280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
41142280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
41152280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
41162280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
41172280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
41182280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
41192280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
41202280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
41212280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
41222280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
41232280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
41242280c16bSPyun YongHyeon 
41252280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
41262280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
41272280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
41282280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
41292280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
41302280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
41312280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
41322280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
41332280c16bSPyun YongHyeon 	stats->InputDiscards +=
41342280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
41352280c16bSPyun YongHyeon 	stats->InputErrors +=
41362280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
41372280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
41382280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
41392280c16bSPyun YongHyeon 
41402280c16bSPyun YongHyeon 	ifp->if_collisions = (u_long)stats->etherStatsCollisions;
41412280c16bSPyun YongHyeon 	ifp->if_ierrors = (u_long)(stats->NoMoreRxBDs + stats->InputDiscards +
41422280c16bSPyun YongHyeon 	    stats->InputErrors);
41432280c16bSPyun YongHyeon }
41442280c16bSPyun YongHyeon 
41452280c16bSPyun YongHyeon static void
41462280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
41472280c16bSPyun YongHyeon {
41482280c16bSPyun YongHyeon 
41492280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
41502280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
41512280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
41522280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
41532280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
41542280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
41552280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
41562280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
41572280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
41582280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
41592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
41602280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
41612280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
41622280c16bSPyun YongHyeon 
41632280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
41642280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
41652280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
41662280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
41672280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
41682280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
41692280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
41702280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
41712280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
41722280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
41732280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
41742280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
41752280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
41762280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
41772280c16bSPyun YongHyeon 
41782280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
41792280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
41802280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
41812280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
41822280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
41832280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
41842280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
41850434d1b8SBill Paul }
41860434d1b8SBill Paul 
41870434d1b8SBill Paul static void
41883f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
418995d67482SBill Paul {
419095d67482SBill Paul 	struct ifnet *ifp;
4191e907febfSPyun YongHyeon 	bus_size_t stats;
41927e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
419395d67482SBill Paul 
4194fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
419595d67482SBill Paul 
4196e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
4197e907febfSPyun YongHyeon 
4198e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
4199e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
420095d67482SBill Paul 
42018634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
42026b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
42036fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
42046fb34dd2SOleg Bulyzhin 
42056fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
42066b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
42076fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
42086fb34dd2SOleg Bulyzhin 
42096fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
42106b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
42116fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
421295d67482SBill Paul 
4213e907febfSPyun YongHyeon #undef	READ_STAT
421495d67482SBill Paul }
421595d67482SBill Paul 
421695d67482SBill Paul /*
4217d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
4218d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
4219d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
4220d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
4221d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
4222d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
4223d375e524SGleb Smirnoff  */
4224d375e524SGleb Smirnoff static __inline int
4225d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
4226d375e524SGleb Smirnoff {
4227d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
4228d375e524SGleb Smirnoff 	struct mbuf *last;
4229d375e524SGleb Smirnoff 
4230d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
4231d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
4232d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
4233d375e524SGleb Smirnoff 		last = m;
4234d375e524SGleb Smirnoff 	} else {
4235d375e524SGleb Smirnoff 		/*
4236d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
4237d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
4238d375e524SGleb Smirnoff 		 */
4239d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
4240d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
4241d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
4242d375e524SGleb Smirnoff 			struct mbuf *n;
4243d375e524SGleb Smirnoff 
4244d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
4245d375e524SGleb Smirnoff 			if (n == NULL)
4246d375e524SGleb Smirnoff 				return (ENOBUFS);
4247d375e524SGleb Smirnoff 			n->m_len = 0;
4248d375e524SGleb Smirnoff 			last->m_next = n;
4249d375e524SGleb Smirnoff 			last = n;
4250d375e524SGleb Smirnoff 		}
4251d375e524SGleb Smirnoff 	}
4252d375e524SGleb Smirnoff 
4253d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
4254d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
4255d375e524SGleb Smirnoff 	last->m_len += padlen;
4256d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
4257d375e524SGleb Smirnoff 
4258d375e524SGleb Smirnoff 	return (0);
4259d375e524SGleb Smirnoff }
4260d375e524SGleb Smirnoff 
4261ca3f1187SPyun YongHyeon static struct mbuf *
4262d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
4263d598b626SPyun YongHyeon {
4264d598b626SPyun YongHyeon 	struct mbuf *n;
4265d598b626SPyun YongHyeon 	int found;
4266d598b626SPyun YongHyeon 
4267d598b626SPyun YongHyeon 	/*
4268d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
4269d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
4270d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
4271d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
4272d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
4273d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
4274d598b626SPyun YongHyeon 	 */
4275d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
4276d598b626SPyun YongHyeon 		if (n->m_len < 8) {
4277d598b626SPyun YongHyeon 			found++;
4278d598b626SPyun YongHyeon 			if (found > 1)
4279d598b626SPyun YongHyeon 				break;
4280d598b626SPyun YongHyeon 			continue;
4281d598b626SPyun YongHyeon 		}
4282d598b626SPyun YongHyeon 		found = 0;
4283d598b626SPyun YongHyeon 	}
4284d598b626SPyun YongHyeon 
4285d598b626SPyun YongHyeon 	if (found > 1) {
4286d598b626SPyun YongHyeon 		n = m_defrag(m, M_DONTWAIT);
4287d598b626SPyun YongHyeon 		if (n == NULL)
4288d598b626SPyun YongHyeon 			m_freem(m);
4289d598b626SPyun YongHyeon 	} else
4290d598b626SPyun YongHyeon 		n = m;
4291d598b626SPyun YongHyeon 	return (n);
4292d598b626SPyun YongHyeon }
4293d598b626SPyun YongHyeon 
4294d598b626SPyun YongHyeon static struct mbuf *
42951108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
42961108273aSPyun YongHyeon     uint16_t *flags)
4297ca3f1187SPyun YongHyeon {
4298ca3f1187SPyun YongHyeon 	struct ip *ip;
4299ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
4300ca3f1187SPyun YongHyeon 	struct mbuf *n;
4301ca3f1187SPyun YongHyeon 	uint16_t hlen;
43025b355c4fSPyun YongHyeon 	uint32_t poff;
4303ca3f1187SPyun YongHyeon 
4304ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
4305ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
4306ca3f1187SPyun YongHyeon 		n = m_dup(m, M_DONTWAIT);
4307ca3f1187SPyun YongHyeon 		m_freem(m);
4308ca3f1187SPyun YongHyeon 		if (n == NULL)
4309ca3f1187SPyun YongHyeon 			return (NULL);
4310ca3f1187SPyun YongHyeon 		m = n;
4311ca3f1187SPyun YongHyeon 	}
43125b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
4313ca3f1187SPyun YongHyeon 	if (m == NULL)
4314ca3f1187SPyun YongHyeon 		return (NULL);
43155b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
43165b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
4317ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
4318ca3f1187SPyun YongHyeon 	if (m == NULL)
4319ca3f1187SPyun YongHyeon 		return (NULL);
4320ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
43215b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
4322ca3f1187SPyun YongHyeon 	if (m == NULL)
4323ca3f1187SPyun YongHyeon 		return (NULL);
4324ca3f1187SPyun YongHyeon 	/*
4325ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
4326ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
4327ca3f1187SPyun YongHyeon 	 */
4328ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
432996486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
4330ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
4331ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
4332ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
433396486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
4334ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
4335ca3f1187SPyun YongHyeon 	/*
4336ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
4337ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
4338ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
43391108273aSPyun YongHyeon 	 * we only support hardware based TSO.
4340ca3f1187SPyun YongHyeon 	 */
43411108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
4342ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
43431108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
43441108273aSPyun YongHyeon 		/*
43451108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
43461108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
43471108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
43481108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
43491108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
43501108273aSPyun YongHyeon 		 * frames are supported.
43511108273aSPyun YongHyeon 		 */
43521108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
43531108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
43541108273aSPyun YongHyeon 	} else {
43551108273aSPyun YongHyeon 		/*
43561108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
43571108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
43581108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
43591108273aSPyun YongHyeon 		 * supported.
43601108273aSPyun YongHyeon 		 */
4361ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
43621108273aSPyun YongHyeon 	}
4363ca3f1187SPyun YongHyeon 	return (m);
4364ca3f1187SPyun YongHyeon }
4365ca3f1187SPyun YongHyeon 
4366d375e524SGleb Smirnoff /*
436795d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
436895d67482SBill Paul  * pointers to descriptors.
436995d67482SBill Paul  */
437095d67482SBill Paul static int
4371676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
437295d67482SBill Paul {
43737e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
4374f41ac2beSBill Paul 	bus_dmamap_t		map;
4375676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
4376676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
43777e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
4378ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
43797e27542aSGleb Smirnoff 	int			nsegs, i, error;
438095d67482SBill Paul 
43816909dc43SGleb Smirnoff 	csum_flags = 0;
4382ca3f1187SPyun YongHyeon 	mss = 0;
4383ca3f1187SPyun YongHyeon 	vlan_tag = 0;
4384d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
4385d598b626SPyun YongHyeon 	    m->m_next != NULL) {
4386d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
4387d598b626SPyun YongHyeon 		if (*m_head == NULL)
4388d598b626SPyun YongHyeon 			return (ENOBUFS);
4389d598b626SPyun YongHyeon 		m = *m_head;
4390d598b626SPyun YongHyeon 	}
4391ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
43921108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
4393ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
4394ca3f1187SPyun YongHyeon 			return (ENOBUFS);
4395ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
4396ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
439735f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
43986909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
43996909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
44006909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
44016909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
44026909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
44036909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
44046909dc43SGleb Smirnoff 				m_freem(m);
44056909dc43SGleb Smirnoff 				*m_head = NULL;
44066909dc43SGleb Smirnoff 				return (error);
44076909dc43SGleb Smirnoff 			}
44086909dc43SGleb Smirnoff 		}
44096909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
44106909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
44116909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
44126909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
44136909dc43SGleb Smirnoff 	}
44146909dc43SGleb Smirnoff 
44151108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
44161108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
44171108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
44181108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
44191108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
4420beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
4421d94f2b85SPyun YongHyeon 			/*
4422d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
4423d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
4424d94f2b85SPyun YongHyeon 			 * DMA read operation.
4425d94f2b85SPyun YongHyeon 			 */
4426beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
4427d94f2b85SPyun YongHyeon 				m = m_defrag(m, M_DONTWAIT);
4428d94f2b85SPyun YongHyeon 			else
44291108273aSPyun YongHyeon 				m = m_collapse(m, M_DONTWAIT,
44301108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
4431261f04d6SPyun YongHyeon 			if (m == NULL)
4432261f04d6SPyun YongHyeon 				m = *m_head;
4433d94f2b85SPyun YongHyeon 			*m_head = m;
4434d94f2b85SPyun YongHyeon 		}
44351108273aSPyun YongHyeon 	}
4436d94f2b85SPyun YongHyeon 
44377e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
44380ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
4439676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
44407e27542aSGleb Smirnoff 	if (error == EFBIG) {
44414eee14cbSMarius Strobl 		m = m_collapse(m, M_DONTWAIT, BGE_NSEG_NEW);
4442676ad2c9SGleb Smirnoff 		if (m == NULL) {
4443676ad2c9SGleb Smirnoff 			m_freem(*m_head);
4444676ad2c9SGleb Smirnoff 			*m_head = NULL;
44457e27542aSGleb Smirnoff 			return (ENOBUFS);
44467e27542aSGleb Smirnoff 		}
4447676ad2c9SGleb Smirnoff 		*m_head = m;
44480ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
44490ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
4450676ad2c9SGleb Smirnoff 		if (error) {
4451676ad2c9SGleb Smirnoff 			m_freem(m);
4452676ad2c9SGleb Smirnoff 			*m_head = NULL;
44537e27542aSGleb Smirnoff 			return (error);
44547e27542aSGleb Smirnoff 		}
4455676ad2c9SGleb Smirnoff 	} else if (error != 0)
4456676ad2c9SGleb Smirnoff 		return (error);
44577e27542aSGleb Smirnoff 
4458167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
4459167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
44600ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
446195d67482SBill Paul 		return (ENOBUFS);
44627e27542aSGleb Smirnoff 	}
44637e27542aSGleb Smirnoff 
44640ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
4465e65bed95SPyun YongHyeon 
4466ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
4467ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
4468ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
4469ca3f1187SPyun YongHyeon 	}
44707e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
44717e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
44727e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
44737e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
44747e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
44757e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
4476ca3f1187SPyun YongHyeon 		d->bge_vlan_tag = vlan_tag;
4477ca3f1187SPyun YongHyeon 		d->bge_mss = mss;
44787e27542aSGleb Smirnoff 		if (i == nsegs - 1)
44797e27542aSGleb Smirnoff 			break;
44807e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
44817e27542aSGleb Smirnoff 	}
44827e27542aSGleb Smirnoff 
44837e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
44847e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
4485676ad2c9SGleb Smirnoff 
4486f41ac2beSBill Paul 	/*
4487f41ac2beSBill Paul 	 * Insure that the map for this transmission
4488f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
4489f41ac2beSBill Paul 	 * in this chain.
4490f41ac2beSBill Paul 	 */
44917e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
44927e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
4493676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
44947e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
449595d67482SBill Paul 
44967e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
44977e27542aSGleb Smirnoff 	*txidx = idx;
449895d67482SBill Paul 
449995d67482SBill Paul 	return (0);
450095d67482SBill Paul }
450195d67482SBill Paul 
450295d67482SBill Paul /*
450395d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
450495d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
450595d67482SBill Paul  */
450695d67482SBill Paul static void
45073f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
450895d67482SBill Paul {
450995d67482SBill Paul 	struct bge_softc *sc;
4510167fdb62SPyun YongHyeon 	struct mbuf *m_head;
451114bbd30fSGleb Smirnoff 	uint32_t prodidx;
4512167fdb62SPyun YongHyeon 	int count;
451395d67482SBill Paul 
451495d67482SBill Paul 	sc = ifp->if_softc;
4515167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
451695d67482SBill Paul 
4517167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
4518167fdb62SPyun YongHyeon 	    (ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
4519167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
452095d67482SBill Paul 		return;
452195d67482SBill Paul 
452214bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
452395d67482SBill Paul 
4524167fdb62SPyun YongHyeon 	for (count = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
4525167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
4526167fdb62SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
4527167fdb62SPyun YongHyeon 			break;
4528167fdb62SPyun YongHyeon 		}
45294d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
453095d67482SBill Paul 		if (m_head == NULL)
453195d67482SBill Paul 			break;
453295d67482SBill Paul 
453395d67482SBill Paul 		/*
453495d67482SBill Paul 		 * XXX
4535b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
4536b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
4537b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
4538b874fdd4SYaroslav Tykhiy 		 *
4539b874fdd4SYaroslav Tykhiy 		 * XXX
454095d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
454195d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
454295d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
454395d67482SBill Paul 		 * chain at once.
454495d67482SBill Paul 		 * (paranoia -- may not actually be needed)
454595d67482SBill Paul 		 */
454695d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
454795d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
454895d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
454995d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
45504d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
455113f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
455295d67482SBill Paul 				break;
455395d67482SBill Paul 			}
455495d67482SBill Paul 		}
455595d67482SBill Paul 
455695d67482SBill Paul 		/*
455795d67482SBill Paul 		 * Pack the data into the transmit ring. If we
455895d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
455995d67482SBill Paul 		 * for the NIC to drain the ring.
456095d67482SBill Paul 		 */
4561676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
4562676ad2c9SGleb Smirnoff 			if (m_head == NULL)
4563676ad2c9SGleb Smirnoff 				break;
45644d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
456513f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
456695d67482SBill Paul 			break;
456795d67482SBill Paul 		}
4568303a718cSDag-Erling Smørgrav 		++count;
456995d67482SBill Paul 
457095d67482SBill Paul 		/*
457195d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
457295d67482SBill Paul 		 * to him.
457395d67482SBill Paul 		 */
45744e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
457545ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
45764e35d186SJung-uk Kim #else
45774e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
45784e35d186SJung-uk Kim #endif
457995d67482SBill Paul 	}
458095d67482SBill Paul 
4581167fdb62SPyun YongHyeon 	if (count > 0) {
4582aa94f333SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
45835c1da2faSPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
45843f74909aSGleb Smirnoff 		/* Transmit. */
458538cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
45863927098fSPaul Saab 		/* 5700 b2 errata */
4587e0ced696SPaul Saab 		if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
458838cc658fSJohn Baldwin 			bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
458995d67482SBill Paul 
459014bbd30fSGleb Smirnoff 		sc->bge_tx_prodidx = prodidx;
459114bbd30fSGleb Smirnoff 
459295d67482SBill Paul 		/*
459395d67482SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
459495d67482SBill Paul 		 */
4595b74e67fbSGleb Smirnoff 		sc->bge_timer = 5;
459695d67482SBill Paul 	}
4597167fdb62SPyun YongHyeon }
459895d67482SBill Paul 
45990f9bd73bSSam Leffler /*
46000f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
46010f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
46020f9bd73bSSam Leffler  */
460395d67482SBill Paul static void
46043f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
460595d67482SBill Paul {
46060f9bd73bSSam Leffler 	struct bge_softc *sc;
46070f9bd73bSSam Leffler 
46080f9bd73bSSam Leffler 	sc = ifp->if_softc;
46090f9bd73bSSam Leffler 	BGE_LOCK(sc);
46100f9bd73bSSam Leffler 	bge_start_locked(ifp);
46110f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
46120f9bd73bSSam Leffler }
46130f9bd73bSSam Leffler 
46140f9bd73bSSam Leffler static void
46153f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
46160f9bd73bSSam Leffler {
461795d67482SBill Paul 	struct ifnet *ifp;
46183f74909aSGleb Smirnoff 	uint16_t *m;
4619f6a65488SPyun YongHyeon 	uint32_t mode;
462095d67482SBill Paul 
46210f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
462295d67482SBill Paul 
4623fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
462495d67482SBill Paul 
462513f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
462695d67482SBill Paul 		return;
462795d67482SBill Paul 
462895d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
462995d67482SBill Paul 	bge_stop(sc);
46308cb1383cSDoug Ambrisko 
46318cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
46328cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
463395d67482SBill Paul 	bge_reset(sc);
46348cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
46358cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
46368cb1383cSDoug Ambrisko 
463795d67482SBill Paul 	bge_chipinit(sc);
463895d67482SBill Paul 
463995d67482SBill Paul 	/*
464095d67482SBill Paul 	 * Init the various state machines, ring
464195d67482SBill Paul 	 * control blocks and firmware.
464295d67482SBill Paul 	 */
464395d67482SBill Paul 	if (bge_blockinit(sc)) {
4644fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
464595d67482SBill Paul 		return;
464695d67482SBill Paul 	}
464795d67482SBill Paul 
4648fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
464995d67482SBill Paul 
465095d67482SBill Paul 	/* Specify MTU. */
465195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
4652cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
4653cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
465495d67482SBill Paul 
465595d67482SBill Paul 	/* Load our MAC address. */
46563f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
465795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
465895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
465995d67482SBill Paul 
46603e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
46613e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
466295d67482SBill Paul 
466395d67482SBill Paul 	/* Program multicast filter. */
466495d67482SBill Paul 	bge_setmulti(sc);
466595d67482SBill Paul 
4666cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
4667cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
4668cb2eacc7SYaroslav Tykhiy 
466935f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
467035f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
467135f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
467235f945cdSPyun YongHyeon 	else
467335f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
467435f945cdSPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_TXCSUM &&
467535f945cdSPyun YongHyeon 	    ifp->if_capenable & IFCAP_TXCSUM) {
467635f945cdSPyun YongHyeon 		ifp->if_hwassist &= ~(BGE_CSUM_FEATURES | CSUM_UDP);
467735f945cdSPyun YongHyeon 		ifp->if_hwassist |= sc->bge_csum_features;
467835f945cdSPyun YongHyeon 	}
467935f945cdSPyun YongHyeon 
468095d67482SBill Paul 	/* Init RX ring. */
46813ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
46823ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
46833ee5d7daSPyun YongHyeon 		bge_stop(sc);
46843ee5d7daSPyun YongHyeon 		return;
46853ee5d7daSPyun YongHyeon 	}
468695d67482SBill Paul 
46870434d1b8SBill Paul 	/*
46880434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
46890434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
46900434d1b8SBill Paul 	 * entry of the ring.
46910434d1b8SBill Paul 	 */
46920434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
46933f74909aSGleb Smirnoff 		uint32_t		v, i;
46940434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
46950434d1b8SBill Paul 			DELAY(20);
46960434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
46970434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
46980434d1b8SBill Paul 				break;
46990434d1b8SBill Paul 		}
47000434d1b8SBill Paul 		if (i == 10)
4701fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
4702fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
47030434d1b8SBill Paul 	}
47040434d1b8SBill Paul 
470595d67482SBill Paul 	/* Init jumbo RX ring. */
4706*f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
4707*f5459d4cSPyun YongHyeon 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
4708c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN)) {
47093ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
4710333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
4711b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
47123ee5d7daSPyun YongHyeon 			bge_stop(sc);
47133ee5d7daSPyun YongHyeon 			return;
47143ee5d7daSPyun YongHyeon 		}
47153ee5d7daSPyun YongHyeon 	}
471695d67482SBill Paul 
47173f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
471895d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
471995d67482SBill Paul 
47207e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
47217e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
47227e6e2507SJung-uk Kim 
472395d67482SBill Paul 	/* Init TX ring. */
472495d67482SBill Paul 	bge_init_tx_ring(sc);
472595d67482SBill Paul 
4726f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
4727f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
4728f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
4729f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
47303f74909aSGleb Smirnoff 	/* Turn on transmitter. */
4731f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
473295d67482SBill Paul 
47333f74909aSGleb Smirnoff 	/* Turn on receiver. */
473495d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
473595d67482SBill Paul 
4736dedcdf57SPyun YongHyeon 	/*
4737dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
4738dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
4739dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
4740dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
4741dedcdf57SPyun YongHyeon 	 */
4742dedcdf57SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
4743dedcdf57SPyun YongHyeon 
47442280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
47452280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
47462280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
47472280c16bSPyun YongHyeon 
474895d67482SBill Paul 	/* Tell firmware we're alive. */
474995d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
475095d67482SBill Paul 
475175719184SGleb Smirnoff #ifdef DEVICE_POLLING
475275719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
475375719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
475475719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
475575719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
475638cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
475775719184SGleb Smirnoff 	} else
475875719184SGleb Smirnoff #endif
475975719184SGleb Smirnoff 
476095d67482SBill Paul 	/* Enable host interrupts. */
476175719184SGleb Smirnoff 	{
476295d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
476395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
476438cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
476575719184SGleb Smirnoff 	}
476695d67482SBill Paul 
476767d5e043SOleg Bulyzhin 	bge_ifmedia_upd_locked(ifp);
476895d67482SBill Paul 
476913f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
477013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
477195d67482SBill Paul 
47720f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
47730f9bd73bSSam Leffler }
47740f9bd73bSSam Leffler 
47750f9bd73bSSam Leffler static void
47763f74909aSGleb Smirnoff bge_init(void *xsc)
47770f9bd73bSSam Leffler {
47780f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
47790f9bd73bSSam Leffler 
47800f9bd73bSSam Leffler 	BGE_LOCK(sc);
47810f9bd73bSSam Leffler 	bge_init_locked(sc);
47820f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
478395d67482SBill Paul }
478495d67482SBill Paul 
478595d67482SBill Paul /*
478695d67482SBill Paul  * Set media options.
478795d67482SBill Paul  */
478895d67482SBill Paul static int
47893f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
479095d67482SBill Paul {
479167d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
479267d5e043SOleg Bulyzhin 	int res;
479367d5e043SOleg Bulyzhin 
479467d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
479567d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
479667d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
479767d5e043SOleg Bulyzhin 
479867d5e043SOleg Bulyzhin 	return (res);
479967d5e043SOleg Bulyzhin }
480067d5e043SOleg Bulyzhin 
480167d5e043SOleg Bulyzhin static int
480267d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
480367d5e043SOleg Bulyzhin {
480467d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
480595d67482SBill Paul 	struct mii_data *mii;
48064f09c4c7SMarius Strobl 	struct mii_softc *miisc;
480795d67482SBill Paul 	struct ifmedia *ifm;
480895d67482SBill Paul 
480967d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
481067d5e043SOleg Bulyzhin 
481195d67482SBill Paul 	ifm = &sc->bge_ifmedia;
481295d67482SBill Paul 
481395d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
4814652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
481595d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
481695d67482SBill Paul 			return (EINVAL);
481795d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
481895d67482SBill Paul 		case IFM_AUTO:
4819ff50922bSDoug White 			/*
4820ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
4821ff50922bSDoug White 			 * mechanism for programming the autoneg
4822ff50922bSDoug White 			 * advertisement registers in TBI mode.
4823ff50922bSDoug White 			 */
48240f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4825ff50922bSDoug White 				uint32_t sgdig;
48260f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
48270f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
4828ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
4829ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
4830ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
4831ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
4832ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
4833ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
4834ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
4835ff50922bSDoug White 					DELAY(5);
4836ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
4837ff50922bSDoug White 				}
48380f89fde2SJung-uk Kim 			}
483995d67482SBill Paul 			break;
484095d67482SBill Paul 		case IFM_1000_SX:
484195d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
484295d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
484395d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
484495d67482SBill Paul 			} else {
484595d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
484695d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
484795d67482SBill Paul 			}
484895d67482SBill Paul 			break;
484995d67482SBill Paul 		default:
485095d67482SBill Paul 			return (EINVAL);
485195d67482SBill Paul 		}
485295d67482SBill Paul 		return (0);
485395d67482SBill Paul 	}
485495d67482SBill Paul 
48551493e883SOleg Bulyzhin 	sc->bge_link_evt++;
485695d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
48574f09c4c7SMarius Strobl 	if (mii->mii_instance)
48584f09c4c7SMarius Strobl 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
485995d67482SBill Paul 			mii_phy_reset(miisc);
486095d67482SBill Paul 	mii_mediachg(mii);
486195d67482SBill Paul 
4862902827f6SBjoern A. Zeeb 	/*
4863902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
4864902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
4865902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
4866902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
4867902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
4868902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
4869902827f6SBjoern A. Zeeb 	 * get an RX intr.
4870902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
4871902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
4872902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
4873902827f6SBjoern A. Zeeb 	 */
48744f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
48754f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
4876902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
48774f0794ffSBjoern A. Zeeb 	else
487863ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
4879902827f6SBjoern A. Zeeb 
488095d67482SBill Paul 	return (0);
488195d67482SBill Paul }
488295d67482SBill Paul 
488395d67482SBill Paul /*
488495d67482SBill Paul  * Report current media status.
488595d67482SBill Paul  */
488695d67482SBill Paul static void
48873f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
488895d67482SBill Paul {
488967d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
489095d67482SBill Paul 	struct mii_data *mii;
489195d67482SBill Paul 
489267d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
489395d67482SBill Paul 
4894652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
489595d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
489695d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
489795d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
489895d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
489995d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
49004c0da0ffSGleb Smirnoff 		else {
49014c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
490267d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
49034c0da0ffSGleb Smirnoff 			return;
49044c0da0ffSGleb Smirnoff 		}
490595d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
490695d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
490795d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
490895d67482SBill Paul 		else
490995d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
491067d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
491195d67482SBill Paul 		return;
491295d67482SBill Paul 	}
491395d67482SBill Paul 
491495d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
491595d67482SBill Paul 	mii_pollstat(mii);
491695d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
491795d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
491867d5e043SOleg Bulyzhin 
491967d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
492095d67482SBill Paul }
492195d67482SBill Paul 
492295d67482SBill Paul static int
49233f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
492495d67482SBill Paul {
492595d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
492695d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
492795d67482SBill Paul 	struct mii_data *mii;
4928f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
492995d67482SBill Paul 
493095d67482SBill Paul 	switch (command) {
493195d67482SBill Paul 	case SIOCSIFMTU:
4932*f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
4933*f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
49344c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
4935*f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
493695d67482SBill Paul 				error = EINVAL;
4937*f5459d4cSPyun YongHyeon 				break;
4938*f5459d4cSPyun YongHyeon 			}
4939*f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
4940*f5459d4cSPyun YongHyeon 			error = EINVAL;
4941*f5459d4cSPyun YongHyeon 			break;
4942*f5459d4cSPyun YongHyeon 		}
4943*f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
4944*f5459d4cSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
494595d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
49463a429c8fSPyun YongHyeon 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
494713f4c340SRobert Watson 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
49483a429c8fSPyun YongHyeon 				bge_init_locked(sc);
494995d67482SBill Paul 			}
49503a429c8fSPyun YongHyeon 		}
49513a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
495295d67482SBill Paul 		break;
495395d67482SBill Paul 	case SIOCSIFFLAGS:
49540f9bd73bSSam Leffler 		BGE_LOCK(sc);
495595d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
495695d67482SBill Paul 			/*
495795d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
495895d67482SBill Paul 			 * then just use the 'set promisc mode' command
495995d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
496095d67482SBill Paul 			 * a full re-init means reloading the firmware and
496195d67482SBill Paul 			 * waiting for it to start up, which may take a
4962d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
496395d67482SBill Paul 			 */
4964f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4965f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
49663e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
49673e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
4968f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
4969d183af7fSRuslan Ermilov 					bge_setmulti(sc);
497095d67482SBill Paul 			} else
49710f9bd73bSSam Leffler 				bge_init_locked(sc);
497295d67482SBill Paul 		} else {
497313f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
497495d67482SBill Paul 				bge_stop(sc);
497595d67482SBill Paul 			}
497695d67482SBill Paul 		}
497795d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
49780f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
497995d67482SBill Paul 		error = 0;
498095d67482SBill Paul 		break;
498195d67482SBill Paul 	case SIOCADDMULTI:
498295d67482SBill Paul 	case SIOCDELMULTI:
498313f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
49840f9bd73bSSam Leffler 			BGE_LOCK(sc);
498595d67482SBill Paul 			bge_setmulti(sc);
49860f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
498795d67482SBill Paul 			error = 0;
498895d67482SBill Paul 		}
498995d67482SBill Paul 		break;
499095d67482SBill Paul 	case SIOCSIFMEDIA:
499195d67482SBill Paul 	case SIOCGIFMEDIA:
4992652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
499395d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
499495d67482SBill Paul 			    &sc->bge_ifmedia, command);
499595d67482SBill Paul 		} else {
499695d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
499795d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
499895d67482SBill Paul 			    &mii->mii_media, command);
499995d67482SBill Paul 		}
500095d67482SBill Paul 		break;
500195d67482SBill Paul 	case SIOCSIFCAP:
500295d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
500375719184SGleb Smirnoff #ifdef DEVICE_POLLING
500475719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
500575719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
500675719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
500775719184SGleb Smirnoff 				if (error)
500875719184SGleb Smirnoff 					return (error);
500975719184SGleb Smirnoff 				BGE_LOCK(sc);
501075719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
501175719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
501238cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
501375719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
501475719184SGleb Smirnoff 				BGE_UNLOCK(sc);
501575719184SGleb Smirnoff 			} else {
501675719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
501775719184SGleb Smirnoff 				/* Enable interrupt even in error case */
501875719184SGleb Smirnoff 				BGE_LOCK(sc);
501975719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
502075719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
502138cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
502275719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
502375719184SGleb Smirnoff 				BGE_UNLOCK(sc);
502475719184SGleb Smirnoff 			}
502575719184SGleb Smirnoff 		}
502675719184SGleb Smirnoff #endif
5027d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5028d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
5029d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
5030d8b57f98SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
503135f945cdSPyun YongHyeon 				ifp->if_hwassist |= sc->bge_csum_features;
503295d67482SBill Paul 			else
503335f945cdSPyun YongHyeon 				ifp->if_hwassist &= ~sc->bge_csum_features;
503495d67482SBill Paul 		}
5035cb2eacc7SYaroslav Tykhiy 
5036d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5037d8b57f98SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
5038d8b57f98SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
5039d8b57f98SPyun YongHyeon 
5040ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5041ca3f1187SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
5042ca3f1187SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
5043ca3f1187SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
5044ca3f1187SPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
5045ca3f1187SPyun YongHyeon 			else
5046ca3f1187SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
5047ca3f1187SPyun YongHyeon 		}
5048ca3f1187SPyun YongHyeon 
5049cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5050cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
5051cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5052cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5053cb2eacc7SYaroslav Tykhiy 		}
5054cb2eacc7SYaroslav Tykhiy 
505504bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
505604bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
505704bde852SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
505804bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
505904bde852SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
5060cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
506104bde852SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
506204bde852SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
5063cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5064cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5065cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
506604bde852SPyun YongHyeon 		}
5067cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5068cb2eacc7SYaroslav Tykhiy 		VLAN_CAPABILITIES(ifp);
5069cb2eacc7SYaroslav Tykhiy #endif
507095d67482SBill Paul 		break;
507195d67482SBill Paul 	default:
5072673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
507395d67482SBill Paul 		break;
507495d67482SBill Paul 	}
507595d67482SBill Paul 
507695d67482SBill Paul 	return (error);
507795d67482SBill Paul }
507895d67482SBill Paul 
507995d67482SBill Paul static void
5080b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
508195d67482SBill Paul {
5082b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
508395d67482SBill Paul 
5084b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5085b74e67fbSGleb Smirnoff 
5086b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5087b74e67fbSGleb Smirnoff 		return;
5088b74e67fbSGleb Smirnoff 
5089b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
509095d67482SBill Paul 
5091fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
509295d67482SBill Paul 
509313f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5094426742bfSGleb Smirnoff 	bge_init_locked(sc);
509595d67482SBill Paul 
509695d67482SBill Paul 	ifp->if_oerrors++;
509795d67482SBill Paul }
509895d67482SBill Paul 
509995d67482SBill Paul /*
510095d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
510195d67482SBill Paul  * RX and TX lists.
510295d67482SBill Paul  */
510395d67482SBill Paul static void
51043f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
510595d67482SBill Paul {
510695d67482SBill Paul 	struct ifnet *ifp;
510795d67482SBill Paul 
51080f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
51090f9bd73bSSam Leffler 
5110fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
511195d67482SBill Paul 
51120f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
511395d67482SBill Paul 
511444b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
511544b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
511644b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
511744b63691SBjoern A. Zeeb 
511844b63691SBjoern A. Zeeb 	/*
511944b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
512044b63691SBjoern A. Zeeb 	 */
512144b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
512244b63691SBjoern A. Zeeb 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
512344b63691SBjoern A. Zeeb 
512495d67482SBill Paul 	/*
51253f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
512695d67482SBill Paul 	 */
512795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
512895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
512995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
51307ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
513195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
513295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
513395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
513495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
513595d67482SBill Paul 
513695d67482SBill Paul 	/*
51373f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
513895d67482SBill Paul 	 */
513995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
514095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
514195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
514295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
514395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
51447ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
514595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
514695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
514795d67482SBill Paul 
514895d67482SBill Paul 	/*
514995d67482SBill Paul 	 * Shut down all of the memory managers and related
515095d67482SBill Paul 	 * state machines.
515195d67482SBill Paul 	 */
515295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
515395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
51547ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
515595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
51560c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
515795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
51587ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
515995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
516095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
51610434d1b8SBill Paul 	}
51622280c16bSPyun YongHyeon 	/* Update MAC statistics. */
51632280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
51642280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
516595d67482SBill Paul 
51668cb1383cSDoug Ambrisko 	bge_reset(sc);
51678cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
51688cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
51698cb1383cSDoug Ambrisko 
51708cb1383cSDoug Ambrisko 	/*
51718cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
51728cb1383cSDoug Ambrisko 	 */
51738cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
51748cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
51758cb1383cSDoug Ambrisko 	else
517695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
517795d67482SBill Paul 
517895d67482SBill Paul 	/* Free the RX lists. */
517995d67482SBill Paul 	bge_free_rx_ring_std(sc);
518095d67482SBill Paul 
518195d67482SBill Paul 	/* Free jumbo RX list. */
51824c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
518395d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
518495d67482SBill Paul 
518595d67482SBill Paul 	/* Free TX buffers. */
518695d67482SBill Paul 	bge_free_tx_ring(sc);
518795d67482SBill Paul 
518895d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
518995d67482SBill Paul 
51905dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
51911493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
51921493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
51931493e883SOleg Bulyzhin 	sc->bge_link = 0;
519495d67482SBill Paul 
51951493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
519695d67482SBill Paul }
519795d67482SBill Paul 
519895d67482SBill Paul /*
519995d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
520095d67482SBill Paul  * get confused by errant DMAs when rebooting.
520195d67482SBill Paul  */
5202b6c974e8SWarner Losh static int
52033f74909aSGleb Smirnoff bge_shutdown(device_t dev)
520495d67482SBill Paul {
520595d67482SBill Paul 	struct bge_softc *sc;
520695d67482SBill Paul 
520795d67482SBill Paul 	sc = device_get_softc(dev);
52080f9bd73bSSam Leffler 	BGE_LOCK(sc);
520995d67482SBill Paul 	bge_stop(sc);
521095d67482SBill Paul 	bge_reset(sc);
52110f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
5212b6c974e8SWarner Losh 
5213b6c974e8SWarner Losh 	return (0);
521495d67482SBill Paul }
521514afefa3SPawel Jakub Dawidek 
521614afefa3SPawel Jakub Dawidek static int
521714afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
521814afefa3SPawel Jakub Dawidek {
521914afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
522014afefa3SPawel Jakub Dawidek 
522114afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
522214afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
522314afefa3SPawel Jakub Dawidek 	bge_stop(sc);
522414afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
522514afefa3SPawel Jakub Dawidek 
522614afefa3SPawel Jakub Dawidek 	return (0);
522714afefa3SPawel Jakub Dawidek }
522814afefa3SPawel Jakub Dawidek 
522914afefa3SPawel Jakub Dawidek static int
523014afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
523114afefa3SPawel Jakub Dawidek {
523214afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
523314afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
523414afefa3SPawel Jakub Dawidek 
523514afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
523614afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
523714afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
523814afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
523914afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
524014afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
524114afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
524214afefa3SPawel Jakub Dawidek 	}
524314afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
524414afefa3SPawel Jakub Dawidek 
524514afefa3SPawel Jakub Dawidek 	return (0);
524614afefa3SPawel Jakub Dawidek }
5247dab5cd05SOleg Bulyzhin 
5248dab5cd05SOleg Bulyzhin static void
52493f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
5250dab5cd05SOleg Bulyzhin {
52511f313773SOleg Bulyzhin 	struct mii_data *mii;
52521f313773SOleg Bulyzhin 	uint32_t link, status;
5253dab5cd05SOleg Bulyzhin 
5254dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
52551f313773SOleg Bulyzhin 
52563f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
52577b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
52587b97099dSOleg Bulyzhin 
5259dab5cd05SOleg Bulyzhin 	/*
5260dab5cd05SOleg Bulyzhin 	 * Process link state changes.
5261dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
5262dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
5263dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
5264dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
5265dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
5266dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
5267dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
5268dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
52691f313773SOleg Bulyzhin 	 *
52701f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
52714c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
5272dab5cd05SOleg Bulyzhin 	 */
5273dab5cd05SOleg Bulyzhin 
52741f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
52754c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
5276dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
5277dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
52781f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
52795dda8085SOleg Bulyzhin 			mii_pollstat(mii);
52801f313773SOleg Bulyzhin 			if (!sc->bge_link &&
52811f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
52821f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
52831f313773SOleg Bulyzhin 				sc->bge_link++;
52841f313773SOleg Bulyzhin 				if (bootverbose)
52851f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
52861f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
52871f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
52881f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
52891f313773SOleg Bulyzhin 				sc->bge_link = 0;
52901f313773SOleg Bulyzhin 				if (bootverbose)
52911f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
52921f313773SOleg Bulyzhin 			}
52931f313773SOleg Bulyzhin 
52943f74909aSGleb Smirnoff 			/* Clear the interrupt. */
5295dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
5296dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
5297dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
5298dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
5299dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
5300dab5cd05SOleg Bulyzhin 		}
5301dab5cd05SOleg Bulyzhin 		return;
5302dab5cd05SOleg Bulyzhin 	}
5303dab5cd05SOleg Bulyzhin 
5304652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
53051f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
53067b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
53077b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
53081f313773SOleg Bulyzhin 				sc->bge_link++;
53091f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
53101f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
53111f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
53120c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
53131f313773SOleg Bulyzhin 				if (bootverbose)
53141f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
53153f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
53163f74909aSGleb Smirnoff 				    LINK_STATE_UP);
53177b97099dSOleg Bulyzhin 			}
53181f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
5319dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
53201f313773SOleg Bulyzhin 			if (bootverbose)
53211f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
53227b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
53231f313773SOleg Bulyzhin 		}
53246ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
53251f313773SOleg Bulyzhin 		/*
53260c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
53270c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
53280c8aa4eaSJung-uk Kim 		 * PHY link status directly.
53291f313773SOleg Bulyzhin 		 */
53301f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
53311f313773SOleg Bulyzhin 
53321f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
53331f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
53341f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
53355dda8085SOleg Bulyzhin 			mii_pollstat(mii);
53361f313773SOleg Bulyzhin 			if (!sc->bge_link &&
53371f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
53381f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
53391f313773SOleg Bulyzhin 				sc->bge_link++;
53401f313773SOleg Bulyzhin 				if (bootverbose)
53411f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
53421f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
53431f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
53441f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
53451f313773SOleg Bulyzhin 				sc->bge_link = 0;
53461f313773SOleg Bulyzhin 				if (bootverbose)
53471f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
53481f313773SOleg Bulyzhin 			}
53491f313773SOleg Bulyzhin 		}
53500c8aa4eaSJung-uk Kim 	} else {
53510c8aa4eaSJung-uk Kim 		/*
53526ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
53536ede2cfaSPyun YongHyeon 		 * link status.
53540c8aa4eaSJung-uk Kim 		 */
53556ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
53566ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
53576ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
5358dab5cd05SOleg Bulyzhin 	}
5359dab5cd05SOleg Bulyzhin 
53603f74909aSGleb Smirnoff 	/* Clear the attention. */
5361dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
5362dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
5363dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
5364dab5cd05SOleg Bulyzhin }
53656f8718a3SScott Long 
53666f8718a3SScott Long static void
53676f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
53686f8718a3SScott Long {
53696f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
53702280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
53717e32f79aSPyun YongHyeon 	char tn[32];
53727e32f79aSPyun YongHyeon 	int unit;
53736f8718a3SScott Long 
53746f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
53756f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
53766f8718a3SScott Long 
53776f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
53786f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
53796f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
53806f8718a3SScott Long 	    "Debug Information");
53816f8718a3SScott Long 
53826f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
53836f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
53846f8718a3SScott Long 	    "Register Read");
53856f8718a3SScott Long 
53866f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
53876f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
53886f8718a3SScott Long 	    "Memory Read");
53896f8718a3SScott Long 
53906f8718a3SScott Long #endif
5391763757b2SScott Long 
53927e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
5393beaa2ae1SPyun YongHyeon 	/*
5394beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
5395beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
5396beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
5397beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
5398beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
5399beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
5400beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
5401beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
5402beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
5403beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
5404beaa2ae1SPyun YongHyeon 	 */
54057e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
54067e32f79aSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.forced_collapse", unit);
54077e32f79aSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_collapse);
5408beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
5409beaa2ae1SPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_collapse, 0,
5410beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
5411beaa2ae1SPyun YongHyeon 	    "forced collapsing");
5412beaa2ae1SPyun YongHyeon 
541335f945cdSPyun YongHyeon 	/*
541435f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
541535f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
541635f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
541735f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
541835f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
541935f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
542035f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
542135f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
542235f945cdSPyun YongHyeon 	 */
542335f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
542435f945cdSPyun YongHyeon 	snprintf(tn, sizeof(tn), "dev.bge.%d.bge_forced_udpcsum", unit);
542535f945cdSPyun YongHyeon 	TUNABLE_INT_FETCH(tn, &sc->bge_forced_udpcsum);
542635f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
542735f945cdSPyun YongHyeon 	    CTLFLAG_RW, &sc->bge_forced_udpcsum, 0,
542835f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
542935f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
543035f945cdSPyun YongHyeon 
5431d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
54322280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
54332280c16bSPyun YongHyeon 	else
54342280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
54352280c16bSPyun YongHyeon }
5436d949071dSJung-uk Kim 
54372280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
54382280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
54392280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
54402280c16bSPyun YongHyeon 	    desc)
54412280c16bSPyun YongHyeon 
54422280c16bSPyun YongHyeon static void
54432280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
54442280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
54452280c16bSPyun YongHyeon {
54462280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
54472280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
54482280c16bSPyun YongHyeon 
54492280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
5450763757b2SScott Long 	    NULL, "BGE Statistics");
5451763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
5452763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
5453763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
5454763757b2SScott Long 	    "FramesDroppedDueToFilters");
5455763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
5456763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
5457763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
5458763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
5459763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
5460763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
546106e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
546206e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
546306e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
546406e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
5465763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
5466763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
5467763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
5468763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
5469763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
5470763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
5471763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
5472763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
5473763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
5474763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
5475763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
5476763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
5477763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
5478763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
5479763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
5480763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
5481763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
5482763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
5483763757b2SScott Long 
5484763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
5485763757b2SScott Long 	    NULL, "BGE RX Statistics");
5486763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
5487763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
54881cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
5489763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
5490763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
5491763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
54921cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
5493763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
5494763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
5495763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
5496763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
5497763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
5498763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
5499763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
5500763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
5501763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
5502763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
5503763757b2SScott Long 	    "xoffPauseFramesReceived");
5504763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
5505763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
5506763757b2SScott Long 	    "ControlFramesReceived");
5507763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
5508763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
5509763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
5510763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
5511763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
5512763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
5513763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
5514763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
5515763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
551606e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
5517763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
551806e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
5519763757b2SScott Long 
5520763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
5521763757b2SScott Long 	    NULL, "BGE TX Statistics");
5522763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
5523763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
55241cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
5525763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
5526763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
5527763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
5528763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
5529763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
5530763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
5531763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
5532763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
5533763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
5534763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
5535763757b2SScott Long 	    "InternalMacTransmitErrors");
5536763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
5537763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
5538763757b2SScott Long 	    "SingleCollisionFrames");
5539763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
5540763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
5541763757b2SScott Long 	    "MultipleCollisionFrames");
5542763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
5543763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
5544763757b2SScott Long 	    "DeferredTransmissions");
5545763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
5546763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
5547763757b2SScott Long 	    "ExcessiveCollisions");
5548763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
554906e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
555006e83c7eSScott Long 	    "LateCollisions");
5551763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
55521cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
5553763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
5554763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
5555763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
5556763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
5557763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
5558763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
5559763757b2SScott Long 	    "CarrierSenseErrors");
5560763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
5561763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
5562763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
5563763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
5564763757b2SScott Long }
5565763757b2SScott Long 
55662280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
55672280c16bSPyun YongHyeon 
55682280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
55696dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
55702280c16bSPyun YongHyeon 
55712280c16bSPyun YongHyeon static void
55722280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
55732280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
55742280c16bSPyun YongHyeon {
55752280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
55762280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
55772280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
55782280c16bSPyun YongHyeon 
55792280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
55802280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
55812280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
55822280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
55832280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
55842280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
55852280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
55862280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
55872280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
55882280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
55892280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
55902280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
55912280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
55922280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
55932280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
55942280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
55952280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
55962280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
55972280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
55982280c16bSPyun YongHyeon 
55992280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
56002280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
56012280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
56022280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
56032280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
56042280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
56052280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
56061cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
56072280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
56082280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
56092280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
56102280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
56112280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
56122280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
56132280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
56142280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
56152280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
56162280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
56172280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
56182280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
56192280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
56202280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
56212280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
56222280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
56232280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
56242280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
56252280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
56262280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
56272280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
56282280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
56292280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
56302280c16bSPyun YongHyeon 
56312280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
56322280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
56332280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
56341cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
56352280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
56362280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
56372280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
56382280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
56392280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
56402280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
56412280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
56422280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
56432280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
56442280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
56452280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
56462280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
56472280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
56482280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
56492280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
56502280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
56512280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
56522280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
56532280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
56542280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
56552280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
56561cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
56572280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
56581cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
56592280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
56601cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
56612280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
56622280c16bSPyun YongHyeon }
56632280c16bSPyun YongHyeon 
56642280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
56652280c16bSPyun YongHyeon 
5666763757b2SScott Long static int
5667763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
5668763757b2SScott Long {
5669763757b2SScott Long 	struct bge_softc *sc;
567006e83c7eSScott Long 	uint32_t result;
5671d949071dSJung-uk Kim 	int offset;
5672763757b2SScott Long 
5673763757b2SScott Long 	sc = (struct bge_softc *)arg1;
5674763757b2SScott Long 	offset = arg2;
5675d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
5676d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
5677041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
56786f8718a3SScott Long }
56796f8718a3SScott Long 
56806f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
56816f8718a3SScott Long static int
56826f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
56836f8718a3SScott Long {
56846f8718a3SScott Long 	struct bge_softc *sc;
56856f8718a3SScott Long 	uint16_t *sbdata;
56866f8718a3SScott Long 	int error;
56876f8718a3SScott Long 	int result;
56886f8718a3SScott Long 	int i, j;
56896f8718a3SScott Long 
56906f8718a3SScott Long 	result = -1;
56916f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
56926f8718a3SScott Long 	if (error || (req->newptr == NULL))
56936f8718a3SScott Long 		return (error);
56946f8718a3SScott Long 
56956f8718a3SScott Long 	if (result == 1) {
56966f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
56976f8718a3SScott Long 
56986f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
56996f8718a3SScott Long 		printf("Status Block:\n");
57006f8718a3SScott Long 		for (i = 0x0; i < (BGE_STATUS_BLK_SZ / 4); ) {
57016f8718a3SScott Long 			printf("%06x:", i);
57026f8718a3SScott Long 			for (j = 0; j < 8; j++) {
57036f8718a3SScott Long 				printf(" %04x", sbdata[i]);
57046f8718a3SScott Long 				i += 4;
57056f8718a3SScott Long 			}
57066f8718a3SScott Long 			printf("\n");
57076f8718a3SScott Long 		}
57086f8718a3SScott Long 
57096f8718a3SScott Long 		printf("Registers:\n");
57100c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
57116f8718a3SScott Long 			printf("%06x:", i);
57126f8718a3SScott Long 			for (j = 0; j < 8; j++) {
57136f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
57146f8718a3SScott Long 				i += 4;
57156f8718a3SScott Long 			}
57166f8718a3SScott Long 			printf("\n");
57176f8718a3SScott Long 		}
57186f8718a3SScott Long 
57196f8718a3SScott Long 		printf("Hardware Flags:\n");
5720a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
5721a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
57225345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
57236f8718a3SScott Long 			printf(" - 575X Plus\n");
57245345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
57256f8718a3SScott Long 			printf(" - 5705 Plus\n");
57265345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
57275345bad0SScott Long 			printf(" - 5714 Family\n");
57285345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
57295345bad0SScott Long 			printf(" - 5700 Family\n");
57306f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
57316f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
57326f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
57336f8718a3SScott Long 			printf(" - PCI-X Bus\n");
57346f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
57356f8718a3SScott Long 			printf(" - PCI Express Bus\n");
57367d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
57376f8718a3SScott Long 			printf(" - No 3 LEDs\n");
57386f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
57396f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
57406f8718a3SScott Long 	}
57416f8718a3SScott Long 
57426f8718a3SScott Long 	return (error);
57436f8718a3SScott Long }
57446f8718a3SScott Long 
57456f8718a3SScott Long static int
57466f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
57476f8718a3SScott Long {
57486f8718a3SScott Long 	struct bge_softc *sc;
57496f8718a3SScott Long 	int error;
57506f8718a3SScott Long 	uint16_t result;
57516f8718a3SScott Long 	uint32_t val;
57526f8718a3SScott Long 
57536f8718a3SScott Long 	result = -1;
57546f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
57556f8718a3SScott Long 	if (error || (req->newptr == NULL))
57566f8718a3SScott Long 		return (error);
57576f8718a3SScott Long 
57586f8718a3SScott Long 	if (result < 0x8000) {
57596f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
57606f8718a3SScott Long 		val = CSR_READ_4(sc, result);
57616f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
57626f8718a3SScott Long 	}
57636f8718a3SScott Long 
57646f8718a3SScott Long 	return (error);
57656f8718a3SScott Long }
57666f8718a3SScott Long 
57676f8718a3SScott Long static int
57686f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
57696f8718a3SScott Long {
57706f8718a3SScott Long 	struct bge_softc *sc;
57716f8718a3SScott Long 	int error;
57726f8718a3SScott Long 	uint16_t result;
57736f8718a3SScott Long 	uint32_t val;
57746f8718a3SScott Long 
57756f8718a3SScott Long 	result = -1;
57766f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
57776f8718a3SScott Long 	if (error || (req->newptr == NULL))
57786f8718a3SScott Long 		return (error);
57796f8718a3SScott Long 
57806f8718a3SScott Long 	if (result < 0x8000) {
57816f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
57826f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
57836f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
57846f8718a3SScott Long 	}
57856f8718a3SScott Long 
57866f8718a3SScott Long 	return (error);
57876f8718a3SScott Long }
57886f8718a3SScott Long #endif
578938cc658fSJohn Baldwin 
579038cc658fSJohn Baldwin static int
57915fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
57925fea260fSMarius Strobl {
57935fea260fSMarius Strobl 
57945fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
57955fea260fSMarius Strobl 		return (1);
57965fea260fSMarius Strobl 
57975fea260fSMarius Strobl #ifdef __sparc64__
57985fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
57995fea260fSMarius Strobl 	return (0);
58005fea260fSMarius Strobl #endif
58015fea260fSMarius Strobl 	return (1);
58025fea260fSMarius Strobl }
58035fea260fSMarius Strobl 
58045fea260fSMarius Strobl static int
580538cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
580638cc658fSJohn Baldwin {
580738cc658fSJohn Baldwin 	uint32_t mac_addr;
580838cc658fSJohn Baldwin 
580938cc658fSJohn Baldwin 	mac_addr = bge_readmem_ind(sc, 0x0c14);
581038cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
581138cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
581238cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
581338cc658fSJohn Baldwin 		mac_addr = bge_readmem_ind(sc, 0x0c18);
581438cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
581538cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
581638cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
581738cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
58185fea260fSMarius Strobl 		return (0);
581938cc658fSJohn Baldwin 	}
58205fea260fSMarius Strobl 	return (1);
582138cc658fSJohn Baldwin }
582238cc658fSJohn Baldwin 
582338cc658fSJohn Baldwin static int
582438cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
582538cc658fSJohn Baldwin {
582638cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
582738cc658fSJohn Baldwin 
582838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
582938cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
583038cc658fSJohn Baldwin 
58315fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
58325fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
583338cc658fSJohn Baldwin }
583438cc658fSJohn Baldwin 
583538cc658fSJohn Baldwin static int
583638cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
583738cc658fSJohn Baldwin {
583838cc658fSJohn Baldwin 
58395fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
58405fea260fSMarius Strobl 		return (1);
58415fea260fSMarius Strobl 
58425fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
58435fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
584438cc658fSJohn Baldwin }
584538cc658fSJohn Baldwin 
584638cc658fSJohn Baldwin static int
584738cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
584838cc658fSJohn Baldwin {
584938cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
585038cc658fSJohn Baldwin 		/* NOTE: Order is critical */
58515fea260fSMarius Strobl 		bge_get_eaddr_fw,
585238cc658fSJohn Baldwin 		bge_get_eaddr_mem,
585338cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
585438cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
585538cc658fSJohn Baldwin 		NULL
585638cc658fSJohn Baldwin 	};
585738cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
585838cc658fSJohn Baldwin 
585938cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
586038cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
586138cc658fSJohn Baldwin 			break;
586238cc658fSJohn Baldwin 	}
586338cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
586438cc658fSJohn Baldwin }
5865