xref: /freebsd/sys/dev/bge/if_bge.c (revision 91c69b972ed635d4df46b0181476a48dba0a11f5)
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>
103ded66962SMark 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);
431ded66962SMark Johnston static void bge_start_locked(if_t);
432ded66962SMark 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 
522ded66962SMark Johnston NETDUMP_DEFINE(bge);
523ded66962SMark 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);
550ea72f463SWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device", pci, bge, bge_devs,
551ea72f463SWarner Losh     sizeof(bge_devs), nitems(bge_devs) - 1);
55295d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
55395d67482SBill Paul 
554f1a7e6d5SScott Long static int bge_allow_asf = 1;
555f1a7e6d5SScott Long 
5566472ac3dSEd Schouten static SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
557af3b2549SHans Petter Selasky SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RDTUN, &bge_allow_asf, 0,
558f1a7e6d5SScott Long 	"Allow ASF mode if available");
559c4529f41SMichael Reifenberger 
56008013fd3SMarius Strobl #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
56108013fd3SMarius Strobl #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
56208013fd3SMarius Strobl #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
56308013fd3SMarius Strobl #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
56408013fd3SMarius Strobl #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
56508013fd3SMarius Strobl 
56608013fd3SMarius Strobl static int
5675fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
56808013fd3SMarius Strobl {
56908013fd3SMarius Strobl #ifdef __sparc64__
57008013fd3SMarius Strobl 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
57108013fd3SMarius Strobl 	device_t dev;
57208013fd3SMarius Strobl 	uint32_t subvendor;
57308013fd3SMarius Strobl 
57408013fd3SMarius Strobl 	dev = sc->bge_dev;
57508013fd3SMarius Strobl 
57608013fd3SMarius Strobl 	/*
57708013fd3SMarius Strobl 	 * The on-board BGEs found in sun4u machines aren't fitted with
57808013fd3SMarius Strobl 	 * an EEPROM which means that we have to obtain the MAC address
57908013fd3SMarius Strobl 	 * via OFW and that some tests will always fail.  We distinguish
58008013fd3SMarius Strobl 	 * such BGEs by the subvendor ID, which also has to be obtained
58108013fd3SMarius Strobl 	 * from OFW instead of the PCI configuration space as the latter
58208013fd3SMarius Strobl 	 * indicates Broadcom as the subvendor of the netboot interface.
58308013fd3SMarius Strobl 	 * For early Blade 1500 and 2500 we even have to check the OFW
58408013fd3SMarius Strobl 	 * device path as the subvendor ID always defaults to Broadcom
58508013fd3SMarius Strobl 	 * there.
58608013fd3SMarius Strobl 	 */
58708013fd3SMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
58808013fd3SMarius Strobl 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
5892d857b9bSMarius Strobl 	    (subvendor == FJTSU_VENDORID || subvendor == SUN_VENDORID))
59008013fd3SMarius Strobl 		return (0);
59108013fd3SMarius Strobl 	memset(buf, 0, sizeof(buf));
59208013fd3SMarius Strobl 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
59308013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
59408013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
59508013fd3SMarius Strobl 			return (0);
59608013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
59708013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
59808013fd3SMarius Strobl 			return (0);
59908013fd3SMarius Strobl 	}
60008013fd3SMarius Strobl #endif
60108013fd3SMarius Strobl 	return (1);
60208013fd3SMarius Strobl }
60308013fd3SMarius Strobl 
6043f74909aSGleb Smirnoff static uint32_t
6053f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
60695d67482SBill Paul {
60795d67482SBill Paul 	device_t dev;
6086f8718a3SScott Long 	uint32_t val;
60995d67482SBill Paul 
610a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
611a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
612a4431ebaSPyun YongHyeon 		return (0);
613a4431ebaSPyun YongHyeon 
61495d67482SBill Paul 	dev = sc->bge_dev;
61595d67482SBill Paul 
61695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
6176f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
6186f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
6196f8718a3SScott Long 	return (val);
62095d67482SBill Paul }
62195d67482SBill Paul 
62295d67482SBill Paul static void
6233f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
62495d67482SBill Paul {
62595d67482SBill Paul 	device_t dev;
62695d67482SBill Paul 
627a4431ebaSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
628a4431ebaSPyun YongHyeon 	    off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
629a4431ebaSPyun YongHyeon 		return;
630a4431ebaSPyun YongHyeon 
63195d67482SBill Paul 	dev = sc->bge_dev;
63295d67482SBill Paul 
63395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
63495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
6356f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
63695d67482SBill Paul }
63795d67482SBill Paul 
63895d67482SBill Paul #ifdef notdef
6393f74909aSGleb Smirnoff static uint32_t
6403f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
64195d67482SBill Paul {
64295d67482SBill Paul 	device_t dev;
64395d67482SBill Paul 
64495d67482SBill Paul 	dev = sc->bge_dev;
64595d67482SBill Paul 
64695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
64795d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
64895d67482SBill Paul }
64995d67482SBill Paul #endif
65095d67482SBill Paul 
65195d67482SBill Paul static void
6523f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
65395d67482SBill Paul {
65495d67482SBill Paul 	device_t dev;
65595d67482SBill Paul 
65695d67482SBill Paul 	dev = sc->bge_dev;
65795d67482SBill Paul 
65895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
65995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
66095d67482SBill Paul }
66195d67482SBill Paul 
6626f8718a3SScott Long static void
6636f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
6646f8718a3SScott Long {
6656f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6666f8718a3SScott Long }
6676f8718a3SScott Long 
66838cc658fSJohn Baldwin static void
66938cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
67038cc658fSJohn Baldwin {
67138cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
67238cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
67338cc658fSJohn Baldwin 
67438cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
675062af0b0SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_MBOX_REORDER) != 0)
676062af0b0SPyun YongHyeon 		CSR_READ_4(sc, off);
67738cc658fSJohn Baldwin }
67838cc658fSJohn Baldwin 
679f41ac2beSBill Paul /*
680548c8f1aSPyun YongHyeon  * Clear all stale locks and select the lock for this driver instance.
681548c8f1aSPyun YongHyeon  */
682548c8f1aSPyun YongHyeon static void
683548c8f1aSPyun YongHyeon bge_ape_lock_init(struct bge_softc *sc)
684548c8f1aSPyun YongHyeon {
685548c8f1aSPyun YongHyeon 	uint32_t bit, regbase;
686548c8f1aSPyun YongHyeon 	int i;
687548c8f1aSPyun YongHyeon 
688548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
689548c8f1aSPyun YongHyeon 		regbase = BGE_APE_LOCK_GRANT;
690548c8f1aSPyun YongHyeon 	else
691548c8f1aSPyun YongHyeon 		regbase = BGE_APE_PER_LOCK_GRANT;
692548c8f1aSPyun YongHyeon 
693548c8f1aSPyun YongHyeon 	/* Clear any stale locks. */
694548c8f1aSPyun YongHyeon 	for (i = BGE_APE_LOCK_PHY0; i <= BGE_APE_LOCK_GPIO; i++) {
695548c8f1aSPyun YongHyeon 		switch (i) {
696548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY0:
697548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY1:
698548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY2:
699548c8f1aSPyun YongHyeon 		case BGE_APE_LOCK_PHY3:
700548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
701548c8f1aSPyun YongHyeon 			break;
702548c8f1aSPyun YongHyeon 		default:
703bd9c196aSPyun YongHyeon 			if (sc->bge_func_addr == 0)
704548c8f1aSPyun YongHyeon 				bit = BGE_APE_LOCK_GRANT_DRIVER0;
705548c8f1aSPyun YongHyeon 			else
706548c8f1aSPyun YongHyeon 				bit = (1 << sc->bge_func_addr);
707548c8f1aSPyun YongHyeon 		}
708548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, regbase + 4 * i, bit);
709548c8f1aSPyun YongHyeon 	}
710548c8f1aSPyun YongHyeon 
711548c8f1aSPyun YongHyeon 	/* Select the PHY lock based on the device's function number. */
712548c8f1aSPyun YongHyeon 	switch (sc->bge_func_addr) {
713548c8f1aSPyun YongHyeon 	case 0:
714548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY0;
715548c8f1aSPyun YongHyeon 		break;
716548c8f1aSPyun YongHyeon 	case 1:
717548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY1;
718548c8f1aSPyun YongHyeon 		break;
719548c8f1aSPyun YongHyeon 	case 2:
720548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY2;
721548c8f1aSPyun YongHyeon 		break;
722548c8f1aSPyun YongHyeon 	case 3:
723548c8f1aSPyun YongHyeon 		sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY3;
724548c8f1aSPyun YongHyeon 		break;
725548c8f1aSPyun YongHyeon 	default:
726548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev,
727548c8f1aSPyun YongHyeon 		    "PHY lock not supported on this function\n");
728548c8f1aSPyun YongHyeon 	}
729548c8f1aSPyun YongHyeon }
730548c8f1aSPyun YongHyeon 
731548c8f1aSPyun YongHyeon /*
732548c8f1aSPyun YongHyeon  * Check for APE firmware, set flags, and print version info.
733548c8f1aSPyun YongHyeon  */
734548c8f1aSPyun YongHyeon static void
735548c8f1aSPyun YongHyeon bge_ape_read_fw_ver(struct bge_softc *sc)
736548c8f1aSPyun YongHyeon {
737548c8f1aSPyun YongHyeon 	const char *fwtype;
738548c8f1aSPyun YongHyeon 	uint32_t apedata, features;
739548c8f1aSPyun YongHyeon 
740548c8f1aSPyun YongHyeon 	/* Check for a valid APE signature in shared memory. */
741548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_SEG_SIG);
742548c8f1aSPyun YongHyeon 	if (apedata != BGE_APE_SEG_SIG_MAGIC) {
743548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags &= ~ BGE_MFW_ON_APE;
744548c8f1aSPyun YongHyeon 		return;
745548c8f1aSPyun YongHyeon 	}
746548c8f1aSPyun YongHyeon 
747548c8f1aSPyun YongHyeon 	/* Check if APE firmware is running. */
748548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_STATUS);
749548c8f1aSPyun YongHyeon 	if ((apedata & BGE_APE_FW_STATUS_READY) == 0) {
750548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE signature found "
751548c8f1aSPyun YongHyeon 		    "but FW status not ready! 0x%08x\n", apedata);
752548c8f1aSPyun YongHyeon 		return;
753548c8f1aSPyun YongHyeon 	}
754548c8f1aSPyun YongHyeon 
755548c8f1aSPyun YongHyeon 	sc->bge_mfw_flags |= BGE_MFW_ON_APE;
756548c8f1aSPyun YongHyeon 
757548c8f1aSPyun YongHyeon 	/* Fetch the APE firwmare type and version. */
758548c8f1aSPyun YongHyeon 	apedata = APE_READ_4(sc, BGE_APE_FW_VERSION);
759548c8f1aSPyun YongHyeon 	features = APE_READ_4(sc, BGE_APE_FW_FEATURES);
760548c8f1aSPyun YongHyeon 	if ((features & BGE_APE_FW_FEATURE_NCSI) != 0) {
761548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_NCSI;
762548c8f1aSPyun YongHyeon 		fwtype = "NCSI";
763548c8f1aSPyun YongHyeon 	} else if ((features & BGE_APE_FW_FEATURE_DASH) != 0) {
764548c8f1aSPyun YongHyeon 		sc->bge_mfw_flags |= BGE_MFW_TYPE_DASH;
765548c8f1aSPyun YongHyeon 		fwtype = "DASH";
766548c8f1aSPyun YongHyeon 	} else
767548c8f1aSPyun YongHyeon 		fwtype = "UNKN";
768548c8f1aSPyun YongHyeon 
769548c8f1aSPyun YongHyeon 	/* Print the APE firmware version. */
770548c8f1aSPyun YongHyeon 	device_printf(sc->bge_dev, "APE FW version: %s v%d.%d.%d.%d\n",
771548c8f1aSPyun YongHyeon 	    fwtype,
772548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MAJMSK) >> BGE_APE_FW_VERSION_MAJSFT,
773548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_MINMSK) >> BGE_APE_FW_VERSION_MINSFT,
774548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_REVMSK) >> BGE_APE_FW_VERSION_REVSFT,
775548c8f1aSPyun YongHyeon 	    (apedata & BGE_APE_FW_VERSION_BLDMSK));
776548c8f1aSPyun YongHyeon }
777548c8f1aSPyun YongHyeon 
778548c8f1aSPyun YongHyeon static int
779548c8f1aSPyun YongHyeon bge_ape_lock(struct bge_softc *sc, int locknum)
780548c8f1aSPyun YongHyeon {
781548c8f1aSPyun YongHyeon 	uint32_t bit, gnt, req, status;
782548c8f1aSPyun YongHyeon 	int i, off;
783548c8f1aSPyun YongHyeon 
784548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
785548c8f1aSPyun YongHyeon 		return (0);
786548c8f1aSPyun YongHyeon 
787548c8f1aSPyun YongHyeon 	/* Lock request/grant registers have different bases. */
788548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761) {
789548c8f1aSPyun YongHyeon 		req = BGE_APE_LOCK_REQ;
790548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
791548c8f1aSPyun YongHyeon 	} else {
792548c8f1aSPyun YongHyeon 		req = BGE_APE_PER_LOCK_REQ;
793548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
794548c8f1aSPyun YongHyeon 	}
795548c8f1aSPyun YongHyeon 
796548c8f1aSPyun YongHyeon 	off = 4 * locknum;
797548c8f1aSPyun YongHyeon 
798548c8f1aSPyun YongHyeon 	switch (locknum) {
799548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
800548c8f1aSPyun YongHyeon 		/* Lock required when using GPIO. */
801548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
802548c8f1aSPyun YongHyeon 			return (0);
803548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
804548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
805548c8f1aSPyun YongHyeon 		else
806548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
807548c8f1aSPyun YongHyeon 		break;
808548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
809548c8f1aSPyun YongHyeon 		/* Lock required to reset the device. */
810548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
811548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
812548c8f1aSPyun YongHyeon 		else
813548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
814548c8f1aSPyun YongHyeon 		break;
815548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
816548c8f1aSPyun YongHyeon 		/* Lock required when accessing certain APE memory. */
817548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
818548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_REQ_DRIVER0;
819548c8f1aSPyun YongHyeon 		else
820548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
821548c8f1aSPyun YongHyeon 		break;
822548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
823548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
824548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
825548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
826548c8f1aSPyun YongHyeon 		/* Lock required when accessing PHYs. */
827548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_REQ_DRIVER0;
828548c8f1aSPyun YongHyeon 		break;
829548c8f1aSPyun YongHyeon 	default:
830548c8f1aSPyun YongHyeon 		return (EINVAL);
831548c8f1aSPyun YongHyeon 	}
832548c8f1aSPyun YongHyeon 
833548c8f1aSPyun YongHyeon 	/* Request a lock. */
834548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, req + off, bit);
835548c8f1aSPyun YongHyeon 
836548c8f1aSPyun YongHyeon 	/* Wait up to 1 second to acquire lock. */
837548c8f1aSPyun YongHyeon 	for (i = 0; i < 20000; i++) {
838548c8f1aSPyun YongHyeon 		status = APE_READ_4(sc, gnt + off);
839548c8f1aSPyun YongHyeon 		if (status == bit)
840548c8f1aSPyun YongHyeon 			break;
841548c8f1aSPyun YongHyeon 		DELAY(50);
842548c8f1aSPyun YongHyeon 	}
843548c8f1aSPyun YongHyeon 
844548c8f1aSPyun YongHyeon 	/* Handle any errors. */
845548c8f1aSPyun YongHyeon 	if (status != bit) {
846548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE lock %d request failed! "
847548c8f1aSPyun YongHyeon 		    "request = 0x%04x[0x%04x], status = 0x%04x[0x%04x]\n",
848548c8f1aSPyun YongHyeon 		    locknum, req + off, bit & 0xFFFF, gnt + off,
849548c8f1aSPyun YongHyeon 		    status & 0xFFFF);
850548c8f1aSPyun YongHyeon 		/* Revoke the lock request. */
851548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, gnt + off, bit);
852548c8f1aSPyun YongHyeon 		return (EBUSY);
853548c8f1aSPyun YongHyeon 	}
854548c8f1aSPyun YongHyeon 
855548c8f1aSPyun YongHyeon 	return (0);
856548c8f1aSPyun YongHyeon }
857548c8f1aSPyun YongHyeon 
858548c8f1aSPyun YongHyeon static void
859548c8f1aSPyun YongHyeon bge_ape_unlock(struct bge_softc *sc, int locknum)
860548c8f1aSPyun YongHyeon {
861548c8f1aSPyun YongHyeon 	uint32_t bit, gnt;
862548c8f1aSPyun YongHyeon 	int off;
863548c8f1aSPyun YongHyeon 
864548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
865548c8f1aSPyun YongHyeon 		return;
866548c8f1aSPyun YongHyeon 
867548c8f1aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
868548c8f1aSPyun YongHyeon 		gnt = BGE_APE_LOCK_GRANT;
869548c8f1aSPyun YongHyeon 	else
870548c8f1aSPyun YongHyeon 		gnt = BGE_APE_PER_LOCK_GRANT;
871548c8f1aSPyun YongHyeon 
872548c8f1aSPyun YongHyeon 	off = 4 * locknum;
873548c8f1aSPyun YongHyeon 
874548c8f1aSPyun YongHyeon 	switch (locknum) {
875548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GPIO:
876548c8f1aSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
877548c8f1aSPyun YongHyeon 			return;
878548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
879548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
880548c8f1aSPyun YongHyeon 		else
881548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
882548c8f1aSPyun YongHyeon 		break;
883548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_GRC:
884548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
885548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
886548c8f1aSPyun YongHyeon 		else
887548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
888548c8f1aSPyun YongHyeon 		break;
889548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_MEM:
890548c8f1aSPyun YongHyeon 		if (sc->bge_func_addr == 0)
891548c8f1aSPyun YongHyeon 			bit = BGE_APE_LOCK_GRANT_DRIVER0;
892548c8f1aSPyun YongHyeon 		else
893548c8f1aSPyun YongHyeon 			bit = (1 << sc->bge_func_addr);
894548c8f1aSPyun YongHyeon 		break;
895548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY0:
896548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY1:
897548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY2:
898548c8f1aSPyun YongHyeon 	case BGE_APE_LOCK_PHY3:
899548c8f1aSPyun YongHyeon 		bit = BGE_APE_LOCK_GRANT_DRIVER0;
900548c8f1aSPyun YongHyeon 		break;
901548c8f1aSPyun YongHyeon 	default:
902548c8f1aSPyun YongHyeon 		return;
903548c8f1aSPyun YongHyeon 	}
904548c8f1aSPyun YongHyeon 
905548c8f1aSPyun YongHyeon 	APE_WRITE_4(sc, gnt + off, bit);
906548c8f1aSPyun YongHyeon }
907548c8f1aSPyun YongHyeon 
908548c8f1aSPyun YongHyeon /*
909548c8f1aSPyun YongHyeon  * Send an event to the APE firmware.
910548c8f1aSPyun YongHyeon  */
911548c8f1aSPyun YongHyeon static void
912548c8f1aSPyun YongHyeon bge_ape_send_event(struct bge_softc *sc, uint32_t event)
913548c8f1aSPyun YongHyeon {
914548c8f1aSPyun YongHyeon 	uint32_t apedata;
915548c8f1aSPyun YongHyeon 	int i;
916548c8f1aSPyun YongHyeon 
917548c8f1aSPyun YongHyeon 	/* NCSI does not support APE events. */
918548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
919548c8f1aSPyun YongHyeon 		return;
920548c8f1aSPyun YongHyeon 
921548c8f1aSPyun YongHyeon 	/* Wait up to 1ms for APE to service previous event. */
922548c8f1aSPyun YongHyeon 	for (i = 10; i > 0; i--) {
923548c8f1aSPyun YongHyeon 		if (bge_ape_lock(sc, BGE_APE_LOCK_MEM) != 0)
924548c8f1aSPyun YongHyeon 			break;
925548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_EVENT_STATUS);
926548c8f1aSPyun YongHyeon 		if ((apedata & BGE_APE_EVENT_STATUS_EVENT_PENDING) == 0) {
927548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT_STATUS, event |
928548c8f1aSPyun YongHyeon 			    BGE_APE_EVENT_STATUS_EVENT_PENDING);
929548c8f1aSPyun YongHyeon 			bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
930548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_EVENT, BGE_APE_EVENT_1);
931548c8f1aSPyun YongHyeon 			break;
932548c8f1aSPyun YongHyeon 		}
933548c8f1aSPyun YongHyeon 		bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
934548c8f1aSPyun YongHyeon 		DELAY(100);
935548c8f1aSPyun YongHyeon 	}
936548c8f1aSPyun YongHyeon 	if (i == 0)
937548c8f1aSPyun YongHyeon 		device_printf(sc->bge_dev, "APE event 0x%08x send timed out\n",
938548c8f1aSPyun YongHyeon 		    event);
939548c8f1aSPyun YongHyeon }
940548c8f1aSPyun YongHyeon 
941548c8f1aSPyun YongHyeon static void
942548c8f1aSPyun YongHyeon bge_ape_driver_state_change(struct bge_softc *sc, int kind)
943548c8f1aSPyun YongHyeon {
944548c8f1aSPyun YongHyeon 	uint32_t apedata, event;
945548c8f1aSPyun YongHyeon 
946548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
947548c8f1aSPyun YongHyeon 		return;
948548c8f1aSPyun YongHyeon 
949548c8f1aSPyun YongHyeon 	switch (kind) {
950548c8f1aSPyun YongHyeon 	case BGE_RESET_START:
951548c8f1aSPyun YongHyeon 		/* If this is the first load, clear the load counter. */
952548c8f1aSPyun YongHyeon 		apedata = APE_READ_4(sc, BGE_APE_HOST_SEG_SIG);
953548c8f1aSPyun YongHyeon 		if (apedata != BGE_APE_HOST_SEG_SIG_MAGIC)
954548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, 0);
955548c8f1aSPyun YongHyeon 		else {
956548c8f1aSPyun YongHyeon 			apedata = APE_READ_4(sc, BGE_APE_HOST_INIT_COUNT);
957548c8f1aSPyun YongHyeon 			APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, ++apedata);
958548c8f1aSPyun YongHyeon 		}
959548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_SIG,
960548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_SIG_MAGIC);
961548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_SEG_LEN,
962548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_SEG_LEN_MAGIC);
963548c8f1aSPyun YongHyeon 
964548c8f1aSPyun YongHyeon 		/* Add some version info if bge(4) supports it. */
965548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRIVER_ID,
966548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRIVER_ID_MAGIC(1, 0));
967548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_BEHAVIOR,
968548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_BEHAV_NO_PHYLOCK);
969548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_HEARTBEAT_INT_MS,
970548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_HEARTBEAT_INT_DISABLE);
971548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
972548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_START);
973548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_START;
974548c8f1aSPyun YongHyeon 		break;
975548c8f1aSPyun YongHyeon 	case BGE_RESET_SHUTDOWN:
976548c8f1aSPyun YongHyeon 		APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
977548c8f1aSPyun YongHyeon 		    BGE_APE_HOST_DRVR_STATE_UNLOAD);
978548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_UNLOAD;
979548c8f1aSPyun YongHyeon 		break;
980548c8f1aSPyun YongHyeon 	case BGE_RESET_SUSPEND:
981548c8f1aSPyun YongHyeon 		event = BGE_APE_EVENT_STATUS_STATE_SUSPEND;
982548c8f1aSPyun YongHyeon 		break;
983548c8f1aSPyun YongHyeon 	default:
984548c8f1aSPyun YongHyeon 		return;
985548c8f1aSPyun YongHyeon 	}
986548c8f1aSPyun YongHyeon 
987548c8f1aSPyun YongHyeon 	bge_ape_send_event(sc, event | BGE_APE_EVENT_STATUS_DRIVER_EVNT |
988548c8f1aSPyun YongHyeon 	    BGE_APE_EVENT_STATUS_STATE_CHNGE);
989548c8f1aSPyun YongHyeon }
990548c8f1aSPyun YongHyeon 
991548c8f1aSPyun YongHyeon /*
992f41ac2beSBill Paul  * Map a single buffer address.
993f41ac2beSBill Paul  */
994f41ac2beSBill Paul 
995f41ac2beSBill Paul static void
9963f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
997f41ac2beSBill Paul {
998f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
999f41ac2beSBill Paul 
1000f41ac2beSBill Paul 	if (error)
1001f41ac2beSBill Paul 		return;
1002f41ac2beSBill Paul 
10035b610048SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
10045b610048SPyun YongHyeon 
1005f41ac2beSBill Paul 	ctx = arg;
1006f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
1007f41ac2beSBill Paul }
1008f41ac2beSBill Paul 
100938cc658fSJohn Baldwin static uint8_t
101038cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
101138cc658fSJohn Baldwin {
101238cc658fSJohn Baldwin 	uint32_t access, byte = 0;
101338cc658fSJohn Baldwin 	int i;
101438cc658fSJohn Baldwin 
101538cc658fSJohn Baldwin 	/* Lock. */
101638cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
101738cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
101838cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
101938cc658fSJohn Baldwin 			break;
102038cc658fSJohn Baldwin 		DELAY(20);
102138cc658fSJohn Baldwin 	}
102238cc658fSJohn Baldwin 	if (i == 8000)
102338cc658fSJohn Baldwin 		return (1);
102438cc658fSJohn Baldwin 
102538cc658fSJohn Baldwin 	/* Enable access. */
102638cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
102738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
102838cc658fSJohn Baldwin 
102938cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
103038cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
103138cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
103238cc658fSJohn Baldwin 		DELAY(10);
103338cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
103438cc658fSJohn Baldwin 			DELAY(10);
103538cc658fSJohn Baldwin 			break;
103638cc658fSJohn Baldwin 		}
103738cc658fSJohn Baldwin 	}
103838cc658fSJohn Baldwin 
103938cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
104038cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
104138cc658fSJohn Baldwin 		return (1);
104238cc658fSJohn Baldwin 	}
104338cc658fSJohn Baldwin 
104438cc658fSJohn Baldwin 	/* Get result. */
104538cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
104638cc658fSJohn Baldwin 
104738cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
104838cc658fSJohn Baldwin 
104938cc658fSJohn Baldwin 	/* Disable access. */
105038cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
105138cc658fSJohn Baldwin 
105238cc658fSJohn Baldwin 	/* Unlock. */
105338cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
105438cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
105538cc658fSJohn Baldwin 
105638cc658fSJohn Baldwin 	return (0);
105738cc658fSJohn Baldwin }
105838cc658fSJohn Baldwin 
105938cc658fSJohn Baldwin /*
106038cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
106138cc658fSJohn Baldwin  */
106238cc658fSJohn Baldwin static int
106338cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
106438cc658fSJohn Baldwin {
106538cc658fSJohn Baldwin 	int err = 0, i;
106638cc658fSJohn Baldwin 	uint8_t byte = 0;
106738cc658fSJohn Baldwin 
106838cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
106938cc658fSJohn Baldwin 		return (1);
107038cc658fSJohn Baldwin 
107138cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
107238cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
107338cc658fSJohn Baldwin 		if (err)
107438cc658fSJohn Baldwin 			break;
107538cc658fSJohn Baldwin 		*(dest + i) = byte;
107638cc658fSJohn Baldwin 	}
107738cc658fSJohn Baldwin 
107838cc658fSJohn Baldwin 	return (err ? 1 : 0);
107938cc658fSJohn Baldwin }
108038cc658fSJohn Baldwin 
108195d67482SBill Paul /*
108295d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
108395d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
108495d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
108595d67482SBill Paul  * access method.
108695d67482SBill Paul  */
10873f74909aSGleb Smirnoff static uint8_t
10883f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
108995d67482SBill Paul {
109095d67482SBill Paul 	int i;
10913f74909aSGleb Smirnoff 	uint32_t byte = 0;
109295d67482SBill Paul 
109395d67482SBill Paul 	/*
109495d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
109595d67482SBill Paul 	 * having to use the bitbang method.
109695d67482SBill Paul 	 */
109795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
109895d67482SBill Paul 
109995d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
110095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
110195d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
110295d67482SBill Paul 	DELAY(20);
110395d67482SBill Paul 
110495d67482SBill Paul 	/* Issue the read EEPROM command. */
110595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
110695d67482SBill Paul 
110795d67482SBill Paul 	/* Wait for completion */
110895d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
110995d67482SBill Paul 		DELAY(10);
111095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
111195d67482SBill Paul 			break;
111295d67482SBill Paul 	}
111395d67482SBill Paul 
1114d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
1115fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
1116f6789fbaSPyun YongHyeon 		return (1);
111795d67482SBill Paul 	}
111895d67482SBill Paul 
111995d67482SBill Paul 	/* Get result. */
112095d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
112195d67482SBill Paul 
11220c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
112395d67482SBill Paul 
112495d67482SBill Paul 	return (0);
112595d67482SBill Paul }
112695d67482SBill Paul 
112795d67482SBill Paul /*
112895d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
112995d67482SBill Paul  */
113095d67482SBill Paul static int
11313f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
113295d67482SBill Paul {
11333f74909aSGleb Smirnoff 	int i, error = 0;
11343f74909aSGleb Smirnoff 	uint8_t byte = 0;
113595d67482SBill Paul 
113695d67482SBill Paul 	for (i = 0; i < cnt; i++) {
11373f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
11383f74909aSGleb Smirnoff 		if (error)
113995d67482SBill Paul 			break;
114095d67482SBill Paul 		*(dest + i) = byte;
114195d67482SBill Paul 	}
114295d67482SBill Paul 
11433f74909aSGleb Smirnoff 	return (error ? 1 : 0);
114495d67482SBill Paul }
114595d67482SBill Paul 
114695d67482SBill Paul static int
11473f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
114895d67482SBill Paul {
114995d67482SBill Paul 	struct bge_softc *sc;
1150a813ed78SPyun YongHyeon 	uint32_t val;
115195d67482SBill Paul 	int i;
115295d67482SBill Paul 
115395d67482SBill Paul 	sc = device_get_softc(dev);
115495d67482SBill Paul 
1155548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1156548c8f1aSPyun YongHyeon 		return (0);
1157548c8f1aSPyun YongHyeon 
1158a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1159a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1160a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1161a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1162a813ed78SPyun YongHyeon 		DELAY(80);
116337ceeb4dSPaul Saab 	}
116437ceeb4dSPaul Saab 
116595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
116695d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
116795d67482SBill Paul 
1168a813ed78SPyun YongHyeon 	/* Poll for the PHY register access to complete. */
116995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1170d5d23857SJung-uk Kim 		DELAY(10);
117195d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
1172a813ed78SPyun YongHyeon 		if ((val & BGE_MICOMM_BUSY) == 0) {
1173a813ed78SPyun YongHyeon 			DELAY(5);
1174a813ed78SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_MI_COMM);
117595d67482SBill Paul 			break;
117695d67482SBill Paul 		}
1177a813ed78SPyun YongHyeon 	}
117895d67482SBill Paul 
117995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
11805fea260fSMarius Strobl 		device_printf(sc->bge_dev,
11815fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
11825fea260fSMarius Strobl 		    phy, reg, val);
118337ceeb4dSPaul Saab 		val = 0;
118495d67482SBill Paul 	}
118595d67482SBill Paul 
1186a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1187a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1188a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1189a813ed78SPyun YongHyeon 		DELAY(80);
119037ceeb4dSPaul Saab 	}
119137ceeb4dSPaul Saab 
1192548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1193548c8f1aSPyun YongHyeon 
119495d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
119595d67482SBill Paul 		return (0);
119695d67482SBill Paul 
11970c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
119895d67482SBill Paul }
119995d67482SBill Paul 
120095d67482SBill Paul static int
12013f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
120295d67482SBill Paul {
120395d67482SBill Paul 	struct bge_softc *sc;
120495d67482SBill Paul 	int i;
120595d67482SBill Paul 
120695d67482SBill Paul 	sc = device_get_softc(dev);
120795d67482SBill Paul 
120838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
120938cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
121038cc658fSJohn Baldwin 		return (0);
121138cc658fSJohn Baldwin 
1212548c8f1aSPyun YongHyeon 	if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1213548c8f1aSPyun YongHyeon 		return (0);
1214548c8f1aSPyun YongHyeon 
1215a813ed78SPyun YongHyeon 	/* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1216a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1217a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE,
1218a813ed78SPyun YongHyeon 		    sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1219a813ed78SPyun YongHyeon 		DELAY(80);
122037ceeb4dSPaul Saab 	}
122137ceeb4dSPaul Saab 
122295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
122395d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
122495d67482SBill Paul 
122595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1226d5d23857SJung-uk Kim 		DELAY(10);
122738cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
122838cc658fSJohn Baldwin 			DELAY(5);
122938cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
123095d67482SBill Paul 			break;
1231d5d23857SJung-uk Kim 		}
123238cc658fSJohn Baldwin 	}
1233d5d23857SJung-uk Kim 
1234a813ed78SPyun YongHyeon 	/* Restore the autopoll bit if necessary. */
1235a813ed78SPyun YongHyeon 	if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1236a813ed78SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1237a813ed78SPyun YongHyeon 		DELAY(80);
1238a813ed78SPyun YongHyeon 	}
1239a813ed78SPyun YongHyeon 
1240548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1241548c8f1aSPyun YongHyeon 
1242a813ed78SPyun YongHyeon 	if (i == BGE_TIMEOUT)
124338cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
12442246e8c6SPyun YongHyeon 		    "PHY write timed out (phy %d, reg %d, val 0x%04x)\n",
124538cc658fSJohn Baldwin 		    phy, reg, val);
124637ceeb4dSPaul Saab 
124795d67482SBill Paul 	return (0);
124895d67482SBill Paul }
124995d67482SBill Paul 
125095d67482SBill Paul static void
12513f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
125295d67482SBill Paul {
125395d67482SBill Paul 	struct bge_softc *sc;
125495d67482SBill Paul 	struct mii_data *mii;
1255a0a03d1eSPyun YongHyeon 	uint32_t mac_mode, rx_mode, tx_mode;
1256e4146b95SPyun YongHyeon 
125795d67482SBill Paul 	sc = device_get_softc(dev);
1258fba8b109SMarcel Moolenaar 	if ((if_getdrvflags(sc->bge_ifp) & IFF_DRV_RUNNING) == 0)
1259e4146b95SPyun YongHyeon 		return;
126095d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
126195d67482SBill Paul 
1262d4f5240aSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1263d4f5240aSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
1264d4f5240aSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
1265d4f5240aSPyun YongHyeon 		case IFM_10_T:
1266d4f5240aSPyun YongHyeon 		case IFM_100_TX:
1267d4f5240aSPyun YongHyeon 			sc->bge_link = 1;
1268d4f5240aSPyun YongHyeon 			break;
1269d4f5240aSPyun YongHyeon 		case IFM_1000_T:
1270d4f5240aSPyun YongHyeon 		case IFM_1000_SX:
1271d4f5240aSPyun YongHyeon 		case IFM_2500_SX:
1272d4f5240aSPyun YongHyeon 			if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
1273d4f5240aSPyun YongHyeon 				sc->bge_link = 1;
1274d4f5240aSPyun YongHyeon 			else
1275d4f5240aSPyun YongHyeon 				sc->bge_link = 0;
1276d4f5240aSPyun YongHyeon 			break;
1277d4f5240aSPyun YongHyeon 		default:
1278d4f5240aSPyun YongHyeon 			sc->bge_link = 0;
1279d4f5240aSPyun YongHyeon 			break;
1280d4f5240aSPyun YongHyeon 		}
1281d4f5240aSPyun YongHyeon 	} else
1282d4f5240aSPyun YongHyeon 		sc->bge_link = 0;
1283d4f5240aSPyun YongHyeon 	if (sc->bge_link == 0)
1284d4f5240aSPyun YongHyeon 		return;
1285a0a03d1eSPyun YongHyeon 
1286a0a03d1eSPyun YongHyeon 	/*
1287a0a03d1eSPyun YongHyeon 	 * APE firmware touches these registers to keep the MAC
1288a0a03d1eSPyun YongHyeon 	 * connected to the outside world.  Try to keep the
1289a0a03d1eSPyun YongHyeon 	 * accesses atomic.
1290a0a03d1eSPyun YongHyeon 	 */
1291a0a03d1eSPyun YongHyeon 
1292a0a03d1eSPyun YongHyeon 	/* Set the port mode (MII/GMII) to match the link speed. */
1293a0a03d1eSPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) &
1294a0a03d1eSPyun YongHyeon 	    ~(BGE_MACMODE_PORTMODE | BGE_MACMODE_HALF_DUPLEX);
1295a0a03d1eSPyun YongHyeon 	tx_mode = CSR_READ_4(sc, BGE_TX_MODE);
1296a0a03d1eSPyun YongHyeon 	rx_mode = CSR_READ_4(sc, BGE_RX_MODE);
1297a0a03d1eSPyun YongHyeon 
1298ea3b4127SPyun YongHyeon 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
1299ea3b4127SPyun YongHyeon 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
1300a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_GMII;
13013f74909aSGleb Smirnoff 	else
1302a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_PORTMODE_MII;
130395d67482SBill Paul 
1304a0a03d1eSPyun YongHyeon 	/* Set MAC flow control behavior to match link flow control settings. */
1305a0a03d1eSPyun YongHyeon 	tx_mode &= ~BGE_TXMODE_FLOWCTL_ENABLE;
1306a0a03d1eSPyun YongHyeon 	rx_mode &= ~BGE_RXMODE_FLOWCTL_ENABLE;
13074951ca86SPyun YongHyeon 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1308a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1309a0a03d1eSPyun YongHyeon 			tx_mode |= BGE_TXMODE_FLOWCTL_ENABLE;
1310a0a03d1eSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1311a0a03d1eSPyun YongHyeon 			rx_mode |= BGE_RXMODE_FLOWCTL_ENABLE;
1312a0a03d1eSPyun YongHyeon 	} else
1313a0a03d1eSPyun YongHyeon 		mac_mode |= BGE_MACMODE_HALF_DUPLEX;
1314a0a03d1eSPyun YongHyeon 
1315a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, mac_mode);
13169b80ffe7SPyun YongHyeon 	DELAY(40);
1317a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, tx_mode);
1318a0a03d1eSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_RX_MODE, rx_mode);
131995d67482SBill Paul }
132095d67482SBill Paul 
132195d67482SBill Paul /*
132295d67482SBill Paul  * Intialize a standard receive ring descriptor.
132395d67482SBill Paul  */
132495d67482SBill Paul static int
1325943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
132695d67482SBill Paul {
1327943787f3SPyun YongHyeon 	struct mbuf *m;
132895d67482SBill Paul 	struct bge_rx_bd *r;
1329a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
1330943787f3SPyun YongHyeon 	bus_dmamap_t map;
1331a23634a1SPyun YongHyeon 	int error, nsegs;
133295d67482SBill Paul 
1333f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD &&
1334fba8b109SMarcel Moolenaar 	    (if_getmtu(sc->bge_ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
1335f5459d4cSPyun YongHyeon 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))) {
1336c6499eccSGleb Smirnoff 		m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
1337f5459d4cSPyun YongHyeon 		if (m == NULL)
1338f5459d4cSPyun YongHyeon 			return (ENOBUFS);
1339f5459d4cSPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1340f5459d4cSPyun YongHyeon 	} else {
1341c6499eccSGleb Smirnoff 		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1342943787f3SPyun YongHyeon 		if (m == NULL)
134395d67482SBill Paul 			return (ENOBUFS);
1344943787f3SPyun YongHyeon 		m->m_len = m->m_pkthdr.len = MCLBYTES;
1345f5459d4cSPyun YongHyeon 	}
1346652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1347943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
1348943787f3SPyun YongHyeon 
13490ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
1350943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
1351a23634a1SPyun YongHyeon 	if (error != 0) {
1352943787f3SPyun YongHyeon 		m_freem(m);
1353a23634a1SPyun YongHyeon 		return (error);
1354f41ac2beSBill Paul 	}
1355943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1356943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1357943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
1358943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1359943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
1360943787f3SPyun YongHyeon 	}
1361943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
1362943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
1363943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
1364943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
1365e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
1366943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
1367a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
1368a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
1369e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
1370a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
1371e907febfSPyun YongHyeon 	r->bge_idx = i;
1372f41ac2beSBill Paul 
13730ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1374943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
137595d67482SBill Paul 
137695d67482SBill Paul 	return (0);
137795d67482SBill Paul }
137895d67482SBill Paul 
137995d67482SBill Paul /*
138095d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
138195d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
138295d67482SBill Paul  */
138395d67482SBill Paul static int
1384943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
138595d67482SBill Paul {
13861be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
1387943787f3SPyun YongHyeon 	bus_dmamap_t map;
13881be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
1389943787f3SPyun YongHyeon 	struct mbuf *m;
1390943787f3SPyun YongHyeon 	int error, nsegs;
139195d67482SBill Paul 
1392c6499eccSGleb Smirnoff 	MGETHDR(m, M_NOWAIT, MT_DATA);
1393943787f3SPyun YongHyeon 	if (m == NULL)
139495d67482SBill Paul 		return (ENOBUFS);
139595d67482SBill Paul 
13962a8c860fSRobert Watson 	if (m_cljget(m, M_NOWAIT, MJUM9BYTES) == NULL) {
1397943787f3SPyun YongHyeon 		m_freem(m);
139895d67482SBill Paul 		return (ENOBUFS);
139995d67482SBill Paul 	}
1400943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1401652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1402943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
14031be6acb7SGleb Smirnoff 
14041be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1405943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1406943787f3SPyun YongHyeon 	if (error != 0) {
1407943787f3SPyun YongHyeon 		m_freem(m);
14081be6acb7SGleb Smirnoff 		return (error);
1409f7cea149SGleb Smirnoff 	}
14101be6acb7SGleb Smirnoff 
1411aa8cbdbfSMarius Strobl 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1412943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1413943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1414943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1415943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1416943787f3SPyun YongHyeon 	}
1417943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1418943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1419943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1420943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1421943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1422e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1423e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1424e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1425e0b7b101SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1426e0b7b101SPyun YongHyeon 
14271be6acb7SGleb Smirnoff 	/*
14281be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
14291be6acb7SGleb Smirnoff 	 */
1430943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
14314e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
14324e7ba1abSGleb Smirnoff 	r->bge_idx = i;
14334e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
14344e7ba1abSGleb Smirnoff 	switch (nsegs) {
14354e7ba1abSGleb Smirnoff 	case 4:
14364e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
14374e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
14384e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
1439e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
14404e7ba1abSGleb Smirnoff 	case 3:
1441e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1442e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1443e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
1444e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
14454e7ba1abSGleb Smirnoff 	case 2:
14464e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
14474e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
14484e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
1449e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
14504e7ba1abSGleb Smirnoff 	case 1:
14514e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
14524e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
14534e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
1454e0b7b101SPyun YongHyeon 		sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
14554e7ba1abSGleb Smirnoff 		break;
14564e7ba1abSGleb Smirnoff 	default:
14574e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
14584e7ba1abSGleb Smirnoff 	}
1459f41ac2beSBill Paul 
1460a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1461943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
146295d67482SBill Paul 
146395d67482SBill Paul 	return (0);
146495d67482SBill Paul }
146595d67482SBill Paul 
146695d67482SBill Paul static int
14673f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
146895d67482SBill Paul {
14693ee5d7daSPyun YongHyeon 	int error, i;
147095d67482SBill Paul 
1471e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
147203e78bd0SPyun YongHyeon 	sc->bge_std = 0;
1473e0b7b101SPyun YongHyeon 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1474943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
14753ee5d7daSPyun YongHyeon 			return (error);
147603e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
14771888f324SPyun YongHyeon 	}
147895d67482SBill Paul 
1479f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1480d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1481f41ac2beSBill Paul 
1482e0b7b101SPyun YongHyeon 	sc->bge_std = 0;
1483e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
148495d67482SBill Paul 
148595d67482SBill Paul 	return (0);
148695d67482SBill Paul }
148795d67482SBill Paul 
148895d67482SBill Paul static void
14893f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
149095d67482SBill Paul {
149195d67482SBill Paul 	int i;
149295d67482SBill Paul 
149395d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
149495d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
14950ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1496e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1497e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
14980ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1499f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1500e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1501e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
150295d67482SBill Paul 		}
1503f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
150495d67482SBill Paul 		    sizeof(struct bge_rx_bd));
150595d67482SBill Paul 	}
150695d67482SBill Paul }
150795d67482SBill Paul 
150895d67482SBill Paul static int
15093f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
151095d67482SBill Paul {
151195d67482SBill Paul 	struct bge_rcb *rcb;
15123ee5d7daSPyun YongHyeon 	int error, i;
151395d67482SBill Paul 
1514e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
151503e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
151695d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1517943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
15183ee5d7daSPyun YongHyeon 			return (error);
151903e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
15201888f324SPyun YongHyeon 	}
152195d67482SBill Paul 
1522f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1523d77e9fa7SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1524f41ac2beSBill Paul 
1525e0b7b101SPyun YongHyeon 	sc->bge_jumbo = 0;
152695d67482SBill Paul 
15278a315a6dSPyun YongHyeon 	/* Enable the jumbo receive producer ring. */
1528f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
15298a315a6dSPyun YongHyeon 	rcb->bge_maxlen_flags =
15308a315a6dSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
153167111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
153295d67482SBill Paul 
1533e0b7b101SPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
153495d67482SBill Paul 
153595d67482SBill Paul 	return (0);
153695d67482SBill Paul }
153795d67482SBill Paul 
153895d67482SBill Paul static void
15393f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
154095d67482SBill Paul {
154195d67482SBill Paul 	int i;
154295d67482SBill Paul 
154395d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
154495d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1545e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1546e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1547e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1548f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1549f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1550e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1551e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
155295d67482SBill Paul 		}
1553f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
15541be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
155595d67482SBill Paul 	}
155695d67482SBill Paul }
155795d67482SBill Paul 
155895d67482SBill Paul static void
15593f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
156095d67482SBill Paul {
156195d67482SBill Paul 	int i;
156295d67482SBill Paul 
1563f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
156495d67482SBill Paul 		return;
156595d67482SBill Paul 
156695d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
156795d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
15680ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1569e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1570e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
15710ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1572f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1573e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1574e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
157595d67482SBill Paul 		}
1576f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
157795d67482SBill Paul 		    sizeof(struct bge_tx_bd));
157895d67482SBill Paul 	}
157995d67482SBill Paul }
158095d67482SBill Paul 
158195d67482SBill Paul static int
15823f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
158395d67482SBill Paul {
158495d67482SBill Paul 	sc->bge_txcnt = 0;
158595d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
15863927098fSPaul Saab 
1587e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1588e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
15895c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1590e6bf277eSPyun YongHyeon 
159114bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
159214bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
159338cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
159414bbd30fSGleb Smirnoff 
15953927098fSPaul Saab 	/* 5700 b2 errata */
1596e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
159738cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
15983927098fSPaul Saab 
159914bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
160038cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
16013927098fSPaul Saab 	/* 5700 b2 errata */
1602e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
160338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
160495d67482SBill Paul 
160595d67482SBill Paul 	return (0);
160695d67482SBill Paul }
160795d67482SBill Paul 
160895d67482SBill Paul static void
16093e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
16103e9b1bcaSJung-uk Kim {
1611fba8b109SMarcel Moolenaar 	if_t ifp;
16123e9b1bcaSJung-uk Kim 
16133e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
16143e9b1bcaSJung-uk Kim 
16153e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
16163e9b1bcaSJung-uk Kim 
161745ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
1618fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_PROMISC)
161945ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
16203e9b1bcaSJung-uk Kim 	else
162145ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
16223e9b1bcaSJung-uk Kim }
16233e9b1bcaSJung-uk Kim 
16243e9b1bcaSJung-uk Kim static void
16253f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
162695d67482SBill Paul {
1627fba8b109SMarcel Moolenaar 	if_t ifp;
1628fba8b109SMarcel Moolenaar 	int mc_count = 0;
16293f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
1630fba8b109SMarcel Moolenaar 	int h, i, mcnt;
1631fba8b109SMarcel Moolenaar 	unsigned char *mta;
163295d67482SBill Paul 
16330f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
16340f9bd73bSSam Leffler 
1635fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
163695d67482SBill Paul 
1637fba8b109SMarcel Moolenaar 	mc_count = if_multiaddr_count(ifp, -1);
1638fba8b109SMarcel Moolenaar 	mta = malloc(sizeof(unsigned char) *  ETHER_ADDR_LEN *
1639fba8b109SMarcel Moolenaar 	    mc_count, M_DEVBUF, M_NOWAIT);
1640fba8b109SMarcel Moolenaar 
1641fba8b109SMarcel Moolenaar 	if(mta == NULL) {
1642fba8b109SMarcel Moolenaar 		device_printf(sc->bge_dev,
1643fba8b109SMarcel Moolenaar 		    "Failed to allocated temp mcast list\n");
1644fba8b109SMarcel Moolenaar 		return;
1645fba8b109SMarcel Moolenaar 	}
1646fba8b109SMarcel Moolenaar 
1647fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_ALLMULTI || if_getflags(ifp) & IFF_PROMISC) {
164895d67482SBill Paul 		for (i = 0; i < 4; i++)
16490c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
1650049855d8SPedro F. Giffuni 		free(mta, M_DEVBUF);
165195d67482SBill Paul 		return;
165295d67482SBill Paul 	}
165395d67482SBill Paul 
165495d67482SBill Paul 	/* First, zot all the existing filters. */
165595d67482SBill Paul 	for (i = 0; i < 4; i++)
165695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
165795d67482SBill Paul 
1658fba8b109SMarcel Moolenaar 	if_multiaddr_array(ifp, mta, &mcnt, mc_count);
1659fba8b109SMarcel Moolenaar 	for(i = 0; i < mcnt; i++) {
1660a127e581SPeter Wemm 		h = ether_crc32_le(mta + (i * ETHER_ADDR_LEN),
1661a127e581SPeter Wemm 		    ETHER_ADDR_LEN) & 0x7F;
16620c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
166395d67482SBill Paul 	}
166495d67482SBill Paul 
166595d67482SBill Paul 	for (i = 0; i < 4; i++)
166695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1667fba8b109SMarcel Moolenaar 
1668fba8b109SMarcel Moolenaar 	free(mta, M_DEVBUF);
166995d67482SBill Paul }
167095d67482SBill Paul 
16718cb1383cSDoug Ambrisko static void
1672cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1673cb2eacc7SYaroslav Tykhiy {
1674fba8b109SMarcel Moolenaar 	if_t ifp;
1675cb2eacc7SYaroslav Tykhiy 
1676cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1677cb2eacc7SYaroslav Tykhiy 
1678cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1679cb2eacc7SYaroslav Tykhiy 
1680cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1681fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING)
1682cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1683cb2eacc7SYaroslav Tykhiy 	else
1684cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1685cb2eacc7SYaroslav Tykhiy }
1686cb2eacc7SYaroslav Tykhiy 
1687cb2eacc7SYaroslav Tykhiy static void
1688797ab05eSPyun YongHyeon bge_sig_pre_reset(struct bge_softc *sc, int type)
16898cb1383cSDoug Ambrisko {
1690797ab05eSPyun YongHyeon 
16918cb1383cSDoug Ambrisko 	/*
16928cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
16938cb1383cSDoug Ambrisko 	 */
16948cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
1695888b47f0SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
16968cb1383cSDoug Ambrisko 
16978cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
16988cb1383cSDoug Ambrisko 		switch (type) {
16998cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1700224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1701224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
17028cb1383cSDoug Ambrisko 			break;
1703548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1704224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1705224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
17068cb1383cSDoug Ambrisko 			break;
1707548c8f1aSPyun YongHyeon 		case BGE_RESET_SUSPEND:
1708548c8f1aSPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1709548c8f1aSPyun YongHyeon 			    BGE_FW_DRV_STATE_SUSPEND);
1710548c8f1aSPyun YongHyeon 			break;
17118cb1383cSDoug Ambrisko 		}
17128cb1383cSDoug Ambrisko 	}
1713548c8f1aSPyun YongHyeon 
1714548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_START || type == BGE_RESET_SUSPEND)
1715548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
17168cb1383cSDoug Ambrisko }
17178cb1383cSDoug Ambrisko 
17188cb1383cSDoug Ambrisko static void
1719797ab05eSPyun YongHyeon bge_sig_post_reset(struct bge_softc *sc, int type)
17208cb1383cSDoug Ambrisko {
1721797ab05eSPyun YongHyeon 
17228cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
17238cb1383cSDoug Ambrisko 		switch (type) {
17248cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1725224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1726224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START_DONE);
17278cb1383cSDoug Ambrisko 			/* START DONE */
17288cb1383cSDoug Ambrisko 			break;
1729548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1730224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1731224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD_DONE);
17328cb1383cSDoug Ambrisko 			break;
17338cb1383cSDoug Ambrisko 		}
17348cb1383cSDoug Ambrisko 	}
1735548c8f1aSPyun YongHyeon 	if (type == BGE_RESET_SHUTDOWN)
1736548c8f1aSPyun YongHyeon 		bge_ape_driver_state_change(sc, type);
17378cb1383cSDoug Ambrisko }
17388cb1383cSDoug Ambrisko 
17398cb1383cSDoug Ambrisko static void
1740797ab05eSPyun YongHyeon bge_sig_legacy(struct bge_softc *sc, int type)
17418cb1383cSDoug Ambrisko {
1742797ab05eSPyun YongHyeon 
17438cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17448cb1383cSDoug Ambrisko 		switch (type) {
17458cb1383cSDoug Ambrisko 		case BGE_RESET_START:
1746224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1747224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_START);
17488cb1383cSDoug Ambrisko 			break;
1749548c8f1aSPyun YongHyeon 		case BGE_RESET_SHUTDOWN:
1750224f8785SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1751224f8785SPyun YongHyeon 			    BGE_FW_DRV_STATE_UNLOAD);
17528cb1383cSDoug Ambrisko 			break;
17538cb1383cSDoug Ambrisko 		}
17548cb1383cSDoug Ambrisko 	}
17558cb1383cSDoug Ambrisko }
17568cb1383cSDoug Ambrisko 
1757797ab05eSPyun YongHyeon static void
1758797ab05eSPyun YongHyeon bge_stop_fw(struct bge_softc *sc)
17598cb1383cSDoug Ambrisko {
17608cb1383cSDoug Ambrisko 	int i;
17618cb1383cSDoug Ambrisko 
17628cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
17633c201200SPyun YongHyeon 		bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB, BGE_FW_CMD_PAUSE);
17643fed2d5dSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
17659931ba85SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RX_CPU_EVENT) | BGE_RX_CPU_DRV_EVENT);
17668cb1383cSDoug Ambrisko 
17678cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
17689931ba85SPyun YongHyeon 			if (!(CSR_READ_4(sc, BGE_RX_CPU_EVENT) &
17699931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT))
17708cb1383cSDoug Ambrisko 				break;
17718cb1383cSDoug Ambrisko 			DELAY(10);
17728cb1383cSDoug Ambrisko 		}
17738cb1383cSDoug Ambrisko 	}
17748cb1383cSDoug Ambrisko }
17758cb1383cSDoug Ambrisko 
177650515680SPyun YongHyeon static uint32_t
177750515680SPyun YongHyeon bge_dma_swap_options(struct bge_softc *sc)
177850515680SPyun YongHyeon {
177950515680SPyun YongHyeon 	uint32_t dma_options;
178050515680SPyun YongHyeon 
178150515680SPyun YongHyeon 	dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
178250515680SPyun YongHyeon 	    BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
178350515680SPyun YongHyeon #if BYTE_ORDER == BIG_ENDIAN
178450515680SPyun YongHyeon 	dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
178550515680SPyun YongHyeon #endif
178650515680SPyun YongHyeon 	return (dma_options);
178750515680SPyun YongHyeon }
178850515680SPyun YongHyeon 
178995d67482SBill Paul /*
1790c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
179195d67482SBill Paul  */
179295d67482SBill Paul static int
17933f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
179495d67482SBill Paul {
179550515680SPyun YongHyeon 	uint32_t dma_rw_ctl, misc_ctl, mode_ctl;
1796fbc374afSPyun YongHyeon 	uint16_t val;
179795d67482SBill Paul 	int i;
179895d67482SBill Paul 
17998cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
18001108273aSPyun YongHyeon 	misc_ctl = BGE_INIT;
18011108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
18021108273aSPyun YongHyeon 		misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
18031108273aSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
180495d67482SBill Paul 
180595d67482SBill Paul 	/*
180695d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
180795d67482SBill Paul 	 * internal memory.
180895d67482SBill Paul 	 */
180995d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
18103f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
181195d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
181295d67482SBill Paul 
181395d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
18143f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
181595d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
181695d67482SBill Paul 
1817fbc374afSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1818fbc374afSPyun YongHyeon 		/*
1819d896b3feSPyun YongHyeon 		 *  Fix data corruption caused by non-qword write with WB.
1820fbc374afSPyun YongHyeon 		 *  Fix master abort in PCI mode.
1821fbc374afSPyun YongHyeon 		 *  Fix PCI latency timer.
1822fbc374afSPyun YongHyeon 		 */
1823fbc374afSPyun YongHyeon 		val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1824fbc374afSPyun YongHyeon 		val |= (1 << 10) | (1 << 12) | (1 << 13);
1825fbc374afSPyun YongHyeon 		pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1826fbc374afSPyun YongHyeon 	}
1827fbc374afSPyun YongHyeon 
1828f8bb33c3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM57765 ||
1829f8bb33c3SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57766) {
1830f8bb33c3SPyun YongHyeon 		/*
1831f8bb33c3SPyun YongHyeon 		 * For the 57766 and non Ax versions of 57765, bootcode
1832f8bb33c3SPyun YongHyeon 		 * needs to setup the PCIE Fast Training Sequence (FTS)
1833f8bb33c3SPyun YongHyeon 		 * value to prevent transmit hangs.
1834f8bb33c3SPyun YongHyeon 		 */
1835f8bb33c3SPyun YongHyeon 		if (sc->bge_chiprev != BGE_CHIPREV_57765_AX) {
1836f8bb33c3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_CPMU_PADRNG_CTL,
1837f8bb33c3SPyun YongHyeon 			    CSR_READ_4(sc, BGE_CPMU_PADRNG_CTL) |
1838f8bb33c3SPyun YongHyeon 			    BGE_CPMU_PADRNG_CTL_RDIV2);
1839f8bb33c3SPyun YongHyeon 		}
1840f8bb33c3SPyun YongHyeon 	}
1841f8bb33c3SPyun YongHyeon 
1842186f842bSJung-uk Kim 	/*
1843186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1844186f842bSJung-uk Kim 	 */
1845186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1846186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1847652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
184848630d79SPyun YongHyeon 		if (sc->bge_mps >= 256)
184948630d79SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
185048630d79SPyun YongHyeon 		else
1851186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1852652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
18534c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1854186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1855186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1856186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1857186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1858186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1859186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1860cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1861cbb2b2feSPyun YongHyeon 			/*
1862cbb2b2feSPyun YongHyeon 			 * In the BCM5703, the DMA read watermark should
1863cbb2b2feSPyun YongHyeon 			 * be set to less than or equal to the maximum
1864cbb2b2feSPyun YongHyeon 			 * memory read byte count of the PCI-X command
1865cbb2b2feSPyun YongHyeon 			 * register.
1866cbb2b2feSPyun YongHyeon 			 */
1867cbb2b2feSPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1868cbb2b2feSPyun YongHyeon 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1869186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1870186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1871186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1872186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1873186f842bSJung-uk Kim 		} else {
1874186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1875186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1876186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
18770c8aa4eaSJung-uk Kim 			    0x0F;
1878186f842bSJung-uk Kim 		}
1879e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1880e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
18813f74909aSGleb Smirnoff 			uint32_t tmp;
18825cba12d3SPaul Saab 
1883186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
18840c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1885186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1886186f842bSJung-uk Kim 				dma_rw_ctl |=
1887186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
18885cba12d3SPaul Saab 
1889186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1890186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1891186f842bSJung-uk Kim 		}
1892186f842bSJung-uk Kim 	} else {
1893186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1894186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1895186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1896186f842bSJung-uk Kim 
1897186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1898186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1899186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1900186f842bSJung-uk Kim 	}
1901186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1902186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1903186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1904186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1905e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1906186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
19075cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1908b4a256acSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
19091108273aSPyun YongHyeon 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1910b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
1911b4a256acSPyun YongHyeon 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1912bbe2ca75SPyun YongHyeon 		/*
1913bbe2ca75SPyun YongHyeon 		 * Enable HW workaround for controllers that misinterpret
1914bbe2ca75SPyun YongHyeon 		 * a status tag update and leave interrupts permanently
1915bbe2ca75SPyun YongHyeon 		 * disabled.
1916bbe2ca75SPyun YongHyeon 		 */
19172927f01fSPyun YongHyeon 		if (!BGE_IS_57765_PLUS(sc) &&
19182927f01fSPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
19192927f01fSPyun YongHyeon 		    sc->bge_asicrev != BGE_ASICREV_BCM5762)
1920bbe2ca75SPyun YongHyeon 			dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1921b4a256acSPyun YongHyeon 	}
19225cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
192395d67482SBill Paul 
192495d67482SBill Paul 	/*
192595d67482SBill Paul 	 * Set up general mode register.
192695d67482SBill Paul 	 */
1927548c8f1aSPyun YongHyeon 	mode_ctl = bge_dma_swap_options(sc);
19282927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
19292927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
1930548c8f1aSPyun YongHyeon 		/* Retain Host-2-BMC settings written by APE firmware. */
1931548c8f1aSPyun YongHyeon 		mode_ctl |= CSR_READ_4(sc, BGE_MODE_CTL) &
1932548c8f1aSPyun YongHyeon 		    (BGE_MODECTL_BYTESWAP_B2HRX_DATA |
1933548c8f1aSPyun YongHyeon 		    BGE_MODECTL_WORDSWAP_B2HRX_DATA |
1934548c8f1aSPyun YongHyeon 		    BGE_MODECTL_B2HRX_ENABLE | BGE_MODECTL_HTX2B_ENABLE);
1935548c8f1aSPyun YongHyeon 	}
1936548c8f1aSPyun YongHyeon 	mode_ctl |= BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1937548c8f1aSPyun YongHyeon 	    BGE_MODECTL_TX_NO_PHDR_CSUM;
193895d67482SBill Paul 
193995d67482SBill Paul 	/*
194090447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
194190447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
194290447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
194390447aadSMarius Strobl 	 * certain bridges.
194490447aadSMarius Strobl 	 */
194590447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
194690447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
194750515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_FORCE_PCI32;
194890447aadSMarius Strobl 
194990447aadSMarius Strobl 	/*
19508cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
19518cb1383cSDoug Ambrisko 	 */
19528cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
195350515680SPyun YongHyeon 		mode_ctl |= BGE_MODECTL_STACKUP;
195450515680SPyun YongHyeon 
195550515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
19568cb1383cSDoug Ambrisko 
19578cb1383cSDoug Ambrisko 	/*
1958ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
195940438c47SMarius Strobl 	 * properly by these devices.
196095d67482SBill Paul 	 */
196140438c47SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
196295d67482SBill Paul 
1963d7acafa1SMarius Strobl 	/* Set the timer prescaler (always 66 MHz). */
19640c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
196595d67482SBill Paul 
196638cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
196738cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
196838cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
196938cc658fSJohn Baldwin 
197038cc658fSJohn Baldwin 		/* Put PHY into ready state */
197138cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
197238cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
197338cc658fSJohn Baldwin 		DELAY(40);
197438cc658fSJohn Baldwin 	}
197538cc658fSJohn Baldwin 
197695d67482SBill Paul 	return (0);
197795d67482SBill Paul }
197895d67482SBill Paul 
197995d67482SBill Paul static int
19803f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
198195d67482SBill Paul {
198295d67482SBill Paul 	struct bge_rcb *rcb;
1983e907febfSPyun YongHyeon 	bus_size_t vrcb;
1984e907febfSPyun YongHyeon 	bge_hostaddr taddr;
19852927f01fSPyun YongHyeon 	uint32_t dmactl, rdmareg, val;
19868a315a6dSPyun YongHyeon 	int i, limit;
198795d67482SBill Paul 
198895d67482SBill Paul 	/*
198995d67482SBill Paul 	 * Initialize the memory window pointer register so that
199095d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
199195d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
199295d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
199395d67482SBill Paul 	 */
199495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
199595d67482SBill Paul 
1996822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1997822f63fcSBill Paul 
19987ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
199995d67482SBill Paul 		/* Configure mbuf memory pool */
20000dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
2001822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
2002822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
2003822f63fcSBill Paul 		else
200495d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
200595d67482SBill Paul 
200695d67482SBill Paul 		/* Configure DMA resource pool */
20070434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
20080434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
200995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
20100434d1b8SBill Paul 	}
201195d67482SBill Paul 
201295d67482SBill Paul 	/* Configure mbuf pool watermarks */
201350515680SPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
20141108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
2015fba8b109SMarcel Moolenaar 		if (if_getmtu(sc->bge_ifp) > ETHERMTU) {
20161108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
20171108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
20181108273aSPyun YongHyeon 		} else {
20191108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
20201108273aSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
20211108273aSPyun YongHyeon 		}
20221108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc)) {
2023fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
2024fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
2025fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
202638cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
202738cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
202838cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
202938cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
203038cc658fSJohn Baldwin 	} else {
203138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
203238cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
203338cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
203438cc658fSJohn Baldwin 	}
203595d67482SBill Paul 
203695d67482SBill Paul 	/* Configure DMA resource watermarks */
203795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
203895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
203995d67482SBill Paul 
204095d67482SBill Paul 	/* Enable buffer manager */
2041bbe2ca75SPyun YongHyeon 	val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
2042bbe2ca75SPyun YongHyeon 	/*
2043bbe2ca75SPyun YongHyeon 	 * Change the arbitration algorithm of TXMBUF read request to
2044bbe2ca75SPyun YongHyeon 	 * round-robin instead of priority based for BCM5719.  When
2045bbe2ca75SPyun YongHyeon 	 * TXFIFO is almost empty, RDMA will hold its request until
2046bbe2ca75SPyun YongHyeon 	 * TXFIFO is not almost empty.
2047bbe2ca75SPyun YongHyeon 	 */
2048bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
2049bbe2ca75SPyun YongHyeon 		val |= BGE_BMANMODE_NO_TX_UNDERRUN;
2050bbe2ca75SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
205195d67482SBill Paul 
205295d67482SBill Paul 	/* Poll for buffer manager start indication */
205395d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2054d5d23857SJung-uk Kim 		DELAY(10);
20550c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
205695d67482SBill Paul 			break;
205795d67482SBill Paul 	}
205895d67482SBill Paul 
205995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
20605a147ba6SPyun YongHyeon 		device_printf(sc->bge_dev, "buffer manager failed to start\n");
206195d67482SBill Paul 		return (ENXIO);
206295d67482SBill Paul 	}
206395d67482SBill Paul 
206495d67482SBill Paul 	/* Enable flow-through queues */
20650c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
206695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
206795d67482SBill Paul 
206895d67482SBill Paul 	/* Wait until queue initialization is complete */
206995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2070d5d23857SJung-uk Kim 		DELAY(10);
207195d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
207295d67482SBill Paul 			break;
207395d67482SBill Paul 	}
207495d67482SBill Paul 
207595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2076fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
207795d67482SBill Paul 		return (ENXIO);
207895d67482SBill Paul 	}
207995d67482SBill Paul 
20808a315a6dSPyun YongHyeon 	/*
20818a315a6dSPyun YongHyeon 	 * Summary of rings supported by the controller:
20828a315a6dSPyun YongHyeon 	 *
20838a315a6dSPyun YongHyeon 	 * Standard Receive Producer Ring
20848a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "standard"
20858a315a6dSPyun YongHyeon 	 *   sized frames (typically 1536 bytes) to the controller.
20868a315a6dSPyun YongHyeon 	 *
20878a315a6dSPyun YongHyeon 	 * Jumbo Receive Producer Ring
20888a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for jumbo sized
20898a315a6dSPyun YongHyeon 	 *   frames (i.e. anything bigger than the "standard" frames)
20908a315a6dSPyun YongHyeon 	 *   to the controller.
20918a315a6dSPyun YongHyeon 	 *
20928a315a6dSPyun YongHyeon 	 * Mini Receive Producer Ring
20938a315a6dSPyun YongHyeon 	 * - This ring is used to feed receive buffers for "mini"
20948a315a6dSPyun YongHyeon 	 *   sized frames to the controller.
20958a315a6dSPyun YongHyeon 	 * - This feature required external memory for the controller
20968a315a6dSPyun YongHyeon 	 *   but was never used in a production system.  Should always
20978a315a6dSPyun YongHyeon 	 *   be disabled.
20988a315a6dSPyun YongHyeon 	 *
20998a315a6dSPyun YongHyeon 	 * Receive Return Ring
21008a315a6dSPyun YongHyeon 	 * - After the controller has placed an incoming frame into a
21018a315a6dSPyun YongHyeon 	 *   receive buffer that buffer is moved into a receive return
21028a315a6dSPyun YongHyeon 	 *   ring.  The driver is then responsible to passing the
21038a315a6dSPyun YongHyeon 	 *   buffer up to the stack.  Many versions of the controller
21048a315a6dSPyun YongHyeon 	 *   support multiple RR rings.
21058a315a6dSPyun YongHyeon 	 *
21068a315a6dSPyun YongHyeon 	 * Send Ring
21078a315a6dSPyun YongHyeon 	 * - This ring is used for outgoing frames.  Many versions of
21088a315a6dSPyun YongHyeon 	 *   the controller support multiple send rings.
21098a315a6dSPyun YongHyeon 	 */
21108a315a6dSPyun YongHyeon 
21118a315a6dSPyun YongHyeon 	/* Initialize the standard receive producer ring control block. */
2112f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
2113f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
2114f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
2115f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
2116f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
2117f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2118f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
21191108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
21201108273aSPyun YongHyeon 		/*
21211108273aSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
21221108273aSPyun YongHyeon 		 * Bits 15-2 : Maximum RX frame size
21231108273aSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring ENabled
21241108273aSPyun YongHyeon 		 * Bit 0     : Reserved
21251108273aSPyun YongHyeon 		 */
21261108273aSPyun YongHyeon 		rcb->bge_maxlen_flags =
21271108273aSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
21281108273aSPyun YongHyeon 	} else if (BGE_IS_5705_PLUS(sc)) {
21298a315a6dSPyun YongHyeon 		/*
21308a315a6dSPyun YongHyeon 		 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
21318a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
21328a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
21338a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
21348a315a6dSPyun YongHyeon 		 */
21350434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
21368a315a6dSPyun YongHyeon 	} else {
21378a315a6dSPyun YongHyeon 		/*
21388a315a6dSPyun YongHyeon 		 * Ring size is always XXX entries
21398a315a6dSPyun YongHyeon 		 * Bits 31-16: Maximum RX frame size
21408a315a6dSPyun YongHyeon 		 * Bits 15-2 : Reserved (should be 0)
21418a315a6dSPyun YongHyeon 		 * Bit 1     : 1 = Ring Disabled, 0 = Ring Enabled
21428a315a6dSPyun YongHyeon 		 * Bit 0     : Reserved
21438a315a6dSPyun YongHyeon 		 */
21440434d1b8SBill Paul 		rcb->bge_maxlen_flags =
21450434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
21468a315a6dSPyun YongHyeon 	}
2147bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
214850515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
214950515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21501108273aSPyun YongHyeon 		rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
21511108273aSPyun YongHyeon 	else
215295d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
21538a315a6dSPyun YongHyeon 	/* Write the standard receive producer ring control block. */
21540c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
21550c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
215667111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
215767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
215895d67482SBill Paul 
21598a315a6dSPyun YongHyeon 	/* Reset the standard receive producer ring producer index. */
21608a315a6dSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
21618a315a6dSPyun YongHyeon 
216295d67482SBill Paul 	/*
21638a315a6dSPyun YongHyeon 	 * Initialize the jumbo RX producer ring control
21648a315a6dSPyun YongHyeon 	 * block.  We set the 'ring disabled' bit in the
21658a315a6dSPyun YongHyeon 	 * flags field until we're actually ready to start
216695d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
216795d67482SBill Paul 	 * high enough to require it).
216895d67482SBill Paul 	 */
21694c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2170f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
21718a315a6dSPyun YongHyeon 		/* Get the jumbo receive producer ring RCB parameters. */
2172f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
2173f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2174f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
2175f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2176f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2177f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2178f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
21791be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
21801be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
2181bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
218250515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
218350515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
21841108273aSPyun YongHyeon 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
21851108273aSPyun YongHyeon 		else
218695d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
218767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
218867111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
218967111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
219067111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
21918a315a6dSPyun YongHyeon 		/* Program the jumbo receive producer ring RCB parameters. */
21920434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
21930434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
219467111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
21958a315a6dSPyun YongHyeon 		/* Reset the jumbo receive producer ring producer index. */
21968a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
21978a315a6dSPyun YongHyeon 	}
219895d67482SBill Paul 
21998a315a6dSPyun YongHyeon 	/* Disable the mini receive producer ring RCB. */
22005e2f96bfSPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc)) {
2201f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
220267111612SJohn Polstra 		rcb->bge_maxlen_flags =
220367111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
22040434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
22050434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
22068a315a6dSPyun YongHyeon 		/* Reset the mini receive producer ring producer index. */
22078a315a6dSPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
22080434d1b8SBill Paul 	}
220995d67482SBill Paul 
2210ca4f8986SPyun YongHyeon 	/* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
2211ca4f8986SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2212427d3f33SPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
2213427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
2214427d3f33SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
22158d5f7181SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
22168d5f7181SPyun YongHyeon 			    (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
2217ca4f8986SPyun YongHyeon 	}
221895d67482SBill Paul 	/*
22198a315a6dSPyun YongHyeon 	 * The BD ring replenish thresholds control how often the
22208a315a6dSPyun YongHyeon 	 * hardware fetches new BD's from the producer rings in host
22218a315a6dSPyun YongHyeon 	 * memory.  Setting the value too low on a busy system can
22228a315a6dSPyun YongHyeon 	 * starve the hardware and recue the throughpout.
22238a315a6dSPyun YongHyeon 	 *
222495d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
222595d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
222695d67482SBill Paul 	 * each ring.
22279ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
22289ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
22299ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
22309ba784dbSScott Long 	 * are reports that it might not need to be so strict.
223138cc658fSJohn Baldwin 	 *
223238cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
223338cc658fSJohn Baldwin 	 * well.
223495d67482SBill Paul 	 */
22355345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
22366f8718a3SScott Long 		val = 8;
22376f8718a3SScott Long 	else
22386f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
22396f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
22402a141b94SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc))
22412a141b94SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
22422a141b94SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT/8);
22431108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
22441108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
22451108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
22461108273aSPyun YongHyeon 	}
224795d67482SBill Paul 
224895d67482SBill Paul 	/*
22498a315a6dSPyun YongHyeon 	 * Disable all send rings by setting the 'ring disabled' bit
22508a315a6dSPyun YongHyeon 	 * in the flags field of all the TX send ring control blocks,
22518a315a6dSPyun YongHyeon 	 * located in NIC memory.
225295d67482SBill Paul 	 */
22538a315a6dSPyun YongHyeon 	if (!BGE_IS_5705_PLUS(sc))
22548a315a6dSPyun YongHyeon 		/* 5700 to 5704 had 16 send rings. */
22558a315a6dSPyun YongHyeon 		limit = BGE_TX_RINGS_EXTSSRAM_MAX;
22562927f01fSPyun YongHyeon 	else if (BGE_IS_57765_PLUS(sc) ||
22572927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762)
22582927f01fSPyun YongHyeon 		limit = 2;
22592927f01fSPyun YongHyeon 	else if (BGE_IS_5717_PLUS(sc))
22602927f01fSPyun YongHyeon 		limit = 4;
22618a315a6dSPyun YongHyeon 	else
22628a315a6dSPyun YongHyeon 		limit = 1;
2263e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
22648a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2265e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2266e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
2267e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2268e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
226995d67482SBill Paul 	}
227095d67482SBill Paul 
22718a315a6dSPyun YongHyeon 	/* Configure send ring RCB 0 (we use only the first ring) */
2272e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
2273e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
2274e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2275e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2276bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
227750515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
227850515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720)
22791108273aSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
22801108273aSPyun YongHyeon 	else
2281e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr,
2282e907febfSPyun YongHyeon 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
2283e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2284e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
228595d67482SBill Paul 
22868a315a6dSPyun YongHyeon 	/*
22878a315a6dSPyun YongHyeon 	 * Disable all receive return rings by setting the
22888a315a6dSPyun YongHyeon 	 * 'ring diabled' bit in the flags field of all the receive
22898a315a6dSPyun YongHyeon 	 * return ring control blocks, located in NIC memory.
22908a315a6dSPyun YongHyeon 	 */
2291bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
229250515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
229350515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
22941108273aSPyun YongHyeon 		/* Should be 17, use 16 until we get an SRAM map. */
22951108273aSPyun YongHyeon 		limit = 16;
22961108273aSPyun YongHyeon 	} else if (!BGE_IS_5705_PLUS(sc))
22978a315a6dSPyun YongHyeon 		limit = BGE_RX_RINGS_MAX;
2298b4a256acSPyun YongHyeon 	else if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
22992927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762 ||
2300fe26ad88SPyun YongHyeon 	    BGE_IS_57765_PLUS(sc))
23018a315a6dSPyun YongHyeon 		limit = 4;
23028a315a6dSPyun YongHyeon 	else
23038a315a6dSPyun YongHyeon 		limit = 1;
23048a315a6dSPyun YongHyeon 	/* Disable all receive return rings. */
2305e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
23068a315a6dSPyun YongHyeon 	for (i = 0; i < limit; i++) {
2307e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
2308e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
2309e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
23108a315a6dSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED);
2311e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
231238cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
23133f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
2314e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
231595d67482SBill Paul 	}
231695d67482SBill Paul 
231795d67482SBill Paul 	/*
23188a315a6dSPyun YongHyeon 	 * Set up receive return ring 0.  Note that the NIC address
23198a315a6dSPyun YongHyeon 	 * for RX return rings is 0x0.  The return rings live entirely
23208a315a6dSPyun YongHyeon 	 * within the host, so the nicaddr field in the RCB isn't used.
232195d67482SBill Paul 	 */
2322e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2323e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
2324e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2325e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
23268a315a6dSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2327e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2328e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
232995d67482SBill Paul 
233095d67482SBill Paul 	/* Set random backoff seed for TX */
233195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
23320a2cc827SPyun YongHyeon 	    (IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
23334a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
23340a2cc827SPyun YongHyeon 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5]) &
233595d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
233695d67482SBill Paul 
233795d67482SBill Paul 	/* Set inter-packet gap */
233850515680SPyun YongHyeon 	val = 0x2620;
23392927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
23402927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762)
234150515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
234250515680SPyun YongHyeon 		    (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
234350515680SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
234495d67482SBill Paul 
234595d67482SBill Paul 	/*
234695d67482SBill Paul 	 * Specify which ring to use for packets that don't match
234795d67482SBill Paul 	 * any RX rules.
234895d67482SBill Paul 	 */
234995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
235095d67482SBill Paul 
235195d67482SBill Paul 	/*
235295d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
235395d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
235495d67482SBill Paul 	 */
235595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
235695d67482SBill Paul 
235795d67482SBill Paul 	/* Inialize RX list placement stats mask. */
23580c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
235995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
236095d67482SBill Paul 
236195d67482SBill Paul 	/* Disable host coalescing until we get it set up */
236295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
236395d67482SBill Paul 
236495d67482SBill Paul 	/* Poll to make sure it's shut down. */
236595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2366d5d23857SJung-uk Kim 		DELAY(10);
236795d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
236895d67482SBill Paul 			break;
236995d67482SBill Paul 	}
237095d67482SBill Paul 
237195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2372fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2373fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
237495d67482SBill Paul 		return (ENXIO);
237595d67482SBill Paul 	}
237695d67482SBill Paul 
237795d67482SBill Paul 	/* Set up host coalescing defaults */
237895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
237995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
238095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
238195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
23827ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
238395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
238495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
23850434d1b8SBill Paul 	}
2386b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
2387b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
238895d67482SBill Paul 
238995d67482SBill Paul 	/* Set up address of statistics block */
23907ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
2391f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
2392f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
239395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
2394f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
23950434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
239695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
23970434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
23980434d1b8SBill Paul 	}
23990434d1b8SBill Paul 
24000434d1b8SBill Paul 	/* Set up address of status block */
2401f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
2402f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
240395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
2404f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
240595d67482SBill Paul 
240630f57f61SPyun YongHyeon 	/* Set up status block size. */
240730f57f61SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2408864104feSPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
240930f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_FULL;
2410864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2411864104feSPyun YongHyeon 	} else {
241230f57f61SPyun YongHyeon 		val = BGE_STATBLKSZ_32BYTE;
2413864104feSPyun YongHyeon 		bzero(sc->bge_ldata.bge_status_block, 32);
2414864104feSPyun YongHyeon 	}
2415864104feSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2416864104feSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
2417864104feSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
241830f57f61SPyun YongHyeon 
241995d67482SBill Paul 	/* Turn on host coalescing state machine */
242030f57f61SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
242195d67482SBill Paul 
242295d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
242395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
242495d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
242595d67482SBill Paul 
242695d67482SBill Paul 	/* Turn on RX list placement state machine */
242795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
242895d67482SBill Paul 
242995d67482SBill Paul 	/* Turn on RX list selector state machine. */
24307ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
243195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
243295d67482SBill Paul 
24332246e8c6SPyun YongHyeon 	/* Turn on DMA, clear stats. */
2434ea3b4127SPyun YongHyeon 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2435ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2436ea3b4127SPyun YongHyeon 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2437ea3b4127SPyun YongHyeon 	    BGE_MACMODE_FRMHDR_DMA_ENB;
2438ea3b4127SPyun YongHyeon 
2439ea3b4127SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
2440ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_TBI;
2441ea3b4127SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
2442ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_GMII;
2443ea3b4127SPyun YongHyeon 	else
2444ea3b4127SPyun YongHyeon 		val |= BGE_PORTMODE_MII;
2445ea3b4127SPyun YongHyeon 
2446548c8f1aSPyun YongHyeon 	/* Allow APE to send/receive frames. */
2447548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
2448548c8f1aSPyun YongHyeon 		val |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
2449548c8f1aSPyun YongHyeon 
2450ea3b4127SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
24519b80ffe7SPyun YongHyeon 	DELAY(40);
245295d67482SBill Paul 
245395d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
245491bd90d8SPyun YongHyeon 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
245595d67482SBill Paul 
245695d67482SBill Paul #ifdef notdef
245795d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
245895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
245995d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
246095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
246195d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
246295d67482SBill Paul #endif
246395d67482SBill Paul 
246495d67482SBill Paul 	/* Turn on DMA completion state machine */
24657ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
246695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
246795d67482SBill Paul 
24686f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
24696f8718a3SScott Long 
24706f8718a3SScott Long 	/* Enable host coalescing bug fix. */
2471a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
24723889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
24736f8718a3SScott Long 
24747aa4b937SPyun YongHyeon 	/* Request larger DMA burst size to get better performance. */
24757aa4b937SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
24767aa4b937SPyun YongHyeon 		val |= BGE_WDMAMODE_BURST_ALL_DATA;
24777aa4b937SPyun YongHyeon 
247895d67482SBill Paul 	/* Turn on write DMA state machine */
24796f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
24804f09c4c7SMarius Strobl 	DELAY(40);
248195d67482SBill Paul 
248295d67482SBill Paul 	/* Turn on read DMA state machine */
24834f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
24841108273aSPyun YongHyeon 
24851108273aSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
24861108273aSPyun YongHyeon 		val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
24871108273aSPyun YongHyeon 
2488a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2489a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2490a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
2491a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2492a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2493a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
24944f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
24954f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
24961108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2497ca3f1187SPyun YongHyeon 		val |= BGE_RDMAMODE_TSO4_ENABLE;
24981108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_TSO3 ||
24991108273aSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
250055a24a05SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM57780)
250155a24a05SPyun YongHyeon 			val |= BGE_RDMAMODE_TSO6_ENABLE;
250255a24a05SPyun YongHyeon 	}
250350515680SPyun YongHyeon 
25042927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
25052927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
250650515680SPyun YongHyeon 		val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
250750515680SPyun YongHyeon 			BGE_RDMAMODE_H2BNC_VLAN_DET;
2508e3215f76SPyun YongHyeon 		/*
2509e3215f76SPyun YongHyeon 		 * Allow multiple outstanding read requests from
2510e3215f76SPyun YongHyeon 		 * non-LSO read DMA engine.
2511e3215f76SPyun YongHyeon 		 */
2512e3215f76SPyun YongHyeon 		val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
2513e3215f76SPyun YongHyeon 	}
251450515680SPyun YongHyeon 
2515d255f2a9SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2516d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2517d255f2a9SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
25181108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
25192927f01fSPyun YongHyeon 	    BGE_IS_5717_PLUS(sc) || BGE_IS_57765_PLUS(sc)) {
25202927f01fSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
25212927f01fSPyun YongHyeon 			rdmareg = BGE_RDMA_RSRVCTRL_REG2;
25222927f01fSPyun YongHyeon 		else
25232927f01fSPyun YongHyeon 			rdmareg = BGE_RDMA_RSRVCTRL;
25242927f01fSPyun YongHyeon 		dmactl = CSR_READ_4(sc, rdmareg);
2525bbe2ca75SPyun YongHyeon 		/*
2526bbe2ca75SPyun YongHyeon 		 * Adjust tx margin to prevent TX data corruption and
2527bbe2ca75SPyun YongHyeon 		 * fix internal FIFO overflow.
2528bbe2ca75SPyun YongHyeon 		 */
25292927f01fSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM5719_A0 ||
25302927f01fSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
2531bbe2ca75SPyun YongHyeon 			dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
2532bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
2533bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
2534bbe2ca75SPyun YongHyeon 			dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
2535bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
2536bbe2ca75SPyun YongHyeon 			    BGE_RDMA_RSRVCTRL_TXMRGN_320B;
2537bbe2ca75SPyun YongHyeon 		}
2538d255f2a9SPyun YongHyeon 		/*
2539d255f2a9SPyun YongHyeon 		 * Enable fix for read DMA FIFO overruns.
2540d255f2a9SPyun YongHyeon 		 * The fix is to limit the number of RX BDs
2541d255f2a9SPyun YongHyeon 		 * the hardware would fetch at a fime.
2542d255f2a9SPyun YongHyeon 		 */
25432927f01fSPyun YongHyeon 		CSR_WRITE_4(sc, rdmareg, dmactl |
2544d255f2a9SPyun YongHyeon 		    BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2545d255f2a9SPyun YongHyeon 	}
2546bbe2ca75SPyun YongHyeon 
2547e3215f76SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719) {
2548bbe2ca75SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2549bbe2ca75SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2550bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2551bbe2ca75SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2552e3215f76SPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2553e3215f76SPyun YongHyeon 		/*
2554e3215f76SPyun YongHyeon 		 * Allow 4KB burst length reads for non-LSO frames.
2555e3215f76SPyun YongHyeon 		 * Enable 512B burst length reads for buffer descriptors.
2556e3215f76SPyun YongHyeon 		 */
2557e3215f76SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2558e3215f76SPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2559e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
2560e3215f76SPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
25612927f01fSPyun YongHyeon 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5762) {
25622927f01fSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2,
25632927f01fSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2) |
25642927f01fSPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
25652927f01fSPyun YongHyeon 		    BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2566bbe2ca75SPyun YongHyeon 	}
2567bbe2ca75SPyun YongHyeon 
25684f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
25694f09c4c7SMarius Strobl 	DELAY(40);
257095d67482SBill Paul 
257129b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
257229b44b09SPyun YongHyeon 		for (i = 0; i < BGE_NUM_RDMA_CHANNELS / 2; i++) {
257329b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LENGTH + i * 4);
257429b44b09SPyun YongHyeon 			if ((val & 0xFFFF) > BGE_FRAMELEN)
257529b44b09SPyun YongHyeon 				break;
257629b44b09SPyun YongHyeon 			if (((val >> 16) & 0xFFFF) > BGE_FRAMELEN)
257729b44b09SPyun YongHyeon 				break;
257829b44b09SPyun YongHyeon 		}
257929b44b09SPyun YongHyeon 		if (i != BGE_NUM_RDMA_CHANNELS / 2) {
258029b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
258129b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
258229b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5719;
258329b44b09SPyun YongHyeon 			else
258429b44b09SPyun YongHyeon 				val |= BGE_RDMA_TX_LENGTH_WA_5720;
258529b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
258629b44b09SPyun YongHyeon 		}
258729b44b09SPyun YongHyeon 	}
258829b44b09SPyun YongHyeon 
258995d67482SBill Paul 	/* Turn on RX data completion state machine */
259095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
259195d67482SBill Paul 
259295d67482SBill Paul 	/* Turn on RX BD initiator state machine */
259395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
259495d67482SBill Paul 
259595d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
259695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
259795d67482SBill Paul 
259895d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
25997ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
260095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
260195d67482SBill Paul 
260295d67482SBill Paul 	/* Turn on send BD completion state machine */
260395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
260495d67482SBill Paul 
260595d67482SBill Paul 	/* Turn on send data completion state machine */
2606a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
2607a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2608a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
2609a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
261095d67482SBill Paul 
261195d67482SBill Paul 	/* Turn on send data initiator state machine */
26121108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
26131108273aSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
26141108273aSPyun YongHyeon 		    BGE_SDIMODE_HW_LSO_PRE_DMA);
2615ca3f1187SPyun YongHyeon 	else
261695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
261795d67482SBill Paul 
261895d67482SBill Paul 	/* Turn on send BD initiator state machine */
261995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
262095d67482SBill Paul 
262195d67482SBill Paul 	/* Turn on send BD selector state machine */
262295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
262395d67482SBill Paul 
26240c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
262595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
262695d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
262795d67482SBill Paul 
262895d67482SBill Paul 	/* ack/clear link change events */
262995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
26300434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
26310434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
2632f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
263395d67482SBill Paul 
26346ede2cfaSPyun YongHyeon 	/*
26356ede2cfaSPyun YongHyeon 	 * Enable attention when the link has changed state for
26366ede2cfaSPyun YongHyeon 	 * devices that use auto polling.
26376ede2cfaSPyun YongHyeon 	 */
2638652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
263995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2640a1d52896SBill Paul 	} else {
26417ed3f0f0SPyun YongHyeon 		if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
26427ed3f0f0SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
26437ed3f0f0SPyun YongHyeon 			DELAY(80);
26447ed3f0f0SPyun YongHyeon 		}
26451f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
26464c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2647a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2648a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
2649a1d52896SBill Paul 	}
265095d67482SBill Paul 
26511f313773SOleg Bulyzhin 	/*
26521f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
26531f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
26541f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
26551f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
26561f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
26571f313773SOleg Bulyzhin 	 */
26581f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
26591f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
26601f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
26611f313773SOleg Bulyzhin 
266295d67482SBill Paul 	/* Enable link state change attentions. */
266395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
266495d67482SBill Paul 
266595d67482SBill Paul 	return (0);
266695d67482SBill Paul }
266795d67482SBill Paul 
2668d7acafa1SMarius Strobl static const struct bge_revision *
26694c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
26704c0da0ffSGleb Smirnoff {
26714c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
26724c0da0ffSGleb Smirnoff 
26734c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
26744c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
26754c0da0ffSGleb Smirnoff 			return (br);
26764c0da0ffSGleb Smirnoff 	}
26774c0da0ffSGleb Smirnoff 
26784c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
26794c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
26804c0da0ffSGleb Smirnoff 			return (br);
26814c0da0ffSGleb Smirnoff 	}
26824c0da0ffSGleb Smirnoff 
26834c0da0ffSGleb Smirnoff 	return (NULL);
26844c0da0ffSGleb Smirnoff }
26854c0da0ffSGleb Smirnoff 
2686d7acafa1SMarius Strobl static const struct bge_vendor *
26874c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
26884c0da0ffSGleb Smirnoff {
26894c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
26904c0da0ffSGleb Smirnoff 
26914c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
26924c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
26934c0da0ffSGleb Smirnoff 			return (v);
26944c0da0ffSGleb Smirnoff 
26954c0da0ffSGleb Smirnoff 	return (NULL);
26964c0da0ffSGleb Smirnoff }
26974c0da0ffSGleb Smirnoff 
2698d7acafa1SMarius Strobl static uint32_t
2699d7acafa1SMarius Strobl bge_chipid(device_t dev)
270095d67482SBill Paul {
2701978f2704SMarius Strobl 	uint32_t id;
270295d67482SBill Paul 
2703a5779553SStanislav Sedov 	id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2704a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
27051108273aSPyun YongHyeon 	if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
27061108273aSPyun YongHyeon 		/*
2707d7acafa1SMarius Strobl 		 * Find the ASCI revision.  Different chips use different
2708d7acafa1SMarius Strobl 		 * registers.
27091108273aSPyun YongHyeon 		 */
27101108273aSPyun YongHyeon 		switch (pci_get_device(dev)) {
2711cb2404b4SSepherosa Ziehau 		case BCOM_DEVICEID_BCM5717C:
27121ca32af8SSepherosa Ziehau 			/* 5717 C0 seems to belong to 5720 line. */
27131ca32af8SSepherosa Ziehau 			id = BGE_CHIPID_BCM5720_A0;
27141ca32af8SSepherosa Ziehau 			break;
27151ca32af8SSepherosa Ziehau 		case BCOM_DEVICEID_BCM5717:
27161108273aSPyun YongHyeon 		case BCOM_DEVICEID_BCM5718:
2717bbe2ca75SPyun YongHyeon 		case BCOM_DEVICEID_BCM5719:
271850515680SPyun YongHyeon 		case BCOM_DEVICEID_BCM5720:
27192927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5725:
27202927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5727:
27212927f01fSPyun YongHyeon 		case BCOM_DEVICEID_BCM5762:
272267129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57764:
272367129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57767:
272467129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57787:
27251108273aSPyun YongHyeon 			id = pci_read_config(dev,
27261108273aSPyun YongHyeon 			    BGE_PCI_GEN2_PRODID_ASICREV, 4);
27271108273aSPyun YongHyeon 			break;
2728b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57761:
2729fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57762:
2730b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57765:
2731fe26ad88SPyun YongHyeon 		case BCOM_DEVICEID_BCM57766:
2732b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57781:
273367129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57782:
2734b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57785:
273567129934SPyun YongHyeon 		case BCOM_DEVICEID_BCM57786:
2736b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57791:
2737b4a256acSPyun YongHyeon 		case BCOM_DEVICEID_BCM57795:
2738b4a256acSPyun YongHyeon 			id = pci_read_config(dev,
2739b4a256acSPyun YongHyeon 			    BGE_PCI_GEN15_PRODID_ASICREV, 4);
2740b4a256acSPyun YongHyeon 			break;
27411108273aSPyun YongHyeon 		default:
2742d7acafa1SMarius Strobl 			id = pci_read_config(dev, BGE_PCI_PRODID_ASICREV, 4);
27431108273aSPyun YongHyeon 		}
27441108273aSPyun YongHyeon 	}
2745d7acafa1SMarius Strobl 	return (id);
2746d7acafa1SMarius Strobl }
2747d7acafa1SMarius Strobl 
2748d7acafa1SMarius Strobl /*
2749d7acafa1SMarius Strobl  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
2750d7acafa1SMarius Strobl  * against our list and return its name if we find a match.
2751d7acafa1SMarius Strobl  *
2752d7acafa1SMarius Strobl  * Note that since the Broadcom controller contains VPD support, we
2753d7acafa1SMarius Strobl  * try to get the device name string from the controller itself instead
2754d7acafa1SMarius Strobl  * of the compiled-in string. It guarantees we'll always announce the
2755d7acafa1SMarius Strobl  * right product name. We fall back to the compiled-in string when
2756d7acafa1SMarius Strobl  * VPD is unavailable or corrupt.
2757d7acafa1SMarius Strobl  */
2758d7acafa1SMarius Strobl static int
2759d7acafa1SMarius Strobl bge_probe(device_t dev)
2760d7acafa1SMarius Strobl {
2761d7acafa1SMarius Strobl 	char buf[96];
2762d7acafa1SMarius Strobl 	char model[64];
2763d7acafa1SMarius Strobl 	const struct bge_revision *br;
2764d7acafa1SMarius Strobl 	const char *pname;
2765d7acafa1SMarius Strobl 	struct bge_softc *sc;
2766d7acafa1SMarius Strobl 	const struct bge_type *t = bge_devs;
2767d7acafa1SMarius Strobl 	const struct bge_vendor *v;
2768d7acafa1SMarius Strobl 	uint32_t id;
2769d7acafa1SMarius Strobl 	uint16_t did, vid;
2770d7acafa1SMarius Strobl 
2771d7acafa1SMarius Strobl 	sc = device_get_softc(dev);
2772d7acafa1SMarius Strobl 	sc->bge_dev = dev;
2773d7acafa1SMarius Strobl 	vid = pci_get_vendor(dev);
2774d7acafa1SMarius Strobl 	did = pci_get_device(dev);
2775d7acafa1SMarius Strobl 	while(t->bge_vid != 0) {
2776d7acafa1SMarius Strobl 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
2777d7acafa1SMarius Strobl 			id = bge_chipid(dev);
27784c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
2779852c67f9SMarius Strobl 			if (bge_has_eaddr(sc) &&
2780852c67f9SMarius Strobl 			    pci_get_vpd_ident(dev, &pname) == 0)
2781d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s", pname);
2782d7acafa1SMarius Strobl 			else {
2783d7acafa1SMarius Strobl 				v = bge_lookup_vendor(vid);
2784d7acafa1SMarius Strobl 				snprintf(model, sizeof(model), "%s %s",
2785d7acafa1SMarius Strobl 				    v != NULL ? v->v_name : "Unknown",
27867c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
27872ad1b396SMarius Strobl 				    "NetXtreme/NetLink Ethernet Controller");
2788d7acafa1SMarius Strobl 			}
2789d7acafa1SMarius Strobl 			snprintf(buf, sizeof(buf), "%s, %sASIC rev. %#08x",
2790d7acafa1SMarius Strobl 			    model, br != NULL ? "" : "unknown ", id);
27914c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
2792d7acafa1SMarius Strobl 			return (BUS_PROBE_DEFAULT);
279395d67482SBill Paul 		}
279495d67482SBill Paul 		t++;
279595d67482SBill Paul 	}
279695d67482SBill Paul 
279795d67482SBill Paul 	return (ENXIO);
279895d67482SBill Paul }
279995d67482SBill Paul 
2800f41ac2beSBill Paul static void
28013f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
2802f41ac2beSBill Paul {
2803f41ac2beSBill Paul 	int i;
2804f41ac2beSBill Paul 
28053f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
2806f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2807f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
28080ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2809f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
2810f41ac2beSBill Paul 	}
2811943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
2812943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2813943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
2814f41ac2beSBill Paul 
28153f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
2816f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2817f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2818f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2819f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2820f41ac2beSBill Paul 	}
2821943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2822943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2823943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2824f41ac2beSBill Paul 
28253f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2826f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2827f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
28280ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2829f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2830f41ac2beSBill Paul 	}
2831f41ac2beSBill Paul 
28320ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
28330ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2834c0220d81SPyun YongHyeon 	if (sc->bge_cdata.bge_mtag_jumbo)
2835c0220d81SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
28360ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
28370ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2838f41ac2beSBill Paul 
28393f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2840068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_std_ring_paddr)
2841e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2842e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2843068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_std_ring)
2844f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2845f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2846f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2847f41ac2beSBill Paul 
2848f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2849f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2850f41ac2beSBill Paul 
28513f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2852068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_jumbo_ring_paddr)
2853e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2854e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2855e65bed95SPyun YongHyeon 
2856068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_jumbo_ring)
2857f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2858f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2859f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2860f41ac2beSBill Paul 
2861f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2862f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2863f41ac2beSBill Paul 
28643f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2865068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_return_ring_paddr)
2866e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2867e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2868e65bed95SPyun YongHyeon 
2869068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_rx_return_ring)
2870f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2871f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2872f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2873f41ac2beSBill Paul 
2874f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2875f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2876f41ac2beSBill Paul 
28773f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2878068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_tx_ring_paddr)
2879e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2880e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2881e65bed95SPyun YongHyeon 
2882068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_tx_ring)
2883f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2884f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2885f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2886f41ac2beSBill Paul 
2887f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2888f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2889f41ac2beSBill Paul 
28903f74909aSGleb Smirnoff 	/* Destroy status block. */
2891068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_status_block_paddr)
2892e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2893e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2894e65bed95SPyun YongHyeon 
2895068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_status_block)
2896f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2897f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2898f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2899f41ac2beSBill Paul 
2900f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2901f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2902f41ac2beSBill Paul 
29033f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2904068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_stats_paddr)
2905e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2906e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2907e65bed95SPyun YongHyeon 
2908068d8643SJohn Baldwin 	if (sc->bge_ldata.bge_stats)
2909f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2910f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2911f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2912f41ac2beSBill Paul 
2913f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2914f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2915f41ac2beSBill Paul 
29165b610048SPyun YongHyeon 	if (sc->bge_cdata.bge_buffer_tag)
29175b610048SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
29185b610048SPyun YongHyeon 
29193f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2920f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2921f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2922f41ac2beSBill Paul }
2923f41ac2beSBill Paul 
2924f41ac2beSBill Paul static int
29255b610048SPyun YongHyeon bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
29265b610048SPyun YongHyeon     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
29275b610048SPyun YongHyeon     bus_addr_t *paddr, const char *msg)
2928f41ac2beSBill Paul {
29293f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
29305b610048SPyun YongHyeon 	int error;
2931f41ac2beSBill Paul 
29325b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2933fdd45796SPyun YongHyeon 	    alignment, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
29345b610048SPyun YongHyeon 	    NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
29355b610048SPyun YongHyeon 	if (error != 0) {
29365b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29375b610048SPyun YongHyeon 		    "could not create %s dma tag\n", msg);
29385b610048SPyun YongHyeon 		return (ENOMEM);
29395b610048SPyun YongHyeon 	}
29405b610048SPyun YongHyeon 	/* Allocate DMA'able memory for ring. */
29415b610048SPyun YongHyeon 	error = bus_dmamem_alloc(*tag, (void **)ring,
29425b610048SPyun YongHyeon 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
29435b610048SPyun YongHyeon 	if (error != 0) {
29445b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29455b610048SPyun YongHyeon 		    "could not allocate DMA'able memory for %s\n", msg);
29465b610048SPyun YongHyeon 		return (ENOMEM);
29475b610048SPyun YongHyeon 	}
29485b610048SPyun YongHyeon 	/* Load the address of the ring. */
29495b610048SPyun YongHyeon 	ctx.bge_busaddr = 0;
29505b610048SPyun YongHyeon 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
29515b610048SPyun YongHyeon 	    &ctx, BUS_DMA_NOWAIT);
29525b610048SPyun YongHyeon 	if (error != 0) {
29535b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
29545b610048SPyun YongHyeon 		    "could not load DMA'able memory for %s\n", msg);
29555b610048SPyun YongHyeon 		return (ENOMEM);
29565b610048SPyun YongHyeon 	}
29575b610048SPyun YongHyeon 	*paddr = ctx.bge_busaddr;
29585b610048SPyun YongHyeon 	return (0);
29595b610048SPyun YongHyeon }
29605b610048SPyun YongHyeon 
29615b610048SPyun YongHyeon static int
29625b610048SPyun YongHyeon bge_dma_alloc(struct bge_softc *sc)
29635b610048SPyun YongHyeon {
29645b610048SPyun YongHyeon 	bus_addr_t lowaddr;
2965fdd45796SPyun YongHyeon 	bus_size_t rxmaxsegsz, sbsz, txsegsz, txmaxsegsz;
29665b610048SPyun YongHyeon 	int i, error;
2967f41ac2beSBill Paul 
2968f681b29aSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
2969f681b29aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2970f681b29aSPyun YongHyeon 		lowaddr = BGE_DMA_MAXADDR;
2971f41ac2beSBill Paul 	/*
2972f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2973f41ac2beSBill Paul 	 */
29744eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2975f681b29aSPyun YongHyeon 	    1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
29764eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
29774eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2978e65bed95SPyun YongHyeon 	if (error != 0) {
2979fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2980fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2981e65bed95SPyun YongHyeon 		return (ENOMEM);
2982e65bed95SPyun YongHyeon 	}
2983e65bed95SPyun YongHyeon 
29845b610048SPyun YongHyeon 	/* Create tag for standard RX ring. */
29855b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
29865b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_tag,
29875b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
29885b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_ring_map,
29895b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
29905b610048SPyun YongHyeon 	if (error)
29915b610048SPyun YongHyeon 		return (error);
29925b610048SPyun YongHyeon 
29935b610048SPyun YongHyeon 	/* Create tag for RX return ring. */
29945b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
29955b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_tag,
29965b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
29975b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_return_ring_map,
29985b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
29995b610048SPyun YongHyeon 	if (error)
30005b610048SPyun YongHyeon 		return (error);
30015b610048SPyun YongHyeon 
30025b610048SPyun YongHyeon 	/* Create tag for TX ring. */
30035b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
30045b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_tag,
30055b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_tx_ring,
30065b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_ring_map,
30075b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
30085b610048SPyun YongHyeon 	if (error)
30095b610048SPyun YongHyeon 		return (error);
30105b610048SPyun YongHyeon 
3011f41ac2beSBill Paul 	/*
30125b610048SPyun YongHyeon 	 * Create tag for status block.
30135b610048SPyun YongHyeon 	 * Because we only use single Tx/Rx/Rx return ring, use
30145b610048SPyun YongHyeon 	 * minimum status block size except BCM5700 AX/BX which
30155b610048SPyun YongHyeon 	 * seems to want to see full status block size regardless
30165b610048SPyun YongHyeon 	 * of configured number of ring.
3017f41ac2beSBill Paul 	 */
30185b610048SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
30195b610048SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
30205b610048SPyun YongHyeon 		sbsz = BGE_STATUS_BLK_SZ;
30215b610048SPyun YongHyeon 	else
30225b610048SPyun YongHyeon 		sbsz = 32;
30235b610048SPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
30245b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_tag,
30255b610048SPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_status_block,
30265b610048SPyun YongHyeon 	    &sc->bge_cdata.bge_status_map,
30275b610048SPyun YongHyeon 	    &sc->bge_ldata.bge_status_block_paddr, "status block");
30285b610048SPyun YongHyeon 	if (error)
30295b610048SPyun YongHyeon 		return (error);
30305b610048SPyun YongHyeon 
303112c65daeSPyun YongHyeon 	/* Create tag for statistics block. */
303212c65daeSPyun YongHyeon 	error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
303312c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_tag,
303412c65daeSPyun YongHyeon 	    (uint8_t **)&sc->bge_ldata.bge_stats,
303512c65daeSPyun YongHyeon 	    &sc->bge_cdata.bge_stats_map,
303612c65daeSPyun YongHyeon 	    &sc->bge_ldata.bge_stats_paddr, "statistics block");
303712c65daeSPyun YongHyeon 	if (error)
303812c65daeSPyun YongHyeon 		return (error);
303912c65daeSPyun YongHyeon 
30405b610048SPyun YongHyeon 	/* Create tag for jumbo RX ring. */
30415b610048SPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
30425b610048SPyun YongHyeon 		error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
30435b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_tag,
30445b610048SPyun YongHyeon 		    (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
30455b610048SPyun YongHyeon 		    &sc->bge_cdata.bge_rx_jumbo_ring_map,
30465b610048SPyun YongHyeon 		    &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
30475b610048SPyun YongHyeon 		if (error)
30485b610048SPyun YongHyeon 			return (error);
30495b610048SPyun YongHyeon 	}
30505b610048SPyun YongHyeon 
30515b610048SPyun YongHyeon 	/* Create parent tag for buffers. */
3052d2ffe15aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
3053d2ffe15aSPyun YongHyeon 		/*
3054d2ffe15aSPyun YongHyeon 		 * XXX
3055d2ffe15aSPyun YongHyeon 		 * watchdog timeout issue was observed on BCM5704 which
3056d2ffe15aSPyun YongHyeon 		 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
3057062af0b0SPyun YongHyeon 		 * Both limiting DMA address space to 32bits and flushing
3058062af0b0SPyun YongHyeon 		 * mailbox write seem to address the issue.
3059d2ffe15aSPyun YongHyeon 		 */
3060062af0b0SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3061d2ffe15aSPyun YongHyeon 			lowaddr = BUS_SPACE_MAXADDR_32BIT;
3062d2ffe15aSPyun YongHyeon 	}
3063fdd45796SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev), 1, 0, lowaddr,
3064fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 0,
3065fdd45796SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
3066fdd45796SPyun YongHyeon 	    &sc->bge_cdata.bge_buffer_tag);
30675b610048SPyun YongHyeon 	if (error != 0) {
30685b610048SPyun YongHyeon 		device_printf(sc->bge_dev,
30695b610048SPyun YongHyeon 		    "could not allocate buffer dma tag\n");
30705b610048SPyun YongHyeon 		return (ENOMEM);
30715b610048SPyun YongHyeon 	}
30725b610048SPyun YongHyeon 	/* Create tag for Tx mbufs. */
30731108273aSPyun YongHyeon 	if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
3074ca3f1187SPyun YongHyeon 		txsegsz = BGE_TSOSEG_SZ;
3075ca3f1187SPyun YongHyeon 		txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
3076ca3f1187SPyun YongHyeon 	} else {
3077ca3f1187SPyun YongHyeon 		txsegsz = MCLBYTES;
3078ca3f1187SPyun YongHyeon 		txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
3079ca3f1187SPyun YongHyeon 	}
30805b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
3081ca3f1187SPyun YongHyeon 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
3082ca3f1187SPyun YongHyeon 	    txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
3083ca3f1187SPyun YongHyeon 	    &sc->bge_cdata.bge_tx_mtag);
3084f41ac2beSBill Paul 
3085f41ac2beSBill Paul 	if (error) {
30860ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
30870ac56796SPyun YongHyeon 		return (ENOMEM);
30880ac56796SPyun YongHyeon 	}
30890ac56796SPyun YongHyeon 
30905b610048SPyun YongHyeon 	/* Create tag for Rx mbufs. */
3091f5459d4cSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
3092f5459d4cSPyun YongHyeon 		rxmaxsegsz = MJUM9BYTES;
3093f5459d4cSPyun YongHyeon 	else
3094f5459d4cSPyun YongHyeon 		rxmaxsegsz = MCLBYTES;
30955b610048SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
3096f5459d4cSPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
3097f5459d4cSPyun YongHyeon 	    rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
30980ac56796SPyun YongHyeon 
30990ac56796SPyun YongHyeon 	if (error) {
31000ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
3101f41ac2beSBill Paul 		return (ENOMEM);
3102f41ac2beSBill Paul 	}
3103f41ac2beSBill Paul 
31043f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
3105943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3106943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
3107943787f3SPyun YongHyeon 	if (error) {
3108943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
3109943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
3110943787f3SPyun YongHyeon 		return (ENOMEM);
3111943787f3SPyun YongHyeon 	}
3112f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
31130ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3114f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
3115f41ac2beSBill Paul 		if (error) {
3116fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
3117fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
3118f41ac2beSBill Paul 			return (ENOMEM);
3119f41ac2beSBill Paul 		}
3120f41ac2beSBill Paul 	}
3121f41ac2beSBill Paul 
31223f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
3123f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
31240ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
3125f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
3126f41ac2beSBill Paul 		if (error) {
3127fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
31280ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
3129f41ac2beSBill Paul 			return (ENOMEM);
3130f41ac2beSBill Paul 		}
3131f41ac2beSBill Paul 	}
3132f41ac2beSBill Paul 
31335b610048SPyun YongHyeon 	/* Create tags for jumbo RX buffers. */
31344c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
31355b610048SPyun YongHyeon 		error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
31368a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
31371be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
31381be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
3139f41ac2beSBill Paul 		if (error) {
3140fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
31413f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
3142f41ac2beSBill Paul 			return (ENOMEM);
3143f41ac2beSBill Paul 		}
31443f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
3145943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3146943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
3147943787f3SPyun YongHyeon 		if (error) {
3148943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
31491b90d0bdSPyun YongHyeon 			    "can't create spare DMA map for jumbo RX\n");
3150943787f3SPyun YongHyeon 			return (ENOMEM);
3151943787f3SPyun YongHyeon 		}
3152f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
3153f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3154f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
3155f41ac2beSBill Paul 			if (error) {
3156fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
31573f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
3158f41ac2beSBill Paul 				return (ENOMEM);
3159f41ac2beSBill Paul 			}
3160f41ac2beSBill Paul 		}
3161f41ac2beSBill Paul 	}
3162f41ac2beSBill Paul 
3163f41ac2beSBill Paul 	return (0);
3164f41ac2beSBill Paul }
3165f41ac2beSBill Paul 
3166bf6ef57aSJohn Polstra /*
3167bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
3168bf6ef57aSJohn Polstra  */
3169bf6ef57aSJohn Polstra static int
3170bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
3171bf6ef57aSJohn Polstra {
3172bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
317355aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
3174bf6ef57aSJohn Polstra 
317555aaf894SMarius Strobl 	d = pci_get_domain(dev);
3176bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
3177bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
3178bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
3179bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
318055aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
3181bf6ef57aSJohn Polstra 			return (1);
3182bf6ef57aSJohn Polstra 	return (0);
3183bf6ef57aSJohn Polstra }
3184bf6ef57aSJohn Polstra 
3185bf6ef57aSJohn Polstra /*
3186bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
3187bf6ef57aSJohn Polstra  */
3188bf6ef57aSJohn Polstra static int
3189bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
3190bf6ef57aSJohn Polstra {
3191bf6ef57aSJohn Polstra 	int can_use_msi = 0;
3192bf6ef57aSJohn Polstra 
3193d9fc28e4SPyun YongHyeon 	if (sc->bge_msi == 0)
31945c952e8dSPyun YongHyeon 		return (0);
31955c952e8dSPyun YongHyeon 
31961108273aSPyun YongHyeon 	/* Disable MSI for polling(4). */
31971108273aSPyun YongHyeon #ifdef DEVICE_POLLING
31981108273aSPyun YongHyeon 	return (0);
31991108273aSPyun YongHyeon #endif
3200bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
3201a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
3202bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
3203bf6ef57aSJohn Polstra 		/*
3204a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
3205a8376f70SMarius Strobl 		 * configured in single-port mode.
3206bf6ef57aSJohn Polstra 		 */
3207bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
3208bf6ef57aSJohn Polstra 			can_use_msi = 1;
3209bf6ef57aSJohn Polstra 		break;
3210bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
3211bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
3212bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
3213bf6ef57aSJohn Polstra 			can_use_msi = 1;
3214bf6ef57aSJohn Polstra 		break;
3215*91c69b97SEugene Grosbein 	case BGE_ASICREV_BCM5784:
3216*91c69b97SEugene Grosbein 		/*
3217*91c69b97SEugene Grosbein 		 * Prevent infinite "watchdog timeout" errors
3218*91c69b97SEugene Grosbein 		 * in some MacBook Pro and make it work out-of-the-box.
3219*91c69b97SEugene Grosbein 		 */
3220*91c69b97SEugene Grosbein 		if (sc->bge_chiprev == BGE_CHIPREV_5784_AX)
3221*91c69b97SEugene Grosbein 			break;
3222*91c69b97SEugene Grosbein 		/* FALLTHROUGH */
3223a8376f70SMarius Strobl 	default:
3224a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
3225bf6ef57aSJohn Polstra 			can_use_msi = 1;
3226bf6ef57aSJohn Polstra 	}
3227bf6ef57aSJohn Polstra 	return (can_use_msi);
3228bf6ef57aSJohn Polstra }
3229bf6ef57aSJohn Polstra 
323095d67482SBill Paul static int
3231062af0b0SPyun YongHyeon bge_mbox_reorder(struct bge_softc *sc)
3232062af0b0SPyun YongHyeon {
3233062af0b0SPyun YongHyeon 	/* Lists of PCI bridges that are known to reorder mailbox writes. */
3234062af0b0SPyun YongHyeon 	static const struct mbox_reorder {
3235062af0b0SPyun YongHyeon 		const uint16_t vendor;
3236062af0b0SPyun YongHyeon 		const uint16_t device;
3237062af0b0SPyun YongHyeon 		const char *desc;
323829658c96SDimitry Andric 	} mbox_reorder_lists[] = {
3239062af0b0SPyun YongHyeon 		{ 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
3240062af0b0SPyun YongHyeon 	};
3241062af0b0SPyun YongHyeon 	devclass_t pci, pcib;
3242062af0b0SPyun YongHyeon 	device_t bus, dev;
324347f4a4dcSMarius Strobl 	int i;
3244062af0b0SPyun YongHyeon 
3245062af0b0SPyun YongHyeon 	pci = devclass_find("pci");
3246062af0b0SPyun YongHyeon 	pcib = devclass_find("pcib");
3247062af0b0SPyun YongHyeon 	dev = sc->bge_dev;
3248062af0b0SPyun YongHyeon 	bus = device_get_parent(dev);
3249062af0b0SPyun YongHyeon 	for (;;) {
3250062af0b0SPyun YongHyeon 		dev = device_get_parent(bus);
3251062af0b0SPyun YongHyeon 		bus = device_get_parent(dev);
3252062af0b0SPyun YongHyeon 		if (device_get_devclass(dev) != pcib)
3253062af0b0SPyun YongHyeon 			break;
325447f4a4dcSMarius Strobl 		for (i = 0; i < nitems(mbox_reorder_lists); i++) {
3255062af0b0SPyun YongHyeon 			if (pci_get_vendor(dev) ==
3256062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].vendor &&
3257062af0b0SPyun YongHyeon 			    pci_get_device(dev) ==
3258062af0b0SPyun YongHyeon 			    mbox_reorder_lists[i].device) {
3259062af0b0SPyun YongHyeon 				device_printf(sc->bge_dev,
3260062af0b0SPyun YongHyeon 				    "enabling MBOX workaround for %s\n",
3261062af0b0SPyun YongHyeon 				    mbox_reorder_lists[i].desc);
3262062af0b0SPyun YongHyeon 				return (1);
3263062af0b0SPyun YongHyeon 			}
3264062af0b0SPyun YongHyeon 		}
3265062af0b0SPyun YongHyeon 		if (device_get_devclass(bus) != pci)
3266062af0b0SPyun YongHyeon 			break;
3267062af0b0SPyun YongHyeon 	}
3268062af0b0SPyun YongHyeon 	return (0);
3269062af0b0SPyun YongHyeon }
3270062af0b0SPyun YongHyeon 
3271ea9c3a30SPyun YongHyeon static void
3272ea9c3a30SPyun YongHyeon bge_devinfo(struct bge_softc *sc)
3273ea9c3a30SPyun YongHyeon {
3274ea9c3a30SPyun YongHyeon 	uint32_t cfg, clk;
3275ea9c3a30SPyun YongHyeon 
3276ea9c3a30SPyun YongHyeon 	device_printf(sc->bge_dev,
3277ea9c3a30SPyun YongHyeon 	    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; ",
3278ea9c3a30SPyun YongHyeon 	    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev);
3279ea9c3a30SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
3280ea9c3a30SPyun YongHyeon 		printf("PCI-E\n");
3281ea9c3a30SPyun YongHyeon 	else if (sc->bge_flags & BGE_FLAG_PCIX) {
3282ea9c3a30SPyun YongHyeon 		printf("PCI-X ");
3283ea9c3a30SPyun YongHyeon 		cfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3284ea9c3a30SPyun YongHyeon 		if (cfg == BGE_MISCCFG_BOARD_ID_5704CIOBE)
3285ea9c3a30SPyun YongHyeon 			clk = 133;
3286ea9c3a30SPyun YongHyeon 		else {
3287ea9c3a30SPyun YongHyeon 			clk = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
3288ea9c3a30SPyun YongHyeon 			switch (clk) {
3289ea9c3a30SPyun YongHyeon 			case 0:
3290ea9c3a30SPyun YongHyeon 				clk = 33;
3291ea9c3a30SPyun YongHyeon 				break;
3292ea9c3a30SPyun YongHyeon 			case 2:
3293ea9c3a30SPyun YongHyeon 				clk = 50;
3294ea9c3a30SPyun YongHyeon 				break;
3295ea9c3a30SPyun YongHyeon 			case 4:
3296ea9c3a30SPyun YongHyeon 				clk = 66;
3297ea9c3a30SPyun YongHyeon 				break;
3298ea9c3a30SPyun YongHyeon 			case 6:
3299ea9c3a30SPyun YongHyeon 				clk = 100;
3300ea9c3a30SPyun YongHyeon 				break;
3301ea9c3a30SPyun YongHyeon 			case 7:
3302ea9c3a30SPyun YongHyeon 				clk = 133;
3303ea9c3a30SPyun YongHyeon 				break;
3304ea9c3a30SPyun YongHyeon 			}
3305ea9c3a30SPyun YongHyeon 		}
3306ea9c3a30SPyun YongHyeon 		printf("%u MHz\n", clk);
3307ea9c3a30SPyun YongHyeon 	} else {
3308ea9c3a30SPyun YongHyeon 		if (sc->bge_pcixcap != 0)
3309ea9c3a30SPyun YongHyeon 			printf("PCI on PCI-X ");
3310ea9c3a30SPyun YongHyeon 		else
3311ea9c3a30SPyun YongHyeon 			printf("PCI ");
3312ea9c3a30SPyun YongHyeon 		cfg = pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4);
3313ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_PCI_BUSSPEED)
3314ea9c3a30SPyun YongHyeon 			clk = 66;
3315ea9c3a30SPyun YongHyeon 		else
3316ea9c3a30SPyun YongHyeon 			clk = 33;
3317ea9c3a30SPyun YongHyeon 		if (cfg & BGE_PCISTATE_32BIT_BUS)
3318ea9c3a30SPyun YongHyeon 			printf("%u MHz; 32bit\n", clk);
3319ea9c3a30SPyun YongHyeon 		else
3320ea9c3a30SPyun YongHyeon 			printf("%u MHz; 64bit\n", clk);
3321ea9c3a30SPyun YongHyeon 	}
3322ea9c3a30SPyun YongHyeon }
3323ea9c3a30SPyun YongHyeon 
3324062af0b0SPyun YongHyeon static int
33253f74909aSGleb Smirnoff bge_attach(device_t dev)
332695d67482SBill Paul {
3327fba8b109SMarcel Moolenaar 	if_t ifp;
332895d67482SBill Paul 	struct bge_softc *sc;
3329548c8f1aSPyun YongHyeon 	uint32_t hwcfg = 0, misccfg, pcistate;
333008013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
3331ad4328baSMarius Strobl 	int capmask, error, reg, rid, trys;
333295d67482SBill Paul 
333395d67482SBill Paul 	sc = device_get_softc(dev);
333495d67482SBill Paul 	sc->bge_dev = dev;
333595d67482SBill Paul 
3336e010b055SPyun YongHyeon 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
3337dfe0df9aSPyun YongHyeon 	TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
3338e010b055SPyun YongHyeon 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
3339dfe0df9aSPyun YongHyeon 
334095d67482SBill Paul 	pci_enable_busmaster(dev);
334195d67482SBill Paul 
3342ad4328baSMarius Strobl 	/*
3343ad4328baSMarius Strobl 	 * Allocate control/status registers.
3344ad4328baSMarius Strobl 	 */
3345736b9319SPyun YongHyeon 	rid = PCIR_BAR(0);
33465f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
334744f8f2fcSMarius Strobl 	    RF_ACTIVE);
334895d67482SBill Paul 
334995d67482SBill Paul 	if (sc->bge_res == NULL) {
3350548c8f1aSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map BAR0 memory\n");
335195d67482SBill Paul 		error = ENXIO;
335295d67482SBill Paul 		goto fail;
335395d67482SBill Paul 	}
335495d67482SBill Paul 
33554f09c4c7SMarius Strobl 	/* Save various chip information. */
3356548c8f1aSPyun YongHyeon 	sc->bge_func_addr = pci_get_function(dev);
3357d7acafa1SMarius Strobl 	sc->bge_chipid = bge_chipid(dev);
3358e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
3359e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
3360e53d81eeSPaul Saab 
3361a813ed78SPyun YongHyeon 	/* Set default PHY address. */
3362daeeb75cSPyun YongHyeon 	sc->bge_phy_addr = 1;
33631108273aSPyun YongHyeon 	 /*
33641108273aSPyun YongHyeon 	  * PHY address mapping for various devices.
33651108273aSPyun YongHyeon 	  *
33661108273aSPyun YongHyeon 	  *          | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
33671108273aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
33681108273aSPyun YongHyeon 	  * BCM57XX  |   1   |   X   |   X   |   X   |
33691108273aSPyun YongHyeon 	  * BCM5704  |   1   |   X   |   1   |   X   |
33701108273aSPyun YongHyeon 	  * BCM5717  |   1   |   8   |   2   |   9   |
3371bbe2ca75SPyun YongHyeon 	  * BCM5719  |   1   |   8   |   2   |   9   |
337250515680SPyun YongHyeon 	  * BCM5720  |   1   |   8   |   2   |   9   |
33731108273aSPyun YongHyeon 	  *
3374548c8f1aSPyun YongHyeon 	  *          | F2 Cu | F2 Sr | F3 Cu | F3 Sr |
3375548c8f1aSPyun YongHyeon 	  * ---------+-------+-------+-------+-------+
3376548c8f1aSPyun YongHyeon 	  * BCM57XX  |   X   |   X   |   X   |   X   |
3377548c8f1aSPyun YongHyeon 	  * BCM5704  |   X   |   X   |   X   |   X   |
3378548c8f1aSPyun YongHyeon 	  * BCM5717  |   X   |   X   |   X   |   X   |
3379548c8f1aSPyun YongHyeon 	  * BCM5719  |   3   |   10  |   4   |   11  |
3380548c8f1aSPyun YongHyeon 	  * BCM5720  |   X   |   X   |   X   |   X   |
3381548c8f1aSPyun YongHyeon 	  *
33821108273aSPyun YongHyeon 	  * Other addresses may respond but they are not
33831108273aSPyun YongHyeon 	  * IEEE compliant PHYs and should be ignored.
33841108273aSPyun YongHyeon 	  */
3385bbe2ca75SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
338650515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
338750515680SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
3388548c8f1aSPyun YongHyeon 		if (sc->bge_chipid != BGE_CHIPID_BCM5717_A0) {
33891108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_SGDIG_STS) &
33901108273aSPyun YongHyeon 			    BGE_SGDIGSTS_IS_SERDES)
3391daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33921108273aSPyun YongHyeon 			else
3393daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
3394bbe2ca75SPyun YongHyeon 		} else {
33951108273aSPyun YongHyeon 			if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
33961108273aSPyun YongHyeon 			    BGE_CPMU_PHY_STRAP_IS_SERDES)
3397daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 8;
33981108273aSPyun YongHyeon 			else
3399daeeb75cSPyun YongHyeon 				sc->bge_phy_addr = sc->bge_func_addr + 1;
34001108273aSPyun YongHyeon 		}
34011108273aSPyun YongHyeon 	}
3402a813ed78SPyun YongHyeon 
34035fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
34045fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
340508013fd3SMarius Strobl 
34060dae9719SJung-uk Kim 	/* Save chipset family. */
34070dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
34082927f01fSPyun YongHyeon 	case BGE_ASICREV_BCM5762:
3409fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57765:
3410fe26ad88SPyun YongHyeon 	case BGE_ASICREV_BCM57766:
3411fe26ad88SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_57765_PLUS;
3412fe26ad88SPyun YongHyeon 		/* FALLTHROUGH */
34131108273aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3414bbe2ca75SPyun YongHyeon 	case BGE_ASICREV_BCM5719:
341550515680SPyun YongHyeon 	case BGE_ASICREV_BCM5720:
34161108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
34171108273aSPyun YongHyeon 		    BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
3418b4a256acSPyun YongHyeon 		    BGE_FLAG_JUMBO_FRAME;
341929b44b09SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
342029b44b09SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720) {
342129b44b09SPyun YongHyeon 			/*
342229b44b09SPyun YongHyeon 			 * Enable work around for DMA engine miscalculation
342329b44b09SPyun YongHyeon 			 * of TXMBUF available space.
342429b44b09SPyun YongHyeon 			 */
342529b44b09SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_RDMA_BUG;
3426bbe2ca75SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3427bbe2ca75SPyun YongHyeon 			    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3428bbe2ca75SPyun YongHyeon 				/* Jumbo frame on BCM5719 A0 does not work. */
3429463a7e27SPyun YongHyeon 				sc->bge_flags &= ~BGE_FLAG_JUMBO;
3430bbe2ca75SPyun YongHyeon 			}
343129b44b09SPyun YongHyeon 		}
34321108273aSPyun YongHyeon 		break;
3433a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
3434a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
3435a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
3436a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
3437a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
3438a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
3439a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
3440a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
3441a5779553SStanislav Sedov 		break;
34420dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
34430dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
34440dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
34450dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
34467ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
34470dae9719SJung-uk Kim 		break;
34480dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
34490dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
34500dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
3451f5459d4cSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
34529fe569d8SXin LI 		/* FALLTHROUGH */
34530dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
34540dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
345538cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
34560dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
34579fe569d8SXin LI 		/* FALLTHROUGH */
34580dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
34590dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
34600dae9719SJung-uk Kim 		break;
34610dae9719SJung-uk Kim 	}
34620dae9719SJung-uk Kim 
3463548c8f1aSPyun YongHyeon 	/* Identify chips with APE processor. */
3464548c8f1aSPyun YongHyeon 	switch (sc->bge_asicrev) {
3465548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5717:
3466548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5719:
3467548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5720:
3468548c8f1aSPyun YongHyeon 	case BGE_ASICREV_BCM5761:
34692927f01fSPyun YongHyeon 	case BGE_ASICREV_BCM5762:
3470548c8f1aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_APE;
3471548c8f1aSPyun YongHyeon 		break;
3472548c8f1aSPyun YongHyeon 	}
3473548c8f1aSPyun YongHyeon 
3474548c8f1aSPyun YongHyeon 	/* Chips with APE need BAR2 access for APE registers/memory. */
3475548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) != 0) {
3476548c8f1aSPyun YongHyeon 		rid = PCIR_BAR(2);
3477548c8f1aSPyun YongHyeon 		sc->bge_res2 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
3478548c8f1aSPyun YongHyeon 		    RF_ACTIVE);
3479548c8f1aSPyun YongHyeon 		if (sc->bge_res2 == NULL) {
3480548c8f1aSPyun YongHyeon 			device_printf (sc->bge_dev,
3481548c8f1aSPyun YongHyeon 			    "couldn't map BAR2 memory\n");
3482548c8f1aSPyun YongHyeon 			error = ENXIO;
3483548c8f1aSPyun YongHyeon 			goto fail;
3484548c8f1aSPyun YongHyeon 		}
3485548c8f1aSPyun YongHyeon 
3486548c8f1aSPyun YongHyeon 		/* Enable APE register/memory access by host driver. */
3487548c8f1aSPyun YongHyeon 		pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
3488548c8f1aSPyun YongHyeon 		pcistate |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
3489548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
3490548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
3491548c8f1aSPyun YongHyeon 		pci_write_config(dev, BGE_PCI_PCISTATE, pcistate, 4);
3492548c8f1aSPyun YongHyeon 
3493548c8f1aSPyun YongHyeon 		bge_ape_lock_init(sc);
3494548c8f1aSPyun YongHyeon 		bge_ape_read_fw_ver(sc);
3495548c8f1aSPyun YongHyeon 	}
3496548c8f1aSPyun YongHyeon 
3497749a5269SMarius Strobl 	/* Add SYSCTLs, requires the chipset family to be set. */
3498749a5269SMarius Strobl 	bge_add_sysctls(sc);
3499749a5269SMarius Strobl 
3500a813ed78SPyun YongHyeon 	/* Identify the chips that use an CPMU. */
35011108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc) ||
35021108273aSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3503a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3504a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
3505a813ed78SPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
3506a813ed78SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
3507a813ed78SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
3508a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
3509a813ed78SPyun YongHyeon 	else
3510a813ed78SPyun YongHyeon 		sc->bge_mi_mode = BGE_MIMODE_BASE;
35117ed3f0f0SPyun YongHyeon 	/* Enable auto polling for BCM570[0-5]. */
35127ed3f0f0SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
35137ed3f0f0SPyun YongHyeon 		sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
3514a813ed78SPyun YongHyeon 
3515f681b29aSPyun YongHyeon 	/*
3516d4622124SPyun YongHyeon 	 * All Broadcom controllers have 4GB boundary DMA bug.
3517f681b29aSPyun YongHyeon 	 * Whenever an address crosses a multiple of the 4GB boundary
3518f681b29aSPyun YongHyeon 	 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3519f681b29aSPyun YongHyeon 	 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3520f681b29aSPyun YongHyeon 	 * state machine will lockup and cause the device to hang.
3521f681b29aSPyun YongHyeon 	 */
3522f681b29aSPyun YongHyeon 	sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
35234f0794ffSBjoern A. Zeeb 
3524d9820cd8SPyun YongHyeon 	/* BCM5755 or higher and BCM5906 have short DMA bug. */
3525d9820cd8SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
3526d9820cd8SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
3527d9820cd8SPyun YongHyeon 
3528a7fcfcf3SPyun YongHyeon 	/*
3529a7fcfcf3SPyun YongHyeon 	 * BCM5719 cannot handle DMA requests for DMA segments that
3530a7fcfcf3SPyun YongHyeon 	 * have larger than 4KB in size.  However the maximum DMA
3531a7fcfcf3SPyun YongHyeon 	 * segment size created in DMA tag is 4KB for TSO, so we
3532a7fcfcf3SPyun YongHyeon 	 * wouldn't encounter the issue here.
3533a7fcfcf3SPyun YongHyeon 	 */
3534a7fcfcf3SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3535a7fcfcf3SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3536a7fcfcf3SPyun YongHyeon 
3537ea9c3a30SPyun YongHyeon 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3538fb772a6cSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
35394f0794ffSBjoern A. Zeeb 		if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
35404f0794ffSBjoern A. Zeeb 		    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
35414f0794ffSBjoern A. Zeeb 			sc->bge_flags |= BGE_FLAG_5788;
354284ac96f8SPyun YongHyeon 	}
35434f0794ffSBjoern A. Zeeb 
3544fb772a6cSMarius Strobl 	capmask = BMSR_DEFCAPMASK;
3545fb772a6cSMarius Strobl 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3546fb772a6cSMarius Strobl 	    (misccfg == 0x4000 || misccfg == 0x8000)) ||
3547fb772a6cSMarius Strobl 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3548fb772a6cSMarius Strobl 	    pci_get_vendor(dev) == BCOM_VENDORID &&
3549fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3550fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3551fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3552fb772a6cSMarius Strobl 	    (pci_get_vendor(dev) == BCOM_VENDORID &&
3553fb772a6cSMarius Strobl 	    (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3554fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3555fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3556fb772a6cSMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3557d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57791 ||
3558d7acafa1SMarius Strobl 	    pci_get_device(dev) == BCOM_DEVICEID_BCM57795 ||
3559fb772a6cSMarius Strobl 	    sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3560fb772a6cSMarius Strobl 		/* These chips are 10/100 only. */
3561fb772a6cSMarius Strobl 		capmask &= ~BMSR_EXTSTAT;
3562d73ea7c6SPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
3563fb772a6cSMarius Strobl 	}
3564fb772a6cSMarius Strobl 
3565e53d81eeSPaul Saab 	/*
3566ca3f1187SPyun YongHyeon 	 * Some controllers seem to require a special firmware to use
3567ca3f1187SPyun YongHyeon 	 * TSO. But the firmware is not available to FreeBSD and Linux
3568ca3f1187SPyun YongHyeon 	 * claims that the TSO performed by the firmware is slower than
3569ca3f1187SPyun YongHyeon 	 * hardware based TSO. Moreover the firmware based TSO has one
3570d7acafa1SMarius Strobl 	 * known bug which can't handle TSO if Ethernet header + IP/TCP
3571d7acafa1SMarius Strobl 	 * header is greater than 80 bytes. A workaround for the TSO
3572ca3f1187SPyun YongHyeon 	 * bug exist but it seems it's too expensive than not using
3573ca3f1187SPyun YongHyeon 	 * TSO at all. Some hardwares also have the TSO bug so limit
3574ca3f1187SPyun YongHyeon 	 * the TSO to the controllers that are not affected TSO issues
3575ca3f1187SPyun YongHyeon 	 * (e.g. 5755 or higher).
3576ca3f1187SPyun YongHyeon 	 */
35771108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
35781108273aSPyun YongHyeon 		/* BCM5717 requires different TSO configuration. */
35791108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TSO3;
3580bbe2ca75SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3581bbe2ca75SPyun YongHyeon 		    sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3582bbe2ca75SPyun YongHyeon 			/* TSO on BCM5719 A0 does not work. */
3583bbe2ca75SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_TSO3;
3584bbe2ca75SPyun YongHyeon 		}
35851108273aSPyun YongHyeon 	} else if (BGE_IS_5755_PLUS(sc)) {
35864f4a16e1SPyun YongHyeon 		/*
35874f4a16e1SPyun YongHyeon 		 * BCM5754 and BCM5787 shares the same ASIC id so
35884f4a16e1SPyun YongHyeon 		 * explicit device id check is required.
3589be95548dSPyun YongHyeon 		 * Due to unknown reason TSO does not work on BCM5755M.
35904f4a16e1SPyun YongHyeon 		 */
35914f4a16e1SPyun YongHyeon 		if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3592be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3593be95548dSPyun YongHyeon 		    pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3594ca3f1187SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_TSO;
35954f4a16e1SPyun YongHyeon 	}
3596ca3f1187SPyun YongHyeon 
3597ca3f1187SPyun YongHyeon 	/*
35986f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
3599e53d81eeSPaul Saab 	 */
36003b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
36014c0da0ffSGleb Smirnoff 		/*
36026f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
36036f8718a3SScott Long 		 * must be a PCI Express device.
36046f8718a3SScott Long 		 */
36056f8718a3SScott Long 		sc->bge_flags |= BGE_FLAG_PCIE;
36060aaf1057SPyun YongHyeon 		sc->bge_expcap = reg;
360748630d79SPyun YongHyeon 		/* Extract supported maximum payload size. */
360848630d79SPyun YongHyeon 		sc->bge_mps = pci_read_config(dev, sc->bge_expcap +
360948630d79SPyun YongHyeon 		    PCIER_DEVICE_CAP, 2);
361048630d79SPyun YongHyeon 		sc->bge_mps = 128 << (sc->bge_mps & PCIEM_CAP_MAX_PAYLOAD);
361150515680SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
361250515680SPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5720)
361348630d79SPyun YongHyeon 			sc->bge_expmrq = 2048;
361448630d79SPyun YongHyeon 		else
361548630d79SPyun YongHyeon 			sc->bge_expmrq = 4096;
361648630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
36176f8718a3SScott Long 	} else {
36186f8718a3SScott Long 		/*
36196f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
36206f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
36214c0da0ffSGleb Smirnoff 		 */
36223b0a4aefSJohn Baldwin 		if (pci_find_cap(dev, PCIY_PCIX, &reg) == 0)
36230aaf1057SPyun YongHyeon 			sc->bge_pcixcap = reg;
362490447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
36254c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
3626652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
36276f8718a3SScott Long 	}
36284c0da0ffSGleb Smirnoff 
3629bf6ef57aSJohn Polstra 	/*
3630fd4d32feSPyun YongHyeon 	 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3631fd4d32feSPyun YongHyeon 	 * not actually a MAC controller bug but an issue with the embedded
3632fd4d32feSPyun YongHyeon 	 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3633fd4d32feSPyun YongHyeon 	 */
3634fd4d32feSPyun YongHyeon 	if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3635fd4d32feSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3636fd4d32feSPyun YongHyeon 	/*
3637062af0b0SPyun YongHyeon 	 * Some PCI-X bridges are known to trigger write reordering to
3638062af0b0SPyun YongHyeon 	 * the mailbox registers. Typical phenomena is watchdog timeouts
3639062af0b0SPyun YongHyeon 	 * caused by out-of-order TX completions.  Enable workaround for
3640062af0b0SPyun YongHyeon 	 * PCI-X devices that live behind these bridges.
3641062af0b0SPyun YongHyeon 	 * Note, PCI-X controllers can run in PCI mode so we can't use
3642062af0b0SPyun YongHyeon 	 * BGE_FLAG_PCIX flag to detect PCI-X controllers.
3643062af0b0SPyun YongHyeon 	 */
3644062af0b0SPyun YongHyeon 	if (sc->bge_pcixcap != 0 && bge_mbox_reorder(sc) != 0)
3645062af0b0SPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_MBOX_REORDER;
3646062af0b0SPyun YongHyeon 	/*
3647bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
3648bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
3649bf6ef57aSJohn Polstra 	 * normal operation.
3650bf6ef57aSJohn Polstra 	 */
36510aaf1057SPyun YongHyeon 	rid = 0;
36523b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->bge_dev, PCIY_MSI, &reg) == 0) {
36530aaf1057SPyun YongHyeon 		sc->bge_msicap = reg;
3654ad4328baSMarius Strobl 		reg = 1;
3655ad4328baSMarius Strobl 		if (bge_can_use_msi(sc) && pci_alloc_msi(dev, &reg) == 0) {
3656bf6ef57aSJohn Polstra 			rid = 1;
3657bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
36580aaf1057SPyun YongHyeon 		}
36590aaf1057SPyun YongHyeon 	}
3660bf6ef57aSJohn Polstra 
36611108273aSPyun YongHyeon 	/*
36621108273aSPyun YongHyeon 	 * All controllers except BCM5700 supports tagged status but
36631108273aSPyun YongHyeon 	 * we use tagged status only for MSI case on BCM5717. Otherwise
36641108273aSPyun YongHyeon 	 * MSI on BCM5717 does not work.
36651108273aSPyun YongHyeon 	 */
36661108273aSPyun YongHyeon #ifndef DEVICE_POLLING
36671108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
36681108273aSPyun YongHyeon 		sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
36691108273aSPyun YongHyeon #endif
36701108273aSPyun YongHyeon 
3671bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3672ad4328baSMarius Strobl 	    RF_ACTIVE | (rid != 0 ? 0 : RF_SHAREABLE));
3673bf6ef57aSJohn Polstra 
3674bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
3675bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
3676bf6ef57aSJohn Polstra 		error = ENXIO;
3677bf6ef57aSJohn Polstra 		goto fail;
3678bf6ef57aSJohn Polstra 	}
3679bf6ef57aSJohn Polstra 
3680ea9c3a30SPyun YongHyeon 	bge_devinfo(sc);
36814f09c4c7SMarius Strobl 
36828cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
3683548c8f1aSPyun YongHyeon 	/* No ASF if APE present. */
3684548c8f1aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_APE) == 0) {
3685888b47f0SPyun YongHyeon 		if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3686888b47f0SPyun YongHyeon 		    BGE_SRAM_DATA_SIG_MAGIC)) {
3687548c8f1aSPyun YongHyeon 			if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG) &
3688548c8f1aSPyun YongHyeon 			    BGE_HWCFG_ASF) {
36898cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_ENABLE;
36908cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_STACKUP;
3691d67eba2fSPyun YongHyeon 				if (BGE_IS_575X_PLUS(sc))
36928cb1383cSDoug Ambrisko 					sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
36938cb1383cSDoug Ambrisko 			}
36948cb1383cSDoug Ambrisko 		}
3695548c8f1aSPyun YongHyeon 	}
36968cb1383cSDoug Ambrisko 
36978cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
36983dd76c98SPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
36998cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
37008cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
37018cb1383cSDoug Ambrisko 		error = ENXIO;
37028cb1383cSDoug Ambrisko 		goto fail;
37038cb1383cSDoug Ambrisko 	}
37048cb1383cSDoug Ambrisko 
37053dd76c98SPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
37063dd76c98SPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
370795d67482SBill Paul 
370895d67482SBill Paul 	if (bge_chipinit(sc)) {
3709fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
371095d67482SBill Paul 		error = ENXIO;
371195d67482SBill Paul 		goto fail;
371295d67482SBill Paul 	}
371395d67482SBill Paul 
371438cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
371538cc658fSJohn Baldwin 	if (error) {
371608013fd3SMarius Strobl 		device_printf(sc->bge_dev,
371708013fd3SMarius Strobl 		    "failed to read station address\n");
371895d67482SBill Paul 		error = ENXIO;
371995d67482SBill Paul 		goto fail;
372095d67482SBill Paul 	}
372195d67482SBill Paul 
3722f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
37231108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc))
37241108273aSPyun YongHyeon 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
37251108273aSPyun YongHyeon 	else if (BGE_IS_5705_PLUS(sc))
3726f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3727f41ac2beSBill Paul 	else
3728f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3729f41ac2beSBill Paul 
37305b610048SPyun YongHyeon 	if (bge_dma_alloc(sc)) {
3731fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
3732fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
3733f41ac2beSBill Paul 		error = ENXIO;
3734f41ac2beSBill Paul 		goto fail;
3735f41ac2beSBill Paul 	}
3736f41ac2beSBill Paul 
373795d67482SBill Paul 	/* Set default tuneable values. */
373895d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
373995d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
374095d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
37416f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
37426f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
374395d67482SBill Paul 
374435f945cdSPyun YongHyeon 	/* Initialize checksum features to use. */
374535f945cdSPyun YongHyeon 	sc->bge_csum_features = BGE_CSUM_FEATURES;
374635f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum != 0)
374735f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
374835f945cdSPyun YongHyeon 
374995d67482SBill Paul 	/* Set up ifnet structure */
3750fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3751fc74a9f9SBrooks Davis 	if (ifp == NULL) {
3752fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
3753fc74a9f9SBrooks Davis 		error = ENXIO;
3754fc74a9f9SBrooks Davis 		goto fail;
3755fc74a9f9SBrooks Davis 	}
3756fba8b109SMarcel Moolenaar 	if_setsoftc(ifp, sc);
37579bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
3758fba8b109SMarcel Moolenaar 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
3759fba8b109SMarcel Moolenaar 	if_setioctlfn(ifp, bge_ioctl);
3760fba8b109SMarcel Moolenaar 	if_setstartfn(ifp, bge_start);
3761fba8b109SMarcel Moolenaar 	if_setinitfn(ifp, bge_init);
3762df360178SGleb Smirnoff 	if_setgetcounterfn(ifp, bge_get_counter);
37634a81240cSMarcel Moolenaar 	if_setsendqlen(ifp, BGE_TX_RING_CNT - 1);
3764fba8b109SMarcel Moolenaar 	if_setsendqready(ifp);
3765fba8b109SMarcel Moolenaar 	if_sethwassist(ifp, sc->bge_csum_features);
3766fba8b109SMarcel Moolenaar 	if_setcapabilities(ifp, IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
3767fba8b109SMarcel Moolenaar 	    IFCAP_VLAN_MTU);
37681108273aSPyun YongHyeon 	if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3769fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, CSUM_TSO, 0);
3770fba8b109SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, IFCAP_TSO4 | IFCAP_VLAN_HWTSO, 0);
3771ca3f1187SPyun YongHyeon 	}
37724e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
3773fba8b109SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWCSUM, 0);
37744e35d186SJung-uk Kim #endif
3775fba8b109SMarcel Moolenaar 	if_setcapenable(ifp, if_getcapabilities(ifp));
377675719184SGleb Smirnoff #ifdef DEVICE_POLLING
3777fba8b109SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
377875719184SGleb Smirnoff #endif
377995d67482SBill Paul 
3780a1d52896SBill Paul 	/*
3781d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
3782d375e524SGleb Smirnoff 	 * to hardware bugs.
3783d375e524SGleb Smirnoff 	 */
3784d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3785fba8b109SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, 0, IFCAP_HWCSUM);
3786fba8b109SMarcel Moolenaar 		if_setcapenablebit(ifp, 0, IFCAP_HWCSUM);
3787fba8b109SMarcel Moolenaar 		if_sethwassist(ifp, 0);
3788d375e524SGleb Smirnoff 	}
3789d375e524SGleb Smirnoff 
3790d375e524SGleb Smirnoff 	/*
3791a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
379241abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
379341abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
379441abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
379541abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
379641abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
379741abcc1bSPaul Saab 	 * SK-9D41.
3798a1d52896SBill Paul 	 */
3799888b47f0SPyun YongHyeon 	if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3800888b47f0SPyun YongHyeon 		hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
38015fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
38025fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3803f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3804f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
3805fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
3806f6789fbaSPyun YongHyeon 			error = ENXIO;
3807f6789fbaSPyun YongHyeon 			goto fail;
3808f6789fbaSPyun YongHyeon 		}
380941abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
381041abcc1bSPaul Saab 	}
381141abcc1bSPaul Saab 
381295d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
3813ea3b4127SPyun YongHyeon 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3814ea3b4127SPyun YongHyeon 	    SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
381570c2071bSPyun YongHyeon 		if (BGE_IS_5705_PLUS(sc)) {
3816ea3b4127SPyun YongHyeon 			sc->bge_flags |= BGE_FLAG_MII_SERDES;
381770c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
381870c2071bSPyun YongHyeon 		} else
3819652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_TBI;
3820ea3b4127SPyun YongHyeon 	}
382195d67482SBill Paul 
382270c2071bSPyun YongHyeon 	/* Set various PHY bug flags. */
382370c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
382470c2071bSPyun YongHyeon 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
382570c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
382670c2071bSPyun YongHyeon 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
382770c2071bSPyun YongHyeon 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
382870c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
382970c2071bSPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
383070c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
383170c2071bSPyun YongHyeon 	if (pci_get_subvendor(dev) == DELL_VENDORID)
383270c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_3LED;
383370c2071bSPyun YongHyeon 	if ((BGE_IS_5705_PLUS(sc)) &&
383470c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
383570c2071bSPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
3836fe26ad88SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM57780 &&
3837fe26ad88SPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc)) {
383870c2071bSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
383970c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
384070c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
384170c2071bSPyun YongHyeon 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
384270c2071bSPyun YongHyeon 			if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
384370c2071bSPyun YongHyeon 			    pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
384470c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
384570c2071bSPyun YongHyeon 			if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
384670c2071bSPyun YongHyeon 				sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
384770c2071bSPyun YongHyeon 		} else
384870c2071bSPyun YongHyeon 			sc->bge_phy_flags |= BGE_PHY_BER_BUG;
384970c2071bSPyun YongHyeon 	}
385070c2071bSPyun YongHyeon 
385170c2071bSPyun YongHyeon 	/*
3852d73ea7c6SPyun YongHyeon 	 * Don't enable Ethernet@WireSpeed for the 5700 or the
385370c2071bSPyun YongHyeon 	 * 5705 A0 and A1 chips.
385470c2071bSPyun YongHyeon 	 */
385570c2071bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
385670c2071bSPyun YongHyeon 	    (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
385770c2071bSPyun YongHyeon 	    (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
3858d73ea7c6SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)))
385970c2071bSPyun YongHyeon 		sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
386070c2071bSPyun YongHyeon 
3861652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
386209a8241fSGleb Smirnoff 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
38630c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
38640c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
38656098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
38666098821cSJung-uk Kim 		    0, NULL);
386795d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
386895d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3869da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
387095d67482SBill Paul 	} else {
387195d67482SBill Paul 		/*
38728cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
38738cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
38748cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
38758cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
38768cb1383cSDoug Ambrisko 		 * the PHY.
387795d67482SBill Paul 		 */
38784012d104SMarius Strobl 		trys = 0;
38798cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
38808cb1383cSDoug Ambrisko again:
38818cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
38828cb1383cSDoug Ambrisko 
3883fba8b109SMarcel Moolenaar 		error = mii_attach(dev, &sc->bge_miibus, ifp,
3884fba8b109SMarcel Moolenaar 		    (ifm_change_cb_t)bge_ifmedia_upd,
3885fba8b109SMarcel Moolenaar 		    (ifm_stat_cb_t)bge_ifmedia_sts, capmask, sc->bge_phy_addr,
3886fba8b109SMarcel Moolenaar 		    MII_OFFSET_ANY, MIIF_DOPAUSE);
38878e5d93dbSMarius Strobl 		if (error != 0) {
38888cb1383cSDoug Ambrisko 			if (trys++ < 4) {
38898cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
3890daeeb75cSPyun YongHyeon 				bge_miibus_writereg(sc->bge_dev,
3891daeeb75cSPyun YongHyeon 				    sc->bge_phy_addr, MII_BMCR, BMCR_RESET);
38928cb1383cSDoug Ambrisko 				goto again;
38938cb1383cSDoug Ambrisko 			}
38948e5d93dbSMarius Strobl 			device_printf(sc->bge_dev, "attaching PHYs failed\n");
389595d67482SBill Paul 			goto fail;
389695d67482SBill Paul 		}
38978cb1383cSDoug Ambrisko 
38988cb1383cSDoug Ambrisko 		/*
38998cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
39008cb1383cSDoug Ambrisko 		 */
39018cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
39028cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
390395d67482SBill Paul 	}
390495d67482SBill Paul 
390595d67482SBill Paul 	/*
3906e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
3907e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
3908e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
3909e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
3910e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
3911e255b776SJohn Polstra 	 * payloads by copying the received packets.
3912e255b776SJohn Polstra 	 */
3913652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3914652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
3915652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3916e255b776SJohn Polstra 
3917e255b776SJohn Polstra 	/*
391895d67482SBill Paul 	 * Call MI attach routine.
391995d67482SBill Paul 	 */
3920fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
39210f9bd73bSSam Leffler 
392261ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
3923fba8b109SMarcel Moolenaar 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
392461ccb9daSPyun YongHyeon 
39250f9bd73bSSam Leffler 	/*
39260f9bd73bSSam Leffler 	 * Hookup IRQ last.
39270f9bd73bSSam Leffler 	 */
3928dfe0df9aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3929dfe0df9aSPyun YongHyeon 		/* Take advantage of single-shot MSI. */
39307e6acdf1SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
39317e6acdf1SPyun YongHyeon 		    ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3932dfe0df9aSPyun YongHyeon 		sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3933dfe0df9aSPyun YongHyeon 		    taskqueue_thread_enqueue, &sc->bge_tq);
3934dfe0df9aSPyun YongHyeon 		if (sc->bge_tq == NULL) {
3935dfe0df9aSPyun YongHyeon 			device_printf(dev, "could not create taskqueue.\n");
3936dfe0df9aSPyun YongHyeon 			ether_ifdetach(ifp);
3937e010b055SPyun YongHyeon 			error = ENOMEM;
3938dfe0df9aSPyun YongHyeon 			goto fail;
3939dfe0df9aSPyun YongHyeon 		}
3940d7acafa1SMarius Strobl 		error = taskqueue_start_threads(&sc->bge_tq, 1, PI_NET,
3941d7acafa1SMarius Strobl 		    "%s taskq", device_get_nameunit(sc->bge_dev));
3942d7acafa1SMarius Strobl 		if (error != 0) {
3943d7acafa1SMarius Strobl 			device_printf(dev, "could not start threads.\n");
3944d7acafa1SMarius Strobl 			ether_ifdetach(ifp);
3945d7acafa1SMarius Strobl 			goto fail;
3946d7acafa1SMarius Strobl 		}
3947dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3948dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3949dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
3950dfe0df9aSPyun YongHyeon 	} else
3951dfe0df9aSPyun YongHyeon 		error = bus_setup_intr(dev, sc->bge_irq,
3952dfe0df9aSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3953dfe0df9aSPyun YongHyeon 		    &sc->bge_intrhand);
39540f9bd73bSSam Leffler 
39550f9bd73bSSam Leffler 	if (error) {
3956e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
3957fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
3958ded66962SMark Johnston 		goto fail;
39590f9bd73bSSam Leffler 	}
396095d67482SBill Paul 
3961ded66962SMark Johnston 	/* Attach driver netdump methods. */
3962ded66962SMark Johnston 	NETDUMP_SET(ifp, bge);
3963ded66962SMark Johnston 
396495d67482SBill Paul fail:
3965e010b055SPyun YongHyeon 	if (error)
3966e010b055SPyun YongHyeon 		bge_detach(dev);
396795d67482SBill Paul 	return (error);
396895d67482SBill Paul }
396995d67482SBill Paul 
397095d67482SBill Paul static int
39713f74909aSGleb Smirnoff bge_detach(device_t dev)
397295d67482SBill Paul {
397395d67482SBill Paul 	struct bge_softc *sc;
3974fba8b109SMarcel Moolenaar 	if_t ifp;
397595d67482SBill Paul 
397695d67482SBill Paul 	sc = device_get_softc(dev);
3977fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
397895d67482SBill Paul 
397975719184SGleb Smirnoff #ifdef DEVICE_POLLING
3980fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING)
398175719184SGleb Smirnoff 		ether_poll_deregister(ifp);
398275719184SGleb Smirnoff #endif
398375719184SGleb Smirnoff 
3984e010b055SPyun YongHyeon 	if (device_is_attached(dev)) {
3985e010b055SPyun YongHyeon 		ether_ifdetach(ifp);
39860f9bd73bSSam Leffler 		BGE_LOCK(sc);
398795d67482SBill Paul 		bge_stop(sc);
39880f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
39895dda8085SOleg Bulyzhin 		callout_drain(&sc->bge_stat_ch);
3990e010b055SPyun YongHyeon 	}
39915dda8085SOleg Bulyzhin 
3992dfe0df9aSPyun YongHyeon 	if (sc->bge_tq)
3993dfe0df9aSPyun YongHyeon 		taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
399495d67482SBill Paul 
39950aba72ddSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TBI)
399695d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
39970aba72ddSPyun YongHyeon 	else if (sc->bge_miibus != NULL) {
399895d67482SBill Paul 		bus_generic_detach(dev);
399995d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
400095d67482SBill Paul 	}
400195d67482SBill Paul 
400295d67482SBill Paul 	bge_release_resources(sc);
400395d67482SBill Paul 
400495d67482SBill Paul 	return (0);
400595d67482SBill Paul }
400695d67482SBill Paul 
400795d67482SBill Paul static void
40083f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
400995d67482SBill Paul {
401095d67482SBill Paul 	device_t dev;
401195d67482SBill Paul 
401295d67482SBill Paul 	dev = sc->bge_dev;
401395d67482SBill Paul 
4014dfe0df9aSPyun YongHyeon 	if (sc->bge_tq != NULL)
4015dfe0df9aSPyun YongHyeon 		taskqueue_free(sc->bge_tq);
4016dfe0df9aSPyun YongHyeon 
401795d67482SBill Paul 	if (sc->bge_intrhand != NULL)
401895d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
401995d67482SBill Paul 
4020ad4328baSMarius Strobl 	if (sc->bge_irq != NULL) {
4021724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
4022ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_irq), sc->bge_irq);
4023724bd939SJohn Polstra 		pci_release_msi(dev);
4024ad4328baSMarius Strobl 	}
402595d67482SBill Paul 
402695d67482SBill Paul 	if (sc->bge_res != NULL)
402795d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
4028ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_res), sc->bge_res);
402995d67482SBill Paul 
4030548c8f1aSPyun YongHyeon 	if (sc->bge_res2 != NULL)
4031548c8f1aSPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY,
4032ad4328baSMarius Strobl 		    rman_get_rid(sc->bge_res2), sc->bge_res2);
4033548c8f1aSPyun YongHyeon 
4034ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
4035ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
4036ad61f896SRuslan Ermilov 
4037f41ac2beSBill Paul 	bge_dma_free(sc);
403895d67482SBill Paul 
40390f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
40400f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
404195d67482SBill Paul }
404295d67482SBill Paul 
40438cb1383cSDoug Ambrisko static int
40443f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
404595d67482SBill Paul {
404695d67482SBill Paul 	device_t dev;
4047cc085b36SPyun YongHyeon 	uint32_t cachesize, command, mac_mode, mac_mode_mask, reset, val;
40486f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
40490aaf1057SPyun YongHyeon 	uint16_t devctl;
40505fea260fSMarius Strobl 	int i;
405195d67482SBill Paul 
405295d67482SBill Paul 	dev = sc->bge_dev;
405395d67482SBill Paul 
4054cc085b36SPyun YongHyeon 	mac_mode_mask = BGE_MACMODE_HALF_DUPLEX | BGE_MACMODE_PORTMODE;
4055548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4056548c8f1aSPyun YongHyeon 		mac_mode_mask |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
4057cc085b36SPyun YongHyeon 	mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) & mac_mode_mask;
4058cc085b36SPyun YongHyeon 
405938cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
406038cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
40616f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
40626f8718a3SScott Long 			write_op = bge_writemem_direct;
40636f8718a3SScott Long 		else
40646f8718a3SScott Long 			write_op = bge_writemem_ind;
40659ba784dbSScott Long 	} else
40666f8718a3SScott Long 		write_op = bge_writereg_ind;
40676f8718a3SScott Long 
40683dd76c98SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
40693dd76c98SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5701) {
40703dd76c98SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
40713dd76c98SPyun YongHyeon 		for (i = 0; i < 8000; i++) {
40723dd76c98SPyun YongHyeon 			if (CSR_READ_4(sc, BGE_NVRAM_SWARB) &
40733dd76c98SPyun YongHyeon 			    BGE_NVRAMSWARB_GNT1)
40743dd76c98SPyun YongHyeon 				break;
40753dd76c98SPyun YongHyeon 			DELAY(20);
40763dd76c98SPyun YongHyeon 		}
40773dd76c98SPyun YongHyeon 		if (i == 8000) {
40783dd76c98SPyun YongHyeon 			if (bootverbose)
40793dd76c98SPyun YongHyeon 				device_printf(dev, "NVRAM lock timedout!\n");
40803dd76c98SPyun YongHyeon 		}
40813dd76c98SPyun YongHyeon 	}
4082548c8f1aSPyun YongHyeon 	/* Take APE lock when performing reset. */
4083548c8f1aSPyun YongHyeon 	bge_ape_lock(sc, BGE_APE_LOCK_GRC);
4084548c8f1aSPyun YongHyeon 
408595d67482SBill Paul 	/* Save some important PCI state. */
408695d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
408795d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
408895d67482SBill Paul 
408995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
409095d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4091e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
409295d67482SBill Paul 
40936f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
40946f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
4095a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
40966f8718a3SScott Long 		if (bootverbose)
4097333704a3SPyun YongHyeon 			device_printf(dev, "Disabling fastboot\n");
40986f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
40996f8718a3SScott Long 	}
41006f8718a3SScott Long 
41016f8718a3SScott Long 	/*
41026f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
41036f8718a3SScott Long 	 * When firmware finishes its initialization it will
4104888b47f0SPyun YongHyeon 	 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
41056f8718a3SScott Long 	 */
4106888b47f0SPyun YongHyeon 	bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
41076f8718a3SScott Long 
41080c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
4109e53d81eeSPaul Saab 
4110e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4111652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4112ad49eccfSPyun YongHyeon 		if (sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
4113ad49eccfSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_5717_PLUS) == 0) {
41140c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
41150c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, 0x7E2C, 0x20);
4116ad49eccfSPyun YongHyeon 		}
4117e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
4118e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
41190c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
41200c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
4121e53d81eeSPaul Saab 		}
4122e53d81eeSPaul Saab 	}
4123e53d81eeSPaul Saab 
4124df4db538SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
4125df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
4126df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
4127df4db538SPyun YongHyeon 		    val | BGE_VCPU_STATUS_DRV_RESET);
4128df4db538SPyun YongHyeon 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
4129df4db538SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
4130df4db538SPyun YongHyeon 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
4131df4db538SPyun YongHyeon 	}
4132df4db538SPyun YongHyeon 
413321c9e407SDavid Christensen 	/*
41346f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
41356f8718a3SScott Long 	 * powered up in D0 uninitialized.
41366f8718a3SScott Long 	 */
41375512ca01SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc) &&
41385512ca01SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
4139caf088fcSPyun YongHyeon 		reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
41406f8718a3SScott Long 
414195d67482SBill Paul 	/* Issue global reset */
41426f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
414395d67482SBill Paul 
4144cc085b36SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIE)
4145cc085b36SPyun YongHyeon 		DELAY(100 * 1000);
4146cc085b36SPyun YongHyeon 	else
414795d67482SBill Paul 		DELAY(1000);
414895d67482SBill Paul 
4149e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4150652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
4151e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
4152e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
41535fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
41545fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
4155e53d81eeSPaul Saab 		}
41560aaf1057SPyun YongHyeon 		devctl = pci_read_config(dev,
4157389c8bd5SGavin Atkinson 		    sc->bge_expcap + PCIER_DEVICE_CTL, 2);
41580aaf1057SPyun YongHyeon 		/* Clear enable no snoop and disable relaxed ordering. */
4159389c8bd5SGavin Atkinson 		devctl &= ~(PCIEM_CTL_RELAXED_ORD_ENABLE |
4160389c8bd5SGavin Atkinson 		    PCIEM_CTL_NOSNOOP_ENABLE);
4161389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_CTL,
41620aaf1057SPyun YongHyeon 		    devctl, 2);
416348630d79SPyun YongHyeon 		pci_set_max_read_req(dev, sc->bge_expmrq);
41640aaf1057SPyun YongHyeon 		/* Clear error status. */
4165389c8bd5SGavin Atkinson 		pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_STA,
4166389c8bd5SGavin Atkinson 		    PCIEM_STA_CORRECTABLE_ERROR |
4167389c8bd5SGavin Atkinson 		    PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
4168389c8bd5SGavin Atkinson 		    PCIEM_STA_UNSUPPORTED_REQ, 2);
4169e53d81eeSPaul Saab 	}
4170e53d81eeSPaul Saab 
41713f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
417295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
417395d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4174e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
4175cc085b36SPyun YongHyeon 	val = BGE_PCISTATE_ROM_ENABLE | BGE_PCISTATE_ROM_RETRY_ENABLE;
4176cc085b36SPyun YongHyeon 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0 &&
4177cc085b36SPyun YongHyeon 	    (sc->bge_flags & BGE_FLAG_PCIX) != 0)
4178cc085b36SPyun YongHyeon 		val |= BGE_PCISTATE_RETRY_SAME_DMA;
4179548c8f1aSPyun YongHyeon 	if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4180548c8f1aSPyun YongHyeon 		val |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
4181548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
4182548c8f1aSPyun YongHyeon 		    BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
4183cc085b36SPyun YongHyeon 	pci_write_config(dev, BGE_PCI_PCISTATE, val, 4);
418495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
418595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
4186cbb2b2feSPyun YongHyeon 	/*
4187cbb2b2feSPyun YongHyeon 	 * Disable PCI-X relaxed ordering to ensure status block update
4188fa8b4d63SPyun YongHyeon 	 * comes first then packet buffer DMA. Otherwise driver may
4189cbb2b2feSPyun YongHyeon 	 * read stale status block.
4190cbb2b2feSPyun YongHyeon 	 */
4191cbb2b2feSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_PCIX) {
4192cbb2b2feSPyun YongHyeon 		devctl = pci_read_config(dev,
4193cbb2b2feSPyun YongHyeon 		    sc->bge_pcixcap + PCIXR_COMMAND, 2);
4194cbb2b2feSPyun YongHyeon 		devctl &= ~PCIXM_COMMAND_ERO;
4195cbb2b2feSPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
4196cbb2b2feSPyun YongHyeon 			devctl &= ~PCIXM_COMMAND_MAX_READ;
4197cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4198cbb2b2feSPyun YongHyeon 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4199cbb2b2feSPyun YongHyeon 			devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
4200cbb2b2feSPyun YongHyeon 			    PCIXM_COMMAND_MAX_READ);
4201cbb2b2feSPyun YongHyeon 			devctl |= PCIXM_COMMAND_MAX_READ_2048;
4202cbb2b2feSPyun YongHyeon 		}
4203cbb2b2feSPyun YongHyeon 		pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
4204cbb2b2feSPyun YongHyeon 		    devctl, 2);
4205cbb2b2feSPyun YongHyeon 	}
420622a4ecedSMarius Strobl 	/* Re-enable MSI, if necessary, and enable the memory arbiter. */
42074c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
4208bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
4209bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
42100aaf1057SPyun YongHyeon 			val = pci_read_config(dev,
42110aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL, 2);
42120aaf1057SPyun YongHyeon 			pci_write_config(dev,
42130aaf1057SPyun YongHyeon 			    sc->bge_msicap + PCIR_MSI_CTRL,
4214bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
4215bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
4216bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
4217bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
4218bf6ef57aSJohn Polstra 		}
42194c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
42204c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
42214c0da0ffSGleb Smirnoff 	} else
4222a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4223a7b0c314SPaul Saab 
4224cc085b36SPyun YongHyeon 	/* Fix up byte swapping. */
4225cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
4226cc085b36SPyun YongHyeon 
4227cc085b36SPyun YongHyeon 	val = CSR_READ_4(sc, BGE_MAC_MODE);
4228cc085b36SPyun YongHyeon 	val = (val & ~mac_mode_mask) | mac_mode;
4229cc085b36SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
4230cc085b36SPyun YongHyeon 	DELAY(40);
4231cc085b36SPyun YongHyeon 
4232548c8f1aSPyun YongHyeon 	bge_ape_unlock(sc, BGE_APE_LOCK_GRC);
4233548c8f1aSPyun YongHyeon 
423438cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
423538cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
423638cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
423738cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
423838cc658fSJohn Baldwin 				break;
423938cc658fSJohn Baldwin 			DELAY(100);
424038cc658fSJohn Baldwin 		}
424138cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
4242333704a3SPyun YongHyeon 			device_printf(dev, "reset timed out\n");
424338cc658fSJohn Baldwin 			return (1);
424438cc658fSJohn Baldwin 		}
424538cc658fSJohn Baldwin 	} else {
424695d67482SBill Paul 		/*
42476f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
424808013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
42495fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
42505fea260fSMarius Strobl 		 * address is fitted though.
425195d67482SBill Paul 		 */
425295d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
4253d5d23857SJung-uk Kim 			DELAY(10);
4254888b47f0SPyun YongHyeon 			val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
4255888b47f0SPyun YongHyeon 			if (val == ~BGE_SRAM_FW_MB_MAGIC)
425695d67482SBill Paul 				break;
425795d67482SBill Paul 		}
425895d67482SBill Paul 
42595fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
4260333704a3SPyun YongHyeon 			device_printf(dev,
4261333704a3SPyun YongHyeon 			    "firmware handshake timed out, found 0x%08x\n",
4262333704a3SPyun YongHyeon 			    val);
4263b4a256acSPyun YongHyeon 		/* BCM57765 A0 needs additional time before accessing. */
4264b4a256acSPyun YongHyeon 		if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
4265b4a256acSPyun YongHyeon 			DELAY(10 * 1000);	/* XXX */
426638cc658fSJohn Baldwin 	}
426795d67482SBill Paul 
426895d67482SBill Paul 	/*
4269da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
4270da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
4271da3003f0SBill Paul 	 * to 1.2V.
4272da3003f0SBill Paul 	 */
4273652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
4274652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
42755fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
42765fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
42775fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
4278da3003f0SBill Paul 	}
4279da3003f0SBill Paul 
4280e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
4281652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
4282b4a256acSPyun YongHyeon 	    !BGE_IS_5717_PLUS(sc) &&
4283a5ad2f15SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
4284a5ad2f15SPyun YongHyeon 	    sc->bge_asicrev != BGE_ASICREV_BCM5785) {
4285a5ad2f15SPyun YongHyeon 		/* Enable Data FIFO protection. */
42865fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
42875fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
4288e53d81eeSPaul Saab 	}
42898cb1383cSDoug Ambrisko 
429050515680SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
429150515680SPyun YongHyeon 		BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
429250515680SPyun YongHyeon 		    CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
429350515680SPyun YongHyeon 
42948cb1383cSDoug Ambrisko 	return (0);
429595d67482SBill Paul }
429695d67482SBill Paul 
4297e0b7b101SPyun YongHyeon static __inline void
4298e0b7b101SPyun YongHyeon bge_rxreuse_std(struct bge_softc *sc, int i)
4299e0b7b101SPyun YongHyeon {
4300e0b7b101SPyun YongHyeon 	struct bge_rx_bd *r;
4301e0b7b101SPyun YongHyeon 
4302e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
4303e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
4304e0b7b101SPyun YongHyeon 	r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
4305e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4306e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
4307e0b7b101SPyun YongHyeon }
4308e0b7b101SPyun YongHyeon 
4309e0b7b101SPyun YongHyeon static __inline void
4310e0b7b101SPyun YongHyeon bge_rxreuse_jumbo(struct bge_softc *sc, int i)
4311e0b7b101SPyun YongHyeon {
4312e0b7b101SPyun YongHyeon 	struct bge_extrx_bd *r;
4313e0b7b101SPyun YongHyeon 
4314e0b7b101SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
4315e0b7b101SPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
4316e0b7b101SPyun YongHyeon 	r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
4317e0b7b101SPyun YongHyeon 	r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
4318e0b7b101SPyun YongHyeon 	r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
4319e0b7b101SPyun YongHyeon 	r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
4320e0b7b101SPyun YongHyeon 	r->bge_idx = i;
4321e0b7b101SPyun YongHyeon 	BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
4322e0b7b101SPyun YongHyeon }
4323e0b7b101SPyun YongHyeon 
432495d67482SBill Paul /*
432595d67482SBill Paul  * Frame reception handling. This is called if there's a frame
432695d67482SBill Paul  * on the receive return list.
432795d67482SBill Paul  *
432895d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
43291be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
433095d67482SBill Paul  * 2) the frame is from the standard receive ring
433195d67482SBill Paul  */
433295d67482SBill Paul 
43331abcdbd1SAttilio Rao static int
4334dfe0df9aSPyun YongHyeon bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
433595d67482SBill Paul {
4336fba8b109SMarcel Moolenaar 	if_t ifp;
43371abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
4338b9c05fa5SPyun YongHyeon 	uint16_t rx_cons;
433995d67482SBill Paul 
43407f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
43410f9bd73bSSam Leffler 
43423f74909aSGleb Smirnoff 	/* Nothing to do. */
43437f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
43441abcdbd1SAttilio Rao 		return (rx_npkts);
4345cfcb5025SOleg Bulyzhin 
4346fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
434795d67482SBill Paul 
4348f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
4349e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
4350f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
435115eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
4352f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
4353fba8b109SMarcel Moolenaar 	    if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
4354fba8b109SMarcel Moolenaar 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))
4355f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
435615eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
4357f41ac2beSBill Paul 
43587f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
435995d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
43603f74909aSGleb Smirnoff 		uint32_t		rxidx;
436195d67482SBill Paul 		struct mbuf		*m = NULL;
43623f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
436395d67482SBill Paul 		int			have_tag = 0;
436495d67482SBill Paul 
436575719184SGleb Smirnoff #ifdef DEVICE_POLLING
4366fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_POLLING) {
436775719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
436875719184SGleb Smirnoff 				break;
436975719184SGleb Smirnoff 			sc->rxcycles--;
437075719184SGleb Smirnoff 		}
437175719184SGleb Smirnoff #endif
437275719184SGleb Smirnoff 
43737f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
437495d67482SBill Paul 
437595d67482SBill Paul 		rxidx = cur_rx->bge_idx;
43767f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
437795d67482SBill Paul 
4378fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING &&
4379cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
438095d67482SBill Paul 			have_tag = 1;
438195d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
438295d67482SBill Paul 		}
438395d67482SBill Paul 
438495d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
438595d67482SBill Paul 			jumbocnt++;
4386943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
438795d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4388e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
438995d67482SBill Paul 				continue;
439095d67482SBill Paul 			}
4391943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
4392e0b7b101SPyun YongHyeon 				bge_rxreuse_jumbo(sc, rxidx);
4393df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
439495d67482SBill Paul 				continue;
439595d67482SBill Paul 			}
439603e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
439795d67482SBill Paul 		} else {
439895d67482SBill Paul 			stdcnt++;
4399e0b7b101SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
440095d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4401e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
440295d67482SBill Paul 				continue;
440395d67482SBill Paul 			}
4404943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
4405e0b7b101SPyun YongHyeon 				bge_rxreuse_std(sc, rxidx);
4406df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
440795d67482SBill Paul 				continue;
440895d67482SBill Paul 			}
440903e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
441095d67482SBill Paul 		}
441195d67482SBill Paul 
4412df360178SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
4413e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
4414e255b776SJohn Polstra 		/*
4415e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
4416e65bed95SPyun YongHyeon 		 * the payload is aligned.
4417e255b776SJohn Polstra 		 */
4418652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
4419e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
4420e255b776SJohn Polstra 			    cur_rx->bge_len);
4421e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
4422e255b776SJohn Polstra 		}
4423e255b776SJohn Polstra #endif
4424473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
442595d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
442695d67482SBill Paul 
4427fba8b109SMarcel Moolenaar 		if (if_getcapenable(ifp) & IFCAP_RXCSUM)
44281108273aSPyun YongHyeon 			bge_rxcsum(sc, cur_rx, m);
442995d67482SBill Paul 
443095d67482SBill Paul 		/*
4431673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
4432673d9191SSam Leffler 		 * attach that information to the packet.
443395d67482SBill Paul 		 */
4434d147662cSGleb Smirnoff 		if (have_tag) {
443578ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
443678ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
4437d147662cSGleb Smirnoff 		}
443895d67482SBill Paul 
4439dfe0df9aSPyun YongHyeon 		if (holdlck != 0) {
44400f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
4441fba8b109SMarcel Moolenaar 			if_input(ifp, m);
44420f9bd73bSSam Leffler 			BGE_LOCK(sc);
4443dfe0df9aSPyun YongHyeon 		} else
4444fba8b109SMarcel Moolenaar 			if_input(ifp, m);
4445d4da719cSAttilio Rao 		rx_npkts++;
444625e13e68SXin LI 
4447fba8b109SMarcel Moolenaar 		if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
44488cf7d13dSAttilio Rao 			return (rx_npkts);
444995d67482SBill Paul 	}
445095d67482SBill Paul 
445115eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
445215eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
4453e65bed95SPyun YongHyeon 	if (stdcnt > 0)
4454f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
4455e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
44564c0da0ffSGleb Smirnoff 
4457c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
4458f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
44594c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
4460f41ac2beSBill Paul 
44617f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
446238cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
446395d67482SBill Paul 	if (stdcnt)
4464767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
4465767c3593SPyun YongHyeon 		    BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
446695d67482SBill Paul 	if (jumbocnt)
4467767c3593SPyun YongHyeon 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
4468767c3593SPyun YongHyeon 		    BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
4469f5a034f9SPyun YongHyeon #ifdef notyet
4470f5a034f9SPyun YongHyeon 	/*
4471f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
4472f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
4473f5a034f9SPyun YongHyeon 	 */
4474f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
4475fba8b109SMarcel Moolenaar 		if_incierrors(ifp, CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS));
4476f5a034f9SPyun YongHyeon #endif
44771abcdbd1SAttilio Rao 	return (rx_npkts);
447895d67482SBill Paul }
447995d67482SBill Paul 
448095d67482SBill Paul static void
44811108273aSPyun YongHyeon bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
44821108273aSPyun YongHyeon {
44831108273aSPyun YongHyeon 
44841108273aSPyun YongHyeon 	if (BGE_IS_5717_PLUS(sc)) {
44851108273aSPyun YongHyeon 		if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
44861108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
44871108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
44881108273aSPyun YongHyeon 				if ((cur_rx->bge_error_flag &
44891108273aSPyun YongHyeon 				    BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
44901108273aSPyun YongHyeon 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
44911108273aSPyun YongHyeon 			}
44921108273aSPyun YongHyeon 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
44931108273aSPyun YongHyeon 				m->m_pkthdr.csum_data =
44941108273aSPyun YongHyeon 				    cur_rx->bge_tcp_udp_csum;
44951108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
44961108273aSPyun YongHyeon 				    CSUM_PSEUDO_HDR;
44971108273aSPyun YongHyeon 			}
44981108273aSPyun YongHyeon 		}
44991108273aSPyun YongHyeon 	} else {
45001108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
45011108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
45021108273aSPyun YongHyeon 			if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
45031108273aSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
45041108273aSPyun YongHyeon 		}
45051108273aSPyun YongHyeon 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
45061108273aSPyun YongHyeon 		    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
45071108273aSPyun YongHyeon 			m->m_pkthdr.csum_data =
45081108273aSPyun YongHyeon 			    cur_rx->bge_tcp_udp_csum;
45091108273aSPyun YongHyeon 			m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
45101108273aSPyun YongHyeon 			    CSUM_PSEUDO_HDR;
45111108273aSPyun YongHyeon 		}
45121108273aSPyun YongHyeon 	}
45131108273aSPyun YongHyeon }
45141108273aSPyun YongHyeon 
45151108273aSPyun YongHyeon static void
4516b9c05fa5SPyun YongHyeon bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
451795d67482SBill Paul {
451895a0a340SPyun YongHyeon 	struct bge_tx_bd *cur_tx;
4519fba8b109SMarcel Moolenaar 	if_t ifp;
452095d67482SBill Paul 
45210f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
45220f9bd73bSSam Leffler 
45233f74909aSGleb Smirnoff 	/* Nothing to do. */
4524b9c05fa5SPyun YongHyeon 	if (sc->bge_tx_saved_considx == tx_cons)
4525cfcb5025SOleg Bulyzhin 		return;
4526cfcb5025SOleg Bulyzhin 
4527fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
452895d67482SBill Paul 
4529e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
45305c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
453195d67482SBill Paul 	/*
453295d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
453395d67482SBill Paul 	 * frames that have been sent.
453495d67482SBill Paul 	 */
4535b9c05fa5SPyun YongHyeon 	while (sc->bge_tx_saved_considx != tx_cons) {
453695a0a340SPyun YongHyeon 		uint32_t		idx;
453795d67482SBill Paul 
453895d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
4539f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
454095d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
4541df360178SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
454295d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
45430ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
4544e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
4545e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
45460ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
4547f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
4548e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
4549e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
455095d67482SBill Paul 		}
455195d67482SBill Paul 		sc->bge_txcnt--;
455295d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
455395d67482SBill Paul 	}
455495d67482SBill Paul 
4555fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
45565b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
45575b01e77cSBruce Evans 		sc->bge_timer = 0;
455895d67482SBill Paul }
455995d67482SBill Paul 
456075719184SGleb Smirnoff #ifdef DEVICE_POLLING
45611abcdbd1SAttilio Rao static int
4562fba8b109SMarcel Moolenaar bge_poll(if_t ifp, enum poll_cmd cmd, int count)
456375719184SGleb Smirnoff {
4564fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
4565b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4566366454f2SOleg Bulyzhin 	uint32_t statusword;
45671abcdbd1SAttilio Rao 	int rx_npkts = 0;
456875719184SGleb Smirnoff 
45693f74909aSGleb Smirnoff 	BGE_LOCK(sc);
4570fba8b109SMarcel Moolenaar 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
45713f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
45721abcdbd1SAttilio Rao 		return (rx_npkts);
45733f74909aSGleb Smirnoff 	}
457475719184SGleb Smirnoff 
4575dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4576b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4577b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
45782246e8c6SPyun YongHyeon 	/* Fetch updates from the status block. */
4579b9c05fa5SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4580b9c05fa5SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4581dab5cd05SOleg Bulyzhin 
4582175f8742SPyun YongHyeon 	statusword = sc->bge_ldata.bge_status_block->bge_status;
45832246e8c6SPyun YongHyeon 	/* Clear the status so the next pass only sees the changes. */
4584175f8742SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4585dab5cd05SOleg Bulyzhin 
4586dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4587b9c05fa5SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4588b9c05fa5SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4589366454f2SOleg Bulyzhin 
45900c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
4591366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
4592366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
4593366454f2SOleg Bulyzhin 
4594366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
4595366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
45964c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4597652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
4598366454f2SOleg Bulyzhin 			bge_link_upd(sc);
4599366454f2SOleg Bulyzhin 
4600366454f2SOleg Bulyzhin 	sc->rxcycles = count;
4601dfe0df9aSPyun YongHyeon 	rx_npkts = bge_rxeof(sc, rx_prod, 1);
4602fba8b109SMarcel Moolenaar 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
460325e13e68SXin LI 		BGE_UNLOCK(sc);
46048cf7d13dSAttilio Rao 		return (rx_npkts);
460525e13e68SXin LI 	}
4606b9c05fa5SPyun YongHyeon 	bge_txeof(sc, tx_cons);
4607fba8b109SMarcel Moolenaar 	if (!if_sendq_empty(ifp))
4608366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
46093f74909aSGleb Smirnoff 
46103f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
46111abcdbd1SAttilio Rao 	return (rx_npkts);
461275719184SGleb Smirnoff }
461375719184SGleb Smirnoff #endif /* DEVICE_POLLING */
461475719184SGleb Smirnoff 
4615dfe0df9aSPyun YongHyeon static int
4616dfe0df9aSPyun YongHyeon bge_msi_intr(void *arg)
4617dfe0df9aSPyun YongHyeon {
4618dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4619dfe0df9aSPyun YongHyeon 
4620dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4621dfe0df9aSPyun YongHyeon 	/*
4622dfe0df9aSPyun YongHyeon 	 * This interrupt is not shared and controller already
4623dfe0df9aSPyun YongHyeon 	 * disabled further interrupt.
4624dfe0df9aSPyun YongHyeon 	 */
4625dfe0df9aSPyun YongHyeon 	taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4626dfe0df9aSPyun YongHyeon 	return (FILTER_HANDLED);
4627dfe0df9aSPyun YongHyeon }
4628dfe0df9aSPyun YongHyeon 
4629dfe0df9aSPyun YongHyeon static void
4630dfe0df9aSPyun YongHyeon bge_intr_task(void *arg, int pending)
4631dfe0df9aSPyun YongHyeon {
4632dfe0df9aSPyun YongHyeon 	struct bge_softc *sc;
4633fba8b109SMarcel Moolenaar 	if_t ifp;
46341108273aSPyun YongHyeon 	uint32_t status, status_tag;
4635dfe0df9aSPyun YongHyeon 	uint16_t rx_prod, tx_cons;
4636dfe0df9aSPyun YongHyeon 
4637dfe0df9aSPyun YongHyeon 	sc = (struct bge_softc *)arg;
4638dfe0df9aSPyun YongHyeon 	ifp = sc->bge_ifp;
4639dfe0df9aSPyun YongHyeon 
464066151edfSPyun YongHyeon 	BGE_LOCK(sc);
4641fba8b109SMarcel Moolenaar 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
464266151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4643dfe0df9aSPyun YongHyeon 		return;
464466151edfSPyun YongHyeon 	}
4645dfe0df9aSPyun YongHyeon 
4646dfe0df9aSPyun YongHyeon 	/* Get updated status block. */
4647dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4648dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4649dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4650dfe0df9aSPyun YongHyeon 
46512246e8c6SPyun YongHyeon 	/* Save producer/consumer indices. */
4652dfe0df9aSPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4653dfe0df9aSPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4654dfe0df9aSPyun YongHyeon 	status = sc->bge_ldata.bge_status_block->bge_status;
46551108273aSPyun YongHyeon 	status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
46562246e8c6SPyun YongHyeon 	/* Dirty the status flag. */
4657dfe0df9aSPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4658dfe0df9aSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4659dfe0df9aSPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4660dfe0df9aSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
46611108273aSPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
46621108273aSPyun YongHyeon 		status_tag = 0;
466366151edfSPyun YongHyeon 
466466151edfSPyun YongHyeon 	if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
466566151edfSPyun YongHyeon 		bge_link_upd(sc);
466666151edfSPyun YongHyeon 
4667dfe0df9aSPyun YongHyeon 	/* Let controller work. */
46681108273aSPyun YongHyeon 	bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4669dfe0df9aSPyun YongHyeon 
4670fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING &&
467166151edfSPyun YongHyeon 	    sc->bge_rx_saved_considx != rx_prod) {
4672dfe0df9aSPyun YongHyeon 		/* Check RX return ring producer/consumer. */
467366151edfSPyun YongHyeon 		BGE_UNLOCK(sc);
4674dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 0);
467566151edfSPyun YongHyeon 		BGE_LOCK(sc);
4676dfe0df9aSPyun YongHyeon 	}
4677fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
4678dfe0df9aSPyun YongHyeon 		/* Check TX ring producer/consumer. */
4679dfe0df9aSPyun YongHyeon 		bge_txeof(sc, tx_cons);
4680fba8b109SMarcel Moolenaar 		if (!if_sendq_empty(ifp))
4681dfe0df9aSPyun YongHyeon 			bge_start_locked(ifp);
4682dfe0df9aSPyun YongHyeon 	}
468366151edfSPyun YongHyeon 	BGE_UNLOCK(sc);
4684dfe0df9aSPyun YongHyeon }
4685dfe0df9aSPyun YongHyeon 
468695d67482SBill Paul static void
46873f74909aSGleb Smirnoff bge_intr(void *xsc)
468895d67482SBill Paul {
468995d67482SBill Paul 	struct bge_softc *sc;
4690fba8b109SMarcel Moolenaar 	if_t ifp;
4691dab5cd05SOleg Bulyzhin 	uint32_t statusword;
4692b9c05fa5SPyun YongHyeon 	uint16_t rx_prod, tx_cons;
469395d67482SBill Paul 
469495d67482SBill Paul 	sc = xsc;
4695f41ac2beSBill Paul 
46960f9bd73bSSam Leffler 	BGE_LOCK(sc);
46970f9bd73bSSam Leffler 
4698dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
4699dab5cd05SOleg Bulyzhin 
470075719184SGleb Smirnoff #ifdef DEVICE_POLLING
4701fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
470275719184SGleb Smirnoff 		BGE_UNLOCK(sc);
470375719184SGleb Smirnoff 		return;
470475719184SGleb Smirnoff 	}
470575719184SGleb Smirnoff #endif
470675719184SGleb Smirnoff 
4707f30cbfc6SScott Long 	/*
4708b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
4709b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
4710b848e032SBruce Evans 	 * our current organization this just gives complications and
4711b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
4712b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
4713b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
4714b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
4715b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
4716b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
4717b848e032SBruce Evans 	 *
4718b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
4719b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
4720b848e032SBruce Evans 	 * changing later because it is more efficient to get another
4721b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
4722b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
4723b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
4724b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
4725b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
4726b848e032SBruce Evans 	 */
472738cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4728b848e032SBruce Evans 
4729f584dfd1SPyun YongHyeon 	/*
4730f584dfd1SPyun YongHyeon 	 * Do the mandatory PCI flush as well as get the link status.
4731f584dfd1SPyun YongHyeon 	 */
4732f584dfd1SPyun YongHyeon 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4733f584dfd1SPyun YongHyeon 
4734f584dfd1SPyun YongHyeon 	/* Make sure the descriptor ring indexes are coherent. */
4735f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4736f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4737f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4738f584dfd1SPyun YongHyeon 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4739f584dfd1SPyun YongHyeon 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4740f584dfd1SPyun YongHyeon 	sc->bge_ldata.bge_status_block->bge_status = 0;
4741f584dfd1SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4742f584dfd1SPyun YongHyeon 	    sc->bge_cdata.bge_status_map,
4743f584dfd1SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4744f584dfd1SPyun YongHyeon 
47451f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
47464c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4747f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
4748dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
474995d67482SBill Paul 
4750fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
47513f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
4752dfe0df9aSPyun YongHyeon 		bge_rxeof(sc, rx_prod, 1);
475325e13e68SXin LI 	}
475495d67482SBill Paul 
4755fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
47563f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
4757b9c05fa5SPyun YongHyeon 		bge_txeof(sc, tx_cons);
475895d67482SBill Paul 	}
475995d67482SBill Paul 
4760fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING &&
4761fba8b109SMarcel Moolenaar 	    !if_sendq_empty(ifp))
47620f9bd73bSSam Leffler 		bge_start_locked(ifp);
47630f9bd73bSSam Leffler 
47640f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
476595d67482SBill Paul }
476695d67482SBill Paul 
476795d67482SBill Paul static void
47688cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
47698cb1383cSDoug Ambrisko {
47708cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
47718cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
47728cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
47738cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
47748cb1383cSDoug Ambrisko 		else {
4775899d6846SPyun YongHyeon 			sc->bge_asf_count = 2;
4776888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
47773c201200SPyun YongHyeon 			    BGE_FW_CMD_DRV_ALIVE);
4778888b47f0SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4779941a6e13SPyun YongHyeon 			bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4780941a6e13SPyun YongHyeon 			    BGE_FW_HB_TIMEOUT_SEC);
47813fed2d5dSPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
47829931ba85SPyun YongHyeon 			    CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
47839931ba85SPyun YongHyeon 			    BGE_RX_CPU_DRV_EVENT);
47848cb1383cSDoug Ambrisko 		}
47858cb1383cSDoug Ambrisko 	}
47868cb1383cSDoug Ambrisko }
47878cb1383cSDoug Ambrisko 
47888cb1383cSDoug Ambrisko static void
4789b74e67fbSGleb Smirnoff bge_tick(void *xsc)
47900f9bd73bSSam Leffler {
4791b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
479295d67482SBill Paul 	struct mii_data *mii = NULL;
479395d67482SBill Paul 
47940f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
479595d67482SBill Paul 
47965dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
47975dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
47985dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
47995dda8085SOleg Bulyzhin 		return;
48005dda8085SOleg Bulyzhin 
48017ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
48020434d1b8SBill Paul 		bge_stats_update_regs(sc);
48030434d1b8SBill Paul 	else
480495d67482SBill Paul 		bge_stats_update(sc);
480595d67482SBill Paul 
4806548c8f1aSPyun YongHyeon 	/* XXX Add APE heartbeat check here? */
4807548c8f1aSPyun YongHyeon 
4808652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
480995d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
481082b67c01SOleg Bulyzhin 		/*
481182b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
481282b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
481382b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
481482b67c01SOleg Bulyzhin 		 */
481582b67c01SOleg Bulyzhin 		if (!sc->bge_link)
481695d67482SBill Paul 			mii_tick(mii);
48177b97099dSOleg Bulyzhin 	} else {
48187b97099dSOleg Bulyzhin 		/*
48197b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
48207b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
48217b97099dSOleg Bulyzhin 		 * and trigger interrupt.
48227b97099dSOleg Bulyzhin 		 */
48237b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
48243f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
482559578ee0SSergey Kandaurov 		if (!(if_getcapenable(sc->bge_ifp) & IFCAP_POLLING))
48267b97099dSOleg Bulyzhin #endif
48277b97099dSOleg Bulyzhin 		{
48287b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
48294f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
48304f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
48317b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
48324f0794ffSBjoern A. Zeeb 		else
48334f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
48347b97099dSOleg Bulyzhin 		}
4835dab5cd05SOleg Bulyzhin 	}
483695d67482SBill Paul 
48378cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
4838b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
48398cb1383cSDoug Ambrisko 
4840dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
484195d67482SBill Paul }
484295d67482SBill Paul 
484395d67482SBill Paul static void
48443f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
48450434d1b8SBill Paul {
4846fba8b109SMarcel Moolenaar 	if_t ifp;
48472280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
484829b44b09SPyun YongHyeon 	uint32_t val;
48490434d1b8SBill Paul 
4850fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
48512280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
48520434d1b8SBill Paul 
48532280c16bSPyun YongHyeon 	stats->ifHCOutOctets +=
48542280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
48552280c16bSPyun YongHyeon 	stats->etherStatsCollisions +=
48562280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
48572280c16bSPyun YongHyeon 	stats->outXonSent +=
48582280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
48592280c16bSPyun YongHyeon 	stats->outXoffSent +=
48602280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
48612280c16bSPyun YongHyeon 	stats->dot3StatsInternalMacTransmitErrors +=
48622280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
48632280c16bSPyun YongHyeon 	stats->dot3StatsSingleCollisionFrames +=
48642280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
48652280c16bSPyun YongHyeon 	stats->dot3StatsMultipleCollisionFrames +=
48662280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
48672280c16bSPyun YongHyeon 	stats->dot3StatsDeferredTransmissions +=
48682280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
48692280c16bSPyun YongHyeon 	stats->dot3StatsExcessiveCollisions +=
48702280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
48712280c16bSPyun YongHyeon 	stats->dot3StatsLateCollisions +=
48722280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
48732280c16bSPyun YongHyeon 	stats->ifHCOutUcastPkts +=
48742280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
48752280c16bSPyun YongHyeon 	stats->ifHCOutMulticastPkts +=
48762280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
48772280c16bSPyun YongHyeon 	stats->ifHCOutBroadcastPkts +=
48782280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
48797e6e2507SJung-uk Kim 
48802280c16bSPyun YongHyeon 	stats->ifHCInOctets +=
48812280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
48822280c16bSPyun YongHyeon 	stats->etherStatsFragments +=
48832280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
48842280c16bSPyun YongHyeon 	stats->ifHCInUcastPkts +=
48852280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
48862280c16bSPyun YongHyeon 	stats->ifHCInMulticastPkts +=
48872280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
48882280c16bSPyun YongHyeon 	stats->ifHCInBroadcastPkts +=
48892280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
48902280c16bSPyun YongHyeon 	stats->dot3StatsFCSErrors +=
48912280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
48922280c16bSPyun YongHyeon 	stats->dot3StatsAlignmentErrors +=
48932280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
48942280c16bSPyun YongHyeon 	stats->xonPauseFramesReceived +=
48952280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
48962280c16bSPyun YongHyeon 	stats->xoffPauseFramesReceived +=
48972280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
48982280c16bSPyun YongHyeon 	stats->macControlFramesReceived +=
48992280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
49002280c16bSPyun YongHyeon 	stats->xoffStateEntered +=
49012280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
49022280c16bSPyun YongHyeon 	stats->dot3StatsFramesTooLong +=
49032280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
49042280c16bSPyun YongHyeon 	stats->etherStatsJabbers +=
49052280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
49062280c16bSPyun YongHyeon 	stats->etherStatsUndersizePkts +=
49072280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
49082280c16bSPyun YongHyeon 
49092280c16bSPyun YongHyeon 	stats->FramesDroppedDueToFilters +=
49102280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
49112280c16bSPyun YongHyeon 	stats->DmaWriteQueueFull +=
49122280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
49132280c16bSPyun YongHyeon 	stats->DmaWriteHighPriQueueFull +=
49142280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
49152280c16bSPyun YongHyeon 	stats->NoMoreRxBDs +=
49162280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4917f78094a5SPyun YongHyeon 	/*
4918f78094a5SPyun YongHyeon 	 * XXX
4919f78094a5SPyun YongHyeon 	 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4920f78094a5SPyun YongHyeon 	 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4921f78094a5SPyun YongHyeon 	 * includes number of unwanted multicast frames.  This comes
4922f78094a5SPyun YongHyeon 	 * from silicon bug and known workaround to get rough(not
4923f78094a5SPyun YongHyeon 	 * exact) counter is to enable interrupt on MBUF low water
4924f78094a5SPyun YongHyeon 	 * attention.  This can be accomplished by setting
4925f78094a5SPyun YongHyeon 	 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4926f78094a5SPyun YongHyeon 	 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4927f78094a5SPyun YongHyeon 	 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4928f78094a5SPyun YongHyeon 	 * However that change would generate more interrupts and
4929f78094a5SPyun YongHyeon 	 * there are still possibilities of losing multiple frames
4930f78094a5SPyun YongHyeon 	 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4931f78094a5SPyun YongHyeon 	 * Given that the workaround still would not get correct
4932f78094a5SPyun YongHyeon 	 * counter I don't think it's worth to implement it.  So
4933f78094a5SPyun YongHyeon 	 * ignore reading the counter on controllers that have the
4934f78094a5SPyun YongHyeon 	 * silicon bug.
4935f78094a5SPyun YongHyeon 	 */
4936f78094a5SPyun YongHyeon 	if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4937f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4938f78094a5SPyun YongHyeon 	    sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
49392280c16bSPyun YongHyeon 		stats->InputDiscards +=
49402280c16bSPyun YongHyeon 		    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
49412280c16bSPyun YongHyeon 	stats->InputErrors +=
49422280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
49432280c16bSPyun YongHyeon 	stats->RecvThresholdHit +=
49442280c16bSPyun YongHyeon 	    CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
49452280c16bSPyun YongHyeon 
494629b44b09SPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
494729b44b09SPyun YongHyeon 		/*
494829b44b09SPyun YongHyeon 		 * If controller transmitted more than BGE_NUM_RDMA_CHANNELS
494929b44b09SPyun YongHyeon 		 * frames, it's safe to disable workaround for DMA engine's
495029b44b09SPyun YongHyeon 		 * miscalculation of TXMBUF space.
495129b44b09SPyun YongHyeon 		 */
495229b44b09SPyun YongHyeon 		if (stats->ifHCOutUcastPkts + stats->ifHCOutMulticastPkts +
495329b44b09SPyun YongHyeon 		    stats->ifHCOutBroadcastPkts > BGE_NUM_RDMA_CHANNELS) {
495429b44b09SPyun YongHyeon 			val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
495529b44b09SPyun YongHyeon 			if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
495629b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5719;
495729b44b09SPyun YongHyeon 			else
495829b44b09SPyun YongHyeon 				val &= ~BGE_RDMA_TX_LENGTH_WA_5720;
495929b44b09SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
496029b44b09SPyun YongHyeon 			sc->bge_flags &= ~BGE_FLAG_RDMA_BUG;
496129b44b09SPyun YongHyeon 		}
496229b44b09SPyun YongHyeon 	}
49632280c16bSPyun YongHyeon }
49642280c16bSPyun YongHyeon 
49652280c16bSPyun YongHyeon static void
49662280c16bSPyun YongHyeon bge_stats_clear_regs(struct bge_softc *sc)
49672280c16bSPyun YongHyeon {
49682280c16bSPyun YongHyeon 
49692280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
49702280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
49712280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
49722280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
49732280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
49742280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
49752280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
49762280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
49772280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
49782280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
49792280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
49802280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
49812280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
49822280c16bSPyun YongHyeon 
49832280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
49842280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
49852280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
49862280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
49872280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
49882280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
49892280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
49902280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
49912280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
49922280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
49932280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
49942280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
49952280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
49962280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
49972280c16bSPyun YongHyeon 
49982280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
49992280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
50002280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
50012280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
50022280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
50032280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
50042280c16bSPyun YongHyeon 	CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
50050434d1b8SBill Paul }
50060434d1b8SBill Paul 
50070434d1b8SBill Paul static void
50083f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
500995d67482SBill Paul {
5010fba8b109SMarcel Moolenaar 	if_t ifp;
5011e907febfSPyun YongHyeon 	bus_size_t stats;
50127e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
501395d67482SBill Paul 
5014fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
501595d67482SBill Paul 
5016e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
5017e907febfSPyun YongHyeon 
5018e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
5019e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
502095d67482SBill Paul 
50218634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
5022df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, cnt - sc->bge_tx_collisions);
50236fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
50246fb34dd2SOleg Bulyzhin 
502537ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, nicNoMoreRxBDs.bge_addr_lo);
5026df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_nobds);
502737ee7cc7SPyun YongHyeon 	sc->bge_rx_nobds = cnt;
502837ee7cc7SPyun YongHyeon 	cnt = READ_STAT(sc, stats, ifInErrors.bge_addr_lo);
5029df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_inerrs);
503037ee7cc7SPyun YongHyeon 	sc->bge_rx_inerrs = cnt;
50316fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
5032df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS, cnt - sc->bge_rx_discards);
50336fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
50346fb34dd2SOleg Bulyzhin 
50356fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
5036df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, cnt - sc->bge_tx_discards);
50376fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
503895d67482SBill Paul 
5039e907febfSPyun YongHyeon #undef	READ_STAT
504095d67482SBill Paul }
504195d67482SBill Paul 
504295d67482SBill Paul /*
5043d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
5044d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
5045d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
5046d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
5047d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
5048d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
5049d375e524SGleb Smirnoff  */
5050d375e524SGleb Smirnoff static __inline int
5051d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
5052d375e524SGleb Smirnoff {
5053d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
5054d375e524SGleb Smirnoff 	struct mbuf *last;
5055d375e524SGleb Smirnoff 
5056d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
5057d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
5058d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
5059d375e524SGleb Smirnoff 		last = m;
5060d375e524SGleb Smirnoff 	} else {
5061d375e524SGleb Smirnoff 		/*
5062d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
5063d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
5064d375e524SGleb Smirnoff 		 */
5065d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
5066d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
5067d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
5068d375e524SGleb Smirnoff 			struct mbuf *n;
5069d375e524SGleb Smirnoff 
5070c6499eccSGleb Smirnoff 			MGET(n, M_NOWAIT, MT_DATA);
5071d375e524SGleb Smirnoff 			if (n == NULL)
5072d375e524SGleb Smirnoff 				return (ENOBUFS);
5073d375e524SGleb Smirnoff 			n->m_len = 0;
5074d375e524SGleb Smirnoff 			last->m_next = n;
5075d375e524SGleb Smirnoff 			last = n;
5076d375e524SGleb Smirnoff 		}
5077d375e524SGleb Smirnoff 	}
5078d375e524SGleb Smirnoff 
5079d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
5080d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
5081d375e524SGleb Smirnoff 	last->m_len += padlen;
5082d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
5083d375e524SGleb Smirnoff 
5084d375e524SGleb Smirnoff 	return (0);
5085d375e524SGleb Smirnoff }
5086d375e524SGleb Smirnoff 
5087ca3f1187SPyun YongHyeon static struct mbuf *
5088d598b626SPyun YongHyeon bge_check_short_dma(struct mbuf *m)
5089d598b626SPyun YongHyeon {
5090d598b626SPyun YongHyeon 	struct mbuf *n;
5091d598b626SPyun YongHyeon 	int found;
5092d598b626SPyun YongHyeon 
5093d598b626SPyun YongHyeon 	/*
5094d598b626SPyun YongHyeon 	 * If device receive two back-to-back send BDs with less than
5095d598b626SPyun YongHyeon 	 * or equal to 8 total bytes then the device may hang.  The two
5096d598b626SPyun YongHyeon 	 * back-to-back send BDs must in the same frame for this failure
5097d598b626SPyun YongHyeon 	 * to occur.  Scan mbuf chains and see whether two back-to-back
5098d598b626SPyun YongHyeon 	 * send BDs are there. If this is the case, allocate new mbuf
5099d598b626SPyun YongHyeon 	 * and copy the frame to workaround the silicon bug.
5100d598b626SPyun YongHyeon 	 */
5101d598b626SPyun YongHyeon 	for (n = m, found = 0; n != NULL; n = n->m_next) {
5102d598b626SPyun YongHyeon 		if (n->m_len < 8) {
5103d598b626SPyun YongHyeon 			found++;
5104d598b626SPyun YongHyeon 			if (found > 1)
5105d598b626SPyun YongHyeon 				break;
5106d598b626SPyun YongHyeon 			continue;
5107d598b626SPyun YongHyeon 		}
5108d598b626SPyun YongHyeon 		found = 0;
5109d598b626SPyun YongHyeon 	}
5110d598b626SPyun YongHyeon 
5111d598b626SPyun YongHyeon 	if (found > 1) {
5112c6499eccSGleb Smirnoff 		n = m_defrag(m, M_NOWAIT);
5113d598b626SPyun YongHyeon 		if (n == NULL)
5114d598b626SPyun YongHyeon 			m_freem(m);
5115d598b626SPyun YongHyeon 	} else
5116d598b626SPyun YongHyeon 		n = m;
5117d598b626SPyun YongHyeon 	return (n);
5118d598b626SPyun YongHyeon }
5119d598b626SPyun YongHyeon 
5120d598b626SPyun YongHyeon static struct mbuf *
51211108273aSPyun YongHyeon bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
51221108273aSPyun YongHyeon     uint16_t *flags)
5123ca3f1187SPyun YongHyeon {
5124ca3f1187SPyun YongHyeon 	struct ip *ip;
5125ca3f1187SPyun YongHyeon 	struct tcphdr *tcp;
5126ca3f1187SPyun YongHyeon 	struct mbuf *n;
5127ca3f1187SPyun YongHyeon 	uint16_t hlen;
51285b355c4fSPyun YongHyeon 	uint32_t poff;
5129ca3f1187SPyun YongHyeon 
5130ca3f1187SPyun YongHyeon 	if (M_WRITABLE(m) == 0) {
5131ca3f1187SPyun YongHyeon 		/* Get a writable copy. */
5132c6499eccSGleb Smirnoff 		n = m_dup(m, M_NOWAIT);
5133ca3f1187SPyun YongHyeon 		m_freem(m);
5134ca3f1187SPyun YongHyeon 		if (n == NULL)
5135ca3f1187SPyun YongHyeon 			return (NULL);
5136ca3f1187SPyun YongHyeon 		m = n;
5137ca3f1187SPyun YongHyeon 	}
51385b355c4fSPyun YongHyeon 	m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
5139ca3f1187SPyun YongHyeon 	if (m == NULL)
5140ca3f1187SPyun YongHyeon 		return (NULL);
51415b355c4fSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
51425b355c4fSPyun YongHyeon 	poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
5143ca3f1187SPyun YongHyeon 	m = m_pullup(m, poff + sizeof(struct tcphdr));
5144ca3f1187SPyun YongHyeon 	if (m == NULL)
5145ca3f1187SPyun YongHyeon 		return (NULL);
5146ca3f1187SPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
51475b355c4fSPyun YongHyeon 	m = m_pullup(m, poff + (tcp->th_off << 2));
5148ca3f1187SPyun YongHyeon 	if (m == NULL)
5149ca3f1187SPyun YongHyeon 		return (NULL);
5150ca3f1187SPyun YongHyeon 	/*
5151ca3f1187SPyun YongHyeon 	 * It seems controller doesn't modify IP length and TCP pseudo
5152ca3f1187SPyun YongHyeon 	 * checksum. These checksum computed by upper stack should be 0.
5153ca3f1187SPyun YongHyeon 	 */
5154ca3f1187SPyun YongHyeon 	*mss = m->m_pkthdr.tso_segsz;
515596486faaSPyun YongHyeon 	ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
5156ca3f1187SPyun YongHyeon 	ip->ip_sum = 0;
5157ca3f1187SPyun YongHyeon 	ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
5158ca3f1187SPyun YongHyeon 	/* Clear pseudo checksum computed by TCP stack. */
515996486faaSPyun YongHyeon 	tcp = (struct tcphdr *)(mtod(m, char *) + poff);
5160ca3f1187SPyun YongHyeon 	tcp->th_sum = 0;
5161ca3f1187SPyun YongHyeon 	/*
5162ca3f1187SPyun YongHyeon 	 * Broadcom controllers uses different descriptor format for
5163ca3f1187SPyun YongHyeon 	 * TSO depending on ASIC revision. Due to TSO-capable firmware
5164ca3f1187SPyun YongHyeon 	 * license issue and lower performance of firmware based TSO
51651108273aSPyun YongHyeon 	 * we only support hardware based TSO.
5166ca3f1187SPyun YongHyeon 	 */
51671108273aSPyun YongHyeon 	/* Calculate header length, incl. TCP/IP options, in 32 bit units. */
5168ca3f1187SPyun YongHyeon 	hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
51691108273aSPyun YongHyeon 	if (sc->bge_flags & BGE_FLAG_TSO3) {
51701108273aSPyun YongHyeon 		/*
51711108273aSPyun YongHyeon 		 * For BCM5717 and newer controllers, hardware based TSO
51721108273aSPyun YongHyeon 		 * uses the 14 lower bits of the bge_mss field to store the
51731108273aSPyun YongHyeon 		 * MSS and the upper 2 bits to store the lowest 2 bits of
51741108273aSPyun YongHyeon 		 * the IP/TCP header length.  The upper 6 bits of the header
51751108273aSPyun YongHyeon 		 * length are stored in the bge_flags[14:10,4] field.  Jumbo
51761108273aSPyun YongHyeon 		 * frames are supported.
51771108273aSPyun YongHyeon 		 */
51781108273aSPyun YongHyeon 		*mss |= ((hlen & 0x3) << 14);
51791108273aSPyun YongHyeon 		*flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
51801108273aSPyun YongHyeon 	} else {
51811108273aSPyun YongHyeon 		/*
51821108273aSPyun YongHyeon 		 * For BCM5755 and newer controllers, hardware based TSO uses
51831108273aSPyun YongHyeon 		 * the lower 11	bits to store the MSS and the upper 5 bits to
51841108273aSPyun YongHyeon 		 * store the IP/TCP header length. Jumbo frames are not
51851108273aSPyun YongHyeon 		 * supported.
51861108273aSPyun YongHyeon 		 */
5187ca3f1187SPyun YongHyeon 		*mss |= (hlen << 11);
51881108273aSPyun YongHyeon 	}
5189ca3f1187SPyun YongHyeon 	return (m);
5190ca3f1187SPyun YongHyeon }
5191ca3f1187SPyun YongHyeon 
5192d375e524SGleb Smirnoff /*
519395d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
519495d67482SBill Paul  * pointers to descriptors.
519595d67482SBill Paul  */
519695d67482SBill Paul static int
5197676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
519895d67482SBill Paul {
51997e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
5200f41ac2beSBill Paul 	bus_dmamap_t		map;
5201676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
5202676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
52037e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
5204ca3f1187SPyun YongHyeon 	uint16_t		csum_flags, mss, vlan_tag;
52057e27542aSGleb Smirnoff 	int			nsegs, i, error;
520695d67482SBill Paul 
52076909dc43SGleb Smirnoff 	csum_flags = 0;
5208ca3f1187SPyun YongHyeon 	mss = 0;
5209ca3f1187SPyun YongHyeon 	vlan_tag = 0;
5210d598b626SPyun YongHyeon 	if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
5211d598b626SPyun YongHyeon 	    m->m_next != NULL) {
5212d598b626SPyun YongHyeon 		*m_head = bge_check_short_dma(m);
5213d598b626SPyun YongHyeon 		if (*m_head == NULL)
5214d598b626SPyun YongHyeon 			return (ENOBUFS);
5215d598b626SPyun YongHyeon 		m = *m_head;
5216d598b626SPyun YongHyeon 	}
5217ca3f1187SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
52181108273aSPyun YongHyeon 		*m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
5219ca3f1187SPyun YongHyeon 		if (*m_head == NULL)
5220ca3f1187SPyun YongHyeon 			return (ENOBUFS);
5221ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
5222ca3f1187SPyun YongHyeon 		    BGE_TXBDFLAG_CPU_POST_DMA;
522335f945cdSPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
52246909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
52256909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
52266909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
52276909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
52286909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
52296909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
52306909dc43SGleb Smirnoff 				m_freem(m);
52316909dc43SGleb Smirnoff 				*m_head = NULL;
52326909dc43SGleb Smirnoff 				return (error);
52336909dc43SGleb Smirnoff 			}
52346909dc43SGleb Smirnoff 		}
52356909dc43SGleb Smirnoff 	}
52366909dc43SGleb Smirnoff 
52371108273aSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
52381108273aSPyun YongHyeon 		if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
52391108273aSPyun YongHyeon 		    m->m_pkthdr.len > ETHER_MAX_LEN)
52401108273aSPyun YongHyeon 			csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
52411108273aSPyun YongHyeon 		if (sc->bge_forced_collapse > 0 &&
5242beaa2ae1SPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
5243d94f2b85SPyun YongHyeon 			/*
5244d94f2b85SPyun YongHyeon 			 * Forcedly collapse mbuf chains to overcome hardware
5245d94f2b85SPyun YongHyeon 			 * limitation which only support a single outstanding
5246d94f2b85SPyun YongHyeon 			 * DMA read operation.
5247d94f2b85SPyun YongHyeon 			 */
5248beaa2ae1SPyun YongHyeon 			if (sc->bge_forced_collapse == 1)
5249c6499eccSGleb Smirnoff 				m = m_defrag(m, M_NOWAIT);
5250d94f2b85SPyun YongHyeon 			else
5251c6499eccSGleb Smirnoff 				m = m_collapse(m, M_NOWAIT,
52521108273aSPyun YongHyeon 				    sc->bge_forced_collapse);
5253261f04d6SPyun YongHyeon 			if (m == NULL)
5254261f04d6SPyun YongHyeon 				m = *m_head;
5255d94f2b85SPyun YongHyeon 			*m_head = m;
5256d94f2b85SPyun YongHyeon 		}
52571108273aSPyun YongHyeon 	}
5258d94f2b85SPyun YongHyeon 
52597e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
52600ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
5261676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
52627e27542aSGleb Smirnoff 	if (error == EFBIG) {
5263c6499eccSGleb Smirnoff 		m = m_collapse(m, M_NOWAIT, BGE_NSEG_NEW);
5264676ad2c9SGleb Smirnoff 		if (m == NULL) {
5265676ad2c9SGleb Smirnoff 			m_freem(*m_head);
5266676ad2c9SGleb Smirnoff 			*m_head = NULL;
52677e27542aSGleb Smirnoff 			return (ENOBUFS);
52687e27542aSGleb Smirnoff 		}
5269676ad2c9SGleb Smirnoff 		*m_head = m;
52700ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
52710ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
5272676ad2c9SGleb Smirnoff 		if (error) {
5273676ad2c9SGleb Smirnoff 			m_freem(m);
5274676ad2c9SGleb Smirnoff 			*m_head = NULL;
52757e27542aSGleb Smirnoff 			return (error);
52767e27542aSGleb Smirnoff 		}
5277676ad2c9SGleb Smirnoff 	} else if (error != 0)
5278676ad2c9SGleb Smirnoff 		return (error);
52797e27542aSGleb Smirnoff 
5280167fdb62SPyun YongHyeon 	/* Check if we have enough free send BDs. */
5281167fdb62SPyun YongHyeon 	if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
52820ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
528395d67482SBill Paul 		return (ENOBUFS);
52847e27542aSGleb Smirnoff 	}
52857e27542aSGleb Smirnoff 
52860ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
5287e65bed95SPyun YongHyeon 
5288ca3f1187SPyun YongHyeon 	if (m->m_flags & M_VLANTAG) {
5289ca3f1187SPyun YongHyeon 		csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
5290ca3f1187SPyun YongHyeon 		vlan_tag = m->m_pkthdr.ether_vtag;
5291ca3f1187SPyun YongHyeon 	}
5292b77d3a3bSPyun YongHyeon 
5293b77d3a3bSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5762 &&
5294b77d3a3bSPyun YongHyeon 	    (m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
5295b77d3a3bSPyun YongHyeon 		/*
5296b77d3a3bSPyun YongHyeon 		 * 5725 family of devices corrupts TSO packets when TSO DMA
5297b77d3a3bSPyun YongHyeon 		 * buffers cross into regions which are within MSS bytes of
5298b77d3a3bSPyun YongHyeon 		 * a 4GB boundary.  If we encounter the condition, drop the
5299b77d3a3bSPyun YongHyeon 		 * packet.
5300b77d3a3bSPyun YongHyeon 		 */
5301b77d3a3bSPyun YongHyeon 		for (i = 0; ; i++) {
5302b77d3a3bSPyun YongHyeon 			d = &sc->bge_ldata.bge_tx_ring[idx];
5303b77d3a3bSPyun YongHyeon 			d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
5304b77d3a3bSPyun YongHyeon 			d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
5305b77d3a3bSPyun YongHyeon 			d->bge_len = segs[i].ds_len;
5306b77d3a3bSPyun YongHyeon 			if (d->bge_addr.bge_addr_lo + segs[i].ds_len + mss <
5307b77d3a3bSPyun YongHyeon 			    d->bge_addr.bge_addr_lo)
5308b77d3a3bSPyun YongHyeon 				break;
5309b77d3a3bSPyun YongHyeon 			d->bge_flags = csum_flags;
5310b77d3a3bSPyun YongHyeon 			d->bge_vlan_tag = vlan_tag;
5311b77d3a3bSPyun YongHyeon 			d->bge_mss = mss;
5312b77d3a3bSPyun YongHyeon 			if (i == nsegs - 1)
5313b77d3a3bSPyun YongHyeon 				break;
5314b77d3a3bSPyun YongHyeon 			BGE_INC(idx, BGE_TX_RING_CNT);
5315b77d3a3bSPyun YongHyeon 		}
5316b77d3a3bSPyun YongHyeon 		if (i != nsegs - 1) {
5317b77d3a3bSPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map,
5318b77d3a3bSPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
5319b77d3a3bSPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
5320b77d3a3bSPyun YongHyeon 			m_freem(*m_head);
5321b77d3a3bSPyun YongHyeon 			*m_head = NULL;
5322b77d3a3bSPyun YongHyeon 			return (EIO);
5323b77d3a3bSPyun YongHyeon 		}
5324b77d3a3bSPyun YongHyeon 	} else {
53257e27542aSGleb Smirnoff 		for (i = 0; ; i++) {
53267e27542aSGleb Smirnoff 			d = &sc->bge_ldata.bge_tx_ring[idx];
53277e27542aSGleb Smirnoff 			d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
53287e27542aSGleb Smirnoff 			d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
53297e27542aSGleb Smirnoff 			d->bge_len = segs[i].ds_len;
53307e27542aSGleb Smirnoff 			d->bge_flags = csum_flags;
5331ca3f1187SPyun YongHyeon 			d->bge_vlan_tag = vlan_tag;
5332ca3f1187SPyun YongHyeon 			d->bge_mss = mss;
53337e27542aSGleb Smirnoff 			if (i == nsegs - 1)
53347e27542aSGleb Smirnoff 				break;
53357e27542aSGleb Smirnoff 			BGE_INC(idx, BGE_TX_RING_CNT);
53367e27542aSGleb Smirnoff 		}
5337b77d3a3bSPyun YongHyeon 	}
53387e27542aSGleb Smirnoff 
53397e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
53407e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
5341676ad2c9SGleb Smirnoff 
5342f41ac2beSBill Paul 	/*
5343f41ac2beSBill Paul 	 * Insure that the map for this transmission
5344f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
5345f41ac2beSBill Paul 	 * in this chain.
5346f41ac2beSBill Paul 	 */
53477e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
53487e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
5349676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
53507e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
535195d67482SBill Paul 
53527e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
53537e27542aSGleb Smirnoff 	*txidx = idx;
535495d67482SBill Paul 
535595d67482SBill Paul 	return (0);
535695d67482SBill Paul }
535795d67482SBill Paul 
535895d67482SBill Paul /*
535995d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
536095d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
536195d67482SBill Paul  */
536295d67482SBill Paul static void
5363fba8b109SMarcel Moolenaar bge_start_locked(if_t ifp)
536495d67482SBill Paul {
536595d67482SBill Paul 	struct bge_softc *sc;
5366167fdb62SPyun YongHyeon 	struct mbuf *m_head;
536714bbd30fSGleb Smirnoff 	uint32_t prodidx;
5368167fdb62SPyun YongHyeon 	int count;
536995d67482SBill Paul 
5370fba8b109SMarcel Moolenaar 	sc = if_getsoftc(ifp);
5371167fdb62SPyun YongHyeon 	BGE_LOCK_ASSERT(sc);
537295d67482SBill Paul 
5373167fdb62SPyun YongHyeon 	if (!sc->bge_link ||
5374fba8b109SMarcel Moolenaar 	    (if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
5375167fdb62SPyun YongHyeon 	    IFF_DRV_RUNNING)
537695d67482SBill Paul 		return;
537795d67482SBill Paul 
537814bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
537995d67482SBill Paul 
5380fba8b109SMarcel Moolenaar 	for (count = 0; !if_sendq_empty(ifp);) {
5381167fdb62SPyun YongHyeon 		if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
5382fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
5383167fdb62SPyun YongHyeon 			break;
5384167fdb62SPyun YongHyeon 		}
5385fba8b109SMarcel Moolenaar 		m_head = if_dequeue(ifp);
538695d67482SBill Paul 		if (m_head == NULL)
538795d67482SBill Paul 			break;
538895d67482SBill Paul 
538995d67482SBill Paul 		/*
539095d67482SBill Paul 		 * Pack the data into the transmit ring. If we
539195d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
539295d67482SBill Paul 		 * for the NIC to drain the ring.
539395d67482SBill Paul 		 */
5394676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
5395676ad2c9SGleb Smirnoff 			if (m_head == NULL)
5396676ad2c9SGleb Smirnoff 				break;
5397fba8b109SMarcel Moolenaar 			if_sendq_prepend(ifp, m_head);
5398fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
539995d67482SBill Paul 			break;
540095d67482SBill Paul 		}
5401303a718cSDag-Erling Smørgrav 		++count;
540295d67482SBill Paul 
540395d67482SBill Paul 		/*
540495d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
540595d67482SBill Paul 		 * to him.
540695d67482SBill Paul 		 */
5407fba8b109SMarcel Moolenaar 		if_bpfmtap(ifp, m_head);
540895d67482SBill Paul 	}
540995d67482SBill Paul 
5410ded66962SMark Johnston 	if (count > 0)
5411ded66962SMark Johnston 		bge_start_tx(sc, prodidx);
5412ded66962SMark Johnston }
5413ded66962SMark Johnston 
5414ded66962SMark Johnston static void
5415ded66962SMark Johnston bge_start_tx(struct bge_softc *sc, uint32_t prodidx)
5416ded66962SMark Johnston {
5417ded66962SMark Johnston 
5418aa94f333SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
54195c1da2faSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
54203f74909aSGleb Smirnoff 	/* Transmit. */
542138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
54223927098fSPaul Saab 	/* 5700 b2 errata */
5423e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
542438cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
542595d67482SBill Paul 
542614bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
542714bbd30fSGleb Smirnoff 
5428ded66962SMark Johnston 	/* Set a timeout in case the chip goes out to lunch. */
5429b584d2b3SPyun YongHyeon 	sc->bge_timer = BGE_TX_TIMEOUT;
543095d67482SBill Paul }
543195d67482SBill Paul 
54320f9bd73bSSam Leffler /*
54330f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
54340f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
54350f9bd73bSSam Leffler  */
543695d67482SBill Paul static void
5437fba8b109SMarcel Moolenaar bge_start(if_t ifp)
543895d67482SBill Paul {
54390f9bd73bSSam Leffler 	struct bge_softc *sc;
54400f9bd73bSSam Leffler 
5441fba8b109SMarcel Moolenaar 	sc = if_getsoftc(ifp);
54420f9bd73bSSam Leffler 	BGE_LOCK(sc);
54430f9bd73bSSam Leffler 	bge_start_locked(ifp);
54440f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
54450f9bd73bSSam Leffler }
54460f9bd73bSSam Leffler 
54470f9bd73bSSam Leffler static void
54483f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
54490f9bd73bSSam Leffler {
5450fba8b109SMarcel Moolenaar 	if_t ifp;
54513f74909aSGleb Smirnoff 	uint16_t *m;
5452f6a65488SPyun YongHyeon 	uint32_t mode;
545395d67482SBill Paul 
54540f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
545595d67482SBill Paul 
5456fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
545795d67482SBill Paul 
5458fba8b109SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
545995d67482SBill Paul 		return;
546095d67482SBill Paul 
546195d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
546295d67482SBill Paul 	bge_stop(sc);
54638cb1383cSDoug Ambrisko 
54648cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
54658cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
546695d67482SBill Paul 	bge_reset(sc);
54678cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
54688cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
54698cb1383cSDoug Ambrisko 
547095d67482SBill Paul 	bge_chipinit(sc);
547195d67482SBill Paul 
547295d67482SBill Paul 	/*
547395d67482SBill Paul 	 * Init the various state machines, ring
547495d67482SBill Paul 	 * control blocks and firmware.
547595d67482SBill Paul 	 */
547695d67482SBill Paul 	if (bge_blockinit(sc)) {
5477fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
547895d67482SBill Paul 		return;
547995d67482SBill Paul 	}
548095d67482SBill Paul 
5481fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
548295d67482SBill Paul 
548395d67482SBill Paul 	/* Specify MTU. */
5484fba8b109SMarcel Moolenaar 	CSR_WRITE_4(sc, BGE_RX_MTU, if_getmtu(ifp) +
5485cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
5486fba8b109SMarcel Moolenaar 	    (if_getcapenable(ifp) & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
548795d67482SBill Paul 
548895d67482SBill Paul 	/* Load our MAC address. */
54893f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
549095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
549195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
549295d67482SBill Paul 
54933e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
54943e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
549595d67482SBill Paul 
549695d67482SBill Paul 	/* Program multicast filter. */
549795d67482SBill Paul 	bge_setmulti(sc);
549895d67482SBill Paul 
5499cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
5500cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
5501cb2eacc7SYaroslav Tykhiy 
550235f945cdSPyun YongHyeon 	/* Override UDP checksum offloading. */
550335f945cdSPyun YongHyeon 	if (sc->bge_forced_udpcsum == 0)
550435f945cdSPyun YongHyeon 		sc->bge_csum_features &= ~CSUM_UDP;
550535f945cdSPyun YongHyeon 	else
550635f945cdSPyun YongHyeon 		sc->bge_csum_features |= CSUM_UDP;
5507fba8b109SMarcel Moolenaar 	if (if_getcapabilities(ifp) & IFCAP_TXCSUM &&
5508fba8b109SMarcel Moolenaar 	    if_getcapenable(ifp) & IFCAP_TXCSUM) {
5509fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, 0, (BGE_CSUM_FEATURES | CSUM_UDP));
5510fba8b109SMarcel Moolenaar 		if_sethwassistbits(ifp, sc->bge_csum_features, 0);
551135f945cdSPyun YongHyeon 	}
551235f945cdSPyun YongHyeon 
551395d67482SBill Paul 	/* Init RX ring. */
55143ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
55153ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
55163ee5d7daSPyun YongHyeon 		bge_stop(sc);
55173ee5d7daSPyun YongHyeon 		return;
55183ee5d7daSPyun YongHyeon 	}
551995d67482SBill Paul 
55200434d1b8SBill Paul 	/*
55210434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
55220434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
55230434d1b8SBill Paul 	 * entry of the ring.
55240434d1b8SBill Paul 	 */
55250434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
55263f74909aSGleb Smirnoff 		uint32_t		v, i;
55270434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
55280434d1b8SBill Paul 			DELAY(20);
55290434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
55300434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
55310434d1b8SBill Paul 				break;
55320434d1b8SBill Paul 		}
55330434d1b8SBill Paul 		if (i == 10)
5534fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
5535fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
55360434d1b8SBill Paul 	}
55370434d1b8SBill Paul 
553895d67482SBill Paul 	/* Init jumbo RX ring. */
5539f5459d4cSPyun YongHyeon 	if (BGE_IS_JUMBO_CAPABLE(sc) &&
5540fba8b109SMarcel Moolenaar 	    if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
5541fba8b109SMarcel Moolenaar      	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN)) {
55423ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
5543333704a3SPyun YongHyeon 			device_printf(sc->bge_dev,
5544b65256d7SPyun YongHyeon 			    "no memory for jumbo Rx buffers.\n");
55453ee5d7daSPyun YongHyeon 			bge_stop(sc);
55463ee5d7daSPyun YongHyeon 			return;
55473ee5d7daSPyun YongHyeon 		}
55483ee5d7daSPyun YongHyeon 	}
554995d67482SBill Paul 
55503f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
555195d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
555295d67482SBill Paul 
55537e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
55547e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
55557e6e2507SJung-uk Kim 
555695d67482SBill Paul 	/* Init TX ring. */
555795d67482SBill Paul 	bge_init_tx_ring(sc);
555895d67482SBill Paul 
5559f6a65488SPyun YongHyeon 	/* Enable TX MAC state machine lockup fix. */
5560f6a65488SPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_TX_MODE);
5561f6a65488SPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
5562f6a65488SPyun YongHyeon 		mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
55632927f01fSPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
55642927f01fSPyun YongHyeon 	    sc->bge_asicrev == BGE_ASICREV_BCM5762) {
556550515680SPyun YongHyeon 		mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
556650515680SPyun YongHyeon 		mode |= CSR_READ_4(sc, BGE_TX_MODE) &
556750515680SPyun YongHyeon 		    (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
556850515680SPyun YongHyeon 	}
55693f74909aSGleb Smirnoff 	/* Turn on transmitter. */
5570f6a65488SPyun YongHyeon 	CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
5571a6e66cd2SPyun YongHyeon 	DELAY(100);
557295d67482SBill Paul 
55733f74909aSGleb Smirnoff 	/* Turn on receiver. */
5574548c8f1aSPyun YongHyeon 	mode = CSR_READ_4(sc, BGE_RX_MODE);
5575548c8f1aSPyun YongHyeon 	if (BGE_IS_5755_PLUS(sc))
5576548c8f1aSPyun YongHyeon 		mode |= BGE_RXMODE_IPV6_ENABLE;
557769b1f509SPyun YongHyeon 	if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
557869b1f509SPyun YongHyeon 		mode |= BGE_RXMODE_IPV4_FRAG_FIX;
5579548c8f1aSPyun YongHyeon 	CSR_WRITE_4(sc,BGE_RX_MODE, mode | BGE_RXMODE_ENABLE);
5580a6e66cd2SPyun YongHyeon 	DELAY(10);
558195d67482SBill Paul 
5582dedcdf57SPyun YongHyeon 	/*
5583dedcdf57SPyun YongHyeon 	 * Set the number of good frames to receive after RX MBUF
5584dedcdf57SPyun YongHyeon 	 * Low Watermark has been reached. After the RX MAC receives
5585dedcdf57SPyun YongHyeon 	 * this number of frames, it will drop subsequent incoming
5586dedcdf57SPyun YongHyeon 	 * frames until the MBUF High Watermark is reached.
5587dedcdf57SPyun YongHyeon 	 */
55883fc5fbfbSPyun YongHyeon 	if (BGE_IS_57765_PLUS(sc))
5589b4a256acSPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
5590b4a256acSPyun YongHyeon 	else
5591dedcdf57SPyun YongHyeon 		CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
5592dedcdf57SPyun YongHyeon 
55932280c16bSPyun YongHyeon 	/* Clear MAC statistics. */
55942280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
55952280c16bSPyun YongHyeon 		bge_stats_clear_regs(sc);
55962280c16bSPyun YongHyeon 
559795d67482SBill Paul 	/* Tell firmware we're alive. */
559895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
559995d67482SBill Paul 
560075719184SGleb Smirnoff #ifdef DEVICE_POLLING
560175719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
5602fba8b109SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
560375719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
560475719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
560538cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
560675719184SGleb Smirnoff 	} else
560775719184SGleb Smirnoff #endif
560875719184SGleb Smirnoff 
560995d67482SBill Paul 	/* Enable host interrupts. */
561075719184SGleb Smirnoff 	{
561195d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
561295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
561338cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
561475719184SGleb Smirnoff 	}
561595d67482SBill Paul 
5616fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
5617fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
561895d67482SBill Paul 
5619e4146b95SPyun YongHyeon 	bge_ifmedia_upd_locked(ifp);
5620e4146b95SPyun YongHyeon 
56210f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
56220f9bd73bSSam Leffler }
56230f9bd73bSSam Leffler 
56240f9bd73bSSam Leffler static void
56253f74909aSGleb Smirnoff bge_init(void *xsc)
56260f9bd73bSSam Leffler {
56270f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
56280f9bd73bSSam Leffler 
56290f9bd73bSSam Leffler 	BGE_LOCK(sc);
56300f9bd73bSSam Leffler 	bge_init_locked(sc);
56310f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
563295d67482SBill Paul }
563395d67482SBill Paul 
563495d67482SBill Paul /*
563595d67482SBill Paul  * Set media options.
563695d67482SBill Paul  */
563795d67482SBill Paul static int
5638fba8b109SMarcel Moolenaar bge_ifmedia_upd(if_t ifp)
563995d67482SBill Paul {
5640fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
564167d5e043SOleg Bulyzhin 	int res;
564267d5e043SOleg Bulyzhin 
564367d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
564467d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
564567d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
564667d5e043SOleg Bulyzhin 
564767d5e043SOleg Bulyzhin 	return (res);
564867d5e043SOleg Bulyzhin }
564967d5e043SOleg Bulyzhin 
565067d5e043SOleg Bulyzhin static int
5651fba8b109SMarcel Moolenaar bge_ifmedia_upd_locked(if_t ifp)
565267d5e043SOleg Bulyzhin {
5653fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
565495d67482SBill Paul 	struct mii_data *mii;
56554f09c4c7SMarius Strobl 	struct mii_softc *miisc;
565695d67482SBill Paul 	struct ifmedia *ifm;
565795d67482SBill Paul 
565867d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
565967d5e043SOleg Bulyzhin 
566095d67482SBill Paul 	ifm = &sc->bge_ifmedia;
566195d67482SBill Paul 
566295d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
5663652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
566495d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
566595d67482SBill Paul 			return (EINVAL);
566695d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
566795d67482SBill Paul 		case IFM_AUTO:
5668ff50922bSDoug White 			/*
5669ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
5670ff50922bSDoug White 			 * mechanism for programming the autoneg
5671ff50922bSDoug White 			 * advertisement registers in TBI mode.
5672ff50922bSDoug White 			 */
56730f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5674ff50922bSDoug White 				uint32_t sgdig;
56750f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
56760f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
5677ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5678ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5679ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
5680ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
5681ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
5682ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5683ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
5684ff50922bSDoug White 					DELAY(5);
5685ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5686ff50922bSDoug White 				}
56870f89fde2SJung-uk Kim 			}
568895d67482SBill Paul 			break;
568995d67482SBill Paul 		case IFM_1000_SX:
569095d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
569195d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
569295d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
569395d67482SBill Paul 			} else {
569495d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
569595d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
569695d67482SBill Paul 			}
56979b80ffe7SPyun YongHyeon 			DELAY(40);
569895d67482SBill Paul 			break;
569995d67482SBill Paul 		default:
570095d67482SBill Paul 			return (EINVAL);
570195d67482SBill Paul 		}
570295d67482SBill Paul 		return (0);
570395d67482SBill Paul 	}
570495d67482SBill Paul 
57051493e883SOleg Bulyzhin 	sc->bge_link_evt++;
570695d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
57074f09c4c7SMarius Strobl 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
57083fcb7a53SMarius Strobl 		PHY_RESET(miisc);
570995d67482SBill Paul 	mii_mediachg(mii);
571095d67482SBill Paul 
5711902827f6SBjoern A. Zeeb 	/*
5712902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
5713902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
5714902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
5715902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
5716902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
5717902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
5718902827f6SBjoern A. Zeeb 	 * get an RX intr.
5719902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
5720902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
5721902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
5722902827f6SBjoern A. Zeeb 	 */
57234f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
57244f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
5725902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
57264f0794ffSBjoern A. Zeeb 	else
572763ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5728902827f6SBjoern A. Zeeb 
572995d67482SBill Paul 	return (0);
573095d67482SBill Paul }
573195d67482SBill Paul 
573295d67482SBill Paul /*
573395d67482SBill Paul  * Report current media status.
573495d67482SBill Paul  */
573595d67482SBill Paul static void
5736fba8b109SMarcel Moolenaar bge_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
573795d67482SBill Paul {
5738fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
573995d67482SBill Paul 	struct mii_data *mii;
574095d67482SBill Paul 
574167d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
574295d67482SBill Paul 
5743fba8b109SMarcel Moolenaar 	if ((if_getflags(ifp) & IFF_UP) == 0) {
5744b9d2edd7SPyun YongHyeon 		BGE_UNLOCK(sc);
5745b9d2edd7SPyun YongHyeon 		return;
5746b9d2edd7SPyun YongHyeon 	}
5747652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
574895d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
574995d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
575095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
575195d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
575295d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
57534c0da0ffSGleb Smirnoff 		else {
57544c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
575567d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
57564c0da0ffSGleb Smirnoff 			return;
57574c0da0ffSGleb Smirnoff 		}
575895d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
575995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
576095d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
576195d67482SBill Paul 		else
576295d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
576367d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
576495d67482SBill Paul 		return;
576595d67482SBill Paul 	}
576695d67482SBill Paul 
576795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
576895d67482SBill Paul 	mii_pollstat(mii);
576995d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
577095d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
577167d5e043SOleg Bulyzhin 
577267d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
577395d67482SBill Paul }
577495d67482SBill Paul 
577595d67482SBill Paul static int
5776fba8b109SMarcel Moolenaar bge_ioctl(if_t ifp, u_long command, caddr_t data)
577795d67482SBill Paul {
5778fba8b109SMarcel Moolenaar 	struct bge_softc *sc = if_getsoftc(ifp);
577995d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
578095d67482SBill Paul 	struct mii_data *mii;
5781f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
578295d67482SBill Paul 
578395d67482SBill Paul 	switch (command) {
578495d67482SBill Paul 	case SIOCSIFMTU:
5785f5459d4cSPyun YongHyeon 		if (BGE_IS_JUMBO_CAPABLE(sc) ||
5786f5459d4cSPyun YongHyeon 		    (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
57874c0da0ffSGleb Smirnoff 			if (ifr->ifr_mtu < ETHERMIN ||
5788f5459d4cSPyun YongHyeon 			    ifr->ifr_mtu > BGE_JUMBO_MTU) {
578995d67482SBill Paul 				error = EINVAL;
5790f5459d4cSPyun YongHyeon 				break;
5791f5459d4cSPyun YongHyeon 			}
5792f5459d4cSPyun YongHyeon 		} else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5793f5459d4cSPyun YongHyeon 			error = EINVAL;
5794f5459d4cSPyun YongHyeon 			break;
5795f5459d4cSPyun YongHyeon 		}
5796f5459d4cSPyun YongHyeon 		BGE_LOCK(sc);
5797fba8b109SMarcel Moolenaar 		if (if_getmtu(ifp) != ifr->ifr_mtu) {
5798fba8b109SMarcel Moolenaar 			if_setmtu(ifp, ifr->ifr_mtu);
5799fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
5800fba8b109SMarcel Moolenaar 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
58013a429c8fSPyun YongHyeon 				bge_init_locked(sc);
580295d67482SBill Paul 			}
58033a429c8fSPyun YongHyeon 		}
58043a429c8fSPyun YongHyeon 		BGE_UNLOCK(sc);
580595d67482SBill Paul 		break;
580695d67482SBill Paul 	case SIOCSIFFLAGS:
58070f9bd73bSSam Leffler 		BGE_LOCK(sc);
5808fba8b109SMarcel Moolenaar 		if (if_getflags(ifp) & IFF_UP) {
580995d67482SBill Paul 			/*
581095d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
581195d67482SBill Paul 			 * then just use the 'set promisc mode' command
581295d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
581395d67482SBill Paul 			 * a full re-init means reloading the firmware and
581495d67482SBill Paul 			 * waiting for it to start up, which may take a
5815d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
581695d67482SBill Paul 			 */
5817fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
5818fba8b109SMarcel Moolenaar 				flags = if_getflags(ifp) ^ sc->bge_if_flags;
58193e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
58203e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
5821f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
5822d183af7fSRuslan Ermilov 					bge_setmulti(sc);
582395d67482SBill Paul 			} else
58240f9bd73bSSam Leffler 				bge_init_locked(sc);
582595d67482SBill Paul 		} else {
5826fba8b109SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
582795d67482SBill Paul 				bge_stop(sc);
582895d67482SBill Paul 			}
582995d67482SBill Paul 		}
5830fba8b109SMarcel Moolenaar 		sc->bge_if_flags = if_getflags(ifp);
58310f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
583295d67482SBill Paul 		error = 0;
583395d67482SBill Paul 		break;
583495d67482SBill Paul 	case SIOCADDMULTI:
583595d67482SBill Paul 	case SIOCDELMULTI:
5836fba8b109SMarcel Moolenaar 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
58370f9bd73bSSam Leffler 			BGE_LOCK(sc);
583895d67482SBill Paul 			bge_setmulti(sc);
58390f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
584095d67482SBill Paul 			error = 0;
584195d67482SBill Paul 		}
584295d67482SBill Paul 		break;
584395d67482SBill Paul 	case SIOCSIFMEDIA:
584495d67482SBill Paul 	case SIOCGIFMEDIA:
5845652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
584695d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
584795d67482SBill Paul 			    &sc->bge_ifmedia, command);
584895d67482SBill Paul 		} else {
584995d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
585095d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
585195d67482SBill Paul 			    &mii->mii_media, command);
585295d67482SBill Paul 		}
585395d67482SBill Paul 		break;
585495d67482SBill Paul 	case SIOCSIFCAP:
5855fba8b109SMarcel Moolenaar 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
585675719184SGleb Smirnoff #ifdef DEVICE_POLLING
585775719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
585875719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
5859bd071d4dSGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
586075719184SGleb Smirnoff 				if (error)
586175719184SGleb Smirnoff 					return (error);
586275719184SGleb Smirnoff 				BGE_LOCK(sc);
586375719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
586475719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
586538cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
5866fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, IFCAP_POLLING, 0);
586775719184SGleb Smirnoff 				BGE_UNLOCK(sc);
586875719184SGleb Smirnoff 			} else {
586975719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
587075719184SGleb Smirnoff 				/* Enable interrupt even in error case */
587175719184SGleb Smirnoff 				BGE_LOCK(sc);
587275719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
587375719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
587438cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
5875fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_POLLING);
587675719184SGleb Smirnoff 				BGE_UNLOCK(sc);
587775719184SGleb Smirnoff 			}
587875719184SGleb Smirnoff 		}
587975719184SGleb Smirnoff #endif
5880d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
5881fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TXCSUM) != 0) {
5882fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TXCSUM);
5883fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
5884fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp,
5885fba8b109SMarcel Moolenaar 				    sc->bge_csum_features, 0);
588695d67482SBill Paul 			else
5887fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, 0,
5888fba8b109SMarcel Moolenaar 				    sc->bge_csum_features);
588995d67482SBill Paul 		}
5890cb2eacc7SYaroslav Tykhiy 
5891d8b57f98SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
5892fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_RXCSUM) != 0)
5893fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_RXCSUM);
5894d8b57f98SPyun YongHyeon 
5895ca3f1187SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
5896fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TSO4) != 0) {
5897fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TSO4);
5898fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TSO4) != 0)
5899fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, CSUM_TSO, 0);
5900ca3f1187SPyun YongHyeon 			else
5901fba8b109SMarcel Moolenaar 				if_sethwassistbits(ifp, 0, CSUM_TSO);
5902ca3f1187SPyun YongHyeon 		}
5903ca3f1187SPyun YongHyeon 
5904cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
5905fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_MTU);
5906fba8b109SMarcel Moolenaar 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
5907cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
5908cb2eacc7SYaroslav Tykhiy 		}
5909cb2eacc7SYaroslav Tykhiy 
591004bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
5911fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTSO) != 0)
5912fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
591304bde852SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
5914fba8b109SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
5915fba8b109SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
5916fba8b109SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0)
5917fba8b109SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_VLAN_HWTSO);
5918cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
5919cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
5920cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
592104bde852SPyun YongHyeon 		}
5922cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
5923fba8b109SMarcel Moolenaar 		if_vlancap(ifp);
5924cb2eacc7SYaroslav Tykhiy #endif
592595d67482SBill Paul 		break;
592695d67482SBill Paul 	default:
5927673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
592895d67482SBill Paul 		break;
592995d67482SBill Paul 	}
593095d67482SBill Paul 
593195d67482SBill Paul 	return (error);
593295d67482SBill Paul }
593395d67482SBill Paul 
593495d67482SBill Paul static void
5935b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
593695d67482SBill Paul {
5937fba8b109SMarcel Moolenaar 	if_t ifp;
5938b584d2b3SPyun YongHyeon 	uint32_t status;
593995d67482SBill Paul 
5940b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
5941b74e67fbSGleb Smirnoff 
5942b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
5943b74e67fbSGleb Smirnoff 		return;
5944b74e67fbSGleb Smirnoff 
5945b584d2b3SPyun YongHyeon 	/* If pause frames are active then don't reset the hardware. */
5946b584d2b3SPyun YongHyeon 	if ((CSR_READ_4(sc, BGE_RX_MODE) & BGE_RXMODE_FLOWCTL_ENABLE) != 0) {
5947b584d2b3SPyun YongHyeon 		status = CSR_READ_4(sc, BGE_RX_STS);
5948b584d2b3SPyun YongHyeon 		if ((status & BGE_RXSTAT_REMOTE_XOFFED) != 0) {
5949b584d2b3SPyun YongHyeon 			/*
5950b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5951b584d2b3SPyun YongHyeon 			 * the condition to clear.
5952b584d2b3SPyun YongHyeon 			 */
5953b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5954b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5955b584d2b3SPyun YongHyeon 			return;
5956b584d2b3SPyun YongHyeon 		} else if ((status & BGE_RXSTAT_RCVD_XOFF) != 0 &&
5957b584d2b3SPyun YongHyeon 		    (status & BGE_RXSTAT_RCVD_XON) != 0) {
5958b584d2b3SPyun YongHyeon 			/*
5959b584d2b3SPyun YongHyeon 			 * If link partner has us in XOFF state then wait for
5960b584d2b3SPyun YongHyeon 			 * the condition to clear.
5961b584d2b3SPyun YongHyeon 			 */
5962b584d2b3SPyun YongHyeon 			CSR_WRITE_4(sc, BGE_RX_STS, status);
5963b584d2b3SPyun YongHyeon 			sc->bge_timer = BGE_TX_TIMEOUT;
5964b584d2b3SPyun YongHyeon 			return;
5965b584d2b3SPyun YongHyeon 		}
5966b584d2b3SPyun YongHyeon 		/*
5967b584d2b3SPyun YongHyeon 		 * Any other condition is unexpected and the controller
5968b584d2b3SPyun YongHyeon 		 * should be reset.
5969b584d2b3SPyun YongHyeon 		 */
5970b584d2b3SPyun YongHyeon 	}
5971b584d2b3SPyun YongHyeon 
5972b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
597395d67482SBill Paul 
5974fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
597595d67482SBill Paul 
5976fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
5977426742bfSGleb Smirnoff 	bge_init_locked(sc);
597895d67482SBill Paul 
5979df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
598095d67482SBill Paul }
598195d67482SBill Paul 
59825a147ba6SPyun YongHyeon static void
59835a147ba6SPyun YongHyeon bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
59845a147ba6SPyun YongHyeon {
59855a147ba6SPyun YongHyeon 	int i;
59865a147ba6SPyun YongHyeon 
59875a147ba6SPyun YongHyeon 	BGE_CLRBIT(sc, reg, bit);
59885a147ba6SPyun YongHyeon 
59895a147ba6SPyun YongHyeon 	for (i = 0; i < BGE_TIMEOUT; i++) {
59905a147ba6SPyun YongHyeon 		if ((CSR_READ_4(sc, reg) & bit) == 0)
59915a147ba6SPyun YongHyeon 			return;
59925a147ba6SPyun YongHyeon 		DELAY(100);
59935a147ba6SPyun YongHyeon         }
59945a147ba6SPyun YongHyeon }
59955a147ba6SPyun YongHyeon 
599695d67482SBill Paul /*
599795d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
599895d67482SBill Paul  * RX and TX lists.
599995d67482SBill Paul  */
600095d67482SBill Paul static void
60013f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
600295d67482SBill Paul {
6003fba8b109SMarcel Moolenaar 	if_t ifp;
600495d67482SBill Paul 
60050f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
60060f9bd73bSSam Leffler 
6007fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
600895d67482SBill Paul 
60090f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
601095d67482SBill Paul 
601144b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
601244b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
601344b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
601444b63691SBjoern A. Zeeb 
601544b63691SBjoern A. Zeeb 	/*
601644b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
601744b63691SBjoern A. Zeeb 	 */
601844b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
6019548c8f1aSPyun YongHyeon 	bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
602044b63691SBjoern A. Zeeb 
602195d67482SBill Paul 	/*
60223f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
602395d67482SBill Paul 	 */
60245a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
60255a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
60265a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
60275a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60285a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
60295a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
60305a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
60315a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
603295d67482SBill Paul 
603395d67482SBill Paul 	/*
60343f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
603595d67482SBill Paul 	 */
60365a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
60375a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
60385a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
60395a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
60405a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
60415a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60425a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
60435a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
604495d67482SBill Paul 
604595d67482SBill Paul 	/*
604695d67482SBill Paul 	 * Shut down all of the memory managers and related
604795d67482SBill Paul 	 * state machines.
604895d67482SBill Paul 	 */
60495a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
60505a147ba6SPyun YongHyeon 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
60515a147ba6SPyun YongHyeon 	if (BGE_IS_5700_FAMILY(sc))
60525a147ba6SPyun YongHyeon 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
60535a147ba6SPyun YongHyeon 
60540c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
605595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
60567ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
605795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
605895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
60590434d1b8SBill Paul 	}
60602280c16bSPyun YongHyeon 	/* Update MAC statistics. */
60612280c16bSPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
60622280c16bSPyun YongHyeon 		bge_stats_update_regs(sc);
606395d67482SBill Paul 
60648cb1383cSDoug Ambrisko 	bge_reset(sc);
6065548c8f1aSPyun YongHyeon 	bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
6066548c8f1aSPyun YongHyeon 	bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
60678cb1383cSDoug Ambrisko 
60688cb1383cSDoug Ambrisko 	/*
60698cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
60708cb1383cSDoug Ambrisko 	 */
60718cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
60728cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
60738cb1383cSDoug Ambrisko 	else
607495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
607595d67482SBill Paul 
607695d67482SBill Paul 	/* Free the RX lists. */
607795d67482SBill Paul 	bge_free_rx_ring_std(sc);
607895d67482SBill Paul 
607995d67482SBill Paul 	/* Free jumbo RX list. */
60804c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
608195d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
608295d67482SBill Paul 
608395d67482SBill Paul 	/* Free TX buffers. */
608495d67482SBill Paul 	bge_free_tx_ring(sc);
608595d67482SBill Paul 
608695d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
608795d67482SBill Paul 
60885dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
60891493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
60901493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
60911493e883SOleg Bulyzhin 	sc->bge_link = 0;
609295d67482SBill Paul 
6093fba8b109SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
609495d67482SBill Paul }
609595d67482SBill Paul 
609695d67482SBill Paul /*
609795d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
609895d67482SBill Paul  * get confused by errant DMAs when rebooting.
609995d67482SBill Paul  */
6100b6c974e8SWarner Losh static int
61013f74909aSGleb Smirnoff bge_shutdown(device_t dev)
610295d67482SBill Paul {
610395d67482SBill Paul 	struct bge_softc *sc;
610495d67482SBill Paul 
610595d67482SBill Paul 	sc = device_get_softc(dev);
61060f9bd73bSSam Leffler 	BGE_LOCK(sc);
610795d67482SBill Paul 	bge_stop(sc);
61080f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
6109b6c974e8SWarner Losh 
6110b6c974e8SWarner Losh 	return (0);
611195d67482SBill Paul }
611214afefa3SPawel Jakub Dawidek 
611314afefa3SPawel Jakub Dawidek static int
611414afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
611514afefa3SPawel Jakub Dawidek {
611614afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
611714afefa3SPawel Jakub Dawidek 
611814afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
611914afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
612014afefa3SPawel Jakub Dawidek 	bge_stop(sc);
612114afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
612214afefa3SPawel Jakub Dawidek 
612314afefa3SPawel Jakub Dawidek 	return (0);
612414afefa3SPawel Jakub Dawidek }
612514afefa3SPawel Jakub Dawidek 
612614afefa3SPawel Jakub Dawidek static int
612714afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
612814afefa3SPawel Jakub Dawidek {
612914afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
6130fba8b109SMarcel Moolenaar 	if_t ifp;
613114afefa3SPawel Jakub Dawidek 
613214afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
613314afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
613414afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
6135fba8b109SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_UP) {
613614afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
6137fba8b109SMarcel Moolenaar 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
613814afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
613914afefa3SPawel Jakub Dawidek 	}
614014afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
614114afefa3SPawel Jakub Dawidek 
614214afefa3SPawel Jakub Dawidek 	return (0);
614314afefa3SPawel Jakub Dawidek }
6144dab5cd05SOleg Bulyzhin 
6145dab5cd05SOleg Bulyzhin static void
61463f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
6147dab5cd05SOleg Bulyzhin {
61481f313773SOleg Bulyzhin 	struct mii_data *mii;
61491f313773SOleg Bulyzhin 	uint32_t link, status;
6150dab5cd05SOleg Bulyzhin 
6151dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
61521f313773SOleg Bulyzhin 
61533f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
61547b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
61557b97099dSOleg Bulyzhin 
6156dab5cd05SOleg Bulyzhin 	/*
6157dab5cd05SOleg Bulyzhin 	 * Process link state changes.
6158dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
6159dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
6160dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
6161dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
6162dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
6163dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
6164dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
6165dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
61661f313773SOleg Bulyzhin 	 *
61671f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
61684c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
6169dab5cd05SOleg Bulyzhin 	 */
6170dab5cd05SOleg Bulyzhin 
61711f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
61724c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
6173dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
6174dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
61751f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
61765dda8085SOleg Bulyzhin 			mii_pollstat(mii);
61771f313773SOleg Bulyzhin 			if (!sc->bge_link &&
61781f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
61791f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
61801f313773SOleg Bulyzhin 				sc->bge_link++;
61811f313773SOleg Bulyzhin 				if (bootverbose)
61821f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
61831f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
61841f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
61851f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
61861f313773SOleg Bulyzhin 				sc->bge_link = 0;
61871f313773SOleg Bulyzhin 				if (bootverbose)
61881f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
61891f313773SOleg Bulyzhin 			}
61901f313773SOleg Bulyzhin 
61913f74909aSGleb Smirnoff 			/* Clear the interrupt. */
6192dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
6193dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
6194daeeb75cSPyun YongHyeon 			bge_miibus_readreg(sc->bge_dev, sc->bge_phy_addr,
6195daeeb75cSPyun YongHyeon 			    BRGPHY_MII_ISR);
6196daeeb75cSPyun YongHyeon 			bge_miibus_writereg(sc->bge_dev, sc->bge_phy_addr,
6197daeeb75cSPyun YongHyeon 			    BRGPHY_MII_IMR, BRGPHY_INTRS);
6198dab5cd05SOleg Bulyzhin 		}
6199dab5cd05SOleg Bulyzhin 		return;
6200dab5cd05SOleg Bulyzhin 	}
6201dab5cd05SOleg Bulyzhin 
6202652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
62031f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
62047b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
62057b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
62061f313773SOleg Bulyzhin 				sc->bge_link++;
62079b80ffe7SPyun YongHyeon 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
62081f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
62091f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
62109b80ffe7SPyun YongHyeon 					DELAY(40);
62119b80ffe7SPyun YongHyeon 				}
62120c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
62131f313773SOleg Bulyzhin 				if (bootverbose)
62141f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
62153f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
62163f74909aSGleb Smirnoff 				    LINK_STATE_UP);
62177b97099dSOleg Bulyzhin 			}
62181f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
6219dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
62201f313773SOleg Bulyzhin 			if (bootverbose)
62211f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
62227b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
62231f313773SOleg Bulyzhin 		}
62246ede2cfaSPyun YongHyeon 	} else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
62251f313773SOleg Bulyzhin 		/*
62260c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
62270c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
62280c8aa4eaSJung-uk Kim 		 * PHY link status directly.
62291f313773SOleg Bulyzhin 		 */
62301f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
62311f313773SOleg Bulyzhin 
62321f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
62331f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
62341f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
62355dda8085SOleg Bulyzhin 			mii_pollstat(mii);
62361f313773SOleg Bulyzhin 			if (!sc->bge_link &&
62371f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
62381f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
62391f313773SOleg Bulyzhin 				sc->bge_link++;
62401f313773SOleg Bulyzhin 				if (bootverbose)
62411f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
62421f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
62431f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
62441f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
62451f313773SOleg Bulyzhin 				sc->bge_link = 0;
62461f313773SOleg Bulyzhin 				if (bootverbose)
62471f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
62481f313773SOleg Bulyzhin 			}
62491f313773SOleg Bulyzhin 		}
62500c8aa4eaSJung-uk Kim 	} else {
62510c8aa4eaSJung-uk Kim 		/*
62526ede2cfaSPyun YongHyeon 		 * For controllers that call mii_tick, we have to poll
62536ede2cfaSPyun YongHyeon 		 * link status.
62540c8aa4eaSJung-uk Kim 		 */
62556ede2cfaSPyun YongHyeon 		mii = device_get_softc(sc->bge_miibus);
62566ede2cfaSPyun YongHyeon 		mii_pollstat(mii);
62576ede2cfaSPyun YongHyeon 		bge_miibus_statchg(sc->bge_dev);
6258dab5cd05SOleg Bulyzhin 	}
6259dab5cd05SOleg Bulyzhin 
62602246e8c6SPyun YongHyeon 	/* Disable MAC attention when link is up. */
6261dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
6262dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
6263dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
6264dab5cd05SOleg Bulyzhin }
62656f8718a3SScott Long 
62666f8718a3SScott Long static void
62676f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
62686f8718a3SScott Long {
62696f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
62702280c16bSPyun YongHyeon 	struct sysctl_oid_list *children;
62717e32f79aSPyun YongHyeon 	int unit;
62726f8718a3SScott Long 
62736f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
62746f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
62756f8718a3SScott Long 
62766f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
62776f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
62786f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
62796f8718a3SScott Long 	    "Debug Information");
62806f8718a3SScott Long 
62816f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
62826f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
6283548c8f1aSPyun YongHyeon 	    "MAC Register Read");
6284548c8f1aSPyun YongHyeon 
6285548c8f1aSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ape_read",
6286548c8f1aSPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_ape_read, "I",
6287548c8f1aSPyun YongHyeon 	    "APE Register Read");
62886f8718a3SScott Long 
62896f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
62906f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
62916f8718a3SScott Long 	    "Memory Read");
62926f8718a3SScott Long 
62936f8718a3SScott Long #endif
6294763757b2SScott Long 
62957e32f79aSPyun YongHyeon 	unit = device_get_unit(sc->bge_dev);
6296beaa2ae1SPyun YongHyeon 	/*
6297beaa2ae1SPyun YongHyeon 	 * A common design characteristic for many Broadcom client controllers
6298beaa2ae1SPyun YongHyeon 	 * is that they only support a single outstanding DMA read operation
6299beaa2ae1SPyun YongHyeon 	 * on the PCIe bus. This means that it will take twice as long to fetch
6300beaa2ae1SPyun YongHyeon 	 * a TX frame that is split into header and payload buffers as it does
6301beaa2ae1SPyun YongHyeon 	 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
6302beaa2ae1SPyun YongHyeon 	 * these controllers, coalescing buffers to reduce the number of memory
6303beaa2ae1SPyun YongHyeon 	 * reads is effective way to get maximum performance(about 940Mbps).
6304beaa2ae1SPyun YongHyeon 	 * Without collapsing TX buffers the maximum TCP bulk transfer
6305beaa2ae1SPyun YongHyeon 	 * performance is about 850Mbps. However forcing coalescing mbufs
6306beaa2ae1SPyun YongHyeon 	 * consumes a lot of CPU cycles, so leave it off by default.
6307beaa2ae1SPyun YongHyeon 	 */
63087e32f79aSPyun YongHyeon 	sc->bge_forced_collapse = 0;
6309beaa2ae1SPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
6310af3b2549SHans Petter Selasky 	    CTLFLAG_RWTUN, &sc->bge_forced_collapse, 0,
6311beaa2ae1SPyun YongHyeon 	    "Number of fragmented TX buffers of a frame allowed before "
6312beaa2ae1SPyun YongHyeon 	    "forced collapsing");
6313beaa2ae1SPyun YongHyeon 
63142ae7f64bSPyun YongHyeon 	sc->bge_msi = 1;
63152ae7f64bSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
6316af3b2549SHans Petter Selasky 	    CTLFLAG_RDTUN, &sc->bge_msi, 0, "Enable MSI");
63175c952e8dSPyun YongHyeon 
631835f945cdSPyun YongHyeon 	/*
631935f945cdSPyun YongHyeon 	 * It seems all Broadcom controllers have a bug that can generate UDP
632035f945cdSPyun YongHyeon 	 * datagrams with checksum value 0 when TX UDP checksum offloading is
632135f945cdSPyun YongHyeon 	 * enabled.  Generating UDP checksum value 0 is RFC 768 violation.
632235f945cdSPyun YongHyeon 	 * Even though the probability of generating such UDP datagrams is
632335f945cdSPyun YongHyeon 	 * low, I don't want to see FreeBSD boxes to inject such datagrams
632435f945cdSPyun YongHyeon 	 * into network so disable UDP checksum offloading by default.  Users
632535f945cdSPyun YongHyeon 	 * still override this behavior by setting a sysctl variable,
632635f945cdSPyun YongHyeon 	 * dev.bge.0.forced_udpcsum.
632735f945cdSPyun YongHyeon 	 */
632835f945cdSPyun YongHyeon 	sc->bge_forced_udpcsum = 0;
632935f945cdSPyun YongHyeon 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
6330af3b2549SHans Petter Selasky 	    CTLFLAG_RWTUN, &sc->bge_forced_udpcsum, 0,
633135f945cdSPyun YongHyeon 	    "Enable UDP checksum offloading even if controller can "
633235f945cdSPyun YongHyeon 	    "generate UDP checksum value 0");
633335f945cdSPyun YongHyeon 
6334d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
63352280c16bSPyun YongHyeon 		bge_add_sysctl_stats_regs(sc, ctx, children);
63362280c16bSPyun YongHyeon 	else
63372280c16bSPyun YongHyeon 		bge_add_sysctl_stats(sc, ctx, children);
63382280c16bSPyun YongHyeon }
6339d949071dSJung-uk Kim 
63402280c16bSPyun YongHyeon #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
63412280c16bSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
63422280c16bSPyun YongHyeon 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
63432280c16bSPyun YongHyeon 	    desc)
63442280c16bSPyun YongHyeon 
63452280c16bSPyun YongHyeon static void
63462280c16bSPyun YongHyeon bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
63472280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
63482280c16bSPyun YongHyeon {
63492280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
63502280c16bSPyun YongHyeon 	struct sysctl_oid_list *children, *schildren;
63512280c16bSPyun YongHyeon 
63522280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
6353763757b2SScott Long 	    NULL, "BGE Statistics");
6354763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
6355763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
6356763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
6357763757b2SScott Long 	    "FramesDroppedDueToFilters");
6358763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
6359763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
6360763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
6361763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
6362763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
6363763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
636406e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
636506e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
636606e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
636706e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
6368763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
6369763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
6370763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
6371763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
6372763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
6373763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
6374763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
6375763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
6376763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
6377763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
6378763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
6379763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
6380763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
6381763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
6382763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
6383763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
6384763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
6385763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
6386763757b2SScott Long 
6387763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
6388763757b2SScott Long 	    NULL, "BGE RX Statistics");
6389763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6390763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
63911cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInOctets, "ifHCInOctets");
6392763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
6393763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
6394763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
63951cd4773bSPyun YongHyeon 	    children, rxstats.ifHCInUcastPkts, "UnicastPkts");
6396763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
6397763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
6398763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
6399763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
6400763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
6401763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
6402763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
6403763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
6404763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
6405763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
6406763757b2SScott Long 	    "xoffPauseFramesReceived");
6407763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
6408763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
6409763757b2SScott Long 	    "ControlFramesReceived");
6410763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
6411763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
6412763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
6413763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
6414763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
6415763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
6416763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
6417763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
6418763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
641906e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
6420763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
642106e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
6422763757b2SScott Long 
6423763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
6424763757b2SScott Long 	    NULL, "BGE TX Statistics");
6425763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
6426763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
64271cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutOctets, "ifHCOutOctets");
6428763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
6429763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
6430763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
6431763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
6432763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
6433763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
6434763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
6435763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
6436763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
6437763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
6438763757b2SScott Long 	    "InternalMacTransmitErrors");
6439763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
6440763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
6441763757b2SScott Long 	    "SingleCollisionFrames");
6442763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
6443763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
6444763757b2SScott Long 	    "MultipleCollisionFrames");
6445763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
6446763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
6447763757b2SScott Long 	    "DeferredTransmissions");
6448763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
6449763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
6450763757b2SScott Long 	    "ExcessiveCollisions");
6451763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
645206e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
645306e83c7eSScott Long 	    "LateCollisions");
6454763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
64551cd4773bSPyun YongHyeon 	    children, txstats.ifHCOutUcastPkts, "UnicastPkts");
6456763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
6457763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
6458763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
6459763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
6460763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
6461763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
6462763757b2SScott Long 	    "CarrierSenseErrors");
6463763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
6464763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
6465763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
6466763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
6467763757b2SScott Long }
6468763757b2SScott Long 
64692280c16bSPyun YongHyeon #undef BGE_SYSCTL_STAT
64702280c16bSPyun YongHyeon 
64712280c16bSPyun YongHyeon #define	BGE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
64726dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
64732280c16bSPyun YongHyeon 
64742280c16bSPyun YongHyeon static void
64752280c16bSPyun YongHyeon bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
64762280c16bSPyun YongHyeon     struct sysctl_oid_list *parent)
64772280c16bSPyun YongHyeon {
64782280c16bSPyun YongHyeon 	struct sysctl_oid *tree;
64792280c16bSPyun YongHyeon 	struct sysctl_oid_list *child, *schild;
64802280c16bSPyun YongHyeon 	struct bge_mac_stats *stats;
64812280c16bSPyun YongHyeon 
64822280c16bSPyun YongHyeon 	stats = &sc->bge_mac_stats;
64832280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
64842280c16bSPyun YongHyeon 	    NULL, "BGE Statistics");
64852280c16bSPyun YongHyeon 	schild = child = SYSCTL_CHILDREN(tree);
64862280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
64872280c16bSPyun YongHyeon 	    &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
64882280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
64892280c16bSPyun YongHyeon 	    &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
64902280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
64912280c16bSPyun YongHyeon 	    &stats->DmaWriteHighPriQueueFull,
64922280c16bSPyun YongHyeon 	    "NIC DMA Write High Priority Queue Full");
64932280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
64942280c16bSPyun YongHyeon 	    &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
64952280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
64962280c16bSPyun YongHyeon 	    &stats->InputDiscards, "Discarded Input Frames");
64972280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
64982280c16bSPyun YongHyeon 	    &stats->InputErrors, "Input Errors");
64992280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
65002280c16bSPyun YongHyeon 	    &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
65012280c16bSPyun YongHyeon 
65022280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
65032280c16bSPyun YongHyeon 	    NULL, "BGE RX Statistics");
65042280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
65052280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
65062280c16bSPyun YongHyeon 	    &stats->ifHCInOctets, "Inbound Octets");
65072280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
65082280c16bSPyun YongHyeon 	    &stats->etherStatsFragments, "Fragments");
65091cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
65102280c16bSPyun YongHyeon 	    &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
65112280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
65122280c16bSPyun YongHyeon 	    &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
65132280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
65142280c16bSPyun YongHyeon 	    &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
65152280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
65162280c16bSPyun YongHyeon 	    &stats->dot3StatsFCSErrors, "FCS Errors");
65172280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
65182280c16bSPyun YongHyeon 	    &stats->dot3StatsAlignmentErrors, "Alignment Errors");
65192280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
65202280c16bSPyun YongHyeon 	    &stats->xonPauseFramesReceived, "XON Pause Frames Received");
65212280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
65222280c16bSPyun YongHyeon 	    &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
65232280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
65242280c16bSPyun YongHyeon 	    &stats->macControlFramesReceived, "MAC Control Frames Received");
65252280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
65262280c16bSPyun YongHyeon 	    &stats->xoffStateEntered, "XOFF State Entered");
65272280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
65282280c16bSPyun YongHyeon 	    &stats->dot3StatsFramesTooLong, "Frames Too Long");
65292280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
65302280c16bSPyun YongHyeon 	    &stats->etherStatsJabbers, "Jabbers");
65312280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
65322280c16bSPyun YongHyeon 	    &stats->etherStatsUndersizePkts, "Undersized Packets");
65332280c16bSPyun YongHyeon 
65342280c16bSPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
65352280c16bSPyun YongHyeon 	    NULL, "BGE TX Statistics");
65362280c16bSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
65371cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
65382280c16bSPyun YongHyeon 	    &stats->ifHCOutOctets, "Outbound Octets");
65392280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
65402280c16bSPyun YongHyeon 	    &stats->etherStatsCollisions, "TX Collisions");
65412280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
65422280c16bSPyun YongHyeon 	    &stats->outXonSent, "XON Sent");
65432280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
65442280c16bSPyun YongHyeon 	    &stats->outXoffSent, "XOFF Sent");
65452280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
65462280c16bSPyun YongHyeon 	    &stats->dot3StatsInternalMacTransmitErrors,
65472280c16bSPyun YongHyeon 	    "Internal MAC TX Errors");
65482280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
65492280c16bSPyun YongHyeon 	    &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
65502280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
65512280c16bSPyun YongHyeon 	    &stats->dot3StatsMultipleCollisionFrames,
65522280c16bSPyun YongHyeon 	    "Multiple Collision Frames");
65532280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
65542280c16bSPyun YongHyeon 	    &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
65552280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
65562280c16bSPyun YongHyeon 	    &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
65572280c16bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
65582280c16bSPyun YongHyeon 	    &stats->dot3StatsLateCollisions, "Late Collisions");
65591cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
65602280c16bSPyun YongHyeon 	    &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
65611cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
65622280c16bSPyun YongHyeon 	    &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
65631cd4773bSPyun YongHyeon 	BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
65642280c16bSPyun YongHyeon 	    &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
65652280c16bSPyun YongHyeon }
65662280c16bSPyun YongHyeon 
65672280c16bSPyun YongHyeon #undef	BGE_SYSCTL_STAT_ADD64
65682280c16bSPyun YongHyeon 
6569763757b2SScott Long static int
6570763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
6571763757b2SScott Long {
6572763757b2SScott Long 	struct bge_softc *sc;
657306e83c7eSScott Long 	uint32_t result;
6574d949071dSJung-uk Kim 	int offset;
6575763757b2SScott Long 
6576763757b2SScott Long 	sc = (struct bge_softc *)arg1;
6577763757b2SScott Long 	offset = arg2;
6578d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
6579d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
6580041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
65816f8718a3SScott Long }
65826f8718a3SScott Long 
65836f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
65846f8718a3SScott Long static int
65856f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
65866f8718a3SScott Long {
65876f8718a3SScott Long 	struct bge_softc *sc;
65886f8718a3SScott Long 	uint16_t *sbdata;
658928276ad6SPyun YongHyeon 	int error, result, sbsz;
65906f8718a3SScott Long 	int i, j;
65916f8718a3SScott Long 
65926f8718a3SScott Long 	result = -1;
65936f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
65946f8718a3SScott Long 	if (error || (req->newptr == NULL))
65956f8718a3SScott Long 		return (error);
65966f8718a3SScott Long 
65976f8718a3SScott Long 	if (result == 1) {
65986f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
65996f8718a3SScott Long 
660028276ad6SPyun YongHyeon 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
660128276ad6SPyun YongHyeon 		    sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
660228276ad6SPyun YongHyeon 			sbsz = BGE_STATUS_BLK_SZ;
660328276ad6SPyun YongHyeon 		else
660428276ad6SPyun YongHyeon 			sbsz = 32;
66056f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
66066f8718a3SScott Long 		printf("Status Block:\n");
660728276ad6SPyun YongHyeon 		BGE_LOCK(sc);
660828276ad6SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
660928276ad6SPyun YongHyeon 		    sc->bge_cdata.bge_status_map,
661028276ad6SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
661128276ad6SPyun YongHyeon 		for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
66126f8718a3SScott Long 			printf("%06x:", i);
661328276ad6SPyun YongHyeon 			for (j = 0; j < 8; j++)
661428276ad6SPyun YongHyeon 				printf(" %04x", sbdata[i++]);
66156f8718a3SScott Long 			printf("\n");
66166f8718a3SScott Long 		}
66176f8718a3SScott Long 
66186f8718a3SScott Long 		printf("Registers:\n");
66190c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
66206f8718a3SScott Long 			printf("%06x:", i);
66216f8718a3SScott Long 			for (j = 0; j < 8; j++) {
66226f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
66236f8718a3SScott Long 				i += 4;
66246f8718a3SScott Long 			}
66256f8718a3SScott Long 			printf("\n");
66266f8718a3SScott Long 		}
662728276ad6SPyun YongHyeon 		BGE_UNLOCK(sc);
66286f8718a3SScott Long 
66296f8718a3SScott Long 		printf("Hardware Flags:\n");
663028276ad6SPyun YongHyeon 		if (BGE_IS_5717_PLUS(sc))
663128276ad6SPyun YongHyeon 			printf(" - 5717 Plus\n");
6632a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
6633a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
66345345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
66356f8718a3SScott Long 			printf(" - 575X Plus\n");
66365345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
66376f8718a3SScott Long 			printf(" - 5705 Plus\n");
66385345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
66395345bad0SScott Long 			printf(" - 5714 Family\n");
66405345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
66415345bad0SScott Long 			printf(" - 5700 Family\n");
66426f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
66436f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
66446f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
66456f8718a3SScott Long 			printf(" - PCI-X Bus\n");
66466f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
66476f8718a3SScott Long 			printf(" - PCI Express Bus\n");
66487d3d9608SPyun YongHyeon 		if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
66496f8718a3SScott Long 			printf(" - No 3 LEDs\n");
66506f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
66516f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
66526f8718a3SScott Long 	}
66536f8718a3SScott Long 
66546f8718a3SScott Long 	return (error);
66556f8718a3SScott Long }
66566f8718a3SScott Long 
66576f8718a3SScott Long static int
66586f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
66596f8718a3SScott Long {
66606f8718a3SScott Long 	struct bge_softc *sc;
66616f8718a3SScott Long 	int error;
66626f8718a3SScott Long 	uint16_t result;
66636f8718a3SScott Long 	uint32_t val;
66646f8718a3SScott Long 
66656f8718a3SScott Long 	result = -1;
66666f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
66676f8718a3SScott Long 	if (error || (req->newptr == NULL))
66686f8718a3SScott Long 		return (error);
66696f8718a3SScott Long 
66706f8718a3SScott Long 	if (result < 0x8000) {
66716f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
66726f8718a3SScott Long 		val = CSR_READ_4(sc, result);
66736f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
66746f8718a3SScott Long 	}
66756f8718a3SScott Long 
66766f8718a3SScott Long 	return (error);
66776f8718a3SScott Long }
66786f8718a3SScott Long 
66796f8718a3SScott Long static int
6680548c8f1aSPyun YongHyeon bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS)
6681548c8f1aSPyun YongHyeon {
6682548c8f1aSPyun YongHyeon 	struct bge_softc *sc;
6683548c8f1aSPyun YongHyeon 	int error;
6684548c8f1aSPyun YongHyeon 	uint16_t result;
6685548c8f1aSPyun YongHyeon 	uint32_t val;
6686548c8f1aSPyun YongHyeon 
6687548c8f1aSPyun YongHyeon 	result = -1;
6688548c8f1aSPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
6689548c8f1aSPyun YongHyeon 	if (error || (req->newptr == NULL))
6690548c8f1aSPyun YongHyeon 		return (error);
6691548c8f1aSPyun YongHyeon 
6692548c8f1aSPyun YongHyeon 	if (result < 0x8000) {
6693548c8f1aSPyun YongHyeon 		sc = (struct bge_softc *)arg1;
6694548c8f1aSPyun YongHyeon 		val = APE_READ_4(sc, result);
6695548c8f1aSPyun YongHyeon 		printf("reg 0x%06X = 0x%08X\n", result, val);
6696548c8f1aSPyun YongHyeon 	}
6697548c8f1aSPyun YongHyeon 
6698548c8f1aSPyun YongHyeon 	return (error);
6699548c8f1aSPyun YongHyeon }
6700548c8f1aSPyun YongHyeon 
6701548c8f1aSPyun YongHyeon static int
67026f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
67036f8718a3SScott Long {
67046f8718a3SScott Long 	struct bge_softc *sc;
67056f8718a3SScott Long 	int error;
67066f8718a3SScott Long 	uint16_t result;
67076f8718a3SScott Long 	uint32_t val;
67086f8718a3SScott Long 
67096f8718a3SScott Long 	result = -1;
67106f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
67116f8718a3SScott Long 	if (error || (req->newptr == NULL))
67126f8718a3SScott Long 		return (error);
67136f8718a3SScott Long 
67146f8718a3SScott Long 	if (result < 0x8000) {
67156f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
67166f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
67176f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
67186f8718a3SScott Long 	}
67196f8718a3SScott Long 
67206f8718a3SScott Long 	return (error);
67216f8718a3SScott Long }
67226f8718a3SScott Long #endif
672338cc658fSJohn Baldwin 
672438cc658fSJohn Baldwin static int
67255fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
67265fea260fSMarius Strobl {
67275fea260fSMarius Strobl 
67285fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
67295fea260fSMarius Strobl 		return (1);
67305fea260fSMarius Strobl 
67315fea260fSMarius Strobl #ifdef __sparc64__
67325fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
67335fea260fSMarius Strobl 	return (0);
67345fea260fSMarius Strobl #endif
67355fea260fSMarius Strobl 	return (1);
67365fea260fSMarius Strobl }
67375fea260fSMarius Strobl 
67385fea260fSMarius Strobl static int
673938cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
674038cc658fSJohn Baldwin {
674138cc658fSJohn Baldwin 	uint32_t mac_addr;
674238cc658fSJohn Baldwin 
674373635418SPyun YongHyeon 	mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
674438cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
674538cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
674638cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
674773635418SPyun YongHyeon 		mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
674838cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
674938cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
675038cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
675138cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
67525fea260fSMarius Strobl 		return (0);
675338cc658fSJohn Baldwin 	}
67545fea260fSMarius Strobl 	return (1);
675538cc658fSJohn Baldwin }
675638cc658fSJohn Baldwin 
675738cc658fSJohn Baldwin static int
675838cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
675938cc658fSJohn Baldwin {
676038cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
676138cc658fSJohn Baldwin 
676238cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
676338cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
676438cc658fSJohn Baldwin 
67655fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
67665fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
676738cc658fSJohn Baldwin }
676838cc658fSJohn Baldwin 
676938cc658fSJohn Baldwin static int
677038cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
677138cc658fSJohn Baldwin {
677238cc658fSJohn Baldwin 
67735fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
67745fea260fSMarius Strobl 		return (1);
67755fea260fSMarius Strobl 
67765fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
67775fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
677838cc658fSJohn Baldwin }
677938cc658fSJohn Baldwin 
678038cc658fSJohn Baldwin static int
678138cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
678238cc658fSJohn Baldwin {
678338cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
678438cc658fSJohn Baldwin 		/* NOTE: Order is critical */
67855fea260fSMarius Strobl 		bge_get_eaddr_fw,
678638cc658fSJohn Baldwin 		bge_get_eaddr_mem,
678738cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
678838cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
678938cc658fSJohn Baldwin 		NULL
679038cc658fSJohn Baldwin 	};
679138cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
679238cc658fSJohn Baldwin 
679338cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
679438cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
679538cc658fSJohn Baldwin 			break;
679638cc658fSJohn Baldwin 	}
679738cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
679838cc658fSJohn Baldwin }
6799df360178SGleb Smirnoff 
6800df360178SGleb Smirnoff static uint64_t
6801df360178SGleb Smirnoff bge_get_counter(if_t ifp, ift_counter cnt)
6802df360178SGleb Smirnoff {
6803df360178SGleb Smirnoff 	struct bge_softc *sc;
6804df360178SGleb Smirnoff 	struct bge_mac_stats *stats;
6805df360178SGleb Smirnoff 
6806df360178SGleb Smirnoff 	sc = if_getsoftc(ifp);
6807df360178SGleb Smirnoff 	if (!BGE_IS_5705_PLUS(sc))
6808df360178SGleb Smirnoff 		return (if_get_counter_default(ifp, cnt));
6809df360178SGleb Smirnoff 	stats = &sc->bge_mac_stats;
6810df360178SGleb Smirnoff 
6811df360178SGleb Smirnoff 	switch (cnt) {
6812df360178SGleb Smirnoff 	case IFCOUNTER_IERRORS:
6813df360178SGleb Smirnoff 		return (stats->NoMoreRxBDs + stats->InputDiscards +
6814df360178SGleb Smirnoff 		    stats->InputErrors);
6815df360178SGleb Smirnoff 	case IFCOUNTER_COLLISIONS:
6816df360178SGleb Smirnoff 		return (stats->etherStatsCollisions);
6817df360178SGleb Smirnoff 	default:
6818df360178SGleb Smirnoff 		return (if_get_counter_default(ifp, cnt));
6819df360178SGleb Smirnoff 	}
6820df360178SGleb Smirnoff }
6821ded66962SMark Johnston 
6822ded66962SMark Johnston #ifdef NETDUMP
6823ded66962SMark Johnston static void
6824ded66962SMark Johnston bge_netdump_init(if_t ifp, int *nrxr, int *ncl, int *clsize)
6825ded66962SMark Johnston {
6826ded66962SMark Johnston 	struct bge_softc *sc;
6827ded66962SMark Johnston 
6828ded66962SMark Johnston 	sc = if_getsoftc(ifp);
6829ded66962SMark Johnston 	BGE_LOCK(sc);
6830ded66962SMark Johnston 	*nrxr = sc->bge_return_ring_cnt;
6831ded66962SMark Johnston 	*ncl = NETDUMP_MAX_IN_FLIGHT;
6832ded66962SMark Johnston 	if ((sc->bge_flags & BGE_FLAG_JUMBO_STD) != 0 &&
6833ded66962SMark Johnston 	    (if_getmtu(sc->bge_ifp) + ETHER_HDR_LEN + ETHER_CRC_LEN +
6834ded66962SMark Johnston 	    ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN)))
6835ded66962SMark Johnston 		*clsize = MJUM9BYTES;
6836ded66962SMark Johnston 	else
6837ded66962SMark Johnston 		*clsize = MCLBYTES;
6838ded66962SMark Johnston 	BGE_UNLOCK(sc);
6839ded66962SMark Johnston }
6840ded66962SMark Johnston 
6841ded66962SMark Johnston static void
6842ded66962SMark Johnston bge_netdump_event(if_t ifp __unused, enum netdump_ev event __unused)
6843ded66962SMark Johnston {
6844ded66962SMark Johnston }
6845ded66962SMark Johnston 
6846ded66962SMark Johnston static int
6847ded66962SMark Johnston bge_netdump_transmit(if_t ifp, struct mbuf *m)
6848ded66962SMark Johnston {
6849ded66962SMark Johnston 	struct bge_softc *sc;
6850ded66962SMark Johnston 	uint32_t prodidx;
6851ded66962SMark Johnston 	int error;
6852ded66962SMark Johnston 
6853ded66962SMark Johnston 	sc = if_getsoftc(ifp);
6854ded66962SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
6855ded66962SMark Johnston 	    IFF_DRV_RUNNING)
6856ded66962SMark Johnston 		return (1);
6857ded66962SMark Johnston 
6858ded66962SMark Johnston 	prodidx = sc->bge_tx_prodidx;
6859ded66962SMark Johnston 	error = bge_encap(sc, &m, &prodidx);
6860ded66962SMark Johnston 	if (error == 0)
6861ded66962SMark Johnston 		bge_start_tx(sc, prodidx);
6862ded66962SMark Johnston 	return (error);
6863ded66962SMark Johnston }
6864ded66962SMark Johnston 
6865ded66962SMark Johnston static int
6866ded66962SMark Johnston bge_netdump_poll(if_t ifp, int count)
6867ded66962SMark Johnston {
6868ded66962SMark Johnston 	struct bge_softc *sc;
6869ded66962SMark Johnston 	uint32_t rx_prod, tx_cons;
6870ded66962SMark Johnston 
6871ded66962SMark Johnston 	sc = if_getsoftc(ifp);
6872ded66962SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
6873ded66962SMark Johnston 	    IFF_DRV_RUNNING)
6874ded66962SMark Johnston 		return (1);
6875ded66962SMark Johnston 
6876ded66962SMark Johnston 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
6877ded66962SMark Johnston 	    sc->bge_cdata.bge_status_map,
6878ded66962SMark Johnston 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
6879ded66962SMark Johnston 
6880ded66962SMark Johnston 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
6881ded66962SMark Johnston 	tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
6882ded66962SMark Johnston 
6883ded66962SMark Johnston 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
6884ded66962SMark Johnston 	    sc->bge_cdata.bge_status_map,
6885ded66962SMark Johnston 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
6886ded66962SMark Johnston 
6887ded66962SMark Johnston 	(void)bge_rxeof(sc, rx_prod, 0);
6888ded66962SMark Johnston 	bge_txeof(sc, tx_cons);
6889ded66962SMark Johnston 	return (0);
6890ded66962SMark Johnston }
6891ded66962SMark Johnston #endif /* NETDUMP */
6892