xref: /freebsd/sys/dev/bge/if_bge.c (revision 676ad2c9ef4837c96e4ab1a28c2b9d93cded15fc)
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>
8295d67482SBill Paul 
8395d67482SBill Paul #include <net/if.h>
8495d67482SBill Paul #include <net/if_arp.h>
8595d67482SBill Paul #include <net/ethernet.h>
8695d67482SBill Paul #include <net/if_dl.h>
8795d67482SBill Paul #include <net/if_media.h>
8895d67482SBill Paul 
8995d67482SBill Paul #include <net/bpf.h>
9095d67482SBill Paul 
9195d67482SBill Paul #include <net/if_types.h>
9295d67482SBill Paul #include <net/if_vlan_var.h>
9395d67482SBill Paul 
9495d67482SBill Paul #include <netinet/in_systm.h>
9595d67482SBill Paul #include <netinet/in.h>
9695d67482SBill Paul #include <netinet/ip.h>
9795d67482SBill Paul 
9895d67482SBill Paul #include <machine/bus.h>
9995d67482SBill Paul #include <machine/resource.h>
10095d67482SBill Paul #include <sys/bus.h>
10195d67482SBill Paul #include <sys/rman.h>
10295d67482SBill Paul 
10395d67482SBill Paul #include <dev/mii/mii.h>
10495d67482SBill Paul #include <dev/mii/miivar.h>
1052d3ce713SDavid E. O'Brien #include "miidevs.h"
10695d67482SBill Paul #include <dev/mii/brgphyreg.h>
10795d67482SBill Paul 
1084fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1094fbd232cSWarner Losh #include <dev/pci/pcivar.h>
11095d67482SBill Paul 
11195d67482SBill Paul #include <dev/bge/if_bgereg.h>
11295d67482SBill Paul 
1135ddc5794SJohn Polstra #define BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
114d375e524SGleb Smirnoff #define ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
11595d67482SBill Paul 
116f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
117f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
11895d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
11995d67482SBill Paul 
1207b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
12195d67482SBill Paul #include "miibus_if.h"
12295d67482SBill Paul 
12395d67482SBill Paul /*
12495d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
12595d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
12695d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
12795d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
12895d67482SBill Paul  */
1294c0da0ffSGleb Smirnoff static struct bge_type {
1304c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1314c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
1324c0da0ffSGleb Smirnoff } bge_devs[] = {
1334c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5700 },
1344c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5701 },
13595d67482SBill Paul 
1364c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1000 },
1374c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1002 },
1384c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC9100 },
1394c0da0ffSGleb Smirnoff 
1404c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	APPLE_DEVICE_BCM5701 },
1414c0da0ffSGleb Smirnoff 
1424c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5700 },
1434c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5701 },
1444c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702 },
1454c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702_ALT },
1464c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702X },
1474c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703 },
1484c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703_ALT },
1494c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703X },
1504c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704C },
1514c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S },
1524c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S_ALT },
1534c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705 },
1544c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705F },
1554c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705K },
1564c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M },
1574c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M_ALT },
1584c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714C },
1594c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714S },
1604c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715 },
1614c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715S },
1624c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1634c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
1644c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1654c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1664c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1674c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1684c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1694c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1704c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1714c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1724c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1734c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1749e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1759e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1769e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1779e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
1784c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
1794c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
1804c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
1814c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
1829e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
1839e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
1849e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
1854c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
1864c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
1874c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
1884c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
1894c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
1904c0da0ffSGleb Smirnoff 
1914c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
1924c0da0ffSGleb Smirnoff 
1934c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C985 },
1944c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
1954c0da0ffSGleb Smirnoff 
1964c0da0ffSGleb Smirnoff 	{ 0, 0 }
19795d67482SBill Paul };
19895d67482SBill Paul 
1994c0da0ffSGleb Smirnoff static const struct bge_vendor {
2004c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2014c0da0ffSGleb Smirnoff 	const char	*v_name;
2024c0da0ffSGleb Smirnoff } bge_vendors[] = {
2034c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2044c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2054c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2064c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2074c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2084c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
2094c0da0ffSGleb Smirnoff 
2104c0da0ffSGleb Smirnoff 	{ 0, NULL }
2114c0da0ffSGleb Smirnoff };
2124c0da0ffSGleb Smirnoff 
2134c0da0ffSGleb Smirnoff static const struct bge_revision {
2144c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2154c0da0ffSGleb Smirnoff 	const char	*br_name;
2164c0da0ffSGleb Smirnoff } bge_revisions[] = {
2174c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2184c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2194c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2204c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2214c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2224c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2234c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2244c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2254c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2264c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2274c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2284c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2294c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2304c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2314c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2324c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2339e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2344c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2354c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2364c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2374c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2384c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2394c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2404c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2414c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2424c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2434c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2444c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2454c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2464c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2474c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2484c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2494c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
25042787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2514c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2524c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2534c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
2544c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
2554c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
2564c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
2574c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
2584c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
2594c0da0ffSGleb Smirnoff 
2604c0da0ffSGleb Smirnoff 	{ 0, NULL }
2614c0da0ffSGleb Smirnoff };
2624c0da0ffSGleb Smirnoff 
2634c0da0ffSGleb Smirnoff /*
2644c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
2654c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
2664c0da0ffSGleb Smirnoff  */
2674c0da0ffSGleb Smirnoff static const struct bge_revision bge_majorrevs[] = {
2689e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
2699e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
2709e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
2719e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
2729e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
2739e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
2749e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
2759e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
2769e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
2779e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
2789e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
2799e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5787,		"unknown BCM5787" },
2804c0da0ffSGleb Smirnoff 
2814c0da0ffSGleb Smirnoff 	{ 0, NULL }
2824c0da0ffSGleb Smirnoff };
2834c0da0ffSGleb Smirnoff 
2844c0da0ffSGleb Smirnoff #define BGE_IS_5705_OR_BEYOND(sc)			   \
2854c0da0ffSGleb Smirnoff 	((sc)->bge_asicrev == BGE_ASICREV_BCM5705	|| \
2864c0da0ffSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5750	|| \
2874c0da0ffSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5714_A0	|| \
2884c0da0ffSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5780	|| \
2894c0da0ffSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5714	|| \
2909e86676bSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5752	|| \
2919e86676bSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5755	|| \
2929e86676bSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5787)
2934c0da0ffSGleb Smirnoff 
2944c0da0ffSGleb Smirnoff #define BGE_IS_575X_PLUS(sc)				   \
2954c0da0ffSGleb Smirnoff 	((sc)->bge_asicrev == BGE_ASICREV_BCM5750	|| \
2964c0da0ffSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5714_A0	|| \
2974c0da0ffSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5780	|| \
2984c0da0ffSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5714	|| \
2999e86676bSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5752	|| \
3009e86676bSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5755	|| \
3019e86676bSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5787)
3024c0da0ffSGleb Smirnoff 
3034c0da0ffSGleb Smirnoff #define BGE_IS_5714_FAMILY(sc)				   \
3044c0da0ffSGleb Smirnoff 	((sc)->bge_asicrev == BGE_ASICREV_BCM5714_A0	|| \
3054c0da0ffSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5780	|| \
3064c0da0ffSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5714)
3074c0da0ffSGleb Smirnoff 
3084c0da0ffSGleb Smirnoff #define BGE_IS_JUMBO_CAPABLE(sc) \
3099e86676bSGleb Smirnoff 	((sc)->bge_asicrev == BGE_ASICREV_BCM5700	|| \
3109e86676bSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5701	|| \
3119e86676bSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5703	|| \
3129e86676bSGleb Smirnoff 	 (sc)->bge_asicrev == BGE_ASICREV_BCM5704)
3134c0da0ffSGleb Smirnoff 
3144c0da0ffSGleb Smirnoff const struct bge_revision * bge_lookup_rev(uint32_t);
3154c0da0ffSGleb Smirnoff const struct bge_vendor * bge_lookup_vendor(uint16_t);
316e51a25f8SAlfred Perlstein static int bge_probe(device_t);
317e51a25f8SAlfred Perlstein static int bge_attach(device_t);
318e51a25f8SAlfred Perlstein static int bge_detach(device_t);
31914afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
32014afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3213f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
322f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
323f41ac2beSBill Paul static int bge_dma_alloc(device_t);
324f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
325f41ac2beSBill Paul 
326e51a25f8SAlfred Perlstein static void bge_txeof(struct bge_softc *);
327e51a25f8SAlfred Perlstein static void bge_rxeof(struct bge_softc *);
32895d67482SBill Paul 
3290f9bd73bSSam Leffler static void bge_tick_locked(struct bge_softc *);
330e51a25f8SAlfred Perlstein static void bge_tick(void *);
331e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
3323f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
333676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
33495d67482SBill Paul 
335e51a25f8SAlfred Perlstein static void bge_intr(void *);
3360f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
337e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
338e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
3390f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
340e51a25f8SAlfred Perlstein static void bge_init(void *);
341e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
342e51a25f8SAlfred Perlstein static void bge_watchdog(struct ifnet *);
343e51a25f8SAlfred Perlstein static void bge_shutdown(device_t);
344e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
345e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
34695d67482SBill Paul 
3473f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
348e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
34995d67482SBill Paul 
350e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
35195d67482SBill Paul 
352e51a25f8SAlfred Perlstein static int bge_newbuf_std(struct bge_softc *, int, struct mbuf *);
353e51a25f8SAlfred Perlstein static int bge_newbuf_jumbo(struct bge_softc *, int, struct mbuf *);
354e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
355e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
356e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
357e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
358e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
359e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
36095d67482SBill Paul 
361e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
362e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
36395d67482SBill Paul 
3643f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
365e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
36695d67482SBill Paul #ifdef notdef
3673f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
36895d67482SBill Paul #endif
369e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
37095d67482SBill Paul 
371e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
372e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
373e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
37475719184SGleb Smirnoff #ifdef DEVICE_POLLING
3753f74909aSGleb Smirnoff static void bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
37675719184SGleb Smirnoff #endif
37795d67482SBill Paul 
378e51a25f8SAlfred Perlstein static void bge_reset(struct bge_softc *);
379dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
38095d67482SBill Paul 
38195d67482SBill Paul static device_method_t bge_methods[] = {
38295d67482SBill Paul 	/* Device interface */
38395d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
38495d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
38595d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
38695d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
38714afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
38814afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
38995d67482SBill Paul 
39095d67482SBill Paul 	/* bus interface */
39195d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
39295d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
39395d67482SBill Paul 
39495d67482SBill Paul 	/* MII interface */
39595d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
39695d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
39795d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
39895d67482SBill Paul 
39995d67482SBill Paul 	{ 0, 0 }
40095d67482SBill Paul };
40195d67482SBill Paul 
40295d67482SBill Paul static driver_t bge_driver = {
40395d67482SBill Paul 	"bge",
40495d67482SBill Paul 	bge_methods,
40595d67482SBill Paul 	sizeof(struct bge_softc)
40695d67482SBill Paul };
40795d67482SBill Paul 
40895d67482SBill Paul static devclass_t bge_devclass;
40995d67482SBill Paul 
410f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
41195d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
41295d67482SBill Paul 
413c4529f41SMichael Reifenberger static int bge_fake_autoneg = 0;
414c4529f41SMichael Reifenberger TUNABLE_INT("hw.bge.fake_autoneg", &bge_fake_autoneg);
415c4529f41SMichael Reifenberger 
4163f74909aSGleb Smirnoff static uint32_t
4173f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
41895d67482SBill Paul {
41995d67482SBill Paul 	device_t dev;
42095d67482SBill Paul 
42195d67482SBill Paul 	dev = sc->bge_dev;
42295d67482SBill Paul 
42395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
42495d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4));
42595d67482SBill Paul }
42695d67482SBill Paul 
42795d67482SBill Paul static void
4283f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
42995d67482SBill Paul {
43095d67482SBill Paul 	device_t dev;
43195d67482SBill Paul 
43295d67482SBill Paul 	dev = sc->bge_dev;
43395d67482SBill Paul 
43495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
43595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
43695d67482SBill Paul }
43795d67482SBill Paul 
43895d67482SBill Paul #ifdef notdef
4393f74909aSGleb Smirnoff static uint32_t
4403f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
44195d67482SBill Paul {
44295d67482SBill Paul 	device_t dev;
44395d67482SBill Paul 
44495d67482SBill Paul 	dev = sc->bge_dev;
44595d67482SBill Paul 
44695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
44795d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
44895d67482SBill Paul }
44995d67482SBill Paul #endif
45095d67482SBill Paul 
45195d67482SBill Paul static void
4523f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
45395d67482SBill Paul {
45495d67482SBill Paul 	device_t dev;
45595d67482SBill Paul 
45695d67482SBill Paul 	dev = sc->bge_dev;
45795d67482SBill Paul 
45895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
45995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
46095d67482SBill Paul }
46195d67482SBill Paul 
462f41ac2beSBill Paul /*
463f41ac2beSBill Paul  * Map a single buffer address.
464f41ac2beSBill Paul  */
465f41ac2beSBill Paul 
466f41ac2beSBill Paul static void
4673f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
468f41ac2beSBill Paul {
469f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
470f41ac2beSBill Paul 
471f41ac2beSBill Paul 	if (error)
472f41ac2beSBill Paul 		return;
473f41ac2beSBill Paul 
474f41ac2beSBill Paul 	ctx = arg;
475f41ac2beSBill Paul 
476f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
477f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
478f41ac2beSBill Paul 		return;
479f41ac2beSBill Paul 	}
480f41ac2beSBill Paul 
481f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
482f41ac2beSBill Paul }
483f41ac2beSBill Paul 
48495d67482SBill Paul /*
48595d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
48695d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
48795d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
48895d67482SBill Paul  * access method.
48995d67482SBill Paul  */
4903f74909aSGleb Smirnoff static uint8_t
4913f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
49295d67482SBill Paul {
49395d67482SBill Paul 	int i;
4943f74909aSGleb Smirnoff 	uint32_t byte = 0;
49595d67482SBill Paul 
49695d67482SBill Paul 	/*
49795d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
49895d67482SBill Paul 	 * having to use the bitbang method.
49995d67482SBill Paul 	 */
50095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
50195d67482SBill Paul 
50295d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
50395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
50495d67482SBill Paul 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
50595d67482SBill Paul 	DELAY(20);
50695d67482SBill Paul 
50795d67482SBill Paul 	/* Issue the read EEPROM command. */
50895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
50995d67482SBill Paul 
51095d67482SBill Paul 	/* Wait for completion */
51195d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
51295d67482SBill Paul 		DELAY(10);
51395d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
51495d67482SBill Paul 			break;
51595d67482SBill Paul 	}
51695d67482SBill Paul 
51795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
518fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
519f6789fbaSPyun YongHyeon 		return (1);
52095d67482SBill Paul 	}
52195d67482SBill Paul 
52295d67482SBill Paul 	/* Get result. */
52395d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
52495d67482SBill Paul 
52595d67482SBill Paul 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
52695d67482SBill Paul 
52795d67482SBill Paul 	return (0);
52895d67482SBill Paul }
52995d67482SBill Paul 
53095d67482SBill Paul /*
53195d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
53295d67482SBill Paul  */
53395d67482SBill Paul static int
5343f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
53595d67482SBill Paul {
5363f74909aSGleb Smirnoff 	int i, error = 0;
5373f74909aSGleb Smirnoff 	uint8_t byte = 0;
53895d67482SBill Paul 
53995d67482SBill Paul 	for (i = 0; i < cnt; i++) {
5403f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
5413f74909aSGleb Smirnoff 		if (error)
54295d67482SBill Paul 			break;
54395d67482SBill Paul 		*(dest + i) = byte;
54495d67482SBill Paul 	}
54595d67482SBill Paul 
5463f74909aSGleb Smirnoff 	return (error ? 1 : 0);
54795d67482SBill Paul }
54895d67482SBill Paul 
54995d67482SBill Paul static int
5503f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
55195d67482SBill Paul {
55295d67482SBill Paul 	struct bge_softc *sc;
5533f74909aSGleb Smirnoff 	uint32_t val, autopoll;
55495d67482SBill Paul 	int i;
55595d67482SBill Paul 
55695d67482SBill Paul 	sc = device_get_softc(dev);
55795d67482SBill Paul 
5580434d1b8SBill Paul 	/*
5590434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
5600434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
5610434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
5620434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
5630434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
5640434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
5650434d1b8SBill Paul 	 * special-cased.
5660434d1b8SBill Paul 	 */
567b1265c1aSJohn Polstra 	if (phy != 1)
56898b28ee5SBill Paul 		return (0);
56998b28ee5SBill Paul 
57037ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
57137ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
57237ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
57337ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
57437ceeb4dSPaul Saab 		DELAY(40);
57537ceeb4dSPaul Saab 	}
57637ceeb4dSPaul Saab 
57795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
57895d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
57995d67482SBill Paul 
58095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
58195d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
58295d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
58395d67482SBill Paul 			break;
58495d67482SBill Paul 	}
58595d67482SBill Paul 
58695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
587fe806fdaSPyun YongHyeon 		if_printf(sc->bge_ifp, "PHY read timed out\n");
58837ceeb4dSPaul Saab 		val = 0;
58937ceeb4dSPaul Saab 		goto done;
59095d67482SBill Paul 	}
59195d67482SBill Paul 
59295d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
59395d67482SBill Paul 
59437ceeb4dSPaul Saab done:
59537ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
59637ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
59737ceeb4dSPaul Saab 		DELAY(40);
59837ceeb4dSPaul Saab 	}
59937ceeb4dSPaul Saab 
60095d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
60195d67482SBill Paul 		return (0);
60295d67482SBill Paul 
60395d67482SBill Paul 	return (val & 0xFFFF);
60495d67482SBill Paul }
60595d67482SBill Paul 
60695d67482SBill Paul static int
6073f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
60895d67482SBill Paul {
60995d67482SBill Paul 	struct bge_softc *sc;
6103f74909aSGleb Smirnoff 	uint32_t autopoll;
61195d67482SBill Paul 	int i;
61295d67482SBill Paul 
61395d67482SBill Paul 	sc = device_get_softc(dev);
61495d67482SBill Paul 
61537ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
61637ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
61737ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
61837ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
61937ceeb4dSPaul Saab 		DELAY(40);
62037ceeb4dSPaul Saab 	}
62137ceeb4dSPaul Saab 
62295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
62395d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
62495d67482SBill Paul 
62595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
62695d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
62795d67482SBill Paul 			break;
62895d67482SBill Paul 	}
62995d67482SBill Paul 
63037ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
63137ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
63237ceeb4dSPaul Saab 		DELAY(40);
63337ceeb4dSPaul Saab 	}
63437ceeb4dSPaul Saab 
63595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
636fe806fdaSPyun YongHyeon 		if_printf(sc->bge_ifp, "PHY read timed out\n");
63795d67482SBill Paul 		return (0);
63895d67482SBill Paul 	}
63995d67482SBill Paul 
64095d67482SBill Paul 	return (0);
64195d67482SBill Paul }
64295d67482SBill Paul 
64395d67482SBill Paul static void
6443f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
64595d67482SBill Paul {
64695d67482SBill Paul 	struct bge_softc *sc;
64795d67482SBill Paul 	struct mii_data *mii;
64895d67482SBill Paul 
64995d67482SBill Paul 	sc = device_get_softc(dev);
65095d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
65195d67482SBill Paul 
65295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
6533f74909aSGleb Smirnoff 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)
65495d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
6553f74909aSGleb Smirnoff 	else
65695d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
65795d67482SBill Paul 
6583f74909aSGleb Smirnoff 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
65995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
6603f74909aSGleb Smirnoff 	else
66195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
66295d67482SBill Paul }
66395d67482SBill Paul 
66495d67482SBill Paul /*
66595d67482SBill Paul  * Intialize a standard receive ring descriptor.
66695d67482SBill Paul  */
66795d67482SBill Paul static int
6683f74909aSGleb Smirnoff bge_newbuf_std(struct bge_softc *sc, int i, struct mbuf *m)
66995d67482SBill Paul {
67095d67482SBill Paul 	struct mbuf *m_new = NULL;
67195d67482SBill Paul 	struct bge_rx_bd *r;
672f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
673f41ac2beSBill Paul 	int error;
67495d67482SBill Paul 
67595d67482SBill Paul 	if (m == NULL) {
676c3a56752SGleb Smirnoff 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
677c3a56752SGleb Smirnoff 		if (m_new == NULL)
67895d67482SBill Paul 			return (ENOBUFS);
67995d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
68095d67482SBill Paul 	} else {
68195d67482SBill Paul 		m_new = m;
68295d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
68395d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
68495d67482SBill Paul 	}
68595d67482SBill Paul 
686e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
68795d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
68895d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
689f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
690f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
691f41ac2beSBill Paul 	ctx.sc = sc;
692f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
693f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
694f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
695f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
696f7cea149SGleb Smirnoff 		if (m == NULL) {
697f7cea149SGleb Smirnoff 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
698f41ac2beSBill Paul 			m_freem(m_new);
699f7cea149SGleb Smirnoff 		}
700f41ac2beSBill Paul 		return (ENOMEM);
701f41ac2beSBill Paul 	}
702e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(ctx.bge_busaddr);
703e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(ctx.bge_busaddr);
704e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
705e907febfSPyun YongHyeon 	r->bge_len = m_new->m_len;
706e907febfSPyun YongHyeon 	r->bge_idx = i;
707f41ac2beSBill Paul 
708f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
709f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
710f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
71195d67482SBill Paul 
71295d67482SBill Paul 	return (0);
71395d67482SBill Paul }
71495d67482SBill Paul 
71595d67482SBill Paul /*
71695d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
71795d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
71895d67482SBill Paul  */
71995d67482SBill Paul static int
7203f74909aSGleb Smirnoff bge_newbuf_jumbo(struct bge_softc *sc, int i, struct mbuf *m)
72195d67482SBill Paul {
7221be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
7231be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
72495d67482SBill Paul 	struct mbuf *m_new = NULL;
7251be6acb7SGleb Smirnoff 	int nsegs;
726f41ac2beSBill Paul 	int error;
72795d67482SBill Paul 
72895d67482SBill Paul 	if (m == NULL) {
729a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
7301be6acb7SGleb Smirnoff 		if (m_new == NULL)
73195d67482SBill Paul 			return (ENOBUFS);
73295d67482SBill Paul 
7331be6acb7SGleb Smirnoff 		m_cljget(m_new, M_DONTWAIT, MJUM9BYTES);
7341be6acb7SGleb Smirnoff 		if (!(m_new->m_flags & M_EXT)) {
73595d67482SBill Paul 			m_freem(m_new);
73695d67482SBill Paul 			return (ENOBUFS);
73795d67482SBill Paul 		}
7381be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
73995d67482SBill Paul 	} else {
74095d67482SBill Paul 		m_new = m;
7411be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
74295d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
74395d67482SBill Paul 	}
74495d67482SBill Paul 
745e255b776SJohn Polstra 	if (!sc->bge_rx_alignment_bug)
74695d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
7471be6acb7SGleb Smirnoff 
7481be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
7491be6acb7SGleb Smirnoff 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
7501be6acb7SGleb Smirnoff 	    m_new, segs, &nsegs, BUS_DMA_NOWAIT);
7511be6acb7SGleb Smirnoff 	if (error) {
7521be6acb7SGleb Smirnoff 		if (m == NULL)
753f41ac2beSBill Paul 			m_freem(m_new);
7541be6acb7SGleb Smirnoff 		return (error);
755f7cea149SGleb Smirnoff 	}
7561be6acb7SGleb Smirnoff 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
7571be6acb7SGleb Smirnoff 
7581be6acb7SGleb Smirnoff 	/*
7591be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
7601be6acb7SGleb Smirnoff 	 */
7611be6acb7SGleb Smirnoff 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
7624e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING|BGE_RXBDFLAG_END;
7634e7ba1abSGleb Smirnoff 	r->bge_idx = i;
7644e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
7654e7ba1abSGleb Smirnoff 	switch (nsegs) {
7664e7ba1abSGleb Smirnoff 	case 4:
7674e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
7684e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
7694e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
7704e7ba1abSGleb Smirnoff 	case 3:
771e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
772e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
773e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
7744e7ba1abSGleb Smirnoff 	case 2:
7754e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
7764e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
7774e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
7784e7ba1abSGleb Smirnoff 	case 1:
7794e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
7804e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
7814e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
7824e7ba1abSGleb Smirnoff 		break;
7834e7ba1abSGleb Smirnoff 	default:
7844e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
7854e7ba1abSGleb Smirnoff 	}
786f41ac2beSBill Paul 
787f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
788f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
789f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
79095d67482SBill Paul 
79195d67482SBill Paul 	return (0);
79295d67482SBill Paul }
79395d67482SBill Paul 
79495d67482SBill Paul /*
79595d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
79695d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
79795d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
79895d67482SBill Paul  * the NIC.
79995d67482SBill Paul  */
80095d67482SBill Paul static int
8013f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
80295d67482SBill Paul {
80395d67482SBill Paul 	int i;
80495d67482SBill Paul 
80595d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
80695d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
80795d67482SBill Paul 			return (ENOBUFS);
80895d67482SBill Paul 	};
80995d67482SBill Paul 
810f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
811f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
812f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
813f41ac2beSBill Paul 
81495d67482SBill Paul 	sc->bge_std = i - 1;
81595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
81695d67482SBill Paul 
81795d67482SBill Paul 	return (0);
81895d67482SBill Paul }
81995d67482SBill Paul 
82095d67482SBill Paul static void
8213f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
82295d67482SBill Paul {
82395d67482SBill Paul 	int i;
82495d67482SBill Paul 
82595d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
82695d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
827e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
828e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
829e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
830f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
831f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
832e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
833e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
83495d67482SBill Paul 		}
835f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
83695d67482SBill Paul 		    sizeof(struct bge_rx_bd));
83795d67482SBill Paul 	}
83895d67482SBill Paul }
83995d67482SBill Paul 
84095d67482SBill Paul static int
8413f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
84295d67482SBill Paul {
84395d67482SBill Paul 	struct bge_rcb *rcb;
8441be6acb7SGleb Smirnoff 	int i;
84595d67482SBill Paul 
84695d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
84795d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
84895d67482SBill Paul 			return (ENOBUFS);
84995d67482SBill Paul 	};
85095d67482SBill Paul 
851f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
852f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
853f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
854f41ac2beSBill Paul 
85595d67482SBill Paul 	sc->bge_jumbo = i - 1;
85695d67482SBill Paul 
857f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
8581be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
8591be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
86067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
86195d67482SBill Paul 
86295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
86395d67482SBill Paul 
86495d67482SBill Paul 	return (0);
86595d67482SBill Paul }
86695d67482SBill Paul 
86795d67482SBill Paul static void
8683f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
86995d67482SBill Paul {
87095d67482SBill Paul 	int i;
87195d67482SBill Paul 
87295d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
87395d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
874e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
875e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
876e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
877f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
878f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
879e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
880e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
88195d67482SBill Paul 		}
882f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
8831be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
88495d67482SBill Paul 	}
88595d67482SBill Paul }
88695d67482SBill Paul 
88795d67482SBill Paul static void
8883f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
88995d67482SBill Paul {
89095d67482SBill Paul 	int i;
89195d67482SBill Paul 
892f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
89395d67482SBill Paul 		return;
89495d67482SBill Paul 
89595d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
89695d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
897e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
898e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
899e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
900f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
901f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
902e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
903e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
90495d67482SBill Paul 		}
905f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
90695d67482SBill Paul 		    sizeof(struct bge_tx_bd));
90795d67482SBill Paul 	}
90895d67482SBill Paul }
90995d67482SBill Paul 
91095d67482SBill Paul static int
9113f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
91295d67482SBill Paul {
91395d67482SBill Paul 	sc->bge_txcnt = 0;
91495d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
9153927098fSPaul Saab 
91614bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
91714bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
91814bbd30fSGleb Smirnoff 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
91914bbd30fSGleb Smirnoff 
9203927098fSPaul Saab 	/* 5700 b2 errata */
921e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
92214bbd30fSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
9233927098fSPaul Saab 
92414bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
9253927098fSPaul Saab 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
9263927098fSPaul Saab 	/* 5700 b2 errata */
927e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
92895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
92995d67482SBill Paul 
93095d67482SBill Paul 	return (0);
93195d67482SBill Paul }
93295d67482SBill Paul 
93395d67482SBill Paul static void
9343f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
93595d67482SBill Paul {
93695d67482SBill Paul 	struct ifnet *ifp;
93795d67482SBill Paul 	struct ifmultiaddr *ifma;
9383f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
93995d67482SBill Paul 	int h, i;
94095d67482SBill Paul 
9410f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
9420f9bd73bSSam Leffler 
943fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
94495d67482SBill Paul 
94595d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
94695d67482SBill Paul 		for (i = 0; i < 4; i++)
94795d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
94895d67482SBill Paul 		return;
94995d67482SBill Paul 	}
95095d67482SBill Paul 
95195d67482SBill Paul 	/* First, zot all the existing filters. */
95295d67482SBill Paul 	for (i = 0; i < 4; i++)
95395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
95495d67482SBill Paul 
95595d67482SBill Paul 	/* Now program new ones. */
95613b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
95795d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
95895d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
95995d67482SBill Paul 			continue;
9600e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
9610e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
96295d67482SBill Paul 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
96395d67482SBill Paul 	}
96413b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
96595d67482SBill Paul 
96695d67482SBill Paul 	for (i = 0; i < 4; i++)
96795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
96895d67482SBill Paul }
96995d67482SBill Paul 
97095d67482SBill Paul /*
97195d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
97295d67482SBill Paul  * self-test results.
97395d67482SBill Paul  */
97495d67482SBill Paul static int
9753f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
97695d67482SBill Paul {
9773f74909aSGleb Smirnoff 	uint32_t dma_rw_ctl;
97895d67482SBill Paul 	int i;
97995d67482SBill Paul 
980e907febfSPyun YongHyeon 	/* Set endian type before we access any non-PCI registers. */
981e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
98295d67482SBill Paul 
98395d67482SBill Paul 	/*
98495d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
98595d67482SBill Paul 	 * self-tests passed.
98695d67482SBill Paul 	 */
98795d67482SBill Paul 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
988fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "RX CPU self-diagnostics failed!\n");
98995d67482SBill Paul 		return (ENODEV);
99095d67482SBill Paul 	}
99195d67482SBill Paul 
99295d67482SBill Paul 	/* Clear the MAC control register */
99395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
99495d67482SBill Paul 
99595d67482SBill Paul 	/*
99695d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
99795d67482SBill Paul 	 * internal memory.
99895d67482SBill Paul 	 */
99995d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
10003f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
100195d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
100295d67482SBill Paul 
100395d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
10043f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
100595d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
100695d67482SBill Paul 
100795d67482SBill Paul 	/* Set up the PCI DMA control register. */
1008e53d81eeSPaul Saab 	if (sc->bge_pcie) {
10094c0da0ffSGleb Smirnoff 		/* PCI Express bus */
1010e53d81eeSPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1011e53d81eeSPaul Saab 		    (0xf << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1012e53d81eeSPaul Saab 		    (0x2 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
10134c0da0ffSGleb Smirnoff 	} else if (sc->bge_pcix) {
10148287860eSJohn Polstra 		/* PCI-X bus */
10154c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
10164c0da0ffSGleb Smirnoff 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD;
10174c0da0ffSGleb Smirnoff 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_ONEDMA_ATONCE; /* XXX */
10184c0da0ffSGleb Smirnoff 			/* XXX magic values, Broadcom-supplied Linux driver */
10194c0da0ffSGleb Smirnoff 			if (sc->bge_asicrev == BGE_ASICREV_BCM5780)
10204c0da0ffSGleb Smirnoff 				dma_rw_ctl |= (1 << 20) | (1 << 18) |
10214c0da0ffSGleb Smirnoff 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE;
10224c0da0ffSGleb Smirnoff 			else
10234c0da0ffSGleb Smirnoff 				dma_rw_ctl |= (1 << 20) | (1 << 18) | (1 << 15);
10244c0da0ffSGleb Smirnoff 
10254c0da0ffSGleb Smirnoff 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
10265cba12d3SPaul Saab 			/*
10275cba12d3SPaul Saab 			 * The 5704 uses a different encoding of read/write
10285cba12d3SPaul Saab 			 * watermarks.
10295cba12d3SPaul Saab 			 */
10305cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10315cba12d3SPaul Saab 			    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10325cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
10335cba12d3SPaul Saab 		else
10345cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10355cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10365cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
10375cba12d3SPaul Saab 			    (0x0F);
10385cba12d3SPaul Saab 
10395cba12d3SPaul Saab 		/*
10405cba12d3SPaul Saab 		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
10415cba12d3SPaul Saab 		 * for hardware bugs.
10425cba12d3SPaul Saab 		 */
1043e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1044e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
10453f74909aSGleb Smirnoff 			uint32_t tmp;
10465cba12d3SPaul Saab 
10475cba12d3SPaul Saab 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
10485cba12d3SPaul Saab 			if (tmp == 0x6 || tmp == 0x7)
10495cba12d3SPaul Saab 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
10508287860eSJohn Polstra 		}
10514c0da0ffSGleb Smirnoff 	} else
10524c0da0ffSGleb Smirnoff 		/* Conventional PCI bus */
10534c0da0ffSGleb Smirnoff 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
10544c0da0ffSGleb Smirnoff 		    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
10554c0da0ffSGleb Smirnoff 		    (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
10564c0da0ffSGleb Smirnoff 		    (0x0F);
10575cba12d3SPaul Saab 
1058e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
10590434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
10604c0da0ffSGleb Smirnoff 	    sc->bge_asicrev == BGE_ASICREV_BCM5705)
10615cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
10625cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
106395d67482SBill Paul 
106495d67482SBill Paul 	/*
106595d67482SBill Paul 	 * Set up general mode register.
106695d67482SBill Paul 	 */
1067e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
106895d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1069ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
107095d67482SBill Paul 
107195d67482SBill Paul 	/*
1072ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1073ea13bdd5SJohn Polstra 	 * properly by these devices.
107495d67482SBill Paul 	 */
1075ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
107695d67482SBill Paul 
107795d67482SBill Paul #ifdef __brokenalpha__
107895d67482SBill Paul 	/*
107995d67482SBill Paul 	 * Must insure that we do not cross an 8K (bytes) boundary
108095d67482SBill Paul 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
108195d67482SBill Paul 	 * restriction on some ALPHA platforms with early revision
108295d67482SBill Paul 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
108395d67482SBill Paul 	 */
108462f1ea9cSJohn Polstra 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
108562f1ea9cSJohn Polstra 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
108695d67482SBill Paul #endif
108795d67482SBill Paul 
108895d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
108995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
109095d67482SBill Paul 
109195d67482SBill Paul 	return (0);
109295d67482SBill Paul }
109395d67482SBill Paul 
109495d67482SBill Paul static int
10953f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
109695d67482SBill Paul {
109795d67482SBill Paul 	struct bge_rcb *rcb;
1098e907febfSPyun YongHyeon 	bus_size_t vrcb;
1099e907febfSPyun YongHyeon 	bge_hostaddr taddr;
110095d67482SBill Paul 	int i;
110195d67482SBill Paul 
110295d67482SBill Paul 	/*
110395d67482SBill Paul 	 * Initialize the memory window pointer register so that
110495d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
110595d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
110695d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
110795d67482SBill Paul 	 */
110895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
110995d67482SBill Paul 
1110822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1111822f63fcSBill Paul 
11124c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc))) {
111395d67482SBill Paul 		/* Configure mbuf memory pool */
111495d67482SBill Paul 		if (sc->bge_extram) {
11150434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
11160434d1b8SBill Paul 			    BGE_EXT_SSRAM);
1117822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1118822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1119822f63fcSBill Paul 			else
112095d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
112195d67482SBill Paul 		} else {
11220434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
11230434d1b8SBill Paul 			    BGE_BUFFPOOL_1);
1124822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1125822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1126822f63fcSBill Paul 			else
112795d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
112895d67482SBill Paul 		}
112995d67482SBill Paul 
113095d67482SBill Paul 		/* Configure DMA resource pool */
11310434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
11320434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
113395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
11340434d1b8SBill Paul 	}
113595d67482SBill Paul 
113695d67482SBill Paul 	/* Configure mbuf pool watermarks */
11374c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc))) {
11380434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
11390434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
11400434d1b8SBill Paul 	} else {
1141fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1142fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
11430434d1b8SBill Paul 	}
1144fa228020SPaul Saab 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
114595d67482SBill Paul 
114695d67482SBill Paul 	/* Configure DMA resource watermarks */
114795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
114895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
114995d67482SBill Paul 
115095d67482SBill Paul 	/* Enable buffer manager */
11514c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc))) {
115295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
115395d67482SBill Paul 		    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
115495d67482SBill Paul 
115595d67482SBill Paul 		/* Poll for buffer manager start indication */
115695d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
115795d67482SBill Paul 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
115895d67482SBill Paul 				break;
115995d67482SBill Paul 			DELAY(10);
116095d67482SBill Paul 		}
116195d67482SBill Paul 
116295d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1163fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1164fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
116595d67482SBill Paul 			return (ENXIO);
116695d67482SBill Paul 		}
11670434d1b8SBill Paul 	}
116895d67482SBill Paul 
116995d67482SBill Paul 	/* Enable flow-through queues */
117095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
117195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
117295d67482SBill Paul 
117395d67482SBill Paul 	/* Wait until queue initialization is complete */
117495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
117595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
117695d67482SBill Paul 			break;
117795d67482SBill Paul 		DELAY(10);
117895d67482SBill Paul 	}
117995d67482SBill Paul 
118095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1181fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
118295d67482SBill Paul 		return (ENXIO);
118395d67482SBill Paul 	}
118495d67482SBill Paul 
118595d67482SBill Paul 	/* Initialize the standard RX ring control block */
1186f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1187f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1188f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1189f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1190f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1191f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1192f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
11934c0da0ffSGleb Smirnoff 	if (BGE_IS_5705_OR_BEYOND(sc))
11940434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
11950434d1b8SBill Paul 	else
11960434d1b8SBill Paul 		rcb->bge_maxlen_flags =
11970434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
119895d67482SBill Paul 	if (sc->bge_extram)
119995d67482SBill Paul 		rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
120095d67482SBill Paul 	else
120195d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
120267111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
120367111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1204f41ac2beSBill Paul 
120567111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
120667111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
120795d67482SBill Paul 
120895d67482SBill Paul 	/*
120995d67482SBill Paul 	 * Initialize the jumbo RX ring control block
121095d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
121195d67482SBill Paul 	 * field until we're actually ready to start
121295d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
121395d67482SBill Paul 	 * high enough to require it).
121495d67482SBill Paul 	 */
12154c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1216f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1217f41ac2beSBill Paul 
1218f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1219f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1220f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1221f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1222f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1223f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1224f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
12251be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
12261be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD|BGE_RCB_FLAG_RING_DISABLED);
122795d67482SBill Paul 		if (sc->bge_extram)
122895d67482SBill Paul 			rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
122995d67482SBill Paul 		else
123095d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
123167111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
123267111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
123367111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
123467111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1235f41ac2beSBill Paul 
12360434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
12370434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
123867111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
123995d67482SBill Paul 
124095d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1241f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
124267111612SJohn Polstra 		rcb->bge_maxlen_flags =
124367111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
12440434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
12450434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
12460434d1b8SBill Paul 	}
124795d67482SBill Paul 
124895d67482SBill Paul 	/*
124995d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
125095d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
125195d67482SBill Paul 	 * each ring.
125295d67482SBill Paul 	 */
125395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
125495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
125595d67482SBill Paul 
125695d67482SBill Paul 	/*
125795d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
125895d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
125995d67482SBill Paul 	 * These are located in NIC memory.
126095d67482SBill Paul 	 */
1261e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
126295d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1263e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1264e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1265e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1266e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
126795d67482SBill Paul 	}
126895d67482SBill Paul 
126995d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
1270e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1271e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1272e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1273e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1274e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1275e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
12764c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
1277e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1278e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
127995d67482SBill Paul 
128095d67482SBill Paul 	/* Disable all unused RX return rings */
1281e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
128295d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1283e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1284e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1285e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
12860434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1287e907febfSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED));
1288e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
128995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
12903f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1291e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
129295d67482SBill Paul 	}
129395d67482SBill Paul 
129495d67482SBill Paul 	/* Initialize RX ring indexes */
129595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
129695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
129795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
129895d67482SBill Paul 
129995d67482SBill Paul 	/*
130095d67482SBill Paul 	 * Set up RX return ring 0
130195d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
130295d67482SBill Paul 	 * The return rings live entirely within the host, so the
130395d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
130495d67482SBill Paul 	 */
1305e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1306e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1307e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1308e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1309e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1310e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1311e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
131295d67482SBill Paul 
131395d67482SBill Paul 	/* Set random backoff seed for TX */
131495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
13154a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
13164a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
13174a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
131895d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
131995d67482SBill Paul 
132095d67482SBill Paul 	/* Set inter-packet gap */
132195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
132295d67482SBill Paul 
132395d67482SBill Paul 	/*
132495d67482SBill Paul 	 * Specify which ring to use for packets that don't match
132595d67482SBill Paul 	 * any RX rules.
132695d67482SBill Paul 	 */
132795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
132895d67482SBill Paul 
132995d67482SBill Paul 	/*
133095d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
133195d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
133295d67482SBill Paul 	 */
133395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
133495d67482SBill Paul 
133595d67482SBill Paul 	/* Inialize RX list placement stats mask. */
133695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
133795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
133895d67482SBill Paul 
133995d67482SBill Paul 	/* Disable host coalescing until we get it set up */
134095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
134195d67482SBill Paul 
134295d67482SBill Paul 	/* Poll to make sure it's shut down. */
134395d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
134495d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
134595d67482SBill Paul 			break;
134695d67482SBill Paul 		DELAY(10);
134795d67482SBill Paul 	}
134895d67482SBill Paul 
134995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1350fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1351fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
135295d67482SBill Paul 		return (ENXIO);
135395d67482SBill Paul 	}
135495d67482SBill Paul 
135595d67482SBill Paul 	/* Set up host coalescing defaults */
135695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
135795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
135895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
135995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
13604c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc))) {
136195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
136295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
13630434d1b8SBill Paul 	}
136495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
136595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
136695d67482SBill Paul 
136795d67482SBill Paul 	/* Set up address of statistics block */
13684c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc))) {
1369f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1370f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
137195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1372f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
13730434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
137495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
13750434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
13760434d1b8SBill Paul 	}
13770434d1b8SBill Paul 
13780434d1b8SBill Paul 	/* Set up address of status block */
1379f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1380f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
138195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1382f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1383f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1384f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
138595d67482SBill Paul 
138695d67482SBill Paul 	/* Turn on host coalescing state machine */
138795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
138895d67482SBill Paul 
138995d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
139095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
139195d67482SBill Paul 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
139295d67482SBill Paul 
139395d67482SBill Paul 	/* Turn on RX list placement state machine */
139495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
139595d67482SBill Paul 
139695d67482SBill Paul 	/* Turn on RX list selector state machine. */
13974c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
139895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
139995d67482SBill Paul 
140095d67482SBill Paul 	/* Turn on DMA, clear stats */
140195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
140295d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
140395d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
140495d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
140595d67482SBill Paul 	    (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
140695d67482SBill Paul 
140795d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
140895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
140995d67482SBill Paul 
141095d67482SBill Paul #ifdef notdef
141195d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
141295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
141395d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
141495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
141595d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
141695d67482SBill Paul #endif
141795d67482SBill Paul 
141895d67482SBill Paul 	/* Turn on DMA completion state machine */
14194c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
142095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
142195d67482SBill Paul 
142295d67482SBill Paul 	/* Turn on write DMA state machine */
142395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_WDMA_MODE,
142495d67482SBill Paul 	    BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
142595d67482SBill Paul 
142695d67482SBill Paul 	/* Turn on read DMA state machine */
142795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
142895d67482SBill Paul 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
142995d67482SBill Paul 
143095d67482SBill Paul 	/* Turn on RX data completion state machine */
143195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
143295d67482SBill Paul 
143395d67482SBill Paul 	/* Turn on RX BD initiator state machine */
143495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
143595d67482SBill Paul 
143695d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
143795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
143895d67482SBill Paul 
143995d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
14404c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
144195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
144295d67482SBill Paul 
144395d67482SBill Paul 	/* Turn on send BD completion state machine */
144495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
144595d67482SBill Paul 
144695d67482SBill Paul 	/* Turn on send data completion state machine */
144795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
144895d67482SBill Paul 
144995d67482SBill Paul 	/* Turn on send data initiator state machine */
145095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
145195d67482SBill Paul 
145295d67482SBill Paul 	/* Turn on send BD initiator state machine */
145395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
145495d67482SBill Paul 
145595d67482SBill Paul 	/* Turn on send BD selector state machine */
145695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
145795d67482SBill Paul 
145895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
145995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
146095d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
146195d67482SBill Paul 
146295d67482SBill Paul 	/* ack/clear link change events */
146395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
14640434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
14650434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1466f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
146795d67482SBill Paul 
146895d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
146995d67482SBill Paul 	if (sc->bge_tbi) {
147095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1471a1d52896SBill Paul 	} else {
147295d67482SBill Paul 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
14731f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
14744c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
1475a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1476a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1477a1d52896SBill Paul 	}
147895d67482SBill Paul 
14791f313773SOleg Bulyzhin 	/*
14801f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
14811f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
14821f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
14831f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
14841f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
14851f313773SOleg Bulyzhin 	 */
14861f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
14871f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
14881f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
14891f313773SOleg Bulyzhin 
149095d67482SBill Paul 	/* Enable link state change attentions. */
149195d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
149295d67482SBill Paul 
149395d67482SBill Paul 	return (0);
149495d67482SBill Paul }
149595d67482SBill Paul 
14964c0da0ffSGleb Smirnoff const struct bge_revision *
14974c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
14984c0da0ffSGleb Smirnoff {
14994c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
15004c0da0ffSGleb Smirnoff 
15014c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
15024c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
15034c0da0ffSGleb Smirnoff 			return (br);
15044c0da0ffSGleb Smirnoff 	}
15054c0da0ffSGleb Smirnoff 
15064c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
15074c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
15084c0da0ffSGleb Smirnoff 			return (br);
15094c0da0ffSGleb Smirnoff 	}
15104c0da0ffSGleb Smirnoff 
15114c0da0ffSGleb Smirnoff 	return (NULL);
15124c0da0ffSGleb Smirnoff }
15134c0da0ffSGleb Smirnoff 
15144c0da0ffSGleb Smirnoff const struct bge_vendor *
15154c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
15164c0da0ffSGleb Smirnoff {
15174c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
15184c0da0ffSGleb Smirnoff 
15194c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
15204c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
15214c0da0ffSGleb Smirnoff 			return (v);
15224c0da0ffSGleb Smirnoff 
15234c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
15244c0da0ffSGleb Smirnoff 	return (NULL);
15254c0da0ffSGleb Smirnoff }
15264c0da0ffSGleb Smirnoff 
152795d67482SBill Paul /*
152895d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
15294c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
15304c0da0ffSGleb Smirnoff  *
15314c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
153295d67482SBill Paul  * can get the device name string from the controller itself instead
153395d67482SBill Paul  * of the compiled-in string. This is a little slow, but it guarantees
15344c0da0ffSGleb Smirnoff  * we'll always announce the right product name. Unfortunately, this
15354c0da0ffSGleb Smirnoff  * is possible only later in bge_attach(), when we have established
15364c0da0ffSGleb Smirnoff  * access to EEPROM.
153795d67482SBill Paul  */
153895d67482SBill Paul static int
15393f74909aSGleb Smirnoff bge_probe(device_t dev)
154095d67482SBill Paul {
15414c0da0ffSGleb Smirnoff 	struct bge_type *t = bge_devs;
15424c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
154395d67482SBill Paul 
154495d67482SBill Paul 	bzero(sc, sizeof(struct bge_softc));
154595d67482SBill Paul 	sc->bge_dev = dev;
154695d67482SBill Paul 
15474c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
154895d67482SBill Paul 		if ((pci_get_vendor(dev) == t->bge_vid) &&
154995d67482SBill Paul 		    (pci_get_device(dev) == t->bge_did)) {
15504c0da0ffSGleb Smirnoff 			char buf[64];
15514c0da0ffSGleb Smirnoff 			const struct bge_revision *br;
15524c0da0ffSGleb Smirnoff 			const struct bge_vendor *v;
15534c0da0ffSGleb Smirnoff 			uint32_t id;
15544c0da0ffSGleb Smirnoff 
15554c0da0ffSGleb Smirnoff 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
15564c0da0ffSGleb Smirnoff 			    BGE_PCIMISCCTL_ASICREV;
15574c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
15584c0da0ffSGleb Smirnoff 			id >>= 16;
15594c0da0ffSGleb Smirnoff 			v = bge_lookup_vendor(t->bge_vid);
15604c0da0ffSGleb Smirnoff 			if (br == NULL)
15614c0da0ffSGleb Smirnoff 				snprintf(buf, 64, "%s unknown ASIC (%#04x)",
15624c0da0ffSGleb Smirnoff 				    v->v_name, id);
15634c0da0ffSGleb Smirnoff 			else
15644c0da0ffSGleb Smirnoff 				snprintf(buf, 64, "%s %s, ASIC rev. %#04x",
15654c0da0ffSGleb Smirnoff 				    v->v_name, br->br_name, id);
15664c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
15676d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
15686d2a9bd6SDoug Ambrisko 				sc->bge_no_3_led = 1;
156995d67482SBill Paul 			return (0);
157095d67482SBill Paul 		}
157195d67482SBill Paul 		t++;
157295d67482SBill Paul 	}
157395d67482SBill Paul 
157495d67482SBill Paul 	return (ENXIO);
157595d67482SBill Paul }
157695d67482SBill Paul 
1577f41ac2beSBill Paul static void
15783f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
1579f41ac2beSBill Paul {
1580f41ac2beSBill Paul 	int i;
1581f41ac2beSBill Paul 
15823f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
1583f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1584f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1585f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1586f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1587f41ac2beSBill Paul 	}
1588f41ac2beSBill Paul 
15893f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
1590f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1591f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1592f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1593f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1594f41ac2beSBill Paul 	}
1595f41ac2beSBill Paul 
15963f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
1597f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1598f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1599f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1600f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1601f41ac2beSBill Paul 	}
1602f41ac2beSBill Paul 
1603f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1604f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1605f41ac2beSBill Paul 
1606f41ac2beSBill Paul 
16073f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
1608e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
1609e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1610e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
1611e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1612f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1613f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1614f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1615f41ac2beSBill Paul 
1616f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1617f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1618f41ac2beSBill Paul 
16193f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
1620e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1621e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1622e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1623e65bed95SPyun YongHyeon 
1624e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1625e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
1626f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1627f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1628f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1629f41ac2beSBill Paul 
1630f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1631f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1632f41ac2beSBill Paul 
16333f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
1634e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
1635e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1636e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
1637e65bed95SPyun YongHyeon 
1638e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1639e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
1640f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1641f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1642f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1643f41ac2beSBill Paul 
1644f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1645f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1646f41ac2beSBill Paul 
16473f74909aSGleb Smirnoff 	/* Destroy TX ring. */
1648e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
1649e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1650e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
1651e65bed95SPyun YongHyeon 
1652e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
1653f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1654f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1655f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1656f41ac2beSBill Paul 
1657f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1658f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1659f41ac2beSBill Paul 
16603f74909aSGleb Smirnoff 	/* Destroy status block. */
1661e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
1662e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1663e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
1664e65bed95SPyun YongHyeon 
1665e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
1666f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1667f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1668f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1669f41ac2beSBill Paul 
1670f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1671f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1672f41ac2beSBill Paul 
16733f74909aSGleb Smirnoff 	/* Destroy statistics block. */
1674e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
1675e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1676e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
1677e65bed95SPyun YongHyeon 
1678e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
1679f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1680f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1681f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1682f41ac2beSBill Paul 
1683f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1684f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1685f41ac2beSBill Paul 
16863f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
1687f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1688f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1689f41ac2beSBill Paul }
1690f41ac2beSBill Paul 
1691f41ac2beSBill Paul static int
16923f74909aSGleb Smirnoff bge_dma_alloc(device_t dev)
1693f41ac2beSBill Paul {
16943f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
1695f41ac2beSBill Paul 	struct bge_softc *sc;
16961be6acb7SGleb Smirnoff 	int i, error;
1697f41ac2beSBill Paul 
1698f41ac2beSBill Paul 	sc = device_get_softc(dev);
1699f41ac2beSBill Paul 
1700f41ac2beSBill Paul 	/*
1701f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1702f41ac2beSBill Paul 	 */
1703f41ac2beSBill Paul 	error = bus_dma_tag_create(NULL,	/* parent */
1704f41ac2beSBill Paul 			PAGE_SIZE, 0,		/* alignment, boundary */
1705f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
17062f28b973SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1707f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1708f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1709f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
17108a40c10eSScott Long 			0,			/* flags */
1711f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1712f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1713f41ac2beSBill Paul 
1714e65bed95SPyun YongHyeon 	if (error != 0) {
1715fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1716fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
1717e65bed95SPyun YongHyeon 		return (ENOMEM);
1718e65bed95SPyun YongHyeon 	}
1719e65bed95SPyun YongHyeon 
1720f41ac2beSBill Paul 	/*
1721f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1722f41ac2beSBill Paul 	 */
17238a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1724f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
17251be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
17261be6acb7SGleb Smirnoff 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
1727f41ac2beSBill Paul 
1728f41ac2beSBill Paul 	if (error) {
1729fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1730f41ac2beSBill Paul 		return (ENOMEM);
1731f41ac2beSBill Paul 	}
1732f41ac2beSBill Paul 
17333f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
1734f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1735f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1736f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1737f41ac2beSBill Paul 		if (error) {
1738fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1739fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1740f41ac2beSBill Paul 			return (ENOMEM);
1741f41ac2beSBill Paul 		}
1742f41ac2beSBill Paul 	}
1743f41ac2beSBill Paul 
17443f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
1745f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1746f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1747f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1748f41ac2beSBill Paul 		if (error) {
1749fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1750fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1751f41ac2beSBill Paul 			return (ENOMEM);
1752f41ac2beSBill Paul 		}
1753f41ac2beSBill Paul 	}
1754f41ac2beSBill Paul 
17553f74909aSGleb Smirnoff 	/* Create tag for standard RX ring. */
1756f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1757f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1758f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1759f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1760f41ac2beSBill Paul 
1761f41ac2beSBill Paul 	if (error) {
1762fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1763f41ac2beSBill Paul 		return (ENOMEM);
1764f41ac2beSBill Paul 	}
1765f41ac2beSBill Paul 
17663f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for standard RX ring. */
1767f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1768f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1769f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1770f41ac2beSBill Paul 	if (error)
1771f41ac2beSBill Paul 		return (ENOMEM);
1772f41ac2beSBill Paul 
1773f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1774f41ac2beSBill Paul 
17753f74909aSGleb Smirnoff 	/* Load the address of the standard RX ring. */
1776f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1777f41ac2beSBill Paul 	ctx.sc = sc;
1778f41ac2beSBill Paul 
1779f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1780f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1781f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1782f41ac2beSBill Paul 
1783f41ac2beSBill Paul 	if (error)
1784f41ac2beSBill Paul 		return (ENOMEM);
1785f41ac2beSBill Paul 
1786f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
1787f41ac2beSBill Paul 
17883f74909aSGleb Smirnoff 	/* Create tags for jumbo mbufs. */
17894c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1790f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
17918a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
17921be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
17931be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
1794f41ac2beSBill Paul 		if (error) {
1795fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
17963f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
1797f41ac2beSBill Paul 			return (ENOMEM);
1798f41ac2beSBill Paul 		}
1799f41ac2beSBill Paul 
18003f74909aSGleb Smirnoff 		/* Create tag for jumbo RX ring. */
1801f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1802f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1803f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
1804f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
1805f41ac2beSBill Paul 
1806f41ac2beSBill Paul 		if (error) {
1807fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
18083f74909aSGleb Smirnoff 			    "could not allocate jumbo ring dma tag\n");
1809f41ac2beSBill Paul 			return (ENOMEM);
1810f41ac2beSBill Paul 		}
1811f41ac2beSBill Paul 
18123f74909aSGleb Smirnoff 		/* Allocate DMA'able memory for jumbo RX ring. */
1813f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
18141be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
18151be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1816f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
1817f41ac2beSBill Paul 		if (error)
1818f41ac2beSBill Paul 			return (ENOMEM);
1819f41ac2beSBill Paul 
18203f74909aSGleb Smirnoff 		/* Load the address of the jumbo RX ring. */
1821f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
1822f41ac2beSBill Paul 		ctx.sc = sc;
1823f41ac2beSBill Paul 
1824f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1825f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1826f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
1827f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1828f41ac2beSBill Paul 
1829f41ac2beSBill Paul 		if (error)
1830f41ac2beSBill Paul 			return (ENOMEM);
1831f41ac2beSBill Paul 
1832f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
1833f41ac2beSBill Paul 
18343f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
1835f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1836f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
1837f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1838f41ac2beSBill Paul 			if (error) {
1839fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
18403f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
1841f41ac2beSBill Paul 				return (ENOMEM);
1842f41ac2beSBill Paul 			}
1843f41ac2beSBill Paul 		}
1844f41ac2beSBill Paul 
1845f41ac2beSBill Paul 	}
1846f41ac2beSBill Paul 
18473f74909aSGleb Smirnoff 	/* Create tag for RX return ring. */
1848f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1849f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1850f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
1851f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
1852f41ac2beSBill Paul 
1853f41ac2beSBill Paul 	if (error) {
1854fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1855f41ac2beSBill Paul 		return (ENOMEM);
1856f41ac2beSBill Paul 	}
1857f41ac2beSBill Paul 
18583f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for RX return ring. */
1859f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
1860f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
1861f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
1862f41ac2beSBill Paul 	if (error)
1863f41ac2beSBill Paul 		return (ENOMEM);
1864f41ac2beSBill Paul 
1865f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
1866f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
1867f41ac2beSBill Paul 
18683f74909aSGleb Smirnoff 	/* Load the address of the RX return ring. */
1869f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1870f41ac2beSBill Paul 	ctx.sc = sc;
1871f41ac2beSBill Paul 
1872f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
1873f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
1874f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
1875f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1876f41ac2beSBill Paul 
1877f41ac2beSBill Paul 	if (error)
1878f41ac2beSBill Paul 		return (ENOMEM);
1879f41ac2beSBill Paul 
1880f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
1881f41ac2beSBill Paul 
18823f74909aSGleb Smirnoff 	/* Create tag for TX ring. */
1883f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1884f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1885f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
1886f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
1887f41ac2beSBill Paul 
1888f41ac2beSBill Paul 	if (error) {
1889fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1890f41ac2beSBill Paul 		return (ENOMEM);
1891f41ac2beSBill Paul 	}
1892f41ac2beSBill Paul 
18933f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for TX ring. */
1894f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
1895f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
1896f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
1897f41ac2beSBill Paul 	if (error)
1898f41ac2beSBill Paul 		return (ENOMEM);
1899f41ac2beSBill Paul 
1900f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1901f41ac2beSBill Paul 
19023f74909aSGleb Smirnoff 	/* Load the address of the TX ring. */
1903f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1904f41ac2beSBill Paul 	ctx.sc = sc;
1905f41ac2beSBill Paul 
1906f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
1907f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
1908f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1909f41ac2beSBill Paul 
1910f41ac2beSBill Paul 	if (error)
1911f41ac2beSBill Paul 		return (ENOMEM);
1912f41ac2beSBill Paul 
1913f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
1914f41ac2beSBill Paul 
19153f74909aSGleb Smirnoff 	/* Create tag for status block. */
1916f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1917f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1918f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
1919f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
1920f41ac2beSBill Paul 
1921f41ac2beSBill Paul 	if (error) {
1922fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1923f41ac2beSBill Paul 		return (ENOMEM);
1924f41ac2beSBill Paul 	}
1925f41ac2beSBill Paul 
19263f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for status block. */
1927f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
1928f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
1929f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
1930f41ac2beSBill Paul 	if (error)
1931f41ac2beSBill Paul 		return (ENOMEM);
1932f41ac2beSBill Paul 
1933f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
1934f41ac2beSBill Paul 
19353f74909aSGleb Smirnoff 	/* Load the address of the status block. */
1936f41ac2beSBill Paul 	ctx.sc = sc;
1937f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1938f41ac2beSBill Paul 
1939f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
1940f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
1941f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1942f41ac2beSBill Paul 
1943f41ac2beSBill Paul 	if (error)
1944f41ac2beSBill Paul 		return (ENOMEM);
1945f41ac2beSBill Paul 
1946f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
1947f41ac2beSBill Paul 
19483f74909aSGleb Smirnoff 	/* Create tag for statistics block. */
1949f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1950f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1951f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
1952f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
1953f41ac2beSBill Paul 
1954f41ac2beSBill Paul 	if (error) {
1955fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1956f41ac2beSBill Paul 		return (ENOMEM);
1957f41ac2beSBill Paul 	}
1958f41ac2beSBill Paul 
19593f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for statistics block. */
1960f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
1961f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
1962f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
1963f41ac2beSBill Paul 	if (error)
1964f41ac2beSBill Paul 		return (ENOMEM);
1965f41ac2beSBill Paul 
1966f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
1967f41ac2beSBill Paul 
19683f74909aSGleb Smirnoff 	/* Load the address of the statstics block. */
1969f41ac2beSBill Paul 	ctx.sc = sc;
1970f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1971f41ac2beSBill Paul 
1972f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
1973f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
1974f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1975f41ac2beSBill Paul 
1976f41ac2beSBill Paul 	if (error)
1977f41ac2beSBill Paul 		return (ENOMEM);
1978f41ac2beSBill Paul 
1979f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
1980f41ac2beSBill Paul 
1981f41ac2beSBill Paul 	return (0);
1982f41ac2beSBill Paul }
1983f41ac2beSBill Paul 
198495d67482SBill Paul static int
19853f74909aSGleb Smirnoff bge_attach(device_t dev)
198695d67482SBill Paul {
198795d67482SBill Paul 	struct ifnet *ifp;
198895d67482SBill Paul 	struct bge_softc *sc;
19893f74909aSGleb Smirnoff 	uint32_t hwcfg = 0;
19903f74909aSGleb Smirnoff 	uint32_t mac_tmp = 0;
1991fc74a9f9SBrooks Davis 	u_char eaddr[6];
1992fe806fdaSPyun YongHyeon 	int error = 0, rid;
199395d67482SBill Paul 
199495d67482SBill Paul 	sc = device_get_softc(dev);
199595d67482SBill Paul 	sc->bge_dev = dev;
199695d67482SBill Paul 
199795d67482SBill Paul 	/*
199895d67482SBill Paul 	 * Map control/status registers.
199995d67482SBill Paul 	 */
200095d67482SBill Paul 	pci_enable_busmaster(dev);
200195d67482SBill Paul 
200295d67482SBill Paul 	rid = BGE_PCI_BAR0;
20035f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
20045f96beb9SNate Lawson 	    RF_ACTIVE|PCI_RF_DENSE);
200595d67482SBill Paul 
200695d67482SBill Paul 	if (sc->bge_res == NULL) {
2007fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
200895d67482SBill Paul 		error = ENXIO;
200995d67482SBill Paul 		goto fail;
201095d67482SBill Paul 	}
201195d67482SBill Paul 
201295d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
201395d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
201495d67482SBill Paul 
20153f74909aSGleb Smirnoff 	/* Allocate interrupt. */
201695d67482SBill Paul 	rid = 0;
201795d67482SBill Paul 
20185f96beb9SNate Lawson 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
201995d67482SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
202095d67482SBill Paul 
202195d67482SBill Paul 	if (sc->bge_irq == NULL) {
2022fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
202395d67482SBill Paul 		error = ENXIO;
202495d67482SBill Paul 		goto fail;
202595d67482SBill Paul 	}
202695d67482SBill Paul 
20270f9bd73bSSam Leffler 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
20280f9bd73bSSam Leffler 
2029e53d81eeSPaul Saab 	/* Save ASIC rev. */
2030e53d81eeSPaul Saab 
2031e53d81eeSPaul Saab 	sc->bge_chipid =
2032e53d81eeSPaul Saab 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2033e53d81eeSPaul Saab 	    BGE_PCIMISCCTL_ASICREV;
2034e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2035e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2036e53d81eeSPaul Saab 
2037e53d81eeSPaul Saab 	/*
2038e53d81eeSPaul Saab 	 * XXX: Broadcom Linux driver.  Not in specs or eratta.
2039e53d81eeSPaul Saab 	 * PCI-Express?
2040e53d81eeSPaul Saab 	 */
20414c0da0ffSGleb Smirnoff 	if (BGE_IS_5705_OR_BEYOND(sc)) {
20423f74909aSGleb Smirnoff 		uint32_t v;
2043e53d81eeSPaul Saab 
2044e53d81eeSPaul Saab 		v = pci_read_config(dev, BGE_PCI_MSI_CAPID, 4);
2045e53d81eeSPaul Saab 		if (((v >> 8) & 0xff) == BGE_PCIE_CAPID_REG) {
2046e53d81eeSPaul Saab 			v = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
2047e53d81eeSPaul Saab 			if ((v & 0xff) == BGE_PCIE_CAPID)
2048e53d81eeSPaul Saab 				sc->bge_pcie = 1;
2049e53d81eeSPaul Saab 		}
2050e53d81eeSPaul Saab 	}
2051e53d81eeSPaul Saab 
20524c0da0ffSGleb Smirnoff 	/*
20534c0da0ffSGleb Smirnoff 	 * PCI-X ?
20544c0da0ffSGleb Smirnoff 	 */
20554c0da0ffSGleb Smirnoff 	if ((pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
20564c0da0ffSGleb Smirnoff 	    BGE_PCISTATE_PCI_BUSMODE) == 0)
20574c0da0ffSGleb Smirnoff 		sc->bge_pcix = 1;
20584c0da0ffSGleb Smirnoff 
205995d67482SBill Paul 	/* Try to reset the chip. */
206095d67482SBill Paul 	bge_reset(sc);
206195d67482SBill Paul 
206295d67482SBill Paul 	if (bge_chipinit(sc)) {
2063fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
206495d67482SBill Paul 		bge_release_resources(sc);
206595d67482SBill Paul 		error = ENXIO;
206695d67482SBill Paul 		goto fail;
206795d67482SBill Paul 	}
206895d67482SBill Paul 
206995d67482SBill Paul 	/*
207095d67482SBill Paul 	 * Get station address from the EEPROM.
207195d67482SBill Paul 	 */
2072fc74a9f9SBrooks Davis 	mac_tmp = bge_readmem_ind(sc, 0x0c14);
2073fc74a9f9SBrooks Davis 	if ((mac_tmp >> 16) == 0x484b) {
2074fc74a9f9SBrooks Davis 		eaddr[0] = (u_char)(mac_tmp >> 8);
2075fc74a9f9SBrooks Davis 		eaddr[1] = (u_char)mac_tmp;
2076fc74a9f9SBrooks Davis 		mac_tmp = bge_readmem_ind(sc, 0x0c18);
2077fc74a9f9SBrooks Davis 		eaddr[2] = (u_char)(mac_tmp >> 24);
2078fc74a9f9SBrooks Davis 		eaddr[3] = (u_char)(mac_tmp >> 16);
2079fc74a9f9SBrooks Davis 		eaddr[4] = (u_char)(mac_tmp >> 8);
2080fc74a9f9SBrooks Davis 		eaddr[5] = (u_char)mac_tmp;
2081fc74a9f9SBrooks Davis 	} else if (bge_read_eeprom(sc, eaddr,
208295d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2083fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to read station address\n");
208495d67482SBill Paul 		bge_release_resources(sc);
208595d67482SBill Paul 		error = ENXIO;
208695d67482SBill Paul 		goto fail;
208795d67482SBill Paul 	}
208895d67482SBill Paul 
2089f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
20904c0da0ffSGleb Smirnoff 	if (BGE_IS_5705_OR_BEYOND(sc))
2091f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2092f41ac2beSBill Paul 	else
2093f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2094f41ac2beSBill Paul 
2095f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2096fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2097fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2098f41ac2beSBill Paul 		bge_release_resources(sc);
2099f41ac2beSBill Paul 		error = ENXIO;
2100f41ac2beSBill Paul 		goto fail;
2101f41ac2beSBill Paul 	}
2102f41ac2beSBill Paul 
210395d67482SBill Paul 	/* Set default tuneable values. */
210495d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
210595d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
210695d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
210795d67482SBill Paul 	sc->bge_rx_max_coal_bds = 64;
210895d67482SBill Paul 	sc->bge_tx_max_coal_bds = 128;
210995d67482SBill Paul 
211095d67482SBill Paul 	/* Set up ifnet structure */
2111fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2112fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2113fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2114fc74a9f9SBrooks Davis 		bge_release_resources(sc);
2115fc74a9f9SBrooks Davis 		error = ENXIO;
2116fc74a9f9SBrooks Davis 		goto fail;
2117fc74a9f9SBrooks Davis 	}
211895d67482SBill Paul 	ifp->if_softc = sc;
21199bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
212095d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
212195d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
212295d67482SBill Paul 	ifp->if_start = bge_start;
212395d67482SBill Paul 	ifp->if_watchdog = bge_watchdog;
212495d67482SBill Paul 	ifp->if_init = bge_init;
212595d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
21264d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
21274d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
21284d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
212995d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2130d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
2131479b23b7SGleb Smirnoff 	    IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM;
213295d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
213375719184SGleb Smirnoff #ifdef DEVICE_POLLING
213475719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
213575719184SGleb Smirnoff #endif
213695d67482SBill Paul 
2137a1d52896SBill Paul 	/*
2138d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2139d375e524SGleb Smirnoff 	 * to hardware bugs.
2140d375e524SGleb Smirnoff 	 */
2141d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2142d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2143d375e524SGleb Smirnoff 		ifp->if_capenable &= IFCAP_HWCSUM;
2144d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2145d375e524SGleb Smirnoff 	}
2146d375e524SGleb Smirnoff 
2147d375e524SGleb Smirnoff 	/*
2148a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
214941abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
215041abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
215141abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
215241abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
215341abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
215441abcc1bSPaul Saab 	 * SK-9D41.
2155a1d52896SBill Paul 	 */
215641abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
215741abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
215841abcc1bSPaul Saab 	else {
2159f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2160f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2161fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2162f6789fbaSPyun YongHyeon 			bge_release_resources(sc);
2163f6789fbaSPyun YongHyeon 			error = ENXIO;
2164f6789fbaSPyun YongHyeon 			goto fail;
2165f6789fbaSPyun YongHyeon 		}
216641abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
216741abcc1bSPaul Saab 	}
216841abcc1bSPaul Saab 
216941abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2170a1d52896SBill Paul 		sc->bge_tbi = 1;
2171a1d52896SBill Paul 
217295d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
217395d67482SBill Paul 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
217495d67482SBill Paul 		sc->bge_tbi = 1;
217595d67482SBill Paul 
217695d67482SBill Paul 	if (sc->bge_tbi) {
217795d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
217895d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
217995d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
218095d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia,
218195d67482SBill Paul 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
218295d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
218395d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2184da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
218595d67482SBill Paul 	} else {
218695d67482SBill Paul 		/*
218795d67482SBill Paul 		 * Do transceiver setup.
218895d67482SBill Paul 		 */
218995d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
219095d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
2191fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
219295d67482SBill Paul 			bge_release_resources(sc);
219395d67482SBill Paul 			error = ENXIO;
219495d67482SBill Paul 			goto fail;
219595d67482SBill Paul 		}
219695d67482SBill Paul 	}
219795d67482SBill Paul 
219895d67482SBill Paul 	/*
2199e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2200e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2201e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2202e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2203e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2204e255b776SJohn Polstra 	 * payloads by copying the received packets.
2205e255b776SJohn Polstra 	 */
22064c0da0ffSGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 && sc->bge_pcix)
2207e255b776SJohn Polstra                 sc->bge_rx_alignment_bug = 1;
2208e255b776SJohn Polstra 
2209e255b776SJohn Polstra 	/*
221095d67482SBill Paul 	 * Call MI attach routine.
221195d67482SBill Paul 	 */
2212fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
22130f9bd73bSSam Leffler 	callout_init(&sc->bge_stat_ch, CALLOUT_MPSAFE);
22140f9bd73bSSam Leffler 
22150f9bd73bSSam Leffler 	/*
22160f9bd73bSSam Leffler 	 * Hookup IRQ last.
22170f9bd73bSSam Leffler 	 */
22180f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
22190f9bd73bSSam Leffler 	   bge_intr, sc, &sc->bge_intrhand);
22200f9bd73bSSam Leffler 
22210f9bd73bSSam Leffler 	if (error) {
2222fc74a9f9SBrooks Davis 		bge_detach(dev);
2223fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
22240f9bd73bSSam Leffler 	}
222595d67482SBill Paul 
222695d67482SBill Paul fail:
222795d67482SBill Paul 	return (error);
222895d67482SBill Paul }
222995d67482SBill Paul 
223095d67482SBill Paul static int
22313f74909aSGleb Smirnoff bge_detach(device_t dev)
223295d67482SBill Paul {
223395d67482SBill Paul 	struct bge_softc *sc;
223495d67482SBill Paul 	struct ifnet *ifp;
223595d67482SBill Paul 
223695d67482SBill Paul 	sc = device_get_softc(dev);
2237fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
223895d67482SBill Paul 
223975719184SGleb Smirnoff #ifdef DEVICE_POLLING
224075719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
224175719184SGleb Smirnoff 		ether_poll_deregister(ifp);
224275719184SGleb Smirnoff #endif
224375719184SGleb Smirnoff 
22440f9bd73bSSam Leffler 	BGE_LOCK(sc);
224595d67482SBill Paul 	bge_stop(sc);
224695d67482SBill Paul 	bge_reset(sc);
22470f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
22480f9bd73bSSam Leffler 
22490f9bd73bSSam Leffler 	ether_ifdetach(ifp);
225095d67482SBill Paul 
225195d67482SBill Paul 	if (sc->bge_tbi) {
225295d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
225395d67482SBill Paul 	} else {
225495d67482SBill Paul 		bus_generic_detach(dev);
225595d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
225695d67482SBill Paul 	}
225795d67482SBill Paul 
225895d67482SBill Paul 	bge_release_resources(sc);
225995d67482SBill Paul 
226095d67482SBill Paul 	return (0);
226195d67482SBill Paul }
226295d67482SBill Paul 
226395d67482SBill Paul static void
22643f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
226595d67482SBill Paul {
226695d67482SBill Paul 	device_t dev;
226795d67482SBill Paul 
226895d67482SBill Paul 	dev = sc->bge_dev;
226995d67482SBill Paul 
227095d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
227195d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
227295d67482SBill Paul 
227395d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
227495d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
227595d67482SBill Paul 
227695d67482SBill Paul 	if (sc->bge_intrhand != NULL)
227795d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
227895d67482SBill Paul 
227995d67482SBill Paul 	if (sc->bge_irq != NULL)
228095d67482SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
228195d67482SBill Paul 
228295d67482SBill Paul 	if (sc->bge_res != NULL)
228395d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
228495d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
228595d67482SBill Paul 
2286ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2287ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2288ad61f896SRuslan Ermilov 
2289f41ac2beSBill Paul 	bge_dma_free(sc);
229095d67482SBill Paul 
22910f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
22920f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
229395d67482SBill Paul }
229495d67482SBill Paul 
229595d67482SBill Paul static void
22963f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
229795d67482SBill Paul {
229895d67482SBill Paul 	device_t dev;
22993f74909aSGleb Smirnoff 	uint32_t cachesize, command, pcistate, reset;
230095d67482SBill Paul 	int i, val = 0;
230195d67482SBill Paul 
230295d67482SBill Paul 	dev = sc->bge_dev;
230395d67482SBill Paul 
230495d67482SBill Paul 	/* Save some important PCI state. */
230595d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
230695d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
230795d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
230895d67482SBill Paul 
230995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
231095d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2311e907febfSPyun YongHyeon 	BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
231295d67482SBill Paul 
2313e53d81eeSPaul Saab 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2314e53d81eeSPaul Saab 
2315e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2316e53d81eeSPaul Saab 	if (sc->bge_pcie) {
2317e53d81eeSPaul Saab 		if (CSR_READ_4(sc, 0x7e2c) == 0x60)	/* PCIE 1.0 */
2318e53d81eeSPaul Saab 			CSR_WRITE_4(sc, 0x7e2c, 0x20);
2319e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2320e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
2321e53d81eeSPaul Saab 			CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2322e53d81eeSPaul Saab 			reset |= (1<<29);
2323e53d81eeSPaul Saab 		}
2324e53d81eeSPaul Saab 	}
2325e53d81eeSPaul Saab 
232695d67482SBill Paul 	/* Issue global reset */
2327e53d81eeSPaul Saab 	bge_writereg_ind(sc, BGE_MISC_CFG, reset);
232895d67482SBill Paul 
232995d67482SBill Paul 	DELAY(1000);
233095d67482SBill Paul 
2331e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2332e53d81eeSPaul Saab 	if (sc->bge_pcie) {
2333e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2334e53d81eeSPaul Saab 			uint32_t v;
2335e53d81eeSPaul Saab 
2336e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
2337e53d81eeSPaul Saab 			v = pci_read_config(dev, 0xc4, 4);
2338e53d81eeSPaul Saab 			pci_write_config(dev, 0xc4, v | (1<<15), 4);
2339e53d81eeSPaul Saab 		}
2340e53d81eeSPaul Saab 		/* Set PCIE max payload size and clear error status. */
2341e53d81eeSPaul Saab 		pci_write_config(dev, 0xd8, 0xf5000, 4);
2342e53d81eeSPaul Saab 	}
2343e53d81eeSPaul Saab 
23443f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
234595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
234695d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2347e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
234895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
234995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
235095d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
235195d67482SBill Paul 
2352a7b0c314SPaul Saab 	/* Enable memory arbiter. */
23534c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
23544c0da0ffSGleb Smirnoff 		uint32_t val;
23554c0da0ffSGleb Smirnoff 
23564c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
23574c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
23584c0da0ffSGleb Smirnoff 	} else
2359a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2360a7b0c314SPaul Saab 
236195d67482SBill Paul 	/*
236295d67482SBill Paul 	 * Prevent PXE restart: write a magic number to the
236395d67482SBill Paul 	 * general communications memory at 0xB50.
236495d67482SBill Paul 	 */
236595d67482SBill Paul 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
236695d67482SBill Paul 	/*
236795d67482SBill Paul 	 * Poll the value location we just wrote until
236895d67482SBill Paul 	 * we see the 1's complement of the magic number.
236995d67482SBill Paul 	 * This indicates that the firmware initialization
237095d67482SBill Paul 	 * is complete.
237195d67482SBill Paul 	 */
237295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
237395d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
237495d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
237595d67482SBill Paul 			break;
237695d67482SBill Paul 		DELAY(10);
237795d67482SBill Paul 	}
237895d67482SBill Paul 
237995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2380fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "firmware handshake timed out\n");
238195d67482SBill Paul 		return;
238295d67482SBill Paul 	}
238395d67482SBill Paul 
238495d67482SBill Paul 	/*
238595d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
238695d67482SBill Paul 	 * return to its original pre-reset state. This is a
238795d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
238895d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
238995d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
239095d67482SBill Paul 	 * results.
239195d67482SBill Paul 	 */
239295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
239395d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
239495d67482SBill Paul 			break;
239595d67482SBill Paul 		DELAY(10);
239695d67482SBill Paul 	}
239795d67482SBill Paul 
23983f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
2399e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
240095d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
240195d67482SBill Paul 
240295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
240395d67482SBill Paul 
2404da3003f0SBill Paul 	/*
2405da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
2406da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
2407da3003f0SBill Paul 	 * to 1.2V.
2408da3003f0SBill Paul 	 */
2409da3003f0SBill Paul 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 && sc->bge_tbi) {
2410da3003f0SBill Paul 		uint32_t serdescfg;
2411da3003f0SBill Paul 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2412da3003f0SBill Paul 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
2413da3003f0SBill Paul 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2414da3003f0SBill Paul 	}
2415da3003f0SBill Paul 
2416e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2417e53d81eeSPaul Saab 	if (sc->bge_pcie && sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2418e53d81eeSPaul Saab 		uint32_t v;
2419e53d81eeSPaul Saab 
2420e53d81eeSPaul Saab 		v = CSR_READ_4(sc, 0x7c00);
2421e53d81eeSPaul Saab 		CSR_WRITE_4(sc, 0x7c00, v | (1<<25));
2422e53d81eeSPaul Saab 	}
242395d67482SBill Paul 	DELAY(10000);
242495d67482SBill Paul }
242595d67482SBill Paul 
242695d67482SBill Paul /*
242795d67482SBill Paul  * Frame reception handling. This is called if there's a frame
242895d67482SBill Paul  * on the receive return list.
242995d67482SBill Paul  *
243095d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
24311be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
243295d67482SBill Paul  * 2) the frame is from the standard receive ring
243395d67482SBill Paul  */
243495d67482SBill Paul 
243595d67482SBill Paul static void
24363f74909aSGleb Smirnoff bge_rxeof(struct bge_softc *sc)
243795d67482SBill Paul {
243895d67482SBill Paul 	struct ifnet *ifp;
243995d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
244095d67482SBill Paul 
24410f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
24420f9bd73bSSam Leffler 
24433f74909aSGleb Smirnoff 	/* Nothing to do. */
2444cfcb5025SOleg Bulyzhin 	if (sc->bge_rx_saved_considx ==
2445cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx)
2446cfcb5025SOleg Bulyzhin 		return;
2447cfcb5025SOleg Bulyzhin 
2448fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
244995d67482SBill Paul 
2450f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2451e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
2452f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2453f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
24544c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
2455f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
24564c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTREAD);
2457f41ac2beSBill Paul 
245895d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2459f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
246095d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
24613f74909aSGleb Smirnoff 		uint32_t		rxidx;
246295d67482SBill Paul 		struct mbuf		*m = NULL;
24633f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
246495d67482SBill Paul 		int			have_tag = 0;
246595d67482SBill Paul 
246675719184SGleb Smirnoff #ifdef DEVICE_POLLING
246775719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
246875719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
246975719184SGleb Smirnoff 				break;
247075719184SGleb Smirnoff 			sc->rxcycles--;
247175719184SGleb Smirnoff 		}
247275719184SGleb Smirnoff #endif
247375719184SGleb Smirnoff 
247495d67482SBill Paul 		cur_rx =
2475f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
247695d67482SBill Paul 
247795d67482SBill Paul 		rxidx = cur_rx->bge_idx;
24780434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
247995d67482SBill Paul 
248095d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
248195d67482SBill Paul 			have_tag = 1;
248295d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
248395d67482SBill Paul 		}
248495d67482SBill Paul 
248595d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
248695d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2487f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2488f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2489f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2490f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2491f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
249295d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
249395d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
249495d67482SBill Paul 			jumbocnt++;
249595d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
249695d67482SBill Paul 				ifp->if_ierrors++;
249795d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
249895d67482SBill Paul 				continue;
249995d67482SBill Paul 			}
250095d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
250195d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
250295d67482SBill Paul 				ifp->if_ierrors++;
250395d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
250495d67482SBill Paul 				continue;
250595d67482SBill Paul 			}
250695d67482SBill Paul 		} else {
250795d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2508f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2509f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2510f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2511f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2512f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
251395d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
251495d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
251595d67482SBill Paul 			stdcnt++;
251695d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
251795d67482SBill Paul 				ifp->if_ierrors++;
251895d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
251995d67482SBill Paul 				continue;
252095d67482SBill Paul 			}
252195d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
252295d67482SBill Paul 			    NULL) == ENOBUFS) {
252395d67482SBill Paul 				ifp->if_ierrors++;
252495d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
252595d67482SBill Paul 				continue;
252695d67482SBill Paul 			}
252795d67482SBill Paul 		}
252895d67482SBill Paul 
252995d67482SBill Paul 		ifp->if_ipackets++;
2530e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2531e255b776SJohn Polstra 		/*
2532e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
2533e65bed95SPyun YongHyeon 		 * the payload is aligned.
2534e255b776SJohn Polstra 		 */
2535e255b776SJohn Polstra 		if (sc->bge_rx_alignment_bug) {
2536e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2537e255b776SJohn Polstra 			    cur_rx->bge_len);
2538e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2539e255b776SJohn Polstra 		}
2540e255b776SJohn Polstra #endif
2541473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
254295d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
254395d67482SBill Paul 
2544b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
254578178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
254695d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
254795d67482SBill Paul 				if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
254895d67482SBill Paul 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
254978178cd1SGleb Smirnoff 			}
2550d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
2551d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
255295d67482SBill Paul 				m->m_pkthdr.csum_data =
255395d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
2554ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
2555ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
255695d67482SBill Paul 			}
255795d67482SBill Paul 		}
255895d67482SBill Paul 
255995d67482SBill Paul 		/*
2560673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
2561673d9191SSam Leffler 		 * attach that information to the packet.
256295d67482SBill Paul 		 */
2563d147662cSGleb Smirnoff 		if (have_tag) {
2564d147662cSGleb Smirnoff 			VLAN_INPUT_TAG(ifp, m, vlan_tag);
2565d147662cSGleb Smirnoff 			if (m == NULL)
2566d147662cSGleb Smirnoff 				continue;
2567d147662cSGleb Smirnoff 		}
256895d67482SBill Paul 
25690f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
2570673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
25710f9bd73bSSam Leffler 		BGE_LOCK(sc);
257295d67482SBill Paul 	}
257395d67482SBill Paul 
2574e65bed95SPyun YongHyeon 	if (stdcnt > 0)
2575f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2576e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
25774c0da0ffSGleb Smirnoff 
25784c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc) && jumbocnt > 0)
2579f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
25804c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
2581f41ac2beSBill Paul 
258295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
258395d67482SBill Paul 	if (stdcnt)
258495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
258595d67482SBill Paul 	if (jumbocnt)
258695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
258795d67482SBill Paul }
258895d67482SBill Paul 
258995d67482SBill Paul static void
25903f74909aSGleb Smirnoff bge_txeof(struct bge_softc *sc)
259195d67482SBill Paul {
259295d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
259395d67482SBill Paul 	struct ifnet *ifp;
259495d67482SBill Paul 
25950f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
25960f9bd73bSSam Leffler 
25973f74909aSGleb Smirnoff 	/* Nothing to do. */
2598cfcb5025SOleg Bulyzhin 	if (sc->bge_tx_saved_considx ==
2599cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx)
2600cfcb5025SOleg Bulyzhin 		return;
2601cfcb5025SOleg Bulyzhin 
2602fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
260395d67482SBill Paul 
2604e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
2605e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
2606e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
260795d67482SBill Paul 	/*
260895d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
260995d67482SBill Paul 	 * frames that have been sent.
261095d67482SBill Paul 	 */
261195d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
2612f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
26133f74909aSGleb Smirnoff 		uint32_t		idx = 0;
261495d67482SBill Paul 
261595d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
2616f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
261795d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
261895d67482SBill Paul 			ifp->if_opackets++;
261995d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
2620e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2621e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
2622e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
2623f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2624f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
2625e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
2626e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
262795d67482SBill Paul 		}
262895d67482SBill Paul 		sc->bge_txcnt--;
262995d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
263095d67482SBill Paul 		ifp->if_timer = 0;
263195d67482SBill Paul 	}
263295d67482SBill Paul 
263395d67482SBill Paul 	if (cur_tx != NULL)
263413f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
263595d67482SBill Paul }
263695d67482SBill Paul 
263775719184SGleb Smirnoff #ifdef DEVICE_POLLING
263875719184SGleb Smirnoff static void
263975719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
264075719184SGleb Smirnoff {
264175719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
2642366454f2SOleg Bulyzhin 	uint32_t statusword;
264375719184SGleb Smirnoff 
26443f74909aSGleb Smirnoff 	BGE_LOCK(sc);
26453f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
26463f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
26473f74909aSGleb Smirnoff 		return;
26483f74909aSGleb Smirnoff 	}
264975719184SGleb Smirnoff 
2650dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2651e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2652dab5cd05SOleg Bulyzhin 
26533f74909aSGleb Smirnoff 	statusword = atomic_readandclear_32(
26543f74909aSGleb Smirnoff 	    &sc->bge_ldata.bge_status_block->bge_status);
2655dab5cd05SOleg Bulyzhin 
2656dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2657e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2658366454f2SOleg Bulyzhin 
2659366454f2SOleg Bulyzhin 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS cmd */
2660366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
2661366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
2662366454f2SOleg Bulyzhin 
2663366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
2664366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
26654c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
2666366454f2SOleg Bulyzhin 		    sc->bge_link_evt || sc->bge_tbi)
2667366454f2SOleg Bulyzhin 			bge_link_upd(sc);
2668366454f2SOleg Bulyzhin 
2669366454f2SOleg Bulyzhin 	sc->rxcycles = count;
2670366454f2SOleg Bulyzhin 	bge_rxeof(sc);
2671366454f2SOleg Bulyzhin 	bge_txeof(sc);
2672366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2673366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
26743f74909aSGleb Smirnoff 
26753f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
267675719184SGleb Smirnoff }
267775719184SGleb Smirnoff #endif /* DEVICE_POLLING */
267875719184SGleb Smirnoff 
267995d67482SBill Paul static void
26803f74909aSGleb Smirnoff bge_intr(void *xsc)
268195d67482SBill Paul {
268295d67482SBill Paul 	struct bge_softc *sc;
268395d67482SBill Paul 	struct ifnet *ifp;
2684dab5cd05SOleg Bulyzhin 	uint32_t statusword;
268595d67482SBill Paul 
268695d67482SBill Paul 	sc = xsc;
2687f41ac2beSBill Paul 
26880f9bd73bSSam Leffler 	BGE_LOCK(sc);
26890f9bd73bSSam Leffler 
2690dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
2691dab5cd05SOleg Bulyzhin 
269275719184SGleb Smirnoff #ifdef DEVICE_POLLING
269375719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
269475719184SGleb Smirnoff 		BGE_UNLOCK(sc);
269575719184SGleb Smirnoff 		return;
269675719184SGleb Smirnoff 	}
269775719184SGleb Smirnoff #endif
269875719184SGleb Smirnoff 
2699f30cbfc6SScott Long 	/*
2700f30cbfc6SScott Long 	 * Do the mandatory PCI flush as well as get the link status.
2701f30cbfc6SScott Long 	 */
2702f30cbfc6SScott Long 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
2703f41ac2beSBill Paul 
270495d67482SBill Paul 	/* Ack interrupt and stop others from occuring. */
270595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
270695d67482SBill Paul 
2707f30cbfc6SScott Long 	/* Make sure the descriptor ring indexes are coherent. */
2708f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2709f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2710f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2711f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2712f30cbfc6SScott Long 
27131f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
27144c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
2715f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
2716dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
271795d67482SBill Paul 
271813f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
27193f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
272095d67482SBill Paul 		bge_rxeof(sc);
272195d67482SBill Paul 
27223f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
272395d67482SBill Paul 		bge_txeof(sc);
272495d67482SBill Paul 	}
272595d67482SBill Paul 
272695d67482SBill Paul 	/* Re-enable interrupts. */
272795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
272895d67482SBill Paul 
272913f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
273013f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
27310f9bd73bSSam Leffler 		bge_start_locked(ifp);
27320f9bd73bSSam Leffler 
27330f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
273495d67482SBill Paul }
273595d67482SBill Paul 
273695d67482SBill Paul static void
27373f74909aSGleb Smirnoff bge_tick_locked(struct bge_softc *sc)
27380f9bd73bSSam Leffler {
273995d67482SBill Paul 	struct mii_data *mii = NULL;
274095d67482SBill Paul 
27410f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
274295d67482SBill Paul 
27434c0da0ffSGleb Smirnoff 	if (BGE_IS_5705_OR_BEYOND(sc))
27440434d1b8SBill Paul 		bge_stats_update_regs(sc);
27450434d1b8SBill Paul 	else
274695d67482SBill Paul 		bge_stats_update(sc);
274795d67482SBill Paul 
27481f313773SOleg Bulyzhin 	if (!sc->bge_tbi) {
274995d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
275095d67482SBill Paul 		mii_tick(mii);
27517b97099dSOleg Bulyzhin 	} else {
27527b97099dSOleg Bulyzhin 		/*
27537b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
27547b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
27557b97099dSOleg Bulyzhin 		 * and trigger interrupt.
27567b97099dSOleg Bulyzhin 		 */
27577b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
27583f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
27597b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
27607b97099dSOleg Bulyzhin #endif
27617b97099dSOleg Bulyzhin 		{
27627b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
27637b97099dSOleg Bulyzhin 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
27647b97099dSOleg Bulyzhin 		}
2765dab5cd05SOleg Bulyzhin 	}
276695d67482SBill Paul 
2767dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
276895d67482SBill Paul }
276995d67482SBill Paul 
277095d67482SBill Paul static void
27713f74909aSGleb Smirnoff bge_tick(void *xsc)
27720f9bd73bSSam Leffler {
27730f9bd73bSSam Leffler 	struct bge_softc *sc;
27740f9bd73bSSam Leffler 
27750f9bd73bSSam Leffler 	sc = xsc;
27760f9bd73bSSam Leffler 
27770f9bd73bSSam Leffler 	BGE_LOCK(sc);
27780f9bd73bSSam Leffler 	bge_tick_locked(sc);
27790f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
27800f9bd73bSSam Leffler }
27810f9bd73bSSam Leffler 
27820f9bd73bSSam Leffler static void
27833f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
27840434d1b8SBill Paul {
27850434d1b8SBill Paul 	struct bge_mac_stats_regs stats;
27863f74909aSGleb Smirnoff 	struct ifnet *ifp;
27873f74909aSGleb Smirnoff 	uint32_t *s;
27886fb34dd2SOleg Bulyzhin 	u_long cnt;	/* current register value */
27890434d1b8SBill Paul 	int i;
27900434d1b8SBill Paul 
2791fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
27920434d1b8SBill Paul 
27933f74909aSGleb Smirnoff 	s = (uint32_t *)&stats;
27940434d1b8SBill Paul 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
27950434d1b8SBill Paul 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
27960434d1b8SBill Paul 		s++;
27970434d1b8SBill Paul 	}
27980434d1b8SBill Paul 
27996fb34dd2SOleg Bulyzhin 	cnt = stats.dot3StatsSingleCollisionFrames +
28000434d1b8SBill Paul 	    stats.dot3StatsMultipleCollisionFrames +
28010434d1b8SBill Paul 	    stats.dot3StatsExcessiveCollisions +
28026fb34dd2SOleg Bulyzhin 	    stats.dot3StatsLateCollisions;
28036fb34dd2SOleg Bulyzhin 	ifp->if_collisions += cnt >= sc->bge_tx_collisions ?
28046fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_collisions : cnt;
28056fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
28060434d1b8SBill Paul }
28070434d1b8SBill Paul 
28080434d1b8SBill Paul static void
28093f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
281095d67482SBill Paul {
281195d67482SBill Paul 	struct ifnet *ifp;
2812e907febfSPyun YongHyeon 	bus_size_t stats;
28136fb34dd2SOleg Bulyzhin 	u_long cnt;	/* current register value */
281495d67482SBill Paul 
2815fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
281695d67482SBill Paul 
2817e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
2818e907febfSPyun YongHyeon 
2819e907febfSPyun YongHyeon #define READ_STAT(sc, stats, stat) \
2820e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
282195d67482SBill Paul 
28226fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats,
28236fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsSingleCollisionFrames.bge_addr_lo);
28246fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
28256fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsMultipleCollisionFrames.bge_addr_lo);
28266fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
28276fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsExcessiveCollisions.bge_addr_lo);
28286fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
28296fb34dd2SOleg Bulyzhin 		txstats.dot3StatsLateCollisions.bge_addr_lo);
28306fb34dd2SOleg Bulyzhin 	ifp->if_collisions += cnt >= sc->bge_tx_collisions ?
28316fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_collisions : cnt;
28326fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
28336fb34dd2SOleg Bulyzhin 
28346fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
28356fb34dd2SOleg Bulyzhin 	ifp->if_ierrors += cnt >= sc->bge_rx_discards ?
28366fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_rx_discards : cnt;
28376fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
28386fb34dd2SOleg Bulyzhin 
28396fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
28406fb34dd2SOleg Bulyzhin 	ifp->if_oerrors += cnt >= sc->bge_tx_discards ?
28416fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_discards : cnt;
28426fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
284395d67482SBill Paul 
2844e907febfSPyun YongHyeon #undef READ_STAT
284595d67482SBill Paul }
284695d67482SBill Paul 
284795d67482SBill Paul /*
2848d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
2849d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
2850d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
2851d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
2852d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
2853d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
2854d375e524SGleb Smirnoff  */
2855d375e524SGleb Smirnoff static __inline int
2856d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
2857d375e524SGleb Smirnoff {
2858d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
2859d375e524SGleb Smirnoff 	struct mbuf *last;
2860d375e524SGleb Smirnoff 
2861d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
2862d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
2863d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
2864d375e524SGleb Smirnoff 		last = m;
2865d375e524SGleb Smirnoff 	} else {
2866d375e524SGleb Smirnoff 		/*
2867d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
2868d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
2869d375e524SGleb Smirnoff 		 */
2870d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
2871d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
2872d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
2873d375e524SGleb Smirnoff 			struct mbuf *n;
2874d375e524SGleb Smirnoff 
2875d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
2876d375e524SGleb Smirnoff 			if (n == NULL)
2877d375e524SGleb Smirnoff 				return (ENOBUFS);
2878d375e524SGleb Smirnoff 			n->m_len = 0;
2879d375e524SGleb Smirnoff 			last->m_next = n;
2880d375e524SGleb Smirnoff 			last = n;
2881d375e524SGleb Smirnoff 		}
2882d375e524SGleb Smirnoff 	}
2883d375e524SGleb Smirnoff 
2884d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
2885d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
2886d375e524SGleb Smirnoff 	last->m_len += padlen;
2887d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
2888d375e524SGleb Smirnoff 
2889d375e524SGleb Smirnoff 	return (0);
2890d375e524SGleb Smirnoff }
2891d375e524SGleb Smirnoff 
2892d375e524SGleb Smirnoff /*
289395d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
289495d67482SBill Paul  * pointers to descriptors.
289595d67482SBill Paul  */
289695d67482SBill Paul static int
2897676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
289895d67482SBill Paul {
28997e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
2900f41ac2beSBill Paul 	bus_dmamap_t		map;
2901676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
2902676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
29037e27542aSGleb Smirnoff 	struct m_tag		*mtag;
29047e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
2905676ad2c9SGleb Smirnoff 	uint16_t		csum_flags;
29067e27542aSGleb Smirnoff 	int			nsegs, i, error;
290795d67482SBill Paul 
29087e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
2909676ad2c9SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m, segs,
2910676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
29117e27542aSGleb Smirnoff 	if (error == EFBIG) {
2912676ad2c9SGleb Smirnoff 		m = m_defrag(m, M_DONTWAIT);
2913676ad2c9SGleb Smirnoff 		if (m == NULL) {
2914676ad2c9SGleb Smirnoff 			m_freem(*m_head);
2915676ad2c9SGleb Smirnoff 			*m_head = NULL;
29167e27542aSGleb Smirnoff 			return (ENOBUFS);
29177e27542aSGleb Smirnoff 		}
2918676ad2c9SGleb Smirnoff 		*m_head = m;
2919676ad2c9SGleb Smirnoff 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m,
2920676ad2c9SGleb Smirnoff 		    segs, &nsegs, BUS_DMA_NOWAIT);
2921676ad2c9SGleb Smirnoff 		if (error) {
2922676ad2c9SGleb Smirnoff 			m_freem(m);
2923676ad2c9SGleb Smirnoff 			*m_head = NULL;
29247e27542aSGleb Smirnoff 			return (error);
29257e27542aSGleb Smirnoff 		}
2926676ad2c9SGleb Smirnoff 	} else if (error != 0)
2927676ad2c9SGleb Smirnoff 		return (error);
29287e27542aSGleb Smirnoff 
292995d67482SBill Paul 	/*
293095d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
293195d67482SBill Paul 	 * of the end of the ring.
293295d67482SBill Paul 	 */
29337e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
29347e27542aSGleb Smirnoff 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
293595d67482SBill Paul 		return (ENOBUFS);
29367e27542aSGleb Smirnoff 	}
29377e27542aSGleb Smirnoff 
2938676ad2c9SGleb Smirnoff 	csum_flags = 0;
2939676ad2c9SGleb Smirnoff 	if (m->m_pkthdr.csum_flags) {
2940676ad2c9SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
2941676ad2c9SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
2942676ad2c9SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
2943676ad2c9SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
2944676ad2c9SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
2945676ad2c9SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
2946676ad2c9SGleb Smirnoff 				bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
2947676ad2c9SGleb Smirnoff 				m_freem(m);
2948676ad2c9SGleb Smirnoff 				*m_head = NULL;
2949676ad2c9SGleb Smirnoff 				return (error);
2950676ad2c9SGleb Smirnoff 			}
2951676ad2c9SGleb Smirnoff 		}
2952676ad2c9SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
2953676ad2c9SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
2954676ad2c9SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
2955676ad2c9SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
2956676ad2c9SGleb Smirnoff 	}
2957676ad2c9SGleb Smirnoff 
2958e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
2959e65bed95SPyun YongHyeon 
29607e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
29617e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
29627e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
29637e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
29647e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
29657e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
29667e27542aSGleb Smirnoff 		if (i == nsegs - 1)
29677e27542aSGleb Smirnoff 			break;
29687e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
29697e27542aSGleb Smirnoff 	}
29707e27542aSGleb Smirnoff 
29717e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
29727e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
2973676ad2c9SGleb Smirnoff 
29747e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
29757e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
2976676ad2c9SGleb Smirnoff 	if ((mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m)) != NULL) {
29777e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
29787e27542aSGleb Smirnoff 		d->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
29797e27542aSGleb Smirnoff 	} else
29807e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
2981f41ac2beSBill Paul 
2982f41ac2beSBill Paul 	/*
2983f41ac2beSBill Paul 	 * Insure that the map for this transmission
2984f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
2985f41ac2beSBill Paul 	 * in this chain.
2986f41ac2beSBill Paul 	 */
29877e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
29887e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
2989676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
29907e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
299195d67482SBill Paul 
29927e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
29937e27542aSGleb Smirnoff 	*txidx = idx;
299495d67482SBill Paul 
299595d67482SBill Paul 	return (0);
299695d67482SBill Paul }
299795d67482SBill Paul 
299895d67482SBill Paul /*
299995d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
300095d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
300195d67482SBill Paul  */
300295d67482SBill Paul static void
30033f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
300495d67482SBill Paul {
300595d67482SBill Paul 	struct bge_softc *sc;
300695d67482SBill Paul 	struct mbuf *m_head = NULL;
300714bbd30fSGleb Smirnoff 	uint32_t prodidx;
3008303a718cSDag-Erling Smørgrav 	int count = 0;
300995d67482SBill Paul 
301095d67482SBill Paul 	sc = ifp->if_softc;
301195d67482SBill Paul 
3012dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
301395d67482SBill Paul 		return;
301495d67482SBill Paul 
301514bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
301695d67482SBill Paul 
301795d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
30184d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
301995d67482SBill Paul 		if (m_head == NULL)
302095d67482SBill Paul 			break;
302195d67482SBill Paul 
302295d67482SBill Paul 		/*
302395d67482SBill Paul 		 * XXX
3024b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
3025b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3026b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
3027b874fdd4SYaroslav Tykhiy 		 *
3028b874fdd4SYaroslav Tykhiy 		 * XXX
302995d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
303095d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
303195d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
303295d67482SBill Paul 		 * chain at once.
303395d67482SBill Paul 		 * (paranoia -- may not actually be needed)
303495d67482SBill Paul 		 */
303595d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
303695d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
303795d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
303895d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
30394d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
304013f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
304195d67482SBill Paul 				break;
304295d67482SBill Paul 			}
304395d67482SBill Paul 		}
304495d67482SBill Paul 
304595d67482SBill Paul 		/*
304695d67482SBill Paul 		 * Pack the data into the transmit ring. If we
304795d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
304895d67482SBill Paul 		 * for the NIC to drain the ring.
304995d67482SBill Paul 		 */
3050676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
3051676ad2c9SGleb Smirnoff 			if (m_head == NULL)
3052676ad2c9SGleb Smirnoff 				break;
30534d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
305413f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
305595d67482SBill Paul 			break;
305695d67482SBill Paul 		}
3057303a718cSDag-Erling Smørgrav 		++count;
305895d67482SBill Paul 
305995d67482SBill Paul 		/*
306095d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
306195d67482SBill Paul 		 * to him.
306295d67482SBill Paul 		 */
3063673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
306495d67482SBill Paul 	}
306595d67482SBill Paul 
30663f74909aSGleb Smirnoff 	if (count == 0)
30673f74909aSGleb Smirnoff 		/* No packets were dequeued. */
3068303a718cSDag-Erling Smørgrav 		return;
3069303a718cSDag-Erling Smørgrav 
30703f74909aSGleb Smirnoff 	/* Transmit. */
307195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
30723927098fSPaul Saab 	/* 5700 b2 errata */
3073e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
30743927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
307595d67482SBill Paul 
307614bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
307714bbd30fSGleb Smirnoff 
307895d67482SBill Paul 	/*
307995d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
308095d67482SBill Paul 	 */
308195d67482SBill Paul 	ifp->if_timer = 5;
308295d67482SBill Paul }
308395d67482SBill Paul 
30840f9bd73bSSam Leffler /*
30850f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
30860f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
30870f9bd73bSSam Leffler  */
308895d67482SBill Paul static void
30893f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
309095d67482SBill Paul {
30910f9bd73bSSam Leffler 	struct bge_softc *sc;
30920f9bd73bSSam Leffler 
30930f9bd73bSSam Leffler 	sc = ifp->if_softc;
30940f9bd73bSSam Leffler 	BGE_LOCK(sc);
30950f9bd73bSSam Leffler 	bge_start_locked(ifp);
30960f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
30970f9bd73bSSam Leffler }
30980f9bd73bSSam Leffler 
30990f9bd73bSSam Leffler static void
31003f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
31010f9bd73bSSam Leffler {
310295d67482SBill Paul 	struct ifnet *ifp;
31033f74909aSGleb Smirnoff 	uint16_t *m;
310495d67482SBill Paul 
31050f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
310695d67482SBill Paul 
3107fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
310895d67482SBill Paul 
310913f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
311095d67482SBill Paul 		return;
311195d67482SBill Paul 
311295d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
311395d67482SBill Paul 	bge_stop(sc);
311495d67482SBill Paul 	bge_reset(sc);
311595d67482SBill Paul 	bge_chipinit(sc);
311695d67482SBill Paul 
311795d67482SBill Paul 	/*
311895d67482SBill Paul 	 * Init the various state machines, ring
311995d67482SBill Paul 	 * control blocks and firmware.
312095d67482SBill Paul 	 */
312195d67482SBill Paul 	if (bge_blockinit(sc)) {
3122fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
312395d67482SBill Paul 		return;
312495d67482SBill Paul 	}
312595d67482SBill Paul 
3126fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
312795d67482SBill Paul 
312895d67482SBill Paul 	/* Specify MTU. */
312995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3130859c6c7dSBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
313195d67482SBill Paul 
313295d67482SBill Paul 	/* Load our MAC address. */
31333f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
313495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
313595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
313695d67482SBill Paul 
313795d67482SBill Paul 	/* Enable or disable promiscuous mode as needed. */
313895d67482SBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
313995d67482SBill Paul 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
314095d67482SBill Paul 	} else {
314195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
314295d67482SBill Paul 	}
314395d67482SBill Paul 
314495d67482SBill Paul 	/* Program multicast filter. */
314595d67482SBill Paul 	bge_setmulti(sc);
314695d67482SBill Paul 
314795d67482SBill Paul 	/* Init RX ring. */
314895d67482SBill Paul 	bge_init_rx_ring_std(sc);
314995d67482SBill Paul 
31500434d1b8SBill Paul 	/*
31510434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
31520434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
31530434d1b8SBill Paul 	 * entry of the ring.
31540434d1b8SBill Paul 	 */
31550434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
31563f74909aSGleb Smirnoff 		uint32_t		v, i;
31570434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
31580434d1b8SBill Paul 			DELAY(20);
31590434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
31600434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
31610434d1b8SBill Paul 				break;
31620434d1b8SBill Paul 		}
31630434d1b8SBill Paul 		if (i == 10)
3164fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
3165fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
31660434d1b8SBill Paul 	}
31670434d1b8SBill Paul 
316895d67482SBill Paul 	/* Init jumbo RX ring. */
316995d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
317095d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
317195d67482SBill Paul 
31723f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
317395d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
317495d67482SBill Paul 
317595d67482SBill Paul 	/* Init TX ring. */
317695d67482SBill Paul 	bge_init_tx_ring(sc);
317795d67482SBill Paul 
31783f74909aSGleb Smirnoff 	/* Turn on transmitter. */
317995d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
318095d67482SBill Paul 
31813f74909aSGleb Smirnoff 	/* Turn on receiver. */
318295d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
318395d67482SBill Paul 
318495d67482SBill Paul 	/* Tell firmware we're alive. */
318595d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
318695d67482SBill Paul 
318775719184SGleb Smirnoff #ifdef DEVICE_POLLING
318875719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
318975719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
319075719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
319175719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
319275719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
319375719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
319475719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
319575719184SGleb Smirnoff 	} else
319675719184SGleb Smirnoff #endif
319775719184SGleb Smirnoff 
319895d67482SBill Paul 	/* Enable host interrupts. */
319975719184SGleb Smirnoff 	{
320095d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
320195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
320295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
320375719184SGleb Smirnoff 	}
320495d67482SBill Paul 
320595d67482SBill Paul 	bge_ifmedia_upd(ifp);
320695d67482SBill Paul 
320713f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
320813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
320995d67482SBill Paul 
32100f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
32110f9bd73bSSam Leffler }
32120f9bd73bSSam Leffler 
32130f9bd73bSSam Leffler static void
32143f74909aSGleb Smirnoff bge_init(void *xsc)
32150f9bd73bSSam Leffler {
32160f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
32170f9bd73bSSam Leffler 
32180f9bd73bSSam Leffler 	BGE_LOCK(sc);
32190f9bd73bSSam Leffler 	bge_init_locked(sc);
32200f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
322195d67482SBill Paul }
322295d67482SBill Paul 
322395d67482SBill Paul /*
322495d67482SBill Paul  * Set media options.
322595d67482SBill Paul  */
322695d67482SBill Paul static int
32273f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
322895d67482SBill Paul {
322995d67482SBill Paul 	struct bge_softc *sc;
323095d67482SBill Paul 	struct mii_data *mii;
323195d67482SBill Paul 	struct ifmedia *ifm;
323295d67482SBill Paul 
323395d67482SBill Paul 	sc = ifp->if_softc;
323495d67482SBill Paul 	ifm = &sc->bge_ifmedia;
323595d67482SBill Paul 
323695d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
323795d67482SBill Paul 	if (sc->bge_tbi) {
323895d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
323995d67482SBill Paul 			return (EINVAL);
324095d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
324195d67482SBill Paul 		case IFM_AUTO:
3242ff50922bSDoug White 			/*
3243ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
3244ff50922bSDoug White 			 * mechanism for programming the autoneg
3245ff50922bSDoug White 			 * advertisement registers in TBI mode.
3246ff50922bSDoug White 			 */
3247c4529f41SMichael Reifenberger 			if (bge_fake_autoneg == 0 &&
3248c4529f41SMichael Reifenberger 			    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3249ff50922bSDoug White 				uint32_t sgdig;
3250ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3251ff50922bSDoug White 				sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3252ff50922bSDoug White 				sgdig |= BGE_SGDIGCFG_AUTO|
3253ff50922bSDoug White 				    BGE_SGDIGCFG_PAUSE_CAP|
3254ff50922bSDoug White 				    BGE_SGDIGCFG_ASYM_PAUSE;
3255ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3256ff50922bSDoug White 				    sgdig|BGE_SGDIGCFG_SEND);
3257ff50922bSDoug White 				DELAY(5);
3258ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3259ff50922bSDoug White 			}
326095d67482SBill Paul 			break;
326195d67482SBill Paul 		case IFM_1000_SX:
326295d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
326395d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
326495d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
326595d67482SBill Paul 			} else {
326695d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
326795d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
326895d67482SBill Paul 			}
326995d67482SBill Paul 			break;
327095d67482SBill Paul 		default:
327195d67482SBill Paul 			return (EINVAL);
327295d67482SBill Paul 		}
327395d67482SBill Paul 		return (0);
327495d67482SBill Paul 	}
327595d67482SBill Paul 
32761493e883SOleg Bulyzhin 	sc->bge_link_evt++;
327795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
327895d67482SBill Paul 	if (mii->mii_instance) {
327995d67482SBill Paul 		struct mii_softc *miisc;
328095d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
328195d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
328295d67482SBill Paul 			mii_phy_reset(miisc);
328395d67482SBill Paul 	}
328495d67482SBill Paul 	mii_mediachg(mii);
328595d67482SBill Paul 
328695d67482SBill Paul 	return (0);
328795d67482SBill Paul }
328895d67482SBill Paul 
328995d67482SBill Paul /*
329095d67482SBill Paul  * Report current media status.
329195d67482SBill Paul  */
329295d67482SBill Paul static void
32933f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
329495d67482SBill Paul {
329595d67482SBill Paul 	struct bge_softc *sc;
329695d67482SBill Paul 	struct mii_data *mii;
329795d67482SBill Paul 
329895d67482SBill Paul 	sc = ifp->if_softc;
329995d67482SBill Paul 
330095d67482SBill Paul 	if (sc->bge_tbi) {
330195d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
330295d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
330395d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
330495d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
330595d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
33064c0da0ffSGleb Smirnoff 		else {
33074c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
33084c0da0ffSGleb Smirnoff 			return;
33094c0da0ffSGleb Smirnoff 		}
331095d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
331195d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
331295d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
331395d67482SBill Paul 		else
331495d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
331595d67482SBill Paul 		return;
331695d67482SBill Paul 	}
331795d67482SBill Paul 
331895d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
331995d67482SBill Paul 	mii_pollstat(mii);
332095d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
332195d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
332295d67482SBill Paul }
332395d67482SBill Paul 
332495d67482SBill Paul static int
33253f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
332695d67482SBill Paul {
332795d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
332895d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
332995d67482SBill Paul 	struct mii_data *mii;
33303f74909aSGleb Smirnoff 	int mask, error = 0;
333195d67482SBill Paul 
333295d67482SBill Paul 	switch (command) {
333395d67482SBill Paul 	case SIOCSIFMTU:
33344c0da0ffSGleb Smirnoff 		if (ifr->ifr_mtu < ETHERMIN ||
33354c0da0ffSGleb Smirnoff 		    ((BGE_IS_JUMBO_CAPABLE(sc)) &&
33364c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > BGE_JUMBO_MTU) ||
33374c0da0ffSGleb Smirnoff 		    ((!BGE_IS_JUMBO_CAPABLE(sc)) &&
33384c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > ETHERMTU))
333995d67482SBill Paul 			error = EINVAL;
33404c0da0ffSGleb Smirnoff 		else if (ifp->if_mtu != ifr->ifr_mtu) {
334195d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
334213f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
334395d67482SBill Paul 			bge_init(sc);
334495d67482SBill Paul 		}
334595d67482SBill Paul 		break;
334695d67482SBill Paul 	case SIOCSIFFLAGS:
33470f9bd73bSSam Leffler 		BGE_LOCK(sc);
334895d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
334995d67482SBill Paul 			/*
335095d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
335195d67482SBill Paul 			 * then just use the 'set promisc mode' command
335295d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
335395d67482SBill Paul 			 * a full re-init means reloading the firmware and
335495d67482SBill Paul 			 * waiting for it to start up, which may take a
3355d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
335695d67482SBill Paul 			 */
335713f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
335895d67482SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
335995d67482SBill Paul 			    !(sc->bge_if_flags & IFF_PROMISC)) {
336095d67482SBill Paul 				BGE_SETBIT(sc, BGE_RX_MODE,
336195d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
336213f4c340SRobert Watson 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
336395d67482SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
336495d67482SBill Paul 			    sc->bge_if_flags & IFF_PROMISC) {
336595d67482SBill Paul 				BGE_CLRBIT(sc, BGE_RX_MODE,
336695d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
3367d183af7fSRuslan Ermilov 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3368d183af7fSRuslan Ermilov 			    (ifp->if_flags ^ sc->bge_if_flags) & IFF_ALLMULTI) {
3369d183af7fSRuslan Ermilov 				bge_setmulti(sc);
337095d67482SBill Paul 			} else
33710f9bd73bSSam Leffler 				bge_init_locked(sc);
337295d67482SBill Paul 		} else {
337313f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
337495d67482SBill Paul 				bge_stop(sc);
337595d67482SBill Paul 			}
337695d67482SBill Paul 		}
337795d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
33780f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
337995d67482SBill Paul 		error = 0;
338095d67482SBill Paul 		break;
338195d67482SBill Paul 	case SIOCADDMULTI:
338295d67482SBill Paul 	case SIOCDELMULTI:
338313f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
33840f9bd73bSSam Leffler 			BGE_LOCK(sc);
338595d67482SBill Paul 			bge_setmulti(sc);
33860f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
338795d67482SBill Paul 			error = 0;
338895d67482SBill Paul 		}
338995d67482SBill Paul 		break;
339095d67482SBill Paul 	case SIOCSIFMEDIA:
339195d67482SBill Paul 	case SIOCGIFMEDIA:
339295d67482SBill Paul 		if (sc->bge_tbi) {
339395d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
339495d67482SBill Paul 			    &sc->bge_ifmedia, command);
339595d67482SBill Paul 		} else {
339695d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
339795d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
339895d67482SBill Paul 			    &mii->mii_media, command);
339995d67482SBill Paul 		}
340095d67482SBill Paul 		break;
340195d67482SBill Paul 	case SIOCSIFCAP:
340295d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
340375719184SGleb Smirnoff #ifdef DEVICE_POLLING
340475719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
340575719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
340675719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
340775719184SGleb Smirnoff 				if (error)
340875719184SGleb Smirnoff 					return (error);
340975719184SGleb Smirnoff 				BGE_LOCK(sc);
341075719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
341175719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
341275719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
341375719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
341475719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
341575719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
341675719184SGleb Smirnoff 				BGE_UNLOCK(sc);
341775719184SGleb Smirnoff 			} else {
341875719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
341975719184SGleb Smirnoff 				/* Enable interrupt even in error case */
342075719184SGleb Smirnoff 				BGE_LOCK(sc);
342175719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
342275719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
342375719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
342475719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
342575719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
342675719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
342775719184SGleb Smirnoff 				BGE_UNLOCK(sc);
342875719184SGleb Smirnoff 			}
342975719184SGleb Smirnoff 		}
343075719184SGleb Smirnoff #endif
3431d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
3432d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
3433d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
3434d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
3435b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
343695d67482SBill Paul 			else
3437b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
3438479b23b7SGleb Smirnoff 			VLAN_CAPABILITIES(ifp);
343995d67482SBill Paul 		}
344095d67482SBill Paul 		break;
344195d67482SBill Paul 	default:
3442673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
344395d67482SBill Paul 		break;
344495d67482SBill Paul 	}
344595d67482SBill Paul 
344695d67482SBill Paul 	return (error);
344795d67482SBill Paul }
344895d67482SBill Paul 
344995d67482SBill Paul static void
34503f74909aSGleb Smirnoff bge_watchdog(struct ifnet *ifp)
345195d67482SBill Paul {
345295d67482SBill Paul 	struct bge_softc *sc;
345395d67482SBill Paul 
345495d67482SBill Paul 	sc = ifp->if_softc;
345595d67482SBill Paul 
3456fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
345795d67482SBill Paul 
345813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
345995d67482SBill Paul 	bge_init(sc);
346095d67482SBill Paul 
346195d67482SBill Paul 	ifp->if_oerrors++;
346295d67482SBill Paul }
346395d67482SBill Paul 
346495d67482SBill Paul /*
346595d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
346695d67482SBill Paul  * RX and TX lists.
346795d67482SBill Paul  */
346895d67482SBill Paul static void
34693f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
347095d67482SBill Paul {
347195d67482SBill Paul 	struct ifnet *ifp;
347295d67482SBill Paul 	struct ifmedia_entry *ifm;
347395d67482SBill Paul 	struct mii_data *mii = NULL;
347495d67482SBill Paul 	int mtmp, itmp;
347595d67482SBill Paul 
34760f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
34770f9bd73bSSam Leffler 
3478fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
347995d67482SBill Paul 
348095d67482SBill Paul 	if (!sc->bge_tbi)
348195d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
348295d67482SBill Paul 
34830f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
348495d67482SBill Paul 
348595d67482SBill Paul 	/*
34863f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
348795d67482SBill Paul 	 */
348895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
348995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
349095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
34914c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
349295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
349395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
349495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
349595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
349695d67482SBill Paul 
349795d67482SBill Paul 	/*
34983f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
349995d67482SBill Paul 	 */
350095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
350195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
350295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
350395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
350495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
35054c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
350695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
350795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
350895d67482SBill Paul 
350995d67482SBill Paul 	/*
351095d67482SBill Paul 	 * Shut down all of the memory managers and related
351195d67482SBill Paul 	 * state machines.
351295d67482SBill Paul 	 */
351395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
351495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
35154c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
351695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
351795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
351895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
35194c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc))) {
352095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
352195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
35220434d1b8SBill Paul 	}
352395d67482SBill Paul 
352495d67482SBill Paul 	/* Disable host interrupts. */
352595d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
352695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
352795d67482SBill Paul 
352895d67482SBill Paul 	/*
352995d67482SBill Paul 	 * Tell firmware we're shutting down.
353095d67482SBill Paul 	 */
353195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
353295d67482SBill Paul 
353395d67482SBill Paul 	/* Free the RX lists. */
353495d67482SBill Paul 	bge_free_rx_ring_std(sc);
353595d67482SBill Paul 
353695d67482SBill Paul 	/* Free jumbo RX list. */
35374c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
353895d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
353995d67482SBill Paul 
354095d67482SBill Paul 	/* Free TX buffers. */
354195d67482SBill Paul 	bge_free_tx_ring(sc);
354295d67482SBill Paul 
354395d67482SBill Paul 	/*
354495d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
354595d67482SBill Paul 	 * unchanged so that things will be put back to normal when
354695d67482SBill Paul 	 * we bring the interface back up.
354795d67482SBill Paul 	 */
354895d67482SBill Paul 	if (!sc->bge_tbi) {
354995d67482SBill Paul 		itmp = ifp->if_flags;
355095d67482SBill Paul 		ifp->if_flags |= IFF_UP;
3551dcc34049SPawel Jakub Dawidek 		/*
3552dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
3553dcc34049SPawel Jakub Dawidek 		 */
3554dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
355595d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
355695d67482SBill Paul 			mtmp = ifm->ifm_media;
355795d67482SBill Paul 			ifm->ifm_media = IFM_ETHER|IFM_NONE;
355895d67482SBill Paul 			mii_mediachg(mii);
355995d67482SBill Paul 			ifm->ifm_media = mtmp;
3560dcc34049SPawel Jakub Dawidek 		}
356195d67482SBill Paul 		ifp->if_flags = itmp;
356295d67482SBill Paul 	}
356395d67482SBill Paul 
356495d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
356595d67482SBill Paul 
35661493e883SOleg Bulyzhin 	/*
35671493e883SOleg Bulyzhin 	 * We can't just call bge_link_upd() cause chip is almost stopped so
35681493e883SOleg Bulyzhin 	 * bge_link_upd -> bge_tick_locked -> bge_stats_update sequence may
35691493e883SOleg Bulyzhin 	 * lead to hardware deadlock. So we just clearing MAC's link state
35701493e883SOleg Bulyzhin 	 * (PHY may still have link UP).
35711493e883SOleg Bulyzhin 	 */
35721493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
35731493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
35741493e883SOleg Bulyzhin 	sc->bge_link = 0;
357595d67482SBill Paul 
35761493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
357795d67482SBill Paul }
357895d67482SBill Paul 
357995d67482SBill Paul /*
358095d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
358195d67482SBill Paul  * get confused by errant DMAs when rebooting.
358295d67482SBill Paul  */
358395d67482SBill Paul static void
35843f74909aSGleb Smirnoff bge_shutdown(device_t dev)
358595d67482SBill Paul {
358695d67482SBill Paul 	struct bge_softc *sc;
358795d67482SBill Paul 
358895d67482SBill Paul 	sc = device_get_softc(dev);
358995d67482SBill Paul 
35900f9bd73bSSam Leffler 	BGE_LOCK(sc);
359195d67482SBill Paul 	bge_stop(sc);
359295d67482SBill Paul 	bge_reset(sc);
35930f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
359495d67482SBill Paul }
359514afefa3SPawel Jakub Dawidek 
359614afefa3SPawel Jakub Dawidek static int
359714afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
359814afefa3SPawel Jakub Dawidek {
359914afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
360014afefa3SPawel Jakub Dawidek 
360114afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
360214afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
360314afefa3SPawel Jakub Dawidek 	bge_stop(sc);
360414afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
360514afefa3SPawel Jakub Dawidek 
360614afefa3SPawel Jakub Dawidek 	return (0);
360714afefa3SPawel Jakub Dawidek }
360814afefa3SPawel Jakub Dawidek 
360914afefa3SPawel Jakub Dawidek static int
361014afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
361114afefa3SPawel Jakub Dawidek {
361214afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
361314afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
361414afefa3SPawel Jakub Dawidek 
361514afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
361614afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
361714afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
361814afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
361914afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
362014afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
362114afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
362214afefa3SPawel Jakub Dawidek 	}
362314afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
362414afefa3SPawel Jakub Dawidek 
362514afefa3SPawel Jakub Dawidek 	return (0);
362614afefa3SPawel Jakub Dawidek }
3627dab5cd05SOleg Bulyzhin 
3628dab5cd05SOleg Bulyzhin static void
36293f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
3630dab5cd05SOleg Bulyzhin {
36311f313773SOleg Bulyzhin 	struct mii_data *mii;
36321f313773SOleg Bulyzhin 	uint32_t link, status;
3633dab5cd05SOleg Bulyzhin 
3634dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
36351f313773SOleg Bulyzhin 
36363f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
36377b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
36387b97099dSOleg Bulyzhin 
3639dab5cd05SOleg Bulyzhin 	/*
3640dab5cd05SOleg Bulyzhin 	 * Process link state changes.
3641dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
3642dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
3643dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
3644dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
3645dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
3646dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
3647dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
3648dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
36491f313773SOleg Bulyzhin 	 *
36501f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
36514c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
3652dab5cd05SOleg Bulyzhin 	 */
3653dab5cd05SOleg Bulyzhin 
36541f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
36554c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
3656dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
3657dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
3658dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3659dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
36601f313773SOleg Bulyzhin 
36611f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
36621f313773SOleg Bulyzhin 			if (!sc->bge_link &&
36631f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
36641f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
36651f313773SOleg Bulyzhin 				sc->bge_link++;
36661f313773SOleg Bulyzhin 				if (bootverbose)
36671f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
36681f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
36691f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
36701f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
36711f313773SOleg Bulyzhin 				sc->bge_link = 0;
36721f313773SOleg Bulyzhin 				if (bootverbose)
36731f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
36741f313773SOleg Bulyzhin 			}
36751f313773SOleg Bulyzhin 
36763f74909aSGleb Smirnoff 			/* Clear the interrupt. */
3677dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
3678dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
3679dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
3680dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
3681dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
3682dab5cd05SOleg Bulyzhin 		}
3683dab5cd05SOleg Bulyzhin 		return;
3684dab5cd05SOleg Bulyzhin 	}
3685dab5cd05SOleg Bulyzhin 
36861f313773SOleg Bulyzhin 	if (sc->bge_tbi) {
36871f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
36887b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
36897b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
36901f313773SOleg Bulyzhin 				sc->bge_link++;
36911f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
36921f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
36931f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
36941f313773SOleg Bulyzhin 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
36951f313773SOleg Bulyzhin 				if (bootverbose)
36961f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
36973f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
36983f74909aSGleb Smirnoff 				    LINK_STATE_UP);
36997b97099dSOleg Bulyzhin 			}
37001f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
3701dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
37021f313773SOleg Bulyzhin 			if (bootverbose)
37031f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
37047b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
37051f313773SOleg Bulyzhin 		}
37061493e883SOleg Bulyzhin 	/* Discard link events for MII/GMII cards if MI auto-polling disabled */
37071493e883SOleg Bulyzhin 	} else if (CSR_READ_4(sc, BGE_MI_MODE) & BGE_MIMODE_AUTOPOLL) {
37081f313773SOleg Bulyzhin 		/*
37091f313773SOleg Bulyzhin 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
37101f313773SOleg Bulyzhin 		 * in status word always set. Workaround this bug by reading
37111f313773SOleg Bulyzhin 		 * PHY link status directly.
37121f313773SOleg Bulyzhin 		 */
37131f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
37141f313773SOleg Bulyzhin 
37151f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
37161f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
3717dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3718dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
37191f313773SOleg Bulyzhin 
37201f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
37211f313773SOleg Bulyzhin 			if (!sc->bge_link &&
37221f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
37231f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
37241f313773SOleg Bulyzhin 				sc->bge_link++;
37251f313773SOleg Bulyzhin 				if (bootverbose)
37261f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
37271f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
37281f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
37291f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
37301f313773SOleg Bulyzhin 				sc->bge_link = 0;
37311f313773SOleg Bulyzhin 				if (bootverbose)
37321f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
37331f313773SOleg Bulyzhin 			}
37341f313773SOleg Bulyzhin 		}
3735dab5cd05SOleg Bulyzhin 	}
3736dab5cd05SOleg Bulyzhin 
37373f74909aSGleb Smirnoff 	/* Clear the attention. */
3738dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
3739dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
3740dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
3741dab5cd05SOleg Bulyzhin }
3742