xref: /freebsd/sys/dev/bge/if_bge.c (revision 3ca4a3390356d72ae4f3be9400b23c52279694e6)
1098ca2bdSWarner Losh /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
495d67482SBill Paul  * Copyright (c) 2001 Wind River Systems
595d67482SBill Paul  * Copyright (c) 1997, 1998, 1999, 2001
695d67482SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
795d67482SBill Paul  *
895d67482SBill Paul  * Redistribution and use in source and binary forms, with or without
995d67482SBill Paul  * modification, are permitted provided that the following conditions
1095d67482SBill Paul  * are met:
1195d67482SBill Paul  * 1. Redistributions of source code must retain the above copyright
1295d67482SBill Paul  *    notice, this list of conditions and the following disclaimer.
1395d67482SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1495d67482SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1595d67482SBill Paul  *    documentation and/or other materials provided with the distribution.
1695d67482SBill Paul  * 3. All advertising materials mentioning features or use of this software
1795d67482SBill Paul  *    must display the following acknowledgement:
1895d67482SBill Paul  *	This product includes software developed by Bill Paul.
1995d67482SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
2095d67482SBill Paul  *    may be used to endorse or promote products derived from this software
2195d67482SBill Paul  *    without specific prior written permission.
2295d67482SBill Paul  *
2395d67482SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2495d67482SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2595d67482SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2695d67482SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2795d67482SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2895d67482SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2995d67482SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3095d67482SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3195d67482SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3295d67482SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3395d67482SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3495d67482SBill Paul  */
3595d67482SBill Paul 
36aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
37aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
38aad970f1SDavid E. O'Brien 
3995d67482SBill Paul /*
40d7acafa1SMarius Strobl  * Broadcom BCM57xx(x)/BCM590x NetXtreme and NetLink family Ethernet driver
4195d67482SBill Paul  *
4295d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
43d7acafa1SMarius Strobl  * Alteon Networks as part of the Tigon I and Tigon II Gigabit Ethernet
4422a4ecedSMarius Strobl  * MAC chips. The BCM5700, sometimes referred to as the Tigon III, has
4595d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4695d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4795d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
4895d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
4995d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
5095d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
5195d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
5295d67482SBill Paul  * into the driver.
5395d67482SBill Paul  *
5495d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5595d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5695d67482SBill Paul  *
5795d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
5898b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
5995d67482SBill Paul  * does not support external SSRAM.
6095d67482SBill Paul  *
6195d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
6295d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
6395d67482SBill Paul  *
6495d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6595d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6695d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6795d67482SBill Paul  * result, this driver does not implement any support for the mini RX
6895d67482SBill Paul  * ring.
6995d67482SBill Paul  */
7095d67482SBill Paul 
7175719184SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
7275719184SGleb Smirnoff #include "opt_device_polling.h"
7375719184SGleb Smirnoff #endif
7475719184SGleb Smirnoff 
7595d67482SBill Paul #include <sys/param.h>
76f41ac2beSBill Paul #include <sys/endian.h>
7795d67482SBill Paul #include <sys/systm.h>
7895d67482SBill Paul #include <sys/sockio.h>
7995d67482SBill Paul #include <sys/mbuf.h>
8095d67482SBill Paul #include <sys/malloc.h>
8195d67482SBill Paul #include <sys/kernel.h>
82fe12f24bSPoul-Henning Kamp #include <sys/module.h>
8395d67482SBill Paul #include <sys/socket.h>
84f1a7e6d5SScott Long #include <sys/sysctl.h>
85dfe0df9aSPyun YongHyeon #include <sys/taskqueue.h>
8695d67482SBill Paul 
877790c8c1SConrad Meyer #include <net/debugnet.h>
8895d67482SBill Paul #include <net/if.h>
8976039bc8SGleb Smirnoff #include <net/if_var.h>
9095d67482SBill Paul #include <net/if_arp.h>
9195d67482SBill Paul #include <net/ethernet.h>
9295d67482SBill Paul #include <net/if_dl.h>
9395d67482SBill Paul #include <net/if_media.h>
9495d67482SBill Paul 
9595d67482SBill Paul #include <net/bpf.h>
9695d67482SBill Paul 
9795d67482SBill Paul #include <net/if_types.h>
9895d67482SBill Paul #include <net/if_vlan_var.h>
9995d67482SBill Paul 
10095d67482SBill Paul #include <netinet/in_systm.h>
10195d67482SBill Paul #include <netinet/in.h>
10295d67482SBill Paul #include <netinet/ip.h>
103ca3f1187SPyun YongHyeon #include <netinet/tcp.h>
10495d67482SBill Paul 
10595d67482SBill Paul #include <machine/bus.h>
10695d67482SBill Paul #include <machine/resource.h>
10795d67482SBill Paul #include <sys/bus.h>
10895d67482SBill Paul #include <sys/rman.h>
10995d67482SBill Paul 
11095d67482SBill Paul #include <dev/mii/mii.h>
11195d67482SBill Paul #include <dev/mii/miivar.h>
1122d3ce713SDavid E. O'Brien #include "miidevs.h"
11395d67482SBill Paul #include <dev/mii/brgphyreg.h>
11495d67482SBill Paul 
1154fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1164fbd232cSWarner Losh #include <dev/pci/pcivar.h>
11795d67482SBill Paul 
11895d67482SBill Paul #include <dev/bge/if_bgereg.h>
11995d67482SBill Paul 
12035f945cdSPyun YongHyeon #define	BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP)
121d375e524SGleb Smirnoff #define	ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
12295d67482SBill Paul 
123f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
124f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
12595d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12695d67482SBill Paul 
1277b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
12895d67482SBill Paul #include "miibus_if.h"
12995d67482SBill Paul 
13095d67482SBill Paul /*
13195d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
13295d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
13395d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13495d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13595d67482SBill Paul  */
136852c67f9SMarius Strobl static const struct bge_type {
1374c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1384c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
13929658c96SDimitry Andric } bge_devs[] = {
1404c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5700 },
1414c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5701 },
14295d67482SBill Paul 
1434c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1000 },
1444c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1002 },
1454c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC9100 },
1464c0da0ffSGleb Smirnoff 
1474c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	APPLE_DEVICE_BCM5701 },
1484c0da0ffSGleb Smirnoff 
1494c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5700 },
1504c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5701 },
1514c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702 },
1524c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702_ALT },
1534c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702X },
1544c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703 },
1554c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703_ALT },
1564c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703X },
1574c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704C },
1584c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S },
1594c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S_ALT },
1604c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705 },
1614c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705F },
1624c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705K },
1634c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M },
1644c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M_ALT },
1654c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714C },
1664c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714S },
1674c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715 },
1684c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715S },
1691108273aSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5717 },
170cb2404b4SSepherosa Ziehau 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5717C },
1711108273aSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5718 },
172bbe2ca75SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5719 },
1734c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1744c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
175effef978SRemko Lodder 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5722 },
176a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5723 },
1772927f01fSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5725 },
1782927f01fSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5727 },
1794c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1804c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1814c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1824c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1834c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1844c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1854c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1864c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1874c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1884c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1899e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1909e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1919e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1929e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
193f7d1b2ebSXin LI 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5756 },
194a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761 },
195a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761E },
196a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761S },
197a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761SE },
1982927f01fSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5762 },
199a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5764 },
2004c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
2014c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
2024c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
2034c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
204a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5784 },
205a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785F },
206a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785G },
2079e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
2089e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
209a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787F },
2109e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
2114c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
2124c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
2134c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
2144c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
2154c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
21638cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906 },
21738cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906M },
218a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57760 },
219b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57761 },
220fe26ad88SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57762 },
22167129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57764 },
222b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57765 },
223fe26ad88SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57766 },
22467129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57767 },
225a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57780 },
226b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57781 },
22767129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57782 },
228b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57785 },
22967129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57786 },
23067129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57787 },
231a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57788 },
232a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57790 },
233b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57791 },
234b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57795 },
2354c0da0ffSGleb Smirnoff 
2364c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
2374c0da0ffSGleb Smirnoff 
2384c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
2394c0da0ffSGleb Smirnoff 
240a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE4 },
241a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE5 },
2424c0da0ffSGleb Smirnoff 	{ 0, 0 }
24395d67482SBill Paul };
24495d67482SBill Paul 
2454c0da0ffSGleb Smirnoff static const struct bge_vendor {
2464c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2474c0da0ffSGleb Smirnoff 	const char	*v_name;
24829658c96SDimitry Andric } bge_vendors[] = {
2494c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2504c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2514c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2524c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2534c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2544c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
255a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	"Fujitsu" },
2564c0da0ffSGleb Smirnoff 	{ 0, NULL }
2574c0da0ffSGleb Smirnoff };
2584c0da0ffSGleb Smirnoff 
2594c0da0ffSGleb Smirnoff static const struct bge_revision {
2604c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2614c0da0ffSGleb Smirnoff 	const char	*br_name;
26229658c96SDimitry Andric } bge_revisions[] = {
2634c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2644c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2654c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2664c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2674c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2684c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2694c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2704c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2714c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2724c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2734c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2744c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2754c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2764c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2774c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2784c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2799e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2804c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2814c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2824c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2834c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2844c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2854c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2864c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2874c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2884c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2894c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2904c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2914c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2924c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2934c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2944c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2954c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
29642787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2974c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2984c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2994c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
3004c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
3014c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
3024c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
3034c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
3044c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
3050c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5715_A3,	"BCM5715 A3" },
3061108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_A0,	"BCM5717 A0" },
3071108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_B0,	"BCM5717 B0" },
308cb2404b4SSepherosa Ziehau 	{ BGE_CHIPID_BCM5717_C0,	"BCM5717 C0" },
309bbe2ca75SPyun YongHyeon 	{ BGE_CHIPID_BCM5719_A0,	"BCM5719 A0" },
31050515680SPyun YongHyeon 	{ BGE_CHIPID_BCM5720_A0,	"BCM5720 A0" },
3110c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A0,	"BCM5755 A0" },
3120c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A1,	"BCM5755 A1" },
3130c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A2,	"BCM5755 A2" },
314bcc20328SJohn Baldwin 	{ BGE_CHIPID_BCM5722_A0,	"BCM5722 A0" },
315a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A0,	"BCM5761 A0" },
316a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A1,	"BCM5761 A1" },
3172927f01fSPyun YongHyeon 	{ BGE_CHIPID_BCM5762_A0,	"BCM5762 A0" },
318a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A0,	"BCM5784 A0" },
319a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A1,	"BCM5784 A1" },
32081179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3216f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
3226f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
3236f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
32438cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A1,	"BCM5906 A1" },
32538cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A2,	"BCM5906 A2" },
326b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_A0,	"BCM57765 A0" },
327b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_B0,	"BCM57765 B0" },
328a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A0,	"BCM57780 A0" },
329a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A1,	"BCM57780 A1" },
3304c0da0ffSGleb Smirnoff 	{ 0, NULL }
3314c0da0ffSGleb Smirnoff };
3324c0da0ffSGleb Smirnoff 
3334c0da0ffSGleb Smirnoff /*
3344c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
3354c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
3364c0da0ffSGleb Smirnoff  */
33729658c96SDimitry Andric static const struct bge_revision bge_majorrevs[] = {
3389e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
3399e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
3409e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
3419e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
3429e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
3439e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
3449e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
3459e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
3469e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
3479e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
3489e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
349a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5761,		"unknown BCM5761" },
350a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5784,		"unknown BCM5784" },
351a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5785,		"unknown BCM5785" },
35281179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3536f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
35438cc658fSJohn Baldwin 	{ BGE_ASICREV_BCM5906,		"unknown BCM5906" },
355b4a256acSPyun YongHyeon 	{ BGE_ASICREV_BCM57765,		"unknown BCM57765" },
356fe26ad88SPyun YongHyeon 	{ BGE_ASICREV_BCM57766,		"unknown BCM57766" },
357a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM57780,		"unknown BCM57780" },
3581108273aSPyun YongHyeon 	{ BGE_ASICREV_BCM5717,		"unknown BCM5717" },
359bbe2ca75SPyun YongHyeon 	{ BGE_ASICREV_BCM5719,		"unknown BCM5719" },
36050515680SPyun YongHyeon 	{ BGE_ASICREV_BCM5720,		"unknown BCM5720" },
3612927f01fSPyun YongHyeon 	{ BGE_ASICREV_BCM5762,		"unknown BCM5762" },
3624c0da0ffSGleb Smirnoff 	{ 0, NULL }
3634c0da0ffSGleb Smirnoff };
3644c0da0ffSGleb Smirnoff 
3650c8aa4eaSJung-uk Kim #define	BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
3660c8aa4eaSJung-uk Kim #define	BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
3670c8aa4eaSJung-uk Kim #define	BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
3680c8aa4eaSJung-uk Kim #define	BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
3690c8aa4eaSJung-uk Kim #define	BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
370a5779553SStanislav Sedov #define	BGE_IS_5755_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5755_PLUS)
3711108273aSPyun YongHyeon #define	BGE_IS_5717_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5717_PLUS)
372fe26ad88SPyun YongHyeon #define	BGE_IS_57765_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_57765_PLUS)
3734c0da0ffSGleb Smirnoff 
374d7acafa1SMarius Strobl static uint32_t bge_chipid(device_t);
375d7acafa1SMarius Strobl static const struct bge_vendor * bge_lookup_vendor(uint16_t);
376d7acafa1SMarius Strobl static const struct bge_revision * bge_lookup_rev(uint32_t);
37738cc658fSJohn Baldwin 
37838cc658fSJohn Baldwin typedef int	(*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
37938cc658fSJohn Baldwin 
380e51a25f8SAlfred Perlstein static int bge_probe(device_t);
381e51a25f8SAlfred Perlstein static int bge_attach(device_t);
382e51a25f8SAlfred Perlstein static int bge_detach(device_t);
38314afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
38414afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3853f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
386f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
3875b610048SPyun YongHyeon static int bge_dma_alloc(struct bge_softc *);
388f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
3895b610048SPyun YongHyeon static int bge_dma_ring_alloc(struct bge_softc *, bus_size_t, bus_size_t,
3905b610048SPyun YongHyeon     bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
391f41ac2beSBill Paul 
392ea9c3a30SPyun YongHyeon static void bge_devinfo(struct bge_softc *);
393062af0b0SPyun YongHyeon static int bge_mbox_reorder(struct bge_softc *);
394062af0b0SPyun YongHyeon 
3955fea260fSMarius Strobl static int bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[]);
39638cc658fSJohn Baldwin static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
39738cc658fSJohn Baldwin static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
39838cc658fSJohn Baldwin static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
39938cc658fSJohn Baldwin static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
40038cc658fSJohn Baldwin 
401b9c05fa5SPyun YongHyeon static void bge_txeof(struct bge_softc *, uint16_t);
4021108273aSPyun YongHyeon static void bge_rxcsum(struct bge_softc *, struct bge_rx_bd *, struct mbuf *);
403dfe0df9aSPyun YongHyeon static int bge_rxeof(struct bge_softc *, uint16_t, int);
40495d67482SBill Paul 
4058cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
406e51a25f8SAlfred Perlstein static void bge_tick(void *);
4072280c16bSPyun YongHyeon static void bge_stats_clear_regs(struct bge_softc *);
408e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
4093f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
410d598b626SPyun YongHyeon static struct mbuf *bge_check_short_dma(struct mbuf *);
4112e1d4df4SPyun YongHyeon static struct mbuf *bge_setup_tso(struct bge_softc *, struct mbuf *,
4121108273aSPyun YongHyeon     uint16_t *, uint16_t *);
413676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
41495d67482SBill Paul 
415e51a25f8SAlfred Perlstein static void bge_intr(void *);
416dfe0df9aSPyun YongHyeon static int bge_msi_intr(void *);
417dfe0df9aSPyun YongHyeon static void bge_intr_task(void *, int);
418fba8b109SMarcel Moolenaar static void bge_start(if_t);
419ded66962SMark Johnston static void bge_start_locked(if_t);
420ded66962SMark Johnston static void bge_start_tx(struct bge_softc *, uint32_t);
421fba8b109SMarcel Moolenaar static int bge_ioctl(if_t, u_long, caddr_t);
4220f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
423e51a25f8SAlfred Perlstein static void bge_init(void *);
4245a147ba6SPyun YongHyeon static void bge_stop_block(struct bge_softc *, bus_size_t, uint32_t);
425e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
426b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
427b6c974e8SWarner Losh static int bge_shutdown(device_t);
428fba8b109SMarcel Moolenaar static int bge_ifmedia_upd_locked(if_t);
429fba8b109SMarcel Moolenaar static int bge_ifmedia_upd(if_t);
430fba8b109SMarcel Moolenaar static void bge_ifmedia_sts(if_t, struct ifmediareq *);
431df360178SGleb Smirnoff static uint64_t bge_get_counter(if_t, ift_counter);
43295d67482SBill Paul 
43338cc658fSJohn Baldwin static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
43438cc658fSJohn Baldwin static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
43538cc658fSJohn Baldwin 
4363f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
437e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
43895d67482SBill Paul 
4393e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
440e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
441cb2eacc7SYaroslav Tykhiy static void bge_setvlan(struct bge_softc *);
44295d67482SBill Paul 
443e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_std(struct bge_softc *, int);
444e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_jumbo(struct bge_softc *, int);
445943787f3SPyun YongHyeon static int bge_newbuf_std(struct bge_softc *, int);
446943787f3SPyun YongHyeon static int bge_newbuf_jumbo(struct bge_softc *, int);
447e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
448e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
449e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
450e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
451e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
452e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
45395d67482SBill Paul 
454e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
455e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
45650515680SPyun YongHyeon static uint32_t bge_dma_swap_options(struct bge_softc *);
45795d67482SBill Paul 
4585fea260fSMarius Strobl static int bge_has_eaddr(struct bge_softc *);
4593f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
460e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
46138cc658fSJohn Baldwin static void bge_writembx(struct bge_softc *, int, int);
46295d67482SBill Paul #ifdef notdef
4633f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
46495d67482SBill Paul #endif
4659ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
466e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
46795d67482SBill Paul 
468e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
469e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
470e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
47175719184SGleb Smirnoff #ifdef DEVICE_POLLING
472fba8b109SMarcel Moolenaar static int bge_poll(if_t ifp, enum poll_cmd cmd, int count);
47375719184SGleb Smirnoff #endif
47495d67482SBill Paul 
475548c8f1aSPyun YongHyeon #define	BGE_RESET_SHUTDOWN	0
4768cb1383cSDoug Ambrisko #define	BGE_RESET_START		1
477548c8f1aSPyun YongHyeon #define	BGE_RESET_SUSPEND	2
4788cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
4798cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
4808cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
481797ab05eSPyun YongHyeon static void bge_stop_fw(struct bge_softc *);
4828cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
483dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
48495d67482SBill Paul 
485548c8f1aSPyun YongHyeon static void bge_ape_lock_init(struct bge_softc *);
486548c8f1aSPyun YongHyeon static void bge_ape_read_fw_ver(struct bge_softc *);
487548c8f1aSPyun YongHyeon static int bge_ape_lock(struct bge_softc *, int);
488548c8f1aSPyun YongHyeon static void bge_ape_unlock(struct bge_softc *, int);
489548c8f1aSPyun YongHyeon static void bge_ape_send_event(struct bge_softc *, uint32_t);
490548c8f1aSPyun YongHyeon static void bge_ape_driver_state_change(struct bge_softc *, int);
491548c8f1aSPyun YongHyeon 
4926f8718a3SScott Long /*
4936f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
4946f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
4956f8718a3SScott Long  * traps on certain architectures.
4966f8718a3SScott Long  */
4976f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
4986f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
4996f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
500548c8f1aSPyun YongHyeon static int bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS);
5016f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
5026f8718a3SScott Long #endif
5036f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
5042280c16bSPyun YongHyeon static void bge_add_sysctl_stats_regs(struct bge_softc *,
5052280c16bSPyun YongHyeon     struct sysctl_ctx_list *, struct sysctl_oid_list *);
5062280c16bSPyun YongHyeon static void bge_add_sysctl_stats(struct bge_softc *, struct sysctl_ctx_list *,
5072280c16bSPyun YongHyeon     struct sysctl_oid_list *);
508763757b2SScott Long static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
5096f8718a3SScott Long 
5107790c8c1SConrad Meyer DEBUGNET_DEFINE(bge);
511ded66962SMark Johnston 
51295d67482SBill Paul static device_method_t bge_methods[] = {
51395d67482SBill Paul 	/* Device interface */
51495d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
51595d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
51695d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
51795d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
51814afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
51914afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
52095d67482SBill Paul 
52195d67482SBill Paul 	/* MII interface */
52295d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
52395d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
52495d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
52595d67482SBill Paul 
5264b7ec270SMarius Strobl 	DEVMETHOD_END
52795d67482SBill Paul };
52895d67482SBill Paul 
52995d67482SBill Paul static driver_t bge_driver = {
53095d67482SBill Paul 	"bge",
53195d67482SBill Paul 	bge_methods,
53295d67482SBill Paul 	sizeof(struct bge_softc)
53395d67482SBill Paul };
53495d67482SBill Paul 
535d628b8c9SJohn Baldwin DRIVER_MODULE(bge, pci, bge_driver, 0, 0);
536ea72f463SWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device", pci, bge, bge_devs,
537329e817fSWarner Losh     nitems(bge_devs) - 1);
5383e38757dSJohn Baldwin DRIVER_MODULE(miibus, bge, miibus_driver, 0, 0);
53995d67482SBill Paul 
540f1a7e6d5SScott Long static int bge_allow_asf = 1;
541f1a7e6d5SScott Long 
5427029da5cSPawel Biernacki static SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
5437029da5cSPawel Biernacki     "BGE driver parameters");
544af3b2549SHans Petter Selasky SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RDTUN, &bge_allow_asf, 0,
545f1a7e6d5SScott Long 	"Allow ASF mode if available");
546c4529f41SMichael Reifenberger 
54708013fd3SMarius Strobl static int
5485fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
54908013fd3SMarius Strobl {
55008013fd3SMarius Strobl 	return (1);
55108013fd3SMarius Strobl }
55208013fd3SMarius Strobl 
5533f74909aSGleb Smirnoff static uint32_t
5543f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
55595d67482SBill Paul {
55695d67482SBill Paul 	device_t dev;
5576f8718a3SScott Long 	uint32_t val;
55895d67482SBill Paul 
559a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
560a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
561a4431ebaSPyun YongHyeon 		return (0);
562a4431ebaSPyun YongHyeon 
56395d67482SBill Paul 	dev = sc->bge_dev;
56495d67482SBill Paul 
56595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
5666f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
5676f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
5686f8718a3SScott Long 	return (val);
56995d67482SBill Paul }
57095d67482SBill Paul 
57195d67482SBill Paul static void
5723f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
57395d67482SBill Paul {
57495d67482SBill Paul 	device_t dev;
57595d67482SBill Paul 
576a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
577a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
578a4431ebaSPyun YongHyeon 		return;
579a4431ebaSPyun YongHyeon 
58095d67482SBill Paul 	dev = sc->bge_dev;
58195d67482SBill Paul 
58295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
58395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
5846f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
58595d67482SBill Paul }
58695d67482SBill Paul 
58795d67482SBill Paul #ifdef notdef
5883f74909aSGleb Smirnoff static uint32_t
5893f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
59095d67482SBill Paul {
59195d67482SBill Paul 	device_t dev;
59295d67482SBill Paul 
59395d67482SBill Paul 	dev = sc->bge_dev;
59495d67482SBill Paul 
59595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
59695d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
59795d67482SBill Paul }
59895d67482SBill Paul #endif
59995d67482SBill Paul 
60095d67482SBill Paul static void
6013f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
60295d67482SBill Paul {
60395d67482SBill Paul 	device_t dev;
60495d67482SBill Paul 
60595d67482SBill Paul 	dev = sc->bge_dev;
60695d67482SBill Paul 
60795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
60895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
60995d67482SBill Paul }
61095d67482SBill Paul 
6116f8718a3SScott Long static void
6126f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
6136f8718a3SScott Long {
6146f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6156f8718a3SScott Long }
6166f8718a3SScott Long 
61738cc658fSJohn Baldwin static void
61838cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
61938cc658fSJohn Baldwin {
62038cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
62138cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
62238cc658fSJohn Baldwin 
62338cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
624062af0b0SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_MBOX_REORDER) != 0)
625062af0b0SPyun YongHyeon 		CSR_READ_4(sc, off);
62638cc658fSJohn Baldwin }
62738cc658fSJohn Baldwin 
628f41ac2beSBill Paul /*
629548c8f1aSPyun YongHyeon  * Clear all stale locks and select the lock for this driver instance.
630548c8f1aSPyun YongHyeon  */
631548c8f1aSPyun YongHyeon static void
632548c8f1aSPyun YongHyeon bge_ape_lock_init(struct bge_softc *sc)
633548c8f1aSPyun YongHyeon {
634548c8f1aSPyun YongHyeon 	uint32_t bit, regbase;
635548c8f1aSPyun YongHyeon 	int i;
636548c8f1aSPyun YongHyeon 
637548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
638548c8f1aSPyun YongHyeon 		regbase = BGE_APE_LOCK_GRANT;
639548c8f1aSPyun YongHyeon 	else
640548c8f1aSPyun YongHyeon 		regbase = BGE_APE_PER_LOCK_GRANT;
641548c8f1aSPyun YongHyeon 
642548c8f1aSPyun YongHyeon 	/* Clear any stale locks. */
643548c8f1aSPyun YongHyeon 	for (i = BGE_APE_LOCK_PHY0; i <= BGE_APE_LOCK_GPIO; i++) {
644548c8f1aSPyun YongHyeon 		switch (i) {
645548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY0:
646548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY1:
647548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY2:
648548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY3:
649548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
650548c8f1aSPyun YongHyeon 			break;
651548c8f1aSPyun YongHyeon 		default:
652bd9c196aSPyun YongHyeon 			if (sc->bge_func_addr == 0)
653548c8f1aSPyun YongHyeon 				bit = BGE_APE_LOCK_GRANT_DRIVER0;
654548c8f1aSPyun YongHyeon 			else
655548c8f1aSPyun YongHyeon 				bit = (1 << sc->bge_func_addr);
656548c8f1aSPyun YongHyeon 		}
657548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, regbase + 4 * i, bit);
658548c8f1aSPyun YongHyeon 	}
659548c8f1aSPyun YongHyeon 
660548c8f1aSPyun YongHyeon 	/* Select the PHY lock based on the device's function number. */
661548c8f1aSPyun YongHyeon 	switch (sc->bge_func_addr) {
662548c8f1aSPyun YongHyeon 	case 0:
663548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY0;
664548c8f1aSPyun YongHyeon 		break;
665548c8f1aSPyun YongHyeon 	case 1:
666548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY1;
667548c8f1aSPyun YongHyeon 		break;
668548c8f1aSPyun YongHyeon 	case 2:
669548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY2;
670548c8f1aSPyun YongHyeon 		break;
671548c8f1aSPyun YongHyeon 	case 3:
672548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY3;
673548c8f1aSPyun YongHyeon 		break;
674548c8f1aSPyun YongHyeon 	default:
675548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev,
676548c8f1aSPyun YongHyeon 		    "PHY lock not supported on this function\n");
677548c8f1aSPyun YongHyeon 	}
678548c8f1aSPyun YongHyeon }
679548c8f1aSPyun YongHyeon 
680548c8f1aSPyun YongHyeon /*
681548c8f1aSPyun YongHyeon  * Check for APE firmware, set flags, and print version info.
682548c8f1aSPyun YongHyeon  */
683548c8f1aSPyun YongHyeon static void
684548c8f1aSPyun YongHyeon bge_ape_read_fw_ver(struct bge_softc *sc)
685548c8f1aSPyun YongHyeon {
686548c8f1aSPyun YongHyeon 	const char *fwtype;
687548c8f1aSPyun YongHyeon 	uint32_t apedata, features;
688548c8f1aSPyun YongHyeon 
689548c8f1aSPyun YongHyeon 	/* Check for a valid APE signature in shared memory. */
690548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_SEG_SIG);
691548c8f1aSPyun YongHyeon 	if (apedata != BGE_APE_SEG_SIG_MAGIC) {
692548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags &= ~ BGE_MFW_ON_APE;
693548c8f1aSPyun YongHyeon 		return;
694548c8f1aSPyun YongHyeon 	}
695548c8f1aSPyun YongHyeon 
696548c8f1aSPyun YongHyeon 	/* Check if APE firmware is running. */
697548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_STATUS);
698548c8f1aSPyun YongHyeon 	if ((apedata & BGE_APE_FW_STATUS_READY) == 0) {
699548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE signature found "
700548c8f1aSPyun YongHyeon 		    "but FW status not ready! 0x%08x\n", apedata);
701548c8f1aSPyun YongHyeon 		return;
702548c8f1aSPyun YongHyeon 	}
703548c8f1aSPyun YongHyeon 
704548c8f1aSPyun YongHyeon 	sc->bge_mfw_flags |= BGE_MFW_ON_APE;
705548c8f1aSPyun YongHyeon 
706548c8f1aSPyun YongHyeon 	/* Fetch the APE firwmare type and version. */
707548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_VERSION);
708548c8f1aSPyun YongHyeon 	features = APE_READ_4(sc, BGE_APE_FW_FEATURES);
709548c8f1aSPyun YongHyeon 	if ((features & BGE_APE_FW_FEATURE_NCSI) != 0) {
710548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_NCSI;
711548c8f1aSPyun YongHyeon 		fwtype = "NCSI";
712548c8f1aSPyun YongHyeon 	} else if ((features & BGE_APE_FW_FEATURE_DASH) != 0) {
713548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_DASH;
714548c8f1aSPyun YongHyeon 		fwtype = "DASH";
715548c8f1aSPyun YongHyeon 	} else
716548c8f1aSPyun YongHyeon 		fwtype = "UNKN";
717548c8f1aSPyun YongHyeon 
718548c8f1aSPyun YongHyeon 	/* Print the APE firmware version. */
719548c8f1aSPyun YongHyeon 	device_printf(sc->bge_dev, "APE FW version: %s v%d.%d.%d.%d\n",
720548c8f1aSPyun YongHyeon 	    fwtype,
721548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MAJMSK) >> BGE_APE_FW_VERSION_MAJSFT,
722548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MINMSK) >> BGE_APE_FW_VERSION_MINSFT,
723548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_REVMSK) >> BGE_APE_FW_VERSION_REVSFT,
724548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_BLDMSK));
725548c8f1aSPyun YongHyeon }
726548c8f1aSPyun YongHyeon 
727548c8f1aSPyun YongHyeon static int
728548c8f1aSPyun YongHyeon bge_ape_lock(struct bge_softc *sc, int locknum)
729548c8f1aSPyun YongHyeon {
730548c8f1aSPyun YongHyeon 	uint32_t bit, gnt, req, status;
731548c8f1aSPyun YongHyeon 	int i, off;
732548c8f1aSPyun YongHyeon 
733548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
734548c8f1aSPyun YongHyeon 		return (0);
735548c8f1aSPyun YongHyeon 
736548c8f1aSPyun YongHyeon 	/* Lock request/grant registers have different bases. */
737548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761) {
738548c8f1aSPyun YongHyeon 		req = BGE_APE_LOCK_REQ;
739548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
740548c8f1aSPyun YongHyeon 	} else {
741548c8f1aSPyun YongHyeon 		req = BGE_APE_PER_LOCK_REQ;
742548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
743548c8f1aSPyun YongHyeon 	}
744548c8f1aSPyun YongHyeon 
745548c8f1aSPyun YongHyeon 	off = 4 * locknum;
746548c8f1aSPyun YongHyeon 
747548c8f1aSPyun YongHyeon 	switch (locknum) {
748548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
749548c8f1aSPyun YongHyeon 		/* Lock required when using GPIO. */
750548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
751548c8f1aSPyun YongHyeon 			return (0);
752548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
753548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
754548c8f1aSPyun YongHyeon 		else
755548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
756548c8f1aSPyun YongHyeon 		break;
757548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
758548c8f1aSPyun YongHyeon 		/* Lock required to reset the device. */
759548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
760548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
761548c8f1aSPyun YongHyeon 		else
762548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
763548c8f1aSPyun YongHyeon 		break;
764548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
765548c8f1aSPyun YongHyeon 		/* Lock required when accessing certain APE memory. */
766548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
767548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
768548c8f1aSPyun YongHyeon 		else
769548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
770548c8f1aSPyun YongHyeon 		break;
771548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
772548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
773548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
774548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
775548c8f1aSPyun YongHyeon 		/* Lock required when accessing PHYs. */
776548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_REQ_DRIVER0;
777548c8f1aSPyun YongHyeon 		break;
778548c8f1aSPyun YongHyeon 	default:
779548c8f1aSPyun YongHyeon 		return (EINVAL);
780548c8f1aSPyun YongHyeon 	}
781548c8f1aSPyun YongHyeon 
782548c8f1aSPyun YongHyeon 	/* Request a lock. */
783548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, req + off, bit);
784548c8f1aSPyun YongHyeon 
785548c8f1aSPyun YongHyeon 	/* Wait up to 1 second to acquire lock. */
786548c8f1aSPyun YongHyeon 	for (i = 0; i < 20000; i++) {
787548c8f1aSPyun YongHyeon 		status = APE_READ_4(sc, gnt + off);
788548c8f1aSPyun YongHyeon 		if (status == bit)
789548c8f1aSPyun YongHyeon 			break;
790548c8f1aSPyun YongHyeon 		DELAY(50);
791548c8f1aSPyun YongHyeon 	}
792548c8f1aSPyun YongHyeon 
793548c8f1aSPyun YongHyeon 	/* Handle any errors. */
794548c8f1aSPyun YongHyeon 	if (status != bit) {
795548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE lock %d request failed! "
796548c8f1aSPyun YongHyeon 		    "request = 0x%04x[0x%04x], status = 0x%04x[0x%04x]\n",
797548c8f1aSPyun YongHyeon 		    locknum, req + off, bit & 0xFFFF, gnt + off,
798548c8f1aSPyun YongHyeon 		    status & 0xFFFF);
799548c8f1aSPyun YongHyeon 		/* Revoke the lock request. */
800548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, gnt + off, bit);
801548c8f1aSPyun YongHyeon 		return (EBUSY);
802548c8f1aSPyun YongHyeon 	}
803548c8f1aSPyun YongHyeon 
804548c8f1aSPyun YongHyeon 	return (0);
805548c8f1aSPyun YongHyeon }
806548c8f1aSPyun YongHyeon 
807548c8f1aSPyun YongHyeon static void
808548c8f1aSPyun YongHyeon bge_ape_unlock(struct bge_softc *sc, int locknum)
809548c8f1aSPyun YongHyeon {
810548c8f1aSPyun YongHyeon 	uint32_t bit, gnt;
811548c8f1aSPyun YongHyeon 	int off;
812548c8f1aSPyun YongHyeon 
813548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
814548c8f1aSPyun YongHyeon 		return;
815548c8f1aSPyun YongHyeon 
816548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
817548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
818548c8f1aSPyun YongHyeon 	else
819548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
820548c8f1aSPyun YongHyeon 
821548c8f1aSPyun YongHyeon 	off = 4 * locknum;
822548c8f1aSPyun YongHyeon 
823548c8f1aSPyun YongHyeon 	switch (locknum) {
824548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
825548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
826548c8f1aSPyun YongHyeon 			return;
827548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
828548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
829548c8f1aSPyun YongHyeon 		else
830548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
831548c8f1aSPyun YongHyeon 		break;
832548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
833548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
834548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
835548c8f1aSPyun YongHyeon 		else
836548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
837548c8f1aSPyun YongHyeon 		break;
838548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
839548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
840548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
841548c8f1aSPyun YongHyeon 		else
842548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
843548c8f1aSPyun YongHyeon 		break;
844548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
845548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
846548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
847548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
848548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_GRANT_DRIVER0;
849548c8f1aSPyun YongHyeon 		break;
850548c8f1aSPyun YongHyeon 	default:
851548c8f1aSPyun YongHyeon 		return;
852548c8f1aSPyun YongHyeon 	}
853548c8f1aSPyun YongHyeon 
854548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, gnt + off, bit);
855548c8f1aSPyun YongHyeon }
856548c8f1aSPyun YongHyeon 
857548c8f1aSPyun YongHyeon /*
858548c8f1aSPyun YongHyeon  * Send an event to the APE firmware.
859548c8f1aSPyun YongHyeon  */
860548c8f1aSPyun YongHyeon static void
861548c8f1aSPyun YongHyeon bge_ape_send_event(struct bge_softc *sc, uint32_t event)
862548c8f1aSPyun YongHyeon {
863548c8f1aSPyun YongHyeon 	uint32_t apedata;
864548c8f1aSPyun YongHyeon 	int i;
865548c8f1aSPyun YongHyeon 
866548c8f1aSPyun YongHyeon 	/* NCSI does not support APE events. */
867548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
868548c8f1aSPyun YongHyeon 		return;
869548c8f1aSPyun YongHyeon 
870548c8f1aSPyun YongHyeon 	/* Wait up to 1ms for APE to service previous event. */
871548c8f1aSPyun YongHyeon 	for (i = 10; i > 0; i--) {
872548c8f1aSPyun YongHyeon 		if (bge_ape_lock(sc, BGE_APE_LOCK_MEM) != 0)
873548c8f1aSPyun YongHyeon 			break;
874548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_EVENT_STATUS);
875548c8f1aSPyun YongHyeon 		if ((apedata & BGE_APE_EVENT_STATUS_EVENT_PENDING) == 0) {
876548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT_STATUS, event |
877548c8f1aSPyun YongHyeon 			    BGE_APE_EVENT_STATUS_EVENT_PENDING);
878548c8f1aSPyun YongHyeon 			bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
879548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT, BGE_APE_EVENT_1);
880548c8f1aSPyun YongHyeon 			break;
881548c8f1aSPyun YongHyeon 		}
882548c8f1aSPyun YongHyeon 		bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
883548c8f1aSPyun YongHyeon 		DELAY(100);
884548c8f1aSPyun YongHyeon 	}
885548c8f1aSPyun YongHyeon 	if (i == 0)
886548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE event 0x%08x send timed out\n",
887548c8f1aSPyun YongHyeon 		    event);
888548c8f1aSPyun YongHyeon }
889548c8f1aSPyun YongHyeon 
890548c8f1aSPyun YongHyeon static void
891548c8f1aSPyun YongHyeon bge_ape_driver_state_change(struct bge_softc *sc, int kind)
892548c8f1aSPyun YongHyeon {
893548c8f1aSPyun YongHyeon 	uint32_t apedata, event;
894548c8f1aSPyun YongHyeon 
895548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
896548c8f1aSPyun YongHyeon 		return;
897548c8f1aSPyun YongHyeon 
898548c8f1aSPyun YongHyeon 	switch (kind) {
899548c8f1aSPyun YongHyeon 	case BGE_RESET_START:
900548c8f1aSPyun YongHyeon 		/* If this is the first load, clear the load counter. */
901548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_HOST_SEG_SIG);
902548c8f1aSPyun YongHyeon 		if (apedata != BGE_APE_HOST_SEG_SIG_MAGIC)
903548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, 0);
904548c8f1aSPyun YongHyeon 		else {
905548c8f1aSPyun YongHyeon 			apedata = APE_READ_4(sc, BGE_APE_HOST_INIT_COUNT);
906548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, ++apedata);
907548c8f1aSPyun YongHyeon 		}
908548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_SIG,
909548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_SIG_MAGIC);
910548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_LEN,
911548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_LEN_MAGIC);
912548c8f1aSPyun YongHyeon 
913548c8f1aSPyun YongHyeon 		/* Add some version info if bge(4) supports it. */
914548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRIVER_ID,
915548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRIVER_ID_MAGIC(1, 0));
916548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_BEHAVIOR,
917548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_BEHAV_NO_PHYLOCK);
918548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_HEARTBEAT_INT_MS,
919548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_HEARTBEAT_INT_DISABLE);
920548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
921548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_START);
922548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_START;
923548c8f1aSPyun YongHyeon 		break;
924548c8f1aSPyun YongHyeon 	case BGE_RESET_SHUTDOWN:
925548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
926548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_UNLOAD);
927548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_UNLOAD;
928548c8f1aSPyun YongHyeon 		break;
929548c8f1aSPyun YongHyeon 	case BGE_RESET_SUSPEND:
930548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_SUSPEND;
931548c8f1aSPyun YongHyeon 		break;
932548c8f1aSPyun YongHyeon 	default:
933548c8f1aSPyun YongHyeon 		return;
934548c8f1aSPyun YongHyeon 	}
935548c8f1aSPyun YongHyeon 
936548c8f1aSPyun YongHyeon 	bge_ape_send_event(sc, event | BGE_APE_EVENT_STATUS_DRIVER_EVNT |
937548c8f1aSPyun YongHyeon 	    BGE_APE_EVENT_STATUS_STATE_CHNGE);
938548c8f1aSPyun YongHyeon }
939548c8f1aSPyun YongHyeon 
940548c8f1aSPyun YongHyeon /*
941f41ac2beSBill Paul  * Map a single buffer address.
942f41ac2beSBill Paul  */
943f41ac2beSBill Paul 
944f41ac2beSBill Paul static void
9453f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
946f41ac2beSBill Paul {
947f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
948f41ac2beSBill Paul 
949f41ac2beSBill Paul 	if (error)
950f41ac2beSBill Paul 		return;
951f41ac2beSBill Paul 
9525b610048SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
9535b610048SPyun YongHyeon 
954f41ac2beSBill Paul 	ctx = arg;
955f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
956f41ac2beSBill Paul }
957f41ac2beSBill Paul 
95838cc658fSJohn Baldwin static uint8_t
95938cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
96038cc658fSJohn Baldwin {
96138cc658fSJohn Baldwin 	uint32_t access, byte = 0;
96238cc658fSJohn Baldwin 	int i;
96338cc658fSJohn Baldwin 
96438cc658fSJohn Baldwin 	/* Lock. */
96538cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
96638cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
96738cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
96838cc658fSJohn Baldwin 			break;
96938cc658fSJohn Baldwin 		DELAY(20);
97038cc658fSJohn Baldwin 	}
97138cc658fSJohn Baldwin 	if (i == 8000)
97238cc658fSJohn Baldwin 		return (1);
97338cc658fSJohn Baldwin 
97438cc658fSJohn Baldwin 	/* Enable access. */
97538cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
97638cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
97738cc658fSJohn Baldwin 
97838cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
97938cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
98038cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
98138cc658fSJohn Baldwin 		DELAY(10);
98238cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
98338cc658fSJohn Baldwin 			DELAY(10);
98438cc658fSJohn Baldwin 			break;
98538cc658fSJohn Baldwin 		}
98638cc658fSJohn Baldwin 	}
98738cc658fSJohn Baldwin 
98838cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
98938cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
99038cc658fSJohn Baldwin 		return (1);
99138cc658fSJohn Baldwin 	}
99238cc658fSJohn Baldwin 
99338cc658fSJohn Baldwin 	/* Get result. */
99438cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
99538cc658fSJohn Baldwin 
99638cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
99738cc658fSJohn Baldwin 
99838cc658fSJohn Baldwin 	/* Disable access. */
99938cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
100038cc658fSJohn Baldwin 
100138cc658fSJohn Baldwin 	/* Unlock. */
100238cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
100338cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
100438cc658fSJohn Baldwin 
100538cc658fSJohn Baldwin 	return (0);
100638cc658fSJohn Baldwin }
100738cc658fSJohn Baldwin 
100838cc658fSJohn Baldwin /*
100938cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
101038cc658fSJohn Baldwin  */
101138cc658fSJohn Baldwin static int
101238cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
101338cc658fSJohn Baldwin {
101438cc658fSJohn Baldwin 	int err = 0, i;
101538cc658fSJohn Baldwin 	uint8_t byte = 0;
101638cc658fSJohn Baldwin 
101738cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
101838cc658fSJohn Baldwin 		return (1);
101938cc658fSJohn Baldwin 
102038cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
102138cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
102238cc658fSJohn Baldwin 		if (err)
102338cc658fSJohn Baldwin 			break;
102438cc658fSJohn Baldwin 		*(dest + i) = byte;
102538cc658fSJohn Baldwin 	}
102638cc658fSJohn Baldwin 
102738cc658fSJohn Baldwin 	return (err ? 1 : 0);
102838cc658fSJohn Baldwin }
102938cc658fSJohn Baldwin 
103095d67482SBill Paul /*
103195d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
103295d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
103395d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
103495d67482SBill Paul  * access method.
103595d67482SBill Paul  */
10363f74909aSGleb Smirnoff static uint8_t
10373f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
103895d67482SBill Paul {
103995d67482SBill Paul 	int i;
10403f74909aSGleb Smirnoff 	uint32_t byte = 0;
104195d67482SBill Paul 
104295d67482SBill Paul 	/*
104395d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
104495d67482SBill Paul 	 * having to use the bitbang method.
104595d67482SBill Paul 	 */
104695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
104795d67482SBill Paul 
104895d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
104995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
105095d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
105195d67482SBill Paul 	DELAY(20);
105295d67482SBill Paul 
105395d67482SBill Paul 	/* Issue the read EEPROM command. */
105495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
105595d67482SBill Paul 
105695d67482SBill Paul 	/* Wait for completion */
105795d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
105895d67482SBill Paul 		DELAY(10);
105995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
106095d67482SBill Paul 			break;
106195d67482SBill Paul 	}
106295d67482SBill Paul 
1063d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
1064fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
1065f6789fbaSPyun YongHyeon 		return (1);
106695d67482SBill Paul 	}
106795d67482SBill Paul 
106895d67482SBill Paul 	/* Get result. */
106995d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
107095d67482SBill Paul 
10710c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
107295d67482SBill Paul 
107395d67482SBill Paul 	return (0);
107495d67482SBill Paul }
107595d67482SBill Paul 
107695d67482SBill Paul /*
107795d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
107895d67482SBill Paul  */
107995d67482SBill Paul static int
10803f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
108195d67482SBill Paul {
10823f74909aSGleb Smirnoff 	int i, error = 0;
10833f74909aSGleb Smirnoff 	uint8_t byte = 0;
108495d67482SBill Paul 
108595d67482SBill Paul 	for (i = 0; i < cnt; i++) {
10863f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
10873f74909aSGleb Smirnoff 		if (error)
108895d67482SBill Paul 			break;
108995d67482SBill Paul 		*(dest + i) = byte;
109095d67482SBill Paul 	}
109195d67482SBill Paul 
10923f74909aSGleb Smirnoff 	return (error ? 1 : 0);
109395d67482SBill Paul }
109495d67482SBill Paul 
109595d67482SBill Paul static int
10963f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
109795d67482SBill Paul {
109895d67482SBill Paul 	struct bge_softc *sc;
1099a813ed78SPyun YongHyeon 	uint32_t val;
110095d67482SBill Paul 	int i;
110195d67482SBill Paul 
110295d67482SBill Paul 	sc = device_get_softc(dev);
110395d67482SBill Paul 
1104548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1105548c8f1aSPyun YongHyeon 		return (0);
1106548c8f1aSPyun YongHyeon 
1107a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1108a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1109a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1110a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1111a813ed78SPyun YongHyeon 		DELAY(80);
111237ceeb4dSPaul Saab 	}
111337ceeb4dSPaul Saab 
111495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
111595d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
111695d67482SBill Paul 
1117a813ed78SPyun YongHyeon 	/* Poll for the PHY register access to complete. */
111895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1119d5d23857SJung-uk Kim 		DELAY(10);
112095d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
1121a813ed78SPyun YongHyeon 		if ((val & BGE_MICOMM_BUSY) == 0) {
1122a813ed78SPyun YongHyeon 			DELAY(5);
1123a813ed78SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_MI_COMM);
112495d67482SBill Paul 			break;
112595d67482SBill Paul 		}
1126a813ed78SPyun YongHyeon 	}
112795d67482SBill Paul 
112895d67482SBill Paul 	if (i == BGE_TIMEOUT) {
11295fea260fSMarius Strobl 		device_printf(sc->bge_dev,
11305fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
11315fea260fSMarius Strobl 		    phy, reg, val);
113237ceeb4dSPaul Saab 		val = 0;
113395d67482SBill Paul 	}
113495d67482SBill Paul 
1135a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1136a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1137a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1138a813ed78SPyun YongHyeon 		DELAY(80);
113937ceeb4dSPaul Saab 	}
114037ceeb4dSPaul Saab 
1141548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1142548c8f1aSPyun YongHyeon 
114395d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
114495d67482SBill Paul 		return (0);
114595d67482SBill Paul 
11460c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
114795d67482SBill Paul }
114895d67482SBill Paul 
114995d67482SBill Paul static int
11503f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
115195d67482SBill Paul {
115295d67482SBill Paul 	struct bge_softc *sc;
115395d67482SBill Paul 	int i;
115495d67482SBill Paul 
115595d67482SBill Paul 	sc = device_get_softc(dev);
115695d67482SBill Paul 
115738cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
115838cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
115938cc658fSJohn Baldwin 		return (0);
116038cc658fSJohn Baldwin 
1161548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1162548c8f1aSPyun YongHyeon 		return (0);
1163548c8f1aSPyun YongHyeon 
1164a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1165a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1166a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1167a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1168a813ed78SPyun YongHyeon 		DELAY(80);
116937ceeb4dSPaul Saab 	}
117037ceeb4dSPaul Saab 
117195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
117295d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
117395d67482SBill Paul 
117495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1175d5d23857SJung-uk Kim 		DELAY(10);
117638cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
117738cc658fSJohn Baldwin 			DELAY(5);
117838cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
117995d67482SBill Paul 			break;
1180d5d23857SJung-uk Kim 		}
118138cc658fSJohn Baldwin 	}
1182d5d23857SJung-uk Kim 
1183a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1184a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1185a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1186a813ed78SPyun YongHyeon 		DELAY(80);
1187a813ed78SPyun YongHyeon 	}
1188a813ed78SPyun YongHyeon 
1189548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1190548c8f1aSPyun YongHyeon 
1191a813ed78SPyun YongHyeon 	if (i == BGE_TIMEOUT)
119238cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
11932246e8c6SPyun YongHyeon 		    "PHY write timed out (phy %d, reg %d, val 0x%04x)\n",
119438cc658fSJohn Baldwin 		    phy, reg, val);
119537ceeb4dSPaul Saab 
119695d67482SBill Paul 	return (0);
119795d67482SBill Paul }
119895d67482SBill Paul 
119995d67482SBill Paul static void
12003f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
120195d67482SBill Paul {
120295d67482SBill Paul 	struct bge_softc *sc;
120395d67482SBill Paul 	struct mii_data *mii;
1204a0a03d1eSPyun YongHyeon 	uint32_t mac_mode, rx_mode, tx_mode;
1205e4146b95SPyun YongHyeon 
120695d67482SBill Paul 	sc = device_get_softc(dev);
1207fba8b109SMarcel Moolenaar 	if ((if_getdrvflags(sc->bge_ifp) & IFF_DRV_RUNNING) == 0)
1208e4146b95SPyun YongHyeon 		return;
120995d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
121095d67482SBill Paul 
1211d4f5240aSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1212d4f5240aSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
1213d4f5240aSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
1214d4f5240aSPyun YongHyeon 		case IFM_10_T:
1215d4f5240aSPyun YongHyeon 		case IFM_100_TX:
1216d4f5240aSPyun YongHyeon 			sc->bge_link = 1;
1217d4f5240aSPyun YongHyeon 			break;
1218d4f5240aSPyun YongHyeon 		case IFM_1000_T:
1219d4f5240aSPyun YongHyeon 		case IFM_1000_SX:
1220d4f5240aSPyun YongHyeon 		case IFM_2500_SX:
1221d4f5240aSPyun YongHyeon 			if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
1222d4f5240aSPyun YongHyeon 				sc->bge_link = 1;
1223d4f5240aSPyun YongHyeon 			else
1224d4f5240aSPyun YongHyeon 				sc->bge_link = 0;
1225d4f5240aSPyun YongHyeon 			break;
1226d4f5240aSPyun YongHyeon 		default:
1227d4f5240aSPyun YongHyeon 			sc->bge_link = 0;
1228d4f5240aSPyun YongHyeon 			break;
1229d4f5240aSPyun YongHyeon 		}
1230d4f5240aSPyun YongHyeon 	} else
1231d4f5240aSPyun YongHyeon 		sc->bge_link = 0;
1232d4f5240aSPyun YongHyeon 	if (sc->bge_link == 0)
1233d4f5240aSPyun YongHyeon 		return;
1234a0a03d1eSPyun YongHyeon 
1235a0a03d1eSPyun YongHyeon 	/*
1236a0a03d1eSPyun YongHyeon 	 * APE firmware touches these registers to keep the MAC
1237a0a03d1eSPyun YongHyeon 	 * connected to the outside world.  Try to keep the
1238a0a03d1eSPyun YongHyeon 	 * accesses atomic.
1239a0a03d1eSPyun YongHyeon 	 */
1240a0a03d1eSPyun YongHyeon 
1241a0a03d1eSPyun YongHyeon 	/* Set the port mode (MII/GMII) to match the link speed. */
1242a0a03d1eSPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) &
1243a0a03d1eSPyun YongHyeon 	    ~(BGE_MACMODE_PORTMODE | BGE_MACMODE_HALF_DUPLEX);
1244a0a03d1eSPyun YongHyeon 	tx_mode = CSR_READ_4(sc, BGE_TX_MODE);
1245a0a03d1eSPyun YongHyeon 	rx_mode = CSR_READ_4(sc, BGE_RX_MODE);
1246a0a03d1eSPyun YongHyeon 
1247ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
1248ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
1249a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_GMII;
12503f74909aSGleb Smirnoff 	else
1251a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_MII;
125295d67482SBill Paul 
1253a0a03d1eSPyun YongHyeon 	/* Set MAC flow control behavior to match link flow control settings. */
1254a0a03d1eSPyun YongHyeon 	tx_mode &= ~BGE_TXMODE_FLOWCTL_ENABLE;
1255a0a03d1eSPyun YongHyeon 	rx_mode &= ~BGE_RXMODE_FLOWCTL_ENABLE;
12564951ca86SPyun YongHyeon 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1257a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1258a0a03d1eSPyun YongHyeon 			tx_mode |= BGE_TXMODE_FLOWCTL_ENABLE;
1259a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1260a0a03d1eSPyun YongHyeon 			rx_mode |= BGE_RXMODE_FLOWCTL_ENABLE;
1261a0a03d1eSPyun YongHyeon 	} else
1262a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_MACMODE_HALF_DUPLEX;
1263a0a03d1eSPyun YongHyeon 
1264a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, mac_mode);
12659b80ffe7SPyun YongHyeon 	DELAY(40);
1266a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, tx_mode);
1267a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_RX_MODE, rx_mode);
126895d67482SBill Paul }
126995d67482SBill Paul 
127095d67482SBill Paul /*
127195d67482SBill Paul  * Intialize a standard receive ring descriptor.
127295d67482SBill Paul  */
127395d67482SBill Paul static int
1274943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
127595d67482SBill Paul {
1276943787f3SPyun YongHyeon 	struct mbuf *m;
127795d67482SBill Paul 	struct bge_rx_bd *r;
1278a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
1279943787f3SPyun YongHyeon 	bus_dmamap_t map;
1280a23634a1SPyun YongHyeon 	int error, nsegs;
128195d67482SBill Paul 
1282f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD &&
1283fba8b109SMarcel Moolenaar 	    (if_getmtu(sc->bge_ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
1284f5459d4cSPyun YongHyeon 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))) {
1285c6499eccSGleb Smirnoff 		m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
1286f5459d4cSPyun YongHyeon 		if (m == NULL)
1287f5459d4cSPyun YongHyeon 			return (ENOBUFS);
1288f5459d4cSPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1289f5459d4cSPyun YongHyeon 	} else {
1290c6499eccSGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1291943787f3SPyun YongHyeon 		if (m == NULL)
129295d67482SBill Paul 			return (ENOBUFS);
1293943787f3SPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MCLBYTES;
1294f5459d4cSPyun YongHyeon 	}
1295652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1296943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
1297943787f3SPyun YongHyeon 
12980ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
1299943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
1300a23634a1SPyun YongHyeon 	if (error != 0) {
1301943787f3SPyun YongHyeon 		m_freem(m);
1302a23634a1SPyun YongHyeon 		return (error);
1303f41ac2beSBill Paul 	}
1304943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1305943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1306943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
1307943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1308943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
1309943787f3SPyun YongHyeon 	}
1310943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
1311943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
1312943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
1313943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
1314e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
1315943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
1316a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
1317a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
1318e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
1319a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
1320e907febfSPyun YongHyeon 	r->bge_idx = i;
1321f41ac2beSBill Paul 
13220ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1323943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
132495d67482SBill Paul 
132595d67482SBill Paul 	return (0);
132695d67482SBill Paul }
132795d67482SBill Paul 
132895d67482SBill Paul /*
132995d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
133095d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
133195d67482SBill Paul  */
133295d67482SBill Paul static int
1333943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
133495d67482SBill Paul {
13351be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
1336943787f3SPyun YongHyeon 	bus_dmamap_t map;
13371be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
1338943787f3SPyun YongHyeon 	struct mbuf *m;
1339943787f3SPyun YongHyeon 	int error, nsegs;
134095d67482SBill Paul 
1341c6499eccSGleb Smirnoff 	MGETHDR(m, M_NOWAIT, MT_DATA);
1342943787f3SPyun YongHyeon 	if (m == NULL)
134395d67482SBill Paul 		return (ENOBUFS);
134495d67482SBill Paul 
13452a8c860fSRobert Watson 	if (m_cljget(m, M_NOWAIT, MJUM9BYTES) == NULL) {
1346943787f3SPyun YongHyeon 		m_freem(m);
134795d67482SBill Paul 		return (ENOBUFS);
134895d67482SBill Paul 	}
1349943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1350652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1351943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
13521be6acb7SGleb Smirnoff 
13531be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1354943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1355943787f3SPyun YongHyeon 	if (error != 0) {
1356943787f3SPyun YongHyeon 		m_freem(m);
13571be6acb7SGleb Smirnoff 		return (error);
1358f7cea149SGleb Smirnoff 	}
13591be6acb7SGleb Smirnoff 
1360aa8cbdbfSMarius Strobl 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1361943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1362943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1363943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1364943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1365943787f3SPyun YongHyeon 	}
1366943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1367943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1368943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1369943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1370943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1371e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1372e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1373e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1374e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1375e0b7b101SPyun YongHyeon 
13761be6acb7SGleb Smirnoff 	/*
13771be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
13781be6acb7SGleb Smirnoff 	 */
1379943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
13804e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
13814e7ba1abSGleb Smirnoff 	r->bge_idx = i;
13824e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
13834e7ba1abSGleb Smirnoff 	switch (nsegs) {
13844e7ba1abSGleb Smirnoff 	case 4:
13854e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
13864e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
13874e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1388e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
13894e7ba1abSGleb Smirnoff 	case 3:
1390e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1391e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1392e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1393e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
13944e7ba1abSGleb Smirnoff 	case 2:
13954e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
13964e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
13974e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1398e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
13994e7ba1abSGleb Smirnoff 	case 1:
14004e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
14014e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
14024e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1403e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
14044e7ba1abSGleb Smirnoff 		break;
14054e7ba1abSGleb Smirnoff 	default:
14064e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
14074e7ba1abSGleb Smirnoff 	}
1408f41ac2beSBill Paul 
1409a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1410943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
141195d67482SBill Paul 
141295d67482SBill Paul 	return (0);
141395d67482SBill Paul }
141495d67482SBill Paul 
141595d67482SBill Paul static int
14163f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
141795d67482SBill Paul {
14183ee5d7daSPyun YongHyeon 	int error, i;
141995d67482SBill Paul 
1420e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
142103e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1422e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1423943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
14243ee5d7daSPyun YongHyeon 			return (error);
142503e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
14261888f324SPyun YongHyeon 	}
142795d67482SBill Paul 
1428f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1429d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1430f41ac2beSBill Paul 
1431e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1432e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
143395d67482SBill Paul 
143495d67482SBill Paul 	return (0);
143595d67482SBill Paul }
143695d67482SBill Paul 
143795d67482SBill Paul static void
14383f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
143995d67482SBill Paul {
144095d67482SBill Paul 	int i;
144195d67482SBill Paul 
144295d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
144395d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
14440ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1445e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1446e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
14470ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1448f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1449e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1450e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
145195d67482SBill Paul 		}
1452f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
145395d67482SBill Paul 		    sizeof(struct bge_rx_bd));
145495d67482SBill Paul 	}
145595d67482SBill Paul }
145695d67482SBill Paul 
145795d67482SBill Paul static int
14583f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
145995d67482SBill Paul {
146095d67482SBill Paul 	struct bge_rcb *rcb;
14613ee5d7daSPyun YongHyeon 	int error, i;
146295d67482SBill Paul 
1463e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
146403e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
146595d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1466943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
14673ee5d7daSPyun YongHyeon 			return (error);
146803e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
14691888f324SPyun YongHyeon 	}
147095d67482SBill Paul 
1471f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1472d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1473f41ac2beSBill Paul 
1474e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
147595d67482SBill Paul 
14768a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1477f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
14788a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
14798a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
148067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
148195d67482SBill Paul 
1482e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
148395d67482SBill Paul 
148495d67482SBill Paul 	return (0);
148595d67482SBill Paul }
148695d67482SBill Paul 
148795d67482SBill Paul static void
14883f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
148995d67482SBill Paul {
149095d67482SBill Paul 	int i;
149195d67482SBill Paul 
149295d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
149395d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1494e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1495e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1496e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1497f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1498f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1499e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1500e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
150195d67482SBill Paul 		}
1502f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
15031be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
150495d67482SBill Paul 	}
150595d67482SBill Paul }
150695d67482SBill Paul 
150795d67482SBill Paul static void
15083f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
150995d67482SBill Paul {
151095d67482SBill Paul 	int i;
151195d67482SBill Paul 
1512f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
151395d67482SBill Paul 		return;
151495d67482SBill Paul 
151595d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
151695d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
15170ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1518e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1519e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
15200ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1521f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1522e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1523e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
152495d67482SBill Paul 		}
1525f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
152695d67482SBill Paul 		    sizeof(struct bge_tx_bd));
152795d67482SBill Paul 	}
152895d67482SBill Paul }
152995d67482SBill Paul 
153095d67482SBill Paul static int
15313f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
153295d67482SBill Paul {
153395d67482SBill Paul 	sc->bge_txcnt = 0;
153495d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
15353927098fSPaul Saab 
1536e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1537e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
15385c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1539e6bf277eSPyun YongHyeon 
154014bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
154114bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
154238cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
154314bbd30fSGleb Smirnoff 
15443927098fSPaul Saab 	/* 5700 b2 errata */
1545e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
154638cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
15473927098fSPaul Saab 
154814bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
154938cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
15503927098fSPaul Saab 	/* 5700 b2 errata */
1551e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
155238cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
155395d67482SBill Paul 
155495d67482SBill Paul 	return (0);
155595d67482SBill Paul }
155695d67482SBill Paul 
155795d67482SBill Paul static void
15583e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
15593e9b1bcaSJung-uk Kim {
1560fba8b109SMarcel Moolenaar 	if_t ifp;
15613e9b1bcaSJung-uk Kim 
15623e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
15633e9b1bcaSJung-uk Kim 
15643e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
15653e9b1bcaSJung-uk Kim 
156645ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
1567fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_PROMISC)
156845ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
15693e9b1bcaSJung-uk Kim 	else
157045ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
15713e9b1bcaSJung-uk Kim }
15723e9b1bcaSJung-uk Kim 
15730322ca23SGleb Smirnoff static u_int
15740322ca23SGleb Smirnoff bge_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
15750322ca23SGleb Smirnoff {
15760322ca23SGleb Smirnoff 	uint32_t *hashes = arg;
15770322ca23SGleb Smirnoff 	int h;
15780322ca23SGleb Smirnoff 
15790322ca23SGleb Smirnoff 	h = ether_crc32_le(LLADDR(sdl), ETHER_ADDR_LEN) & 0x7F;
15800322ca23SGleb Smirnoff 	hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
15810322ca23SGleb Smirnoff 
15820322ca23SGleb Smirnoff 	return (1);
15830322ca23SGleb Smirnoff }
15840322ca23SGleb Smirnoff 
15853e9b1bcaSJung-uk Kim static void
15863f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
158795d67482SBill Paul {
1588fba8b109SMarcel Moolenaar 	if_t ifp;
15893f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
15900322ca23SGleb Smirnoff 	int i;
159195d67482SBill Paul 
15920f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
15930f9bd73bSSam Leffler 
1594fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
159595d67482SBill Paul 
1596fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_ALLMULTI || if_getflags(ifp) & IFF_PROMISC) {
159795d67482SBill Paul 		for (i = 0; i < 4; i++)
15980c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
159995d67482SBill Paul 		return;
160095d67482SBill Paul 	}
160195d67482SBill Paul 
160295d67482SBill Paul 	/* First, zot all the existing filters. */
160395d67482SBill Paul 	for (i = 0; i < 4; i++)
160495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
160595d67482SBill Paul 
16060322ca23SGleb Smirnoff 	if_foreach_llmaddr(ifp, bge_hash_maddr, hashes);
160795d67482SBill Paul 
160895d67482SBill Paul 	for (i = 0; i < 4; i++)
160995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
161095d67482SBill Paul }
161195d67482SBill Paul 
16128cb1383cSDoug Ambrisko static void
1613cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1614cb2eacc7SYaroslav Tykhiy {
1615fba8b109SMarcel Moolenaar 	if_t ifp;
1616cb2eacc7SYaroslav Tykhiy 
1617cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1618cb2eacc7SYaroslav Tykhiy 
1619cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1620cb2eacc7SYaroslav Tykhiy 
1621cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1622fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING)
1623cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1624cb2eacc7SYaroslav Tykhiy 	else
1625cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1626cb2eacc7SYaroslav Tykhiy }
1627cb2eacc7SYaroslav Tykhiy 
1628cb2eacc7SYaroslav Tykhiy static void
1629797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
16308cb1383cSDoug Ambrisko {
1631797ab05eSPyun YongHyeon 
16328cb1383cSDoug Ambrisko 	/*
16338cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
16348cb1383cSDoug Ambrisko 	 */
16358cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
1636888b47f0SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
16378cb1383cSDoug Ambrisko 
16388cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16398cb1383cSDoug Ambrisko 		switch (type) {
16408cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1641224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1642224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
16438cb1383cSDoug Ambrisko 			break;
1644548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1645224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1646224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
16478cb1383cSDoug Ambrisko 			break;
1648548c8f1aSPyun YongHyeon 		case BGE_RESET_SUSPEND:
1649548c8f1aSPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1650548c8f1aSPyun YongHyeon 			    BGE_FW_DRV_STATE_SUSPEND);
1651548c8f1aSPyun YongHyeon 			break;
16528cb1383cSDoug Ambrisko 		}
16538cb1383cSDoug Ambrisko 	}
1654548c8f1aSPyun YongHyeon 
1655548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_START || type == BGE_RESET_SUSPEND)
1656548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
16578cb1383cSDoug Ambrisko }
16588cb1383cSDoug Ambrisko 
16598cb1383cSDoug Ambrisko static void
1660797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
16618cb1383cSDoug Ambrisko {
1662797ab05eSPyun YongHyeon 
16638cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16648cb1383cSDoug Ambrisko 		switch (type) {
16658cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1666224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1667224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START_DONE);
16688cb1383cSDoug Ambrisko 			/* START DONE */
16698cb1383cSDoug Ambrisko 			break;
1670548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1671224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1672224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD_DONE);
16738cb1383cSDoug Ambrisko 			break;
16748cb1383cSDoug Ambrisko 		}
16758cb1383cSDoug Ambrisko 	}
1676548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_SHUTDOWN)
1677548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
16788cb1383cSDoug Ambrisko }
16798cb1383cSDoug Ambrisko 
16808cb1383cSDoug Ambrisko static void
1681797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
16828cb1383cSDoug Ambrisko {
1683797ab05eSPyun YongHyeon 
16848cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
16858cb1383cSDoug Ambrisko 		switch (type) {
16868cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1687224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1688224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
16898cb1383cSDoug Ambrisko 			break;
1690548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1691224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1692224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
16938cb1383cSDoug Ambrisko 			break;
16948cb1383cSDoug Ambrisko 		}
16958cb1383cSDoug Ambrisko 	}
16968cb1383cSDoug Ambrisko }
16978cb1383cSDoug Ambrisko 
1698797ab05eSPyun YongHyeon static void
1699797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
17008cb1383cSDoug Ambrisko {
17018cb1383cSDoug Ambrisko 	int i;
17028cb1383cSDoug Ambrisko 
17038cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17043c201200SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB, BGE_FW_CMD_PAUSE);
17053fed2d5dSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
17069931ba85SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RX_CPU_EVENT) | BGE_RX_CPU_DRV_EVENT);
17078cb1383cSDoug Ambrisko 
17088cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
17099931ba85SPyun YongHyeon 			if (!(CSR_READ_4(sc, BGE_RX_CPU_EVENT) &
17109931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT))
17118cb1383cSDoug Ambrisko 				break;
17128cb1383cSDoug Ambrisko 			DELAY(10);
17138cb1383cSDoug Ambrisko 		}
17148cb1383cSDoug Ambrisko 	}
17158cb1383cSDoug Ambrisko }
17168cb1383cSDoug Ambrisko 
171750515680SPyun YongHyeon static uint32_t
171850515680SPyun YongHyeon bge_dma_swap_options(struct bge_softc *sc)
171950515680SPyun YongHyeon {
172050515680SPyun YongHyeon 	uint32_t dma_options;
172150515680SPyun YongHyeon 
172250515680SPyun YongHyeon 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
172350515680SPyun YongHyeon 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
172450515680SPyun YongHyeon #if BYTE_ORDER == BIG_ENDIAN
172550515680SPyun YongHyeon 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
172650515680SPyun YongHyeon #endif
172750515680SPyun YongHyeon 	return (dma_options);
172850515680SPyun YongHyeon }
172950515680SPyun YongHyeon 
173095d67482SBill Paul /*
1731c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
173295d67482SBill Paul  */
173395d67482SBill Paul static int
17343f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
173595d67482SBill Paul {
173650515680SPyun YongHyeon 	uint32_t dma_rw_ctl, misc_ctl, mode_ctl;
1737fbc374afSPyun YongHyeon 	uint16_t val;
173895d67482SBill Paul 	int i;
173995d67482SBill Paul 
17408cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
17411108273aSPyun YongHyeon 	misc_ctl = BGE_INIT;
17421108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
17431108273aSPyun YongHyeon 		misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
17441108273aSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
174595d67482SBill Paul 
174695d67482SBill Paul 	/*
174795d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
174895d67482SBill Paul 	 * internal memory.
174995d67482SBill Paul 	 */
175095d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
17513f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
175295d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
175395d67482SBill Paul 
175495d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
17553f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
175695d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
175795d67482SBill Paul 
1758fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1759fbc374afSPyun YongHyeon 		/*
1760d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1761fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1762fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1763fbc374afSPyun YongHyeon 		 */
1764fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1765fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1766fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1767fbc374afSPyun YongHyeon 	}
1768fbc374afSPyun YongHyeon 
1769f8bb33c3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM57765 ||
1770f8bb33c3SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57766) {
1771f8bb33c3SPyun YongHyeon 		/*
1772f8bb33c3SPyun YongHyeon 		 * For the 57766 and non Ax versions of 57765, bootcode
1773f8bb33c3SPyun YongHyeon 		 * needs to setup the PCIE Fast Training Sequence (FTS)
1774f8bb33c3SPyun YongHyeon 		 * value to prevent transmit hangs.
1775f8bb33c3SPyun YongHyeon 		 */
1776f8bb33c3SPyun YongHyeon 		if (sc->bge_chiprev != BGE_CHIPREV_57765_AX) {
1777f8bb33c3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_CPMU_PADRNG_CTL,
1778f8bb33c3SPyun YongHyeon 			    CSR_READ_4(sc, BGE_CPMU_PADRNG_CTL) |
1779f8bb33c3SPyun YongHyeon 			    BGE_CPMU_PADRNG_CTL_RDIV2);
1780f8bb33c3SPyun YongHyeon 		}
1781f8bb33c3SPyun YongHyeon 	}
1782f8bb33c3SPyun YongHyeon 
1783186f842bSJung-uk Kim 	/*
1784186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1785186f842bSJung-uk Kim 	 */
1786186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1787186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1788652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
178948630d79SPyun YongHyeon 		if (sc->bge_mps >= 256)
179048630d79SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
179148630d79SPyun YongHyeon 		else
1792186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1793652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
17944c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1795186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1796186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1797186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1798186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1799186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1800186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1801cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1802cbb2b2feSPyun YongHyeon 			/*
1803cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1804cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1805cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1806cbb2b2feSPyun YongHyeon 			 * register.
1807cbb2b2feSPyun YongHyeon 			 */
1808cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1809cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1810186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1811186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1812186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1813186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1814186f842bSJung-uk Kim 		} else {
1815186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1816186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1817186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
18180c8aa4eaSJung-uk Kim 			    0x0F;
1819186f842bSJung-uk Kim 		}
1820e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1821e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
18223f74909aSGleb Smirnoff 			uint32_t tmp;
18235cba12d3SPaul Saab 
1824186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
18250c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1826186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1827186f842bSJung-uk Kim 				dma_rw_ctl |=
1828186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
18295cba12d3SPaul Saab 
1830186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1831186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1832186f842bSJung-uk Kim 		}
1833186f842bSJung-uk Kim 	} else {
1834186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1835186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1836186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1837186f842bSJung-uk Kim 
1838186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1839186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1840186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1841186f842bSJung-uk Kim 	}
1842186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1843186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1844186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1845186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1846e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1847186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
18485cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1849b4a256acSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
18501108273aSPyun YongHyeon 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1851b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
1852b4a256acSPyun YongHyeon 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1853bbe2ca75SPyun YongHyeon 		/*
1854bbe2ca75SPyun YongHyeon 		 * Enable HW workaround for controllers that misinterpret
1855bbe2ca75SPyun YongHyeon 		 * a status tag update and leave interrupts permanently
1856bbe2ca75SPyun YongHyeon 		 * disabled.
1857bbe2ca75SPyun YongHyeon 		 */
18582927f01fSPyun YongHyeon 		if (!BGE_IS_57765_PLUS(sc) &&
18592927f01fSPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
18602927f01fSPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM5762)
1861bbe2ca75SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1862b4a256acSPyun YongHyeon 	}
18635cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
186495d67482SBill Paul 
186595d67482SBill Paul 	/*
186695d67482SBill Paul 	 * Set up general mode register.
186795d67482SBill Paul 	 */
1868548c8f1aSPyun YongHyeon 	mode_ctl = bge_dma_swap_options(sc);
18692927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
18702927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
1871548c8f1aSPyun YongHyeon 		/* Retain Host-2-BMC settings written by APE firmware. */
1872548c8f1aSPyun YongHyeon 		mode_ctl |= CSR_READ_4(sc, BGE_MODE_CTL) &
1873548c8f1aSPyun YongHyeon 		    (BGE_MODECTL_BYTESWAP_B2HRX_DATA |
1874548c8f1aSPyun YongHyeon 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA |
1875548c8f1aSPyun YongHyeon 		    BGE_MODECTL_B2HRX_ENABLE | BGE_MODECTL_HTX2B_ENABLE);
1876548c8f1aSPyun YongHyeon 	}
1877548c8f1aSPyun YongHyeon 	mode_ctl |= BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1878548c8f1aSPyun YongHyeon 	    BGE_MODECTL_TX_NO_PHDR_CSUM;
187995d67482SBill Paul 
188095d67482SBill Paul 	/*
188190447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
188290447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
188390447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
188490447aadSMarius Strobl 	 * certain bridges.
188590447aadSMarius Strobl 	 */
188690447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
188790447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
188850515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_FORCE_PCI32;
188990447aadSMarius Strobl 
189090447aadSMarius Strobl 	/*
18918cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
18928cb1383cSDoug Ambrisko 	 */
18938cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
189450515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_STACKUP;
189550515680SPyun YongHyeon 
189650515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
18978cb1383cSDoug Ambrisko 
18988cb1383cSDoug Ambrisko 	/*
1899ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
190040438c47SMarius Strobl 	 * properly by these devices.
190195d67482SBill Paul 	 */
190240438c47SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
190395d67482SBill Paul 
1904d7acafa1SMarius Strobl 	/* Set the timer prescaler (always 66 MHz). */
19050c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
190695d67482SBill Paul 
190738cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
190838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
190938cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
191038cc658fSJohn Baldwin 
191138cc658fSJohn Baldwin 		/* Put PHY into ready state */
191238cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
191338cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
191438cc658fSJohn Baldwin 		DELAY(40);
191538cc658fSJohn Baldwin 	}
191638cc658fSJohn Baldwin 
191795d67482SBill Paul 	return (0);
191895d67482SBill Paul }
191995d67482SBill Paul 
192095d67482SBill Paul static int
19213f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
192295d67482SBill Paul {
192395d67482SBill Paul 	struct bge_rcb *rcb;
1924e907febfSPyun YongHyeon 	bus_size_t vrcb;
1925e907febfSPyun YongHyeon 	bge_hostaddr taddr;
19262927f01fSPyun YongHyeon 	uint32_t dmactl, rdmareg, val;
19278a315a6dSPyun YongHyeon 	int i, limit;
192895d67482SBill Paul 
192995d67482SBill Paul 	/*
193095d67482SBill Paul 	 * Initialize the memory window pointer register so that
193195d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
193295d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
193395d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
193495d67482SBill Paul 	 */
193595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
193695d67482SBill Paul 
1937822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1938822f63fcSBill Paul 
19397ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
194095d67482SBill Paul 		/* Configure mbuf memory pool */
19410dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1942822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1943822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1944822f63fcSBill Paul 		else
194595d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
194695d67482SBill Paul 
194795d67482SBill Paul 		/* Configure DMA resource pool */
19480434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
19490434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
195095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
19510434d1b8SBill Paul 	}
195295d67482SBill Paul 
195395d67482SBill Paul 	/* Configure mbuf pool watermarks */
195450515680SPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
19551108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1956fba8b109SMarcel Moolenaar 		if (if_getmtu(sc->bge_ifp) > ETHERMTU) {
19571108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
19581108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
19591108273aSPyun YongHyeon 		} else {
19601108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
19611108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
19621108273aSPyun YongHyeon 		}
19631108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc)) {
1964fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1965fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1966fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
196738cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
196838cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
196938cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
197038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
197138cc658fSJohn Baldwin 	} else {
197238cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
197338cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
197438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
197538cc658fSJohn Baldwin 	}
197695d67482SBill Paul 
197795d67482SBill Paul 	/* Configure DMA resource watermarks */
197895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
197995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
198095d67482SBill Paul 
198195d67482SBill Paul 	/* Enable buffer manager */
1982bbe2ca75SPyun YongHyeon 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
1983bbe2ca75SPyun YongHyeon 	/*
1984bbe2ca75SPyun YongHyeon 	 * Change the arbitration algorithm of TXMBUF read request to
1985bbe2ca75SPyun YongHyeon 	 * round-robin instead of priority based for BCM5719.  When
1986bbe2ca75SPyun YongHyeon 	 * TXFIFO is almost empty, RDMA will hold its request until
1987bbe2ca75SPyun YongHyeon 	 * TXFIFO is not almost empty.
1988bbe2ca75SPyun YongHyeon 	 */
1989bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
1990bbe2ca75SPyun YongHyeon 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
1991bbe2ca75SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
199295d67482SBill Paul 
199395d67482SBill Paul 	/* Poll for buffer manager start indication */
199495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1995d5d23857SJung-uk Kim 		DELAY(10);
19960c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
199795d67482SBill Paul 			break;
199895d67482SBill Paul 	}
199995d67482SBill Paul 
200095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
20015a147ba6SPyun YongHyeon 		device_printf(sc->bge_dev, "buffer manager failed to start\n");
200295d67482SBill Paul 		return (ENXIO);
200395d67482SBill Paul 	}
200495d67482SBill Paul 
200595d67482SBill Paul 	/* Enable flow-through queues */
20060c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
200795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
200895d67482SBill Paul 
200995d67482SBill Paul 	/* Wait until queue initialization is complete */
201095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2011d5d23857SJung-uk Kim 		DELAY(10);
201295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
201395d67482SBill Paul 			break;
201495d67482SBill Paul 	}
201595d67482SBill Paul 
201695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2017fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
201895d67482SBill Paul 		return (ENXIO);
201995d67482SBill Paul 	}
202095d67482SBill Paul 
20218a315a6dSPyun YongHyeon 	/*
20228a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
20238a315a6dSPyun YongHyeon 	 *
20248a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
20258a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
20268a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
20278a315a6dSPyun YongHyeon 	 *
20288a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
20298a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
20308a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
20318a315a6dSPyun YongHyeon 	 *   to the controller.
20328a315a6dSPyun YongHyeon 	 *
20338a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
20348a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
20358a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
20368a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
20378a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
20388a315a6dSPyun YongHyeon 	 *   be disabled.
20398a315a6dSPyun YongHyeon 	 *
20408a315a6dSPyun YongHyeon 	 * Receive Return Ring
20418a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
20428a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
20438a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
20448a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
20458a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
20468a315a6dSPyun YongHyeon 	 *
20478a315a6dSPyun YongHyeon 	 * Send Ring
20488a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
20498a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
20508a315a6dSPyun YongHyeon 	 */
20518a315a6dSPyun YongHyeon 
20528a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
2053f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
2054f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
2055f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
2056f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
2057f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
2058f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2059f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
20601108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
20611108273aSPyun YongHyeon 		/*
20621108273aSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
20631108273aSPyun YongHyeon 		 * Bits 15-2 : Maximum RX frame size
20641108273aSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
20651108273aSPyun YongHyeon 		 * Bit 0     : Reserved
20661108273aSPyun YongHyeon 		 */
20671108273aSPyun YongHyeon 		rcb->bge_maxlen_flags =
20681108273aSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
20691108273aSPyun YongHyeon 	} else if (BGE_IS_5705_PLUS(sc)) {
20708a315a6dSPyun YongHyeon 		/*
20718a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
20728a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
20738a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
20748a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
20758a315a6dSPyun YongHyeon 		 */
20760434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
20778a315a6dSPyun YongHyeon 	} else {
20788a315a6dSPyun YongHyeon 		/*
20798a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
20808a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
20818a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
20828a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
20838a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
20848a315a6dSPyun YongHyeon 		 */
20850434d1b8SBill Paul 		rcb->bge_maxlen_flags =
20860434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
20878a315a6dSPyun YongHyeon 	}
2088bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
208950515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
209050515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
20911108273aSPyun YongHyeon 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
20921108273aSPyun YongHyeon 	else
209395d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
20948a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
20950c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
20960c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
209767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
209867111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
209995d67482SBill Paul 
21008a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
21018a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
21028a315a6dSPyun YongHyeon 
210395d67482SBill Paul 	/*
21048a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
21058a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
21068a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
210795d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
210895d67482SBill Paul 	 * high enough to require it).
210995d67482SBill Paul 	 */
21104c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2111f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
21128a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
2113f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
2114f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2115f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
2116f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2117f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2118f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2119f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
21201be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
21211be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
2122bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
212350515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
212450515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21251108273aSPyun YongHyeon 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
21261108273aSPyun YongHyeon 		else
212795d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
212867111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
212967111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
213067111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
213167111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
21328a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
21330434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
21340434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
213567111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
21368a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
21378a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
21388a315a6dSPyun YongHyeon 	}
213995d67482SBill Paul 
21408a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
21415e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
2142f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
214367111612SJohn Polstra 		rcb->bge_maxlen_flags =
214467111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
21450434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
21460434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
21478a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
21488a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
21490434d1b8SBill Paul 	}
215095d67482SBill Paul 
2151ca4f8986SPyun YongHyeon 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
2152ca4f8986SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2153427d3f33SPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
2154427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
2155427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
21568d5f7181SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
21578d5f7181SPyun YongHyeon 			    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
2158ca4f8986SPyun YongHyeon 	}
215995d67482SBill Paul 	/*
21608a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
21618a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
21628a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
21638a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
21648a315a6dSPyun YongHyeon 	 *
216595d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
216695d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
216795d67482SBill Paul 	 * each ring.
21689ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
21699ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
21709ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
21719ba784dbSScott Long 	 * are reports that it might not need to be so strict.
217238cc658fSJohn Baldwin 	 *
217338cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
217438cc658fSJohn Baldwin 	 * well.
217595d67482SBill Paul 	 */
21765345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
21776f8718a3SScott Long 		val = 8;
21786f8718a3SScott Long 	else
21796f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
21806f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
21812a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
21822a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
21832a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
21841108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
21851108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
21861108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
21871108273aSPyun YongHyeon 	}
218895d67482SBill Paul 
218995d67482SBill Paul 	/*
21908a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
21918a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
21928a315a6dSPyun YongHyeon 	 * located in NIC memory.
219395d67482SBill Paul 	 */
21948a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
21958a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
21968a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
21972927f01fSPyun YongHyeon 	else if (BGE_IS_57765_PLUS(sc) ||
21982927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762)
21992927f01fSPyun YongHyeon 		limit = 2;
22002927f01fSPyun YongHyeon 	else if (BGE_IS_5717_PLUS(sc))
22012927f01fSPyun YongHyeon 		limit = 4;
22028a315a6dSPyun YongHyeon 	else
22038a315a6dSPyun YongHyeon 		limit = 1;
2204e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
22058a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2206e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2207e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
2208e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2209e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
221095d67482SBill Paul 	}
221195d67482SBill Paul 
22128a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
2213e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
2214e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
2215e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2216e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2217bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
221850515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
221950515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
22201108273aSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
22211108273aSPyun YongHyeon 	else
2222e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
2223e907febfSPyun YongHyeon 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
2224e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2225e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
222695d67482SBill Paul 
22278a315a6dSPyun YongHyeon 	/*
22288a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
22298a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
22308a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
22318a315a6dSPyun YongHyeon 	 */
2232bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
223350515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
223450515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
22351108273aSPyun YongHyeon 		/* Should be 17, use 16 until we get an SRAM map. */
22361108273aSPyun YongHyeon 		limit = 16;
22371108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc))
22388a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
2239b4a256acSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
22402927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762 ||
2241fe26ad88SPyun YongHyeon 	    BGE_IS_57765_PLUS(sc))
22428a315a6dSPyun YongHyeon 		limit = 4;
22438a315a6dSPyun YongHyeon 	else
22448a315a6dSPyun YongHyeon 		limit = 1;
22458a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
2246e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
22478a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2248e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
2249e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
2250e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
22518a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
2252e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
225338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
22543f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
2255e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
225695d67482SBill Paul 	}
225795d67482SBill Paul 
225895d67482SBill Paul 	/*
22598a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
22608a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
22618a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
226295d67482SBill Paul 	 */
2263e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2264e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
2265e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2266e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
22678a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2268e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2269e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
227095d67482SBill Paul 
227195d67482SBill Paul 	/* Set random backoff seed for TX */
227295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
22730a2cc827SPyun YongHyeon 	    (IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
22744a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
22750a2cc827SPyun YongHyeon 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5]) &
227695d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
227795d67482SBill Paul 
227895d67482SBill Paul 	/* Set inter-packet gap */
227950515680SPyun YongHyeon 	val = 0x2620;
22802927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
22812927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762)
228250515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
228350515680SPyun YongHyeon 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
228450515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
228595d67482SBill Paul 
228695d67482SBill Paul 	/*
228795d67482SBill Paul 	 * Specify which ring to use for packets that don't match
228895d67482SBill Paul 	 * any RX rules.
228995d67482SBill Paul 	 */
229095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
229195d67482SBill Paul 
229295d67482SBill Paul 	/*
229395d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
229495d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
229595d67482SBill Paul 	 */
229695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
229795d67482SBill Paul 
229895d67482SBill Paul 	/* Inialize RX list placement stats mask. */
22990c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
230095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
230195d67482SBill Paul 
230295d67482SBill Paul 	/* Disable host coalescing until we get it set up */
230395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
230495d67482SBill Paul 
230595d67482SBill Paul 	/* Poll to make sure it's shut down. */
230695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2307d5d23857SJung-uk Kim 		DELAY(10);
230895d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
230995d67482SBill Paul 			break;
231095d67482SBill Paul 	}
231195d67482SBill Paul 
231295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2313fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2314fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
231595d67482SBill Paul 		return (ENXIO);
231695d67482SBill Paul 	}
231795d67482SBill Paul 
231895d67482SBill Paul 	/* Set up host coalescing defaults */
231995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
232095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
232195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
232295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
23237ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
232495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
232595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
23260434d1b8SBill Paul 	}
2327b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
2328b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
232995d67482SBill Paul 
233095d67482SBill Paul 	/* Set up address of statistics block */
23317ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
2332f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
2333f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
233495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
2335f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
23360434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
233795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
23380434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
23390434d1b8SBill Paul 	}
23400434d1b8SBill Paul 
23410434d1b8SBill Paul 	/* Set up address of status block */
2342f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
2343f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
234495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
2345f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
234695d67482SBill Paul 
234730f57f61SPyun YongHyeon 	/* Set up status block size. */
234830f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2349864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
235030f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
2351864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2352864104feSPyun YongHyeon 	} else {
235330f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
2354864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
2355864104feSPyun YongHyeon 	}
2356864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2357864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
2358864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
235930f57f61SPyun YongHyeon 
236095d67482SBill Paul 	/* Turn on host coalescing state machine */
236130f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
236295d67482SBill Paul 
236395d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
236495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
236595d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
236695d67482SBill Paul 
236795d67482SBill Paul 	/* Turn on RX list placement state machine */
236895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
236995d67482SBill Paul 
237095d67482SBill Paul 	/* Turn on RX list selector state machine. */
23717ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
237295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
237395d67482SBill Paul 
23742246e8c6SPyun YongHyeon 	/* Turn on DMA, clear stats. */
2375ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2376ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2377ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2378ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
2379ea3b4127SPyun YongHyeon 
2380ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
2381ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
2382ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
2383ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
2384ea3b4127SPyun YongHyeon 	else
2385ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
2386ea3b4127SPyun YongHyeon 
2387548c8f1aSPyun YongHyeon 	/* Allow APE to send/receive frames. */
2388548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
2389548c8f1aSPyun YongHyeon 		val |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
2390548c8f1aSPyun YongHyeon 
2391ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
23929b80ffe7SPyun YongHyeon 	DELAY(40);
239395d67482SBill Paul 
239495d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
239591bd90d8SPyun YongHyeon 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
239695d67482SBill Paul 
239795d67482SBill Paul #ifdef notdef
239895d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
239995d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
240095d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
240195d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
240295d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
240395d67482SBill Paul #endif
240495d67482SBill Paul 
240595d67482SBill Paul 	/* Turn on DMA completion state machine */
24067ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
240795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
240895d67482SBill Paul 
24096f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
24106f8718a3SScott Long 
24116f8718a3SScott Long 	/* Enable host coalescing bug fix. */
2412a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
24133889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
24146f8718a3SScott Long 
24157aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
24167aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
24177aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
24187aa4b937SPyun YongHyeon 
241995d67482SBill Paul 	/* Turn on write DMA state machine */
24206f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
24214f09c4c7SMarius Strobl 	DELAY(40);
242295d67482SBill Paul 
242395d67482SBill Paul 	/* Turn on read DMA state machine */
24244f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
24251108273aSPyun YongHyeon 
24261108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
24271108273aSPyun YongHyeon 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
24281108273aSPyun YongHyeon 
2429a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2430a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2431a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2432a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2433a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2434a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
24354f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
24364f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
24371108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2438ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
24391108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_TSO3 ||
24401108273aSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
244155a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
244255a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
244355a24a05SPyun YongHyeon 	}
244450515680SPyun YongHyeon 
24452927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
24462927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
244750515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
244850515680SPyun YongHyeon 			BGE_RDMAMODE_H2BNC_VLAN_DET;
2449e3215f76SPyun YongHyeon 		/*
2450e3215f76SPyun YongHyeon 		 * Allow multiple outstanding read requests from
2451e3215f76SPyun YongHyeon 		 * non-LSO read DMA engine.
2452e3215f76SPyun YongHyeon 		 */
2453e3215f76SPyun YongHyeon 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
2454e3215f76SPyun YongHyeon 	}
245550515680SPyun YongHyeon 
2456d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2457d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2458d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
24591108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
24602927f01fSPyun YongHyeon 	    BGE_IS_5717_PLUS(sc) || BGE_IS_57765_PLUS(sc)) {
24612927f01fSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
24622927f01fSPyun YongHyeon 			rdmareg = BGE_RDMA_RSRVCTRL_REG2;
24632927f01fSPyun YongHyeon 		else
24642927f01fSPyun YongHyeon 			rdmareg = BGE_RDMA_RSRVCTRL;
24652927f01fSPyun YongHyeon 		dmactl = CSR_READ_4(sc, rdmareg);
2466bbe2ca75SPyun YongHyeon 		/*
2467bbe2ca75SPyun YongHyeon 		 * Adjust tx margin to prevent TX data corruption and
2468bbe2ca75SPyun YongHyeon 		 * fix internal FIFO overflow.
2469bbe2ca75SPyun YongHyeon 		 */
24702927f01fSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5719_A0 ||
24712927f01fSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
2472bbe2ca75SPyun YongHyeon 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
2473bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
2474bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
2475bbe2ca75SPyun YongHyeon 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
2476bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
2477bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
2478bbe2ca75SPyun YongHyeon 		}
2479d255f2a9SPyun YongHyeon 		/*
2480d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
2481d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
2482d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
2483d255f2a9SPyun YongHyeon 		 */
24842927f01fSPyun YongHyeon 		CSR_WRITE_4(sc, rdmareg, dmactl |
2485d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2486d255f2a9SPyun YongHyeon 	}
2487bbe2ca75SPyun YongHyeon 
2488e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719) {
2489bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2490bbe2ca75SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2491bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2492bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2493e3215f76SPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2494e3215f76SPyun YongHyeon 		/*
2495e3215f76SPyun YongHyeon 		 * Allow 4KB burst length reads for non-LSO frames.
2496e3215f76SPyun YongHyeon 		 * Enable 512B burst length reads for buffer descriptors.
2497e3215f76SPyun YongHyeon 		 */
2498e3215f76SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2499e3215f76SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2500e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
2501e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
25022927f01fSPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5762) {
25032927f01fSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2,
25042927f01fSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2) |
25052927f01fSPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
25062927f01fSPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2507bbe2ca75SPyun YongHyeon 	}
2508bbe2ca75SPyun YongHyeon 
25094f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
25104f09c4c7SMarius Strobl 	DELAY(40);
251195d67482SBill Paul 
251229b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
251329b44b09SPyun YongHyeon 		for (i = 0; i < BGE_NUM_RDMA_CHANNELS / 2; i++) {
251429b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LENGTH + i * 4);
251529b44b09SPyun YongHyeon 			if ((val & 0xFFFF) > BGE_FRAMELEN)
251629b44b09SPyun YongHyeon 				break;
251729b44b09SPyun YongHyeon 			if (((val >> 16) & 0xFFFF) > BGE_FRAMELEN)
251829b44b09SPyun YongHyeon 				break;
251929b44b09SPyun YongHyeon 		}
252029b44b09SPyun YongHyeon 		if (i != BGE_NUM_RDMA_CHANNELS / 2) {
252129b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
252229b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
252329b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5719;
252429b44b09SPyun YongHyeon 			else
252529b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5720;
252629b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
252729b44b09SPyun YongHyeon 		}
252829b44b09SPyun YongHyeon 	}
252929b44b09SPyun YongHyeon 
253095d67482SBill Paul 	/* Turn on RX data completion state machine */
253195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
253295d67482SBill Paul 
253395d67482SBill Paul 	/* Turn on RX BD initiator state machine */
253495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
253595d67482SBill Paul 
253695d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
253795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
253895d67482SBill Paul 
253995d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
25407ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
254195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
254295d67482SBill Paul 
254395d67482SBill Paul 	/* Turn on send BD completion state machine */
254495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
254595d67482SBill Paul 
254695d67482SBill Paul 	/* Turn on send data completion state machine */
2547a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2548a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2549a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2550a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
255195d67482SBill Paul 
255295d67482SBill Paul 	/* Turn on send data initiator state machine */
25531108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
25541108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
25551108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2556ca3f1187SPyun YongHyeon 	else
255795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
255895d67482SBill Paul 
255995d67482SBill Paul 	/* Turn on send BD initiator state machine */
256095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
256195d67482SBill Paul 
256295d67482SBill Paul 	/* Turn on send BD selector state machine */
256395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
256495d67482SBill Paul 
25650c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
256695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
256795d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
256895d67482SBill Paul 
256995d67482SBill Paul 	/* ack/clear link change events */
257095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
25710434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
25720434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2573f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
257495d67482SBill Paul 
25756ede2cfaSPyun YongHyeon 	/*
25766ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
25776ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
25786ede2cfaSPyun YongHyeon 	 */
2579652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
258095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2581a1d52896SBill Paul 	} else {
25827ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
25837ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
25847ed3f0f0SPyun YongHyeon 			DELAY(80);
25857ed3f0f0SPyun YongHyeon 		}
25861f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
25874c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2588a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2589a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2590a1d52896SBill Paul 	}
259195d67482SBill Paul 
25921f313773SOleg Bulyzhin 	/*
25931f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
25941f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
25951f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
25961f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
25971f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
25981f313773SOleg Bulyzhin 	 */
25991f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
26001f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
26011f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
26021f313773SOleg Bulyzhin 
260395d67482SBill Paul 	/* Enable link state change attentions. */
260495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
260595d67482SBill Paul 
260695d67482SBill Paul 	return (0);
260795d67482SBill Paul }
260895d67482SBill Paul 
2609d7acafa1SMarius Strobl static const struct bge_revision *
26104c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
26114c0da0ffSGleb Smirnoff {
26124c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
26134c0da0ffSGleb Smirnoff 
26144c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
26154c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
26164c0da0ffSGleb Smirnoff 			return (br);
26174c0da0ffSGleb Smirnoff 	}
26184c0da0ffSGleb Smirnoff 
26194c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
26204c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
26214c0da0ffSGleb Smirnoff 			return (br);
26224c0da0ffSGleb Smirnoff 	}
26234c0da0ffSGleb Smirnoff 
26244c0da0ffSGleb Smirnoff 	return (NULL);
26254c0da0ffSGleb Smirnoff }
26264c0da0ffSGleb Smirnoff 
2627d7acafa1SMarius Strobl static const struct bge_vendor *
26284c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
26294c0da0ffSGleb Smirnoff {
26304c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
26314c0da0ffSGleb Smirnoff 
26324c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
26334c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
26344c0da0ffSGleb Smirnoff 			return (v);
26354c0da0ffSGleb Smirnoff 
26364c0da0ffSGleb Smirnoff 	return (NULL);
26374c0da0ffSGleb Smirnoff }
26384c0da0ffSGleb Smirnoff 
2639d7acafa1SMarius Strobl static uint32_t
2640d7acafa1SMarius Strobl bge_chipid(device_t dev)
264195d67482SBill Paul {
2642978f2704SMarius Strobl 	uint32_t id;
264395d67482SBill Paul 
2644a5779553SStanislav Sedov 	id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2645a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
26461108273aSPyun YongHyeon 	if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
26471108273aSPyun YongHyeon 		/*
2648d7acafa1SMarius Strobl 		 * Find the ASCI revision.  Different chips use different
2649d7acafa1SMarius Strobl 		 * registers.
26501108273aSPyun YongHyeon 		 */
26511108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
2652cb2404b4SSepherosa Ziehau 		case BCOM_DEVICEID_BCM5717C:
26531ca32af8SSepherosa Ziehau 			/* 5717 C0 seems to belong to 5720 line. */
26541ca32af8SSepherosa Ziehau 			id = BGE_CHIPID_BCM5720_A0;
26551ca32af8SSepherosa Ziehau 			break;
26561ca32af8SSepherosa Ziehau 		case BCOM_DEVICEID_BCM5717:
26571108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
2658bbe2ca75SPyun YongHyeon 		case BCOM_DEVICEID_BCM5719:
265950515680SPyun YongHyeon 		case BCOM_DEVICEID_BCM5720:
26602927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5725:
26612927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5727:
26622927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5762:
266367129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57764:
266467129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57767:
266567129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57787:
26661108273aSPyun YongHyeon 			id = pci_read_config(dev,
26671108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
26681108273aSPyun YongHyeon 			break;
2669b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57761:
2670fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57762:
2671b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57765:
2672fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57766:
2673b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57781:
267467129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57782:
2675b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57785:
267667129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57786:
2677b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57791:
2678b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57795:
2679b4a256acSPyun YongHyeon 			id = pci_read_config(dev,
2680b4a256acSPyun YongHyeon 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2681b4a256acSPyun YongHyeon 			break;
26821108273aSPyun YongHyeon 		default:
2683d7acafa1SMarius Strobl 			id = pci_read_config(dev, BGE_PCI_PRODID_ASICREV, 4);
26841108273aSPyun YongHyeon 		}
26851108273aSPyun YongHyeon 	}
2686d7acafa1SMarius Strobl 	return (id);
2687d7acafa1SMarius Strobl }
2688d7acafa1SMarius Strobl 
2689d7acafa1SMarius Strobl /*
2690d7acafa1SMarius Strobl  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
2691d7acafa1SMarius Strobl  * against our list and return its name if we find a match.
2692d7acafa1SMarius Strobl  *
2693d7acafa1SMarius Strobl  * Note that since the Broadcom controller contains VPD support, we
2694d7acafa1SMarius Strobl  * try to get the device name string from the controller itself instead
2695d7acafa1SMarius Strobl  * of the compiled-in string. It guarantees we'll always announce the
2696d7acafa1SMarius Strobl  * right product name. We fall back to the compiled-in string when
2697d7acafa1SMarius Strobl  * VPD is unavailable or corrupt.
2698d7acafa1SMarius Strobl  */
2699d7acafa1SMarius Strobl static int
2700d7acafa1SMarius Strobl bge_probe(device_t dev)
2701d7acafa1SMarius Strobl {
2702d7acafa1SMarius Strobl 	char buf[96];
2703d7acafa1SMarius Strobl 	char model[64];
2704d7acafa1SMarius Strobl 	const struct bge_revision *br;
2705d7acafa1SMarius Strobl 	const char *pname;
2706d7acafa1SMarius Strobl 	struct bge_softc *sc;
2707d7acafa1SMarius Strobl 	const struct bge_type *t = bge_devs;
2708d7acafa1SMarius Strobl 	const struct bge_vendor *v;
2709d7acafa1SMarius Strobl 	uint32_t id;
2710d7acafa1SMarius Strobl 	uint16_t did, vid;
2711d7acafa1SMarius Strobl 
2712d7acafa1SMarius Strobl 	sc = device_get_softc(dev);
2713d7acafa1SMarius Strobl 	sc->bge_dev = dev;
2714d7acafa1SMarius Strobl 	vid = pci_get_vendor(dev);
2715d7acafa1SMarius Strobl 	did = pci_get_device(dev);
2716d7acafa1SMarius Strobl 	while(t->bge_vid != 0) {
2717d7acafa1SMarius Strobl 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2718d7acafa1SMarius Strobl 			id = bge_chipid(dev);
27194c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
2720852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2721852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
2722d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s", pname);
2723d7acafa1SMarius Strobl 			else {
2724d7acafa1SMarius Strobl 				v = bge_lookup_vendor(vid);
2725d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s %s",
2726d7acafa1SMarius Strobl 				    v != NULL ? v->v_name : "Unknown",
27277c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
27282ad1b396SMarius Strobl 				    "NetXtreme/NetLink Ethernet Controller");
2729d7acafa1SMarius Strobl 			}
2730d7acafa1SMarius Strobl 			snprintf(buf, sizeof(buf), "%s, %sASIC rev. %#08x",
2731d7acafa1SMarius Strobl 			    model, br != NULL ? "" : "unknown ", id);
27324c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
2733d7acafa1SMarius Strobl 			return (BUS_PROBE_DEFAULT);
273495d67482SBill Paul 		}
273595d67482SBill Paul 		t++;
273695d67482SBill Paul 	}
273795d67482SBill Paul 
273895d67482SBill Paul 	return (ENXIO);
273995d67482SBill Paul }
274095d67482SBill Paul 
2741f41ac2beSBill Paul static void
27423f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2743f41ac2beSBill Paul {
2744f41ac2beSBill Paul 	int i;
2745f41ac2beSBill Paul 
27463f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2747f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2748f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
27490ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2750f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2751f41ac2beSBill Paul 	}
2752943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2753943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2754943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2755f41ac2beSBill Paul 
27563f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2757f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2758f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2759f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2760f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2761f41ac2beSBill Paul 	}
2762943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2763943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2764943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2765f41ac2beSBill Paul 
27663f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2767f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2768f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
27690ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2770f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2771f41ac2beSBill Paul 	}
2772f41ac2beSBill Paul 
27730ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
27740ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2775c0220d81SPyun YongHyeon 	if (sc->bge_cdata.bge_mtag_jumbo)
2776c0220d81SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
27770ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
27780ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2779f41ac2beSBill Paul 
27803f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2781068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_std_ring_paddr)
2782e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2783e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2784068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_std_ring)
2785f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2786f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2787f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2788f41ac2beSBill Paul 
2789f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2790f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2791f41ac2beSBill Paul 
27923f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2793068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_jumbo_ring_paddr)
2794e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2795e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2796e65bed95SPyun YongHyeon 
2797068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_jumbo_ring)
2798f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2799f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2800f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2801f41ac2beSBill Paul 
2802f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2803f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2804f41ac2beSBill Paul 
28053f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2806068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_return_ring_paddr)
2807e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2808e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2809e65bed95SPyun YongHyeon 
2810068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_return_ring)
2811f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2812f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2813f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2814f41ac2beSBill Paul 
2815f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2816f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2817f41ac2beSBill Paul 
28183f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2819068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_tx_ring_paddr)
2820e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2821e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2822e65bed95SPyun YongHyeon 
2823068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_tx_ring)
2824f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2825f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2826f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2827f41ac2beSBill Paul 
2828f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2829f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2830f41ac2beSBill Paul 
28313f74909aSGleb Smirnoff 	/* Destroy status block. */
2832068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_status_block_paddr)
2833e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2834e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2835e65bed95SPyun YongHyeon 
2836068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_status_block)
2837f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2838f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2839f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2840f41ac2beSBill Paul 
2841f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2842f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2843f41ac2beSBill Paul 
28443f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2845068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_stats_paddr)
2846e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2847e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2848e65bed95SPyun YongHyeon 
2849068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_stats)
2850f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2851f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2852f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2853f41ac2beSBill Paul 
2854f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2855f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2856f41ac2beSBill Paul 
28575b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
28585b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
28595b610048SPyun YongHyeon 
28603f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2861f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2862f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2863f41ac2beSBill Paul }
2864f41ac2beSBill Paul 
2865f41ac2beSBill Paul static int
28665b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
28675b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
28685b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2869f41ac2beSBill Paul {
28703f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
2871e86fa024STycho Nightingale 	bus_addr_t lowaddr;
2872e86fa024STycho Nightingale 	bus_size_t ring_end;
28735b610048SPyun YongHyeon 	int error;
2874f41ac2beSBill Paul 
2875e86fa024STycho Nightingale 	lowaddr = BUS_SPACE_MAXADDR;
2876e86fa024STycho Nightingale again:
28775b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2878e86fa024STycho Nightingale 	    alignment, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
28795b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
28805b610048SPyun YongHyeon 	if (error != 0) {
28815b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28825b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
28835b610048SPyun YongHyeon 		return (ENOMEM);
28845b610048SPyun YongHyeon 	}
28855b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
28865b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
28875b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
28885b610048SPyun YongHyeon 	if (error != 0) {
28895b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28905b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
28915b610048SPyun YongHyeon 		return (ENOMEM);
28925b610048SPyun YongHyeon 	}
28935b610048SPyun YongHyeon 	/* Load the address of the ring. */
28945b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
28955b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
28965b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
28975b610048SPyun YongHyeon 	if (error != 0) {
28985b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28995b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
29005b610048SPyun YongHyeon 		return (ENOMEM);
29015b610048SPyun YongHyeon 	}
29025b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
2903e86fa024STycho Nightingale 	ring_end = *paddr + maxsize;
2904e86fa024STycho Nightingale 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0 &&
2905e86fa024STycho Nightingale 	    BGE_ADDR_HI(*paddr) != BGE_ADDR_HI(ring_end)) {
2906e86fa024STycho Nightingale 		/*
2907e86fa024STycho Nightingale 		 * 4GB boundary crossed.  Limit maximum allowable DMA
2908e86fa024STycho Nightingale 		 * address space to 32bit and try again.
2909e86fa024STycho Nightingale 		 */
2910e86fa024STycho Nightingale 		bus_dmamap_unload(*tag, *map);
2911e86fa024STycho Nightingale 		bus_dmamem_free(*tag, *ring, *map);
2912e86fa024STycho Nightingale 		bus_dma_tag_destroy(*tag);
2913e86fa024STycho Nightingale 		if (bootverbose)
2914e86fa024STycho Nightingale 			device_printf(sc->bge_dev, "4GB boundary crossed, "
2915e86fa024STycho Nightingale 			    "limit DMA address space to 32bit for %s\n", msg);
2916e86fa024STycho Nightingale 		*ring = NULL;
2917e86fa024STycho Nightingale 		*tag = NULL;
2918e86fa024STycho Nightingale 		*map = NULL;
2919e86fa024STycho Nightingale 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
2920e86fa024STycho Nightingale 		goto again;
2921e86fa024STycho Nightingale 	}
29225b610048SPyun YongHyeon 	return (0);
29235b610048SPyun YongHyeon }
29245b610048SPyun YongHyeon 
29255b610048SPyun YongHyeon static int
29265b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
29275b610048SPyun YongHyeon {
29285b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2929e86fa024STycho Nightingale 	bus_size_t boundary, sbsz, rxmaxsegsz, txsegsz, txmaxsegsz;
29305b610048SPyun YongHyeon 	int i, error;
2931f41ac2beSBill Paul 
2932f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2933f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2934f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2935f41ac2beSBill Paul 	/*
2936f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2937f41ac2beSBill Paul 	 */
29384eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2939f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
29404eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
29414eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2942e65bed95SPyun YongHyeon 	if (error != 0) {
2943fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2944fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2945e65bed95SPyun YongHyeon 		return (ENOMEM);
2946e65bed95SPyun YongHyeon 	}
2947e65bed95SPyun YongHyeon 
29485b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
29495b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
29505b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
29515b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
29525b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
29535b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
29545b610048SPyun YongHyeon 	if (error)
29555b610048SPyun YongHyeon 		return (error);
29565b610048SPyun YongHyeon 
29575b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
29585b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
29595b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
29605b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
29615b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
29625b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
29635b610048SPyun YongHyeon 	if (error)
29645b610048SPyun YongHyeon 		return (error);
29655b610048SPyun YongHyeon 
29665b610048SPyun YongHyeon 	/* Create tag for TX ring. */
29675b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
29685b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
29695b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
29705b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
29715b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
29725b610048SPyun YongHyeon 	if (error)
29735b610048SPyun YongHyeon 		return (error);
29745b610048SPyun YongHyeon 
2975f41ac2beSBill Paul 	/*
29765b610048SPyun YongHyeon 	 * Create tag for status block.
29775b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
29785b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
29795b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
29805b610048SPyun YongHyeon 	 * of configured number of ring.
2981f41ac2beSBill Paul 	 */
29825b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
29835b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
29845b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
29855b610048SPyun YongHyeon 	else
29865b610048SPyun YongHyeon 		sbsz = 32;
29875b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
29885b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
29895b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
29905b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
29915b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
29925b610048SPyun YongHyeon 	if (error)
29935b610048SPyun YongHyeon 		return (error);
29945b610048SPyun YongHyeon 
299512c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
299612c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
299712c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
299812c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
299912c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
300012c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
300112c65daeSPyun YongHyeon 	if (error)
300212c65daeSPyun YongHyeon 		return (error);
300312c65daeSPyun YongHyeon 
30045b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
30055b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
30065b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
30075b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
30085b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
30095b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
30105b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
30115b610048SPyun YongHyeon 		if (error)
30125b610048SPyun YongHyeon 			return (error);
30135b610048SPyun YongHyeon 	}
30145b610048SPyun YongHyeon 
30155b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
3016e86fa024STycho Nightingale 	boundary = 0;
3017d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
3018e86fa024STycho Nightingale 		boundary = BGE_DMA_BNDRY;
3019d2ffe15aSPyun YongHyeon 		/*
3020d2ffe15aSPyun YongHyeon 		 * XXX
3021d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
3022d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
3023062af0b0SPyun YongHyeon 		 * Both limiting DMA address space to 32bits and flushing
3024062af0b0SPyun YongHyeon 		 * mailbox write seem to address the issue.
3025d2ffe15aSPyun YongHyeon 		 */
3026062af0b0SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3027d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
3028d2ffe15aSPyun YongHyeon 	}
3029e86fa024STycho Nightingale 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
3030e86fa024STycho Nightingale 	    1, boundary, lowaddr, BUS_SPACE_MAXADDR, NULL,
3031e86fa024STycho Nightingale 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
3032e86fa024STycho Nightingale 	    0, NULL, NULL, &sc->bge_cdata.bge_buffer_tag);
30335b610048SPyun YongHyeon 	if (error != 0) {
30345b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
30355b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
30365b610048SPyun YongHyeon 		return (ENOMEM);
30375b610048SPyun YongHyeon 	}
30385b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
30391108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
3040ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
3041ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
3042ca3f1187SPyun YongHyeon 	} else {
3043ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
3044ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
3045ca3f1187SPyun YongHyeon 	}
30465b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
3047ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
3048ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
3049ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
3050f41ac2beSBill Paul 
3051f41ac2beSBill Paul 	if (error) {
30520ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
30530ac56796SPyun YongHyeon 		return (ENOMEM);
30540ac56796SPyun YongHyeon 	}
30550ac56796SPyun YongHyeon 
30565b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
3057f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
3058f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
3059f5459d4cSPyun YongHyeon 	else
3060f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
30615b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
3062f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
3063f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
30640ac56796SPyun YongHyeon 
30650ac56796SPyun YongHyeon 	if (error) {
30660ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
3067f41ac2beSBill Paul 		return (ENOMEM);
3068f41ac2beSBill Paul 	}
3069f41ac2beSBill Paul 
30703f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
3071943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3072943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
3073943787f3SPyun YongHyeon 	if (error) {
3074943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
3075943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
3076943787f3SPyun YongHyeon 		return (ENOMEM);
3077943787f3SPyun YongHyeon 	}
3078f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
30790ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3080f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
3081f41ac2beSBill Paul 		if (error) {
3082fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
3083fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
3084f41ac2beSBill Paul 			return (ENOMEM);
3085f41ac2beSBill Paul 		}
3086f41ac2beSBill Paul 	}
3087f41ac2beSBill Paul 
30883f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
3089f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
30900ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
3091f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
3092f41ac2beSBill Paul 		if (error) {
3093fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
30940ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
3095f41ac2beSBill Paul 			return (ENOMEM);
3096f41ac2beSBill Paul 		}
3097f41ac2beSBill Paul 	}
3098f41ac2beSBill Paul 
30995b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
31004c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
31015b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
31028a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
31031be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
31041be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
3105f41ac2beSBill Paul 		if (error) {
3106fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
31073f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
3108f41ac2beSBill Paul 			return (ENOMEM);
3109f41ac2beSBill Paul 		}
31103f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
3111943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3112943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
3113943787f3SPyun YongHyeon 		if (error) {
3114943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
31151b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
3116943787f3SPyun YongHyeon 			return (ENOMEM);
3117943787f3SPyun YongHyeon 		}
3118f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
3119f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3120f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
3121f41ac2beSBill Paul 			if (error) {
3122fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
31233f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
3124f41ac2beSBill Paul 				return (ENOMEM);
3125f41ac2beSBill Paul 			}
3126f41ac2beSBill Paul 		}
3127f41ac2beSBill Paul 	}
3128f41ac2beSBill Paul 
3129f41ac2beSBill Paul 	return (0);
3130f41ac2beSBill Paul }
3131f41ac2beSBill Paul 
3132bf6ef57aSJohn Polstra /*
3133bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
3134bf6ef57aSJohn Polstra  */
3135bf6ef57aSJohn Polstra static int
3136bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
3137bf6ef57aSJohn Polstra {
3138bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
313955aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
3140bf6ef57aSJohn Polstra 
314155aaf894SMarius Strobl 	d = pci_get_domain(dev);
3142bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
3143bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
3144bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
3145bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
314655aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
3147bf6ef57aSJohn Polstra 			return (1);
3148bf6ef57aSJohn Polstra 	return (0);
3149bf6ef57aSJohn Polstra }
3150bf6ef57aSJohn Polstra 
3151bf6ef57aSJohn Polstra /*
3152bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
3153bf6ef57aSJohn Polstra  */
3154bf6ef57aSJohn Polstra static int
3155bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
3156bf6ef57aSJohn Polstra {
3157bf6ef57aSJohn Polstra 	int can_use_msi = 0;
3158bf6ef57aSJohn Polstra 
3159d9fc28e4SPyun YongHyeon 	if (sc->bge_msi == 0)
31605c952e8dSPyun YongHyeon 		return (0);
31615c952e8dSPyun YongHyeon 
31621108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
31631108273aSPyun YongHyeon #ifdef DEVICE_POLLING
31641108273aSPyun YongHyeon 	return (0);
31651108273aSPyun YongHyeon #endif
3166bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
3167a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
3168bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
3169bf6ef57aSJohn Polstra 		/*
3170a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
3171a8376f70SMarius Strobl 		 * configured in single-port mode.
3172bf6ef57aSJohn Polstra 		 */
3173bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
3174bf6ef57aSJohn Polstra 			can_use_msi = 1;
3175bf6ef57aSJohn Polstra 		break;
3176bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
3177bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
3178bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
3179bf6ef57aSJohn Polstra 			can_use_msi = 1;
3180bf6ef57aSJohn Polstra 		break;
318191c69b97SEugene Grosbein 	case BGE_ASICREV_BCM5784:
318291c69b97SEugene Grosbein 		/*
318391c69b97SEugene Grosbein 		 * Prevent infinite "watchdog timeout" errors
318491c69b97SEugene Grosbein 		 * in some MacBook Pro and make it work out-of-the-box.
318591c69b97SEugene Grosbein 		 */
318691c69b97SEugene Grosbein 		if (sc->bge_chiprev == BGE_CHIPREV_5784_AX)
318791c69b97SEugene Grosbein 			break;
318891c69b97SEugene Grosbein 		/* FALLTHROUGH */
3189a8376f70SMarius Strobl 	default:
3190a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
3191bf6ef57aSJohn Polstra 			can_use_msi = 1;
3192bf6ef57aSJohn Polstra 	}
3193bf6ef57aSJohn Polstra 	return (can_use_msi);
3194bf6ef57aSJohn Polstra }
3195bf6ef57aSJohn Polstra 
319695d67482SBill Paul static int
3197062af0b0SPyun YongHyeon bge_mbox_reorder(struct bge_softc *sc)
3198062af0b0SPyun YongHyeon {
3199062af0b0SPyun YongHyeon 	/* Lists of PCI bridges that are known to reorder mailbox writes. */
3200062af0b0SPyun YongHyeon 	static const struct mbox_reorder {
3201062af0b0SPyun YongHyeon 		const uint16_t vendor;
3202062af0b0SPyun YongHyeon 		const uint16_t device;
3203062af0b0SPyun YongHyeon 		const char *desc;
320429658c96SDimitry Andric 	} mbox_reorder_lists[] = {
3205062af0b0SPyun YongHyeon 		{ 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
3206062af0b0SPyun YongHyeon 	};
3207062af0b0SPyun YongHyeon 	devclass_t pci, pcib;
3208062af0b0SPyun YongHyeon 	device_t bus, dev;
320947f4a4dcSMarius Strobl 	int i;
3210062af0b0SPyun YongHyeon 
3211062af0b0SPyun YongHyeon 	pci = devclass_find("pci");
3212062af0b0SPyun YongHyeon 	pcib = devclass_find("pcib");
3213062af0b0SPyun YongHyeon 	dev = sc->bge_dev;
3214062af0b0SPyun YongHyeon 	bus = device_get_parent(dev);
3215062af0b0SPyun YongHyeon 	for (;;) {
3216062af0b0SPyun YongHyeon 		dev = device_get_parent(bus);
3217062af0b0SPyun YongHyeon 		bus = device_get_parent(dev);
3218062af0b0SPyun YongHyeon 		if (device_get_devclass(dev) != pcib)
3219062af0b0SPyun YongHyeon 			break;
3220a70e114dSAndriy Gapon 		if (device_get_devclass(bus) != pci)
3221a70e114dSAndriy Gapon 			break;
322247f4a4dcSMarius Strobl 		for (i = 0; i < nitems(mbox_reorder_lists); i++) {
3223062af0b0SPyun YongHyeon 			if (pci_get_vendor(dev) ==
3224062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].vendor &&
3225062af0b0SPyun YongHyeon 			    pci_get_device(dev) ==
3226062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].device) {
3227062af0b0SPyun YongHyeon 				device_printf(sc->bge_dev,
3228062af0b0SPyun YongHyeon 				    "enabling MBOX workaround for %s\n",
3229062af0b0SPyun YongHyeon 				    mbox_reorder_lists[i].desc);
3230062af0b0SPyun YongHyeon 				return (1);
3231062af0b0SPyun YongHyeon 			}
3232062af0b0SPyun YongHyeon 		}
3233062af0b0SPyun YongHyeon 	}
3234062af0b0SPyun YongHyeon 	return (0);
3235062af0b0SPyun YongHyeon }
3236062af0b0SPyun YongHyeon 
3237ea9c3a30SPyun YongHyeon static void
3238ea9c3a30SPyun YongHyeon bge_devinfo(struct bge_softc *sc)
3239ea9c3a30SPyun YongHyeon {
3240ea9c3a30SPyun YongHyeon 	uint32_t cfg, clk;
3241ea9c3a30SPyun YongHyeon 
3242ea9c3a30SPyun YongHyeon 	device_printf(sc->bge_dev,
3243ea9c3a30SPyun YongHyeon 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; ",
3244ea9c3a30SPyun YongHyeon 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev);
3245ea9c3a30SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
3246ea9c3a30SPyun YongHyeon 		printf("PCI-E\n");
3247ea9c3a30SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_PCIX) {
3248ea9c3a30SPyun YongHyeon 		printf("PCI-X ");
3249ea9c3a30SPyun YongHyeon 		cfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3250ea9c3a30SPyun YongHyeon 		if (cfg == BGE_MISCCFG_BOARD_ID_5704CIOBE)
3251ea9c3a30SPyun YongHyeon 			clk = 133;
3252ea9c3a30SPyun YongHyeon 		else {
3253ea9c3a30SPyun YongHyeon 			clk = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
3254ea9c3a30SPyun YongHyeon 			switch (clk) {
3255ea9c3a30SPyun YongHyeon 			case 0:
3256ea9c3a30SPyun YongHyeon 				clk = 33;
3257ea9c3a30SPyun YongHyeon 				break;
3258ea9c3a30SPyun YongHyeon 			case 2:
3259ea9c3a30SPyun YongHyeon 				clk = 50;
3260ea9c3a30SPyun YongHyeon 				break;
3261ea9c3a30SPyun YongHyeon 			case 4:
3262ea9c3a30SPyun YongHyeon 				clk = 66;
3263ea9c3a30SPyun YongHyeon 				break;
3264ea9c3a30SPyun YongHyeon 			case 6:
3265ea9c3a30SPyun YongHyeon 				clk = 100;
3266ea9c3a30SPyun YongHyeon 				break;
3267ea9c3a30SPyun YongHyeon 			case 7:
3268ea9c3a30SPyun YongHyeon 				clk = 133;
3269ea9c3a30SPyun YongHyeon 				break;
3270ea9c3a30SPyun YongHyeon 			}
3271ea9c3a30SPyun YongHyeon 		}
3272ea9c3a30SPyun YongHyeon 		printf("%u MHz\n", clk);
3273ea9c3a30SPyun YongHyeon 	} else {
3274ea9c3a30SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3275ea9c3a30SPyun YongHyeon 			printf("PCI on PCI-X ");
3276ea9c3a30SPyun YongHyeon 		else
3277ea9c3a30SPyun YongHyeon 			printf("PCI ");
3278ea9c3a30SPyun YongHyeon 		cfg = pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4);
3279ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_PCI_BUSSPEED)
3280ea9c3a30SPyun YongHyeon 			clk = 66;
3281ea9c3a30SPyun YongHyeon 		else
3282ea9c3a30SPyun YongHyeon 			clk = 33;
3283ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_32BIT_BUS)
3284ea9c3a30SPyun YongHyeon 			printf("%u MHz; 32bit\n", clk);
3285ea9c3a30SPyun YongHyeon 		else
3286ea9c3a30SPyun YongHyeon 			printf("%u MHz; 64bit\n", clk);
3287ea9c3a30SPyun YongHyeon 	}
3288ea9c3a30SPyun YongHyeon }
3289ea9c3a30SPyun YongHyeon 
3290062af0b0SPyun YongHyeon static int
32913f74909aSGleb Smirnoff bge_attach(device_t dev)
329295d67482SBill Paul {
3293fba8b109SMarcel Moolenaar 	if_t ifp;
329495d67482SBill Paul 	struct bge_softc *sc;
3295548c8f1aSPyun YongHyeon 	uint32_t hwcfg = 0, misccfg, pcistate;
329608013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
3297ad4328baSMarius Strobl 	int capmask, error, reg, rid, trys;
329895d67482SBill Paul 
329995d67482SBill Paul 	sc = device_get_softc(dev);
330095d67482SBill Paul 	sc->bge_dev = dev;
330195d67482SBill Paul 
3302e010b055SPyun YongHyeon 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
33036c3e93cbSGleb Smirnoff 	NET_TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
3304e010b055SPyun YongHyeon 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
3305dfe0df9aSPyun YongHyeon 
330695d67482SBill Paul 	pci_enable_busmaster(dev);
330795d67482SBill Paul 
3308ad4328baSMarius Strobl 	/*
3309ad4328baSMarius Strobl 	 * Allocate control/status registers.
3310ad4328baSMarius Strobl 	 */
3311736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
33125f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
331344f8f2fcSMarius Strobl 	    RF_ACTIVE);
331495d67482SBill Paul 
331595d67482SBill Paul 	if (sc->bge_res == NULL) {
3316548c8f1aSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map BAR0 memory\n");
331795d67482SBill Paul 		error = ENXIO;
331895d67482SBill Paul 		goto fail;
331995d67482SBill Paul 	}
332095d67482SBill Paul 
33214f09c4c7SMarius Strobl 	/* Save various chip information. */
3322548c8f1aSPyun YongHyeon 	sc->bge_func_addr = pci_get_function(dev);
3323d7acafa1SMarius Strobl 	sc->bge_chipid = bge_chipid(dev);
3324e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
3325e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
3326e53d81eeSPaul Saab 
3327a813ed78SPyun YongHyeon 	/* Set default PHY address. */
3328daeeb75cSPyun YongHyeon 	sc->bge_phy_addr = 1;
33291108273aSPyun YongHyeon 	 /*
33301108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
33311108273aSPyun YongHyeon 	  *
33321108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
33331108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
33341108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
33351108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
33361108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
3337bbe2ca75SPyun YongHyeon 	  * BCM5719  |   1   |   8   |   2   |   9   |
333850515680SPyun YongHyeon 	  * BCM5720  |   1   |   8   |   2   |   9   |
33391108273aSPyun YongHyeon 	  *
3340548c8f1aSPyun YongHyeon 	  *          | F2 Cu | F2 Sr | F3 Cu | F3 Sr |
3341548c8f1aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
3342548c8f1aSPyun YongHyeon 	  * BCM57XX  |   X   |   X   |   X   |   X   |
3343548c8f1aSPyun YongHyeon 	  * BCM5704  |   X   |   X   |   X   |   X   |
3344548c8f1aSPyun YongHyeon 	  * BCM5717  |   X   |   X   |   X   |   X   |
3345548c8f1aSPyun YongHyeon 	  * BCM5719  |   3   |   10  |   4   |   11  |
3346548c8f1aSPyun YongHyeon 	  * BCM5720  |   X   |   X   |   X   |   X   |
3347548c8f1aSPyun YongHyeon 	  *
33481108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
33491108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
33501108273aSPyun YongHyeon 	  */
3351bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
335250515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
335350515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
3354548c8f1aSPyun YongHyeon 		if (sc->bge_chipid != BGE_CHIPID_BCM5717_A0) {
33551108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
33561108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
3357daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33581108273aSPyun YongHyeon 			else
3359daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
3360bbe2ca75SPyun YongHyeon 		} else {
33611108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
33621108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
3363daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33641108273aSPyun YongHyeon 			else
3365daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
33661108273aSPyun YongHyeon 		}
33671108273aSPyun YongHyeon 	}
3368a813ed78SPyun YongHyeon 
33695fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
33705fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
337108013fd3SMarius Strobl 
33720dae9719SJung-uk Kim 	/* Save chipset family. */
33730dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
33742927f01fSPyun YongHyeon 	case BGE_ASICREV_BCM5762:
3375fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57765:
3376fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57766:
3377fe26ad88SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_57765_PLUS;
3378fe26ad88SPyun YongHyeon 		/* FALLTHROUGH */
33791108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3380bbe2ca75SPyun YongHyeon 	case BGE_ASICREV_BCM5719:
338150515680SPyun YongHyeon 	case BGE_ASICREV_BCM5720:
33821108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
33831108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
3384b4a256acSPyun YongHyeon 		    BGE_FLAG_JUMBO_FRAME;
338529b44b09SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
338629b44b09SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
338729b44b09SPyun YongHyeon 			/*
338829b44b09SPyun YongHyeon 			 * Enable work around for DMA engine miscalculation
338929b44b09SPyun YongHyeon 			 * of TXMBUF available space.
339029b44b09SPyun YongHyeon 			 */
339129b44b09SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_RDMA_BUG;
3392bbe2ca75SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3393bbe2ca75SPyun YongHyeon 			    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3394bbe2ca75SPyun YongHyeon 				/* Jumbo frame on BCM5719 A0 does not work. */
3395463a7e27SPyun YongHyeon 				sc->bge_flags &= ~BGE_FLAG_JUMBO;
3396bbe2ca75SPyun YongHyeon 			}
339729b44b09SPyun YongHyeon 		}
33981108273aSPyun YongHyeon 		break;
3399a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
3400a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
3401a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
3402a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
3403a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
3404a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
3405a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
3406a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
3407a5779553SStanislav Sedov 		break;
34080dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
34090dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
34100dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
34110dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
34127ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
34130dae9719SJung-uk Kim 		break;
34140dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
34150dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
34160dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
3417f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
34189fe569d8SXin LI 		/* FALLTHROUGH */
34190dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
34200dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
342138cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
34220dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
34239fe569d8SXin LI 		/* FALLTHROUGH */
34240dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
34250dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
34260dae9719SJung-uk Kim 		break;
34270dae9719SJung-uk Kim 	}
34280dae9719SJung-uk Kim 
3429548c8f1aSPyun YongHyeon 	/* Identify chips with APE processor. */
3430548c8f1aSPyun YongHyeon 	switch (sc->bge_asicrev) {
3431548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3432548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5719:
3433548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5720:
3434548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5761:
34352927f01fSPyun YongHyeon 	case BGE_ASICREV_BCM5762:
3436548c8f1aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_APE;
3437548c8f1aSPyun YongHyeon 		break;
3438548c8f1aSPyun YongHyeon 	}
3439548c8f1aSPyun YongHyeon 
3440548c8f1aSPyun YongHyeon 	/* Chips with APE need BAR2 access for APE registers/memory. */
3441548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) != 0) {
3442548c8f1aSPyun YongHyeon 		rid = PCIR_BAR(2);
3443548c8f1aSPyun YongHyeon 		sc->bge_res2 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
3444548c8f1aSPyun YongHyeon 		    RF_ACTIVE);
3445548c8f1aSPyun YongHyeon 		if (sc->bge_res2 == NULL) {
3446548c8f1aSPyun YongHyeon 			device_printf (sc->bge_dev,
3447548c8f1aSPyun YongHyeon 			    "couldn't map BAR2 memory\n");
3448548c8f1aSPyun YongHyeon 			error = ENXIO;
3449548c8f1aSPyun YongHyeon 			goto fail;
3450548c8f1aSPyun YongHyeon 		}
3451548c8f1aSPyun YongHyeon 
3452548c8f1aSPyun YongHyeon 		/* Enable APE register/memory access by host driver. */
3453548c8f1aSPyun YongHyeon 		pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
3454548c8f1aSPyun YongHyeon 		pcistate |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
3455548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
3456548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
3457548c8f1aSPyun YongHyeon 		pci_write_config(dev, BGE_PCI_PCISTATE, pcistate, 4);
3458548c8f1aSPyun YongHyeon 
3459548c8f1aSPyun YongHyeon 		bge_ape_lock_init(sc);
3460548c8f1aSPyun YongHyeon 		bge_ape_read_fw_ver(sc);
3461548c8f1aSPyun YongHyeon 	}
3462548c8f1aSPyun YongHyeon 
3463749a5269SMarius Strobl 	/* Add SYSCTLs, requires the chipset family to be set. */
3464749a5269SMarius Strobl 	bge_add_sysctls(sc);
3465749a5269SMarius Strobl 
3466a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
34671108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
34681108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3469a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3470a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
3471a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
3472a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
3473a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
3474a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
3475a813ed78SPyun YongHyeon 	else
3476a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
34777ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
34787ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
34797ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
3480a813ed78SPyun YongHyeon 
3481f681b29aSPyun YongHyeon 	/*
3482d4622124SPyun YongHyeon 	 * All Broadcom controllers have 4GB boundary DMA bug.
3483f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
3484f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3485f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3486f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
3487f681b29aSPyun YongHyeon 	 */
3488f681b29aSPyun YongHyeon 	sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
34894f0794ffSBjoern A. Zeeb 
3490d9820cd8SPyun YongHyeon 	/* BCM5755 or higher and BCM5906 have short DMA bug. */
3491d9820cd8SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
3492d9820cd8SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
3493d9820cd8SPyun YongHyeon 
3494a7fcfcf3SPyun YongHyeon 	/*
3495a7fcfcf3SPyun YongHyeon 	 * BCM5719 cannot handle DMA requests for DMA segments that
3496a7fcfcf3SPyun YongHyeon 	 * have larger than 4KB in size.  However the maximum DMA
3497a7fcfcf3SPyun YongHyeon 	 * segment size created in DMA tag is 4KB for TSO, so we
3498a7fcfcf3SPyun YongHyeon 	 * wouldn't encounter the issue here.
3499a7fcfcf3SPyun YongHyeon 	 */
3500a7fcfcf3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3501a7fcfcf3SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3502a7fcfcf3SPyun YongHyeon 
3503ea9c3a30SPyun YongHyeon 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3504fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
35054f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
35064f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
35074f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
350884ac96f8SPyun YongHyeon 	}
35094f0794ffSBjoern A. Zeeb 
3510fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
3511fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3512fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
3513fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3514fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
3515fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3516fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3517fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3518fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
3519fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3520fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3521fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3522fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3523d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57791 ||
3524d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57795 ||
3525fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3526fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
3527fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
3528d73ea7c6SPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
3529fb772a6cSMarius Strobl 	}
3530fb772a6cSMarius Strobl 
3531e53d81eeSPaul Saab 	/*
3532ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
3533ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
3534ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
3535ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
3536d7acafa1SMarius Strobl 	 * known bug which can't handle TSO if Ethernet header + IP/TCP
3537d7acafa1SMarius Strobl 	 * header is greater than 80 bytes. A workaround for the TSO
3538ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
3539ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
3540ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
3541ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
3542ca3f1187SPyun YongHyeon 	 */
35431108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
35441108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
35451108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
3546bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3547bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3548bbe2ca75SPyun YongHyeon 			/* TSO on BCM5719 A0 does not work. */
3549bbe2ca75SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_TSO3;
3550bbe2ca75SPyun YongHyeon 		}
35511108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
35524f4a16e1SPyun YongHyeon 		/*
35534f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
35544f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
3555be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
35564f4a16e1SPyun YongHyeon 		 */
35574f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3558be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3559be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3560ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
35614f4a16e1SPyun YongHyeon 	}
3562ca3f1187SPyun YongHyeon 
3563ca3f1187SPyun YongHyeon 	/*
35646f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
3565e53d81eeSPaul Saab 	 */
35663b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
35674c0da0ffSGleb Smirnoff 		/*
35686f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
35696f8718a3SScott Long 		 * must be a PCI Express device.
35706f8718a3SScott Long 		 */
35716f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
35720aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
357348630d79SPyun YongHyeon 		/* Extract supported maximum payload size. */
357448630d79SPyun YongHyeon 		sc->bge_mps = pci_read_config(dev, sc->bge_expcap +
357548630d79SPyun YongHyeon 		    PCIER_DEVICE_CAP, 2);
357648630d79SPyun YongHyeon 		sc->bge_mps = 128 << (sc->bge_mps & PCIEM_CAP_MAX_PAYLOAD);
357750515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
357850515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
357948630d79SPyun YongHyeon 			sc->bge_expmrq = 2048;
358048630d79SPyun YongHyeon 		else
358148630d79SPyun YongHyeon 			sc->bge_expmrq = 4096;
358248630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
35836f8718a3SScott Long 	} else {
35846f8718a3SScott Long 		/*
35856f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
35866f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
35874c0da0ffSGleb Smirnoff 		 */
35883b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
35890aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
359090447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
35914c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
3592652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
35936f8718a3SScott Long 	}
35944c0da0ffSGleb Smirnoff 
3595bf6ef57aSJohn Polstra 	/*
3596fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3597fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
3598fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3599fd4d32feSPyun YongHyeon 	 */
3600fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3601fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3602fd4d32feSPyun YongHyeon 	/*
3603062af0b0SPyun YongHyeon 	 * Some PCI-X bridges are known to trigger write reordering to
3604062af0b0SPyun YongHyeon 	 * the mailbox registers. Typical phenomena is watchdog timeouts
3605062af0b0SPyun YongHyeon 	 * caused by out-of-order TX completions.  Enable workaround for
3606062af0b0SPyun YongHyeon 	 * PCI-X devices that live behind these bridges.
3607062af0b0SPyun YongHyeon 	 * Note, PCI-X controllers can run in PCI mode so we can't use
3608062af0b0SPyun YongHyeon 	 * BGE_FLAG_PCIX flag to detect PCI-X controllers.
3609062af0b0SPyun YongHyeon 	 */
3610062af0b0SPyun YongHyeon 	if (sc->bge_pcixcap != 0 && bge_mbox_reorder(sc) != 0)
3611062af0b0SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_MBOX_REORDER;
3612062af0b0SPyun YongHyeon 	/*
3613bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
3614bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
3615bf6ef57aSJohn Polstra 	 * normal operation.
3616bf6ef57aSJohn Polstra 	 */
36170aaf1057SPyun YongHyeon 	rid = 0;
36183b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
36190aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
3620ad4328baSMarius Strobl 		reg = 1;
3621ad4328baSMarius Strobl 		if (bge_can_use_msi(sc) && pci_alloc_msi(dev, &reg) == 0) {
3622bf6ef57aSJohn Polstra 			rid = 1;
3623bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
36240aaf1057SPyun YongHyeon 		}
36250aaf1057SPyun YongHyeon 	}
3626bf6ef57aSJohn Polstra 
36271108273aSPyun YongHyeon 	/*
36281108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
36291108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
36301108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
36311108273aSPyun YongHyeon 	 */
36321108273aSPyun YongHyeon #ifndef DEVICE_POLLING
36331108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
36341108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
36351108273aSPyun YongHyeon #endif
36361108273aSPyun YongHyeon 
3637bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3638ad4328baSMarius Strobl 	    RF_ACTIVE | (rid != 0 ? 0 : RF_SHAREABLE));
3639bf6ef57aSJohn Polstra 
3640bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
3641bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
3642bf6ef57aSJohn Polstra 		error = ENXIO;
3643bf6ef57aSJohn Polstra 		goto fail;
3644bf6ef57aSJohn Polstra 	}
3645bf6ef57aSJohn Polstra 
3646ea9c3a30SPyun YongHyeon 	bge_devinfo(sc);
36474f09c4c7SMarius Strobl 
36488cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
3649548c8f1aSPyun YongHyeon 	/* No ASF if APE present. */
3650548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) == 0) {
3651888b47f0SPyun YongHyeon 		if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3652888b47f0SPyun YongHyeon 		    BGE_SRAM_DATA_SIG_MAGIC)) {
3653548c8f1aSPyun YongHyeon 			if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG) &
3654548c8f1aSPyun YongHyeon 			    BGE_HWCFG_ASF) {
36558cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_ENABLE;
36568cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_STACKUP;
3657d67eba2fSPyun YongHyeon 				if (BGE_IS_575X_PLUS(sc))
36588cb1383cSDoug Ambrisko 					sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
36598cb1383cSDoug Ambrisko 			}
36608cb1383cSDoug Ambrisko 		}
3661548c8f1aSPyun YongHyeon 	}
36628cb1383cSDoug Ambrisko 
36638cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
36643dd76c98SPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
36658cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
36668cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
36678cb1383cSDoug Ambrisko 		error = ENXIO;
36688cb1383cSDoug Ambrisko 		goto fail;
36698cb1383cSDoug Ambrisko 	}
36708cb1383cSDoug Ambrisko 
36713dd76c98SPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
36723dd76c98SPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
367395d67482SBill Paul 
367495d67482SBill Paul 	if (bge_chipinit(sc)) {
3675fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
367695d67482SBill Paul 		error = ENXIO;
367795d67482SBill Paul 		goto fail;
367895d67482SBill Paul 	}
367995d67482SBill Paul 
368038cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
368138cc658fSJohn Baldwin 	if (error) {
368208013fd3SMarius Strobl 		device_printf(sc->bge_dev,
368308013fd3SMarius Strobl 		    "failed to read station address\n");
368495d67482SBill Paul 		error = ENXIO;
368595d67482SBill Paul 		goto fail;
368695d67482SBill Paul 	}
368795d67482SBill Paul 
3688f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
36891108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
36901108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
36911108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3692f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3693f41ac2beSBill Paul 	else
3694f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3695f41ac2beSBill Paul 
36965b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3697fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3698fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3699f41ac2beSBill Paul 		error = ENXIO;
3700f41ac2beSBill Paul 		goto fail;
3701f41ac2beSBill Paul 	}
3702f41ac2beSBill Paul 
370395d67482SBill Paul 	/* Set default tuneable values. */
370495d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
370595d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
370695d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
37076f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
37086f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
370995d67482SBill Paul 
371035f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
371135f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
371235f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
371335f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
371435f945cdSPyun YongHyeon 
371595d67482SBill Paul 	/* Set up ifnet structure */
3716fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3717fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3718fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3719fc74a9f9SBrooks Davis 		error = ENXIO;
3720fc74a9f9SBrooks Davis 		goto fail;
3721fc74a9f9SBrooks Davis 	}
3722fba8b109SMarcel Moolenaar 	if_setsoftc(ifp, sc);
37239bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
3724fba8b109SMarcel Moolenaar 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
3725fba8b109SMarcel Moolenaar 	if_setioctlfn(ifp, bge_ioctl);
3726fba8b109SMarcel Moolenaar 	if_setstartfn(ifp, bge_start);
3727fba8b109SMarcel Moolenaar 	if_setinitfn(ifp, bge_init);
3728df360178SGleb Smirnoff 	if_setgetcounterfn(ifp, bge_get_counter);
37294a81240cSMarcel Moolenaar 	if_setsendqlen(ifp, BGE_TX_RING_CNT - 1);
3730fba8b109SMarcel Moolenaar 	if_setsendqready(ifp);
3731fba8b109SMarcel Moolenaar 	if_sethwassist(ifp, sc->bge_csum_features);
3732fba8b109SMarcel Moolenaar 	if_setcapabilities(ifp, IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
3733fba8b109SMarcel Moolenaar 	    IFCAP_VLAN_MTU);
37341108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3735fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, CSUM_TSO, 0);
3736fba8b109SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, IFCAP_TSO4 | IFCAP_VLAN_HWTSO, 0);
3737ca3f1187SPyun YongHyeon 	}
37384e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
3739fba8b109SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWCSUM, 0);
37404e35d186SJung-uk Kim #endif
3741fba8b109SMarcel Moolenaar 	if_setcapenable(ifp, if_getcapabilities(ifp));
374275719184SGleb Smirnoff #ifdef DEVICE_POLLING
3743fba8b109SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
374475719184SGleb Smirnoff #endif
374595d67482SBill Paul 
3746a1d52896SBill Paul 	/*
3747d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3748d375e524SGleb Smirnoff 	 * to hardware bugs.
3749d375e524SGleb Smirnoff 	 */
3750d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3751fba8b109SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, 0, IFCAP_HWCSUM);
3752fba8b109SMarcel Moolenaar 		if_setcapenablebit(ifp, 0, IFCAP_HWCSUM);
3753fba8b109SMarcel Moolenaar 		if_sethwassist(ifp, 0);
3754d375e524SGleb Smirnoff 	}
3755d375e524SGleb Smirnoff 
3756d375e524SGleb Smirnoff 	/*
3757a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
375841abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
375941abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
376041abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
376141abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
376241abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
376341abcc1bSPaul Saab 	 * SK-9D41.
3764a1d52896SBill Paul 	 */
3765888b47f0SPyun YongHyeon 	if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3766888b47f0SPyun YongHyeon 		hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
37675fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
37685fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3769f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3770f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3771fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3772f6789fbaSPyun YongHyeon 			error = ENXIO;
3773f6789fbaSPyun YongHyeon 			goto fail;
3774f6789fbaSPyun YongHyeon 		}
377541abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
377641abcc1bSPaul Saab 	}
377741abcc1bSPaul Saab 
377895d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3779ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3780ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
378170c2071bSPyun YongHyeon 		if (BGE_IS_5705_PLUS(sc)) {
3782ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
378370c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
378470c2071bSPyun YongHyeon 		} else
3785652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3786ea3b4127SPyun YongHyeon 	}
378795d67482SBill Paul 
378870c2071bSPyun YongHyeon 	/* Set various PHY bug flags. */
378970c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
379070c2071bSPyun YongHyeon 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
379170c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
379270c2071bSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
379370c2071bSPyun YongHyeon 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
379470c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
379570c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
379670c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
379770c2071bSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
379870c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
379970c2071bSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
380070c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
380170c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
3802fe26ad88SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780 &&
3803fe26ad88SPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc)) {
380470c2071bSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
380570c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
380670c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
380770c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
380870c2071bSPyun YongHyeon 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
380970c2071bSPyun YongHyeon 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
381070c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
381170c2071bSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
381270c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
381370c2071bSPyun YongHyeon 		} else
381470c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
381570c2071bSPyun YongHyeon 	}
381670c2071bSPyun YongHyeon 
381770c2071bSPyun YongHyeon 	/*
3818d73ea7c6SPyun YongHyeon 	 * Don't enable Ethernet@WireSpeed for the 5700 or the
381970c2071bSPyun YongHyeon 	 * 5705 A0 and A1 chips.
382070c2071bSPyun YongHyeon 	 */
382170c2071bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
382270c2071bSPyun YongHyeon 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
382370c2071bSPyun YongHyeon 	    (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
3824d73ea7c6SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)))
382570c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
382670c2071bSPyun YongHyeon 
3827652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
382809a8241fSGleb Smirnoff 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
38290c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
38300c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
38316098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
38326098821cSJung-uk Kim 		    0, NULL);
383395d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
383495d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3835da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
383695d67482SBill Paul 	} else {
383795d67482SBill Paul 		/*
38388cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
38398cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
38408cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
38418cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
38428cb1383cSDoug Ambrisko 		 * the PHY.
384395d67482SBill Paul 		 */
38444012d104SMarius Strobl 		trys = 0;
38458cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
38468cb1383cSDoug Ambrisko again:
38478cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
38488cb1383cSDoug Ambrisko 
3849fba8b109SMarcel Moolenaar 		error = mii_attach(dev, &sc->bge_miibus, ifp,
3850fba8b109SMarcel Moolenaar 		    (ifm_change_cb_t)bge_ifmedia_upd,
3851fba8b109SMarcel Moolenaar 		    (ifm_stat_cb_t)bge_ifmedia_sts, capmask, sc->bge_phy_addr,
3852fba8b109SMarcel Moolenaar 		    MII_OFFSET_ANY, MIIF_DOPAUSE);
38538e5d93dbSMarius Strobl 		if (error != 0) {
38548cb1383cSDoug Ambrisko 			if (trys++ < 4) {
38558cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
3856daeeb75cSPyun YongHyeon 				bge_miibus_writereg(sc->bge_dev,
3857daeeb75cSPyun YongHyeon 				    sc->bge_phy_addr, MII_BMCR, BMCR_RESET);
38588cb1383cSDoug Ambrisko 				goto again;
38598cb1383cSDoug Ambrisko 			}
38608e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
386195d67482SBill Paul 			goto fail;
386295d67482SBill Paul 		}
38638cb1383cSDoug Ambrisko 
38648cb1383cSDoug Ambrisko 		/*
38658cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
38668cb1383cSDoug Ambrisko 		 */
38678cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
38688cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
386995d67482SBill Paul 	}
387095d67482SBill Paul 
387195d67482SBill Paul 	/*
3872e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3873e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3874e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3875e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3876e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3877e255b776SJohn Polstra 	 * payloads by copying the received packets.
3878e255b776SJohn Polstra 	 */
3879652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3880652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3881652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3882e255b776SJohn Polstra 
3883e255b776SJohn Polstra 	/*
388495d67482SBill Paul 	 * Call MI attach routine.
388595d67482SBill Paul 	 */
3886fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
38870f9bd73bSSam Leffler 
388861ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
3889fba8b109SMarcel Moolenaar 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
389061ccb9daSPyun YongHyeon 
38910f9bd73bSSam Leffler 	/*
38920f9bd73bSSam Leffler 	 * Hookup IRQ last.
38930f9bd73bSSam Leffler 	 */
3894dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3895dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
38967e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
38977e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3898dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3899dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3900dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3901dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3902dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3903e010b055SPyun YongHyeon 			error = ENOMEM;
3904dfe0df9aSPyun YongHyeon 			goto fail;
3905dfe0df9aSPyun YongHyeon 		}
3906d7acafa1SMarius Strobl 		error = taskqueue_start_threads(&sc->bge_tq, 1, PI_NET,
3907d7acafa1SMarius Strobl 		    "%s taskq", device_get_nameunit(sc->bge_dev));
3908d7acafa1SMarius Strobl 		if (error != 0) {
3909d7acafa1SMarius Strobl 			device_printf(dev, "could not start threads.\n");
3910d7acafa1SMarius Strobl 			ether_ifdetach(ifp);
3911d7acafa1SMarius Strobl 			goto fail;
3912d7acafa1SMarius Strobl 		}
3913dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3914dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3915dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3916dfe0df9aSPyun YongHyeon 	} else
3917dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3918dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3919dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
39200f9bd73bSSam Leffler 
39210f9bd73bSSam Leffler 	if (error) {
3922e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
3923fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
3924ded66962SMark Johnston 		goto fail;
39250f9bd73bSSam Leffler 	}
392695d67482SBill Paul 
39277790c8c1SConrad Meyer 	/* Attach driver debugnet methods. */
39287790c8c1SConrad Meyer 	DEBUGNET_SET(ifp, bge);
3929ded66962SMark Johnston 
393095d67482SBill Paul fail:
3931e010b055SPyun YongHyeon 	if (error)
3932e010b055SPyun YongHyeon 		bge_detach(dev);
393395d67482SBill Paul 	return (error);
393495d67482SBill Paul }
393595d67482SBill Paul 
393695d67482SBill Paul static int
39373f74909aSGleb Smirnoff bge_detach(device_t dev)
393895d67482SBill Paul {
393995d67482SBill Paul 	struct bge_softc *sc;
3940fba8b109SMarcel Moolenaar 	if_t ifp;
394195d67482SBill Paul 
394295d67482SBill Paul 	sc = device_get_softc(dev);
3943fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
394495d67482SBill Paul 
394575719184SGleb Smirnoff #ifdef DEVICE_POLLING
3946fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING)
394775719184SGleb Smirnoff 		ether_poll_deregister(ifp);
394875719184SGleb Smirnoff #endif
394975719184SGleb Smirnoff 
3950e010b055SPyun YongHyeon 	if (device_is_attached(dev)) {
3951e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
39520f9bd73bSSam Leffler 		BGE_LOCK(sc);
395395d67482SBill Paul 		bge_stop(sc);
39540f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
39555dda8085SOleg Bulyzhin 		callout_drain(&sc->bge_stat_ch);
3956e010b055SPyun YongHyeon 	}
39575dda8085SOleg Bulyzhin 
3958dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3959dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
396095d67482SBill Paul 
39610aba72ddSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
396295d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
39630aba72ddSPyun YongHyeon 	else if (sc->bge_miibus != NULL) {
396495d67482SBill Paul 		bus_generic_detach(dev);
396595d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
396695d67482SBill Paul 	}
396795d67482SBill Paul 
396895d67482SBill Paul 	bge_release_resources(sc);
396995d67482SBill Paul 
397095d67482SBill Paul 	return (0);
397195d67482SBill Paul }
397295d67482SBill Paul 
397395d67482SBill Paul static void
39743f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
397595d67482SBill Paul {
397695d67482SBill Paul 	device_t dev;
397795d67482SBill Paul 
397895d67482SBill Paul 	dev = sc->bge_dev;
397995d67482SBill Paul 
3980dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3981dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3982dfe0df9aSPyun YongHyeon 
398395d67482SBill Paul 	if (sc->bge_intrhand != NULL)
398495d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
398595d67482SBill Paul 
3986ad4328baSMarius Strobl 	if (sc->bge_irq != NULL) {
3987724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3988ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_irq), sc->bge_irq);
3989724bd939SJohn Polstra 		pci_release_msi(dev);
3990ad4328baSMarius Strobl 	}
399195d67482SBill Paul 
399295d67482SBill Paul 	if (sc->bge_res != NULL)
399395d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
3994ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_res), sc->bge_res);
399595d67482SBill Paul 
3996548c8f1aSPyun YongHyeon 	if (sc->bge_res2 != NULL)
3997548c8f1aSPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY,
3998ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_res2), sc->bge_res2);
3999548c8f1aSPyun YongHyeon 
4000ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
4001ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
4002ad61f896SRuslan Ermilov 
4003f41ac2beSBill Paul 	bge_dma_free(sc);
400495d67482SBill Paul 
40050f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
40060f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
400795d67482SBill Paul }
400895d67482SBill Paul 
40098cb1383cSDoug Ambrisko static int
40103f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
401195d67482SBill Paul {
401295d67482SBill Paul 	device_t dev;
4013cc085b36SPyun YongHyeon 	uint32_t cachesize, command, mac_mode, mac_mode_mask, reset, val;
40146f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
40150aaf1057SPyun YongHyeon 	uint16_t devctl;
40165fea260fSMarius Strobl 	int i;
401795d67482SBill Paul 
401895d67482SBill Paul 	dev = sc->bge_dev;
401995d67482SBill Paul 
4020cc085b36SPyun YongHyeon 	mac_mode_mask = BGE_MACMODE_HALF_DUPLEX | BGE_MACMODE_PORTMODE;
4021548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4022548c8f1aSPyun YongHyeon 		mac_mode_mask |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
4023cc085b36SPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) & mac_mode_mask;
4024cc085b36SPyun YongHyeon 
402538cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
402638cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
40276f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
40286f8718a3SScott Long 			write_op = bge_writemem_direct;
40296f8718a3SScott Long 		else
40306f8718a3SScott Long 			write_op = bge_writemem_ind;
40319ba784dbSScott Long 	} else
40326f8718a3SScott Long 		write_op = bge_writereg_ind;
40336f8718a3SScott Long 
40343dd76c98SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
40353dd76c98SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5701) {
40363dd76c98SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
40373dd76c98SPyun YongHyeon 		for (i = 0; i < 8000; i++) {
40383dd76c98SPyun YongHyeon 			if (CSR_READ_4(sc, BGE_NVRAM_SWARB) &
40393dd76c98SPyun YongHyeon 			    BGE_NVRAMSWARB_GNT1)
40403dd76c98SPyun YongHyeon 				break;
40413dd76c98SPyun YongHyeon 			DELAY(20);
40423dd76c98SPyun YongHyeon 		}
40433dd76c98SPyun YongHyeon 		if (i == 8000) {
40443dd76c98SPyun YongHyeon 			if (bootverbose)
40453dd76c98SPyun YongHyeon 				device_printf(dev, "NVRAM lock timedout!\n");
40463dd76c98SPyun YongHyeon 		}
40473dd76c98SPyun YongHyeon 	}
4048548c8f1aSPyun YongHyeon 	/* Take APE lock when performing reset. */
4049548c8f1aSPyun YongHyeon 	bge_ape_lock(sc, BGE_APE_LOCK_GRC);
4050548c8f1aSPyun YongHyeon 
405195d67482SBill Paul 	/* Save some important PCI state. */
405295d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
405395d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
405495d67482SBill Paul 
405595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
405695d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4057e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
405895d67482SBill Paul 
40596f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
40606f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
4061a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
40626f8718a3SScott Long 		if (bootverbose)
4063333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
40646f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
40656f8718a3SScott Long 	}
40666f8718a3SScott Long 
40676f8718a3SScott Long 	/*
40686f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
40696f8718a3SScott Long 	 * When firmware finishes its initialization it will
4070888b47f0SPyun YongHyeon 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
40716f8718a3SScott Long 	 */
4072888b47f0SPyun YongHyeon 	bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
40736f8718a3SScott Long 
40740c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
4075e53d81eeSPaul Saab 
4076e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4077652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4078ad49eccfSPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
4079ad49eccfSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_5717_PLUS) == 0) {
40800c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
40810c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, 0x7E2C, 0x20);
4082ad49eccfSPyun YongHyeon 		}
4083e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
4084e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
40850c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
40860c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
4087e53d81eeSPaul Saab 		}
4088e53d81eeSPaul Saab 	}
4089e53d81eeSPaul Saab 
4090df4db538SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
4091df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
4092df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
4093df4db538SPyun YongHyeon 		    val | BGE_VCPU_STATUS_DRV_RESET);
4094df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
4095df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
4096df4db538SPyun YongHyeon 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
4097df4db538SPyun YongHyeon 	}
4098df4db538SPyun YongHyeon 
409921c9e407SDavid Christensen 	/*
41006f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
41016f8718a3SScott Long 	 * powered up in D0 uninitialized.
41026f8718a3SScott Long 	 */
41035512ca01SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc) &&
41045512ca01SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
4105caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
41066f8718a3SScott Long 
410795d67482SBill Paul 	/* Issue global reset */
41086f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
410995d67482SBill Paul 
4110cc085b36SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
4111cc085b36SPyun YongHyeon 		DELAY(100 * 1000);
4112cc085b36SPyun YongHyeon 	else
411395d67482SBill Paul 		DELAY(1000);
411495d67482SBill Paul 
4115e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4116652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4117e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
4118e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
41195fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
41205fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
4121e53d81eeSPaul Saab 		}
41220aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
4123389c8bd5SGavin Atkinson 		    sc->bge_expcap + PCIER_DEVICE_CTL, 2);
41240aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
4125389c8bd5SGavin Atkinson 		devctl &= ~(PCIEM_CTL_RELAXED_ORD_ENABLE |
4126389c8bd5SGavin Atkinson 		    PCIEM_CTL_NOSNOOP_ENABLE);
4127389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_CTL,
41280aaf1057SPyun YongHyeon 		    devctl, 2);
412948630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
41300aaf1057SPyun YongHyeon 		/* Clear error status. */
4131389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_STA,
4132389c8bd5SGavin Atkinson 		    PCIEM_STA_CORRECTABLE_ERROR |
4133389c8bd5SGavin Atkinson 		    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
4134389c8bd5SGavin Atkinson 		    PCIEM_STA_UNSUPPORTED_REQ, 2);
4135e53d81eeSPaul Saab 	}
4136e53d81eeSPaul Saab 
41373f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
413895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
413995d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4140e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
4141cc085b36SPyun YongHyeon 	val = BGE_PCISTATE_ROM_ENABLE | BGE_PCISTATE_ROM_RETRY_ENABLE;
4142cc085b36SPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0 &&
4143cc085b36SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_PCIX) != 0)
4144cc085b36SPyun YongHyeon 		val |= BGE_PCISTATE_RETRY_SAME_DMA;
4145548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4146548c8f1aSPyun YongHyeon 		val |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
4147548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
4148548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
4149cc085b36SPyun YongHyeon 	pci_write_config(dev, BGE_PCI_PCISTATE, val, 4);
415095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
415195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
4152cbb2b2feSPyun YongHyeon 	/*
4153cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
4154fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
4155cbb2b2feSPyun YongHyeon 	 * read stale status block.
4156cbb2b2feSPyun YongHyeon 	 */
4157cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
4158cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
4159cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
4160cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
4161cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
4162cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
4163cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4164cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4165cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
4166cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
4167cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4168cbb2b2feSPyun YongHyeon 		}
4169cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
4170cbb2b2feSPyun YongHyeon 		    devctl, 2);
4171cbb2b2feSPyun YongHyeon 	}
417222a4ecedSMarius Strobl 	/* Re-enable MSI, if necessary, and enable the memory arbiter. */
41734c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
4174bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
4175bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
41760aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
41770aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
41780aaf1057SPyun YongHyeon 			pci_write_config(dev,
41790aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
4180bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
4181bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
4182bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
4183bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
4184bf6ef57aSJohn Polstra 		}
41854c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
41864c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
41874c0da0ffSGleb Smirnoff 	} else
4188a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4189a7b0c314SPaul Saab 
4190cc085b36SPyun YongHyeon 	/* Fix up byte swapping. */
4191cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
4192cc085b36SPyun YongHyeon 
4193cc085b36SPyun YongHyeon 	val = CSR_READ_4(sc, BGE_MAC_MODE);
4194cc085b36SPyun YongHyeon 	val = (val & ~mac_mode_mask) | mac_mode;
4195cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
4196cc085b36SPyun YongHyeon 	DELAY(40);
4197cc085b36SPyun YongHyeon 
4198548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, BGE_APE_LOCK_GRC);
4199548c8f1aSPyun YongHyeon 
420038cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
420138cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
420238cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
420338cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
420438cc658fSJohn Baldwin 				break;
420538cc658fSJohn Baldwin 			DELAY(100);
420638cc658fSJohn Baldwin 		}
420738cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
4208333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
420938cc658fSJohn Baldwin 			return (1);
421038cc658fSJohn Baldwin 		}
421138cc658fSJohn Baldwin 	} else {
421295d67482SBill Paul 		/*
42136f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
421408013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
42155fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
42165fea260fSMarius Strobl 		 * address is fitted though.
421795d67482SBill Paul 		 */
421895d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
4219d5d23857SJung-uk Kim 			DELAY(10);
4220888b47f0SPyun YongHyeon 			val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
4221888b47f0SPyun YongHyeon 			if (val == ~BGE_SRAM_FW_MB_MAGIC)
422295d67482SBill Paul 				break;
422395d67482SBill Paul 		}
422495d67482SBill Paul 
42255fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
4226333704a3SPyun YongHyeon 			device_printf(dev,
4227333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
4228333704a3SPyun YongHyeon 			    val);
4229b4a256acSPyun YongHyeon 		/* BCM57765 A0 needs additional time before accessing. */
4230b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
4231b4a256acSPyun YongHyeon 			DELAY(10 * 1000);	/* XXX */
423238cc658fSJohn Baldwin 	}
423395d67482SBill Paul 
423495d67482SBill Paul 	/*
4235da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
4236da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
4237da3003f0SBill Paul 	 * to 1.2V.
4238da3003f0SBill Paul 	 */
4239652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
4240652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
42415fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
42425fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
42435fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
4244da3003f0SBill Paul 	}
4245da3003f0SBill Paul 
4246e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4247652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
4248b4a256acSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc) &&
4249a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
4250a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
4251a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
42525fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
42535fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
4254e53d81eeSPaul Saab 	}
42558cb1383cSDoug Ambrisko 
425650515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
425750515680SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
425850515680SPyun YongHyeon 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
425950515680SPyun YongHyeon 
42608cb1383cSDoug Ambrisko 	return (0);
426195d67482SBill Paul }
426295d67482SBill Paul 
4263e0b7b101SPyun YongHyeon static __inline void
4264e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
4265e0b7b101SPyun YongHyeon {
4266e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
4267e0b7b101SPyun YongHyeon 
4268e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
4269e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
4270e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
4271e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4272e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
4273e0b7b101SPyun YongHyeon }
4274e0b7b101SPyun YongHyeon 
4275e0b7b101SPyun YongHyeon static __inline void
4276e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
4277e0b7b101SPyun YongHyeon {
4278e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
4279e0b7b101SPyun YongHyeon 
4280e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
4281e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
4282e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
4283e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
4284e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
4285e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
4286e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4287e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
4288e0b7b101SPyun YongHyeon }
4289e0b7b101SPyun YongHyeon 
429095d67482SBill Paul /*
429195d67482SBill Paul  * Frame reception handling. This is called if there's a frame
429295d67482SBill Paul  * on the receive return list.
429395d67482SBill Paul  *
429495d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
42951be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
429695d67482SBill Paul  * 2) the frame is from the standard receive ring
429795d67482SBill Paul  */
429895d67482SBill Paul 
42991abcdbd1SAttilio Rao static int
4300dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
430195d67482SBill Paul {
4302fba8b109SMarcel Moolenaar 	if_t ifp;
43031abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
4304b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
430595d67482SBill Paul 
43067f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
43070f9bd73bSSam Leffler 
43083f74909aSGleb Smirnoff 	/* Nothing to do. */
43097f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
43101abcdbd1SAttilio Rao 		return (rx_npkts);
4311cfcb5025SOleg Bulyzhin 
4312fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
431395d67482SBill Paul 
4314f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
4315e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
4316f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
431715eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
4318f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
4319fba8b109SMarcel Moolenaar 	    if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
4320fba8b109SMarcel Moolenaar 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))
4321f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
432215eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
4323f41ac2beSBill Paul 
43247f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
432595d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
43263f74909aSGleb Smirnoff 		uint32_t		rxidx;
432795d67482SBill Paul 		struct mbuf		*m = NULL;
43283f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
432995d67482SBill Paul 		int			have_tag = 0;
433095d67482SBill Paul 
433175719184SGleb Smirnoff #ifdef DEVICE_POLLING
4332fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_POLLING) {
433375719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
433475719184SGleb Smirnoff 				break;
433575719184SGleb Smirnoff 			sc->rxcycles--;
433675719184SGleb Smirnoff 		}
433775719184SGleb Smirnoff #endif
433875719184SGleb Smirnoff 
43397f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
434095d67482SBill Paul 
434195d67482SBill Paul 		rxidx = cur_rx->bge_idx;
43427f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
434395d67482SBill Paul 
4344fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING &&
4345cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
434695d67482SBill Paul 			have_tag = 1;
434795d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
434895d67482SBill Paul 		}
434995d67482SBill Paul 
435095d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
435195d67482SBill Paul 			jumbocnt++;
4352943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
435395d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4354e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
435595d67482SBill Paul 				continue;
435695d67482SBill Paul 			}
4357943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
4358e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
4359df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
436095d67482SBill Paul 				continue;
436195d67482SBill Paul 			}
436203e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
436395d67482SBill Paul 		} else {
436495d67482SBill Paul 			stdcnt++;
4365e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
436695d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4367e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
436895d67482SBill Paul 				continue;
436995d67482SBill Paul 			}
4370943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
4371e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
4372df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
437395d67482SBill Paul 				continue;
437495d67482SBill Paul 			}
437503e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
437695d67482SBill Paul 		}
437795d67482SBill Paul 
4378df360178SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
4379e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
4380e255b776SJohn Polstra 		/*
4381e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
4382e65bed95SPyun YongHyeon 		 * the payload is aligned.
4383e255b776SJohn Polstra 		 */
4384652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
4385e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
4386e255b776SJohn Polstra 			    cur_rx->bge_len);
4387e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
4388e255b776SJohn Polstra 		}
4389e255b776SJohn Polstra #endif
4390473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
439195d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
439295d67482SBill Paul 
4393fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_RXCSUM)
43941108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
439595d67482SBill Paul 
439695d67482SBill Paul 		/*
4397673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
4398673d9191SSam Leffler 		 * attach that information to the packet.
439995d67482SBill Paul 		 */
4400d147662cSGleb Smirnoff 		if (have_tag) {
440178ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
440278ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
4403d147662cSGleb Smirnoff 		}
440495d67482SBill Paul 
4405dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
44060f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
4407fba8b109SMarcel Moolenaar 			if_input(ifp, m);
44080f9bd73bSSam Leffler 			BGE_LOCK(sc);
4409dfe0df9aSPyun YongHyeon 		} else
4410fba8b109SMarcel Moolenaar 			if_input(ifp, m);
4411d4da719cSAttilio Rao 		rx_npkts++;
441225e13e68SXin LI 
4413fba8b109SMarcel Moolenaar 		if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
44148cf7d13dSAttilio Rao 			return (rx_npkts);
441595d67482SBill Paul 	}
441695d67482SBill Paul 
441715eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
441815eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
4419e65bed95SPyun YongHyeon 	if (stdcnt > 0)
4420f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
4421e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
44224c0da0ffSGleb Smirnoff 
4423c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
4424f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
44254c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
4426f41ac2beSBill Paul 
44277f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
442838cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
442995d67482SBill Paul 	if (stdcnt)
4430767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
4431767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
443295d67482SBill Paul 	if (jumbocnt)
4433767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
4434767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
4435f5a034f9SPyun YongHyeon #ifdef notyet
4436f5a034f9SPyun YongHyeon 	/*
4437f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
4438f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
4439f5a034f9SPyun YongHyeon 	 */
4440f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
4441fba8b109SMarcel Moolenaar 		if_incierrors(ifp, CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS));
4442f5a034f9SPyun YongHyeon #endif
44431abcdbd1SAttilio Rao 	return (rx_npkts);
444495d67482SBill Paul }
444595d67482SBill Paul 
444695d67482SBill Paul static void
44471108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
44481108273aSPyun YongHyeon {
44491108273aSPyun YongHyeon 
44501108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
44511108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
44521108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44531108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44541108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
44551108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
44561108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44571108273aSPyun YongHyeon 			}
44581108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
44591108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
44601108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
44611108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44621108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
44631108273aSPyun YongHyeon 			}
44641108273aSPyun YongHyeon 		}
44651108273aSPyun YongHyeon 	} else {
44661108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44671108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44681108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
44691108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44701108273aSPyun YongHyeon 		}
44711108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
44721108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
44731108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
44741108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
44751108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44761108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
44771108273aSPyun YongHyeon 		}
44781108273aSPyun YongHyeon 	}
44791108273aSPyun YongHyeon }
44801108273aSPyun YongHyeon 
44811108273aSPyun YongHyeon static void
4482b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
448395d67482SBill Paul {
448495a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
4485fba8b109SMarcel Moolenaar 	if_t ifp;
448695d67482SBill Paul 
44870f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
44880f9bd73bSSam Leffler 
44893f74909aSGleb Smirnoff 	/* Nothing to do. */
4490b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
4491cfcb5025SOleg Bulyzhin 		return;
4492cfcb5025SOleg Bulyzhin 
4493fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
449495d67482SBill Paul 
4495e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
44965c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
449795d67482SBill Paul 	/*
449895d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
449995d67482SBill Paul 	 * frames that have been sent.
450095d67482SBill Paul 	 */
4501b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
450295a0a340SPyun YongHyeon 		uint32_t		idx;
450395d67482SBill Paul 
450495d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
4505f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
450695d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
4507df360178SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
450895d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
45090ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
4510e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
4511e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
45120ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
4513f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
4514e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
4515e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
451695d67482SBill Paul 		}
451795d67482SBill Paul 		sc->bge_txcnt--;
451895d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
451995d67482SBill Paul 	}
452095d67482SBill Paul 
4521fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
45225b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
45235b01e77cSBruce Evans 		sc->bge_timer = 0;
452495d67482SBill Paul }
452595d67482SBill Paul 
452675719184SGleb Smirnoff #ifdef DEVICE_POLLING
45271abcdbd1SAttilio Rao static int
4528fba8b109SMarcel Moolenaar bge_poll(if_t ifp, enum poll_cmd cmd, int count)
452975719184SGleb Smirnoff {
4530fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
4531b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4532366454f2SOleg Bulyzhin 	uint32_t statusword;
45331abcdbd1SAttilio Rao 	int rx_npkts = 0;
453475719184SGleb Smirnoff 
45353f74909aSGleb Smirnoff 	BGE_LOCK(sc);
4536fba8b109SMarcel Moolenaar 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
45373f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
45381abcdbd1SAttilio Rao 		return (rx_npkts);
45393f74909aSGleb Smirnoff 	}
454075719184SGleb Smirnoff 
4541dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4542b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4543b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
45442246e8c6SPyun YongHyeon 	/* Fetch updates from the status block. */
4545b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4546b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4547dab5cd05SOleg Bulyzhin 
4548175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
45492246e8c6SPyun YongHyeon 	/* Clear the status so the next pass only sees the changes. */
4550175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4551dab5cd05SOleg Bulyzhin 
4552dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4553b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4554b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4555366454f2SOleg Bulyzhin 
45560c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
4557366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
4558366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
4559366454f2SOleg Bulyzhin 
4560366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
4561366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
45624c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4563652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
4564366454f2SOleg Bulyzhin 			bge_link_upd(sc);
4565366454f2SOleg Bulyzhin 
4566366454f2SOleg Bulyzhin 	sc->rxcycles = count;
4567dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
4568fba8b109SMarcel Moolenaar 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
456925e13e68SXin LI 		BGE_UNLOCK(sc);
45708cf7d13dSAttilio Rao 		return (rx_npkts);
457125e13e68SXin LI 	}
4572b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
4573fba8b109SMarcel Moolenaar 	if (!if_sendq_empty(ifp))
4574366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
45753f74909aSGleb Smirnoff 
45763f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
45771abcdbd1SAttilio Rao 	return (rx_npkts);
457875719184SGleb Smirnoff }
457975719184SGleb Smirnoff #endif /* DEVICE_POLLING */
458075719184SGleb Smirnoff 
4581dfe0df9aSPyun YongHyeon static int
4582dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
4583dfe0df9aSPyun YongHyeon {
4584dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4585dfe0df9aSPyun YongHyeon 
4586dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4587dfe0df9aSPyun YongHyeon 	/*
4588dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
4589dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
4590dfe0df9aSPyun YongHyeon 	 */
4591dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4592dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
4593dfe0df9aSPyun YongHyeon }
4594dfe0df9aSPyun YongHyeon 
4595dfe0df9aSPyun YongHyeon static void
4596dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
4597dfe0df9aSPyun YongHyeon {
4598dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4599fba8b109SMarcel Moolenaar 	if_t ifp;
46001108273aSPyun YongHyeon 	uint32_t status, status_tag;
4601dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4602dfe0df9aSPyun YongHyeon 
4603dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4604dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
4605dfe0df9aSPyun YongHyeon 
460666151edfSPyun YongHyeon 	BGE_LOCK(sc);
4607fba8b109SMarcel Moolenaar 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
460866151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4609dfe0df9aSPyun YongHyeon 		return;
461066151edfSPyun YongHyeon 	}
4611dfe0df9aSPyun YongHyeon 
4612dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
4613dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4614dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4615dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4616dfe0df9aSPyun YongHyeon 
46172246e8c6SPyun YongHyeon 	/* Save producer/consumer indices. */
4618dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4619dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4620dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
46211108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
46222246e8c6SPyun YongHyeon 	/* Dirty the status flag. */
4623dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4624dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4625dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4626dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
46271108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
46281108273aSPyun YongHyeon 		status_tag = 0;
462966151edfSPyun YongHyeon 
463066151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
463166151edfSPyun YongHyeon 		bge_link_upd(sc);
463266151edfSPyun YongHyeon 
4633dfe0df9aSPyun YongHyeon 	/* Let controller work. */
46341108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4635dfe0df9aSPyun YongHyeon 
4636fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING &&
463766151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
4638dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
463966151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4640dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
464166151edfSPyun YongHyeon 		BGE_LOCK(sc);
4642dfe0df9aSPyun YongHyeon 	}
4643fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4644dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
4645dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
4646fba8b109SMarcel Moolenaar 		if (!if_sendq_empty(ifp))
4647dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
4648dfe0df9aSPyun YongHyeon 	}
464966151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
4650dfe0df9aSPyun YongHyeon }
4651dfe0df9aSPyun YongHyeon 
465295d67482SBill Paul static void
46533f74909aSGleb Smirnoff bge_intr(void *xsc)
465495d67482SBill Paul {
465595d67482SBill Paul 	struct bge_softc *sc;
4656fba8b109SMarcel Moolenaar 	if_t ifp;
4657dab5cd05SOleg Bulyzhin 	uint32_t statusword;
4658b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
465995d67482SBill Paul 
466095d67482SBill Paul 	sc = xsc;
4661f41ac2beSBill Paul 
46620f9bd73bSSam Leffler 	BGE_LOCK(sc);
46630f9bd73bSSam Leffler 
4664dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
4665dab5cd05SOleg Bulyzhin 
466675719184SGleb Smirnoff #ifdef DEVICE_POLLING
4667fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
466875719184SGleb Smirnoff 		BGE_UNLOCK(sc);
466975719184SGleb Smirnoff 		return;
467075719184SGleb Smirnoff 	}
467175719184SGleb Smirnoff #endif
467275719184SGleb Smirnoff 
4673f30cbfc6SScott Long 	/*
4674b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
4675b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
4676b848e032SBruce Evans 	 * our current organization this just gives complications and
4677b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
4678b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
4679b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
4680b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
4681b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
4682b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
4683b848e032SBruce Evans 	 *
4684b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
4685b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
4686b848e032SBruce Evans 	 * changing later because it is more efficient to get another
4687b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
4688b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
4689b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
4690b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
4691b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
4692b848e032SBruce Evans 	 */
469338cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4694b848e032SBruce Evans 
4695f584dfd1SPyun YongHyeon 	/*
4696f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
4697f584dfd1SPyun YongHyeon 	 */
4698f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4699f584dfd1SPyun YongHyeon 
4700f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
4701f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4702f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4703f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4704f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4705f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4706f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4707f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4708f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4709f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4710f584dfd1SPyun YongHyeon 
47111f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
47124c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4713f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
4714dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
471595d67482SBill Paul 
4716fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
47173f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
4718dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
471925e13e68SXin LI 	}
472095d67482SBill Paul 
4721fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
47223f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
4723b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
472495d67482SBill Paul 	}
472595d67482SBill Paul 
4726fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING &&
4727fba8b109SMarcel Moolenaar 	    !if_sendq_empty(ifp))
47280f9bd73bSSam Leffler 		bge_start_locked(ifp);
47290f9bd73bSSam Leffler 
47300f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
473195d67482SBill Paul }
473295d67482SBill Paul 
473395d67482SBill Paul static void
47348cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
47358cb1383cSDoug Ambrisko {
47368cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
47378cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
47388cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
47398cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
47408cb1383cSDoug Ambrisko 		else {
4741899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
4742888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
47433c201200SPyun YongHyeon 			    BGE_FW_CMD_DRV_ALIVE);
4744888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4745941a6e13SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4746941a6e13SPyun YongHyeon 			    BGE_FW_HB_TIMEOUT_SEC);
47473fed2d5dSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
47489931ba85SPyun YongHyeon 			    CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
47499931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT);
47508cb1383cSDoug Ambrisko 		}
47518cb1383cSDoug Ambrisko 	}
47528cb1383cSDoug Ambrisko }
47538cb1383cSDoug Ambrisko 
47548cb1383cSDoug Ambrisko static void
4755b74e67fbSGleb Smirnoff bge_tick(void *xsc)
47560f9bd73bSSam Leffler {
4757b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
475895d67482SBill Paul 	struct mii_data *mii = NULL;
475995d67482SBill Paul 
47600f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
476195d67482SBill Paul 
47625dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
47635dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
47645dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
47655dda8085SOleg Bulyzhin 		return;
47665dda8085SOleg Bulyzhin 
47677ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
47680434d1b8SBill Paul 		bge_stats_update_regs(sc);
47690434d1b8SBill Paul 	else
477095d67482SBill Paul 		bge_stats_update(sc);
477195d67482SBill Paul 
4772548c8f1aSPyun YongHyeon 	/* XXX Add APE heartbeat check here? */
4773548c8f1aSPyun YongHyeon 
4774652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
477595d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
477682b67c01SOleg Bulyzhin 		/*
477782b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
477882b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
477982b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
478082b67c01SOleg Bulyzhin 		 */
478182b67c01SOleg Bulyzhin 		if (!sc->bge_link)
478295d67482SBill Paul 			mii_tick(mii);
47837b97099dSOleg Bulyzhin 	} else {
47847b97099dSOleg Bulyzhin 		/*
47857b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
47867b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
47877b97099dSOleg Bulyzhin 		 * and trigger interrupt.
47887b97099dSOleg Bulyzhin 		 */
47897b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
47903f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
479159578ee0SSergey Kandaurov 		if (!(if_getcapenable(sc->bge_ifp) & IFCAP_POLLING))
47927b97099dSOleg Bulyzhin #endif
47937b97099dSOleg Bulyzhin 		{
47947b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
47954f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
47964f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
47977b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
47984f0794ffSBjoern A. Zeeb 		else
47994f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
48007b97099dSOleg Bulyzhin 		}
4801dab5cd05SOleg Bulyzhin 	}
480295d67482SBill Paul 
48038cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4804b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
48058cb1383cSDoug Ambrisko 
4806dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
480795d67482SBill Paul }
480895d67482SBill Paul 
480995d67482SBill Paul static void
48103f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
48110434d1b8SBill Paul {
48122280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
481329b44b09SPyun YongHyeon 	uint32_t val;
48140434d1b8SBill Paul 
48152280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
48160434d1b8SBill Paul 
48172280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
48182280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
48192280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
48202280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
48212280c16bSPyun YongHyeon 	stats->outXonSent +=
48222280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
48232280c16bSPyun YongHyeon 	stats->outXoffSent +=
48242280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
48252280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
48262280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
48272280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
48282280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
48292280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
48302280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
48312280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
48322280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
48332280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
48342280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
48352280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
48362280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
48372280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
48382280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
48392280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
48402280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
48412280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
48422280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
48437e6e2507SJung-uk Kim 
48442280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
48452280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
48462280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
48472280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
48482280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
48492280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
48502280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
48512280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
48522280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
48532280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
48542280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
48552280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
48562280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
48572280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
48582280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
48592280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
48602280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
48612280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
48622280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
48632280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
48642280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
48652280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
48662280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
48672280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
48682280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
48692280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
48702280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
48712280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
48722280c16bSPyun YongHyeon 
48732280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
48742280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
48752280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
48762280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
48772280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
48782280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
48792280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
48802280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4881f78094a5SPyun YongHyeon 	/*
4882f78094a5SPyun YongHyeon 	 * XXX
4883f78094a5SPyun YongHyeon 	 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4884f78094a5SPyun YongHyeon 	 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4885f78094a5SPyun YongHyeon 	 * includes number of unwanted multicast frames.  This comes
4886f78094a5SPyun YongHyeon 	 * from silicon bug and known workaround to get rough(not
4887f78094a5SPyun YongHyeon 	 * exact) counter is to enable interrupt on MBUF low water
4888f78094a5SPyun YongHyeon 	 * attention.  This can be accomplished by setting
4889f78094a5SPyun YongHyeon 	 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4890f78094a5SPyun YongHyeon 	 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4891f78094a5SPyun YongHyeon 	 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4892f78094a5SPyun YongHyeon 	 * However that change would generate more interrupts and
4893f78094a5SPyun YongHyeon 	 * there are still possibilities of losing multiple frames
4894f78094a5SPyun YongHyeon 	 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4895f78094a5SPyun YongHyeon 	 * Given that the workaround still would not get correct
4896f78094a5SPyun YongHyeon 	 * counter I don't think it's worth to implement it.  So
4897f78094a5SPyun YongHyeon 	 * ignore reading the counter on controllers that have the
4898f78094a5SPyun YongHyeon 	 * silicon bug.
4899f78094a5SPyun YongHyeon 	 */
4900f78094a5SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4901f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4902f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
49032280c16bSPyun YongHyeon 		stats->InputDiscards +=
49042280c16bSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
49052280c16bSPyun YongHyeon 	stats->InputErrors +=
49062280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
49072280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
49082280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
49092280c16bSPyun YongHyeon 
491029b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
491129b44b09SPyun YongHyeon 		/*
491229b44b09SPyun YongHyeon 		 * If controller transmitted more than BGE_NUM_RDMA_CHANNELS
491329b44b09SPyun YongHyeon 		 * frames, it's safe to disable workaround for DMA engine's
491429b44b09SPyun YongHyeon 		 * miscalculation of TXMBUF space.
491529b44b09SPyun YongHyeon 		 */
491629b44b09SPyun YongHyeon 		if (stats->ifHCOutUcastPkts + stats->ifHCOutMulticastPkts +
491729b44b09SPyun YongHyeon 		    stats->ifHCOutBroadcastPkts > BGE_NUM_RDMA_CHANNELS) {
491829b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
491929b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
492029b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5719;
492129b44b09SPyun YongHyeon 			else
492229b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5720;
492329b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
492429b44b09SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_RDMA_BUG;
492529b44b09SPyun YongHyeon 		}
492629b44b09SPyun YongHyeon 	}
49272280c16bSPyun YongHyeon }
49282280c16bSPyun YongHyeon 
49292280c16bSPyun YongHyeon static void
49302280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
49312280c16bSPyun YongHyeon {
49322280c16bSPyun YongHyeon 
49332280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
49342280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
49352280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
49362280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
49372280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
49382280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
49392280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
49402280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
49412280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
49422280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
49432280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
49442280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
49452280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
49462280c16bSPyun YongHyeon 
49472280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
49482280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
49492280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
49502280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
49512280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
49522280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
49532280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
49542280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
49552280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
49562280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
49572280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
49582280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
49592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
49602280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
49612280c16bSPyun YongHyeon 
49622280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
49632280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
49642280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
49652280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
49662280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
49672280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
49682280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
49690434d1b8SBill Paul }
49700434d1b8SBill Paul 
49710434d1b8SBill Paul static void
49723f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
497395d67482SBill Paul {
4974fba8b109SMarcel Moolenaar 	if_t ifp;
4975e907febfSPyun YongHyeon 	bus_size_t stats;
49767e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
497795d67482SBill Paul 
4978fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
497995d67482SBill Paul 
4980e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
4981e907febfSPyun YongHyeon 
4982e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
4983e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
498495d67482SBill Paul 
49858634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
4986df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cnt - sc->bge_tx_collisions);
49876fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
49886fb34dd2SOleg Bulyzhin 
498937ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, nicNoMoreRxBDs.bge_addr_lo);
4990df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_nobds);
499137ee7cc7SPyun YongHyeon 	sc->bge_rx_nobds = cnt;
499237ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, ifInErrors.bge_addr_lo);
4993df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_inerrs);
499437ee7cc7SPyun YongHyeon 	sc->bge_rx_inerrs = cnt;
49956fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
4996df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_discards);
49976fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
49986fb34dd2SOleg Bulyzhin 
49996fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
5000df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, cnt - sc->bge_tx_discards);
50016fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
500295d67482SBill Paul 
5003e907febfSPyun YongHyeon #undef	READ_STAT
500495d67482SBill Paul }
500595d67482SBill Paul 
500695d67482SBill Paul /*
5007d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
5008d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
5009d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
5010d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
5011d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
5012d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
5013d375e524SGleb Smirnoff  */
5014d375e524SGleb Smirnoff static __inline int
5015d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
5016d375e524SGleb Smirnoff {
5017d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
5018d375e524SGleb Smirnoff 	struct mbuf *last;
5019d375e524SGleb Smirnoff 
5020d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
5021d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
5022d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
5023d375e524SGleb Smirnoff 		last = m;
5024d375e524SGleb Smirnoff 	} else {
5025d375e524SGleb Smirnoff 		/*
5026d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
5027d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
5028d375e524SGleb Smirnoff 		 */
5029d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
5030d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
5031d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
5032d375e524SGleb Smirnoff 			struct mbuf *n;
5033d375e524SGleb Smirnoff 
5034c6499eccSGleb Smirnoff 			MGET(n, M_NOWAIT, MT_DATA);
5035d375e524SGleb Smirnoff 			if (n == NULL)
5036d375e524SGleb Smirnoff 				return (ENOBUFS);
5037d375e524SGleb Smirnoff 			n->m_len = 0;
5038d375e524SGleb Smirnoff 			last->m_next = n;
5039d375e524SGleb Smirnoff 			last = n;
5040d375e524SGleb Smirnoff 		}
5041d375e524SGleb Smirnoff 	}
5042d375e524SGleb Smirnoff 
5043d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
5044d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
5045d375e524SGleb Smirnoff 	last->m_len += padlen;
5046d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
5047d375e524SGleb Smirnoff 
5048d375e524SGleb Smirnoff 	return (0);
5049d375e524SGleb Smirnoff }
5050d375e524SGleb Smirnoff 
5051ca3f1187SPyun YongHyeon static struct mbuf *
5052d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
5053d598b626SPyun YongHyeon {
5054d598b626SPyun YongHyeon 	struct mbuf *n;
5055d598b626SPyun YongHyeon 	int found;
5056d598b626SPyun YongHyeon 
5057d598b626SPyun YongHyeon 	/*
5058d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
5059d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
5060d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
5061d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
5062d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
5063d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
5064d598b626SPyun YongHyeon 	 */
5065d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
5066d598b626SPyun YongHyeon 		if (n->m_len < 8) {
5067d598b626SPyun YongHyeon 			found++;
5068d598b626SPyun YongHyeon 			if (found > 1)
5069d598b626SPyun YongHyeon 				break;
5070d598b626SPyun YongHyeon 			continue;
5071d598b626SPyun YongHyeon 		}
5072d598b626SPyun YongHyeon 		found = 0;
5073d598b626SPyun YongHyeon 	}
5074d598b626SPyun YongHyeon 
5075d598b626SPyun YongHyeon 	if (found > 1) {
5076c6499eccSGleb Smirnoff 		n = m_defrag(m, M_NOWAIT);
5077d598b626SPyun YongHyeon 		if (n == NULL)
5078d598b626SPyun YongHyeon 			m_freem(m);
5079d598b626SPyun YongHyeon 	} else
5080d598b626SPyun YongHyeon 		n = m;
5081d598b626SPyun YongHyeon 	return (n);
5082d598b626SPyun YongHyeon }
5083d598b626SPyun YongHyeon 
5084d598b626SPyun YongHyeon static struct mbuf *
50851108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
50861108273aSPyun YongHyeon     uint16_t *flags)
5087ca3f1187SPyun YongHyeon {
5088ca3f1187SPyun YongHyeon 	struct ip *ip;
5089ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
5090ca3f1187SPyun YongHyeon 	struct mbuf *n;
5091ca3f1187SPyun YongHyeon 	uint16_t hlen;
50925b355c4fSPyun YongHyeon 	uint32_t poff;
5093ca3f1187SPyun YongHyeon 
5094ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
5095ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
5096c6499eccSGleb Smirnoff 		n = m_dup(m, M_NOWAIT);
5097ca3f1187SPyun YongHyeon 		m_freem(m);
5098ca3f1187SPyun YongHyeon 		if (n == NULL)
5099ca3f1187SPyun YongHyeon 			return (NULL);
5100ca3f1187SPyun YongHyeon 		m = n;
5101ca3f1187SPyun YongHyeon 	}
51025b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
5103ca3f1187SPyun YongHyeon 	if (m == NULL)
5104ca3f1187SPyun YongHyeon 		return (NULL);
51055b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
51065b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
5107ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
5108ca3f1187SPyun YongHyeon 	if (m == NULL)
5109ca3f1187SPyun YongHyeon 		return (NULL);
5110ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
51115b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
5112ca3f1187SPyun YongHyeon 	if (m == NULL)
5113ca3f1187SPyun YongHyeon 		return (NULL);
5114ca3f1187SPyun YongHyeon 	/*
5115ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
5116ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
5117ca3f1187SPyun YongHyeon 	 */
5118ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
511996486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
5120ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
5121ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
5122ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
512396486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
5124ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
5125ca3f1187SPyun YongHyeon 	/*
5126ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
5127ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
5128ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
51291108273aSPyun YongHyeon 	 * we only support hardware based TSO.
5130ca3f1187SPyun YongHyeon 	 */
51311108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
5132ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
51331108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
51341108273aSPyun YongHyeon 		/*
51351108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
51361108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
51371108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
51381108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
51391108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
51401108273aSPyun YongHyeon 		 * frames are supported.
51411108273aSPyun YongHyeon 		 */
51421108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
51431108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
51441108273aSPyun YongHyeon 	} else {
51451108273aSPyun YongHyeon 		/*
51461108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
51471108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
51481108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
51491108273aSPyun YongHyeon 		 * supported.
51501108273aSPyun YongHyeon 		 */
5151ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
51521108273aSPyun YongHyeon 	}
5153ca3f1187SPyun YongHyeon 	return (m);
5154ca3f1187SPyun YongHyeon }
5155ca3f1187SPyun YongHyeon 
5156d375e524SGleb Smirnoff /*
515795d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
515895d67482SBill Paul  * pointers to descriptors.
515995d67482SBill Paul  */
516095d67482SBill Paul static int
5161676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
516295d67482SBill Paul {
51637e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
5164f41ac2beSBill Paul 	bus_dmamap_t		map;
5165676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
5166676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
51677e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
5168ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
51697e27542aSGleb Smirnoff 	int			nsegs, i, error;
517095d67482SBill Paul 
51716909dc43SGleb Smirnoff 	csum_flags = 0;
5172ca3f1187SPyun YongHyeon 	mss = 0;
5173ca3f1187SPyun YongHyeon 	vlan_tag = 0;
5174d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
5175d598b626SPyun YongHyeon 	    m->m_next != NULL) {
5176d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
5177d598b626SPyun YongHyeon 		if (*m_head == NULL)
5178d598b626SPyun YongHyeon 			return (ENOBUFS);
5179d598b626SPyun YongHyeon 		m = *m_head;
5180d598b626SPyun YongHyeon 	}
5181ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
51821108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
5183ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
5184ca3f1187SPyun YongHyeon 			return (ENOBUFS);
5185ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
5186ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
518735f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
51886909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
51896909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
51906909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
51916909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
51926909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
51936909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
51946909dc43SGleb Smirnoff 				m_freem(m);
51956909dc43SGleb Smirnoff 				*m_head = NULL;
51966909dc43SGleb Smirnoff 				return (error);
51976909dc43SGleb Smirnoff 			}
51986909dc43SGleb Smirnoff 		}
51996909dc43SGleb Smirnoff 	}
52006909dc43SGleb Smirnoff 
52011108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
52021108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
52031108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
52041108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
52051108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
5206beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
5207d94f2b85SPyun YongHyeon 			/*
5208d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
5209d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
5210d94f2b85SPyun YongHyeon 			 * DMA read operation.
5211d94f2b85SPyun YongHyeon 			 */
5212beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
5213c6499eccSGleb Smirnoff 				m = m_defrag(m, M_NOWAIT);
5214d94f2b85SPyun YongHyeon 			else
5215c6499eccSGleb Smirnoff 				m = m_collapse(m, M_NOWAIT,
52161108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
5217261f04d6SPyun YongHyeon 			if (m == NULL)
5218261f04d6SPyun YongHyeon 				m = *m_head;
5219d94f2b85SPyun YongHyeon 			*m_head = m;
5220d94f2b85SPyun YongHyeon 		}
52211108273aSPyun YongHyeon 	}
5222d94f2b85SPyun YongHyeon 
52237e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
52240ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
5225676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
52267e27542aSGleb Smirnoff 	if (error == EFBIG) {
5227c6499eccSGleb Smirnoff 		m = m_collapse(m, M_NOWAIT, BGE_NSEG_NEW);
5228676ad2c9SGleb Smirnoff 		if (m == NULL) {
5229676ad2c9SGleb Smirnoff 			m_freem(*m_head);
5230676ad2c9SGleb Smirnoff 			*m_head = NULL;
52317e27542aSGleb Smirnoff 			return (ENOBUFS);
52327e27542aSGleb Smirnoff 		}
5233676ad2c9SGleb Smirnoff 		*m_head = m;
52340ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
52350ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
5236676ad2c9SGleb Smirnoff 		if (error) {
5237676ad2c9SGleb Smirnoff 			m_freem(m);
5238676ad2c9SGleb Smirnoff 			*m_head = NULL;
52397e27542aSGleb Smirnoff 			return (error);
52407e27542aSGleb Smirnoff 		}
5241676ad2c9SGleb Smirnoff 	} else if (error != 0)
5242676ad2c9SGleb Smirnoff 		return (error);
52437e27542aSGleb Smirnoff 
5244167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
5245167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
52460ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
524795d67482SBill Paul 		return (ENOBUFS);
52487e27542aSGleb Smirnoff 	}
52497e27542aSGleb Smirnoff 
52500ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
5251e65bed95SPyun YongHyeon 
5252ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
5253ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
5254ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
5255ca3f1187SPyun YongHyeon 	}
5256b77d3a3bSPyun YongHyeon 
5257b77d3a3bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5762 &&
5258b77d3a3bSPyun YongHyeon 	    (m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
5259b77d3a3bSPyun YongHyeon 		/*
5260b77d3a3bSPyun YongHyeon 		 * 5725 family of devices corrupts TSO packets when TSO DMA
5261b77d3a3bSPyun YongHyeon 		 * buffers cross into regions which are within MSS bytes of
5262b77d3a3bSPyun YongHyeon 		 * a 4GB boundary.  If we encounter the condition, drop the
5263b77d3a3bSPyun YongHyeon 		 * packet.
5264b77d3a3bSPyun YongHyeon 		 */
5265b77d3a3bSPyun YongHyeon 		for (i = 0; ; i++) {
5266b77d3a3bSPyun YongHyeon 			d = &sc->bge_ldata.bge_tx_ring[idx];
5267b77d3a3bSPyun YongHyeon 			d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
5268b77d3a3bSPyun YongHyeon 			d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
5269b77d3a3bSPyun YongHyeon 			d->bge_len = segs[i].ds_len;
5270b77d3a3bSPyun YongHyeon 			if (d->bge_addr.bge_addr_lo + segs[i].ds_len + mss <
5271b77d3a3bSPyun YongHyeon 			    d->bge_addr.bge_addr_lo)
5272b77d3a3bSPyun YongHyeon 				break;
5273b77d3a3bSPyun YongHyeon 			d->bge_flags = csum_flags;
5274b77d3a3bSPyun YongHyeon 			d->bge_vlan_tag = vlan_tag;
5275b77d3a3bSPyun YongHyeon 			d->bge_mss = mss;
5276b77d3a3bSPyun YongHyeon 			if (i == nsegs - 1)
5277b77d3a3bSPyun YongHyeon 				break;
5278b77d3a3bSPyun YongHyeon 			BGE_INC(idx, BGE_TX_RING_CNT);
5279b77d3a3bSPyun YongHyeon 		}
5280b77d3a3bSPyun YongHyeon 		if (i != nsegs - 1) {
5281b77d3a3bSPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map,
5282b77d3a3bSPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
5283b77d3a3bSPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
5284b77d3a3bSPyun YongHyeon 			m_freem(*m_head);
5285b77d3a3bSPyun YongHyeon 			*m_head = NULL;
5286b77d3a3bSPyun YongHyeon 			return (EIO);
5287b77d3a3bSPyun YongHyeon 		}
5288b77d3a3bSPyun YongHyeon 	} else {
52897e27542aSGleb Smirnoff 		for (i = 0; ; i++) {
52907e27542aSGleb Smirnoff 			d = &sc->bge_ldata.bge_tx_ring[idx];
52917e27542aSGleb Smirnoff 			d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
52927e27542aSGleb Smirnoff 			d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
52937e27542aSGleb Smirnoff 			d->bge_len = segs[i].ds_len;
52947e27542aSGleb Smirnoff 			d->bge_flags = csum_flags;
5295ca3f1187SPyun YongHyeon 			d->bge_vlan_tag = vlan_tag;
5296ca3f1187SPyun YongHyeon 			d->bge_mss = mss;
52977e27542aSGleb Smirnoff 			if (i == nsegs - 1)
52987e27542aSGleb Smirnoff 				break;
52997e27542aSGleb Smirnoff 			BGE_INC(idx, BGE_TX_RING_CNT);
53007e27542aSGleb Smirnoff 		}
5301b77d3a3bSPyun YongHyeon 	}
53027e27542aSGleb Smirnoff 
53037e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
53047e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
5305676ad2c9SGleb Smirnoff 
5306f41ac2beSBill Paul 	/*
5307f41ac2beSBill Paul 	 * Insure that the map for this transmission
5308f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
5309f41ac2beSBill Paul 	 * in this chain.
5310f41ac2beSBill Paul 	 */
53117e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
53127e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
5313676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
53147e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
531595d67482SBill Paul 
53167e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
53177e27542aSGleb Smirnoff 	*txidx = idx;
531895d67482SBill Paul 
531995d67482SBill Paul 	return (0);
532095d67482SBill Paul }
532195d67482SBill Paul 
532295d67482SBill Paul /*
532395d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
532495d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
532595d67482SBill Paul  */
532695d67482SBill Paul static void
5327fba8b109SMarcel Moolenaar bge_start_locked(if_t ifp)
532895d67482SBill Paul {
532995d67482SBill Paul 	struct bge_softc *sc;
5330167fdb62SPyun YongHyeon 	struct mbuf *m_head;
533114bbd30fSGleb Smirnoff 	uint32_t prodidx;
5332167fdb62SPyun YongHyeon 	int count;
533395d67482SBill Paul 
5334fba8b109SMarcel Moolenaar 	sc = if_getsoftc(ifp);
5335167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
533695d67482SBill Paul 
5337167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
5338fba8b109SMarcel Moolenaar 	    (if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
5339167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
534095d67482SBill Paul 		return;
534195d67482SBill Paul 
534214bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
534395d67482SBill Paul 
5344fba8b109SMarcel Moolenaar 	for (count = 0; !if_sendq_empty(ifp);) {
5345167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
5346fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5347167fdb62SPyun YongHyeon 			break;
5348167fdb62SPyun YongHyeon 		}
5349fba8b109SMarcel Moolenaar 		m_head = if_dequeue(ifp);
535095d67482SBill Paul 		if (m_head == NULL)
535195d67482SBill Paul 			break;
535295d67482SBill Paul 
535395d67482SBill Paul 		/*
535495d67482SBill Paul 		 * Pack the data into the transmit ring. If we
535595d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
535695d67482SBill Paul 		 * for the NIC to drain the ring.
535795d67482SBill Paul 		 */
5358676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
5359676ad2c9SGleb Smirnoff 			if (m_head == NULL)
5360676ad2c9SGleb Smirnoff 				break;
5361fba8b109SMarcel Moolenaar 			if_sendq_prepend(ifp, m_head);
5362fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
536395d67482SBill Paul 			break;
536495d67482SBill Paul 		}
5365303a718cSDag-Erling Smørgrav 		++count;
536695d67482SBill Paul 
536795d67482SBill Paul 		/*
536895d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
536995d67482SBill Paul 		 * to him.
537095d67482SBill Paul 		 */
5371fba8b109SMarcel Moolenaar 		if_bpfmtap(ifp, m_head);
537295d67482SBill Paul 	}
537395d67482SBill Paul 
5374ded66962SMark Johnston 	if (count > 0)
5375ded66962SMark Johnston 		bge_start_tx(sc, prodidx);
5376ded66962SMark Johnston }
5377ded66962SMark Johnston 
5378ded66962SMark Johnston static void
5379ded66962SMark Johnston bge_start_tx(struct bge_softc *sc, uint32_t prodidx)
5380ded66962SMark Johnston {
5381ded66962SMark Johnston 
5382aa94f333SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
53835c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
53843f74909aSGleb Smirnoff 	/* Transmit. */
538538cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
53863927098fSPaul Saab 	/* 5700 b2 errata */
5387e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
538838cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
538995d67482SBill Paul 
539014bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
539114bbd30fSGleb Smirnoff 
5392ded66962SMark Johnston 	/* Set a timeout in case the chip goes out to lunch. */
5393b584d2b3SPyun YongHyeon 	sc->bge_timer = BGE_TX_TIMEOUT;
539495d67482SBill Paul }
539595d67482SBill Paul 
53960f9bd73bSSam Leffler /*
53970f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
53980f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
53990f9bd73bSSam Leffler  */
540095d67482SBill Paul static void
5401fba8b109SMarcel Moolenaar bge_start(if_t ifp)
540295d67482SBill Paul {
54030f9bd73bSSam Leffler 	struct bge_softc *sc;
54040f9bd73bSSam Leffler 
5405fba8b109SMarcel Moolenaar 	sc = if_getsoftc(ifp);
54060f9bd73bSSam Leffler 	BGE_LOCK(sc);
54070f9bd73bSSam Leffler 	bge_start_locked(ifp);
54080f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
54090f9bd73bSSam Leffler }
54100f9bd73bSSam Leffler 
54110f9bd73bSSam Leffler static void
54123f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
54130f9bd73bSSam Leffler {
5414fba8b109SMarcel Moolenaar 	if_t ifp;
54153f74909aSGleb Smirnoff 	uint16_t *m;
5416f6a65488SPyun YongHyeon 	uint32_t mode;
541795d67482SBill Paul 
54180f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
541995d67482SBill Paul 
5420fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
542195d67482SBill Paul 
5422fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
542395d67482SBill Paul 		return;
542495d67482SBill Paul 
542595d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
542695d67482SBill Paul 	bge_stop(sc);
54278cb1383cSDoug Ambrisko 
54288cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
54298cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
543095d67482SBill Paul 	bge_reset(sc);
54318cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
54328cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
54338cb1383cSDoug Ambrisko 
543495d67482SBill Paul 	bge_chipinit(sc);
543595d67482SBill Paul 
543695d67482SBill Paul 	/*
543795d67482SBill Paul 	 * Init the various state machines, ring
543895d67482SBill Paul 	 * control blocks and firmware.
543995d67482SBill Paul 	 */
544095d67482SBill Paul 	if (bge_blockinit(sc)) {
5441fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
544295d67482SBill Paul 		return;
544395d67482SBill Paul 	}
544495d67482SBill Paul 
5445fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
544695d67482SBill Paul 
544795d67482SBill Paul 	/* Specify MTU. */
5448fba8b109SMarcel Moolenaar 	CSR_WRITE_4(sc, BGE_RX_MTU, if_getmtu(ifp) +
5449cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
5450fba8b109SMarcel Moolenaar 	    (if_getcapenable(ifp) & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
545195d67482SBill Paul 
545295d67482SBill Paul 	/* Load our MAC address. */
54533f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
545495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
545595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
545695d67482SBill Paul 
54573e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
54583e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
545995d67482SBill Paul 
546095d67482SBill Paul 	/* Program multicast filter. */
546195d67482SBill Paul 	bge_setmulti(sc);
546295d67482SBill Paul 
5463cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
5464cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
5465cb2eacc7SYaroslav Tykhiy 
546635f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
546735f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
546835f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
546935f945cdSPyun YongHyeon 	else
547035f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
5471fba8b109SMarcel Moolenaar 	if (if_getcapabilities(ifp) & IFCAP_TXCSUM &&
5472fba8b109SMarcel Moolenaar 	    if_getcapenable(ifp) & IFCAP_TXCSUM) {
5473fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, 0, (BGE_CSUM_FEATURES | CSUM_UDP));
5474fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, sc->bge_csum_features, 0);
547535f945cdSPyun YongHyeon 	}
547635f945cdSPyun YongHyeon 
547795d67482SBill Paul 	/* Init RX ring. */
54783ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
54793ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
54803ee5d7daSPyun YongHyeon 		bge_stop(sc);
54813ee5d7daSPyun YongHyeon 		return;
54823ee5d7daSPyun YongHyeon 	}
548395d67482SBill Paul 
54840434d1b8SBill Paul 	/*
54850434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
54860434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
54870434d1b8SBill Paul 	 * entry of the ring.
54880434d1b8SBill Paul 	 */
54890434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
54903f74909aSGleb Smirnoff 		uint32_t		v, i;
54910434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
54920434d1b8SBill Paul 			DELAY(20);
54930434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
54940434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
54950434d1b8SBill Paul 				break;
54960434d1b8SBill Paul 		}
54970434d1b8SBill Paul 		if (i == 10)
5498fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
5499fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
55000434d1b8SBill Paul 	}
55010434d1b8SBill Paul 
550295d67482SBill Paul 	/* Init jumbo RX ring. */
5503f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
5504fba8b109SMarcel Moolenaar 	    if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
5505fba8b109SMarcel Moolenaar      	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN)) {
55063ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
5507333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
5508b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
55093ee5d7daSPyun YongHyeon 			bge_stop(sc);
55103ee5d7daSPyun YongHyeon 			return;
55113ee5d7daSPyun YongHyeon 		}
55123ee5d7daSPyun YongHyeon 	}
551395d67482SBill Paul 
55143f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
551595d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
551695d67482SBill Paul 
55177e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
55187e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
55197e6e2507SJung-uk Kim 
552095d67482SBill Paul 	/* Init TX ring. */
552195d67482SBill Paul 	bge_init_tx_ring(sc);
552295d67482SBill Paul 
5523f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
5524f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
5525f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
5526f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
55272927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
55282927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
552950515680SPyun YongHyeon 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
553050515680SPyun YongHyeon 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
553150515680SPyun YongHyeon 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
553250515680SPyun YongHyeon 	}
55333f74909aSGleb Smirnoff 	/* Turn on transmitter. */
5534f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
5535a6e66cd2SPyun YongHyeon 	DELAY(100);
553695d67482SBill Paul 
55373f74909aSGleb Smirnoff 	/* Turn on receiver. */
5538548c8f1aSPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_RX_MODE);
5539548c8f1aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc))
5540548c8f1aSPyun YongHyeon 		mode |= BGE_RXMODE_IPV6_ENABLE;
554169b1f509SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
554269b1f509SPyun YongHyeon 		mode |= BGE_RXMODE_IPV4_FRAG_FIX;
5543548c8f1aSPyun YongHyeon 	CSR_WRITE_4(sc,BGE_RX_MODE, mode | BGE_RXMODE_ENABLE);
5544a6e66cd2SPyun YongHyeon 	DELAY(10);
554595d67482SBill Paul 
5546dedcdf57SPyun YongHyeon 	/*
5547dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
5548dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
5549dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
5550dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
5551dedcdf57SPyun YongHyeon 	 */
55523fc5fbfbSPyun YongHyeon 	if (BGE_IS_57765_PLUS(sc))
5553b4a256acSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
5554b4a256acSPyun YongHyeon 	else
5555dedcdf57SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
5556dedcdf57SPyun YongHyeon 
55572280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
55582280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
55592280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
55602280c16bSPyun YongHyeon 
556195d67482SBill Paul 	/* Tell firmware we're alive. */
556295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
556395d67482SBill Paul 
556475719184SGleb Smirnoff #ifdef DEVICE_POLLING
556575719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
5566fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
556775719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
556875719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
556938cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
557075719184SGleb Smirnoff 	} else
557175719184SGleb Smirnoff #endif
557275719184SGleb Smirnoff 
557395d67482SBill Paul 	/* Enable host interrupts. */
557475719184SGleb Smirnoff 	{
557595d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
557695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
557738cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
557875719184SGleb Smirnoff 	}
557995d67482SBill Paul 
5580fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
5581fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
558295d67482SBill Paul 
5583e4146b95SPyun YongHyeon 	bge_ifmedia_upd_locked(ifp);
5584e4146b95SPyun YongHyeon 
55850f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
55860f9bd73bSSam Leffler }
55870f9bd73bSSam Leffler 
55880f9bd73bSSam Leffler static void
55893f74909aSGleb Smirnoff bge_init(void *xsc)
55900f9bd73bSSam Leffler {
55910f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
55920f9bd73bSSam Leffler 
55930f9bd73bSSam Leffler 	BGE_LOCK(sc);
55940f9bd73bSSam Leffler 	bge_init_locked(sc);
55950f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
559695d67482SBill Paul }
559795d67482SBill Paul 
559895d67482SBill Paul /*
559995d67482SBill Paul  * Set media options.
560095d67482SBill Paul  */
560195d67482SBill Paul static int
5602fba8b109SMarcel Moolenaar bge_ifmedia_upd(if_t ifp)
560395d67482SBill Paul {
5604fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
560567d5e043SOleg Bulyzhin 	int res;
560667d5e043SOleg Bulyzhin 
560767d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
560867d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
560967d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
561067d5e043SOleg Bulyzhin 
561167d5e043SOleg Bulyzhin 	return (res);
561267d5e043SOleg Bulyzhin }
561367d5e043SOleg Bulyzhin 
561467d5e043SOleg Bulyzhin static int
5615fba8b109SMarcel Moolenaar bge_ifmedia_upd_locked(if_t ifp)
561667d5e043SOleg Bulyzhin {
5617fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
561895d67482SBill Paul 	struct mii_data *mii;
56194f09c4c7SMarius Strobl 	struct mii_softc *miisc;
562095d67482SBill Paul 	struct ifmedia *ifm;
562195d67482SBill Paul 
562267d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
562367d5e043SOleg Bulyzhin 
562495d67482SBill Paul 	ifm = &sc->bge_ifmedia;
562595d67482SBill Paul 
562695d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
5627652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
562895d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
562995d67482SBill Paul 			return (EINVAL);
563095d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
563195d67482SBill Paul 		case IFM_AUTO:
5632ff50922bSDoug White 			/*
5633ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
5634ff50922bSDoug White 			 * mechanism for programming the autoneg
5635ff50922bSDoug White 			 * advertisement registers in TBI mode.
5636ff50922bSDoug White 			 */
56370f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5638ff50922bSDoug White 				uint32_t sgdig;
56390f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
56400f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
5641ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5642ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5643ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
5644ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
5645ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
5646ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5647ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
5648ff50922bSDoug White 					DELAY(5);
5649ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5650ff50922bSDoug White 				}
56510f89fde2SJung-uk Kim 			}
565295d67482SBill Paul 			break;
565395d67482SBill Paul 		case IFM_1000_SX:
565495d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
565595d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
565695d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
565795d67482SBill Paul 			} else {
565895d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
565995d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
566095d67482SBill Paul 			}
56619b80ffe7SPyun YongHyeon 			DELAY(40);
566295d67482SBill Paul 			break;
566395d67482SBill Paul 		default:
566495d67482SBill Paul 			return (EINVAL);
566595d67482SBill Paul 		}
566695d67482SBill Paul 		return (0);
566795d67482SBill Paul 	}
566895d67482SBill Paul 
56691493e883SOleg Bulyzhin 	sc->bge_link_evt++;
567095d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
56714f09c4c7SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
56723fcb7a53SMarius Strobl 		PHY_RESET(miisc);
567395d67482SBill Paul 	mii_mediachg(mii);
567495d67482SBill Paul 
5675902827f6SBjoern A. Zeeb 	/*
5676902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
5677902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
5678902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
5679902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
5680902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
5681902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
5682902827f6SBjoern A. Zeeb 	 * get an RX intr.
5683902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
5684902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
5685902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
5686902827f6SBjoern A. Zeeb 	 */
56874f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
56884f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
5689902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
56904f0794ffSBjoern A. Zeeb 	else
569163ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5692902827f6SBjoern A. Zeeb 
569395d67482SBill Paul 	return (0);
569495d67482SBill Paul }
569595d67482SBill Paul 
569695d67482SBill Paul /*
569795d67482SBill Paul  * Report current media status.
569895d67482SBill Paul  */
569995d67482SBill Paul static void
5700fba8b109SMarcel Moolenaar bge_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
570195d67482SBill Paul {
5702fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
570395d67482SBill Paul 	struct mii_data *mii;
570495d67482SBill Paul 
570567d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
570695d67482SBill Paul 
5707fba8b109SMarcel Moolenaar 	if ((if_getflags(ifp) & IFF_UP) == 0) {
5708b9d2edd7SPyun YongHyeon 		BGE_UNLOCK(sc);
5709b9d2edd7SPyun YongHyeon 		return;
5710b9d2edd7SPyun YongHyeon 	}
5711652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
571295d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
571395d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
571495d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
571595d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
571695d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
57174c0da0ffSGleb Smirnoff 		else {
57184c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
571967d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
57204c0da0ffSGleb Smirnoff 			return;
57214c0da0ffSGleb Smirnoff 		}
572295d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
572395d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
572495d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
572595d67482SBill Paul 		else
572695d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
572767d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
572895d67482SBill Paul 		return;
572995d67482SBill Paul 	}
573095d67482SBill Paul 
573195d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
573295d67482SBill Paul 	mii_pollstat(mii);
573395d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
573495d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
573567d5e043SOleg Bulyzhin 
573667d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
573795d67482SBill Paul }
573895d67482SBill Paul 
573995d67482SBill Paul static int
5740fba8b109SMarcel Moolenaar bge_ioctl(if_t ifp, u_long command, caddr_t data)
574195d67482SBill Paul {
5742fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
574395d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
574495d67482SBill Paul 	struct mii_data *mii;
5745f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
574695d67482SBill Paul 
574795d67482SBill Paul 	switch (command) {
574895d67482SBill Paul 	case SIOCSIFMTU:
5749f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
5750f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
57514c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
5752f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
575395d67482SBill Paul 				error = EINVAL;
5754f5459d4cSPyun YongHyeon 				break;
5755f5459d4cSPyun YongHyeon 			}
5756f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5757f5459d4cSPyun YongHyeon 			error = EINVAL;
5758f5459d4cSPyun YongHyeon 			break;
5759f5459d4cSPyun YongHyeon 		}
5760f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
5761fba8b109SMarcel Moolenaar 		if (if_getmtu(ifp) != ifr->ifr_mtu) {
5762fba8b109SMarcel Moolenaar 			if_setmtu(ifp, ifr->ifr_mtu);
5763fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
5764fba8b109SMarcel Moolenaar 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
57653a429c8fSPyun YongHyeon 				bge_init_locked(sc);
576695d67482SBill Paul 			}
57673a429c8fSPyun YongHyeon 		}
57683a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
576995d67482SBill Paul 		break;
577095d67482SBill Paul 	case SIOCSIFFLAGS:
57710f9bd73bSSam Leffler 		BGE_LOCK(sc);
5772fba8b109SMarcel Moolenaar 		if (if_getflags(ifp) & IFF_UP) {
577395d67482SBill Paul 			/*
577495d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
577595d67482SBill Paul 			 * then just use the 'set promisc mode' command
577695d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
577795d67482SBill Paul 			 * a full re-init means reloading the firmware and
577895d67482SBill Paul 			 * waiting for it to start up, which may take a
5779d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
578095d67482SBill Paul 			 */
5781fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
5782fba8b109SMarcel Moolenaar 				flags = if_getflags(ifp) ^ sc->bge_if_flags;
57833e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
57843e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
5785f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
5786d183af7fSRuslan Ermilov 					bge_setmulti(sc);
578795d67482SBill Paul 			} else
57880f9bd73bSSam Leffler 				bge_init_locked(sc);
578995d67482SBill Paul 		} else {
5790fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
579195d67482SBill Paul 				bge_stop(sc);
579295d67482SBill Paul 			}
579395d67482SBill Paul 		}
5794fba8b109SMarcel Moolenaar 		sc->bge_if_flags = if_getflags(ifp);
57950f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
579695d67482SBill Paul 		error = 0;
579795d67482SBill Paul 		break;
579895d67482SBill Paul 	case SIOCADDMULTI:
579995d67482SBill Paul 	case SIOCDELMULTI:
5800fba8b109SMarcel Moolenaar 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
58010f9bd73bSSam Leffler 			BGE_LOCK(sc);
580295d67482SBill Paul 			bge_setmulti(sc);
58030f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
580495d67482SBill Paul 			error = 0;
580595d67482SBill Paul 		}
580695d67482SBill Paul 		break;
580795d67482SBill Paul 	case SIOCSIFMEDIA:
580895d67482SBill Paul 	case SIOCGIFMEDIA:
5809652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
581095d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
581195d67482SBill Paul 			    &sc->bge_ifmedia, command);
581295d67482SBill Paul 		} else {
581395d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
581495d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
581595d67482SBill Paul 			    &mii->mii_media, command);
581695d67482SBill Paul 		}
581795d67482SBill Paul 		break;
581895d67482SBill Paul 	case SIOCSIFCAP:
5819fba8b109SMarcel Moolenaar 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
582075719184SGleb Smirnoff #ifdef DEVICE_POLLING
582175719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
582275719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
5823bd071d4dSGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
582475719184SGleb Smirnoff 				if (error)
582575719184SGleb Smirnoff 					return (error);
582675719184SGleb Smirnoff 				BGE_LOCK(sc);
582775719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
582875719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
582938cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
5830fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, IFCAP_POLLING, 0);
583175719184SGleb Smirnoff 				BGE_UNLOCK(sc);
583275719184SGleb Smirnoff 			} else {
583375719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
583475719184SGleb Smirnoff 				/* Enable interrupt even in error case */
583575719184SGleb Smirnoff 				BGE_LOCK(sc);
583675719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
583775719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
583838cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
5839fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_POLLING);
584075719184SGleb Smirnoff 				BGE_UNLOCK(sc);
584175719184SGleb Smirnoff 			}
584275719184SGleb Smirnoff 		}
584375719184SGleb Smirnoff #endif
5844d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5845fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TXCSUM) != 0) {
5846fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TXCSUM);
5847fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
5848fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp,
5849fba8b109SMarcel Moolenaar 				    sc->bge_csum_features, 0);
585095d67482SBill Paul 			else
5851fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, 0,
5852fba8b109SMarcel Moolenaar 				    sc->bge_csum_features);
585395d67482SBill Paul 		}
5854cb2eacc7SYaroslav Tykhiy 
5855d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5856fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_RXCSUM) != 0)
5857fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_RXCSUM);
5858d8b57f98SPyun YongHyeon 
5859ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5860fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TSO4) != 0) {
5861fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TSO4);
5862fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TSO4) != 0)
5863fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, CSUM_TSO, 0);
5864ca3f1187SPyun YongHyeon 			else
5865fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, 0, CSUM_TSO);
5866ca3f1187SPyun YongHyeon 		}
5867ca3f1187SPyun YongHyeon 
5868cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5869fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
5870fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
5871cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5872cb2eacc7SYaroslav Tykhiy 		}
5873cb2eacc7SYaroslav Tykhiy 
587404bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
5875fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTSO) != 0)
5876fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
587704bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
5878fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
5879fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
5880fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0)
5881fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_VLAN_HWTSO);
5882cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5883cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5884cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
588504bde852SPyun YongHyeon 		}
5886cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5887fba8b109SMarcel Moolenaar 		if_vlancap(ifp);
5888cb2eacc7SYaroslav Tykhiy #endif
588995d67482SBill Paul 		break;
589095d67482SBill Paul 	default:
5891673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
589295d67482SBill Paul 		break;
589395d67482SBill Paul 	}
589495d67482SBill Paul 
589595d67482SBill Paul 	return (error);
589695d67482SBill Paul }
589795d67482SBill Paul 
589895d67482SBill Paul static void
5899b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
590095d67482SBill Paul {
5901fba8b109SMarcel Moolenaar 	if_t ifp;
5902b584d2b3SPyun YongHyeon 	uint32_t status;
590395d67482SBill Paul 
5904b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5905b74e67fbSGleb Smirnoff 
5906b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5907b74e67fbSGleb Smirnoff 		return;
5908b74e67fbSGleb Smirnoff 
5909b584d2b3SPyun YongHyeon 	/* If pause frames are active then don't reset the hardware. */
5910b584d2b3SPyun YongHyeon 	if ((CSR_READ_4(sc, BGE_RX_MODE) & BGE_RXMODE_FLOWCTL_ENABLE) != 0) {
5911b584d2b3SPyun YongHyeon 		status = CSR_READ_4(sc, BGE_RX_STS);
5912b584d2b3SPyun YongHyeon 		if ((status & BGE_RXSTAT_REMOTE_XOFFED) != 0) {
5913b584d2b3SPyun YongHyeon 			/*
5914b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5915b584d2b3SPyun YongHyeon 			 * the condition to clear.
5916b584d2b3SPyun YongHyeon 			 */
5917b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5918b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5919b584d2b3SPyun YongHyeon 			return;
5920b584d2b3SPyun YongHyeon 		} else if ((status & BGE_RXSTAT_RCVD_XOFF) != 0 &&
5921b584d2b3SPyun YongHyeon 		    (status & BGE_RXSTAT_RCVD_XON) != 0) {
5922b584d2b3SPyun YongHyeon 			/*
5923b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5924b584d2b3SPyun YongHyeon 			 * the condition to clear.
5925b584d2b3SPyun YongHyeon 			 */
5926b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5927b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5928b584d2b3SPyun YongHyeon 			return;
5929b584d2b3SPyun YongHyeon 		}
5930b584d2b3SPyun YongHyeon 		/*
5931b584d2b3SPyun YongHyeon 		 * Any other condition is unexpected and the controller
5932b584d2b3SPyun YongHyeon 		 * should be reset.
5933b584d2b3SPyun YongHyeon 		 */
5934b584d2b3SPyun YongHyeon 	}
5935b584d2b3SPyun YongHyeon 
5936b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
593795d67482SBill Paul 
5938fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
593995d67482SBill Paul 
5940fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
5941426742bfSGleb Smirnoff 	bge_init_locked(sc);
594295d67482SBill Paul 
5943df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
594495d67482SBill Paul }
594595d67482SBill Paul 
59465a147ba6SPyun YongHyeon static void
59475a147ba6SPyun YongHyeon bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
59485a147ba6SPyun YongHyeon {
59495a147ba6SPyun YongHyeon 	int i;
59505a147ba6SPyun YongHyeon 
59515a147ba6SPyun YongHyeon 	BGE_CLRBIT(sc, reg, bit);
59525a147ba6SPyun YongHyeon 
59535a147ba6SPyun YongHyeon 	for (i = 0; i < BGE_TIMEOUT; i++) {
59545a147ba6SPyun YongHyeon 		if ((CSR_READ_4(sc, reg) & bit) == 0)
59555a147ba6SPyun YongHyeon 			return;
59565a147ba6SPyun YongHyeon 		DELAY(100);
59575a147ba6SPyun YongHyeon         }
59585a147ba6SPyun YongHyeon }
59595a147ba6SPyun YongHyeon 
596095d67482SBill Paul /*
596195d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
596295d67482SBill Paul  * RX and TX lists.
596395d67482SBill Paul  */
596495d67482SBill Paul static void
59653f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
596695d67482SBill Paul {
5967fba8b109SMarcel Moolenaar 	if_t ifp;
596895d67482SBill Paul 
59690f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
59700f9bd73bSSam Leffler 
5971fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
597295d67482SBill Paul 
59730f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
597495d67482SBill Paul 
597544b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
597644b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
597744b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
597844b63691SBjoern A. Zeeb 
597944b63691SBjoern A. Zeeb 	/*
598044b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
598144b63691SBjoern A. Zeeb 	 */
598244b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
5983548c8f1aSPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
598444b63691SBjoern A. Zeeb 
598595d67482SBill Paul 	/*
59863f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
598795d67482SBill Paul 	 */
59885a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
59895a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
59905a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
59915a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
59925a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
59935a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
59945a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
59955a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
599695d67482SBill Paul 
599795d67482SBill Paul 	/*
59983f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
599995d67482SBill Paul 	 */
60005a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
60015a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
60025a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
60035a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
60045a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
60055a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60065a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
60075a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
600895d67482SBill Paul 
600995d67482SBill Paul 	/*
601095d67482SBill Paul 	 * Shut down all of the memory managers and related
601195d67482SBill Paul 	 * state machines.
601295d67482SBill Paul 	 */
60135a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
60145a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
60155a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60165a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
60175a147ba6SPyun YongHyeon 
60180c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
601995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
60207ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
602195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
602295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
60230434d1b8SBill Paul 	}
60242280c16bSPyun YongHyeon 	/* Update MAC statistics. */
60252280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
60262280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
602795d67482SBill Paul 
60288cb1383cSDoug Ambrisko 	bge_reset(sc);
6029548c8f1aSPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
6030548c8f1aSPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
60318cb1383cSDoug Ambrisko 
60328cb1383cSDoug Ambrisko 	/*
60338cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
60348cb1383cSDoug Ambrisko 	 */
60358cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
60368cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
60378cb1383cSDoug Ambrisko 	else
603895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
603995d67482SBill Paul 
604095d67482SBill Paul 	/* Free the RX lists. */
604195d67482SBill Paul 	bge_free_rx_ring_std(sc);
604295d67482SBill Paul 
604395d67482SBill Paul 	/* Free jumbo RX list. */
60444c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
604595d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
604695d67482SBill Paul 
604795d67482SBill Paul 	/* Free TX buffers. */
604895d67482SBill Paul 	bge_free_tx_ring(sc);
604995d67482SBill Paul 
605095d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
605195d67482SBill Paul 
60525dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
60531493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
60541493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
60551493e883SOleg Bulyzhin 	sc->bge_link = 0;
605695d67482SBill Paul 
6057fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
605895d67482SBill Paul }
605995d67482SBill Paul 
606095d67482SBill Paul /*
606195d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
606295d67482SBill Paul  * get confused by errant DMAs when rebooting.
606395d67482SBill Paul  */
6064b6c974e8SWarner Losh static int
60653f74909aSGleb Smirnoff bge_shutdown(device_t dev)
606695d67482SBill Paul {
606795d67482SBill Paul 	struct bge_softc *sc;
606895d67482SBill Paul 
606995d67482SBill Paul 	sc = device_get_softc(dev);
60700f9bd73bSSam Leffler 	BGE_LOCK(sc);
607195d67482SBill Paul 	bge_stop(sc);
60720f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
6073b6c974e8SWarner Losh 
6074b6c974e8SWarner Losh 	return (0);
607595d67482SBill Paul }
607614afefa3SPawel Jakub Dawidek 
607714afefa3SPawel Jakub Dawidek static int
607814afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
607914afefa3SPawel Jakub Dawidek {
608014afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
608114afefa3SPawel Jakub Dawidek 
608214afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
608314afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
608414afefa3SPawel Jakub Dawidek 	bge_stop(sc);
608514afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
608614afefa3SPawel Jakub Dawidek 
608714afefa3SPawel Jakub Dawidek 	return (0);
608814afefa3SPawel Jakub Dawidek }
608914afefa3SPawel Jakub Dawidek 
609014afefa3SPawel Jakub Dawidek static int
609114afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
609214afefa3SPawel Jakub Dawidek {
609314afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
6094fba8b109SMarcel Moolenaar 	if_t ifp;
609514afefa3SPawel Jakub Dawidek 
609614afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
609714afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
609814afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
6099fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_UP) {
610014afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
6101fba8b109SMarcel Moolenaar 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
610214afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
610314afefa3SPawel Jakub Dawidek 	}
610414afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
610514afefa3SPawel Jakub Dawidek 
610614afefa3SPawel Jakub Dawidek 	return (0);
610714afefa3SPawel Jakub Dawidek }
6108dab5cd05SOleg Bulyzhin 
6109dab5cd05SOleg Bulyzhin static void
61103f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
6111dab5cd05SOleg Bulyzhin {
61121f313773SOleg Bulyzhin 	struct mii_data *mii;
61131f313773SOleg Bulyzhin 	uint32_t link, status;
6114dab5cd05SOleg Bulyzhin 
6115dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
61161f313773SOleg Bulyzhin 
61173f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
61187b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
61197b97099dSOleg Bulyzhin 
6120dab5cd05SOleg Bulyzhin 	/*
6121dab5cd05SOleg Bulyzhin 	 * Process link state changes.
6122dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
6123dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
6124dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
6125dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
6126dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
6127dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
6128dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
6129dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
61301f313773SOleg Bulyzhin 	 *
61311f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
61324c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
6133dab5cd05SOleg Bulyzhin 	 */
6134dab5cd05SOleg Bulyzhin 
61351f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
61364c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
6137dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
6138dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
61391f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
61405dda8085SOleg Bulyzhin 			mii_pollstat(mii);
61411f313773SOleg Bulyzhin 			if (!sc->bge_link &&
61421f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
61431f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
61441f313773SOleg Bulyzhin 				sc->bge_link++;
61451f313773SOleg Bulyzhin 				if (bootverbose)
61461f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
61471f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
61481f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
61491f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
61501f313773SOleg Bulyzhin 				sc->bge_link = 0;
61511f313773SOleg Bulyzhin 				if (bootverbose)
61521f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
61531f313773SOleg Bulyzhin 			}
61541f313773SOleg Bulyzhin 
61553f74909aSGleb Smirnoff 			/* Clear the interrupt. */
6156dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
6157dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
6158daeeb75cSPyun YongHyeon 			bge_miibus_readreg(sc->bge_dev, sc->bge_phy_addr,
6159daeeb75cSPyun YongHyeon 			    BRGPHY_MII_ISR);
6160daeeb75cSPyun YongHyeon 			bge_miibus_writereg(sc->bge_dev, sc->bge_phy_addr,
6161daeeb75cSPyun YongHyeon 			    BRGPHY_MII_IMR, BRGPHY_INTRS);
6162dab5cd05SOleg Bulyzhin 		}
6163dab5cd05SOleg Bulyzhin 		return;
6164dab5cd05SOleg Bulyzhin 	}
6165dab5cd05SOleg Bulyzhin 
6166652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
61671f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
61687b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
61697b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
61701f313773SOleg Bulyzhin 				sc->bge_link++;
61719b80ffe7SPyun YongHyeon 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
61721f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
61731f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
61749b80ffe7SPyun YongHyeon 					DELAY(40);
61759b80ffe7SPyun YongHyeon 				}
61760c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
61771f313773SOleg Bulyzhin 				if (bootverbose)
61781f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
61793f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
61803f74909aSGleb Smirnoff 				    LINK_STATE_UP);
61817b97099dSOleg Bulyzhin 			}
61821f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
6183dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
61841f313773SOleg Bulyzhin 			if (bootverbose)
61851f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
61867b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
61871f313773SOleg Bulyzhin 		}
61886ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
61891f313773SOleg Bulyzhin 		/*
61900c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
61910c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
61920c8aa4eaSJung-uk Kim 		 * PHY link status directly.
61931f313773SOleg Bulyzhin 		 */
61941f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
61951f313773SOleg Bulyzhin 
61961f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
61971f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
61981f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
61995dda8085SOleg Bulyzhin 			mii_pollstat(mii);
62001f313773SOleg Bulyzhin 			if (!sc->bge_link &&
62011f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
62021f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
62031f313773SOleg Bulyzhin 				sc->bge_link++;
62041f313773SOleg Bulyzhin 				if (bootverbose)
62051f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
62061f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
62071f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
62081f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
62091f313773SOleg Bulyzhin 				sc->bge_link = 0;
62101f313773SOleg Bulyzhin 				if (bootverbose)
62111f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
62121f313773SOleg Bulyzhin 			}
62131f313773SOleg Bulyzhin 		}
62140c8aa4eaSJung-uk Kim 	} else {
62150c8aa4eaSJung-uk Kim 		/*
62166ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
62176ede2cfaSPyun YongHyeon 		 * link status.
62180c8aa4eaSJung-uk Kim 		 */
62196ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
62206ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
62216ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
6222dab5cd05SOleg Bulyzhin 	}
6223dab5cd05SOleg Bulyzhin 
62242246e8c6SPyun YongHyeon 	/* Disable MAC attention when link is up. */
6225dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
6226dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
6227dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
6228dab5cd05SOleg Bulyzhin }
62296f8718a3SScott Long 
62306f8718a3SScott Long static void
62316f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
62326f8718a3SScott Long {
62336f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
62342280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
62356f8718a3SScott Long 
62366f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
62376f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
62386f8718a3SScott Long 
62396f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
62406f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
62417029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
62427029da5cSPawel Biernacki 	    bge_sysctl_debug_info, "I", "Debug Information");
62436f8718a3SScott Long 
62446f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
62457029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
62467029da5cSPawel Biernacki 	    bge_sysctl_reg_read, "I", "MAC Register Read");
6247548c8f1aSPyun YongHyeon 
6248548c8f1aSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ape_read",
62497029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
62507029da5cSPawel Biernacki 	    bge_sysctl_ape_read, "I", "APE Register Read");
62516f8718a3SScott Long 
62526f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
62537029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
62547029da5cSPawel Biernacki 	    bge_sysctl_mem_read, "I", "Memory Read");
62556f8718a3SScott Long 
62566f8718a3SScott Long #endif
6257763757b2SScott Long 
6258beaa2ae1SPyun YongHyeon 	/*
6259beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
6260beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
6261beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
6262beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
6263beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
6264beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
6265beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
6266beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
6267beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
6268beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
6269beaa2ae1SPyun YongHyeon 	 */
62707e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
6271beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
6272af3b2549SHans Petter Selasky 	    CTLFLAG_RWTUN, &sc->bge_forced_collapse, 0,
6273beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
6274beaa2ae1SPyun YongHyeon 	    "forced collapsing");
6275beaa2ae1SPyun YongHyeon 
62762ae7f64bSPyun YongHyeon 	sc->bge_msi = 1;
62772ae7f64bSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
6278af3b2549SHans Petter Selasky 	    CTLFLAG_RDTUN, &sc->bge_msi, 0, "Enable MSI");
62795c952e8dSPyun YongHyeon 
628035f945cdSPyun YongHyeon 	/*
628135f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
628235f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
628335f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
628435f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
628535f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
628635f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
628735f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
628835f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
628935f945cdSPyun YongHyeon 	 */
629035f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
629135f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
6292af3b2549SHans Petter Selasky 	    CTLFLAG_RWTUN, &sc->bge_forced_udpcsum, 0,
629335f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
629435f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
629535f945cdSPyun YongHyeon 
6296d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
62972280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
62982280c16bSPyun YongHyeon 	else
62992280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
63002280c16bSPyun YongHyeon }
6301d949071dSJung-uk Kim 
63022280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
63037029da5cSPawel Biernacki     SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, \
63047029da5cSPawel Biernacki         CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, \
63057029da5cSPawel Biernacki 	offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", desc)
63062280c16bSPyun YongHyeon 
63072280c16bSPyun YongHyeon static void
63082280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
63092280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
63102280c16bSPyun YongHyeon {
63112280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
63122280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
63132280c16bSPyun YongHyeon 
63147029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats",
63157029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "BGE Statistics");
6316763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
6317763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
6318763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
6319763757b2SScott Long 	    "FramesDroppedDueToFilters");
6320763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
6321763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
6322763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
6323763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
6324763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
6325763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
632606e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
632706e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
632806e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
632906e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
6330763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
6331763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
6332763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
6333763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
6334763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
6335763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
6336763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
6337763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
6338763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
6339763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
6340763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
6341763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
6342763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
6343763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
6344763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
6345763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
6346763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
6347763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
6348763757b2SScott Long 
63497029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx",
63507029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "BGE RX Statistics");
6351763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6352763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
63531cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
6354763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
6355763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
6356763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
63571cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
6358763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
6359763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
6360763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
6361763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
6362763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
6363763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
6364763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
6365763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
6366763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
6367763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
6368763757b2SScott Long 	    "xoffPauseFramesReceived");
6369763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
6370763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
6371763757b2SScott Long 	    "ControlFramesReceived");
6372763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
6373763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
6374763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
6375763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
6376763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
6377763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
6378763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
6379763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
6380763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
638106e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
6382763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
638306e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
6384763757b2SScott Long 
63857029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx",
63867029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "BGE TX Statistics");
6387763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6388763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
63891cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
6390763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
6391763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
6392763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
6393763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
6394763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
6395763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
6396763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
6397763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
6398763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
6399763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
6400763757b2SScott Long 	    "InternalMacTransmitErrors");
6401763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
6402763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
6403763757b2SScott Long 	    "SingleCollisionFrames");
6404763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
6405763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
6406763757b2SScott Long 	    "MultipleCollisionFrames");
6407763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
6408763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
6409763757b2SScott Long 	    "DeferredTransmissions");
6410763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
6411763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
6412763757b2SScott Long 	    "ExcessiveCollisions");
6413763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
641406e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
641506e83c7eSScott Long 	    "LateCollisions");
6416763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
64171cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
6418763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
6419763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
6420763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
6421763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
6422763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
6423763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
6424763757b2SScott Long 	    "CarrierSenseErrors");
6425763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
6426763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
6427763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
6428763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
6429763757b2SScott Long }
6430763757b2SScott Long 
64312280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
64322280c16bSPyun YongHyeon 
64332280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
64346dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
64352280c16bSPyun YongHyeon 
64362280c16bSPyun YongHyeon static void
64372280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
64382280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
64392280c16bSPyun YongHyeon {
64402280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
64412280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
64422280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
64432280c16bSPyun YongHyeon 
64442280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
64457029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats",
64467029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "BGE Statistics");
64472280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
64482280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
64492280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
64502280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
64512280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
64522280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
64532280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
64542280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
64552280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
64562280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
64572280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
64582280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
64592280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
64602280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
64612280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
64622280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
64632280c16bSPyun YongHyeon 
64647029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx",
64657029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "BGE RX Statistics");
64662280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
64672280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
64682280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
64692280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
64702280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
64711cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
64722280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
64732280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
64742280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
64752280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
64762280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
64772280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
64782280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
64792280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
64802280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
64812280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
64822280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
64832280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
64842280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
64852280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
64862280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
64872280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
64882280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
64892280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
64902280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
64912280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
64922280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
64932280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
64942280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
64952280c16bSPyun YongHyeon 
64967029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx",
64977029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "BGE TX Statistics");
64982280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
64991cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
65002280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
65012280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
65022280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
65032280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
65042280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
65052280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
65062280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
65072280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
65082280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
65092280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
65102280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
65112280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
65122280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
65132280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
65142280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
65152280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
65162280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
65172280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
65182280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
65192280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
65202280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
65211cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
65222280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
65231cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
65242280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
65251cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
65262280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
65272280c16bSPyun YongHyeon }
65282280c16bSPyun YongHyeon 
65292280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
65302280c16bSPyun YongHyeon 
6531763757b2SScott Long static int
6532763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
6533763757b2SScott Long {
6534763757b2SScott Long 	struct bge_softc *sc;
653506e83c7eSScott Long 	uint32_t result;
6536d949071dSJung-uk Kim 	int offset;
6537763757b2SScott Long 
6538763757b2SScott Long 	sc = (struct bge_softc *)arg1;
6539763757b2SScott Long 	offset = arg2;
6540d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
6541d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
6542041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
65436f8718a3SScott Long }
65446f8718a3SScott Long 
65456f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
65466f8718a3SScott Long static int
65476f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
65486f8718a3SScott Long {
65496f8718a3SScott Long 	struct bge_softc *sc;
65506f8718a3SScott Long 	uint16_t *sbdata;
655128276ad6SPyun YongHyeon 	int error, result, sbsz;
65526f8718a3SScott Long 	int i, j;
65536f8718a3SScott Long 
65546f8718a3SScott Long 	result = -1;
65556f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
65566f8718a3SScott Long 	if (error || (req->newptr == NULL))
65576f8718a3SScott Long 		return (error);
65586f8718a3SScott Long 
65596f8718a3SScott Long 	if (result == 1) {
65606f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
65616f8718a3SScott Long 
656228276ad6SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
656328276ad6SPyun YongHyeon 		    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
656428276ad6SPyun YongHyeon 			sbsz = BGE_STATUS_BLK_SZ;
656528276ad6SPyun YongHyeon 		else
656628276ad6SPyun YongHyeon 			sbsz = 32;
65676f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
65686f8718a3SScott Long 		printf("Status Block:\n");
656928276ad6SPyun YongHyeon 		BGE_LOCK(sc);
657028276ad6SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
657128276ad6SPyun YongHyeon 		    sc->bge_cdata.bge_status_map,
657228276ad6SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
657328276ad6SPyun YongHyeon 		for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
65746f8718a3SScott Long 			printf("%06x:", i);
657528276ad6SPyun YongHyeon 			for (j = 0; j < 8; j++)
657628276ad6SPyun YongHyeon 				printf(" %04x", sbdata[i++]);
65776f8718a3SScott Long 			printf("\n");
65786f8718a3SScott Long 		}
65796f8718a3SScott Long 
65806f8718a3SScott Long 		printf("Registers:\n");
65810c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
65826f8718a3SScott Long 			printf("%06x:", i);
65836f8718a3SScott Long 			for (j = 0; j < 8; j++) {
65846f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
65856f8718a3SScott Long 				i += 4;
65866f8718a3SScott Long 			}
65876f8718a3SScott Long 			printf("\n");
65886f8718a3SScott Long 		}
658928276ad6SPyun YongHyeon 		BGE_UNLOCK(sc);
65906f8718a3SScott Long 
65916f8718a3SScott Long 		printf("Hardware Flags:\n");
659228276ad6SPyun YongHyeon 		if (BGE_IS_5717_PLUS(sc))
659328276ad6SPyun YongHyeon 			printf(" - 5717 Plus\n");
6594a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
6595a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
65965345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
65976f8718a3SScott Long 			printf(" - 575X Plus\n");
65985345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
65996f8718a3SScott Long 			printf(" - 5705 Plus\n");
66005345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
66015345bad0SScott Long 			printf(" - 5714 Family\n");
66025345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
66035345bad0SScott Long 			printf(" - 5700 Family\n");
66046f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
66056f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
66066f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
66076f8718a3SScott Long 			printf(" - PCI-X Bus\n");
66086f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
66096f8718a3SScott Long 			printf(" - PCI Express Bus\n");
66107d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
66116f8718a3SScott Long 			printf(" - No 3 LEDs\n");
66126f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
66136f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
66146f8718a3SScott Long 	}
66156f8718a3SScott Long 
66166f8718a3SScott Long 	return (error);
66176f8718a3SScott Long }
66186f8718a3SScott Long 
66196f8718a3SScott Long static int
66206f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
66216f8718a3SScott Long {
66226f8718a3SScott Long 	struct bge_softc *sc;
66236f8718a3SScott Long 	int error;
66246f8718a3SScott Long 	uint16_t result;
66256f8718a3SScott Long 	uint32_t val;
66266f8718a3SScott Long 
66276f8718a3SScott Long 	result = -1;
66286f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
66296f8718a3SScott Long 	if (error || (req->newptr == NULL))
66306f8718a3SScott Long 		return (error);
66316f8718a3SScott Long 
66326f8718a3SScott Long 	if (result < 0x8000) {
66336f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
66346f8718a3SScott Long 		val = CSR_READ_4(sc, result);
66356f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
66366f8718a3SScott Long 	}
66376f8718a3SScott Long 
66386f8718a3SScott Long 	return (error);
66396f8718a3SScott Long }
66406f8718a3SScott Long 
66416f8718a3SScott Long static int
6642548c8f1aSPyun YongHyeon bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS)
6643548c8f1aSPyun YongHyeon {
6644548c8f1aSPyun YongHyeon 	struct bge_softc *sc;
6645548c8f1aSPyun YongHyeon 	int error;
6646548c8f1aSPyun YongHyeon 	uint16_t result;
6647548c8f1aSPyun YongHyeon 	uint32_t val;
6648548c8f1aSPyun YongHyeon 
6649548c8f1aSPyun YongHyeon 	result = -1;
6650548c8f1aSPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
6651548c8f1aSPyun YongHyeon 	if (error || (req->newptr == NULL))
6652548c8f1aSPyun YongHyeon 		return (error);
6653548c8f1aSPyun YongHyeon 
6654548c8f1aSPyun YongHyeon 	if (result < 0x8000) {
6655548c8f1aSPyun YongHyeon 		sc = (struct bge_softc *)arg1;
6656548c8f1aSPyun YongHyeon 		val = APE_READ_4(sc, result);
6657548c8f1aSPyun YongHyeon 		printf("reg 0x%06X = 0x%08X\n", result, val);
6658548c8f1aSPyun YongHyeon 	}
6659548c8f1aSPyun YongHyeon 
6660548c8f1aSPyun YongHyeon 	return (error);
6661548c8f1aSPyun YongHyeon }
6662548c8f1aSPyun YongHyeon 
6663548c8f1aSPyun YongHyeon static int
66646f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
66656f8718a3SScott Long {
66666f8718a3SScott Long 	struct bge_softc *sc;
66676f8718a3SScott Long 	int error;
66686f8718a3SScott Long 	uint16_t result;
66696f8718a3SScott Long 	uint32_t val;
66706f8718a3SScott Long 
66716f8718a3SScott Long 	result = -1;
66726f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
66736f8718a3SScott Long 	if (error || (req->newptr == NULL))
66746f8718a3SScott Long 		return (error);
66756f8718a3SScott Long 
66766f8718a3SScott Long 	if (result < 0x8000) {
66776f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
66786f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
66796f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
66806f8718a3SScott Long 	}
66816f8718a3SScott Long 
66826f8718a3SScott Long 	return (error);
66836f8718a3SScott Long }
66846f8718a3SScott Long #endif
668538cc658fSJohn Baldwin 
668638cc658fSJohn Baldwin static int
66875fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
66885fea260fSMarius Strobl {
66895fea260fSMarius Strobl 	return (1);
66905fea260fSMarius Strobl }
66915fea260fSMarius Strobl 
66925fea260fSMarius Strobl static int
669338cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
669438cc658fSJohn Baldwin {
669538cc658fSJohn Baldwin 	uint32_t mac_addr;
669638cc658fSJohn Baldwin 
669773635418SPyun YongHyeon 	mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
669838cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
669938cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
670038cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
670173635418SPyun YongHyeon 		mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
670238cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
670338cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
670438cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
670538cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
67065fea260fSMarius Strobl 		return (0);
670738cc658fSJohn Baldwin 	}
67085fea260fSMarius Strobl 	return (1);
670938cc658fSJohn Baldwin }
671038cc658fSJohn Baldwin 
671138cc658fSJohn Baldwin static int
671238cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
671338cc658fSJohn Baldwin {
671438cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
671538cc658fSJohn Baldwin 
671638cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
671738cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
671838cc658fSJohn Baldwin 
67195fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
67205fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
672138cc658fSJohn Baldwin }
672238cc658fSJohn Baldwin 
672338cc658fSJohn Baldwin static int
672438cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
672538cc658fSJohn Baldwin {
672638cc658fSJohn Baldwin 
67275fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
67285fea260fSMarius Strobl 		return (1);
67295fea260fSMarius Strobl 
67305fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
67315fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
673238cc658fSJohn Baldwin }
673338cc658fSJohn Baldwin 
673438cc658fSJohn Baldwin static int
673538cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
673638cc658fSJohn Baldwin {
673738cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
673838cc658fSJohn Baldwin 		/* NOTE: Order is critical */
67395fea260fSMarius Strobl 		bge_get_eaddr_fw,
674038cc658fSJohn Baldwin 		bge_get_eaddr_mem,
674138cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
674238cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
674338cc658fSJohn Baldwin 		NULL
674438cc658fSJohn Baldwin 	};
674538cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
674638cc658fSJohn Baldwin 
674738cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
674838cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
674938cc658fSJohn Baldwin 			break;
675038cc658fSJohn Baldwin 	}
675138cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
675238cc658fSJohn Baldwin }
6753df360178SGleb Smirnoff 
6754df360178SGleb Smirnoff static uint64_t
6755df360178SGleb Smirnoff bge_get_counter(if_t ifp, ift_counter cnt)
6756df360178SGleb Smirnoff {
6757df360178SGleb Smirnoff 	struct bge_softc *sc;
6758df360178SGleb Smirnoff 	struct bge_mac_stats *stats;
6759df360178SGleb Smirnoff 
6760df360178SGleb Smirnoff 	sc = if_getsoftc(ifp);
6761df360178SGleb Smirnoff 	if (!BGE_IS_5705_PLUS(sc))
6762df360178SGleb Smirnoff 		return (if_get_counter_default(ifp, cnt));
6763df360178SGleb Smirnoff 	stats = &sc->bge_mac_stats;
6764df360178SGleb Smirnoff 
6765df360178SGleb Smirnoff 	switch (cnt) {
6766df360178SGleb Smirnoff 	case IFCOUNTER_IERRORS:
6767df360178SGleb Smirnoff 		return (stats->NoMoreRxBDs + stats->InputDiscards +
6768df360178SGleb Smirnoff 		    stats->InputErrors);
6769df360178SGleb Smirnoff 	case IFCOUNTER_COLLISIONS:
6770df360178SGleb Smirnoff 		return (stats->etherStatsCollisions);
6771df360178SGleb Smirnoff 	default:
6772df360178SGleb Smirnoff 		return (if_get_counter_default(ifp, cnt));
6773df360178SGleb Smirnoff 	}
6774df360178SGleb Smirnoff }
6775ded66962SMark Johnston 
67767790c8c1SConrad Meyer #ifdef DEBUGNET
6777ded66962SMark Johnston static void
67787790c8c1SConrad Meyer bge_debugnet_init(if_t ifp, int *nrxr, int *ncl, int *clsize)
6779ded66962SMark Johnston {
6780ded66962SMark Johnston 	struct bge_softc *sc;
6781ded66962SMark Johnston 
6782ded66962SMark Johnston 	sc = if_getsoftc(ifp);
6783ded66962SMark Johnston 	BGE_LOCK(sc);
6784*3ca4a339SEric van Gyzen 	/*
6785*3ca4a339SEric van Gyzen 	 * There is only one logical receive ring, but it is backed
6786*3ca4a339SEric van Gyzen 	 * by two actual rings, for cluster- and jumbo-sized mbufs.
6787*3ca4a339SEric van Gyzen 	 * Debugnet expects only one size, so if jumbo is in use,
6788*3ca4a339SEric van Gyzen 	 * this says we have two rings of jumbo mbufs, but that's
6789*3ca4a339SEric van Gyzen 	 * only a little wasteful.
6790*3ca4a339SEric van Gyzen 	 */
6791*3ca4a339SEric van Gyzen 	*nrxr = 2;
67927790c8c1SConrad Meyer 	*ncl = DEBUGNET_MAX_IN_FLIGHT;
6793ded66962SMark Johnston 	if ((sc->bge_flags & BGE_FLAG_JUMBO_STD) != 0 &&
6794ded66962SMark Johnston 	    (if_getmtu(sc->bge_ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
6795ded66962SMark Johnston 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN)))
6796ded66962SMark Johnston 		*clsize = MJUM9BYTES;
6797ded66962SMark Johnston 	else
6798ded66962SMark Johnston 		*clsize = MCLBYTES;
6799ded66962SMark Johnston 	BGE_UNLOCK(sc);
6800ded66962SMark Johnston }
6801ded66962SMark Johnston 
6802ded66962SMark Johnston static void
68037790c8c1SConrad Meyer bge_debugnet_event(if_t ifp __unused, enum debugnet_ev event __unused)
6804ded66962SMark Johnston {
6805ded66962SMark Johnston }
6806ded66962SMark Johnston 
6807ded66962SMark Johnston static int
68087790c8c1SConrad Meyer bge_debugnet_transmit(if_t ifp, struct mbuf *m)
6809ded66962SMark Johnston {
6810ded66962SMark Johnston 	struct bge_softc *sc;
6811ded66962SMark Johnston 	uint32_t prodidx;
6812ded66962SMark Johnston 	int error;
6813ded66962SMark Johnston 
6814ded66962SMark Johnston 	sc = if_getsoftc(ifp);
6815ded66962SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
6816ded66962SMark Johnston 	    IFF_DRV_RUNNING)
6817ded66962SMark Johnston 		return (1);
6818ded66962SMark Johnston 
6819ded66962SMark Johnston 	prodidx = sc->bge_tx_prodidx;
6820ded66962SMark Johnston 	error = bge_encap(sc, &m, &prodidx);
6821ded66962SMark Johnston 	if (error == 0)
6822ded66962SMark Johnston 		bge_start_tx(sc, prodidx);
6823ded66962SMark Johnston 	return (error);
6824ded66962SMark Johnston }
6825ded66962SMark Johnston 
6826ded66962SMark Johnston static int
68277790c8c1SConrad Meyer bge_debugnet_poll(if_t ifp, int count)
6828ded66962SMark Johnston {
6829ded66962SMark Johnston 	struct bge_softc *sc;
6830ded66962SMark Johnston 	uint32_t rx_prod, tx_cons;
6831ded66962SMark Johnston 
6832ded66962SMark Johnston 	sc = if_getsoftc(ifp);
6833ded66962SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
6834ded66962SMark Johnston 	    IFF_DRV_RUNNING)
6835ded66962SMark Johnston 		return (1);
6836ded66962SMark Johnston 
6837ded66962SMark Johnston 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
6838ded66962SMark Johnston 	    sc->bge_cdata.bge_status_map,
6839ded66962SMark Johnston 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
6840ded66962SMark Johnston 
6841ded66962SMark Johnston 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
6842ded66962SMark Johnston 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
6843ded66962SMark Johnston 
6844ded66962SMark Johnston 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
6845ded66962SMark Johnston 	    sc->bge_cdata.bge_status_map,
6846ded66962SMark Johnston 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
6847ded66962SMark Johnston 
6848ded66962SMark Johnston 	(void)bge_rxeof(sc, rx_prod, 0);
6849ded66962SMark Johnston 	bge_txeof(sc, tx_cons);
6850ded66962SMark Johnston 	return (0);
6851ded66962SMark Johnston }
68527790c8c1SConrad Meyer #endif /* DEBUGNET */
6853