xref: /freebsd/sys/dev/bge/if_bge.c (revision 4f09c4c7e59f37dffc9118cc2af298b8760e93c3)
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 },
1734c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1744c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1754c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1764c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1774c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1784c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1794c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1804c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1814c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1824c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1839e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1849e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1859e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1869e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
1874c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
1884c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
1894c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
1904c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
1919e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
1929e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
1939e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
1944c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
1954c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
1964c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
1974c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
1984c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
19938cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906 },
20038cc658fSJohn Baldwin 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906M },
2014c0da0ffSGleb Smirnoff 
2024c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
2034c0da0ffSGleb Smirnoff 
2044c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
2054c0da0ffSGleb Smirnoff 
2064c0da0ffSGleb Smirnoff 	{ 0, 0 }
20795d67482SBill Paul };
20895d67482SBill Paul 
2094c0da0ffSGleb Smirnoff static const struct bge_vendor {
2104c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2114c0da0ffSGleb Smirnoff 	const char	*v_name;
2124c0da0ffSGleb Smirnoff } bge_vendors[] = {
2134c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2144c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2154c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2164c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2174c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2184c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
2194c0da0ffSGleb Smirnoff 
2204c0da0ffSGleb Smirnoff 	{ 0, NULL }
2214c0da0ffSGleb Smirnoff };
2224c0da0ffSGleb Smirnoff 
2234c0da0ffSGleb Smirnoff static const struct bge_revision {
2244c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2254c0da0ffSGleb Smirnoff 	const char	*br_name;
2264c0da0ffSGleb Smirnoff } bge_revisions[] = {
2274c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2284c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2294c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2304c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2314c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2324c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2334c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2344c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2354c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2364c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2374c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2384c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2394c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2404c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2414c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2424c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2439e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2444c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2454c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2464c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2474c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2484c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2494c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2504c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2514c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2524c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2534c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2544c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2554c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2564c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2574c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2584c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2594c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
26042787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2614c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2624c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2634c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
2644c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
2654c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
2664c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
2674c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
2684c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
2690c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5715_A3,	"BCM5715 A3" },
2700c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A0,	"BCM5755 A0" },
2710c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A1,	"BCM5755 A1" },
2720c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A2,	"BCM5755 A2" },
273bcc20328SJohn Baldwin 	{ BGE_CHIPID_BCM5722_A0,	"BCM5722 A0" },
27481179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
2756f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
2766f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
2776f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
27838cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A1,	"BCM5906 A1" },
27938cc658fSJohn Baldwin 	{ BGE_CHIPID_BCM5906_A2,	"BCM5906 A2" },
2804c0da0ffSGleb Smirnoff 
2814c0da0ffSGleb Smirnoff 	{ 0, NULL }
2824c0da0ffSGleb Smirnoff };
2834c0da0ffSGleb Smirnoff 
2844c0da0ffSGleb Smirnoff /*
2854c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
2864c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
2874c0da0ffSGleb Smirnoff  */
2884c0da0ffSGleb Smirnoff static const struct bge_revision bge_majorrevs[] = {
2899e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
2909e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
2919e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
2929e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
2939e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
2949e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
2959e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
2969e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
2979e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
2989e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
2999e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
30081179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
3016f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
30238cc658fSJohn Baldwin 	{ BGE_ASICREV_BCM5906,		"unknown BCM5906" },
3034c0da0ffSGleb Smirnoff 
3044c0da0ffSGleb Smirnoff 	{ 0, NULL }
3054c0da0ffSGleb Smirnoff };
3064c0da0ffSGleb Smirnoff 
3070c8aa4eaSJung-uk Kim #define	BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
3080c8aa4eaSJung-uk Kim #define	BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
3090c8aa4eaSJung-uk Kim #define	BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
3100c8aa4eaSJung-uk Kim #define	BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
3110c8aa4eaSJung-uk Kim #define	BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
3124c0da0ffSGleb Smirnoff 
3134c0da0ffSGleb Smirnoff const struct bge_revision * bge_lookup_rev(uint32_t);
3144c0da0ffSGleb Smirnoff const struct bge_vendor * bge_lookup_vendor(uint16_t);
31538cc658fSJohn Baldwin 
31638cc658fSJohn Baldwin typedef int	(*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
31738cc658fSJohn Baldwin 
318e51a25f8SAlfred Perlstein static int bge_probe(device_t);
319e51a25f8SAlfred Perlstein static int bge_attach(device_t);
320e51a25f8SAlfred Perlstein static int bge_detach(device_t);
32114afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
32214afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3233f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
324f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
325f41ac2beSBill Paul static int bge_dma_alloc(device_t);
326f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
327f41ac2beSBill Paul 
3285fea260fSMarius Strobl static int bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[]);
32938cc658fSJohn Baldwin static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
33038cc658fSJohn Baldwin static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
33138cc658fSJohn Baldwin static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
33238cc658fSJohn Baldwin static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
33338cc658fSJohn Baldwin 
334e51a25f8SAlfred Perlstein static void bge_txeof(struct bge_softc *);
335e51a25f8SAlfred Perlstein static void bge_rxeof(struct bge_softc *);
33695d67482SBill Paul 
3378cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
338e51a25f8SAlfred Perlstein static void bge_tick(void *);
339e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
3403f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
341676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
34295d67482SBill Paul 
343e51a25f8SAlfred Perlstein static void bge_intr(void *);
3440f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
345e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
346e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
3470f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
348e51a25f8SAlfred Perlstein static void bge_init(void *);
349e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
350b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
351b6c974e8SWarner Losh static int bge_shutdown(device_t);
35267d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
353e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
354e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
35595d67482SBill Paul 
35638cc658fSJohn Baldwin static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
35738cc658fSJohn Baldwin static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
35838cc658fSJohn Baldwin 
3593f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
360e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
36195d67482SBill Paul 
3623e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
363e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
364cb2eacc7SYaroslav Tykhiy static void bge_setvlan(struct bge_softc *);
36595d67482SBill Paul 
366e51a25f8SAlfred Perlstein static int bge_newbuf_std(struct bge_softc *, int, struct mbuf *);
367e51a25f8SAlfred Perlstein static int bge_newbuf_jumbo(struct bge_softc *, int, struct mbuf *);
368e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
369e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
370e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
371e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
372e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
373e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
37495d67482SBill Paul 
375e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
376e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
37795d67482SBill Paul 
3785fea260fSMarius Strobl static int bge_has_eaddr(struct bge_softc *);
3793f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
380e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
38138cc658fSJohn Baldwin static void bge_writembx(struct bge_softc *, int, int);
38295d67482SBill Paul #ifdef notdef
3833f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
38495d67482SBill Paul #endif
3859ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
386e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
3874f09c4c7SMarius Strobl static void bge_set_max_readrq(struct bge_softc *, int);
38895d67482SBill Paul 
389e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
390e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
391e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
39275719184SGleb Smirnoff #ifdef DEVICE_POLLING
3933f74909aSGleb Smirnoff static void bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
39475719184SGleb Smirnoff #endif
39595d67482SBill Paul 
3968cb1383cSDoug Ambrisko #define	BGE_RESET_START 1
3978cb1383cSDoug Ambrisko #define	BGE_RESET_STOP  2
3988cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
3998cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
4008cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
4018cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
402dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
40395d67482SBill Paul 
4046f8718a3SScott Long /*
4056f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
4066f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
4076f8718a3SScott Long  * traps on certain architectures.
4086f8718a3SScott Long  */
4096f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
4106f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
4116f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
4126f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
4136f8718a3SScott Long #endif
4146f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
415763757b2SScott Long static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
4166f8718a3SScott Long 
41795d67482SBill Paul static device_method_t bge_methods[] = {
41895d67482SBill Paul 	/* Device interface */
41995d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
42095d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
42195d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
42295d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
42314afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
42414afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
42595d67482SBill Paul 
42695d67482SBill Paul 	/* bus interface */
42795d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
42895d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
42995d67482SBill Paul 
43095d67482SBill Paul 	/* MII interface */
43195d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
43295d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
43395d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
43495d67482SBill Paul 
43595d67482SBill Paul 	{ 0, 0 }
43695d67482SBill Paul };
43795d67482SBill Paul 
43895d67482SBill Paul static driver_t bge_driver = {
43995d67482SBill Paul 	"bge",
44095d67482SBill Paul 	bge_methods,
44195d67482SBill Paul 	sizeof(struct bge_softc)
44295d67482SBill Paul };
44395d67482SBill Paul 
44495d67482SBill Paul static devclass_t bge_devclass;
44595d67482SBill Paul 
446f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
44795d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
44895d67482SBill Paul 
449f1a7e6d5SScott Long static int bge_allow_asf = 1;
450f1a7e6d5SScott Long 
451f1a7e6d5SScott Long TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
452f1a7e6d5SScott Long 
453f1a7e6d5SScott Long SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
454f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
455f1a7e6d5SScott Long 	"Allow ASF mode if available");
456c4529f41SMichael Reifenberger 
45708013fd3SMarius Strobl #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
45808013fd3SMarius Strobl #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
45908013fd3SMarius Strobl #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
46008013fd3SMarius Strobl #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
46108013fd3SMarius Strobl #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
46208013fd3SMarius Strobl 
46308013fd3SMarius Strobl static int
4645fea260fSMarius Strobl bge_has_eaddr(struct bge_softc *sc)
46508013fd3SMarius Strobl {
46608013fd3SMarius Strobl #ifdef __sparc64__
46708013fd3SMarius Strobl 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
46808013fd3SMarius Strobl 	device_t dev;
46908013fd3SMarius Strobl 	uint32_t subvendor;
47008013fd3SMarius Strobl 
47108013fd3SMarius Strobl 	dev = sc->bge_dev;
47208013fd3SMarius Strobl 
47308013fd3SMarius Strobl 	/*
47408013fd3SMarius Strobl 	 * The on-board BGEs found in sun4u machines aren't fitted with
47508013fd3SMarius Strobl 	 * an EEPROM which means that we have to obtain the MAC address
47608013fd3SMarius Strobl 	 * via OFW and that some tests will always fail.  We distinguish
47708013fd3SMarius Strobl 	 * such BGEs by the subvendor ID, which also has to be obtained
47808013fd3SMarius Strobl 	 * from OFW instead of the PCI configuration space as the latter
47908013fd3SMarius Strobl 	 * indicates Broadcom as the subvendor of the netboot interface.
48008013fd3SMarius Strobl 	 * For early Blade 1500 and 2500 we even have to check the OFW
48108013fd3SMarius Strobl 	 * device path as the subvendor ID always defaults to Broadcom
48208013fd3SMarius Strobl 	 * there.
48308013fd3SMarius Strobl 	 */
48408013fd3SMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
48508013fd3SMarius Strobl 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
48608013fd3SMarius Strobl 	    subvendor == SUN_VENDORID)
48708013fd3SMarius Strobl 		return (0);
48808013fd3SMarius Strobl 	memset(buf, 0, sizeof(buf));
48908013fd3SMarius Strobl 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
49008013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
49108013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
49208013fd3SMarius Strobl 			return (0);
49308013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
49408013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
49508013fd3SMarius Strobl 			return (0);
49608013fd3SMarius Strobl 	}
49708013fd3SMarius Strobl #endif
49808013fd3SMarius Strobl 	return (1);
49908013fd3SMarius Strobl }
50008013fd3SMarius Strobl 
5013f74909aSGleb Smirnoff static uint32_t
5023f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
50395d67482SBill Paul {
50495d67482SBill Paul 	device_t dev;
5056f8718a3SScott Long 	uint32_t val;
50695d67482SBill Paul 
50795d67482SBill Paul 	dev = sc->bge_dev;
50895d67482SBill Paul 
50995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
5106f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
5116f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
5126f8718a3SScott Long 	return (val);
51395d67482SBill Paul }
51495d67482SBill Paul 
51595d67482SBill Paul static void
5163f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
51795d67482SBill Paul {
51895d67482SBill Paul 	device_t dev;
51995d67482SBill Paul 
52095d67482SBill Paul 	dev = sc->bge_dev;
52195d67482SBill Paul 
52295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
52395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
5246f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
52595d67482SBill Paul }
52695d67482SBill Paul 
5274f09c4c7SMarius Strobl /*
5284f09c4c7SMarius Strobl  * PCI Express only
5294f09c4c7SMarius Strobl  */
5304f09c4c7SMarius Strobl static void
5314f09c4c7SMarius Strobl bge_set_max_readrq(struct bge_softc *sc, int expr_ptr)
5324f09c4c7SMarius Strobl {
5334f09c4c7SMarius Strobl 	device_t dev;
5344f09c4c7SMarius Strobl 	uint16_t val;
5354f09c4c7SMarius Strobl 
5364f09c4c7SMarius Strobl 	KASSERT((sc->bge_flags & BGE_FLAG_PCIE) && expr_ptr != 0,
5374f09c4c7SMarius Strobl 	    ("%s: not applicable", __func__));
5384f09c4c7SMarius Strobl 
5394f09c4c7SMarius Strobl 	dev = sc->bge_dev;
5404f09c4c7SMarius Strobl 
5414f09c4c7SMarius Strobl 	val = pci_read_config(dev, expr_ptr + BGE_PCIE_DEVCTL, 2);
5424f09c4c7SMarius Strobl 	if ((val & BGE_PCIE_DEVCTL_MAX_READRQ_MASK) !=
5434f09c4c7SMarius Strobl 	    BGE_PCIE_DEVCTL_MAX_READRQ_4096) {
5444f09c4c7SMarius Strobl 		if (bootverbose)
5454f09c4c7SMarius Strobl 			device_printf(dev, "adjust device control 0x%04x ",
5464f09c4c7SMarius Strobl 			    val);
5474f09c4c7SMarius Strobl 		val &= ~BGE_PCIE_DEVCTL_MAX_READRQ_MASK;
5484f09c4c7SMarius Strobl 		val |= BGE_PCIE_DEVCTL_MAX_READRQ_4096;
5494f09c4c7SMarius Strobl 		pci_write_config(dev, expr_ptr + BGE_PCIE_DEVCTL, val, 2);
5504f09c4c7SMarius Strobl 		if (bootverbose)
5514f09c4c7SMarius Strobl 			printf("-> 0x%04x\n", val);
5524f09c4c7SMarius Strobl 	}
5534f09c4c7SMarius Strobl }
5544f09c4c7SMarius Strobl 
55595d67482SBill Paul #ifdef notdef
5563f74909aSGleb Smirnoff static uint32_t
5573f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
55895d67482SBill Paul {
55995d67482SBill Paul 	device_t dev;
56095d67482SBill Paul 
56195d67482SBill Paul 	dev = sc->bge_dev;
56295d67482SBill Paul 
56395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
56495d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
56595d67482SBill Paul }
56695d67482SBill Paul #endif
56795d67482SBill Paul 
56895d67482SBill Paul static void
5693f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
57095d67482SBill Paul {
57195d67482SBill Paul 	device_t dev;
57295d67482SBill Paul 
57395d67482SBill Paul 	dev = sc->bge_dev;
57495d67482SBill Paul 
57595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
57695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
57795d67482SBill Paul }
57895d67482SBill Paul 
5796f8718a3SScott Long static void
5806f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
5816f8718a3SScott Long {
5826f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
5836f8718a3SScott Long }
5846f8718a3SScott Long 
58538cc658fSJohn Baldwin static void
58638cc658fSJohn Baldwin bge_writembx(struct bge_softc *sc, int off, int val)
58738cc658fSJohn Baldwin {
58838cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
58938cc658fSJohn Baldwin 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
59038cc658fSJohn Baldwin 
59138cc658fSJohn Baldwin 	CSR_WRITE_4(sc, off, val);
59238cc658fSJohn Baldwin }
59338cc658fSJohn Baldwin 
594f41ac2beSBill Paul /*
595f41ac2beSBill Paul  * Map a single buffer address.
596f41ac2beSBill Paul  */
597f41ac2beSBill Paul 
598f41ac2beSBill Paul static void
5993f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
600f41ac2beSBill Paul {
601f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
602f41ac2beSBill Paul 
603f41ac2beSBill Paul 	if (error)
604f41ac2beSBill Paul 		return;
605f41ac2beSBill Paul 
606f41ac2beSBill Paul 	ctx = arg;
607f41ac2beSBill Paul 
608f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
609f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
610f41ac2beSBill Paul 		return;
611f41ac2beSBill Paul 	}
612f41ac2beSBill Paul 
613f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
614f41ac2beSBill Paul }
615f41ac2beSBill Paul 
61638cc658fSJohn Baldwin static uint8_t
61738cc658fSJohn Baldwin bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
61838cc658fSJohn Baldwin {
61938cc658fSJohn Baldwin 	uint32_t access, byte = 0;
62038cc658fSJohn Baldwin 	int i;
62138cc658fSJohn Baldwin 
62238cc658fSJohn Baldwin 	/* Lock. */
62338cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
62438cc658fSJohn Baldwin 	for (i = 0; i < 8000; i++) {
62538cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
62638cc658fSJohn Baldwin 			break;
62738cc658fSJohn Baldwin 		DELAY(20);
62838cc658fSJohn Baldwin 	}
62938cc658fSJohn Baldwin 	if (i == 8000)
63038cc658fSJohn Baldwin 		return (1);
63138cc658fSJohn Baldwin 
63238cc658fSJohn Baldwin 	/* Enable access. */
63338cc658fSJohn Baldwin 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
63438cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
63538cc658fSJohn Baldwin 
63638cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
63738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
63838cc658fSJohn Baldwin 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
63938cc658fSJohn Baldwin 		DELAY(10);
64038cc658fSJohn Baldwin 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
64138cc658fSJohn Baldwin 			DELAY(10);
64238cc658fSJohn Baldwin 			break;
64338cc658fSJohn Baldwin 		}
64438cc658fSJohn Baldwin 	}
64538cc658fSJohn Baldwin 
64638cc658fSJohn Baldwin 	if (i == BGE_TIMEOUT * 10) {
64738cc658fSJohn Baldwin 		if_printf(sc->bge_ifp, "nvram read timed out\n");
64838cc658fSJohn Baldwin 		return (1);
64938cc658fSJohn Baldwin 	}
65038cc658fSJohn Baldwin 
65138cc658fSJohn Baldwin 	/* Get result. */
65238cc658fSJohn Baldwin 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
65338cc658fSJohn Baldwin 
65438cc658fSJohn Baldwin 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
65538cc658fSJohn Baldwin 
65638cc658fSJohn Baldwin 	/* Disable access. */
65738cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
65838cc658fSJohn Baldwin 
65938cc658fSJohn Baldwin 	/* Unlock. */
66038cc658fSJohn Baldwin 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
66138cc658fSJohn Baldwin 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
66238cc658fSJohn Baldwin 
66338cc658fSJohn Baldwin 	return (0);
66438cc658fSJohn Baldwin }
66538cc658fSJohn Baldwin 
66638cc658fSJohn Baldwin /*
66738cc658fSJohn Baldwin  * Read a sequence of bytes from NVRAM.
66838cc658fSJohn Baldwin  */
66938cc658fSJohn Baldwin static int
67038cc658fSJohn Baldwin bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
67138cc658fSJohn Baldwin {
67238cc658fSJohn Baldwin 	int err = 0, i;
67338cc658fSJohn Baldwin 	uint8_t byte = 0;
67438cc658fSJohn Baldwin 
67538cc658fSJohn Baldwin 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
67638cc658fSJohn Baldwin 		return (1);
67738cc658fSJohn Baldwin 
67838cc658fSJohn Baldwin 	for (i = 0; i < cnt; i++) {
67938cc658fSJohn Baldwin 		err = bge_nvram_getbyte(sc, off + i, &byte);
68038cc658fSJohn Baldwin 		if (err)
68138cc658fSJohn Baldwin 			break;
68238cc658fSJohn Baldwin 		*(dest + i) = byte;
68338cc658fSJohn Baldwin 	}
68438cc658fSJohn Baldwin 
68538cc658fSJohn Baldwin 	return (err ? 1 : 0);
68638cc658fSJohn Baldwin }
68738cc658fSJohn Baldwin 
68895d67482SBill Paul /*
68995d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
69095d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
69195d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
69295d67482SBill Paul  * access method.
69395d67482SBill Paul  */
6943f74909aSGleb Smirnoff static uint8_t
6953f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
69695d67482SBill Paul {
69795d67482SBill Paul 	int i;
6983f74909aSGleb Smirnoff 	uint32_t byte = 0;
69995d67482SBill Paul 
70095d67482SBill Paul 	/*
70195d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
70295d67482SBill Paul 	 * having to use the bitbang method.
70395d67482SBill Paul 	 */
70495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
70595d67482SBill Paul 
70695d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
70795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
70895d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
70995d67482SBill Paul 	DELAY(20);
71095d67482SBill Paul 
71195d67482SBill Paul 	/* Issue the read EEPROM command. */
71295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
71395d67482SBill Paul 
71495d67482SBill Paul 	/* Wait for completion */
71595d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
71695d67482SBill Paul 		DELAY(10);
71795d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
71895d67482SBill Paul 			break;
71995d67482SBill Paul 	}
72095d67482SBill Paul 
721d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
722fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
723f6789fbaSPyun YongHyeon 		return (1);
72495d67482SBill Paul 	}
72595d67482SBill Paul 
72695d67482SBill Paul 	/* Get result. */
72795d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
72895d67482SBill Paul 
7290c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
73095d67482SBill Paul 
73195d67482SBill Paul 	return (0);
73295d67482SBill Paul }
73395d67482SBill Paul 
73495d67482SBill Paul /*
73595d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
73695d67482SBill Paul  */
73795d67482SBill Paul static int
7383f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
73995d67482SBill Paul {
7403f74909aSGleb Smirnoff 	int i, error = 0;
7413f74909aSGleb Smirnoff 	uint8_t byte = 0;
74295d67482SBill Paul 
74395d67482SBill Paul 	for (i = 0; i < cnt; i++) {
7443f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
7453f74909aSGleb Smirnoff 		if (error)
74695d67482SBill Paul 			break;
74795d67482SBill Paul 		*(dest + i) = byte;
74895d67482SBill Paul 	}
74995d67482SBill Paul 
7503f74909aSGleb Smirnoff 	return (error ? 1 : 0);
75195d67482SBill Paul }
75295d67482SBill Paul 
75395d67482SBill Paul static int
7543f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
75595d67482SBill Paul {
75695d67482SBill Paul 	struct bge_softc *sc;
7573f74909aSGleb Smirnoff 	uint32_t val, autopoll;
75895d67482SBill Paul 	int i;
75995d67482SBill Paul 
76095d67482SBill Paul 	sc = device_get_softc(dev);
76195d67482SBill Paul 
7620434d1b8SBill Paul 	/*
7630434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
7640434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
7650434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
7660434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
7670434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
7680434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
7690434d1b8SBill Paul 	 * special-cased.
7700434d1b8SBill Paul 	 */
771b1265c1aSJohn Polstra 	if (phy != 1)
77298b28ee5SBill Paul 		return (0);
77398b28ee5SBill Paul 
77437ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
77537ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
77637ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
77737ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
77837ceeb4dSPaul Saab 		DELAY(40);
77937ceeb4dSPaul Saab 	}
78037ceeb4dSPaul Saab 
78195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
78295d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
78395d67482SBill Paul 
78495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
785d5d23857SJung-uk Kim 		DELAY(10);
78695d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
78795d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
78895d67482SBill Paul 			break;
78995d67482SBill Paul 	}
79095d67482SBill Paul 
79195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
7925fea260fSMarius Strobl 		device_printf(sc->bge_dev,
7935fea260fSMarius Strobl 		    "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
7945fea260fSMarius Strobl 		    phy, reg, val);
79537ceeb4dSPaul Saab 		val = 0;
79637ceeb4dSPaul Saab 		goto done;
79795d67482SBill Paul 	}
79895d67482SBill Paul 
79938cc658fSJohn Baldwin 	DELAY(5);
80095d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
80195d67482SBill Paul 
80237ceeb4dSPaul Saab done:
80337ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
80437ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
80537ceeb4dSPaul Saab 		DELAY(40);
80637ceeb4dSPaul Saab 	}
80737ceeb4dSPaul Saab 
80895d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
80995d67482SBill Paul 		return (0);
81095d67482SBill Paul 
8110c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
81295d67482SBill Paul }
81395d67482SBill Paul 
81495d67482SBill Paul static int
8153f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
81695d67482SBill Paul {
81795d67482SBill Paul 	struct bge_softc *sc;
8183f74909aSGleb Smirnoff 	uint32_t autopoll;
81995d67482SBill Paul 	int i;
82095d67482SBill Paul 
82195d67482SBill Paul 	sc = device_get_softc(dev);
82295d67482SBill Paul 
82338cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
82438cc658fSJohn Baldwin 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
82538cc658fSJohn Baldwin 		return(0);
82638cc658fSJohn Baldwin 
82737ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
82837ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
82937ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
83037ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
83137ceeb4dSPaul Saab 		DELAY(40);
83237ceeb4dSPaul Saab 	}
83337ceeb4dSPaul Saab 
83495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
83595d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
83695d67482SBill Paul 
83795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
838d5d23857SJung-uk Kim 		DELAY(10);
83938cc658fSJohn Baldwin 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
84038cc658fSJohn Baldwin 			DELAY(5);
84138cc658fSJohn Baldwin 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
84295d67482SBill Paul 			break;
843d5d23857SJung-uk Kim 		}
84438cc658fSJohn Baldwin 	}
845d5d23857SJung-uk Kim 
846d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT) {
84738cc658fSJohn Baldwin 		device_printf(sc->bge_dev,
84838cc658fSJohn Baldwin 		    "PHY write timed out (phy %d, reg %d, val %d)\n",
84938cc658fSJohn Baldwin 		    phy, reg, val);
850d5d23857SJung-uk Kim 		return (0);
85195d67482SBill Paul 	}
85295d67482SBill Paul 
85337ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
85437ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
85537ceeb4dSPaul Saab 		DELAY(40);
85637ceeb4dSPaul Saab 	}
85737ceeb4dSPaul Saab 
85895d67482SBill Paul 	return (0);
85995d67482SBill Paul }
86095d67482SBill Paul 
86195d67482SBill Paul static void
8623f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
86395d67482SBill Paul {
86495d67482SBill Paul 	struct bge_softc *sc;
86595d67482SBill Paul 	struct mii_data *mii;
86695d67482SBill Paul 	sc = device_get_softc(dev);
86795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
86895d67482SBill Paul 
86995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
8703f74909aSGleb Smirnoff 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)
87195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
8723f74909aSGleb Smirnoff 	else
87395d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
87495d67482SBill Paul 
8753f74909aSGleb Smirnoff 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
87695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
8773f74909aSGleb Smirnoff 	else
87895d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
87995d67482SBill Paul }
88095d67482SBill Paul 
88195d67482SBill Paul /*
88295d67482SBill Paul  * Intialize a standard receive ring descriptor.
88395d67482SBill Paul  */
88495d67482SBill Paul static int
8853f74909aSGleb Smirnoff bge_newbuf_std(struct bge_softc *sc, int i, struct mbuf *m)
88695d67482SBill Paul {
88795d67482SBill Paul 	struct mbuf *m_new = NULL;
88895d67482SBill Paul 	struct bge_rx_bd *r;
889f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
890f41ac2beSBill Paul 	int error;
89195d67482SBill Paul 
89295d67482SBill Paul 	if (m == NULL) {
893c3a56752SGleb Smirnoff 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
894c3a56752SGleb Smirnoff 		if (m_new == NULL)
89595d67482SBill Paul 			return (ENOBUFS);
89695d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
89795d67482SBill Paul 	} else {
89895d67482SBill Paul 		m_new = m;
89995d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
90095d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
90195d67482SBill Paul 	}
90295d67482SBill Paul 
903652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
90495d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
90595d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
906f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
907f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
908f41ac2beSBill Paul 	ctx.sc = sc;
909f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
910f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
911f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
912f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
913f7cea149SGleb Smirnoff 		if (m == NULL) {
914f7cea149SGleb Smirnoff 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
915f41ac2beSBill Paul 			m_freem(m_new);
916f7cea149SGleb Smirnoff 		}
917f41ac2beSBill Paul 		return (ENOMEM);
918f41ac2beSBill Paul 	}
919e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(ctx.bge_busaddr);
920e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(ctx.bge_busaddr);
921e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
922e907febfSPyun YongHyeon 	r->bge_len = m_new->m_len;
923e907febfSPyun YongHyeon 	r->bge_idx = i;
924f41ac2beSBill Paul 
925f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
926f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
927f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
92895d67482SBill Paul 
92995d67482SBill Paul 	return (0);
93095d67482SBill Paul }
93195d67482SBill Paul 
93295d67482SBill Paul /*
93395d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
93495d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
93595d67482SBill Paul  */
93695d67482SBill Paul static int
9373f74909aSGleb Smirnoff bge_newbuf_jumbo(struct bge_softc *sc, int i, struct mbuf *m)
93895d67482SBill Paul {
9391be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
9401be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
94195d67482SBill Paul 	struct mbuf *m_new = NULL;
9421be6acb7SGleb Smirnoff 	int nsegs;
943f41ac2beSBill Paul 	int error;
94495d67482SBill Paul 
94595d67482SBill Paul 	if (m == NULL) {
946a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
9471be6acb7SGleb Smirnoff 		if (m_new == NULL)
94895d67482SBill Paul 			return (ENOBUFS);
94995d67482SBill Paul 
9501be6acb7SGleb Smirnoff 		m_cljget(m_new, M_DONTWAIT, MJUM9BYTES);
9511be6acb7SGleb Smirnoff 		if (!(m_new->m_flags & M_EXT)) {
95295d67482SBill Paul 			m_freem(m_new);
95395d67482SBill Paul 			return (ENOBUFS);
95495d67482SBill Paul 		}
9551be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
95695d67482SBill Paul 	} else {
95795d67482SBill Paul 		m_new = m;
9581be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
95995d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
96095d67482SBill Paul 	}
96195d67482SBill Paul 
962652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
96395d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
9641be6acb7SGleb Smirnoff 
9651be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
9661be6acb7SGleb Smirnoff 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
9671be6acb7SGleb Smirnoff 	    m_new, segs, &nsegs, BUS_DMA_NOWAIT);
9681be6acb7SGleb Smirnoff 	if (error) {
9691be6acb7SGleb Smirnoff 		if (m == NULL)
970f41ac2beSBill Paul 			m_freem(m_new);
9711be6acb7SGleb Smirnoff 		return (error);
972f7cea149SGleb Smirnoff 	}
9731be6acb7SGleb Smirnoff 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
9741be6acb7SGleb Smirnoff 
9751be6acb7SGleb Smirnoff 	/*
9761be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
9771be6acb7SGleb Smirnoff 	 */
9781be6acb7SGleb Smirnoff 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
9794e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
9804e7ba1abSGleb Smirnoff 	r->bge_idx = i;
9814e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
9824e7ba1abSGleb Smirnoff 	switch (nsegs) {
9834e7ba1abSGleb Smirnoff 	case 4:
9844e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
9854e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
9864e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
9874e7ba1abSGleb Smirnoff 	case 3:
988e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
989e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
990e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
9914e7ba1abSGleb Smirnoff 	case 2:
9924e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
9934e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
9944e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
9954e7ba1abSGleb Smirnoff 	case 1:
9964e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
9974e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
9984e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
9994e7ba1abSGleb Smirnoff 		break;
10004e7ba1abSGleb Smirnoff 	default:
10014e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
10024e7ba1abSGleb Smirnoff 	}
1003f41ac2beSBill Paul 
1004f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
1005f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1006f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
100795d67482SBill Paul 
100895d67482SBill Paul 	return (0);
100995d67482SBill Paul }
101095d67482SBill Paul 
101195d67482SBill Paul /*
101295d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
101395d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
101495d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
101595d67482SBill Paul  * the NIC.
101695d67482SBill Paul  */
101795d67482SBill Paul static int
10183f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
101995d67482SBill Paul {
102095d67482SBill Paul 	int i;
102195d67482SBill Paul 
102295d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
102395d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
102495d67482SBill Paul 			return (ENOBUFS);
102595d67482SBill Paul 	};
102695d67482SBill Paul 
1027f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1028f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
1029f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1030f41ac2beSBill Paul 
103195d67482SBill Paul 	sc->bge_std = i - 1;
103238cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
103395d67482SBill Paul 
103495d67482SBill Paul 	return (0);
103595d67482SBill Paul }
103695d67482SBill Paul 
103795d67482SBill Paul static void
10383f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
103995d67482SBill Paul {
104095d67482SBill Paul 	int i;
104195d67482SBill Paul 
104295d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
104395d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1044e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
1045e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1046e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1047f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
1048f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1049e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1050e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
105195d67482SBill Paul 		}
1052f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
105395d67482SBill Paul 		    sizeof(struct bge_rx_bd));
105495d67482SBill Paul 	}
105595d67482SBill Paul }
105695d67482SBill Paul 
105795d67482SBill Paul static int
10583f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
105995d67482SBill Paul {
106095d67482SBill Paul 	struct bge_rcb *rcb;
10611be6acb7SGleb Smirnoff 	int i;
106295d67482SBill Paul 
106395d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
106495d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
106595d67482SBill Paul 			return (ENOBUFS);
106695d67482SBill Paul 	};
106795d67482SBill Paul 
1068f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1069f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
1070f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1071f41ac2beSBill Paul 
107295d67482SBill Paul 	sc->bge_jumbo = i - 1;
107395d67482SBill Paul 
1074f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
10751be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
10761be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
107767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
107895d67482SBill Paul 
107938cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
108095d67482SBill Paul 
108195d67482SBill Paul 	return (0);
108295d67482SBill Paul }
108395d67482SBill Paul 
108495d67482SBill Paul static void
10853f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
108695d67482SBill Paul {
108795d67482SBill Paul 	int i;
108895d67482SBill Paul 
108995d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
109095d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1091e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1092e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1093e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
1094f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1095f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1096e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1097e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
109895d67482SBill Paul 		}
1099f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
11001be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
110195d67482SBill Paul 	}
110295d67482SBill Paul }
110395d67482SBill Paul 
110495d67482SBill Paul static void
11053f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
110695d67482SBill Paul {
110795d67482SBill Paul 	int i;
110895d67482SBill Paul 
1109f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
111095d67482SBill Paul 		return;
111195d67482SBill Paul 
111295d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
111395d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
1114e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
1115e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
1116e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
1117f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
1118f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1119e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1120e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
112195d67482SBill Paul 		}
1122f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
112395d67482SBill Paul 		    sizeof(struct bge_tx_bd));
112495d67482SBill Paul 	}
112595d67482SBill Paul }
112695d67482SBill Paul 
112795d67482SBill Paul static int
11283f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
112995d67482SBill Paul {
113095d67482SBill Paul 	sc->bge_txcnt = 0;
113195d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
11323927098fSPaul Saab 
113314bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
113414bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
113538cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
113614bbd30fSGleb Smirnoff 
11373927098fSPaul Saab 	/* 5700 b2 errata */
1138e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
113938cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
11403927098fSPaul Saab 
114114bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
114238cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
11433927098fSPaul Saab 	/* 5700 b2 errata */
1144e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
114538cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
114695d67482SBill Paul 
114795d67482SBill Paul 	return (0);
114895d67482SBill Paul }
114995d67482SBill Paul 
115095d67482SBill Paul static void
11513e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
11523e9b1bcaSJung-uk Kim {
11533e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
11543e9b1bcaSJung-uk Kim 
11553e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
11563e9b1bcaSJung-uk Kim 
11573e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
11583e9b1bcaSJung-uk Kim 
115945ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
11603e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
116145ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
11623e9b1bcaSJung-uk Kim 	else
116345ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
11643e9b1bcaSJung-uk Kim }
11653e9b1bcaSJung-uk Kim 
11663e9b1bcaSJung-uk Kim static void
11673f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
116895d67482SBill Paul {
116995d67482SBill Paul 	struct ifnet *ifp;
117095d67482SBill Paul 	struct ifmultiaddr *ifma;
11713f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
117295d67482SBill Paul 	int h, i;
117395d67482SBill Paul 
11740f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
11750f9bd73bSSam Leffler 
1176fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
117795d67482SBill Paul 
117895d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
117995d67482SBill Paul 		for (i = 0; i < 4; i++)
11800c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
118195d67482SBill Paul 		return;
118295d67482SBill Paul 	}
118395d67482SBill Paul 
118495d67482SBill Paul 	/* First, zot all the existing filters. */
118595d67482SBill Paul 	for (i = 0; i < 4; i++)
118695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
118795d67482SBill Paul 
118895d67482SBill Paul 	/* Now program new ones. */
118913b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
119095d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
119195d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
119295d67482SBill Paul 			continue;
11930e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
11940c8aa4eaSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
11950c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
119695d67482SBill Paul 	}
119713b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
119895d67482SBill Paul 
119995d67482SBill Paul 	for (i = 0; i < 4; i++)
120095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
120195d67482SBill Paul }
120295d67482SBill Paul 
12038cb1383cSDoug Ambrisko static void
1204cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1205cb2eacc7SYaroslav Tykhiy {
1206cb2eacc7SYaroslav Tykhiy 	struct ifnet *ifp;
1207cb2eacc7SYaroslav Tykhiy 
1208cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1209cb2eacc7SYaroslav Tykhiy 
1210cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1211cb2eacc7SYaroslav Tykhiy 
1212cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1213cb2eacc7SYaroslav Tykhiy 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1214cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1215cb2eacc7SYaroslav Tykhiy 	else
1216cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1217cb2eacc7SYaroslav Tykhiy }
1218cb2eacc7SYaroslav Tykhiy 
1219cb2eacc7SYaroslav Tykhiy static void
12208cb1383cSDoug Ambrisko bge_sig_pre_reset(sc, type)
12218cb1383cSDoug Ambrisko 	struct bge_softc *sc;
12228cb1383cSDoug Ambrisko 	int type;
12238cb1383cSDoug Ambrisko {
12248cb1383cSDoug Ambrisko 	/*
12258cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
12268cb1383cSDoug Ambrisko 	 */
12278cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
12288cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
12298cb1383cSDoug Ambrisko 
12308cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
12318cb1383cSDoug Ambrisko 		switch (type) {
12328cb1383cSDoug Ambrisko 		case BGE_RESET_START:
12338cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
12348cb1383cSDoug Ambrisko 			break;
12358cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
12368cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
12378cb1383cSDoug Ambrisko 			break;
12388cb1383cSDoug Ambrisko 		}
12398cb1383cSDoug Ambrisko 	}
12408cb1383cSDoug Ambrisko }
12418cb1383cSDoug Ambrisko 
12428cb1383cSDoug Ambrisko static void
12438cb1383cSDoug Ambrisko bge_sig_post_reset(sc, type)
12448cb1383cSDoug Ambrisko 	struct bge_softc *sc;
12458cb1383cSDoug Ambrisko 	int type;
12468cb1383cSDoug Ambrisko {
12478cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
12488cb1383cSDoug Ambrisko 		switch (type) {
12498cb1383cSDoug Ambrisko 		case BGE_RESET_START:
12508cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000001);
12518cb1383cSDoug Ambrisko 			/* START DONE */
12528cb1383cSDoug Ambrisko 			break;
12538cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
12548cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000002);
12558cb1383cSDoug Ambrisko 			break;
12568cb1383cSDoug Ambrisko 		}
12578cb1383cSDoug Ambrisko 	}
12588cb1383cSDoug Ambrisko }
12598cb1383cSDoug Ambrisko 
12608cb1383cSDoug Ambrisko static void
12618cb1383cSDoug Ambrisko bge_sig_legacy(sc, type)
12628cb1383cSDoug Ambrisko 	struct bge_softc *sc;
12638cb1383cSDoug Ambrisko 	int type;
12648cb1383cSDoug Ambrisko {
12658cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
12668cb1383cSDoug Ambrisko 		switch (type) {
12678cb1383cSDoug Ambrisko 		case BGE_RESET_START:
12688cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
12698cb1383cSDoug Ambrisko 			break;
12708cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
12718cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
12728cb1383cSDoug Ambrisko 			break;
12738cb1383cSDoug Ambrisko 		}
12748cb1383cSDoug Ambrisko 	}
12758cb1383cSDoug Ambrisko }
12768cb1383cSDoug Ambrisko 
12778cb1383cSDoug Ambrisko void bge_stop_fw(struct bge_softc *);
12788cb1383cSDoug Ambrisko void
12798cb1383cSDoug Ambrisko bge_stop_fw(sc)
12808cb1383cSDoug Ambrisko 	struct bge_softc *sc;
12818cb1383cSDoug Ambrisko {
12828cb1383cSDoug Ambrisko 	int i;
12838cb1383cSDoug Ambrisko 
12848cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
12858cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW, BGE_FW_PAUSE);
12868cb1383cSDoug Ambrisko 		CSR_WRITE_4(sc, BGE_CPU_EVENT,
128739153c5aSJung-uk Kim 		    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
12888cb1383cSDoug Ambrisko 
12898cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
12908cb1383cSDoug Ambrisko 			if (!(CSR_READ_4(sc, BGE_CPU_EVENT) & (1 << 14)))
12918cb1383cSDoug Ambrisko 				break;
12928cb1383cSDoug Ambrisko 			DELAY(10);
12938cb1383cSDoug Ambrisko 		}
12948cb1383cSDoug Ambrisko 	}
12958cb1383cSDoug Ambrisko }
12968cb1383cSDoug Ambrisko 
129795d67482SBill Paul /*
129895d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
129995d67482SBill Paul  * self-test results.
130095d67482SBill Paul  */
130195d67482SBill Paul static int
13023f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
130395d67482SBill Paul {
13043f74909aSGleb Smirnoff 	uint32_t dma_rw_ctl;
130595d67482SBill Paul 	int i;
130695d67482SBill Paul 
13078cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
1308e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
130995d67482SBill Paul 
131095d67482SBill Paul 	/* Clear the MAC control register */
131195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
131295d67482SBill Paul 
131395d67482SBill Paul 	/*
131495d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
131595d67482SBill Paul 	 * internal memory.
131695d67482SBill Paul 	 */
131795d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
13183f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
131995d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
132095d67482SBill Paul 
132195d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
13223f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
132395d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
132495d67482SBill Paul 
1325186f842bSJung-uk Kim 	/*
1326186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1327186f842bSJung-uk Kim 	 */
1328186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1329186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1330652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
1331186f842bSJung-uk Kim 		/* Read watermark not used, 128 bytes for write. */
1332186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1333652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
13344c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1335186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1336186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1337186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1338186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1339186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1340186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1341186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1342186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1343186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1344186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1345186f842bSJung-uk Kim 		} else {
1346186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1347186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1348186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
13490c8aa4eaSJung-uk Kim 			    0x0F;
1350186f842bSJung-uk Kim 		}
1351e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1352e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
13533f74909aSGleb Smirnoff 			uint32_t tmp;
13545cba12d3SPaul Saab 
1355186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
13560c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1357186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1358186f842bSJung-uk Kim 				dma_rw_ctl |=
1359186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
13605cba12d3SPaul Saab 
1361186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1362186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1363186f842bSJung-uk Kim 		}
1364186f842bSJung-uk Kim 	} else {
1365186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1366186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1367186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1368186f842bSJung-uk Kim 
1369186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1370186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1371186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1372186f842bSJung-uk Kim 	}
1373186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1374186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1375186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1376186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1377e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1378186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
13795cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
13805cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
138195d67482SBill Paul 
138295d67482SBill Paul 	/*
138395d67482SBill Paul 	 * Set up general mode register.
138495d67482SBill Paul 	 */
1385e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
138695d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1387ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
138895d67482SBill Paul 
138995d67482SBill Paul 	/*
139090447aadSMarius Strobl 	 * BCM5701 B5 have a bug causing data corruption when using
139190447aadSMarius Strobl 	 * 64-bit DMA reads, which can be terminated early and then
139290447aadSMarius Strobl 	 * completed later as 32-bit accesses, in combination with
139390447aadSMarius Strobl 	 * certain bridges.
139490447aadSMarius Strobl 	 */
139590447aadSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
139690447aadSMarius Strobl 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
139790447aadSMarius Strobl 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_FORCE_PCI32);
139890447aadSMarius Strobl 
139990447aadSMarius Strobl 	/*
14008cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
14018cb1383cSDoug Ambrisko 	 */
14028cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
14038cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
14048cb1383cSDoug Ambrisko 
14058cb1383cSDoug Ambrisko 	/*
1406ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1407ea13bdd5SJohn Polstra 	 * properly by these devices.
140895d67482SBill Paul 	 */
1409ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
141095d67482SBill Paul 
141195d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
14120c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
141395d67482SBill Paul 
141438cc658fSJohn Baldwin 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
141538cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
141638cc658fSJohn Baldwin 		DELAY(40);	/* XXX */
141738cc658fSJohn Baldwin 
141838cc658fSJohn Baldwin 		/* Put PHY into ready state */
141938cc658fSJohn Baldwin 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
142038cc658fSJohn Baldwin 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
142138cc658fSJohn Baldwin 		DELAY(40);
142238cc658fSJohn Baldwin 	}
142338cc658fSJohn Baldwin 
142495d67482SBill Paul 	return (0);
142595d67482SBill Paul }
142695d67482SBill Paul 
142795d67482SBill Paul static int
14283f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
142995d67482SBill Paul {
143095d67482SBill Paul 	struct bge_rcb *rcb;
1431e907febfSPyun YongHyeon 	bus_size_t vrcb;
1432e907febfSPyun YongHyeon 	bge_hostaddr taddr;
14336f8718a3SScott Long 	uint32_t val;
143495d67482SBill Paul 	int i;
143595d67482SBill Paul 
143695d67482SBill Paul 	/*
143795d67482SBill Paul 	 * Initialize the memory window pointer register so that
143895d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
143995d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
144095d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
144195d67482SBill Paul 	 */
144295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
144395d67482SBill Paul 
1444822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1445822f63fcSBill Paul 
14467ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
144795d67482SBill Paul 		/* Configure mbuf memory pool */
14480dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1449822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1450822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1451822f63fcSBill Paul 		else
145295d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
145395d67482SBill Paul 
145495d67482SBill Paul 		/* Configure DMA resource pool */
14550434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
14560434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
145795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
14580434d1b8SBill Paul 	}
145995d67482SBill Paul 
146095d67482SBill Paul 	/* Configure mbuf pool watermarks */
146138cc658fSJohn Baldwin 	if (!BGE_IS_5705_PLUS(sc)) {
1462fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1463fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1464fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
146538cc658fSJohn Baldwin 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
146638cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
146738cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
146838cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
146938cc658fSJohn Baldwin 	} else {
147038cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
147138cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
147238cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
147338cc658fSJohn Baldwin 	}
147495d67482SBill Paul 
147595d67482SBill Paul 	/* Configure DMA resource watermarks */
147695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
147795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
147895d67482SBill Paul 
147995d67482SBill Paul 	/* Enable buffer manager */
14807ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
148195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
148295d67482SBill Paul 		    BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN);
148395d67482SBill Paul 
148495d67482SBill Paul 		/* Poll for buffer manager start indication */
148595d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
1486d5d23857SJung-uk Kim 			DELAY(10);
14870c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
148895d67482SBill Paul 				break;
148995d67482SBill Paul 		}
149095d67482SBill Paul 
149195d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1492fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1493fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
149495d67482SBill Paul 			return (ENXIO);
149595d67482SBill Paul 		}
14960434d1b8SBill Paul 	}
149795d67482SBill Paul 
149895d67482SBill Paul 	/* Enable flow-through queues */
14990c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
150095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
150195d67482SBill Paul 
150295d67482SBill Paul 	/* Wait until queue initialization is complete */
150395d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1504d5d23857SJung-uk Kim 		DELAY(10);
150595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
150695d67482SBill Paul 			break;
150795d67482SBill Paul 	}
150895d67482SBill Paul 
150995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1510fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
151195d67482SBill Paul 		return (ENXIO);
151295d67482SBill Paul 	}
151395d67482SBill Paul 
151495d67482SBill Paul 	/* Initialize the standard RX ring control block */
1515f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1516f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1517f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1518f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1519f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1520f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1521f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
15227ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
15230434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
15240434d1b8SBill Paul 	else
15250434d1b8SBill Paul 		rcb->bge_maxlen_flags =
15260434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
152795d67482SBill Paul 	rcb->bge_nicaddr = BGE_STD_RX_RINGS;
15280c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
15290c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1530f41ac2beSBill Paul 
153167111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
153267111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
153395d67482SBill Paul 
153495d67482SBill Paul 	/*
153595d67482SBill Paul 	 * Initialize the jumbo RX ring control block
153695d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
153795d67482SBill Paul 	 * field until we're actually ready to start
153895d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
153995d67482SBill Paul 	 * high enough to require it).
154095d67482SBill Paul 	 */
15414c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1542f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1543f41ac2beSBill Paul 
1544f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1545f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1546f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1547f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1548f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1549f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1550f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
15511be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
15521be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
155395d67482SBill Paul 		rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
155467111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
155567111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
155667111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
155767111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1558f41ac2beSBill Paul 
15590434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
15600434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
156167111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
156295d67482SBill Paul 
156395d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1564f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
156567111612SJohn Polstra 		rcb->bge_maxlen_flags =
156667111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
15670434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
15680434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
15690434d1b8SBill Paul 	}
157095d67482SBill Paul 
157195d67482SBill Paul 	/*
157295d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
157395d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
157495d67482SBill Paul 	 * each ring.
15759ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
15769ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
15779ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
15789ba784dbSScott Long 	 * are reports that it might not need to be so strict.
157938cc658fSJohn Baldwin 	 *
158038cc658fSJohn Baldwin 	 * XXX Linux does some extra fiddling here for the 5906 parts as
158138cc658fSJohn Baldwin 	 * well.
158295d67482SBill Paul 	 */
15835345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
15846f8718a3SScott Long 		val = 8;
15856f8718a3SScott Long 	else
15866f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
15876f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
158895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
158995d67482SBill Paul 
159095d67482SBill Paul 	/*
159195d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
159295d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
159395d67482SBill Paul 	 * These are located in NIC memory.
159495d67482SBill Paul 	 */
1595e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
159695d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1597e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1598e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1599e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1600e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
160195d67482SBill Paul 	}
160295d67482SBill Paul 
160395d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
1604e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1605e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1606e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1607e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1608e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1609e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
16107ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
1611e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1612e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
161395d67482SBill Paul 
161495d67482SBill Paul 	/* Disable all unused RX return rings */
1615e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
161695d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1617e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1618e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1619e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
16200434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1621e907febfSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED));
1622e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
162338cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
16243f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1625e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
162695d67482SBill Paul 	}
162795d67482SBill Paul 
162895d67482SBill Paul 	/* Initialize RX ring indexes */
162938cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
163038cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
163138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
163295d67482SBill Paul 
163395d67482SBill Paul 	/*
163495d67482SBill Paul 	 * Set up RX return ring 0
163595d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
163695d67482SBill Paul 	 * The return rings live entirely within the host, so the
163795d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
163895d67482SBill Paul 	 */
1639e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1640e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1641e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1642e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1643e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1644e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1645e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
164695d67482SBill Paul 
164795d67482SBill Paul 	/* Set random backoff seed for TX */
164895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
16494a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
16504a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
16514a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
165295d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
165395d67482SBill Paul 
165495d67482SBill Paul 	/* Set inter-packet gap */
165595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
165695d67482SBill Paul 
165795d67482SBill Paul 	/*
165895d67482SBill Paul 	 * Specify which ring to use for packets that don't match
165995d67482SBill Paul 	 * any RX rules.
166095d67482SBill Paul 	 */
166195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
166295d67482SBill Paul 
166395d67482SBill Paul 	/*
166495d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
166595d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
166695d67482SBill Paul 	 */
166795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
166895d67482SBill Paul 
166995d67482SBill Paul 	/* Inialize RX list placement stats mask. */
16700c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
167195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
167295d67482SBill Paul 
167395d67482SBill Paul 	/* Disable host coalescing until we get it set up */
167495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
167595d67482SBill Paul 
167695d67482SBill Paul 	/* Poll to make sure it's shut down. */
167795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1678d5d23857SJung-uk Kim 		DELAY(10);
167995d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
168095d67482SBill Paul 			break;
168195d67482SBill Paul 	}
168295d67482SBill Paul 
168395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1684fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1685fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
168695d67482SBill Paul 		return (ENXIO);
168795d67482SBill Paul 	}
168895d67482SBill Paul 
168995d67482SBill Paul 	/* Set up host coalescing defaults */
169095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
169195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
169295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
169395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
16947ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
169595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
169695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
16970434d1b8SBill Paul 	}
1698b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
1699b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
170095d67482SBill Paul 
170195d67482SBill Paul 	/* Set up address of statistics block */
17027ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
1703f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1704f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
170595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1706f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
17070434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
170895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
17090434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
17100434d1b8SBill Paul 	}
17110434d1b8SBill Paul 
17120434d1b8SBill Paul 	/* Set up address of status block */
1713f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1714f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
171595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1716f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1717f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1718f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
171995d67482SBill Paul 
172095d67482SBill Paul 	/* Turn on host coalescing state machine */
172195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
172295d67482SBill Paul 
172395d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
172495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
172595d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
172695d67482SBill Paul 
172795d67482SBill Paul 	/* Turn on RX list placement state machine */
172895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
172995d67482SBill Paul 
173095d67482SBill Paul 	/* Turn on RX list selector state machine. */
17317ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
173295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
173395d67482SBill Paul 
173495d67482SBill Paul 	/* Turn on DMA, clear stats */
173595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB |
173695d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB | BGE_MACMODE_RX_STATS_CLEAR |
173795d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR | BGE_MACMODE_RX_STATS_ENB |
173895d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB | BGE_MACMODE_FRMHDR_DMA_ENB |
1739652ae483SGleb Smirnoff 	    ((sc->bge_flags & BGE_FLAG_TBI) ?
1740652ae483SGleb Smirnoff 	    BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
174195d67482SBill Paul 
174295d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
174395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
174495d67482SBill Paul 
174595d67482SBill Paul #ifdef notdef
174695d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
174795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
174895d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
174995d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
175095d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
175195d67482SBill Paul #endif
175295d67482SBill Paul 
175395d67482SBill Paul 	/* Turn on DMA completion state machine */
17547ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
175595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
175695d67482SBill Paul 
17576f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
17586f8718a3SScott Long 
17596f8718a3SScott Long 	/* Enable host coalescing bug fix. */
17606f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
17616f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5787)
17620c8aa4eaSJung-uk Kim 		val |= 1 << 29;
17636f8718a3SScott Long 
176495d67482SBill Paul 	/* Turn on write DMA state machine */
17656f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
17664f09c4c7SMarius Strobl 	DELAY(40);
176795d67482SBill Paul 
176895d67482SBill Paul 	/* Turn on read DMA state machine */
17694f09c4c7SMarius Strobl 	val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
17704f09c4c7SMarius Strobl 	if (sc->bge_flags & BGE_FLAG_PCIE)
17714f09c4c7SMarius Strobl 		val |= BGE_RDMAMODE_FIFO_LONG_BURST;
17724f09c4c7SMarius Strobl 	CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
17734f09c4c7SMarius Strobl 	DELAY(40);
177495d67482SBill Paul 
177595d67482SBill Paul 	/* Turn on RX data completion state machine */
177695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
177795d67482SBill Paul 
177895d67482SBill Paul 	/* Turn on RX BD initiator state machine */
177995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
178095d67482SBill Paul 
178195d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
178295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
178395d67482SBill Paul 
178495d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
17857ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
178695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
178795d67482SBill Paul 
178895d67482SBill Paul 	/* Turn on send BD completion state machine */
178995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
179095d67482SBill Paul 
179195d67482SBill Paul 	/* Turn on send data completion state machine */
179295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
179395d67482SBill Paul 
179495d67482SBill Paul 	/* Turn on send data initiator state machine */
179595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
179695d67482SBill Paul 
179795d67482SBill Paul 	/* Turn on send BD initiator state machine */
179895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
179995d67482SBill Paul 
180095d67482SBill Paul 	/* Turn on send BD selector state machine */
180195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
180295d67482SBill Paul 
18030c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
180495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
180595d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
180695d67482SBill Paul 
180795d67482SBill Paul 	/* ack/clear link change events */
180895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
18090434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
18100434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1811f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
181295d67482SBill Paul 
181395d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
1814652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
181595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1816a1d52896SBill Paul 	} else {
18176098821cSJung-uk Kim 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL | (10 << 16));
18181f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
18194c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
1820a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1821a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1822a1d52896SBill Paul 	}
182395d67482SBill Paul 
18241f313773SOleg Bulyzhin 	/*
18251f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
18261f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
18271f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
18281f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
18291f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
18301f313773SOleg Bulyzhin 	 */
18311f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
18321f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
18331f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
18341f313773SOleg Bulyzhin 
183595d67482SBill Paul 	/* Enable link state change attentions. */
183695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
183795d67482SBill Paul 
183895d67482SBill Paul 	return (0);
183995d67482SBill Paul }
184095d67482SBill Paul 
18414c0da0ffSGleb Smirnoff const struct bge_revision *
18424c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
18434c0da0ffSGleb Smirnoff {
18444c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
18454c0da0ffSGleb Smirnoff 
18464c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
18474c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
18484c0da0ffSGleb Smirnoff 			return (br);
18494c0da0ffSGleb Smirnoff 	}
18504c0da0ffSGleb Smirnoff 
18514c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
18524c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
18534c0da0ffSGleb Smirnoff 			return (br);
18544c0da0ffSGleb Smirnoff 	}
18554c0da0ffSGleb Smirnoff 
18564c0da0ffSGleb Smirnoff 	return (NULL);
18574c0da0ffSGleb Smirnoff }
18584c0da0ffSGleb Smirnoff 
18594c0da0ffSGleb Smirnoff const struct bge_vendor *
18604c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
18614c0da0ffSGleb Smirnoff {
18624c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
18634c0da0ffSGleb Smirnoff 
18644c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
18654c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
18664c0da0ffSGleb Smirnoff 			return (v);
18674c0da0ffSGleb Smirnoff 
18684c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
18694c0da0ffSGleb Smirnoff 	return (NULL);
18704c0da0ffSGleb Smirnoff }
18714c0da0ffSGleb Smirnoff 
187295d67482SBill Paul /*
187395d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
18744c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
18754c0da0ffSGleb Smirnoff  *
18764c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
18777c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
18787c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
18797c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
18807c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
188195d67482SBill Paul  */
188295d67482SBill Paul static int
18833f74909aSGleb Smirnoff bge_probe(device_t dev)
188495d67482SBill Paul {
1885852c67f9SMarius Strobl 	const struct bge_type *t = bge_devs;
18864c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
18877c929cf9SJung-uk Kim 	uint16_t vid, did;
188895d67482SBill Paul 
188995d67482SBill Paul 	sc->bge_dev = dev;
18907c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
18917c929cf9SJung-uk Kim 	did = pci_get_device(dev);
18924c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
18937c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
18947c929cf9SJung-uk Kim 			char model[64], buf[96];
18954c0da0ffSGleb Smirnoff 			const struct bge_revision *br;
18964c0da0ffSGleb Smirnoff 			const struct bge_vendor *v;
18974c0da0ffSGleb Smirnoff 			uint32_t id;
18984c0da0ffSGleb Smirnoff 
18994c0da0ffSGleb Smirnoff 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
19004c0da0ffSGleb Smirnoff 			    BGE_PCIMISCCTL_ASICREV;
19014c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
19027c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
19034e35d186SJung-uk Kim 			{
19044e35d186SJung-uk Kim #if __FreeBSD_version > 700024
19054e35d186SJung-uk Kim 				const char *pname;
19064e35d186SJung-uk Kim 
1907852c67f9SMarius Strobl 				if (bge_has_eaddr(sc) &&
1908852c67f9SMarius Strobl 				    pci_get_vpd_ident(dev, &pname) == 0)
19094e35d186SJung-uk Kim 					snprintf(model, 64, "%s", pname);
19104e35d186SJung-uk Kim 				else
19114e35d186SJung-uk Kim #endif
19127c929cf9SJung-uk Kim 					snprintf(model, 64, "%s %s",
19137c929cf9SJung-uk Kim 					    v->v_name,
19147c929cf9SJung-uk Kim 					    br != NULL ? br->br_name :
19157c929cf9SJung-uk Kim 					    "NetXtreme Ethernet Controller");
19164e35d186SJung-uk Kim 			}
19177c929cf9SJung-uk Kim 			snprintf(buf, 96, "%s, %sASIC rev. %#04x", model,
19187c929cf9SJung-uk Kim 			    br != NULL ? "" : "unknown ", id >> 16);
19194c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
19206d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
19215ee49a3aSJung-uk Kim 				sc->bge_flags |= BGE_FLAG_NO_3LED;
192208bf8bb7SJung-uk Kim 			if (did == BCOM_DEVICEID_BCM5755M)
192308bf8bb7SJung-uk Kim 				sc->bge_flags |= BGE_FLAG_ADJUST_TRIM;
192495d67482SBill Paul 			return (0);
192595d67482SBill Paul 		}
192695d67482SBill Paul 		t++;
192795d67482SBill Paul 	}
192895d67482SBill Paul 
192995d67482SBill Paul 	return (ENXIO);
193095d67482SBill Paul }
193195d67482SBill Paul 
1932f41ac2beSBill Paul static void
19333f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
1934f41ac2beSBill Paul {
1935f41ac2beSBill Paul 	int i;
1936f41ac2beSBill Paul 
19373f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
1938f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1939f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1940f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1941f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1942f41ac2beSBill Paul 	}
1943f41ac2beSBill Paul 
19443f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
1945f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1946f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1947f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1948f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1949f41ac2beSBill Paul 	}
1950f41ac2beSBill Paul 
19513f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
1952f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1953f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1954f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1955f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1956f41ac2beSBill Paul 	}
1957f41ac2beSBill Paul 
1958f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1959f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1960f41ac2beSBill Paul 
1961f41ac2beSBill Paul 
19623f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
1963e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
1964e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1965e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
1966e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1967f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1968f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1969f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1970f41ac2beSBill Paul 
1971f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1972f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1973f41ac2beSBill Paul 
19743f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
1975e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1976e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1977e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1978e65bed95SPyun YongHyeon 
1979e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1980e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
1981f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1982f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1983f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1984f41ac2beSBill Paul 
1985f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1986f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1987f41ac2beSBill Paul 
19883f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
1989e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
1990e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1991e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
1992e65bed95SPyun YongHyeon 
1993e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1994e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
1995f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1996f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1997f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1998f41ac2beSBill Paul 
1999f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
2000f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2001f41ac2beSBill Paul 
20023f74909aSGleb Smirnoff 	/* Destroy TX ring. */
2003e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
2004e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2005e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
2006e65bed95SPyun YongHyeon 
2007e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2008f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2009f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
2010f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
2011f41ac2beSBill Paul 
2012f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
2013f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2014f41ac2beSBill Paul 
20153f74909aSGleb Smirnoff 	/* Destroy status block. */
2016e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
2017e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2018e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
2019e65bed95SPyun YongHyeon 
2020e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2021f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2022f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
2023f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
2024f41ac2beSBill Paul 
2025f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
2026f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2027f41ac2beSBill Paul 
20283f74909aSGleb Smirnoff 	/* Destroy statistics block. */
2029e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
2030e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2031e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
2032e65bed95SPyun YongHyeon 
2033e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2034f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2035f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
2036f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
2037f41ac2beSBill Paul 
2038f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
2039f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2040f41ac2beSBill Paul 
20413f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
2042f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
2043f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2044f41ac2beSBill Paul }
2045f41ac2beSBill Paul 
2046f41ac2beSBill Paul static int
20473f74909aSGleb Smirnoff bge_dma_alloc(device_t dev)
2048f41ac2beSBill Paul {
20493f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
2050f41ac2beSBill Paul 	struct bge_softc *sc;
20511be6acb7SGleb Smirnoff 	int i, error;
2052f41ac2beSBill Paul 
2053f41ac2beSBill Paul 	sc = device_get_softc(dev);
2054f41ac2beSBill Paul 
2055f41ac2beSBill Paul 	/*
2056f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
2057f41ac2beSBill Paul 	 */
20584eee14cbSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
20594eee14cbSMarius Strobl 	    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,	NULL,
20604eee14cbSMarius Strobl 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
20614eee14cbSMarius Strobl 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2062f41ac2beSBill Paul 
2063e65bed95SPyun YongHyeon 	if (error != 0) {
2064fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2065fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
2066e65bed95SPyun YongHyeon 		return (ENOMEM);
2067e65bed95SPyun YongHyeon 	}
2068e65bed95SPyun YongHyeon 
2069f41ac2beSBill Paul 	/*
20704eee14cbSMarius Strobl 	 * Create tag for mbufs.
2071f41ac2beSBill Paul 	 */
20728a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
2073f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
20741be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
20751be6acb7SGleb Smirnoff 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
2076f41ac2beSBill Paul 
2077f41ac2beSBill Paul 	if (error) {
2078fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2079f41ac2beSBill Paul 		return (ENOMEM);
2080f41ac2beSBill Paul 	}
2081f41ac2beSBill Paul 
20823f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
2083f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2084f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
2085f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
2086f41ac2beSBill Paul 		if (error) {
2087fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
2088fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
2089f41ac2beSBill Paul 			return (ENOMEM);
2090f41ac2beSBill Paul 		}
2091f41ac2beSBill Paul 	}
2092f41ac2beSBill Paul 
20933f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
2094f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2095f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
2096f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
2097f41ac2beSBill Paul 		if (error) {
2098fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
2099fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
2100f41ac2beSBill Paul 			return (ENOMEM);
2101f41ac2beSBill Paul 		}
2102f41ac2beSBill Paul 	}
2103f41ac2beSBill Paul 
21043f74909aSGleb Smirnoff 	/* Create tag for standard RX ring. */
2105f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2106f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2107f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
2108f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
2109f41ac2beSBill Paul 
2110f41ac2beSBill Paul 	if (error) {
2111fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2112f41ac2beSBill Paul 		return (ENOMEM);
2113f41ac2beSBill Paul 	}
2114f41ac2beSBill Paul 
21153f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for standard RX ring. */
2116f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
2117f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
2118f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
2119f41ac2beSBill Paul 	if (error)
2120f41ac2beSBill Paul 		return (ENOMEM);
2121f41ac2beSBill Paul 
2122f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
2123f41ac2beSBill Paul 
21243f74909aSGleb Smirnoff 	/* Load the address of the standard RX ring. */
2125f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2126f41ac2beSBill Paul 	ctx.sc = sc;
2127f41ac2beSBill Paul 
2128f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
2129f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
2130f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2131f41ac2beSBill Paul 
2132f41ac2beSBill Paul 	if (error)
2133f41ac2beSBill Paul 		return (ENOMEM);
2134f41ac2beSBill Paul 
2135f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
2136f41ac2beSBill Paul 
21373f74909aSGleb Smirnoff 	/* Create tags for jumbo mbufs. */
21384c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2139f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
21408a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
21411be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
21421be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
2143f41ac2beSBill Paul 		if (error) {
2144fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
21453f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
2146f41ac2beSBill Paul 			return (ENOMEM);
2147f41ac2beSBill Paul 		}
2148f41ac2beSBill Paul 
21493f74909aSGleb Smirnoff 		/* Create tag for jumbo RX ring. */
2150f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2151f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2152f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
2153f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
2154f41ac2beSBill Paul 
2155f41ac2beSBill Paul 		if (error) {
2156fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
21573f74909aSGleb Smirnoff 			    "could not allocate jumbo ring dma tag\n");
2158f41ac2beSBill Paul 			return (ENOMEM);
2159f41ac2beSBill Paul 		}
2160f41ac2beSBill Paul 
21613f74909aSGleb Smirnoff 		/* Allocate DMA'able memory for jumbo RX ring. */
2162f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
21631be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
21641be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
2165f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
2166f41ac2beSBill Paul 		if (error)
2167f41ac2beSBill Paul 			return (ENOMEM);
2168f41ac2beSBill Paul 
21693f74909aSGleb Smirnoff 		/* Load the address of the jumbo RX ring. */
2170f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
2171f41ac2beSBill Paul 		ctx.sc = sc;
2172f41ac2beSBill Paul 
2173f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2174f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2175f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
2176f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2177f41ac2beSBill Paul 
2178f41ac2beSBill Paul 		if (error)
2179f41ac2beSBill Paul 			return (ENOMEM);
2180f41ac2beSBill Paul 
2181f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
2182f41ac2beSBill Paul 
21833f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
2184f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2185f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2186f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2187f41ac2beSBill Paul 			if (error) {
2188fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
21893f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
2190f41ac2beSBill Paul 				return (ENOMEM);
2191f41ac2beSBill Paul 			}
2192f41ac2beSBill Paul 		}
2193f41ac2beSBill Paul 
2194f41ac2beSBill Paul 	}
2195f41ac2beSBill Paul 
21963f74909aSGleb Smirnoff 	/* Create tag for RX return ring. */
2197f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2198f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2199f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
2200f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
2201f41ac2beSBill Paul 
2202f41ac2beSBill Paul 	if (error) {
2203fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2204f41ac2beSBill Paul 		return (ENOMEM);
2205f41ac2beSBill Paul 	}
2206f41ac2beSBill Paul 
22073f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for RX return ring. */
2208f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
2209f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
2210f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
2211f41ac2beSBill Paul 	if (error)
2212f41ac2beSBill Paul 		return (ENOMEM);
2213f41ac2beSBill Paul 
2214f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
2215f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
2216f41ac2beSBill Paul 
22173f74909aSGleb Smirnoff 	/* Load the address of the RX return ring. */
2218f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2219f41ac2beSBill Paul 	ctx.sc = sc;
2220f41ac2beSBill Paul 
2221f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
2222f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
2223f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
2224f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2225f41ac2beSBill Paul 
2226f41ac2beSBill Paul 	if (error)
2227f41ac2beSBill Paul 		return (ENOMEM);
2228f41ac2beSBill Paul 
2229f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
2230f41ac2beSBill Paul 
22313f74909aSGleb Smirnoff 	/* Create tag for TX ring. */
2232f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2233f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2234f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
2235f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
2236f41ac2beSBill Paul 
2237f41ac2beSBill Paul 	if (error) {
2238fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2239f41ac2beSBill Paul 		return (ENOMEM);
2240f41ac2beSBill Paul 	}
2241f41ac2beSBill Paul 
22423f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for TX ring. */
2243f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
2244f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
2245f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
2246f41ac2beSBill Paul 	if (error)
2247f41ac2beSBill Paul 		return (ENOMEM);
2248f41ac2beSBill Paul 
2249f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
2250f41ac2beSBill Paul 
22513f74909aSGleb Smirnoff 	/* Load the address of the TX ring. */
2252f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2253f41ac2beSBill Paul 	ctx.sc = sc;
2254f41ac2beSBill Paul 
2255f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
2256f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
2257f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2258f41ac2beSBill Paul 
2259f41ac2beSBill Paul 	if (error)
2260f41ac2beSBill Paul 		return (ENOMEM);
2261f41ac2beSBill Paul 
2262f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
2263f41ac2beSBill Paul 
22643f74909aSGleb Smirnoff 	/* Create tag for status block. */
2265f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2266f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2267f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
2268f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
2269f41ac2beSBill Paul 
2270f41ac2beSBill Paul 	if (error) {
2271fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2272f41ac2beSBill Paul 		return (ENOMEM);
2273f41ac2beSBill Paul 	}
2274f41ac2beSBill Paul 
22753f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for status block. */
2276f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
2277f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
2278f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
2279f41ac2beSBill Paul 	if (error)
2280f41ac2beSBill Paul 		return (ENOMEM);
2281f41ac2beSBill Paul 
2282f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2283f41ac2beSBill Paul 
22843f74909aSGleb Smirnoff 	/* Load the address of the status block. */
2285f41ac2beSBill Paul 	ctx.sc = sc;
2286f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2287f41ac2beSBill Paul 
2288f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2289f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2290f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2291f41ac2beSBill Paul 
2292f41ac2beSBill Paul 	if (error)
2293f41ac2beSBill Paul 		return (ENOMEM);
2294f41ac2beSBill Paul 
2295f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2296f41ac2beSBill Paul 
22973f74909aSGleb Smirnoff 	/* Create tag for statistics block. */
2298f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2299f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2300f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2301f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
2302f41ac2beSBill Paul 
2303f41ac2beSBill Paul 	if (error) {
2304fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2305f41ac2beSBill Paul 		return (ENOMEM);
2306f41ac2beSBill Paul 	}
2307f41ac2beSBill Paul 
23083f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for statistics block. */
2309f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2310f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2311f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
2312f41ac2beSBill Paul 	if (error)
2313f41ac2beSBill Paul 		return (ENOMEM);
2314f41ac2beSBill Paul 
2315f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2316f41ac2beSBill Paul 
23173f74909aSGleb Smirnoff 	/* Load the address of the statstics block. */
2318f41ac2beSBill Paul 	ctx.sc = sc;
2319f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2320f41ac2beSBill Paul 
2321f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2322f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2323f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2324f41ac2beSBill Paul 
2325f41ac2beSBill Paul 	if (error)
2326f41ac2beSBill Paul 		return (ENOMEM);
2327f41ac2beSBill Paul 
2328f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2329f41ac2beSBill Paul 
2330f41ac2beSBill Paul 	return (0);
2331f41ac2beSBill Paul }
2332f41ac2beSBill Paul 
23330a55a034SJung-uk Kim #if __FreeBSD_version > 602105
2334bf6ef57aSJohn Polstra /*
2335bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
2336bf6ef57aSJohn Polstra  */
2337bf6ef57aSJohn Polstra static int
2338bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
2339bf6ef57aSJohn Polstra {
2340bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
234155aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
2342bf6ef57aSJohn Polstra 
234355aaf894SMarius Strobl 	d = pci_get_domain(dev);
2344bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
2345bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
2346bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
2347bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
234855aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
2349bf6ef57aSJohn Polstra 			return (1);
2350bf6ef57aSJohn Polstra 	return (0);
2351bf6ef57aSJohn Polstra }
2352bf6ef57aSJohn Polstra 
2353bf6ef57aSJohn Polstra /*
2354bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
2355bf6ef57aSJohn Polstra  */
2356bf6ef57aSJohn Polstra static int
2357bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
2358bf6ef57aSJohn Polstra {
2359bf6ef57aSJohn Polstra 	int can_use_msi = 0;
2360bf6ef57aSJohn Polstra 
2361bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
2362a8376f70SMarius Strobl 	case BGE_ASICREV_BCM5714_A0:
2363bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
2364bf6ef57aSJohn Polstra 		/*
2365a8376f70SMarius Strobl 		 * Apparently, MSI doesn't work when these chips are
2366a8376f70SMarius Strobl 		 * configured in single-port mode.
2367bf6ef57aSJohn Polstra 		 */
2368bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
2369bf6ef57aSJohn Polstra 			can_use_msi = 1;
2370bf6ef57aSJohn Polstra 		break;
2371bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
2372bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
2373bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
2374bf6ef57aSJohn Polstra 			can_use_msi = 1;
2375bf6ef57aSJohn Polstra 		break;
2376a8376f70SMarius Strobl 	default:
2377a8376f70SMarius Strobl 		if (BGE_IS_575X_PLUS(sc))
2378bf6ef57aSJohn Polstra 			can_use_msi = 1;
2379bf6ef57aSJohn Polstra 	}
2380bf6ef57aSJohn Polstra 	return (can_use_msi);
2381bf6ef57aSJohn Polstra }
23824e35d186SJung-uk Kim #endif
2383bf6ef57aSJohn Polstra 
238495d67482SBill Paul static int
23853f74909aSGleb Smirnoff bge_attach(device_t dev)
238695d67482SBill Paul {
238795d67482SBill Paul 	struct ifnet *ifp;
238895d67482SBill Paul 	struct bge_softc *sc;
23894f0794ffSBjoern A. Zeeb 	uint32_t hwcfg = 0, misccfg;
239008013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
239108013fd3SMarius Strobl 	int error, reg, rid, trys;
239295d67482SBill Paul 
239395d67482SBill Paul 	sc = device_get_softc(dev);
239495d67482SBill Paul 	sc->bge_dev = dev;
239595d67482SBill Paul 
239695d67482SBill Paul 	/*
239795d67482SBill Paul 	 * Map control/status registers.
239895d67482SBill Paul 	 */
239995d67482SBill Paul 	pci_enable_busmaster(dev);
240095d67482SBill Paul 
240195d67482SBill Paul 	rid = BGE_PCI_BAR0;
24025f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
240344f8f2fcSMarius Strobl 	    RF_ACTIVE);
240495d67482SBill Paul 
240595d67482SBill Paul 	if (sc->bge_res == NULL) {
2406fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
240795d67482SBill Paul 		error = ENXIO;
240895d67482SBill Paul 		goto fail;
240995d67482SBill Paul 	}
241095d67482SBill Paul 
24114f09c4c7SMarius Strobl 	/* Save various chip information. */
2412e53d81eeSPaul Saab 	sc->bge_chipid =
2413e53d81eeSPaul Saab 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2414e53d81eeSPaul Saab 	    BGE_PCIMISCCTL_ASICREV;
2415e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2416e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2417e53d81eeSPaul Saab 
241886543395SJung-uk Kim 	/*
241938cc658fSJohn Baldwin 	 * Don't enable Ethernet@WireSpeed for the 5700, 5906, or the
242086543395SJung-uk Kim 	 * 5705 A0 and A1 chips.
242186543395SJung-uk Kim 	 */
242286543395SJung-uk Kim 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
242338cc658fSJohn Baldwin 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
242486543395SJung-uk Kim 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
242586543395SJung-uk Kim 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)
242686543395SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_WIRESPEED;
242786543395SJung-uk Kim 
24285fea260fSMarius Strobl 	if (bge_has_eaddr(sc))
24295fea260fSMarius Strobl 		sc->bge_flags |= BGE_FLAG_EADDR;
243008013fd3SMarius Strobl 
24310dae9719SJung-uk Kim 	/* Save chipset family. */
24320dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
24330dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
24340dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
24350dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
24360dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
24377ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
24380dae9719SJung-uk Kim 		break;
24390dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
24400dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
24410dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
24427ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5714_FAMILY /* | BGE_FLAG_JUMBO */;
24435ee49a3aSJung-uk Kim 		/* FALLTHRU */
24440dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
24450dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
24460dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5755:
24470dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5787:
244838cc658fSJohn Baldwin 	case BGE_ASICREV_BCM5906:
24490dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
24505ee49a3aSJung-uk Kim 		/* FALLTHRU */
24510dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
24520dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
24530dae9719SJung-uk Kim 		break;
24540dae9719SJung-uk Kim 	}
24550dae9719SJung-uk Kim 
24565ee49a3aSJung-uk Kim 	/* Set various bug flags. */
24571ec4c3a8SJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
24581ec4c3a8SJung-uk Kim 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
24591ec4c3a8SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_CRC_BUG;
24605ee49a3aSJung-uk Kim 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
24615ee49a3aSJung-uk Kim 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
24625ee49a3aSJung-uk Kim 		sc->bge_flags |= BGE_FLAG_ADC_BUG;
24635ee49a3aSJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
24645ee49a3aSJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5704_A0_BUG;
246508bf8bb7SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc) &&
246608bf8bb7SJung-uk Kim 	    !(sc->bge_flags & BGE_FLAG_ADJUST_TRIM)) {
24675ee49a3aSJung-uk Kim 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
24684fcf220bSJohn Baldwin 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
24694fcf220bSJohn Baldwin 			if (sc->bge_chipid != BGE_CHIPID_BCM5722_A0)
24705ee49a3aSJung-uk Kim 				sc->bge_flags |= BGE_FLAG_JITTER_BUG;
247138cc658fSJohn Baldwin 		} else if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
24725ee49a3aSJung-uk Kim 			sc->bge_flags |= BGE_FLAG_BER_BUG;
24735ee49a3aSJung-uk Kim 	}
24745ee49a3aSJung-uk Kim 
24754f0794ffSBjoern A. Zeeb 
24764f0794ffSBjoern A. Zeeb 	/*
24774f0794ffSBjoern A. Zeeb 	 * We could possibly check for BCOM_DEVICEID_BCM5788 in bge_probe()
24784f0794ffSBjoern A. Zeeb 	 * but I do not know the DEVICEID for the 5788M.
24794f0794ffSBjoern A. Zeeb 	 */
24804f0794ffSBjoern A. Zeeb 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID;
24814f0794ffSBjoern A. Zeeb 	if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
24824f0794ffSBjoern A. Zeeb 	    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
24834f0794ffSBjoern A. Zeeb 		sc->bge_flags |= BGE_FLAG_5788;
24844f0794ffSBjoern A. Zeeb 
2485e53d81eeSPaul Saab   	/*
24866f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
2487e53d81eeSPaul Saab   	 */
2488fe09b799SJung-uk Kim #if __FreeBSD_version > 602101
24896f8718a3SScott Long 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
24904c0da0ffSGleb Smirnoff 		/*
24916f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
24926f8718a3SScott Long 		 * must be a PCI Express device.
24936f8718a3SScott Long 		 */
24944f09c4c7SMarius Strobl 		if (reg != 0) {
24956f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIE;
24966f8718a3SScott Long #else
24975345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc)) {
24986f8718a3SScott Long 		reg = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
24994f09c4c7SMarius Strobl 		if ((reg & 0xFF) == BGE_PCIE_CAPID) {
25006f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIE;
25014f09c4c7SMarius Strobl 			reg = BGE_PCIE_CAPID;
250290447aadSMarius Strobl #endif
25034f09c4c7SMarius Strobl 			bge_set_max_readrq(sc, reg);
25044f09c4c7SMarius Strobl 		}
25056f8718a3SScott Long 	} else {
25066f8718a3SScott Long 		/*
25076f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
25086f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
25094c0da0ffSGleb Smirnoff 		 */
251090447aadSMarius Strobl 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
25114c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
2512652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
25136f8718a3SScott Long 	}
25144c0da0ffSGleb Smirnoff 
25150a55a034SJung-uk Kim #if __FreeBSD_version > 602105
25164e35d186SJung-uk Kim 	{
25174e35d186SJung-uk Kim 		int msicount;
25184e35d186SJung-uk Kim 
2519bf6ef57aSJohn Polstra 		/*
2520bf6ef57aSJohn Polstra 		 * Allocate the interrupt, using MSI if possible.  These devices
2521bf6ef57aSJohn Polstra 		 * support 8 MSI messages, but only the first one is used in
2522bf6ef57aSJohn Polstra 		 * normal operation.
2523bf6ef57aSJohn Polstra 		 */
2524bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
2525bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
2526bf6ef57aSJohn Polstra 			if (msicount > 1)
2527bf6ef57aSJohn Polstra 				msicount = 1;
2528bf6ef57aSJohn Polstra 		} else
2529bf6ef57aSJohn Polstra 			msicount = 0;
2530bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
2531bf6ef57aSJohn Polstra 			rid = 1;
2532bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
2533bf6ef57aSJohn Polstra 		} else
2534bf6ef57aSJohn Polstra 			rid = 0;
25354e35d186SJung-uk Kim 	}
25364e35d186SJung-uk Kim #else
25374e35d186SJung-uk Kim 	rid = 0;
25384e35d186SJung-uk Kim #endif
2539bf6ef57aSJohn Polstra 
2540bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2541bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
2542bf6ef57aSJohn Polstra 
2543bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
2544bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
2545bf6ef57aSJohn Polstra 		error = ENXIO;
2546bf6ef57aSJohn Polstra 		goto fail;
2547bf6ef57aSJohn Polstra 	}
2548bf6ef57aSJohn Polstra 
25494f09c4c7SMarius Strobl 	if (bootverbose)
25504f09c4c7SMarius Strobl 		device_printf(dev,
25514f09c4c7SMarius Strobl 		    "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; %s\n",
25524f09c4c7SMarius Strobl 		    sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev,
25534f09c4c7SMarius Strobl 		    (sc->bge_flags & BGE_FLAG_PCIX) ? "PCI-X" :
25544f09c4c7SMarius Strobl 		    ((sc->bge_flags & BGE_FLAG_PCIE) ? "PCI-E" : "PCI"));
25554f09c4c7SMarius Strobl 
2556bf6ef57aSJohn Polstra 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
2557bf6ef57aSJohn Polstra 
255895d67482SBill Paul 	/* Try to reset the chip. */
25598cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
25608cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
25618cb1383cSDoug Ambrisko 		error = ENXIO;
25628cb1383cSDoug Ambrisko 		goto fail;
25638cb1383cSDoug Ambrisko 	}
25648cb1383cSDoug Ambrisko 
25658cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
2566f1a7e6d5SScott Long 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG)
2567f1a7e6d5SScott Long 	    == BGE_MAGIC_NUMBER)) {
25688cb1383cSDoug Ambrisko 		if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG)
25698cb1383cSDoug Ambrisko 		    & BGE_HWCFG_ASF) {
25708cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_ENABLE;
25718cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_STACKUP;
25728cb1383cSDoug Ambrisko 			if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
25738cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
25748cb1383cSDoug Ambrisko 			}
25758cb1383cSDoug Ambrisko 		}
25768cb1383cSDoug Ambrisko 	}
25778cb1383cSDoug Ambrisko 
25788cb1383cSDoug Ambrisko 	/* Try to reset the chip again the nice way. */
25798cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
25808cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
25818cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
25828cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
25838cb1383cSDoug Ambrisko 		error = ENXIO;
25848cb1383cSDoug Ambrisko 		goto fail;
25858cb1383cSDoug Ambrisko 	}
25868cb1383cSDoug Ambrisko 
25878cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
25888cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
258995d67482SBill Paul 
259095d67482SBill Paul 	if (bge_chipinit(sc)) {
2591fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
259295d67482SBill Paul 		error = ENXIO;
259395d67482SBill Paul 		goto fail;
259495d67482SBill Paul 	}
259595d67482SBill Paul 
259638cc658fSJohn Baldwin 	error = bge_get_eaddr(sc, eaddr);
259738cc658fSJohn Baldwin 	if (error) {
259808013fd3SMarius Strobl 		device_printf(sc->bge_dev,
259908013fd3SMarius Strobl 		    "failed to read station address\n");
260095d67482SBill Paul 		error = ENXIO;
260195d67482SBill Paul 		goto fail;
260295d67482SBill Paul 	}
260395d67482SBill Paul 
2604f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
26057ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
2606f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2607f41ac2beSBill Paul 	else
2608f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2609f41ac2beSBill Paul 
2610f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2611fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2612fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2613f41ac2beSBill Paul 		error = ENXIO;
2614f41ac2beSBill Paul 		goto fail;
2615f41ac2beSBill Paul 	}
2616f41ac2beSBill Paul 
261795d67482SBill Paul 	/* Set default tuneable values. */
261895d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
261995d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
262095d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
26216f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
26226f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
262395d67482SBill Paul 
262495d67482SBill Paul 	/* Set up ifnet structure */
2625fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2626fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2627fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2628fc74a9f9SBrooks Davis 		error = ENXIO;
2629fc74a9f9SBrooks Davis 		goto fail;
2630fc74a9f9SBrooks Davis 	}
263195d67482SBill Paul 	ifp->if_softc = sc;
26329bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
263395d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
263495d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
263595d67482SBill Paul 	ifp->if_start = bge_start;
263695d67482SBill Paul 	ifp->if_init = bge_init;
263795d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
26384d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
26394d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
26404d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
264195d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2642d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
26434e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
26444e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
26454e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
26464e35d186SJung-uk Kim #endif
264795d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
264875719184SGleb Smirnoff #ifdef DEVICE_POLLING
264975719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
265075719184SGleb Smirnoff #endif
265195d67482SBill Paul 
2652a1d52896SBill Paul 	/*
2653d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2654d375e524SGleb Smirnoff 	 * to hardware bugs.
2655d375e524SGleb Smirnoff 	 */
2656d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2657d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2658d375e524SGleb Smirnoff 		ifp->if_capenable &= IFCAP_HWCSUM;
2659d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2660d375e524SGleb Smirnoff 	}
2661d375e524SGleb Smirnoff 
2662d375e524SGleb Smirnoff 	/*
2663a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
266441abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
266541abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
266641abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
266741abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
266841abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
266941abcc1bSPaul Saab 	 * SK-9D41.
2670a1d52896SBill Paul 	 */
267141abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
267241abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
26735fea260fSMarius Strobl 	else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
26745fea260fSMarius Strobl 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
2675f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2676f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2677fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2678f6789fbaSPyun YongHyeon 			error = ENXIO;
2679f6789fbaSPyun YongHyeon 			goto fail;
2680f6789fbaSPyun YongHyeon 		}
268141abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
268241abcc1bSPaul Saab 	}
268341abcc1bSPaul Saab 
268441abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2685652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
2686a1d52896SBill Paul 
268795d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
26880c8aa4eaSJung-uk Kim 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
2689652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
269095d67482SBill Paul 
2691652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
26920c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
26930c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
26940c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
26956098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
26966098821cSJung-uk Kim 		    0, NULL);
269795d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
269895d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
2699da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
270095d67482SBill Paul 	} else {
270195d67482SBill Paul 		/*
27028cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
27038cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
27048cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
27058cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
27068cb1383cSDoug Ambrisko 		 * the PHY.
270795d67482SBill Paul 		 */
27084012d104SMarius Strobl 		trys = 0;
27098cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
27108cb1383cSDoug Ambrisko again:
27118cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
27128cb1383cSDoug Ambrisko 
271395d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
271495d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
27158cb1383cSDoug Ambrisko 			if (trys++ < 4) {
27168cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
27174e35d186SJung-uk Kim 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR,
27184e35d186SJung-uk Kim 				    BMCR_RESET);
27198cb1383cSDoug Ambrisko 				goto again;
27208cb1383cSDoug Ambrisko 			}
27218cb1383cSDoug Ambrisko 
2722fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
272395d67482SBill Paul 			error = ENXIO;
272495d67482SBill Paul 			goto fail;
272595d67482SBill Paul 		}
27268cb1383cSDoug Ambrisko 
27278cb1383cSDoug Ambrisko 		/*
27288cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
27298cb1383cSDoug Ambrisko 		 */
27308cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
27318cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
273295d67482SBill Paul 	}
273395d67482SBill Paul 
273495d67482SBill Paul 	/*
2735e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2736e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2737e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2738e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2739e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2740e255b776SJohn Polstra 	 * payloads by copying the received packets.
2741e255b776SJohn Polstra 	 */
2742652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
2743652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
2744652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
2745e255b776SJohn Polstra 
2746e255b776SJohn Polstra 	/*
274795d67482SBill Paul 	 * Call MI attach routine.
274895d67482SBill Paul 	 */
2749fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
2750b74e67fbSGleb Smirnoff 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
27510f9bd73bSSam Leffler 
27520f9bd73bSSam Leffler 	/*
27530f9bd73bSSam Leffler 	 * Hookup IRQ last.
27540f9bd73bSSam Leffler 	 */
27554e35d186SJung-uk Kim #if __FreeBSD_version > 700030
27560f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
2757ef544f63SPaolo Pisati 	   NULL, bge_intr, sc, &sc->bge_intrhand);
27584e35d186SJung-uk Kim #else
27594e35d186SJung-uk Kim 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
27604e35d186SJung-uk Kim 	   bge_intr, sc, &sc->bge_intrhand);
27614e35d186SJung-uk Kim #endif
27620f9bd73bSSam Leffler 
27630f9bd73bSSam Leffler 	if (error) {
2764fc74a9f9SBrooks Davis 		bge_detach(dev);
2765fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
27660f9bd73bSSam Leffler 	}
276795d67482SBill Paul 
27686f8718a3SScott Long 	bge_add_sysctls(sc);
27696f8718a3SScott Long 
277008013fd3SMarius Strobl 	return (0);
277108013fd3SMarius Strobl 
277295d67482SBill Paul fail:
277308013fd3SMarius Strobl 	bge_release_resources(sc);
277408013fd3SMarius Strobl 
277595d67482SBill Paul 	return (error);
277695d67482SBill Paul }
277795d67482SBill Paul 
277895d67482SBill Paul static int
27793f74909aSGleb Smirnoff bge_detach(device_t dev)
278095d67482SBill Paul {
278195d67482SBill Paul 	struct bge_softc *sc;
278295d67482SBill Paul 	struct ifnet *ifp;
278395d67482SBill Paul 
278495d67482SBill Paul 	sc = device_get_softc(dev);
2785fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
278695d67482SBill Paul 
278775719184SGleb Smirnoff #ifdef DEVICE_POLLING
278875719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
278975719184SGleb Smirnoff 		ether_poll_deregister(ifp);
279075719184SGleb Smirnoff #endif
279175719184SGleb Smirnoff 
27920f9bd73bSSam Leffler 	BGE_LOCK(sc);
279395d67482SBill Paul 	bge_stop(sc);
279495d67482SBill Paul 	bge_reset(sc);
27950f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
27960f9bd73bSSam Leffler 
27975dda8085SOleg Bulyzhin 	callout_drain(&sc->bge_stat_ch);
27985dda8085SOleg Bulyzhin 
27990f9bd73bSSam Leffler 	ether_ifdetach(ifp);
280095d67482SBill Paul 
2801652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
280295d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
280395d67482SBill Paul 	} else {
280495d67482SBill Paul 		bus_generic_detach(dev);
280595d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
280695d67482SBill Paul 	}
280795d67482SBill Paul 
280895d67482SBill Paul 	bge_release_resources(sc);
280995d67482SBill Paul 
281095d67482SBill Paul 	return (0);
281195d67482SBill Paul }
281295d67482SBill Paul 
281395d67482SBill Paul static void
28143f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
281595d67482SBill Paul {
281695d67482SBill Paul 	device_t dev;
281795d67482SBill Paul 
281895d67482SBill Paul 	dev = sc->bge_dev;
281995d67482SBill Paul 
282095d67482SBill Paul 	if (sc->bge_intrhand != NULL)
282195d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
282295d67482SBill Paul 
282395d67482SBill Paul 	if (sc->bge_irq != NULL)
2824724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
2825724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
2826724bd939SJohn Polstra 
28270a55a034SJung-uk Kim #if __FreeBSD_version > 602105
2828724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
2829724bd939SJohn Polstra 		pci_release_msi(dev);
28304e35d186SJung-uk Kim #endif
283195d67482SBill Paul 
283295d67482SBill Paul 	if (sc->bge_res != NULL)
283395d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
283495d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
283595d67482SBill Paul 
2836ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2837ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2838ad61f896SRuslan Ermilov 
2839f41ac2beSBill Paul 	bge_dma_free(sc);
284095d67482SBill Paul 
28410f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
28420f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
284395d67482SBill Paul }
284495d67482SBill Paul 
28458cb1383cSDoug Ambrisko static int
28463f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
284795d67482SBill Paul {
284895d67482SBill Paul 	device_t dev;
28495fea260fSMarius Strobl 	uint32_t cachesize, command, pcistate, reset, val;
28506f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
28515fea260fSMarius Strobl 	int i;
285295d67482SBill Paul 
285395d67482SBill Paul 	dev = sc->bge_dev;
285495d67482SBill Paul 
285538cc658fSJohn Baldwin 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
285638cc658fSJohn Baldwin 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
28576f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
28586f8718a3SScott Long 			write_op = bge_writemem_direct;
28596f8718a3SScott Long 		else
28606f8718a3SScott Long 			write_op = bge_writemem_ind;
28619ba784dbSScott Long 	} else
28626f8718a3SScott Long 		write_op = bge_writereg_ind;
28636f8718a3SScott Long 
286495d67482SBill Paul 	/* Save some important PCI state. */
286595d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
286695d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
286795d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
286895d67482SBill Paul 
286995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
287095d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
2871e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
287295d67482SBill Paul 
28736f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
28746f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
28756f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
28766f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
28776f8718a3SScott Long 		if (bootverbose)
28789ba784dbSScott Long 			device_printf(sc->bge_dev, "Disabling fastboot\n");
28796f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
28806f8718a3SScott Long 	}
28816f8718a3SScott Long 
28826f8718a3SScott Long 	/*
28836f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
28846f8718a3SScott Long 	 * When firmware finishes its initialization it will
28856f8718a3SScott Long 	 * write ~BGE_MAGIC_NUMBER to the same location.
28866f8718a3SScott Long 	 */
28876f8718a3SScott Long 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
28886f8718a3SScott Long 
28890c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
2890e53d81eeSPaul Saab 
2891e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2892652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
28930c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
28940c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, 0x7E2C, 0x20);
2895e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2896e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
28970c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
28980c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
2899e53d81eeSPaul Saab 		}
2900e53d81eeSPaul Saab 	}
2901e53d81eeSPaul Saab 
290221c9e407SDavid Christensen 	/*
29036f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
29046f8718a3SScott Long 	 * powered up in D0 uninitialized.
29056f8718a3SScott Long 	 */
29065345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
29076f8718a3SScott Long 		reset |= 0x04000000;
29086f8718a3SScott Long 
290995d67482SBill Paul 	/* Issue global reset */
29106f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
291195d67482SBill Paul 
291238cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
29135fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_STATUS);
291438cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
29155fea260fSMarius Strobl 		    val | BGE_VCPU_STATUS_DRV_RESET);
29165fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
291738cc658fSJohn Baldwin 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
29185fea260fSMarius Strobl 		    val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
291938cc658fSJohn Baldwin 	}
292038cc658fSJohn Baldwin 
292195d67482SBill Paul 	DELAY(1000);
292295d67482SBill Paul 
2923e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2924652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2925e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2926e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
29275fea260fSMarius Strobl 			val = pci_read_config(dev, 0xC4, 4);
29285fea260fSMarius Strobl 			pci_write_config(dev, 0xC4, val | (1 << 15), 4);
2929e53d81eeSPaul Saab 		}
29309ba784dbSScott Long 		/*
29319ba784dbSScott Long 		 * Set PCIE max payload size to 128 bytes and clear error
29329ba784dbSScott Long 		 * status.
29339ba784dbSScott Long 		 */
29340c8aa4eaSJung-uk Kim 		pci_write_config(dev, 0xD8, 0xF5000, 4);
2935e53d81eeSPaul Saab 	}
2936e53d81eeSPaul Saab 
29373f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
293895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
293995d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
2940e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
294195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
294295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
29430c8aa4eaSJung-uk Kim 	write_op(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
294495d67482SBill Paul 
2945bf6ef57aSJohn Polstra 	/* Re-enable MSI, if neccesary, and enable the memory arbiter. */
29464c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
2947bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
2948bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
2949bf6ef57aSJohn Polstra 			val = pci_read_config(dev, BGE_PCI_MSI_CTL, 2);
2950bf6ef57aSJohn Polstra 			pci_write_config(dev, BGE_PCI_MSI_CTL,
2951bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
2952bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
2953bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
2954bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
2955bf6ef57aSJohn Polstra 		}
29564c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
29574c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
29584c0da0ffSGleb Smirnoff 	} else
2959a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2960a7b0c314SPaul Saab 
296138cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
296238cc658fSJohn Baldwin 		for (i = 0; i < BGE_TIMEOUT; i++) {
296338cc658fSJohn Baldwin 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
296438cc658fSJohn Baldwin 			if (val & BGE_VCPU_STATUS_INIT_DONE)
296538cc658fSJohn Baldwin 				break;
296638cc658fSJohn Baldwin 			DELAY(100);
296738cc658fSJohn Baldwin 		}
296838cc658fSJohn Baldwin 		if (i == BGE_TIMEOUT) {
296938cc658fSJohn Baldwin 			device_printf(sc->bge_dev, "reset timed out\n");
297038cc658fSJohn Baldwin 			return (1);
297138cc658fSJohn Baldwin 		}
297238cc658fSJohn Baldwin 	} else {
297395d67482SBill Paul 		/*
29746f8718a3SScott Long 		 * Poll until we see the 1's complement of the magic number.
297508013fd3SMarius Strobl 		 * This indicates that the firmware initialization is complete.
29765fea260fSMarius Strobl 		 * We expect this to fail if no chip containing the Ethernet
29775fea260fSMarius Strobl 		 * address is fitted though.
297895d67482SBill Paul 		 */
297995d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
2980d5d23857SJung-uk Kim 			DELAY(10);
298195d67482SBill Paul 			val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
298295d67482SBill Paul 			if (val == ~BGE_MAGIC_NUMBER)
298395d67482SBill Paul 				break;
298495d67482SBill Paul 		}
298595d67482SBill Paul 
29865fea260fSMarius Strobl 		if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
29879ba784dbSScott Long 			device_printf(sc->bge_dev, "firmware handshake timed out, "
29889ba784dbSScott Long 			    "found 0x%08x\n", val);
298938cc658fSJohn Baldwin 	}
299095d67482SBill Paul 
299195d67482SBill Paul 	/*
299295d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
299395d67482SBill Paul 	 * return to its original pre-reset state. This is a
299495d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
299595d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
299695d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
299795d67482SBill Paul 	 * results.
299895d67482SBill Paul 	 */
299995d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
300095d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
300195d67482SBill Paul 			break;
300295d67482SBill Paul 		DELAY(10);
300395d67482SBill Paul 	}
300495d67482SBill Paul 
30056f8718a3SScott Long 	if (sc->bge_flags & BGE_FLAG_PCIE) {
30060c8aa4eaSJung-uk Kim 		reset = bge_readmem_ind(sc, 0x7C00);
30070c8aa4eaSJung-uk Kim 		bge_writemem_ind(sc, 0x7C00, reset | (1 << 25));
30086f8718a3SScott Long 	}
30096f8718a3SScott Long 
30103f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
3011e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
301295d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
301395d67482SBill Paul 
30148cb1383cSDoug Ambrisko 	/* Tell the ASF firmware we are up */
30158cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
30168cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
30178cb1383cSDoug Ambrisko 
301895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
301995d67482SBill Paul 
3020da3003f0SBill Paul 	/*
3021da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
3022da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
3023da3003f0SBill Paul 	 * to 1.2V.
3024da3003f0SBill Paul 	 */
3025652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
3026652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
30275fea260fSMarius Strobl 		val = CSR_READ_4(sc, BGE_SERDES_CFG);
30285fea260fSMarius Strobl 		val = (val & ~0xFFF) | 0x880;
30295fea260fSMarius Strobl 		CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
3030da3003f0SBill Paul 	}
3031da3003f0SBill Paul 
3032e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
3033652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
3034652ae483SGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
30355fea260fSMarius Strobl 		val = CSR_READ_4(sc, 0x7C00);
30365fea260fSMarius Strobl 		CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
3037e53d81eeSPaul Saab 	}
303895d67482SBill Paul 	DELAY(10000);
30398cb1383cSDoug Ambrisko 
30408cb1383cSDoug Ambrisko 	return(0);
304195d67482SBill Paul }
304295d67482SBill Paul 
304395d67482SBill Paul /*
304495d67482SBill Paul  * Frame reception handling. This is called if there's a frame
304595d67482SBill Paul  * on the receive return list.
304695d67482SBill Paul  *
304795d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
30481be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
304995d67482SBill Paul  * 2) the frame is from the standard receive ring
305095d67482SBill Paul  */
305195d67482SBill Paul 
305295d67482SBill Paul static void
30533f74909aSGleb Smirnoff bge_rxeof(struct bge_softc *sc)
305495d67482SBill Paul {
305595d67482SBill Paul 	struct ifnet *ifp;
305695d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
305795d67482SBill Paul 
30580f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
30590f9bd73bSSam Leffler 
30603f74909aSGleb Smirnoff 	/* Nothing to do. */
3061cfcb5025SOleg Bulyzhin 	if (sc->bge_rx_saved_considx ==
3062cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx)
3063cfcb5025SOleg Bulyzhin 		return;
3064cfcb5025SOleg Bulyzhin 
3065fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
306695d67482SBill Paul 
3067f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
3068e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
3069f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
3070f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
30714c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
3072f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
30734c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTREAD);
3074f41ac2beSBill Paul 
307595d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
3076f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
307795d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
30783f74909aSGleb Smirnoff 		uint32_t		rxidx;
307995d67482SBill Paul 		struct mbuf		*m = NULL;
30803f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
308195d67482SBill Paul 		int			have_tag = 0;
308295d67482SBill Paul 
308375719184SGleb Smirnoff #ifdef DEVICE_POLLING
308475719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
308575719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
308675719184SGleb Smirnoff 				break;
308775719184SGleb Smirnoff 			sc->rxcycles--;
308875719184SGleb Smirnoff 		}
308975719184SGleb Smirnoff #endif
309075719184SGleb Smirnoff 
309195d67482SBill Paul 		cur_rx =
3092f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
309395d67482SBill Paul 
309495d67482SBill Paul 		rxidx = cur_rx->bge_idx;
30950434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
309695d67482SBill Paul 
3097cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
3098cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
309995d67482SBill Paul 			have_tag = 1;
310095d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
310195d67482SBill Paul 		}
310295d67482SBill Paul 
310395d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
310495d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
3105f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
3106f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
3107f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
3108f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
3109f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
311095d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
311195d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
311295d67482SBill Paul 			jumbocnt++;
311395d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
311495d67482SBill Paul 				ifp->if_ierrors++;
311595d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
311695d67482SBill Paul 				continue;
311795d67482SBill Paul 			}
311895d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
311995d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
312095d67482SBill Paul 				ifp->if_ierrors++;
312195d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
312295d67482SBill Paul 				continue;
312395d67482SBill Paul 			}
312495d67482SBill Paul 		} else {
312595d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
3126f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
3127f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
3128f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
3129f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
3130f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
313195d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
313295d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
313395d67482SBill Paul 			stdcnt++;
313495d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
313595d67482SBill Paul 				ifp->if_ierrors++;
313695d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
313795d67482SBill Paul 				continue;
313895d67482SBill Paul 			}
313995d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
314095d67482SBill Paul 			    NULL) == ENOBUFS) {
314195d67482SBill Paul 				ifp->if_ierrors++;
314295d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
314395d67482SBill Paul 				continue;
314495d67482SBill Paul 			}
314595d67482SBill Paul 		}
314695d67482SBill Paul 
314795d67482SBill Paul 		ifp->if_ipackets++;
3148e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
3149e255b776SJohn Polstra 		/*
3150e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
3151e65bed95SPyun YongHyeon 		 * the payload is aligned.
3152e255b776SJohn Polstra 		 */
3153652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
3154e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
3155e255b776SJohn Polstra 			    cur_rx->bge_len);
3156e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
3157e255b776SJohn Polstra 		}
3158e255b776SJohn Polstra #endif
3159473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
316095d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
316195d67482SBill Paul 
3162b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
316378178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
316495d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
31650c8aa4eaSJung-uk Kim 				if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
31660c8aa4eaSJung-uk Kim 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
316778178cd1SGleb Smirnoff 			}
3168d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
3169d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
317095d67482SBill Paul 				m->m_pkthdr.csum_data =
317195d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
3172ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
3173ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
317495d67482SBill Paul 			}
317595d67482SBill Paul 		}
317695d67482SBill Paul 
317795d67482SBill Paul 		/*
3178673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
3179673d9191SSam Leffler 		 * attach that information to the packet.
318095d67482SBill Paul 		 */
3181d147662cSGleb Smirnoff 		if (have_tag) {
31824e35d186SJung-uk Kim #if __FreeBSD_version > 700022
318378ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
318478ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
31854e35d186SJung-uk Kim #else
31864e35d186SJung-uk Kim 			VLAN_INPUT_TAG_NEW(ifp, m, vlan_tag);
31874e35d186SJung-uk Kim 			if (m == NULL)
31884e35d186SJung-uk Kim 				continue;
31894e35d186SJung-uk Kim #endif
3190d147662cSGleb Smirnoff 		}
319195d67482SBill Paul 
31920f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
3193673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
31940f9bd73bSSam Leffler 		BGE_LOCK(sc);
319595d67482SBill Paul 	}
319695d67482SBill Paul 
3197e65bed95SPyun YongHyeon 	if (stdcnt > 0)
3198f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
3199e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
32004c0da0ffSGleb Smirnoff 
32014c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc) && jumbocnt > 0)
3202f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
32034c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
3204f41ac2beSBill Paul 
320538cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
320695d67482SBill Paul 	if (stdcnt)
320738cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
320895d67482SBill Paul 	if (jumbocnt)
320938cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
32106b037352SJung-uk Kim #ifdef notyet
32116b037352SJung-uk Kim 	/*
32126b037352SJung-uk Kim 	 * This register wraps very quickly under heavy packet drops.
32136b037352SJung-uk Kim 	 * If you need correct statistics, you can enable this check.
32146b037352SJung-uk Kim 	 */
32156b037352SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
32166b037352SJung-uk Kim 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
32176b037352SJung-uk Kim #endif
321895d67482SBill Paul }
321995d67482SBill Paul 
322095d67482SBill Paul static void
32213f74909aSGleb Smirnoff bge_txeof(struct bge_softc *sc)
322295d67482SBill Paul {
322395d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
322495d67482SBill Paul 	struct ifnet *ifp;
322595d67482SBill Paul 
32260f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
32270f9bd73bSSam Leffler 
32283f74909aSGleb Smirnoff 	/* Nothing to do. */
3229cfcb5025SOleg Bulyzhin 	if (sc->bge_tx_saved_considx ==
3230cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx)
3231cfcb5025SOleg Bulyzhin 		return;
3232cfcb5025SOleg Bulyzhin 
3233fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
323495d67482SBill Paul 
3235e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
3236e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
3237e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
323895d67482SBill Paul 	/*
323995d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
324095d67482SBill Paul 	 * frames that have been sent.
324195d67482SBill Paul 	 */
324295d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
3243f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
32443f74909aSGleb Smirnoff 		uint32_t		idx = 0;
324595d67482SBill Paul 
324695d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
3247f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
324895d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
324995d67482SBill Paul 			ifp->if_opackets++;
325095d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
3251e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
3252e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
3253e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
3254f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
3255f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
3256e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
3257e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
325895d67482SBill Paul 		}
325995d67482SBill Paul 		sc->bge_txcnt--;
326095d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
326195d67482SBill Paul 	}
326295d67482SBill Paul 
326395d67482SBill Paul 	if (cur_tx != NULL)
326413f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
32655b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
32665b01e77cSBruce Evans 		sc->bge_timer = 0;
326795d67482SBill Paul }
326895d67482SBill Paul 
326975719184SGleb Smirnoff #ifdef DEVICE_POLLING
327075719184SGleb Smirnoff static void
327175719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
327275719184SGleb Smirnoff {
327375719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
3274366454f2SOleg Bulyzhin 	uint32_t statusword;
327575719184SGleb Smirnoff 
32763f74909aSGleb Smirnoff 	BGE_LOCK(sc);
32773f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
32783f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
32793f74909aSGleb Smirnoff 		return;
32803f74909aSGleb Smirnoff 	}
328175719184SGleb Smirnoff 
3282dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3283e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
3284dab5cd05SOleg Bulyzhin 
32853f74909aSGleb Smirnoff 	statusword = atomic_readandclear_32(
32863f74909aSGleb Smirnoff 	    &sc->bge_ldata.bge_status_block->bge_status);
3287dab5cd05SOleg Bulyzhin 
3288dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3289e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
3290366454f2SOleg Bulyzhin 
32910c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
3292366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
3293366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
3294366454f2SOleg Bulyzhin 
3295366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
3296366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
32974c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3298652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
3299366454f2SOleg Bulyzhin 			bge_link_upd(sc);
3300366454f2SOleg Bulyzhin 
3301366454f2SOleg Bulyzhin 	sc->rxcycles = count;
3302366454f2SOleg Bulyzhin 	bge_rxeof(sc);
3303366454f2SOleg Bulyzhin 	bge_txeof(sc);
3304366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3305366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
33063f74909aSGleb Smirnoff 
33073f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
330875719184SGleb Smirnoff }
330975719184SGleb Smirnoff #endif /* DEVICE_POLLING */
331075719184SGleb Smirnoff 
331195d67482SBill Paul static void
33123f74909aSGleb Smirnoff bge_intr(void *xsc)
331395d67482SBill Paul {
331495d67482SBill Paul 	struct bge_softc *sc;
331595d67482SBill Paul 	struct ifnet *ifp;
3316dab5cd05SOleg Bulyzhin 	uint32_t statusword;
331795d67482SBill Paul 
331895d67482SBill Paul 	sc = xsc;
3319f41ac2beSBill Paul 
33200f9bd73bSSam Leffler 	BGE_LOCK(sc);
33210f9bd73bSSam Leffler 
3322dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
3323dab5cd05SOleg Bulyzhin 
332475719184SGleb Smirnoff #ifdef DEVICE_POLLING
332575719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
332675719184SGleb Smirnoff 		BGE_UNLOCK(sc);
332775719184SGleb Smirnoff 		return;
332875719184SGleb Smirnoff 	}
332975719184SGleb Smirnoff #endif
333075719184SGleb Smirnoff 
3331f30cbfc6SScott Long 	/*
3332b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
3333b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
3334b848e032SBruce Evans 	 * our current organization this just gives complications and
3335b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
3336b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
3337b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
3338b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
3339b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
3340b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
3341b848e032SBruce Evans 	 *
3342b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
3343b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
3344b848e032SBruce Evans 	 * changing later because it is more efficient to get another
3345b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
3346b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
3347b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
3348b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
3349b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
3350b848e032SBruce Evans 	 */
335138cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
3352b848e032SBruce Evans 
3353b848e032SBruce Evans 	/*
3354f30cbfc6SScott Long 	 * Do the mandatory PCI flush as well as get the link status.
3355f30cbfc6SScott Long 	 */
3356f30cbfc6SScott Long 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
3357f41ac2beSBill Paul 
3358f30cbfc6SScott Long 	/* Make sure the descriptor ring indexes are coherent. */
3359f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3360f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
3361f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3362f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
3363f30cbfc6SScott Long 
33641f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
33654c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3366f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
3367dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
336895d67482SBill Paul 
336913f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
33703f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
337195d67482SBill Paul 		bge_rxeof(sc);
337295d67482SBill Paul 
33733f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
337495d67482SBill Paul 		bge_txeof(sc);
337595d67482SBill Paul 	}
337695d67482SBill Paul 
337713f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
337813f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
33790f9bd73bSSam Leffler 		bge_start_locked(ifp);
33800f9bd73bSSam Leffler 
33810f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
338295d67482SBill Paul }
338395d67482SBill Paul 
338495d67482SBill Paul static void
33858cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
33868cb1383cSDoug Ambrisko {
33878cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
33888cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
33898cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
33908cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
33918cb1383cSDoug Ambrisko 		else {
33928cb1383cSDoug Ambrisko 			sc->bge_asf_count = 5;
33938cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW,
33948cb1383cSDoug Ambrisko 			    BGE_FW_DRV_ALIVE);
33958cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_LEN, 4);
33968cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_DATA, 3);
33978cb1383cSDoug Ambrisko 			CSR_WRITE_4(sc, BGE_CPU_EVENT,
339839153c5aSJung-uk Kim 			    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
33998cb1383cSDoug Ambrisko 		}
34008cb1383cSDoug Ambrisko 	}
34018cb1383cSDoug Ambrisko }
34028cb1383cSDoug Ambrisko 
34038cb1383cSDoug Ambrisko static void
3404b74e67fbSGleb Smirnoff bge_tick(void *xsc)
34050f9bd73bSSam Leffler {
3406b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
340795d67482SBill Paul 	struct mii_data *mii = NULL;
340895d67482SBill Paul 
34090f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
341095d67482SBill Paul 
34115dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
34125dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
34135dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
34145dda8085SOleg Bulyzhin 	    	return;
34155dda8085SOleg Bulyzhin 
34167ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
34170434d1b8SBill Paul 		bge_stats_update_regs(sc);
34180434d1b8SBill Paul 	else
341995d67482SBill Paul 		bge_stats_update(sc);
342095d67482SBill Paul 
3421652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
342295d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
342382b67c01SOleg Bulyzhin 		/*
342482b67c01SOleg Bulyzhin 		 * Do not touch PHY if we have link up. This could break
342582b67c01SOleg Bulyzhin 		 * IPMI/ASF mode or produce extra input errors
342682b67c01SOleg Bulyzhin 		 * (extra errors was reported for bcm5701 & bcm5704).
342782b67c01SOleg Bulyzhin 		 */
342882b67c01SOleg Bulyzhin 		if (!sc->bge_link)
342995d67482SBill Paul 			mii_tick(mii);
34307b97099dSOleg Bulyzhin 	} else {
34317b97099dSOleg Bulyzhin 		/*
34327b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
34337b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
34347b97099dSOleg Bulyzhin 		 * and trigger interrupt.
34357b97099dSOleg Bulyzhin 		 */
34367b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
34373f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
34387b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
34397b97099dSOleg Bulyzhin #endif
34407b97099dSOleg Bulyzhin 		{
34417b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
34424f0794ffSBjoern A. Zeeb 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
34434f0794ffSBjoern A. Zeeb 		    sc->bge_flags & BGE_FLAG_5788)
34447b97099dSOleg Bulyzhin 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
34454f0794ffSBjoern A. Zeeb 		else
34464f0794ffSBjoern A. Zeeb 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
34477b97099dSOleg Bulyzhin 		}
3448dab5cd05SOleg Bulyzhin 	}
344995d67482SBill Paul 
34508cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
3451b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
34528cb1383cSDoug Ambrisko 
3453dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
345495d67482SBill Paul }
345595d67482SBill Paul 
345695d67482SBill Paul static void
34573f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
34580434d1b8SBill Paul {
34593f74909aSGleb Smirnoff 	struct ifnet *ifp;
34600434d1b8SBill Paul 
3461fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
34620434d1b8SBill Paul 
34636b037352SJung-uk Kim 	ifp->if_collisions += CSR_READ_4(sc, BGE_MAC_STATS +
34647e6e2507SJung-uk Kim 	    offsetof(struct bge_mac_stats_regs, etherStatsCollisions));
34657e6e2507SJung-uk Kim 
34666b037352SJung-uk Kim 	ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
34670434d1b8SBill Paul }
34680434d1b8SBill Paul 
34690434d1b8SBill Paul static void
34703f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
347195d67482SBill Paul {
347295d67482SBill Paul 	struct ifnet *ifp;
3473e907febfSPyun YongHyeon 	bus_size_t stats;
34747e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
347595d67482SBill Paul 
3476fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
347795d67482SBill Paul 
3478e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
3479e907febfSPyun YongHyeon 
3480e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
3481e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
348295d67482SBill Paul 
34838634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
34846b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
34856fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
34866fb34dd2SOleg Bulyzhin 
34876fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
34886b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
34896fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
34906fb34dd2SOleg Bulyzhin 
34916fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
34926b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
34936fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
349495d67482SBill Paul 
3495e907febfSPyun YongHyeon #undef	READ_STAT
349695d67482SBill Paul }
349795d67482SBill Paul 
349895d67482SBill Paul /*
3499d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
3500d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
3501d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
3502d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
3503d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
3504d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
3505d375e524SGleb Smirnoff  */
3506d375e524SGleb Smirnoff static __inline int
3507d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
3508d375e524SGleb Smirnoff {
3509d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
3510d375e524SGleb Smirnoff 	struct mbuf *last;
3511d375e524SGleb Smirnoff 
3512d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
3513d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
3514d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
3515d375e524SGleb Smirnoff 		last = m;
3516d375e524SGleb Smirnoff 	} else {
3517d375e524SGleb Smirnoff 		/*
3518d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
3519d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
3520d375e524SGleb Smirnoff 		 */
3521d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
3522d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
3523d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
3524d375e524SGleb Smirnoff 			struct mbuf *n;
3525d375e524SGleb Smirnoff 
3526d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
3527d375e524SGleb Smirnoff 			if (n == NULL)
3528d375e524SGleb Smirnoff 				return (ENOBUFS);
3529d375e524SGleb Smirnoff 			n->m_len = 0;
3530d375e524SGleb Smirnoff 			last->m_next = n;
3531d375e524SGleb Smirnoff 			last = n;
3532d375e524SGleb Smirnoff 		}
3533d375e524SGleb Smirnoff 	}
3534d375e524SGleb Smirnoff 
3535d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
3536d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
3537d375e524SGleb Smirnoff 	last->m_len += padlen;
3538d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
3539d375e524SGleb Smirnoff 
3540d375e524SGleb Smirnoff 	return (0);
3541d375e524SGleb Smirnoff }
3542d375e524SGleb Smirnoff 
3543d375e524SGleb Smirnoff /*
354495d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
354595d67482SBill Paul  * pointers to descriptors.
354695d67482SBill Paul  */
354795d67482SBill Paul static int
3548676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
354995d67482SBill Paul {
35507e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
3551f41ac2beSBill Paul 	bus_dmamap_t		map;
3552676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
3553676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
35547e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
3555676ad2c9SGleb Smirnoff 	uint16_t		csum_flags;
35567e27542aSGleb Smirnoff 	int			nsegs, i, error;
355795d67482SBill Paul 
35586909dc43SGleb Smirnoff 	csum_flags = 0;
35596909dc43SGleb Smirnoff 	if (m->m_pkthdr.csum_flags) {
35606909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
35616909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
35626909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
35636909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
35646909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
35656909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
35666909dc43SGleb Smirnoff 				m_freem(m);
35676909dc43SGleb Smirnoff 				*m_head = NULL;
35686909dc43SGleb Smirnoff 				return (error);
35696909dc43SGleb Smirnoff 			}
35706909dc43SGleb Smirnoff 		}
35716909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
35726909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
35736909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
35746909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
35756909dc43SGleb Smirnoff 	}
35766909dc43SGleb Smirnoff 
35777e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
3578676ad2c9SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m, segs,
3579676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
35807e27542aSGleb Smirnoff 	if (error == EFBIG) {
35814eee14cbSMarius Strobl 		m = m_collapse(m, M_DONTWAIT, BGE_NSEG_NEW);
3582676ad2c9SGleb Smirnoff 		if (m == NULL) {
3583676ad2c9SGleb Smirnoff 			m_freem(*m_head);
3584676ad2c9SGleb Smirnoff 			*m_head = NULL;
35857e27542aSGleb Smirnoff 			return (ENOBUFS);
35867e27542aSGleb Smirnoff 		}
3587676ad2c9SGleb Smirnoff 		*m_head = m;
3588676ad2c9SGleb Smirnoff 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m,
3589676ad2c9SGleb Smirnoff 		    segs, &nsegs, BUS_DMA_NOWAIT);
3590676ad2c9SGleb Smirnoff 		if (error) {
3591676ad2c9SGleb Smirnoff 			m_freem(m);
3592676ad2c9SGleb Smirnoff 			*m_head = NULL;
35937e27542aSGleb Smirnoff 			return (error);
35947e27542aSGleb Smirnoff 		}
3595676ad2c9SGleb Smirnoff 	} else if (error != 0)
3596676ad2c9SGleb Smirnoff 		return (error);
35977e27542aSGleb Smirnoff 
359895d67482SBill Paul 	/*
359995d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
360095d67482SBill Paul 	 * of the end of the ring.
360195d67482SBill Paul 	 */
36027e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
36037e27542aSGleb Smirnoff 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
360495d67482SBill Paul 		return (ENOBUFS);
36057e27542aSGleb Smirnoff 	}
36067e27542aSGleb Smirnoff 
3607e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
3608e65bed95SPyun YongHyeon 
36097e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
36107e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
36117e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
36127e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
36137e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
36147e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
36157e27542aSGleb Smirnoff 		if (i == nsegs - 1)
36167e27542aSGleb Smirnoff 			break;
36177e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
36187e27542aSGleb Smirnoff 	}
36197e27542aSGleb Smirnoff 
36207e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
36217e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
3622676ad2c9SGleb Smirnoff 
36237e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
36247e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
36254e35d186SJung-uk Kim #if __FreeBSD_version > 700022
362678ba57b9SAndre Oppermann 	if (m->m_flags & M_VLANTAG) {
36277e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
362878ba57b9SAndre Oppermann 		d->bge_vlan_tag = m->m_pkthdr.ether_vtag;
36297e27542aSGleb Smirnoff 	} else
36307e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
36314e35d186SJung-uk Kim #else
36324e35d186SJung-uk Kim 	{
36334e35d186SJung-uk Kim 		struct m_tag		*mtag;
36344e35d186SJung-uk Kim 
36354e35d186SJung-uk Kim 		if ((mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m)) != NULL) {
36364e35d186SJung-uk Kim 			d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
36374e35d186SJung-uk Kim 			d->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
36384e35d186SJung-uk Kim 		} else
36394e35d186SJung-uk Kim 			d->bge_vlan_tag = 0;
36404e35d186SJung-uk Kim 	}
36414e35d186SJung-uk Kim #endif
3642f41ac2beSBill Paul 
3643f41ac2beSBill Paul 	/*
3644f41ac2beSBill Paul 	 * Insure that the map for this transmission
3645f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
3646f41ac2beSBill Paul 	 * in this chain.
3647f41ac2beSBill Paul 	 */
36487e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
36497e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
3650676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
36517e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
365295d67482SBill Paul 
36537e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
36547e27542aSGleb Smirnoff 	*txidx = idx;
365595d67482SBill Paul 
365695d67482SBill Paul 	return (0);
365795d67482SBill Paul }
365895d67482SBill Paul 
365995d67482SBill Paul /*
366095d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
366195d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
366295d67482SBill Paul  */
366395d67482SBill Paul static void
36643f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
366595d67482SBill Paul {
366695d67482SBill Paul 	struct bge_softc *sc;
366795d67482SBill Paul 	struct mbuf *m_head = NULL;
366814bbd30fSGleb Smirnoff 	uint32_t prodidx;
3669303a718cSDag-Erling Smørgrav 	int count = 0;
367095d67482SBill Paul 
367195d67482SBill Paul 	sc = ifp->if_softc;
367295d67482SBill Paul 
3673dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
367495d67482SBill Paul 		return;
367595d67482SBill Paul 
367614bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
367795d67482SBill Paul 
367895d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
36794d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
368095d67482SBill Paul 		if (m_head == NULL)
368195d67482SBill Paul 			break;
368295d67482SBill Paul 
368395d67482SBill Paul 		/*
368495d67482SBill Paul 		 * XXX
3685b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
3686b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3687b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
3688b874fdd4SYaroslav Tykhiy 		 *
3689b874fdd4SYaroslav Tykhiy 		 * XXX
369095d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
369195d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
369295d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
369395d67482SBill Paul 		 * chain at once.
369495d67482SBill Paul 		 * (paranoia -- may not actually be needed)
369595d67482SBill Paul 		 */
369695d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
369795d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
369895d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
369995d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
37004d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
370113f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
370295d67482SBill Paul 				break;
370395d67482SBill Paul 			}
370495d67482SBill Paul 		}
370595d67482SBill Paul 
370695d67482SBill Paul 		/*
370795d67482SBill Paul 		 * Pack the data into the transmit ring. If we
370895d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
370995d67482SBill Paul 		 * for the NIC to drain the ring.
371095d67482SBill Paul 		 */
3711676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
3712676ad2c9SGleb Smirnoff 			if (m_head == NULL)
3713676ad2c9SGleb Smirnoff 				break;
37144d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
371513f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
371695d67482SBill Paul 			break;
371795d67482SBill Paul 		}
3718303a718cSDag-Erling Smørgrav 		++count;
371995d67482SBill Paul 
372095d67482SBill Paul 		/*
372195d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
372295d67482SBill Paul 		 * to him.
372395d67482SBill Paul 		 */
37244e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
372545ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
37264e35d186SJung-uk Kim #else
37274e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
37284e35d186SJung-uk Kim #endif
372995d67482SBill Paul 	}
373095d67482SBill Paul 
37313f74909aSGleb Smirnoff 	if (count == 0)
37323f74909aSGleb Smirnoff 		/* No packets were dequeued. */
3733303a718cSDag-Erling Smørgrav 		return;
3734303a718cSDag-Erling Smørgrav 
37353f74909aSGleb Smirnoff 	/* Transmit. */
373638cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
37373927098fSPaul Saab 	/* 5700 b2 errata */
3738e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
373938cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
374095d67482SBill Paul 
374114bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
374214bbd30fSGleb Smirnoff 
374395d67482SBill Paul 	/*
374495d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
374595d67482SBill Paul 	 */
3746b74e67fbSGleb Smirnoff 	sc->bge_timer = 5;
374795d67482SBill Paul }
374895d67482SBill Paul 
37490f9bd73bSSam Leffler /*
37500f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
37510f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
37520f9bd73bSSam Leffler  */
375395d67482SBill Paul static void
37543f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
375595d67482SBill Paul {
37560f9bd73bSSam Leffler 	struct bge_softc *sc;
37570f9bd73bSSam Leffler 
37580f9bd73bSSam Leffler 	sc = ifp->if_softc;
37590f9bd73bSSam Leffler 	BGE_LOCK(sc);
37600f9bd73bSSam Leffler 	bge_start_locked(ifp);
37610f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
37620f9bd73bSSam Leffler }
37630f9bd73bSSam Leffler 
37640f9bd73bSSam Leffler static void
37653f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
37660f9bd73bSSam Leffler {
376795d67482SBill Paul 	struct ifnet *ifp;
37683f74909aSGleb Smirnoff 	uint16_t *m;
376995d67482SBill Paul 
37700f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
377195d67482SBill Paul 
3772fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
377395d67482SBill Paul 
377413f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
377595d67482SBill Paul 		return;
377695d67482SBill Paul 
377795d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
377895d67482SBill Paul 	bge_stop(sc);
37798cb1383cSDoug Ambrisko 
37808cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
37818cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
378295d67482SBill Paul 	bge_reset(sc);
37838cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
37848cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
37858cb1383cSDoug Ambrisko 
378695d67482SBill Paul 	bge_chipinit(sc);
378795d67482SBill Paul 
378895d67482SBill Paul 	/*
378995d67482SBill Paul 	 * Init the various state machines, ring
379095d67482SBill Paul 	 * control blocks and firmware.
379195d67482SBill Paul 	 */
379295d67482SBill Paul 	if (bge_blockinit(sc)) {
3793fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
379495d67482SBill Paul 		return;
379595d67482SBill Paul 	}
379695d67482SBill Paul 
3797fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
379895d67482SBill Paul 
379995d67482SBill Paul 	/* Specify MTU. */
380095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3801cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
3802cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
380395d67482SBill Paul 
380495d67482SBill Paul 	/* Load our MAC address. */
38053f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
380695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
380795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
380895d67482SBill Paul 
38093e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
38103e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
381195d67482SBill Paul 
381295d67482SBill Paul 	/* Program multicast filter. */
381395d67482SBill Paul 	bge_setmulti(sc);
381495d67482SBill Paul 
3815cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
3816cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
3817cb2eacc7SYaroslav Tykhiy 
381895d67482SBill Paul 	/* Init RX ring. */
381995d67482SBill Paul 	bge_init_rx_ring_std(sc);
382095d67482SBill Paul 
38210434d1b8SBill Paul 	/*
38220434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
38230434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
38240434d1b8SBill Paul 	 * entry of the ring.
38250434d1b8SBill Paul 	 */
38260434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
38273f74909aSGleb Smirnoff 		uint32_t		v, i;
38280434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
38290434d1b8SBill Paul 			DELAY(20);
38300434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
38310434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
38320434d1b8SBill Paul 				break;
38330434d1b8SBill Paul 		}
38340434d1b8SBill Paul 		if (i == 10)
3835fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
3836fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
38370434d1b8SBill Paul 	}
38380434d1b8SBill Paul 
383995d67482SBill Paul 	/* Init jumbo RX ring. */
384095d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
384195d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
384295d67482SBill Paul 
38433f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
384495d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
384595d67482SBill Paul 
38467e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
38477e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
38487e6e2507SJung-uk Kim 
384995d67482SBill Paul 	/* Init TX ring. */
385095d67482SBill Paul 	bge_init_tx_ring(sc);
385195d67482SBill Paul 
38523f74909aSGleb Smirnoff 	/* Turn on transmitter. */
385395d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
385495d67482SBill Paul 
38553f74909aSGleb Smirnoff 	/* Turn on receiver. */
385695d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
385795d67482SBill Paul 
385895d67482SBill Paul 	/* Tell firmware we're alive. */
385995d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
386095d67482SBill Paul 
386175719184SGleb Smirnoff #ifdef DEVICE_POLLING
386275719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
386375719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
386475719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
386575719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
386638cc658fSJohn Baldwin 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
386775719184SGleb Smirnoff 	} else
386875719184SGleb Smirnoff #endif
386975719184SGleb Smirnoff 
387095d67482SBill Paul 	/* Enable host interrupts. */
387175719184SGleb Smirnoff 	{
387295d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
387395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
387438cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
387575719184SGleb Smirnoff 	}
387695d67482SBill Paul 
387767d5e043SOleg Bulyzhin 	bge_ifmedia_upd_locked(ifp);
387895d67482SBill Paul 
387913f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
388013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
388195d67482SBill Paul 
38820f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
38830f9bd73bSSam Leffler }
38840f9bd73bSSam Leffler 
38850f9bd73bSSam Leffler static void
38863f74909aSGleb Smirnoff bge_init(void *xsc)
38870f9bd73bSSam Leffler {
38880f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
38890f9bd73bSSam Leffler 
38900f9bd73bSSam Leffler 	BGE_LOCK(sc);
38910f9bd73bSSam Leffler 	bge_init_locked(sc);
38920f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
389395d67482SBill Paul }
389495d67482SBill Paul 
389595d67482SBill Paul /*
389695d67482SBill Paul  * Set media options.
389795d67482SBill Paul  */
389895d67482SBill Paul static int
38993f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
390095d67482SBill Paul {
390167d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
390267d5e043SOleg Bulyzhin 	int res;
390367d5e043SOleg Bulyzhin 
390467d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
390567d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
390667d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
390767d5e043SOleg Bulyzhin 
390867d5e043SOleg Bulyzhin 	return (res);
390967d5e043SOleg Bulyzhin }
391067d5e043SOleg Bulyzhin 
391167d5e043SOleg Bulyzhin static int
391267d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
391367d5e043SOleg Bulyzhin {
391467d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
391595d67482SBill Paul 	struct mii_data *mii;
39164f09c4c7SMarius Strobl 	struct mii_softc *miisc;
391795d67482SBill Paul 	struct ifmedia *ifm;
391895d67482SBill Paul 
391967d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
392067d5e043SOleg Bulyzhin 
392195d67482SBill Paul 	ifm = &sc->bge_ifmedia;
392295d67482SBill Paul 
392395d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
3924652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
392595d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
392695d67482SBill Paul 			return (EINVAL);
392795d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
392895d67482SBill Paul 		case IFM_AUTO:
3929ff50922bSDoug White 			/*
3930ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
3931ff50922bSDoug White 			 * mechanism for programming the autoneg
3932ff50922bSDoug White 			 * advertisement registers in TBI mode.
3933ff50922bSDoug White 			 */
39340f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3935ff50922bSDoug White 				uint32_t sgdig;
39360f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
39370f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
3938ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3939ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3940ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
3941ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
3942ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
3943ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3944ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
3945ff50922bSDoug White 					DELAY(5);
3946ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3947ff50922bSDoug White 				}
39480f89fde2SJung-uk Kim 			}
394995d67482SBill Paul 			break;
395095d67482SBill Paul 		case IFM_1000_SX:
395195d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
395295d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
395395d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
395495d67482SBill Paul 			} else {
395595d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
395695d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
395795d67482SBill Paul 			}
395895d67482SBill Paul 			break;
395995d67482SBill Paul 		default:
396095d67482SBill Paul 			return (EINVAL);
396195d67482SBill Paul 		}
396295d67482SBill Paul 		return (0);
396395d67482SBill Paul 	}
396495d67482SBill Paul 
39651493e883SOleg Bulyzhin 	sc->bge_link_evt++;
396695d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
39674f09c4c7SMarius Strobl 	if (mii->mii_instance)
39684f09c4c7SMarius Strobl 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
396995d67482SBill Paul 			mii_phy_reset(miisc);
397095d67482SBill Paul 	mii_mediachg(mii);
397195d67482SBill Paul 
3972902827f6SBjoern A. Zeeb 	/*
3973902827f6SBjoern A. Zeeb 	 * Force an interrupt so that we will call bge_link_upd
3974902827f6SBjoern A. Zeeb 	 * if needed and clear any pending link state attention.
3975902827f6SBjoern A. Zeeb 	 * Without this we are not getting any further interrupts
3976902827f6SBjoern A. Zeeb 	 * for link state changes and thus will not UP the link and
3977902827f6SBjoern A. Zeeb 	 * not be able to send in bge_start_locked. The only
3978902827f6SBjoern A. Zeeb 	 * way to get things working was to receive a packet and
3979902827f6SBjoern A. Zeeb 	 * get an RX intr.
3980902827f6SBjoern A. Zeeb 	 * bge_tick should help for fiber cards and we might not
3981902827f6SBjoern A. Zeeb 	 * need to do this here if BGE_FLAG_TBI is set but as
3982902827f6SBjoern A. Zeeb 	 * we poll for fiber anyway it should not harm.
3983902827f6SBjoern A. Zeeb 	 */
39844f0794ffSBjoern A. Zeeb 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
39854f0794ffSBjoern A. Zeeb 	    sc->bge_flags & BGE_FLAG_5788)
3986902827f6SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
39874f0794ffSBjoern A. Zeeb 	else
398863ccfe30SBjoern A. Zeeb 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
3989902827f6SBjoern A. Zeeb 
399095d67482SBill Paul 	return (0);
399195d67482SBill Paul }
399295d67482SBill Paul 
399395d67482SBill Paul /*
399495d67482SBill Paul  * Report current media status.
399595d67482SBill Paul  */
399695d67482SBill Paul static void
39973f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
399895d67482SBill Paul {
399967d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
400095d67482SBill Paul 	struct mii_data *mii;
400195d67482SBill Paul 
400267d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
400395d67482SBill Paul 
4004652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
400595d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
400695d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
400795d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
400895d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
400995d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
40104c0da0ffSGleb Smirnoff 		else {
40114c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
401267d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
40134c0da0ffSGleb Smirnoff 			return;
40144c0da0ffSGleb Smirnoff 		}
401595d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
401695d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
401795d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
401895d67482SBill Paul 		else
401995d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
402067d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
402195d67482SBill Paul 		return;
402295d67482SBill Paul 	}
402395d67482SBill Paul 
402495d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
402595d67482SBill Paul 	mii_pollstat(mii);
402695d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
402795d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
402867d5e043SOleg Bulyzhin 
402967d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
403095d67482SBill Paul }
403195d67482SBill Paul 
403295d67482SBill Paul static int
40333f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
403495d67482SBill Paul {
403595d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
403695d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
403795d67482SBill Paul 	struct mii_data *mii;
4038f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
403995d67482SBill Paul 
404095d67482SBill Paul 	switch (command) {
404195d67482SBill Paul 	case SIOCSIFMTU:
40424c0da0ffSGleb Smirnoff 		if (ifr->ifr_mtu < ETHERMIN ||
40434c0da0ffSGleb Smirnoff 		    ((BGE_IS_JUMBO_CAPABLE(sc)) &&
40444c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > BGE_JUMBO_MTU) ||
40454c0da0ffSGleb Smirnoff 		    ((!BGE_IS_JUMBO_CAPABLE(sc)) &&
40464c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > ETHERMTU))
404795d67482SBill Paul 			error = EINVAL;
40484c0da0ffSGleb Smirnoff 		else if (ifp->if_mtu != ifr->ifr_mtu) {
404995d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
405013f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
405195d67482SBill Paul 			bge_init(sc);
405295d67482SBill Paul 		}
405395d67482SBill Paul 		break;
405495d67482SBill Paul 	case SIOCSIFFLAGS:
40550f9bd73bSSam Leffler 		BGE_LOCK(sc);
405695d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
405795d67482SBill Paul 			/*
405895d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
405995d67482SBill Paul 			 * then just use the 'set promisc mode' command
406095d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
406195d67482SBill Paul 			 * a full re-init means reloading the firmware and
406295d67482SBill Paul 			 * waiting for it to start up, which may take a
4063d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
406495d67482SBill Paul 			 */
4065f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4066f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
40673e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
40683e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
4069f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
4070d183af7fSRuslan Ermilov 					bge_setmulti(sc);
407195d67482SBill Paul 			} else
40720f9bd73bSSam Leffler 				bge_init_locked(sc);
407395d67482SBill Paul 		} else {
407413f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
407595d67482SBill Paul 				bge_stop(sc);
407695d67482SBill Paul 			}
407795d67482SBill Paul 		}
407895d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
40790f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
408095d67482SBill Paul 		error = 0;
408195d67482SBill Paul 		break;
408295d67482SBill Paul 	case SIOCADDMULTI:
408395d67482SBill Paul 	case SIOCDELMULTI:
408413f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
40850f9bd73bSSam Leffler 			BGE_LOCK(sc);
408695d67482SBill Paul 			bge_setmulti(sc);
40870f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
408895d67482SBill Paul 			error = 0;
408995d67482SBill Paul 		}
409095d67482SBill Paul 		break;
409195d67482SBill Paul 	case SIOCSIFMEDIA:
409295d67482SBill Paul 	case SIOCGIFMEDIA:
4093652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
409495d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
409595d67482SBill Paul 			    &sc->bge_ifmedia, command);
409695d67482SBill Paul 		} else {
409795d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
409895d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
409995d67482SBill Paul 			    &mii->mii_media, command);
410095d67482SBill Paul 		}
410195d67482SBill Paul 		break;
410295d67482SBill Paul 	case SIOCSIFCAP:
410395d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
410475719184SGleb Smirnoff #ifdef DEVICE_POLLING
410575719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
410675719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
410775719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
410875719184SGleb Smirnoff 				if (error)
410975719184SGleb Smirnoff 					return (error);
411075719184SGleb Smirnoff 				BGE_LOCK(sc);
411175719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
411275719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
411338cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
411475719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
411575719184SGleb Smirnoff 				BGE_UNLOCK(sc);
411675719184SGleb Smirnoff 			} else {
411775719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
411875719184SGleb Smirnoff 				/* Enable interrupt even in error case */
411975719184SGleb Smirnoff 				BGE_LOCK(sc);
412075719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
412175719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
412238cc658fSJohn Baldwin 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
412375719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
412475719184SGleb Smirnoff 				BGE_UNLOCK(sc);
412575719184SGleb Smirnoff 			}
412675719184SGleb Smirnoff 		}
412775719184SGleb Smirnoff #endif
4128d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
4129d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
4130d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
4131d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
4132b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
413395d67482SBill Paul 			else
4134b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
41354e35d186SJung-uk Kim #ifdef VLAN_CAPABILITIES
4136479b23b7SGleb Smirnoff 			VLAN_CAPABILITIES(ifp);
41374e35d186SJung-uk Kim #endif
413895d67482SBill Paul 		}
4139cb2eacc7SYaroslav Tykhiy 
4140cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
4141cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
4142cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
4143cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
4144cb2eacc7SYaroslav Tykhiy 		}
4145cb2eacc7SYaroslav Tykhiy 
4146cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_HWTAGGING) {
4147cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
4148cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
4149cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
4150cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
4151cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
4152cb2eacc7SYaroslav Tykhiy 			VLAN_CAPABILITIES(ifp);
4153cb2eacc7SYaroslav Tykhiy #endif
4154cb2eacc7SYaroslav Tykhiy 		}
4155cb2eacc7SYaroslav Tykhiy 
415695d67482SBill Paul 		break;
415795d67482SBill Paul 	default:
4158673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
415995d67482SBill Paul 		break;
416095d67482SBill Paul 	}
416195d67482SBill Paul 
416295d67482SBill Paul 	return (error);
416395d67482SBill Paul }
416495d67482SBill Paul 
416595d67482SBill Paul static void
4166b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
416795d67482SBill Paul {
4168b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
416995d67482SBill Paul 
4170b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
4171b74e67fbSGleb Smirnoff 
4172b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
4173b74e67fbSGleb Smirnoff 		return;
4174b74e67fbSGleb Smirnoff 
4175b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
417695d67482SBill Paul 
4177fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
417895d67482SBill Paul 
417913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
4180426742bfSGleb Smirnoff 	bge_init_locked(sc);
418195d67482SBill Paul 
418295d67482SBill Paul 	ifp->if_oerrors++;
418395d67482SBill Paul }
418495d67482SBill Paul 
418595d67482SBill Paul /*
418695d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
418795d67482SBill Paul  * RX and TX lists.
418895d67482SBill Paul  */
418995d67482SBill Paul static void
41903f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
419195d67482SBill Paul {
419295d67482SBill Paul 	struct ifnet *ifp;
419395d67482SBill Paul 	struct ifmedia_entry *ifm;
419495d67482SBill Paul 	struct mii_data *mii = NULL;
419595d67482SBill Paul 	int mtmp, itmp;
419695d67482SBill Paul 
41970f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
41980f9bd73bSSam Leffler 
4199fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
420095d67482SBill Paul 
4201652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0)
420295d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
420395d67482SBill Paul 
42040f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
420595d67482SBill Paul 
420695d67482SBill Paul 	/*
42073f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
420895d67482SBill Paul 	 */
420995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
421095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
421195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
42127ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
421395d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
421495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
421595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
421695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
421795d67482SBill Paul 
421895d67482SBill Paul 	/*
42193f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
422095d67482SBill Paul 	 */
422195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
422295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
422395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
422495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
422595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
42267ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
422795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
422895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
422995d67482SBill Paul 
423095d67482SBill Paul 	/*
423195d67482SBill Paul 	 * Shut down all of the memory managers and related
423295d67482SBill Paul 	 * state machines.
423395d67482SBill Paul 	 */
423495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
423595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
42367ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
423795d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
42380c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
423995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
42407ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
424195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
424295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
42430434d1b8SBill Paul 	}
424495d67482SBill Paul 
424595d67482SBill Paul 	/* Disable host interrupts. */
424695d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
424738cc658fSJohn Baldwin 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
424895d67482SBill Paul 
424995d67482SBill Paul 	/*
425095d67482SBill Paul 	 * Tell firmware we're shutting down.
425195d67482SBill Paul 	 */
42528cb1383cSDoug Ambrisko 
42538cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
42548cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
42558cb1383cSDoug Ambrisko 	bge_reset(sc);
42568cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
42578cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
42588cb1383cSDoug Ambrisko 
42598cb1383cSDoug Ambrisko 	/*
42608cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
42618cb1383cSDoug Ambrisko 	 */
42628cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
42638cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
42648cb1383cSDoug Ambrisko 	else
426595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
426695d67482SBill Paul 
426795d67482SBill Paul 	/* Free the RX lists. */
426895d67482SBill Paul 	bge_free_rx_ring_std(sc);
426995d67482SBill Paul 
427095d67482SBill Paul 	/* Free jumbo RX list. */
42714c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
427295d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
427395d67482SBill Paul 
427495d67482SBill Paul 	/* Free TX buffers. */
427595d67482SBill Paul 	bge_free_tx_ring(sc);
427695d67482SBill Paul 
427795d67482SBill Paul 	/*
427895d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
427995d67482SBill Paul 	 * unchanged so that things will be put back to normal when
428095d67482SBill Paul 	 * we bring the interface back up.
428195d67482SBill Paul 	 */
4282652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
428395d67482SBill Paul 		itmp = ifp->if_flags;
428495d67482SBill Paul 		ifp->if_flags |= IFF_UP;
4285dcc34049SPawel Jakub Dawidek 		/*
4286dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
4287dcc34049SPawel Jakub Dawidek 		 */
4288dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
428995d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
429095d67482SBill Paul 			mtmp = ifm->ifm_media;
429195d67482SBill Paul 			ifm->ifm_media = IFM_ETHER | IFM_NONE;
429295d67482SBill Paul 			mii_mediachg(mii);
429395d67482SBill Paul 			ifm->ifm_media = mtmp;
4294dcc34049SPawel Jakub Dawidek 		}
429595d67482SBill Paul 		ifp->if_flags = itmp;
429695d67482SBill Paul 	}
429795d67482SBill Paul 
429895d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
429995d67482SBill Paul 
43005dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
43011493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
43021493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
43031493e883SOleg Bulyzhin 	sc->bge_link = 0;
430495d67482SBill Paul 
43051493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
430695d67482SBill Paul }
430795d67482SBill Paul 
430895d67482SBill Paul /*
430995d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
431095d67482SBill Paul  * get confused by errant DMAs when rebooting.
431195d67482SBill Paul  */
4312b6c974e8SWarner Losh static int
43133f74909aSGleb Smirnoff bge_shutdown(device_t dev)
431495d67482SBill Paul {
431595d67482SBill Paul 	struct bge_softc *sc;
431695d67482SBill Paul 
431795d67482SBill Paul 	sc = device_get_softc(dev);
43180f9bd73bSSam Leffler 	BGE_LOCK(sc);
431995d67482SBill Paul 	bge_stop(sc);
432095d67482SBill Paul 	bge_reset(sc);
43210f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
4322b6c974e8SWarner Losh 
4323b6c974e8SWarner Losh 	return (0);
432495d67482SBill Paul }
432514afefa3SPawel Jakub Dawidek 
432614afefa3SPawel Jakub Dawidek static int
432714afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
432814afefa3SPawel Jakub Dawidek {
432914afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
433014afefa3SPawel Jakub Dawidek 
433114afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
433214afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
433314afefa3SPawel Jakub Dawidek 	bge_stop(sc);
433414afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
433514afefa3SPawel Jakub Dawidek 
433614afefa3SPawel Jakub Dawidek 	return (0);
433714afefa3SPawel Jakub Dawidek }
433814afefa3SPawel Jakub Dawidek 
433914afefa3SPawel Jakub Dawidek static int
434014afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
434114afefa3SPawel Jakub Dawidek {
434214afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
434314afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
434414afefa3SPawel Jakub Dawidek 
434514afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
434614afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
434714afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
434814afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
434914afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
435014afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
435114afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
435214afefa3SPawel Jakub Dawidek 	}
435314afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
435414afefa3SPawel Jakub Dawidek 
435514afefa3SPawel Jakub Dawidek 	return (0);
435614afefa3SPawel Jakub Dawidek }
4357dab5cd05SOleg Bulyzhin 
4358dab5cd05SOleg Bulyzhin static void
43593f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
4360dab5cd05SOleg Bulyzhin {
43611f313773SOleg Bulyzhin 	struct mii_data *mii;
43621f313773SOleg Bulyzhin 	uint32_t link, status;
4363dab5cd05SOleg Bulyzhin 
4364dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
43651f313773SOleg Bulyzhin 
43663f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
43677b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
43687b97099dSOleg Bulyzhin 
4369dab5cd05SOleg Bulyzhin 	/*
4370dab5cd05SOleg Bulyzhin 	 * Process link state changes.
4371dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
4372dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
4373dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
4374dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
4375dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
4376dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
4377dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
4378dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
43791f313773SOleg Bulyzhin 	 *
43801f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
43814c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
4382dab5cd05SOleg Bulyzhin 	 */
4383dab5cd05SOleg Bulyzhin 
43841f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
43854c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
4386dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
4387dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
43881f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
43895dda8085SOleg Bulyzhin 			mii_pollstat(mii);
43901f313773SOleg Bulyzhin 			if (!sc->bge_link &&
43911f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
43921f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
43931f313773SOleg Bulyzhin 				sc->bge_link++;
43941f313773SOleg Bulyzhin 				if (bootverbose)
43951f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
43961f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
43971f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
43981f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
43991f313773SOleg Bulyzhin 				sc->bge_link = 0;
44001f313773SOleg Bulyzhin 				if (bootverbose)
44011f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
44021f313773SOleg Bulyzhin 			}
44031f313773SOleg Bulyzhin 
44043f74909aSGleb Smirnoff 			/* Clear the interrupt. */
4405dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
4406dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
4407dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
4408dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
4409dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
4410dab5cd05SOleg Bulyzhin 		}
4411dab5cd05SOleg Bulyzhin 		return;
4412dab5cd05SOleg Bulyzhin 	}
4413dab5cd05SOleg Bulyzhin 
4414652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
44151f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
44167b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
44177b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
44181f313773SOleg Bulyzhin 				sc->bge_link++;
44191f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
44201f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
44211f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
44220c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
44231f313773SOleg Bulyzhin 				if (bootverbose)
44241f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
44253f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
44263f74909aSGleb Smirnoff 				    LINK_STATE_UP);
44277b97099dSOleg Bulyzhin 			}
44281f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
4429dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
44301f313773SOleg Bulyzhin 			if (bootverbose)
44311f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
44327b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
44331f313773SOleg Bulyzhin 		}
44341493e883SOleg Bulyzhin 	} else if (CSR_READ_4(sc, BGE_MI_MODE) & BGE_MIMODE_AUTOPOLL) {
44351f313773SOleg Bulyzhin 		/*
44360c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
44370c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
44380c8aa4eaSJung-uk Kim 		 * PHY link status directly.
44391f313773SOleg Bulyzhin 		 */
44401f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
44411f313773SOleg Bulyzhin 
44421f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
44431f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
44441f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
44455dda8085SOleg Bulyzhin 			mii_pollstat(mii);
44461f313773SOleg Bulyzhin 			if (!sc->bge_link &&
44471f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
44481f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
44491f313773SOleg Bulyzhin 				sc->bge_link++;
44501f313773SOleg Bulyzhin 				if (bootverbose)
44511f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
44521f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
44531f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
44541f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
44551f313773SOleg Bulyzhin 				sc->bge_link = 0;
44561f313773SOleg Bulyzhin 				if (bootverbose)
44571f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
44581f313773SOleg Bulyzhin 			}
44591f313773SOleg Bulyzhin 		}
44600c8aa4eaSJung-uk Kim 	} else {
44610c8aa4eaSJung-uk Kim 		/*
44620c8aa4eaSJung-uk Kim 		 * Discard link events for MII/GMII controllers
44630c8aa4eaSJung-uk Kim 		 * if MI auto-polling is disabled.
44640c8aa4eaSJung-uk Kim 		 */
4465dab5cd05SOleg Bulyzhin 	}
4466dab5cd05SOleg Bulyzhin 
44673f74909aSGleb Smirnoff 	/* Clear the attention. */
4468dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
4469dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
4470dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
4471dab5cd05SOleg Bulyzhin }
44726f8718a3SScott Long 
4473763757b2SScott Long #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
447406e83c7eSScott Long 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
4475763757b2SScott Long 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
4476763757b2SScott Long 	    desc)
4477763757b2SScott Long 
44786f8718a3SScott Long static void
44796f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
44806f8718a3SScott Long {
44816f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
4482763757b2SScott Long 	struct sysctl_oid_list *children, *schildren;
4483763757b2SScott Long 	struct sysctl_oid *tree;
44846f8718a3SScott Long 
44856f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
44866f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
44876f8718a3SScott Long 
44886f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
44896f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
44906f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
44916f8718a3SScott Long 	    "Debug Information");
44926f8718a3SScott Long 
44936f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
44946f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
44956f8718a3SScott Long 	    "Register Read");
44966f8718a3SScott Long 
44976f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
44986f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
44996f8718a3SScott Long 	    "Memory Read");
45006f8718a3SScott Long 
45016f8718a3SScott Long #endif
4502763757b2SScott Long 
4503d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
4504d949071dSJung-uk Kim 		return;
4505d949071dSJung-uk Kim 
4506763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats", CTLFLAG_RD,
4507763757b2SScott Long 	    NULL, "BGE Statistics");
4508763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
4509763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
4510763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
4511763757b2SScott Long 	    "FramesDroppedDueToFilters");
4512763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
4513763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
4514763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
4515763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
4516763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
4517763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
451806e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
451906e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
452006e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
452106e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
4522763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
4523763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
4524763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
4525763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
4526763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
4527763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
4528763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
4529763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
4530763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
4531763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
4532763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
4533763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
4534763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
4535763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
4536763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
4537763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
4538763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
4539763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
4540763757b2SScott Long 
4541763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
4542763757b2SScott Long 	    NULL, "BGE RX Statistics");
4543763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
4544763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
4545763757b2SScott Long 	    children, rxstats.ifHCInOctets, "Octets");
4546763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
4547763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
4548763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
4549763757b2SScott Long 	    children, rxstats.ifHCInUcastPkts, "UcastPkts");
4550763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
4551763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
4552763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
4553763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
4554763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
4555763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
4556763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
4557763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
4558763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
4559763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
4560763757b2SScott Long 	    "xoffPauseFramesReceived");
4561763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
4562763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
4563763757b2SScott Long 	    "ControlFramesReceived");
4564763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
4565763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
4566763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
4567763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
4568763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
4569763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
4570763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
4571763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
4572763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
457306e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
4574763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
457506e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
4576763757b2SScott Long 
4577763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
4578763757b2SScott Long 	    NULL, "BGE TX Statistics");
4579763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
4580763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
4581763757b2SScott Long 	    children, txstats.ifHCOutOctets, "Octets");
4582763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
4583763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
4584763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
4585763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
4586763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
4587763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
4588763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
4589763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
4590763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
4591763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
4592763757b2SScott Long 	    "InternalMacTransmitErrors");
4593763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
4594763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
4595763757b2SScott Long 	    "SingleCollisionFrames");
4596763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
4597763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
4598763757b2SScott Long 	    "MultipleCollisionFrames");
4599763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
4600763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
4601763757b2SScott Long 	    "DeferredTransmissions");
4602763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
4603763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
4604763757b2SScott Long 	    "ExcessiveCollisions");
4605763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
460606e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
460706e83c7eSScott Long 	    "LateCollisions");
4608763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
4609763757b2SScott Long 	    children, txstats.ifHCOutUcastPkts, "UcastPkts");
4610763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
4611763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
4612763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
4613763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
4614763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
4615763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
4616763757b2SScott Long 	    "CarrierSenseErrors");
4617763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
4618763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
4619763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
4620763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
4621763757b2SScott Long }
4622763757b2SScott Long 
4623763757b2SScott Long static int
4624763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
4625763757b2SScott Long {
4626763757b2SScott Long 	struct bge_softc *sc;
462706e83c7eSScott Long 	uint32_t result;
4628d949071dSJung-uk Kim 	int offset;
4629763757b2SScott Long 
4630763757b2SScott Long 	sc = (struct bge_softc *)arg1;
4631763757b2SScott Long 	offset = arg2;
4632d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
4633d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
4634041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
46356f8718a3SScott Long }
46366f8718a3SScott Long 
46376f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
46386f8718a3SScott Long static int
46396f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
46406f8718a3SScott Long {
46416f8718a3SScott Long 	struct bge_softc *sc;
46426f8718a3SScott Long 	uint16_t *sbdata;
46436f8718a3SScott Long 	int error;
46446f8718a3SScott Long 	int result;
46456f8718a3SScott Long 	int i, j;
46466f8718a3SScott Long 
46476f8718a3SScott Long 	result = -1;
46486f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
46496f8718a3SScott Long 	if (error || (req->newptr == NULL))
46506f8718a3SScott Long 		return (error);
46516f8718a3SScott Long 
46526f8718a3SScott Long 	if (result == 1) {
46536f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
46546f8718a3SScott Long 
46556f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
46566f8718a3SScott Long 		printf("Status Block:\n");
46576f8718a3SScott Long 		for (i = 0x0; i < (BGE_STATUS_BLK_SZ / 4); ) {
46586f8718a3SScott Long 			printf("%06x:", i);
46596f8718a3SScott Long 			for (j = 0; j < 8; j++) {
46606f8718a3SScott Long 				printf(" %04x", sbdata[i]);
46616f8718a3SScott Long 				i += 4;
46626f8718a3SScott Long 			}
46636f8718a3SScott Long 			printf("\n");
46646f8718a3SScott Long 		}
46656f8718a3SScott Long 
46666f8718a3SScott Long 		printf("Registers:\n");
46670c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
46686f8718a3SScott Long 			printf("%06x:", i);
46696f8718a3SScott Long 			for (j = 0; j < 8; j++) {
46706f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
46716f8718a3SScott Long 				i += 4;
46726f8718a3SScott Long 			}
46736f8718a3SScott Long 			printf("\n");
46746f8718a3SScott Long 		}
46756f8718a3SScott Long 
46766f8718a3SScott Long 		printf("Hardware Flags:\n");
46775345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
46786f8718a3SScott Long 			printf(" - 575X Plus\n");
46795345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
46806f8718a3SScott Long 			printf(" - 5705 Plus\n");
46815345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
46825345bad0SScott Long 			printf(" - 5714 Family\n");
46835345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
46845345bad0SScott Long 			printf(" - 5700 Family\n");
46856f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
46866f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
46876f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
46886f8718a3SScott Long 			printf(" - PCI-X Bus\n");
46896f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
46906f8718a3SScott Long 			printf(" - PCI Express Bus\n");
46915ee49a3aSJung-uk Kim 		if (sc->bge_flags & BGE_FLAG_NO_3LED)
46926f8718a3SScott Long 			printf(" - No 3 LEDs\n");
46936f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
46946f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
46956f8718a3SScott Long 	}
46966f8718a3SScott Long 
46976f8718a3SScott Long 	return (error);
46986f8718a3SScott Long }
46996f8718a3SScott Long 
47006f8718a3SScott Long static int
47016f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
47026f8718a3SScott Long {
47036f8718a3SScott Long 	struct bge_softc *sc;
47046f8718a3SScott Long 	int error;
47056f8718a3SScott Long 	uint16_t result;
47066f8718a3SScott Long 	uint32_t val;
47076f8718a3SScott Long 
47086f8718a3SScott Long 	result = -1;
47096f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
47106f8718a3SScott Long 	if (error || (req->newptr == NULL))
47116f8718a3SScott Long 		return (error);
47126f8718a3SScott Long 
47136f8718a3SScott Long 	if (result < 0x8000) {
47146f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
47156f8718a3SScott Long 		val = CSR_READ_4(sc, result);
47166f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
47176f8718a3SScott Long 	}
47186f8718a3SScott Long 
47196f8718a3SScott Long 	return (error);
47206f8718a3SScott Long }
47216f8718a3SScott Long 
47226f8718a3SScott Long static int
47236f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
47246f8718a3SScott Long {
47256f8718a3SScott Long 	struct bge_softc *sc;
47266f8718a3SScott Long 	int error;
47276f8718a3SScott Long 	uint16_t result;
47286f8718a3SScott Long 	uint32_t val;
47296f8718a3SScott Long 
47306f8718a3SScott Long 	result = -1;
47316f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
47326f8718a3SScott Long 	if (error || (req->newptr == NULL))
47336f8718a3SScott Long 		return (error);
47346f8718a3SScott Long 
47356f8718a3SScott Long 	if (result < 0x8000) {
47366f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
47376f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
47386f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
47396f8718a3SScott Long 	}
47406f8718a3SScott Long 
47416f8718a3SScott Long 	return (error);
47426f8718a3SScott Long }
47436f8718a3SScott Long #endif
474438cc658fSJohn Baldwin 
474538cc658fSJohn Baldwin static int
47465fea260fSMarius Strobl bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
47475fea260fSMarius Strobl {
47485fea260fSMarius Strobl 
47495fea260fSMarius Strobl 	if (sc->bge_flags & BGE_FLAG_EADDR)
47505fea260fSMarius Strobl 		return (1);
47515fea260fSMarius Strobl 
47525fea260fSMarius Strobl #ifdef __sparc64__
47535fea260fSMarius Strobl 	OF_getetheraddr(sc->bge_dev, ether_addr);
47545fea260fSMarius Strobl 	return (0);
47555fea260fSMarius Strobl #endif
47565fea260fSMarius Strobl 	return (1);
47575fea260fSMarius Strobl }
47585fea260fSMarius Strobl 
47595fea260fSMarius Strobl static int
476038cc658fSJohn Baldwin bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
476138cc658fSJohn Baldwin {
476238cc658fSJohn Baldwin 	uint32_t mac_addr;
476338cc658fSJohn Baldwin 
476438cc658fSJohn Baldwin 	mac_addr = bge_readmem_ind(sc, 0x0c14);
476538cc658fSJohn Baldwin 	if ((mac_addr >> 16) == 0x484b) {
476638cc658fSJohn Baldwin 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
476738cc658fSJohn Baldwin 		ether_addr[1] = (uint8_t)mac_addr;
476838cc658fSJohn Baldwin 		mac_addr = bge_readmem_ind(sc, 0x0c18);
476938cc658fSJohn Baldwin 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
477038cc658fSJohn Baldwin 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
477138cc658fSJohn Baldwin 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
477238cc658fSJohn Baldwin 		ether_addr[5] = (uint8_t)mac_addr;
47735fea260fSMarius Strobl 		return (0);
477438cc658fSJohn Baldwin 	}
47755fea260fSMarius Strobl 	return (1);
477638cc658fSJohn Baldwin }
477738cc658fSJohn Baldwin 
477838cc658fSJohn Baldwin static int
477938cc658fSJohn Baldwin bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
478038cc658fSJohn Baldwin {
478138cc658fSJohn Baldwin 	int mac_offset = BGE_EE_MAC_OFFSET;
478238cc658fSJohn Baldwin 
478338cc658fSJohn Baldwin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
478438cc658fSJohn Baldwin 		mac_offset = BGE_EE_MAC_OFFSET_5906;
478538cc658fSJohn Baldwin 
47865fea260fSMarius Strobl 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
47875fea260fSMarius Strobl 	    ETHER_ADDR_LEN));
478838cc658fSJohn Baldwin }
478938cc658fSJohn Baldwin 
479038cc658fSJohn Baldwin static int
479138cc658fSJohn Baldwin bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
479238cc658fSJohn Baldwin {
479338cc658fSJohn Baldwin 
47945fea260fSMarius Strobl 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
47955fea260fSMarius Strobl 		return (1);
47965fea260fSMarius Strobl 
47975fea260fSMarius Strobl 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
47985fea260fSMarius Strobl 	   ETHER_ADDR_LEN));
479938cc658fSJohn Baldwin }
480038cc658fSJohn Baldwin 
480138cc658fSJohn Baldwin static int
480238cc658fSJohn Baldwin bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
480338cc658fSJohn Baldwin {
480438cc658fSJohn Baldwin 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
480538cc658fSJohn Baldwin 		/* NOTE: Order is critical */
48065fea260fSMarius Strobl 		bge_get_eaddr_fw,
480738cc658fSJohn Baldwin 		bge_get_eaddr_mem,
480838cc658fSJohn Baldwin 		bge_get_eaddr_nvram,
480938cc658fSJohn Baldwin 		bge_get_eaddr_eeprom,
481038cc658fSJohn Baldwin 		NULL
481138cc658fSJohn Baldwin 	};
481238cc658fSJohn Baldwin 	const bge_eaddr_fcn_t *func;
481338cc658fSJohn Baldwin 
481438cc658fSJohn Baldwin 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
481538cc658fSJohn Baldwin 		if ((*func)(sc, eaddr) == 0)
481638cc658fSJohn Baldwin 			break;
481738cc658fSJohn Baldwin 	}
481838cc658fSJohn Baldwin 	return (*func == NULL ? ENXIO : 0);
481938cc658fSJohn Baldwin }
4820