xref: /freebsd/sys/dev/bge/if_bge.c (revision e6bf277effaa89ed0726c948327607543d7b6040)
1098ca2bdSWarner Losh /*-
295d67482SBill Paul  * Copyright (c) 2001 Wind River Systems
395d67482SBill Paul  * Copyright (c) 1997, 1998, 1999, 2001
495d67482SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
595d67482SBill Paul  *
695d67482SBill Paul  * Redistribution and use in source and binary forms, with or without
795d67482SBill Paul  * modification, are permitted provided that the following conditions
895d67482SBill Paul  * are met:
995d67482SBill Paul  * 1. Redistributions of source code must retain the above copyright
1095d67482SBill Paul  *    notice, this list of conditions and the following disclaimer.
1195d67482SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1295d67482SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1395d67482SBill Paul  *    documentation and/or other materials provided with the distribution.
1495d67482SBill Paul  * 3. All advertising materials mentioning features or use of this software
1595d67482SBill Paul  *    must display the following acknowledgement:
1695d67482SBill Paul  *	This product includes software developed by Bill Paul.
1795d67482SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1895d67482SBill Paul  *    may be used to endorse or promote products derived from this software
1995d67482SBill Paul  *    without specific prior written permission.
2095d67482SBill Paul  *
2195d67482SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2295d67482SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2395d67482SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2495d67482SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2595d67482SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2695d67482SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2795d67482SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2895d67482SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2995d67482SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3095d67482SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3195d67482SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3295d67482SBill Paul  */
3395d67482SBill Paul 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
3795d67482SBill Paul /*
3895d67482SBill Paul  * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
3995d67482SBill Paul  *
4095d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
4195d67482SBill Paul  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
4295d67482SBill Paul  * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
4395d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4495d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4595d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
4695d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
4795d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
4895d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
4995d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
5095d67482SBill Paul  * into the driver.
5195d67482SBill Paul  *
5295d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5395d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5495d67482SBill Paul  *
5595d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
5698b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
5795d67482SBill Paul  * does not support external SSRAM.
5895d67482SBill Paul  *
5995d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
6095d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
6195d67482SBill Paul  *
6295d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6395d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6495d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6595d67482SBill Paul  * result, this driver does not implement any support for the mini RX
6695d67482SBill Paul  * ring.
6795d67482SBill Paul  */
6895d67482SBill Paul 
6975719184SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
7075719184SGleb Smirnoff #include "opt_device_polling.h"
7175719184SGleb Smirnoff #endif
7275719184SGleb Smirnoff 
7395d67482SBill Paul #include <sys/param.h>
74f41ac2beSBill Paul #include <sys/endian.h>
7595d67482SBill Paul #include <sys/systm.h>
7695d67482SBill Paul #include <sys/sockio.h>
7795d67482SBill Paul #include <sys/mbuf.h>
7895d67482SBill Paul #include <sys/malloc.h>
7995d67482SBill Paul #include <sys/kernel.h>
80fe12f24bSPoul-Henning Kamp #include <sys/module.h>
8195d67482SBill Paul #include <sys/socket.h>
82f1a7e6d5SScott Long #include <sys/sysctl.h>
8395d67482SBill Paul 
8495d67482SBill Paul #include <net/if.h>
8595d67482SBill Paul #include <net/if_arp.h>
8695d67482SBill Paul #include <net/ethernet.h>
8795d67482SBill Paul #include <net/if_dl.h>
8895d67482SBill Paul #include <net/if_media.h>
8995d67482SBill Paul 
9095d67482SBill Paul #include <net/bpf.h>
9195d67482SBill Paul 
9295d67482SBill Paul #include <net/if_types.h>
9395d67482SBill Paul #include <net/if_vlan_var.h>
9495d67482SBill Paul 
9595d67482SBill Paul #include <netinet/in_systm.h>
9695d67482SBill Paul #include <netinet/in.h>
9795d67482SBill Paul #include <netinet/ip.h>
9895d67482SBill Paul 
9995d67482SBill Paul #include <machine/bus.h>
10095d67482SBill Paul #include <machine/resource.h>
10195d67482SBill Paul #include <sys/bus.h>
10295d67482SBill Paul #include <sys/rman.h>
10395d67482SBill Paul 
10495d67482SBill Paul #include <dev/mii/mii.h>
10595d67482SBill Paul #include <dev/mii/miivar.h>
1062d3ce713SDavid E. O'Brien #include "miidevs.h"
10795d67482SBill Paul #include <dev/mii/brgphyreg.h>
10895d67482SBill Paul 
10908013fd3SMarius Strobl #ifdef __sparc64__
11008013fd3SMarius Strobl #include <dev/ofw/ofw_bus.h>
11108013fd3SMarius Strobl #include <dev/ofw/openfirm.h>
11208013fd3SMarius Strobl #include <machine/ofw_machdep.h>
11308013fd3SMarius Strobl #include <machine/ver.h>
11408013fd3SMarius Strobl #endif
11508013fd3SMarius Strobl 
1164fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1174fbd232cSWarner Losh #include <dev/pci/pcivar.h>
11895d67482SBill Paul 
11995d67482SBill Paul #include <dev/bge/if_bgereg.h>
12095d67482SBill Paul 
1215ddc5794SJohn Polstra #define	BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
122d375e524SGleb Smirnoff #define	ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
12395d67482SBill Paul 
124f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
125f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
12695d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12795d67482SBill Paul 
1287b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
12995d67482SBill Paul #include "miibus_if.h"
13095d67482SBill Paul 
13195d67482SBill Paul /*
13295d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
13395d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
13495d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13595d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13695d67482SBill Paul  */
137852c67f9SMarius Strobl static const struct bge_type {
1384c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1394c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
1404c0da0ffSGleb Smirnoff } bge_devs[] = {
1414c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5700 },
1424c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5701 },
14395d67482SBill Paul 
1444c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1000 },
1454c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1002 },
1464c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC9100 },
1474c0da0ffSGleb Smirnoff 
1484c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	APPLE_DEVICE_BCM5701 },
1494c0da0ffSGleb Smirnoff 
1504c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5700 },
1514c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5701 },
1524c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702 },
1534c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702_ALT },
1544c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702X },
1554c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703 },
1564c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703_ALT },
1574c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703X },
1584c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704C },
1594c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S },
1604c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S_ALT },
1614c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705 },
1624c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705F },
1634c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705K },
1644c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M },
1654c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M_ALT },
1664c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714C },
1674c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714S },
1684c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715 },
1694c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715S },
1704c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1714c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
172effef978SRemko Lodder 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5722 },
173a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5723 },
1744c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1754c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1764c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1774c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1784c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1794c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1804c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1814c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1824c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1834c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1849e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1859e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1869e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1879e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
188a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761 },
189a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761E },
190a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761S },
191a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5761SE },
192a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5764 },
1934c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
1944c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
1954c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
1964c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
197a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5784 },
198a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785F },
199a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5785G },
2009e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
2019e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
202a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787F },
2039e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
2044c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
2054c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
2064c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
2074c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
2084c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
20938cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906 },
21038cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906M },
211a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57760 },
212a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57780 },
213a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57788 },
214a5779553SStanislav Sedov 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM57790 },
2154c0da0ffSGleb Smirnoff 
2164c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
2174c0da0ffSGleb Smirnoff 
2184c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
2194c0da0ffSGleb Smirnoff 
220a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE4 },
221a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PW008GE5 },
222a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	FJTSU_DEVICEID_PP250450 },
223a5779553SStanislav Sedov 
2244c0da0ffSGleb Smirnoff 	{ 0, 0 }
22595d67482SBill Paul };
22695d67482SBill Paul 
2274c0da0ffSGleb Smirnoff static const struct bge_vendor {
2284c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2294c0da0ffSGleb Smirnoff 	const char	*v_name;
2304c0da0ffSGleb Smirnoff } bge_vendors[] = {
2314c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2324c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2334c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2344c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2354c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2364c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
237a5779553SStanislav Sedov 	{ FJTSU_VENDORID,	"Fujitsu" },
2384c0da0ffSGleb Smirnoff 
2394c0da0ffSGleb Smirnoff 	{ 0, NULL }
2404c0da0ffSGleb Smirnoff };
2414c0da0ffSGleb Smirnoff 
2424c0da0ffSGleb Smirnoff static const struct bge_revision {
2434c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2444c0da0ffSGleb Smirnoff 	const char	*br_name;
2454c0da0ffSGleb Smirnoff } bge_revisions[] = {
2464c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2474c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2484c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2494c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2504c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2514c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2524c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2534c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2544c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2554c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2564c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2574c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2584c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2594c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2604c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2614c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2629e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2634c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2644c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2654c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2664c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2674c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2684c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2694c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2704c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2714c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2724c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2734c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2744c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2754c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2764c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2774c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2784c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
27942787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2804c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2814c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2824c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
2834c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
2844c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
2854c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
2864c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
2874c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
2880c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5715_A3,	"BCM5715 A3" },
2890c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A0,	"BCM5755 A0" },
2900c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A1,	"BCM5755 A1" },
2910c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A2,	"BCM5755 A2" },
292bcc20328SJohn Baldwin 	{ BGE_CHIPID_BCM5722_A0,	"BCM5722 A0" },
293a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A0,	"BCM5761 A0" },
294a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5761_A1,	"BCM5761 A1" },
295a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A0,	"BCM5784 A0" },
296a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM5784_A1,	"BCM5784 A1" },
29781179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
2986f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
2996f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
3006f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
30138cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A1,	"BCM5906 A1" },
30238cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A2,	"BCM5906 A2" },
303a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A0,	"BCM57780 A0" },
304a5779553SStanislav Sedov 	{ BGE_CHIPID_BCM57780_A1,	"BCM57780 A1" },
3054c0da0ffSGleb Smirnoff 
3064c0da0ffSGleb Smirnoff 	{ 0, NULL }
3074c0da0ffSGleb Smirnoff };
3084c0da0ffSGleb Smirnoff 
3094c0da0ffSGleb Smirnoff /*
3104c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
3114c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
3124c0da0ffSGleb Smirnoff  */
3134c0da0ffSGleb Smirnoff static const struct bge_revision bge_majorrevs[] = {
3149e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
3159e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
3169e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
3179e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
3189e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
3199e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
3209e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
3219e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
3229e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
3239e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
3249e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
325a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5761,		"unknown BCM5761" },
326a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5784,		"unknown BCM5784" },
327a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM5785,		"unknown BCM5785" },
32881179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3296f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
33038cc658fSJohn Baldwin 	{ BGE_ASICREV_BCM5906,		"unknown BCM5906" },
331a5779553SStanislav Sedov 	{ BGE_ASICREV_BCM57780,		"unknown BCM57780" },
3324c0da0ffSGleb Smirnoff 
3334c0da0ffSGleb Smirnoff 	{ 0, NULL }
3344c0da0ffSGleb Smirnoff };
3354c0da0ffSGleb Smirnoff 
3360c8aa4eaSJung-uk Kim #define	BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
3370c8aa4eaSJung-uk Kim #define	BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
3380c8aa4eaSJung-uk Kim #define	BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
3390c8aa4eaSJung-uk Kim #define	BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
3400c8aa4eaSJung-uk Kim #define	BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
341a5779553SStanislav Sedov #define	BGE_IS_5755_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5755_PLUS)
3424c0da0ffSGleb Smirnoff 
3434c0da0ffSGleb Smirnoff const struct bge_revision * bge_lookup_rev(uint32_t);
3444c0da0ffSGleb Smirnoff const struct bge_vendor * bge_lookup_vendor(uint16_t);
34538cc658fSJohn Baldwin 
34638cc658fSJohn Baldwin typedef int	(*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
34738cc658fSJohn Baldwin 
348e51a25f8SAlfred Perlstein static int bge_probe(device_t);
349e51a25f8SAlfred Perlstein static int bge_attach(device_t);
350e51a25f8SAlfred Perlstein static int bge_detach(device_t);
35114afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
35214afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3533f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
354f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
355f41ac2beSBill Paul static int bge_dma_alloc(device_t);
356f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
357f41ac2beSBill Paul 
3585fea260fSMarius Strobl static int bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[]);
35938cc658fSJohn Baldwin static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
36038cc658fSJohn Baldwin static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
36138cc658fSJohn Baldwin static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
36238cc658fSJohn Baldwin static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
36338cc658fSJohn Baldwin 
364e51a25f8SAlfred Perlstein static void bge_txeof(struct bge_softc *);
3651abcdbd1SAttilio Rao static int bge_rxeof(struct bge_softc *);
36695d67482SBill Paul 
3678cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
368e51a25f8SAlfred Perlstein static void bge_tick(void *);
369e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
3703f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
371676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
37295d67482SBill Paul 
373e51a25f8SAlfred Perlstein static void bge_intr(void *);
3740f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
375e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
376e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
3770f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
378e51a25f8SAlfred Perlstein static void bge_init(void *);
379e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
380b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
381b6c974e8SWarner Losh static int bge_shutdown(device_t);
38267d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
383e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
384e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
38595d67482SBill Paul 
38638cc658fSJohn Baldwin static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
38738cc658fSJohn Baldwin static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
38838cc658fSJohn Baldwin 
3893f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
390e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
39195d67482SBill Paul 
3923e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
393e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
394cb2eacc7SYaroslav Tykhiy static void bge_setvlan(struct bge_softc *);
39595d67482SBill Paul 
396943787f3SPyun YongHyeon static int bge_newbuf_std(struct bge_softc *, int);
397943787f3SPyun YongHyeon static int bge_newbuf_jumbo(struct bge_softc *, int);
398e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
399e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
400e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
401e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
402e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
403e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
40495d67482SBill Paul 
405e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
406e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
40795d67482SBill Paul 
4085fea260fSMarius Strobl static int bge_has_eaddr(struct bge_softc *);
4093f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
410e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
41138cc658fSJohn Baldwin static void bge_writembx(struct bge_softc *, int, int);
41295d67482SBill Paul #ifdef notdef
4133f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
41495d67482SBill Paul #endif
4159ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
416e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
4174f09c4c7SMarius Strobl static void bge_set_max_readrq(struct bge_softc *, int);
41895d67482SBill Paul 
419e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
420e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
421e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
42275719184SGleb Smirnoff #ifdef DEVICE_POLLING
4231abcdbd1SAttilio Rao static int bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
42475719184SGleb Smirnoff #endif
42595d67482SBill Paul 
4268cb1383cSDoug Ambrisko #define	BGE_RESET_START 1
4278cb1383cSDoug Ambrisko #define	BGE_RESET_STOP  2
4288cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
4298cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
4308cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
4318cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
432dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
43395d67482SBill Paul 
4346f8718a3SScott Long /*
4356f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
4366f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
4376f8718a3SScott Long  * traps on certain architectures.
4386f8718a3SScott Long  */
4396f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
4406f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
4416f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
4426f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
4436f8718a3SScott Long #endif
4446f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
445763757b2SScott Long static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
4466f8718a3SScott Long 
44795d67482SBill Paul static device_method_t bge_methods[] = {
44895d67482SBill Paul 	/* Device interface */
44995d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
45095d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
45195d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
45295d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
45314afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
45414afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
45595d67482SBill Paul 
45695d67482SBill Paul 	/* bus interface */
45795d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
45895d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
45995d67482SBill Paul 
46095d67482SBill Paul 	/* MII interface */
46195d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
46295d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
46395d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
46495d67482SBill Paul 
46595d67482SBill Paul 	{ 0, 0 }
46695d67482SBill Paul };
46795d67482SBill Paul 
46895d67482SBill Paul static driver_t bge_driver = {
46995d67482SBill Paul 	"bge",
47095d67482SBill Paul 	bge_methods,
47195d67482SBill Paul 	sizeof(struct bge_softc)
47295d67482SBill Paul };
47395d67482SBill Paul 
47495d67482SBill Paul static devclass_t bge_devclass;
47595d67482SBill Paul 
476f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
47795d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
47895d67482SBill Paul 
479f1a7e6d5SScott Long static int bge_allow_asf = 1;
480f1a7e6d5SScott Long 
481f1a7e6d5SScott Long TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
482f1a7e6d5SScott Long 
483f1a7e6d5SScott Long SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
484f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
485f1a7e6d5SScott Long 	"Allow ASF mode if available");
486c4529f41SMichael Reifenberger 
48708013fd3SMarius Strobl #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
48808013fd3SMarius Strobl #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
48908013fd3SMarius Strobl #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
49008013fd3SMarius Strobl #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
49108013fd3SMarius Strobl #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
49208013fd3SMarius Strobl 
49308013fd3SMarius Strobl static int
4945fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
49508013fd3SMarius Strobl {
49608013fd3SMarius Strobl #ifdef __sparc64__
49708013fd3SMarius Strobl 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
49808013fd3SMarius Strobl 	device_t dev;
49908013fd3SMarius Strobl 	uint32_t subvendor;
50008013fd3SMarius Strobl 
50108013fd3SMarius Strobl 	dev = sc->bge_dev;
50208013fd3SMarius Strobl 
50308013fd3SMarius Strobl 	/*
50408013fd3SMarius Strobl 	 * The on-board BGEs found in sun4u machines aren't fitted with
50508013fd3SMarius Strobl 	 * an EEPROM which means that we have to obtain the MAC address
50608013fd3SMarius Strobl 	 * via OFW and that some tests will always fail.  We distinguish
50708013fd3SMarius Strobl 	 * such BGEs by the subvendor ID, which also has to be obtained
50808013fd3SMarius Strobl 	 * from OFW instead of the PCI configuration space as the latter
50908013fd3SMarius Strobl 	 * indicates Broadcom as the subvendor of the netboot interface.
51008013fd3SMarius Strobl 	 * For early Blade 1500 and 2500 we even have to check the OFW
51108013fd3SMarius Strobl 	 * device path as the subvendor ID always defaults to Broadcom
51208013fd3SMarius Strobl 	 * there.
51308013fd3SMarius Strobl 	 */
51408013fd3SMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
51508013fd3SMarius Strobl 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
51608013fd3SMarius Strobl 	    subvendor == SUN_VENDORID)
51708013fd3SMarius Strobl 		return (0);
51808013fd3SMarius Strobl 	memset(buf, 0, sizeof(buf));
51908013fd3SMarius Strobl 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
52008013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
52108013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
52208013fd3SMarius Strobl 			return (0);
52308013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
52408013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
52508013fd3SMarius Strobl 			return (0);
52608013fd3SMarius Strobl 	}
52708013fd3SMarius Strobl #endif
52808013fd3SMarius Strobl 	return (1);
52908013fd3SMarius Strobl }
53008013fd3SMarius Strobl 
5313f74909aSGleb Smirnoff static uint32_t
5323f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
53395d67482SBill Paul {
53495d67482SBill Paul 	device_t dev;
5356f8718a3SScott Long 	uint32_t val;
53695d67482SBill Paul 
53795d67482SBill Paul 	dev = sc->bge_dev;
53895d67482SBill Paul 
53995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
5406f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
5416f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
5426f8718a3SScott Long 	return (val);
54395d67482SBill Paul }
54495d67482SBill Paul 
54595d67482SBill Paul static void
5463f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
54795d67482SBill Paul {
54895d67482SBill Paul 	device_t dev;
54995d67482SBill Paul 
55095d67482SBill Paul 	dev = sc->bge_dev;
55195d67482SBill Paul 
55295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
55395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
5546f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
55595d67482SBill Paul }
55695d67482SBill Paul 
5574f09c4c7SMarius Strobl /*
5584f09c4c7SMarius Strobl  * PCI Express only
5594f09c4c7SMarius Strobl  */
5604f09c4c7SMarius Strobl static void
5614f09c4c7SMarius Strobl bge_set_max_readrq(struct bge_softc *sc, int expr_ptr)
5624f09c4c7SMarius Strobl {
5634f09c4c7SMarius Strobl 	device_t dev;
5644f09c4c7SMarius Strobl 	uint16_t val;
5654f09c4c7SMarius Strobl 
5664f09c4c7SMarius Strobl 	KASSERT((sc->bge_flags & BGE_FLAG_PCIE) && expr_ptr != 0,
5674f09c4c7SMarius Strobl 	    ("%s: not applicable", __func__));
5684f09c4c7SMarius Strobl 
5694f09c4c7SMarius Strobl 	dev = sc->bge_dev;
5704f09c4c7SMarius Strobl 
5714f09c4c7SMarius Strobl 	val = pci_read_config(dev, expr_ptr + BGE_PCIE_DEVCTL, 2);
5724f09c4c7SMarius Strobl 	if ((val & BGE_PCIE_DEVCTL_MAX_READRQ_MASK) !=
5734f09c4c7SMarius Strobl 	    BGE_PCIE_DEVCTL_MAX_READRQ_4096) {
5744f09c4c7SMarius Strobl 		if (bootverbose)
5754f09c4c7SMarius Strobl 			device_printf(dev, "adjust device control 0x%04x ",
5764f09c4c7SMarius Strobl 			    val);
5774f09c4c7SMarius Strobl 		val &= ~BGE_PCIE_DEVCTL_MAX_READRQ_MASK;
5784f09c4c7SMarius Strobl 		val |= BGE_PCIE_DEVCTL_MAX_READRQ_4096;
5794f09c4c7SMarius Strobl 		pci_write_config(dev, expr_ptr + BGE_PCIE_DEVCTL, val, 2);
5804f09c4c7SMarius Strobl 		if (bootverbose)
5814f09c4c7SMarius Strobl 			printf("-> 0x%04x\n", val);
5824f09c4c7SMarius Strobl 	}
5834f09c4c7SMarius Strobl }
5844f09c4c7SMarius Strobl 
58595d67482SBill Paul #ifdef notdef
5863f74909aSGleb Smirnoff static uint32_t
5873f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
58895d67482SBill Paul {
58995d67482SBill Paul 	device_t dev;
59095d67482SBill Paul 
59195d67482SBill Paul 	dev = sc->bge_dev;
59295d67482SBill Paul 
59395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
59495d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
59595d67482SBill Paul }
59695d67482SBill Paul #endif
59795d67482SBill Paul 
59895d67482SBill Paul static void
5993f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
60095d67482SBill Paul {
60195d67482SBill Paul 	device_t dev;
60295d67482SBill Paul 
60395d67482SBill Paul 	dev = sc->bge_dev;
60495d67482SBill Paul 
60595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
60695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
60795d67482SBill Paul }
60895d67482SBill Paul 
6096f8718a3SScott Long static void
6106f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
6116f8718a3SScott Long {
6126f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
6136f8718a3SScott Long }
6146f8718a3SScott Long 
61538cc658fSJohn Baldwin static void
61638cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
61738cc658fSJohn Baldwin {
61838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
61938cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
62038cc658fSJohn Baldwin 
62138cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
62238cc658fSJohn Baldwin }
62338cc658fSJohn Baldwin 
624f41ac2beSBill Paul /*
625f41ac2beSBill Paul  * Map a single buffer address.
626f41ac2beSBill Paul  */
627f41ac2beSBill Paul 
628f41ac2beSBill Paul static void
6293f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
630f41ac2beSBill Paul {
631f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
632f41ac2beSBill Paul 
633f41ac2beSBill Paul 	if (error)
634f41ac2beSBill Paul 		return;
635f41ac2beSBill Paul 
636f41ac2beSBill Paul 	ctx = arg;
637f41ac2beSBill Paul 
638f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
639f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
640f41ac2beSBill Paul 		return;
641f41ac2beSBill Paul 	}
642f41ac2beSBill Paul 
643f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
644f41ac2beSBill Paul }
645f41ac2beSBill Paul 
64638cc658fSJohn Baldwin static uint8_t
64738cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
64838cc658fSJohn Baldwin {
64938cc658fSJohn Baldwin 	uint32_t access, byte = 0;
65038cc658fSJohn Baldwin 	int i;
65138cc658fSJohn Baldwin 
65238cc658fSJohn Baldwin 	/* Lock. */
65338cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
65438cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
65538cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
65638cc658fSJohn Baldwin 			break;
65738cc658fSJohn Baldwin 		DELAY(20);
65838cc658fSJohn Baldwin 	}
65938cc658fSJohn Baldwin 	if (i == 8000)
66038cc658fSJohn Baldwin 		return (1);
66138cc658fSJohn Baldwin 
66238cc658fSJohn Baldwin 	/* Enable access. */
66338cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
66438cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
66538cc658fSJohn Baldwin 
66638cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
66738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
66838cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
66938cc658fSJohn Baldwin 		DELAY(10);
67038cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
67138cc658fSJohn Baldwin 			DELAY(10);
67238cc658fSJohn Baldwin 			break;
67338cc658fSJohn Baldwin 		}
67438cc658fSJohn Baldwin 	}
67538cc658fSJohn Baldwin 
67638cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
67738cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
67838cc658fSJohn Baldwin 		return (1);
67938cc658fSJohn Baldwin 	}
68038cc658fSJohn Baldwin 
68138cc658fSJohn Baldwin 	/* Get result. */
68238cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
68338cc658fSJohn Baldwin 
68438cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
68538cc658fSJohn Baldwin 
68638cc658fSJohn Baldwin 	/* Disable access. */
68738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
68838cc658fSJohn Baldwin 
68938cc658fSJohn Baldwin 	/* Unlock. */
69038cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
69138cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
69238cc658fSJohn Baldwin 
69338cc658fSJohn Baldwin 	return (0);
69438cc658fSJohn Baldwin }
69538cc658fSJohn Baldwin 
69638cc658fSJohn Baldwin /*
69738cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
69838cc658fSJohn Baldwin  */
69938cc658fSJohn Baldwin static int
70038cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
70138cc658fSJohn Baldwin {
70238cc658fSJohn Baldwin 	int err = 0, i;
70338cc658fSJohn Baldwin 	uint8_t byte = 0;
70438cc658fSJohn Baldwin 
70538cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
70638cc658fSJohn Baldwin 		return (1);
70738cc658fSJohn Baldwin 
70838cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
70938cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
71038cc658fSJohn Baldwin 		if (err)
71138cc658fSJohn Baldwin 			break;
71238cc658fSJohn Baldwin 		*(dest + i) = byte;
71338cc658fSJohn Baldwin 	}
71438cc658fSJohn Baldwin 
71538cc658fSJohn Baldwin 	return (err ? 1 : 0);
71638cc658fSJohn Baldwin }
71738cc658fSJohn Baldwin 
71895d67482SBill Paul /*
71995d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
72095d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
72195d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
72295d67482SBill Paul  * access method.
72395d67482SBill Paul  */
7243f74909aSGleb Smirnoff static uint8_t
7253f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
72695d67482SBill Paul {
72795d67482SBill Paul 	int i;
7283f74909aSGleb Smirnoff 	uint32_t byte = 0;
72995d67482SBill Paul 
73095d67482SBill Paul 	/*
73195d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
73295d67482SBill Paul 	 * having to use the bitbang method.
73395d67482SBill Paul 	 */
73495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
73595d67482SBill Paul 
73695d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
73795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
73895d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
73995d67482SBill Paul 	DELAY(20);
74095d67482SBill Paul 
74195d67482SBill Paul 	/* Issue the read EEPROM command. */
74295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
74395d67482SBill Paul 
74495d67482SBill Paul 	/* Wait for completion */
74595d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
74695d67482SBill Paul 		DELAY(10);
74795d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
74895d67482SBill Paul 			break;
74995d67482SBill Paul 	}
75095d67482SBill Paul 
751d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
752fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
753f6789fbaSPyun YongHyeon 		return (1);
75495d67482SBill Paul 	}
75595d67482SBill Paul 
75695d67482SBill Paul 	/* Get result. */
75795d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
75895d67482SBill Paul 
7590c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
76095d67482SBill Paul 
76195d67482SBill Paul 	return (0);
76295d67482SBill Paul }
76395d67482SBill Paul 
76495d67482SBill Paul /*
76595d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
76695d67482SBill Paul  */
76795d67482SBill Paul static int
7683f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
76995d67482SBill Paul {
7703f74909aSGleb Smirnoff 	int i, error = 0;
7713f74909aSGleb Smirnoff 	uint8_t byte = 0;
77295d67482SBill Paul 
77395d67482SBill Paul 	for (i = 0; i < cnt; i++) {
7743f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
7753f74909aSGleb Smirnoff 		if (error)
77695d67482SBill Paul 			break;
77795d67482SBill Paul 		*(dest + i) = byte;
77895d67482SBill Paul 	}
77995d67482SBill Paul 
7803f74909aSGleb Smirnoff 	return (error ? 1 : 0);
78195d67482SBill Paul }
78295d67482SBill Paul 
78395d67482SBill Paul static int
7843f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
78595d67482SBill Paul {
78695d67482SBill Paul 	struct bge_softc *sc;
7873f74909aSGleb Smirnoff 	uint32_t val, autopoll;
78895d67482SBill Paul 	int i;
78995d67482SBill Paul 
79095d67482SBill Paul 	sc = device_get_softc(dev);
79195d67482SBill Paul 
7920434d1b8SBill Paul 	/*
7930434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
7940434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
7950434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
7960434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
7970434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
7980434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
7990434d1b8SBill Paul 	 * special-cased.
8000434d1b8SBill Paul 	 */
801b1265c1aSJohn Polstra 	if (phy != 1)
80298b28ee5SBill Paul 		return (0);
80398b28ee5SBill Paul 
80437ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
80537ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
80637ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
80737ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
80837ceeb4dSPaul Saab 		DELAY(40);
80937ceeb4dSPaul Saab 	}
81037ceeb4dSPaul Saab 
81195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
81295d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
81395d67482SBill Paul 
81495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
815d5d23857SJung-uk Kim 		DELAY(10);
81695d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
81795d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
81895d67482SBill Paul 			break;
81995d67482SBill Paul 	}
82095d67482SBill Paul 
82195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
8225fea260fSMarius Strobl 		device_printf(sc->bge_dev,
8235fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
8245fea260fSMarius Strobl 		    phy, reg, val);
82537ceeb4dSPaul Saab 		val = 0;
82637ceeb4dSPaul Saab 		goto done;
82795d67482SBill Paul 	}
82895d67482SBill Paul 
82938cc658fSJohn Baldwin 	DELAY(5);
83095d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
83195d67482SBill Paul 
83237ceeb4dSPaul Saab done:
83337ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
83437ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
83537ceeb4dSPaul Saab 		DELAY(40);
83637ceeb4dSPaul Saab 	}
83737ceeb4dSPaul Saab 
83895d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
83995d67482SBill Paul 		return (0);
84095d67482SBill Paul 
8410c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
84295d67482SBill Paul }
84395d67482SBill Paul 
84495d67482SBill Paul static int
8453f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
84695d67482SBill Paul {
84795d67482SBill Paul 	struct bge_softc *sc;
8483f74909aSGleb Smirnoff 	uint32_t autopoll;
84995d67482SBill Paul 	int i;
85095d67482SBill Paul 
85195d67482SBill Paul 	sc = device_get_softc(dev);
85295d67482SBill Paul 
85338cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
85438cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
85538cc658fSJohn Baldwin 		return(0);
85638cc658fSJohn Baldwin 
85737ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
85837ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
85937ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
86037ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
86137ceeb4dSPaul Saab 		DELAY(40);
86237ceeb4dSPaul Saab 	}
86337ceeb4dSPaul Saab 
86495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
86595d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
86695d67482SBill Paul 
86795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
868d5d23857SJung-uk Kim 		DELAY(10);
86938cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
87038cc658fSJohn Baldwin 			DELAY(5);
87138cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
87295d67482SBill Paul 			break;
873d5d23857SJung-uk Kim 		}
87438cc658fSJohn Baldwin 	}
875d5d23857SJung-uk Kim 
876d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT) {
87738cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
87838cc658fSJohn Baldwin 		    "PHY write timed out (phy %d, reg %d, val %d)\n",
87938cc658fSJohn Baldwin 		    phy, reg, val);
880d5d23857SJung-uk Kim 		return (0);
88195d67482SBill Paul 	}
88295d67482SBill Paul 
88337ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
88437ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
88537ceeb4dSPaul Saab 		DELAY(40);
88637ceeb4dSPaul Saab 	}
88737ceeb4dSPaul Saab 
88895d67482SBill Paul 	return (0);
88995d67482SBill Paul }
89095d67482SBill Paul 
89195d67482SBill Paul static void
8923f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
89395d67482SBill Paul {
89495d67482SBill Paul 	struct bge_softc *sc;
89595d67482SBill Paul 	struct mii_data *mii;
89695d67482SBill Paul 	sc = device_get_softc(dev);
89795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
89895d67482SBill Paul 
89995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
9003f74909aSGleb Smirnoff 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)
90195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
9023f74909aSGleb Smirnoff 	else
90395d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
90495d67482SBill Paul 
9053f74909aSGleb Smirnoff 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
90695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
9073f74909aSGleb Smirnoff 	else
90895d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
90995d67482SBill Paul }
91095d67482SBill Paul 
91195d67482SBill Paul /*
91295d67482SBill Paul  * Intialize a standard receive ring descriptor.
91395d67482SBill Paul  */
91495d67482SBill Paul static int
915943787f3SPyun YongHyeon bge_newbuf_std(struct bge_softc *sc, int i)
91695d67482SBill Paul {
917943787f3SPyun YongHyeon 	struct mbuf *m;
91895d67482SBill Paul 	struct bge_rx_bd *r;
919a23634a1SPyun YongHyeon 	bus_dma_segment_t segs[1];
920943787f3SPyun YongHyeon 	bus_dmamap_t map;
921a23634a1SPyun YongHyeon 	int error, nsegs;
92295d67482SBill Paul 
923943787f3SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
924943787f3SPyun YongHyeon 	if (m == NULL)
92595d67482SBill Paul 		return (ENOBUFS);
926943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
927652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
928943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
929943787f3SPyun YongHyeon 
9300ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
931943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
932a23634a1SPyun YongHyeon 	if (error != 0) {
933943787f3SPyun YongHyeon 		m_freem(m);
934a23634a1SPyun YongHyeon 		return (error);
935f41ac2beSBill Paul 	}
936943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
937943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
938943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
939943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
940943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_dmamap[i]);
941943787f3SPyun YongHyeon 	}
942943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_std_dmamap[i];
943943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
944943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_sparemap = map;
945943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_std_chain[i] = m;
946943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
947a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
948a23634a1SPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
949e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
950a23634a1SPyun YongHyeon 	r->bge_len = segs[0].ds_len;
951e907febfSPyun YongHyeon 	r->bge_idx = i;
952f41ac2beSBill Paul 
9530ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
954943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
95595d67482SBill Paul 
95695d67482SBill Paul 	return (0);
95795d67482SBill Paul }
95895d67482SBill Paul 
95995d67482SBill Paul /*
96095d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
96195d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
96295d67482SBill Paul  */
96395d67482SBill Paul static int
964943787f3SPyun YongHyeon bge_newbuf_jumbo(struct bge_softc *sc, int i)
96595d67482SBill Paul {
9661be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
967943787f3SPyun YongHyeon 	bus_dmamap_t map;
9681be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
969943787f3SPyun YongHyeon 	struct mbuf *m;
970943787f3SPyun YongHyeon 	int error, nsegs;
97195d67482SBill Paul 
972943787f3SPyun YongHyeon 	MGETHDR(m, M_DONTWAIT, MT_DATA);
973943787f3SPyun YongHyeon 	if (m == NULL)
97495d67482SBill Paul 		return (ENOBUFS);
97595d67482SBill Paul 
976943787f3SPyun YongHyeon 	m_cljget(m, M_DONTWAIT, MJUM9BYTES);
977943787f3SPyun YongHyeon 	if (!(m->m_flags & M_EXT)) {
978943787f3SPyun YongHyeon 		m_freem(m);
97995d67482SBill Paul 		return (ENOBUFS);
98095d67482SBill Paul 	}
981943787f3SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
982652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
983943787f3SPyun YongHyeon 		m_adj(m, ETHER_ALIGN);
9841be6acb7SGleb Smirnoff 
9851be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
986943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
987943787f3SPyun YongHyeon 	if (error != 0) {
988943787f3SPyun YongHyeon 		m_freem(m);
9891be6acb7SGleb Smirnoff 		return (error);
990f7cea149SGleb Smirnoff 	}
9911be6acb7SGleb Smirnoff 
992943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_chain[i] == NULL) {
993943787f3SPyun YongHyeon 		bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
994943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
995943787f3SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
996943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
997943787f3SPyun YongHyeon 	}
998943787f3SPyun YongHyeon 	map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
999943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1000943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_sparemap;
1001943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1002943787f3SPyun YongHyeon 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
10031be6acb7SGleb Smirnoff 	/*
10041be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
10051be6acb7SGleb Smirnoff 	 */
1006943787f3SPyun YongHyeon 	r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
10074e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
10084e7ba1abSGleb Smirnoff 	r->bge_idx = i;
10094e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
10104e7ba1abSGleb Smirnoff 	switch (nsegs) {
10114e7ba1abSGleb Smirnoff 	case 4:
10124e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
10134e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
10144e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
10154e7ba1abSGleb Smirnoff 	case 3:
1016e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1017e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1018e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
10194e7ba1abSGleb Smirnoff 	case 2:
10204e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
10214e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
10224e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
10234e7ba1abSGleb Smirnoff 	case 1:
10244e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
10254e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
10264e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
10274e7ba1abSGleb Smirnoff 		break;
10284e7ba1abSGleb Smirnoff 	default:
10294e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
10304e7ba1abSGleb Smirnoff 	}
1031f41ac2beSBill Paul 
1032a41504a9SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1033943787f3SPyun YongHyeon 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
103495d67482SBill Paul 
103595d67482SBill Paul 	return (0);
103695d67482SBill Paul }
103795d67482SBill Paul 
103895d67482SBill Paul /*
103995d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
104095d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
104195d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
104295d67482SBill Paul  * the NIC.
104395d67482SBill Paul  */
104495d67482SBill Paul static int
10453f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
104695d67482SBill Paul {
10473ee5d7daSPyun YongHyeon 	int error, i;
104895d67482SBill Paul 
1049e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
105003e78bd0SPyun YongHyeon 	sc->bge_std = 0;
105195d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
1052943787f3SPyun YongHyeon 		if ((error = bge_newbuf_std(sc, i)) != 0)
10533ee5d7daSPyun YongHyeon 			return (error);
105403e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
105595d67482SBill Paul 	};
105695d67482SBill Paul 
1057f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1058f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
1059f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1060f41ac2beSBill Paul 
106195d67482SBill Paul 	sc->bge_std = i - 1;
106238cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
106395d67482SBill Paul 
106495d67482SBill Paul 	return (0);
106595d67482SBill Paul }
106695d67482SBill Paul 
106795d67482SBill Paul static void
10683f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
106995d67482SBill Paul {
107095d67482SBill Paul 	int i;
107195d67482SBill Paul 
107295d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
107395d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
10740ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1075e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1076e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
10770ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1078f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1079e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1080e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
108195d67482SBill Paul 		}
1082f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
108395d67482SBill Paul 		    sizeof(struct bge_rx_bd));
108495d67482SBill Paul 	}
108595d67482SBill Paul }
108695d67482SBill Paul 
108795d67482SBill Paul static int
10883f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
108995d67482SBill Paul {
109095d67482SBill Paul 	struct bge_rcb *rcb;
10913ee5d7daSPyun YongHyeon 	int error, i;
109295d67482SBill Paul 
1093e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
109403e78bd0SPyun YongHyeon 	sc->bge_jumbo = 0;
109595d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1096943787f3SPyun YongHyeon 		if ((error = bge_newbuf_jumbo(sc, i)) != 0)
10973ee5d7daSPyun YongHyeon 			return (error);
109803e78bd0SPyun YongHyeon 		BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
109995d67482SBill Paul 	};
110095d67482SBill Paul 
1101f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1102f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
1103f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1104f41ac2beSBill Paul 
110595d67482SBill Paul 	sc->bge_jumbo = i - 1;
110695d67482SBill Paul 
1107f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
11081be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
11091be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
111067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
111195d67482SBill Paul 
111238cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
111395d67482SBill Paul 
111495d67482SBill Paul 	return (0);
111595d67482SBill Paul }
111695d67482SBill Paul 
111795d67482SBill Paul static void
11183f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
111995d67482SBill Paul {
112095d67482SBill Paul 	int i;
112195d67482SBill Paul 
112295d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
112395d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1124e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1125e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1126e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1127f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1128f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1129e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1130e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
113195d67482SBill Paul 		}
1132f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
11331be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
113495d67482SBill Paul 	}
113595d67482SBill Paul }
113695d67482SBill Paul 
113795d67482SBill Paul static void
11383f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
113995d67482SBill Paul {
114095d67482SBill Paul 	int i;
114195d67482SBill Paul 
1142f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
114395d67482SBill Paul 		return;
114495d67482SBill Paul 
114595d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
114695d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
11470ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1148e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1149e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
11500ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1151f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1152e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1153e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
115495d67482SBill Paul 		}
1155f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
115695d67482SBill Paul 		    sizeof(struct bge_tx_bd));
115795d67482SBill Paul 	}
115895d67482SBill Paul }
115995d67482SBill Paul 
116095d67482SBill Paul static int
11613f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
116295d67482SBill Paul {
116395d67482SBill Paul 	sc->bge_txcnt = 0;
116495d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
11653927098fSPaul Saab 
1166e6bf277eSPyun YongHyeon 	bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1167e6bf277eSPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
1168e6bf277eSPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
1169e6bf277eSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1170e6bf277eSPyun YongHyeon 
117114bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
117214bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
117338cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
117414bbd30fSGleb Smirnoff 
11753927098fSPaul Saab 	/* 5700 b2 errata */
1176e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
117738cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
11783927098fSPaul Saab 
117914bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
118038cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
11813927098fSPaul Saab 	/* 5700 b2 errata */
1182e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
118338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
118495d67482SBill Paul 
118595d67482SBill Paul 	return (0);
118695d67482SBill Paul }
118795d67482SBill Paul 
118895d67482SBill Paul static void
11893e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
11903e9b1bcaSJung-uk Kim {
11913e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
11923e9b1bcaSJung-uk Kim 
11933e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
11943e9b1bcaSJung-uk Kim 
11953e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
11963e9b1bcaSJung-uk Kim 
119745ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
11983e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
119945ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
12003e9b1bcaSJung-uk Kim 	else
120145ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
12023e9b1bcaSJung-uk Kim }
12033e9b1bcaSJung-uk Kim 
12043e9b1bcaSJung-uk Kim static void
12053f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
120695d67482SBill Paul {
120795d67482SBill Paul 	struct ifnet *ifp;
120895d67482SBill Paul 	struct ifmultiaddr *ifma;
12093f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
121095d67482SBill Paul 	int h, i;
121195d67482SBill Paul 
12120f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
12130f9bd73bSSam Leffler 
1214fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
121595d67482SBill Paul 
121695d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
121795d67482SBill Paul 		for (i = 0; i < 4; i++)
12180c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
121995d67482SBill Paul 		return;
122095d67482SBill Paul 	}
122195d67482SBill Paul 
122295d67482SBill Paul 	/* First, zot all the existing filters. */
122395d67482SBill Paul 	for (i = 0; i < 4; i++)
122495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
122595d67482SBill Paul 
122695d67482SBill Paul 	/* Now program new ones. */
1227eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
122895d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
122995d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
123095d67482SBill Paul 			continue;
12310e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
12320c8aa4eaSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
12330c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
123495d67482SBill Paul 	}
1235eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
123695d67482SBill Paul 
123795d67482SBill Paul 	for (i = 0; i < 4; i++)
123895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
123995d67482SBill Paul }
124095d67482SBill Paul 
12418cb1383cSDoug Ambrisko static void
1242cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1243cb2eacc7SYaroslav Tykhiy {
1244cb2eacc7SYaroslav Tykhiy 	struct ifnet *ifp;
1245cb2eacc7SYaroslav Tykhiy 
1246cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1247cb2eacc7SYaroslav Tykhiy 
1248cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1249cb2eacc7SYaroslav Tykhiy 
1250cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1251cb2eacc7SYaroslav Tykhiy 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1252cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1253cb2eacc7SYaroslav Tykhiy 	else
1254cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1255cb2eacc7SYaroslav Tykhiy }
1256cb2eacc7SYaroslav Tykhiy 
1257cb2eacc7SYaroslav Tykhiy static void
12588cb1383cSDoug Ambrisko bge_sig_pre_reset(sc, type)
12598cb1383cSDoug Ambrisko 	struct bge_softc *sc;
12608cb1383cSDoug Ambrisko 	int type;
12618cb1383cSDoug Ambrisko {
12628cb1383cSDoug Ambrisko 	/*
12638cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
12648cb1383cSDoug Ambrisko 	 */
12658cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
12668cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
12678cb1383cSDoug Ambrisko 
12688cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
12698cb1383cSDoug Ambrisko 		switch (type) {
12708cb1383cSDoug Ambrisko 		case BGE_RESET_START:
12718cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
12728cb1383cSDoug Ambrisko 			break;
12738cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
12748cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
12758cb1383cSDoug Ambrisko 			break;
12768cb1383cSDoug Ambrisko 		}
12778cb1383cSDoug Ambrisko 	}
12788cb1383cSDoug Ambrisko }
12798cb1383cSDoug Ambrisko 
12808cb1383cSDoug Ambrisko static void
12818cb1383cSDoug Ambrisko bge_sig_post_reset(sc, type)
12828cb1383cSDoug Ambrisko 	struct bge_softc *sc;
12838cb1383cSDoug Ambrisko 	int type;
12848cb1383cSDoug Ambrisko {
12858cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
12868cb1383cSDoug Ambrisko 		switch (type) {
12878cb1383cSDoug Ambrisko 		case BGE_RESET_START:
12888cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000001);
12898cb1383cSDoug Ambrisko 			/* START DONE */
12908cb1383cSDoug Ambrisko 			break;
12918cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
12928cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000002);
12938cb1383cSDoug Ambrisko 			break;
12948cb1383cSDoug Ambrisko 		}
12958cb1383cSDoug Ambrisko 	}
12968cb1383cSDoug Ambrisko }
12978cb1383cSDoug Ambrisko 
12988cb1383cSDoug Ambrisko static void
12998cb1383cSDoug Ambrisko bge_sig_legacy(sc, type)
13008cb1383cSDoug Ambrisko 	struct bge_softc *sc;
13018cb1383cSDoug Ambrisko 	int type;
13028cb1383cSDoug Ambrisko {
13038cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
13048cb1383cSDoug Ambrisko 		switch (type) {
13058cb1383cSDoug Ambrisko 		case BGE_RESET_START:
13068cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
13078cb1383cSDoug Ambrisko 			break;
13088cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
13098cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
13108cb1383cSDoug Ambrisko 			break;
13118cb1383cSDoug Ambrisko 		}
13128cb1383cSDoug Ambrisko 	}
13138cb1383cSDoug Ambrisko }
13148cb1383cSDoug Ambrisko 
13158cb1383cSDoug Ambrisko void bge_stop_fw(struct bge_softc *);
13168cb1383cSDoug Ambrisko void
13178cb1383cSDoug Ambrisko bge_stop_fw(sc)
13188cb1383cSDoug Ambrisko 	struct bge_softc *sc;
13198cb1383cSDoug Ambrisko {
13208cb1383cSDoug Ambrisko 	int i;
13218cb1383cSDoug Ambrisko 
13228cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
13238cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW, BGE_FW_PAUSE);
13248cb1383cSDoug Ambrisko 		CSR_WRITE_4(sc, BGE_CPU_EVENT,
132539153c5aSJung-uk Kim 		    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
13268cb1383cSDoug Ambrisko 
13278cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
13288cb1383cSDoug Ambrisko 			if (!(CSR_READ_4(sc, BGE_CPU_EVENT) & (1 << 14)))
13298cb1383cSDoug Ambrisko 				break;
13308cb1383cSDoug Ambrisko 			DELAY(10);
13318cb1383cSDoug Ambrisko 		}
13328cb1383cSDoug Ambrisko 	}
13338cb1383cSDoug Ambrisko }
13348cb1383cSDoug Ambrisko 
133595d67482SBill Paul /*
1336c9ffd9f0SMarius Strobl  * Do endian, PCI and DMA initialization.
133795d67482SBill Paul  */
133895d67482SBill Paul static int
13393f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
134095d67482SBill Paul {
13413f74909aSGleb Smirnoff 	uint32_t dma_rw_ctl;
134295d67482SBill Paul 	int i;
134395d67482SBill Paul 
13448cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
1345e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
134695d67482SBill Paul 
134795d67482SBill Paul 	/* Clear the MAC control register */
134895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
134995d67482SBill Paul 
135095d67482SBill Paul 	/*
135195d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
135295d67482SBill Paul 	 * internal memory.
135395d67482SBill Paul 	 */
135495d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
13553f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
135695d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
135795d67482SBill Paul 
135895d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
13593f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
136095d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
136195d67482SBill Paul 
1362186f842bSJung-uk Kim 	/*
1363186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1364186f842bSJung-uk Kim 	 */
1365186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1366186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1367652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
1368186f842bSJung-uk Kim 		/* Read watermark not used, 128 bytes for write. */
1369186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1370652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
13714c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1372186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1373186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1374186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1375186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1376186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1377186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1378186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1379186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1380186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1381186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1382186f842bSJung-uk Kim 		} else {
1383186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1384186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1385186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
13860c8aa4eaSJung-uk Kim 			    0x0F;
1387186f842bSJung-uk Kim 		}
1388e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1389e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
13903f74909aSGleb Smirnoff 			uint32_t tmp;
13915cba12d3SPaul Saab 
1392186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
13930c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1394186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1395186f842bSJung-uk Kim 				dma_rw_ctl |=
1396186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
13975cba12d3SPaul Saab 
1398186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1399186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1400186f842bSJung-uk Kim 		}
1401186f842bSJung-uk Kim 	} else {
1402186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1403186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1404186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1405186f842bSJung-uk Kim 
1406186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1407186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1408186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1409186f842bSJung-uk Kim 	}
1410186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1411186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1412186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1413186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1414e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1415186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
14165cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
14175cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
141895d67482SBill Paul 
141995d67482SBill Paul 	/*
142095d67482SBill Paul 	 * Set up general mode register.
142195d67482SBill Paul 	 */
1422e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
142395d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1424ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
142595d67482SBill Paul 
142695d67482SBill Paul 	/*
142790447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
142890447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
142990447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
143090447aadSMarius Strobl 	 * certain bridges.
143190447aadSMarius Strobl 	 */
143290447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
143390447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
143490447aadSMarius Strobl 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_FORCE_PCI32);
143590447aadSMarius Strobl 
143690447aadSMarius Strobl 	/*
14378cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
14388cb1383cSDoug Ambrisko 	 */
14398cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
14408cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
14418cb1383cSDoug Ambrisko 
14428cb1383cSDoug Ambrisko 	/*
1443ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1444c9ffd9f0SMarius Strobl 	 * properly by these devices.  Also ensure that INTx isn't disabled,
1445c9ffd9f0SMarius Strobl 	 * as these chips need it even when using MSI.
144695d67482SBill Paul 	 */
1447c9ffd9f0SMarius Strobl 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD,
1448c9ffd9f0SMarius Strobl 	    PCIM_CMD_INTxDIS | PCIM_CMD_MWIEN, 4);
144995d67482SBill Paul 
145095d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
14510c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
145295d67482SBill Paul 
145338cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
145438cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
145538cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
145638cc658fSJohn Baldwin 
145738cc658fSJohn Baldwin 		/* Put PHY into ready state */
145838cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
145938cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
146038cc658fSJohn Baldwin 		DELAY(40);
146138cc658fSJohn Baldwin 	}
146238cc658fSJohn Baldwin 
146395d67482SBill Paul 	return (0);
146495d67482SBill Paul }
146595d67482SBill Paul 
146695d67482SBill Paul static int
14673f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
146895d67482SBill Paul {
146995d67482SBill Paul 	struct bge_rcb *rcb;
1470e907febfSPyun YongHyeon 	bus_size_t vrcb;
1471e907febfSPyun YongHyeon 	bge_hostaddr taddr;
14726f8718a3SScott Long 	uint32_t val;
147395d67482SBill Paul 	int i;
147495d67482SBill Paul 
147595d67482SBill Paul 	/*
147695d67482SBill Paul 	 * Initialize the memory window pointer register so that
147795d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
147895d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
147995d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
148095d67482SBill Paul 	 */
148195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
148295d67482SBill Paul 
1483822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1484822f63fcSBill Paul 
14857ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
148695d67482SBill Paul 		/* Configure mbuf memory pool */
14870dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1488822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1489822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1490822f63fcSBill Paul 		else
149195d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
149295d67482SBill Paul 
149395d67482SBill Paul 		/* Configure DMA resource pool */
14940434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
14950434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
149695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
14970434d1b8SBill Paul 	}
149895d67482SBill Paul 
149995d67482SBill Paul 	/* Configure mbuf pool watermarks */
150038cc658fSJohn Baldwin 	if (!BGE_IS_5705_PLUS(sc)) {
1501fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1502fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1503fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
150438cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
150538cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
150638cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
150738cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
150838cc658fSJohn Baldwin 	} else {
150938cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
151038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
151138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
151238cc658fSJohn Baldwin 	}
151395d67482SBill Paul 
151495d67482SBill Paul 	/* Configure DMA resource watermarks */
151595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
151695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
151795d67482SBill Paul 
151895d67482SBill Paul 	/* Enable buffer manager */
15197ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
152095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
152195d67482SBill Paul 		    BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN);
152295d67482SBill Paul 
152395d67482SBill Paul 		/* Poll for buffer manager start indication */
152495d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
1525d5d23857SJung-uk Kim 			DELAY(10);
15260c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
152795d67482SBill Paul 				break;
152895d67482SBill Paul 		}
152995d67482SBill Paul 
153095d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1531fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1532fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
153395d67482SBill Paul 			return (ENXIO);
153495d67482SBill Paul 		}
15350434d1b8SBill Paul 	}
153695d67482SBill Paul 
153795d67482SBill Paul 	/* Enable flow-through queues */
15380c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
153995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
154095d67482SBill Paul 
154195d67482SBill Paul 	/* Wait until queue initialization is complete */
154295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1543d5d23857SJung-uk Kim 		DELAY(10);
154495d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
154595d67482SBill Paul 			break;
154695d67482SBill Paul 	}
154795d67482SBill Paul 
154895d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1549fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
155095d67482SBill Paul 		return (ENXIO);
155195d67482SBill Paul 	}
155295d67482SBill Paul 
155395d67482SBill Paul 	/* Initialize the standard RX ring control block */
1554f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1555f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1556f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1557f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1558f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1559f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1560f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
15617ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
15620434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
15630434d1b8SBill Paul 	else
15640434d1b8SBill Paul 		rcb->bge_maxlen_flags =
15650434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
156695d67482SBill Paul 	rcb->bge_nicaddr = BGE_STD_RX_RINGS;
15670c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
15680c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1569f41ac2beSBill Paul 
157067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
157167111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
157295d67482SBill Paul 
157395d67482SBill Paul 	/*
157495d67482SBill Paul 	 * Initialize the jumbo RX ring control block
157595d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
157695d67482SBill Paul 	 * field until we're actually ready to start
157795d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
157895d67482SBill Paul 	 * high enough to require it).
157995d67482SBill Paul 	 */
15804c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1581f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1582f41ac2beSBill Paul 
1583f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1584f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1585f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1586f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1587f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1588f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1589f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
15901be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
15911be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
159295d67482SBill Paul 		rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
159367111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
159467111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
159567111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
159667111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1597f41ac2beSBill Paul 
15980434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
15990434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
160067111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
160195d67482SBill Paul 
160295d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1603f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
160467111612SJohn Polstra 		rcb->bge_maxlen_flags =
160567111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
16060434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
16070434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
16080434d1b8SBill Paul 	}
160995d67482SBill Paul 
161095d67482SBill Paul 	/*
161195d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
161295d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
161395d67482SBill Paul 	 * each ring.
16149ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
16159ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
16169ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
16179ba784dbSScott Long 	 * are reports that it might not need to be so strict.
161838cc658fSJohn Baldwin 	 *
161938cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
162038cc658fSJohn Baldwin 	 * well.
162195d67482SBill Paul 	 */
16225345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
16236f8718a3SScott Long 		val = 8;
16246f8718a3SScott Long 	else
16256f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
16266f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
162795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
162895d67482SBill Paul 
162995d67482SBill Paul 	/*
163095d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
163195d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
163295d67482SBill Paul 	 * These are located in NIC memory.
163395d67482SBill Paul 	 */
1634e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
163595d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1636e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1637e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1638e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1639e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
164095d67482SBill Paul 	}
164195d67482SBill Paul 
164295d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
1643e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1644e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1645e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1646e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1647e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1648e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
16497ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
1650e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1651e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
165295d67482SBill Paul 
165395d67482SBill Paul 	/* Disable all unused RX return rings */
1654e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
165595d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1656e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1657e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1658e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
16590434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1660e907febfSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED));
1661e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
166238cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
16633f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1664e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
166595d67482SBill Paul 	}
166695d67482SBill Paul 
166795d67482SBill Paul 	/* Initialize RX ring indexes */
166838cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
166938cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
167038cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
167195d67482SBill Paul 
167295d67482SBill Paul 	/*
167395d67482SBill Paul 	 * Set up RX return ring 0
167495d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
167595d67482SBill Paul 	 * The return rings live entirely within the host, so the
167695d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
167795d67482SBill Paul 	 */
1678e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1679e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1680e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1681e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1682e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1683e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1684e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
168595d67482SBill Paul 
168695d67482SBill Paul 	/* Set random backoff seed for TX */
168795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
16884a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
16894a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
16904a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
169195d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
169295d67482SBill Paul 
169395d67482SBill Paul 	/* Set inter-packet gap */
169495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
169595d67482SBill Paul 
169695d67482SBill Paul 	/*
169795d67482SBill Paul 	 * Specify which ring to use for packets that don't match
169895d67482SBill Paul 	 * any RX rules.
169995d67482SBill Paul 	 */
170095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
170195d67482SBill Paul 
170295d67482SBill Paul 	/*
170395d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
170495d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
170595d67482SBill Paul 	 */
170695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
170795d67482SBill Paul 
170895d67482SBill Paul 	/* Inialize RX list placement stats mask. */
17090c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
171095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
171195d67482SBill Paul 
171295d67482SBill Paul 	/* Disable host coalescing until we get it set up */
171395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
171495d67482SBill Paul 
171595d67482SBill Paul 	/* Poll to make sure it's shut down. */
171695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1717d5d23857SJung-uk Kim 		DELAY(10);
171895d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
171995d67482SBill Paul 			break;
172095d67482SBill Paul 	}
172195d67482SBill Paul 
172295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1723fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1724fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
172595d67482SBill Paul 		return (ENXIO);
172695d67482SBill Paul 	}
172795d67482SBill Paul 
172895d67482SBill Paul 	/* Set up host coalescing defaults */
172995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
173095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
173195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
173295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
17337ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
173495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
173595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
17360434d1b8SBill Paul 	}
1737b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
1738b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
173995d67482SBill Paul 
174095d67482SBill Paul 	/* Set up address of statistics block */
17417ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
1742f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1743f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
174495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1745f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
17460434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
174795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
17480434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
17490434d1b8SBill Paul 	}
17500434d1b8SBill Paul 
17510434d1b8SBill Paul 	/* Set up address of status block */
1752f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1753f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
175495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1755f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1756f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1757f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
175895d67482SBill Paul 
175995d67482SBill Paul 	/* Turn on host coalescing state machine */
176095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
176195d67482SBill Paul 
176295d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
176395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
176495d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
176595d67482SBill Paul 
176695d67482SBill Paul 	/* Turn on RX list placement state machine */
176795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
176895d67482SBill Paul 
176995d67482SBill Paul 	/* Turn on RX list selector state machine. */
17707ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
177195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
177295d67482SBill Paul 
177395d67482SBill Paul 	/* Turn on DMA, clear stats */
177495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB |
177595d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB | BGE_MACMODE_RX_STATS_CLEAR |
177695d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR | BGE_MACMODE_RX_STATS_ENB |
177795d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB | BGE_MACMODE_FRMHDR_DMA_ENB |
1778652ae483SGleb Smirnoff 	    ((sc->bge_flags & BGE_FLAG_TBI) ?
1779652ae483SGleb Smirnoff 	    BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
178095d67482SBill Paul 
178195d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
178295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
178395d67482SBill Paul 
178495d67482SBill Paul #ifdef notdef
178595d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
178695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
178795d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
178895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
178995d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
179095d67482SBill Paul #endif
179195d67482SBill Paul 
179295d67482SBill Paul 	/* Turn on DMA completion state machine */
17937ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
179495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
179595d67482SBill Paul 
17966f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
17976f8718a3SScott Long 
17986f8718a3SScott Long 	/* Enable host coalescing bug fix. */
1799a5779553SStanislav Sedov 	if (BGE_IS_5755_PLUS(sc))
18003889907fSStanislav Sedov 		val |= BGE_WDMAMODE_STATUS_TAG_FIX;
18016f8718a3SScott Long 
180295d67482SBill Paul 	/* Turn on write DMA state machine */
18036f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
18044f09c4c7SMarius Strobl 	DELAY(40);
180595d67482SBill Paul 
180695d67482SBill Paul 	/* Turn on read DMA state machine */
18074f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
1808a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
1809a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
1810a5779553SStanislav Sedov 	    sc->bge_asicrev == BGE_ASICREV_BCM57780)
1811a5779553SStanislav Sedov 		val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
1812a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
1813a5779553SStanislav Sedov 		    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
18144f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
18154f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
18164f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
18174f09c4c7SMarius Strobl 	DELAY(40);
181895d67482SBill Paul 
181995d67482SBill Paul 	/* Turn on RX data completion state machine */
182095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
182195d67482SBill Paul 
182295d67482SBill Paul 	/* Turn on RX BD initiator state machine */
182395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
182495d67482SBill Paul 
182595d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
182695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
182795d67482SBill Paul 
182895d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
18297ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
183095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
183195d67482SBill Paul 
183295d67482SBill Paul 	/* Turn on send BD completion state machine */
183395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
183495d67482SBill Paul 
183595d67482SBill Paul 	/* Turn on send data completion state machine */
1836a5779553SStanislav Sedov 	val = BGE_SDCMODE_ENABLE;
1837a5779553SStanislav Sedov 	if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
1838a5779553SStanislav Sedov 		val |= BGE_SDCMODE_CDELAY;
1839a5779553SStanislav Sedov 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
184095d67482SBill Paul 
184195d67482SBill Paul 	/* Turn on send data initiator state machine */
184295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
184395d67482SBill Paul 
184495d67482SBill Paul 	/* Turn on send BD initiator state machine */
184595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
184695d67482SBill Paul 
184795d67482SBill Paul 	/* Turn on send BD selector state machine */
184895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
184995d67482SBill Paul 
18500c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
185195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
185295d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
185395d67482SBill Paul 
185495d67482SBill Paul 	/* ack/clear link change events */
185595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
18560434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
18570434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1858f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
185995d67482SBill Paul 
186095d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
1861652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
186295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1863a1d52896SBill Paul 	} else {
18646098821cSJung-uk Kim 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL | (10 << 16));
18651f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
18664c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
1867a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1868a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1869a1d52896SBill Paul 	}
187095d67482SBill Paul 
18711f313773SOleg Bulyzhin 	/*
18721f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
18731f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
18741f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
18751f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
18761f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
18771f313773SOleg Bulyzhin 	 */
18781f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
18791f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
18801f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
18811f313773SOleg Bulyzhin 
188295d67482SBill Paul 	/* Enable link state change attentions. */
188395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
188495d67482SBill Paul 
188595d67482SBill Paul 	return (0);
188695d67482SBill Paul }
188795d67482SBill Paul 
18884c0da0ffSGleb Smirnoff const struct bge_revision *
18894c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
18904c0da0ffSGleb Smirnoff {
18914c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
18924c0da0ffSGleb Smirnoff 
18934c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
18944c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
18954c0da0ffSGleb Smirnoff 			return (br);
18964c0da0ffSGleb Smirnoff 	}
18974c0da0ffSGleb Smirnoff 
18984c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
18994c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
19004c0da0ffSGleb Smirnoff 			return (br);
19014c0da0ffSGleb Smirnoff 	}
19024c0da0ffSGleb Smirnoff 
19034c0da0ffSGleb Smirnoff 	return (NULL);
19044c0da0ffSGleb Smirnoff }
19054c0da0ffSGleb Smirnoff 
19064c0da0ffSGleb Smirnoff const struct bge_vendor *
19074c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
19084c0da0ffSGleb Smirnoff {
19094c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
19104c0da0ffSGleb Smirnoff 
19114c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
19124c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
19134c0da0ffSGleb Smirnoff 			return (v);
19144c0da0ffSGleb Smirnoff 
19154c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
19164c0da0ffSGleb Smirnoff 	return (NULL);
19174c0da0ffSGleb Smirnoff }
19184c0da0ffSGleb Smirnoff 
191995d67482SBill Paul /*
192095d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
19214c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
19224c0da0ffSGleb Smirnoff  *
19234c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
19247c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
19257c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
19267c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
19277c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
192895d67482SBill Paul  */
192995d67482SBill Paul static int
19303f74909aSGleb Smirnoff bge_probe(device_t dev)
193195d67482SBill Paul {
1932852c67f9SMarius Strobl 	const struct bge_type *t = bge_devs;
19334c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
19347c929cf9SJung-uk Kim 	uint16_t vid, did;
193595d67482SBill Paul 
193695d67482SBill Paul 	sc->bge_dev = dev;
19377c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
19387c929cf9SJung-uk Kim 	did = pci_get_device(dev);
19394c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
19407c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
19417c929cf9SJung-uk Kim 			char model[64], buf[96];
19424c0da0ffSGleb Smirnoff 			const struct bge_revision *br;
19434c0da0ffSGleb Smirnoff 			const struct bge_vendor *v;
19444c0da0ffSGleb Smirnoff 			uint32_t id;
19454c0da0ffSGleb Smirnoff 
1946a5779553SStanislav Sedov 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
1947a5779553SStanislav Sedov 			    BGE_PCIMISCCTL_ASICREV_SHIFT;
1948a5779553SStanislav Sedov 			if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG)
1949a5779553SStanislav Sedov 				id = pci_read_config(dev,
1950a5779553SStanislav Sedov 				    BGE_PCI_PRODID_ASICREV, 4);
19514c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
19527c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
19534e35d186SJung-uk Kim 			{
19544e35d186SJung-uk Kim #if __FreeBSD_version > 700024
19554e35d186SJung-uk Kim 				const char *pname;
19564e35d186SJung-uk Kim 
1957852c67f9SMarius Strobl 				if (bge_has_eaddr(sc) &&
1958852c67f9SMarius Strobl 				    pci_get_vpd_ident(dev, &pname) == 0)
19594e35d186SJung-uk Kim 					snprintf(model, 64, "%s", pname);
19604e35d186SJung-uk Kim 				else
19614e35d186SJung-uk Kim #endif
19627c929cf9SJung-uk Kim 					snprintf(model, 64, "%s %s",
19637c929cf9SJung-uk Kim 					    v->v_name,
19647c929cf9SJung-uk Kim 					    br != NULL ? br->br_name :
19657c929cf9SJung-uk Kim 					    "NetXtreme Ethernet Controller");
19664e35d186SJung-uk Kim 			}
1967a5779553SStanislav Sedov 			snprintf(buf, 96, "%s, %sASIC rev. %#08x", model,
1968a5779553SStanislav Sedov 			    br != NULL ? "" : "unknown ", id);
19694c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
19706d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
19715ee49a3aSJung-uk Kim 				sc->bge_flags |= BGE_FLAG_NO_3LED;
197208bf8bb7SJung-uk Kim 			if (did == BCOM_DEVICEID_BCM5755M)
197308bf8bb7SJung-uk Kim 				sc->bge_flags |= BGE_FLAG_ADJUST_TRIM;
197495d67482SBill Paul 			return (0);
197595d67482SBill Paul 		}
197695d67482SBill Paul 		t++;
197795d67482SBill Paul 	}
197895d67482SBill Paul 
197995d67482SBill Paul 	return (ENXIO);
198095d67482SBill Paul }
198195d67482SBill Paul 
1982f41ac2beSBill Paul static void
19833f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
1984f41ac2beSBill Paul {
1985f41ac2beSBill Paul 	int i;
1986f41ac2beSBill Paul 
19873f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
1988f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1989f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
19900ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
1991f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1992f41ac2beSBill Paul 	}
1993943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_sparemap)
1994943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
1995943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_sparemap);
1996f41ac2beSBill Paul 
19973f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
1998f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1999f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2000f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2001f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2002f41ac2beSBill Paul 	}
2003943787f3SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2004943787f3SPyun YongHyeon 		bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2005943787f3SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_sparemap);
2006f41ac2beSBill Paul 
20073f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
2008f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2009f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
20100ac56796SPyun YongHyeon 			bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2011f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
2012f41ac2beSBill Paul 	}
2013f41ac2beSBill Paul 
20140ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_mtag)
20150ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
20160ac56796SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_mtag)
20170ac56796SPyun YongHyeon 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2018f41ac2beSBill Paul 
2019f41ac2beSBill Paul 
20203f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
2021e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
2022e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2023e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
2024e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
2025f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2026f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
2027f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
2028f41ac2beSBill Paul 
2029f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
2030f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2031f41ac2beSBill Paul 
20323f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
2033e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
2034e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2035e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2036e65bed95SPyun YongHyeon 
2037e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
2038e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
2039f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2040f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
2041f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
2042f41ac2beSBill Paul 
2043f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2044f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2045f41ac2beSBill Paul 
20463f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
2047e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
2048e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2049e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
2050e65bed95SPyun YongHyeon 
2051e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
2052e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
2053f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2054f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
2055f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
2056f41ac2beSBill Paul 
2057f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2058f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2059f41ac2beSBill Paul 
20603f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2061e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
2062e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2063e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2064e65bed95SPyun YongHyeon 
2065e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2066f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2067f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2068f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2069f41ac2beSBill Paul 
2070f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2071f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2072f41ac2beSBill Paul 
20733f74909aSGleb Smirnoff 	/* Destroy status block. */
2074e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
2075e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2076e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2077e65bed95SPyun YongHyeon 
2078e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2079f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2080f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2081f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2082f41ac2beSBill Paul 
2083f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2084f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2085f41ac2beSBill Paul 
20863f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2087e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
2088e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2089e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2090e65bed95SPyun YongHyeon 
2091e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2092f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2093f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2094f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2095f41ac2beSBill Paul 
2096f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2097f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2098f41ac2beSBill Paul 
20993f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2100f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2101f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2102f41ac2beSBill Paul }
2103f41ac2beSBill Paul 
2104f41ac2beSBill Paul static int
21053f74909aSGleb Smirnoff bge_dma_alloc(device_t dev)
2106f41ac2beSBill Paul {
21073f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
2108f41ac2beSBill Paul 	struct bge_softc *sc;
21091be6acb7SGleb Smirnoff 	int i, error;
2110f41ac2beSBill Paul 
2111f41ac2beSBill Paul 	sc = device_get_softc(dev);
2112f41ac2beSBill Paul 
2113f41ac2beSBill Paul 	/*
2114f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2115f41ac2beSBill Paul 	 */
21164eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
21174eee14cbSMarius Strobl 	    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,	NULL,
21184eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
21194eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2120f41ac2beSBill Paul 
2121e65bed95SPyun YongHyeon 	if (error != 0) {
2122fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2123fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2124e65bed95SPyun YongHyeon 		return (ENOMEM);
2125e65bed95SPyun YongHyeon 	}
2126e65bed95SPyun YongHyeon 
2127f41ac2beSBill Paul 	/*
21280ac56796SPyun YongHyeon 	 * Create tag for Tx mbufs.
2129f41ac2beSBill Paul 	 */
21308a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
2131f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
21321be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
21330ac56796SPyun YongHyeon 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_tx_mtag);
2134f41ac2beSBill Paul 
2135f41ac2beSBill Paul 	if (error) {
21360ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
21370ac56796SPyun YongHyeon 		return (ENOMEM);
21380ac56796SPyun YongHyeon 	}
21390ac56796SPyun YongHyeon 
21400ac56796SPyun YongHyeon 	/*
21410ac56796SPyun YongHyeon 	 * Create tag for Rx mbufs.
21420ac56796SPyun YongHyeon 	 */
21430ac56796SPyun YongHyeon 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1, 0,
21440ac56796SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1,
21450ac56796SPyun YongHyeon 	    MCLBYTES, BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
21460ac56796SPyun YongHyeon 
21470ac56796SPyun YongHyeon 	if (error) {
21480ac56796SPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
2149f41ac2beSBill Paul 		return (ENOMEM);
2150f41ac2beSBill Paul 	}
2151f41ac2beSBill Paul 
21523f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
2153943787f3SPyun YongHyeon 	error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2154943787f3SPyun YongHyeon 	    &sc->bge_cdata.bge_rx_std_sparemap);
2155943787f3SPyun YongHyeon 	if (error) {
2156943787f3SPyun YongHyeon 		device_printf(sc->bge_dev,
2157943787f3SPyun YongHyeon 		    "can't create spare DMA map for RX\n");
2158943787f3SPyun YongHyeon 		return (ENOMEM);
2159943787f3SPyun YongHyeon 	}
2160f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
21610ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
2162f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
2163f41ac2beSBill Paul 		if (error) {
2164fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
2165fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
2166f41ac2beSBill Paul 			return (ENOMEM);
2167f41ac2beSBill Paul 		}
2168f41ac2beSBill Paul 	}
2169f41ac2beSBill Paul 
21703f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
2171f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
21720ac56796SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
2173f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
2174f41ac2beSBill Paul 		if (error) {
2175fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
21760ac56796SPyun YongHyeon 			    "can't create DMA map for TX\n");
2177f41ac2beSBill Paul 			return (ENOMEM);
2178f41ac2beSBill Paul 		}
2179f41ac2beSBill Paul 	}
2180f41ac2beSBill Paul 
21813f74909aSGleb Smirnoff 	/* Create tag for standard RX ring. */
2182f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2183f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2184f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
2185f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
2186f41ac2beSBill Paul 
2187f41ac2beSBill Paul 	if (error) {
2188fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2189f41ac2beSBill Paul 		return (ENOMEM);
2190f41ac2beSBill Paul 	}
2191f41ac2beSBill Paul 
21923f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for standard RX ring. */
2193f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
2194f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
2195f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
2196f41ac2beSBill Paul 	if (error)
2197f41ac2beSBill Paul 		return (ENOMEM);
2198f41ac2beSBill Paul 
2199f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
2200f41ac2beSBill Paul 
22013f74909aSGleb Smirnoff 	/* Load the address of the standard RX ring. */
2202f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2203f41ac2beSBill Paul 	ctx.sc = sc;
2204f41ac2beSBill Paul 
2205f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
2206f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
2207f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2208f41ac2beSBill Paul 
2209f41ac2beSBill Paul 	if (error)
2210f41ac2beSBill Paul 		return (ENOMEM);
2211f41ac2beSBill Paul 
2212f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
2213f41ac2beSBill Paul 
22143f74909aSGleb Smirnoff 	/* Create tags for jumbo mbufs. */
22154c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2216f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
22178a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
22181be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
22191be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
2220f41ac2beSBill Paul 		if (error) {
2221fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
22223f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
2223f41ac2beSBill Paul 			return (ENOMEM);
2224f41ac2beSBill Paul 		}
2225f41ac2beSBill Paul 
22263f74909aSGleb Smirnoff 		/* Create tag for jumbo RX ring. */
2227f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2228f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2229f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
2230f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
2231f41ac2beSBill Paul 
2232f41ac2beSBill Paul 		if (error) {
2233fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
22343f74909aSGleb Smirnoff 			    "could not allocate jumbo ring dma tag\n");
2235f41ac2beSBill Paul 			return (ENOMEM);
2236f41ac2beSBill Paul 		}
2237f41ac2beSBill Paul 
22383f74909aSGleb Smirnoff 		/* Allocate DMA'able memory for jumbo RX ring. */
2239f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
22401be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
22411be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
2242f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
2243f41ac2beSBill Paul 		if (error)
2244f41ac2beSBill Paul 			return (ENOMEM);
2245f41ac2beSBill Paul 
22463f74909aSGleb Smirnoff 		/* Load the address of the jumbo RX ring. */
2247f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
2248f41ac2beSBill Paul 		ctx.sc = sc;
2249f41ac2beSBill Paul 
2250f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2251f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2252f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
2253f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2254f41ac2beSBill Paul 
2255f41ac2beSBill Paul 		if (error)
2256f41ac2beSBill Paul 			return (ENOMEM);
2257f41ac2beSBill Paul 
2258f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
2259f41ac2beSBill Paul 
22603f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
2261943787f3SPyun YongHyeon 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2262943787f3SPyun YongHyeon 		    0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
2263943787f3SPyun YongHyeon 		if (error) {
2264943787f3SPyun YongHyeon 			device_printf(sc->bge_dev,
2265943787f3SPyun YongHyeon 			    "can't create sapre DMA map for jumbo RX\n");
2266943787f3SPyun YongHyeon 			return (ENOMEM);
2267943787f3SPyun YongHyeon 		}
2268f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2269f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2270f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2271f41ac2beSBill Paul 			if (error) {
2272fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
22733f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
2274f41ac2beSBill Paul 				return (ENOMEM);
2275f41ac2beSBill Paul 			}
2276f41ac2beSBill Paul 		}
2277f41ac2beSBill Paul 
2278f41ac2beSBill Paul 	}
2279f41ac2beSBill Paul 
22803f74909aSGleb Smirnoff 	/* Create tag for RX return ring. */
2281f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2282f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2283f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
2284f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
2285f41ac2beSBill Paul 
2286f41ac2beSBill Paul 	if (error) {
2287fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2288f41ac2beSBill Paul 		return (ENOMEM);
2289f41ac2beSBill Paul 	}
2290f41ac2beSBill Paul 
22913f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for RX return ring. */
2292f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
2293f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
2294f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
2295f41ac2beSBill Paul 	if (error)
2296f41ac2beSBill Paul 		return (ENOMEM);
2297f41ac2beSBill Paul 
2298f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
2299f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
2300f41ac2beSBill Paul 
23013f74909aSGleb Smirnoff 	/* Load the address of the RX return ring. */
2302f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2303f41ac2beSBill Paul 	ctx.sc = sc;
2304f41ac2beSBill Paul 
2305f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
2306f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
2307f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
2308f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2309f41ac2beSBill Paul 
2310f41ac2beSBill Paul 	if (error)
2311f41ac2beSBill Paul 		return (ENOMEM);
2312f41ac2beSBill Paul 
2313f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
2314f41ac2beSBill Paul 
23153f74909aSGleb Smirnoff 	/* Create tag for TX ring. */
2316f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2317f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2318f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
2319f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
2320f41ac2beSBill Paul 
2321f41ac2beSBill Paul 	if (error) {
2322fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2323f41ac2beSBill Paul 		return (ENOMEM);
2324f41ac2beSBill Paul 	}
2325f41ac2beSBill Paul 
23263f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for TX ring. */
2327f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
2328f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
2329f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
2330f41ac2beSBill Paul 	if (error)
2331f41ac2beSBill Paul 		return (ENOMEM);
2332f41ac2beSBill Paul 
2333f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
2334f41ac2beSBill Paul 
23353f74909aSGleb Smirnoff 	/* Load the address of the TX ring. */
2336f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2337f41ac2beSBill Paul 	ctx.sc = sc;
2338f41ac2beSBill Paul 
2339f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
2340f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
2341f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2342f41ac2beSBill Paul 
2343f41ac2beSBill Paul 	if (error)
2344f41ac2beSBill Paul 		return (ENOMEM);
2345f41ac2beSBill Paul 
2346f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
2347f41ac2beSBill Paul 
23483f74909aSGleb Smirnoff 	/* Create tag for status block. */
2349f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2350f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2351f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
2352f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
2353f41ac2beSBill Paul 
2354f41ac2beSBill Paul 	if (error) {
2355fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2356f41ac2beSBill Paul 		return (ENOMEM);
2357f41ac2beSBill Paul 	}
2358f41ac2beSBill Paul 
23593f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for status block. */
2360f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
2361f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
2362f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
2363f41ac2beSBill Paul 	if (error)
2364f41ac2beSBill Paul 		return (ENOMEM);
2365f41ac2beSBill Paul 
2366f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2367f41ac2beSBill Paul 
23683f74909aSGleb Smirnoff 	/* Load the address of the status block. */
2369f41ac2beSBill Paul 	ctx.sc = sc;
2370f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2371f41ac2beSBill Paul 
2372f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2373f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2374f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2375f41ac2beSBill Paul 
2376f41ac2beSBill Paul 	if (error)
2377f41ac2beSBill Paul 		return (ENOMEM);
2378f41ac2beSBill Paul 
2379f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2380f41ac2beSBill Paul 
23813f74909aSGleb Smirnoff 	/* Create tag for statistics block. */
2382f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2383f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2384f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2385f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
2386f41ac2beSBill Paul 
2387f41ac2beSBill Paul 	if (error) {
2388fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2389f41ac2beSBill Paul 		return (ENOMEM);
2390f41ac2beSBill Paul 	}
2391f41ac2beSBill Paul 
23923f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for statistics block. */
2393f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2394f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2395f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
2396f41ac2beSBill Paul 	if (error)
2397f41ac2beSBill Paul 		return (ENOMEM);
2398f41ac2beSBill Paul 
2399f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2400f41ac2beSBill Paul 
24013f74909aSGleb Smirnoff 	/* Load the address of the statstics block. */
2402f41ac2beSBill Paul 	ctx.sc = sc;
2403f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2404f41ac2beSBill Paul 
2405f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2406f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2407f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2408f41ac2beSBill Paul 
2409f41ac2beSBill Paul 	if (error)
2410f41ac2beSBill Paul 		return (ENOMEM);
2411f41ac2beSBill Paul 
2412f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2413f41ac2beSBill Paul 
2414f41ac2beSBill Paul 	return (0);
2415f41ac2beSBill Paul }
2416f41ac2beSBill Paul 
24170a55a034SJung-uk Kim #if __FreeBSD_version > 602105
2418bf6ef57aSJohn Polstra /*
2419bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
2420bf6ef57aSJohn Polstra  */
2421bf6ef57aSJohn Polstra static int
2422bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
2423bf6ef57aSJohn Polstra {
2424bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
242555aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
2426bf6ef57aSJohn Polstra 
242755aaf894SMarius Strobl 	d = pci_get_domain(dev);
2428bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
2429bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
2430bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
2431bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
243255aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
2433bf6ef57aSJohn Polstra 			return (1);
2434bf6ef57aSJohn Polstra 	return (0);
2435bf6ef57aSJohn Polstra }
2436bf6ef57aSJohn Polstra 
2437bf6ef57aSJohn Polstra /*
2438bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
2439bf6ef57aSJohn Polstra  */
2440bf6ef57aSJohn Polstra static int
2441bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
2442bf6ef57aSJohn Polstra {
2443bf6ef57aSJohn Polstra 	int can_use_msi = 0;
2444bf6ef57aSJohn Polstra 
2445bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
2446a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
2447bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
2448bf6ef57aSJohn Polstra 		/*
2449a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
2450a8376f70SMarius Strobl 		 * configured in single-port mode.
2451bf6ef57aSJohn Polstra 		 */
2452bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
2453bf6ef57aSJohn Polstra 			can_use_msi = 1;
2454bf6ef57aSJohn Polstra 		break;
2455bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
2456bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
2457bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
2458bf6ef57aSJohn Polstra 			can_use_msi = 1;
2459bf6ef57aSJohn Polstra 		break;
2460a8376f70SMarius Strobl 	default:
2461a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
2462bf6ef57aSJohn Polstra 			can_use_msi = 1;
2463bf6ef57aSJohn Polstra 	}
2464bf6ef57aSJohn Polstra 	return (can_use_msi);
2465bf6ef57aSJohn Polstra }
24664e35d186SJung-uk Kim #endif
2467bf6ef57aSJohn Polstra 
246895d67482SBill Paul static int
24693f74909aSGleb Smirnoff bge_attach(device_t dev)
247095d67482SBill Paul {
247195d67482SBill Paul 	struct ifnet *ifp;
247295d67482SBill Paul 	struct bge_softc *sc;
24734f0794ffSBjoern A. Zeeb 	uint32_t hwcfg = 0, misccfg;
247408013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
247508013fd3SMarius Strobl 	int error, reg, rid, trys;
247695d67482SBill Paul 
247795d67482SBill Paul 	sc = device_get_softc(dev);
247895d67482SBill Paul 	sc->bge_dev = dev;
247995d67482SBill Paul 
248095d67482SBill Paul 	/*
248195d67482SBill Paul 	 * Map control/status registers.
248295d67482SBill Paul 	 */
248395d67482SBill Paul 	pci_enable_busmaster(dev);
248495d67482SBill Paul 
248595d67482SBill Paul 	rid = BGE_PCI_BAR0;
24865f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
248744f8f2fcSMarius Strobl 	    RF_ACTIVE);
248895d67482SBill Paul 
248995d67482SBill Paul 	if (sc->bge_res == NULL) {
2490fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
249195d67482SBill Paul 		error = ENXIO;
249295d67482SBill Paul 		goto fail;
249395d67482SBill Paul 	}
249495d67482SBill Paul 
24954f09c4c7SMarius Strobl 	/* Save various chip information. */
2496e53d81eeSPaul Saab 	sc->bge_chipid =
2497a5779553SStanislav Sedov 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2498a5779553SStanislav Sedov 	    BGE_PCIMISCCTL_ASICREV_SHIFT;
2499a5779553SStanislav Sedov 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_USE_PRODID_REG)
2500a5779553SStanislav Sedov 		sc->bge_chipid = pci_read_config(dev, BGE_PCI_PRODID_ASICREV,
2501a5779553SStanislav Sedov 		    4);
2502e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2503e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2504e53d81eeSPaul Saab 
250586543395SJung-uk Kim 	/*
250638cc658fSJohn Baldwin 	 * Don't enable Ethernet@WireSpeed for the 5700, 5906, or the
250786543395SJung-uk Kim 	 * 5705 A0 and A1 chips.
250886543395SJung-uk Kim 	 */
250986543395SJung-uk Kim 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
251038cc658fSJohn Baldwin 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
251186543395SJung-uk Kim 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
251286543395SJung-uk Kim 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)
251386543395SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_WIRESPEED;
251486543395SJung-uk Kim 
25155fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
25165fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
251708013fd3SMarius Strobl 
25180dae9719SJung-uk Kim 	/* Save chipset family. */
25190dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
2520a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5755:
2521a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5761:
2522a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5784:
2523a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5785:
2524a5779553SStanislav Sedov 	case BGE_ASICREV_BCM5787:
2525a5779553SStanislav Sedov 	case BGE_ASICREV_BCM57780:
2526a5779553SStanislav Sedov 		sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
2527a5779553SStanislav Sedov 		    BGE_FLAG_5705_PLUS;
2528a5779553SStanislav Sedov 		break;
25290dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
25300dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
25310dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
25320dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
25337ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
25340dae9719SJung-uk Kim 		break;
25350dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
25360dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
25370dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
25387ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5714_FAMILY /* | BGE_FLAG_JUMBO */;
25399fe569d8SXin LI 		/* FALLTHROUGH */
25400dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
25410dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
254238cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
25430dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
25449fe569d8SXin LI 		/* FALLTHROUGH */
25450dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
25460dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
25470dae9719SJung-uk Kim 		break;
25480dae9719SJung-uk Kim 	}
25490dae9719SJung-uk Kim 
25505ee49a3aSJung-uk Kim 	/* Set various bug flags. */
25511ec4c3a8SJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
25521ec4c3a8SJung-uk Kim 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
25531ec4c3a8SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_CRC_BUG;
25545ee49a3aSJung-uk Kim 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
25555ee49a3aSJung-uk Kim 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
25565ee49a3aSJung-uk Kim 		sc->bge_flags |= BGE_FLAG_ADC_BUG;
25575ee49a3aSJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
25585ee49a3aSJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5704_A0_BUG;
255908bf8bb7SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc) &&
256008bf8bb7SJung-uk Kim 	    !(sc->bge_flags & BGE_FLAG_ADJUST_TRIM)) {
25615ee49a3aSJung-uk Kim 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
2562a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2563a5779553SStanislav Sedov 		    sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
25644fcf220bSJohn Baldwin 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
25654fcf220bSJohn Baldwin 			if (sc->bge_chipid != BGE_CHIPID_BCM5722_A0)
25665ee49a3aSJung-uk Kim 				sc->bge_flags |= BGE_FLAG_JITTER_BUG;
256738cc658fSJohn Baldwin 		} else if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
25685ee49a3aSJung-uk Kim 			sc->bge_flags |= BGE_FLAG_BER_BUG;
25695ee49a3aSJung-uk Kim 	}
25705ee49a3aSJung-uk Kim 
25714f0794ffSBjoern A. Zeeb 
25724f0794ffSBjoern A. Zeeb 	/*
25734f0794ffSBjoern A. Zeeb 	 * We could possibly check for BCOM_DEVICEID_BCM5788 in bge_probe()
25744f0794ffSBjoern A. Zeeb 	 * but I do not know the DEVICEID for the 5788M.
25754f0794ffSBjoern A. Zeeb 	 */
25764f0794ffSBjoern A. Zeeb 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID;
25774f0794ffSBjoern A. Zeeb 	if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
25784f0794ffSBjoern A. Zeeb 	    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
25794f0794ffSBjoern A. Zeeb 		sc->bge_flags |= BGE_FLAG_5788;
25804f0794ffSBjoern A. Zeeb 
2581e53d81eeSPaul Saab   	/*
25826f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
2583e53d81eeSPaul Saab   	 */
2584fe09b799SJung-uk Kim #if __FreeBSD_version > 602101
25856f8718a3SScott Long 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
25864c0da0ffSGleb Smirnoff 		/*
25876f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
25886f8718a3SScott Long 		 * must be a PCI Express device.
25896f8718a3SScott Long 		 */
25904f09c4c7SMarius Strobl 		if (reg != 0) {
25916f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIE;
25926f8718a3SScott Long #else
25935345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc)) {
25946f8718a3SScott Long 		reg = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
25954f09c4c7SMarius Strobl 		if ((reg & 0xFF) == BGE_PCIE_CAPID) {
25966f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIE;
25974f09c4c7SMarius Strobl 			reg = BGE_PCIE_CAPID;
259890447aadSMarius Strobl #endif
25994f09c4c7SMarius Strobl 			bge_set_max_readrq(sc, reg);
26004f09c4c7SMarius Strobl 		}
26016f8718a3SScott Long 	} else {
26026f8718a3SScott Long 		/*
26036f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
26046f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
26054c0da0ffSGleb Smirnoff 		 */
260690447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
26074c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
2608652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
26096f8718a3SScott Long 	}
26104c0da0ffSGleb Smirnoff 
26110a55a034SJung-uk Kim #if __FreeBSD_version > 602105
26124e35d186SJung-uk Kim 	{
26134e35d186SJung-uk Kim 		int msicount;
26144e35d186SJung-uk Kim 
2615bf6ef57aSJohn Polstra 		/*
2616bf6ef57aSJohn Polstra 		 * Allocate the interrupt, using MSI if possible.  These devices
2617bf6ef57aSJohn Polstra 		 * support 8 MSI messages, but only the first one is used in
2618bf6ef57aSJohn Polstra 		 * normal operation.
2619bf6ef57aSJohn Polstra 		 */
2620bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
2621bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
2622bf6ef57aSJohn Polstra 			if (msicount > 1)
2623bf6ef57aSJohn Polstra 				msicount = 1;
2624bf6ef57aSJohn Polstra 		} else
2625bf6ef57aSJohn Polstra 			msicount = 0;
2626bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
2627bf6ef57aSJohn Polstra 			rid = 1;
2628bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
2629bf6ef57aSJohn Polstra 		} else
2630bf6ef57aSJohn Polstra 			rid = 0;
26314e35d186SJung-uk Kim 	}
26324e35d186SJung-uk Kim #else
26334e35d186SJung-uk Kim 	rid = 0;
26344e35d186SJung-uk Kim #endif
2635bf6ef57aSJohn Polstra 
2636bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2637bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
2638bf6ef57aSJohn Polstra 
2639bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
2640bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
2641bf6ef57aSJohn Polstra 		error = ENXIO;
2642bf6ef57aSJohn Polstra 		goto fail;
2643bf6ef57aSJohn Polstra 	}
2644bf6ef57aSJohn Polstra 
26454f09c4c7SMarius Strobl 	if (bootverbose)
26464f09c4c7SMarius Strobl 		device_printf(dev,
26474f09c4c7SMarius Strobl 		    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; %s\n",
26484f09c4c7SMarius Strobl 		    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev,
26494f09c4c7SMarius Strobl 		    (sc->bge_flags & BGE_FLAG_PCIX) ? "PCI-X" :
26504f09c4c7SMarius Strobl 		    ((sc->bge_flags & BGE_FLAG_PCIE) ? "PCI-E" : "PCI"));
26514f09c4c7SMarius Strobl 
2652bf6ef57aSJohn Polstra 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
2653bf6ef57aSJohn Polstra 
265495d67482SBill Paul 	/* Try to reset the chip. */
26558cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
26568cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
26578cb1383cSDoug Ambrisko 		error = ENXIO;
26588cb1383cSDoug Ambrisko 		goto fail;
26598cb1383cSDoug Ambrisko 	}
26608cb1383cSDoug Ambrisko 
26618cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
2662f1a7e6d5SScott Long 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG)
2663f1a7e6d5SScott Long 	    == BGE_MAGIC_NUMBER)) {
26648cb1383cSDoug Ambrisko 		if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG)
26658cb1383cSDoug Ambrisko 		    & BGE_HWCFG_ASF) {
26668cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_ENABLE;
26678cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_STACKUP;
26688cb1383cSDoug Ambrisko 			if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
26698cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
26708cb1383cSDoug Ambrisko 			}
26718cb1383cSDoug Ambrisko 		}
26728cb1383cSDoug Ambrisko 	}
26738cb1383cSDoug Ambrisko 
26748cb1383cSDoug Ambrisko 	/* Try to reset the chip again the nice way. */
26758cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
26768cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
26778cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
26788cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
26798cb1383cSDoug Ambrisko 		error = ENXIO;
26808cb1383cSDoug Ambrisko 		goto fail;
26818cb1383cSDoug Ambrisko 	}
26828cb1383cSDoug Ambrisko 
26838cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
26848cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
268595d67482SBill Paul 
268695d67482SBill Paul 	if (bge_chipinit(sc)) {
2687fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
268895d67482SBill Paul 		error = ENXIO;
268995d67482SBill Paul 		goto fail;
269095d67482SBill Paul 	}
269195d67482SBill Paul 
269238cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
269338cc658fSJohn Baldwin 	if (error) {
269408013fd3SMarius Strobl 		device_printf(sc->bge_dev,
269508013fd3SMarius Strobl 		    "failed to read station address\n");
269695d67482SBill Paul 		error = ENXIO;
269795d67482SBill Paul 		goto fail;
269895d67482SBill Paul 	}
269995d67482SBill Paul 
2700f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
27017ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
2702f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2703f41ac2beSBill Paul 	else
2704f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2705f41ac2beSBill Paul 
2706f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2707fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2708fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2709f41ac2beSBill Paul 		error = ENXIO;
2710f41ac2beSBill Paul 		goto fail;
2711f41ac2beSBill Paul 	}
2712f41ac2beSBill Paul 
271395d67482SBill Paul 	/* Set default tuneable values. */
271495d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
271595d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
271695d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
27176f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
27186f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
271995d67482SBill Paul 
272095d67482SBill Paul 	/* Set up ifnet structure */
2721fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2722fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2723fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2724fc74a9f9SBrooks Davis 		error = ENXIO;
2725fc74a9f9SBrooks Davis 		goto fail;
2726fc74a9f9SBrooks Davis 	}
272795d67482SBill Paul 	ifp->if_softc = sc;
27289bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
272995d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
273095d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
273195d67482SBill Paul 	ifp->if_start = bge_start;
273295d67482SBill Paul 	ifp->if_init = bge_init;
27334d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
27344d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
27354d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
273695d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2737d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
27384e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
27394e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
27404e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
27414e35d186SJung-uk Kim #endif
274295d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
274375719184SGleb Smirnoff #ifdef DEVICE_POLLING
274475719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
274575719184SGleb Smirnoff #endif
274695d67482SBill Paul 
2747a1d52896SBill Paul 	/*
2748d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2749d375e524SGleb Smirnoff 	 * to hardware bugs.
2750d375e524SGleb Smirnoff 	 */
2751d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2752d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
27534d3a629cSPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_HWCSUM;
2754d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2755d375e524SGleb Smirnoff 	}
2756d375e524SGleb Smirnoff 
2757d375e524SGleb Smirnoff 	/*
2758a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
275941abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
276041abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
276141abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
276241abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
276341abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
276441abcc1bSPaul Saab 	 * SK-9D41.
2765a1d52896SBill Paul 	 */
276641abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
276741abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
27685fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
27695fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
2770f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2771f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2772fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2773f6789fbaSPyun YongHyeon 			error = ENXIO;
2774f6789fbaSPyun YongHyeon 			goto fail;
2775f6789fbaSPyun YongHyeon 		}
277641abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
277741abcc1bSPaul Saab 	}
277841abcc1bSPaul Saab 
277941abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2780652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
2781a1d52896SBill Paul 
278295d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
27830c8aa4eaSJung-uk Kim 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
2784652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
278595d67482SBill Paul 
2786652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
27870c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
27880c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
27890c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
27906098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
27916098821cSJung-uk Kim 		    0, NULL);
279295d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
279395d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
2794da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
279595d67482SBill Paul 	} else {
279695d67482SBill Paul 		/*
27978cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
27988cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
27998cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
28008cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
28018cb1383cSDoug Ambrisko 		 * the PHY.
280295d67482SBill Paul 		 */
28034012d104SMarius Strobl 		trys = 0;
28048cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
28058cb1383cSDoug Ambrisko again:
28068cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
28078cb1383cSDoug Ambrisko 
280895d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
280995d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
28108cb1383cSDoug Ambrisko 			if (trys++ < 4) {
28118cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
28124e35d186SJung-uk Kim 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR,
28134e35d186SJung-uk Kim 				    BMCR_RESET);
28148cb1383cSDoug Ambrisko 				goto again;
28158cb1383cSDoug Ambrisko 			}
28168cb1383cSDoug Ambrisko 
2817fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
281895d67482SBill Paul 			error = ENXIO;
281995d67482SBill Paul 			goto fail;
282095d67482SBill Paul 		}
28218cb1383cSDoug Ambrisko 
28228cb1383cSDoug Ambrisko 		/*
28238cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
28248cb1383cSDoug Ambrisko 		 */
28258cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
28268cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
282795d67482SBill Paul 	}
282895d67482SBill Paul 
282995d67482SBill Paul 	/*
2830e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2831e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2832e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2833e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2834e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2835e255b776SJohn Polstra 	 * payloads by copying the received packets.
2836e255b776SJohn Polstra 	 */
2837652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
2838652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
2839652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
2840e255b776SJohn Polstra 
2841e255b776SJohn Polstra 	/*
284295d67482SBill Paul 	 * Call MI attach routine.
284395d67482SBill Paul 	 */
2844fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
2845b74e67fbSGleb Smirnoff 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
28460f9bd73bSSam Leffler 
284761ccb9daSPyun YongHyeon 	/* Tell upper layer we support long frames. */
284861ccb9daSPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
284961ccb9daSPyun YongHyeon 
28500f9bd73bSSam Leffler 	/*
28510f9bd73bSSam Leffler 	 * Hookup IRQ last.
28520f9bd73bSSam Leffler 	 */
28534e35d186SJung-uk Kim #if __FreeBSD_version > 700030
28540f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
2855ef544f63SPaolo Pisati 	   NULL, bge_intr, sc, &sc->bge_intrhand);
28564e35d186SJung-uk Kim #else
28574e35d186SJung-uk Kim 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
28584e35d186SJung-uk Kim 	   bge_intr, sc, &sc->bge_intrhand);
28594e35d186SJung-uk Kim #endif
28600f9bd73bSSam Leffler 
28610f9bd73bSSam Leffler 	if (error) {
2862fc74a9f9SBrooks Davis 		bge_detach(dev);
2863fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
28640f9bd73bSSam Leffler 	}
286595d67482SBill Paul 
28666f8718a3SScott Long 	bge_add_sysctls(sc);
28676f8718a3SScott Long 
286808013fd3SMarius Strobl 	return (0);
286908013fd3SMarius Strobl 
287095d67482SBill Paul fail:
287108013fd3SMarius Strobl 	bge_release_resources(sc);
287208013fd3SMarius Strobl 
287395d67482SBill Paul 	return (error);
287495d67482SBill Paul }
287595d67482SBill Paul 
287695d67482SBill Paul static int
28773f74909aSGleb Smirnoff bge_detach(device_t dev)
287895d67482SBill Paul {
287995d67482SBill Paul 	struct bge_softc *sc;
288095d67482SBill Paul 	struct ifnet *ifp;
288195d67482SBill Paul 
288295d67482SBill Paul 	sc = device_get_softc(dev);
2883fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
288495d67482SBill Paul 
288575719184SGleb Smirnoff #ifdef DEVICE_POLLING
288675719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
288775719184SGleb Smirnoff 		ether_poll_deregister(ifp);
288875719184SGleb Smirnoff #endif
288975719184SGleb Smirnoff 
28900f9bd73bSSam Leffler 	BGE_LOCK(sc);
289195d67482SBill Paul 	bge_stop(sc);
289295d67482SBill Paul 	bge_reset(sc);
28930f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
28940f9bd73bSSam Leffler 
28955dda8085SOleg Bulyzhin 	callout_drain(&sc->bge_stat_ch);
28965dda8085SOleg Bulyzhin 
28970f9bd73bSSam Leffler 	ether_ifdetach(ifp);
289895d67482SBill Paul 
2899652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
290095d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
290195d67482SBill Paul 	} else {
290295d67482SBill Paul 		bus_generic_detach(dev);
290395d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
290495d67482SBill Paul 	}
290595d67482SBill Paul 
290695d67482SBill Paul 	bge_release_resources(sc);
290795d67482SBill Paul 
290895d67482SBill Paul 	return (0);
290995d67482SBill Paul }
291095d67482SBill Paul 
291195d67482SBill Paul static void
29123f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
291395d67482SBill Paul {
291495d67482SBill Paul 	device_t dev;
291595d67482SBill Paul 
291695d67482SBill Paul 	dev = sc->bge_dev;
291795d67482SBill Paul 
291895d67482SBill Paul 	if (sc->bge_intrhand != NULL)
291995d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
292095d67482SBill Paul 
292195d67482SBill Paul 	if (sc->bge_irq != NULL)
2922724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
2923724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
2924724bd939SJohn Polstra 
29250a55a034SJung-uk Kim #if __FreeBSD_version > 602105
2926724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
2927724bd939SJohn Polstra 		pci_release_msi(dev);
29284e35d186SJung-uk Kim #endif
292995d67482SBill Paul 
293095d67482SBill Paul 	if (sc->bge_res != NULL)
293195d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
293295d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
293395d67482SBill Paul 
2934ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2935ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2936ad61f896SRuslan Ermilov 
2937f41ac2beSBill Paul 	bge_dma_free(sc);
293895d67482SBill Paul 
29390f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
29400f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
294195d67482SBill Paul }
294295d67482SBill Paul 
29438cb1383cSDoug Ambrisko static int
29443f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
294595d67482SBill Paul {
294695d67482SBill Paul 	device_t dev;
29475fea260fSMarius Strobl 	uint32_t cachesize, command, pcistate, reset, val;
29486f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
29495fea260fSMarius Strobl 	int i;
295095d67482SBill Paul 
295195d67482SBill Paul 	dev = sc->bge_dev;
295295d67482SBill Paul 
295338cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
295438cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
29556f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
29566f8718a3SScott Long 			write_op = bge_writemem_direct;
29576f8718a3SScott Long 		else
29586f8718a3SScott Long 			write_op = bge_writemem_ind;
29599ba784dbSScott Long 	} else
29606f8718a3SScott Long 		write_op = bge_writereg_ind;
29616f8718a3SScott Long 
296295d67482SBill Paul 	/* Save some important PCI state. */
296395d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
296495d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
296595d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
296695d67482SBill Paul 
296795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
296895d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
2969e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
297095d67482SBill Paul 
29716f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
29726f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
2973a5779553SStanislav Sedov 	    BGE_IS_5755_PLUS(sc)) {
29746f8718a3SScott Long 		if (bootverbose)
29759ba784dbSScott Long 			device_printf(sc->bge_dev, "Disabling fastboot\n");
29766f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
29776f8718a3SScott Long 	}
29786f8718a3SScott Long 
29796f8718a3SScott Long 	/*
29806f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
29816f8718a3SScott Long 	 * When firmware finishes its initialization it will
29826f8718a3SScott Long 	 * write ~BGE_MAGIC_NUMBER to the same location.
29836f8718a3SScott Long 	 */
29846f8718a3SScott Long 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
29856f8718a3SScott Long 
29860c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
2987e53d81eeSPaul Saab 
2988e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2989652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
29900c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
29910c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, 0x7E2C, 0x20);
2992e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2993e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
29940c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
29950c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
2996e53d81eeSPaul Saab 		}
2997e53d81eeSPaul Saab 	}
2998e53d81eeSPaul Saab 
299921c9e407SDavid Christensen 	/*
30006f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
30016f8718a3SScott Long 	 * powered up in D0 uninitialized.
30026f8718a3SScott Long 	 */
30035345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
30046f8718a3SScott Long 		reset |= 0x04000000;
30056f8718a3SScott Long 
300695d67482SBill Paul 	/* Issue global reset */
30076f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
300895d67482SBill Paul 
300938cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
30105fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
301138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
30125fea260fSMarius Strobl 		    val | BGE_VCPU_STATUS_DRV_RESET);
30135fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
301438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
30155fea260fSMarius Strobl 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
301638cc658fSJohn Baldwin 	}
301738cc658fSJohn Baldwin 
301895d67482SBill Paul 	DELAY(1000);
301995d67482SBill Paul 
3020e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3021652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
3022e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
3023e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
30245fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
30255fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
3026e53d81eeSPaul Saab 		}
30279ba784dbSScott Long 		/*
30289ba784dbSScott Long 		 * Set PCIE max payload size to 128 bytes and clear error
30299ba784dbSScott Long 		 * status.
30309ba784dbSScott Long 		 */
30310c8aa4eaSJung-uk Kim 		pci_write_config(dev, 0xD8, 0xF5000, 4);
3032e53d81eeSPaul Saab 	}
3033e53d81eeSPaul Saab 
30343f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
303595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
303695d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3037e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
303895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
303995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
30400c8aa4eaSJung-uk Kim 	write_op(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
304195d67482SBill Paul 
3042bf6ef57aSJohn Polstra 	/* Re-enable MSI, if neccesary, and enable the memory arbiter. */
30434c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
3044bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
3045bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
3046bf6ef57aSJohn Polstra 			val = pci_read_config(dev, BGE_PCI_MSI_CTL, 2);
3047bf6ef57aSJohn Polstra 			pci_write_config(dev, BGE_PCI_MSI_CTL,
3048bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
3049bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
3050bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
3051bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
3052bf6ef57aSJohn Polstra 		}
30534c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
30544c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
30554c0da0ffSGleb Smirnoff 	} else
3056a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
3057a7b0c314SPaul Saab 
305838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
305938cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
306038cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
306138cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
306238cc658fSJohn Baldwin 				break;
306338cc658fSJohn Baldwin 			DELAY(100);
306438cc658fSJohn Baldwin 		}
306538cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
306638cc658fSJohn Baldwin 			device_printf(sc->bge_dev, "reset timed out\n");
306738cc658fSJohn Baldwin 			return (1);
306838cc658fSJohn Baldwin 		}
306938cc658fSJohn Baldwin 	} else {
307095d67482SBill Paul 		/*
30716f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
307208013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
30735fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
30745fea260fSMarius Strobl 		 * address is fitted though.
307595d67482SBill Paul 		 */
307695d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
3077d5d23857SJung-uk Kim 			DELAY(10);
307895d67482SBill Paul 			val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
307995d67482SBill Paul 			if (val == ~BGE_MAGIC_NUMBER)
308095d67482SBill Paul 				break;
308195d67482SBill Paul 		}
308295d67482SBill Paul 
30835fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
30849ba784dbSScott Long 			device_printf(sc->bge_dev, "firmware handshake timed out, "
30859ba784dbSScott Long 			    "found 0x%08x\n", val);
308638cc658fSJohn Baldwin 	}
308795d67482SBill Paul 
308895d67482SBill Paul 	/*
308995d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
309095d67482SBill Paul 	 * return to its original pre-reset state. This is a
309195d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
309295d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
309395d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
309495d67482SBill Paul 	 * results.
309595d67482SBill Paul 	 */
309695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
309795d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
309895d67482SBill Paul 			break;
309995d67482SBill Paul 		DELAY(10);
310095d67482SBill Paul 	}
310195d67482SBill Paul 
31026f8718a3SScott Long 	if (sc->bge_flags & BGE_FLAG_PCIE) {
31030c8aa4eaSJung-uk Kim 		reset = bge_readmem_ind(sc, 0x7C00);
31040c8aa4eaSJung-uk Kim 		bge_writemem_ind(sc, 0x7C00, reset | (1 << 25));
31056f8718a3SScott Long 	}
31066f8718a3SScott Long 
31073f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
3108e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
310995d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
311095d67482SBill Paul 
31118cb1383cSDoug Ambrisko 	/* Tell the ASF firmware we are up */
31128cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
31138cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
31148cb1383cSDoug Ambrisko 
311595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
311695d67482SBill Paul 
3117da3003f0SBill Paul 	/*
3118da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
3119da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
3120da3003f0SBill Paul 	 * to 1.2V.
3121da3003f0SBill Paul 	 */
3122652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
3123652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
31245fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
31255fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
31265fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
3127da3003f0SBill Paul 	}
3128da3003f0SBill Paul 
3129e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3130652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
3131652ae483SGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
31325fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
31335fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
3134e53d81eeSPaul Saab 	}
313595d67482SBill Paul 	DELAY(10000);
31368cb1383cSDoug Ambrisko 
31378cb1383cSDoug Ambrisko 	return(0);
313895d67482SBill Paul }
313995d67482SBill Paul 
314095d67482SBill Paul /*
314195d67482SBill Paul  * Frame reception handling. This is called if there's a frame
314295d67482SBill Paul  * on the receive return list.
314395d67482SBill Paul  *
314495d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
31451be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
314695d67482SBill Paul  * 2) the frame is from the standard receive ring
314795d67482SBill Paul  */
314895d67482SBill Paul 
31491abcdbd1SAttilio Rao static int
31503f74909aSGleb Smirnoff bge_rxeof(struct bge_softc *sc)
315195d67482SBill Paul {
315295d67482SBill Paul 	struct ifnet *ifp;
31531abcdbd1SAttilio Rao 	int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
31547f21e273SStanislav Sedov 	uint16_t rx_prod, rx_cons;
315595d67482SBill Paul 
31560f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
31577f21e273SStanislav Sedov 	rx_cons = sc->bge_rx_saved_considx;
31587f21e273SStanislav Sedov 	rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
31590f9bd73bSSam Leffler 
31603f74909aSGleb Smirnoff 	/* Nothing to do. */
31617f21e273SStanislav Sedov 	if (rx_cons == rx_prod)
31621abcdbd1SAttilio Rao 		return (rx_npkts);
3163cfcb5025SOleg Bulyzhin 
3164fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
316595d67482SBill Paul 
3166f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
3167e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
3168f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
316915eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
3170c215fd77SPyun YongHyeon 	if (ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
3171c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN))
3172f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
317315eda801SStanislav Sedov 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
3174f41ac2beSBill Paul 
31757f21e273SStanislav Sedov 	while (rx_cons != rx_prod) {
317695d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
31773f74909aSGleb Smirnoff 		uint32_t		rxidx;
317895d67482SBill Paul 		struct mbuf		*m = NULL;
31793f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
318095d67482SBill Paul 		int			have_tag = 0;
318195d67482SBill Paul 
318275719184SGleb Smirnoff #ifdef DEVICE_POLLING
318375719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
318475719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
318575719184SGleb Smirnoff 				break;
318675719184SGleb Smirnoff 			sc->rxcycles--;
318775719184SGleb Smirnoff 		}
318875719184SGleb Smirnoff #endif
318975719184SGleb Smirnoff 
31907f21e273SStanislav Sedov 		cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
319195d67482SBill Paul 
319295d67482SBill Paul 		rxidx = cur_rx->bge_idx;
31937f21e273SStanislav Sedov 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
319495d67482SBill Paul 
3195cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
3196cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
319795d67482SBill Paul 			have_tag = 1;
319895d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
319995d67482SBill Paul 		}
320095d67482SBill Paul 
320195d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
320295d67482SBill Paul 			jumbocnt++;
3203943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
320495d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3205943787f3SPyun YongHyeon 				BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
320695d67482SBill Paul 				continue;
320795d67482SBill Paul 			}
3208943787f3SPyun YongHyeon 			if (bge_newbuf_jumbo(sc, rxidx) != 0) {
3209943787f3SPyun YongHyeon 				BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
3210943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
321195d67482SBill Paul 				continue;
321295d67482SBill Paul 			}
321303e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
321495d67482SBill Paul 		} else {
321595d67482SBill Paul 			stdcnt++;
321695d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3217943787f3SPyun YongHyeon 				BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
321895d67482SBill Paul 				continue;
321995d67482SBill Paul 			}
3220943787f3SPyun YongHyeon 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
3221943787f3SPyun YongHyeon 			if (bge_newbuf_std(sc, rxidx) != 0) {
3222943787f3SPyun YongHyeon 				BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
3223943787f3SPyun YongHyeon 				ifp->if_iqdrops++;
322495d67482SBill Paul 				continue;
322595d67482SBill Paul 			}
322603e78bd0SPyun YongHyeon 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
322795d67482SBill Paul 		}
322895d67482SBill Paul 
322995d67482SBill Paul 		ifp->if_ipackets++;
3230e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
3231e255b776SJohn Polstra 		/*
3232e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
3233e65bed95SPyun YongHyeon 		 * the payload is aligned.
3234e255b776SJohn Polstra 		 */
3235652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
3236e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
3237e255b776SJohn Polstra 			    cur_rx->bge_len);
3238e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
3239e255b776SJohn Polstra 		}
3240e255b776SJohn Polstra #endif
3241473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
324295d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
324395d67482SBill Paul 
3244b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
324578178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
324695d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
32470c8aa4eaSJung-uk Kim 				if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
32480c8aa4eaSJung-uk Kim 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
324978178cd1SGleb Smirnoff 			}
3250d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
3251d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
325295d67482SBill Paul 				m->m_pkthdr.csum_data =
325395d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
3254ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
3255ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
325695d67482SBill Paul 			}
325795d67482SBill Paul 		}
325895d67482SBill Paul 
325995d67482SBill Paul 		/*
3260673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
3261673d9191SSam Leffler 		 * attach that information to the packet.
326295d67482SBill Paul 		 */
3263d147662cSGleb Smirnoff 		if (have_tag) {
32644e35d186SJung-uk Kim #if __FreeBSD_version > 700022
326578ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
326678ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
32674e35d186SJung-uk Kim #else
32684e35d186SJung-uk Kim 			VLAN_INPUT_TAG_NEW(ifp, m, vlan_tag);
32694e35d186SJung-uk Kim 			if (m == NULL)
32704e35d186SJung-uk Kim 				continue;
32714e35d186SJung-uk Kim #endif
3272d147662cSGleb Smirnoff 		}
327395d67482SBill Paul 
32740f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
3275673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
32760f9bd73bSSam Leffler 		BGE_LOCK(sc);
3277d4da719cSAttilio Rao 		rx_npkts++;
327825e13e68SXin LI 
327925e13e68SXin LI 		if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
32808cf7d13dSAttilio Rao 			return (rx_npkts);
328195d67482SBill Paul 	}
328295d67482SBill Paul 
328315eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
328415eda801SStanislav Sedov 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
3285e65bed95SPyun YongHyeon 	if (stdcnt > 0)
3286f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
3287e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
32884c0da0ffSGleb Smirnoff 
3289c215fd77SPyun YongHyeon 	if (jumbocnt > 0)
3290f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
32914c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
3292f41ac2beSBill Paul 
32937f21e273SStanislav Sedov 	sc->bge_rx_saved_considx = rx_cons;
329438cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
329595d67482SBill Paul 	if (stdcnt)
329638cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
329795d67482SBill Paul 	if (jumbocnt)
329838cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
3299f5a034f9SPyun YongHyeon #ifdef notyet
3300f5a034f9SPyun YongHyeon 	/*
3301f5a034f9SPyun YongHyeon 	 * This register wraps very quickly under heavy packet drops.
3302f5a034f9SPyun YongHyeon 	 * If you need correct statistics, you can enable this check.
3303f5a034f9SPyun YongHyeon 	 */
3304f5a034f9SPyun YongHyeon 	if (BGE_IS_5705_PLUS(sc))
3305f5a034f9SPyun YongHyeon 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
3306f5a034f9SPyun YongHyeon #endif
33071abcdbd1SAttilio Rao 	return (rx_npkts);
330895d67482SBill Paul }
330995d67482SBill Paul 
331095d67482SBill Paul static void
33113f74909aSGleb Smirnoff bge_txeof(struct bge_softc *sc)
331295d67482SBill Paul {
331395d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
331495d67482SBill Paul 	struct ifnet *ifp;
331595d67482SBill Paul 
33160f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
33170f9bd73bSSam Leffler 
33183f74909aSGleb Smirnoff 	/* Nothing to do. */
3319cfcb5025SOleg Bulyzhin 	if (sc->bge_tx_saved_considx ==
3320cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx)
3321cfcb5025SOleg Bulyzhin 		return;
3322cfcb5025SOleg Bulyzhin 
3323fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
332495d67482SBill Paul 
3325e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
3326e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
3327e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
332895d67482SBill Paul 	/*
332995d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
333095d67482SBill Paul 	 * frames that have been sent.
333195d67482SBill Paul 	 */
333295d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
3333f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
33343f74909aSGleb Smirnoff 		uint32_t		idx = 0;
333595d67482SBill Paul 
333695d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
3337f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
333895d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
333995d67482SBill Paul 			ifp->if_opackets++;
334095d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
33410ac56796SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
3342e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
3343e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
33440ac56796SPyun YongHyeon 			bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
3345f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
3346e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
3347e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
334895d67482SBill Paul 		}
334995d67482SBill Paul 		sc->bge_txcnt--;
335095d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
335195d67482SBill Paul 	}
335295d67482SBill Paul 
335395d67482SBill Paul 	if (cur_tx != NULL)
335413f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
33555b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
33565b01e77cSBruce Evans 		sc->bge_timer = 0;
335795d67482SBill Paul }
335895d67482SBill Paul 
335975719184SGleb Smirnoff #ifdef DEVICE_POLLING
33601abcdbd1SAttilio Rao static int
336175719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
336275719184SGleb Smirnoff {
336375719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
3364366454f2SOleg Bulyzhin 	uint32_t statusword;
33651abcdbd1SAttilio Rao 	int rx_npkts = 0;
336675719184SGleb Smirnoff 
33673f74909aSGleb Smirnoff 	BGE_LOCK(sc);
33683f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
33693f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
33701abcdbd1SAttilio Rao 		return (rx_npkts);
33713f74909aSGleb Smirnoff 	}
337275719184SGleb Smirnoff 
3373dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3374e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
3375dab5cd05SOleg Bulyzhin 
33763f74909aSGleb Smirnoff 	statusword = atomic_readandclear_32(
33773f74909aSGleb Smirnoff 	    &sc->bge_ldata.bge_status_block->bge_status);
3378dab5cd05SOleg Bulyzhin 
3379dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3380e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
3381366454f2SOleg Bulyzhin 
33820c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
3383366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
3384366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
3385366454f2SOleg Bulyzhin 
3386366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
3387366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
33884c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3389652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
3390366454f2SOleg Bulyzhin 			bge_link_upd(sc);
3391366454f2SOleg Bulyzhin 
3392366454f2SOleg Bulyzhin 	sc->rxcycles = count;
33931abcdbd1SAttilio Rao 	rx_npkts = bge_rxeof(sc);
339425e13e68SXin LI 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
339525e13e68SXin LI 		BGE_UNLOCK(sc);
33968cf7d13dSAttilio Rao 		return (rx_npkts);
339725e13e68SXin LI 	}
3398366454f2SOleg Bulyzhin 	bge_txeof(sc);
3399366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3400366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
34013f74909aSGleb Smirnoff 
34023f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
34031abcdbd1SAttilio Rao 	return (rx_npkts);
340475719184SGleb Smirnoff }
340575719184SGleb Smirnoff #endif /* DEVICE_POLLING */
340675719184SGleb Smirnoff 
340795d67482SBill Paul static void
34083f74909aSGleb Smirnoff bge_intr(void *xsc)
340995d67482SBill Paul {
341095d67482SBill Paul 	struct bge_softc *sc;
341195d67482SBill Paul 	struct ifnet *ifp;
3412dab5cd05SOleg Bulyzhin 	uint32_t statusword;
341395d67482SBill Paul 
341495d67482SBill Paul 	sc = xsc;
3415f41ac2beSBill Paul 
34160f9bd73bSSam Leffler 	BGE_LOCK(sc);
34170f9bd73bSSam Leffler 
3418dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
3419dab5cd05SOleg Bulyzhin 
342075719184SGleb Smirnoff #ifdef DEVICE_POLLING
342175719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
342275719184SGleb Smirnoff 		BGE_UNLOCK(sc);
342375719184SGleb Smirnoff 		return;
342475719184SGleb Smirnoff 	}
342575719184SGleb Smirnoff #endif
342675719184SGleb Smirnoff 
3427f30cbfc6SScott Long 	/*
3428b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
3429b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
3430b848e032SBruce Evans 	 * our current organization this just gives complications and
3431b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
3432b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
3433b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
3434b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
3435b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
3436b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
3437b848e032SBruce Evans 	 *
3438b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
3439b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
3440b848e032SBruce Evans 	 * changing later because it is more efficient to get another
3441b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
3442b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
3443b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
3444b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
3445b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
3446b848e032SBruce Evans 	 */
344738cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
3448b848e032SBruce Evans 
3449b848e032SBruce Evans 	/*
3450f30cbfc6SScott Long 	 * Do the mandatory PCI flush as well as get the link status.
3451f30cbfc6SScott Long 	 */
3452f30cbfc6SScott Long 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
3453f41ac2beSBill Paul 
3454f30cbfc6SScott Long 	/* Make sure the descriptor ring indexes are coherent. */
3455f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3456f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
3457f30cbfc6SScott Long 
34581f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
34594c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3460f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
3461dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
346295d67482SBill Paul 
346313f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
34643f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
346595d67482SBill Paul 		bge_rxeof(sc);
346625e13e68SXin LI 	}
346795d67482SBill Paul 
346825e13e68SXin LI 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
34693f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
347095d67482SBill Paul 		bge_txeof(sc);
347195d67482SBill Paul 	}
347295d67482SBill Paul 
347313f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
347413f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
34750f9bd73bSSam Leffler 		bge_start_locked(ifp);
34760f9bd73bSSam Leffler 
347715eda801SStanislav Sedov 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
347815eda801SStanislav Sedov 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
347915eda801SStanislav Sedov 
34800f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
348195d67482SBill Paul }
348295d67482SBill Paul 
348395d67482SBill Paul static void
34848cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
34858cb1383cSDoug Ambrisko {
34868cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
34878cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
34888cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
34898cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
34908cb1383cSDoug Ambrisko 		else {
34918cb1383cSDoug Ambrisko 			sc->bge_asf_count = 5;
34928cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW,
34938cb1383cSDoug Ambrisko 			    BGE_FW_DRV_ALIVE);
34948cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_LEN, 4);
34958cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_DATA, 3);
34968cb1383cSDoug Ambrisko 			CSR_WRITE_4(sc, BGE_CPU_EVENT,
349739153c5aSJung-uk Kim 			    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
34988cb1383cSDoug Ambrisko 		}
34998cb1383cSDoug Ambrisko 	}
35008cb1383cSDoug Ambrisko }
35018cb1383cSDoug Ambrisko 
35028cb1383cSDoug Ambrisko static void
3503b74e67fbSGleb Smirnoff bge_tick(void *xsc)
35040f9bd73bSSam Leffler {
3505b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
350695d67482SBill Paul 	struct mii_data *mii = NULL;
350795d67482SBill Paul 
35080f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
350995d67482SBill Paul 
35105dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
35115dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
35125dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
35135dda8085SOleg Bulyzhin 	    	return;
35145dda8085SOleg Bulyzhin 
35157ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
35160434d1b8SBill Paul 		bge_stats_update_regs(sc);
35170434d1b8SBill Paul 	else
351895d67482SBill Paul 		bge_stats_update(sc);
351995d67482SBill Paul 
3520652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
352195d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
352282b67c01SOleg Bulyzhin 		/*
352382b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
352482b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
352582b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
352682b67c01SOleg Bulyzhin 		 */
352782b67c01SOleg Bulyzhin 		if (!sc->bge_link)
352895d67482SBill Paul 			mii_tick(mii);
35297b97099dSOleg Bulyzhin 	} else {
35307b97099dSOleg Bulyzhin 		/*
35317b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
35327b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
35337b97099dSOleg Bulyzhin 		 * and trigger interrupt.
35347b97099dSOleg Bulyzhin 		 */
35357b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
35363f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
35377b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
35387b97099dSOleg Bulyzhin #endif
35397b97099dSOleg Bulyzhin 		{
35407b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
35414f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
35424f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
35437b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
35444f0794ffSBjoern A. Zeeb 		else
35454f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
35467b97099dSOleg Bulyzhin 		}
3547dab5cd05SOleg Bulyzhin 	}
354895d67482SBill Paul 
35498cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
3550b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
35518cb1383cSDoug Ambrisko 
3552dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
355395d67482SBill Paul }
355495d67482SBill Paul 
355595d67482SBill Paul static void
35563f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
35570434d1b8SBill Paul {
35583f74909aSGleb Smirnoff 	struct ifnet *ifp;
35590434d1b8SBill Paul 
3560fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
35610434d1b8SBill Paul 
35626b037352SJung-uk Kim 	ifp->if_collisions += CSR_READ_4(sc, BGE_MAC_STATS +
35637e6e2507SJung-uk Kim 	    offsetof(struct bge_mac_stats_regs, etherStatsCollisions));
35647e6e2507SJung-uk Kim 
3565e238d4eaSPyun YongHyeon 	ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
35666b037352SJung-uk Kim 	ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
3567e238d4eaSPyun YongHyeon 	ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
35680434d1b8SBill Paul }
35690434d1b8SBill Paul 
35700434d1b8SBill Paul static void
35713f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
357295d67482SBill Paul {
357395d67482SBill Paul 	struct ifnet *ifp;
3574e907febfSPyun YongHyeon 	bus_size_t stats;
35757e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
357695d67482SBill Paul 
3577fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
357895d67482SBill Paul 
3579e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
3580e907febfSPyun YongHyeon 
3581e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
3582e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
358395d67482SBill Paul 
35848634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
35856b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
35866fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
35876fb34dd2SOleg Bulyzhin 
35886fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
35896b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
35906fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
35916fb34dd2SOleg Bulyzhin 
35926fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
35936b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
35946fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
359595d67482SBill Paul 
3596e907febfSPyun YongHyeon #undef	READ_STAT
359795d67482SBill Paul }
359895d67482SBill Paul 
359995d67482SBill Paul /*
3600d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
3601d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
3602d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
3603d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
3604d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
3605d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
3606d375e524SGleb Smirnoff  */
3607d375e524SGleb Smirnoff static __inline int
3608d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
3609d375e524SGleb Smirnoff {
3610d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
3611d375e524SGleb Smirnoff 	struct mbuf *last;
3612d375e524SGleb Smirnoff 
3613d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
3614d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
3615d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
3616d375e524SGleb Smirnoff 		last = m;
3617d375e524SGleb Smirnoff 	} else {
3618d375e524SGleb Smirnoff 		/*
3619d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
3620d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
3621d375e524SGleb Smirnoff 		 */
3622d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
3623d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
3624d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
3625d375e524SGleb Smirnoff 			struct mbuf *n;
3626d375e524SGleb Smirnoff 
3627d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
3628d375e524SGleb Smirnoff 			if (n == NULL)
3629d375e524SGleb Smirnoff 				return (ENOBUFS);
3630d375e524SGleb Smirnoff 			n->m_len = 0;
3631d375e524SGleb Smirnoff 			last->m_next = n;
3632d375e524SGleb Smirnoff 			last = n;
3633d375e524SGleb Smirnoff 		}
3634d375e524SGleb Smirnoff 	}
3635d375e524SGleb Smirnoff 
3636d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
3637d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
3638d375e524SGleb Smirnoff 	last->m_len += padlen;
3639d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
3640d375e524SGleb Smirnoff 
3641d375e524SGleb Smirnoff 	return (0);
3642d375e524SGleb Smirnoff }
3643d375e524SGleb Smirnoff 
3644d375e524SGleb Smirnoff /*
364595d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
364695d67482SBill Paul  * pointers to descriptors.
364795d67482SBill Paul  */
364895d67482SBill Paul static int
3649676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
365095d67482SBill Paul {
36517e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
3652f41ac2beSBill Paul 	bus_dmamap_t		map;
3653676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
3654676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
36557e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
3656676ad2c9SGleb Smirnoff 	uint16_t		csum_flags;
36577e27542aSGleb Smirnoff 	int			nsegs, i, error;
365895d67482SBill Paul 
36596909dc43SGleb Smirnoff 	csum_flags = 0;
36606909dc43SGleb Smirnoff 	if (m->m_pkthdr.csum_flags) {
36616909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
36626909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
36636909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
36646909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
36656909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
36666909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
36676909dc43SGleb Smirnoff 				m_freem(m);
36686909dc43SGleb Smirnoff 				*m_head = NULL;
36696909dc43SGleb Smirnoff 				return (error);
36706909dc43SGleb Smirnoff 			}
36716909dc43SGleb Smirnoff 		}
36726909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
36736909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
36746909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
36756909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
36766909dc43SGleb Smirnoff 	}
36776909dc43SGleb Smirnoff 
36787e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
36790ac56796SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
3680676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
36817e27542aSGleb Smirnoff 	if (error == EFBIG) {
36824eee14cbSMarius Strobl 		m = m_collapse(m, M_DONTWAIT, BGE_NSEG_NEW);
3683676ad2c9SGleb Smirnoff 		if (m == NULL) {
3684676ad2c9SGleb Smirnoff 			m_freem(*m_head);
3685676ad2c9SGleb Smirnoff 			*m_head = NULL;
36867e27542aSGleb Smirnoff 			return (ENOBUFS);
36877e27542aSGleb Smirnoff 		}
3688676ad2c9SGleb Smirnoff 		*m_head = m;
36890ac56796SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
36900ac56796SPyun YongHyeon 		    m, segs, &nsegs, BUS_DMA_NOWAIT);
3691676ad2c9SGleb Smirnoff 		if (error) {
3692676ad2c9SGleb Smirnoff 			m_freem(m);
3693676ad2c9SGleb Smirnoff 			*m_head = NULL;
36947e27542aSGleb Smirnoff 			return (error);
36957e27542aSGleb Smirnoff 		}
3696676ad2c9SGleb Smirnoff 	} else if (error != 0)
3697676ad2c9SGleb Smirnoff 		return (error);
36987e27542aSGleb Smirnoff 
369995d67482SBill Paul 	/*
370095d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
370195d67482SBill Paul 	 * of the end of the ring.
370295d67482SBill Paul 	 */
37037e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
37040ac56796SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
370595d67482SBill Paul 		return (ENOBUFS);
37067e27542aSGleb Smirnoff 	}
37077e27542aSGleb Smirnoff 
37080ac56796SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
3709e65bed95SPyun YongHyeon 
37107e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
37117e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
37127e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
37137e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
37147e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
37157e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
37167e27542aSGleb Smirnoff 		if (i == nsegs - 1)
37177e27542aSGleb Smirnoff 			break;
37187e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
37197e27542aSGleb Smirnoff 	}
37207e27542aSGleb Smirnoff 
37217e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
37227e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
3723676ad2c9SGleb Smirnoff 
37247e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
37257e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
37264e35d186SJung-uk Kim #if __FreeBSD_version > 700022
372778ba57b9SAndre Oppermann 	if (m->m_flags & M_VLANTAG) {
37287e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
372978ba57b9SAndre Oppermann 		d->bge_vlan_tag = m->m_pkthdr.ether_vtag;
37307e27542aSGleb Smirnoff 	} else
37317e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
37324e35d186SJung-uk Kim #else
37334e35d186SJung-uk Kim 	{
37344e35d186SJung-uk Kim 		struct m_tag		*mtag;
37354e35d186SJung-uk Kim 
37364e35d186SJung-uk Kim 		if ((mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m)) != NULL) {
37374e35d186SJung-uk Kim 			d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
37384e35d186SJung-uk Kim 			d->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
37394e35d186SJung-uk Kim 		} else
37404e35d186SJung-uk Kim 			d->bge_vlan_tag = 0;
37414e35d186SJung-uk Kim 	}
37424e35d186SJung-uk Kim #endif
3743f41ac2beSBill Paul 
3744f41ac2beSBill Paul 	/*
3745f41ac2beSBill Paul 	 * Insure that the map for this transmission
3746f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
3747f41ac2beSBill Paul 	 * in this chain.
3748f41ac2beSBill Paul 	 */
37497e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
37507e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
3751676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
37527e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
375395d67482SBill Paul 
37547e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
37557e27542aSGleb Smirnoff 	*txidx = idx;
375695d67482SBill Paul 
375795d67482SBill Paul 	return (0);
375895d67482SBill Paul }
375995d67482SBill Paul 
376095d67482SBill Paul /*
376195d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
376295d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
376395d67482SBill Paul  */
376495d67482SBill Paul static void
37653f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
376695d67482SBill Paul {
376795d67482SBill Paul 	struct bge_softc *sc;
376895d67482SBill Paul 	struct mbuf *m_head = NULL;
376914bbd30fSGleb Smirnoff 	uint32_t prodidx;
3770303a718cSDag-Erling Smørgrav 	int count = 0;
377195d67482SBill Paul 
377295d67482SBill Paul 	sc = ifp->if_softc;
377395d67482SBill Paul 
3774dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
377595d67482SBill Paul 		return;
377695d67482SBill Paul 
377714bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
377895d67482SBill Paul 
377995d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
37804d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
378195d67482SBill Paul 		if (m_head == NULL)
378295d67482SBill Paul 			break;
378395d67482SBill Paul 
378495d67482SBill Paul 		/*
378595d67482SBill Paul 		 * XXX
3786b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
3787b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3788b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
3789b874fdd4SYaroslav Tykhiy 		 *
3790b874fdd4SYaroslav Tykhiy 		 * XXX
379195d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
379295d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
379395d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
379495d67482SBill Paul 		 * chain at once.
379595d67482SBill Paul 		 * (paranoia -- may not actually be needed)
379695d67482SBill Paul 		 */
379795d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
379895d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
379995d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
380095d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
38014d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
380213f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
380395d67482SBill Paul 				break;
380495d67482SBill Paul 			}
380595d67482SBill Paul 		}
380695d67482SBill Paul 
380795d67482SBill Paul 		/*
380895d67482SBill Paul 		 * Pack the data into the transmit ring. If we
380995d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
381095d67482SBill Paul 		 * for the NIC to drain the ring.
381195d67482SBill Paul 		 */
3812676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
3813676ad2c9SGleb Smirnoff 			if (m_head == NULL)
3814676ad2c9SGleb Smirnoff 				break;
38154d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
381613f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
381795d67482SBill Paul 			break;
381895d67482SBill Paul 		}
3819303a718cSDag-Erling Smørgrav 		++count;
382095d67482SBill Paul 
382195d67482SBill Paul 		/*
382295d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
382395d67482SBill Paul 		 * to him.
382495d67482SBill Paul 		 */
38254e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
382645ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
38274e35d186SJung-uk Kim #else
38284e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
38294e35d186SJung-uk Kim #endif
383095d67482SBill Paul 	}
383195d67482SBill Paul 
38323f74909aSGleb Smirnoff 	if (count == 0)
38333f74909aSGleb Smirnoff 		/* No packets were dequeued. */
3834303a718cSDag-Erling Smørgrav 		return;
3835303a718cSDag-Erling Smørgrav 
3836aa94f333SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
3837aa94f333SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
3838aa94f333SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
38393f74909aSGleb Smirnoff 	/* Transmit. */
384038cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
38413927098fSPaul Saab 	/* 5700 b2 errata */
3842e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
384338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
384495d67482SBill Paul 
384514bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
384614bbd30fSGleb Smirnoff 
384795d67482SBill Paul 	/*
384895d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
384995d67482SBill Paul 	 */
3850b74e67fbSGleb Smirnoff 	sc->bge_timer = 5;
385195d67482SBill Paul }
385295d67482SBill Paul 
38530f9bd73bSSam Leffler /*
38540f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
38550f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
38560f9bd73bSSam Leffler  */
385795d67482SBill Paul static void
38583f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
385995d67482SBill Paul {
38600f9bd73bSSam Leffler 	struct bge_softc *sc;
38610f9bd73bSSam Leffler 
38620f9bd73bSSam Leffler 	sc = ifp->if_softc;
38630f9bd73bSSam Leffler 	BGE_LOCK(sc);
38640f9bd73bSSam Leffler 	bge_start_locked(ifp);
38650f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
38660f9bd73bSSam Leffler }
38670f9bd73bSSam Leffler 
38680f9bd73bSSam Leffler static void
38693f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
38700f9bd73bSSam Leffler {
387195d67482SBill Paul 	struct ifnet *ifp;
38723f74909aSGleb Smirnoff 	uint16_t *m;
387395d67482SBill Paul 
38740f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
387595d67482SBill Paul 
3876fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
387795d67482SBill Paul 
387813f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
387995d67482SBill Paul 		return;
388095d67482SBill Paul 
388195d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
388295d67482SBill Paul 	bge_stop(sc);
38838cb1383cSDoug Ambrisko 
38848cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
38858cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
388695d67482SBill Paul 	bge_reset(sc);
38878cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
38888cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
38898cb1383cSDoug Ambrisko 
389095d67482SBill Paul 	bge_chipinit(sc);
389195d67482SBill Paul 
389295d67482SBill Paul 	/*
389395d67482SBill Paul 	 * Init the various state machines, ring
389495d67482SBill Paul 	 * control blocks and firmware.
389595d67482SBill Paul 	 */
389695d67482SBill Paul 	if (bge_blockinit(sc)) {
3897fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
389895d67482SBill Paul 		return;
389995d67482SBill Paul 	}
390095d67482SBill Paul 
3901fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
390295d67482SBill Paul 
390395d67482SBill Paul 	/* Specify MTU. */
390495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3905cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
3906cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
390795d67482SBill Paul 
390895d67482SBill Paul 	/* Load our MAC address. */
39093f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
391095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
391195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
391295d67482SBill Paul 
39133e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
39143e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
391595d67482SBill Paul 
391695d67482SBill Paul 	/* Program multicast filter. */
391795d67482SBill Paul 	bge_setmulti(sc);
391895d67482SBill Paul 
3919cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
3920cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
3921cb2eacc7SYaroslav Tykhiy 
392295d67482SBill Paul 	/* Init RX ring. */
39233ee5d7daSPyun YongHyeon 	if (bge_init_rx_ring_std(sc) != 0) {
39243ee5d7daSPyun YongHyeon 		device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
39253ee5d7daSPyun YongHyeon 		bge_stop(sc);
39263ee5d7daSPyun YongHyeon 		return;
39273ee5d7daSPyun YongHyeon 	}
392895d67482SBill Paul 
39290434d1b8SBill Paul 	/*
39300434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
39310434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
39320434d1b8SBill Paul 	 * entry of the ring.
39330434d1b8SBill Paul 	 */
39340434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
39353f74909aSGleb Smirnoff 		uint32_t		v, i;
39360434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
39370434d1b8SBill Paul 			DELAY(20);
39380434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
39390434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
39400434d1b8SBill Paul 				break;
39410434d1b8SBill Paul 		}
39420434d1b8SBill Paul 		if (i == 10)
3943fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
3944fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
39450434d1b8SBill Paul 	}
39460434d1b8SBill Paul 
394795d67482SBill Paul 	/* Init jumbo RX ring. */
3948c215fd77SPyun YongHyeon 	if (ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
3949c215fd77SPyun YongHyeon 	    (MCLBYTES - ETHER_ALIGN)) {
39503ee5d7daSPyun YongHyeon 		if (bge_init_rx_ring_jumbo(sc) != 0) {
39513ee5d7daSPyun YongHyeon 			device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
39523ee5d7daSPyun YongHyeon 			bge_stop(sc);
39533ee5d7daSPyun YongHyeon 			return;
39543ee5d7daSPyun YongHyeon 		}
39553ee5d7daSPyun YongHyeon 	}
395695d67482SBill Paul 
39573f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
395895d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
395995d67482SBill Paul 
39607e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
39617e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
39627e6e2507SJung-uk Kim 
396395d67482SBill Paul 	/* Init TX ring. */
396495d67482SBill Paul 	bge_init_tx_ring(sc);
396595d67482SBill Paul 
39663f74909aSGleb Smirnoff 	/* Turn on transmitter. */
396795d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
396895d67482SBill Paul 
39693f74909aSGleb Smirnoff 	/* Turn on receiver. */
397095d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
397195d67482SBill Paul 
397295d67482SBill Paul 	/* Tell firmware we're alive. */
397395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
397495d67482SBill Paul 
397575719184SGleb Smirnoff #ifdef DEVICE_POLLING
397675719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
397775719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
397875719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
397975719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
398038cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
398175719184SGleb Smirnoff 	} else
398275719184SGleb Smirnoff #endif
398375719184SGleb Smirnoff 
398495d67482SBill Paul 	/* Enable host interrupts. */
398575719184SGleb Smirnoff 	{
398695d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
398795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
398838cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
398975719184SGleb Smirnoff 	}
399095d67482SBill Paul 
399167d5e043SOleg Bulyzhin 	bge_ifmedia_upd_locked(ifp);
399295d67482SBill Paul 
399313f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
399413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
399595d67482SBill Paul 
39960f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
39970f9bd73bSSam Leffler }
39980f9bd73bSSam Leffler 
39990f9bd73bSSam Leffler static void
40003f74909aSGleb Smirnoff bge_init(void *xsc)
40010f9bd73bSSam Leffler {
40020f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
40030f9bd73bSSam Leffler 
40040f9bd73bSSam Leffler 	BGE_LOCK(sc);
40050f9bd73bSSam Leffler 	bge_init_locked(sc);
40060f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
400795d67482SBill Paul }
400895d67482SBill Paul 
400995d67482SBill Paul /*
401095d67482SBill Paul  * Set media options.
401195d67482SBill Paul  */
401295d67482SBill Paul static int
40133f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
401495d67482SBill Paul {
401567d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
401667d5e043SOleg Bulyzhin 	int res;
401767d5e043SOleg Bulyzhin 
401867d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
401967d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
402067d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
402167d5e043SOleg Bulyzhin 
402267d5e043SOleg Bulyzhin 	return (res);
402367d5e043SOleg Bulyzhin }
402467d5e043SOleg Bulyzhin 
402567d5e043SOleg Bulyzhin static int
402667d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
402767d5e043SOleg Bulyzhin {
402867d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
402995d67482SBill Paul 	struct mii_data *mii;
40304f09c4c7SMarius Strobl 	struct mii_softc *miisc;
403195d67482SBill Paul 	struct ifmedia *ifm;
403295d67482SBill Paul 
403367d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
403467d5e043SOleg Bulyzhin 
403595d67482SBill Paul 	ifm = &sc->bge_ifmedia;
403695d67482SBill Paul 
403795d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
4038652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
403995d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
404095d67482SBill Paul 			return (EINVAL);
404195d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
404295d67482SBill Paul 		case IFM_AUTO:
4043ff50922bSDoug White 			/*
4044ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
4045ff50922bSDoug White 			 * mechanism for programming the autoneg
4046ff50922bSDoug White 			 * advertisement registers in TBI mode.
4047ff50922bSDoug White 			 */
40480f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4049ff50922bSDoug White 				uint32_t sgdig;
40500f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
40510f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
4052ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
4053ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
4054ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
4055ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
4056ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
4057ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
4058ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
4059ff50922bSDoug White 					DELAY(5);
4060ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
4061ff50922bSDoug White 				}
40620f89fde2SJung-uk Kim 			}
406395d67482SBill Paul 			break;
406495d67482SBill Paul 		case IFM_1000_SX:
406595d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
406695d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
406795d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
406895d67482SBill Paul 			} else {
406995d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
407095d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
407195d67482SBill Paul 			}
407295d67482SBill Paul 			break;
407395d67482SBill Paul 		default:
407495d67482SBill Paul 			return (EINVAL);
407595d67482SBill Paul 		}
407695d67482SBill Paul 		return (0);
407795d67482SBill Paul 	}
407895d67482SBill Paul 
40791493e883SOleg Bulyzhin 	sc->bge_link_evt++;
408095d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
40814f09c4c7SMarius Strobl 	if (mii->mii_instance)
40824f09c4c7SMarius Strobl 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
408395d67482SBill Paul 			mii_phy_reset(miisc);
408495d67482SBill Paul 	mii_mediachg(mii);
408595d67482SBill Paul 
4086902827f6SBjoern A. Zeeb 	/*
4087902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
4088902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
4089902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
4090902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
4091902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
4092902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
4093902827f6SBjoern A. Zeeb 	 * get an RX intr.
4094902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
4095902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
4096902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
4097902827f6SBjoern A. Zeeb 	 */
40984f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
40994f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
4100902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
41014f0794ffSBjoern A. Zeeb 	else
410263ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
4103902827f6SBjoern A. Zeeb 
410495d67482SBill Paul 	return (0);
410595d67482SBill Paul }
410695d67482SBill Paul 
410795d67482SBill Paul /*
410895d67482SBill Paul  * Report current media status.
410995d67482SBill Paul  */
411095d67482SBill Paul static void
41113f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
411295d67482SBill Paul {
411367d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
411495d67482SBill Paul 	struct mii_data *mii;
411595d67482SBill Paul 
411667d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
411795d67482SBill Paul 
4118652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
411995d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
412095d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
412195d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
412295d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
412395d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
41244c0da0ffSGleb Smirnoff 		else {
41254c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
412667d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
41274c0da0ffSGleb Smirnoff 			return;
41284c0da0ffSGleb Smirnoff 		}
412995d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
413095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
413195d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
413295d67482SBill Paul 		else
413395d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
413467d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
413595d67482SBill Paul 		return;
413695d67482SBill Paul 	}
413795d67482SBill Paul 
413895d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
413995d67482SBill Paul 	mii_pollstat(mii);
414095d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
414195d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
414267d5e043SOleg Bulyzhin 
414367d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
414495d67482SBill Paul }
414595d67482SBill Paul 
414695d67482SBill Paul static int
41473f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
414895d67482SBill Paul {
414995d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
415095d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
415195d67482SBill Paul 	struct mii_data *mii;
4152f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
415395d67482SBill Paul 
415495d67482SBill Paul 	switch (command) {
415595d67482SBill Paul 	case SIOCSIFMTU:
41564c0da0ffSGleb Smirnoff 		if (ifr->ifr_mtu < ETHERMIN ||
41574c0da0ffSGleb Smirnoff 		    ((BGE_IS_JUMBO_CAPABLE(sc)) &&
41584c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > BGE_JUMBO_MTU) ||
41594c0da0ffSGleb Smirnoff 		    ((!BGE_IS_JUMBO_CAPABLE(sc)) &&
41604c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > ETHERMTU))
416195d67482SBill Paul 			error = EINVAL;
41624c0da0ffSGleb Smirnoff 		else if (ifp->if_mtu != ifr->ifr_mtu) {
416395d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
416413f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
416595d67482SBill Paul 			bge_init(sc);
416695d67482SBill Paul 		}
416795d67482SBill Paul 		break;
416895d67482SBill Paul 	case SIOCSIFFLAGS:
41690f9bd73bSSam Leffler 		BGE_LOCK(sc);
417095d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
417195d67482SBill Paul 			/*
417295d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
417395d67482SBill Paul 			 * then just use the 'set promisc mode' command
417495d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
417595d67482SBill Paul 			 * a full re-init means reloading the firmware and
417695d67482SBill Paul 			 * waiting for it to start up, which may take a
4177d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
417895d67482SBill Paul 			 */
4179f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4180f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
41813e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
41823e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
4183f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
4184d183af7fSRuslan Ermilov 					bge_setmulti(sc);
418595d67482SBill Paul 			} else
41860f9bd73bSSam Leffler 				bge_init_locked(sc);
418795d67482SBill Paul 		} else {
418813f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
418995d67482SBill Paul 				bge_stop(sc);
419095d67482SBill Paul 			}
419195d67482SBill Paul 		}
419295d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
41930f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
419495d67482SBill Paul 		error = 0;
419595d67482SBill Paul 		break;
419695d67482SBill Paul 	case SIOCADDMULTI:
419795d67482SBill Paul 	case SIOCDELMULTI:
419813f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
41990f9bd73bSSam Leffler 			BGE_LOCK(sc);
420095d67482SBill Paul 			bge_setmulti(sc);
42010f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
420295d67482SBill Paul 			error = 0;
420395d67482SBill Paul 		}
420495d67482SBill Paul 		break;
420595d67482SBill Paul 	case SIOCSIFMEDIA:
420695d67482SBill Paul 	case SIOCGIFMEDIA:
4207652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
420895d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
420995d67482SBill Paul 			    &sc->bge_ifmedia, command);
421095d67482SBill Paul 		} else {
421195d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
421295d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
421395d67482SBill Paul 			    &mii->mii_media, command);
421495d67482SBill Paul 		}
421595d67482SBill Paul 		break;
421695d67482SBill Paul 	case SIOCSIFCAP:
421795d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
421875719184SGleb Smirnoff #ifdef DEVICE_POLLING
421975719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
422075719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
422175719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
422275719184SGleb Smirnoff 				if (error)
422375719184SGleb Smirnoff 					return (error);
422475719184SGleb Smirnoff 				BGE_LOCK(sc);
422575719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
422675719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
422738cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
422875719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
422975719184SGleb Smirnoff 				BGE_UNLOCK(sc);
423075719184SGleb Smirnoff 			} else {
423175719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
423275719184SGleb Smirnoff 				/* Enable interrupt even in error case */
423375719184SGleb Smirnoff 				BGE_LOCK(sc);
423475719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
423575719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
423638cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
423775719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
423875719184SGleb Smirnoff 				BGE_UNLOCK(sc);
423975719184SGleb Smirnoff 			}
424075719184SGleb Smirnoff 		}
424175719184SGleb Smirnoff #endif
4242d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
4243d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
4244d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
4245d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
4246b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
424795d67482SBill Paul 			else
4248b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
42494e35d186SJung-uk Kim #ifdef VLAN_CAPABILITIES
4250479b23b7SGleb Smirnoff 			VLAN_CAPABILITIES(ifp);
42514e35d186SJung-uk Kim #endif
425295d67482SBill Paul 		}
4253cb2eacc7SYaroslav Tykhiy 
4254cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
4255cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
4256cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
4257cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
4258cb2eacc7SYaroslav Tykhiy 		}
4259cb2eacc7SYaroslav Tykhiy 
4260cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_HWTAGGING) {
4261cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
4262cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
4263cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
4264cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
4265cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
4266cb2eacc7SYaroslav Tykhiy 			VLAN_CAPABILITIES(ifp);
4267cb2eacc7SYaroslav Tykhiy #endif
4268cb2eacc7SYaroslav Tykhiy 		}
4269cb2eacc7SYaroslav Tykhiy 
427095d67482SBill Paul 		break;
427195d67482SBill Paul 	default:
4272673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
427395d67482SBill Paul 		break;
427495d67482SBill Paul 	}
427595d67482SBill Paul 
427695d67482SBill Paul 	return (error);
427795d67482SBill Paul }
427895d67482SBill Paul 
427995d67482SBill Paul static void
4280b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
428195d67482SBill Paul {
4282b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
428395d67482SBill Paul 
4284b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
4285b74e67fbSGleb Smirnoff 
4286b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
4287b74e67fbSGleb Smirnoff 		return;
4288b74e67fbSGleb Smirnoff 
4289b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
429095d67482SBill Paul 
4291fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
429295d67482SBill Paul 
429313f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
4294426742bfSGleb Smirnoff 	bge_init_locked(sc);
429595d67482SBill Paul 
429695d67482SBill Paul 	ifp->if_oerrors++;
429795d67482SBill Paul }
429895d67482SBill Paul 
429995d67482SBill Paul /*
430095d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
430195d67482SBill Paul  * RX and TX lists.
430295d67482SBill Paul  */
430395d67482SBill Paul static void
43043f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
430595d67482SBill Paul {
430695d67482SBill Paul 	struct ifnet *ifp;
430795d67482SBill Paul 	struct ifmedia_entry *ifm;
430895d67482SBill Paul 	struct mii_data *mii = NULL;
430995d67482SBill Paul 	int mtmp, itmp;
431095d67482SBill Paul 
43110f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
43120f9bd73bSSam Leffler 
4313fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
431495d67482SBill Paul 
4315652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0)
431695d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
431795d67482SBill Paul 
43180f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
431995d67482SBill Paul 
432044b63691SBjoern A. Zeeb 	/* Disable host interrupts. */
432144b63691SBjoern A. Zeeb 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
432244b63691SBjoern A. Zeeb 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
432344b63691SBjoern A. Zeeb 
432444b63691SBjoern A. Zeeb 	/*
432544b63691SBjoern A. Zeeb 	 * Tell firmware we're shutting down.
432644b63691SBjoern A. Zeeb 	 */
432744b63691SBjoern A. Zeeb 	bge_stop_fw(sc);
432844b63691SBjoern A. Zeeb 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
432944b63691SBjoern A. Zeeb 
433095d67482SBill Paul 	/*
43313f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
433295d67482SBill Paul 	 */
433395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
433495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
433595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
43367ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
433795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
433895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
433995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
434095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
434195d67482SBill Paul 
434295d67482SBill Paul 	/*
43433f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
434495d67482SBill Paul 	 */
434595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
434695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
434795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
434895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
434995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
43507ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
435195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
435295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
435395d67482SBill Paul 
435495d67482SBill Paul 	/*
435595d67482SBill Paul 	 * Shut down all of the memory managers and related
435695d67482SBill Paul 	 * state machines.
435795d67482SBill Paul 	 */
435895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
435995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
43607ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
436195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
43620c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
436395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
43647ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
436595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
436695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
43670434d1b8SBill Paul 	}
436895d67482SBill Paul 
43698cb1383cSDoug Ambrisko 	bge_reset(sc);
43708cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
43718cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
43728cb1383cSDoug Ambrisko 
43738cb1383cSDoug Ambrisko 	/*
43748cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
43758cb1383cSDoug Ambrisko 	 */
43768cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
43778cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
43788cb1383cSDoug Ambrisko 	else
437995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
438095d67482SBill Paul 
438195d67482SBill Paul 	/* Free the RX lists. */
438295d67482SBill Paul 	bge_free_rx_ring_std(sc);
438395d67482SBill Paul 
438495d67482SBill Paul 	/* Free jumbo RX list. */
43854c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
438695d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
438795d67482SBill Paul 
438895d67482SBill Paul 	/* Free TX buffers. */
438995d67482SBill Paul 	bge_free_tx_ring(sc);
439095d67482SBill Paul 
439195d67482SBill Paul 	/*
439295d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
439395d67482SBill Paul 	 * unchanged so that things will be put back to normal when
439495d67482SBill Paul 	 * we bring the interface back up.
439595d67482SBill Paul 	 */
4396652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
439795d67482SBill Paul 		itmp = ifp->if_flags;
439895d67482SBill Paul 		ifp->if_flags |= IFF_UP;
4399dcc34049SPawel Jakub Dawidek 		/*
4400dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
4401dcc34049SPawel Jakub Dawidek 		 */
4402dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
440395d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
440495d67482SBill Paul 			mtmp = ifm->ifm_media;
440595d67482SBill Paul 			ifm->ifm_media = IFM_ETHER | IFM_NONE;
440695d67482SBill Paul 			mii_mediachg(mii);
440795d67482SBill Paul 			ifm->ifm_media = mtmp;
4408dcc34049SPawel Jakub Dawidek 		}
440995d67482SBill Paul 		ifp->if_flags = itmp;
441095d67482SBill Paul 	}
441195d67482SBill Paul 
441295d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
441395d67482SBill Paul 
44145dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
44151493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
44161493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
44171493e883SOleg Bulyzhin 	sc->bge_link = 0;
441895d67482SBill Paul 
44191493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
442095d67482SBill Paul }
442195d67482SBill Paul 
442295d67482SBill Paul /*
442395d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
442495d67482SBill Paul  * get confused by errant DMAs when rebooting.
442595d67482SBill Paul  */
4426b6c974e8SWarner Losh static int
44273f74909aSGleb Smirnoff bge_shutdown(device_t dev)
442895d67482SBill Paul {
442995d67482SBill Paul 	struct bge_softc *sc;
443095d67482SBill Paul 
443195d67482SBill Paul 	sc = device_get_softc(dev);
44320f9bd73bSSam Leffler 	BGE_LOCK(sc);
443395d67482SBill Paul 	bge_stop(sc);
443495d67482SBill Paul 	bge_reset(sc);
44350f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
4436b6c974e8SWarner Losh 
4437b6c974e8SWarner Losh 	return (0);
443895d67482SBill Paul }
443914afefa3SPawel Jakub Dawidek 
444014afefa3SPawel Jakub Dawidek static int
444114afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
444214afefa3SPawel Jakub Dawidek {
444314afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
444414afefa3SPawel Jakub Dawidek 
444514afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
444614afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
444714afefa3SPawel Jakub Dawidek 	bge_stop(sc);
444814afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
444914afefa3SPawel Jakub Dawidek 
445014afefa3SPawel Jakub Dawidek 	return (0);
445114afefa3SPawel Jakub Dawidek }
445214afefa3SPawel Jakub Dawidek 
445314afefa3SPawel Jakub Dawidek static int
445414afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
445514afefa3SPawel Jakub Dawidek {
445614afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
445714afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
445814afefa3SPawel Jakub Dawidek 
445914afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
446014afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
446114afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
446214afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
446314afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
446414afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
446514afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
446614afefa3SPawel Jakub Dawidek 	}
446714afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
446814afefa3SPawel Jakub Dawidek 
446914afefa3SPawel Jakub Dawidek 	return (0);
447014afefa3SPawel Jakub Dawidek }
4471dab5cd05SOleg Bulyzhin 
4472dab5cd05SOleg Bulyzhin static void
44733f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
4474dab5cd05SOleg Bulyzhin {
44751f313773SOleg Bulyzhin 	struct mii_data *mii;
44761f313773SOleg Bulyzhin 	uint32_t link, status;
4477dab5cd05SOleg Bulyzhin 
4478dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
44791f313773SOleg Bulyzhin 
44803f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
44817b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
44827b97099dSOleg Bulyzhin 
4483dab5cd05SOleg Bulyzhin 	/*
4484dab5cd05SOleg Bulyzhin 	 * Process link state changes.
4485dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
4486dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
4487dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
4488dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
4489dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
4490dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
4491dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
4492dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
44931f313773SOleg Bulyzhin 	 *
44941f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
44954c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
4496dab5cd05SOleg Bulyzhin 	 */
4497dab5cd05SOleg Bulyzhin 
44981f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
44994c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
4500dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
4501dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
45021f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
45035dda8085SOleg Bulyzhin 			mii_pollstat(mii);
45041f313773SOleg Bulyzhin 			if (!sc->bge_link &&
45051f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
45061f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
45071f313773SOleg Bulyzhin 				sc->bge_link++;
45081f313773SOleg Bulyzhin 				if (bootverbose)
45091f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
45101f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
45111f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
45121f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
45131f313773SOleg Bulyzhin 				sc->bge_link = 0;
45141f313773SOleg Bulyzhin 				if (bootverbose)
45151f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
45161f313773SOleg Bulyzhin 			}
45171f313773SOleg Bulyzhin 
45183f74909aSGleb Smirnoff 			/* Clear the interrupt. */
4519dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
4520dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
4521dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
4522dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
4523dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
4524dab5cd05SOleg Bulyzhin 		}
4525dab5cd05SOleg Bulyzhin 		return;
4526dab5cd05SOleg Bulyzhin 	}
4527dab5cd05SOleg Bulyzhin 
4528652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
45291f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
45307b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
45317b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
45321f313773SOleg Bulyzhin 				sc->bge_link++;
45331f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
45341f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
45351f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
45360c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
45371f313773SOleg Bulyzhin 				if (bootverbose)
45381f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
45393f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
45403f74909aSGleb Smirnoff 				    LINK_STATE_UP);
45417b97099dSOleg Bulyzhin 			}
45421f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
4543dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
45441f313773SOleg Bulyzhin 			if (bootverbose)
45451f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
45467b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
45471f313773SOleg Bulyzhin 		}
45481493e883SOleg Bulyzhin 	} else if (CSR_READ_4(sc, BGE_MI_MODE) & BGE_MIMODE_AUTOPOLL) {
45491f313773SOleg Bulyzhin 		/*
45500c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
45510c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
45520c8aa4eaSJung-uk Kim 		 * PHY link status directly.
45531f313773SOleg Bulyzhin 		 */
45541f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
45551f313773SOleg Bulyzhin 
45561f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
45571f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
45581f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
45595dda8085SOleg Bulyzhin 			mii_pollstat(mii);
45601f313773SOleg Bulyzhin 			if (!sc->bge_link &&
45611f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
45621f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
45631f313773SOleg Bulyzhin 				sc->bge_link++;
45641f313773SOleg Bulyzhin 				if (bootverbose)
45651f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
45661f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
45671f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
45681f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
45691f313773SOleg Bulyzhin 				sc->bge_link = 0;
45701f313773SOleg Bulyzhin 				if (bootverbose)
45711f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
45721f313773SOleg Bulyzhin 			}
45731f313773SOleg Bulyzhin 		}
45740c8aa4eaSJung-uk Kim 	} else {
45750c8aa4eaSJung-uk Kim 		/*
45760c8aa4eaSJung-uk Kim 		 * Discard link events for MII/GMII controllers
45770c8aa4eaSJung-uk Kim 		 * if MI auto-polling is disabled.
45780c8aa4eaSJung-uk Kim 		 */
4579dab5cd05SOleg Bulyzhin 	}
4580dab5cd05SOleg Bulyzhin 
45813f74909aSGleb Smirnoff 	/* Clear the attention. */
4582dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
4583dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
4584dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
4585dab5cd05SOleg Bulyzhin }
45866f8718a3SScott Long 
4587763757b2SScott Long #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
458806e83c7eSScott Long 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
4589763757b2SScott Long 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
4590763757b2SScott Long 	    desc)
4591763757b2SScott Long 
45926f8718a3SScott Long static void
45936f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
45946f8718a3SScott Long {
45956f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
4596763757b2SScott Long 	struct sysctl_oid_list *children, *schildren;
4597763757b2SScott Long 	struct sysctl_oid *tree;
45986f8718a3SScott Long 
45996f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
46006f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
46016f8718a3SScott Long 
46026f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
46036f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
46046f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
46056f8718a3SScott Long 	    "Debug Information");
46066f8718a3SScott Long 
46076f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
46086f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
46096f8718a3SScott Long 	    "Register Read");
46106f8718a3SScott Long 
46116f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
46126f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
46136f8718a3SScott Long 	    "Memory Read");
46146f8718a3SScott Long 
46156f8718a3SScott Long #endif
4616763757b2SScott Long 
4617d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
4618d949071dSJung-uk Kim 		return;
4619d949071dSJung-uk Kim 
4620763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats", CTLFLAG_RD,
4621763757b2SScott Long 	    NULL, "BGE Statistics");
4622763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
4623763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
4624763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
4625763757b2SScott Long 	    "FramesDroppedDueToFilters");
4626763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
4627763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
4628763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
4629763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
4630763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
4631763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
463206e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
463306e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
463406e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
463506e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
4636763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
4637763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
4638763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
4639763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
4640763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
4641763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
4642763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
4643763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
4644763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
4645763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
4646763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
4647763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
4648763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
4649763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
4650763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
4651763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
4652763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
4653763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
4654763757b2SScott Long 
4655763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
4656763757b2SScott Long 	    NULL, "BGE RX Statistics");
4657763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
4658763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
4659763757b2SScott Long 	    children, rxstats.ifHCInOctets, "Octets");
4660763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
4661763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
4662763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
4663763757b2SScott Long 	    children, rxstats.ifHCInUcastPkts, "UcastPkts");
4664763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
4665763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
4666763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
4667763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
4668763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
4669763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
4670763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
4671763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
4672763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
4673763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
4674763757b2SScott Long 	    "xoffPauseFramesReceived");
4675763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
4676763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
4677763757b2SScott Long 	    "ControlFramesReceived");
4678763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
4679763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
4680763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
4681763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
4682763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
4683763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
4684763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
4685763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
4686763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
468706e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
4688763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
468906e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
4690763757b2SScott Long 
4691763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
4692763757b2SScott Long 	    NULL, "BGE TX Statistics");
4693763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
4694763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
4695763757b2SScott Long 	    children, txstats.ifHCOutOctets, "Octets");
4696763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
4697763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
4698763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
4699763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
4700763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
4701763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
4702763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
4703763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
4704763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
4705763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
4706763757b2SScott Long 	    "InternalMacTransmitErrors");
4707763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
4708763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
4709763757b2SScott Long 	    "SingleCollisionFrames");
4710763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
4711763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
4712763757b2SScott Long 	    "MultipleCollisionFrames");
4713763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
4714763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
4715763757b2SScott Long 	    "DeferredTransmissions");
4716763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
4717763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
4718763757b2SScott Long 	    "ExcessiveCollisions");
4719763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
472006e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
472106e83c7eSScott Long 	    "LateCollisions");
4722763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
4723763757b2SScott Long 	    children, txstats.ifHCOutUcastPkts, "UcastPkts");
4724763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
4725763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
4726763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
4727763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
4728763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
4729763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
4730763757b2SScott Long 	    "CarrierSenseErrors");
4731763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
4732763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
4733763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
4734763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
4735763757b2SScott Long }
4736763757b2SScott Long 
4737763757b2SScott Long static int
4738763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
4739763757b2SScott Long {
4740763757b2SScott Long 	struct bge_softc *sc;
474106e83c7eSScott Long 	uint32_t result;
4742d949071dSJung-uk Kim 	int offset;
4743763757b2SScott Long 
4744763757b2SScott Long 	sc = (struct bge_softc *)arg1;
4745763757b2SScott Long 	offset = arg2;
4746d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
4747d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
4748041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
47496f8718a3SScott Long }
47506f8718a3SScott Long 
47516f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
47526f8718a3SScott Long static int
47536f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
47546f8718a3SScott Long {
47556f8718a3SScott Long 	struct bge_softc *sc;
47566f8718a3SScott Long 	uint16_t *sbdata;
47576f8718a3SScott Long 	int error;
47586f8718a3SScott Long 	int result;
47596f8718a3SScott Long 	int i, j;
47606f8718a3SScott Long 
47616f8718a3SScott Long 	result = -1;
47626f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
47636f8718a3SScott Long 	if (error || (req->newptr == NULL))
47646f8718a3SScott Long 		return (error);
47656f8718a3SScott Long 
47666f8718a3SScott Long 	if (result == 1) {
47676f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
47686f8718a3SScott Long 
47696f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
47706f8718a3SScott Long 		printf("Status Block:\n");
47716f8718a3SScott Long 		for (i = 0x0; i < (BGE_STATUS_BLK_SZ / 4); ) {
47726f8718a3SScott Long 			printf("%06x:", i);
47736f8718a3SScott Long 			for (j = 0; j < 8; j++) {
47746f8718a3SScott Long 				printf(" %04x", sbdata[i]);
47756f8718a3SScott Long 				i += 4;
47766f8718a3SScott Long 			}
47776f8718a3SScott Long 			printf("\n");
47786f8718a3SScott Long 		}
47796f8718a3SScott Long 
47806f8718a3SScott Long 		printf("Registers:\n");
47810c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
47826f8718a3SScott Long 			printf("%06x:", i);
47836f8718a3SScott Long 			for (j = 0; j < 8; j++) {
47846f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
47856f8718a3SScott Long 				i += 4;
47866f8718a3SScott Long 			}
47876f8718a3SScott Long 			printf("\n");
47886f8718a3SScott Long 		}
47896f8718a3SScott Long 
47906f8718a3SScott Long 		printf("Hardware Flags:\n");
4791a5779553SStanislav Sedov 		if (BGE_IS_5755_PLUS(sc))
4792a5779553SStanislav Sedov 			printf(" - 5755 Plus\n");
47935345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
47946f8718a3SScott Long 			printf(" - 575X Plus\n");
47955345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
47966f8718a3SScott Long 			printf(" - 5705 Plus\n");
47975345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
47985345bad0SScott Long 			printf(" - 5714 Family\n");
47995345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
48005345bad0SScott Long 			printf(" - 5700 Family\n");
48016f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
48026f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
48036f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
48046f8718a3SScott Long 			printf(" - PCI-X Bus\n");
48056f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
48066f8718a3SScott Long 			printf(" - PCI Express Bus\n");
48075ee49a3aSJung-uk Kim 		if (sc->bge_flags & BGE_FLAG_NO_3LED)
48086f8718a3SScott Long 			printf(" - No 3 LEDs\n");
48096f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
48106f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
48116f8718a3SScott Long 	}
48126f8718a3SScott Long 
48136f8718a3SScott Long 	return (error);
48146f8718a3SScott Long }
48156f8718a3SScott Long 
48166f8718a3SScott Long static int
48176f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
48186f8718a3SScott Long {
48196f8718a3SScott Long 	struct bge_softc *sc;
48206f8718a3SScott Long 	int error;
48216f8718a3SScott Long 	uint16_t result;
48226f8718a3SScott Long 	uint32_t val;
48236f8718a3SScott Long 
48246f8718a3SScott Long 	result = -1;
48256f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
48266f8718a3SScott Long 	if (error || (req->newptr == NULL))
48276f8718a3SScott Long 		return (error);
48286f8718a3SScott Long 
48296f8718a3SScott Long 	if (result < 0x8000) {
48306f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
48316f8718a3SScott Long 		val = CSR_READ_4(sc, result);
48326f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
48336f8718a3SScott Long 	}
48346f8718a3SScott Long 
48356f8718a3SScott Long 	return (error);
48366f8718a3SScott Long }
48376f8718a3SScott Long 
48386f8718a3SScott Long static int
48396f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
48406f8718a3SScott Long {
48416f8718a3SScott Long 	struct bge_softc *sc;
48426f8718a3SScott Long 	int error;
48436f8718a3SScott Long 	uint16_t result;
48446f8718a3SScott Long 	uint32_t val;
48456f8718a3SScott Long 
48466f8718a3SScott Long 	result = -1;
48476f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
48486f8718a3SScott Long 	if (error || (req->newptr == NULL))
48496f8718a3SScott Long 		return (error);
48506f8718a3SScott Long 
48516f8718a3SScott Long 	if (result < 0x8000) {
48526f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
48536f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
48546f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
48556f8718a3SScott Long 	}
48566f8718a3SScott Long 
48576f8718a3SScott Long 	return (error);
48586f8718a3SScott Long }
48596f8718a3SScott Long #endif
486038cc658fSJohn Baldwin 
486138cc658fSJohn Baldwin static int
48625fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
48635fea260fSMarius Strobl {
48645fea260fSMarius Strobl 
48655fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
48665fea260fSMarius Strobl 		return (1);
48675fea260fSMarius Strobl 
48685fea260fSMarius Strobl #ifdef __sparc64__
48695fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
48705fea260fSMarius Strobl 	return (0);
48715fea260fSMarius Strobl #endif
48725fea260fSMarius Strobl 	return (1);
48735fea260fSMarius Strobl }
48745fea260fSMarius Strobl 
48755fea260fSMarius Strobl static int
487638cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
487738cc658fSJohn Baldwin {
487838cc658fSJohn Baldwin 	uint32_t mac_addr;
487938cc658fSJohn Baldwin 
488038cc658fSJohn Baldwin 	mac_addr = bge_readmem_ind(sc, 0x0c14);
488138cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
488238cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
488338cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
488438cc658fSJohn Baldwin 		mac_addr = bge_readmem_ind(sc, 0x0c18);
488538cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
488638cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
488738cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
488838cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
48895fea260fSMarius Strobl 		return (0);
489038cc658fSJohn Baldwin 	}
48915fea260fSMarius Strobl 	return (1);
489238cc658fSJohn Baldwin }
489338cc658fSJohn Baldwin 
489438cc658fSJohn Baldwin static int
489538cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
489638cc658fSJohn Baldwin {
489738cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
489838cc658fSJohn Baldwin 
489938cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
490038cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
490138cc658fSJohn Baldwin 
49025fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
49035fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
490438cc658fSJohn Baldwin }
490538cc658fSJohn Baldwin 
490638cc658fSJohn Baldwin static int
490738cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
490838cc658fSJohn Baldwin {
490938cc658fSJohn Baldwin 
49105fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
49115fea260fSMarius Strobl 		return (1);
49125fea260fSMarius Strobl 
49135fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
49145fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
491538cc658fSJohn Baldwin }
491638cc658fSJohn Baldwin 
491738cc658fSJohn Baldwin static int
491838cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
491938cc658fSJohn Baldwin {
492038cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
492138cc658fSJohn Baldwin 		/* NOTE: Order is critical */
49225fea260fSMarius Strobl 		bge_get_eaddr_fw,
492338cc658fSJohn Baldwin 		bge_get_eaddr_mem,
492438cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
492538cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
492638cc658fSJohn Baldwin 		NULL
492738cc658fSJohn Baldwin 	};
492838cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
492938cc658fSJohn Baldwin 
493038cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
493138cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
493238cc658fSJohn Baldwin 			break;
493338cc658fSJohn Baldwin 	}
493438cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
493538cc658fSJohn Baldwin }
4936