xref: /freebsd/sys/dev/bge/if_bge.c (revision ded669627a68f64f5253e2cc080089214f27ca0c)
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 
8795d67482SBill Paul #include <net/if.h>
8876039bc8SGleb Smirnoff #include <net/if_var.h>
8995d67482SBill Paul #include <net/if_arp.h>
9095d67482SBill Paul #include <net/ethernet.h>
9195d67482SBill Paul #include <net/if_dl.h>
9295d67482SBill Paul #include <net/if_media.h>
9395d67482SBill Paul 
9495d67482SBill Paul #include <net/bpf.h>
9595d67482SBill Paul 
9695d67482SBill Paul #include <net/if_types.h>
9795d67482SBill Paul #include <net/if_vlan_var.h>
9895d67482SBill Paul 
9995d67482SBill Paul #include <netinet/in_systm.h>
10095d67482SBill Paul #include <netinet/in.h>
10195d67482SBill Paul #include <netinet/ip.h>
102ca3f1187SPyun YongHyeon #include <netinet/tcp.h>
103*ded66962SMark Johnston #include <netinet/netdump/netdump.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 
11508013fd3SMarius Strobl #ifdef __sparc64__
11608013fd3SMarius Strobl #include <dev/ofw/ofw_bus.h>
11708013fd3SMarius Strobl #include <dev/ofw/openfirm.h>
11808013fd3SMarius Strobl #include <machine/ofw_machdep.h>
11908013fd3SMarius Strobl #include <machine/ver.h>
12008013fd3SMarius Strobl #endif
12108013fd3SMarius Strobl 
1224fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1234fbd232cSWarner Losh #include <dev/pci/pcivar.h>
12495d67482SBill Paul 
12595d67482SBill Paul #include <dev/bge/if_bgereg.h>
12695d67482SBill Paul 
12735f945cdSPyun YongHyeon #define	BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP)
128d375e524SGleb Smirnoff #define	ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
12995d67482SBill Paul 
130f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
131f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
13295d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
13395d67482SBill Paul 
1347b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
13595d67482SBill Paul #include "miibus_if.h"
13695d67482SBill Paul 
13795d67482SBill Paul /*
13895d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
13995d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
14095d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
14195d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
14295d67482SBill Paul  */
143852c67f9SMarius Strobl static const struct bge_type {
1444c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1454c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
14629658c96SDimitry Andric } bge_devs[] = {
1474c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5700 },
1484c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5701 },
14995d67482SBill Paul 
1504c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1000 },
1514c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1002 },
1524c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC9100 },
1534c0da0ffSGleb Smirnoff 
1544c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	APPLE_DEVICE_BCM5701 },
1554c0da0ffSGleb Smirnoff 
1564c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5700 },
1574c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5701 },
1584c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702 },
1594c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702_ALT },
1604c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702X },
1614c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703 },
1624c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703_ALT },
1634c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703X },
1644c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704C },
1654c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S },
1664c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S_ALT },
1674c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705 },
1684c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705F },
1694c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705K },
1704c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M },
1714c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M_ALT },
1724c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714C },
1734c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714S },
1744c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715 },
1754c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715S },
1761108273aSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5717 },
177cb2404b4SSepherosa Ziehau 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5717C },
1781108273aSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5718 },
179bbe2ca75SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5719 },
1804c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1814c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
182effef978SRemko Lodder 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5722 },
183a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5723 },
1842927f01fSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5725 },
1852927f01fSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5727 },
1864c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1874c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1884c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1894c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1904c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1914c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1924c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1934c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1944c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1954c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1969e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1979e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1989e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1999e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
200f7d1b2ebSXin LI 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5756 },
201a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761 },
202a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761E },
203a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761S },
204a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761SE },
2052927f01fSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5762 },
206a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5764 },
2074c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
2084c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
2094c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
2104c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
211a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5784 },
212a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785F },
213a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785G },
2149e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
2159e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
216a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787F },
2179e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
2184c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
2194c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
2204c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
2214c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
2224c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
22338cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906 },
22438cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906M },
225a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57760 },
226b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57761 },
227fe26ad88SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57762 },
22867129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57764 },
229b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57765 },
230fe26ad88SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57766 },
23167129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57767 },
232a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57780 },
233b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57781 },
23467129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57782 },
235b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57785 },
23667129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57786 },
23767129934SPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57787 },
238a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57788 },
239a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57790 },
240b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57791 },
241b4a256acSPyun YongHyeon 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57795 },
2424c0da0ffSGleb Smirnoff 
2434c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
2444c0da0ffSGleb Smirnoff 
2454c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
2464c0da0ffSGleb Smirnoff 
247a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE4 },
248a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE5 },
249a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PP250450 },
250a5779553SStanislav Sedov 
2514c0da0ffSGleb Smirnoff 	{ 0, 0 }
25295d67482SBill Paul };
25395d67482SBill Paul 
2544c0da0ffSGleb Smirnoff static const struct bge_vendor {
2554c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2564c0da0ffSGleb Smirnoff 	const char	*v_name;
25729658c96SDimitry Andric } bge_vendors[] = {
2584c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2594c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2604c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2614c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2624c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2634c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
264a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	"Fujitsu" },
2654c0da0ffSGleb Smirnoff 
2664c0da0ffSGleb Smirnoff 	{ 0, NULL }
2674c0da0ffSGleb Smirnoff };
2684c0da0ffSGleb Smirnoff 
2694c0da0ffSGleb Smirnoff static const struct bge_revision {
2704c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2714c0da0ffSGleb Smirnoff 	const char	*br_name;
27229658c96SDimitry Andric } bge_revisions[] = {
2734c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2744c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2754c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2764c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2774c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2784c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2794c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2804c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2814c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2824c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2834c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2844c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2854c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2864c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2874c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2884c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2899e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2904c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2914c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2924c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2934c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2944c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2954c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2964c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2974c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2984c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2994c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
3004c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
3014c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
3024c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
3034c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
3044c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
3054c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
30642787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
3074c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
3084c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
3094c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
3104c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
3114c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
3124c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
3134c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
3144c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
3150c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5715_A3,	"BCM5715 A3" },
3161108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_A0,	"BCM5717 A0" },
3171108273aSPyun YongHyeon 	{ BGE_CHIPID_BCM5717_B0,	"BCM5717 B0" },
318cb2404b4SSepherosa Ziehau 	{ BGE_CHIPID_BCM5717_C0,	"BCM5717 C0" },
319bbe2ca75SPyun YongHyeon 	{ BGE_CHIPID_BCM5719_A0,	"BCM5719 A0" },
32050515680SPyun YongHyeon 	{ BGE_CHIPID_BCM5720_A0,	"BCM5720 A0" },
3210c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A0,	"BCM5755 A0" },
3220c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A1,	"BCM5755 A1" },
3230c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A2,	"BCM5755 A2" },
324bcc20328SJohn Baldwin 	{ BGE_CHIPID_BCM5722_A0,	"BCM5722 A0" },
325a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A0,	"BCM5761 A0" },
326a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A1,	"BCM5761 A1" },
3272927f01fSPyun YongHyeon 	{ BGE_CHIPID_BCM5762_A0,	"BCM5762 A0" },
328a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A0,	"BCM5784 A0" },
329a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A1,	"BCM5784 A1" },
33081179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3316f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
3326f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
3336f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
33438cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A1,	"BCM5906 A1" },
33538cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A2,	"BCM5906 A2" },
336b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_A0,	"BCM57765 A0" },
337b4a256acSPyun YongHyeon 	{ BGE_CHIPID_BCM57765_B0,	"BCM57765 B0" },
338a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A0,	"BCM57780 A0" },
339a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A1,	"BCM57780 A1" },
3404c0da0ffSGleb Smirnoff 
3414c0da0ffSGleb Smirnoff 	{ 0, NULL }
3424c0da0ffSGleb Smirnoff };
3434c0da0ffSGleb Smirnoff 
3444c0da0ffSGleb Smirnoff /*
3454c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
3464c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
3474c0da0ffSGleb Smirnoff  */
34829658c96SDimitry Andric static const struct bge_revision bge_majorrevs[] = {
3499e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
3509e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
3519e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
3529e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
3539e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
3549e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
3559e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
3569e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
3579e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
3589e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
3599e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
360a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5761,		"unknown BCM5761" },
361a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5784,		"unknown BCM5784" },
362a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5785,		"unknown BCM5785" },
36381179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3646f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
36538cc658fSJohn Baldwin 	{ BGE_ASICREV_BCM5906,		"unknown BCM5906" },
366b4a256acSPyun YongHyeon 	{ BGE_ASICREV_BCM57765,		"unknown BCM57765" },
367fe26ad88SPyun YongHyeon 	{ BGE_ASICREV_BCM57766,		"unknown BCM57766" },
368a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM57780,		"unknown BCM57780" },
3691108273aSPyun YongHyeon 	{ BGE_ASICREV_BCM5717,		"unknown BCM5717" },
370bbe2ca75SPyun YongHyeon 	{ BGE_ASICREV_BCM5719,		"unknown BCM5719" },
37150515680SPyun YongHyeon 	{ BGE_ASICREV_BCM5720,		"unknown BCM5720" },
3722927f01fSPyun YongHyeon 	{ BGE_ASICREV_BCM5762,		"unknown BCM5762" },
3734c0da0ffSGleb Smirnoff 
3744c0da0ffSGleb Smirnoff 	{ 0, NULL }
3754c0da0ffSGleb Smirnoff };
3764c0da0ffSGleb Smirnoff 
3770c8aa4eaSJung-uk Kim #define	BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
3780c8aa4eaSJung-uk Kim #define	BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
3790c8aa4eaSJung-uk Kim #define	BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
3800c8aa4eaSJung-uk Kim #define	BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
3810c8aa4eaSJung-uk Kim #define	BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
382a5779553SStanislav Sedov #define	BGE_IS_5755_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5755_PLUS)
3831108273aSPyun YongHyeon #define	BGE_IS_5717_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5717_PLUS)
384fe26ad88SPyun YongHyeon #define	BGE_IS_57765_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_57765_PLUS)
3854c0da0ffSGleb Smirnoff 
386d7acafa1SMarius Strobl static uint32_t bge_chipid(device_t);
387d7acafa1SMarius Strobl static const struct bge_vendor * bge_lookup_vendor(uint16_t);
388d7acafa1SMarius Strobl static const struct bge_revision * bge_lookup_rev(uint32_t);
38938cc658fSJohn Baldwin 
39038cc658fSJohn Baldwin typedef int	(*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
39138cc658fSJohn Baldwin 
392e51a25f8SAlfred Perlstein static int bge_probe(device_t);
393e51a25f8SAlfred Perlstein static int bge_attach(device_t);
394e51a25f8SAlfred Perlstein static int bge_detach(device_t);
39514afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
39614afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3973f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
398f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
3995b610048SPyun YongHyeon static int bge_dma_alloc(struct bge_softc *);
400f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
4015b610048SPyun YongHyeon static int bge_dma_ring_alloc(struct bge_softc *, bus_size_t, bus_size_t,
4025b610048SPyun YongHyeon     bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
403f41ac2beSBill Paul 
404ea9c3a30SPyun YongHyeon static void bge_devinfo(struct bge_softc *);
405062af0b0SPyun YongHyeon static int bge_mbox_reorder(struct bge_softc *);
406062af0b0SPyun YongHyeon 
4075fea260fSMarius Strobl static int bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[]);
40838cc658fSJohn Baldwin static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
40938cc658fSJohn Baldwin static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
41038cc658fSJohn Baldwin static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
41138cc658fSJohn Baldwin static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
41238cc658fSJohn Baldwin 
413b9c05fa5SPyun YongHyeon static void bge_txeof(struct bge_softc *, uint16_t);
4141108273aSPyun YongHyeon static void bge_rxcsum(struct bge_softc *, struct bge_rx_bd *, struct mbuf *);
415dfe0df9aSPyun YongHyeon static int bge_rxeof(struct bge_softc *, uint16_t, int);
41695d67482SBill Paul 
4178cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
418e51a25f8SAlfred Perlstein static void bge_tick(void *);
4192280c16bSPyun YongHyeon static void bge_stats_clear_regs(struct bge_softc *);
420e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
4213f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
422d598b626SPyun YongHyeon static struct mbuf *bge_check_short_dma(struct mbuf *);
4232e1d4df4SPyun YongHyeon static struct mbuf *bge_setup_tso(struct bge_softc *, struct mbuf *,
4241108273aSPyun YongHyeon     uint16_t *, uint16_t *);
425676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
42695d67482SBill Paul 
427e51a25f8SAlfred Perlstein static void bge_intr(void *);
428dfe0df9aSPyun YongHyeon static int bge_msi_intr(void *);
429dfe0df9aSPyun YongHyeon static void bge_intr_task(void *, int);
430fba8b109SMarcel Moolenaar static void bge_start(if_t);
431*ded66962SMark Johnston static void bge_start_locked(if_t);
432*ded66962SMark Johnston static void bge_start_tx(struct bge_softc *, uint32_t);
433fba8b109SMarcel Moolenaar static int bge_ioctl(if_t, u_long, caddr_t);
4340f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
435e51a25f8SAlfred Perlstein static void bge_init(void *);
4365a147ba6SPyun YongHyeon static void bge_stop_block(struct bge_softc *, bus_size_t, uint32_t);
437e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
438b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
439b6c974e8SWarner Losh static int bge_shutdown(device_t);
440fba8b109SMarcel Moolenaar static int bge_ifmedia_upd_locked(if_t);
441fba8b109SMarcel Moolenaar static int bge_ifmedia_upd(if_t);
442fba8b109SMarcel Moolenaar static void bge_ifmedia_sts(if_t, struct ifmediareq *);
443df360178SGleb Smirnoff static uint64_t bge_get_counter(if_t, ift_counter);
44495d67482SBill Paul 
44538cc658fSJohn Baldwin static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
44638cc658fSJohn Baldwin static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
44738cc658fSJohn Baldwin 
4483f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
449e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
45095d67482SBill Paul 
4513e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
452e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
453cb2eacc7SYaroslav Tykhiy static void bge_setvlan(struct bge_softc *);
45495d67482SBill Paul 
455e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_std(struct bge_softc *, int);
456e0b7b101SPyun YongHyeon static __inline void bge_rxreuse_jumbo(struct bge_softc *, int);
457943787f3SPyun YongHyeon static int bge_newbuf_std(struct bge_softc *, int);
458943787f3SPyun YongHyeon static int bge_newbuf_jumbo(struct bge_softc *, int);
459e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
460e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
461e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
462e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
463e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
464e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
46595d67482SBill Paul 
466e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
467e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
46850515680SPyun YongHyeon static uint32_t bge_dma_swap_options(struct bge_softc *);
46995d67482SBill Paul 
4705fea260fSMarius Strobl static int bge_has_eaddr(struct bge_softc *);
4713f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
472e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
47338cc658fSJohn Baldwin static void bge_writembx(struct bge_softc *, int, int);
47495d67482SBill Paul #ifdef notdef
4753f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
47695d67482SBill Paul #endif
4779ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
478e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
47995d67482SBill Paul 
480e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
481e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
482e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
48375719184SGleb Smirnoff #ifdef DEVICE_POLLING
484fba8b109SMarcel Moolenaar static int bge_poll(if_t ifp, enum poll_cmd cmd, int count);
48575719184SGleb Smirnoff #endif
48695d67482SBill Paul 
487548c8f1aSPyun YongHyeon #define	BGE_RESET_SHUTDOWN	0
4888cb1383cSDoug Ambrisko #define	BGE_RESET_START		1
489548c8f1aSPyun YongHyeon #define	BGE_RESET_SUSPEND	2
4908cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
4918cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
4928cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
493797ab05eSPyun YongHyeon static void bge_stop_fw(struct bge_softc *);
4948cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
495dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
49695d67482SBill Paul 
497548c8f1aSPyun YongHyeon static void bge_ape_lock_init(struct bge_softc *);
498548c8f1aSPyun YongHyeon static void bge_ape_read_fw_ver(struct bge_softc *);
499548c8f1aSPyun YongHyeon static int bge_ape_lock(struct bge_softc *, int);
500548c8f1aSPyun YongHyeon static void bge_ape_unlock(struct bge_softc *, int);
501548c8f1aSPyun YongHyeon static void bge_ape_send_event(struct bge_softc *, uint32_t);
502548c8f1aSPyun YongHyeon static void bge_ape_driver_state_change(struct bge_softc *, int);
503548c8f1aSPyun YongHyeon 
5046f8718a3SScott Long /*
5056f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
5066f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
5076f8718a3SScott Long  * traps on certain architectures.
5086f8718a3SScott Long  */
5096f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
5106f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
5116f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
512548c8f1aSPyun YongHyeon static int bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS);
5136f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
5146f8718a3SScott Long #endif
5156f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
5162280c16bSPyun YongHyeon static void bge_add_sysctl_stats_regs(struct bge_softc *,
5172280c16bSPyun YongHyeon     struct sysctl_ctx_list *, struct sysctl_oid_list *);
5182280c16bSPyun YongHyeon static void bge_add_sysctl_stats(struct bge_softc *, struct sysctl_ctx_list *,
5192280c16bSPyun YongHyeon     struct sysctl_oid_list *);
520763757b2SScott Long static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
5216f8718a3SScott Long 
522*ded66962SMark Johnston NETDUMP_DEFINE(bge);
523*ded66962SMark Johnston 
52495d67482SBill Paul static device_method_t bge_methods[] = {
52595d67482SBill Paul 	/* Device interface */
52695d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
52795d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
52895d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
52995d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
53014afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
53114afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
53295d67482SBill Paul 
53395d67482SBill Paul 	/* MII interface */
53495d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
53595d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
53695d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
53795d67482SBill Paul 
5384b7ec270SMarius Strobl 	DEVMETHOD_END
53995d67482SBill Paul };
54095d67482SBill Paul 
54195d67482SBill Paul static driver_t bge_driver = {
54295d67482SBill Paul 	"bge",
54395d67482SBill Paul 	bge_methods,
54495d67482SBill Paul 	sizeof(struct bge_softc)
54595d67482SBill Paul };
54695d67482SBill Paul 
54795d67482SBill Paul static devclass_t bge_devclass;
54895d67482SBill Paul 
549f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
55095d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
55195d67482SBill Paul 
552f1a7e6d5SScott Long static int bge_allow_asf = 1;
553f1a7e6d5SScott Long 
5546472ac3dSEd Schouten static SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
555af3b2549SHans Petter Selasky SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RDTUN, &bge_allow_asf, 0,
556f1a7e6d5SScott Long 	"Allow ASF mode if available");
557c4529f41SMichael Reifenberger 
55808013fd3SMarius Strobl #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
55908013fd3SMarius Strobl #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
56008013fd3SMarius Strobl #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
56108013fd3SMarius Strobl #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
56208013fd3SMarius Strobl #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
56308013fd3SMarius Strobl 
56408013fd3SMarius Strobl static int
5655fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
56608013fd3SMarius Strobl {
56708013fd3SMarius Strobl #ifdef __sparc64__
56808013fd3SMarius Strobl 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
56908013fd3SMarius Strobl 	device_t dev;
57008013fd3SMarius Strobl 	uint32_t subvendor;
57108013fd3SMarius Strobl 
57208013fd3SMarius Strobl 	dev = sc->bge_dev;
57308013fd3SMarius Strobl 
57408013fd3SMarius Strobl 	/*
57508013fd3SMarius Strobl 	 * The on-board BGEs found in sun4u machines aren't fitted with
57608013fd3SMarius Strobl 	 * an EEPROM which means that we have to obtain the MAC address
57708013fd3SMarius Strobl 	 * via OFW and that some tests will always fail.  We distinguish
57808013fd3SMarius Strobl 	 * such BGEs by the subvendor ID, which also has to be obtained
57908013fd3SMarius Strobl 	 * from OFW instead of the PCI configuration space as the latter
58008013fd3SMarius Strobl 	 * indicates Broadcom as the subvendor of the netboot interface.
58108013fd3SMarius Strobl 	 * For early Blade 1500 and 2500 we even have to check the OFW
58208013fd3SMarius Strobl 	 * device path as the subvendor ID always defaults to Broadcom
58308013fd3SMarius Strobl 	 * there.
58408013fd3SMarius Strobl 	 */
58508013fd3SMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
58608013fd3SMarius Strobl 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
5872d857b9bSMarius Strobl 	    (subvendor == FJTSU_VENDORID || subvendor == SUN_VENDORID))
58808013fd3SMarius Strobl 		return (0);
58908013fd3SMarius Strobl 	memset(buf, 0, sizeof(buf));
59008013fd3SMarius Strobl 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
59108013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
59208013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
59308013fd3SMarius Strobl 			return (0);
59408013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
59508013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
59608013fd3SMarius Strobl 			return (0);
59708013fd3SMarius Strobl 	}
59808013fd3SMarius Strobl #endif
59908013fd3SMarius Strobl 	return (1);
60008013fd3SMarius Strobl }
60108013fd3SMarius Strobl 
6023f74909aSGleb Smirnoff static uint32_t
6033f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
60495d67482SBill Paul {
60595d67482SBill Paul 	device_t dev;
6066f8718a3SScott Long 	uint32_t val;
60795d67482SBill Paul 
608a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
609a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
610a4431ebaSPyun YongHyeon 		return (0);
611a4431ebaSPyun YongHyeon 
61295d67482SBill Paul 	dev = sc->bge_dev;
61395d67482SBill Paul 
61495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
6156f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
6166f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
6176f8718a3SScott Long 	return (val);
61895d67482SBill Paul }
61995d67482SBill Paul 
62095d67482SBill Paul static void
6213f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
62295d67482SBill Paul {
62395d67482SBill Paul 	device_t dev;
62495d67482SBill Paul 
625a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
626a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
627a4431ebaSPyun YongHyeon 		return;
628a4431ebaSPyun YongHyeon 
62995d67482SBill Paul 	dev = sc->bge_dev;
63095d67482SBill Paul 
63195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
63295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
6336f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
63495d67482SBill Paul }
63595d67482SBill Paul 
63695d67482SBill Paul #ifdef notdef
6373f74909aSGleb Smirnoff static uint32_t
6383f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
63995d67482SBill Paul {
64095d67482SBill Paul 	device_t dev;
64195d67482SBill Paul 
64295d67482SBill Paul 	dev = sc->bge_dev;
64395d67482SBill Paul 
64495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
64595d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
64695d67482SBill Paul }
64795d67482SBill Paul #endif
64895d67482SBill Paul 
64995d67482SBill Paul static void
6503f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
65195d67482SBill Paul {
65295d67482SBill Paul 	device_t dev;
65395d67482SBill Paul 
65495d67482SBill Paul 	dev = sc->bge_dev;
65595d67482SBill Paul 
65695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
65795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
65895d67482SBill Paul }
65995d67482SBill Paul 
6606f8718a3SScott Long static void
6616f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
6626f8718a3SScott Long {
6636f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6646f8718a3SScott Long }
6656f8718a3SScott Long 
66638cc658fSJohn Baldwin static void
66738cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
66838cc658fSJohn Baldwin {
66938cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
67038cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
67138cc658fSJohn Baldwin 
67238cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
673062af0b0SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_MBOX_REORDER) != 0)
674062af0b0SPyun YongHyeon 		CSR_READ_4(sc, off);
67538cc658fSJohn Baldwin }
67638cc658fSJohn Baldwin 
677f41ac2beSBill Paul /*
678548c8f1aSPyun YongHyeon  * Clear all stale locks and select the lock for this driver instance.
679548c8f1aSPyun YongHyeon  */
680548c8f1aSPyun YongHyeon static void
681548c8f1aSPyun YongHyeon bge_ape_lock_init(struct bge_softc *sc)
682548c8f1aSPyun YongHyeon {
683548c8f1aSPyun YongHyeon 	uint32_t bit, regbase;
684548c8f1aSPyun YongHyeon 	int i;
685548c8f1aSPyun YongHyeon 
686548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
687548c8f1aSPyun YongHyeon 		regbase = BGE_APE_LOCK_GRANT;
688548c8f1aSPyun YongHyeon 	else
689548c8f1aSPyun YongHyeon 		regbase = BGE_APE_PER_LOCK_GRANT;
690548c8f1aSPyun YongHyeon 
691548c8f1aSPyun YongHyeon 	/* Clear any stale locks. */
692548c8f1aSPyun YongHyeon 	for (i = BGE_APE_LOCK_PHY0; i <= BGE_APE_LOCK_GPIO; i++) {
693548c8f1aSPyun YongHyeon 		switch (i) {
694548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY0:
695548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY1:
696548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY2:
697548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY3:
698548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
699548c8f1aSPyun YongHyeon 			break;
700548c8f1aSPyun YongHyeon 		default:
701bd9c196aSPyun YongHyeon 			if (sc->bge_func_addr == 0)
702548c8f1aSPyun YongHyeon 				bit = BGE_APE_LOCK_GRANT_DRIVER0;
703548c8f1aSPyun YongHyeon 			else
704548c8f1aSPyun YongHyeon 				bit = (1 << sc->bge_func_addr);
705548c8f1aSPyun YongHyeon 		}
706548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, regbase + 4 * i, bit);
707548c8f1aSPyun YongHyeon 	}
708548c8f1aSPyun YongHyeon 
709548c8f1aSPyun YongHyeon 	/* Select the PHY lock based on the device's function number. */
710548c8f1aSPyun YongHyeon 	switch (sc->bge_func_addr) {
711548c8f1aSPyun YongHyeon 	case 0:
712548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY0;
713548c8f1aSPyun YongHyeon 		break;
714548c8f1aSPyun YongHyeon 	case 1:
715548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY1;
716548c8f1aSPyun YongHyeon 		break;
717548c8f1aSPyun YongHyeon 	case 2:
718548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY2;
719548c8f1aSPyun YongHyeon 		break;
720548c8f1aSPyun YongHyeon 	case 3:
721548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY3;
722548c8f1aSPyun YongHyeon 		break;
723548c8f1aSPyun YongHyeon 	default:
724548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev,
725548c8f1aSPyun YongHyeon 		    "PHY lock not supported on this function\n");
726548c8f1aSPyun YongHyeon 	}
727548c8f1aSPyun YongHyeon }
728548c8f1aSPyun YongHyeon 
729548c8f1aSPyun YongHyeon /*
730548c8f1aSPyun YongHyeon  * Check for APE firmware, set flags, and print version info.
731548c8f1aSPyun YongHyeon  */
732548c8f1aSPyun YongHyeon static void
733548c8f1aSPyun YongHyeon bge_ape_read_fw_ver(struct bge_softc *sc)
734548c8f1aSPyun YongHyeon {
735548c8f1aSPyun YongHyeon 	const char *fwtype;
736548c8f1aSPyun YongHyeon 	uint32_t apedata, features;
737548c8f1aSPyun YongHyeon 
738548c8f1aSPyun YongHyeon 	/* Check for a valid APE signature in shared memory. */
739548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_SEG_SIG);
740548c8f1aSPyun YongHyeon 	if (apedata != BGE_APE_SEG_SIG_MAGIC) {
741548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags &= ~ BGE_MFW_ON_APE;
742548c8f1aSPyun YongHyeon 		return;
743548c8f1aSPyun YongHyeon 	}
744548c8f1aSPyun YongHyeon 
745548c8f1aSPyun YongHyeon 	/* Check if APE firmware is running. */
746548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_STATUS);
747548c8f1aSPyun YongHyeon 	if ((apedata & BGE_APE_FW_STATUS_READY) == 0) {
748548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE signature found "
749548c8f1aSPyun YongHyeon 		    "but FW status not ready! 0x%08x\n", apedata);
750548c8f1aSPyun YongHyeon 		return;
751548c8f1aSPyun YongHyeon 	}
752548c8f1aSPyun YongHyeon 
753548c8f1aSPyun YongHyeon 	sc->bge_mfw_flags |= BGE_MFW_ON_APE;
754548c8f1aSPyun YongHyeon 
755548c8f1aSPyun YongHyeon 	/* Fetch the APE firwmare type and version. */
756548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_VERSION);
757548c8f1aSPyun YongHyeon 	features = APE_READ_4(sc, BGE_APE_FW_FEATURES);
758548c8f1aSPyun YongHyeon 	if ((features & BGE_APE_FW_FEATURE_NCSI) != 0) {
759548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_NCSI;
760548c8f1aSPyun YongHyeon 		fwtype = "NCSI";
761548c8f1aSPyun YongHyeon 	} else if ((features & BGE_APE_FW_FEATURE_DASH) != 0) {
762548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_DASH;
763548c8f1aSPyun YongHyeon 		fwtype = "DASH";
764548c8f1aSPyun YongHyeon 	} else
765548c8f1aSPyun YongHyeon 		fwtype = "UNKN";
766548c8f1aSPyun YongHyeon 
767548c8f1aSPyun YongHyeon 	/* Print the APE firmware version. */
768548c8f1aSPyun YongHyeon 	device_printf(sc->bge_dev, "APE FW version: %s v%d.%d.%d.%d\n",
769548c8f1aSPyun YongHyeon 	    fwtype,
770548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MAJMSK) >> BGE_APE_FW_VERSION_MAJSFT,
771548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MINMSK) >> BGE_APE_FW_VERSION_MINSFT,
772548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_REVMSK) >> BGE_APE_FW_VERSION_REVSFT,
773548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_BLDMSK));
774548c8f1aSPyun YongHyeon }
775548c8f1aSPyun YongHyeon 
776548c8f1aSPyun YongHyeon static int
777548c8f1aSPyun YongHyeon bge_ape_lock(struct bge_softc *sc, int locknum)
778548c8f1aSPyun YongHyeon {
779548c8f1aSPyun YongHyeon 	uint32_t bit, gnt, req, status;
780548c8f1aSPyun YongHyeon 	int i, off;
781548c8f1aSPyun YongHyeon 
782548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
783548c8f1aSPyun YongHyeon 		return (0);
784548c8f1aSPyun YongHyeon 
785548c8f1aSPyun YongHyeon 	/* Lock request/grant registers have different bases. */
786548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761) {
787548c8f1aSPyun YongHyeon 		req = BGE_APE_LOCK_REQ;
788548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
789548c8f1aSPyun YongHyeon 	} else {
790548c8f1aSPyun YongHyeon 		req = BGE_APE_PER_LOCK_REQ;
791548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
792548c8f1aSPyun YongHyeon 	}
793548c8f1aSPyun YongHyeon 
794548c8f1aSPyun YongHyeon 	off = 4 * locknum;
795548c8f1aSPyun YongHyeon 
796548c8f1aSPyun YongHyeon 	switch (locknum) {
797548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
798548c8f1aSPyun YongHyeon 		/* Lock required when using GPIO. */
799548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
800548c8f1aSPyun YongHyeon 			return (0);
801548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
802548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
803548c8f1aSPyun YongHyeon 		else
804548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
805548c8f1aSPyun YongHyeon 		break;
806548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
807548c8f1aSPyun YongHyeon 		/* Lock required to reset the device. */
808548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
809548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
810548c8f1aSPyun YongHyeon 		else
811548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
812548c8f1aSPyun YongHyeon 		break;
813548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
814548c8f1aSPyun YongHyeon 		/* Lock required when accessing certain APE memory. */
815548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
816548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
817548c8f1aSPyun YongHyeon 		else
818548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
819548c8f1aSPyun YongHyeon 		break;
820548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
821548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
822548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
823548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
824548c8f1aSPyun YongHyeon 		/* Lock required when accessing PHYs. */
825548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_REQ_DRIVER0;
826548c8f1aSPyun YongHyeon 		break;
827548c8f1aSPyun YongHyeon 	default:
828548c8f1aSPyun YongHyeon 		return (EINVAL);
829548c8f1aSPyun YongHyeon 	}
830548c8f1aSPyun YongHyeon 
831548c8f1aSPyun YongHyeon 	/* Request a lock. */
832548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, req + off, bit);
833548c8f1aSPyun YongHyeon 
834548c8f1aSPyun YongHyeon 	/* Wait up to 1 second to acquire lock. */
835548c8f1aSPyun YongHyeon 	for (i = 0; i < 20000; i++) {
836548c8f1aSPyun YongHyeon 		status = APE_READ_4(sc, gnt + off);
837548c8f1aSPyun YongHyeon 		if (status == bit)
838548c8f1aSPyun YongHyeon 			break;
839548c8f1aSPyun YongHyeon 		DELAY(50);
840548c8f1aSPyun YongHyeon 	}
841548c8f1aSPyun YongHyeon 
842548c8f1aSPyun YongHyeon 	/* Handle any errors. */
843548c8f1aSPyun YongHyeon 	if (status != bit) {
844548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE lock %d request failed! "
845548c8f1aSPyun YongHyeon 		    "request = 0x%04x[0x%04x], status = 0x%04x[0x%04x]\n",
846548c8f1aSPyun YongHyeon 		    locknum, req + off, bit & 0xFFFF, gnt + off,
847548c8f1aSPyun YongHyeon 		    status & 0xFFFF);
848548c8f1aSPyun YongHyeon 		/* Revoke the lock request. */
849548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, gnt + off, bit);
850548c8f1aSPyun YongHyeon 		return (EBUSY);
851548c8f1aSPyun YongHyeon 	}
852548c8f1aSPyun YongHyeon 
853548c8f1aSPyun YongHyeon 	return (0);
854548c8f1aSPyun YongHyeon }
855548c8f1aSPyun YongHyeon 
856548c8f1aSPyun YongHyeon static void
857548c8f1aSPyun YongHyeon bge_ape_unlock(struct bge_softc *sc, int locknum)
858548c8f1aSPyun YongHyeon {
859548c8f1aSPyun YongHyeon 	uint32_t bit, gnt;
860548c8f1aSPyun YongHyeon 	int off;
861548c8f1aSPyun YongHyeon 
862548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
863548c8f1aSPyun YongHyeon 		return;
864548c8f1aSPyun YongHyeon 
865548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
866548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
867548c8f1aSPyun YongHyeon 	else
868548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
869548c8f1aSPyun YongHyeon 
870548c8f1aSPyun YongHyeon 	off = 4 * locknum;
871548c8f1aSPyun YongHyeon 
872548c8f1aSPyun YongHyeon 	switch (locknum) {
873548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
874548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
875548c8f1aSPyun YongHyeon 			return;
876548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
877548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
878548c8f1aSPyun YongHyeon 		else
879548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
880548c8f1aSPyun YongHyeon 		break;
881548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
882548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
883548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
884548c8f1aSPyun YongHyeon 		else
885548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
886548c8f1aSPyun YongHyeon 		break;
887548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
888548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
889548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
890548c8f1aSPyun YongHyeon 		else
891548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
892548c8f1aSPyun YongHyeon 		break;
893548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
894548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
895548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
896548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
897548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_GRANT_DRIVER0;
898548c8f1aSPyun YongHyeon 		break;
899548c8f1aSPyun YongHyeon 	default:
900548c8f1aSPyun YongHyeon 		return;
901548c8f1aSPyun YongHyeon 	}
902548c8f1aSPyun YongHyeon 
903548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, gnt + off, bit);
904548c8f1aSPyun YongHyeon }
905548c8f1aSPyun YongHyeon 
906548c8f1aSPyun YongHyeon /*
907548c8f1aSPyun YongHyeon  * Send an event to the APE firmware.
908548c8f1aSPyun YongHyeon  */
909548c8f1aSPyun YongHyeon static void
910548c8f1aSPyun YongHyeon bge_ape_send_event(struct bge_softc *sc, uint32_t event)
911548c8f1aSPyun YongHyeon {
912548c8f1aSPyun YongHyeon 	uint32_t apedata;
913548c8f1aSPyun YongHyeon 	int i;
914548c8f1aSPyun YongHyeon 
915548c8f1aSPyun YongHyeon 	/* NCSI does not support APE events. */
916548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
917548c8f1aSPyun YongHyeon 		return;
918548c8f1aSPyun YongHyeon 
919548c8f1aSPyun YongHyeon 	/* Wait up to 1ms for APE to service previous event. */
920548c8f1aSPyun YongHyeon 	for (i = 10; i > 0; i--) {
921548c8f1aSPyun YongHyeon 		if (bge_ape_lock(sc, BGE_APE_LOCK_MEM) != 0)
922548c8f1aSPyun YongHyeon 			break;
923548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_EVENT_STATUS);
924548c8f1aSPyun YongHyeon 		if ((apedata & BGE_APE_EVENT_STATUS_EVENT_PENDING) == 0) {
925548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT_STATUS, event |
926548c8f1aSPyun YongHyeon 			    BGE_APE_EVENT_STATUS_EVENT_PENDING);
927548c8f1aSPyun YongHyeon 			bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
928548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT, BGE_APE_EVENT_1);
929548c8f1aSPyun YongHyeon 			break;
930548c8f1aSPyun YongHyeon 		}
931548c8f1aSPyun YongHyeon 		bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
932548c8f1aSPyun YongHyeon 		DELAY(100);
933548c8f1aSPyun YongHyeon 	}
934548c8f1aSPyun YongHyeon 	if (i == 0)
935548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE event 0x%08x send timed out\n",
936548c8f1aSPyun YongHyeon 		    event);
937548c8f1aSPyun YongHyeon }
938548c8f1aSPyun YongHyeon 
939548c8f1aSPyun YongHyeon static void
940548c8f1aSPyun YongHyeon bge_ape_driver_state_change(struct bge_softc *sc, int kind)
941548c8f1aSPyun YongHyeon {
942548c8f1aSPyun YongHyeon 	uint32_t apedata, event;
943548c8f1aSPyun YongHyeon 
944548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
945548c8f1aSPyun YongHyeon 		return;
946548c8f1aSPyun YongHyeon 
947548c8f1aSPyun YongHyeon 	switch (kind) {
948548c8f1aSPyun YongHyeon 	case BGE_RESET_START:
949548c8f1aSPyun YongHyeon 		/* If this is the first load, clear the load counter. */
950548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_HOST_SEG_SIG);
951548c8f1aSPyun YongHyeon 		if (apedata != BGE_APE_HOST_SEG_SIG_MAGIC)
952548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, 0);
953548c8f1aSPyun YongHyeon 		else {
954548c8f1aSPyun YongHyeon 			apedata = APE_READ_4(sc, BGE_APE_HOST_INIT_COUNT);
955548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, ++apedata);
956548c8f1aSPyun YongHyeon 		}
957548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_SIG,
958548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_SIG_MAGIC);
959548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_LEN,
960548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_LEN_MAGIC);
961548c8f1aSPyun YongHyeon 
962548c8f1aSPyun YongHyeon 		/* Add some version info if bge(4) supports it. */
963548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRIVER_ID,
964548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRIVER_ID_MAGIC(1, 0));
965548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_BEHAVIOR,
966548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_BEHAV_NO_PHYLOCK);
967548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_HEARTBEAT_INT_MS,
968548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_HEARTBEAT_INT_DISABLE);
969548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
970548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_START);
971548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_START;
972548c8f1aSPyun YongHyeon 		break;
973548c8f1aSPyun YongHyeon 	case BGE_RESET_SHUTDOWN:
974548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
975548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_UNLOAD);
976548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_UNLOAD;
977548c8f1aSPyun YongHyeon 		break;
978548c8f1aSPyun YongHyeon 	case BGE_RESET_SUSPEND:
979548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_SUSPEND;
980548c8f1aSPyun YongHyeon 		break;
981548c8f1aSPyun YongHyeon 	default:
982548c8f1aSPyun YongHyeon 		return;
983548c8f1aSPyun YongHyeon 	}
984548c8f1aSPyun YongHyeon 
985548c8f1aSPyun YongHyeon 	bge_ape_send_event(sc, event | BGE_APE_EVENT_STATUS_DRIVER_EVNT |
986548c8f1aSPyun YongHyeon 	    BGE_APE_EVENT_STATUS_STATE_CHNGE);
987548c8f1aSPyun YongHyeon }
988548c8f1aSPyun YongHyeon 
989548c8f1aSPyun YongHyeon /*
990f41ac2beSBill Paul  * Map a single buffer address.
991f41ac2beSBill Paul  */
992f41ac2beSBill Paul 
993f41ac2beSBill Paul static void
9943f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
995f41ac2beSBill Paul {
996f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
997f41ac2beSBill Paul 
998f41ac2beSBill Paul 	if (error)
999f41ac2beSBill Paul 		return;
1000f41ac2beSBill Paul 
10015b610048SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
10025b610048SPyun YongHyeon 
1003f41ac2beSBill Paul 	ctx = arg;
1004f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
1005f41ac2beSBill Paul }
1006f41ac2beSBill Paul 
100738cc658fSJohn Baldwin static uint8_t
100838cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
100938cc658fSJohn Baldwin {
101038cc658fSJohn Baldwin 	uint32_t access, byte = 0;
101138cc658fSJohn Baldwin 	int i;
101238cc658fSJohn Baldwin 
101338cc658fSJohn Baldwin 	/* Lock. */
101438cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
101538cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
101638cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
101738cc658fSJohn Baldwin 			break;
101838cc658fSJohn Baldwin 		DELAY(20);
101938cc658fSJohn Baldwin 	}
102038cc658fSJohn Baldwin 	if (i == 8000)
102138cc658fSJohn Baldwin 		return (1);
102238cc658fSJohn Baldwin 
102338cc658fSJohn Baldwin 	/* Enable access. */
102438cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
102538cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
102638cc658fSJohn Baldwin 
102738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
102838cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
102938cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
103038cc658fSJohn Baldwin 		DELAY(10);
103138cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
103238cc658fSJohn Baldwin 			DELAY(10);
103338cc658fSJohn Baldwin 			break;
103438cc658fSJohn Baldwin 		}
103538cc658fSJohn Baldwin 	}
103638cc658fSJohn Baldwin 
103738cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
103838cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
103938cc658fSJohn Baldwin 		return (1);
104038cc658fSJohn Baldwin 	}
104138cc658fSJohn Baldwin 
104238cc658fSJohn Baldwin 	/* Get result. */
104338cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
104438cc658fSJohn Baldwin 
104538cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
104638cc658fSJohn Baldwin 
104738cc658fSJohn Baldwin 	/* Disable access. */
104838cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
104938cc658fSJohn Baldwin 
105038cc658fSJohn Baldwin 	/* Unlock. */
105138cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
105238cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
105338cc658fSJohn Baldwin 
105438cc658fSJohn Baldwin 	return (0);
105538cc658fSJohn Baldwin }
105638cc658fSJohn Baldwin 
105738cc658fSJohn Baldwin /*
105838cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
105938cc658fSJohn Baldwin  */
106038cc658fSJohn Baldwin static int
106138cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
106238cc658fSJohn Baldwin {
106338cc658fSJohn Baldwin 	int err = 0, i;
106438cc658fSJohn Baldwin 	uint8_t byte = 0;
106538cc658fSJohn Baldwin 
106638cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
106738cc658fSJohn Baldwin 		return (1);
106838cc658fSJohn Baldwin 
106938cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
107038cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
107138cc658fSJohn Baldwin 		if (err)
107238cc658fSJohn Baldwin 			break;
107338cc658fSJohn Baldwin 		*(dest + i) = byte;
107438cc658fSJohn Baldwin 	}
107538cc658fSJohn Baldwin 
107638cc658fSJohn Baldwin 	return (err ? 1 : 0);
107738cc658fSJohn Baldwin }
107838cc658fSJohn Baldwin 
107995d67482SBill Paul /*
108095d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
108195d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
108295d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
108395d67482SBill Paul  * access method.
108495d67482SBill Paul  */
10853f74909aSGleb Smirnoff static uint8_t
10863f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
108795d67482SBill Paul {
108895d67482SBill Paul 	int i;
10893f74909aSGleb Smirnoff 	uint32_t byte = 0;
109095d67482SBill Paul 
109195d67482SBill Paul 	/*
109295d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
109395d67482SBill Paul 	 * having to use the bitbang method.
109495d67482SBill Paul 	 */
109595d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
109695d67482SBill Paul 
109795d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
109895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
109995d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
110095d67482SBill Paul 	DELAY(20);
110195d67482SBill Paul 
110295d67482SBill Paul 	/* Issue the read EEPROM command. */
110395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
110495d67482SBill Paul 
110595d67482SBill Paul 	/* Wait for completion */
110695d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
110795d67482SBill Paul 		DELAY(10);
110895d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
110995d67482SBill Paul 			break;
111095d67482SBill Paul 	}
111195d67482SBill Paul 
1112d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
1113fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
1114f6789fbaSPyun YongHyeon 		return (1);
111595d67482SBill Paul 	}
111695d67482SBill Paul 
111795d67482SBill Paul 	/* Get result. */
111895d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
111995d67482SBill Paul 
11200c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
112195d67482SBill Paul 
112295d67482SBill Paul 	return (0);
112395d67482SBill Paul }
112495d67482SBill Paul 
112595d67482SBill Paul /*
112695d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
112795d67482SBill Paul  */
112895d67482SBill Paul static int
11293f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
113095d67482SBill Paul {
11313f74909aSGleb Smirnoff 	int i, error = 0;
11323f74909aSGleb Smirnoff 	uint8_t byte = 0;
113395d67482SBill Paul 
113495d67482SBill Paul 	for (i = 0; i < cnt; i++) {
11353f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
11363f74909aSGleb Smirnoff 		if (error)
113795d67482SBill Paul 			break;
113895d67482SBill Paul 		*(dest + i) = byte;
113995d67482SBill Paul 	}
114095d67482SBill Paul 
11413f74909aSGleb Smirnoff 	return (error ? 1 : 0);
114295d67482SBill Paul }
114395d67482SBill Paul 
114495d67482SBill Paul static int
11453f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
114695d67482SBill Paul {
114795d67482SBill Paul 	struct bge_softc *sc;
1148a813ed78SPyun YongHyeon 	uint32_t val;
114995d67482SBill Paul 	int i;
115095d67482SBill Paul 
115195d67482SBill Paul 	sc = device_get_softc(dev);
115295d67482SBill Paul 
1153548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1154548c8f1aSPyun YongHyeon 		return (0);
1155548c8f1aSPyun YongHyeon 
1156a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1157a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1158a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1159a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1160a813ed78SPyun YongHyeon 		DELAY(80);
116137ceeb4dSPaul Saab 	}
116237ceeb4dSPaul Saab 
116395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
116495d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
116595d67482SBill Paul 
1166a813ed78SPyun YongHyeon 	/* Poll for the PHY register access to complete. */
116795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1168d5d23857SJung-uk Kim 		DELAY(10);
116995d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
1170a813ed78SPyun YongHyeon 		if ((val & BGE_MICOMM_BUSY) == 0) {
1171a813ed78SPyun YongHyeon 			DELAY(5);
1172a813ed78SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_MI_COMM);
117395d67482SBill Paul 			break;
117495d67482SBill Paul 		}
1175a813ed78SPyun YongHyeon 	}
117695d67482SBill Paul 
117795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
11785fea260fSMarius Strobl 		device_printf(sc->bge_dev,
11795fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
11805fea260fSMarius Strobl 		    phy, reg, val);
118137ceeb4dSPaul Saab 		val = 0;
118295d67482SBill Paul 	}
118395d67482SBill Paul 
1184a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1185a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1186a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1187a813ed78SPyun YongHyeon 		DELAY(80);
118837ceeb4dSPaul Saab 	}
118937ceeb4dSPaul Saab 
1190548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1191548c8f1aSPyun YongHyeon 
119295d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
119395d67482SBill Paul 		return (0);
119495d67482SBill Paul 
11950c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
119695d67482SBill Paul }
119795d67482SBill Paul 
119895d67482SBill Paul static int
11993f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
120095d67482SBill Paul {
120195d67482SBill Paul 	struct bge_softc *sc;
120295d67482SBill Paul 	int i;
120395d67482SBill Paul 
120495d67482SBill Paul 	sc = device_get_softc(dev);
120595d67482SBill Paul 
120638cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
120738cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
120838cc658fSJohn Baldwin 		return (0);
120938cc658fSJohn Baldwin 
1210548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1211548c8f1aSPyun YongHyeon 		return (0);
1212548c8f1aSPyun YongHyeon 
1213a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1214a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1215a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1216a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1217a813ed78SPyun YongHyeon 		DELAY(80);
121837ceeb4dSPaul Saab 	}
121937ceeb4dSPaul Saab 
122095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
122195d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
122295d67482SBill Paul 
122395d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1224d5d23857SJung-uk Kim 		DELAY(10);
122538cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
122638cc658fSJohn Baldwin 			DELAY(5);
122738cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
122895d67482SBill Paul 			break;
1229d5d23857SJung-uk Kim 		}
123038cc658fSJohn Baldwin 	}
1231d5d23857SJung-uk Kim 
1232a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1233a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1234a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1235a813ed78SPyun YongHyeon 		DELAY(80);
1236a813ed78SPyun YongHyeon 	}
1237a813ed78SPyun YongHyeon 
1238548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1239548c8f1aSPyun YongHyeon 
1240a813ed78SPyun YongHyeon 	if (i == BGE_TIMEOUT)
124138cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
12422246e8c6SPyun YongHyeon 		    "PHY write timed out (phy %d, reg %d, val 0x%04x)\n",
124338cc658fSJohn Baldwin 		    phy, reg, val);
124437ceeb4dSPaul Saab 
124595d67482SBill Paul 	return (0);
124695d67482SBill Paul }
124795d67482SBill Paul 
124895d67482SBill Paul static void
12493f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
125095d67482SBill Paul {
125195d67482SBill Paul 	struct bge_softc *sc;
125295d67482SBill Paul 	struct mii_data *mii;
1253a0a03d1eSPyun YongHyeon 	uint32_t mac_mode, rx_mode, tx_mode;
1254e4146b95SPyun YongHyeon 
125595d67482SBill Paul 	sc = device_get_softc(dev);
1256fba8b109SMarcel Moolenaar 	if ((if_getdrvflags(sc->bge_ifp) & IFF_DRV_RUNNING) == 0)
1257e4146b95SPyun YongHyeon 		return;
125895d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
125995d67482SBill Paul 
1260d4f5240aSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1261d4f5240aSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
1262d4f5240aSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
1263d4f5240aSPyun YongHyeon 		case IFM_10_T:
1264d4f5240aSPyun YongHyeon 		case IFM_100_TX:
1265d4f5240aSPyun YongHyeon 			sc->bge_link = 1;
1266d4f5240aSPyun YongHyeon 			break;
1267d4f5240aSPyun YongHyeon 		case IFM_1000_T:
1268d4f5240aSPyun YongHyeon 		case IFM_1000_SX:
1269d4f5240aSPyun YongHyeon 		case IFM_2500_SX:
1270d4f5240aSPyun YongHyeon 			if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
1271d4f5240aSPyun YongHyeon 				sc->bge_link = 1;
1272d4f5240aSPyun YongHyeon 			else
1273d4f5240aSPyun YongHyeon 				sc->bge_link = 0;
1274d4f5240aSPyun YongHyeon 			break;
1275d4f5240aSPyun YongHyeon 		default:
1276d4f5240aSPyun YongHyeon 			sc->bge_link = 0;
1277d4f5240aSPyun YongHyeon 			break;
1278d4f5240aSPyun YongHyeon 		}
1279d4f5240aSPyun YongHyeon 	} else
1280d4f5240aSPyun YongHyeon 		sc->bge_link = 0;
1281d4f5240aSPyun YongHyeon 	if (sc->bge_link == 0)
1282d4f5240aSPyun YongHyeon 		return;
1283a0a03d1eSPyun YongHyeon 
1284a0a03d1eSPyun YongHyeon 	/*
1285a0a03d1eSPyun YongHyeon 	 * APE firmware touches these registers to keep the MAC
1286a0a03d1eSPyun YongHyeon 	 * connected to the outside world.  Try to keep the
1287a0a03d1eSPyun YongHyeon 	 * accesses atomic.
1288a0a03d1eSPyun YongHyeon 	 */
1289a0a03d1eSPyun YongHyeon 
1290a0a03d1eSPyun YongHyeon 	/* Set the port mode (MII/GMII) to match the link speed. */
1291a0a03d1eSPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) &
1292a0a03d1eSPyun YongHyeon 	    ~(BGE_MACMODE_PORTMODE | BGE_MACMODE_HALF_DUPLEX);
1293a0a03d1eSPyun YongHyeon 	tx_mode = CSR_READ_4(sc, BGE_TX_MODE);
1294a0a03d1eSPyun YongHyeon 	rx_mode = CSR_READ_4(sc, BGE_RX_MODE);
1295a0a03d1eSPyun YongHyeon 
1296ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
1297ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
1298a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_GMII;
12993f74909aSGleb Smirnoff 	else
1300a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_MII;
130195d67482SBill Paul 
1302a0a03d1eSPyun YongHyeon 	/* Set MAC flow control behavior to match link flow control settings. */
1303a0a03d1eSPyun YongHyeon 	tx_mode &= ~BGE_TXMODE_FLOWCTL_ENABLE;
1304a0a03d1eSPyun YongHyeon 	rx_mode &= ~BGE_RXMODE_FLOWCTL_ENABLE;
13054951ca86SPyun YongHyeon 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1306a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1307a0a03d1eSPyun YongHyeon 			tx_mode |= BGE_TXMODE_FLOWCTL_ENABLE;
1308a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1309a0a03d1eSPyun YongHyeon 			rx_mode |= BGE_RXMODE_FLOWCTL_ENABLE;
1310a0a03d1eSPyun YongHyeon 	} else
1311a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_MACMODE_HALF_DUPLEX;
1312a0a03d1eSPyun YongHyeon 
1313a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, mac_mode);
13149b80ffe7SPyun YongHyeon 	DELAY(40);
1315a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, tx_mode);
1316a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_RX_MODE, rx_mode);
131795d67482SBill Paul }
131895d67482SBill Paul 
131995d67482SBill Paul /*
132095d67482SBill Paul  * Intialize a standard receive ring descriptor.
132195d67482SBill Paul  */
132295d67482SBill Paul static int
1323943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
132495d67482SBill Paul {
1325943787f3SPyun YongHyeon 	struct mbuf *m;
132695d67482SBill Paul 	struct bge_rx_bd *r;
1327a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
1328943787f3SPyun YongHyeon 	bus_dmamap_t map;
1329a23634a1SPyun YongHyeon 	int error, nsegs;
133095d67482SBill Paul 
1331f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD &&
1332fba8b109SMarcel Moolenaar 	    (if_getmtu(sc->bge_ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
1333f5459d4cSPyun YongHyeon 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))) {
1334c6499eccSGleb Smirnoff 		m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
1335f5459d4cSPyun YongHyeon 		if (m == NULL)
1336f5459d4cSPyun YongHyeon 			return (ENOBUFS);
1337f5459d4cSPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1338f5459d4cSPyun YongHyeon 	} else {
1339c6499eccSGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1340943787f3SPyun YongHyeon 		if (m == NULL)
134195d67482SBill Paul 			return (ENOBUFS);
1342943787f3SPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MCLBYTES;
1343f5459d4cSPyun YongHyeon 	}
1344652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1345943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
1346943787f3SPyun YongHyeon 
13470ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
1348943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
1349a23634a1SPyun YongHyeon 	if (error != 0) {
1350943787f3SPyun YongHyeon 		m_freem(m);
1351a23634a1SPyun YongHyeon 		return (error);
1352f41ac2beSBill Paul 	}
1353943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1354943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1355943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
1356943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1357943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
1358943787f3SPyun YongHyeon 	}
1359943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
1360943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
1361943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
1362943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
1363e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
1364943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
1365a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
1366a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
1367e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
1368a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
1369e907febfSPyun YongHyeon 	r->bge_idx = i;
1370f41ac2beSBill Paul 
13710ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1372943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
137395d67482SBill Paul 
137495d67482SBill Paul 	return (0);
137595d67482SBill Paul }
137695d67482SBill Paul 
137795d67482SBill Paul /*
137895d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
137995d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
138095d67482SBill Paul  */
138195d67482SBill Paul static int
1382943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
138395d67482SBill Paul {
13841be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
1385943787f3SPyun YongHyeon 	bus_dmamap_t map;
13861be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
1387943787f3SPyun YongHyeon 	struct mbuf *m;
1388943787f3SPyun YongHyeon 	int error, nsegs;
138995d67482SBill Paul 
1390c6499eccSGleb Smirnoff 	MGETHDR(m, M_NOWAIT, MT_DATA);
1391943787f3SPyun YongHyeon 	if (m == NULL)
139295d67482SBill Paul 		return (ENOBUFS);
139395d67482SBill Paul 
13942a8c860fSRobert Watson 	if (m_cljget(m, M_NOWAIT, MJUM9BYTES) == NULL) {
1395943787f3SPyun YongHyeon 		m_freem(m);
139695d67482SBill Paul 		return (ENOBUFS);
139795d67482SBill Paul 	}
1398943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1399652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1400943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
14011be6acb7SGleb Smirnoff 
14021be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1403943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1404943787f3SPyun YongHyeon 	if (error != 0) {
1405943787f3SPyun YongHyeon 		m_freem(m);
14061be6acb7SGleb Smirnoff 		return (error);
1407f7cea149SGleb Smirnoff 	}
14081be6acb7SGleb Smirnoff 
1409aa8cbdbfSMarius Strobl 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1410943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1411943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1412943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1413943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1414943787f3SPyun YongHyeon 	}
1415943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1416943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1417943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1418943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1419943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1420e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1421e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1422e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1423e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1424e0b7b101SPyun YongHyeon 
14251be6acb7SGleb Smirnoff 	/*
14261be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
14271be6acb7SGleb Smirnoff 	 */
1428943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
14294e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
14304e7ba1abSGleb Smirnoff 	r->bge_idx = i;
14314e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
14324e7ba1abSGleb Smirnoff 	switch (nsegs) {
14334e7ba1abSGleb Smirnoff 	case 4:
14344e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
14354e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
14364e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1437e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
14384e7ba1abSGleb Smirnoff 	case 3:
1439e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1440e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1441e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1442e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
14434e7ba1abSGleb Smirnoff 	case 2:
14444e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
14454e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
14464e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1447e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
14484e7ba1abSGleb Smirnoff 	case 1:
14494e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
14504e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
14514e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1452e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
14534e7ba1abSGleb Smirnoff 		break;
14544e7ba1abSGleb Smirnoff 	default:
14554e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
14564e7ba1abSGleb Smirnoff 	}
1457f41ac2beSBill Paul 
1458a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1459943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
146095d67482SBill Paul 
146195d67482SBill Paul 	return (0);
146295d67482SBill Paul }
146395d67482SBill Paul 
146495d67482SBill Paul static int
14653f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
146695d67482SBill Paul {
14673ee5d7daSPyun YongHyeon 	int error, i;
146895d67482SBill Paul 
1469e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
147003e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1471e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1472943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
14733ee5d7daSPyun YongHyeon 			return (error);
147403e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
14751888f324SPyun YongHyeon 	}
147695d67482SBill Paul 
1477f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1478d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1479f41ac2beSBill Paul 
1480e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1481e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
148295d67482SBill Paul 
148395d67482SBill Paul 	return (0);
148495d67482SBill Paul }
148595d67482SBill Paul 
148695d67482SBill Paul static void
14873f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
148895d67482SBill Paul {
148995d67482SBill Paul 	int i;
149095d67482SBill Paul 
149195d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
149295d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
14930ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1494e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1495e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
14960ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1497f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1498e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1499e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
150095d67482SBill Paul 		}
1501f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
150295d67482SBill Paul 		    sizeof(struct bge_rx_bd));
150395d67482SBill Paul 	}
150495d67482SBill Paul }
150595d67482SBill Paul 
150695d67482SBill Paul static int
15073f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
150895d67482SBill Paul {
150995d67482SBill Paul 	struct bge_rcb *rcb;
15103ee5d7daSPyun YongHyeon 	int error, i;
151195d67482SBill Paul 
1512e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
151303e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
151495d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1515943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
15163ee5d7daSPyun YongHyeon 			return (error);
151703e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
15181888f324SPyun YongHyeon 	}
151995d67482SBill Paul 
1520f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1521d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1522f41ac2beSBill Paul 
1523e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
152495d67482SBill Paul 
15258a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1526f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
15278a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
15288a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
152967111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
153095d67482SBill Paul 
1531e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
153295d67482SBill Paul 
153395d67482SBill Paul 	return (0);
153495d67482SBill Paul }
153595d67482SBill Paul 
153695d67482SBill Paul static void
15373f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
153895d67482SBill Paul {
153995d67482SBill Paul 	int i;
154095d67482SBill Paul 
154195d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
154295d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1543e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1544e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1545e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1546f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1547f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1548e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1549e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
155095d67482SBill Paul 		}
1551f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
15521be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
155395d67482SBill Paul 	}
155495d67482SBill Paul }
155595d67482SBill Paul 
155695d67482SBill Paul static void
15573f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
155895d67482SBill Paul {
155995d67482SBill Paul 	int i;
156095d67482SBill Paul 
1561f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
156295d67482SBill Paul 		return;
156395d67482SBill Paul 
156495d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
156595d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
15660ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1567e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1568e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
15690ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1570f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1571e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1572e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
157395d67482SBill Paul 		}
1574f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
157595d67482SBill Paul 		    sizeof(struct bge_tx_bd));
157695d67482SBill Paul 	}
157795d67482SBill Paul }
157895d67482SBill Paul 
157995d67482SBill Paul static int
15803f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
158195d67482SBill Paul {
158295d67482SBill Paul 	sc->bge_txcnt = 0;
158395d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
15843927098fSPaul Saab 
1585e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1586e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
15875c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1588e6bf277eSPyun YongHyeon 
158914bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
159014bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
159138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
159214bbd30fSGleb Smirnoff 
15933927098fSPaul Saab 	/* 5700 b2 errata */
1594e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
159538cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
15963927098fSPaul Saab 
159714bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
159838cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
15993927098fSPaul Saab 	/* 5700 b2 errata */
1600e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
160138cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
160295d67482SBill Paul 
160395d67482SBill Paul 	return (0);
160495d67482SBill Paul }
160595d67482SBill Paul 
160695d67482SBill Paul static void
16073e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
16083e9b1bcaSJung-uk Kim {
1609fba8b109SMarcel Moolenaar 	if_t ifp;
16103e9b1bcaSJung-uk Kim 
16113e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
16123e9b1bcaSJung-uk Kim 
16133e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
16143e9b1bcaSJung-uk Kim 
161545ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
1616fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_PROMISC)
161745ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
16183e9b1bcaSJung-uk Kim 	else
161945ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
16203e9b1bcaSJung-uk Kim }
16213e9b1bcaSJung-uk Kim 
16223e9b1bcaSJung-uk Kim static void
16233f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
162495d67482SBill Paul {
1625fba8b109SMarcel Moolenaar 	if_t ifp;
1626fba8b109SMarcel Moolenaar 	int mc_count = 0;
16273f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
1628fba8b109SMarcel Moolenaar 	int h, i, mcnt;
1629fba8b109SMarcel Moolenaar 	unsigned char *mta;
163095d67482SBill Paul 
16310f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
16320f9bd73bSSam Leffler 
1633fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
163495d67482SBill Paul 
1635fba8b109SMarcel Moolenaar 	mc_count = if_multiaddr_count(ifp, -1);
1636fba8b109SMarcel Moolenaar 	mta = malloc(sizeof(unsigned char) *  ETHER_ADDR_LEN *
1637fba8b109SMarcel Moolenaar 	    mc_count, M_DEVBUF, M_NOWAIT);
1638fba8b109SMarcel Moolenaar 
1639fba8b109SMarcel Moolenaar 	if(mta == NULL) {
1640fba8b109SMarcel Moolenaar 		device_printf(sc->bge_dev,
1641fba8b109SMarcel Moolenaar 		    "Failed to allocated temp mcast list\n");
1642fba8b109SMarcel Moolenaar 		return;
1643fba8b109SMarcel Moolenaar 	}
1644fba8b109SMarcel Moolenaar 
1645fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_ALLMULTI || if_getflags(ifp) & IFF_PROMISC) {
164695d67482SBill Paul 		for (i = 0; i < 4; i++)
16470c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
1648049855d8SPedro F. Giffuni 		free(mta, M_DEVBUF);
164995d67482SBill Paul 		return;
165095d67482SBill Paul 	}
165195d67482SBill Paul 
165295d67482SBill Paul 	/* First, zot all the existing filters. */
165395d67482SBill Paul 	for (i = 0; i < 4; i++)
165495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
165595d67482SBill Paul 
1656fba8b109SMarcel Moolenaar 	if_multiaddr_array(ifp, mta, &mcnt, mc_count);
1657fba8b109SMarcel Moolenaar 	for(i = 0; i < mcnt; i++) {
1658a127e581SPeter Wemm 		h = ether_crc32_le(mta + (i * ETHER_ADDR_LEN),
1659a127e581SPeter Wemm 		    ETHER_ADDR_LEN) & 0x7F;
16600c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
166195d67482SBill Paul 	}
166295d67482SBill Paul 
166395d67482SBill Paul 	for (i = 0; i < 4; i++)
166495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1665fba8b109SMarcel Moolenaar 
1666fba8b109SMarcel Moolenaar 	free(mta, M_DEVBUF);
166795d67482SBill Paul }
166895d67482SBill Paul 
16698cb1383cSDoug Ambrisko static void
1670cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1671cb2eacc7SYaroslav Tykhiy {
1672fba8b109SMarcel Moolenaar 	if_t ifp;
1673cb2eacc7SYaroslav Tykhiy 
1674cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1675cb2eacc7SYaroslav Tykhiy 
1676cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1677cb2eacc7SYaroslav Tykhiy 
1678cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1679fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING)
1680cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1681cb2eacc7SYaroslav Tykhiy 	else
1682cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1683cb2eacc7SYaroslav Tykhiy }
1684cb2eacc7SYaroslav Tykhiy 
1685cb2eacc7SYaroslav Tykhiy static void
1686797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
16878cb1383cSDoug Ambrisko {
1688797ab05eSPyun YongHyeon 
16898cb1383cSDoug Ambrisko 	/*
16908cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
16918cb1383cSDoug Ambrisko 	 */
16928cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
1693888b47f0SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
16948cb1383cSDoug Ambrisko 
16958cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16968cb1383cSDoug Ambrisko 		switch (type) {
16978cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1698224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1699224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
17008cb1383cSDoug Ambrisko 			break;
1701548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1702224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1703224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
17048cb1383cSDoug Ambrisko 			break;
1705548c8f1aSPyun YongHyeon 		case BGE_RESET_SUSPEND:
1706548c8f1aSPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1707548c8f1aSPyun YongHyeon 			    BGE_FW_DRV_STATE_SUSPEND);
1708548c8f1aSPyun YongHyeon 			break;
17098cb1383cSDoug Ambrisko 		}
17108cb1383cSDoug Ambrisko 	}
1711548c8f1aSPyun YongHyeon 
1712548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_START || type == BGE_RESET_SUSPEND)
1713548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
17148cb1383cSDoug Ambrisko }
17158cb1383cSDoug Ambrisko 
17168cb1383cSDoug Ambrisko static void
1717797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
17188cb1383cSDoug Ambrisko {
1719797ab05eSPyun YongHyeon 
17208cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
17218cb1383cSDoug Ambrisko 		switch (type) {
17228cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1723224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1724224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START_DONE);
17258cb1383cSDoug Ambrisko 			/* START DONE */
17268cb1383cSDoug Ambrisko 			break;
1727548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1728224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1729224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD_DONE);
17308cb1383cSDoug Ambrisko 			break;
17318cb1383cSDoug Ambrisko 		}
17328cb1383cSDoug Ambrisko 	}
1733548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_SHUTDOWN)
1734548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
17358cb1383cSDoug Ambrisko }
17368cb1383cSDoug Ambrisko 
17378cb1383cSDoug Ambrisko static void
1738797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
17398cb1383cSDoug Ambrisko {
1740797ab05eSPyun YongHyeon 
17418cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17428cb1383cSDoug Ambrisko 		switch (type) {
17438cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1744224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1745224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
17468cb1383cSDoug Ambrisko 			break;
1747548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1748224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1749224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
17508cb1383cSDoug Ambrisko 			break;
17518cb1383cSDoug Ambrisko 		}
17528cb1383cSDoug Ambrisko 	}
17538cb1383cSDoug Ambrisko }
17548cb1383cSDoug Ambrisko 
1755797ab05eSPyun YongHyeon static void
1756797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
17578cb1383cSDoug Ambrisko {
17588cb1383cSDoug Ambrisko 	int i;
17598cb1383cSDoug Ambrisko 
17608cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17613c201200SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB, BGE_FW_CMD_PAUSE);
17623fed2d5dSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
17639931ba85SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RX_CPU_EVENT) | BGE_RX_CPU_DRV_EVENT);
17648cb1383cSDoug Ambrisko 
17658cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
17669931ba85SPyun YongHyeon 			if (!(CSR_READ_4(sc, BGE_RX_CPU_EVENT) &
17679931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT))
17688cb1383cSDoug Ambrisko 				break;
17698cb1383cSDoug Ambrisko 			DELAY(10);
17708cb1383cSDoug Ambrisko 		}
17718cb1383cSDoug Ambrisko 	}
17728cb1383cSDoug Ambrisko }
17738cb1383cSDoug Ambrisko 
177450515680SPyun YongHyeon static uint32_t
177550515680SPyun YongHyeon bge_dma_swap_options(struct bge_softc *sc)
177650515680SPyun YongHyeon {
177750515680SPyun YongHyeon 	uint32_t dma_options;
177850515680SPyun YongHyeon 
177950515680SPyun YongHyeon 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
178050515680SPyun YongHyeon 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
178150515680SPyun YongHyeon #if BYTE_ORDER == BIG_ENDIAN
178250515680SPyun YongHyeon 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
178350515680SPyun YongHyeon #endif
178450515680SPyun YongHyeon 	return (dma_options);
178550515680SPyun YongHyeon }
178650515680SPyun YongHyeon 
178795d67482SBill Paul /*
1788c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
178995d67482SBill Paul  */
179095d67482SBill Paul static int
17913f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
179295d67482SBill Paul {
179350515680SPyun YongHyeon 	uint32_t dma_rw_ctl, misc_ctl, mode_ctl;
1794fbc374afSPyun YongHyeon 	uint16_t val;
179595d67482SBill Paul 	int i;
179695d67482SBill Paul 
17978cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
17981108273aSPyun YongHyeon 	misc_ctl = BGE_INIT;
17991108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
18001108273aSPyun YongHyeon 		misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
18011108273aSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
180295d67482SBill Paul 
180395d67482SBill Paul 	/*
180495d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
180595d67482SBill Paul 	 * internal memory.
180695d67482SBill Paul 	 */
180795d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
18083f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
180995d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
181095d67482SBill Paul 
181195d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
18123f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
181395d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
181495d67482SBill Paul 
1815fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1816fbc374afSPyun YongHyeon 		/*
1817d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1818fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1819fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1820fbc374afSPyun YongHyeon 		 */
1821fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1822fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1823fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1824fbc374afSPyun YongHyeon 	}
1825fbc374afSPyun YongHyeon 
1826f8bb33c3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM57765 ||
1827f8bb33c3SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57766) {
1828f8bb33c3SPyun YongHyeon 		/*
1829f8bb33c3SPyun YongHyeon 		 * For the 57766 and non Ax versions of 57765, bootcode
1830f8bb33c3SPyun YongHyeon 		 * needs to setup the PCIE Fast Training Sequence (FTS)
1831f8bb33c3SPyun YongHyeon 		 * value to prevent transmit hangs.
1832f8bb33c3SPyun YongHyeon 		 */
1833f8bb33c3SPyun YongHyeon 		if (sc->bge_chiprev != BGE_CHIPREV_57765_AX) {
1834f8bb33c3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_CPMU_PADRNG_CTL,
1835f8bb33c3SPyun YongHyeon 			    CSR_READ_4(sc, BGE_CPMU_PADRNG_CTL) |
1836f8bb33c3SPyun YongHyeon 			    BGE_CPMU_PADRNG_CTL_RDIV2);
1837f8bb33c3SPyun YongHyeon 		}
1838f8bb33c3SPyun YongHyeon 	}
1839f8bb33c3SPyun YongHyeon 
1840186f842bSJung-uk Kim 	/*
1841186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1842186f842bSJung-uk Kim 	 */
1843186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1844186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1845652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
184648630d79SPyun YongHyeon 		if (sc->bge_mps >= 256)
184748630d79SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
184848630d79SPyun YongHyeon 		else
1849186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1850652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
18514c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1852186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1853186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1854186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1855186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1856186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1857186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1858cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1859cbb2b2feSPyun YongHyeon 			/*
1860cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1861cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1862cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1863cbb2b2feSPyun YongHyeon 			 * register.
1864cbb2b2feSPyun YongHyeon 			 */
1865cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1866cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1867186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1868186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1869186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1870186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1871186f842bSJung-uk Kim 		} else {
1872186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1873186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1874186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
18750c8aa4eaSJung-uk Kim 			    0x0F;
1876186f842bSJung-uk Kim 		}
1877e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1878e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
18793f74909aSGleb Smirnoff 			uint32_t tmp;
18805cba12d3SPaul Saab 
1881186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
18820c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1883186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1884186f842bSJung-uk Kim 				dma_rw_ctl |=
1885186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
18865cba12d3SPaul Saab 
1887186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1888186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1889186f842bSJung-uk Kim 		}
1890186f842bSJung-uk Kim 	} else {
1891186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1892186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1893186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1894186f842bSJung-uk Kim 
1895186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1896186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1897186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1898186f842bSJung-uk Kim 	}
1899186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1900186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1901186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1902186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1903e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1904186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
19055cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1906b4a256acSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
19071108273aSPyun YongHyeon 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1908b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
1909b4a256acSPyun YongHyeon 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1910bbe2ca75SPyun YongHyeon 		/*
1911bbe2ca75SPyun YongHyeon 		 * Enable HW workaround for controllers that misinterpret
1912bbe2ca75SPyun YongHyeon 		 * a status tag update and leave interrupts permanently
1913bbe2ca75SPyun YongHyeon 		 * disabled.
1914bbe2ca75SPyun YongHyeon 		 */
19152927f01fSPyun YongHyeon 		if (!BGE_IS_57765_PLUS(sc) &&
19162927f01fSPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
19172927f01fSPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM5762)
1918bbe2ca75SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1919b4a256acSPyun YongHyeon 	}
19205cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
192195d67482SBill Paul 
192295d67482SBill Paul 	/*
192395d67482SBill Paul 	 * Set up general mode register.
192495d67482SBill Paul 	 */
1925548c8f1aSPyun YongHyeon 	mode_ctl = bge_dma_swap_options(sc);
19262927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
19272927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
1928548c8f1aSPyun YongHyeon 		/* Retain Host-2-BMC settings written by APE firmware. */
1929548c8f1aSPyun YongHyeon 		mode_ctl |= CSR_READ_4(sc, BGE_MODE_CTL) &
1930548c8f1aSPyun YongHyeon 		    (BGE_MODECTL_BYTESWAP_B2HRX_DATA |
1931548c8f1aSPyun YongHyeon 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA |
1932548c8f1aSPyun YongHyeon 		    BGE_MODECTL_B2HRX_ENABLE | BGE_MODECTL_HTX2B_ENABLE);
1933548c8f1aSPyun YongHyeon 	}
1934548c8f1aSPyun YongHyeon 	mode_ctl |= BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1935548c8f1aSPyun YongHyeon 	    BGE_MODECTL_TX_NO_PHDR_CSUM;
193695d67482SBill Paul 
193795d67482SBill Paul 	/*
193890447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
193990447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
194090447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
194190447aadSMarius Strobl 	 * certain bridges.
194290447aadSMarius Strobl 	 */
194390447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
194490447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
194550515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_FORCE_PCI32;
194690447aadSMarius Strobl 
194790447aadSMarius Strobl 	/*
19488cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
19498cb1383cSDoug Ambrisko 	 */
19508cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
195150515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_STACKUP;
195250515680SPyun YongHyeon 
195350515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
19548cb1383cSDoug Ambrisko 
19558cb1383cSDoug Ambrisko 	/*
1956ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
195740438c47SMarius Strobl 	 * properly by these devices.
195895d67482SBill Paul 	 */
195940438c47SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
196095d67482SBill Paul 
1961d7acafa1SMarius Strobl 	/* Set the timer prescaler (always 66 MHz). */
19620c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
196395d67482SBill Paul 
196438cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
196538cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
196638cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
196738cc658fSJohn Baldwin 
196838cc658fSJohn Baldwin 		/* Put PHY into ready state */
196938cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
197038cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
197138cc658fSJohn Baldwin 		DELAY(40);
197238cc658fSJohn Baldwin 	}
197338cc658fSJohn Baldwin 
197495d67482SBill Paul 	return (0);
197595d67482SBill Paul }
197695d67482SBill Paul 
197795d67482SBill Paul static int
19783f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
197995d67482SBill Paul {
198095d67482SBill Paul 	struct bge_rcb *rcb;
1981e907febfSPyun YongHyeon 	bus_size_t vrcb;
1982e907febfSPyun YongHyeon 	bge_hostaddr taddr;
19832927f01fSPyun YongHyeon 	uint32_t dmactl, rdmareg, val;
19848a315a6dSPyun YongHyeon 	int i, limit;
198595d67482SBill Paul 
198695d67482SBill Paul 	/*
198795d67482SBill Paul 	 * Initialize the memory window pointer register so that
198895d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
198995d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
199095d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
199195d67482SBill Paul 	 */
199295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
199395d67482SBill Paul 
1994822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1995822f63fcSBill Paul 
19967ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
199795d67482SBill Paul 		/* Configure mbuf memory pool */
19980dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1999822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
2000822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
2001822f63fcSBill Paul 		else
200295d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
200395d67482SBill Paul 
200495d67482SBill Paul 		/* Configure DMA resource pool */
20050434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
20060434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
200795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
20080434d1b8SBill Paul 	}
200995d67482SBill Paul 
201095d67482SBill Paul 	/* Configure mbuf pool watermarks */
201150515680SPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
20121108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
2013fba8b109SMarcel Moolenaar 		if (if_getmtu(sc->bge_ifp) > ETHERMTU) {
20141108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
20151108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
20161108273aSPyun YongHyeon 		} else {
20171108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
20181108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
20191108273aSPyun YongHyeon 		}
20201108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc)) {
2021fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
2022fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
2023fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
202438cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
202538cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
202638cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
202738cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
202838cc658fSJohn Baldwin 	} else {
202938cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
203038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
203138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
203238cc658fSJohn Baldwin 	}
203395d67482SBill Paul 
203495d67482SBill Paul 	/* Configure DMA resource watermarks */
203595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
203695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
203795d67482SBill Paul 
203895d67482SBill Paul 	/* Enable buffer manager */
2039bbe2ca75SPyun YongHyeon 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
2040bbe2ca75SPyun YongHyeon 	/*
2041bbe2ca75SPyun YongHyeon 	 * Change the arbitration algorithm of TXMBUF read request to
2042bbe2ca75SPyun YongHyeon 	 * round-robin instead of priority based for BCM5719.  When
2043bbe2ca75SPyun YongHyeon 	 * TXFIFO is almost empty, RDMA will hold its request until
2044bbe2ca75SPyun YongHyeon 	 * TXFIFO is not almost empty.
2045bbe2ca75SPyun YongHyeon 	 */
2046bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
2047bbe2ca75SPyun YongHyeon 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
2048bbe2ca75SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
204995d67482SBill Paul 
205095d67482SBill Paul 	/* Poll for buffer manager start indication */
205195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2052d5d23857SJung-uk Kim 		DELAY(10);
20530c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
205495d67482SBill Paul 			break;
205595d67482SBill Paul 	}
205695d67482SBill Paul 
205795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
20585a147ba6SPyun YongHyeon 		device_printf(sc->bge_dev, "buffer manager failed to start\n");
205995d67482SBill Paul 		return (ENXIO);
206095d67482SBill Paul 	}
206195d67482SBill Paul 
206295d67482SBill Paul 	/* Enable flow-through queues */
20630c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
206495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
206595d67482SBill Paul 
206695d67482SBill Paul 	/* Wait until queue initialization is complete */
206795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2068d5d23857SJung-uk Kim 		DELAY(10);
206995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
207095d67482SBill Paul 			break;
207195d67482SBill Paul 	}
207295d67482SBill Paul 
207395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2074fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
207595d67482SBill Paul 		return (ENXIO);
207695d67482SBill Paul 	}
207795d67482SBill Paul 
20788a315a6dSPyun YongHyeon 	/*
20798a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
20808a315a6dSPyun YongHyeon 	 *
20818a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
20828a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
20838a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
20848a315a6dSPyun YongHyeon 	 *
20858a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
20868a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
20878a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
20888a315a6dSPyun YongHyeon 	 *   to the controller.
20898a315a6dSPyun YongHyeon 	 *
20908a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
20918a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
20928a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
20938a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
20948a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
20958a315a6dSPyun YongHyeon 	 *   be disabled.
20968a315a6dSPyun YongHyeon 	 *
20978a315a6dSPyun YongHyeon 	 * Receive Return Ring
20988a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
20998a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
21008a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
21018a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
21028a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
21038a315a6dSPyun YongHyeon 	 *
21048a315a6dSPyun YongHyeon 	 * Send Ring
21058a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
21068a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
21078a315a6dSPyun YongHyeon 	 */
21088a315a6dSPyun YongHyeon 
21098a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
2110f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
2111f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
2112f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
2113f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
2114f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
2115f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2116f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
21171108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
21181108273aSPyun YongHyeon 		/*
21191108273aSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
21201108273aSPyun YongHyeon 		 * Bits 15-2 : Maximum RX frame size
21211108273aSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
21221108273aSPyun YongHyeon 		 * Bit 0     : Reserved
21231108273aSPyun YongHyeon 		 */
21241108273aSPyun YongHyeon 		rcb->bge_maxlen_flags =
21251108273aSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
21261108273aSPyun YongHyeon 	} else if (BGE_IS_5705_PLUS(sc)) {
21278a315a6dSPyun YongHyeon 		/*
21288a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
21298a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
21308a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
21318a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
21328a315a6dSPyun YongHyeon 		 */
21330434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
21348a315a6dSPyun YongHyeon 	} else {
21358a315a6dSPyun YongHyeon 		/*
21368a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
21378a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
21388a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
21398a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
21408a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
21418a315a6dSPyun YongHyeon 		 */
21420434d1b8SBill Paul 		rcb->bge_maxlen_flags =
21430434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
21448a315a6dSPyun YongHyeon 	}
2145bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
214650515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
214750515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21481108273aSPyun YongHyeon 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
21491108273aSPyun YongHyeon 	else
215095d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
21518a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
21520c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
21530c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
215467111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
215567111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
215695d67482SBill Paul 
21578a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
21588a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
21598a315a6dSPyun YongHyeon 
216095d67482SBill Paul 	/*
21618a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
21628a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
21638a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
216495d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
216595d67482SBill Paul 	 * high enough to require it).
216695d67482SBill Paul 	 */
21674c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2168f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
21698a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
2170f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
2171f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2172f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
2173f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2174f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2175f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2176f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
21771be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
21781be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
2179bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
218050515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
218150515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21821108273aSPyun YongHyeon 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
21831108273aSPyun YongHyeon 		else
218495d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
218567111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
218667111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
218767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
218867111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
21898a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
21900434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
21910434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
219267111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
21938a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
21948a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
21958a315a6dSPyun YongHyeon 	}
219695d67482SBill Paul 
21978a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
21985e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
2199f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
220067111612SJohn Polstra 		rcb->bge_maxlen_flags =
220167111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
22020434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
22030434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
22048a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
22058a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
22060434d1b8SBill Paul 	}
220795d67482SBill Paul 
2208ca4f8986SPyun YongHyeon 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
2209ca4f8986SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2210427d3f33SPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
2211427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
2212427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
22138d5f7181SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
22148d5f7181SPyun YongHyeon 			    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
2215ca4f8986SPyun YongHyeon 	}
221695d67482SBill Paul 	/*
22178a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
22188a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
22198a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
22208a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
22218a315a6dSPyun YongHyeon 	 *
222295d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
222395d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
222495d67482SBill Paul 	 * each ring.
22259ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
22269ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
22279ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
22289ba784dbSScott Long 	 * are reports that it might not need to be so strict.
222938cc658fSJohn Baldwin 	 *
223038cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
223138cc658fSJohn Baldwin 	 * well.
223295d67482SBill Paul 	 */
22335345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
22346f8718a3SScott Long 		val = 8;
22356f8718a3SScott Long 	else
22366f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
22376f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
22382a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
22392a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
22402a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
22411108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
22421108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
22431108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
22441108273aSPyun YongHyeon 	}
224595d67482SBill Paul 
224695d67482SBill Paul 	/*
22478a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
22488a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
22498a315a6dSPyun YongHyeon 	 * located in NIC memory.
225095d67482SBill Paul 	 */
22518a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
22528a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
22538a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
22542927f01fSPyun YongHyeon 	else if (BGE_IS_57765_PLUS(sc) ||
22552927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762)
22562927f01fSPyun YongHyeon 		limit = 2;
22572927f01fSPyun YongHyeon 	else if (BGE_IS_5717_PLUS(sc))
22582927f01fSPyun YongHyeon 		limit = 4;
22598a315a6dSPyun YongHyeon 	else
22608a315a6dSPyun YongHyeon 		limit = 1;
2261e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
22628a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2263e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2264e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
2265e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2266e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
226795d67482SBill Paul 	}
226895d67482SBill Paul 
22698a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
2270e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
2271e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
2272e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2273e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2274bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
227550515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
227650515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
22771108273aSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
22781108273aSPyun YongHyeon 	else
2279e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
2280e907febfSPyun YongHyeon 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
2281e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2282e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
228395d67482SBill Paul 
22848a315a6dSPyun YongHyeon 	/*
22858a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
22868a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
22878a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
22888a315a6dSPyun YongHyeon 	 */
2289bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
229050515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
229150515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
22921108273aSPyun YongHyeon 		/* Should be 17, use 16 until we get an SRAM map. */
22931108273aSPyun YongHyeon 		limit = 16;
22941108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc))
22958a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
2296b4a256acSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
22972927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762 ||
2298fe26ad88SPyun YongHyeon 	    BGE_IS_57765_PLUS(sc))
22998a315a6dSPyun YongHyeon 		limit = 4;
23008a315a6dSPyun YongHyeon 	else
23018a315a6dSPyun YongHyeon 		limit = 1;
23028a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
2303e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
23048a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2305e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
2306e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
2307e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
23088a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
2309e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
231038cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
23113f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
2312e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
231395d67482SBill Paul 	}
231495d67482SBill Paul 
231595d67482SBill Paul 	/*
23168a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
23178a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
23188a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
231995d67482SBill Paul 	 */
2320e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2321e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
2322e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2323e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
23248a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2325e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2326e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
232795d67482SBill Paul 
232895d67482SBill Paul 	/* Set random backoff seed for TX */
232995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
23300a2cc827SPyun YongHyeon 	    (IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
23314a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
23320a2cc827SPyun YongHyeon 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5]) &
233395d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
233495d67482SBill Paul 
233595d67482SBill Paul 	/* Set inter-packet gap */
233650515680SPyun YongHyeon 	val = 0x2620;
23372927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
23382927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762)
233950515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
234050515680SPyun YongHyeon 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
234150515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
234295d67482SBill Paul 
234395d67482SBill Paul 	/*
234495d67482SBill Paul 	 * Specify which ring to use for packets that don't match
234595d67482SBill Paul 	 * any RX rules.
234695d67482SBill Paul 	 */
234795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
234895d67482SBill Paul 
234995d67482SBill Paul 	/*
235095d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
235195d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
235295d67482SBill Paul 	 */
235395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
235495d67482SBill Paul 
235595d67482SBill Paul 	/* Inialize RX list placement stats mask. */
23560c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
235795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
235895d67482SBill Paul 
235995d67482SBill Paul 	/* Disable host coalescing until we get it set up */
236095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
236195d67482SBill Paul 
236295d67482SBill Paul 	/* Poll to make sure it's shut down. */
236395d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2364d5d23857SJung-uk Kim 		DELAY(10);
236595d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
236695d67482SBill Paul 			break;
236795d67482SBill Paul 	}
236895d67482SBill Paul 
236995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2370fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2371fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
237295d67482SBill Paul 		return (ENXIO);
237395d67482SBill Paul 	}
237495d67482SBill Paul 
237595d67482SBill Paul 	/* Set up host coalescing defaults */
237695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
237795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
237895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
237995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
23807ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
238195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
238295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
23830434d1b8SBill Paul 	}
2384b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
2385b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
238695d67482SBill Paul 
238795d67482SBill Paul 	/* Set up address of statistics block */
23887ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
2389f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
2390f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
239195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
2392f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
23930434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
239495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
23950434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
23960434d1b8SBill Paul 	}
23970434d1b8SBill Paul 
23980434d1b8SBill Paul 	/* Set up address of status block */
2399f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
2400f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
240195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
2402f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
240395d67482SBill Paul 
240430f57f61SPyun YongHyeon 	/* Set up status block size. */
240530f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2406864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
240730f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
2408864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2409864104feSPyun YongHyeon 	} else {
241030f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
2411864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
2412864104feSPyun YongHyeon 	}
2413864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2414864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
2415864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
241630f57f61SPyun YongHyeon 
241795d67482SBill Paul 	/* Turn on host coalescing state machine */
241830f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
241995d67482SBill Paul 
242095d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
242195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
242295d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
242395d67482SBill Paul 
242495d67482SBill Paul 	/* Turn on RX list placement state machine */
242595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
242695d67482SBill Paul 
242795d67482SBill Paul 	/* Turn on RX list selector state machine. */
24287ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
242995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
243095d67482SBill Paul 
24312246e8c6SPyun YongHyeon 	/* Turn on DMA, clear stats. */
2432ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2433ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2434ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2435ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
2436ea3b4127SPyun YongHyeon 
2437ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
2438ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
2439ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
2440ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
2441ea3b4127SPyun YongHyeon 	else
2442ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
2443ea3b4127SPyun YongHyeon 
2444548c8f1aSPyun YongHyeon 	/* Allow APE to send/receive frames. */
2445548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
2446548c8f1aSPyun YongHyeon 		val |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
2447548c8f1aSPyun YongHyeon 
2448ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
24499b80ffe7SPyun YongHyeon 	DELAY(40);
245095d67482SBill Paul 
245195d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
245291bd90d8SPyun YongHyeon 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
245395d67482SBill Paul 
245495d67482SBill Paul #ifdef notdef
245595d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
245695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
245795d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
245895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
245995d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
246095d67482SBill Paul #endif
246195d67482SBill Paul 
246295d67482SBill Paul 	/* Turn on DMA completion state machine */
24637ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
246495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
246595d67482SBill Paul 
24666f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
24676f8718a3SScott Long 
24686f8718a3SScott Long 	/* Enable host coalescing bug fix. */
2469a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
24703889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
24716f8718a3SScott Long 
24727aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
24737aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
24747aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
24757aa4b937SPyun YongHyeon 
247695d67482SBill Paul 	/* Turn on write DMA state machine */
24776f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
24784f09c4c7SMarius Strobl 	DELAY(40);
247995d67482SBill Paul 
248095d67482SBill Paul 	/* Turn on read DMA state machine */
24814f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
24821108273aSPyun YongHyeon 
24831108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
24841108273aSPyun YongHyeon 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
24851108273aSPyun YongHyeon 
2486a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2487a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2488a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2489a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2490a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2491a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
24924f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
24934f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
24941108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2495ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
24961108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_TSO3 ||
24971108273aSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
249855a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
249955a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
250055a24a05SPyun YongHyeon 	}
250150515680SPyun YongHyeon 
25022927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
25032927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
250450515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
250550515680SPyun YongHyeon 			BGE_RDMAMODE_H2BNC_VLAN_DET;
2506e3215f76SPyun YongHyeon 		/*
2507e3215f76SPyun YongHyeon 		 * Allow multiple outstanding read requests from
2508e3215f76SPyun YongHyeon 		 * non-LSO read DMA engine.
2509e3215f76SPyun YongHyeon 		 */
2510e3215f76SPyun YongHyeon 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
2511e3215f76SPyun YongHyeon 	}
251250515680SPyun YongHyeon 
2513d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2514d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2515d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
25161108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
25172927f01fSPyun YongHyeon 	    BGE_IS_5717_PLUS(sc) || BGE_IS_57765_PLUS(sc)) {
25182927f01fSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
25192927f01fSPyun YongHyeon 			rdmareg = BGE_RDMA_RSRVCTRL_REG2;
25202927f01fSPyun YongHyeon 		else
25212927f01fSPyun YongHyeon 			rdmareg = BGE_RDMA_RSRVCTRL;
25222927f01fSPyun YongHyeon 		dmactl = CSR_READ_4(sc, rdmareg);
2523bbe2ca75SPyun YongHyeon 		/*
2524bbe2ca75SPyun YongHyeon 		 * Adjust tx margin to prevent TX data corruption and
2525bbe2ca75SPyun YongHyeon 		 * fix internal FIFO overflow.
2526bbe2ca75SPyun YongHyeon 		 */
25272927f01fSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5719_A0 ||
25282927f01fSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
2529bbe2ca75SPyun YongHyeon 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
2530bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
2531bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
2532bbe2ca75SPyun YongHyeon 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
2533bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
2534bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
2535bbe2ca75SPyun YongHyeon 		}
2536d255f2a9SPyun YongHyeon 		/*
2537d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
2538d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
2539d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
2540d255f2a9SPyun YongHyeon 		 */
25412927f01fSPyun YongHyeon 		CSR_WRITE_4(sc, rdmareg, dmactl |
2542d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2543d255f2a9SPyun YongHyeon 	}
2544bbe2ca75SPyun YongHyeon 
2545e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719) {
2546bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2547bbe2ca75SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2548bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2549bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2550e3215f76SPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2551e3215f76SPyun YongHyeon 		/*
2552e3215f76SPyun YongHyeon 		 * Allow 4KB burst length reads for non-LSO frames.
2553e3215f76SPyun YongHyeon 		 * Enable 512B burst length reads for buffer descriptors.
2554e3215f76SPyun YongHyeon 		 */
2555e3215f76SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2556e3215f76SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2557e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
2558e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
25592927f01fSPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5762) {
25602927f01fSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2,
25612927f01fSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2) |
25622927f01fSPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
25632927f01fSPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2564bbe2ca75SPyun YongHyeon 	}
2565bbe2ca75SPyun YongHyeon 
25664f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
25674f09c4c7SMarius Strobl 	DELAY(40);
256895d67482SBill Paul 
256929b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
257029b44b09SPyun YongHyeon 		for (i = 0; i < BGE_NUM_RDMA_CHANNELS / 2; i++) {
257129b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LENGTH + i * 4);
257229b44b09SPyun YongHyeon 			if ((val & 0xFFFF) > BGE_FRAMELEN)
257329b44b09SPyun YongHyeon 				break;
257429b44b09SPyun YongHyeon 			if (((val >> 16) & 0xFFFF) > BGE_FRAMELEN)
257529b44b09SPyun YongHyeon 				break;
257629b44b09SPyun YongHyeon 		}
257729b44b09SPyun YongHyeon 		if (i != BGE_NUM_RDMA_CHANNELS / 2) {
257829b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
257929b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
258029b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5719;
258129b44b09SPyun YongHyeon 			else
258229b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5720;
258329b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
258429b44b09SPyun YongHyeon 		}
258529b44b09SPyun YongHyeon 	}
258629b44b09SPyun YongHyeon 
258795d67482SBill Paul 	/* Turn on RX data completion state machine */
258895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
258995d67482SBill Paul 
259095d67482SBill Paul 	/* Turn on RX BD initiator state machine */
259195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
259295d67482SBill Paul 
259395d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
259495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
259595d67482SBill Paul 
259695d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
25977ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
259895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
259995d67482SBill Paul 
260095d67482SBill Paul 	/* Turn on send BD completion state machine */
260195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
260295d67482SBill Paul 
260395d67482SBill Paul 	/* Turn on send data completion state machine */
2604a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2605a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2606a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2607a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
260895d67482SBill Paul 
260995d67482SBill Paul 	/* Turn on send data initiator state machine */
26101108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
26111108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
26121108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2613ca3f1187SPyun YongHyeon 	else
261495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
261595d67482SBill Paul 
261695d67482SBill Paul 	/* Turn on send BD initiator state machine */
261795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
261895d67482SBill Paul 
261995d67482SBill Paul 	/* Turn on send BD selector state machine */
262095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
262195d67482SBill Paul 
26220c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
262395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
262495d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
262595d67482SBill Paul 
262695d67482SBill Paul 	/* ack/clear link change events */
262795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
26280434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
26290434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2630f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
263195d67482SBill Paul 
26326ede2cfaSPyun YongHyeon 	/*
26336ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
26346ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
26356ede2cfaSPyun YongHyeon 	 */
2636652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
263795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2638a1d52896SBill Paul 	} else {
26397ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
26407ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
26417ed3f0f0SPyun YongHyeon 			DELAY(80);
26427ed3f0f0SPyun YongHyeon 		}
26431f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
26444c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2645a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2646a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2647a1d52896SBill Paul 	}
264895d67482SBill Paul 
26491f313773SOleg Bulyzhin 	/*
26501f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
26511f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
26521f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
26531f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
26541f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
26551f313773SOleg Bulyzhin 	 */
26561f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
26571f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
26581f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
26591f313773SOleg Bulyzhin 
266095d67482SBill Paul 	/* Enable link state change attentions. */
266195d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
266295d67482SBill Paul 
266395d67482SBill Paul 	return (0);
266495d67482SBill Paul }
266595d67482SBill Paul 
2666d7acafa1SMarius Strobl static const struct bge_revision *
26674c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
26684c0da0ffSGleb Smirnoff {
26694c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
26704c0da0ffSGleb Smirnoff 
26714c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
26724c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
26734c0da0ffSGleb Smirnoff 			return (br);
26744c0da0ffSGleb Smirnoff 	}
26754c0da0ffSGleb Smirnoff 
26764c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
26774c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
26784c0da0ffSGleb Smirnoff 			return (br);
26794c0da0ffSGleb Smirnoff 	}
26804c0da0ffSGleb Smirnoff 
26814c0da0ffSGleb Smirnoff 	return (NULL);
26824c0da0ffSGleb Smirnoff }
26834c0da0ffSGleb Smirnoff 
2684d7acafa1SMarius Strobl static const struct bge_vendor *
26854c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
26864c0da0ffSGleb Smirnoff {
26874c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
26884c0da0ffSGleb Smirnoff 
26894c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
26904c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
26914c0da0ffSGleb Smirnoff 			return (v);
26924c0da0ffSGleb Smirnoff 
26934c0da0ffSGleb Smirnoff 	return (NULL);
26944c0da0ffSGleb Smirnoff }
26954c0da0ffSGleb Smirnoff 
2696d7acafa1SMarius Strobl static uint32_t
2697d7acafa1SMarius Strobl bge_chipid(device_t dev)
269895d67482SBill Paul {
2699978f2704SMarius Strobl 	uint32_t id;
270095d67482SBill Paul 
2701a5779553SStanislav Sedov 	id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2702a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
27031108273aSPyun YongHyeon 	if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
27041108273aSPyun YongHyeon 		/*
2705d7acafa1SMarius Strobl 		 * Find the ASCI revision.  Different chips use different
2706d7acafa1SMarius Strobl 		 * registers.
27071108273aSPyun YongHyeon 		 */
27081108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
2709cb2404b4SSepherosa Ziehau 		case BCOM_DEVICEID_BCM5717C:
27101ca32af8SSepherosa Ziehau 			/* 5717 C0 seems to belong to 5720 line. */
27111ca32af8SSepherosa Ziehau 			id = BGE_CHIPID_BCM5720_A0;
27121ca32af8SSepherosa Ziehau 			break;
27131ca32af8SSepherosa Ziehau 		case BCOM_DEVICEID_BCM5717:
27141108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
2715bbe2ca75SPyun YongHyeon 		case BCOM_DEVICEID_BCM5719:
271650515680SPyun YongHyeon 		case BCOM_DEVICEID_BCM5720:
27172927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5725:
27182927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5727:
27192927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5762:
272067129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57764:
272167129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57767:
272267129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57787:
27231108273aSPyun YongHyeon 			id = pci_read_config(dev,
27241108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
27251108273aSPyun YongHyeon 			break;
2726b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57761:
2727fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57762:
2728b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57765:
2729fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57766:
2730b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57781:
273167129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57782:
2732b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57785:
273367129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57786:
2734b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57791:
2735b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57795:
2736b4a256acSPyun YongHyeon 			id = pci_read_config(dev,
2737b4a256acSPyun YongHyeon 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2738b4a256acSPyun YongHyeon 			break;
27391108273aSPyun YongHyeon 		default:
2740d7acafa1SMarius Strobl 			id = pci_read_config(dev, BGE_PCI_PRODID_ASICREV, 4);
27411108273aSPyun YongHyeon 		}
27421108273aSPyun YongHyeon 	}
2743d7acafa1SMarius Strobl 	return (id);
2744d7acafa1SMarius Strobl }
2745d7acafa1SMarius Strobl 
2746d7acafa1SMarius Strobl /*
2747d7acafa1SMarius Strobl  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
2748d7acafa1SMarius Strobl  * against our list and return its name if we find a match.
2749d7acafa1SMarius Strobl  *
2750d7acafa1SMarius Strobl  * Note that since the Broadcom controller contains VPD support, we
2751d7acafa1SMarius Strobl  * try to get the device name string from the controller itself instead
2752d7acafa1SMarius Strobl  * of the compiled-in string. It guarantees we'll always announce the
2753d7acafa1SMarius Strobl  * right product name. We fall back to the compiled-in string when
2754d7acafa1SMarius Strobl  * VPD is unavailable or corrupt.
2755d7acafa1SMarius Strobl  */
2756d7acafa1SMarius Strobl static int
2757d7acafa1SMarius Strobl bge_probe(device_t dev)
2758d7acafa1SMarius Strobl {
2759d7acafa1SMarius Strobl 	char buf[96];
2760d7acafa1SMarius Strobl 	char model[64];
2761d7acafa1SMarius Strobl 	const struct bge_revision *br;
2762d7acafa1SMarius Strobl 	const char *pname;
2763d7acafa1SMarius Strobl 	struct bge_softc *sc;
2764d7acafa1SMarius Strobl 	const struct bge_type *t = bge_devs;
2765d7acafa1SMarius Strobl 	const struct bge_vendor *v;
2766d7acafa1SMarius Strobl 	uint32_t id;
2767d7acafa1SMarius Strobl 	uint16_t did, vid;
2768d7acafa1SMarius Strobl 
2769d7acafa1SMarius Strobl 	sc = device_get_softc(dev);
2770d7acafa1SMarius Strobl 	sc->bge_dev = dev;
2771d7acafa1SMarius Strobl 	vid = pci_get_vendor(dev);
2772d7acafa1SMarius Strobl 	did = pci_get_device(dev);
2773d7acafa1SMarius Strobl 	while(t->bge_vid != 0) {
2774d7acafa1SMarius Strobl 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2775d7acafa1SMarius Strobl 			id = bge_chipid(dev);
27764c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
2777852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2778852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
2779d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s", pname);
2780d7acafa1SMarius Strobl 			else {
2781d7acafa1SMarius Strobl 				v = bge_lookup_vendor(vid);
2782d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s %s",
2783d7acafa1SMarius Strobl 				    v != NULL ? v->v_name : "Unknown",
27847c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
27852ad1b396SMarius Strobl 				    "NetXtreme/NetLink Ethernet Controller");
2786d7acafa1SMarius Strobl 			}
2787d7acafa1SMarius Strobl 			snprintf(buf, sizeof(buf), "%s, %sASIC rev. %#08x",
2788d7acafa1SMarius Strobl 			    model, br != NULL ? "" : "unknown ", id);
27894c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
2790d7acafa1SMarius Strobl 			return (BUS_PROBE_DEFAULT);
279195d67482SBill Paul 		}
279295d67482SBill Paul 		t++;
279395d67482SBill Paul 	}
279495d67482SBill Paul 
279595d67482SBill Paul 	return (ENXIO);
279695d67482SBill Paul }
279795d67482SBill Paul 
2798f41ac2beSBill Paul static void
27993f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2800f41ac2beSBill Paul {
2801f41ac2beSBill Paul 	int i;
2802f41ac2beSBill Paul 
28033f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2804f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2805f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
28060ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2807f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2808f41ac2beSBill Paul 	}
2809943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2810943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2811943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2812f41ac2beSBill Paul 
28133f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2814f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2815f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2816f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2817f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2818f41ac2beSBill Paul 	}
2819943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2820943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2821943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2822f41ac2beSBill Paul 
28233f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2824f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2825f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
28260ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2827f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2828f41ac2beSBill Paul 	}
2829f41ac2beSBill Paul 
28300ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
28310ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2832c0220d81SPyun YongHyeon 	if (sc->bge_cdata.bge_mtag_jumbo)
2833c0220d81SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
28340ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
28350ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2836f41ac2beSBill Paul 
28373f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2838068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_std_ring_paddr)
2839e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2840e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2841068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_std_ring)
2842f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2843f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2844f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2845f41ac2beSBill Paul 
2846f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2847f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2848f41ac2beSBill Paul 
28493f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2850068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_jumbo_ring_paddr)
2851e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2852e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2853e65bed95SPyun YongHyeon 
2854068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_jumbo_ring)
2855f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2856f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2857f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2858f41ac2beSBill Paul 
2859f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2860f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2861f41ac2beSBill Paul 
28623f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2863068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_return_ring_paddr)
2864e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2865e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2866e65bed95SPyun YongHyeon 
2867068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_return_ring)
2868f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2869f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2870f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2871f41ac2beSBill Paul 
2872f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2873f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2874f41ac2beSBill Paul 
28753f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2876068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_tx_ring_paddr)
2877e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2878e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2879e65bed95SPyun YongHyeon 
2880068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_tx_ring)
2881f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2882f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2883f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2884f41ac2beSBill Paul 
2885f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2886f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2887f41ac2beSBill Paul 
28883f74909aSGleb Smirnoff 	/* Destroy status block. */
2889068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_status_block_paddr)
2890e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2891e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2892e65bed95SPyun YongHyeon 
2893068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_status_block)
2894f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2895f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2896f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2897f41ac2beSBill Paul 
2898f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2899f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2900f41ac2beSBill Paul 
29013f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2902068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_stats_paddr)
2903e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2904e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2905e65bed95SPyun YongHyeon 
2906068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_stats)
2907f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2908f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2909f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2910f41ac2beSBill Paul 
2911f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2912f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2913f41ac2beSBill Paul 
29145b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
29155b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
29165b610048SPyun YongHyeon 
29173f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2918f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2919f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2920f41ac2beSBill Paul }
2921f41ac2beSBill Paul 
2922f41ac2beSBill Paul static int
29235b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
29245b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
29255b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2926f41ac2beSBill Paul {
29273f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
29285b610048SPyun YongHyeon 	int error;
2929f41ac2beSBill Paul 
29305b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2931fdd45796SPyun YongHyeon 	    alignment, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
29325b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
29335b610048SPyun YongHyeon 	if (error != 0) {
29345b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29355b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
29365b610048SPyun YongHyeon 		return (ENOMEM);
29375b610048SPyun YongHyeon 	}
29385b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
29395b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
29405b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
29415b610048SPyun YongHyeon 	if (error != 0) {
29425b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29435b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
29445b610048SPyun YongHyeon 		return (ENOMEM);
29455b610048SPyun YongHyeon 	}
29465b610048SPyun YongHyeon 	/* Load the address of the ring. */
29475b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
29485b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
29495b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
29505b610048SPyun YongHyeon 	if (error != 0) {
29515b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29525b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
29535b610048SPyun YongHyeon 		return (ENOMEM);
29545b610048SPyun YongHyeon 	}
29555b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
29565b610048SPyun YongHyeon 	return (0);
29575b610048SPyun YongHyeon }
29585b610048SPyun YongHyeon 
29595b610048SPyun YongHyeon static int
29605b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
29615b610048SPyun YongHyeon {
29625b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2963fdd45796SPyun YongHyeon 	bus_size_t rxmaxsegsz, sbsz, txsegsz, txmaxsegsz;
29645b610048SPyun YongHyeon 	int i, error;
2965f41ac2beSBill Paul 
2966f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2967f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2968f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2969f41ac2beSBill Paul 	/*
2970f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2971f41ac2beSBill Paul 	 */
29724eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2973f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
29744eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
29754eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2976e65bed95SPyun YongHyeon 	if (error != 0) {
2977fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2978fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2979e65bed95SPyun YongHyeon 		return (ENOMEM);
2980e65bed95SPyun YongHyeon 	}
2981e65bed95SPyun YongHyeon 
29825b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
29835b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
29845b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
29855b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
29865b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
29875b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
29885b610048SPyun YongHyeon 	if (error)
29895b610048SPyun YongHyeon 		return (error);
29905b610048SPyun YongHyeon 
29915b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
29925b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
29935b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
29945b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
29955b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
29965b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
29975b610048SPyun YongHyeon 	if (error)
29985b610048SPyun YongHyeon 		return (error);
29995b610048SPyun YongHyeon 
30005b610048SPyun YongHyeon 	/* Create tag for TX ring. */
30015b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
30025b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
30035b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
30045b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
30055b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
30065b610048SPyun YongHyeon 	if (error)
30075b610048SPyun YongHyeon 		return (error);
30085b610048SPyun YongHyeon 
3009f41ac2beSBill Paul 	/*
30105b610048SPyun YongHyeon 	 * Create tag for status block.
30115b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
30125b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
30135b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
30145b610048SPyun YongHyeon 	 * of configured number of ring.
3015f41ac2beSBill Paul 	 */
30165b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
30175b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
30185b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
30195b610048SPyun YongHyeon 	else
30205b610048SPyun YongHyeon 		sbsz = 32;
30215b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
30225b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
30235b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
30245b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
30255b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
30265b610048SPyun YongHyeon 	if (error)
30275b610048SPyun YongHyeon 		return (error);
30285b610048SPyun YongHyeon 
302912c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
303012c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
303112c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
303212c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
303312c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
303412c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
303512c65daeSPyun YongHyeon 	if (error)
303612c65daeSPyun YongHyeon 		return (error);
303712c65daeSPyun YongHyeon 
30385b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
30395b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
30405b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
30415b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
30425b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
30435b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
30445b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
30455b610048SPyun YongHyeon 		if (error)
30465b610048SPyun YongHyeon 			return (error);
30475b610048SPyun YongHyeon 	}
30485b610048SPyun YongHyeon 
30495b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
3050d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
3051d2ffe15aSPyun YongHyeon 		/*
3052d2ffe15aSPyun YongHyeon 		 * XXX
3053d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
3054d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
3055062af0b0SPyun YongHyeon 		 * Both limiting DMA address space to 32bits and flushing
3056062af0b0SPyun YongHyeon 		 * mailbox write seem to address the issue.
3057d2ffe15aSPyun YongHyeon 		 */
3058062af0b0SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3059d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
3060d2ffe15aSPyun YongHyeon 	}
3061fdd45796SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev), 1, 0, lowaddr,
3062fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 0,
3063fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
3064fdd45796SPyun YongHyeon 	    &sc->bge_cdata.bge_buffer_tag);
30655b610048SPyun YongHyeon 	if (error != 0) {
30665b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
30675b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
30685b610048SPyun YongHyeon 		return (ENOMEM);
30695b610048SPyun YongHyeon 	}
30705b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
30711108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
3072ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
3073ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
3074ca3f1187SPyun YongHyeon 	} else {
3075ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
3076ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
3077ca3f1187SPyun YongHyeon 	}
30785b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
3079ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
3080ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
3081ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
3082f41ac2beSBill Paul 
3083f41ac2beSBill Paul 	if (error) {
30840ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
30850ac56796SPyun YongHyeon 		return (ENOMEM);
30860ac56796SPyun YongHyeon 	}
30870ac56796SPyun YongHyeon 
30885b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
3089f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
3090f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
3091f5459d4cSPyun YongHyeon 	else
3092f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
30935b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
3094f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
3095f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
30960ac56796SPyun YongHyeon 
30970ac56796SPyun YongHyeon 	if (error) {
30980ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
3099f41ac2beSBill Paul 		return (ENOMEM);
3100f41ac2beSBill Paul 	}
3101f41ac2beSBill Paul 
31023f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
3103943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3104943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
3105943787f3SPyun YongHyeon 	if (error) {
3106943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
3107943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
3108943787f3SPyun YongHyeon 		return (ENOMEM);
3109943787f3SPyun YongHyeon 	}
3110f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
31110ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3112f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
3113f41ac2beSBill Paul 		if (error) {
3114fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
3115fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
3116f41ac2beSBill Paul 			return (ENOMEM);
3117f41ac2beSBill Paul 		}
3118f41ac2beSBill Paul 	}
3119f41ac2beSBill Paul 
31203f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
3121f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
31220ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
3123f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
3124f41ac2beSBill Paul 		if (error) {
3125fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
31260ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
3127f41ac2beSBill Paul 			return (ENOMEM);
3128f41ac2beSBill Paul 		}
3129f41ac2beSBill Paul 	}
3130f41ac2beSBill Paul 
31315b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
31324c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
31335b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
31348a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
31351be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
31361be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
3137f41ac2beSBill Paul 		if (error) {
3138fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
31393f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
3140f41ac2beSBill Paul 			return (ENOMEM);
3141f41ac2beSBill Paul 		}
31423f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
3143943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3144943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
3145943787f3SPyun YongHyeon 		if (error) {
3146943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
31471b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
3148943787f3SPyun YongHyeon 			return (ENOMEM);
3149943787f3SPyun YongHyeon 		}
3150f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
3151f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3152f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
3153f41ac2beSBill Paul 			if (error) {
3154fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
31553f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
3156f41ac2beSBill Paul 				return (ENOMEM);
3157f41ac2beSBill Paul 			}
3158f41ac2beSBill Paul 		}
3159f41ac2beSBill Paul 	}
3160f41ac2beSBill Paul 
3161f41ac2beSBill Paul 	return (0);
3162f41ac2beSBill Paul }
3163f41ac2beSBill Paul 
3164bf6ef57aSJohn Polstra /*
3165bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
3166bf6ef57aSJohn Polstra  */
3167bf6ef57aSJohn Polstra static int
3168bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
3169bf6ef57aSJohn Polstra {
3170bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
317155aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
3172bf6ef57aSJohn Polstra 
317355aaf894SMarius Strobl 	d = pci_get_domain(dev);
3174bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
3175bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
3176bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
3177bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
317855aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
3179bf6ef57aSJohn Polstra 			return (1);
3180bf6ef57aSJohn Polstra 	return (0);
3181bf6ef57aSJohn Polstra }
3182bf6ef57aSJohn Polstra 
3183bf6ef57aSJohn Polstra /*
3184bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
3185bf6ef57aSJohn Polstra  */
3186bf6ef57aSJohn Polstra static int
3187bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
3188bf6ef57aSJohn Polstra {
3189bf6ef57aSJohn Polstra 	int can_use_msi = 0;
3190bf6ef57aSJohn Polstra 
3191d9fc28e4SPyun YongHyeon 	if (sc->bge_msi == 0)
31925c952e8dSPyun YongHyeon 		return (0);
31935c952e8dSPyun YongHyeon 
31941108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
31951108273aSPyun YongHyeon #ifdef DEVICE_POLLING
31961108273aSPyun YongHyeon 	return (0);
31971108273aSPyun YongHyeon #endif
3198bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
3199a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
3200bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
3201bf6ef57aSJohn Polstra 		/*
3202a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
3203a8376f70SMarius Strobl 		 * configured in single-port mode.
3204bf6ef57aSJohn Polstra 		 */
3205bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
3206bf6ef57aSJohn Polstra 			can_use_msi = 1;
3207bf6ef57aSJohn Polstra 		break;
3208bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
3209bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
3210bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
3211bf6ef57aSJohn Polstra 			can_use_msi = 1;
3212bf6ef57aSJohn Polstra 		break;
3213a8376f70SMarius Strobl 	default:
3214a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
3215bf6ef57aSJohn Polstra 			can_use_msi = 1;
3216bf6ef57aSJohn Polstra 	}
3217bf6ef57aSJohn Polstra 	return (can_use_msi);
3218bf6ef57aSJohn Polstra }
3219bf6ef57aSJohn Polstra 
322095d67482SBill Paul static int
3221062af0b0SPyun YongHyeon bge_mbox_reorder(struct bge_softc *sc)
3222062af0b0SPyun YongHyeon {
3223062af0b0SPyun YongHyeon 	/* Lists of PCI bridges that are known to reorder mailbox writes. */
3224062af0b0SPyun YongHyeon 	static const struct mbox_reorder {
3225062af0b0SPyun YongHyeon 		const uint16_t vendor;
3226062af0b0SPyun YongHyeon 		const uint16_t device;
3227062af0b0SPyun YongHyeon 		const char *desc;
322829658c96SDimitry Andric 	} mbox_reorder_lists[] = {
3229062af0b0SPyun YongHyeon 		{ 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
3230062af0b0SPyun YongHyeon 	};
3231062af0b0SPyun YongHyeon 	devclass_t pci, pcib;
3232062af0b0SPyun YongHyeon 	device_t bus, dev;
323347f4a4dcSMarius Strobl 	int i;
3234062af0b0SPyun YongHyeon 
3235062af0b0SPyun YongHyeon 	pci = devclass_find("pci");
3236062af0b0SPyun YongHyeon 	pcib = devclass_find("pcib");
3237062af0b0SPyun YongHyeon 	dev = sc->bge_dev;
3238062af0b0SPyun YongHyeon 	bus = device_get_parent(dev);
3239062af0b0SPyun YongHyeon 	for (;;) {
3240062af0b0SPyun YongHyeon 		dev = device_get_parent(bus);
3241062af0b0SPyun YongHyeon 		bus = device_get_parent(dev);
3242062af0b0SPyun YongHyeon 		if (device_get_devclass(dev) != pcib)
3243062af0b0SPyun YongHyeon 			break;
324447f4a4dcSMarius Strobl 		for (i = 0; i < nitems(mbox_reorder_lists); i++) {
3245062af0b0SPyun YongHyeon 			if (pci_get_vendor(dev) ==
3246062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].vendor &&
3247062af0b0SPyun YongHyeon 			    pci_get_device(dev) ==
3248062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].device) {
3249062af0b0SPyun YongHyeon 				device_printf(sc->bge_dev,
3250062af0b0SPyun YongHyeon 				    "enabling MBOX workaround for %s\n",
3251062af0b0SPyun YongHyeon 				    mbox_reorder_lists[i].desc);
3252062af0b0SPyun YongHyeon 				return (1);
3253062af0b0SPyun YongHyeon 			}
3254062af0b0SPyun YongHyeon 		}
3255062af0b0SPyun YongHyeon 		if (device_get_devclass(bus) != pci)
3256062af0b0SPyun YongHyeon 			break;
3257062af0b0SPyun YongHyeon 	}
3258062af0b0SPyun YongHyeon 	return (0);
3259062af0b0SPyun YongHyeon }
3260062af0b0SPyun YongHyeon 
3261ea9c3a30SPyun YongHyeon static void
3262ea9c3a30SPyun YongHyeon bge_devinfo(struct bge_softc *sc)
3263ea9c3a30SPyun YongHyeon {
3264ea9c3a30SPyun YongHyeon 	uint32_t cfg, clk;
3265ea9c3a30SPyun YongHyeon 
3266ea9c3a30SPyun YongHyeon 	device_printf(sc->bge_dev,
3267ea9c3a30SPyun YongHyeon 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; ",
3268ea9c3a30SPyun YongHyeon 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev);
3269ea9c3a30SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
3270ea9c3a30SPyun YongHyeon 		printf("PCI-E\n");
3271ea9c3a30SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_PCIX) {
3272ea9c3a30SPyun YongHyeon 		printf("PCI-X ");
3273ea9c3a30SPyun YongHyeon 		cfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3274ea9c3a30SPyun YongHyeon 		if (cfg == BGE_MISCCFG_BOARD_ID_5704CIOBE)
3275ea9c3a30SPyun YongHyeon 			clk = 133;
3276ea9c3a30SPyun YongHyeon 		else {
3277ea9c3a30SPyun YongHyeon 			clk = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
3278ea9c3a30SPyun YongHyeon 			switch (clk) {
3279ea9c3a30SPyun YongHyeon 			case 0:
3280ea9c3a30SPyun YongHyeon 				clk = 33;
3281ea9c3a30SPyun YongHyeon 				break;
3282ea9c3a30SPyun YongHyeon 			case 2:
3283ea9c3a30SPyun YongHyeon 				clk = 50;
3284ea9c3a30SPyun YongHyeon 				break;
3285ea9c3a30SPyun YongHyeon 			case 4:
3286ea9c3a30SPyun YongHyeon 				clk = 66;
3287ea9c3a30SPyun YongHyeon 				break;
3288ea9c3a30SPyun YongHyeon 			case 6:
3289ea9c3a30SPyun YongHyeon 				clk = 100;
3290ea9c3a30SPyun YongHyeon 				break;
3291ea9c3a30SPyun YongHyeon 			case 7:
3292ea9c3a30SPyun YongHyeon 				clk = 133;
3293ea9c3a30SPyun YongHyeon 				break;
3294ea9c3a30SPyun YongHyeon 			}
3295ea9c3a30SPyun YongHyeon 		}
3296ea9c3a30SPyun YongHyeon 		printf("%u MHz\n", clk);
3297ea9c3a30SPyun YongHyeon 	} else {
3298ea9c3a30SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3299ea9c3a30SPyun YongHyeon 			printf("PCI on PCI-X ");
3300ea9c3a30SPyun YongHyeon 		else
3301ea9c3a30SPyun YongHyeon 			printf("PCI ");
3302ea9c3a30SPyun YongHyeon 		cfg = pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4);
3303ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_PCI_BUSSPEED)
3304ea9c3a30SPyun YongHyeon 			clk = 66;
3305ea9c3a30SPyun YongHyeon 		else
3306ea9c3a30SPyun YongHyeon 			clk = 33;
3307ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_32BIT_BUS)
3308ea9c3a30SPyun YongHyeon 			printf("%u MHz; 32bit\n", clk);
3309ea9c3a30SPyun YongHyeon 		else
3310ea9c3a30SPyun YongHyeon 			printf("%u MHz; 64bit\n", clk);
3311ea9c3a30SPyun YongHyeon 	}
3312ea9c3a30SPyun YongHyeon }
3313ea9c3a30SPyun YongHyeon 
3314062af0b0SPyun YongHyeon static int
33153f74909aSGleb Smirnoff bge_attach(device_t dev)
331695d67482SBill Paul {
3317fba8b109SMarcel Moolenaar 	if_t ifp;
331895d67482SBill Paul 	struct bge_softc *sc;
3319548c8f1aSPyun YongHyeon 	uint32_t hwcfg = 0, misccfg, pcistate;
332008013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
3321ad4328baSMarius Strobl 	int capmask, error, reg, rid, trys;
332295d67482SBill Paul 
332395d67482SBill Paul 	sc = device_get_softc(dev);
332495d67482SBill Paul 	sc->bge_dev = dev;
332595d67482SBill Paul 
3326e010b055SPyun YongHyeon 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
3327dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
3328e010b055SPyun YongHyeon 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
3329dfe0df9aSPyun YongHyeon 
333095d67482SBill Paul 	pci_enable_busmaster(dev);
333195d67482SBill Paul 
3332ad4328baSMarius Strobl 	/*
3333ad4328baSMarius Strobl 	 * Allocate control/status registers.
3334ad4328baSMarius Strobl 	 */
3335736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
33365f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
333744f8f2fcSMarius Strobl 	    RF_ACTIVE);
333895d67482SBill Paul 
333995d67482SBill Paul 	if (sc->bge_res == NULL) {
3340548c8f1aSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map BAR0 memory\n");
334195d67482SBill Paul 		error = ENXIO;
334295d67482SBill Paul 		goto fail;
334395d67482SBill Paul 	}
334495d67482SBill Paul 
33454f09c4c7SMarius Strobl 	/* Save various chip information. */
3346548c8f1aSPyun YongHyeon 	sc->bge_func_addr = pci_get_function(dev);
3347d7acafa1SMarius Strobl 	sc->bge_chipid = bge_chipid(dev);
3348e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
3349e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
3350e53d81eeSPaul Saab 
3351a813ed78SPyun YongHyeon 	/* Set default PHY address. */
3352daeeb75cSPyun YongHyeon 	sc->bge_phy_addr = 1;
33531108273aSPyun YongHyeon 	 /*
33541108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
33551108273aSPyun YongHyeon 	  *
33561108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
33571108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
33581108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
33591108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
33601108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
3361bbe2ca75SPyun YongHyeon 	  * BCM5719  |   1   |   8   |   2   |   9   |
336250515680SPyun YongHyeon 	  * BCM5720  |   1   |   8   |   2   |   9   |
33631108273aSPyun YongHyeon 	  *
3364548c8f1aSPyun YongHyeon 	  *          | F2 Cu | F2 Sr | F3 Cu | F3 Sr |
3365548c8f1aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
3366548c8f1aSPyun YongHyeon 	  * BCM57XX  |   X   |   X   |   X   |   X   |
3367548c8f1aSPyun YongHyeon 	  * BCM5704  |   X   |   X   |   X   |   X   |
3368548c8f1aSPyun YongHyeon 	  * BCM5717  |   X   |   X   |   X   |   X   |
3369548c8f1aSPyun YongHyeon 	  * BCM5719  |   3   |   10  |   4   |   11  |
3370548c8f1aSPyun YongHyeon 	  * BCM5720  |   X   |   X   |   X   |   X   |
3371548c8f1aSPyun YongHyeon 	  *
33721108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
33731108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
33741108273aSPyun YongHyeon 	  */
3375bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
337650515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
337750515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
3378548c8f1aSPyun YongHyeon 		if (sc->bge_chipid != BGE_CHIPID_BCM5717_A0) {
33791108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
33801108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
3381daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33821108273aSPyun YongHyeon 			else
3383daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
3384bbe2ca75SPyun YongHyeon 		} else {
33851108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
33861108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
3387daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33881108273aSPyun YongHyeon 			else
3389daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
33901108273aSPyun YongHyeon 		}
33911108273aSPyun YongHyeon 	}
3392a813ed78SPyun YongHyeon 
33935fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
33945fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
339508013fd3SMarius Strobl 
33960dae9719SJung-uk Kim 	/* Save chipset family. */
33970dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
33982927f01fSPyun YongHyeon 	case BGE_ASICREV_BCM5762:
3399fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57765:
3400fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57766:
3401fe26ad88SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_57765_PLUS;
3402fe26ad88SPyun YongHyeon 		/* FALLTHROUGH */
34031108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3404bbe2ca75SPyun YongHyeon 	case BGE_ASICREV_BCM5719:
340550515680SPyun YongHyeon 	case BGE_ASICREV_BCM5720:
34061108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
34071108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
3408b4a256acSPyun YongHyeon 		    BGE_FLAG_JUMBO_FRAME;
340929b44b09SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
341029b44b09SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
341129b44b09SPyun YongHyeon 			/*
341229b44b09SPyun YongHyeon 			 * Enable work around for DMA engine miscalculation
341329b44b09SPyun YongHyeon 			 * of TXMBUF available space.
341429b44b09SPyun YongHyeon 			 */
341529b44b09SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_RDMA_BUG;
3416bbe2ca75SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3417bbe2ca75SPyun YongHyeon 			    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3418bbe2ca75SPyun YongHyeon 				/* Jumbo frame on BCM5719 A0 does not work. */
3419463a7e27SPyun YongHyeon 				sc->bge_flags &= ~BGE_FLAG_JUMBO;
3420bbe2ca75SPyun YongHyeon 			}
342129b44b09SPyun YongHyeon 		}
34221108273aSPyun YongHyeon 		break;
3423a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
3424a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
3425a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
3426a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
3427a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
3428a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
3429a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
3430a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
3431a5779553SStanislav Sedov 		break;
34320dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
34330dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
34340dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
34350dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
34367ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
34370dae9719SJung-uk Kim 		break;
34380dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
34390dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
34400dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
3441f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
34429fe569d8SXin LI 		/* FALLTHROUGH */
34430dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
34440dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
344538cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
34460dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
34479fe569d8SXin LI 		/* FALLTHROUGH */
34480dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
34490dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
34500dae9719SJung-uk Kim 		break;
34510dae9719SJung-uk Kim 	}
34520dae9719SJung-uk Kim 
3453548c8f1aSPyun YongHyeon 	/* Identify chips with APE processor. */
3454548c8f1aSPyun YongHyeon 	switch (sc->bge_asicrev) {
3455548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3456548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5719:
3457548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5720:
3458548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5761:
34592927f01fSPyun YongHyeon 	case BGE_ASICREV_BCM5762:
3460548c8f1aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_APE;
3461548c8f1aSPyun YongHyeon 		break;
3462548c8f1aSPyun YongHyeon 	}
3463548c8f1aSPyun YongHyeon 
3464548c8f1aSPyun YongHyeon 	/* Chips with APE need BAR2 access for APE registers/memory. */
3465548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) != 0) {
3466548c8f1aSPyun YongHyeon 		rid = PCIR_BAR(2);
3467548c8f1aSPyun YongHyeon 		sc->bge_res2 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
3468548c8f1aSPyun YongHyeon 		    RF_ACTIVE);
3469548c8f1aSPyun YongHyeon 		if (sc->bge_res2 == NULL) {
3470548c8f1aSPyun YongHyeon 			device_printf (sc->bge_dev,
3471548c8f1aSPyun YongHyeon 			    "couldn't map BAR2 memory\n");
3472548c8f1aSPyun YongHyeon 			error = ENXIO;
3473548c8f1aSPyun YongHyeon 			goto fail;
3474548c8f1aSPyun YongHyeon 		}
3475548c8f1aSPyun YongHyeon 
3476548c8f1aSPyun YongHyeon 		/* Enable APE register/memory access by host driver. */
3477548c8f1aSPyun YongHyeon 		pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
3478548c8f1aSPyun YongHyeon 		pcistate |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
3479548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
3480548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
3481548c8f1aSPyun YongHyeon 		pci_write_config(dev, BGE_PCI_PCISTATE, pcistate, 4);
3482548c8f1aSPyun YongHyeon 
3483548c8f1aSPyun YongHyeon 		bge_ape_lock_init(sc);
3484548c8f1aSPyun YongHyeon 		bge_ape_read_fw_ver(sc);
3485548c8f1aSPyun YongHyeon 	}
3486548c8f1aSPyun YongHyeon 
3487749a5269SMarius Strobl 	/* Add SYSCTLs, requires the chipset family to be set. */
3488749a5269SMarius Strobl 	bge_add_sysctls(sc);
3489749a5269SMarius Strobl 
3490a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
34911108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
34921108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3493a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3494a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
3495a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
3496a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
3497a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
3498a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
3499a813ed78SPyun YongHyeon 	else
3500a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
35017ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
35027ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
35037ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
3504a813ed78SPyun YongHyeon 
3505f681b29aSPyun YongHyeon 	/*
3506d4622124SPyun YongHyeon 	 * All Broadcom controllers have 4GB boundary DMA bug.
3507f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
3508f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3509f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3510f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
3511f681b29aSPyun YongHyeon 	 */
3512f681b29aSPyun YongHyeon 	sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
35134f0794ffSBjoern A. Zeeb 
3514d9820cd8SPyun YongHyeon 	/* BCM5755 or higher and BCM5906 have short DMA bug. */
3515d9820cd8SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
3516d9820cd8SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
3517d9820cd8SPyun YongHyeon 
3518a7fcfcf3SPyun YongHyeon 	/*
3519a7fcfcf3SPyun YongHyeon 	 * BCM5719 cannot handle DMA requests for DMA segments that
3520a7fcfcf3SPyun YongHyeon 	 * have larger than 4KB in size.  However the maximum DMA
3521a7fcfcf3SPyun YongHyeon 	 * segment size created in DMA tag is 4KB for TSO, so we
3522a7fcfcf3SPyun YongHyeon 	 * wouldn't encounter the issue here.
3523a7fcfcf3SPyun YongHyeon 	 */
3524a7fcfcf3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3525a7fcfcf3SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3526a7fcfcf3SPyun YongHyeon 
3527ea9c3a30SPyun YongHyeon 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3528fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
35294f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
35304f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
35314f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
353284ac96f8SPyun YongHyeon 	}
35334f0794ffSBjoern A. Zeeb 
3534fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
3535fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3536fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
3537fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3538fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
3539fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3540fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3541fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3542fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
3543fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3544fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3545fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3546fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3547d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57791 ||
3548d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57795 ||
3549fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3550fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
3551fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
3552d73ea7c6SPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
3553fb772a6cSMarius Strobl 	}
3554fb772a6cSMarius Strobl 
3555e53d81eeSPaul Saab 	/*
3556ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
3557ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
3558ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
3559ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
3560d7acafa1SMarius Strobl 	 * known bug which can't handle TSO if Ethernet header + IP/TCP
3561d7acafa1SMarius Strobl 	 * header is greater than 80 bytes. A workaround for the TSO
3562ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
3563ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
3564ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
3565ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
3566ca3f1187SPyun YongHyeon 	 */
35671108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
35681108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
35691108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
3570bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3571bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3572bbe2ca75SPyun YongHyeon 			/* TSO on BCM5719 A0 does not work. */
3573bbe2ca75SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_TSO3;
3574bbe2ca75SPyun YongHyeon 		}
35751108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
35764f4a16e1SPyun YongHyeon 		/*
35774f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
35784f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
3579be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
35804f4a16e1SPyun YongHyeon 		 */
35814f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3582be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3583be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3584ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
35854f4a16e1SPyun YongHyeon 	}
3586ca3f1187SPyun YongHyeon 
3587ca3f1187SPyun YongHyeon 	/*
35886f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
3589e53d81eeSPaul Saab 	 */
35903b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
35914c0da0ffSGleb Smirnoff 		/*
35926f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
35936f8718a3SScott Long 		 * must be a PCI Express device.
35946f8718a3SScott Long 		 */
35956f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
35960aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
359748630d79SPyun YongHyeon 		/* Extract supported maximum payload size. */
359848630d79SPyun YongHyeon 		sc->bge_mps = pci_read_config(dev, sc->bge_expcap +
359948630d79SPyun YongHyeon 		    PCIER_DEVICE_CAP, 2);
360048630d79SPyun YongHyeon 		sc->bge_mps = 128 << (sc->bge_mps & PCIEM_CAP_MAX_PAYLOAD);
360150515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
360250515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
360348630d79SPyun YongHyeon 			sc->bge_expmrq = 2048;
360448630d79SPyun YongHyeon 		else
360548630d79SPyun YongHyeon 			sc->bge_expmrq = 4096;
360648630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
36076f8718a3SScott Long 	} else {
36086f8718a3SScott Long 		/*
36096f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
36106f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
36114c0da0ffSGleb Smirnoff 		 */
36123b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
36130aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
361490447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
36154c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
3616652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
36176f8718a3SScott Long 	}
36184c0da0ffSGleb Smirnoff 
3619bf6ef57aSJohn Polstra 	/*
3620fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3621fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
3622fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3623fd4d32feSPyun YongHyeon 	 */
3624fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3625fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3626fd4d32feSPyun YongHyeon 	/*
3627062af0b0SPyun YongHyeon 	 * Some PCI-X bridges are known to trigger write reordering to
3628062af0b0SPyun YongHyeon 	 * the mailbox registers. Typical phenomena is watchdog timeouts
3629062af0b0SPyun YongHyeon 	 * caused by out-of-order TX completions.  Enable workaround for
3630062af0b0SPyun YongHyeon 	 * PCI-X devices that live behind these bridges.
3631062af0b0SPyun YongHyeon 	 * Note, PCI-X controllers can run in PCI mode so we can't use
3632062af0b0SPyun YongHyeon 	 * BGE_FLAG_PCIX flag to detect PCI-X controllers.
3633062af0b0SPyun YongHyeon 	 */
3634062af0b0SPyun YongHyeon 	if (sc->bge_pcixcap != 0 && bge_mbox_reorder(sc) != 0)
3635062af0b0SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_MBOX_REORDER;
3636062af0b0SPyun YongHyeon 	/*
3637bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
3638bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
3639bf6ef57aSJohn Polstra 	 * normal operation.
3640bf6ef57aSJohn Polstra 	 */
36410aaf1057SPyun YongHyeon 	rid = 0;
36423b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
36430aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
3644ad4328baSMarius Strobl 		reg = 1;
3645ad4328baSMarius Strobl 		if (bge_can_use_msi(sc) && pci_alloc_msi(dev, &reg) == 0) {
3646bf6ef57aSJohn Polstra 			rid = 1;
3647bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
36480aaf1057SPyun YongHyeon 		}
36490aaf1057SPyun YongHyeon 	}
3650bf6ef57aSJohn Polstra 
36511108273aSPyun YongHyeon 	/*
36521108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
36531108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
36541108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
36551108273aSPyun YongHyeon 	 */
36561108273aSPyun YongHyeon #ifndef DEVICE_POLLING
36571108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
36581108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
36591108273aSPyun YongHyeon #endif
36601108273aSPyun YongHyeon 
3661bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3662ad4328baSMarius Strobl 	    RF_ACTIVE | (rid != 0 ? 0 : RF_SHAREABLE));
3663bf6ef57aSJohn Polstra 
3664bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
3665bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
3666bf6ef57aSJohn Polstra 		error = ENXIO;
3667bf6ef57aSJohn Polstra 		goto fail;
3668bf6ef57aSJohn Polstra 	}
3669bf6ef57aSJohn Polstra 
3670ea9c3a30SPyun YongHyeon 	bge_devinfo(sc);
36714f09c4c7SMarius Strobl 
36728cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
3673548c8f1aSPyun YongHyeon 	/* No ASF if APE present. */
3674548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) == 0) {
3675888b47f0SPyun YongHyeon 		if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3676888b47f0SPyun YongHyeon 		    BGE_SRAM_DATA_SIG_MAGIC)) {
3677548c8f1aSPyun YongHyeon 			if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG) &
3678548c8f1aSPyun YongHyeon 			    BGE_HWCFG_ASF) {
36798cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_ENABLE;
36808cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_STACKUP;
3681d67eba2fSPyun YongHyeon 				if (BGE_IS_575X_PLUS(sc))
36828cb1383cSDoug Ambrisko 					sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
36838cb1383cSDoug Ambrisko 			}
36848cb1383cSDoug Ambrisko 		}
3685548c8f1aSPyun YongHyeon 	}
36868cb1383cSDoug Ambrisko 
36878cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
36883dd76c98SPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
36898cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
36908cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
36918cb1383cSDoug Ambrisko 		error = ENXIO;
36928cb1383cSDoug Ambrisko 		goto fail;
36938cb1383cSDoug Ambrisko 	}
36948cb1383cSDoug Ambrisko 
36953dd76c98SPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
36963dd76c98SPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
369795d67482SBill Paul 
369895d67482SBill Paul 	if (bge_chipinit(sc)) {
3699fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
370095d67482SBill Paul 		error = ENXIO;
370195d67482SBill Paul 		goto fail;
370295d67482SBill Paul 	}
370395d67482SBill Paul 
370438cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
370538cc658fSJohn Baldwin 	if (error) {
370608013fd3SMarius Strobl 		device_printf(sc->bge_dev,
370708013fd3SMarius Strobl 		    "failed to read station address\n");
370895d67482SBill Paul 		error = ENXIO;
370995d67482SBill Paul 		goto fail;
371095d67482SBill Paul 	}
371195d67482SBill Paul 
3712f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
37131108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
37141108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
37151108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3716f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3717f41ac2beSBill Paul 	else
3718f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3719f41ac2beSBill Paul 
37205b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3721fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3722fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3723f41ac2beSBill Paul 		error = ENXIO;
3724f41ac2beSBill Paul 		goto fail;
3725f41ac2beSBill Paul 	}
3726f41ac2beSBill Paul 
372795d67482SBill Paul 	/* Set default tuneable values. */
372895d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
372995d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
373095d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
37316f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
37326f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
373395d67482SBill Paul 
373435f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
373535f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
373635f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
373735f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
373835f945cdSPyun YongHyeon 
373995d67482SBill Paul 	/* Set up ifnet structure */
3740fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3741fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3742fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3743fc74a9f9SBrooks Davis 		error = ENXIO;
3744fc74a9f9SBrooks Davis 		goto fail;
3745fc74a9f9SBrooks Davis 	}
3746fba8b109SMarcel Moolenaar 	if_setsoftc(ifp, sc);
37479bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
3748fba8b109SMarcel Moolenaar 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
3749fba8b109SMarcel Moolenaar 	if_setioctlfn(ifp, bge_ioctl);
3750fba8b109SMarcel Moolenaar 	if_setstartfn(ifp, bge_start);
3751fba8b109SMarcel Moolenaar 	if_setinitfn(ifp, bge_init);
3752df360178SGleb Smirnoff 	if_setgetcounterfn(ifp, bge_get_counter);
37534a81240cSMarcel Moolenaar 	if_setsendqlen(ifp, BGE_TX_RING_CNT - 1);
3754fba8b109SMarcel Moolenaar 	if_setsendqready(ifp);
3755fba8b109SMarcel Moolenaar 	if_sethwassist(ifp, sc->bge_csum_features);
3756fba8b109SMarcel Moolenaar 	if_setcapabilities(ifp, IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
3757fba8b109SMarcel Moolenaar 	    IFCAP_VLAN_MTU);
37581108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3759fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, CSUM_TSO, 0);
3760fba8b109SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, IFCAP_TSO4 | IFCAP_VLAN_HWTSO, 0);
3761ca3f1187SPyun YongHyeon 	}
37624e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
3763fba8b109SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWCSUM, 0);
37644e35d186SJung-uk Kim #endif
3765fba8b109SMarcel Moolenaar 	if_setcapenable(ifp, if_getcapabilities(ifp));
376675719184SGleb Smirnoff #ifdef DEVICE_POLLING
3767fba8b109SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
376875719184SGleb Smirnoff #endif
376995d67482SBill Paul 
3770a1d52896SBill Paul 	/*
3771d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3772d375e524SGleb Smirnoff 	 * to hardware bugs.
3773d375e524SGleb Smirnoff 	 */
3774d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3775fba8b109SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, 0, IFCAP_HWCSUM);
3776fba8b109SMarcel Moolenaar 		if_setcapenablebit(ifp, 0, IFCAP_HWCSUM);
3777fba8b109SMarcel Moolenaar 		if_sethwassist(ifp, 0);
3778d375e524SGleb Smirnoff 	}
3779d375e524SGleb Smirnoff 
3780d375e524SGleb Smirnoff 	/*
3781a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
378241abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
378341abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
378441abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
378541abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
378641abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
378741abcc1bSPaul Saab 	 * SK-9D41.
3788a1d52896SBill Paul 	 */
3789888b47f0SPyun YongHyeon 	if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3790888b47f0SPyun YongHyeon 		hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
37915fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
37925fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3793f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3794f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3795fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3796f6789fbaSPyun YongHyeon 			error = ENXIO;
3797f6789fbaSPyun YongHyeon 			goto fail;
3798f6789fbaSPyun YongHyeon 		}
379941abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
380041abcc1bSPaul Saab 	}
380141abcc1bSPaul Saab 
380295d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3803ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3804ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
380570c2071bSPyun YongHyeon 		if (BGE_IS_5705_PLUS(sc)) {
3806ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
380770c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
380870c2071bSPyun YongHyeon 		} else
3809652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3810ea3b4127SPyun YongHyeon 	}
381195d67482SBill Paul 
381270c2071bSPyun YongHyeon 	/* Set various PHY bug flags. */
381370c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
381470c2071bSPyun YongHyeon 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
381570c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
381670c2071bSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
381770c2071bSPyun YongHyeon 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
381870c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
381970c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
382070c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
382170c2071bSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
382270c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
382370c2071bSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
382470c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
382570c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
3826fe26ad88SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780 &&
3827fe26ad88SPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc)) {
382870c2071bSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
382970c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
383070c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
383170c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
383270c2071bSPyun YongHyeon 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
383370c2071bSPyun YongHyeon 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
383470c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
383570c2071bSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
383670c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
383770c2071bSPyun YongHyeon 		} else
383870c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
383970c2071bSPyun YongHyeon 	}
384070c2071bSPyun YongHyeon 
384170c2071bSPyun YongHyeon 	/*
3842d73ea7c6SPyun YongHyeon 	 * Don't enable Ethernet@WireSpeed for the 5700 or the
384370c2071bSPyun YongHyeon 	 * 5705 A0 and A1 chips.
384470c2071bSPyun YongHyeon 	 */
384570c2071bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
384670c2071bSPyun YongHyeon 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
384770c2071bSPyun YongHyeon 	    (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
3848d73ea7c6SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)))
384970c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
385070c2071bSPyun YongHyeon 
3851652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
385209a8241fSGleb Smirnoff 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
38530c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
38540c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
38556098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
38566098821cSJung-uk Kim 		    0, NULL);
385795d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
385895d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3859da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
386095d67482SBill Paul 	} else {
386195d67482SBill Paul 		/*
38628cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
38638cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
38648cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
38658cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
38668cb1383cSDoug Ambrisko 		 * the PHY.
386795d67482SBill Paul 		 */
38684012d104SMarius Strobl 		trys = 0;
38698cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
38708cb1383cSDoug Ambrisko again:
38718cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
38728cb1383cSDoug Ambrisko 
3873fba8b109SMarcel Moolenaar 		error = mii_attach(dev, &sc->bge_miibus, ifp,
3874fba8b109SMarcel Moolenaar 		    (ifm_change_cb_t)bge_ifmedia_upd,
3875fba8b109SMarcel Moolenaar 		    (ifm_stat_cb_t)bge_ifmedia_sts, capmask, sc->bge_phy_addr,
3876fba8b109SMarcel Moolenaar 		    MII_OFFSET_ANY, MIIF_DOPAUSE);
38778e5d93dbSMarius Strobl 		if (error != 0) {
38788cb1383cSDoug Ambrisko 			if (trys++ < 4) {
38798cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
3880daeeb75cSPyun YongHyeon 				bge_miibus_writereg(sc->bge_dev,
3881daeeb75cSPyun YongHyeon 				    sc->bge_phy_addr, MII_BMCR, BMCR_RESET);
38828cb1383cSDoug Ambrisko 				goto again;
38838cb1383cSDoug Ambrisko 			}
38848e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
388595d67482SBill Paul 			goto fail;
388695d67482SBill Paul 		}
38878cb1383cSDoug Ambrisko 
38888cb1383cSDoug Ambrisko 		/*
38898cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
38908cb1383cSDoug Ambrisko 		 */
38918cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
38928cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
389395d67482SBill Paul 	}
389495d67482SBill Paul 
389595d67482SBill Paul 	/*
3896e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3897e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3898e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3899e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3900e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3901e255b776SJohn Polstra 	 * payloads by copying the received packets.
3902e255b776SJohn Polstra 	 */
3903652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3904652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3905652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3906e255b776SJohn Polstra 
3907e255b776SJohn Polstra 	/*
390895d67482SBill Paul 	 * Call MI attach routine.
390995d67482SBill Paul 	 */
3910fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
39110f9bd73bSSam Leffler 
391261ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
3913fba8b109SMarcel Moolenaar 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
391461ccb9daSPyun YongHyeon 
39150f9bd73bSSam Leffler 	/*
39160f9bd73bSSam Leffler 	 * Hookup IRQ last.
39170f9bd73bSSam Leffler 	 */
3918dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3919dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
39207e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
39217e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3922dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3923dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3924dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3925dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3926dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3927e010b055SPyun YongHyeon 			error = ENOMEM;
3928dfe0df9aSPyun YongHyeon 			goto fail;
3929dfe0df9aSPyun YongHyeon 		}
3930d7acafa1SMarius Strobl 		error = taskqueue_start_threads(&sc->bge_tq, 1, PI_NET,
3931d7acafa1SMarius Strobl 		    "%s taskq", device_get_nameunit(sc->bge_dev));
3932d7acafa1SMarius Strobl 		if (error != 0) {
3933d7acafa1SMarius Strobl 			device_printf(dev, "could not start threads.\n");
3934d7acafa1SMarius Strobl 			ether_ifdetach(ifp);
3935d7acafa1SMarius Strobl 			goto fail;
3936d7acafa1SMarius Strobl 		}
3937dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3938dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3939dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3940dfe0df9aSPyun YongHyeon 	} else
3941dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3942dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3943dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
39440f9bd73bSSam Leffler 
39450f9bd73bSSam Leffler 	if (error) {
3946e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
3947fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
3948*ded66962SMark Johnston 		goto fail;
39490f9bd73bSSam Leffler 	}
395095d67482SBill Paul 
3951*ded66962SMark Johnston 	/* Attach driver netdump methods. */
3952*ded66962SMark Johnston 	NETDUMP_SET(ifp, bge);
3953*ded66962SMark Johnston 
395495d67482SBill Paul fail:
3955e010b055SPyun YongHyeon 	if (error)
3956e010b055SPyun YongHyeon 		bge_detach(dev);
395795d67482SBill Paul 	return (error);
395895d67482SBill Paul }
395995d67482SBill Paul 
396095d67482SBill Paul static int
39613f74909aSGleb Smirnoff bge_detach(device_t dev)
396295d67482SBill Paul {
396395d67482SBill Paul 	struct bge_softc *sc;
3964fba8b109SMarcel Moolenaar 	if_t ifp;
396595d67482SBill Paul 
396695d67482SBill Paul 	sc = device_get_softc(dev);
3967fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
396895d67482SBill Paul 
396975719184SGleb Smirnoff #ifdef DEVICE_POLLING
3970fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING)
397175719184SGleb Smirnoff 		ether_poll_deregister(ifp);
397275719184SGleb Smirnoff #endif
397375719184SGleb Smirnoff 
3974e010b055SPyun YongHyeon 	if (device_is_attached(dev)) {
3975e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
39760f9bd73bSSam Leffler 		BGE_LOCK(sc);
397795d67482SBill Paul 		bge_stop(sc);
39780f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
39795dda8085SOleg Bulyzhin 		callout_drain(&sc->bge_stat_ch);
3980e010b055SPyun YongHyeon 	}
39815dda8085SOleg Bulyzhin 
3982dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3983dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
398495d67482SBill Paul 
39850aba72ddSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
398695d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
39870aba72ddSPyun YongHyeon 	else if (sc->bge_miibus != NULL) {
398895d67482SBill Paul 		bus_generic_detach(dev);
398995d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
399095d67482SBill Paul 	}
399195d67482SBill Paul 
399295d67482SBill Paul 	bge_release_resources(sc);
399395d67482SBill Paul 
399495d67482SBill Paul 	return (0);
399595d67482SBill Paul }
399695d67482SBill Paul 
399795d67482SBill Paul static void
39983f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
399995d67482SBill Paul {
400095d67482SBill Paul 	device_t dev;
400195d67482SBill Paul 
400295d67482SBill Paul 	dev = sc->bge_dev;
400395d67482SBill Paul 
4004dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
4005dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
4006dfe0df9aSPyun YongHyeon 
400795d67482SBill Paul 	if (sc->bge_intrhand != NULL)
400895d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
400995d67482SBill Paul 
4010ad4328baSMarius Strobl 	if (sc->bge_irq != NULL) {
4011724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
4012ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_irq), sc->bge_irq);
4013724bd939SJohn Polstra 		pci_release_msi(dev);
4014ad4328baSMarius Strobl 	}
401595d67482SBill Paul 
401695d67482SBill Paul 	if (sc->bge_res != NULL)
401795d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
4018ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_res), sc->bge_res);
401995d67482SBill Paul 
4020548c8f1aSPyun YongHyeon 	if (sc->bge_res2 != NULL)
4021548c8f1aSPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY,
4022ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_res2), sc->bge_res2);
4023548c8f1aSPyun YongHyeon 
4024ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
4025ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
4026ad61f896SRuslan Ermilov 
4027f41ac2beSBill Paul 	bge_dma_free(sc);
402895d67482SBill Paul 
40290f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
40300f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
403195d67482SBill Paul }
403295d67482SBill Paul 
40338cb1383cSDoug Ambrisko static int
40343f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
403595d67482SBill Paul {
403695d67482SBill Paul 	device_t dev;
4037cc085b36SPyun YongHyeon 	uint32_t cachesize, command, mac_mode, mac_mode_mask, reset, val;
40386f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
40390aaf1057SPyun YongHyeon 	uint16_t devctl;
40405fea260fSMarius Strobl 	int i;
404195d67482SBill Paul 
404295d67482SBill Paul 	dev = sc->bge_dev;
404395d67482SBill Paul 
4044cc085b36SPyun YongHyeon 	mac_mode_mask = BGE_MACMODE_HALF_DUPLEX | BGE_MACMODE_PORTMODE;
4045548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4046548c8f1aSPyun YongHyeon 		mac_mode_mask |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
4047cc085b36SPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) & mac_mode_mask;
4048cc085b36SPyun YongHyeon 
404938cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
405038cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
40516f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
40526f8718a3SScott Long 			write_op = bge_writemem_direct;
40536f8718a3SScott Long 		else
40546f8718a3SScott Long 			write_op = bge_writemem_ind;
40559ba784dbSScott Long 	} else
40566f8718a3SScott Long 		write_op = bge_writereg_ind;
40576f8718a3SScott Long 
40583dd76c98SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
40593dd76c98SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5701) {
40603dd76c98SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
40613dd76c98SPyun YongHyeon 		for (i = 0; i < 8000; i++) {
40623dd76c98SPyun YongHyeon 			if (CSR_READ_4(sc, BGE_NVRAM_SWARB) &
40633dd76c98SPyun YongHyeon 			    BGE_NVRAMSWARB_GNT1)
40643dd76c98SPyun YongHyeon 				break;
40653dd76c98SPyun YongHyeon 			DELAY(20);
40663dd76c98SPyun YongHyeon 		}
40673dd76c98SPyun YongHyeon 		if (i == 8000) {
40683dd76c98SPyun YongHyeon 			if (bootverbose)
40693dd76c98SPyun YongHyeon 				device_printf(dev, "NVRAM lock timedout!\n");
40703dd76c98SPyun YongHyeon 		}
40713dd76c98SPyun YongHyeon 	}
4072548c8f1aSPyun YongHyeon 	/* Take APE lock when performing reset. */
4073548c8f1aSPyun YongHyeon 	bge_ape_lock(sc, BGE_APE_LOCK_GRC);
4074548c8f1aSPyun YongHyeon 
407595d67482SBill Paul 	/* Save some important PCI state. */
407695d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
407795d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
407895d67482SBill Paul 
407995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
408095d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4081e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
408295d67482SBill Paul 
40836f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
40846f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
4085a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
40866f8718a3SScott Long 		if (bootverbose)
4087333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
40886f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
40896f8718a3SScott Long 	}
40906f8718a3SScott Long 
40916f8718a3SScott Long 	/*
40926f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
40936f8718a3SScott Long 	 * When firmware finishes its initialization it will
4094888b47f0SPyun YongHyeon 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
40956f8718a3SScott Long 	 */
4096888b47f0SPyun YongHyeon 	bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
40976f8718a3SScott Long 
40980c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
4099e53d81eeSPaul Saab 
4100e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4101652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4102ad49eccfSPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
4103ad49eccfSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_5717_PLUS) == 0) {
41040c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
41050c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, 0x7E2C, 0x20);
4106ad49eccfSPyun YongHyeon 		}
4107e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
4108e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
41090c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
41100c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
4111e53d81eeSPaul Saab 		}
4112e53d81eeSPaul Saab 	}
4113e53d81eeSPaul Saab 
4114df4db538SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
4115df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
4116df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
4117df4db538SPyun YongHyeon 		    val | BGE_VCPU_STATUS_DRV_RESET);
4118df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
4119df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
4120df4db538SPyun YongHyeon 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
4121df4db538SPyun YongHyeon 	}
4122df4db538SPyun YongHyeon 
412321c9e407SDavid Christensen 	/*
41246f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
41256f8718a3SScott Long 	 * powered up in D0 uninitialized.
41266f8718a3SScott Long 	 */
41275512ca01SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc) &&
41285512ca01SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
4129caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
41306f8718a3SScott Long 
413195d67482SBill Paul 	/* Issue global reset */
41326f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
413395d67482SBill Paul 
4134cc085b36SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
4135cc085b36SPyun YongHyeon 		DELAY(100 * 1000);
4136cc085b36SPyun YongHyeon 	else
413795d67482SBill Paul 		DELAY(1000);
413895d67482SBill Paul 
4139e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4140652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4141e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
4142e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
41435fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
41445fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
4145e53d81eeSPaul Saab 		}
41460aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
4147389c8bd5SGavin Atkinson 		    sc->bge_expcap + PCIER_DEVICE_CTL, 2);
41480aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
4149389c8bd5SGavin Atkinson 		devctl &= ~(PCIEM_CTL_RELAXED_ORD_ENABLE |
4150389c8bd5SGavin Atkinson 		    PCIEM_CTL_NOSNOOP_ENABLE);
4151389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_CTL,
41520aaf1057SPyun YongHyeon 		    devctl, 2);
415348630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
41540aaf1057SPyun YongHyeon 		/* Clear error status. */
4155389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_STA,
4156389c8bd5SGavin Atkinson 		    PCIEM_STA_CORRECTABLE_ERROR |
4157389c8bd5SGavin Atkinson 		    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
4158389c8bd5SGavin Atkinson 		    PCIEM_STA_UNSUPPORTED_REQ, 2);
4159e53d81eeSPaul Saab 	}
4160e53d81eeSPaul Saab 
41613f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
416295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
416395d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4164e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
4165cc085b36SPyun YongHyeon 	val = BGE_PCISTATE_ROM_ENABLE | BGE_PCISTATE_ROM_RETRY_ENABLE;
4166cc085b36SPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0 &&
4167cc085b36SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_PCIX) != 0)
4168cc085b36SPyun YongHyeon 		val |= BGE_PCISTATE_RETRY_SAME_DMA;
4169548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4170548c8f1aSPyun YongHyeon 		val |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
4171548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
4172548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
4173cc085b36SPyun YongHyeon 	pci_write_config(dev, BGE_PCI_PCISTATE, val, 4);
417495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
417595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
4176cbb2b2feSPyun YongHyeon 	/*
4177cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
4178fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
4179cbb2b2feSPyun YongHyeon 	 * read stale status block.
4180cbb2b2feSPyun YongHyeon 	 */
4181cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
4182cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
4183cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
4184cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
4185cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
4186cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
4187cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4188cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4189cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
4190cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
4191cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4192cbb2b2feSPyun YongHyeon 		}
4193cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
4194cbb2b2feSPyun YongHyeon 		    devctl, 2);
4195cbb2b2feSPyun YongHyeon 	}
419622a4ecedSMarius Strobl 	/* Re-enable MSI, if necessary, and enable the memory arbiter. */
41974c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
4198bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
4199bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
42000aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
42010aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
42020aaf1057SPyun YongHyeon 			pci_write_config(dev,
42030aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
4204bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
4205bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
4206bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
4207bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
4208bf6ef57aSJohn Polstra 		}
42094c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
42104c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
42114c0da0ffSGleb Smirnoff 	} else
4212a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4213a7b0c314SPaul Saab 
4214cc085b36SPyun YongHyeon 	/* Fix up byte swapping. */
4215cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
4216cc085b36SPyun YongHyeon 
4217cc085b36SPyun YongHyeon 	val = CSR_READ_4(sc, BGE_MAC_MODE);
4218cc085b36SPyun YongHyeon 	val = (val & ~mac_mode_mask) | mac_mode;
4219cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
4220cc085b36SPyun YongHyeon 	DELAY(40);
4221cc085b36SPyun YongHyeon 
4222548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, BGE_APE_LOCK_GRC);
4223548c8f1aSPyun YongHyeon 
422438cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
422538cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
422638cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
422738cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
422838cc658fSJohn Baldwin 				break;
422938cc658fSJohn Baldwin 			DELAY(100);
423038cc658fSJohn Baldwin 		}
423138cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
4232333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
423338cc658fSJohn Baldwin 			return (1);
423438cc658fSJohn Baldwin 		}
423538cc658fSJohn Baldwin 	} else {
423695d67482SBill Paul 		/*
42376f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
423808013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
42395fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
42405fea260fSMarius Strobl 		 * address is fitted though.
424195d67482SBill Paul 		 */
424295d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
4243d5d23857SJung-uk Kim 			DELAY(10);
4244888b47f0SPyun YongHyeon 			val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
4245888b47f0SPyun YongHyeon 			if (val == ~BGE_SRAM_FW_MB_MAGIC)
424695d67482SBill Paul 				break;
424795d67482SBill Paul 		}
424895d67482SBill Paul 
42495fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
4250333704a3SPyun YongHyeon 			device_printf(dev,
4251333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
4252333704a3SPyun YongHyeon 			    val);
4253b4a256acSPyun YongHyeon 		/* BCM57765 A0 needs additional time before accessing. */
4254b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
4255b4a256acSPyun YongHyeon 			DELAY(10 * 1000);	/* XXX */
425638cc658fSJohn Baldwin 	}
425795d67482SBill Paul 
425895d67482SBill Paul 	/*
4259da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
4260da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
4261da3003f0SBill Paul 	 * to 1.2V.
4262da3003f0SBill Paul 	 */
4263652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
4264652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
42655fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
42665fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
42675fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
4268da3003f0SBill Paul 	}
4269da3003f0SBill Paul 
4270e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4271652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
4272b4a256acSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc) &&
4273a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
4274a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
4275a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
42765fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
42775fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
4278e53d81eeSPaul Saab 	}
42798cb1383cSDoug Ambrisko 
428050515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
428150515680SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
428250515680SPyun YongHyeon 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
428350515680SPyun YongHyeon 
42848cb1383cSDoug Ambrisko 	return (0);
428595d67482SBill Paul }
428695d67482SBill Paul 
4287e0b7b101SPyun YongHyeon static __inline void
4288e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
4289e0b7b101SPyun YongHyeon {
4290e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
4291e0b7b101SPyun YongHyeon 
4292e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
4293e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
4294e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
4295e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4296e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
4297e0b7b101SPyun YongHyeon }
4298e0b7b101SPyun YongHyeon 
4299e0b7b101SPyun YongHyeon static __inline void
4300e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
4301e0b7b101SPyun YongHyeon {
4302e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
4303e0b7b101SPyun YongHyeon 
4304e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
4305e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
4306e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
4307e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
4308e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
4309e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
4310e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4311e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
4312e0b7b101SPyun YongHyeon }
4313e0b7b101SPyun YongHyeon 
431495d67482SBill Paul /*
431595d67482SBill Paul  * Frame reception handling. This is called if there's a frame
431695d67482SBill Paul  * on the receive return list.
431795d67482SBill Paul  *
431895d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
43191be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
432095d67482SBill Paul  * 2) the frame is from the standard receive ring
432195d67482SBill Paul  */
432295d67482SBill Paul 
43231abcdbd1SAttilio Rao static int
4324dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
432595d67482SBill Paul {
4326fba8b109SMarcel Moolenaar 	if_t ifp;
43271abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
4328b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
432995d67482SBill Paul 
43307f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
43310f9bd73bSSam Leffler 
43323f74909aSGleb Smirnoff 	/* Nothing to do. */
43337f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
43341abcdbd1SAttilio Rao 		return (rx_npkts);
4335cfcb5025SOleg Bulyzhin 
4336fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
433795d67482SBill Paul 
4338f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
4339e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
4340f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
434115eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
4342f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
4343fba8b109SMarcel Moolenaar 	    if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
4344fba8b109SMarcel Moolenaar 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))
4345f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
434615eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
4347f41ac2beSBill Paul 
43487f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
434995d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
43503f74909aSGleb Smirnoff 		uint32_t		rxidx;
435195d67482SBill Paul 		struct mbuf		*m = NULL;
43523f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
435395d67482SBill Paul 		int			have_tag = 0;
435495d67482SBill Paul 
435575719184SGleb Smirnoff #ifdef DEVICE_POLLING
4356fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_POLLING) {
435775719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
435875719184SGleb Smirnoff 				break;
435975719184SGleb Smirnoff 			sc->rxcycles--;
436075719184SGleb Smirnoff 		}
436175719184SGleb Smirnoff #endif
436275719184SGleb Smirnoff 
43637f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
436495d67482SBill Paul 
436595d67482SBill Paul 		rxidx = cur_rx->bge_idx;
43667f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
436795d67482SBill Paul 
4368fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING &&
4369cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
437095d67482SBill Paul 			have_tag = 1;
437195d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
437295d67482SBill Paul 		}
437395d67482SBill Paul 
437495d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
437595d67482SBill Paul 			jumbocnt++;
4376943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
437795d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4378e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
437995d67482SBill Paul 				continue;
438095d67482SBill Paul 			}
4381943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
4382e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
4383df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
438495d67482SBill Paul 				continue;
438595d67482SBill Paul 			}
438603e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
438795d67482SBill Paul 		} else {
438895d67482SBill Paul 			stdcnt++;
4389e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
439095d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4391e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
439295d67482SBill Paul 				continue;
439395d67482SBill Paul 			}
4394943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
4395e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
4396df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
439795d67482SBill Paul 				continue;
439895d67482SBill Paul 			}
439903e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
440095d67482SBill Paul 		}
440195d67482SBill Paul 
4402df360178SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
4403e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
4404e255b776SJohn Polstra 		/*
4405e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
4406e65bed95SPyun YongHyeon 		 * the payload is aligned.
4407e255b776SJohn Polstra 		 */
4408652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
4409e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
4410e255b776SJohn Polstra 			    cur_rx->bge_len);
4411e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
4412e255b776SJohn Polstra 		}
4413e255b776SJohn Polstra #endif
4414473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
441595d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
441695d67482SBill Paul 
4417fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_RXCSUM)
44181108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
441995d67482SBill Paul 
442095d67482SBill Paul 		/*
4421673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
4422673d9191SSam Leffler 		 * attach that information to the packet.
442395d67482SBill Paul 		 */
4424d147662cSGleb Smirnoff 		if (have_tag) {
442578ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
442678ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
4427d147662cSGleb Smirnoff 		}
442895d67482SBill Paul 
4429dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
44300f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
4431fba8b109SMarcel Moolenaar 			if_input(ifp, m);
44320f9bd73bSSam Leffler 			BGE_LOCK(sc);
4433dfe0df9aSPyun YongHyeon 		} else
4434fba8b109SMarcel Moolenaar 			if_input(ifp, m);
4435d4da719cSAttilio Rao 		rx_npkts++;
443625e13e68SXin LI 
4437fba8b109SMarcel Moolenaar 		if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
44388cf7d13dSAttilio Rao 			return (rx_npkts);
443995d67482SBill Paul 	}
444095d67482SBill Paul 
444115eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
444215eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
4443e65bed95SPyun YongHyeon 	if (stdcnt > 0)
4444f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
4445e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
44464c0da0ffSGleb Smirnoff 
4447c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
4448f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
44494c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
4450f41ac2beSBill Paul 
44517f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
445238cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
445395d67482SBill Paul 	if (stdcnt)
4454767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
4455767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
445695d67482SBill Paul 	if (jumbocnt)
4457767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
4458767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
4459f5a034f9SPyun YongHyeon #ifdef notyet
4460f5a034f9SPyun YongHyeon 	/*
4461f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
4462f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
4463f5a034f9SPyun YongHyeon 	 */
4464f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
4465fba8b109SMarcel Moolenaar 		if_incierrors(ifp, CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS));
4466f5a034f9SPyun YongHyeon #endif
44671abcdbd1SAttilio Rao 	return (rx_npkts);
446895d67482SBill Paul }
446995d67482SBill Paul 
447095d67482SBill Paul static void
44711108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
44721108273aSPyun YongHyeon {
44731108273aSPyun YongHyeon 
44741108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
44751108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
44761108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44771108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44781108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
44791108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
44801108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44811108273aSPyun YongHyeon 			}
44821108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
44831108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
44841108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
44851108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44861108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
44871108273aSPyun YongHyeon 			}
44881108273aSPyun YongHyeon 		}
44891108273aSPyun YongHyeon 	} else {
44901108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44911108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44921108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
44931108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44941108273aSPyun YongHyeon 		}
44951108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
44961108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
44971108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
44981108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
44991108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
45001108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
45011108273aSPyun YongHyeon 		}
45021108273aSPyun YongHyeon 	}
45031108273aSPyun YongHyeon }
45041108273aSPyun YongHyeon 
45051108273aSPyun YongHyeon static void
4506b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
450795d67482SBill Paul {
450895a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
4509fba8b109SMarcel Moolenaar 	if_t ifp;
451095d67482SBill Paul 
45110f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
45120f9bd73bSSam Leffler 
45133f74909aSGleb Smirnoff 	/* Nothing to do. */
4514b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
4515cfcb5025SOleg Bulyzhin 		return;
4516cfcb5025SOleg Bulyzhin 
4517fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
451895d67482SBill Paul 
4519e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
45205c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
452195d67482SBill Paul 	/*
452295d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
452395d67482SBill Paul 	 * frames that have been sent.
452495d67482SBill Paul 	 */
4525b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
452695a0a340SPyun YongHyeon 		uint32_t		idx;
452795d67482SBill Paul 
452895d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
4529f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
453095d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
4531df360178SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
453295d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
45330ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
4534e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
4535e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
45360ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
4537f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
4538e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
4539e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
454095d67482SBill Paul 		}
454195d67482SBill Paul 		sc->bge_txcnt--;
454295d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
454395d67482SBill Paul 	}
454495d67482SBill Paul 
4545fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
45465b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
45475b01e77cSBruce Evans 		sc->bge_timer = 0;
454895d67482SBill Paul }
454995d67482SBill Paul 
455075719184SGleb Smirnoff #ifdef DEVICE_POLLING
45511abcdbd1SAttilio Rao static int
4552fba8b109SMarcel Moolenaar bge_poll(if_t ifp, enum poll_cmd cmd, int count)
455375719184SGleb Smirnoff {
4554fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
4555b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4556366454f2SOleg Bulyzhin 	uint32_t statusword;
45571abcdbd1SAttilio Rao 	int rx_npkts = 0;
455875719184SGleb Smirnoff 
45593f74909aSGleb Smirnoff 	BGE_LOCK(sc);
4560fba8b109SMarcel Moolenaar 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
45613f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
45621abcdbd1SAttilio Rao 		return (rx_npkts);
45633f74909aSGleb Smirnoff 	}
456475719184SGleb Smirnoff 
4565dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4566b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4567b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
45682246e8c6SPyun YongHyeon 	/* Fetch updates from the status block. */
4569b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4570b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4571dab5cd05SOleg Bulyzhin 
4572175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
45732246e8c6SPyun YongHyeon 	/* Clear the status so the next pass only sees the changes. */
4574175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4575dab5cd05SOleg Bulyzhin 
4576dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4577b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4578b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4579366454f2SOleg Bulyzhin 
45800c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
4581366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
4582366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
4583366454f2SOleg Bulyzhin 
4584366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
4585366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
45864c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4587652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
4588366454f2SOleg Bulyzhin 			bge_link_upd(sc);
4589366454f2SOleg Bulyzhin 
4590366454f2SOleg Bulyzhin 	sc->rxcycles = count;
4591dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
4592fba8b109SMarcel Moolenaar 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
459325e13e68SXin LI 		BGE_UNLOCK(sc);
45948cf7d13dSAttilio Rao 		return (rx_npkts);
459525e13e68SXin LI 	}
4596b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
4597fba8b109SMarcel Moolenaar 	if (!if_sendq_empty(ifp))
4598366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
45993f74909aSGleb Smirnoff 
46003f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
46011abcdbd1SAttilio Rao 	return (rx_npkts);
460275719184SGleb Smirnoff }
460375719184SGleb Smirnoff #endif /* DEVICE_POLLING */
460475719184SGleb Smirnoff 
4605dfe0df9aSPyun YongHyeon static int
4606dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
4607dfe0df9aSPyun YongHyeon {
4608dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4609dfe0df9aSPyun YongHyeon 
4610dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4611dfe0df9aSPyun YongHyeon 	/*
4612dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
4613dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
4614dfe0df9aSPyun YongHyeon 	 */
4615dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4616dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
4617dfe0df9aSPyun YongHyeon }
4618dfe0df9aSPyun YongHyeon 
4619dfe0df9aSPyun YongHyeon static void
4620dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
4621dfe0df9aSPyun YongHyeon {
4622dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4623fba8b109SMarcel Moolenaar 	if_t ifp;
46241108273aSPyun YongHyeon 	uint32_t status, status_tag;
4625dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4626dfe0df9aSPyun YongHyeon 
4627dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4628dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
4629dfe0df9aSPyun YongHyeon 
463066151edfSPyun YongHyeon 	BGE_LOCK(sc);
4631fba8b109SMarcel Moolenaar 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
463266151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4633dfe0df9aSPyun YongHyeon 		return;
463466151edfSPyun YongHyeon 	}
4635dfe0df9aSPyun YongHyeon 
4636dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
4637dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4638dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4639dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4640dfe0df9aSPyun YongHyeon 
46412246e8c6SPyun YongHyeon 	/* Save producer/consumer indices. */
4642dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4643dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4644dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
46451108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
46462246e8c6SPyun YongHyeon 	/* Dirty the status flag. */
4647dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4648dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4649dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4650dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
46511108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
46521108273aSPyun YongHyeon 		status_tag = 0;
465366151edfSPyun YongHyeon 
465466151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
465566151edfSPyun YongHyeon 		bge_link_upd(sc);
465666151edfSPyun YongHyeon 
4657dfe0df9aSPyun YongHyeon 	/* Let controller work. */
46581108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4659dfe0df9aSPyun YongHyeon 
4660fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING &&
466166151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
4662dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
466366151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4664dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
466566151edfSPyun YongHyeon 		BGE_LOCK(sc);
4666dfe0df9aSPyun YongHyeon 	}
4667fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4668dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
4669dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
4670fba8b109SMarcel Moolenaar 		if (!if_sendq_empty(ifp))
4671dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
4672dfe0df9aSPyun YongHyeon 	}
467366151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
4674dfe0df9aSPyun YongHyeon }
4675dfe0df9aSPyun YongHyeon 
467695d67482SBill Paul static void
46773f74909aSGleb Smirnoff bge_intr(void *xsc)
467895d67482SBill Paul {
467995d67482SBill Paul 	struct bge_softc *sc;
4680fba8b109SMarcel Moolenaar 	if_t ifp;
4681dab5cd05SOleg Bulyzhin 	uint32_t statusword;
4682b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
468395d67482SBill Paul 
468495d67482SBill Paul 	sc = xsc;
4685f41ac2beSBill Paul 
46860f9bd73bSSam Leffler 	BGE_LOCK(sc);
46870f9bd73bSSam Leffler 
4688dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
4689dab5cd05SOleg Bulyzhin 
469075719184SGleb Smirnoff #ifdef DEVICE_POLLING
4691fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
469275719184SGleb Smirnoff 		BGE_UNLOCK(sc);
469375719184SGleb Smirnoff 		return;
469475719184SGleb Smirnoff 	}
469575719184SGleb Smirnoff #endif
469675719184SGleb Smirnoff 
4697f30cbfc6SScott Long 	/*
4698b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
4699b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
4700b848e032SBruce Evans 	 * our current organization this just gives complications and
4701b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
4702b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
4703b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
4704b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
4705b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
4706b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
4707b848e032SBruce Evans 	 *
4708b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
4709b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
4710b848e032SBruce Evans 	 * changing later because it is more efficient to get another
4711b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
4712b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
4713b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
4714b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
4715b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
4716b848e032SBruce Evans 	 */
471738cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4718b848e032SBruce Evans 
4719f584dfd1SPyun YongHyeon 	/*
4720f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
4721f584dfd1SPyun YongHyeon 	 */
4722f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4723f584dfd1SPyun YongHyeon 
4724f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
4725f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4726f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4727f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4728f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4729f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4730f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4731f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4732f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4733f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4734f584dfd1SPyun YongHyeon 
47351f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
47364c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4737f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
4738dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
473995d67482SBill Paul 
4740fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
47413f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
4742dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
474325e13e68SXin LI 	}
474495d67482SBill Paul 
4745fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
47463f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
4747b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
474895d67482SBill Paul 	}
474995d67482SBill Paul 
4750fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING &&
4751fba8b109SMarcel Moolenaar 	    !if_sendq_empty(ifp))
47520f9bd73bSSam Leffler 		bge_start_locked(ifp);
47530f9bd73bSSam Leffler 
47540f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
475595d67482SBill Paul }
475695d67482SBill Paul 
475795d67482SBill Paul static void
47588cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
47598cb1383cSDoug Ambrisko {
47608cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
47618cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
47628cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
47638cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
47648cb1383cSDoug Ambrisko 		else {
4765899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
4766888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
47673c201200SPyun YongHyeon 			    BGE_FW_CMD_DRV_ALIVE);
4768888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4769941a6e13SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4770941a6e13SPyun YongHyeon 			    BGE_FW_HB_TIMEOUT_SEC);
47713fed2d5dSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
47729931ba85SPyun YongHyeon 			    CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
47739931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT);
47748cb1383cSDoug Ambrisko 		}
47758cb1383cSDoug Ambrisko 	}
47768cb1383cSDoug Ambrisko }
47778cb1383cSDoug Ambrisko 
47788cb1383cSDoug Ambrisko static void
4779b74e67fbSGleb Smirnoff bge_tick(void *xsc)
47800f9bd73bSSam Leffler {
4781b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
478295d67482SBill Paul 	struct mii_data *mii = NULL;
478395d67482SBill Paul 
47840f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
478595d67482SBill Paul 
47865dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
47875dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
47885dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
47895dda8085SOleg Bulyzhin 		return;
47905dda8085SOleg Bulyzhin 
47917ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
47920434d1b8SBill Paul 		bge_stats_update_regs(sc);
47930434d1b8SBill Paul 	else
479495d67482SBill Paul 		bge_stats_update(sc);
479595d67482SBill Paul 
4796548c8f1aSPyun YongHyeon 	/* XXX Add APE heartbeat check here? */
4797548c8f1aSPyun YongHyeon 
4798652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
479995d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
480082b67c01SOleg Bulyzhin 		/*
480182b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
480282b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
480382b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
480482b67c01SOleg Bulyzhin 		 */
480582b67c01SOleg Bulyzhin 		if (!sc->bge_link)
480695d67482SBill Paul 			mii_tick(mii);
48077b97099dSOleg Bulyzhin 	} else {
48087b97099dSOleg Bulyzhin 		/*
48097b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
48107b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
48117b97099dSOleg Bulyzhin 		 * and trigger interrupt.
48127b97099dSOleg Bulyzhin 		 */
48137b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
48143f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
481559578ee0SSergey Kandaurov 		if (!(if_getcapenable(sc->bge_ifp) & IFCAP_POLLING))
48167b97099dSOleg Bulyzhin #endif
48177b97099dSOleg Bulyzhin 		{
48187b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
48194f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
48204f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
48217b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
48224f0794ffSBjoern A. Zeeb 		else
48234f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
48247b97099dSOleg Bulyzhin 		}
4825dab5cd05SOleg Bulyzhin 	}
482695d67482SBill Paul 
48278cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4828b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
48298cb1383cSDoug Ambrisko 
4830dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
483195d67482SBill Paul }
483295d67482SBill Paul 
483395d67482SBill Paul static void
48343f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
48350434d1b8SBill Paul {
4836fba8b109SMarcel Moolenaar 	if_t ifp;
48372280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
483829b44b09SPyun YongHyeon 	uint32_t val;
48390434d1b8SBill Paul 
4840fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
48412280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
48420434d1b8SBill Paul 
48432280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
48442280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
48452280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
48462280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
48472280c16bSPyun YongHyeon 	stats->outXonSent +=
48482280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
48492280c16bSPyun YongHyeon 	stats->outXoffSent +=
48502280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
48512280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
48522280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
48532280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
48542280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
48552280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
48562280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
48572280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
48582280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
48592280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
48602280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
48612280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
48622280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
48632280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
48642280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
48652280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
48662280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
48672280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
48682280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
48697e6e2507SJung-uk Kim 
48702280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
48712280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
48722280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
48732280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
48742280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
48752280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
48762280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
48772280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
48782280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
48792280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
48802280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
48812280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
48822280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
48832280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
48842280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
48852280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
48862280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
48872280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
48882280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
48892280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
48902280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
48912280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
48922280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
48932280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
48942280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
48952280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
48962280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
48972280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
48982280c16bSPyun YongHyeon 
48992280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
49002280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
49012280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
49022280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
49032280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
49042280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
49052280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
49062280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4907f78094a5SPyun YongHyeon 	/*
4908f78094a5SPyun YongHyeon 	 * XXX
4909f78094a5SPyun YongHyeon 	 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4910f78094a5SPyun YongHyeon 	 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4911f78094a5SPyun YongHyeon 	 * includes number of unwanted multicast frames.  This comes
4912f78094a5SPyun YongHyeon 	 * from silicon bug and known workaround to get rough(not
4913f78094a5SPyun YongHyeon 	 * exact) counter is to enable interrupt on MBUF low water
4914f78094a5SPyun YongHyeon 	 * attention.  This can be accomplished by setting
4915f78094a5SPyun YongHyeon 	 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4916f78094a5SPyun YongHyeon 	 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4917f78094a5SPyun YongHyeon 	 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4918f78094a5SPyun YongHyeon 	 * However that change would generate more interrupts and
4919f78094a5SPyun YongHyeon 	 * there are still possibilities of losing multiple frames
4920f78094a5SPyun YongHyeon 	 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4921f78094a5SPyun YongHyeon 	 * Given that the workaround still would not get correct
4922f78094a5SPyun YongHyeon 	 * counter I don't think it's worth to implement it.  So
4923f78094a5SPyun YongHyeon 	 * ignore reading the counter on controllers that have the
4924f78094a5SPyun YongHyeon 	 * silicon bug.
4925f78094a5SPyun YongHyeon 	 */
4926f78094a5SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4927f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4928f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
49292280c16bSPyun YongHyeon 		stats->InputDiscards +=
49302280c16bSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
49312280c16bSPyun YongHyeon 	stats->InputErrors +=
49322280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
49332280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
49342280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
49352280c16bSPyun YongHyeon 
493629b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
493729b44b09SPyun YongHyeon 		/*
493829b44b09SPyun YongHyeon 		 * If controller transmitted more than BGE_NUM_RDMA_CHANNELS
493929b44b09SPyun YongHyeon 		 * frames, it's safe to disable workaround for DMA engine's
494029b44b09SPyun YongHyeon 		 * miscalculation of TXMBUF space.
494129b44b09SPyun YongHyeon 		 */
494229b44b09SPyun YongHyeon 		if (stats->ifHCOutUcastPkts + stats->ifHCOutMulticastPkts +
494329b44b09SPyun YongHyeon 		    stats->ifHCOutBroadcastPkts > BGE_NUM_RDMA_CHANNELS) {
494429b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
494529b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
494629b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5719;
494729b44b09SPyun YongHyeon 			else
494829b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5720;
494929b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
495029b44b09SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_RDMA_BUG;
495129b44b09SPyun YongHyeon 		}
495229b44b09SPyun YongHyeon 	}
49532280c16bSPyun YongHyeon }
49542280c16bSPyun YongHyeon 
49552280c16bSPyun YongHyeon static void
49562280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
49572280c16bSPyun YongHyeon {
49582280c16bSPyun YongHyeon 
49592280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
49602280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
49612280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
49622280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
49632280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
49642280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
49652280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
49662280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
49672280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
49682280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
49692280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
49702280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
49712280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
49722280c16bSPyun YongHyeon 
49732280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
49742280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
49752280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
49762280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
49772280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
49782280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
49792280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
49802280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
49812280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
49822280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
49832280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
49842280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
49852280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
49862280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
49872280c16bSPyun YongHyeon 
49882280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
49892280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
49902280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
49912280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
49922280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
49932280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
49942280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
49950434d1b8SBill Paul }
49960434d1b8SBill Paul 
49970434d1b8SBill Paul static void
49983f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
499995d67482SBill Paul {
5000fba8b109SMarcel Moolenaar 	if_t ifp;
5001e907febfSPyun YongHyeon 	bus_size_t stats;
50027e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
500395d67482SBill Paul 
5004fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
500595d67482SBill Paul 
5006e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
5007e907febfSPyun YongHyeon 
5008e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
5009e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
501095d67482SBill Paul 
50118634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
5012df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cnt - sc->bge_tx_collisions);
50136fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
50146fb34dd2SOleg Bulyzhin 
501537ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, nicNoMoreRxBDs.bge_addr_lo);
5016df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_nobds);
501737ee7cc7SPyun YongHyeon 	sc->bge_rx_nobds = cnt;
501837ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, ifInErrors.bge_addr_lo);
5019df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_inerrs);
502037ee7cc7SPyun YongHyeon 	sc->bge_rx_inerrs = cnt;
50216fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
5022df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_discards);
50236fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
50246fb34dd2SOleg Bulyzhin 
50256fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
5026df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, cnt - sc->bge_tx_discards);
50276fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
502895d67482SBill Paul 
5029e907febfSPyun YongHyeon #undef	READ_STAT
503095d67482SBill Paul }
503195d67482SBill Paul 
503295d67482SBill Paul /*
5033d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
5034d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
5035d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
5036d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
5037d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
5038d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
5039d375e524SGleb Smirnoff  */
5040d375e524SGleb Smirnoff static __inline int
5041d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
5042d375e524SGleb Smirnoff {
5043d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
5044d375e524SGleb Smirnoff 	struct mbuf *last;
5045d375e524SGleb Smirnoff 
5046d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
5047d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
5048d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
5049d375e524SGleb Smirnoff 		last = m;
5050d375e524SGleb Smirnoff 	} else {
5051d375e524SGleb Smirnoff 		/*
5052d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
5053d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
5054d375e524SGleb Smirnoff 		 */
5055d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
5056d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
5057d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
5058d375e524SGleb Smirnoff 			struct mbuf *n;
5059d375e524SGleb Smirnoff 
5060c6499eccSGleb Smirnoff 			MGET(n, M_NOWAIT, MT_DATA);
5061d375e524SGleb Smirnoff 			if (n == NULL)
5062d375e524SGleb Smirnoff 				return (ENOBUFS);
5063d375e524SGleb Smirnoff 			n->m_len = 0;
5064d375e524SGleb Smirnoff 			last->m_next = n;
5065d375e524SGleb Smirnoff 			last = n;
5066d375e524SGleb Smirnoff 		}
5067d375e524SGleb Smirnoff 	}
5068d375e524SGleb Smirnoff 
5069d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
5070d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
5071d375e524SGleb Smirnoff 	last->m_len += padlen;
5072d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
5073d375e524SGleb Smirnoff 
5074d375e524SGleb Smirnoff 	return (0);
5075d375e524SGleb Smirnoff }
5076d375e524SGleb Smirnoff 
5077ca3f1187SPyun YongHyeon static struct mbuf *
5078d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
5079d598b626SPyun YongHyeon {
5080d598b626SPyun YongHyeon 	struct mbuf *n;
5081d598b626SPyun YongHyeon 	int found;
5082d598b626SPyun YongHyeon 
5083d598b626SPyun YongHyeon 	/*
5084d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
5085d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
5086d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
5087d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
5088d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
5089d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
5090d598b626SPyun YongHyeon 	 */
5091d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
5092d598b626SPyun YongHyeon 		if (n->m_len < 8) {
5093d598b626SPyun YongHyeon 			found++;
5094d598b626SPyun YongHyeon 			if (found > 1)
5095d598b626SPyun YongHyeon 				break;
5096d598b626SPyun YongHyeon 			continue;
5097d598b626SPyun YongHyeon 		}
5098d598b626SPyun YongHyeon 		found = 0;
5099d598b626SPyun YongHyeon 	}
5100d598b626SPyun YongHyeon 
5101d598b626SPyun YongHyeon 	if (found > 1) {
5102c6499eccSGleb Smirnoff 		n = m_defrag(m, M_NOWAIT);
5103d598b626SPyun YongHyeon 		if (n == NULL)
5104d598b626SPyun YongHyeon 			m_freem(m);
5105d598b626SPyun YongHyeon 	} else
5106d598b626SPyun YongHyeon 		n = m;
5107d598b626SPyun YongHyeon 	return (n);
5108d598b626SPyun YongHyeon }
5109d598b626SPyun YongHyeon 
5110d598b626SPyun YongHyeon static struct mbuf *
51111108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
51121108273aSPyun YongHyeon     uint16_t *flags)
5113ca3f1187SPyun YongHyeon {
5114ca3f1187SPyun YongHyeon 	struct ip *ip;
5115ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
5116ca3f1187SPyun YongHyeon 	struct mbuf *n;
5117ca3f1187SPyun YongHyeon 	uint16_t hlen;
51185b355c4fSPyun YongHyeon 	uint32_t poff;
5119ca3f1187SPyun YongHyeon 
5120ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
5121ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
5122c6499eccSGleb Smirnoff 		n = m_dup(m, M_NOWAIT);
5123ca3f1187SPyun YongHyeon 		m_freem(m);
5124ca3f1187SPyun YongHyeon 		if (n == NULL)
5125ca3f1187SPyun YongHyeon 			return (NULL);
5126ca3f1187SPyun YongHyeon 		m = n;
5127ca3f1187SPyun YongHyeon 	}
51285b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
5129ca3f1187SPyun YongHyeon 	if (m == NULL)
5130ca3f1187SPyun YongHyeon 		return (NULL);
51315b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
51325b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
5133ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
5134ca3f1187SPyun YongHyeon 	if (m == NULL)
5135ca3f1187SPyun YongHyeon 		return (NULL);
5136ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
51375b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
5138ca3f1187SPyun YongHyeon 	if (m == NULL)
5139ca3f1187SPyun YongHyeon 		return (NULL);
5140ca3f1187SPyun YongHyeon 	/*
5141ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
5142ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
5143ca3f1187SPyun YongHyeon 	 */
5144ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
514596486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
5146ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
5147ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
5148ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
514996486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
5150ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
5151ca3f1187SPyun YongHyeon 	/*
5152ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
5153ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
5154ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
51551108273aSPyun YongHyeon 	 * we only support hardware based TSO.
5156ca3f1187SPyun YongHyeon 	 */
51571108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
5158ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
51591108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
51601108273aSPyun YongHyeon 		/*
51611108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
51621108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
51631108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
51641108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
51651108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
51661108273aSPyun YongHyeon 		 * frames are supported.
51671108273aSPyun YongHyeon 		 */
51681108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
51691108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
51701108273aSPyun YongHyeon 	} else {
51711108273aSPyun YongHyeon 		/*
51721108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
51731108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
51741108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
51751108273aSPyun YongHyeon 		 * supported.
51761108273aSPyun YongHyeon 		 */
5177ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
51781108273aSPyun YongHyeon 	}
5179ca3f1187SPyun YongHyeon 	return (m);
5180ca3f1187SPyun YongHyeon }
5181ca3f1187SPyun YongHyeon 
5182d375e524SGleb Smirnoff /*
518395d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
518495d67482SBill Paul  * pointers to descriptors.
518595d67482SBill Paul  */
518695d67482SBill Paul static int
5187676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
518895d67482SBill Paul {
51897e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
5190f41ac2beSBill Paul 	bus_dmamap_t		map;
5191676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
5192676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
51937e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
5194ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
51957e27542aSGleb Smirnoff 	int			nsegs, i, error;
519695d67482SBill Paul 
51976909dc43SGleb Smirnoff 	csum_flags = 0;
5198ca3f1187SPyun YongHyeon 	mss = 0;
5199ca3f1187SPyun YongHyeon 	vlan_tag = 0;
5200d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
5201d598b626SPyun YongHyeon 	    m->m_next != NULL) {
5202d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
5203d598b626SPyun YongHyeon 		if (*m_head == NULL)
5204d598b626SPyun YongHyeon 			return (ENOBUFS);
5205d598b626SPyun YongHyeon 		m = *m_head;
5206d598b626SPyun YongHyeon 	}
5207ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
52081108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
5209ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
5210ca3f1187SPyun YongHyeon 			return (ENOBUFS);
5211ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
5212ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
521335f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
52146909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
52156909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
52166909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
52176909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
52186909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
52196909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
52206909dc43SGleb Smirnoff 				m_freem(m);
52216909dc43SGleb Smirnoff 				*m_head = NULL;
52226909dc43SGleb Smirnoff 				return (error);
52236909dc43SGleb Smirnoff 			}
52246909dc43SGleb Smirnoff 		}
52256909dc43SGleb Smirnoff 	}
52266909dc43SGleb Smirnoff 
52271108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
52281108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
52291108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
52301108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
52311108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
5232beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
5233d94f2b85SPyun YongHyeon 			/*
5234d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
5235d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
5236d94f2b85SPyun YongHyeon 			 * DMA read operation.
5237d94f2b85SPyun YongHyeon 			 */
5238beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
5239c6499eccSGleb Smirnoff 				m = m_defrag(m, M_NOWAIT);
5240d94f2b85SPyun YongHyeon 			else
5241c6499eccSGleb Smirnoff 				m = m_collapse(m, M_NOWAIT,
52421108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
5243261f04d6SPyun YongHyeon 			if (m == NULL)
5244261f04d6SPyun YongHyeon 				m = *m_head;
5245d94f2b85SPyun YongHyeon 			*m_head = m;
5246d94f2b85SPyun YongHyeon 		}
52471108273aSPyun YongHyeon 	}
5248d94f2b85SPyun YongHyeon 
52497e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
52500ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
5251676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
52527e27542aSGleb Smirnoff 	if (error == EFBIG) {
5253c6499eccSGleb Smirnoff 		m = m_collapse(m, M_NOWAIT, BGE_NSEG_NEW);
5254676ad2c9SGleb Smirnoff 		if (m == NULL) {
5255676ad2c9SGleb Smirnoff 			m_freem(*m_head);
5256676ad2c9SGleb Smirnoff 			*m_head = NULL;
52577e27542aSGleb Smirnoff 			return (ENOBUFS);
52587e27542aSGleb Smirnoff 		}
5259676ad2c9SGleb Smirnoff 		*m_head = m;
52600ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
52610ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
5262676ad2c9SGleb Smirnoff 		if (error) {
5263676ad2c9SGleb Smirnoff 			m_freem(m);
5264676ad2c9SGleb Smirnoff 			*m_head = NULL;
52657e27542aSGleb Smirnoff 			return (error);
52667e27542aSGleb Smirnoff 		}
5267676ad2c9SGleb Smirnoff 	} else if (error != 0)
5268676ad2c9SGleb Smirnoff 		return (error);
52697e27542aSGleb Smirnoff 
5270167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
5271167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
52720ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
527395d67482SBill Paul 		return (ENOBUFS);
52747e27542aSGleb Smirnoff 	}
52757e27542aSGleb Smirnoff 
52760ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
5277e65bed95SPyun YongHyeon 
5278ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
5279ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
5280ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
5281ca3f1187SPyun YongHyeon 	}
5282b77d3a3bSPyun YongHyeon 
5283b77d3a3bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5762 &&
5284b77d3a3bSPyun YongHyeon 	    (m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
5285b77d3a3bSPyun YongHyeon 		/*
5286b77d3a3bSPyun YongHyeon 		 * 5725 family of devices corrupts TSO packets when TSO DMA
5287b77d3a3bSPyun YongHyeon 		 * buffers cross into regions which are within MSS bytes of
5288b77d3a3bSPyun YongHyeon 		 * a 4GB boundary.  If we encounter the condition, drop the
5289b77d3a3bSPyun YongHyeon 		 * packet.
5290b77d3a3bSPyun YongHyeon 		 */
5291b77d3a3bSPyun YongHyeon 		for (i = 0; ; i++) {
5292b77d3a3bSPyun YongHyeon 			d = &sc->bge_ldata.bge_tx_ring[idx];
5293b77d3a3bSPyun YongHyeon 			d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
5294b77d3a3bSPyun YongHyeon 			d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
5295b77d3a3bSPyun YongHyeon 			d->bge_len = segs[i].ds_len;
5296b77d3a3bSPyun YongHyeon 			if (d->bge_addr.bge_addr_lo + segs[i].ds_len + mss <
5297b77d3a3bSPyun YongHyeon 			    d->bge_addr.bge_addr_lo)
5298b77d3a3bSPyun YongHyeon 				break;
5299b77d3a3bSPyun YongHyeon 			d->bge_flags = csum_flags;
5300b77d3a3bSPyun YongHyeon 			d->bge_vlan_tag = vlan_tag;
5301b77d3a3bSPyun YongHyeon 			d->bge_mss = mss;
5302b77d3a3bSPyun YongHyeon 			if (i == nsegs - 1)
5303b77d3a3bSPyun YongHyeon 				break;
5304b77d3a3bSPyun YongHyeon 			BGE_INC(idx, BGE_TX_RING_CNT);
5305b77d3a3bSPyun YongHyeon 		}
5306b77d3a3bSPyun YongHyeon 		if (i != nsegs - 1) {
5307b77d3a3bSPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map,
5308b77d3a3bSPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
5309b77d3a3bSPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
5310b77d3a3bSPyun YongHyeon 			m_freem(*m_head);
5311b77d3a3bSPyun YongHyeon 			*m_head = NULL;
5312b77d3a3bSPyun YongHyeon 			return (EIO);
5313b77d3a3bSPyun YongHyeon 		}
5314b77d3a3bSPyun YongHyeon 	} else {
53157e27542aSGleb Smirnoff 		for (i = 0; ; i++) {
53167e27542aSGleb Smirnoff 			d = &sc->bge_ldata.bge_tx_ring[idx];
53177e27542aSGleb Smirnoff 			d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
53187e27542aSGleb Smirnoff 			d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
53197e27542aSGleb Smirnoff 			d->bge_len = segs[i].ds_len;
53207e27542aSGleb Smirnoff 			d->bge_flags = csum_flags;
5321ca3f1187SPyun YongHyeon 			d->bge_vlan_tag = vlan_tag;
5322ca3f1187SPyun YongHyeon 			d->bge_mss = mss;
53237e27542aSGleb Smirnoff 			if (i == nsegs - 1)
53247e27542aSGleb Smirnoff 				break;
53257e27542aSGleb Smirnoff 			BGE_INC(idx, BGE_TX_RING_CNT);
53267e27542aSGleb Smirnoff 		}
5327b77d3a3bSPyun YongHyeon 	}
53287e27542aSGleb Smirnoff 
53297e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
53307e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
5331676ad2c9SGleb Smirnoff 
5332f41ac2beSBill Paul 	/*
5333f41ac2beSBill Paul 	 * Insure that the map for this transmission
5334f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
5335f41ac2beSBill Paul 	 * in this chain.
5336f41ac2beSBill Paul 	 */
53377e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
53387e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
5339676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
53407e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
534195d67482SBill Paul 
53427e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
53437e27542aSGleb Smirnoff 	*txidx = idx;
534495d67482SBill Paul 
534595d67482SBill Paul 	return (0);
534695d67482SBill Paul }
534795d67482SBill Paul 
534895d67482SBill Paul /*
534995d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
535095d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
535195d67482SBill Paul  */
535295d67482SBill Paul static void
5353fba8b109SMarcel Moolenaar bge_start_locked(if_t ifp)
535495d67482SBill Paul {
535595d67482SBill Paul 	struct bge_softc *sc;
5356167fdb62SPyun YongHyeon 	struct mbuf *m_head;
535714bbd30fSGleb Smirnoff 	uint32_t prodidx;
5358167fdb62SPyun YongHyeon 	int count;
535995d67482SBill Paul 
5360fba8b109SMarcel Moolenaar 	sc = if_getsoftc(ifp);
5361167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
536295d67482SBill Paul 
5363167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
5364fba8b109SMarcel Moolenaar 	    (if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
5365167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
536695d67482SBill Paul 		return;
536795d67482SBill Paul 
536814bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
536995d67482SBill Paul 
5370fba8b109SMarcel Moolenaar 	for (count = 0; !if_sendq_empty(ifp);) {
5371167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
5372fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5373167fdb62SPyun YongHyeon 			break;
5374167fdb62SPyun YongHyeon 		}
5375fba8b109SMarcel Moolenaar 		m_head = if_dequeue(ifp);
537695d67482SBill Paul 		if (m_head == NULL)
537795d67482SBill Paul 			break;
537895d67482SBill Paul 
537995d67482SBill Paul 		/*
538095d67482SBill Paul 		 * Pack the data into the transmit ring. If we
538195d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
538295d67482SBill Paul 		 * for the NIC to drain the ring.
538395d67482SBill Paul 		 */
5384676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
5385676ad2c9SGleb Smirnoff 			if (m_head == NULL)
5386676ad2c9SGleb Smirnoff 				break;
5387fba8b109SMarcel Moolenaar 			if_sendq_prepend(ifp, m_head);
5388fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
538995d67482SBill Paul 			break;
539095d67482SBill Paul 		}
5391303a718cSDag-Erling Smørgrav 		++count;
539295d67482SBill Paul 
539395d67482SBill Paul 		/*
539495d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
539595d67482SBill Paul 		 * to him.
539695d67482SBill Paul 		 */
5397fba8b109SMarcel Moolenaar 		if_bpfmtap(ifp, m_head);
539895d67482SBill Paul 	}
539995d67482SBill Paul 
5400*ded66962SMark Johnston 	if (count > 0)
5401*ded66962SMark Johnston 		bge_start_tx(sc, prodidx);
5402*ded66962SMark Johnston }
5403*ded66962SMark Johnston 
5404*ded66962SMark Johnston static void
5405*ded66962SMark Johnston bge_start_tx(struct bge_softc *sc, uint32_t prodidx)
5406*ded66962SMark Johnston {
5407*ded66962SMark Johnston 
5408aa94f333SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
54095c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
54103f74909aSGleb Smirnoff 	/* Transmit. */
541138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
54123927098fSPaul Saab 	/* 5700 b2 errata */
5413e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
541438cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
541595d67482SBill Paul 
541614bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
541714bbd30fSGleb Smirnoff 
5418*ded66962SMark Johnston 	/* Set a timeout in case the chip goes out to lunch. */
5419b584d2b3SPyun YongHyeon 	sc->bge_timer = BGE_TX_TIMEOUT;
542095d67482SBill Paul }
542195d67482SBill Paul 
54220f9bd73bSSam Leffler /*
54230f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
54240f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
54250f9bd73bSSam Leffler  */
542695d67482SBill Paul static void
5427fba8b109SMarcel Moolenaar bge_start(if_t ifp)
542895d67482SBill Paul {
54290f9bd73bSSam Leffler 	struct bge_softc *sc;
54300f9bd73bSSam Leffler 
5431fba8b109SMarcel Moolenaar 	sc = if_getsoftc(ifp);
54320f9bd73bSSam Leffler 	BGE_LOCK(sc);
54330f9bd73bSSam Leffler 	bge_start_locked(ifp);
54340f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
54350f9bd73bSSam Leffler }
54360f9bd73bSSam Leffler 
54370f9bd73bSSam Leffler static void
54383f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
54390f9bd73bSSam Leffler {
5440fba8b109SMarcel Moolenaar 	if_t ifp;
54413f74909aSGleb Smirnoff 	uint16_t *m;
5442f6a65488SPyun YongHyeon 	uint32_t mode;
544395d67482SBill Paul 
54440f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
544595d67482SBill Paul 
5446fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
544795d67482SBill Paul 
5448fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
544995d67482SBill Paul 		return;
545095d67482SBill Paul 
545195d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
545295d67482SBill Paul 	bge_stop(sc);
54538cb1383cSDoug Ambrisko 
54548cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
54558cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
545695d67482SBill Paul 	bge_reset(sc);
54578cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
54588cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
54598cb1383cSDoug Ambrisko 
546095d67482SBill Paul 	bge_chipinit(sc);
546195d67482SBill Paul 
546295d67482SBill Paul 	/*
546395d67482SBill Paul 	 * Init the various state machines, ring
546495d67482SBill Paul 	 * control blocks and firmware.
546595d67482SBill Paul 	 */
546695d67482SBill Paul 	if (bge_blockinit(sc)) {
5467fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
546895d67482SBill Paul 		return;
546995d67482SBill Paul 	}
547095d67482SBill Paul 
5471fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
547295d67482SBill Paul 
547395d67482SBill Paul 	/* Specify MTU. */
5474fba8b109SMarcel Moolenaar 	CSR_WRITE_4(sc, BGE_RX_MTU, if_getmtu(ifp) +
5475cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
5476fba8b109SMarcel Moolenaar 	    (if_getcapenable(ifp) & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
547795d67482SBill Paul 
547895d67482SBill Paul 	/* Load our MAC address. */
54793f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
548095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
548195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
548295d67482SBill Paul 
54833e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
54843e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
548595d67482SBill Paul 
548695d67482SBill Paul 	/* Program multicast filter. */
548795d67482SBill Paul 	bge_setmulti(sc);
548895d67482SBill Paul 
5489cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
5490cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
5491cb2eacc7SYaroslav Tykhiy 
549235f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
549335f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
549435f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
549535f945cdSPyun YongHyeon 	else
549635f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
5497fba8b109SMarcel Moolenaar 	if (if_getcapabilities(ifp) & IFCAP_TXCSUM &&
5498fba8b109SMarcel Moolenaar 	    if_getcapenable(ifp) & IFCAP_TXCSUM) {
5499fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, 0, (BGE_CSUM_FEATURES | CSUM_UDP));
5500fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, sc->bge_csum_features, 0);
550135f945cdSPyun YongHyeon 	}
550235f945cdSPyun YongHyeon 
550395d67482SBill Paul 	/* Init RX ring. */
55043ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
55053ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
55063ee5d7daSPyun YongHyeon 		bge_stop(sc);
55073ee5d7daSPyun YongHyeon 		return;
55083ee5d7daSPyun YongHyeon 	}
550995d67482SBill Paul 
55100434d1b8SBill Paul 	/*
55110434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
55120434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
55130434d1b8SBill Paul 	 * entry of the ring.
55140434d1b8SBill Paul 	 */
55150434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
55163f74909aSGleb Smirnoff 		uint32_t		v, i;
55170434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
55180434d1b8SBill Paul 			DELAY(20);
55190434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
55200434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
55210434d1b8SBill Paul 				break;
55220434d1b8SBill Paul 		}
55230434d1b8SBill Paul 		if (i == 10)
5524fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
5525fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
55260434d1b8SBill Paul 	}
55270434d1b8SBill Paul 
552895d67482SBill Paul 	/* Init jumbo RX ring. */
5529f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
5530fba8b109SMarcel Moolenaar 	    if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
5531fba8b109SMarcel Moolenaar      	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN)) {
55323ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
5533333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
5534b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
55353ee5d7daSPyun YongHyeon 			bge_stop(sc);
55363ee5d7daSPyun YongHyeon 			return;
55373ee5d7daSPyun YongHyeon 		}
55383ee5d7daSPyun YongHyeon 	}
553995d67482SBill Paul 
55403f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
554195d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
554295d67482SBill Paul 
55437e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
55447e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
55457e6e2507SJung-uk Kim 
554695d67482SBill Paul 	/* Init TX ring. */
554795d67482SBill Paul 	bge_init_tx_ring(sc);
554895d67482SBill Paul 
5549f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
5550f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
5551f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
5552f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
55532927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
55542927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
555550515680SPyun YongHyeon 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
555650515680SPyun YongHyeon 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
555750515680SPyun YongHyeon 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
555850515680SPyun YongHyeon 	}
55593f74909aSGleb Smirnoff 	/* Turn on transmitter. */
5560f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
5561a6e66cd2SPyun YongHyeon 	DELAY(100);
556295d67482SBill Paul 
55633f74909aSGleb Smirnoff 	/* Turn on receiver. */
5564548c8f1aSPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_RX_MODE);
5565548c8f1aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc))
5566548c8f1aSPyun YongHyeon 		mode |= BGE_RXMODE_IPV6_ENABLE;
556769b1f509SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
556869b1f509SPyun YongHyeon 		mode |= BGE_RXMODE_IPV4_FRAG_FIX;
5569548c8f1aSPyun YongHyeon 	CSR_WRITE_4(sc,BGE_RX_MODE, mode | BGE_RXMODE_ENABLE);
5570a6e66cd2SPyun YongHyeon 	DELAY(10);
557195d67482SBill Paul 
5572dedcdf57SPyun YongHyeon 	/*
5573dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
5574dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
5575dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
5576dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
5577dedcdf57SPyun YongHyeon 	 */
55783fc5fbfbSPyun YongHyeon 	if (BGE_IS_57765_PLUS(sc))
5579b4a256acSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
5580b4a256acSPyun YongHyeon 	else
5581dedcdf57SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
5582dedcdf57SPyun YongHyeon 
55832280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
55842280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
55852280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
55862280c16bSPyun YongHyeon 
558795d67482SBill Paul 	/* Tell firmware we're alive. */
558895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
558995d67482SBill Paul 
559075719184SGleb Smirnoff #ifdef DEVICE_POLLING
559175719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
5592fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
559375719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
559475719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
559538cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
559675719184SGleb Smirnoff 	} else
559775719184SGleb Smirnoff #endif
559875719184SGleb Smirnoff 
559995d67482SBill Paul 	/* Enable host interrupts. */
560075719184SGleb Smirnoff 	{
560195d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
560295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
560338cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
560475719184SGleb Smirnoff 	}
560595d67482SBill Paul 
5606fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
5607fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
560895d67482SBill Paul 
5609e4146b95SPyun YongHyeon 	bge_ifmedia_upd_locked(ifp);
5610e4146b95SPyun YongHyeon 
56110f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
56120f9bd73bSSam Leffler }
56130f9bd73bSSam Leffler 
56140f9bd73bSSam Leffler static void
56153f74909aSGleb Smirnoff bge_init(void *xsc)
56160f9bd73bSSam Leffler {
56170f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
56180f9bd73bSSam Leffler 
56190f9bd73bSSam Leffler 	BGE_LOCK(sc);
56200f9bd73bSSam Leffler 	bge_init_locked(sc);
56210f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
562295d67482SBill Paul }
562395d67482SBill Paul 
562495d67482SBill Paul /*
562595d67482SBill Paul  * Set media options.
562695d67482SBill Paul  */
562795d67482SBill Paul static int
5628fba8b109SMarcel Moolenaar bge_ifmedia_upd(if_t ifp)
562995d67482SBill Paul {
5630fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
563167d5e043SOleg Bulyzhin 	int res;
563267d5e043SOleg Bulyzhin 
563367d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
563467d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
563567d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
563667d5e043SOleg Bulyzhin 
563767d5e043SOleg Bulyzhin 	return (res);
563867d5e043SOleg Bulyzhin }
563967d5e043SOleg Bulyzhin 
564067d5e043SOleg Bulyzhin static int
5641fba8b109SMarcel Moolenaar bge_ifmedia_upd_locked(if_t ifp)
564267d5e043SOleg Bulyzhin {
5643fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
564495d67482SBill Paul 	struct mii_data *mii;
56454f09c4c7SMarius Strobl 	struct mii_softc *miisc;
564695d67482SBill Paul 	struct ifmedia *ifm;
564795d67482SBill Paul 
564867d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
564967d5e043SOleg Bulyzhin 
565095d67482SBill Paul 	ifm = &sc->bge_ifmedia;
565195d67482SBill Paul 
565295d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
5653652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
565495d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
565595d67482SBill Paul 			return (EINVAL);
565695d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
565795d67482SBill Paul 		case IFM_AUTO:
5658ff50922bSDoug White 			/*
5659ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
5660ff50922bSDoug White 			 * mechanism for programming the autoneg
5661ff50922bSDoug White 			 * advertisement registers in TBI mode.
5662ff50922bSDoug White 			 */
56630f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5664ff50922bSDoug White 				uint32_t sgdig;
56650f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
56660f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
5667ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5668ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5669ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
5670ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
5671ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
5672ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5673ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
5674ff50922bSDoug White 					DELAY(5);
5675ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5676ff50922bSDoug White 				}
56770f89fde2SJung-uk Kim 			}
567895d67482SBill Paul 			break;
567995d67482SBill Paul 		case IFM_1000_SX:
568095d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
568195d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
568295d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
568395d67482SBill Paul 			} else {
568495d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
568595d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
568695d67482SBill Paul 			}
56879b80ffe7SPyun YongHyeon 			DELAY(40);
568895d67482SBill Paul 			break;
568995d67482SBill Paul 		default:
569095d67482SBill Paul 			return (EINVAL);
569195d67482SBill Paul 		}
569295d67482SBill Paul 		return (0);
569395d67482SBill Paul 	}
569495d67482SBill Paul 
56951493e883SOleg Bulyzhin 	sc->bge_link_evt++;
569695d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
56974f09c4c7SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
56983fcb7a53SMarius Strobl 		PHY_RESET(miisc);
569995d67482SBill Paul 	mii_mediachg(mii);
570095d67482SBill Paul 
5701902827f6SBjoern A. Zeeb 	/*
5702902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
5703902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
5704902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
5705902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
5706902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
5707902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
5708902827f6SBjoern A. Zeeb 	 * get an RX intr.
5709902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
5710902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
5711902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
5712902827f6SBjoern A. Zeeb 	 */
57134f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
57144f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
5715902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
57164f0794ffSBjoern A. Zeeb 	else
571763ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5718902827f6SBjoern A. Zeeb 
571995d67482SBill Paul 	return (0);
572095d67482SBill Paul }
572195d67482SBill Paul 
572295d67482SBill Paul /*
572395d67482SBill Paul  * Report current media status.
572495d67482SBill Paul  */
572595d67482SBill Paul static void
5726fba8b109SMarcel Moolenaar bge_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
572795d67482SBill Paul {
5728fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
572995d67482SBill Paul 	struct mii_data *mii;
573095d67482SBill Paul 
573167d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
573295d67482SBill Paul 
5733fba8b109SMarcel Moolenaar 	if ((if_getflags(ifp) & IFF_UP) == 0) {
5734b9d2edd7SPyun YongHyeon 		BGE_UNLOCK(sc);
5735b9d2edd7SPyun YongHyeon 		return;
5736b9d2edd7SPyun YongHyeon 	}
5737652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
573895d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
573995d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
574095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
574195d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
574295d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
57434c0da0ffSGleb Smirnoff 		else {
57444c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
574567d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
57464c0da0ffSGleb Smirnoff 			return;
57474c0da0ffSGleb Smirnoff 		}
574895d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
574995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
575095d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
575195d67482SBill Paul 		else
575295d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
575367d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
575495d67482SBill Paul 		return;
575595d67482SBill Paul 	}
575695d67482SBill Paul 
575795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
575895d67482SBill Paul 	mii_pollstat(mii);
575995d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
576095d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
576167d5e043SOleg Bulyzhin 
576267d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
576395d67482SBill Paul }
576495d67482SBill Paul 
576595d67482SBill Paul static int
5766fba8b109SMarcel Moolenaar bge_ioctl(if_t ifp, u_long command, caddr_t data)
576795d67482SBill Paul {
5768fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
576995d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
577095d67482SBill Paul 	struct mii_data *mii;
5771f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
577295d67482SBill Paul 
577395d67482SBill Paul 	switch (command) {
577495d67482SBill Paul 	case SIOCSIFMTU:
5775f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
5776f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
57774c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
5778f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
577995d67482SBill Paul 				error = EINVAL;
5780f5459d4cSPyun YongHyeon 				break;
5781f5459d4cSPyun YongHyeon 			}
5782f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5783f5459d4cSPyun YongHyeon 			error = EINVAL;
5784f5459d4cSPyun YongHyeon 			break;
5785f5459d4cSPyun YongHyeon 		}
5786f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
5787fba8b109SMarcel Moolenaar 		if (if_getmtu(ifp) != ifr->ifr_mtu) {
5788fba8b109SMarcel Moolenaar 			if_setmtu(ifp, ifr->ifr_mtu);
5789fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
5790fba8b109SMarcel Moolenaar 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
57913a429c8fSPyun YongHyeon 				bge_init_locked(sc);
579295d67482SBill Paul 			}
57933a429c8fSPyun YongHyeon 		}
57943a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
579595d67482SBill Paul 		break;
579695d67482SBill Paul 	case SIOCSIFFLAGS:
57970f9bd73bSSam Leffler 		BGE_LOCK(sc);
5798fba8b109SMarcel Moolenaar 		if (if_getflags(ifp) & IFF_UP) {
579995d67482SBill Paul 			/*
580095d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
580195d67482SBill Paul 			 * then just use the 'set promisc mode' command
580295d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
580395d67482SBill Paul 			 * a full re-init means reloading the firmware and
580495d67482SBill Paul 			 * waiting for it to start up, which may take a
5805d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
580695d67482SBill Paul 			 */
5807fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
5808fba8b109SMarcel Moolenaar 				flags = if_getflags(ifp) ^ sc->bge_if_flags;
58093e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
58103e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
5811f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
5812d183af7fSRuslan Ermilov 					bge_setmulti(sc);
581395d67482SBill Paul 			} else
58140f9bd73bSSam Leffler 				bge_init_locked(sc);
581595d67482SBill Paul 		} else {
5816fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
581795d67482SBill Paul 				bge_stop(sc);
581895d67482SBill Paul 			}
581995d67482SBill Paul 		}
5820fba8b109SMarcel Moolenaar 		sc->bge_if_flags = if_getflags(ifp);
58210f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
582295d67482SBill Paul 		error = 0;
582395d67482SBill Paul 		break;
582495d67482SBill Paul 	case SIOCADDMULTI:
582595d67482SBill Paul 	case SIOCDELMULTI:
5826fba8b109SMarcel Moolenaar 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
58270f9bd73bSSam Leffler 			BGE_LOCK(sc);
582895d67482SBill Paul 			bge_setmulti(sc);
58290f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
583095d67482SBill Paul 			error = 0;
583195d67482SBill Paul 		}
583295d67482SBill Paul 		break;
583395d67482SBill Paul 	case SIOCSIFMEDIA:
583495d67482SBill Paul 	case SIOCGIFMEDIA:
5835652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
583695d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
583795d67482SBill Paul 			    &sc->bge_ifmedia, command);
583895d67482SBill Paul 		} else {
583995d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
584095d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
584195d67482SBill Paul 			    &mii->mii_media, command);
584295d67482SBill Paul 		}
584395d67482SBill Paul 		break;
584495d67482SBill Paul 	case SIOCSIFCAP:
5845fba8b109SMarcel Moolenaar 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
584675719184SGleb Smirnoff #ifdef DEVICE_POLLING
584775719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
584875719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
5849bd071d4dSGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
585075719184SGleb Smirnoff 				if (error)
585175719184SGleb Smirnoff 					return (error);
585275719184SGleb Smirnoff 				BGE_LOCK(sc);
585375719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
585475719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
585538cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
5856fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, IFCAP_POLLING, 0);
585775719184SGleb Smirnoff 				BGE_UNLOCK(sc);
585875719184SGleb Smirnoff 			} else {
585975719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
586075719184SGleb Smirnoff 				/* Enable interrupt even in error case */
586175719184SGleb Smirnoff 				BGE_LOCK(sc);
586275719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
586375719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
586438cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
5865fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_POLLING);
586675719184SGleb Smirnoff 				BGE_UNLOCK(sc);
586775719184SGleb Smirnoff 			}
586875719184SGleb Smirnoff 		}
586975719184SGleb Smirnoff #endif
5870d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5871fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TXCSUM) != 0) {
5872fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TXCSUM);
5873fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
5874fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp,
5875fba8b109SMarcel Moolenaar 				    sc->bge_csum_features, 0);
587695d67482SBill Paul 			else
5877fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, 0,
5878fba8b109SMarcel Moolenaar 				    sc->bge_csum_features);
587995d67482SBill Paul 		}
5880cb2eacc7SYaroslav Tykhiy 
5881d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5882fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_RXCSUM) != 0)
5883fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_RXCSUM);
5884d8b57f98SPyun YongHyeon 
5885ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5886fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TSO4) != 0) {
5887fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TSO4);
5888fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TSO4) != 0)
5889fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, CSUM_TSO, 0);
5890ca3f1187SPyun YongHyeon 			else
5891fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, 0, CSUM_TSO);
5892ca3f1187SPyun YongHyeon 		}
5893ca3f1187SPyun YongHyeon 
5894cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5895fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
5896fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
5897cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5898cb2eacc7SYaroslav Tykhiy 		}
5899cb2eacc7SYaroslav Tykhiy 
590004bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
5901fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTSO) != 0)
5902fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
590304bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
5904fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
5905fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
5906fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0)
5907fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_VLAN_HWTSO);
5908cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5909cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5910cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
591104bde852SPyun YongHyeon 		}
5912cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5913fba8b109SMarcel Moolenaar 		if_vlancap(ifp);
5914cb2eacc7SYaroslav Tykhiy #endif
591595d67482SBill Paul 		break;
591695d67482SBill Paul 	default:
5917673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
591895d67482SBill Paul 		break;
591995d67482SBill Paul 	}
592095d67482SBill Paul 
592195d67482SBill Paul 	return (error);
592295d67482SBill Paul }
592395d67482SBill Paul 
592495d67482SBill Paul static void
5925b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
592695d67482SBill Paul {
5927fba8b109SMarcel Moolenaar 	if_t ifp;
5928b584d2b3SPyun YongHyeon 	uint32_t status;
592995d67482SBill Paul 
5930b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5931b74e67fbSGleb Smirnoff 
5932b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5933b74e67fbSGleb Smirnoff 		return;
5934b74e67fbSGleb Smirnoff 
5935b584d2b3SPyun YongHyeon 	/* If pause frames are active then don't reset the hardware. */
5936b584d2b3SPyun YongHyeon 	if ((CSR_READ_4(sc, BGE_RX_MODE) & BGE_RXMODE_FLOWCTL_ENABLE) != 0) {
5937b584d2b3SPyun YongHyeon 		status = CSR_READ_4(sc, BGE_RX_STS);
5938b584d2b3SPyun YongHyeon 		if ((status & BGE_RXSTAT_REMOTE_XOFFED) != 0) {
5939b584d2b3SPyun YongHyeon 			/*
5940b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5941b584d2b3SPyun YongHyeon 			 * the condition to clear.
5942b584d2b3SPyun YongHyeon 			 */
5943b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5944b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5945b584d2b3SPyun YongHyeon 			return;
5946b584d2b3SPyun YongHyeon 		} else if ((status & BGE_RXSTAT_RCVD_XOFF) != 0 &&
5947b584d2b3SPyun YongHyeon 		    (status & BGE_RXSTAT_RCVD_XON) != 0) {
5948b584d2b3SPyun YongHyeon 			/*
5949b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5950b584d2b3SPyun YongHyeon 			 * the condition to clear.
5951b584d2b3SPyun YongHyeon 			 */
5952b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5953b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5954b584d2b3SPyun YongHyeon 			return;
5955b584d2b3SPyun YongHyeon 		}
5956b584d2b3SPyun YongHyeon 		/*
5957b584d2b3SPyun YongHyeon 		 * Any other condition is unexpected and the controller
5958b584d2b3SPyun YongHyeon 		 * should be reset.
5959b584d2b3SPyun YongHyeon 		 */
5960b584d2b3SPyun YongHyeon 	}
5961b584d2b3SPyun YongHyeon 
5962b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
596395d67482SBill Paul 
5964fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
596595d67482SBill Paul 
5966fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
5967426742bfSGleb Smirnoff 	bge_init_locked(sc);
596895d67482SBill Paul 
5969df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
597095d67482SBill Paul }
597195d67482SBill Paul 
59725a147ba6SPyun YongHyeon static void
59735a147ba6SPyun YongHyeon bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
59745a147ba6SPyun YongHyeon {
59755a147ba6SPyun YongHyeon 	int i;
59765a147ba6SPyun YongHyeon 
59775a147ba6SPyun YongHyeon 	BGE_CLRBIT(sc, reg, bit);
59785a147ba6SPyun YongHyeon 
59795a147ba6SPyun YongHyeon 	for (i = 0; i < BGE_TIMEOUT; i++) {
59805a147ba6SPyun YongHyeon 		if ((CSR_READ_4(sc, reg) & bit) == 0)
59815a147ba6SPyun YongHyeon 			return;
59825a147ba6SPyun YongHyeon 		DELAY(100);
59835a147ba6SPyun YongHyeon         }
59845a147ba6SPyun YongHyeon }
59855a147ba6SPyun YongHyeon 
598695d67482SBill Paul /*
598795d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
598895d67482SBill Paul  * RX and TX lists.
598995d67482SBill Paul  */
599095d67482SBill Paul static void
59913f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
599295d67482SBill Paul {
5993fba8b109SMarcel Moolenaar 	if_t ifp;
599495d67482SBill Paul 
59950f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
59960f9bd73bSSam Leffler 
5997fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
599895d67482SBill Paul 
59990f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
600095d67482SBill Paul 
600144b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
600244b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
600344b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
600444b63691SBjoern A. Zeeb 
600544b63691SBjoern A. Zeeb 	/*
600644b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
600744b63691SBjoern A. Zeeb 	 */
600844b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
6009548c8f1aSPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
601044b63691SBjoern A. Zeeb 
601195d67482SBill Paul 	/*
60123f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
601395d67482SBill Paul 	 */
60145a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
60155a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
60165a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
60175a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60185a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
60195a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
60205a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
60215a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
602295d67482SBill Paul 
602395d67482SBill Paul 	/*
60243f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
602595d67482SBill Paul 	 */
60265a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
60275a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
60285a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
60295a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
60305a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
60315a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60325a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
60335a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
603495d67482SBill Paul 
603595d67482SBill Paul 	/*
603695d67482SBill Paul 	 * Shut down all of the memory managers and related
603795d67482SBill Paul 	 * state machines.
603895d67482SBill Paul 	 */
60395a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
60405a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
60415a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60425a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
60435a147ba6SPyun YongHyeon 
60440c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
604595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
60467ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
604795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
604895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
60490434d1b8SBill Paul 	}
60502280c16bSPyun YongHyeon 	/* Update MAC statistics. */
60512280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
60522280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
605395d67482SBill Paul 
60548cb1383cSDoug Ambrisko 	bge_reset(sc);
6055548c8f1aSPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
6056548c8f1aSPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
60578cb1383cSDoug Ambrisko 
60588cb1383cSDoug Ambrisko 	/*
60598cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
60608cb1383cSDoug Ambrisko 	 */
60618cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
60628cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
60638cb1383cSDoug Ambrisko 	else
606495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
606595d67482SBill Paul 
606695d67482SBill Paul 	/* Free the RX lists. */
606795d67482SBill Paul 	bge_free_rx_ring_std(sc);
606895d67482SBill Paul 
606995d67482SBill Paul 	/* Free jumbo RX list. */
60704c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
607195d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
607295d67482SBill Paul 
607395d67482SBill Paul 	/* Free TX buffers. */
607495d67482SBill Paul 	bge_free_tx_ring(sc);
607595d67482SBill Paul 
607695d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
607795d67482SBill Paul 
60785dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
60791493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
60801493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
60811493e883SOleg Bulyzhin 	sc->bge_link = 0;
608295d67482SBill Paul 
6083fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
608495d67482SBill Paul }
608595d67482SBill Paul 
608695d67482SBill Paul /*
608795d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
608895d67482SBill Paul  * get confused by errant DMAs when rebooting.
608995d67482SBill Paul  */
6090b6c974e8SWarner Losh static int
60913f74909aSGleb Smirnoff bge_shutdown(device_t dev)
609295d67482SBill Paul {
609395d67482SBill Paul 	struct bge_softc *sc;
609495d67482SBill Paul 
609595d67482SBill Paul 	sc = device_get_softc(dev);
60960f9bd73bSSam Leffler 	BGE_LOCK(sc);
609795d67482SBill Paul 	bge_stop(sc);
60980f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
6099b6c974e8SWarner Losh 
6100b6c974e8SWarner Losh 	return (0);
610195d67482SBill Paul }
610214afefa3SPawel Jakub Dawidek 
610314afefa3SPawel Jakub Dawidek static int
610414afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
610514afefa3SPawel Jakub Dawidek {
610614afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
610714afefa3SPawel Jakub Dawidek 
610814afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
610914afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
611014afefa3SPawel Jakub Dawidek 	bge_stop(sc);
611114afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
611214afefa3SPawel Jakub Dawidek 
611314afefa3SPawel Jakub Dawidek 	return (0);
611414afefa3SPawel Jakub Dawidek }
611514afefa3SPawel Jakub Dawidek 
611614afefa3SPawel Jakub Dawidek static int
611714afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
611814afefa3SPawel Jakub Dawidek {
611914afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
6120fba8b109SMarcel Moolenaar 	if_t ifp;
612114afefa3SPawel Jakub Dawidek 
612214afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
612314afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
612414afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
6125fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_UP) {
612614afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
6127fba8b109SMarcel Moolenaar 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
612814afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
612914afefa3SPawel Jakub Dawidek 	}
613014afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
613114afefa3SPawel Jakub Dawidek 
613214afefa3SPawel Jakub Dawidek 	return (0);
613314afefa3SPawel Jakub Dawidek }
6134dab5cd05SOleg Bulyzhin 
6135dab5cd05SOleg Bulyzhin static void
61363f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
6137dab5cd05SOleg Bulyzhin {
61381f313773SOleg Bulyzhin 	struct mii_data *mii;
61391f313773SOleg Bulyzhin 	uint32_t link, status;
6140dab5cd05SOleg Bulyzhin 
6141dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
61421f313773SOleg Bulyzhin 
61433f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
61447b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
61457b97099dSOleg Bulyzhin 
6146dab5cd05SOleg Bulyzhin 	/*
6147dab5cd05SOleg Bulyzhin 	 * Process link state changes.
6148dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
6149dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
6150dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
6151dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
6152dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
6153dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
6154dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
6155dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
61561f313773SOleg Bulyzhin 	 *
61571f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
61584c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
6159dab5cd05SOleg Bulyzhin 	 */
6160dab5cd05SOleg Bulyzhin 
61611f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
61624c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
6163dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
6164dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
61651f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
61665dda8085SOleg Bulyzhin 			mii_pollstat(mii);
61671f313773SOleg Bulyzhin 			if (!sc->bge_link &&
61681f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
61691f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
61701f313773SOleg Bulyzhin 				sc->bge_link++;
61711f313773SOleg Bulyzhin 				if (bootverbose)
61721f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
61731f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
61741f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
61751f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
61761f313773SOleg Bulyzhin 				sc->bge_link = 0;
61771f313773SOleg Bulyzhin 				if (bootverbose)
61781f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
61791f313773SOleg Bulyzhin 			}
61801f313773SOleg Bulyzhin 
61813f74909aSGleb Smirnoff 			/* Clear the interrupt. */
6182dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
6183dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
6184daeeb75cSPyun YongHyeon 			bge_miibus_readreg(sc->bge_dev, sc->bge_phy_addr,
6185daeeb75cSPyun YongHyeon 			    BRGPHY_MII_ISR);
6186daeeb75cSPyun YongHyeon 			bge_miibus_writereg(sc->bge_dev, sc->bge_phy_addr,
6187daeeb75cSPyun YongHyeon 			    BRGPHY_MII_IMR, BRGPHY_INTRS);
6188dab5cd05SOleg Bulyzhin 		}
6189dab5cd05SOleg Bulyzhin 		return;
6190dab5cd05SOleg Bulyzhin 	}
6191dab5cd05SOleg Bulyzhin 
6192652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
61931f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
61947b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
61957b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
61961f313773SOleg Bulyzhin 				sc->bge_link++;
61979b80ffe7SPyun YongHyeon 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
61981f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
61991f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
62009b80ffe7SPyun YongHyeon 					DELAY(40);
62019b80ffe7SPyun YongHyeon 				}
62020c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
62031f313773SOleg Bulyzhin 				if (bootverbose)
62041f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
62053f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
62063f74909aSGleb Smirnoff 				    LINK_STATE_UP);
62077b97099dSOleg Bulyzhin 			}
62081f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
6209dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
62101f313773SOleg Bulyzhin 			if (bootverbose)
62111f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
62127b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
62131f313773SOleg Bulyzhin 		}
62146ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
62151f313773SOleg Bulyzhin 		/*
62160c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
62170c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
62180c8aa4eaSJung-uk Kim 		 * PHY link status directly.
62191f313773SOleg Bulyzhin 		 */
62201f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
62211f313773SOleg Bulyzhin 
62221f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
62231f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
62241f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
62255dda8085SOleg Bulyzhin 			mii_pollstat(mii);
62261f313773SOleg Bulyzhin 			if (!sc->bge_link &&
62271f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
62281f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
62291f313773SOleg Bulyzhin 				sc->bge_link++;
62301f313773SOleg Bulyzhin 				if (bootverbose)
62311f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
62321f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
62331f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
62341f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
62351f313773SOleg Bulyzhin 				sc->bge_link = 0;
62361f313773SOleg Bulyzhin 				if (bootverbose)
62371f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
62381f313773SOleg Bulyzhin 			}
62391f313773SOleg Bulyzhin 		}
62400c8aa4eaSJung-uk Kim 	} else {
62410c8aa4eaSJung-uk Kim 		/*
62426ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
62436ede2cfaSPyun YongHyeon 		 * link status.
62440c8aa4eaSJung-uk Kim 		 */
62456ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
62466ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
62476ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
6248dab5cd05SOleg Bulyzhin 	}
6249dab5cd05SOleg Bulyzhin 
62502246e8c6SPyun YongHyeon 	/* Disable MAC attention when link is up. */
6251dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
6252dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
6253dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
6254dab5cd05SOleg Bulyzhin }
62556f8718a3SScott Long 
62566f8718a3SScott Long static void
62576f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
62586f8718a3SScott Long {
62596f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
62602280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
62617e32f79aSPyun YongHyeon 	int unit;
62626f8718a3SScott Long 
62636f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
62646f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
62656f8718a3SScott Long 
62666f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
62676f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
62686f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
62696f8718a3SScott Long 	    "Debug Information");
62706f8718a3SScott Long 
62716f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
62726f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
6273548c8f1aSPyun YongHyeon 	    "MAC Register Read");
6274548c8f1aSPyun YongHyeon 
6275548c8f1aSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ape_read",
6276548c8f1aSPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_ape_read, "I",
6277548c8f1aSPyun YongHyeon 	    "APE Register Read");
62786f8718a3SScott Long 
62796f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
62806f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
62816f8718a3SScott Long 	    "Memory Read");
62826f8718a3SScott Long 
62836f8718a3SScott Long #endif
6284763757b2SScott Long 
62857e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
6286beaa2ae1SPyun YongHyeon 	/*
6287beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
6288beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
6289beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
6290beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
6291beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
6292beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
6293beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
6294beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
6295beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
6296beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
6297beaa2ae1SPyun YongHyeon 	 */
62987e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
6299beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
6300af3b2549SHans Petter Selasky 	    CTLFLAG_RWTUN, &sc->bge_forced_collapse, 0,
6301beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
6302beaa2ae1SPyun YongHyeon 	    "forced collapsing");
6303beaa2ae1SPyun YongHyeon 
63042ae7f64bSPyun YongHyeon 	sc->bge_msi = 1;
63052ae7f64bSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
6306af3b2549SHans Petter Selasky 	    CTLFLAG_RDTUN, &sc->bge_msi, 0, "Enable MSI");
63075c952e8dSPyun YongHyeon 
630835f945cdSPyun YongHyeon 	/*
630935f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
631035f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
631135f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
631235f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
631335f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
631435f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
631535f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
631635f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
631735f945cdSPyun YongHyeon 	 */
631835f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
631935f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
6320af3b2549SHans Petter Selasky 	    CTLFLAG_RWTUN, &sc->bge_forced_udpcsum, 0,
632135f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
632235f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
632335f945cdSPyun YongHyeon 
6324d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
63252280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
63262280c16bSPyun YongHyeon 	else
63272280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
63282280c16bSPyun YongHyeon }
6329d949071dSJung-uk Kim 
63302280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
63312280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
63322280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
63332280c16bSPyun YongHyeon 	    desc)
63342280c16bSPyun YongHyeon 
63352280c16bSPyun YongHyeon static void
63362280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
63372280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
63382280c16bSPyun YongHyeon {
63392280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
63402280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
63412280c16bSPyun YongHyeon 
63422280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
6343763757b2SScott Long 	    NULL, "BGE Statistics");
6344763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
6345763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
6346763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
6347763757b2SScott Long 	    "FramesDroppedDueToFilters");
6348763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
6349763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
6350763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
6351763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
6352763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
6353763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
635406e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
635506e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
635606e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
635706e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
6358763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
6359763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
6360763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
6361763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
6362763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
6363763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
6364763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
6365763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
6366763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
6367763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
6368763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
6369763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
6370763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
6371763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
6372763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
6373763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
6374763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
6375763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
6376763757b2SScott Long 
6377763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
6378763757b2SScott Long 	    NULL, "BGE RX Statistics");
6379763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6380763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
63811cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
6382763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
6383763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
6384763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
63851cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
6386763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
6387763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
6388763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
6389763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
6390763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
6391763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
6392763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
6393763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
6394763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
6395763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
6396763757b2SScott Long 	    "xoffPauseFramesReceived");
6397763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
6398763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
6399763757b2SScott Long 	    "ControlFramesReceived");
6400763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
6401763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
6402763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
6403763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
6404763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
6405763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
6406763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
6407763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
6408763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
640906e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
6410763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
641106e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
6412763757b2SScott Long 
6413763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
6414763757b2SScott Long 	    NULL, "BGE TX Statistics");
6415763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6416763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
64171cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
6418763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
6419763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
6420763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
6421763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
6422763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
6423763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
6424763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
6425763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
6426763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
6427763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
6428763757b2SScott Long 	    "InternalMacTransmitErrors");
6429763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
6430763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
6431763757b2SScott Long 	    "SingleCollisionFrames");
6432763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
6433763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
6434763757b2SScott Long 	    "MultipleCollisionFrames");
6435763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
6436763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
6437763757b2SScott Long 	    "DeferredTransmissions");
6438763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
6439763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
6440763757b2SScott Long 	    "ExcessiveCollisions");
6441763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
644206e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
644306e83c7eSScott Long 	    "LateCollisions");
6444763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
64451cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
6446763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
6447763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
6448763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
6449763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
6450763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
6451763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
6452763757b2SScott Long 	    "CarrierSenseErrors");
6453763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
6454763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
6455763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
6456763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
6457763757b2SScott Long }
6458763757b2SScott Long 
64592280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
64602280c16bSPyun YongHyeon 
64612280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
64626dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
64632280c16bSPyun YongHyeon 
64642280c16bSPyun YongHyeon static void
64652280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
64662280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
64672280c16bSPyun YongHyeon {
64682280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
64692280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
64702280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
64712280c16bSPyun YongHyeon 
64722280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
64732280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
64742280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
64752280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
64762280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
64772280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
64782280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
64792280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
64802280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
64812280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
64822280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
64832280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
64842280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
64852280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
64862280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
64872280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
64882280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
64892280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
64902280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
64912280c16bSPyun YongHyeon 
64922280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
64932280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
64942280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
64952280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
64962280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
64972280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
64982280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
64991cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
65002280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
65012280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
65022280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
65032280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
65042280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
65052280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
65062280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
65072280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
65082280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
65092280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
65102280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
65112280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
65122280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
65132280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
65142280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
65152280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
65162280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
65172280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
65182280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
65192280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
65202280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
65212280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
65222280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
65232280c16bSPyun YongHyeon 
65242280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
65252280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
65262280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
65271cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
65282280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
65292280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
65302280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
65312280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
65322280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
65332280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
65342280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
65352280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
65362280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
65372280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
65382280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
65392280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
65402280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
65412280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
65422280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
65432280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
65442280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
65452280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
65462280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
65472280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
65482280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
65491cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
65502280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
65511cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
65522280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
65531cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
65542280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
65552280c16bSPyun YongHyeon }
65562280c16bSPyun YongHyeon 
65572280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
65582280c16bSPyun YongHyeon 
6559763757b2SScott Long static int
6560763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
6561763757b2SScott Long {
6562763757b2SScott Long 	struct bge_softc *sc;
656306e83c7eSScott Long 	uint32_t result;
6564d949071dSJung-uk Kim 	int offset;
6565763757b2SScott Long 
6566763757b2SScott Long 	sc = (struct bge_softc *)arg1;
6567763757b2SScott Long 	offset = arg2;
6568d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
6569d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
6570041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
65716f8718a3SScott Long }
65726f8718a3SScott Long 
65736f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
65746f8718a3SScott Long static int
65756f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
65766f8718a3SScott Long {
65776f8718a3SScott Long 	struct bge_softc *sc;
65786f8718a3SScott Long 	uint16_t *sbdata;
657928276ad6SPyun YongHyeon 	int error, result, sbsz;
65806f8718a3SScott Long 	int i, j;
65816f8718a3SScott Long 
65826f8718a3SScott Long 	result = -1;
65836f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
65846f8718a3SScott Long 	if (error || (req->newptr == NULL))
65856f8718a3SScott Long 		return (error);
65866f8718a3SScott Long 
65876f8718a3SScott Long 	if (result == 1) {
65886f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
65896f8718a3SScott Long 
659028276ad6SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
659128276ad6SPyun YongHyeon 		    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
659228276ad6SPyun YongHyeon 			sbsz = BGE_STATUS_BLK_SZ;
659328276ad6SPyun YongHyeon 		else
659428276ad6SPyun YongHyeon 			sbsz = 32;
65956f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
65966f8718a3SScott Long 		printf("Status Block:\n");
659728276ad6SPyun YongHyeon 		BGE_LOCK(sc);
659828276ad6SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
659928276ad6SPyun YongHyeon 		    sc->bge_cdata.bge_status_map,
660028276ad6SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
660128276ad6SPyun YongHyeon 		for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
66026f8718a3SScott Long 			printf("%06x:", i);
660328276ad6SPyun YongHyeon 			for (j = 0; j < 8; j++)
660428276ad6SPyun YongHyeon 				printf(" %04x", sbdata[i++]);
66056f8718a3SScott Long 			printf("\n");
66066f8718a3SScott Long 		}
66076f8718a3SScott Long 
66086f8718a3SScott Long 		printf("Registers:\n");
66090c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
66106f8718a3SScott Long 			printf("%06x:", i);
66116f8718a3SScott Long 			for (j = 0; j < 8; j++) {
66126f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
66136f8718a3SScott Long 				i += 4;
66146f8718a3SScott Long 			}
66156f8718a3SScott Long 			printf("\n");
66166f8718a3SScott Long 		}
661728276ad6SPyun YongHyeon 		BGE_UNLOCK(sc);
66186f8718a3SScott Long 
66196f8718a3SScott Long 		printf("Hardware Flags:\n");
662028276ad6SPyun YongHyeon 		if (BGE_IS_5717_PLUS(sc))
662128276ad6SPyun YongHyeon 			printf(" - 5717 Plus\n");
6622a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
6623a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
66245345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
66256f8718a3SScott Long 			printf(" - 575X Plus\n");
66265345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
66276f8718a3SScott Long 			printf(" - 5705 Plus\n");
66285345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
66295345bad0SScott Long 			printf(" - 5714 Family\n");
66305345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
66315345bad0SScott Long 			printf(" - 5700 Family\n");
66326f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
66336f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
66346f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
66356f8718a3SScott Long 			printf(" - PCI-X Bus\n");
66366f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
66376f8718a3SScott Long 			printf(" - PCI Express Bus\n");
66387d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
66396f8718a3SScott Long 			printf(" - No 3 LEDs\n");
66406f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
66416f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
66426f8718a3SScott Long 	}
66436f8718a3SScott Long 
66446f8718a3SScott Long 	return (error);
66456f8718a3SScott Long }
66466f8718a3SScott Long 
66476f8718a3SScott Long static int
66486f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
66496f8718a3SScott Long {
66506f8718a3SScott Long 	struct bge_softc *sc;
66516f8718a3SScott Long 	int error;
66526f8718a3SScott Long 	uint16_t result;
66536f8718a3SScott Long 	uint32_t val;
66546f8718a3SScott Long 
66556f8718a3SScott Long 	result = -1;
66566f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
66576f8718a3SScott Long 	if (error || (req->newptr == NULL))
66586f8718a3SScott Long 		return (error);
66596f8718a3SScott Long 
66606f8718a3SScott Long 	if (result < 0x8000) {
66616f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
66626f8718a3SScott Long 		val = CSR_READ_4(sc, result);
66636f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
66646f8718a3SScott Long 	}
66656f8718a3SScott Long 
66666f8718a3SScott Long 	return (error);
66676f8718a3SScott Long }
66686f8718a3SScott Long 
66696f8718a3SScott Long static int
6670548c8f1aSPyun YongHyeon bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS)
6671548c8f1aSPyun YongHyeon {
6672548c8f1aSPyun YongHyeon 	struct bge_softc *sc;
6673548c8f1aSPyun YongHyeon 	int error;
6674548c8f1aSPyun YongHyeon 	uint16_t result;
6675548c8f1aSPyun YongHyeon 	uint32_t val;
6676548c8f1aSPyun YongHyeon 
6677548c8f1aSPyun YongHyeon 	result = -1;
6678548c8f1aSPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
6679548c8f1aSPyun YongHyeon 	if (error || (req->newptr == NULL))
6680548c8f1aSPyun YongHyeon 		return (error);
6681548c8f1aSPyun YongHyeon 
6682548c8f1aSPyun YongHyeon 	if (result < 0x8000) {
6683548c8f1aSPyun YongHyeon 		sc = (struct bge_softc *)arg1;
6684548c8f1aSPyun YongHyeon 		val = APE_READ_4(sc, result);
6685548c8f1aSPyun YongHyeon 		printf("reg 0x%06X = 0x%08X\n", result, val);
6686548c8f1aSPyun YongHyeon 	}
6687548c8f1aSPyun YongHyeon 
6688548c8f1aSPyun YongHyeon 	return (error);
6689548c8f1aSPyun YongHyeon }
6690548c8f1aSPyun YongHyeon 
6691548c8f1aSPyun YongHyeon static int
66926f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
66936f8718a3SScott Long {
66946f8718a3SScott Long 	struct bge_softc *sc;
66956f8718a3SScott Long 	int error;
66966f8718a3SScott Long 	uint16_t result;
66976f8718a3SScott Long 	uint32_t val;
66986f8718a3SScott Long 
66996f8718a3SScott Long 	result = -1;
67006f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
67016f8718a3SScott Long 	if (error || (req->newptr == NULL))
67026f8718a3SScott Long 		return (error);
67036f8718a3SScott Long 
67046f8718a3SScott Long 	if (result < 0x8000) {
67056f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
67066f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
67076f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
67086f8718a3SScott Long 	}
67096f8718a3SScott Long 
67106f8718a3SScott Long 	return (error);
67116f8718a3SScott Long }
67126f8718a3SScott Long #endif
671338cc658fSJohn Baldwin 
671438cc658fSJohn Baldwin static int
67155fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
67165fea260fSMarius Strobl {
67175fea260fSMarius Strobl 
67185fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
67195fea260fSMarius Strobl 		return (1);
67205fea260fSMarius Strobl 
67215fea260fSMarius Strobl #ifdef __sparc64__
67225fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
67235fea260fSMarius Strobl 	return (0);
67245fea260fSMarius Strobl #endif
67255fea260fSMarius Strobl 	return (1);
67265fea260fSMarius Strobl }
67275fea260fSMarius Strobl 
67285fea260fSMarius Strobl static int
672938cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
673038cc658fSJohn Baldwin {
673138cc658fSJohn Baldwin 	uint32_t mac_addr;
673238cc658fSJohn Baldwin 
673373635418SPyun YongHyeon 	mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
673438cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
673538cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
673638cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
673773635418SPyun YongHyeon 		mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
673838cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
673938cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
674038cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
674138cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
67425fea260fSMarius Strobl 		return (0);
674338cc658fSJohn Baldwin 	}
67445fea260fSMarius Strobl 	return (1);
674538cc658fSJohn Baldwin }
674638cc658fSJohn Baldwin 
674738cc658fSJohn Baldwin static int
674838cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
674938cc658fSJohn Baldwin {
675038cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
675138cc658fSJohn Baldwin 
675238cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
675338cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
675438cc658fSJohn Baldwin 
67555fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
67565fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
675738cc658fSJohn Baldwin }
675838cc658fSJohn Baldwin 
675938cc658fSJohn Baldwin static int
676038cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
676138cc658fSJohn Baldwin {
676238cc658fSJohn Baldwin 
67635fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
67645fea260fSMarius Strobl 		return (1);
67655fea260fSMarius Strobl 
67665fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
67675fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
676838cc658fSJohn Baldwin }
676938cc658fSJohn Baldwin 
677038cc658fSJohn Baldwin static int
677138cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
677238cc658fSJohn Baldwin {
677338cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
677438cc658fSJohn Baldwin 		/* NOTE: Order is critical */
67755fea260fSMarius Strobl 		bge_get_eaddr_fw,
677638cc658fSJohn Baldwin 		bge_get_eaddr_mem,
677738cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
677838cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
677938cc658fSJohn Baldwin 		NULL
678038cc658fSJohn Baldwin 	};
678138cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
678238cc658fSJohn Baldwin 
678338cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
678438cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
678538cc658fSJohn Baldwin 			break;
678638cc658fSJohn Baldwin 	}
678738cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
678838cc658fSJohn Baldwin }
6789df360178SGleb Smirnoff 
6790df360178SGleb Smirnoff static uint64_t
6791df360178SGleb Smirnoff bge_get_counter(if_t ifp, ift_counter cnt)
6792df360178SGleb Smirnoff {
6793df360178SGleb Smirnoff 	struct bge_softc *sc;
6794df360178SGleb Smirnoff 	struct bge_mac_stats *stats;
6795df360178SGleb Smirnoff 
6796df360178SGleb Smirnoff 	sc = if_getsoftc(ifp);
6797df360178SGleb Smirnoff 	if (!BGE_IS_5705_PLUS(sc))
6798df360178SGleb Smirnoff 		return (if_get_counter_default(ifp, cnt));
6799df360178SGleb Smirnoff 	stats = &sc->bge_mac_stats;
6800df360178SGleb Smirnoff 
6801df360178SGleb Smirnoff 	switch (cnt) {
6802df360178SGleb Smirnoff 	case IFCOUNTER_IERRORS:
6803df360178SGleb Smirnoff 		return (stats->NoMoreRxBDs + stats->InputDiscards +
6804df360178SGleb Smirnoff 		    stats->InputErrors);
6805df360178SGleb Smirnoff 	case IFCOUNTER_COLLISIONS:
6806df360178SGleb Smirnoff 		return (stats->etherStatsCollisions);
6807df360178SGleb Smirnoff 	default:
6808df360178SGleb Smirnoff 		return (if_get_counter_default(ifp, cnt));
6809df360178SGleb Smirnoff 	}
6810df360178SGleb Smirnoff }
6811*ded66962SMark Johnston 
6812*ded66962SMark Johnston #ifdef NETDUMP
6813*ded66962SMark Johnston static void
6814*ded66962SMark Johnston bge_netdump_init(if_t ifp, int *nrxr, int *ncl, int *clsize)
6815*ded66962SMark Johnston {
6816*ded66962SMark Johnston 	struct bge_softc *sc;
6817*ded66962SMark Johnston 
6818*ded66962SMark Johnston 	sc = if_getsoftc(ifp);
6819*ded66962SMark Johnston 	BGE_LOCK(sc);
6820*ded66962SMark Johnston 	*nrxr = sc->bge_return_ring_cnt;
6821*ded66962SMark Johnston 	*ncl = NETDUMP_MAX_IN_FLIGHT;
6822*ded66962SMark Johnston 	if ((sc->bge_flags & BGE_FLAG_JUMBO_STD) != 0 &&
6823*ded66962SMark Johnston 	    (if_getmtu(sc->bge_ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
6824*ded66962SMark Johnston 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN)))
6825*ded66962SMark Johnston 		*clsize = MJUM9BYTES;
6826*ded66962SMark Johnston 	else
6827*ded66962SMark Johnston 		*clsize = MCLBYTES;
6828*ded66962SMark Johnston 	BGE_UNLOCK(sc);
6829*ded66962SMark Johnston }
6830*ded66962SMark Johnston 
6831*ded66962SMark Johnston static void
6832*ded66962SMark Johnston bge_netdump_event(if_t ifp __unused, enum netdump_ev event __unused)
6833*ded66962SMark Johnston {
6834*ded66962SMark Johnston }
6835*ded66962SMark Johnston 
6836*ded66962SMark Johnston static int
6837*ded66962SMark Johnston bge_netdump_transmit(if_t ifp, struct mbuf *m)
6838*ded66962SMark Johnston {
6839*ded66962SMark Johnston 	struct bge_softc *sc;
6840*ded66962SMark Johnston 	uint32_t prodidx;
6841*ded66962SMark Johnston 	int error;
6842*ded66962SMark Johnston 
6843*ded66962SMark Johnston 	sc = if_getsoftc(ifp);
6844*ded66962SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
6845*ded66962SMark Johnston 	    IFF_DRV_RUNNING)
6846*ded66962SMark Johnston 		return (1);
6847*ded66962SMark Johnston 
6848*ded66962SMark Johnston 	prodidx = sc->bge_tx_prodidx;
6849*ded66962SMark Johnston 	error = bge_encap(sc, &m, &prodidx);
6850*ded66962SMark Johnston 	if (error == 0)
6851*ded66962SMark Johnston 		bge_start_tx(sc, prodidx);
6852*ded66962SMark Johnston 	return (error);
6853*ded66962SMark Johnston }
6854*ded66962SMark Johnston 
6855*ded66962SMark Johnston static int
6856*ded66962SMark Johnston bge_netdump_poll(if_t ifp, int count)
6857*ded66962SMark Johnston {
6858*ded66962SMark Johnston 	struct bge_softc *sc;
6859*ded66962SMark Johnston 	uint32_t rx_prod, tx_cons;
6860*ded66962SMark Johnston 
6861*ded66962SMark Johnston 	sc = if_getsoftc(ifp);
6862*ded66962SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
6863*ded66962SMark Johnston 	    IFF_DRV_RUNNING)
6864*ded66962SMark Johnston 		return (1);
6865*ded66962SMark Johnston 
6866*ded66962SMark Johnston 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
6867*ded66962SMark Johnston 	    sc->bge_cdata.bge_status_map,
6868*ded66962SMark Johnston 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
6869*ded66962SMark Johnston 
6870*ded66962SMark Johnston 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
6871*ded66962SMark Johnston 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
6872*ded66962SMark Johnston 
6873*ded66962SMark Johnston 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
6874*ded66962SMark Johnston 	    sc->bge_cdata.bge_status_map,
6875*ded66962SMark Johnston 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
6876*ded66962SMark Johnston 
6877*ded66962SMark Johnston 	(void)bge_rxeof(sc, rx_prod, 0);
6878*ded66962SMark Johnston 	bge_txeof(sc, tx_cons);
6879*ded66962SMark Johnston 	return (0);
6880*ded66962SMark Johnston }
6881*ded66962SMark Johnston #endif /* NETDUMP */
6882