xref: /freebsd/sys/dev/bge/if_bge.c (revision 3e38757d4c52f7c2b33e4ab667ebc55e334a6ca0)
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 
53595d67482SBill Paul static devclass_t bge_devclass;
53695d67482SBill Paul 
537f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
538ea72f463SWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device", pci, bge, bge_devs,
539329e817fSWarner Losh     nitems(bge_devs) - 1);
540*3e38757dSJohn Baldwin DRIVER_MODULE(miibus, bge, miibus_driver, 0, 0);
54195d67482SBill Paul 
542f1a7e6d5SScott Long static int bge_allow_asf = 1;
543f1a7e6d5SScott Long 
5447029da5cSPawel Biernacki static SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
5457029da5cSPawel Biernacki     "BGE driver parameters");
546af3b2549SHans Petter Selasky SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RDTUN, &bge_allow_asf, 0,
547f1a7e6d5SScott Long 	"Allow ASF mode if available");
548c4529f41SMichael Reifenberger 
54908013fd3SMarius Strobl static int
5505fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
55108013fd3SMarius Strobl {
55208013fd3SMarius Strobl 	return (1);
55308013fd3SMarius Strobl }
55408013fd3SMarius Strobl 
5553f74909aSGleb Smirnoff static uint32_t
5563f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
55795d67482SBill Paul {
55895d67482SBill Paul 	device_t dev;
5596f8718a3SScott Long 	uint32_t val;
56095d67482SBill Paul 
561a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
562a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
563a4431ebaSPyun YongHyeon 		return (0);
564a4431ebaSPyun YongHyeon 
56595d67482SBill Paul 	dev = sc->bge_dev;
56695d67482SBill Paul 
56795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
5686f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
5696f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
5706f8718a3SScott Long 	return (val);
57195d67482SBill Paul }
57295d67482SBill Paul 
57395d67482SBill Paul static void
5743f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
57595d67482SBill Paul {
57695d67482SBill Paul 	device_t dev;
57795d67482SBill Paul 
578a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
579a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
580a4431ebaSPyun YongHyeon 		return;
581a4431ebaSPyun YongHyeon 
58295d67482SBill Paul 	dev = sc->bge_dev;
58395d67482SBill Paul 
58495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
58595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
5866f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
58795d67482SBill Paul }
58895d67482SBill Paul 
58995d67482SBill Paul #ifdef notdef
5903f74909aSGleb Smirnoff static uint32_t
5913f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
59295d67482SBill Paul {
59395d67482SBill Paul 	device_t dev;
59495d67482SBill Paul 
59595d67482SBill Paul 	dev = sc->bge_dev;
59695d67482SBill Paul 
59795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
59895d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
59995d67482SBill Paul }
60095d67482SBill Paul #endif
60195d67482SBill Paul 
60295d67482SBill Paul static void
6033f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
60495d67482SBill Paul {
60595d67482SBill Paul 	device_t dev;
60695d67482SBill Paul 
60795d67482SBill Paul 	dev = sc->bge_dev;
60895d67482SBill Paul 
60995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
61095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
61195d67482SBill Paul }
61295d67482SBill Paul 
6136f8718a3SScott Long static void
6146f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
6156f8718a3SScott Long {
6166f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6176f8718a3SScott Long }
6186f8718a3SScott Long 
61938cc658fSJohn Baldwin static void
62038cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
62138cc658fSJohn Baldwin {
62238cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
62338cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
62438cc658fSJohn Baldwin 
62538cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
626062af0b0SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_MBOX_REORDER) != 0)
627062af0b0SPyun YongHyeon 		CSR_READ_4(sc, off);
62838cc658fSJohn Baldwin }
62938cc658fSJohn Baldwin 
630f41ac2beSBill Paul /*
631548c8f1aSPyun YongHyeon  * Clear all stale locks and select the lock for this driver instance.
632548c8f1aSPyun YongHyeon  */
633548c8f1aSPyun YongHyeon static void
634548c8f1aSPyun YongHyeon bge_ape_lock_init(struct bge_softc *sc)
635548c8f1aSPyun YongHyeon {
636548c8f1aSPyun YongHyeon 	uint32_t bit, regbase;
637548c8f1aSPyun YongHyeon 	int i;
638548c8f1aSPyun YongHyeon 
639548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
640548c8f1aSPyun YongHyeon 		regbase = BGE_APE_LOCK_GRANT;
641548c8f1aSPyun YongHyeon 	else
642548c8f1aSPyun YongHyeon 		regbase = BGE_APE_PER_LOCK_GRANT;
643548c8f1aSPyun YongHyeon 
644548c8f1aSPyun YongHyeon 	/* Clear any stale locks. */
645548c8f1aSPyun YongHyeon 	for (i = BGE_APE_LOCK_PHY0; i <= BGE_APE_LOCK_GPIO; i++) {
646548c8f1aSPyun YongHyeon 		switch (i) {
647548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY0:
648548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY1:
649548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY2:
650548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY3:
651548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
652548c8f1aSPyun YongHyeon 			break;
653548c8f1aSPyun YongHyeon 		default:
654bd9c196aSPyun YongHyeon 			if (sc->bge_func_addr == 0)
655548c8f1aSPyun YongHyeon 				bit = BGE_APE_LOCK_GRANT_DRIVER0;
656548c8f1aSPyun YongHyeon 			else
657548c8f1aSPyun YongHyeon 				bit = (1 << sc->bge_func_addr);
658548c8f1aSPyun YongHyeon 		}
659548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, regbase + 4 * i, bit);
660548c8f1aSPyun YongHyeon 	}
661548c8f1aSPyun YongHyeon 
662548c8f1aSPyun YongHyeon 	/* Select the PHY lock based on the device's function number. */
663548c8f1aSPyun YongHyeon 	switch (sc->bge_func_addr) {
664548c8f1aSPyun YongHyeon 	case 0:
665548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY0;
666548c8f1aSPyun YongHyeon 		break;
667548c8f1aSPyun YongHyeon 	case 1:
668548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY1;
669548c8f1aSPyun YongHyeon 		break;
670548c8f1aSPyun YongHyeon 	case 2:
671548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY2;
672548c8f1aSPyun YongHyeon 		break;
673548c8f1aSPyun YongHyeon 	case 3:
674548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY3;
675548c8f1aSPyun YongHyeon 		break;
676548c8f1aSPyun YongHyeon 	default:
677548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev,
678548c8f1aSPyun YongHyeon 		    "PHY lock not supported on this function\n");
679548c8f1aSPyun YongHyeon 	}
680548c8f1aSPyun YongHyeon }
681548c8f1aSPyun YongHyeon 
682548c8f1aSPyun YongHyeon /*
683548c8f1aSPyun YongHyeon  * Check for APE firmware, set flags, and print version info.
684548c8f1aSPyun YongHyeon  */
685548c8f1aSPyun YongHyeon static void
686548c8f1aSPyun YongHyeon bge_ape_read_fw_ver(struct bge_softc *sc)
687548c8f1aSPyun YongHyeon {
688548c8f1aSPyun YongHyeon 	const char *fwtype;
689548c8f1aSPyun YongHyeon 	uint32_t apedata, features;
690548c8f1aSPyun YongHyeon 
691548c8f1aSPyun YongHyeon 	/* Check for a valid APE signature in shared memory. */
692548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_SEG_SIG);
693548c8f1aSPyun YongHyeon 	if (apedata != BGE_APE_SEG_SIG_MAGIC) {
694548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags &= ~ BGE_MFW_ON_APE;
695548c8f1aSPyun YongHyeon 		return;
696548c8f1aSPyun YongHyeon 	}
697548c8f1aSPyun YongHyeon 
698548c8f1aSPyun YongHyeon 	/* Check if APE firmware is running. */
699548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_STATUS);
700548c8f1aSPyun YongHyeon 	if ((apedata & BGE_APE_FW_STATUS_READY) == 0) {
701548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE signature found "
702548c8f1aSPyun YongHyeon 		    "but FW status not ready! 0x%08x\n", apedata);
703548c8f1aSPyun YongHyeon 		return;
704548c8f1aSPyun YongHyeon 	}
705548c8f1aSPyun YongHyeon 
706548c8f1aSPyun YongHyeon 	sc->bge_mfw_flags |= BGE_MFW_ON_APE;
707548c8f1aSPyun YongHyeon 
708548c8f1aSPyun YongHyeon 	/* Fetch the APE firwmare type and version. */
709548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_VERSION);
710548c8f1aSPyun YongHyeon 	features = APE_READ_4(sc, BGE_APE_FW_FEATURES);
711548c8f1aSPyun YongHyeon 	if ((features & BGE_APE_FW_FEATURE_NCSI) != 0) {
712548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_NCSI;
713548c8f1aSPyun YongHyeon 		fwtype = "NCSI";
714548c8f1aSPyun YongHyeon 	} else if ((features & BGE_APE_FW_FEATURE_DASH) != 0) {
715548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_DASH;
716548c8f1aSPyun YongHyeon 		fwtype = "DASH";
717548c8f1aSPyun YongHyeon 	} else
718548c8f1aSPyun YongHyeon 		fwtype = "UNKN";
719548c8f1aSPyun YongHyeon 
720548c8f1aSPyun YongHyeon 	/* Print the APE firmware version. */
721548c8f1aSPyun YongHyeon 	device_printf(sc->bge_dev, "APE FW version: %s v%d.%d.%d.%d\n",
722548c8f1aSPyun YongHyeon 	    fwtype,
723548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MAJMSK) >> BGE_APE_FW_VERSION_MAJSFT,
724548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MINMSK) >> BGE_APE_FW_VERSION_MINSFT,
725548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_REVMSK) >> BGE_APE_FW_VERSION_REVSFT,
726548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_BLDMSK));
727548c8f1aSPyun YongHyeon }
728548c8f1aSPyun YongHyeon 
729548c8f1aSPyun YongHyeon static int
730548c8f1aSPyun YongHyeon bge_ape_lock(struct bge_softc *sc, int locknum)
731548c8f1aSPyun YongHyeon {
732548c8f1aSPyun YongHyeon 	uint32_t bit, gnt, req, status;
733548c8f1aSPyun YongHyeon 	int i, off;
734548c8f1aSPyun YongHyeon 
735548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
736548c8f1aSPyun YongHyeon 		return (0);
737548c8f1aSPyun YongHyeon 
738548c8f1aSPyun YongHyeon 	/* Lock request/grant registers have different bases. */
739548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761) {
740548c8f1aSPyun YongHyeon 		req = BGE_APE_LOCK_REQ;
741548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
742548c8f1aSPyun YongHyeon 	} else {
743548c8f1aSPyun YongHyeon 		req = BGE_APE_PER_LOCK_REQ;
744548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
745548c8f1aSPyun YongHyeon 	}
746548c8f1aSPyun YongHyeon 
747548c8f1aSPyun YongHyeon 	off = 4 * locknum;
748548c8f1aSPyun YongHyeon 
749548c8f1aSPyun YongHyeon 	switch (locknum) {
750548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
751548c8f1aSPyun YongHyeon 		/* Lock required when using GPIO. */
752548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
753548c8f1aSPyun YongHyeon 			return (0);
754548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
755548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
756548c8f1aSPyun YongHyeon 		else
757548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
758548c8f1aSPyun YongHyeon 		break;
759548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
760548c8f1aSPyun YongHyeon 		/* Lock required to reset the device. */
761548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
762548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
763548c8f1aSPyun YongHyeon 		else
764548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
765548c8f1aSPyun YongHyeon 		break;
766548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
767548c8f1aSPyun YongHyeon 		/* Lock required when accessing certain APE memory. */
768548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
769548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
770548c8f1aSPyun YongHyeon 		else
771548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
772548c8f1aSPyun YongHyeon 		break;
773548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
774548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
775548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
776548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
777548c8f1aSPyun YongHyeon 		/* Lock required when accessing PHYs. */
778548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_REQ_DRIVER0;
779548c8f1aSPyun YongHyeon 		break;
780548c8f1aSPyun YongHyeon 	default:
781548c8f1aSPyun YongHyeon 		return (EINVAL);
782548c8f1aSPyun YongHyeon 	}
783548c8f1aSPyun YongHyeon 
784548c8f1aSPyun YongHyeon 	/* Request a lock. */
785548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, req + off, bit);
786548c8f1aSPyun YongHyeon 
787548c8f1aSPyun YongHyeon 	/* Wait up to 1 second to acquire lock. */
788548c8f1aSPyun YongHyeon 	for (i = 0; i < 20000; i++) {
789548c8f1aSPyun YongHyeon 		status = APE_READ_4(sc, gnt + off);
790548c8f1aSPyun YongHyeon 		if (status == bit)
791548c8f1aSPyun YongHyeon 			break;
792548c8f1aSPyun YongHyeon 		DELAY(50);
793548c8f1aSPyun YongHyeon 	}
794548c8f1aSPyun YongHyeon 
795548c8f1aSPyun YongHyeon 	/* Handle any errors. */
796548c8f1aSPyun YongHyeon 	if (status != bit) {
797548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE lock %d request failed! "
798548c8f1aSPyun YongHyeon 		    "request = 0x%04x[0x%04x], status = 0x%04x[0x%04x]\n",
799548c8f1aSPyun YongHyeon 		    locknum, req + off, bit & 0xFFFF, gnt + off,
800548c8f1aSPyun YongHyeon 		    status & 0xFFFF);
801548c8f1aSPyun YongHyeon 		/* Revoke the lock request. */
802548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, gnt + off, bit);
803548c8f1aSPyun YongHyeon 		return (EBUSY);
804548c8f1aSPyun YongHyeon 	}
805548c8f1aSPyun YongHyeon 
806548c8f1aSPyun YongHyeon 	return (0);
807548c8f1aSPyun YongHyeon }
808548c8f1aSPyun YongHyeon 
809548c8f1aSPyun YongHyeon static void
810548c8f1aSPyun YongHyeon bge_ape_unlock(struct bge_softc *sc, int locknum)
811548c8f1aSPyun YongHyeon {
812548c8f1aSPyun YongHyeon 	uint32_t bit, gnt;
813548c8f1aSPyun YongHyeon 	int off;
814548c8f1aSPyun YongHyeon 
815548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
816548c8f1aSPyun YongHyeon 		return;
817548c8f1aSPyun YongHyeon 
818548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
819548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
820548c8f1aSPyun YongHyeon 	else
821548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
822548c8f1aSPyun YongHyeon 
823548c8f1aSPyun YongHyeon 	off = 4 * locknum;
824548c8f1aSPyun YongHyeon 
825548c8f1aSPyun YongHyeon 	switch (locknum) {
826548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
827548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
828548c8f1aSPyun YongHyeon 			return;
829548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
830548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
831548c8f1aSPyun YongHyeon 		else
832548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
833548c8f1aSPyun YongHyeon 		break;
834548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
835548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
836548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
837548c8f1aSPyun YongHyeon 		else
838548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
839548c8f1aSPyun YongHyeon 		break;
840548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
841548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
842548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
843548c8f1aSPyun YongHyeon 		else
844548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
845548c8f1aSPyun YongHyeon 		break;
846548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
847548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
848548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
849548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
850548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_GRANT_DRIVER0;
851548c8f1aSPyun YongHyeon 		break;
852548c8f1aSPyun YongHyeon 	default:
853548c8f1aSPyun YongHyeon 		return;
854548c8f1aSPyun YongHyeon 	}
855548c8f1aSPyun YongHyeon 
856548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, gnt + off, bit);
857548c8f1aSPyun YongHyeon }
858548c8f1aSPyun YongHyeon 
859548c8f1aSPyun YongHyeon /*
860548c8f1aSPyun YongHyeon  * Send an event to the APE firmware.
861548c8f1aSPyun YongHyeon  */
862548c8f1aSPyun YongHyeon static void
863548c8f1aSPyun YongHyeon bge_ape_send_event(struct bge_softc *sc, uint32_t event)
864548c8f1aSPyun YongHyeon {
865548c8f1aSPyun YongHyeon 	uint32_t apedata;
866548c8f1aSPyun YongHyeon 	int i;
867548c8f1aSPyun YongHyeon 
868548c8f1aSPyun YongHyeon 	/* NCSI does not support APE events. */
869548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
870548c8f1aSPyun YongHyeon 		return;
871548c8f1aSPyun YongHyeon 
872548c8f1aSPyun YongHyeon 	/* Wait up to 1ms for APE to service previous event. */
873548c8f1aSPyun YongHyeon 	for (i = 10; i > 0; i--) {
874548c8f1aSPyun YongHyeon 		if (bge_ape_lock(sc, BGE_APE_LOCK_MEM) != 0)
875548c8f1aSPyun YongHyeon 			break;
876548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_EVENT_STATUS);
877548c8f1aSPyun YongHyeon 		if ((apedata & BGE_APE_EVENT_STATUS_EVENT_PENDING) == 0) {
878548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT_STATUS, event |
879548c8f1aSPyun YongHyeon 			    BGE_APE_EVENT_STATUS_EVENT_PENDING);
880548c8f1aSPyun YongHyeon 			bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
881548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT, BGE_APE_EVENT_1);
882548c8f1aSPyun YongHyeon 			break;
883548c8f1aSPyun YongHyeon 		}
884548c8f1aSPyun YongHyeon 		bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
885548c8f1aSPyun YongHyeon 		DELAY(100);
886548c8f1aSPyun YongHyeon 	}
887548c8f1aSPyun YongHyeon 	if (i == 0)
888548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE event 0x%08x send timed out\n",
889548c8f1aSPyun YongHyeon 		    event);
890548c8f1aSPyun YongHyeon }
891548c8f1aSPyun YongHyeon 
892548c8f1aSPyun YongHyeon static void
893548c8f1aSPyun YongHyeon bge_ape_driver_state_change(struct bge_softc *sc, int kind)
894548c8f1aSPyun YongHyeon {
895548c8f1aSPyun YongHyeon 	uint32_t apedata, event;
896548c8f1aSPyun YongHyeon 
897548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
898548c8f1aSPyun YongHyeon 		return;
899548c8f1aSPyun YongHyeon 
900548c8f1aSPyun YongHyeon 	switch (kind) {
901548c8f1aSPyun YongHyeon 	case BGE_RESET_START:
902548c8f1aSPyun YongHyeon 		/* If this is the first load, clear the load counter. */
903548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_HOST_SEG_SIG);
904548c8f1aSPyun YongHyeon 		if (apedata != BGE_APE_HOST_SEG_SIG_MAGIC)
905548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, 0);
906548c8f1aSPyun YongHyeon 		else {
907548c8f1aSPyun YongHyeon 			apedata = APE_READ_4(sc, BGE_APE_HOST_INIT_COUNT);
908548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, ++apedata);
909548c8f1aSPyun YongHyeon 		}
910548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_SIG,
911548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_SIG_MAGIC);
912548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_LEN,
913548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_LEN_MAGIC);
914548c8f1aSPyun YongHyeon 
915548c8f1aSPyun YongHyeon 		/* Add some version info if bge(4) supports it. */
916548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRIVER_ID,
917548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRIVER_ID_MAGIC(1, 0));
918548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_BEHAVIOR,
919548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_BEHAV_NO_PHYLOCK);
920548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_HEARTBEAT_INT_MS,
921548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_HEARTBEAT_INT_DISABLE);
922548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
923548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_START);
924548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_START;
925548c8f1aSPyun YongHyeon 		break;
926548c8f1aSPyun YongHyeon 	case BGE_RESET_SHUTDOWN:
927548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
928548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_UNLOAD);
929548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_UNLOAD;
930548c8f1aSPyun YongHyeon 		break;
931548c8f1aSPyun YongHyeon 	case BGE_RESET_SUSPEND:
932548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_SUSPEND;
933548c8f1aSPyun YongHyeon 		break;
934548c8f1aSPyun YongHyeon 	default:
935548c8f1aSPyun YongHyeon 		return;
936548c8f1aSPyun YongHyeon 	}
937548c8f1aSPyun YongHyeon 
938548c8f1aSPyun YongHyeon 	bge_ape_send_event(sc, event | BGE_APE_EVENT_STATUS_DRIVER_EVNT |
939548c8f1aSPyun YongHyeon 	    BGE_APE_EVENT_STATUS_STATE_CHNGE);
940548c8f1aSPyun YongHyeon }
941548c8f1aSPyun YongHyeon 
942548c8f1aSPyun YongHyeon /*
943f41ac2beSBill Paul  * Map a single buffer address.
944f41ac2beSBill Paul  */
945f41ac2beSBill Paul 
946f41ac2beSBill Paul static void
9473f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
948f41ac2beSBill Paul {
949f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
950f41ac2beSBill Paul 
951f41ac2beSBill Paul 	if (error)
952f41ac2beSBill Paul 		return;
953f41ac2beSBill Paul 
9545b610048SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
9555b610048SPyun YongHyeon 
956f41ac2beSBill Paul 	ctx = arg;
957f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
958f41ac2beSBill Paul }
959f41ac2beSBill Paul 
96038cc658fSJohn Baldwin static uint8_t
96138cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
96238cc658fSJohn Baldwin {
96338cc658fSJohn Baldwin 	uint32_t access, byte = 0;
96438cc658fSJohn Baldwin 	int i;
96538cc658fSJohn Baldwin 
96638cc658fSJohn Baldwin 	/* Lock. */
96738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
96838cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
96938cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
97038cc658fSJohn Baldwin 			break;
97138cc658fSJohn Baldwin 		DELAY(20);
97238cc658fSJohn Baldwin 	}
97338cc658fSJohn Baldwin 	if (i == 8000)
97438cc658fSJohn Baldwin 		return (1);
97538cc658fSJohn Baldwin 
97638cc658fSJohn Baldwin 	/* Enable access. */
97738cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
97838cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
97938cc658fSJohn Baldwin 
98038cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
98138cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
98238cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
98338cc658fSJohn Baldwin 		DELAY(10);
98438cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
98538cc658fSJohn Baldwin 			DELAY(10);
98638cc658fSJohn Baldwin 			break;
98738cc658fSJohn Baldwin 		}
98838cc658fSJohn Baldwin 	}
98938cc658fSJohn Baldwin 
99038cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
99138cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
99238cc658fSJohn Baldwin 		return (1);
99338cc658fSJohn Baldwin 	}
99438cc658fSJohn Baldwin 
99538cc658fSJohn Baldwin 	/* Get result. */
99638cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
99738cc658fSJohn Baldwin 
99838cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
99938cc658fSJohn Baldwin 
100038cc658fSJohn Baldwin 	/* Disable access. */
100138cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
100238cc658fSJohn Baldwin 
100338cc658fSJohn Baldwin 	/* Unlock. */
100438cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
100538cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
100638cc658fSJohn Baldwin 
100738cc658fSJohn Baldwin 	return (0);
100838cc658fSJohn Baldwin }
100938cc658fSJohn Baldwin 
101038cc658fSJohn Baldwin /*
101138cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
101238cc658fSJohn Baldwin  */
101338cc658fSJohn Baldwin static int
101438cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
101538cc658fSJohn Baldwin {
101638cc658fSJohn Baldwin 	int err = 0, i;
101738cc658fSJohn Baldwin 	uint8_t byte = 0;
101838cc658fSJohn Baldwin 
101938cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
102038cc658fSJohn Baldwin 		return (1);
102138cc658fSJohn Baldwin 
102238cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
102338cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
102438cc658fSJohn Baldwin 		if (err)
102538cc658fSJohn Baldwin 			break;
102638cc658fSJohn Baldwin 		*(dest + i) = byte;
102738cc658fSJohn Baldwin 	}
102838cc658fSJohn Baldwin 
102938cc658fSJohn Baldwin 	return (err ? 1 : 0);
103038cc658fSJohn Baldwin }
103138cc658fSJohn Baldwin 
103295d67482SBill Paul /*
103395d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
103495d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
103595d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
103695d67482SBill Paul  * access method.
103795d67482SBill Paul  */
10383f74909aSGleb Smirnoff static uint8_t
10393f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
104095d67482SBill Paul {
104195d67482SBill Paul 	int i;
10423f74909aSGleb Smirnoff 	uint32_t byte = 0;
104395d67482SBill Paul 
104495d67482SBill Paul 	/*
104595d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
104695d67482SBill Paul 	 * having to use the bitbang method.
104795d67482SBill Paul 	 */
104895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
104995d67482SBill Paul 
105095d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
105195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
105295d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
105395d67482SBill Paul 	DELAY(20);
105495d67482SBill Paul 
105595d67482SBill Paul 	/* Issue the read EEPROM command. */
105695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
105795d67482SBill Paul 
105895d67482SBill Paul 	/* Wait for completion */
105995d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
106095d67482SBill Paul 		DELAY(10);
106195d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
106295d67482SBill Paul 			break;
106395d67482SBill Paul 	}
106495d67482SBill Paul 
1065d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
1066fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
1067f6789fbaSPyun YongHyeon 		return (1);
106895d67482SBill Paul 	}
106995d67482SBill Paul 
107095d67482SBill Paul 	/* Get result. */
107195d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
107295d67482SBill Paul 
10730c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
107495d67482SBill Paul 
107595d67482SBill Paul 	return (0);
107695d67482SBill Paul }
107795d67482SBill Paul 
107895d67482SBill Paul /*
107995d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
108095d67482SBill Paul  */
108195d67482SBill Paul static int
10823f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
108395d67482SBill Paul {
10843f74909aSGleb Smirnoff 	int i, error = 0;
10853f74909aSGleb Smirnoff 	uint8_t byte = 0;
108695d67482SBill Paul 
108795d67482SBill Paul 	for (i = 0; i < cnt; i++) {
10883f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
10893f74909aSGleb Smirnoff 		if (error)
109095d67482SBill Paul 			break;
109195d67482SBill Paul 		*(dest + i) = byte;
109295d67482SBill Paul 	}
109395d67482SBill Paul 
10943f74909aSGleb Smirnoff 	return (error ? 1 : 0);
109595d67482SBill Paul }
109695d67482SBill Paul 
109795d67482SBill Paul static int
10983f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
109995d67482SBill Paul {
110095d67482SBill Paul 	struct bge_softc *sc;
1101a813ed78SPyun YongHyeon 	uint32_t val;
110295d67482SBill Paul 	int i;
110395d67482SBill Paul 
110495d67482SBill Paul 	sc = device_get_softc(dev);
110595d67482SBill Paul 
1106548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1107548c8f1aSPyun YongHyeon 		return (0);
1108548c8f1aSPyun YongHyeon 
1109a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1110a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1111a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1112a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1113a813ed78SPyun YongHyeon 		DELAY(80);
111437ceeb4dSPaul Saab 	}
111537ceeb4dSPaul Saab 
111695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
111795d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
111895d67482SBill Paul 
1119a813ed78SPyun YongHyeon 	/* Poll for the PHY register access to complete. */
112095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1121d5d23857SJung-uk Kim 		DELAY(10);
112295d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
1123a813ed78SPyun YongHyeon 		if ((val & BGE_MICOMM_BUSY) == 0) {
1124a813ed78SPyun YongHyeon 			DELAY(5);
1125a813ed78SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_MI_COMM);
112695d67482SBill Paul 			break;
112795d67482SBill Paul 		}
1128a813ed78SPyun YongHyeon 	}
112995d67482SBill Paul 
113095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
11315fea260fSMarius Strobl 		device_printf(sc->bge_dev,
11325fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
11335fea260fSMarius Strobl 		    phy, reg, val);
113437ceeb4dSPaul Saab 		val = 0;
113595d67482SBill Paul 	}
113695d67482SBill Paul 
1137a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1138a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1139a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1140a813ed78SPyun YongHyeon 		DELAY(80);
114137ceeb4dSPaul Saab 	}
114237ceeb4dSPaul Saab 
1143548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1144548c8f1aSPyun YongHyeon 
114595d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
114695d67482SBill Paul 		return (0);
114795d67482SBill Paul 
11480c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
114995d67482SBill Paul }
115095d67482SBill Paul 
115195d67482SBill Paul static int
11523f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
115395d67482SBill Paul {
115495d67482SBill Paul 	struct bge_softc *sc;
115595d67482SBill Paul 	int i;
115695d67482SBill Paul 
115795d67482SBill Paul 	sc = device_get_softc(dev);
115895d67482SBill Paul 
115938cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
116038cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
116138cc658fSJohn Baldwin 		return (0);
116238cc658fSJohn Baldwin 
1163548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1164548c8f1aSPyun YongHyeon 		return (0);
1165548c8f1aSPyun YongHyeon 
1166a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1167a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1168a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1169a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1170a813ed78SPyun YongHyeon 		DELAY(80);
117137ceeb4dSPaul Saab 	}
117237ceeb4dSPaul Saab 
117395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
117495d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
117595d67482SBill Paul 
117695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1177d5d23857SJung-uk Kim 		DELAY(10);
117838cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
117938cc658fSJohn Baldwin 			DELAY(5);
118038cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
118195d67482SBill Paul 			break;
1182d5d23857SJung-uk Kim 		}
118338cc658fSJohn Baldwin 	}
1184d5d23857SJung-uk Kim 
1185a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1186a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1187a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1188a813ed78SPyun YongHyeon 		DELAY(80);
1189a813ed78SPyun YongHyeon 	}
1190a813ed78SPyun YongHyeon 
1191548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1192548c8f1aSPyun YongHyeon 
1193a813ed78SPyun YongHyeon 	if (i == BGE_TIMEOUT)
119438cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
11952246e8c6SPyun YongHyeon 		    "PHY write timed out (phy %d, reg %d, val 0x%04x)\n",
119638cc658fSJohn Baldwin 		    phy, reg, val);
119737ceeb4dSPaul Saab 
119895d67482SBill Paul 	return (0);
119995d67482SBill Paul }
120095d67482SBill Paul 
120195d67482SBill Paul static void
12023f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
120395d67482SBill Paul {
120495d67482SBill Paul 	struct bge_softc *sc;
120595d67482SBill Paul 	struct mii_data *mii;
1206a0a03d1eSPyun YongHyeon 	uint32_t mac_mode, rx_mode, tx_mode;
1207e4146b95SPyun YongHyeon 
120895d67482SBill Paul 	sc = device_get_softc(dev);
1209fba8b109SMarcel Moolenaar 	if ((if_getdrvflags(sc->bge_ifp) & IFF_DRV_RUNNING) == 0)
1210e4146b95SPyun YongHyeon 		return;
121195d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
121295d67482SBill Paul 
1213d4f5240aSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1214d4f5240aSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
1215d4f5240aSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
1216d4f5240aSPyun YongHyeon 		case IFM_10_T:
1217d4f5240aSPyun YongHyeon 		case IFM_100_TX:
1218d4f5240aSPyun YongHyeon 			sc->bge_link = 1;
1219d4f5240aSPyun YongHyeon 			break;
1220d4f5240aSPyun YongHyeon 		case IFM_1000_T:
1221d4f5240aSPyun YongHyeon 		case IFM_1000_SX:
1222d4f5240aSPyun YongHyeon 		case IFM_2500_SX:
1223d4f5240aSPyun YongHyeon 			if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
1224d4f5240aSPyun YongHyeon 				sc->bge_link = 1;
1225d4f5240aSPyun YongHyeon 			else
1226d4f5240aSPyun YongHyeon 				sc->bge_link = 0;
1227d4f5240aSPyun YongHyeon 			break;
1228d4f5240aSPyun YongHyeon 		default:
1229d4f5240aSPyun YongHyeon 			sc->bge_link = 0;
1230d4f5240aSPyun YongHyeon 			break;
1231d4f5240aSPyun YongHyeon 		}
1232d4f5240aSPyun YongHyeon 	} else
1233d4f5240aSPyun YongHyeon 		sc->bge_link = 0;
1234d4f5240aSPyun YongHyeon 	if (sc->bge_link == 0)
1235d4f5240aSPyun YongHyeon 		return;
1236a0a03d1eSPyun YongHyeon 
1237a0a03d1eSPyun YongHyeon 	/*
1238a0a03d1eSPyun YongHyeon 	 * APE firmware touches these registers to keep the MAC
1239a0a03d1eSPyun YongHyeon 	 * connected to the outside world.  Try to keep the
1240a0a03d1eSPyun YongHyeon 	 * accesses atomic.
1241a0a03d1eSPyun YongHyeon 	 */
1242a0a03d1eSPyun YongHyeon 
1243a0a03d1eSPyun YongHyeon 	/* Set the port mode (MII/GMII) to match the link speed. */
1244a0a03d1eSPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) &
1245a0a03d1eSPyun YongHyeon 	    ~(BGE_MACMODE_PORTMODE | BGE_MACMODE_HALF_DUPLEX);
1246a0a03d1eSPyun YongHyeon 	tx_mode = CSR_READ_4(sc, BGE_TX_MODE);
1247a0a03d1eSPyun YongHyeon 	rx_mode = CSR_READ_4(sc, BGE_RX_MODE);
1248a0a03d1eSPyun YongHyeon 
1249ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
1250ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
1251a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_GMII;
12523f74909aSGleb Smirnoff 	else
1253a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_MII;
125495d67482SBill Paul 
1255a0a03d1eSPyun YongHyeon 	/* Set MAC flow control behavior to match link flow control settings. */
1256a0a03d1eSPyun YongHyeon 	tx_mode &= ~BGE_TXMODE_FLOWCTL_ENABLE;
1257a0a03d1eSPyun YongHyeon 	rx_mode &= ~BGE_RXMODE_FLOWCTL_ENABLE;
12584951ca86SPyun YongHyeon 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1259a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1260a0a03d1eSPyun YongHyeon 			tx_mode |= BGE_TXMODE_FLOWCTL_ENABLE;
1261a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1262a0a03d1eSPyun YongHyeon 			rx_mode |= BGE_RXMODE_FLOWCTL_ENABLE;
1263a0a03d1eSPyun YongHyeon 	} else
1264a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_MACMODE_HALF_DUPLEX;
1265a0a03d1eSPyun YongHyeon 
1266a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, mac_mode);
12679b80ffe7SPyun YongHyeon 	DELAY(40);
1268a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, tx_mode);
1269a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_RX_MODE, rx_mode);
127095d67482SBill Paul }
127195d67482SBill Paul 
127295d67482SBill Paul /*
127395d67482SBill Paul  * Intialize a standard receive ring descriptor.
127495d67482SBill Paul  */
127595d67482SBill Paul static int
1276943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
127795d67482SBill Paul {
1278943787f3SPyun YongHyeon 	struct mbuf *m;
127995d67482SBill Paul 	struct bge_rx_bd *r;
1280a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
1281943787f3SPyun YongHyeon 	bus_dmamap_t map;
1282a23634a1SPyun YongHyeon 	int error, nsegs;
128395d67482SBill Paul 
1284f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD &&
1285fba8b109SMarcel Moolenaar 	    (if_getmtu(sc->bge_ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
1286f5459d4cSPyun YongHyeon 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))) {
1287c6499eccSGleb Smirnoff 		m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
1288f5459d4cSPyun YongHyeon 		if (m == NULL)
1289f5459d4cSPyun YongHyeon 			return (ENOBUFS);
1290f5459d4cSPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1291f5459d4cSPyun YongHyeon 	} else {
1292c6499eccSGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1293943787f3SPyun YongHyeon 		if (m == NULL)
129495d67482SBill Paul 			return (ENOBUFS);
1295943787f3SPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MCLBYTES;
1296f5459d4cSPyun YongHyeon 	}
1297652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1298943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
1299943787f3SPyun YongHyeon 
13000ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
1301943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
1302a23634a1SPyun YongHyeon 	if (error != 0) {
1303943787f3SPyun YongHyeon 		m_freem(m);
1304a23634a1SPyun YongHyeon 		return (error);
1305f41ac2beSBill Paul 	}
1306943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1307943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1308943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
1309943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1310943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
1311943787f3SPyun YongHyeon 	}
1312943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
1313943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
1314943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
1315943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
1316e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
1317943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
1318a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
1319a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
1320e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
1321a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
1322e907febfSPyun YongHyeon 	r->bge_idx = i;
1323f41ac2beSBill Paul 
13240ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1325943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
132695d67482SBill Paul 
132795d67482SBill Paul 	return (0);
132895d67482SBill Paul }
132995d67482SBill Paul 
133095d67482SBill Paul /*
133195d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
133295d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
133395d67482SBill Paul  */
133495d67482SBill Paul static int
1335943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
133695d67482SBill Paul {
13371be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
1338943787f3SPyun YongHyeon 	bus_dmamap_t map;
13391be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
1340943787f3SPyun YongHyeon 	struct mbuf *m;
1341943787f3SPyun YongHyeon 	int error, nsegs;
134295d67482SBill Paul 
1343c6499eccSGleb Smirnoff 	MGETHDR(m, M_NOWAIT, MT_DATA);
1344943787f3SPyun YongHyeon 	if (m == NULL)
134595d67482SBill Paul 		return (ENOBUFS);
134695d67482SBill Paul 
13472a8c860fSRobert Watson 	if (m_cljget(m, M_NOWAIT, MJUM9BYTES) == NULL) {
1348943787f3SPyun YongHyeon 		m_freem(m);
134995d67482SBill Paul 		return (ENOBUFS);
135095d67482SBill Paul 	}
1351943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1352652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1353943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
13541be6acb7SGleb Smirnoff 
13551be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1356943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1357943787f3SPyun YongHyeon 	if (error != 0) {
1358943787f3SPyun YongHyeon 		m_freem(m);
13591be6acb7SGleb Smirnoff 		return (error);
1360f7cea149SGleb Smirnoff 	}
13611be6acb7SGleb Smirnoff 
1362aa8cbdbfSMarius Strobl 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1363943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1364943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1365943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1366943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1367943787f3SPyun YongHyeon 	}
1368943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1369943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1370943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1371943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1372943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1373e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1374e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1375e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1376e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1377e0b7b101SPyun YongHyeon 
13781be6acb7SGleb Smirnoff 	/*
13791be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
13801be6acb7SGleb Smirnoff 	 */
1381943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
13824e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
13834e7ba1abSGleb Smirnoff 	r->bge_idx = i;
13844e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
13854e7ba1abSGleb Smirnoff 	switch (nsegs) {
13864e7ba1abSGleb Smirnoff 	case 4:
13874e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
13884e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
13894e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1390e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
13914e7ba1abSGleb Smirnoff 	case 3:
1392e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1393e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1394e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1395e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
13964e7ba1abSGleb Smirnoff 	case 2:
13974e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
13984e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
13994e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1400e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
14014e7ba1abSGleb Smirnoff 	case 1:
14024e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
14034e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
14044e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1405e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
14064e7ba1abSGleb Smirnoff 		break;
14074e7ba1abSGleb Smirnoff 	default:
14084e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
14094e7ba1abSGleb Smirnoff 	}
1410f41ac2beSBill Paul 
1411a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1412943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
141395d67482SBill Paul 
141495d67482SBill Paul 	return (0);
141595d67482SBill Paul }
141695d67482SBill Paul 
141795d67482SBill Paul static int
14183f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
141995d67482SBill Paul {
14203ee5d7daSPyun YongHyeon 	int error, i;
142195d67482SBill Paul 
1422e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
142303e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1424e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1425943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
14263ee5d7daSPyun YongHyeon 			return (error);
142703e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
14281888f324SPyun YongHyeon 	}
142995d67482SBill Paul 
1430f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1431d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1432f41ac2beSBill Paul 
1433e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1434e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
143595d67482SBill Paul 
143695d67482SBill Paul 	return (0);
143795d67482SBill Paul }
143895d67482SBill Paul 
143995d67482SBill Paul static void
14403f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
144195d67482SBill Paul {
144295d67482SBill Paul 	int i;
144395d67482SBill Paul 
144495d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
144595d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
14460ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1447e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1448e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
14490ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1450f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1451e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1452e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
145395d67482SBill Paul 		}
1454f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
145595d67482SBill Paul 		    sizeof(struct bge_rx_bd));
145695d67482SBill Paul 	}
145795d67482SBill Paul }
145895d67482SBill Paul 
145995d67482SBill Paul static int
14603f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
146195d67482SBill Paul {
146295d67482SBill Paul 	struct bge_rcb *rcb;
14633ee5d7daSPyun YongHyeon 	int error, i;
146495d67482SBill Paul 
1465e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
146603e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
146795d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1468943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
14693ee5d7daSPyun YongHyeon 			return (error);
147003e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
14711888f324SPyun YongHyeon 	}
147295d67482SBill Paul 
1473f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1474d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1475f41ac2beSBill Paul 
1476e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
147795d67482SBill Paul 
14788a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1479f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
14808a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
14818a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
148267111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
148395d67482SBill Paul 
1484e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
148595d67482SBill Paul 
148695d67482SBill Paul 	return (0);
148795d67482SBill Paul }
148895d67482SBill Paul 
148995d67482SBill Paul static void
14903f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
149195d67482SBill Paul {
149295d67482SBill Paul 	int i;
149395d67482SBill Paul 
149495d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
149595d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1496e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1497e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1498e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1499f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1500f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1501e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1502e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
150395d67482SBill Paul 		}
1504f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
15051be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
150695d67482SBill Paul 	}
150795d67482SBill Paul }
150895d67482SBill Paul 
150995d67482SBill Paul static void
15103f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
151195d67482SBill Paul {
151295d67482SBill Paul 	int i;
151395d67482SBill Paul 
1514f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
151595d67482SBill Paul 		return;
151695d67482SBill Paul 
151795d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
151895d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
15190ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1520e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1521e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
15220ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1523f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1524e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1525e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
152695d67482SBill Paul 		}
1527f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
152895d67482SBill Paul 		    sizeof(struct bge_tx_bd));
152995d67482SBill Paul 	}
153095d67482SBill Paul }
153195d67482SBill Paul 
153295d67482SBill Paul static int
15333f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
153495d67482SBill Paul {
153595d67482SBill Paul 	sc->bge_txcnt = 0;
153695d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
15373927098fSPaul Saab 
1538e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1539e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
15405c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1541e6bf277eSPyun YongHyeon 
154214bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
154314bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
154438cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
154514bbd30fSGleb Smirnoff 
15463927098fSPaul Saab 	/* 5700 b2 errata */
1547e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
154838cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
15493927098fSPaul Saab 
155014bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
155138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
15523927098fSPaul Saab 	/* 5700 b2 errata */
1553e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
155438cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
155595d67482SBill Paul 
155695d67482SBill Paul 	return (0);
155795d67482SBill Paul }
155895d67482SBill Paul 
155995d67482SBill Paul static void
15603e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
15613e9b1bcaSJung-uk Kim {
1562fba8b109SMarcel Moolenaar 	if_t ifp;
15633e9b1bcaSJung-uk Kim 
15643e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
15653e9b1bcaSJung-uk Kim 
15663e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
15673e9b1bcaSJung-uk Kim 
156845ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
1569fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_PROMISC)
157045ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
15713e9b1bcaSJung-uk Kim 	else
157245ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
15733e9b1bcaSJung-uk Kim }
15743e9b1bcaSJung-uk Kim 
15750322ca23SGleb Smirnoff static u_int
15760322ca23SGleb Smirnoff bge_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
15770322ca23SGleb Smirnoff {
15780322ca23SGleb Smirnoff 	uint32_t *hashes = arg;
15790322ca23SGleb Smirnoff 	int h;
15800322ca23SGleb Smirnoff 
15810322ca23SGleb Smirnoff 	h = ether_crc32_le(LLADDR(sdl), ETHER_ADDR_LEN) & 0x7F;
15820322ca23SGleb Smirnoff 	hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
15830322ca23SGleb Smirnoff 
15840322ca23SGleb Smirnoff 	return (1);
15850322ca23SGleb Smirnoff }
15860322ca23SGleb Smirnoff 
15873e9b1bcaSJung-uk Kim static void
15883f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
158995d67482SBill Paul {
1590fba8b109SMarcel Moolenaar 	if_t ifp;
15913f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
15920322ca23SGleb Smirnoff 	int i;
159395d67482SBill Paul 
15940f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
15950f9bd73bSSam Leffler 
1596fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
159795d67482SBill Paul 
1598fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_ALLMULTI || if_getflags(ifp) & IFF_PROMISC) {
159995d67482SBill Paul 		for (i = 0; i < 4; i++)
16000c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
160195d67482SBill Paul 		return;
160295d67482SBill Paul 	}
160395d67482SBill Paul 
160495d67482SBill Paul 	/* First, zot all the existing filters. */
160595d67482SBill Paul 	for (i = 0; i < 4; i++)
160695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
160795d67482SBill Paul 
16080322ca23SGleb Smirnoff 	if_foreach_llmaddr(ifp, bge_hash_maddr, hashes);
160995d67482SBill Paul 
161095d67482SBill Paul 	for (i = 0; i < 4; i++)
161195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
161295d67482SBill Paul }
161395d67482SBill Paul 
16148cb1383cSDoug Ambrisko static void
1615cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1616cb2eacc7SYaroslav Tykhiy {
1617fba8b109SMarcel Moolenaar 	if_t ifp;
1618cb2eacc7SYaroslav Tykhiy 
1619cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1620cb2eacc7SYaroslav Tykhiy 
1621cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1622cb2eacc7SYaroslav Tykhiy 
1623cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1624fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING)
1625cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1626cb2eacc7SYaroslav Tykhiy 	else
1627cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1628cb2eacc7SYaroslav Tykhiy }
1629cb2eacc7SYaroslav Tykhiy 
1630cb2eacc7SYaroslav Tykhiy static void
1631797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
16328cb1383cSDoug Ambrisko {
1633797ab05eSPyun YongHyeon 
16348cb1383cSDoug Ambrisko 	/*
16358cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
16368cb1383cSDoug Ambrisko 	 */
16378cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
1638888b47f0SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
16398cb1383cSDoug Ambrisko 
16408cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16418cb1383cSDoug Ambrisko 		switch (type) {
16428cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1643224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1644224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
16458cb1383cSDoug Ambrisko 			break;
1646548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1647224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1648224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
16498cb1383cSDoug Ambrisko 			break;
1650548c8f1aSPyun YongHyeon 		case BGE_RESET_SUSPEND:
1651548c8f1aSPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1652548c8f1aSPyun YongHyeon 			    BGE_FW_DRV_STATE_SUSPEND);
1653548c8f1aSPyun YongHyeon 			break;
16548cb1383cSDoug Ambrisko 		}
16558cb1383cSDoug Ambrisko 	}
1656548c8f1aSPyun YongHyeon 
1657548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_START || type == BGE_RESET_SUSPEND)
1658548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
16598cb1383cSDoug Ambrisko }
16608cb1383cSDoug Ambrisko 
16618cb1383cSDoug Ambrisko static void
1662797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
16638cb1383cSDoug Ambrisko {
1664797ab05eSPyun YongHyeon 
16658cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16668cb1383cSDoug Ambrisko 		switch (type) {
16678cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1668224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1669224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START_DONE);
16708cb1383cSDoug Ambrisko 			/* START DONE */
16718cb1383cSDoug Ambrisko 			break;
1672548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1673224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1674224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD_DONE);
16758cb1383cSDoug Ambrisko 			break;
16768cb1383cSDoug Ambrisko 		}
16778cb1383cSDoug Ambrisko 	}
1678548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_SHUTDOWN)
1679548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
16808cb1383cSDoug Ambrisko }
16818cb1383cSDoug Ambrisko 
16828cb1383cSDoug Ambrisko static void
1683797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
16848cb1383cSDoug Ambrisko {
1685797ab05eSPyun YongHyeon 
16868cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
16878cb1383cSDoug Ambrisko 		switch (type) {
16888cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1689224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1690224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
16918cb1383cSDoug Ambrisko 			break;
1692548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1693224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1694224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
16958cb1383cSDoug Ambrisko 			break;
16968cb1383cSDoug Ambrisko 		}
16978cb1383cSDoug Ambrisko 	}
16988cb1383cSDoug Ambrisko }
16998cb1383cSDoug Ambrisko 
1700797ab05eSPyun YongHyeon static void
1701797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
17028cb1383cSDoug Ambrisko {
17038cb1383cSDoug Ambrisko 	int i;
17048cb1383cSDoug Ambrisko 
17058cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17063c201200SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB, BGE_FW_CMD_PAUSE);
17073fed2d5dSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
17089931ba85SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RX_CPU_EVENT) | BGE_RX_CPU_DRV_EVENT);
17098cb1383cSDoug Ambrisko 
17108cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
17119931ba85SPyun YongHyeon 			if (!(CSR_READ_4(sc, BGE_RX_CPU_EVENT) &
17129931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT))
17138cb1383cSDoug Ambrisko 				break;
17148cb1383cSDoug Ambrisko 			DELAY(10);
17158cb1383cSDoug Ambrisko 		}
17168cb1383cSDoug Ambrisko 	}
17178cb1383cSDoug Ambrisko }
17188cb1383cSDoug Ambrisko 
171950515680SPyun YongHyeon static uint32_t
172050515680SPyun YongHyeon bge_dma_swap_options(struct bge_softc *sc)
172150515680SPyun YongHyeon {
172250515680SPyun YongHyeon 	uint32_t dma_options;
172350515680SPyun YongHyeon 
172450515680SPyun YongHyeon 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
172550515680SPyun YongHyeon 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
172650515680SPyun YongHyeon #if BYTE_ORDER == BIG_ENDIAN
172750515680SPyun YongHyeon 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
172850515680SPyun YongHyeon #endif
172950515680SPyun YongHyeon 	return (dma_options);
173050515680SPyun YongHyeon }
173150515680SPyun YongHyeon 
173295d67482SBill Paul /*
1733c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
173495d67482SBill Paul  */
173595d67482SBill Paul static int
17363f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
173795d67482SBill Paul {
173850515680SPyun YongHyeon 	uint32_t dma_rw_ctl, misc_ctl, mode_ctl;
1739fbc374afSPyun YongHyeon 	uint16_t val;
174095d67482SBill Paul 	int i;
174195d67482SBill Paul 
17428cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
17431108273aSPyun YongHyeon 	misc_ctl = BGE_INIT;
17441108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
17451108273aSPyun YongHyeon 		misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
17461108273aSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
174795d67482SBill Paul 
174895d67482SBill Paul 	/*
174995d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
175095d67482SBill Paul 	 * internal memory.
175195d67482SBill Paul 	 */
175295d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
17533f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
175495d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
175595d67482SBill Paul 
175695d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
17573f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
175895d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
175995d67482SBill Paul 
1760fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1761fbc374afSPyun YongHyeon 		/*
1762d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1763fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1764fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1765fbc374afSPyun YongHyeon 		 */
1766fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1767fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1768fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1769fbc374afSPyun YongHyeon 	}
1770fbc374afSPyun YongHyeon 
1771f8bb33c3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM57765 ||
1772f8bb33c3SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57766) {
1773f8bb33c3SPyun YongHyeon 		/*
1774f8bb33c3SPyun YongHyeon 		 * For the 57766 and non Ax versions of 57765, bootcode
1775f8bb33c3SPyun YongHyeon 		 * needs to setup the PCIE Fast Training Sequence (FTS)
1776f8bb33c3SPyun YongHyeon 		 * value to prevent transmit hangs.
1777f8bb33c3SPyun YongHyeon 		 */
1778f8bb33c3SPyun YongHyeon 		if (sc->bge_chiprev != BGE_CHIPREV_57765_AX) {
1779f8bb33c3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_CPMU_PADRNG_CTL,
1780f8bb33c3SPyun YongHyeon 			    CSR_READ_4(sc, BGE_CPMU_PADRNG_CTL) |
1781f8bb33c3SPyun YongHyeon 			    BGE_CPMU_PADRNG_CTL_RDIV2);
1782f8bb33c3SPyun YongHyeon 		}
1783f8bb33c3SPyun YongHyeon 	}
1784f8bb33c3SPyun YongHyeon 
1785186f842bSJung-uk Kim 	/*
1786186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1787186f842bSJung-uk Kim 	 */
1788186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1789186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1790652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
179148630d79SPyun YongHyeon 		if (sc->bge_mps >= 256)
179248630d79SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
179348630d79SPyun YongHyeon 		else
1794186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1795652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
17964c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1797186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1798186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1799186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1800186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1801186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1802186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1803cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1804cbb2b2feSPyun YongHyeon 			/*
1805cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1806cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1807cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1808cbb2b2feSPyun YongHyeon 			 * register.
1809cbb2b2feSPyun YongHyeon 			 */
1810cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1811cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1812186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1813186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1814186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1815186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1816186f842bSJung-uk Kim 		} else {
1817186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1818186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1819186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
18200c8aa4eaSJung-uk Kim 			    0x0F;
1821186f842bSJung-uk Kim 		}
1822e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1823e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
18243f74909aSGleb Smirnoff 			uint32_t tmp;
18255cba12d3SPaul Saab 
1826186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
18270c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1828186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1829186f842bSJung-uk Kim 				dma_rw_ctl |=
1830186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
18315cba12d3SPaul Saab 
1832186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1833186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1834186f842bSJung-uk Kim 		}
1835186f842bSJung-uk Kim 	} else {
1836186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1837186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1838186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1839186f842bSJung-uk Kim 
1840186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1841186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1842186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1843186f842bSJung-uk Kim 	}
1844186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1845186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1846186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1847186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1848e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1849186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
18505cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1851b4a256acSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
18521108273aSPyun YongHyeon 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1853b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
1854b4a256acSPyun YongHyeon 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1855bbe2ca75SPyun YongHyeon 		/*
1856bbe2ca75SPyun YongHyeon 		 * Enable HW workaround for controllers that misinterpret
1857bbe2ca75SPyun YongHyeon 		 * a status tag update and leave interrupts permanently
1858bbe2ca75SPyun YongHyeon 		 * disabled.
1859bbe2ca75SPyun YongHyeon 		 */
18602927f01fSPyun YongHyeon 		if (!BGE_IS_57765_PLUS(sc) &&
18612927f01fSPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
18622927f01fSPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM5762)
1863bbe2ca75SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1864b4a256acSPyun YongHyeon 	}
18655cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
186695d67482SBill Paul 
186795d67482SBill Paul 	/*
186895d67482SBill Paul 	 * Set up general mode register.
186995d67482SBill Paul 	 */
1870548c8f1aSPyun YongHyeon 	mode_ctl = bge_dma_swap_options(sc);
18712927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
18722927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
1873548c8f1aSPyun YongHyeon 		/* Retain Host-2-BMC settings written by APE firmware. */
1874548c8f1aSPyun YongHyeon 		mode_ctl |= CSR_READ_4(sc, BGE_MODE_CTL) &
1875548c8f1aSPyun YongHyeon 		    (BGE_MODECTL_BYTESWAP_B2HRX_DATA |
1876548c8f1aSPyun YongHyeon 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA |
1877548c8f1aSPyun YongHyeon 		    BGE_MODECTL_B2HRX_ENABLE | BGE_MODECTL_HTX2B_ENABLE);
1878548c8f1aSPyun YongHyeon 	}
1879548c8f1aSPyun YongHyeon 	mode_ctl |= BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1880548c8f1aSPyun YongHyeon 	    BGE_MODECTL_TX_NO_PHDR_CSUM;
188195d67482SBill Paul 
188295d67482SBill Paul 	/*
188390447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
188490447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
188590447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
188690447aadSMarius Strobl 	 * certain bridges.
188790447aadSMarius Strobl 	 */
188890447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
188990447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
189050515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_FORCE_PCI32;
189190447aadSMarius Strobl 
189290447aadSMarius Strobl 	/*
18938cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
18948cb1383cSDoug Ambrisko 	 */
18958cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
189650515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_STACKUP;
189750515680SPyun YongHyeon 
189850515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
18998cb1383cSDoug Ambrisko 
19008cb1383cSDoug Ambrisko 	/*
1901ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
190240438c47SMarius Strobl 	 * properly by these devices.
190395d67482SBill Paul 	 */
190440438c47SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
190595d67482SBill Paul 
1906d7acafa1SMarius Strobl 	/* Set the timer prescaler (always 66 MHz). */
19070c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
190895d67482SBill Paul 
190938cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
191038cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
191138cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
191238cc658fSJohn Baldwin 
191338cc658fSJohn Baldwin 		/* Put PHY into ready state */
191438cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
191538cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
191638cc658fSJohn Baldwin 		DELAY(40);
191738cc658fSJohn Baldwin 	}
191838cc658fSJohn Baldwin 
191995d67482SBill Paul 	return (0);
192095d67482SBill Paul }
192195d67482SBill Paul 
192295d67482SBill Paul static int
19233f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
192495d67482SBill Paul {
192595d67482SBill Paul 	struct bge_rcb *rcb;
1926e907febfSPyun YongHyeon 	bus_size_t vrcb;
1927e907febfSPyun YongHyeon 	bge_hostaddr taddr;
19282927f01fSPyun YongHyeon 	uint32_t dmactl, rdmareg, val;
19298a315a6dSPyun YongHyeon 	int i, limit;
193095d67482SBill Paul 
193195d67482SBill Paul 	/*
193295d67482SBill Paul 	 * Initialize the memory window pointer register so that
193395d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
193495d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
193595d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
193695d67482SBill Paul 	 */
193795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
193895d67482SBill Paul 
1939822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1940822f63fcSBill Paul 
19417ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
194295d67482SBill Paul 		/* Configure mbuf memory pool */
19430dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1944822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1945822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1946822f63fcSBill Paul 		else
194795d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
194895d67482SBill Paul 
194995d67482SBill Paul 		/* Configure DMA resource pool */
19500434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
19510434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
195295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
19530434d1b8SBill Paul 	}
195495d67482SBill Paul 
195595d67482SBill Paul 	/* Configure mbuf pool watermarks */
195650515680SPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
19571108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1958fba8b109SMarcel Moolenaar 		if (if_getmtu(sc->bge_ifp) > ETHERMTU) {
19591108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
19601108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
19611108273aSPyun YongHyeon 		} else {
19621108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
19631108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
19641108273aSPyun YongHyeon 		}
19651108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc)) {
1966fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1967fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1968fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
196938cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
197038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
197138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
197238cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
197338cc658fSJohn Baldwin 	} else {
197438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
197538cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
197638cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
197738cc658fSJohn Baldwin 	}
197895d67482SBill Paul 
197995d67482SBill Paul 	/* Configure DMA resource watermarks */
198095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
198195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
198295d67482SBill Paul 
198395d67482SBill Paul 	/* Enable buffer manager */
1984bbe2ca75SPyun YongHyeon 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
1985bbe2ca75SPyun YongHyeon 	/*
1986bbe2ca75SPyun YongHyeon 	 * Change the arbitration algorithm of TXMBUF read request to
1987bbe2ca75SPyun YongHyeon 	 * round-robin instead of priority based for BCM5719.  When
1988bbe2ca75SPyun YongHyeon 	 * TXFIFO is almost empty, RDMA will hold its request until
1989bbe2ca75SPyun YongHyeon 	 * TXFIFO is not almost empty.
1990bbe2ca75SPyun YongHyeon 	 */
1991bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
1992bbe2ca75SPyun YongHyeon 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
1993bbe2ca75SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
199495d67482SBill Paul 
199595d67482SBill Paul 	/* Poll for buffer manager start indication */
199695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1997d5d23857SJung-uk Kim 		DELAY(10);
19980c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
199995d67482SBill Paul 			break;
200095d67482SBill Paul 	}
200195d67482SBill Paul 
200295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
20035a147ba6SPyun YongHyeon 		device_printf(sc->bge_dev, "buffer manager failed to start\n");
200495d67482SBill Paul 		return (ENXIO);
200595d67482SBill Paul 	}
200695d67482SBill Paul 
200795d67482SBill Paul 	/* Enable flow-through queues */
20080c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
200995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
201095d67482SBill Paul 
201195d67482SBill Paul 	/* Wait until queue initialization is complete */
201295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2013d5d23857SJung-uk Kim 		DELAY(10);
201495d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
201595d67482SBill Paul 			break;
201695d67482SBill Paul 	}
201795d67482SBill Paul 
201895d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2019fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
202095d67482SBill Paul 		return (ENXIO);
202195d67482SBill Paul 	}
202295d67482SBill Paul 
20238a315a6dSPyun YongHyeon 	/*
20248a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
20258a315a6dSPyun YongHyeon 	 *
20268a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
20278a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
20288a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
20298a315a6dSPyun YongHyeon 	 *
20308a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
20318a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
20328a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
20338a315a6dSPyun YongHyeon 	 *   to the controller.
20348a315a6dSPyun YongHyeon 	 *
20358a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
20368a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
20378a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
20388a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
20398a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
20408a315a6dSPyun YongHyeon 	 *   be disabled.
20418a315a6dSPyun YongHyeon 	 *
20428a315a6dSPyun YongHyeon 	 * Receive Return Ring
20438a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
20448a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
20458a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
20468a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
20478a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
20488a315a6dSPyun YongHyeon 	 *
20498a315a6dSPyun YongHyeon 	 * Send Ring
20508a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
20518a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
20528a315a6dSPyun YongHyeon 	 */
20538a315a6dSPyun YongHyeon 
20548a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
2055f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
2056f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
2057f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
2058f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
2059f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
2060f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2061f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
20621108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
20631108273aSPyun YongHyeon 		/*
20641108273aSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
20651108273aSPyun YongHyeon 		 * Bits 15-2 : Maximum RX frame size
20661108273aSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
20671108273aSPyun YongHyeon 		 * Bit 0     : Reserved
20681108273aSPyun YongHyeon 		 */
20691108273aSPyun YongHyeon 		rcb->bge_maxlen_flags =
20701108273aSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
20711108273aSPyun YongHyeon 	} else if (BGE_IS_5705_PLUS(sc)) {
20728a315a6dSPyun YongHyeon 		/*
20738a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
20748a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
20758a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
20768a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
20778a315a6dSPyun YongHyeon 		 */
20780434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
20798a315a6dSPyun YongHyeon 	} else {
20808a315a6dSPyun YongHyeon 		/*
20818a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
20828a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
20838a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
20848a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
20858a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
20868a315a6dSPyun YongHyeon 		 */
20870434d1b8SBill Paul 		rcb->bge_maxlen_flags =
20880434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
20898a315a6dSPyun YongHyeon 	}
2090bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
209150515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
209250515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
20931108273aSPyun YongHyeon 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
20941108273aSPyun YongHyeon 	else
209595d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
20968a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
20970c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
20980c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
209967111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
210067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
210195d67482SBill Paul 
21028a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
21038a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
21048a315a6dSPyun YongHyeon 
210595d67482SBill Paul 	/*
21068a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
21078a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
21088a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
210995d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
211095d67482SBill Paul 	 * high enough to require it).
211195d67482SBill Paul 	 */
21124c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2113f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
21148a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
2115f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
2116f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2117f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
2118f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2119f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2120f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2121f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
21221be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
21231be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
2124bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
212550515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
212650515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21271108273aSPyun YongHyeon 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
21281108273aSPyun YongHyeon 		else
212995d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
213067111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
213167111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
213267111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
213367111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
21348a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
21350434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
21360434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
213767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
21388a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
21398a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
21408a315a6dSPyun YongHyeon 	}
214195d67482SBill Paul 
21428a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
21435e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
2144f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
214567111612SJohn Polstra 		rcb->bge_maxlen_flags =
214667111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
21470434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
21480434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
21498a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
21508a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
21510434d1b8SBill Paul 	}
215295d67482SBill Paul 
2153ca4f8986SPyun YongHyeon 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
2154ca4f8986SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2155427d3f33SPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
2156427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
2157427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
21588d5f7181SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
21598d5f7181SPyun YongHyeon 			    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
2160ca4f8986SPyun YongHyeon 	}
216195d67482SBill Paul 	/*
21628a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
21638a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
21648a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
21658a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
21668a315a6dSPyun YongHyeon 	 *
216795d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
216895d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
216995d67482SBill Paul 	 * each ring.
21709ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
21719ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
21729ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
21739ba784dbSScott Long 	 * are reports that it might not need to be so strict.
217438cc658fSJohn Baldwin 	 *
217538cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
217638cc658fSJohn Baldwin 	 * well.
217795d67482SBill Paul 	 */
21785345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
21796f8718a3SScott Long 		val = 8;
21806f8718a3SScott Long 	else
21816f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
21826f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
21832a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
21842a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
21852a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
21861108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
21871108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
21881108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
21891108273aSPyun YongHyeon 	}
219095d67482SBill Paul 
219195d67482SBill Paul 	/*
21928a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
21938a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
21948a315a6dSPyun YongHyeon 	 * located in NIC memory.
219595d67482SBill Paul 	 */
21968a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
21978a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
21988a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
21992927f01fSPyun YongHyeon 	else if (BGE_IS_57765_PLUS(sc) ||
22002927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762)
22012927f01fSPyun YongHyeon 		limit = 2;
22022927f01fSPyun YongHyeon 	else if (BGE_IS_5717_PLUS(sc))
22032927f01fSPyun YongHyeon 		limit = 4;
22048a315a6dSPyun YongHyeon 	else
22058a315a6dSPyun YongHyeon 		limit = 1;
2206e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
22078a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2208e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2209e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
2210e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2211e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
221295d67482SBill Paul 	}
221395d67482SBill Paul 
22148a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
2215e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
2216e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
2217e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2218e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2219bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
222050515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
222150515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
22221108273aSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
22231108273aSPyun YongHyeon 	else
2224e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
2225e907febfSPyun YongHyeon 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
2226e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2227e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
222895d67482SBill Paul 
22298a315a6dSPyun YongHyeon 	/*
22308a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
22318a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
22328a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
22338a315a6dSPyun YongHyeon 	 */
2234bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
223550515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
223650515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
22371108273aSPyun YongHyeon 		/* Should be 17, use 16 until we get an SRAM map. */
22381108273aSPyun YongHyeon 		limit = 16;
22391108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc))
22408a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
2241b4a256acSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
22422927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762 ||
2243fe26ad88SPyun YongHyeon 	    BGE_IS_57765_PLUS(sc))
22448a315a6dSPyun YongHyeon 		limit = 4;
22458a315a6dSPyun YongHyeon 	else
22468a315a6dSPyun YongHyeon 		limit = 1;
22478a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
2248e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
22498a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2250e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
2251e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
2252e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
22538a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
2254e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
225538cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
22563f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
2257e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
225895d67482SBill Paul 	}
225995d67482SBill Paul 
226095d67482SBill Paul 	/*
22618a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
22628a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
22638a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
226495d67482SBill Paul 	 */
2265e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2266e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
2267e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2268e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
22698a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2270e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2271e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
227295d67482SBill Paul 
227395d67482SBill Paul 	/* Set random backoff seed for TX */
227495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
22750a2cc827SPyun YongHyeon 	    (IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
22764a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
22770a2cc827SPyun YongHyeon 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5]) &
227895d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
227995d67482SBill Paul 
228095d67482SBill Paul 	/* Set inter-packet gap */
228150515680SPyun YongHyeon 	val = 0x2620;
22822927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
22832927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762)
228450515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
228550515680SPyun YongHyeon 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
228650515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
228795d67482SBill Paul 
228895d67482SBill Paul 	/*
228995d67482SBill Paul 	 * Specify which ring to use for packets that don't match
229095d67482SBill Paul 	 * any RX rules.
229195d67482SBill Paul 	 */
229295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
229395d67482SBill Paul 
229495d67482SBill Paul 	/*
229595d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
229695d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
229795d67482SBill Paul 	 */
229895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
229995d67482SBill Paul 
230095d67482SBill Paul 	/* Inialize RX list placement stats mask. */
23010c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
230295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
230395d67482SBill Paul 
230495d67482SBill Paul 	/* Disable host coalescing until we get it set up */
230595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
230695d67482SBill Paul 
230795d67482SBill Paul 	/* Poll to make sure it's shut down. */
230895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2309d5d23857SJung-uk Kim 		DELAY(10);
231095d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
231195d67482SBill Paul 			break;
231295d67482SBill Paul 	}
231395d67482SBill Paul 
231495d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2315fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2316fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
231795d67482SBill Paul 		return (ENXIO);
231895d67482SBill Paul 	}
231995d67482SBill Paul 
232095d67482SBill Paul 	/* Set up host coalescing defaults */
232195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
232295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
232395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
232495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
23257ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
232695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
232795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
23280434d1b8SBill Paul 	}
2329b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
2330b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
233195d67482SBill Paul 
233295d67482SBill Paul 	/* Set up address of statistics block */
23337ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
2334f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
2335f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
233695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
2337f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
23380434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
233995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
23400434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
23410434d1b8SBill Paul 	}
23420434d1b8SBill Paul 
23430434d1b8SBill Paul 	/* Set up address of status block */
2344f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
2345f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
234695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
2347f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
234895d67482SBill Paul 
234930f57f61SPyun YongHyeon 	/* Set up status block size. */
235030f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2351864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
235230f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
2353864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2354864104feSPyun YongHyeon 	} else {
235530f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
2356864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
2357864104feSPyun YongHyeon 	}
2358864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2359864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
2360864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
236130f57f61SPyun YongHyeon 
236295d67482SBill Paul 	/* Turn on host coalescing state machine */
236330f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
236495d67482SBill Paul 
236595d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
236695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
236795d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
236895d67482SBill Paul 
236995d67482SBill Paul 	/* Turn on RX list placement state machine */
237095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
237195d67482SBill Paul 
237295d67482SBill Paul 	/* Turn on RX list selector state machine. */
23737ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
237495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
237595d67482SBill Paul 
23762246e8c6SPyun YongHyeon 	/* Turn on DMA, clear stats. */
2377ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2378ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2379ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2380ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
2381ea3b4127SPyun YongHyeon 
2382ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
2383ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
2384ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
2385ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
2386ea3b4127SPyun YongHyeon 	else
2387ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
2388ea3b4127SPyun YongHyeon 
2389548c8f1aSPyun YongHyeon 	/* Allow APE to send/receive frames. */
2390548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
2391548c8f1aSPyun YongHyeon 		val |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
2392548c8f1aSPyun YongHyeon 
2393ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
23949b80ffe7SPyun YongHyeon 	DELAY(40);
239595d67482SBill Paul 
239695d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
239791bd90d8SPyun YongHyeon 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
239895d67482SBill Paul 
239995d67482SBill Paul #ifdef notdef
240095d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
240195d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
240295d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
240395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
240495d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
240595d67482SBill Paul #endif
240695d67482SBill Paul 
240795d67482SBill Paul 	/* Turn on DMA completion state machine */
24087ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
240995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
241095d67482SBill Paul 
24116f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
24126f8718a3SScott Long 
24136f8718a3SScott Long 	/* Enable host coalescing bug fix. */
2414a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
24153889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
24166f8718a3SScott Long 
24177aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
24187aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
24197aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
24207aa4b937SPyun YongHyeon 
242195d67482SBill Paul 	/* Turn on write DMA state machine */
24226f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
24234f09c4c7SMarius Strobl 	DELAY(40);
242495d67482SBill Paul 
242595d67482SBill Paul 	/* Turn on read DMA state machine */
24264f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
24271108273aSPyun YongHyeon 
24281108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
24291108273aSPyun YongHyeon 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
24301108273aSPyun YongHyeon 
2431a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2432a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2433a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2434a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2435a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2436a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
24374f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
24384f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
24391108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2440ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
24411108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_TSO3 ||
24421108273aSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
244355a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
244455a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
244555a24a05SPyun YongHyeon 	}
244650515680SPyun YongHyeon 
24472927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
24482927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
244950515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
245050515680SPyun YongHyeon 			BGE_RDMAMODE_H2BNC_VLAN_DET;
2451e3215f76SPyun YongHyeon 		/*
2452e3215f76SPyun YongHyeon 		 * Allow multiple outstanding read requests from
2453e3215f76SPyun YongHyeon 		 * non-LSO read DMA engine.
2454e3215f76SPyun YongHyeon 		 */
2455e3215f76SPyun YongHyeon 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
2456e3215f76SPyun YongHyeon 	}
245750515680SPyun YongHyeon 
2458d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2459d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2460d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
24611108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
24622927f01fSPyun YongHyeon 	    BGE_IS_5717_PLUS(sc) || BGE_IS_57765_PLUS(sc)) {
24632927f01fSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
24642927f01fSPyun YongHyeon 			rdmareg = BGE_RDMA_RSRVCTRL_REG2;
24652927f01fSPyun YongHyeon 		else
24662927f01fSPyun YongHyeon 			rdmareg = BGE_RDMA_RSRVCTRL;
24672927f01fSPyun YongHyeon 		dmactl = CSR_READ_4(sc, rdmareg);
2468bbe2ca75SPyun YongHyeon 		/*
2469bbe2ca75SPyun YongHyeon 		 * Adjust tx margin to prevent TX data corruption and
2470bbe2ca75SPyun YongHyeon 		 * fix internal FIFO overflow.
2471bbe2ca75SPyun YongHyeon 		 */
24722927f01fSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5719_A0 ||
24732927f01fSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
2474bbe2ca75SPyun YongHyeon 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
2475bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
2476bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
2477bbe2ca75SPyun YongHyeon 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
2478bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
2479bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
2480bbe2ca75SPyun YongHyeon 		}
2481d255f2a9SPyun YongHyeon 		/*
2482d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
2483d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
2484d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
2485d255f2a9SPyun YongHyeon 		 */
24862927f01fSPyun YongHyeon 		CSR_WRITE_4(sc, rdmareg, dmactl |
2487d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2488d255f2a9SPyun YongHyeon 	}
2489bbe2ca75SPyun YongHyeon 
2490e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719) {
2491bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2492bbe2ca75SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2493bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2494bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2495e3215f76SPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2496e3215f76SPyun YongHyeon 		/*
2497e3215f76SPyun YongHyeon 		 * Allow 4KB burst length reads for non-LSO frames.
2498e3215f76SPyun YongHyeon 		 * Enable 512B burst length reads for buffer descriptors.
2499e3215f76SPyun YongHyeon 		 */
2500e3215f76SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2501e3215f76SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2502e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
2503e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
25042927f01fSPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5762) {
25052927f01fSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2,
25062927f01fSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2) |
25072927f01fSPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
25082927f01fSPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2509bbe2ca75SPyun YongHyeon 	}
2510bbe2ca75SPyun YongHyeon 
25114f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
25124f09c4c7SMarius Strobl 	DELAY(40);
251395d67482SBill Paul 
251429b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
251529b44b09SPyun YongHyeon 		for (i = 0; i < BGE_NUM_RDMA_CHANNELS / 2; i++) {
251629b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LENGTH + i * 4);
251729b44b09SPyun YongHyeon 			if ((val & 0xFFFF) > BGE_FRAMELEN)
251829b44b09SPyun YongHyeon 				break;
251929b44b09SPyun YongHyeon 			if (((val >> 16) & 0xFFFF) > BGE_FRAMELEN)
252029b44b09SPyun YongHyeon 				break;
252129b44b09SPyun YongHyeon 		}
252229b44b09SPyun YongHyeon 		if (i != BGE_NUM_RDMA_CHANNELS / 2) {
252329b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
252429b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
252529b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5719;
252629b44b09SPyun YongHyeon 			else
252729b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5720;
252829b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
252929b44b09SPyun YongHyeon 		}
253029b44b09SPyun YongHyeon 	}
253129b44b09SPyun YongHyeon 
253295d67482SBill Paul 	/* Turn on RX data completion state machine */
253395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
253495d67482SBill Paul 
253595d67482SBill Paul 	/* Turn on RX BD initiator state machine */
253695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
253795d67482SBill Paul 
253895d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
253995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
254095d67482SBill Paul 
254195d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
25427ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
254395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
254495d67482SBill Paul 
254595d67482SBill Paul 	/* Turn on send BD completion state machine */
254695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
254795d67482SBill Paul 
254895d67482SBill Paul 	/* Turn on send data completion state machine */
2549a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2550a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2551a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2552a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
255395d67482SBill Paul 
255495d67482SBill Paul 	/* Turn on send data initiator state machine */
25551108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
25561108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
25571108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2558ca3f1187SPyun YongHyeon 	else
255995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
256095d67482SBill Paul 
256195d67482SBill Paul 	/* Turn on send BD initiator state machine */
256295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
256395d67482SBill Paul 
256495d67482SBill Paul 	/* Turn on send BD selector state machine */
256595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
256695d67482SBill Paul 
25670c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
256895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
256995d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
257095d67482SBill Paul 
257195d67482SBill Paul 	/* ack/clear link change events */
257295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
25730434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
25740434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2575f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
257695d67482SBill Paul 
25776ede2cfaSPyun YongHyeon 	/*
25786ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
25796ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
25806ede2cfaSPyun YongHyeon 	 */
2581652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
258295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2583a1d52896SBill Paul 	} else {
25847ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
25857ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
25867ed3f0f0SPyun YongHyeon 			DELAY(80);
25877ed3f0f0SPyun YongHyeon 		}
25881f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
25894c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2590a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2591a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2592a1d52896SBill Paul 	}
259395d67482SBill Paul 
25941f313773SOleg Bulyzhin 	/*
25951f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
25961f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
25971f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
25981f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
25991f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
26001f313773SOleg Bulyzhin 	 */
26011f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
26021f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
26031f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
26041f313773SOleg Bulyzhin 
260595d67482SBill Paul 	/* Enable link state change attentions. */
260695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
260795d67482SBill Paul 
260895d67482SBill Paul 	return (0);
260995d67482SBill Paul }
261095d67482SBill Paul 
2611d7acafa1SMarius Strobl static const struct bge_revision *
26124c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
26134c0da0ffSGleb Smirnoff {
26144c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
26154c0da0ffSGleb Smirnoff 
26164c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
26174c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
26184c0da0ffSGleb Smirnoff 			return (br);
26194c0da0ffSGleb Smirnoff 	}
26204c0da0ffSGleb Smirnoff 
26214c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
26224c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
26234c0da0ffSGleb Smirnoff 			return (br);
26244c0da0ffSGleb Smirnoff 	}
26254c0da0ffSGleb Smirnoff 
26264c0da0ffSGleb Smirnoff 	return (NULL);
26274c0da0ffSGleb Smirnoff }
26284c0da0ffSGleb Smirnoff 
2629d7acafa1SMarius Strobl static const struct bge_vendor *
26304c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
26314c0da0ffSGleb Smirnoff {
26324c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
26334c0da0ffSGleb Smirnoff 
26344c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
26354c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
26364c0da0ffSGleb Smirnoff 			return (v);
26374c0da0ffSGleb Smirnoff 
26384c0da0ffSGleb Smirnoff 	return (NULL);
26394c0da0ffSGleb Smirnoff }
26404c0da0ffSGleb Smirnoff 
2641d7acafa1SMarius Strobl static uint32_t
2642d7acafa1SMarius Strobl bge_chipid(device_t dev)
264395d67482SBill Paul {
2644978f2704SMarius Strobl 	uint32_t id;
264595d67482SBill Paul 
2646a5779553SStanislav Sedov 	id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2647a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
26481108273aSPyun YongHyeon 	if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
26491108273aSPyun YongHyeon 		/*
2650d7acafa1SMarius Strobl 		 * Find the ASCI revision.  Different chips use different
2651d7acafa1SMarius Strobl 		 * registers.
26521108273aSPyun YongHyeon 		 */
26531108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
2654cb2404b4SSepherosa Ziehau 		case BCOM_DEVICEID_BCM5717C:
26551ca32af8SSepherosa Ziehau 			/* 5717 C0 seems to belong to 5720 line. */
26561ca32af8SSepherosa Ziehau 			id = BGE_CHIPID_BCM5720_A0;
26571ca32af8SSepherosa Ziehau 			break;
26581ca32af8SSepherosa Ziehau 		case BCOM_DEVICEID_BCM5717:
26591108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
2660bbe2ca75SPyun YongHyeon 		case BCOM_DEVICEID_BCM5719:
266150515680SPyun YongHyeon 		case BCOM_DEVICEID_BCM5720:
26622927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5725:
26632927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5727:
26642927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5762:
266567129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57764:
266667129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57767:
266767129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57787:
26681108273aSPyun YongHyeon 			id = pci_read_config(dev,
26691108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
26701108273aSPyun YongHyeon 			break;
2671b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57761:
2672fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57762:
2673b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57765:
2674fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57766:
2675b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57781:
267667129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57782:
2677b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57785:
267867129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57786:
2679b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57791:
2680b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57795:
2681b4a256acSPyun YongHyeon 			id = pci_read_config(dev,
2682b4a256acSPyun YongHyeon 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2683b4a256acSPyun YongHyeon 			break;
26841108273aSPyun YongHyeon 		default:
2685d7acafa1SMarius Strobl 			id = pci_read_config(dev, BGE_PCI_PRODID_ASICREV, 4);
26861108273aSPyun YongHyeon 		}
26871108273aSPyun YongHyeon 	}
2688d7acafa1SMarius Strobl 	return (id);
2689d7acafa1SMarius Strobl }
2690d7acafa1SMarius Strobl 
2691d7acafa1SMarius Strobl /*
2692d7acafa1SMarius Strobl  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
2693d7acafa1SMarius Strobl  * against our list and return its name if we find a match.
2694d7acafa1SMarius Strobl  *
2695d7acafa1SMarius Strobl  * Note that since the Broadcom controller contains VPD support, we
2696d7acafa1SMarius Strobl  * try to get the device name string from the controller itself instead
2697d7acafa1SMarius Strobl  * of the compiled-in string. It guarantees we'll always announce the
2698d7acafa1SMarius Strobl  * right product name. We fall back to the compiled-in string when
2699d7acafa1SMarius Strobl  * VPD is unavailable or corrupt.
2700d7acafa1SMarius Strobl  */
2701d7acafa1SMarius Strobl static int
2702d7acafa1SMarius Strobl bge_probe(device_t dev)
2703d7acafa1SMarius Strobl {
2704d7acafa1SMarius Strobl 	char buf[96];
2705d7acafa1SMarius Strobl 	char model[64];
2706d7acafa1SMarius Strobl 	const struct bge_revision *br;
2707d7acafa1SMarius Strobl 	const char *pname;
2708d7acafa1SMarius Strobl 	struct bge_softc *sc;
2709d7acafa1SMarius Strobl 	const struct bge_type *t = bge_devs;
2710d7acafa1SMarius Strobl 	const struct bge_vendor *v;
2711d7acafa1SMarius Strobl 	uint32_t id;
2712d7acafa1SMarius Strobl 	uint16_t did, vid;
2713d7acafa1SMarius Strobl 
2714d7acafa1SMarius Strobl 	sc = device_get_softc(dev);
2715d7acafa1SMarius Strobl 	sc->bge_dev = dev;
2716d7acafa1SMarius Strobl 	vid = pci_get_vendor(dev);
2717d7acafa1SMarius Strobl 	did = pci_get_device(dev);
2718d7acafa1SMarius Strobl 	while(t->bge_vid != 0) {
2719d7acafa1SMarius Strobl 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2720d7acafa1SMarius Strobl 			id = bge_chipid(dev);
27214c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
2722852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2723852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
2724d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s", pname);
2725d7acafa1SMarius Strobl 			else {
2726d7acafa1SMarius Strobl 				v = bge_lookup_vendor(vid);
2727d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s %s",
2728d7acafa1SMarius Strobl 				    v != NULL ? v->v_name : "Unknown",
27297c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
27302ad1b396SMarius Strobl 				    "NetXtreme/NetLink Ethernet Controller");
2731d7acafa1SMarius Strobl 			}
2732d7acafa1SMarius Strobl 			snprintf(buf, sizeof(buf), "%s, %sASIC rev. %#08x",
2733d7acafa1SMarius Strobl 			    model, br != NULL ? "" : "unknown ", id);
27344c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
2735d7acafa1SMarius Strobl 			return (BUS_PROBE_DEFAULT);
273695d67482SBill Paul 		}
273795d67482SBill Paul 		t++;
273895d67482SBill Paul 	}
273995d67482SBill Paul 
274095d67482SBill Paul 	return (ENXIO);
274195d67482SBill Paul }
274295d67482SBill Paul 
2743f41ac2beSBill Paul static void
27443f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2745f41ac2beSBill Paul {
2746f41ac2beSBill Paul 	int i;
2747f41ac2beSBill Paul 
27483f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2749f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2750f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
27510ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2752f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2753f41ac2beSBill Paul 	}
2754943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2755943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2756943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2757f41ac2beSBill Paul 
27583f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2759f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2760f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2761f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2762f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2763f41ac2beSBill Paul 	}
2764943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2765943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2766943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2767f41ac2beSBill Paul 
27683f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2769f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2770f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
27710ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2772f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2773f41ac2beSBill Paul 	}
2774f41ac2beSBill Paul 
27750ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
27760ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2777c0220d81SPyun YongHyeon 	if (sc->bge_cdata.bge_mtag_jumbo)
2778c0220d81SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
27790ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
27800ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2781f41ac2beSBill Paul 
27823f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2783068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_std_ring_paddr)
2784e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2785e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2786068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_std_ring)
2787f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2788f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2789f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2790f41ac2beSBill Paul 
2791f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2792f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2793f41ac2beSBill Paul 
27943f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2795068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_jumbo_ring_paddr)
2796e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2797e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2798e65bed95SPyun YongHyeon 
2799068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_jumbo_ring)
2800f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2801f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2802f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2803f41ac2beSBill Paul 
2804f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2805f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2806f41ac2beSBill Paul 
28073f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2808068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_return_ring_paddr)
2809e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2810e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2811e65bed95SPyun YongHyeon 
2812068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_return_ring)
2813f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2814f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2815f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2816f41ac2beSBill Paul 
2817f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2818f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2819f41ac2beSBill Paul 
28203f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2821068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_tx_ring_paddr)
2822e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2823e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2824e65bed95SPyun YongHyeon 
2825068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_tx_ring)
2826f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2827f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2828f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2829f41ac2beSBill Paul 
2830f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2831f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2832f41ac2beSBill Paul 
28333f74909aSGleb Smirnoff 	/* Destroy status block. */
2834068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_status_block_paddr)
2835e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2836e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2837e65bed95SPyun YongHyeon 
2838068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_status_block)
2839f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2840f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2841f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2842f41ac2beSBill Paul 
2843f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2844f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2845f41ac2beSBill Paul 
28463f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2847068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_stats_paddr)
2848e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2849e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2850e65bed95SPyun YongHyeon 
2851068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_stats)
2852f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2853f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2854f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2855f41ac2beSBill Paul 
2856f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2857f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2858f41ac2beSBill Paul 
28595b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
28605b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
28615b610048SPyun YongHyeon 
28623f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2863f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2864f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2865f41ac2beSBill Paul }
2866f41ac2beSBill Paul 
2867f41ac2beSBill Paul static int
28685b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
28695b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
28705b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2871f41ac2beSBill Paul {
28723f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
2873e86fa024STycho Nightingale 	bus_addr_t lowaddr;
2874e86fa024STycho Nightingale 	bus_size_t ring_end;
28755b610048SPyun YongHyeon 	int error;
2876f41ac2beSBill Paul 
2877e86fa024STycho Nightingale 	lowaddr = BUS_SPACE_MAXADDR;
2878e86fa024STycho Nightingale again:
28795b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2880e86fa024STycho Nightingale 	    alignment, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
28815b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
28825b610048SPyun YongHyeon 	if (error != 0) {
28835b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28845b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
28855b610048SPyun YongHyeon 		return (ENOMEM);
28865b610048SPyun YongHyeon 	}
28875b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
28885b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
28895b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
28905b610048SPyun YongHyeon 	if (error != 0) {
28915b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
28925b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
28935b610048SPyun YongHyeon 		return (ENOMEM);
28945b610048SPyun YongHyeon 	}
28955b610048SPyun YongHyeon 	/* Load the address of the ring. */
28965b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
28975b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
28985b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
28995b610048SPyun YongHyeon 	if (error != 0) {
29005b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29015b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
29025b610048SPyun YongHyeon 		return (ENOMEM);
29035b610048SPyun YongHyeon 	}
29045b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
2905e86fa024STycho Nightingale 	ring_end = *paddr + maxsize;
2906e86fa024STycho Nightingale 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0 &&
2907e86fa024STycho Nightingale 	    BGE_ADDR_HI(*paddr) != BGE_ADDR_HI(ring_end)) {
2908e86fa024STycho Nightingale 		/*
2909e86fa024STycho Nightingale 		 * 4GB boundary crossed.  Limit maximum allowable DMA
2910e86fa024STycho Nightingale 		 * address space to 32bit and try again.
2911e86fa024STycho Nightingale 		 */
2912e86fa024STycho Nightingale 		bus_dmamap_unload(*tag, *map);
2913e86fa024STycho Nightingale 		bus_dmamem_free(*tag, *ring, *map);
2914e86fa024STycho Nightingale 		bus_dma_tag_destroy(*tag);
2915e86fa024STycho Nightingale 		if (bootverbose)
2916e86fa024STycho Nightingale 			device_printf(sc->bge_dev, "4GB boundary crossed, "
2917e86fa024STycho Nightingale 			    "limit DMA address space to 32bit for %s\n", msg);
2918e86fa024STycho Nightingale 		*ring = NULL;
2919e86fa024STycho Nightingale 		*tag = NULL;
2920e86fa024STycho Nightingale 		*map = NULL;
2921e86fa024STycho Nightingale 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
2922e86fa024STycho Nightingale 		goto again;
2923e86fa024STycho Nightingale 	}
29245b610048SPyun YongHyeon 	return (0);
29255b610048SPyun YongHyeon }
29265b610048SPyun YongHyeon 
29275b610048SPyun YongHyeon static int
29285b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
29295b610048SPyun YongHyeon {
29305b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2931e86fa024STycho Nightingale 	bus_size_t boundary, sbsz, rxmaxsegsz, txsegsz, txmaxsegsz;
29325b610048SPyun YongHyeon 	int i, error;
2933f41ac2beSBill Paul 
2934f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2935f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2936f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2937f41ac2beSBill Paul 	/*
2938f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2939f41ac2beSBill Paul 	 */
29404eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2941f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
29424eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
29434eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2944e65bed95SPyun YongHyeon 	if (error != 0) {
2945fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2946fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2947e65bed95SPyun YongHyeon 		return (ENOMEM);
2948e65bed95SPyun YongHyeon 	}
2949e65bed95SPyun YongHyeon 
29505b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
29515b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
29525b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
29535b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
29545b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
29555b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
29565b610048SPyun YongHyeon 	if (error)
29575b610048SPyun YongHyeon 		return (error);
29585b610048SPyun YongHyeon 
29595b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
29605b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
29615b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
29625b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
29635b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
29645b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
29655b610048SPyun YongHyeon 	if (error)
29665b610048SPyun YongHyeon 		return (error);
29675b610048SPyun YongHyeon 
29685b610048SPyun YongHyeon 	/* Create tag for TX ring. */
29695b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
29705b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
29715b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
29725b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
29735b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
29745b610048SPyun YongHyeon 	if (error)
29755b610048SPyun YongHyeon 		return (error);
29765b610048SPyun YongHyeon 
2977f41ac2beSBill Paul 	/*
29785b610048SPyun YongHyeon 	 * Create tag for status block.
29795b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
29805b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
29815b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
29825b610048SPyun YongHyeon 	 * of configured number of ring.
2983f41ac2beSBill Paul 	 */
29845b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
29855b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
29865b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
29875b610048SPyun YongHyeon 	else
29885b610048SPyun YongHyeon 		sbsz = 32;
29895b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
29905b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
29915b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
29925b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
29935b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
29945b610048SPyun YongHyeon 	if (error)
29955b610048SPyun YongHyeon 		return (error);
29965b610048SPyun YongHyeon 
299712c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
299812c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
299912c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
300012c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
300112c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
300212c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
300312c65daeSPyun YongHyeon 	if (error)
300412c65daeSPyun YongHyeon 		return (error);
300512c65daeSPyun YongHyeon 
30065b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
30075b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
30085b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
30095b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
30105b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
30115b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
30125b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
30135b610048SPyun YongHyeon 		if (error)
30145b610048SPyun YongHyeon 			return (error);
30155b610048SPyun YongHyeon 	}
30165b610048SPyun YongHyeon 
30175b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
3018e86fa024STycho Nightingale 	boundary = 0;
3019d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
3020e86fa024STycho Nightingale 		boundary = BGE_DMA_BNDRY;
3021d2ffe15aSPyun YongHyeon 		/*
3022d2ffe15aSPyun YongHyeon 		 * XXX
3023d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
3024d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
3025062af0b0SPyun YongHyeon 		 * Both limiting DMA address space to 32bits and flushing
3026062af0b0SPyun YongHyeon 		 * mailbox write seem to address the issue.
3027d2ffe15aSPyun YongHyeon 		 */
3028062af0b0SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3029d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
3030d2ffe15aSPyun YongHyeon 	}
3031e86fa024STycho Nightingale 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
3032e86fa024STycho Nightingale 	    1, boundary, lowaddr, BUS_SPACE_MAXADDR, NULL,
3033e86fa024STycho Nightingale 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
3034e86fa024STycho Nightingale 	    0, NULL, NULL, &sc->bge_cdata.bge_buffer_tag);
30355b610048SPyun YongHyeon 	if (error != 0) {
30365b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
30375b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
30385b610048SPyun YongHyeon 		return (ENOMEM);
30395b610048SPyun YongHyeon 	}
30405b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
30411108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
3042ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
3043ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
3044ca3f1187SPyun YongHyeon 	} else {
3045ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
3046ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
3047ca3f1187SPyun YongHyeon 	}
30485b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
3049ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
3050ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
3051ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
3052f41ac2beSBill Paul 
3053f41ac2beSBill Paul 	if (error) {
30540ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
30550ac56796SPyun YongHyeon 		return (ENOMEM);
30560ac56796SPyun YongHyeon 	}
30570ac56796SPyun YongHyeon 
30585b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
3059f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
3060f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
3061f5459d4cSPyun YongHyeon 	else
3062f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
30635b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
3064f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
3065f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
30660ac56796SPyun YongHyeon 
30670ac56796SPyun YongHyeon 	if (error) {
30680ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
3069f41ac2beSBill Paul 		return (ENOMEM);
3070f41ac2beSBill Paul 	}
3071f41ac2beSBill Paul 
30723f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
3073943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3074943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
3075943787f3SPyun YongHyeon 	if (error) {
3076943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
3077943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
3078943787f3SPyun YongHyeon 		return (ENOMEM);
3079943787f3SPyun YongHyeon 	}
3080f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
30810ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3082f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
3083f41ac2beSBill Paul 		if (error) {
3084fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
3085fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
3086f41ac2beSBill Paul 			return (ENOMEM);
3087f41ac2beSBill Paul 		}
3088f41ac2beSBill Paul 	}
3089f41ac2beSBill Paul 
30903f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
3091f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
30920ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
3093f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
3094f41ac2beSBill Paul 		if (error) {
3095fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
30960ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
3097f41ac2beSBill Paul 			return (ENOMEM);
3098f41ac2beSBill Paul 		}
3099f41ac2beSBill Paul 	}
3100f41ac2beSBill Paul 
31015b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
31024c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
31035b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
31048a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
31051be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
31061be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
3107f41ac2beSBill Paul 		if (error) {
3108fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
31093f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
3110f41ac2beSBill Paul 			return (ENOMEM);
3111f41ac2beSBill Paul 		}
31123f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
3113943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3114943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
3115943787f3SPyun YongHyeon 		if (error) {
3116943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
31171b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
3118943787f3SPyun YongHyeon 			return (ENOMEM);
3119943787f3SPyun YongHyeon 		}
3120f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
3121f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3122f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
3123f41ac2beSBill Paul 			if (error) {
3124fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
31253f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
3126f41ac2beSBill Paul 				return (ENOMEM);
3127f41ac2beSBill Paul 			}
3128f41ac2beSBill Paul 		}
3129f41ac2beSBill Paul 	}
3130f41ac2beSBill Paul 
3131f41ac2beSBill Paul 	return (0);
3132f41ac2beSBill Paul }
3133f41ac2beSBill Paul 
3134bf6ef57aSJohn Polstra /*
3135bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
3136bf6ef57aSJohn Polstra  */
3137bf6ef57aSJohn Polstra static int
3138bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
3139bf6ef57aSJohn Polstra {
3140bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
314155aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
3142bf6ef57aSJohn Polstra 
314355aaf894SMarius Strobl 	d = pci_get_domain(dev);
3144bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
3145bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
3146bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
3147bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
314855aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
3149bf6ef57aSJohn Polstra 			return (1);
3150bf6ef57aSJohn Polstra 	return (0);
3151bf6ef57aSJohn Polstra }
3152bf6ef57aSJohn Polstra 
3153bf6ef57aSJohn Polstra /*
3154bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
3155bf6ef57aSJohn Polstra  */
3156bf6ef57aSJohn Polstra static int
3157bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
3158bf6ef57aSJohn Polstra {
3159bf6ef57aSJohn Polstra 	int can_use_msi = 0;
3160bf6ef57aSJohn Polstra 
3161d9fc28e4SPyun YongHyeon 	if (sc->bge_msi == 0)
31625c952e8dSPyun YongHyeon 		return (0);
31635c952e8dSPyun YongHyeon 
31641108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
31651108273aSPyun YongHyeon #ifdef DEVICE_POLLING
31661108273aSPyun YongHyeon 	return (0);
31671108273aSPyun YongHyeon #endif
3168bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
3169a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
3170bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
3171bf6ef57aSJohn Polstra 		/*
3172a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
3173a8376f70SMarius Strobl 		 * configured in single-port mode.
3174bf6ef57aSJohn Polstra 		 */
3175bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
3176bf6ef57aSJohn Polstra 			can_use_msi = 1;
3177bf6ef57aSJohn Polstra 		break;
3178bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
3179bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
3180bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
3181bf6ef57aSJohn Polstra 			can_use_msi = 1;
3182bf6ef57aSJohn Polstra 		break;
318391c69b97SEugene Grosbein 	case BGE_ASICREV_BCM5784:
318491c69b97SEugene Grosbein 		/*
318591c69b97SEugene Grosbein 		 * Prevent infinite "watchdog timeout" errors
318691c69b97SEugene Grosbein 		 * in some MacBook Pro and make it work out-of-the-box.
318791c69b97SEugene Grosbein 		 */
318891c69b97SEugene Grosbein 		if (sc->bge_chiprev == BGE_CHIPREV_5784_AX)
318991c69b97SEugene Grosbein 			break;
319091c69b97SEugene Grosbein 		/* FALLTHROUGH */
3191a8376f70SMarius Strobl 	default:
3192a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
3193bf6ef57aSJohn Polstra 			can_use_msi = 1;
3194bf6ef57aSJohn Polstra 	}
3195bf6ef57aSJohn Polstra 	return (can_use_msi);
3196bf6ef57aSJohn Polstra }
3197bf6ef57aSJohn Polstra 
319895d67482SBill Paul static int
3199062af0b0SPyun YongHyeon bge_mbox_reorder(struct bge_softc *sc)
3200062af0b0SPyun YongHyeon {
3201062af0b0SPyun YongHyeon 	/* Lists of PCI bridges that are known to reorder mailbox writes. */
3202062af0b0SPyun YongHyeon 	static const struct mbox_reorder {
3203062af0b0SPyun YongHyeon 		const uint16_t vendor;
3204062af0b0SPyun YongHyeon 		const uint16_t device;
3205062af0b0SPyun YongHyeon 		const char *desc;
320629658c96SDimitry Andric 	} mbox_reorder_lists[] = {
3207062af0b0SPyun YongHyeon 		{ 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
3208062af0b0SPyun YongHyeon 	};
3209062af0b0SPyun YongHyeon 	devclass_t pci, pcib;
3210062af0b0SPyun YongHyeon 	device_t bus, dev;
321147f4a4dcSMarius Strobl 	int i;
3212062af0b0SPyun YongHyeon 
3213062af0b0SPyun YongHyeon 	pci = devclass_find("pci");
3214062af0b0SPyun YongHyeon 	pcib = devclass_find("pcib");
3215062af0b0SPyun YongHyeon 	dev = sc->bge_dev;
3216062af0b0SPyun YongHyeon 	bus = device_get_parent(dev);
3217062af0b0SPyun YongHyeon 	for (;;) {
3218062af0b0SPyun YongHyeon 		dev = device_get_parent(bus);
3219062af0b0SPyun YongHyeon 		bus = device_get_parent(dev);
3220062af0b0SPyun YongHyeon 		if (device_get_devclass(dev) != pcib)
3221062af0b0SPyun YongHyeon 			break;
3222a70e114dSAndriy Gapon 		if (device_get_devclass(bus) != pci)
3223a70e114dSAndriy Gapon 			break;
322447f4a4dcSMarius Strobl 		for (i = 0; i < nitems(mbox_reorder_lists); i++) {
3225062af0b0SPyun YongHyeon 			if (pci_get_vendor(dev) ==
3226062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].vendor &&
3227062af0b0SPyun YongHyeon 			    pci_get_device(dev) ==
3228062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].device) {
3229062af0b0SPyun YongHyeon 				device_printf(sc->bge_dev,
3230062af0b0SPyun YongHyeon 				    "enabling MBOX workaround for %s\n",
3231062af0b0SPyun YongHyeon 				    mbox_reorder_lists[i].desc);
3232062af0b0SPyun YongHyeon 				return (1);
3233062af0b0SPyun YongHyeon 			}
3234062af0b0SPyun YongHyeon 		}
3235062af0b0SPyun YongHyeon 	}
3236062af0b0SPyun YongHyeon 	return (0);
3237062af0b0SPyun YongHyeon }
3238062af0b0SPyun YongHyeon 
3239ea9c3a30SPyun YongHyeon static void
3240ea9c3a30SPyun YongHyeon bge_devinfo(struct bge_softc *sc)
3241ea9c3a30SPyun YongHyeon {
3242ea9c3a30SPyun YongHyeon 	uint32_t cfg, clk;
3243ea9c3a30SPyun YongHyeon 
3244ea9c3a30SPyun YongHyeon 	device_printf(sc->bge_dev,
3245ea9c3a30SPyun YongHyeon 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; ",
3246ea9c3a30SPyun YongHyeon 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev);
3247ea9c3a30SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
3248ea9c3a30SPyun YongHyeon 		printf("PCI-E\n");
3249ea9c3a30SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_PCIX) {
3250ea9c3a30SPyun YongHyeon 		printf("PCI-X ");
3251ea9c3a30SPyun YongHyeon 		cfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3252ea9c3a30SPyun YongHyeon 		if (cfg == BGE_MISCCFG_BOARD_ID_5704CIOBE)
3253ea9c3a30SPyun YongHyeon 			clk = 133;
3254ea9c3a30SPyun YongHyeon 		else {
3255ea9c3a30SPyun YongHyeon 			clk = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
3256ea9c3a30SPyun YongHyeon 			switch (clk) {
3257ea9c3a30SPyun YongHyeon 			case 0:
3258ea9c3a30SPyun YongHyeon 				clk = 33;
3259ea9c3a30SPyun YongHyeon 				break;
3260ea9c3a30SPyun YongHyeon 			case 2:
3261ea9c3a30SPyun YongHyeon 				clk = 50;
3262ea9c3a30SPyun YongHyeon 				break;
3263ea9c3a30SPyun YongHyeon 			case 4:
3264ea9c3a30SPyun YongHyeon 				clk = 66;
3265ea9c3a30SPyun YongHyeon 				break;
3266ea9c3a30SPyun YongHyeon 			case 6:
3267ea9c3a30SPyun YongHyeon 				clk = 100;
3268ea9c3a30SPyun YongHyeon 				break;
3269ea9c3a30SPyun YongHyeon 			case 7:
3270ea9c3a30SPyun YongHyeon 				clk = 133;
3271ea9c3a30SPyun YongHyeon 				break;
3272ea9c3a30SPyun YongHyeon 			}
3273ea9c3a30SPyun YongHyeon 		}
3274ea9c3a30SPyun YongHyeon 		printf("%u MHz\n", clk);
3275ea9c3a30SPyun YongHyeon 	} else {
3276ea9c3a30SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3277ea9c3a30SPyun YongHyeon 			printf("PCI on PCI-X ");
3278ea9c3a30SPyun YongHyeon 		else
3279ea9c3a30SPyun YongHyeon 			printf("PCI ");
3280ea9c3a30SPyun YongHyeon 		cfg = pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4);
3281ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_PCI_BUSSPEED)
3282ea9c3a30SPyun YongHyeon 			clk = 66;
3283ea9c3a30SPyun YongHyeon 		else
3284ea9c3a30SPyun YongHyeon 			clk = 33;
3285ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_32BIT_BUS)
3286ea9c3a30SPyun YongHyeon 			printf("%u MHz; 32bit\n", clk);
3287ea9c3a30SPyun YongHyeon 		else
3288ea9c3a30SPyun YongHyeon 			printf("%u MHz; 64bit\n", clk);
3289ea9c3a30SPyun YongHyeon 	}
3290ea9c3a30SPyun YongHyeon }
3291ea9c3a30SPyun YongHyeon 
3292062af0b0SPyun YongHyeon static int
32933f74909aSGleb Smirnoff bge_attach(device_t dev)
329495d67482SBill Paul {
3295fba8b109SMarcel Moolenaar 	if_t ifp;
329695d67482SBill Paul 	struct bge_softc *sc;
3297548c8f1aSPyun YongHyeon 	uint32_t hwcfg = 0, misccfg, pcistate;
329808013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
3299ad4328baSMarius Strobl 	int capmask, error, reg, rid, trys;
330095d67482SBill Paul 
330195d67482SBill Paul 	sc = device_get_softc(dev);
330295d67482SBill Paul 	sc->bge_dev = dev;
330395d67482SBill Paul 
3304e010b055SPyun YongHyeon 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
33056c3e93cbSGleb Smirnoff 	NET_TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
3306e010b055SPyun YongHyeon 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
3307dfe0df9aSPyun YongHyeon 
330895d67482SBill Paul 	pci_enable_busmaster(dev);
330995d67482SBill Paul 
3310ad4328baSMarius Strobl 	/*
3311ad4328baSMarius Strobl 	 * Allocate control/status registers.
3312ad4328baSMarius Strobl 	 */
3313736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
33145f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
331544f8f2fcSMarius Strobl 	    RF_ACTIVE);
331695d67482SBill Paul 
331795d67482SBill Paul 	if (sc->bge_res == NULL) {
3318548c8f1aSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map BAR0 memory\n");
331995d67482SBill Paul 		error = ENXIO;
332095d67482SBill Paul 		goto fail;
332195d67482SBill Paul 	}
332295d67482SBill Paul 
33234f09c4c7SMarius Strobl 	/* Save various chip information. */
3324548c8f1aSPyun YongHyeon 	sc->bge_func_addr = pci_get_function(dev);
3325d7acafa1SMarius Strobl 	sc->bge_chipid = bge_chipid(dev);
3326e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
3327e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
3328e53d81eeSPaul Saab 
3329a813ed78SPyun YongHyeon 	/* Set default PHY address. */
3330daeeb75cSPyun YongHyeon 	sc->bge_phy_addr = 1;
33311108273aSPyun YongHyeon 	 /*
33321108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
33331108273aSPyun YongHyeon 	  *
33341108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
33351108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
33361108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
33371108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
33381108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
3339bbe2ca75SPyun YongHyeon 	  * BCM5719  |   1   |   8   |   2   |   9   |
334050515680SPyun YongHyeon 	  * BCM5720  |   1   |   8   |   2   |   9   |
33411108273aSPyun YongHyeon 	  *
3342548c8f1aSPyun YongHyeon 	  *          | F2 Cu | F2 Sr | F3 Cu | F3 Sr |
3343548c8f1aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
3344548c8f1aSPyun YongHyeon 	  * BCM57XX  |   X   |   X   |   X   |   X   |
3345548c8f1aSPyun YongHyeon 	  * BCM5704  |   X   |   X   |   X   |   X   |
3346548c8f1aSPyun YongHyeon 	  * BCM5717  |   X   |   X   |   X   |   X   |
3347548c8f1aSPyun YongHyeon 	  * BCM5719  |   3   |   10  |   4   |   11  |
3348548c8f1aSPyun YongHyeon 	  * BCM5720  |   X   |   X   |   X   |   X   |
3349548c8f1aSPyun YongHyeon 	  *
33501108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
33511108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
33521108273aSPyun YongHyeon 	  */
3353bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
335450515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
335550515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
3356548c8f1aSPyun YongHyeon 		if (sc->bge_chipid != BGE_CHIPID_BCM5717_A0) {
33571108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
33581108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
3359daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33601108273aSPyun YongHyeon 			else
3361daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
3362bbe2ca75SPyun YongHyeon 		} else {
33631108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
33641108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
3365daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33661108273aSPyun YongHyeon 			else
3367daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
33681108273aSPyun YongHyeon 		}
33691108273aSPyun YongHyeon 	}
3370a813ed78SPyun YongHyeon 
33715fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
33725fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
337308013fd3SMarius Strobl 
33740dae9719SJung-uk Kim 	/* Save chipset family. */
33750dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
33762927f01fSPyun YongHyeon 	case BGE_ASICREV_BCM5762:
3377fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57765:
3378fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57766:
3379fe26ad88SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_57765_PLUS;
3380fe26ad88SPyun YongHyeon 		/* FALLTHROUGH */
33811108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3382bbe2ca75SPyun YongHyeon 	case BGE_ASICREV_BCM5719:
338350515680SPyun YongHyeon 	case BGE_ASICREV_BCM5720:
33841108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
33851108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
3386b4a256acSPyun YongHyeon 		    BGE_FLAG_JUMBO_FRAME;
338729b44b09SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
338829b44b09SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
338929b44b09SPyun YongHyeon 			/*
339029b44b09SPyun YongHyeon 			 * Enable work around for DMA engine miscalculation
339129b44b09SPyun YongHyeon 			 * of TXMBUF available space.
339229b44b09SPyun YongHyeon 			 */
339329b44b09SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_RDMA_BUG;
3394bbe2ca75SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3395bbe2ca75SPyun YongHyeon 			    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3396bbe2ca75SPyun YongHyeon 				/* Jumbo frame on BCM5719 A0 does not work. */
3397463a7e27SPyun YongHyeon 				sc->bge_flags &= ~BGE_FLAG_JUMBO;
3398bbe2ca75SPyun YongHyeon 			}
339929b44b09SPyun YongHyeon 		}
34001108273aSPyun YongHyeon 		break;
3401a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
3402a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
3403a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
3404a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
3405a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
3406a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
3407a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
3408a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
3409a5779553SStanislav Sedov 		break;
34100dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
34110dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
34120dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
34130dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
34147ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
34150dae9719SJung-uk Kim 		break;
34160dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
34170dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
34180dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
3419f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
34209fe569d8SXin LI 		/* FALLTHROUGH */
34210dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
34220dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
342338cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
34240dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
34259fe569d8SXin LI 		/* FALLTHROUGH */
34260dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
34270dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
34280dae9719SJung-uk Kim 		break;
34290dae9719SJung-uk Kim 	}
34300dae9719SJung-uk Kim 
3431548c8f1aSPyun YongHyeon 	/* Identify chips with APE processor. */
3432548c8f1aSPyun YongHyeon 	switch (sc->bge_asicrev) {
3433548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3434548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5719:
3435548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5720:
3436548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5761:
34372927f01fSPyun YongHyeon 	case BGE_ASICREV_BCM5762:
3438548c8f1aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_APE;
3439548c8f1aSPyun YongHyeon 		break;
3440548c8f1aSPyun YongHyeon 	}
3441548c8f1aSPyun YongHyeon 
3442548c8f1aSPyun YongHyeon 	/* Chips with APE need BAR2 access for APE registers/memory. */
3443548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) != 0) {
3444548c8f1aSPyun YongHyeon 		rid = PCIR_BAR(2);
3445548c8f1aSPyun YongHyeon 		sc->bge_res2 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
3446548c8f1aSPyun YongHyeon 		    RF_ACTIVE);
3447548c8f1aSPyun YongHyeon 		if (sc->bge_res2 == NULL) {
3448548c8f1aSPyun YongHyeon 			device_printf (sc->bge_dev,
3449548c8f1aSPyun YongHyeon 			    "couldn't map BAR2 memory\n");
3450548c8f1aSPyun YongHyeon 			error = ENXIO;
3451548c8f1aSPyun YongHyeon 			goto fail;
3452548c8f1aSPyun YongHyeon 		}
3453548c8f1aSPyun YongHyeon 
3454548c8f1aSPyun YongHyeon 		/* Enable APE register/memory access by host driver. */
3455548c8f1aSPyun YongHyeon 		pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
3456548c8f1aSPyun YongHyeon 		pcistate |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
3457548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
3458548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
3459548c8f1aSPyun YongHyeon 		pci_write_config(dev, BGE_PCI_PCISTATE, pcistate, 4);
3460548c8f1aSPyun YongHyeon 
3461548c8f1aSPyun YongHyeon 		bge_ape_lock_init(sc);
3462548c8f1aSPyun YongHyeon 		bge_ape_read_fw_ver(sc);
3463548c8f1aSPyun YongHyeon 	}
3464548c8f1aSPyun YongHyeon 
3465749a5269SMarius Strobl 	/* Add SYSCTLs, requires the chipset family to be set. */
3466749a5269SMarius Strobl 	bge_add_sysctls(sc);
3467749a5269SMarius Strobl 
3468a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
34691108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
34701108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3471a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3472a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
3473a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
3474a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
3475a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
3476a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
3477a813ed78SPyun YongHyeon 	else
3478a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
34797ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
34807ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
34817ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
3482a813ed78SPyun YongHyeon 
3483f681b29aSPyun YongHyeon 	/*
3484d4622124SPyun YongHyeon 	 * All Broadcom controllers have 4GB boundary DMA bug.
3485f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
3486f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3487f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3488f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
3489f681b29aSPyun YongHyeon 	 */
3490f681b29aSPyun YongHyeon 	sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
34914f0794ffSBjoern A. Zeeb 
3492d9820cd8SPyun YongHyeon 	/* BCM5755 or higher and BCM5906 have short DMA bug. */
3493d9820cd8SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
3494d9820cd8SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
3495d9820cd8SPyun YongHyeon 
3496a7fcfcf3SPyun YongHyeon 	/*
3497a7fcfcf3SPyun YongHyeon 	 * BCM5719 cannot handle DMA requests for DMA segments that
3498a7fcfcf3SPyun YongHyeon 	 * have larger than 4KB in size.  However the maximum DMA
3499a7fcfcf3SPyun YongHyeon 	 * segment size created in DMA tag is 4KB for TSO, so we
3500a7fcfcf3SPyun YongHyeon 	 * wouldn't encounter the issue here.
3501a7fcfcf3SPyun YongHyeon 	 */
3502a7fcfcf3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3503a7fcfcf3SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3504a7fcfcf3SPyun YongHyeon 
3505ea9c3a30SPyun YongHyeon 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3506fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
35074f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
35084f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
35094f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
351084ac96f8SPyun YongHyeon 	}
35114f0794ffSBjoern A. Zeeb 
3512fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
3513fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3514fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
3515fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3516fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
3517fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3518fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3519fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3520fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
3521fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3522fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3523fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3524fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3525d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57791 ||
3526d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57795 ||
3527fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3528fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
3529fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
3530d73ea7c6SPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
3531fb772a6cSMarius Strobl 	}
3532fb772a6cSMarius Strobl 
3533e53d81eeSPaul Saab 	/*
3534ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
3535ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
3536ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
3537ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
3538d7acafa1SMarius Strobl 	 * known bug which can't handle TSO if Ethernet header + IP/TCP
3539d7acafa1SMarius Strobl 	 * header is greater than 80 bytes. A workaround for the TSO
3540ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
3541ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
3542ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
3543ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
3544ca3f1187SPyun YongHyeon 	 */
35451108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
35461108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
35471108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
3548bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3549bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3550bbe2ca75SPyun YongHyeon 			/* TSO on BCM5719 A0 does not work. */
3551bbe2ca75SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_TSO3;
3552bbe2ca75SPyun YongHyeon 		}
35531108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
35544f4a16e1SPyun YongHyeon 		/*
35554f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
35564f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
3557be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
35584f4a16e1SPyun YongHyeon 		 */
35594f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3560be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3561be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3562ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
35634f4a16e1SPyun YongHyeon 	}
3564ca3f1187SPyun YongHyeon 
3565ca3f1187SPyun YongHyeon 	/*
35666f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
3567e53d81eeSPaul Saab 	 */
35683b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
35694c0da0ffSGleb Smirnoff 		/*
35706f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
35716f8718a3SScott Long 		 * must be a PCI Express device.
35726f8718a3SScott Long 		 */
35736f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
35740aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
357548630d79SPyun YongHyeon 		/* Extract supported maximum payload size. */
357648630d79SPyun YongHyeon 		sc->bge_mps = pci_read_config(dev, sc->bge_expcap +
357748630d79SPyun YongHyeon 		    PCIER_DEVICE_CAP, 2);
357848630d79SPyun YongHyeon 		sc->bge_mps = 128 << (sc->bge_mps & PCIEM_CAP_MAX_PAYLOAD);
357950515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
358050515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
358148630d79SPyun YongHyeon 			sc->bge_expmrq = 2048;
358248630d79SPyun YongHyeon 		else
358348630d79SPyun YongHyeon 			sc->bge_expmrq = 4096;
358448630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
35856f8718a3SScott Long 	} else {
35866f8718a3SScott Long 		/*
35876f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
35886f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
35894c0da0ffSGleb Smirnoff 		 */
35903b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
35910aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
359290447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
35934c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
3594652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
35956f8718a3SScott Long 	}
35964c0da0ffSGleb Smirnoff 
3597bf6ef57aSJohn Polstra 	/*
3598fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3599fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
3600fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3601fd4d32feSPyun YongHyeon 	 */
3602fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3603fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3604fd4d32feSPyun YongHyeon 	/*
3605062af0b0SPyun YongHyeon 	 * Some PCI-X bridges are known to trigger write reordering to
3606062af0b0SPyun YongHyeon 	 * the mailbox registers. Typical phenomena is watchdog timeouts
3607062af0b0SPyun YongHyeon 	 * caused by out-of-order TX completions.  Enable workaround for
3608062af0b0SPyun YongHyeon 	 * PCI-X devices that live behind these bridges.
3609062af0b0SPyun YongHyeon 	 * Note, PCI-X controllers can run in PCI mode so we can't use
3610062af0b0SPyun YongHyeon 	 * BGE_FLAG_PCIX flag to detect PCI-X controllers.
3611062af0b0SPyun YongHyeon 	 */
3612062af0b0SPyun YongHyeon 	if (sc->bge_pcixcap != 0 && bge_mbox_reorder(sc) != 0)
3613062af0b0SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_MBOX_REORDER;
3614062af0b0SPyun YongHyeon 	/*
3615bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
3616bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
3617bf6ef57aSJohn Polstra 	 * normal operation.
3618bf6ef57aSJohn Polstra 	 */
36190aaf1057SPyun YongHyeon 	rid = 0;
36203b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
36210aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
3622ad4328baSMarius Strobl 		reg = 1;
3623ad4328baSMarius Strobl 		if (bge_can_use_msi(sc) && pci_alloc_msi(dev, &reg) == 0) {
3624bf6ef57aSJohn Polstra 			rid = 1;
3625bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
36260aaf1057SPyun YongHyeon 		}
36270aaf1057SPyun YongHyeon 	}
3628bf6ef57aSJohn Polstra 
36291108273aSPyun YongHyeon 	/*
36301108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
36311108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
36321108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
36331108273aSPyun YongHyeon 	 */
36341108273aSPyun YongHyeon #ifndef DEVICE_POLLING
36351108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
36361108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
36371108273aSPyun YongHyeon #endif
36381108273aSPyun YongHyeon 
3639bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3640ad4328baSMarius Strobl 	    RF_ACTIVE | (rid != 0 ? 0 : RF_SHAREABLE));
3641bf6ef57aSJohn Polstra 
3642bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
3643bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
3644bf6ef57aSJohn Polstra 		error = ENXIO;
3645bf6ef57aSJohn Polstra 		goto fail;
3646bf6ef57aSJohn Polstra 	}
3647bf6ef57aSJohn Polstra 
3648ea9c3a30SPyun YongHyeon 	bge_devinfo(sc);
36494f09c4c7SMarius Strobl 
36508cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
3651548c8f1aSPyun YongHyeon 	/* No ASF if APE present. */
3652548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) == 0) {
3653888b47f0SPyun YongHyeon 		if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3654888b47f0SPyun YongHyeon 		    BGE_SRAM_DATA_SIG_MAGIC)) {
3655548c8f1aSPyun YongHyeon 			if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG) &
3656548c8f1aSPyun YongHyeon 			    BGE_HWCFG_ASF) {
36578cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_ENABLE;
36588cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_STACKUP;
3659d67eba2fSPyun YongHyeon 				if (BGE_IS_575X_PLUS(sc))
36608cb1383cSDoug Ambrisko 					sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
36618cb1383cSDoug Ambrisko 			}
36628cb1383cSDoug Ambrisko 		}
3663548c8f1aSPyun YongHyeon 	}
36648cb1383cSDoug Ambrisko 
36658cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
36663dd76c98SPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
36678cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
36688cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
36698cb1383cSDoug Ambrisko 		error = ENXIO;
36708cb1383cSDoug Ambrisko 		goto fail;
36718cb1383cSDoug Ambrisko 	}
36728cb1383cSDoug Ambrisko 
36733dd76c98SPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
36743dd76c98SPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
367595d67482SBill Paul 
367695d67482SBill Paul 	if (bge_chipinit(sc)) {
3677fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
367895d67482SBill Paul 		error = ENXIO;
367995d67482SBill Paul 		goto fail;
368095d67482SBill Paul 	}
368195d67482SBill Paul 
368238cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
368338cc658fSJohn Baldwin 	if (error) {
368408013fd3SMarius Strobl 		device_printf(sc->bge_dev,
368508013fd3SMarius Strobl 		    "failed to read station address\n");
368695d67482SBill Paul 		error = ENXIO;
368795d67482SBill Paul 		goto fail;
368895d67482SBill Paul 	}
368995d67482SBill Paul 
3690f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
36911108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
36921108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
36931108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3694f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3695f41ac2beSBill Paul 	else
3696f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3697f41ac2beSBill Paul 
36985b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3699fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3700fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3701f41ac2beSBill Paul 		error = ENXIO;
3702f41ac2beSBill Paul 		goto fail;
3703f41ac2beSBill Paul 	}
3704f41ac2beSBill Paul 
370595d67482SBill Paul 	/* Set default tuneable values. */
370695d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
370795d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
370895d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
37096f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
37106f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
371195d67482SBill Paul 
371235f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
371335f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
371435f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
371535f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
371635f945cdSPyun YongHyeon 
371795d67482SBill Paul 	/* Set up ifnet structure */
3718fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3719fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3720fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3721fc74a9f9SBrooks Davis 		error = ENXIO;
3722fc74a9f9SBrooks Davis 		goto fail;
3723fc74a9f9SBrooks Davis 	}
3724fba8b109SMarcel Moolenaar 	if_setsoftc(ifp, sc);
37259bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
3726fba8b109SMarcel Moolenaar 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
3727fba8b109SMarcel Moolenaar 	if_setioctlfn(ifp, bge_ioctl);
3728fba8b109SMarcel Moolenaar 	if_setstartfn(ifp, bge_start);
3729fba8b109SMarcel Moolenaar 	if_setinitfn(ifp, bge_init);
3730df360178SGleb Smirnoff 	if_setgetcounterfn(ifp, bge_get_counter);
37314a81240cSMarcel Moolenaar 	if_setsendqlen(ifp, BGE_TX_RING_CNT - 1);
3732fba8b109SMarcel Moolenaar 	if_setsendqready(ifp);
3733fba8b109SMarcel Moolenaar 	if_sethwassist(ifp, sc->bge_csum_features);
3734fba8b109SMarcel Moolenaar 	if_setcapabilities(ifp, IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
3735fba8b109SMarcel Moolenaar 	    IFCAP_VLAN_MTU);
37361108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3737fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, CSUM_TSO, 0);
3738fba8b109SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, IFCAP_TSO4 | IFCAP_VLAN_HWTSO, 0);
3739ca3f1187SPyun YongHyeon 	}
37404e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
3741fba8b109SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWCSUM, 0);
37424e35d186SJung-uk Kim #endif
3743fba8b109SMarcel Moolenaar 	if_setcapenable(ifp, if_getcapabilities(ifp));
374475719184SGleb Smirnoff #ifdef DEVICE_POLLING
3745fba8b109SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
374675719184SGleb Smirnoff #endif
374795d67482SBill Paul 
3748a1d52896SBill Paul 	/*
3749d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3750d375e524SGleb Smirnoff 	 * to hardware bugs.
3751d375e524SGleb Smirnoff 	 */
3752d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3753fba8b109SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, 0, IFCAP_HWCSUM);
3754fba8b109SMarcel Moolenaar 		if_setcapenablebit(ifp, 0, IFCAP_HWCSUM);
3755fba8b109SMarcel Moolenaar 		if_sethwassist(ifp, 0);
3756d375e524SGleb Smirnoff 	}
3757d375e524SGleb Smirnoff 
3758d375e524SGleb Smirnoff 	/*
3759a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
376041abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
376141abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
376241abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
376341abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
376441abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
376541abcc1bSPaul Saab 	 * SK-9D41.
3766a1d52896SBill Paul 	 */
3767888b47f0SPyun YongHyeon 	if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3768888b47f0SPyun YongHyeon 		hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
37695fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
37705fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3771f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3772f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3773fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3774f6789fbaSPyun YongHyeon 			error = ENXIO;
3775f6789fbaSPyun YongHyeon 			goto fail;
3776f6789fbaSPyun YongHyeon 		}
377741abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
377841abcc1bSPaul Saab 	}
377941abcc1bSPaul Saab 
378095d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3781ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3782ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
378370c2071bSPyun YongHyeon 		if (BGE_IS_5705_PLUS(sc)) {
3784ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
378570c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
378670c2071bSPyun YongHyeon 		} else
3787652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3788ea3b4127SPyun YongHyeon 	}
378995d67482SBill Paul 
379070c2071bSPyun YongHyeon 	/* Set various PHY bug flags. */
379170c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
379270c2071bSPyun YongHyeon 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
379370c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
379470c2071bSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
379570c2071bSPyun YongHyeon 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
379670c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
379770c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
379870c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
379970c2071bSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
380070c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
380170c2071bSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
380270c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
380370c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
3804fe26ad88SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780 &&
3805fe26ad88SPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc)) {
380670c2071bSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
380770c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
380870c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
380970c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
381070c2071bSPyun YongHyeon 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
381170c2071bSPyun YongHyeon 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
381270c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
381370c2071bSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
381470c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
381570c2071bSPyun YongHyeon 		} else
381670c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
381770c2071bSPyun YongHyeon 	}
381870c2071bSPyun YongHyeon 
381970c2071bSPyun YongHyeon 	/*
3820d73ea7c6SPyun YongHyeon 	 * Don't enable Ethernet@WireSpeed for the 5700 or the
382170c2071bSPyun YongHyeon 	 * 5705 A0 and A1 chips.
382270c2071bSPyun YongHyeon 	 */
382370c2071bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
382470c2071bSPyun YongHyeon 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
382570c2071bSPyun YongHyeon 	    (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
3826d73ea7c6SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)))
382770c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
382870c2071bSPyun YongHyeon 
3829652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
383009a8241fSGleb Smirnoff 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
38310c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
38320c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
38336098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
38346098821cSJung-uk Kim 		    0, NULL);
383595d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
383695d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3837da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
383895d67482SBill Paul 	} else {
383995d67482SBill Paul 		/*
38408cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
38418cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
38428cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
38438cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
38448cb1383cSDoug Ambrisko 		 * the PHY.
384595d67482SBill Paul 		 */
38464012d104SMarius Strobl 		trys = 0;
38478cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
38488cb1383cSDoug Ambrisko again:
38498cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
38508cb1383cSDoug Ambrisko 
3851fba8b109SMarcel Moolenaar 		error = mii_attach(dev, &sc->bge_miibus, ifp,
3852fba8b109SMarcel Moolenaar 		    (ifm_change_cb_t)bge_ifmedia_upd,
3853fba8b109SMarcel Moolenaar 		    (ifm_stat_cb_t)bge_ifmedia_sts, capmask, sc->bge_phy_addr,
3854fba8b109SMarcel Moolenaar 		    MII_OFFSET_ANY, MIIF_DOPAUSE);
38558e5d93dbSMarius Strobl 		if (error != 0) {
38568cb1383cSDoug Ambrisko 			if (trys++ < 4) {
38578cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
3858daeeb75cSPyun YongHyeon 				bge_miibus_writereg(sc->bge_dev,
3859daeeb75cSPyun YongHyeon 				    sc->bge_phy_addr, MII_BMCR, BMCR_RESET);
38608cb1383cSDoug Ambrisko 				goto again;
38618cb1383cSDoug Ambrisko 			}
38628e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
386395d67482SBill Paul 			goto fail;
386495d67482SBill Paul 		}
38658cb1383cSDoug Ambrisko 
38668cb1383cSDoug Ambrisko 		/*
38678cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
38688cb1383cSDoug Ambrisko 		 */
38698cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
38708cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
387195d67482SBill Paul 	}
387295d67482SBill Paul 
387395d67482SBill Paul 	/*
3874e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3875e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3876e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3877e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3878e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3879e255b776SJohn Polstra 	 * payloads by copying the received packets.
3880e255b776SJohn Polstra 	 */
3881652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3882652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3883652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3884e255b776SJohn Polstra 
3885e255b776SJohn Polstra 	/*
388695d67482SBill Paul 	 * Call MI attach routine.
388795d67482SBill Paul 	 */
3888fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
38890f9bd73bSSam Leffler 
389061ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
3891fba8b109SMarcel Moolenaar 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
389261ccb9daSPyun YongHyeon 
38930f9bd73bSSam Leffler 	/*
38940f9bd73bSSam Leffler 	 * Hookup IRQ last.
38950f9bd73bSSam Leffler 	 */
3896dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3897dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
38987e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
38997e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3900dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3901dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3902dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3903dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3904dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3905e010b055SPyun YongHyeon 			error = ENOMEM;
3906dfe0df9aSPyun YongHyeon 			goto fail;
3907dfe0df9aSPyun YongHyeon 		}
3908d7acafa1SMarius Strobl 		error = taskqueue_start_threads(&sc->bge_tq, 1, PI_NET,
3909d7acafa1SMarius Strobl 		    "%s taskq", device_get_nameunit(sc->bge_dev));
3910d7acafa1SMarius Strobl 		if (error != 0) {
3911d7acafa1SMarius Strobl 			device_printf(dev, "could not start threads.\n");
3912d7acafa1SMarius Strobl 			ether_ifdetach(ifp);
3913d7acafa1SMarius Strobl 			goto fail;
3914d7acafa1SMarius Strobl 		}
3915dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3916dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3917dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3918dfe0df9aSPyun YongHyeon 	} else
3919dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3920dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3921dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
39220f9bd73bSSam Leffler 
39230f9bd73bSSam Leffler 	if (error) {
3924e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
3925fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
3926ded66962SMark Johnston 		goto fail;
39270f9bd73bSSam Leffler 	}
392895d67482SBill Paul 
39297790c8c1SConrad Meyer 	/* Attach driver debugnet methods. */
39307790c8c1SConrad Meyer 	DEBUGNET_SET(ifp, bge);
3931ded66962SMark Johnston 
393295d67482SBill Paul fail:
3933e010b055SPyun YongHyeon 	if (error)
3934e010b055SPyun YongHyeon 		bge_detach(dev);
393595d67482SBill Paul 	return (error);
393695d67482SBill Paul }
393795d67482SBill Paul 
393895d67482SBill Paul static int
39393f74909aSGleb Smirnoff bge_detach(device_t dev)
394095d67482SBill Paul {
394195d67482SBill Paul 	struct bge_softc *sc;
3942fba8b109SMarcel Moolenaar 	if_t ifp;
394395d67482SBill Paul 
394495d67482SBill Paul 	sc = device_get_softc(dev);
3945fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
394695d67482SBill Paul 
394775719184SGleb Smirnoff #ifdef DEVICE_POLLING
3948fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING)
394975719184SGleb Smirnoff 		ether_poll_deregister(ifp);
395075719184SGleb Smirnoff #endif
395175719184SGleb Smirnoff 
3952e010b055SPyun YongHyeon 	if (device_is_attached(dev)) {
3953e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
39540f9bd73bSSam Leffler 		BGE_LOCK(sc);
395595d67482SBill Paul 		bge_stop(sc);
39560f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
39575dda8085SOleg Bulyzhin 		callout_drain(&sc->bge_stat_ch);
3958e010b055SPyun YongHyeon 	}
39595dda8085SOleg Bulyzhin 
3960dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3961dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
396295d67482SBill Paul 
39630aba72ddSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
396495d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
39650aba72ddSPyun YongHyeon 	else if (sc->bge_miibus != NULL) {
396695d67482SBill Paul 		bus_generic_detach(dev);
396795d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
396895d67482SBill Paul 	}
396995d67482SBill Paul 
397095d67482SBill Paul 	bge_release_resources(sc);
397195d67482SBill Paul 
397295d67482SBill Paul 	return (0);
397395d67482SBill Paul }
397495d67482SBill Paul 
397595d67482SBill Paul static void
39763f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
397795d67482SBill Paul {
397895d67482SBill Paul 	device_t dev;
397995d67482SBill Paul 
398095d67482SBill Paul 	dev = sc->bge_dev;
398195d67482SBill Paul 
3982dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
3983dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
3984dfe0df9aSPyun YongHyeon 
398595d67482SBill Paul 	if (sc->bge_intrhand != NULL)
398695d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
398795d67482SBill Paul 
3988ad4328baSMarius Strobl 	if (sc->bge_irq != NULL) {
3989724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
3990ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_irq), sc->bge_irq);
3991724bd939SJohn Polstra 		pci_release_msi(dev);
3992ad4328baSMarius Strobl 	}
399395d67482SBill Paul 
399495d67482SBill Paul 	if (sc->bge_res != NULL)
399595d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
3996ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_res), sc->bge_res);
399795d67482SBill Paul 
3998548c8f1aSPyun YongHyeon 	if (sc->bge_res2 != NULL)
3999548c8f1aSPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY,
4000ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_res2), sc->bge_res2);
4001548c8f1aSPyun YongHyeon 
4002ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
4003ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
4004ad61f896SRuslan Ermilov 
4005f41ac2beSBill Paul 	bge_dma_free(sc);
400695d67482SBill Paul 
40070f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
40080f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
400995d67482SBill Paul }
401095d67482SBill Paul 
40118cb1383cSDoug Ambrisko static int
40123f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
401395d67482SBill Paul {
401495d67482SBill Paul 	device_t dev;
4015cc085b36SPyun YongHyeon 	uint32_t cachesize, command, mac_mode, mac_mode_mask, reset, val;
40166f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
40170aaf1057SPyun YongHyeon 	uint16_t devctl;
40185fea260fSMarius Strobl 	int i;
401995d67482SBill Paul 
402095d67482SBill Paul 	dev = sc->bge_dev;
402195d67482SBill Paul 
4022cc085b36SPyun YongHyeon 	mac_mode_mask = BGE_MACMODE_HALF_DUPLEX | BGE_MACMODE_PORTMODE;
4023548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4024548c8f1aSPyun YongHyeon 		mac_mode_mask |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
4025cc085b36SPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) & mac_mode_mask;
4026cc085b36SPyun YongHyeon 
402738cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
402838cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
40296f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
40306f8718a3SScott Long 			write_op = bge_writemem_direct;
40316f8718a3SScott Long 		else
40326f8718a3SScott Long 			write_op = bge_writemem_ind;
40339ba784dbSScott Long 	} else
40346f8718a3SScott Long 		write_op = bge_writereg_ind;
40356f8718a3SScott Long 
40363dd76c98SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
40373dd76c98SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5701) {
40383dd76c98SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
40393dd76c98SPyun YongHyeon 		for (i = 0; i < 8000; i++) {
40403dd76c98SPyun YongHyeon 			if (CSR_READ_4(sc, BGE_NVRAM_SWARB) &
40413dd76c98SPyun YongHyeon 			    BGE_NVRAMSWARB_GNT1)
40423dd76c98SPyun YongHyeon 				break;
40433dd76c98SPyun YongHyeon 			DELAY(20);
40443dd76c98SPyun YongHyeon 		}
40453dd76c98SPyun YongHyeon 		if (i == 8000) {
40463dd76c98SPyun YongHyeon 			if (bootverbose)
40473dd76c98SPyun YongHyeon 				device_printf(dev, "NVRAM lock timedout!\n");
40483dd76c98SPyun YongHyeon 		}
40493dd76c98SPyun YongHyeon 	}
4050548c8f1aSPyun YongHyeon 	/* Take APE lock when performing reset. */
4051548c8f1aSPyun YongHyeon 	bge_ape_lock(sc, BGE_APE_LOCK_GRC);
4052548c8f1aSPyun YongHyeon 
405395d67482SBill Paul 	/* Save some important PCI state. */
405495d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
405595d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
405695d67482SBill Paul 
405795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
405895d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4059e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
406095d67482SBill Paul 
40616f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
40626f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
4063a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
40646f8718a3SScott Long 		if (bootverbose)
4065333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
40666f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
40676f8718a3SScott Long 	}
40686f8718a3SScott Long 
40696f8718a3SScott Long 	/*
40706f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
40716f8718a3SScott Long 	 * When firmware finishes its initialization it will
4072888b47f0SPyun YongHyeon 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
40736f8718a3SScott Long 	 */
4074888b47f0SPyun YongHyeon 	bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
40756f8718a3SScott Long 
40760c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
4077e53d81eeSPaul Saab 
4078e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4079652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4080ad49eccfSPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
4081ad49eccfSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_5717_PLUS) == 0) {
40820c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
40830c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, 0x7E2C, 0x20);
4084ad49eccfSPyun YongHyeon 		}
4085e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
4086e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
40870c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
40880c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
4089e53d81eeSPaul Saab 		}
4090e53d81eeSPaul Saab 	}
4091e53d81eeSPaul Saab 
4092df4db538SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
4093df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
4094df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
4095df4db538SPyun YongHyeon 		    val | BGE_VCPU_STATUS_DRV_RESET);
4096df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
4097df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
4098df4db538SPyun YongHyeon 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
4099df4db538SPyun YongHyeon 	}
4100df4db538SPyun YongHyeon 
410121c9e407SDavid Christensen 	/*
41026f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
41036f8718a3SScott Long 	 * powered up in D0 uninitialized.
41046f8718a3SScott Long 	 */
41055512ca01SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc) &&
41065512ca01SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
4107caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
41086f8718a3SScott Long 
410995d67482SBill Paul 	/* Issue global reset */
41106f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
411195d67482SBill Paul 
4112cc085b36SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
4113cc085b36SPyun YongHyeon 		DELAY(100 * 1000);
4114cc085b36SPyun YongHyeon 	else
411595d67482SBill Paul 		DELAY(1000);
411695d67482SBill Paul 
4117e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4118652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4119e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
4120e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
41215fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
41225fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
4123e53d81eeSPaul Saab 		}
41240aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
4125389c8bd5SGavin Atkinson 		    sc->bge_expcap + PCIER_DEVICE_CTL, 2);
41260aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
4127389c8bd5SGavin Atkinson 		devctl &= ~(PCIEM_CTL_RELAXED_ORD_ENABLE |
4128389c8bd5SGavin Atkinson 		    PCIEM_CTL_NOSNOOP_ENABLE);
4129389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_CTL,
41300aaf1057SPyun YongHyeon 		    devctl, 2);
413148630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
41320aaf1057SPyun YongHyeon 		/* Clear error status. */
4133389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_STA,
4134389c8bd5SGavin Atkinson 		    PCIEM_STA_CORRECTABLE_ERROR |
4135389c8bd5SGavin Atkinson 		    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
4136389c8bd5SGavin Atkinson 		    PCIEM_STA_UNSUPPORTED_REQ, 2);
4137e53d81eeSPaul Saab 	}
4138e53d81eeSPaul Saab 
41393f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
414095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
414195d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4142e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
4143cc085b36SPyun YongHyeon 	val = BGE_PCISTATE_ROM_ENABLE | BGE_PCISTATE_ROM_RETRY_ENABLE;
4144cc085b36SPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0 &&
4145cc085b36SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_PCIX) != 0)
4146cc085b36SPyun YongHyeon 		val |= BGE_PCISTATE_RETRY_SAME_DMA;
4147548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4148548c8f1aSPyun YongHyeon 		val |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
4149548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
4150548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
4151cc085b36SPyun YongHyeon 	pci_write_config(dev, BGE_PCI_PCISTATE, val, 4);
415295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
415395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
4154cbb2b2feSPyun YongHyeon 	/*
4155cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
4156fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
4157cbb2b2feSPyun YongHyeon 	 * read stale status block.
4158cbb2b2feSPyun YongHyeon 	 */
4159cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
4160cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
4161cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
4162cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
4163cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
4164cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
4165cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4166cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4167cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
4168cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
4169cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4170cbb2b2feSPyun YongHyeon 		}
4171cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
4172cbb2b2feSPyun YongHyeon 		    devctl, 2);
4173cbb2b2feSPyun YongHyeon 	}
417422a4ecedSMarius Strobl 	/* Re-enable MSI, if necessary, and enable the memory arbiter. */
41754c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
4176bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
4177bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
41780aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
41790aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
41800aaf1057SPyun YongHyeon 			pci_write_config(dev,
41810aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
4182bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
4183bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
4184bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
4185bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
4186bf6ef57aSJohn Polstra 		}
41874c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
41884c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
41894c0da0ffSGleb Smirnoff 	} else
4190a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4191a7b0c314SPaul Saab 
4192cc085b36SPyun YongHyeon 	/* Fix up byte swapping. */
4193cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
4194cc085b36SPyun YongHyeon 
4195cc085b36SPyun YongHyeon 	val = CSR_READ_4(sc, BGE_MAC_MODE);
4196cc085b36SPyun YongHyeon 	val = (val & ~mac_mode_mask) | mac_mode;
4197cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
4198cc085b36SPyun YongHyeon 	DELAY(40);
4199cc085b36SPyun YongHyeon 
4200548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, BGE_APE_LOCK_GRC);
4201548c8f1aSPyun YongHyeon 
420238cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
420338cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
420438cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
420538cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
420638cc658fSJohn Baldwin 				break;
420738cc658fSJohn Baldwin 			DELAY(100);
420838cc658fSJohn Baldwin 		}
420938cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
4210333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
421138cc658fSJohn Baldwin 			return (1);
421238cc658fSJohn Baldwin 		}
421338cc658fSJohn Baldwin 	} else {
421495d67482SBill Paul 		/*
42156f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
421608013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
42175fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
42185fea260fSMarius Strobl 		 * address is fitted though.
421995d67482SBill Paul 		 */
422095d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
4221d5d23857SJung-uk Kim 			DELAY(10);
4222888b47f0SPyun YongHyeon 			val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
4223888b47f0SPyun YongHyeon 			if (val == ~BGE_SRAM_FW_MB_MAGIC)
422495d67482SBill Paul 				break;
422595d67482SBill Paul 		}
422695d67482SBill Paul 
42275fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
4228333704a3SPyun YongHyeon 			device_printf(dev,
4229333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
4230333704a3SPyun YongHyeon 			    val);
4231b4a256acSPyun YongHyeon 		/* BCM57765 A0 needs additional time before accessing. */
4232b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
4233b4a256acSPyun YongHyeon 			DELAY(10 * 1000);	/* XXX */
423438cc658fSJohn Baldwin 	}
423595d67482SBill Paul 
423695d67482SBill Paul 	/*
4237da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
4238da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
4239da3003f0SBill Paul 	 * to 1.2V.
4240da3003f0SBill Paul 	 */
4241652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
4242652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
42435fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
42445fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
42455fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
4246da3003f0SBill Paul 	}
4247da3003f0SBill Paul 
4248e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4249652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
4250b4a256acSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc) &&
4251a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
4252a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
4253a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
42545fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
42555fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
4256e53d81eeSPaul Saab 	}
42578cb1383cSDoug Ambrisko 
425850515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
425950515680SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
426050515680SPyun YongHyeon 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
426150515680SPyun YongHyeon 
42628cb1383cSDoug Ambrisko 	return (0);
426395d67482SBill Paul }
426495d67482SBill Paul 
4265e0b7b101SPyun YongHyeon static __inline void
4266e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
4267e0b7b101SPyun YongHyeon {
4268e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
4269e0b7b101SPyun YongHyeon 
4270e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
4271e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
4272e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
4273e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4274e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
4275e0b7b101SPyun YongHyeon }
4276e0b7b101SPyun YongHyeon 
4277e0b7b101SPyun YongHyeon static __inline void
4278e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
4279e0b7b101SPyun YongHyeon {
4280e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
4281e0b7b101SPyun YongHyeon 
4282e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
4283e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
4284e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
4285e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
4286e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
4287e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
4288e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4289e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
4290e0b7b101SPyun YongHyeon }
4291e0b7b101SPyun YongHyeon 
429295d67482SBill Paul /*
429395d67482SBill Paul  * Frame reception handling. This is called if there's a frame
429495d67482SBill Paul  * on the receive return list.
429595d67482SBill Paul  *
429695d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
42971be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
429895d67482SBill Paul  * 2) the frame is from the standard receive ring
429995d67482SBill Paul  */
430095d67482SBill Paul 
43011abcdbd1SAttilio Rao static int
4302dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
430395d67482SBill Paul {
4304fba8b109SMarcel Moolenaar 	if_t ifp;
43051abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
4306b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
430795d67482SBill Paul 
43087f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
43090f9bd73bSSam Leffler 
43103f74909aSGleb Smirnoff 	/* Nothing to do. */
43117f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
43121abcdbd1SAttilio Rao 		return (rx_npkts);
4313cfcb5025SOleg Bulyzhin 
4314fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
431595d67482SBill Paul 
4316f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
4317e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
4318f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
431915eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
4320f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
4321fba8b109SMarcel Moolenaar 	    if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
4322fba8b109SMarcel Moolenaar 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))
4323f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
432415eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
4325f41ac2beSBill Paul 
43267f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
432795d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
43283f74909aSGleb Smirnoff 		uint32_t		rxidx;
432995d67482SBill Paul 		struct mbuf		*m = NULL;
43303f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
433195d67482SBill Paul 		int			have_tag = 0;
433295d67482SBill Paul 
433375719184SGleb Smirnoff #ifdef DEVICE_POLLING
4334fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_POLLING) {
433575719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
433675719184SGleb Smirnoff 				break;
433775719184SGleb Smirnoff 			sc->rxcycles--;
433875719184SGleb Smirnoff 		}
433975719184SGleb Smirnoff #endif
434075719184SGleb Smirnoff 
43417f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
434295d67482SBill Paul 
434395d67482SBill Paul 		rxidx = cur_rx->bge_idx;
43447f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
434595d67482SBill Paul 
4346fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING &&
4347cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
434895d67482SBill Paul 			have_tag = 1;
434995d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
435095d67482SBill Paul 		}
435195d67482SBill Paul 
435295d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
435395d67482SBill Paul 			jumbocnt++;
4354943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
435595d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4356e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
435795d67482SBill Paul 				continue;
435895d67482SBill Paul 			}
4359943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
4360e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
4361df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
436295d67482SBill Paul 				continue;
436395d67482SBill Paul 			}
436403e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
436595d67482SBill Paul 		} else {
436695d67482SBill Paul 			stdcnt++;
4367e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
436895d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4369e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
437095d67482SBill Paul 				continue;
437195d67482SBill Paul 			}
4372943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
4373e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
4374df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
437595d67482SBill Paul 				continue;
437695d67482SBill Paul 			}
437703e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
437895d67482SBill Paul 		}
437995d67482SBill Paul 
4380df360178SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
4381e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
4382e255b776SJohn Polstra 		/*
4383e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
4384e65bed95SPyun YongHyeon 		 * the payload is aligned.
4385e255b776SJohn Polstra 		 */
4386652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
4387e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
4388e255b776SJohn Polstra 			    cur_rx->bge_len);
4389e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
4390e255b776SJohn Polstra 		}
4391e255b776SJohn Polstra #endif
4392473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
439395d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
439495d67482SBill Paul 
4395fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_RXCSUM)
43961108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
439795d67482SBill Paul 
439895d67482SBill Paul 		/*
4399673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
4400673d9191SSam Leffler 		 * attach that information to the packet.
440195d67482SBill Paul 		 */
4402d147662cSGleb Smirnoff 		if (have_tag) {
440378ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
440478ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
4405d147662cSGleb Smirnoff 		}
440695d67482SBill Paul 
4407dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
44080f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
4409fba8b109SMarcel Moolenaar 			if_input(ifp, m);
44100f9bd73bSSam Leffler 			BGE_LOCK(sc);
4411dfe0df9aSPyun YongHyeon 		} else
4412fba8b109SMarcel Moolenaar 			if_input(ifp, m);
4413d4da719cSAttilio Rao 		rx_npkts++;
441425e13e68SXin LI 
4415fba8b109SMarcel Moolenaar 		if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
44168cf7d13dSAttilio Rao 			return (rx_npkts);
441795d67482SBill Paul 	}
441895d67482SBill Paul 
441915eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
442015eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
4421e65bed95SPyun YongHyeon 	if (stdcnt > 0)
4422f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
4423e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
44244c0da0ffSGleb Smirnoff 
4425c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
4426f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
44274c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
4428f41ac2beSBill Paul 
44297f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
443038cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
443195d67482SBill Paul 	if (stdcnt)
4432767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
4433767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
443495d67482SBill Paul 	if (jumbocnt)
4435767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
4436767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
4437f5a034f9SPyun YongHyeon #ifdef notyet
4438f5a034f9SPyun YongHyeon 	/*
4439f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
4440f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
4441f5a034f9SPyun YongHyeon 	 */
4442f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
4443fba8b109SMarcel Moolenaar 		if_incierrors(ifp, CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS));
4444f5a034f9SPyun YongHyeon #endif
44451abcdbd1SAttilio Rao 	return (rx_npkts);
444695d67482SBill Paul }
444795d67482SBill Paul 
444895d67482SBill Paul static void
44491108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
44501108273aSPyun YongHyeon {
44511108273aSPyun YongHyeon 
44521108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
44531108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
44541108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44551108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44561108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
44571108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
44581108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44591108273aSPyun YongHyeon 			}
44601108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
44611108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
44621108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
44631108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44641108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
44651108273aSPyun YongHyeon 			}
44661108273aSPyun YongHyeon 		}
44671108273aSPyun YongHyeon 	} else {
44681108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44691108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44701108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
44711108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44721108273aSPyun YongHyeon 		}
44731108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
44741108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
44751108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
44761108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
44771108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44781108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
44791108273aSPyun YongHyeon 		}
44801108273aSPyun YongHyeon 	}
44811108273aSPyun YongHyeon }
44821108273aSPyun YongHyeon 
44831108273aSPyun YongHyeon static void
4484b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
448595d67482SBill Paul {
448695a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
4487fba8b109SMarcel Moolenaar 	if_t ifp;
448895d67482SBill Paul 
44890f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
44900f9bd73bSSam Leffler 
44913f74909aSGleb Smirnoff 	/* Nothing to do. */
4492b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
4493cfcb5025SOleg Bulyzhin 		return;
4494cfcb5025SOleg Bulyzhin 
4495fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
449695d67482SBill Paul 
4497e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
44985c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
449995d67482SBill Paul 	/*
450095d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
450195d67482SBill Paul 	 * frames that have been sent.
450295d67482SBill Paul 	 */
4503b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
450495a0a340SPyun YongHyeon 		uint32_t		idx;
450595d67482SBill Paul 
450695d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
4507f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
450895d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
4509df360178SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
451095d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
45110ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
4512e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
4513e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
45140ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
4515f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
4516e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
4517e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
451895d67482SBill Paul 		}
451995d67482SBill Paul 		sc->bge_txcnt--;
452095d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
452195d67482SBill Paul 	}
452295d67482SBill Paul 
4523fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
45245b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
45255b01e77cSBruce Evans 		sc->bge_timer = 0;
452695d67482SBill Paul }
452795d67482SBill Paul 
452875719184SGleb Smirnoff #ifdef DEVICE_POLLING
45291abcdbd1SAttilio Rao static int
4530fba8b109SMarcel Moolenaar bge_poll(if_t ifp, enum poll_cmd cmd, int count)
453175719184SGleb Smirnoff {
4532fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
4533b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4534366454f2SOleg Bulyzhin 	uint32_t statusword;
45351abcdbd1SAttilio Rao 	int rx_npkts = 0;
453675719184SGleb Smirnoff 
45373f74909aSGleb Smirnoff 	BGE_LOCK(sc);
4538fba8b109SMarcel Moolenaar 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
45393f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
45401abcdbd1SAttilio Rao 		return (rx_npkts);
45413f74909aSGleb Smirnoff 	}
454275719184SGleb Smirnoff 
4543dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4544b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4545b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
45462246e8c6SPyun YongHyeon 	/* Fetch updates from the status block. */
4547b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4548b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4549dab5cd05SOleg Bulyzhin 
4550175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
45512246e8c6SPyun YongHyeon 	/* Clear the status so the next pass only sees the changes. */
4552175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4553dab5cd05SOleg Bulyzhin 
4554dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4555b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4556b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4557366454f2SOleg Bulyzhin 
45580c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
4559366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
4560366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
4561366454f2SOleg Bulyzhin 
4562366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
4563366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
45644c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4565652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
4566366454f2SOleg Bulyzhin 			bge_link_upd(sc);
4567366454f2SOleg Bulyzhin 
4568366454f2SOleg Bulyzhin 	sc->rxcycles = count;
4569dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
4570fba8b109SMarcel Moolenaar 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
457125e13e68SXin LI 		BGE_UNLOCK(sc);
45728cf7d13dSAttilio Rao 		return (rx_npkts);
457325e13e68SXin LI 	}
4574b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
4575fba8b109SMarcel Moolenaar 	if (!if_sendq_empty(ifp))
4576366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
45773f74909aSGleb Smirnoff 
45783f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
45791abcdbd1SAttilio Rao 	return (rx_npkts);
458075719184SGleb Smirnoff }
458175719184SGleb Smirnoff #endif /* DEVICE_POLLING */
458275719184SGleb Smirnoff 
4583dfe0df9aSPyun YongHyeon static int
4584dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
4585dfe0df9aSPyun YongHyeon {
4586dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4587dfe0df9aSPyun YongHyeon 
4588dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4589dfe0df9aSPyun YongHyeon 	/*
4590dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
4591dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
4592dfe0df9aSPyun YongHyeon 	 */
4593dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4594dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
4595dfe0df9aSPyun YongHyeon }
4596dfe0df9aSPyun YongHyeon 
4597dfe0df9aSPyun YongHyeon static void
4598dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
4599dfe0df9aSPyun YongHyeon {
4600dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4601fba8b109SMarcel Moolenaar 	if_t ifp;
46021108273aSPyun YongHyeon 	uint32_t status, status_tag;
4603dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4604dfe0df9aSPyun YongHyeon 
4605dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4606dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
4607dfe0df9aSPyun YongHyeon 
460866151edfSPyun YongHyeon 	BGE_LOCK(sc);
4609fba8b109SMarcel Moolenaar 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
461066151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4611dfe0df9aSPyun YongHyeon 		return;
461266151edfSPyun YongHyeon 	}
4613dfe0df9aSPyun YongHyeon 
4614dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
4615dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4616dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4617dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4618dfe0df9aSPyun YongHyeon 
46192246e8c6SPyun YongHyeon 	/* Save producer/consumer indices. */
4620dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4621dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4622dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
46231108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
46242246e8c6SPyun YongHyeon 	/* Dirty the status flag. */
4625dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4626dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4627dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4628dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
46291108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
46301108273aSPyun YongHyeon 		status_tag = 0;
463166151edfSPyun YongHyeon 
463266151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
463366151edfSPyun YongHyeon 		bge_link_upd(sc);
463466151edfSPyun YongHyeon 
4635dfe0df9aSPyun YongHyeon 	/* Let controller work. */
46361108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4637dfe0df9aSPyun YongHyeon 
4638fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING &&
463966151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
4640dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
464166151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4642dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
464366151edfSPyun YongHyeon 		BGE_LOCK(sc);
4644dfe0df9aSPyun YongHyeon 	}
4645fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4646dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
4647dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
4648fba8b109SMarcel Moolenaar 		if (!if_sendq_empty(ifp))
4649dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
4650dfe0df9aSPyun YongHyeon 	}
465166151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
4652dfe0df9aSPyun YongHyeon }
4653dfe0df9aSPyun YongHyeon 
465495d67482SBill Paul static void
46553f74909aSGleb Smirnoff bge_intr(void *xsc)
465695d67482SBill Paul {
465795d67482SBill Paul 	struct bge_softc *sc;
4658fba8b109SMarcel Moolenaar 	if_t ifp;
4659dab5cd05SOleg Bulyzhin 	uint32_t statusword;
4660b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
466195d67482SBill Paul 
466295d67482SBill Paul 	sc = xsc;
4663f41ac2beSBill Paul 
46640f9bd73bSSam Leffler 	BGE_LOCK(sc);
46650f9bd73bSSam Leffler 
4666dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
4667dab5cd05SOleg Bulyzhin 
466875719184SGleb Smirnoff #ifdef DEVICE_POLLING
4669fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
467075719184SGleb Smirnoff 		BGE_UNLOCK(sc);
467175719184SGleb Smirnoff 		return;
467275719184SGleb Smirnoff 	}
467375719184SGleb Smirnoff #endif
467475719184SGleb Smirnoff 
4675f30cbfc6SScott Long 	/*
4676b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
4677b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
4678b848e032SBruce Evans 	 * our current organization this just gives complications and
4679b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
4680b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
4681b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
4682b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
4683b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
4684b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
4685b848e032SBruce Evans 	 *
4686b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
4687b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
4688b848e032SBruce Evans 	 * changing later because it is more efficient to get another
4689b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
4690b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
4691b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
4692b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
4693b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
4694b848e032SBruce Evans 	 */
469538cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4696b848e032SBruce Evans 
4697f584dfd1SPyun YongHyeon 	/*
4698f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
4699f584dfd1SPyun YongHyeon 	 */
4700f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4701f584dfd1SPyun YongHyeon 
4702f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
4703f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4704f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4705f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4706f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4707f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4708f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4709f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4710f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4711f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4712f584dfd1SPyun YongHyeon 
47131f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
47144c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4715f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
4716dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
471795d67482SBill Paul 
4718fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
47193f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
4720dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
472125e13e68SXin LI 	}
472295d67482SBill Paul 
4723fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
47243f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
4725b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
472695d67482SBill Paul 	}
472795d67482SBill Paul 
4728fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING &&
4729fba8b109SMarcel Moolenaar 	    !if_sendq_empty(ifp))
47300f9bd73bSSam Leffler 		bge_start_locked(ifp);
47310f9bd73bSSam Leffler 
47320f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
473395d67482SBill Paul }
473495d67482SBill Paul 
473595d67482SBill Paul static void
47368cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
47378cb1383cSDoug Ambrisko {
47388cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
47398cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
47408cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
47418cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
47428cb1383cSDoug Ambrisko 		else {
4743899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
4744888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
47453c201200SPyun YongHyeon 			    BGE_FW_CMD_DRV_ALIVE);
4746888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4747941a6e13SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4748941a6e13SPyun YongHyeon 			    BGE_FW_HB_TIMEOUT_SEC);
47493fed2d5dSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
47509931ba85SPyun YongHyeon 			    CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
47519931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT);
47528cb1383cSDoug Ambrisko 		}
47538cb1383cSDoug Ambrisko 	}
47548cb1383cSDoug Ambrisko }
47558cb1383cSDoug Ambrisko 
47568cb1383cSDoug Ambrisko static void
4757b74e67fbSGleb Smirnoff bge_tick(void *xsc)
47580f9bd73bSSam Leffler {
4759b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
476095d67482SBill Paul 	struct mii_data *mii = NULL;
476195d67482SBill Paul 
47620f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
476395d67482SBill Paul 
47645dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
47655dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
47665dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
47675dda8085SOleg Bulyzhin 		return;
47685dda8085SOleg Bulyzhin 
47697ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
47700434d1b8SBill Paul 		bge_stats_update_regs(sc);
47710434d1b8SBill Paul 	else
477295d67482SBill Paul 		bge_stats_update(sc);
477395d67482SBill Paul 
4774548c8f1aSPyun YongHyeon 	/* XXX Add APE heartbeat check here? */
4775548c8f1aSPyun YongHyeon 
4776652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
477795d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
477882b67c01SOleg Bulyzhin 		/*
477982b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
478082b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
478182b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
478282b67c01SOleg Bulyzhin 		 */
478382b67c01SOleg Bulyzhin 		if (!sc->bge_link)
478495d67482SBill Paul 			mii_tick(mii);
47857b97099dSOleg Bulyzhin 	} else {
47867b97099dSOleg Bulyzhin 		/*
47877b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
47887b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
47897b97099dSOleg Bulyzhin 		 * and trigger interrupt.
47907b97099dSOleg Bulyzhin 		 */
47917b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
47923f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
479359578ee0SSergey Kandaurov 		if (!(if_getcapenable(sc->bge_ifp) & IFCAP_POLLING))
47947b97099dSOleg Bulyzhin #endif
47957b97099dSOleg Bulyzhin 		{
47967b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
47974f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
47984f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
47997b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
48004f0794ffSBjoern A. Zeeb 		else
48014f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
48027b97099dSOleg Bulyzhin 		}
4803dab5cd05SOleg Bulyzhin 	}
480495d67482SBill Paul 
48058cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4806b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
48078cb1383cSDoug Ambrisko 
4808dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
480995d67482SBill Paul }
481095d67482SBill Paul 
481195d67482SBill Paul static void
48123f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
48130434d1b8SBill Paul {
48142280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
481529b44b09SPyun YongHyeon 	uint32_t val;
48160434d1b8SBill Paul 
48172280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
48180434d1b8SBill Paul 
48192280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
48202280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
48212280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
48222280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
48232280c16bSPyun YongHyeon 	stats->outXonSent +=
48242280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
48252280c16bSPyun YongHyeon 	stats->outXoffSent +=
48262280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
48272280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
48282280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
48292280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
48302280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
48312280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
48322280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
48332280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
48342280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
48352280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
48362280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
48372280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
48382280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
48392280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
48402280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
48412280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
48422280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
48432280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
48442280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
48457e6e2507SJung-uk Kim 
48462280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
48472280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
48482280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
48492280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
48502280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
48512280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
48522280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
48532280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
48542280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
48552280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
48562280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
48572280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
48582280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
48592280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
48602280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
48612280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
48622280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
48632280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
48642280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
48652280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
48662280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
48672280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
48682280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
48692280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
48702280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
48712280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
48722280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
48732280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
48742280c16bSPyun YongHyeon 
48752280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
48762280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
48772280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
48782280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
48792280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
48802280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
48812280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
48822280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4883f78094a5SPyun YongHyeon 	/*
4884f78094a5SPyun YongHyeon 	 * XXX
4885f78094a5SPyun YongHyeon 	 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4886f78094a5SPyun YongHyeon 	 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4887f78094a5SPyun YongHyeon 	 * includes number of unwanted multicast frames.  This comes
4888f78094a5SPyun YongHyeon 	 * from silicon bug and known workaround to get rough(not
4889f78094a5SPyun YongHyeon 	 * exact) counter is to enable interrupt on MBUF low water
4890f78094a5SPyun YongHyeon 	 * attention.  This can be accomplished by setting
4891f78094a5SPyun YongHyeon 	 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4892f78094a5SPyun YongHyeon 	 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4893f78094a5SPyun YongHyeon 	 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4894f78094a5SPyun YongHyeon 	 * However that change would generate more interrupts and
4895f78094a5SPyun YongHyeon 	 * there are still possibilities of losing multiple frames
4896f78094a5SPyun YongHyeon 	 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4897f78094a5SPyun YongHyeon 	 * Given that the workaround still would not get correct
4898f78094a5SPyun YongHyeon 	 * counter I don't think it's worth to implement it.  So
4899f78094a5SPyun YongHyeon 	 * ignore reading the counter on controllers that have the
4900f78094a5SPyun YongHyeon 	 * silicon bug.
4901f78094a5SPyun YongHyeon 	 */
4902f78094a5SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4903f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4904f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
49052280c16bSPyun YongHyeon 		stats->InputDiscards +=
49062280c16bSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
49072280c16bSPyun YongHyeon 	stats->InputErrors +=
49082280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
49092280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
49102280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
49112280c16bSPyun YongHyeon 
491229b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
491329b44b09SPyun YongHyeon 		/*
491429b44b09SPyun YongHyeon 		 * If controller transmitted more than BGE_NUM_RDMA_CHANNELS
491529b44b09SPyun YongHyeon 		 * frames, it's safe to disable workaround for DMA engine's
491629b44b09SPyun YongHyeon 		 * miscalculation of TXMBUF space.
491729b44b09SPyun YongHyeon 		 */
491829b44b09SPyun YongHyeon 		if (stats->ifHCOutUcastPkts + stats->ifHCOutMulticastPkts +
491929b44b09SPyun YongHyeon 		    stats->ifHCOutBroadcastPkts > BGE_NUM_RDMA_CHANNELS) {
492029b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
492129b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
492229b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5719;
492329b44b09SPyun YongHyeon 			else
492429b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5720;
492529b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
492629b44b09SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_RDMA_BUG;
492729b44b09SPyun YongHyeon 		}
492829b44b09SPyun YongHyeon 	}
49292280c16bSPyun YongHyeon }
49302280c16bSPyun YongHyeon 
49312280c16bSPyun YongHyeon static void
49322280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
49332280c16bSPyun YongHyeon {
49342280c16bSPyun YongHyeon 
49352280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
49362280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
49372280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
49382280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
49392280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
49402280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
49412280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
49422280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
49432280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
49442280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
49452280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
49462280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
49472280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
49482280c16bSPyun YongHyeon 
49492280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
49502280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
49512280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
49522280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
49532280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
49542280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
49552280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
49562280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
49572280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
49582280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
49592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
49602280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
49612280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
49622280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
49632280c16bSPyun YongHyeon 
49642280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
49652280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
49662280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
49672280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
49682280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
49692280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
49702280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
49710434d1b8SBill Paul }
49720434d1b8SBill Paul 
49730434d1b8SBill Paul static void
49743f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
497595d67482SBill Paul {
4976fba8b109SMarcel Moolenaar 	if_t ifp;
4977e907febfSPyun YongHyeon 	bus_size_t stats;
49787e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
497995d67482SBill Paul 
4980fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
498195d67482SBill Paul 
4982e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
4983e907febfSPyun YongHyeon 
4984e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
4985e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
498695d67482SBill Paul 
49878634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
4988df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cnt - sc->bge_tx_collisions);
49896fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
49906fb34dd2SOleg Bulyzhin 
499137ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, nicNoMoreRxBDs.bge_addr_lo);
4992df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_nobds);
499337ee7cc7SPyun YongHyeon 	sc->bge_rx_nobds = cnt;
499437ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, ifInErrors.bge_addr_lo);
4995df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_inerrs);
499637ee7cc7SPyun YongHyeon 	sc->bge_rx_inerrs = cnt;
49976fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
4998df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_discards);
49996fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
50006fb34dd2SOleg Bulyzhin 
50016fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
5002df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, cnt - sc->bge_tx_discards);
50036fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
500495d67482SBill Paul 
5005e907febfSPyun YongHyeon #undef	READ_STAT
500695d67482SBill Paul }
500795d67482SBill Paul 
500895d67482SBill Paul /*
5009d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
5010d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
5011d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
5012d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
5013d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
5014d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
5015d375e524SGleb Smirnoff  */
5016d375e524SGleb Smirnoff static __inline int
5017d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
5018d375e524SGleb Smirnoff {
5019d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
5020d375e524SGleb Smirnoff 	struct mbuf *last;
5021d375e524SGleb Smirnoff 
5022d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
5023d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
5024d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
5025d375e524SGleb Smirnoff 		last = m;
5026d375e524SGleb Smirnoff 	} else {
5027d375e524SGleb Smirnoff 		/*
5028d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
5029d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
5030d375e524SGleb Smirnoff 		 */
5031d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
5032d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
5033d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
5034d375e524SGleb Smirnoff 			struct mbuf *n;
5035d375e524SGleb Smirnoff 
5036c6499eccSGleb Smirnoff 			MGET(n, M_NOWAIT, MT_DATA);
5037d375e524SGleb Smirnoff 			if (n == NULL)
5038d375e524SGleb Smirnoff 				return (ENOBUFS);
5039d375e524SGleb Smirnoff 			n->m_len = 0;
5040d375e524SGleb Smirnoff 			last->m_next = n;
5041d375e524SGleb Smirnoff 			last = n;
5042d375e524SGleb Smirnoff 		}
5043d375e524SGleb Smirnoff 	}
5044d375e524SGleb Smirnoff 
5045d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
5046d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
5047d375e524SGleb Smirnoff 	last->m_len += padlen;
5048d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
5049d375e524SGleb Smirnoff 
5050d375e524SGleb Smirnoff 	return (0);
5051d375e524SGleb Smirnoff }
5052d375e524SGleb Smirnoff 
5053ca3f1187SPyun YongHyeon static struct mbuf *
5054d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
5055d598b626SPyun YongHyeon {
5056d598b626SPyun YongHyeon 	struct mbuf *n;
5057d598b626SPyun YongHyeon 	int found;
5058d598b626SPyun YongHyeon 
5059d598b626SPyun YongHyeon 	/*
5060d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
5061d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
5062d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
5063d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
5064d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
5065d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
5066d598b626SPyun YongHyeon 	 */
5067d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
5068d598b626SPyun YongHyeon 		if (n->m_len < 8) {
5069d598b626SPyun YongHyeon 			found++;
5070d598b626SPyun YongHyeon 			if (found > 1)
5071d598b626SPyun YongHyeon 				break;
5072d598b626SPyun YongHyeon 			continue;
5073d598b626SPyun YongHyeon 		}
5074d598b626SPyun YongHyeon 		found = 0;
5075d598b626SPyun YongHyeon 	}
5076d598b626SPyun YongHyeon 
5077d598b626SPyun YongHyeon 	if (found > 1) {
5078c6499eccSGleb Smirnoff 		n = m_defrag(m, M_NOWAIT);
5079d598b626SPyun YongHyeon 		if (n == NULL)
5080d598b626SPyun YongHyeon 			m_freem(m);
5081d598b626SPyun YongHyeon 	} else
5082d598b626SPyun YongHyeon 		n = m;
5083d598b626SPyun YongHyeon 	return (n);
5084d598b626SPyun YongHyeon }
5085d598b626SPyun YongHyeon 
5086d598b626SPyun YongHyeon static struct mbuf *
50871108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
50881108273aSPyun YongHyeon     uint16_t *flags)
5089ca3f1187SPyun YongHyeon {
5090ca3f1187SPyun YongHyeon 	struct ip *ip;
5091ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
5092ca3f1187SPyun YongHyeon 	struct mbuf *n;
5093ca3f1187SPyun YongHyeon 	uint16_t hlen;
50945b355c4fSPyun YongHyeon 	uint32_t poff;
5095ca3f1187SPyun YongHyeon 
5096ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
5097ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
5098c6499eccSGleb Smirnoff 		n = m_dup(m, M_NOWAIT);
5099ca3f1187SPyun YongHyeon 		m_freem(m);
5100ca3f1187SPyun YongHyeon 		if (n == NULL)
5101ca3f1187SPyun YongHyeon 			return (NULL);
5102ca3f1187SPyun YongHyeon 		m = n;
5103ca3f1187SPyun YongHyeon 	}
51045b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
5105ca3f1187SPyun YongHyeon 	if (m == NULL)
5106ca3f1187SPyun YongHyeon 		return (NULL);
51075b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
51085b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
5109ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
5110ca3f1187SPyun YongHyeon 	if (m == NULL)
5111ca3f1187SPyun YongHyeon 		return (NULL);
5112ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
51135b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
5114ca3f1187SPyun YongHyeon 	if (m == NULL)
5115ca3f1187SPyun YongHyeon 		return (NULL);
5116ca3f1187SPyun YongHyeon 	/*
5117ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
5118ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
5119ca3f1187SPyun YongHyeon 	 */
5120ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
512196486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
5122ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
5123ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
5124ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
512596486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
5126ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
5127ca3f1187SPyun YongHyeon 	/*
5128ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
5129ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
5130ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
51311108273aSPyun YongHyeon 	 * we only support hardware based TSO.
5132ca3f1187SPyun YongHyeon 	 */
51331108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
5134ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
51351108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
51361108273aSPyun YongHyeon 		/*
51371108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
51381108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
51391108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
51401108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
51411108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
51421108273aSPyun YongHyeon 		 * frames are supported.
51431108273aSPyun YongHyeon 		 */
51441108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
51451108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
51461108273aSPyun YongHyeon 	} else {
51471108273aSPyun YongHyeon 		/*
51481108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
51491108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
51501108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
51511108273aSPyun YongHyeon 		 * supported.
51521108273aSPyun YongHyeon 		 */
5153ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
51541108273aSPyun YongHyeon 	}
5155ca3f1187SPyun YongHyeon 	return (m);
5156ca3f1187SPyun YongHyeon }
5157ca3f1187SPyun YongHyeon 
5158d375e524SGleb Smirnoff /*
515995d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
516095d67482SBill Paul  * pointers to descriptors.
516195d67482SBill Paul  */
516295d67482SBill Paul static int
5163676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
516495d67482SBill Paul {
51657e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
5166f41ac2beSBill Paul 	bus_dmamap_t		map;
5167676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
5168676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
51697e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
5170ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
51717e27542aSGleb Smirnoff 	int			nsegs, i, error;
517295d67482SBill Paul 
51736909dc43SGleb Smirnoff 	csum_flags = 0;
5174ca3f1187SPyun YongHyeon 	mss = 0;
5175ca3f1187SPyun YongHyeon 	vlan_tag = 0;
5176d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
5177d598b626SPyun YongHyeon 	    m->m_next != NULL) {
5178d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
5179d598b626SPyun YongHyeon 		if (*m_head == NULL)
5180d598b626SPyun YongHyeon 			return (ENOBUFS);
5181d598b626SPyun YongHyeon 		m = *m_head;
5182d598b626SPyun YongHyeon 	}
5183ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
51841108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
5185ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
5186ca3f1187SPyun YongHyeon 			return (ENOBUFS);
5187ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
5188ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
518935f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
51906909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
51916909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
51926909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
51936909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
51946909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
51956909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
51966909dc43SGleb Smirnoff 				m_freem(m);
51976909dc43SGleb Smirnoff 				*m_head = NULL;
51986909dc43SGleb Smirnoff 				return (error);
51996909dc43SGleb Smirnoff 			}
52006909dc43SGleb Smirnoff 		}
52016909dc43SGleb Smirnoff 	}
52026909dc43SGleb Smirnoff 
52031108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
52041108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
52051108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
52061108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
52071108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
5208beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
5209d94f2b85SPyun YongHyeon 			/*
5210d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
5211d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
5212d94f2b85SPyun YongHyeon 			 * DMA read operation.
5213d94f2b85SPyun YongHyeon 			 */
5214beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
5215c6499eccSGleb Smirnoff 				m = m_defrag(m, M_NOWAIT);
5216d94f2b85SPyun YongHyeon 			else
5217c6499eccSGleb Smirnoff 				m = m_collapse(m, M_NOWAIT,
52181108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
5219261f04d6SPyun YongHyeon 			if (m == NULL)
5220261f04d6SPyun YongHyeon 				m = *m_head;
5221d94f2b85SPyun YongHyeon 			*m_head = m;
5222d94f2b85SPyun YongHyeon 		}
52231108273aSPyun YongHyeon 	}
5224d94f2b85SPyun YongHyeon 
52257e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
52260ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
5227676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
52287e27542aSGleb Smirnoff 	if (error == EFBIG) {
5229c6499eccSGleb Smirnoff 		m = m_collapse(m, M_NOWAIT, BGE_NSEG_NEW);
5230676ad2c9SGleb Smirnoff 		if (m == NULL) {
5231676ad2c9SGleb Smirnoff 			m_freem(*m_head);
5232676ad2c9SGleb Smirnoff 			*m_head = NULL;
52337e27542aSGleb Smirnoff 			return (ENOBUFS);
52347e27542aSGleb Smirnoff 		}
5235676ad2c9SGleb Smirnoff 		*m_head = m;
52360ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
52370ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
5238676ad2c9SGleb Smirnoff 		if (error) {
5239676ad2c9SGleb Smirnoff 			m_freem(m);
5240676ad2c9SGleb Smirnoff 			*m_head = NULL;
52417e27542aSGleb Smirnoff 			return (error);
52427e27542aSGleb Smirnoff 		}
5243676ad2c9SGleb Smirnoff 	} else if (error != 0)
5244676ad2c9SGleb Smirnoff 		return (error);
52457e27542aSGleb Smirnoff 
5246167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
5247167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
52480ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
524995d67482SBill Paul 		return (ENOBUFS);
52507e27542aSGleb Smirnoff 	}
52517e27542aSGleb Smirnoff 
52520ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
5253e65bed95SPyun YongHyeon 
5254ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
5255ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
5256ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
5257ca3f1187SPyun YongHyeon 	}
5258b77d3a3bSPyun YongHyeon 
5259b77d3a3bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5762 &&
5260b77d3a3bSPyun YongHyeon 	    (m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
5261b77d3a3bSPyun YongHyeon 		/*
5262b77d3a3bSPyun YongHyeon 		 * 5725 family of devices corrupts TSO packets when TSO DMA
5263b77d3a3bSPyun YongHyeon 		 * buffers cross into regions which are within MSS bytes of
5264b77d3a3bSPyun YongHyeon 		 * a 4GB boundary.  If we encounter the condition, drop the
5265b77d3a3bSPyun YongHyeon 		 * packet.
5266b77d3a3bSPyun YongHyeon 		 */
5267b77d3a3bSPyun YongHyeon 		for (i = 0; ; i++) {
5268b77d3a3bSPyun YongHyeon 			d = &sc->bge_ldata.bge_tx_ring[idx];
5269b77d3a3bSPyun YongHyeon 			d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
5270b77d3a3bSPyun YongHyeon 			d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
5271b77d3a3bSPyun YongHyeon 			d->bge_len = segs[i].ds_len;
5272b77d3a3bSPyun YongHyeon 			if (d->bge_addr.bge_addr_lo + segs[i].ds_len + mss <
5273b77d3a3bSPyun YongHyeon 			    d->bge_addr.bge_addr_lo)
5274b77d3a3bSPyun YongHyeon 				break;
5275b77d3a3bSPyun YongHyeon 			d->bge_flags = csum_flags;
5276b77d3a3bSPyun YongHyeon 			d->bge_vlan_tag = vlan_tag;
5277b77d3a3bSPyun YongHyeon 			d->bge_mss = mss;
5278b77d3a3bSPyun YongHyeon 			if (i == nsegs - 1)
5279b77d3a3bSPyun YongHyeon 				break;
5280b77d3a3bSPyun YongHyeon 			BGE_INC(idx, BGE_TX_RING_CNT);
5281b77d3a3bSPyun YongHyeon 		}
5282b77d3a3bSPyun YongHyeon 		if (i != nsegs - 1) {
5283b77d3a3bSPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map,
5284b77d3a3bSPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
5285b77d3a3bSPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
5286b77d3a3bSPyun YongHyeon 			m_freem(*m_head);
5287b77d3a3bSPyun YongHyeon 			*m_head = NULL;
5288b77d3a3bSPyun YongHyeon 			return (EIO);
5289b77d3a3bSPyun YongHyeon 		}
5290b77d3a3bSPyun YongHyeon 	} else {
52917e27542aSGleb Smirnoff 		for (i = 0; ; i++) {
52927e27542aSGleb Smirnoff 			d = &sc->bge_ldata.bge_tx_ring[idx];
52937e27542aSGleb Smirnoff 			d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
52947e27542aSGleb Smirnoff 			d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
52957e27542aSGleb Smirnoff 			d->bge_len = segs[i].ds_len;
52967e27542aSGleb Smirnoff 			d->bge_flags = csum_flags;
5297ca3f1187SPyun YongHyeon 			d->bge_vlan_tag = vlan_tag;
5298ca3f1187SPyun YongHyeon 			d->bge_mss = mss;
52997e27542aSGleb Smirnoff 			if (i == nsegs - 1)
53007e27542aSGleb Smirnoff 				break;
53017e27542aSGleb Smirnoff 			BGE_INC(idx, BGE_TX_RING_CNT);
53027e27542aSGleb Smirnoff 		}
5303b77d3a3bSPyun YongHyeon 	}
53047e27542aSGleb Smirnoff 
53057e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
53067e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
5307676ad2c9SGleb Smirnoff 
5308f41ac2beSBill Paul 	/*
5309f41ac2beSBill Paul 	 * Insure that the map for this transmission
5310f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
5311f41ac2beSBill Paul 	 * in this chain.
5312f41ac2beSBill Paul 	 */
53137e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
53147e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
5315676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
53167e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
531795d67482SBill Paul 
53187e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
53197e27542aSGleb Smirnoff 	*txidx = idx;
532095d67482SBill Paul 
532195d67482SBill Paul 	return (0);
532295d67482SBill Paul }
532395d67482SBill Paul 
532495d67482SBill Paul /*
532595d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
532695d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
532795d67482SBill Paul  */
532895d67482SBill Paul static void
5329fba8b109SMarcel Moolenaar bge_start_locked(if_t ifp)
533095d67482SBill Paul {
533195d67482SBill Paul 	struct bge_softc *sc;
5332167fdb62SPyun YongHyeon 	struct mbuf *m_head;
533314bbd30fSGleb Smirnoff 	uint32_t prodidx;
5334167fdb62SPyun YongHyeon 	int count;
533595d67482SBill Paul 
5336fba8b109SMarcel Moolenaar 	sc = if_getsoftc(ifp);
5337167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
533895d67482SBill Paul 
5339167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
5340fba8b109SMarcel Moolenaar 	    (if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
5341167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
534295d67482SBill Paul 		return;
534395d67482SBill Paul 
534414bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
534595d67482SBill Paul 
5346fba8b109SMarcel Moolenaar 	for (count = 0; !if_sendq_empty(ifp);) {
5347167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
5348fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5349167fdb62SPyun YongHyeon 			break;
5350167fdb62SPyun YongHyeon 		}
5351fba8b109SMarcel Moolenaar 		m_head = if_dequeue(ifp);
535295d67482SBill Paul 		if (m_head == NULL)
535395d67482SBill Paul 			break;
535495d67482SBill Paul 
535595d67482SBill Paul 		/*
535695d67482SBill Paul 		 * Pack the data into the transmit ring. If we
535795d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
535895d67482SBill Paul 		 * for the NIC to drain the ring.
535995d67482SBill Paul 		 */
5360676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
5361676ad2c9SGleb Smirnoff 			if (m_head == NULL)
5362676ad2c9SGleb Smirnoff 				break;
5363fba8b109SMarcel Moolenaar 			if_sendq_prepend(ifp, m_head);
5364fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
536595d67482SBill Paul 			break;
536695d67482SBill Paul 		}
5367303a718cSDag-Erling Smørgrav 		++count;
536895d67482SBill Paul 
536995d67482SBill Paul 		/*
537095d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
537195d67482SBill Paul 		 * to him.
537295d67482SBill Paul 		 */
5373fba8b109SMarcel Moolenaar 		if_bpfmtap(ifp, m_head);
537495d67482SBill Paul 	}
537595d67482SBill Paul 
5376ded66962SMark Johnston 	if (count > 0)
5377ded66962SMark Johnston 		bge_start_tx(sc, prodidx);
5378ded66962SMark Johnston }
5379ded66962SMark Johnston 
5380ded66962SMark Johnston static void
5381ded66962SMark Johnston bge_start_tx(struct bge_softc *sc, uint32_t prodidx)
5382ded66962SMark Johnston {
5383ded66962SMark Johnston 
5384aa94f333SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
53855c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
53863f74909aSGleb Smirnoff 	/* Transmit. */
538738cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
53883927098fSPaul Saab 	/* 5700 b2 errata */
5389e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
539038cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
539195d67482SBill Paul 
539214bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
539314bbd30fSGleb Smirnoff 
5394ded66962SMark Johnston 	/* Set a timeout in case the chip goes out to lunch. */
5395b584d2b3SPyun YongHyeon 	sc->bge_timer = BGE_TX_TIMEOUT;
539695d67482SBill Paul }
539795d67482SBill Paul 
53980f9bd73bSSam Leffler /*
53990f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
54000f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
54010f9bd73bSSam Leffler  */
540295d67482SBill Paul static void
5403fba8b109SMarcel Moolenaar bge_start(if_t ifp)
540495d67482SBill Paul {
54050f9bd73bSSam Leffler 	struct bge_softc *sc;
54060f9bd73bSSam Leffler 
5407fba8b109SMarcel Moolenaar 	sc = if_getsoftc(ifp);
54080f9bd73bSSam Leffler 	BGE_LOCK(sc);
54090f9bd73bSSam Leffler 	bge_start_locked(ifp);
54100f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
54110f9bd73bSSam Leffler }
54120f9bd73bSSam Leffler 
54130f9bd73bSSam Leffler static void
54143f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
54150f9bd73bSSam Leffler {
5416fba8b109SMarcel Moolenaar 	if_t ifp;
54173f74909aSGleb Smirnoff 	uint16_t *m;
5418f6a65488SPyun YongHyeon 	uint32_t mode;
541995d67482SBill Paul 
54200f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
542195d67482SBill Paul 
5422fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
542395d67482SBill Paul 
5424fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
542595d67482SBill Paul 		return;
542695d67482SBill Paul 
542795d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
542895d67482SBill Paul 	bge_stop(sc);
54298cb1383cSDoug Ambrisko 
54308cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
54318cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
543295d67482SBill Paul 	bge_reset(sc);
54338cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
54348cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
54358cb1383cSDoug Ambrisko 
543695d67482SBill Paul 	bge_chipinit(sc);
543795d67482SBill Paul 
543895d67482SBill Paul 	/*
543995d67482SBill Paul 	 * Init the various state machines, ring
544095d67482SBill Paul 	 * control blocks and firmware.
544195d67482SBill Paul 	 */
544295d67482SBill Paul 	if (bge_blockinit(sc)) {
5443fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
544495d67482SBill Paul 		return;
544595d67482SBill Paul 	}
544695d67482SBill Paul 
5447fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
544895d67482SBill Paul 
544995d67482SBill Paul 	/* Specify MTU. */
5450fba8b109SMarcel Moolenaar 	CSR_WRITE_4(sc, BGE_RX_MTU, if_getmtu(ifp) +
5451cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
5452fba8b109SMarcel Moolenaar 	    (if_getcapenable(ifp) & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
545395d67482SBill Paul 
545495d67482SBill Paul 	/* Load our MAC address. */
54553f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
545695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
545795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
545895d67482SBill Paul 
54593e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
54603e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
546195d67482SBill Paul 
546295d67482SBill Paul 	/* Program multicast filter. */
546395d67482SBill Paul 	bge_setmulti(sc);
546495d67482SBill Paul 
5465cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
5466cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
5467cb2eacc7SYaroslav Tykhiy 
546835f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
546935f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
547035f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
547135f945cdSPyun YongHyeon 	else
547235f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
5473fba8b109SMarcel Moolenaar 	if (if_getcapabilities(ifp) & IFCAP_TXCSUM &&
5474fba8b109SMarcel Moolenaar 	    if_getcapenable(ifp) & IFCAP_TXCSUM) {
5475fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, 0, (BGE_CSUM_FEATURES | CSUM_UDP));
5476fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, sc->bge_csum_features, 0);
547735f945cdSPyun YongHyeon 	}
547835f945cdSPyun YongHyeon 
547995d67482SBill Paul 	/* Init RX ring. */
54803ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
54813ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
54823ee5d7daSPyun YongHyeon 		bge_stop(sc);
54833ee5d7daSPyun YongHyeon 		return;
54843ee5d7daSPyun YongHyeon 	}
548595d67482SBill Paul 
54860434d1b8SBill Paul 	/*
54870434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
54880434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
54890434d1b8SBill Paul 	 * entry of the ring.
54900434d1b8SBill Paul 	 */
54910434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
54923f74909aSGleb Smirnoff 		uint32_t		v, i;
54930434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
54940434d1b8SBill Paul 			DELAY(20);
54950434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
54960434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
54970434d1b8SBill Paul 				break;
54980434d1b8SBill Paul 		}
54990434d1b8SBill Paul 		if (i == 10)
5500fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
5501fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
55020434d1b8SBill Paul 	}
55030434d1b8SBill Paul 
550495d67482SBill Paul 	/* Init jumbo RX ring. */
5505f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
5506fba8b109SMarcel Moolenaar 	    if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
5507fba8b109SMarcel Moolenaar      	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN)) {
55083ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
5509333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
5510b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
55113ee5d7daSPyun YongHyeon 			bge_stop(sc);
55123ee5d7daSPyun YongHyeon 			return;
55133ee5d7daSPyun YongHyeon 		}
55143ee5d7daSPyun YongHyeon 	}
551595d67482SBill Paul 
55163f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
551795d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
551895d67482SBill Paul 
55197e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
55207e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
55217e6e2507SJung-uk Kim 
552295d67482SBill Paul 	/* Init TX ring. */
552395d67482SBill Paul 	bge_init_tx_ring(sc);
552495d67482SBill Paul 
5525f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
5526f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
5527f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
5528f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
55292927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
55302927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
553150515680SPyun YongHyeon 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
553250515680SPyun YongHyeon 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
553350515680SPyun YongHyeon 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
553450515680SPyun YongHyeon 	}
55353f74909aSGleb Smirnoff 	/* Turn on transmitter. */
5536f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
5537a6e66cd2SPyun YongHyeon 	DELAY(100);
553895d67482SBill Paul 
55393f74909aSGleb Smirnoff 	/* Turn on receiver. */
5540548c8f1aSPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_RX_MODE);
5541548c8f1aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc))
5542548c8f1aSPyun YongHyeon 		mode |= BGE_RXMODE_IPV6_ENABLE;
554369b1f509SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
554469b1f509SPyun YongHyeon 		mode |= BGE_RXMODE_IPV4_FRAG_FIX;
5545548c8f1aSPyun YongHyeon 	CSR_WRITE_4(sc,BGE_RX_MODE, mode | BGE_RXMODE_ENABLE);
5546a6e66cd2SPyun YongHyeon 	DELAY(10);
554795d67482SBill Paul 
5548dedcdf57SPyun YongHyeon 	/*
5549dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
5550dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
5551dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
5552dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
5553dedcdf57SPyun YongHyeon 	 */
55543fc5fbfbSPyun YongHyeon 	if (BGE_IS_57765_PLUS(sc))
5555b4a256acSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
5556b4a256acSPyun YongHyeon 	else
5557dedcdf57SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
5558dedcdf57SPyun YongHyeon 
55592280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
55602280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
55612280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
55622280c16bSPyun YongHyeon 
556395d67482SBill Paul 	/* Tell firmware we're alive. */
556495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
556595d67482SBill Paul 
556675719184SGleb Smirnoff #ifdef DEVICE_POLLING
556775719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
5568fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
556975719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
557075719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
557138cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
557275719184SGleb Smirnoff 	} else
557375719184SGleb Smirnoff #endif
557475719184SGleb Smirnoff 
557595d67482SBill Paul 	/* Enable host interrupts. */
557675719184SGleb Smirnoff 	{
557795d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
557895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
557938cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
558075719184SGleb Smirnoff 	}
558195d67482SBill Paul 
5582fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
5583fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
558495d67482SBill Paul 
5585e4146b95SPyun YongHyeon 	bge_ifmedia_upd_locked(ifp);
5586e4146b95SPyun YongHyeon 
55870f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
55880f9bd73bSSam Leffler }
55890f9bd73bSSam Leffler 
55900f9bd73bSSam Leffler static void
55913f74909aSGleb Smirnoff bge_init(void *xsc)
55920f9bd73bSSam Leffler {
55930f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
55940f9bd73bSSam Leffler 
55950f9bd73bSSam Leffler 	BGE_LOCK(sc);
55960f9bd73bSSam Leffler 	bge_init_locked(sc);
55970f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
559895d67482SBill Paul }
559995d67482SBill Paul 
560095d67482SBill Paul /*
560195d67482SBill Paul  * Set media options.
560295d67482SBill Paul  */
560395d67482SBill Paul static int
5604fba8b109SMarcel Moolenaar bge_ifmedia_upd(if_t ifp)
560595d67482SBill Paul {
5606fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
560767d5e043SOleg Bulyzhin 	int res;
560867d5e043SOleg Bulyzhin 
560967d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
561067d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
561167d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
561267d5e043SOleg Bulyzhin 
561367d5e043SOleg Bulyzhin 	return (res);
561467d5e043SOleg Bulyzhin }
561567d5e043SOleg Bulyzhin 
561667d5e043SOleg Bulyzhin static int
5617fba8b109SMarcel Moolenaar bge_ifmedia_upd_locked(if_t ifp)
561867d5e043SOleg Bulyzhin {
5619fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
562095d67482SBill Paul 	struct mii_data *mii;
56214f09c4c7SMarius Strobl 	struct mii_softc *miisc;
562295d67482SBill Paul 	struct ifmedia *ifm;
562395d67482SBill Paul 
562467d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
562567d5e043SOleg Bulyzhin 
562695d67482SBill Paul 	ifm = &sc->bge_ifmedia;
562795d67482SBill Paul 
562895d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
5629652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
563095d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
563195d67482SBill Paul 			return (EINVAL);
563295d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
563395d67482SBill Paul 		case IFM_AUTO:
5634ff50922bSDoug White 			/*
5635ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
5636ff50922bSDoug White 			 * mechanism for programming the autoneg
5637ff50922bSDoug White 			 * advertisement registers in TBI mode.
5638ff50922bSDoug White 			 */
56390f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5640ff50922bSDoug White 				uint32_t sgdig;
56410f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
56420f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
5643ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5644ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5645ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
5646ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
5647ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
5648ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5649ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
5650ff50922bSDoug White 					DELAY(5);
5651ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5652ff50922bSDoug White 				}
56530f89fde2SJung-uk Kim 			}
565495d67482SBill Paul 			break;
565595d67482SBill Paul 		case IFM_1000_SX:
565695d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
565795d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
565895d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
565995d67482SBill Paul 			} else {
566095d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
566195d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
566295d67482SBill Paul 			}
56639b80ffe7SPyun YongHyeon 			DELAY(40);
566495d67482SBill Paul 			break;
566595d67482SBill Paul 		default:
566695d67482SBill Paul 			return (EINVAL);
566795d67482SBill Paul 		}
566895d67482SBill Paul 		return (0);
566995d67482SBill Paul 	}
567095d67482SBill Paul 
56711493e883SOleg Bulyzhin 	sc->bge_link_evt++;
567295d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
56734f09c4c7SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
56743fcb7a53SMarius Strobl 		PHY_RESET(miisc);
567595d67482SBill Paul 	mii_mediachg(mii);
567695d67482SBill Paul 
5677902827f6SBjoern A. Zeeb 	/*
5678902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
5679902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
5680902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
5681902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
5682902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
5683902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
5684902827f6SBjoern A. Zeeb 	 * get an RX intr.
5685902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
5686902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
5687902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
5688902827f6SBjoern A. Zeeb 	 */
56894f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
56904f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
5691902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
56924f0794ffSBjoern A. Zeeb 	else
569363ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5694902827f6SBjoern A. Zeeb 
569595d67482SBill Paul 	return (0);
569695d67482SBill Paul }
569795d67482SBill Paul 
569895d67482SBill Paul /*
569995d67482SBill Paul  * Report current media status.
570095d67482SBill Paul  */
570195d67482SBill Paul static void
5702fba8b109SMarcel Moolenaar bge_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
570395d67482SBill Paul {
5704fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
570595d67482SBill Paul 	struct mii_data *mii;
570695d67482SBill Paul 
570767d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
570895d67482SBill Paul 
5709fba8b109SMarcel Moolenaar 	if ((if_getflags(ifp) & IFF_UP) == 0) {
5710b9d2edd7SPyun YongHyeon 		BGE_UNLOCK(sc);
5711b9d2edd7SPyun YongHyeon 		return;
5712b9d2edd7SPyun YongHyeon 	}
5713652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
571495d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
571595d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
571695d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
571795d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
571895d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
57194c0da0ffSGleb Smirnoff 		else {
57204c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
572167d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
57224c0da0ffSGleb Smirnoff 			return;
57234c0da0ffSGleb Smirnoff 		}
572495d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
572595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
572695d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
572795d67482SBill Paul 		else
572895d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
572967d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
573095d67482SBill Paul 		return;
573195d67482SBill Paul 	}
573295d67482SBill Paul 
573395d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
573495d67482SBill Paul 	mii_pollstat(mii);
573595d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
573695d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
573767d5e043SOleg Bulyzhin 
573867d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
573995d67482SBill Paul }
574095d67482SBill Paul 
574195d67482SBill Paul static int
5742fba8b109SMarcel Moolenaar bge_ioctl(if_t ifp, u_long command, caddr_t data)
574395d67482SBill Paul {
5744fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
574595d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
574695d67482SBill Paul 	struct mii_data *mii;
5747f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
574895d67482SBill Paul 
574995d67482SBill Paul 	switch (command) {
575095d67482SBill Paul 	case SIOCSIFMTU:
5751f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
5752f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
57534c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
5754f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
575595d67482SBill Paul 				error = EINVAL;
5756f5459d4cSPyun YongHyeon 				break;
5757f5459d4cSPyun YongHyeon 			}
5758f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5759f5459d4cSPyun YongHyeon 			error = EINVAL;
5760f5459d4cSPyun YongHyeon 			break;
5761f5459d4cSPyun YongHyeon 		}
5762f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
5763fba8b109SMarcel Moolenaar 		if (if_getmtu(ifp) != ifr->ifr_mtu) {
5764fba8b109SMarcel Moolenaar 			if_setmtu(ifp, ifr->ifr_mtu);
5765fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
5766fba8b109SMarcel Moolenaar 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
57673a429c8fSPyun YongHyeon 				bge_init_locked(sc);
576895d67482SBill Paul 			}
57693a429c8fSPyun YongHyeon 		}
57703a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
577195d67482SBill Paul 		break;
577295d67482SBill Paul 	case SIOCSIFFLAGS:
57730f9bd73bSSam Leffler 		BGE_LOCK(sc);
5774fba8b109SMarcel Moolenaar 		if (if_getflags(ifp) & IFF_UP) {
577595d67482SBill Paul 			/*
577695d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
577795d67482SBill Paul 			 * then just use the 'set promisc mode' command
577895d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
577995d67482SBill Paul 			 * a full re-init means reloading the firmware and
578095d67482SBill Paul 			 * waiting for it to start up, which may take a
5781d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
578295d67482SBill Paul 			 */
5783fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
5784fba8b109SMarcel Moolenaar 				flags = if_getflags(ifp) ^ sc->bge_if_flags;
57853e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
57863e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
5787f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
5788d183af7fSRuslan Ermilov 					bge_setmulti(sc);
578995d67482SBill Paul 			} else
57900f9bd73bSSam Leffler 				bge_init_locked(sc);
579195d67482SBill Paul 		} else {
5792fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
579395d67482SBill Paul 				bge_stop(sc);
579495d67482SBill Paul 			}
579595d67482SBill Paul 		}
5796fba8b109SMarcel Moolenaar 		sc->bge_if_flags = if_getflags(ifp);
57970f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
579895d67482SBill Paul 		error = 0;
579995d67482SBill Paul 		break;
580095d67482SBill Paul 	case SIOCADDMULTI:
580195d67482SBill Paul 	case SIOCDELMULTI:
5802fba8b109SMarcel Moolenaar 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
58030f9bd73bSSam Leffler 			BGE_LOCK(sc);
580495d67482SBill Paul 			bge_setmulti(sc);
58050f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
580695d67482SBill Paul 			error = 0;
580795d67482SBill Paul 		}
580895d67482SBill Paul 		break;
580995d67482SBill Paul 	case SIOCSIFMEDIA:
581095d67482SBill Paul 	case SIOCGIFMEDIA:
5811652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
581295d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
581395d67482SBill Paul 			    &sc->bge_ifmedia, command);
581495d67482SBill Paul 		} else {
581595d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
581695d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
581795d67482SBill Paul 			    &mii->mii_media, command);
581895d67482SBill Paul 		}
581995d67482SBill Paul 		break;
582095d67482SBill Paul 	case SIOCSIFCAP:
5821fba8b109SMarcel Moolenaar 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
582275719184SGleb Smirnoff #ifdef DEVICE_POLLING
582375719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
582475719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
5825bd071d4dSGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
582675719184SGleb Smirnoff 				if (error)
582775719184SGleb Smirnoff 					return (error);
582875719184SGleb Smirnoff 				BGE_LOCK(sc);
582975719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
583075719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
583138cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
5832fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, IFCAP_POLLING, 0);
583375719184SGleb Smirnoff 				BGE_UNLOCK(sc);
583475719184SGleb Smirnoff 			} else {
583575719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
583675719184SGleb Smirnoff 				/* Enable interrupt even in error case */
583775719184SGleb Smirnoff 				BGE_LOCK(sc);
583875719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
583975719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
584038cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
5841fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_POLLING);
584275719184SGleb Smirnoff 				BGE_UNLOCK(sc);
584375719184SGleb Smirnoff 			}
584475719184SGleb Smirnoff 		}
584575719184SGleb Smirnoff #endif
5846d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5847fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TXCSUM) != 0) {
5848fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TXCSUM);
5849fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
5850fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp,
5851fba8b109SMarcel Moolenaar 				    sc->bge_csum_features, 0);
585295d67482SBill Paul 			else
5853fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, 0,
5854fba8b109SMarcel Moolenaar 				    sc->bge_csum_features);
585595d67482SBill Paul 		}
5856cb2eacc7SYaroslav Tykhiy 
5857d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5858fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_RXCSUM) != 0)
5859fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_RXCSUM);
5860d8b57f98SPyun YongHyeon 
5861ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5862fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TSO4) != 0) {
5863fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TSO4);
5864fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TSO4) != 0)
5865fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, CSUM_TSO, 0);
5866ca3f1187SPyun YongHyeon 			else
5867fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, 0, CSUM_TSO);
5868ca3f1187SPyun YongHyeon 		}
5869ca3f1187SPyun YongHyeon 
5870cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5871fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
5872fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
5873cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5874cb2eacc7SYaroslav Tykhiy 		}
5875cb2eacc7SYaroslav Tykhiy 
587604bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
5877fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTSO) != 0)
5878fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
587904bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
5880fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
5881fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
5882fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0)
5883fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_VLAN_HWTSO);
5884cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5885cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5886cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
588704bde852SPyun YongHyeon 		}
5888cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5889fba8b109SMarcel Moolenaar 		if_vlancap(ifp);
5890cb2eacc7SYaroslav Tykhiy #endif
589195d67482SBill Paul 		break;
589295d67482SBill Paul 	default:
5893673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
589495d67482SBill Paul 		break;
589595d67482SBill Paul 	}
589695d67482SBill Paul 
589795d67482SBill Paul 	return (error);
589895d67482SBill Paul }
589995d67482SBill Paul 
590095d67482SBill Paul static void
5901b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
590295d67482SBill Paul {
5903fba8b109SMarcel Moolenaar 	if_t ifp;
5904b584d2b3SPyun YongHyeon 	uint32_t status;
590595d67482SBill Paul 
5906b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5907b74e67fbSGleb Smirnoff 
5908b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5909b74e67fbSGleb Smirnoff 		return;
5910b74e67fbSGleb Smirnoff 
5911b584d2b3SPyun YongHyeon 	/* If pause frames are active then don't reset the hardware. */
5912b584d2b3SPyun YongHyeon 	if ((CSR_READ_4(sc, BGE_RX_MODE) & BGE_RXMODE_FLOWCTL_ENABLE) != 0) {
5913b584d2b3SPyun YongHyeon 		status = CSR_READ_4(sc, BGE_RX_STS);
5914b584d2b3SPyun YongHyeon 		if ((status & BGE_RXSTAT_REMOTE_XOFFED) != 0) {
5915b584d2b3SPyun YongHyeon 			/*
5916b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5917b584d2b3SPyun YongHyeon 			 * the condition to clear.
5918b584d2b3SPyun YongHyeon 			 */
5919b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5920b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5921b584d2b3SPyun YongHyeon 			return;
5922b584d2b3SPyun YongHyeon 		} else if ((status & BGE_RXSTAT_RCVD_XOFF) != 0 &&
5923b584d2b3SPyun YongHyeon 		    (status & BGE_RXSTAT_RCVD_XON) != 0) {
5924b584d2b3SPyun YongHyeon 			/*
5925b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5926b584d2b3SPyun YongHyeon 			 * the condition to clear.
5927b584d2b3SPyun YongHyeon 			 */
5928b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5929b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5930b584d2b3SPyun YongHyeon 			return;
5931b584d2b3SPyun YongHyeon 		}
5932b584d2b3SPyun YongHyeon 		/*
5933b584d2b3SPyun YongHyeon 		 * Any other condition is unexpected and the controller
5934b584d2b3SPyun YongHyeon 		 * should be reset.
5935b584d2b3SPyun YongHyeon 		 */
5936b584d2b3SPyun YongHyeon 	}
5937b584d2b3SPyun YongHyeon 
5938b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
593995d67482SBill Paul 
5940fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
594195d67482SBill Paul 
5942fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
5943426742bfSGleb Smirnoff 	bge_init_locked(sc);
594495d67482SBill Paul 
5945df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
594695d67482SBill Paul }
594795d67482SBill Paul 
59485a147ba6SPyun YongHyeon static void
59495a147ba6SPyun YongHyeon bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
59505a147ba6SPyun YongHyeon {
59515a147ba6SPyun YongHyeon 	int i;
59525a147ba6SPyun YongHyeon 
59535a147ba6SPyun YongHyeon 	BGE_CLRBIT(sc, reg, bit);
59545a147ba6SPyun YongHyeon 
59555a147ba6SPyun YongHyeon 	for (i = 0; i < BGE_TIMEOUT; i++) {
59565a147ba6SPyun YongHyeon 		if ((CSR_READ_4(sc, reg) & bit) == 0)
59575a147ba6SPyun YongHyeon 			return;
59585a147ba6SPyun YongHyeon 		DELAY(100);
59595a147ba6SPyun YongHyeon         }
59605a147ba6SPyun YongHyeon }
59615a147ba6SPyun YongHyeon 
596295d67482SBill Paul /*
596395d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
596495d67482SBill Paul  * RX and TX lists.
596595d67482SBill Paul  */
596695d67482SBill Paul static void
59673f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
596895d67482SBill Paul {
5969fba8b109SMarcel Moolenaar 	if_t ifp;
597095d67482SBill Paul 
59710f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
59720f9bd73bSSam Leffler 
5973fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
597495d67482SBill Paul 
59750f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
597695d67482SBill Paul 
597744b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
597844b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
597944b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
598044b63691SBjoern A. Zeeb 
598144b63691SBjoern A. Zeeb 	/*
598244b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
598344b63691SBjoern A. Zeeb 	 */
598444b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
5985548c8f1aSPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
598644b63691SBjoern A. Zeeb 
598795d67482SBill Paul 	/*
59883f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
598995d67482SBill Paul 	 */
59905a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
59915a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
59925a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
59935a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
59945a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
59955a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
59965a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
59975a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
599895d67482SBill Paul 
599995d67482SBill Paul 	/*
60003f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
600195d67482SBill Paul 	 */
60025a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
60035a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
60045a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
60055a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
60065a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
60075a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60085a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
60095a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
601095d67482SBill Paul 
601195d67482SBill Paul 	/*
601295d67482SBill Paul 	 * Shut down all of the memory managers and related
601395d67482SBill Paul 	 * state machines.
601495d67482SBill Paul 	 */
60155a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
60165a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
60175a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60185a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
60195a147ba6SPyun YongHyeon 
60200c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
602195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
60227ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
602395d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
602495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
60250434d1b8SBill Paul 	}
60262280c16bSPyun YongHyeon 	/* Update MAC statistics. */
60272280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
60282280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
602995d67482SBill Paul 
60308cb1383cSDoug Ambrisko 	bge_reset(sc);
6031548c8f1aSPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
6032548c8f1aSPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
60338cb1383cSDoug Ambrisko 
60348cb1383cSDoug Ambrisko 	/*
60358cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
60368cb1383cSDoug Ambrisko 	 */
60378cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
60388cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
60398cb1383cSDoug Ambrisko 	else
604095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
604195d67482SBill Paul 
604295d67482SBill Paul 	/* Free the RX lists. */
604395d67482SBill Paul 	bge_free_rx_ring_std(sc);
604495d67482SBill Paul 
604595d67482SBill Paul 	/* Free jumbo RX list. */
60464c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
604795d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
604895d67482SBill Paul 
604995d67482SBill Paul 	/* Free TX buffers. */
605095d67482SBill Paul 	bge_free_tx_ring(sc);
605195d67482SBill Paul 
605295d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
605395d67482SBill Paul 
60545dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
60551493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
60561493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
60571493e883SOleg Bulyzhin 	sc->bge_link = 0;
605895d67482SBill Paul 
6059fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
606095d67482SBill Paul }
606195d67482SBill Paul 
606295d67482SBill Paul /*
606395d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
606495d67482SBill Paul  * get confused by errant DMAs when rebooting.
606595d67482SBill Paul  */
6066b6c974e8SWarner Losh static int
60673f74909aSGleb Smirnoff bge_shutdown(device_t dev)
606895d67482SBill Paul {
606995d67482SBill Paul 	struct bge_softc *sc;
607095d67482SBill Paul 
607195d67482SBill Paul 	sc = device_get_softc(dev);
60720f9bd73bSSam Leffler 	BGE_LOCK(sc);
607395d67482SBill Paul 	bge_stop(sc);
60740f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
6075b6c974e8SWarner Losh 
6076b6c974e8SWarner Losh 	return (0);
607795d67482SBill Paul }
607814afefa3SPawel Jakub Dawidek 
607914afefa3SPawel Jakub Dawidek static int
608014afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
608114afefa3SPawel Jakub Dawidek {
608214afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
608314afefa3SPawel Jakub Dawidek 
608414afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
608514afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
608614afefa3SPawel Jakub Dawidek 	bge_stop(sc);
608714afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
608814afefa3SPawel Jakub Dawidek 
608914afefa3SPawel Jakub Dawidek 	return (0);
609014afefa3SPawel Jakub Dawidek }
609114afefa3SPawel Jakub Dawidek 
609214afefa3SPawel Jakub Dawidek static int
609314afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
609414afefa3SPawel Jakub Dawidek {
609514afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
6096fba8b109SMarcel Moolenaar 	if_t ifp;
609714afefa3SPawel Jakub Dawidek 
609814afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
609914afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
610014afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
6101fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_UP) {
610214afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
6103fba8b109SMarcel Moolenaar 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
610414afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
610514afefa3SPawel Jakub Dawidek 	}
610614afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
610714afefa3SPawel Jakub Dawidek 
610814afefa3SPawel Jakub Dawidek 	return (0);
610914afefa3SPawel Jakub Dawidek }
6110dab5cd05SOleg Bulyzhin 
6111dab5cd05SOleg Bulyzhin static void
61123f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
6113dab5cd05SOleg Bulyzhin {
61141f313773SOleg Bulyzhin 	struct mii_data *mii;
61151f313773SOleg Bulyzhin 	uint32_t link, status;
6116dab5cd05SOleg Bulyzhin 
6117dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
61181f313773SOleg Bulyzhin 
61193f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
61207b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
61217b97099dSOleg Bulyzhin 
6122dab5cd05SOleg Bulyzhin 	/*
6123dab5cd05SOleg Bulyzhin 	 * Process link state changes.
6124dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
6125dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
6126dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
6127dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
6128dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
6129dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
6130dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
6131dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
61321f313773SOleg Bulyzhin 	 *
61331f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
61344c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
6135dab5cd05SOleg Bulyzhin 	 */
6136dab5cd05SOleg Bulyzhin 
61371f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
61384c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
6139dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
6140dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
61411f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
61425dda8085SOleg Bulyzhin 			mii_pollstat(mii);
61431f313773SOleg Bulyzhin 			if (!sc->bge_link &&
61441f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
61451f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
61461f313773SOleg Bulyzhin 				sc->bge_link++;
61471f313773SOleg Bulyzhin 				if (bootverbose)
61481f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
61491f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
61501f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
61511f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
61521f313773SOleg Bulyzhin 				sc->bge_link = 0;
61531f313773SOleg Bulyzhin 				if (bootverbose)
61541f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
61551f313773SOleg Bulyzhin 			}
61561f313773SOleg Bulyzhin 
61573f74909aSGleb Smirnoff 			/* Clear the interrupt. */
6158dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
6159dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
6160daeeb75cSPyun YongHyeon 			bge_miibus_readreg(sc->bge_dev, sc->bge_phy_addr,
6161daeeb75cSPyun YongHyeon 			    BRGPHY_MII_ISR);
6162daeeb75cSPyun YongHyeon 			bge_miibus_writereg(sc->bge_dev, sc->bge_phy_addr,
6163daeeb75cSPyun YongHyeon 			    BRGPHY_MII_IMR, BRGPHY_INTRS);
6164dab5cd05SOleg Bulyzhin 		}
6165dab5cd05SOleg Bulyzhin 		return;
6166dab5cd05SOleg Bulyzhin 	}
6167dab5cd05SOleg Bulyzhin 
6168652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
61691f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
61707b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
61717b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
61721f313773SOleg Bulyzhin 				sc->bge_link++;
61739b80ffe7SPyun YongHyeon 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
61741f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
61751f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
61769b80ffe7SPyun YongHyeon 					DELAY(40);
61779b80ffe7SPyun YongHyeon 				}
61780c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
61791f313773SOleg Bulyzhin 				if (bootverbose)
61801f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
61813f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
61823f74909aSGleb Smirnoff 				    LINK_STATE_UP);
61837b97099dSOleg Bulyzhin 			}
61841f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
6185dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
61861f313773SOleg Bulyzhin 			if (bootverbose)
61871f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
61887b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
61891f313773SOleg Bulyzhin 		}
61906ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
61911f313773SOleg Bulyzhin 		/*
61920c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
61930c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
61940c8aa4eaSJung-uk Kim 		 * PHY link status directly.
61951f313773SOleg Bulyzhin 		 */
61961f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
61971f313773SOleg Bulyzhin 
61981f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
61991f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
62001f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
62015dda8085SOleg Bulyzhin 			mii_pollstat(mii);
62021f313773SOleg Bulyzhin 			if (!sc->bge_link &&
62031f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
62041f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
62051f313773SOleg Bulyzhin 				sc->bge_link++;
62061f313773SOleg Bulyzhin 				if (bootverbose)
62071f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
62081f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
62091f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
62101f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
62111f313773SOleg Bulyzhin 				sc->bge_link = 0;
62121f313773SOleg Bulyzhin 				if (bootverbose)
62131f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
62141f313773SOleg Bulyzhin 			}
62151f313773SOleg Bulyzhin 		}
62160c8aa4eaSJung-uk Kim 	} else {
62170c8aa4eaSJung-uk Kim 		/*
62186ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
62196ede2cfaSPyun YongHyeon 		 * link status.
62200c8aa4eaSJung-uk Kim 		 */
62216ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
62226ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
62236ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
6224dab5cd05SOleg Bulyzhin 	}
6225dab5cd05SOleg Bulyzhin 
62262246e8c6SPyun YongHyeon 	/* Disable MAC attention when link is up. */
6227dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
6228dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
6229dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
6230dab5cd05SOleg Bulyzhin }
62316f8718a3SScott Long 
62326f8718a3SScott Long static void
62336f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
62346f8718a3SScott Long {
62356f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
62362280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
62376f8718a3SScott Long 
62386f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
62396f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
62406f8718a3SScott Long 
62416f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
62426f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
62437029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
62447029da5cSPawel Biernacki 	    bge_sysctl_debug_info, "I", "Debug Information");
62456f8718a3SScott Long 
62466f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
62477029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
62487029da5cSPawel Biernacki 	    bge_sysctl_reg_read, "I", "MAC Register Read");
6249548c8f1aSPyun YongHyeon 
6250548c8f1aSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ape_read",
62517029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
62527029da5cSPawel Biernacki 	    bge_sysctl_ape_read, "I", "APE Register Read");
62536f8718a3SScott Long 
62546f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
62557029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
62567029da5cSPawel Biernacki 	    bge_sysctl_mem_read, "I", "Memory Read");
62576f8718a3SScott Long 
62586f8718a3SScott Long #endif
6259763757b2SScott Long 
6260beaa2ae1SPyun YongHyeon 	/*
6261beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
6262beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
6263beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
6264beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
6265beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
6266beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
6267beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
6268beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
6269beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
6270beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
6271beaa2ae1SPyun YongHyeon 	 */
62727e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
6273beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
6274af3b2549SHans Petter Selasky 	    CTLFLAG_RWTUN, &sc->bge_forced_collapse, 0,
6275beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
6276beaa2ae1SPyun YongHyeon 	    "forced collapsing");
6277beaa2ae1SPyun YongHyeon 
62782ae7f64bSPyun YongHyeon 	sc->bge_msi = 1;
62792ae7f64bSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
6280af3b2549SHans Petter Selasky 	    CTLFLAG_RDTUN, &sc->bge_msi, 0, "Enable MSI");
62815c952e8dSPyun YongHyeon 
628235f945cdSPyun YongHyeon 	/*
628335f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
628435f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
628535f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
628635f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
628735f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
628835f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
628935f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
629035f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
629135f945cdSPyun YongHyeon 	 */
629235f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
629335f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
6294af3b2549SHans Petter Selasky 	    CTLFLAG_RWTUN, &sc->bge_forced_udpcsum, 0,
629535f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
629635f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
629735f945cdSPyun YongHyeon 
6298d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
62992280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
63002280c16bSPyun YongHyeon 	else
63012280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
63022280c16bSPyun YongHyeon }
6303d949071dSJung-uk Kim 
63042280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
63057029da5cSPawel Biernacki     SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, \
63067029da5cSPawel Biernacki         CTLTYPE_UINT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, \
63077029da5cSPawel Biernacki 	offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", desc)
63082280c16bSPyun YongHyeon 
63092280c16bSPyun YongHyeon static void
63102280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
63112280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
63122280c16bSPyun YongHyeon {
63132280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
63142280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
63152280c16bSPyun YongHyeon 
63167029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats",
63177029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "BGE Statistics");
6318763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
6319763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
6320763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
6321763757b2SScott Long 	    "FramesDroppedDueToFilters");
6322763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
6323763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
6324763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
6325763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
6326763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
6327763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
632806e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
632906e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
633006e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
633106e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
6332763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
6333763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
6334763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
6335763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
6336763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
6337763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
6338763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
6339763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
6340763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
6341763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
6342763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
6343763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
6344763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
6345763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
6346763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
6347763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
6348763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
6349763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
6350763757b2SScott Long 
63517029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx",
63527029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "BGE RX Statistics");
6353763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6354763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
63551cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
6356763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
6357763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
6358763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
63591cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
6360763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
6361763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
6362763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
6363763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
6364763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
6365763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
6366763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
6367763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
6368763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
6369763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
6370763757b2SScott Long 	    "xoffPauseFramesReceived");
6371763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
6372763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
6373763757b2SScott Long 	    "ControlFramesReceived");
6374763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
6375763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
6376763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
6377763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
6378763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
6379763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
6380763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
6381763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
6382763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
638306e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
6384763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
638506e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
6386763757b2SScott Long 
63877029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx",
63887029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "BGE TX Statistics");
6389763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6390763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
63911cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
6392763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
6393763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
6394763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
6395763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
6396763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
6397763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
6398763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
6399763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
6400763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
6401763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
6402763757b2SScott Long 	    "InternalMacTransmitErrors");
6403763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
6404763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
6405763757b2SScott Long 	    "SingleCollisionFrames");
6406763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
6407763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
6408763757b2SScott Long 	    "MultipleCollisionFrames");
6409763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
6410763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
6411763757b2SScott Long 	    "DeferredTransmissions");
6412763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
6413763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
6414763757b2SScott Long 	    "ExcessiveCollisions");
6415763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
641606e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
641706e83c7eSScott Long 	    "LateCollisions");
6418763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
64191cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
6420763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
6421763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
6422763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
6423763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
6424763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
6425763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
6426763757b2SScott Long 	    "CarrierSenseErrors");
6427763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
6428763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
6429763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
6430763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
6431763757b2SScott Long }
6432763757b2SScott Long 
64332280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
64342280c16bSPyun YongHyeon 
64352280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
64366dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
64372280c16bSPyun YongHyeon 
64382280c16bSPyun YongHyeon static void
64392280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
64402280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
64412280c16bSPyun YongHyeon {
64422280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
64432280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
64442280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
64452280c16bSPyun YongHyeon 
64462280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
64477029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats",
64487029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "BGE Statistics");
64492280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
64502280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
64512280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
64522280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
64532280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
64542280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
64552280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
64562280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
64572280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
64582280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
64592280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
64602280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
64612280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
64622280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
64632280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
64642280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
64652280c16bSPyun YongHyeon 
64667029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx",
64677029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "BGE RX Statistics");
64682280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
64692280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
64702280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
64712280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
64722280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
64731cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
64742280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
64752280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
64762280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
64772280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
64782280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
64792280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
64802280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
64812280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
64822280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
64832280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
64842280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
64852280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
64862280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
64872280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
64882280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
64892280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
64902280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
64912280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
64922280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
64932280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
64942280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
64952280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
64962280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
64972280c16bSPyun YongHyeon 
64987029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx",
64997029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "BGE TX Statistics");
65002280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
65011cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
65022280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
65032280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
65042280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
65052280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
65062280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
65072280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
65082280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
65092280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
65102280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
65112280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
65122280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
65132280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
65142280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
65152280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
65162280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
65172280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
65182280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
65192280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
65202280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
65212280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
65222280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
65231cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
65242280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
65251cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
65262280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
65271cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
65282280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
65292280c16bSPyun YongHyeon }
65302280c16bSPyun YongHyeon 
65312280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
65322280c16bSPyun YongHyeon 
6533763757b2SScott Long static int
6534763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
6535763757b2SScott Long {
6536763757b2SScott Long 	struct bge_softc *sc;
653706e83c7eSScott Long 	uint32_t result;
6538d949071dSJung-uk Kim 	int offset;
6539763757b2SScott Long 
6540763757b2SScott Long 	sc = (struct bge_softc *)arg1;
6541763757b2SScott Long 	offset = arg2;
6542d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
6543d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
6544041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
65456f8718a3SScott Long }
65466f8718a3SScott Long 
65476f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
65486f8718a3SScott Long static int
65496f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
65506f8718a3SScott Long {
65516f8718a3SScott Long 	struct bge_softc *sc;
65526f8718a3SScott Long 	uint16_t *sbdata;
655328276ad6SPyun YongHyeon 	int error, result, sbsz;
65546f8718a3SScott Long 	int i, j;
65556f8718a3SScott Long 
65566f8718a3SScott Long 	result = -1;
65576f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
65586f8718a3SScott Long 	if (error || (req->newptr == NULL))
65596f8718a3SScott Long 		return (error);
65606f8718a3SScott Long 
65616f8718a3SScott Long 	if (result == 1) {
65626f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
65636f8718a3SScott Long 
656428276ad6SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
656528276ad6SPyun YongHyeon 		    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
656628276ad6SPyun YongHyeon 			sbsz = BGE_STATUS_BLK_SZ;
656728276ad6SPyun YongHyeon 		else
656828276ad6SPyun YongHyeon 			sbsz = 32;
65696f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
65706f8718a3SScott Long 		printf("Status Block:\n");
657128276ad6SPyun YongHyeon 		BGE_LOCK(sc);
657228276ad6SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
657328276ad6SPyun YongHyeon 		    sc->bge_cdata.bge_status_map,
657428276ad6SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
657528276ad6SPyun YongHyeon 		for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
65766f8718a3SScott Long 			printf("%06x:", i);
657728276ad6SPyun YongHyeon 			for (j = 0; j < 8; j++)
657828276ad6SPyun YongHyeon 				printf(" %04x", sbdata[i++]);
65796f8718a3SScott Long 			printf("\n");
65806f8718a3SScott Long 		}
65816f8718a3SScott Long 
65826f8718a3SScott Long 		printf("Registers:\n");
65830c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
65846f8718a3SScott Long 			printf("%06x:", i);
65856f8718a3SScott Long 			for (j = 0; j < 8; j++) {
65866f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
65876f8718a3SScott Long 				i += 4;
65886f8718a3SScott Long 			}
65896f8718a3SScott Long 			printf("\n");
65906f8718a3SScott Long 		}
659128276ad6SPyun YongHyeon 		BGE_UNLOCK(sc);
65926f8718a3SScott Long 
65936f8718a3SScott Long 		printf("Hardware Flags:\n");
659428276ad6SPyun YongHyeon 		if (BGE_IS_5717_PLUS(sc))
659528276ad6SPyun YongHyeon 			printf(" - 5717 Plus\n");
6596a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
6597a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
65985345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
65996f8718a3SScott Long 			printf(" - 575X Plus\n");
66005345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
66016f8718a3SScott Long 			printf(" - 5705 Plus\n");
66025345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
66035345bad0SScott Long 			printf(" - 5714 Family\n");
66045345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
66055345bad0SScott Long 			printf(" - 5700 Family\n");
66066f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
66076f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
66086f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
66096f8718a3SScott Long 			printf(" - PCI-X Bus\n");
66106f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
66116f8718a3SScott Long 			printf(" - PCI Express Bus\n");
66127d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
66136f8718a3SScott Long 			printf(" - No 3 LEDs\n");
66146f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
66156f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
66166f8718a3SScott Long 	}
66176f8718a3SScott Long 
66186f8718a3SScott Long 	return (error);
66196f8718a3SScott Long }
66206f8718a3SScott Long 
66216f8718a3SScott Long static int
66226f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
66236f8718a3SScott Long {
66246f8718a3SScott Long 	struct bge_softc *sc;
66256f8718a3SScott Long 	int error;
66266f8718a3SScott Long 	uint16_t result;
66276f8718a3SScott Long 	uint32_t val;
66286f8718a3SScott Long 
66296f8718a3SScott Long 	result = -1;
66306f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
66316f8718a3SScott Long 	if (error || (req->newptr == NULL))
66326f8718a3SScott Long 		return (error);
66336f8718a3SScott Long 
66346f8718a3SScott Long 	if (result < 0x8000) {
66356f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
66366f8718a3SScott Long 		val = CSR_READ_4(sc, result);
66376f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
66386f8718a3SScott Long 	}
66396f8718a3SScott Long 
66406f8718a3SScott Long 	return (error);
66416f8718a3SScott Long }
66426f8718a3SScott Long 
66436f8718a3SScott Long static int
6644548c8f1aSPyun YongHyeon bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS)
6645548c8f1aSPyun YongHyeon {
6646548c8f1aSPyun YongHyeon 	struct bge_softc *sc;
6647548c8f1aSPyun YongHyeon 	int error;
6648548c8f1aSPyun YongHyeon 	uint16_t result;
6649548c8f1aSPyun YongHyeon 	uint32_t val;
6650548c8f1aSPyun YongHyeon 
6651548c8f1aSPyun YongHyeon 	result = -1;
6652548c8f1aSPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
6653548c8f1aSPyun YongHyeon 	if (error || (req->newptr == NULL))
6654548c8f1aSPyun YongHyeon 		return (error);
6655548c8f1aSPyun YongHyeon 
6656548c8f1aSPyun YongHyeon 	if (result < 0x8000) {
6657548c8f1aSPyun YongHyeon 		sc = (struct bge_softc *)arg1;
6658548c8f1aSPyun YongHyeon 		val = APE_READ_4(sc, result);
6659548c8f1aSPyun YongHyeon 		printf("reg 0x%06X = 0x%08X\n", result, val);
6660548c8f1aSPyun YongHyeon 	}
6661548c8f1aSPyun YongHyeon 
6662548c8f1aSPyun YongHyeon 	return (error);
6663548c8f1aSPyun YongHyeon }
6664548c8f1aSPyun YongHyeon 
6665548c8f1aSPyun YongHyeon static int
66666f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
66676f8718a3SScott Long {
66686f8718a3SScott Long 	struct bge_softc *sc;
66696f8718a3SScott Long 	int error;
66706f8718a3SScott Long 	uint16_t result;
66716f8718a3SScott Long 	uint32_t val;
66726f8718a3SScott Long 
66736f8718a3SScott Long 	result = -1;
66746f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
66756f8718a3SScott Long 	if (error || (req->newptr == NULL))
66766f8718a3SScott Long 		return (error);
66776f8718a3SScott Long 
66786f8718a3SScott Long 	if (result < 0x8000) {
66796f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
66806f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
66816f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
66826f8718a3SScott Long 	}
66836f8718a3SScott Long 
66846f8718a3SScott Long 	return (error);
66856f8718a3SScott Long }
66866f8718a3SScott Long #endif
668738cc658fSJohn Baldwin 
668838cc658fSJohn Baldwin static int
66895fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
66905fea260fSMarius Strobl {
66915fea260fSMarius Strobl 	return (1);
66925fea260fSMarius Strobl }
66935fea260fSMarius Strobl 
66945fea260fSMarius Strobl static int
669538cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
669638cc658fSJohn Baldwin {
669738cc658fSJohn Baldwin 	uint32_t mac_addr;
669838cc658fSJohn Baldwin 
669973635418SPyun YongHyeon 	mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
670038cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
670138cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
670238cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
670373635418SPyun YongHyeon 		mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
670438cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
670538cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
670638cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
670738cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
67085fea260fSMarius Strobl 		return (0);
670938cc658fSJohn Baldwin 	}
67105fea260fSMarius Strobl 	return (1);
671138cc658fSJohn Baldwin }
671238cc658fSJohn Baldwin 
671338cc658fSJohn Baldwin static int
671438cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
671538cc658fSJohn Baldwin {
671638cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
671738cc658fSJohn Baldwin 
671838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
671938cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
672038cc658fSJohn Baldwin 
67215fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
67225fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
672338cc658fSJohn Baldwin }
672438cc658fSJohn Baldwin 
672538cc658fSJohn Baldwin static int
672638cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
672738cc658fSJohn Baldwin {
672838cc658fSJohn Baldwin 
67295fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
67305fea260fSMarius Strobl 		return (1);
67315fea260fSMarius Strobl 
67325fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
67335fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
673438cc658fSJohn Baldwin }
673538cc658fSJohn Baldwin 
673638cc658fSJohn Baldwin static int
673738cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
673838cc658fSJohn Baldwin {
673938cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
674038cc658fSJohn Baldwin 		/* NOTE: Order is critical */
67415fea260fSMarius Strobl 		bge_get_eaddr_fw,
674238cc658fSJohn Baldwin 		bge_get_eaddr_mem,
674338cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
674438cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
674538cc658fSJohn Baldwin 		NULL
674638cc658fSJohn Baldwin 	};
674738cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
674838cc658fSJohn Baldwin 
674938cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
675038cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
675138cc658fSJohn Baldwin 			break;
675238cc658fSJohn Baldwin 	}
675338cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
675438cc658fSJohn Baldwin }
6755df360178SGleb Smirnoff 
6756df360178SGleb Smirnoff static uint64_t
6757df360178SGleb Smirnoff bge_get_counter(if_t ifp, ift_counter cnt)
6758df360178SGleb Smirnoff {
6759df360178SGleb Smirnoff 	struct bge_softc *sc;
6760df360178SGleb Smirnoff 	struct bge_mac_stats *stats;
6761df360178SGleb Smirnoff 
6762df360178SGleb Smirnoff 	sc = if_getsoftc(ifp);
6763df360178SGleb Smirnoff 	if (!BGE_IS_5705_PLUS(sc))
6764df360178SGleb Smirnoff 		return (if_get_counter_default(ifp, cnt));
6765df360178SGleb Smirnoff 	stats = &sc->bge_mac_stats;
6766df360178SGleb Smirnoff 
6767df360178SGleb Smirnoff 	switch (cnt) {
6768df360178SGleb Smirnoff 	case IFCOUNTER_IERRORS:
6769df360178SGleb Smirnoff 		return (stats->NoMoreRxBDs + stats->InputDiscards +
6770df360178SGleb Smirnoff 		    stats->InputErrors);
6771df360178SGleb Smirnoff 	case IFCOUNTER_COLLISIONS:
6772df360178SGleb Smirnoff 		return (stats->etherStatsCollisions);
6773df360178SGleb Smirnoff 	default:
6774df360178SGleb Smirnoff 		return (if_get_counter_default(ifp, cnt));
6775df360178SGleb Smirnoff 	}
6776df360178SGleb Smirnoff }
6777ded66962SMark Johnston 
67787790c8c1SConrad Meyer #ifdef DEBUGNET
6779ded66962SMark Johnston static void
67807790c8c1SConrad Meyer bge_debugnet_init(if_t ifp, int *nrxr, int *ncl, int *clsize)
6781ded66962SMark Johnston {
6782ded66962SMark Johnston 	struct bge_softc *sc;
6783ded66962SMark Johnston 
6784ded66962SMark Johnston 	sc = if_getsoftc(ifp);
6785ded66962SMark Johnston 	BGE_LOCK(sc);
6786ded66962SMark Johnston 	*nrxr = sc->bge_return_ring_cnt;
67877790c8c1SConrad Meyer 	*ncl = DEBUGNET_MAX_IN_FLIGHT;
6788ded66962SMark Johnston 	if ((sc->bge_flags & BGE_FLAG_JUMBO_STD) != 0 &&
6789ded66962SMark Johnston 	    (if_getmtu(sc->bge_ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
6790ded66962SMark Johnston 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN)))
6791ded66962SMark Johnston 		*clsize = MJUM9BYTES;
6792ded66962SMark Johnston 	else
6793ded66962SMark Johnston 		*clsize = MCLBYTES;
6794ded66962SMark Johnston 	BGE_UNLOCK(sc);
6795ded66962SMark Johnston }
6796ded66962SMark Johnston 
6797ded66962SMark Johnston static void
67987790c8c1SConrad Meyer bge_debugnet_event(if_t ifp __unused, enum debugnet_ev event __unused)
6799ded66962SMark Johnston {
6800ded66962SMark Johnston }
6801ded66962SMark Johnston 
6802ded66962SMark Johnston static int
68037790c8c1SConrad Meyer bge_debugnet_transmit(if_t ifp, struct mbuf *m)
6804ded66962SMark Johnston {
6805ded66962SMark Johnston 	struct bge_softc *sc;
6806ded66962SMark Johnston 	uint32_t prodidx;
6807ded66962SMark Johnston 	int error;
6808ded66962SMark Johnston 
6809ded66962SMark Johnston 	sc = if_getsoftc(ifp);
6810ded66962SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
6811ded66962SMark Johnston 	    IFF_DRV_RUNNING)
6812ded66962SMark Johnston 		return (1);
6813ded66962SMark Johnston 
6814ded66962SMark Johnston 	prodidx = sc->bge_tx_prodidx;
6815ded66962SMark Johnston 	error = bge_encap(sc, &m, &prodidx);
6816ded66962SMark Johnston 	if (error == 0)
6817ded66962SMark Johnston 		bge_start_tx(sc, prodidx);
6818ded66962SMark Johnston 	return (error);
6819ded66962SMark Johnston }
6820ded66962SMark Johnston 
6821ded66962SMark Johnston static int
68227790c8c1SConrad Meyer bge_debugnet_poll(if_t ifp, int count)
6823ded66962SMark Johnston {
6824ded66962SMark Johnston 	struct bge_softc *sc;
6825ded66962SMark Johnston 	uint32_t rx_prod, tx_cons;
6826ded66962SMark Johnston 
6827ded66962SMark Johnston 	sc = if_getsoftc(ifp);
6828ded66962SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
6829ded66962SMark Johnston 	    IFF_DRV_RUNNING)
6830ded66962SMark Johnston 		return (1);
6831ded66962SMark Johnston 
6832ded66962SMark Johnston 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
6833ded66962SMark Johnston 	    sc->bge_cdata.bge_status_map,
6834ded66962SMark Johnston 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
6835ded66962SMark Johnston 
6836ded66962SMark Johnston 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
6837ded66962SMark Johnston 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
6838ded66962SMark Johnston 
6839ded66962SMark Johnston 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
6840ded66962SMark Johnston 	    sc->bge_cdata.bge_status_map,
6841ded66962SMark Johnston 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
6842ded66962SMark Johnston 
6843ded66962SMark Johnston 	(void)bge_rxeof(sc, rx_prod, 0);
6844ded66962SMark Johnston 	bge_txeof(sc, tx_cons);
6845ded66962SMark Johnston 	return (0);
6846ded66962SMark Johnston }
68477790c8c1SConrad Meyer #endif /* DEBUGNET */
6848