xref: /freebsd/sys/dev/bge/if_bge.c (revision 652ae483f8a289223f283b1d04b39337c856a9fe)
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 
686652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
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 
745652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
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. */
1008652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_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);
1013652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_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 */
1114652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_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);
1198652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_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);
1227652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_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|
1405652ae483SGleb Smirnoff 	    ((sc->bge_flags & BGE_FLAG_TBI) ?
1406652ae483SGleb Smirnoff 	    BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
140795d67482SBill Paul 
140895d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
140995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
141095d67482SBill Paul 
141195d67482SBill Paul #ifdef notdef
141295d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
141395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
141495d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
141595d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
141695d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
141795d67482SBill Paul #endif
141895d67482SBill Paul 
141995d67482SBill Paul 	/* Turn on DMA completion state machine */
14204c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
142195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
142295d67482SBill Paul 
142395d67482SBill Paul 	/* Turn on write DMA state machine */
142495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_WDMA_MODE,
142595d67482SBill Paul 	    BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
142695d67482SBill Paul 
142795d67482SBill Paul 	/* Turn on read DMA state machine */
142895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
142995d67482SBill Paul 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
143095d67482SBill Paul 
143195d67482SBill Paul 	/* Turn on RX data completion state machine */
143295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
143395d67482SBill Paul 
143495d67482SBill Paul 	/* Turn on RX BD initiator state machine */
143595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
143695d67482SBill Paul 
143795d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
143895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
143995d67482SBill Paul 
144095d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
14414c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
144295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
144395d67482SBill Paul 
144495d67482SBill Paul 	/* Turn on send BD completion state machine */
144595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
144695d67482SBill Paul 
144795d67482SBill Paul 	/* Turn on send data completion state machine */
144895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
144995d67482SBill Paul 
145095d67482SBill Paul 	/* Turn on send data initiator state machine */
145195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
145295d67482SBill Paul 
145395d67482SBill Paul 	/* Turn on send BD initiator state machine */
145495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
145595d67482SBill Paul 
145695d67482SBill Paul 	/* Turn on send BD selector state machine */
145795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
145895d67482SBill Paul 
145995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
146095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
146195d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
146295d67482SBill Paul 
146395d67482SBill Paul 	/* ack/clear link change events */
146495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
14650434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
14660434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1467f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
146895d67482SBill Paul 
146995d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
1470652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
147195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1472a1d52896SBill Paul 	} else {
147395d67482SBill Paul 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
14741f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
14754c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
1476a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1477a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1478a1d52896SBill Paul 	}
147995d67482SBill Paul 
14801f313773SOleg Bulyzhin 	/*
14811f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
14821f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
14831f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
14841f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
14851f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
14861f313773SOleg Bulyzhin 	 */
14871f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
14881f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
14891f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
14901f313773SOleg Bulyzhin 
149195d67482SBill Paul 	/* Enable link state change attentions. */
149295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
149395d67482SBill Paul 
149495d67482SBill Paul 	return (0);
149595d67482SBill Paul }
149695d67482SBill Paul 
14974c0da0ffSGleb Smirnoff const struct bge_revision *
14984c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
14994c0da0ffSGleb Smirnoff {
15004c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
15014c0da0ffSGleb Smirnoff 
15024c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
15034c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
15044c0da0ffSGleb Smirnoff 			return (br);
15054c0da0ffSGleb Smirnoff 	}
15064c0da0ffSGleb Smirnoff 
15074c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
15084c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
15094c0da0ffSGleb Smirnoff 			return (br);
15104c0da0ffSGleb Smirnoff 	}
15114c0da0ffSGleb Smirnoff 
15124c0da0ffSGleb Smirnoff 	return (NULL);
15134c0da0ffSGleb Smirnoff }
15144c0da0ffSGleb Smirnoff 
15154c0da0ffSGleb Smirnoff const struct bge_vendor *
15164c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
15174c0da0ffSGleb Smirnoff {
15184c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
15194c0da0ffSGleb Smirnoff 
15204c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
15214c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
15224c0da0ffSGleb Smirnoff 			return (v);
15234c0da0ffSGleb Smirnoff 
15244c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
15254c0da0ffSGleb Smirnoff 	return (NULL);
15264c0da0ffSGleb Smirnoff }
15274c0da0ffSGleb Smirnoff 
152895d67482SBill Paul /*
152995d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
15304c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
15314c0da0ffSGleb Smirnoff  *
15324c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
153395d67482SBill Paul  * can get the device name string from the controller itself instead
153495d67482SBill Paul  * of the compiled-in string. This is a little slow, but it guarantees
15354c0da0ffSGleb Smirnoff  * we'll always announce the right product name. Unfortunately, this
15364c0da0ffSGleb Smirnoff  * is possible only later in bge_attach(), when we have established
15374c0da0ffSGleb Smirnoff  * access to EEPROM.
153895d67482SBill Paul  */
153995d67482SBill Paul static int
15403f74909aSGleb Smirnoff bge_probe(device_t dev)
154195d67482SBill Paul {
15424c0da0ffSGleb Smirnoff 	struct bge_type *t = bge_devs;
15434c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
154495d67482SBill Paul 
154595d67482SBill Paul 	bzero(sc, sizeof(struct bge_softc));
154695d67482SBill Paul 	sc->bge_dev = dev;
154795d67482SBill Paul 
15484c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
154995d67482SBill Paul 		if ((pci_get_vendor(dev) == t->bge_vid) &&
155095d67482SBill Paul 		    (pci_get_device(dev) == t->bge_did)) {
15514c0da0ffSGleb Smirnoff 			char buf[64];
15524c0da0ffSGleb Smirnoff 			const struct bge_revision *br;
15534c0da0ffSGleb Smirnoff 			const struct bge_vendor *v;
15544c0da0ffSGleb Smirnoff 			uint32_t id;
15554c0da0ffSGleb Smirnoff 
15564c0da0ffSGleb Smirnoff 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
15574c0da0ffSGleb Smirnoff 			    BGE_PCIMISCCTL_ASICREV;
15584c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
15594c0da0ffSGleb Smirnoff 			id >>= 16;
15604c0da0ffSGleb Smirnoff 			v = bge_lookup_vendor(t->bge_vid);
15614c0da0ffSGleb Smirnoff 			if (br == NULL)
15624c0da0ffSGleb Smirnoff 				snprintf(buf, 64, "%s unknown ASIC (%#04x)",
15634c0da0ffSGleb Smirnoff 				    v->v_name, id);
15644c0da0ffSGleb Smirnoff 			else
15654c0da0ffSGleb Smirnoff 				snprintf(buf, 64, "%s %s, ASIC rev. %#04x",
15664c0da0ffSGleb Smirnoff 				    v->v_name, br->br_name, id);
15674c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
15686d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
1569652ae483SGleb Smirnoff 				sc->bge_flags |= BGE_FLAG_NO3LED;
157095d67482SBill Paul 			return (0);
157195d67482SBill Paul 		}
157295d67482SBill Paul 		t++;
157395d67482SBill Paul 	}
157495d67482SBill Paul 
157595d67482SBill Paul 	return (ENXIO);
157695d67482SBill Paul }
157795d67482SBill Paul 
1578f41ac2beSBill Paul static void
15793f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
1580f41ac2beSBill Paul {
1581f41ac2beSBill Paul 	int i;
1582f41ac2beSBill Paul 
15833f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
1584f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1585f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1586f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1587f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1588f41ac2beSBill Paul 	}
1589f41ac2beSBill Paul 
15903f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
1591f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1592f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1593f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1594f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1595f41ac2beSBill Paul 	}
1596f41ac2beSBill Paul 
15973f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
1598f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1599f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1600f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1601f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1602f41ac2beSBill Paul 	}
1603f41ac2beSBill Paul 
1604f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1605f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1606f41ac2beSBill Paul 
1607f41ac2beSBill Paul 
16083f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
1609e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
1610e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1611e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
1612e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1613f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1614f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1615f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1616f41ac2beSBill Paul 
1617f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1618f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1619f41ac2beSBill Paul 
16203f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
1621e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1622e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1623e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1624e65bed95SPyun YongHyeon 
1625e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1626e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
1627f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1628f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1629f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1630f41ac2beSBill Paul 
1631f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1632f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1633f41ac2beSBill Paul 
16343f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
1635e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
1636e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1637e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
1638e65bed95SPyun YongHyeon 
1639e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1640e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
1641f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1642f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1643f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1644f41ac2beSBill Paul 
1645f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1646f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1647f41ac2beSBill Paul 
16483f74909aSGleb Smirnoff 	/* Destroy TX ring. */
1649e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
1650e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1651e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
1652e65bed95SPyun YongHyeon 
1653e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
1654f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1655f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1656f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1657f41ac2beSBill Paul 
1658f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1659f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1660f41ac2beSBill Paul 
16613f74909aSGleb Smirnoff 	/* Destroy status block. */
1662e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
1663e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1664e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
1665e65bed95SPyun YongHyeon 
1666e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
1667f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1668f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1669f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1670f41ac2beSBill Paul 
1671f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1672f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1673f41ac2beSBill Paul 
16743f74909aSGleb Smirnoff 	/* Destroy statistics block. */
1675e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
1676e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1677e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
1678e65bed95SPyun YongHyeon 
1679e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
1680f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1681f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1682f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1683f41ac2beSBill Paul 
1684f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1685f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1686f41ac2beSBill Paul 
16873f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
1688f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1689f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1690f41ac2beSBill Paul }
1691f41ac2beSBill Paul 
1692f41ac2beSBill Paul static int
16933f74909aSGleb Smirnoff bge_dma_alloc(device_t dev)
1694f41ac2beSBill Paul {
16953f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
1696f41ac2beSBill Paul 	struct bge_softc *sc;
16971be6acb7SGleb Smirnoff 	int i, error;
1698f41ac2beSBill Paul 
1699f41ac2beSBill Paul 	sc = device_get_softc(dev);
1700f41ac2beSBill Paul 
1701f41ac2beSBill Paul 	/*
1702f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1703f41ac2beSBill Paul 	 */
1704f41ac2beSBill Paul 	error = bus_dma_tag_create(NULL,	/* parent */
1705f41ac2beSBill Paul 			PAGE_SIZE, 0,		/* alignment, boundary */
1706f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
17072f28b973SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1708f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1709f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1710f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
17118a40c10eSScott Long 			0,			/* flags */
1712f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1713f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1714f41ac2beSBill Paul 
1715e65bed95SPyun YongHyeon 	if (error != 0) {
1716fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1717fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
1718e65bed95SPyun YongHyeon 		return (ENOMEM);
1719e65bed95SPyun YongHyeon 	}
1720e65bed95SPyun YongHyeon 
1721f41ac2beSBill Paul 	/*
1722f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1723f41ac2beSBill Paul 	 */
17248a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1725f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
17261be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
17271be6acb7SGleb Smirnoff 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
1728f41ac2beSBill Paul 
1729f41ac2beSBill Paul 	if (error) {
1730fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1731f41ac2beSBill Paul 		return (ENOMEM);
1732f41ac2beSBill Paul 	}
1733f41ac2beSBill Paul 
17343f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
1735f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1736f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1737f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1738f41ac2beSBill Paul 		if (error) {
1739fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1740fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1741f41ac2beSBill Paul 			return (ENOMEM);
1742f41ac2beSBill Paul 		}
1743f41ac2beSBill Paul 	}
1744f41ac2beSBill Paul 
17453f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
1746f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1747f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1748f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1749f41ac2beSBill Paul 		if (error) {
1750fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1751fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1752f41ac2beSBill Paul 			return (ENOMEM);
1753f41ac2beSBill Paul 		}
1754f41ac2beSBill Paul 	}
1755f41ac2beSBill Paul 
17563f74909aSGleb Smirnoff 	/* Create tag for standard RX ring. */
1757f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1758f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1759f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1760f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1761f41ac2beSBill Paul 
1762f41ac2beSBill Paul 	if (error) {
1763fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1764f41ac2beSBill Paul 		return (ENOMEM);
1765f41ac2beSBill Paul 	}
1766f41ac2beSBill Paul 
17673f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for standard RX ring. */
1768f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1769f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1770f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1771f41ac2beSBill Paul 	if (error)
1772f41ac2beSBill Paul 		return (ENOMEM);
1773f41ac2beSBill Paul 
1774f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1775f41ac2beSBill Paul 
17763f74909aSGleb Smirnoff 	/* Load the address of the standard RX ring. */
1777f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1778f41ac2beSBill Paul 	ctx.sc = sc;
1779f41ac2beSBill Paul 
1780f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1781f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1782f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1783f41ac2beSBill Paul 
1784f41ac2beSBill Paul 	if (error)
1785f41ac2beSBill Paul 		return (ENOMEM);
1786f41ac2beSBill Paul 
1787f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
1788f41ac2beSBill Paul 
17893f74909aSGleb Smirnoff 	/* Create tags for jumbo mbufs. */
17904c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1791f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
17928a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
17931be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
17941be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
1795f41ac2beSBill Paul 		if (error) {
1796fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
17973f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
1798f41ac2beSBill Paul 			return (ENOMEM);
1799f41ac2beSBill Paul 		}
1800f41ac2beSBill Paul 
18013f74909aSGleb Smirnoff 		/* Create tag for jumbo RX ring. */
1802f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1803f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1804f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
1805f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
1806f41ac2beSBill Paul 
1807f41ac2beSBill Paul 		if (error) {
1808fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
18093f74909aSGleb Smirnoff 			    "could not allocate jumbo ring dma tag\n");
1810f41ac2beSBill Paul 			return (ENOMEM);
1811f41ac2beSBill Paul 		}
1812f41ac2beSBill Paul 
18133f74909aSGleb Smirnoff 		/* Allocate DMA'able memory for jumbo RX ring. */
1814f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
18151be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
18161be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1817f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
1818f41ac2beSBill Paul 		if (error)
1819f41ac2beSBill Paul 			return (ENOMEM);
1820f41ac2beSBill Paul 
18213f74909aSGleb Smirnoff 		/* Load the address of the jumbo RX ring. */
1822f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
1823f41ac2beSBill Paul 		ctx.sc = sc;
1824f41ac2beSBill Paul 
1825f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1826f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1827f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
1828f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1829f41ac2beSBill Paul 
1830f41ac2beSBill Paul 		if (error)
1831f41ac2beSBill Paul 			return (ENOMEM);
1832f41ac2beSBill Paul 
1833f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
1834f41ac2beSBill Paul 
18353f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
1836f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1837f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
1838f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1839f41ac2beSBill Paul 			if (error) {
1840fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
18413f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
1842f41ac2beSBill Paul 				return (ENOMEM);
1843f41ac2beSBill Paul 			}
1844f41ac2beSBill Paul 		}
1845f41ac2beSBill Paul 
1846f41ac2beSBill Paul 	}
1847f41ac2beSBill Paul 
18483f74909aSGleb Smirnoff 	/* Create tag for RX return ring. */
1849f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1850f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1851f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
1852f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
1853f41ac2beSBill Paul 
1854f41ac2beSBill Paul 	if (error) {
1855fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1856f41ac2beSBill Paul 		return (ENOMEM);
1857f41ac2beSBill Paul 	}
1858f41ac2beSBill Paul 
18593f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for RX return ring. */
1860f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
1861f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
1862f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
1863f41ac2beSBill Paul 	if (error)
1864f41ac2beSBill Paul 		return (ENOMEM);
1865f41ac2beSBill Paul 
1866f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
1867f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
1868f41ac2beSBill Paul 
18693f74909aSGleb Smirnoff 	/* Load the address of the RX return ring. */
1870f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1871f41ac2beSBill Paul 	ctx.sc = sc;
1872f41ac2beSBill Paul 
1873f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
1874f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
1875f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
1876f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1877f41ac2beSBill Paul 
1878f41ac2beSBill Paul 	if (error)
1879f41ac2beSBill Paul 		return (ENOMEM);
1880f41ac2beSBill Paul 
1881f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
1882f41ac2beSBill Paul 
18833f74909aSGleb Smirnoff 	/* Create tag for TX ring. */
1884f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1885f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1886f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
1887f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
1888f41ac2beSBill Paul 
1889f41ac2beSBill Paul 	if (error) {
1890fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1891f41ac2beSBill Paul 		return (ENOMEM);
1892f41ac2beSBill Paul 	}
1893f41ac2beSBill Paul 
18943f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for TX ring. */
1895f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
1896f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
1897f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
1898f41ac2beSBill Paul 	if (error)
1899f41ac2beSBill Paul 		return (ENOMEM);
1900f41ac2beSBill Paul 
1901f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1902f41ac2beSBill Paul 
19033f74909aSGleb Smirnoff 	/* Load the address of the TX ring. */
1904f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1905f41ac2beSBill Paul 	ctx.sc = sc;
1906f41ac2beSBill Paul 
1907f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
1908f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
1909f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1910f41ac2beSBill Paul 
1911f41ac2beSBill Paul 	if (error)
1912f41ac2beSBill Paul 		return (ENOMEM);
1913f41ac2beSBill Paul 
1914f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
1915f41ac2beSBill Paul 
19163f74909aSGleb Smirnoff 	/* Create tag for status block. */
1917f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1918f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1919f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
1920f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
1921f41ac2beSBill Paul 
1922f41ac2beSBill Paul 	if (error) {
1923fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1924f41ac2beSBill Paul 		return (ENOMEM);
1925f41ac2beSBill Paul 	}
1926f41ac2beSBill Paul 
19273f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for status block. */
1928f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
1929f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
1930f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
1931f41ac2beSBill Paul 	if (error)
1932f41ac2beSBill Paul 		return (ENOMEM);
1933f41ac2beSBill Paul 
1934f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
1935f41ac2beSBill Paul 
19363f74909aSGleb Smirnoff 	/* Load the address of the status block. */
1937f41ac2beSBill Paul 	ctx.sc = sc;
1938f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1939f41ac2beSBill Paul 
1940f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
1941f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
1942f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1943f41ac2beSBill Paul 
1944f41ac2beSBill Paul 	if (error)
1945f41ac2beSBill Paul 		return (ENOMEM);
1946f41ac2beSBill Paul 
1947f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
1948f41ac2beSBill Paul 
19493f74909aSGleb Smirnoff 	/* Create tag for statistics block. */
1950f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1951f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1952f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
1953f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
1954f41ac2beSBill Paul 
1955f41ac2beSBill Paul 	if (error) {
1956fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1957f41ac2beSBill Paul 		return (ENOMEM);
1958f41ac2beSBill Paul 	}
1959f41ac2beSBill Paul 
19603f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for statistics block. */
1961f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
1962f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
1963f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
1964f41ac2beSBill Paul 	if (error)
1965f41ac2beSBill Paul 		return (ENOMEM);
1966f41ac2beSBill Paul 
1967f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
1968f41ac2beSBill Paul 
19693f74909aSGleb Smirnoff 	/* Load the address of the statstics block. */
1970f41ac2beSBill Paul 	ctx.sc = sc;
1971f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1972f41ac2beSBill Paul 
1973f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
1974f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
1975f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1976f41ac2beSBill Paul 
1977f41ac2beSBill Paul 	if (error)
1978f41ac2beSBill Paul 		return (ENOMEM);
1979f41ac2beSBill Paul 
1980f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
1981f41ac2beSBill Paul 
1982f41ac2beSBill Paul 	return (0);
1983f41ac2beSBill Paul }
1984f41ac2beSBill Paul 
198595d67482SBill Paul static int
19863f74909aSGleb Smirnoff bge_attach(device_t dev)
198795d67482SBill Paul {
198895d67482SBill Paul 	struct ifnet *ifp;
198995d67482SBill Paul 	struct bge_softc *sc;
19903f74909aSGleb Smirnoff 	uint32_t hwcfg = 0;
19913f74909aSGleb Smirnoff 	uint32_t mac_tmp = 0;
1992fc74a9f9SBrooks Davis 	u_char eaddr[6];
1993fe806fdaSPyun YongHyeon 	int error = 0, rid;
199495d67482SBill Paul 
199595d67482SBill Paul 	sc = device_get_softc(dev);
199695d67482SBill Paul 	sc->bge_dev = dev;
199795d67482SBill Paul 
199895d67482SBill Paul 	/*
199995d67482SBill Paul 	 * Map control/status registers.
200095d67482SBill Paul 	 */
200195d67482SBill Paul 	pci_enable_busmaster(dev);
200295d67482SBill Paul 
200395d67482SBill Paul 	rid = BGE_PCI_BAR0;
20045f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
20055f96beb9SNate Lawson 	    RF_ACTIVE|PCI_RF_DENSE);
200695d67482SBill Paul 
200795d67482SBill Paul 	if (sc->bge_res == NULL) {
2008fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
200995d67482SBill Paul 		error = ENXIO;
201095d67482SBill Paul 		goto fail;
201195d67482SBill Paul 	}
201295d67482SBill Paul 
201395d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
201495d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
201595d67482SBill Paul 
20163f74909aSGleb Smirnoff 	/* Allocate interrupt. */
201795d67482SBill Paul 	rid = 0;
201895d67482SBill Paul 
20195f96beb9SNate Lawson 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
202095d67482SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
202195d67482SBill Paul 
202295d67482SBill Paul 	if (sc->bge_irq == NULL) {
2023fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
202495d67482SBill Paul 		error = ENXIO;
202595d67482SBill Paul 		goto fail;
202695d67482SBill Paul 	}
202795d67482SBill Paul 
20280f9bd73bSSam Leffler 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
20290f9bd73bSSam Leffler 
2030e53d81eeSPaul Saab 	/* Save ASIC rev. */
2031e53d81eeSPaul Saab 
2032e53d81eeSPaul Saab 	sc->bge_chipid =
2033e53d81eeSPaul Saab 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2034e53d81eeSPaul Saab 	    BGE_PCIMISCCTL_ASICREV;
2035e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2036e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2037e53d81eeSPaul Saab 
2038e53d81eeSPaul Saab 	/*
2039e53d81eeSPaul Saab 	 * XXX: Broadcom Linux driver.  Not in specs or eratta.
2040e53d81eeSPaul Saab 	 * PCI-Express?
2041e53d81eeSPaul Saab 	 */
20424c0da0ffSGleb Smirnoff 	if (BGE_IS_5705_OR_BEYOND(sc)) {
20433f74909aSGleb Smirnoff 		uint32_t v;
2044e53d81eeSPaul Saab 
2045e53d81eeSPaul Saab 		v = pci_read_config(dev, BGE_PCI_MSI_CAPID, 4);
2046e53d81eeSPaul Saab 		if (((v >> 8) & 0xff) == BGE_PCIE_CAPID_REG) {
2047e53d81eeSPaul Saab 			v = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
2048e53d81eeSPaul Saab 			if ((v & 0xff) == BGE_PCIE_CAPID)
2049652ae483SGleb Smirnoff 				sc->bge_flags |= BGE_FLAG_PCIE;
2050e53d81eeSPaul Saab 		}
2051e53d81eeSPaul Saab 	}
2052e53d81eeSPaul Saab 
20534c0da0ffSGleb Smirnoff 	/*
20544c0da0ffSGleb Smirnoff 	 * PCI-X ?
20554c0da0ffSGleb Smirnoff 	 */
20564c0da0ffSGleb Smirnoff 	if ((pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
20574c0da0ffSGleb Smirnoff 	    BGE_PCISTATE_PCI_BUSMODE) == 0)
2058652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_PCIX;
20594c0da0ffSGleb Smirnoff 
206095d67482SBill Paul 	/* Try to reset the chip. */
206195d67482SBill Paul 	bge_reset(sc);
206295d67482SBill Paul 
206395d67482SBill Paul 	if (bge_chipinit(sc)) {
2064fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
206595d67482SBill Paul 		bge_release_resources(sc);
206695d67482SBill Paul 		error = ENXIO;
206795d67482SBill Paul 		goto fail;
206895d67482SBill Paul 	}
206995d67482SBill Paul 
207095d67482SBill Paul 	/*
207195d67482SBill Paul 	 * Get station address from the EEPROM.
207295d67482SBill Paul 	 */
2073fc74a9f9SBrooks Davis 	mac_tmp = bge_readmem_ind(sc, 0x0c14);
2074fc74a9f9SBrooks Davis 	if ((mac_tmp >> 16) == 0x484b) {
2075fc74a9f9SBrooks Davis 		eaddr[0] = (u_char)(mac_tmp >> 8);
2076fc74a9f9SBrooks Davis 		eaddr[1] = (u_char)mac_tmp;
2077fc74a9f9SBrooks Davis 		mac_tmp = bge_readmem_ind(sc, 0x0c18);
2078fc74a9f9SBrooks Davis 		eaddr[2] = (u_char)(mac_tmp >> 24);
2079fc74a9f9SBrooks Davis 		eaddr[3] = (u_char)(mac_tmp >> 16);
2080fc74a9f9SBrooks Davis 		eaddr[4] = (u_char)(mac_tmp >> 8);
2081fc74a9f9SBrooks Davis 		eaddr[5] = (u_char)mac_tmp;
2082fc74a9f9SBrooks Davis 	} else if (bge_read_eeprom(sc, eaddr,
208395d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2084fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to read station address\n");
208595d67482SBill Paul 		bge_release_resources(sc);
208695d67482SBill Paul 		error = ENXIO;
208795d67482SBill Paul 		goto fail;
208895d67482SBill Paul 	}
208995d67482SBill Paul 
2090f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
20914c0da0ffSGleb Smirnoff 	if (BGE_IS_5705_OR_BEYOND(sc))
2092f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2093f41ac2beSBill Paul 	else
2094f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2095f41ac2beSBill Paul 
2096f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2097fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2098fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2099f41ac2beSBill Paul 		bge_release_resources(sc);
2100f41ac2beSBill Paul 		error = ENXIO;
2101f41ac2beSBill Paul 		goto fail;
2102f41ac2beSBill Paul 	}
2103f41ac2beSBill Paul 
210495d67482SBill Paul 	/* Set default tuneable values. */
210595d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
210695d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
210795d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
210895d67482SBill Paul 	sc->bge_rx_max_coal_bds = 64;
210995d67482SBill Paul 	sc->bge_tx_max_coal_bds = 128;
211095d67482SBill Paul 
211195d67482SBill Paul 	/* Set up ifnet structure */
2112fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2113fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2114fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2115fc74a9f9SBrooks Davis 		bge_release_resources(sc);
2116fc74a9f9SBrooks Davis 		error = ENXIO;
2117fc74a9f9SBrooks Davis 		goto fail;
2118fc74a9f9SBrooks Davis 	}
211995d67482SBill Paul 	ifp->if_softc = sc;
21209bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
212195d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
212295d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
212395d67482SBill Paul 	ifp->if_start = bge_start;
212495d67482SBill Paul 	ifp->if_watchdog = bge_watchdog;
212595d67482SBill Paul 	ifp->if_init = bge_init;
212695d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
21274d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
21284d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
21294d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
213095d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2131d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
2132479b23b7SGleb Smirnoff 	    IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM;
213395d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
213475719184SGleb Smirnoff #ifdef DEVICE_POLLING
213575719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
213675719184SGleb Smirnoff #endif
213795d67482SBill Paul 
2138a1d52896SBill Paul 	/*
2139d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2140d375e524SGleb Smirnoff 	 * to hardware bugs.
2141d375e524SGleb Smirnoff 	 */
2142d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2143d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2144d375e524SGleb Smirnoff 		ifp->if_capenable &= IFCAP_HWCSUM;
2145d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2146d375e524SGleb Smirnoff 	}
2147d375e524SGleb Smirnoff 
2148d375e524SGleb Smirnoff 	/*
2149a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
215041abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
215141abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
215241abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
215341abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
215441abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
215541abcc1bSPaul Saab 	 * SK-9D41.
2156a1d52896SBill Paul 	 */
215741abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
215841abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
215941abcc1bSPaul Saab 	else {
2160f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2161f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2162fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2163f6789fbaSPyun YongHyeon 			bge_release_resources(sc);
2164f6789fbaSPyun YongHyeon 			error = ENXIO;
2165f6789fbaSPyun YongHyeon 			goto fail;
2166f6789fbaSPyun YongHyeon 		}
216741abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
216841abcc1bSPaul Saab 	}
216941abcc1bSPaul Saab 
217041abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2171652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
2172a1d52896SBill Paul 
217395d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
217495d67482SBill Paul 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
2175652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
217695d67482SBill Paul 
2177652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
217895d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
217995d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
218095d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
218195d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia,
218295d67482SBill Paul 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
218395d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
218495d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2185da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
218695d67482SBill Paul 	} else {
218795d67482SBill Paul 		/*
218895d67482SBill Paul 		 * Do transceiver setup.
218995d67482SBill Paul 		 */
219095d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
219195d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
2192fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
219395d67482SBill Paul 			bge_release_resources(sc);
219495d67482SBill Paul 			error = ENXIO;
219595d67482SBill Paul 			goto fail;
219695d67482SBill Paul 		}
219795d67482SBill Paul 	}
219895d67482SBill Paul 
219995d67482SBill Paul 	/*
2200e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2201e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2202e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2203e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2204e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2205e255b776SJohn Polstra 	 * payloads by copying the received packets.
2206e255b776SJohn Polstra 	 */
2207652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
2208652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
2209652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
2210e255b776SJohn Polstra 
2211e255b776SJohn Polstra 	/*
221295d67482SBill Paul 	 * Call MI attach routine.
221395d67482SBill Paul 	 */
2214fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
22150f9bd73bSSam Leffler 	callout_init(&sc->bge_stat_ch, CALLOUT_MPSAFE);
22160f9bd73bSSam Leffler 
22170f9bd73bSSam Leffler 	/*
22180f9bd73bSSam Leffler 	 * Hookup IRQ last.
22190f9bd73bSSam Leffler 	 */
22200f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
22210f9bd73bSSam Leffler 	   bge_intr, sc, &sc->bge_intrhand);
22220f9bd73bSSam Leffler 
22230f9bd73bSSam Leffler 	if (error) {
2224fc74a9f9SBrooks Davis 		bge_detach(dev);
2225fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
22260f9bd73bSSam Leffler 	}
222795d67482SBill Paul 
222895d67482SBill Paul fail:
222995d67482SBill Paul 	return (error);
223095d67482SBill Paul }
223195d67482SBill Paul 
223295d67482SBill Paul static int
22333f74909aSGleb Smirnoff bge_detach(device_t dev)
223495d67482SBill Paul {
223595d67482SBill Paul 	struct bge_softc *sc;
223695d67482SBill Paul 	struct ifnet *ifp;
223795d67482SBill Paul 
223895d67482SBill Paul 	sc = device_get_softc(dev);
2239fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
224095d67482SBill Paul 
224175719184SGleb Smirnoff #ifdef DEVICE_POLLING
224275719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
224375719184SGleb Smirnoff 		ether_poll_deregister(ifp);
224475719184SGleb Smirnoff #endif
224575719184SGleb Smirnoff 
22460f9bd73bSSam Leffler 	BGE_LOCK(sc);
224795d67482SBill Paul 	bge_stop(sc);
224895d67482SBill Paul 	bge_reset(sc);
22490f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
22500f9bd73bSSam Leffler 
22510f9bd73bSSam Leffler 	ether_ifdetach(ifp);
225295d67482SBill Paul 
2253652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
225495d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
225595d67482SBill Paul 	} else {
225695d67482SBill Paul 		bus_generic_detach(dev);
225795d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
225895d67482SBill Paul 	}
225995d67482SBill Paul 
226095d67482SBill Paul 	bge_release_resources(sc);
226195d67482SBill Paul 
226295d67482SBill Paul 	return (0);
226395d67482SBill Paul }
226495d67482SBill Paul 
226595d67482SBill Paul static void
22663f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
226795d67482SBill Paul {
226895d67482SBill Paul 	device_t dev;
226995d67482SBill Paul 
227095d67482SBill Paul 	dev = sc->bge_dev;
227195d67482SBill Paul 
227295d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
227395d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
227495d67482SBill Paul 
227595d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
227695d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
227795d67482SBill Paul 
227895d67482SBill Paul 	if (sc->bge_intrhand != NULL)
227995d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
228095d67482SBill Paul 
228195d67482SBill Paul 	if (sc->bge_irq != NULL)
228295d67482SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
228395d67482SBill Paul 
228495d67482SBill Paul 	if (sc->bge_res != NULL)
228595d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
228695d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
228795d67482SBill Paul 
2288ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2289ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2290ad61f896SRuslan Ermilov 
2291f41ac2beSBill Paul 	bge_dma_free(sc);
229295d67482SBill Paul 
22930f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
22940f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
229595d67482SBill Paul }
229695d67482SBill Paul 
229795d67482SBill Paul static void
22983f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
229995d67482SBill Paul {
230095d67482SBill Paul 	device_t dev;
23013f74909aSGleb Smirnoff 	uint32_t cachesize, command, pcistate, reset;
230295d67482SBill Paul 	int i, val = 0;
230395d67482SBill Paul 
230495d67482SBill Paul 	dev = sc->bge_dev;
230595d67482SBill Paul 
230695d67482SBill Paul 	/* Save some important PCI state. */
230795d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
230895d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
230995d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
231095d67482SBill Paul 
231195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
231295d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2313e907febfSPyun YongHyeon 	BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
231495d67482SBill Paul 
2315e53d81eeSPaul Saab 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2316e53d81eeSPaul Saab 
2317e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2318652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2319e53d81eeSPaul Saab 		if (CSR_READ_4(sc, 0x7e2c) == 0x60)	/* PCIE 1.0 */
2320e53d81eeSPaul Saab 			CSR_WRITE_4(sc, 0x7e2c, 0x20);
2321e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2322e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
2323e53d81eeSPaul Saab 			CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2324e53d81eeSPaul Saab 			reset |= (1<<29);
2325e53d81eeSPaul Saab 		}
2326e53d81eeSPaul Saab 	}
2327e53d81eeSPaul Saab 
232895d67482SBill Paul 	/* Issue global reset */
2329e53d81eeSPaul Saab 	bge_writereg_ind(sc, BGE_MISC_CFG, reset);
233095d67482SBill Paul 
233195d67482SBill Paul 	DELAY(1000);
233295d67482SBill Paul 
2333e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2334652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2335e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2336e53d81eeSPaul Saab 			uint32_t v;
2337e53d81eeSPaul Saab 
2338e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
2339e53d81eeSPaul Saab 			v = pci_read_config(dev, 0xc4, 4);
2340e53d81eeSPaul Saab 			pci_write_config(dev, 0xc4, v | (1<<15), 4);
2341e53d81eeSPaul Saab 		}
2342e53d81eeSPaul Saab 		/* Set PCIE max payload size and clear error status. */
2343e53d81eeSPaul Saab 		pci_write_config(dev, 0xd8, 0xf5000, 4);
2344e53d81eeSPaul Saab 	}
2345e53d81eeSPaul Saab 
23463f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
234795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
234895d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2349e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
235095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
235195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
235295d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
235395d67482SBill Paul 
2354a7b0c314SPaul Saab 	/* Enable memory arbiter. */
23554c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
23564c0da0ffSGleb Smirnoff 		uint32_t val;
23574c0da0ffSGleb Smirnoff 
23584c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
23594c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
23604c0da0ffSGleb Smirnoff 	} else
2361a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2362a7b0c314SPaul Saab 
236395d67482SBill Paul 	/*
236495d67482SBill Paul 	 * Prevent PXE restart: write a magic number to the
236595d67482SBill Paul 	 * general communications memory at 0xB50.
236695d67482SBill Paul 	 */
236795d67482SBill Paul 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
236895d67482SBill Paul 	/*
236995d67482SBill Paul 	 * Poll the value location we just wrote until
237095d67482SBill Paul 	 * we see the 1's complement of the magic number.
237195d67482SBill Paul 	 * This indicates that the firmware initialization
237295d67482SBill Paul 	 * is complete.
237395d67482SBill Paul 	 */
237495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
237595d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
237695d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
237795d67482SBill Paul 			break;
237895d67482SBill Paul 		DELAY(10);
237995d67482SBill Paul 	}
238095d67482SBill Paul 
238195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2382fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "firmware handshake timed out\n");
238395d67482SBill Paul 		return;
238495d67482SBill Paul 	}
238595d67482SBill Paul 
238695d67482SBill Paul 	/*
238795d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
238895d67482SBill Paul 	 * return to its original pre-reset state. This is a
238995d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
239095d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
239195d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
239295d67482SBill Paul 	 * results.
239395d67482SBill Paul 	 */
239495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
239595d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
239695d67482SBill Paul 			break;
239795d67482SBill Paul 		DELAY(10);
239895d67482SBill Paul 	}
239995d67482SBill Paul 
24003f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
2401e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
240295d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
240395d67482SBill Paul 
240495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
240595d67482SBill Paul 
2406da3003f0SBill Paul 	/*
2407da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
2408da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
2409da3003f0SBill Paul 	 * to 1.2V.
2410da3003f0SBill Paul 	 */
2411652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
2412652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
2413da3003f0SBill Paul 		uint32_t serdescfg;
2414652ae483SGleb Smirnoff 
2415da3003f0SBill Paul 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2416da3003f0SBill Paul 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
2417da3003f0SBill Paul 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2418da3003f0SBill Paul 	}
2419da3003f0SBill Paul 
2420e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2421652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
2422652ae483SGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2423e53d81eeSPaul Saab 		uint32_t v;
2424e53d81eeSPaul Saab 
2425e53d81eeSPaul Saab 		v = CSR_READ_4(sc, 0x7c00);
2426e53d81eeSPaul Saab 		CSR_WRITE_4(sc, 0x7c00, v | (1<<25));
2427e53d81eeSPaul Saab 	}
242895d67482SBill Paul 	DELAY(10000);
242995d67482SBill Paul }
243095d67482SBill Paul 
243195d67482SBill Paul /*
243295d67482SBill Paul  * Frame reception handling. This is called if there's a frame
243395d67482SBill Paul  * on the receive return list.
243495d67482SBill Paul  *
243595d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
24361be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
243795d67482SBill Paul  * 2) the frame is from the standard receive ring
243895d67482SBill Paul  */
243995d67482SBill Paul 
244095d67482SBill Paul static void
24413f74909aSGleb Smirnoff bge_rxeof(struct bge_softc *sc)
244295d67482SBill Paul {
244395d67482SBill Paul 	struct ifnet *ifp;
244495d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
244595d67482SBill Paul 
24460f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
24470f9bd73bSSam Leffler 
24483f74909aSGleb Smirnoff 	/* Nothing to do. */
2449cfcb5025SOleg Bulyzhin 	if (sc->bge_rx_saved_considx ==
2450cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx)
2451cfcb5025SOleg Bulyzhin 		return;
2452cfcb5025SOleg Bulyzhin 
2453fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
245495d67482SBill Paul 
2455f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2456e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
2457f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2458f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
24594c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
2460f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
24614c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTREAD);
2462f41ac2beSBill Paul 
246395d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2464f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
246595d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
24663f74909aSGleb Smirnoff 		uint32_t		rxidx;
246795d67482SBill Paul 		struct mbuf		*m = NULL;
24683f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
246995d67482SBill Paul 		int			have_tag = 0;
247095d67482SBill Paul 
247175719184SGleb Smirnoff #ifdef DEVICE_POLLING
247275719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
247375719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
247475719184SGleb Smirnoff 				break;
247575719184SGleb Smirnoff 			sc->rxcycles--;
247675719184SGleb Smirnoff 		}
247775719184SGleb Smirnoff #endif
247875719184SGleb Smirnoff 
247995d67482SBill Paul 		cur_rx =
2480f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
248195d67482SBill Paul 
248295d67482SBill Paul 		rxidx = cur_rx->bge_idx;
24830434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
248495d67482SBill Paul 
248595d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
248695d67482SBill Paul 			have_tag = 1;
248795d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
248895d67482SBill Paul 		}
248995d67482SBill Paul 
249095d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
249195d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2492f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2493f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2494f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2495f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2496f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
249795d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
249895d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
249995d67482SBill Paul 			jumbocnt++;
250095d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
250195d67482SBill Paul 				ifp->if_ierrors++;
250295d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
250395d67482SBill Paul 				continue;
250495d67482SBill Paul 			}
250595d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
250695d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
250795d67482SBill Paul 				ifp->if_ierrors++;
250895d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
250995d67482SBill Paul 				continue;
251095d67482SBill Paul 			}
251195d67482SBill Paul 		} else {
251295d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2513f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2514f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2515f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2516f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2517f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
251895d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
251995d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
252095d67482SBill Paul 			stdcnt++;
252195d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
252295d67482SBill Paul 				ifp->if_ierrors++;
252395d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
252495d67482SBill Paul 				continue;
252595d67482SBill Paul 			}
252695d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
252795d67482SBill Paul 			    NULL) == ENOBUFS) {
252895d67482SBill Paul 				ifp->if_ierrors++;
252995d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
253095d67482SBill Paul 				continue;
253195d67482SBill Paul 			}
253295d67482SBill Paul 		}
253395d67482SBill Paul 
253495d67482SBill Paul 		ifp->if_ipackets++;
2535e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2536e255b776SJohn Polstra 		/*
2537e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
2538e65bed95SPyun YongHyeon 		 * the payload is aligned.
2539e255b776SJohn Polstra 		 */
2540652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
2541e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2542e255b776SJohn Polstra 			    cur_rx->bge_len);
2543e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2544e255b776SJohn Polstra 		}
2545e255b776SJohn Polstra #endif
2546473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
254795d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
254895d67482SBill Paul 
2549b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
255078178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
255195d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
255295d67482SBill Paul 				if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
255395d67482SBill Paul 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
255478178cd1SGleb Smirnoff 			}
2555d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
2556d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
255795d67482SBill Paul 				m->m_pkthdr.csum_data =
255895d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
2559ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
2560ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
256195d67482SBill Paul 			}
256295d67482SBill Paul 		}
256395d67482SBill Paul 
256495d67482SBill Paul 		/*
2565673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
2566673d9191SSam Leffler 		 * attach that information to the packet.
256795d67482SBill Paul 		 */
2568d147662cSGleb Smirnoff 		if (have_tag) {
2569d147662cSGleb Smirnoff 			VLAN_INPUT_TAG(ifp, m, vlan_tag);
2570d147662cSGleb Smirnoff 			if (m == NULL)
2571d147662cSGleb Smirnoff 				continue;
2572d147662cSGleb Smirnoff 		}
257395d67482SBill Paul 
25740f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
2575673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
25760f9bd73bSSam Leffler 		BGE_LOCK(sc);
257795d67482SBill Paul 	}
257895d67482SBill Paul 
2579e65bed95SPyun YongHyeon 	if (stdcnt > 0)
2580f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2581e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
25824c0da0ffSGleb Smirnoff 
25834c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc) && jumbocnt > 0)
2584f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
25854c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
2586f41ac2beSBill Paul 
258795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
258895d67482SBill Paul 	if (stdcnt)
258995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
259095d67482SBill Paul 	if (jumbocnt)
259195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
259295d67482SBill Paul }
259395d67482SBill Paul 
259495d67482SBill Paul static void
25953f74909aSGleb Smirnoff bge_txeof(struct bge_softc *sc)
259695d67482SBill Paul {
259795d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
259895d67482SBill Paul 	struct ifnet *ifp;
259995d67482SBill Paul 
26000f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
26010f9bd73bSSam Leffler 
26023f74909aSGleb Smirnoff 	/* Nothing to do. */
2603cfcb5025SOleg Bulyzhin 	if (sc->bge_tx_saved_considx ==
2604cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx)
2605cfcb5025SOleg Bulyzhin 		return;
2606cfcb5025SOleg Bulyzhin 
2607fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
260895d67482SBill Paul 
2609e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
2610e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
2611e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
261295d67482SBill Paul 	/*
261395d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
261495d67482SBill Paul 	 * frames that have been sent.
261595d67482SBill Paul 	 */
261695d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
2617f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
26183f74909aSGleb Smirnoff 		uint32_t		idx = 0;
261995d67482SBill Paul 
262095d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
2621f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
262295d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
262395d67482SBill Paul 			ifp->if_opackets++;
262495d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
2625e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2626e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
2627e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
2628f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2629f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
2630e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
2631e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
263295d67482SBill Paul 		}
263395d67482SBill Paul 		sc->bge_txcnt--;
263495d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
263595d67482SBill Paul 		ifp->if_timer = 0;
263695d67482SBill Paul 	}
263795d67482SBill Paul 
263895d67482SBill Paul 	if (cur_tx != NULL)
263913f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
264095d67482SBill Paul }
264195d67482SBill Paul 
264275719184SGleb Smirnoff #ifdef DEVICE_POLLING
264375719184SGleb Smirnoff static void
264475719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
264575719184SGleb Smirnoff {
264675719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
2647366454f2SOleg Bulyzhin 	uint32_t statusword;
264875719184SGleb Smirnoff 
26493f74909aSGleb Smirnoff 	BGE_LOCK(sc);
26503f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
26513f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
26523f74909aSGleb Smirnoff 		return;
26533f74909aSGleb Smirnoff 	}
265475719184SGleb Smirnoff 
2655dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2656e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2657dab5cd05SOleg Bulyzhin 
26583f74909aSGleb Smirnoff 	statusword = atomic_readandclear_32(
26593f74909aSGleb Smirnoff 	    &sc->bge_ldata.bge_status_block->bge_status);
2660dab5cd05SOleg Bulyzhin 
2661dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2662e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2663366454f2SOleg Bulyzhin 
2664366454f2SOleg Bulyzhin 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS cmd */
2665366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
2666366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
2667366454f2SOleg Bulyzhin 
2668366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
2669366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
26704c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
2671652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
2672366454f2SOleg Bulyzhin 			bge_link_upd(sc);
2673366454f2SOleg Bulyzhin 
2674366454f2SOleg Bulyzhin 	sc->rxcycles = count;
2675366454f2SOleg Bulyzhin 	bge_rxeof(sc);
2676366454f2SOleg Bulyzhin 	bge_txeof(sc);
2677366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2678366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
26793f74909aSGleb Smirnoff 
26803f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
268175719184SGleb Smirnoff }
268275719184SGleb Smirnoff #endif /* DEVICE_POLLING */
268375719184SGleb Smirnoff 
268495d67482SBill Paul static void
26853f74909aSGleb Smirnoff bge_intr(void *xsc)
268695d67482SBill Paul {
268795d67482SBill Paul 	struct bge_softc *sc;
268895d67482SBill Paul 	struct ifnet *ifp;
2689dab5cd05SOleg Bulyzhin 	uint32_t statusword;
269095d67482SBill Paul 
269195d67482SBill Paul 	sc = xsc;
2692f41ac2beSBill Paul 
26930f9bd73bSSam Leffler 	BGE_LOCK(sc);
26940f9bd73bSSam Leffler 
2695dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
2696dab5cd05SOleg Bulyzhin 
269775719184SGleb Smirnoff #ifdef DEVICE_POLLING
269875719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
269975719184SGleb Smirnoff 		BGE_UNLOCK(sc);
270075719184SGleb Smirnoff 		return;
270175719184SGleb Smirnoff 	}
270275719184SGleb Smirnoff #endif
270375719184SGleb Smirnoff 
2704f30cbfc6SScott Long 	/*
2705f30cbfc6SScott Long 	 * Do the mandatory PCI flush as well as get the link status.
2706f30cbfc6SScott Long 	 */
2707f30cbfc6SScott Long 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
2708f41ac2beSBill Paul 
270995d67482SBill Paul 	/* Ack interrupt and stop others from occuring. */
271095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
271195d67482SBill Paul 
2712f30cbfc6SScott Long 	/* Make sure the descriptor ring indexes are coherent. */
2713f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2714f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2715f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2716f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2717f30cbfc6SScott Long 
27181f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
27194c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
2720f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
2721dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
272295d67482SBill Paul 
272313f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
27243f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
272595d67482SBill Paul 		bge_rxeof(sc);
272695d67482SBill Paul 
27273f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
272895d67482SBill Paul 		bge_txeof(sc);
272995d67482SBill Paul 	}
273095d67482SBill Paul 
273195d67482SBill Paul 	/* Re-enable interrupts. */
273295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
273395d67482SBill Paul 
273413f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
273513f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
27360f9bd73bSSam Leffler 		bge_start_locked(ifp);
27370f9bd73bSSam Leffler 
27380f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
273995d67482SBill Paul }
274095d67482SBill Paul 
274195d67482SBill Paul static void
27423f74909aSGleb Smirnoff bge_tick_locked(struct bge_softc *sc)
27430f9bd73bSSam Leffler {
274495d67482SBill Paul 	struct mii_data *mii = NULL;
274595d67482SBill Paul 
27460f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
274795d67482SBill Paul 
27484c0da0ffSGleb Smirnoff 	if (BGE_IS_5705_OR_BEYOND(sc))
27490434d1b8SBill Paul 		bge_stats_update_regs(sc);
27500434d1b8SBill Paul 	else
275195d67482SBill Paul 		bge_stats_update(sc);
275295d67482SBill Paul 
2753652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
275495d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
275595d67482SBill Paul 		mii_tick(mii);
27567b97099dSOleg Bulyzhin 	} else {
27577b97099dSOleg Bulyzhin 		/*
27587b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
27597b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
27607b97099dSOleg Bulyzhin 		 * and trigger interrupt.
27617b97099dSOleg Bulyzhin 		 */
27627b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
27633f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
27647b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
27657b97099dSOleg Bulyzhin #endif
27667b97099dSOleg Bulyzhin 		{
27677b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
27687b97099dSOleg Bulyzhin 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
27697b97099dSOleg Bulyzhin 		}
2770dab5cd05SOleg Bulyzhin 	}
277195d67482SBill Paul 
2772dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
277395d67482SBill Paul }
277495d67482SBill Paul 
277595d67482SBill Paul static void
27763f74909aSGleb Smirnoff bge_tick(void *xsc)
27770f9bd73bSSam Leffler {
27780f9bd73bSSam Leffler 	struct bge_softc *sc;
27790f9bd73bSSam Leffler 
27800f9bd73bSSam Leffler 	sc = xsc;
27810f9bd73bSSam Leffler 
27820f9bd73bSSam Leffler 	BGE_LOCK(sc);
27830f9bd73bSSam Leffler 	bge_tick_locked(sc);
27840f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
27850f9bd73bSSam Leffler }
27860f9bd73bSSam Leffler 
27870f9bd73bSSam Leffler static void
27883f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
27890434d1b8SBill Paul {
27900434d1b8SBill Paul 	struct bge_mac_stats_regs stats;
27913f74909aSGleb Smirnoff 	struct ifnet *ifp;
27923f74909aSGleb Smirnoff 	uint32_t *s;
27936fb34dd2SOleg Bulyzhin 	u_long cnt;	/* current register value */
27940434d1b8SBill Paul 	int i;
27950434d1b8SBill Paul 
2796fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
27970434d1b8SBill Paul 
27983f74909aSGleb Smirnoff 	s = (uint32_t *)&stats;
27990434d1b8SBill Paul 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
28000434d1b8SBill Paul 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
28010434d1b8SBill Paul 		s++;
28020434d1b8SBill Paul 	}
28030434d1b8SBill Paul 
28046fb34dd2SOleg Bulyzhin 	cnt = stats.dot3StatsSingleCollisionFrames +
28050434d1b8SBill Paul 	    stats.dot3StatsMultipleCollisionFrames +
28060434d1b8SBill Paul 	    stats.dot3StatsExcessiveCollisions +
28076fb34dd2SOleg Bulyzhin 	    stats.dot3StatsLateCollisions;
28086fb34dd2SOleg Bulyzhin 	ifp->if_collisions += cnt >= sc->bge_tx_collisions ?
28096fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_collisions : cnt;
28106fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
28110434d1b8SBill Paul }
28120434d1b8SBill Paul 
28130434d1b8SBill Paul static void
28143f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
281595d67482SBill Paul {
281695d67482SBill Paul 	struct ifnet *ifp;
2817e907febfSPyun YongHyeon 	bus_size_t stats;
28186fb34dd2SOleg Bulyzhin 	u_long cnt;	/* current register value */
281995d67482SBill Paul 
2820fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
282195d67482SBill Paul 
2822e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
2823e907febfSPyun YongHyeon 
2824e907febfSPyun YongHyeon #define READ_STAT(sc, stats, stat) \
2825e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
282695d67482SBill Paul 
28276fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats,
28286fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsSingleCollisionFrames.bge_addr_lo);
28296fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
28306fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsMultipleCollisionFrames.bge_addr_lo);
28316fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
28326fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsExcessiveCollisions.bge_addr_lo);
28336fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
28346fb34dd2SOleg Bulyzhin 		txstats.dot3StatsLateCollisions.bge_addr_lo);
28356fb34dd2SOleg Bulyzhin 	ifp->if_collisions += cnt >= sc->bge_tx_collisions ?
28366fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_collisions : cnt;
28376fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
28386fb34dd2SOleg Bulyzhin 
28396fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
28406fb34dd2SOleg Bulyzhin 	ifp->if_ierrors += cnt >= sc->bge_rx_discards ?
28416fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_rx_discards : cnt;
28426fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
28436fb34dd2SOleg Bulyzhin 
28446fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
28456fb34dd2SOleg Bulyzhin 	ifp->if_oerrors += cnt >= sc->bge_tx_discards ?
28466fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_discards : cnt;
28476fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
284895d67482SBill Paul 
2849e907febfSPyun YongHyeon #undef READ_STAT
285095d67482SBill Paul }
285195d67482SBill Paul 
285295d67482SBill Paul /*
2853d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
2854d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
2855d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
2856d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
2857d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
2858d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
2859d375e524SGleb Smirnoff  */
2860d375e524SGleb Smirnoff static __inline int
2861d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
2862d375e524SGleb Smirnoff {
2863d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
2864d375e524SGleb Smirnoff 	struct mbuf *last;
2865d375e524SGleb Smirnoff 
2866d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
2867d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
2868d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
2869d375e524SGleb Smirnoff 		last = m;
2870d375e524SGleb Smirnoff 	} else {
2871d375e524SGleb Smirnoff 		/*
2872d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
2873d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
2874d375e524SGleb Smirnoff 		 */
2875d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
2876d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
2877d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
2878d375e524SGleb Smirnoff 			struct mbuf *n;
2879d375e524SGleb Smirnoff 
2880d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
2881d375e524SGleb Smirnoff 			if (n == NULL)
2882d375e524SGleb Smirnoff 				return (ENOBUFS);
2883d375e524SGleb Smirnoff 			n->m_len = 0;
2884d375e524SGleb Smirnoff 			last->m_next = n;
2885d375e524SGleb Smirnoff 			last = n;
2886d375e524SGleb Smirnoff 		}
2887d375e524SGleb Smirnoff 	}
2888d375e524SGleb Smirnoff 
2889d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
2890d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
2891d375e524SGleb Smirnoff 	last->m_len += padlen;
2892d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
2893d375e524SGleb Smirnoff 
2894d375e524SGleb Smirnoff 	return (0);
2895d375e524SGleb Smirnoff }
2896d375e524SGleb Smirnoff 
2897d375e524SGleb Smirnoff /*
289895d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
289995d67482SBill Paul  * pointers to descriptors.
290095d67482SBill Paul  */
290195d67482SBill Paul static int
2902676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
290395d67482SBill Paul {
29047e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
2905f41ac2beSBill Paul 	bus_dmamap_t		map;
2906676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
2907676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
29087e27542aSGleb Smirnoff 	struct m_tag		*mtag;
29097e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
2910676ad2c9SGleb Smirnoff 	uint16_t		csum_flags;
29117e27542aSGleb Smirnoff 	int			nsegs, i, error;
291295d67482SBill Paul 
29136909dc43SGleb Smirnoff 	csum_flags = 0;
29146909dc43SGleb Smirnoff 	if (m->m_pkthdr.csum_flags) {
29156909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
29166909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
29176909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
29186909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
29196909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
29206909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
29216909dc43SGleb Smirnoff 				m_freem(m);
29226909dc43SGleb Smirnoff 				*m_head = NULL;
29236909dc43SGleb Smirnoff 				return (error);
29246909dc43SGleb Smirnoff 			}
29256909dc43SGleb Smirnoff 		}
29266909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
29276909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
29286909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
29296909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
29306909dc43SGleb Smirnoff 	}
29316909dc43SGleb Smirnoff 
29327e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
2933676ad2c9SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m, segs,
2934676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
29357e27542aSGleb Smirnoff 	if (error == EFBIG) {
2936676ad2c9SGleb Smirnoff 		m = m_defrag(m, M_DONTWAIT);
2937676ad2c9SGleb Smirnoff 		if (m == NULL) {
2938676ad2c9SGleb Smirnoff 			m_freem(*m_head);
2939676ad2c9SGleb Smirnoff 			*m_head = NULL;
29407e27542aSGleb Smirnoff 			return (ENOBUFS);
29417e27542aSGleb Smirnoff 		}
2942676ad2c9SGleb Smirnoff 		*m_head = m;
2943676ad2c9SGleb Smirnoff 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m,
2944676ad2c9SGleb Smirnoff 		    segs, &nsegs, BUS_DMA_NOWAIT);
2945676ad2c9SGleb Smirnoff 		if (error) {
2946676ad2c9SGleb Smirnoff 			m_freem(m);
2947676ad2c9SGleb Smirnoff 			*m_head = NULL;
29487e27542aSGleb Smirnoff 			return (error);
29497e27542aSGleb Smirnoff 		}
2950676ad2c9SGleb Smirnoff 	} else if (error != 0)
2951676ad2c9SGleb Smirnoff 		return (error);
29527e27542aSGleb Smirnoff 
295395d67482SBill Paul 	/*
295495d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
295595d67482SBill Paul 	 * of the end of the ring.
295695d67482SBill Paul 	 */
29577e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
29587e27542aSGleb Smirnoff 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
295995d67482SBill Paul 		return (ENOBUFS);
29607e27542aSGleb Smirnoff 	}
29617e27542aSGleb Smirnoff 
2962e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
2963e65bed95SPyun YongHyeon 
29647e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
29657e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
29667e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
29677e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
29687e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
29697e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
29707e27542aSGleb Smirnoff 		if (i == nsegs - 1)
29717e27542aSGleb Smirnoff 			break;
29727e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
29737e27542aSGleb Smirnoff 	}
29747e27542aSGleb Smirnoff 
29757e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
29767e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
2977676ad2c9SGleb Smirnoff 
29787e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
29797e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
2980676ad2c9SGleb Smirnoff 	if ((mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m)) != NULL) {
29817e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
29827e27542aSGleb Smirnoff 		d->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
29837e27542aSGleb Smirnoff 	} else
29847e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
2985f41ac2beSBill Paul 
2986f41ac2beSBill Paul 	/*
2987f41ac2beSBill Paul 	 * Insure that the map for this transmission
2988f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
2989f41ac2beSBill Paul 	 * in this chain.
2990f41ac2beSBill Paul 	 */
29917e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
29927e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
2993676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
29947e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
299595d67482SBill Paul 
29967e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
29977e27542aSGleb Smirnoff 	*txidx = idx;
299895d67482SBill Paul 
299995d67482SBill Paul 	return (0);
300095d67482SBill Paul }
300195d67482SBill Paul 
300295d67482SBill Paul /*
300395d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
300495d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
300595d67482SBill Paul  */
300695d67482SBill Paul static void
30073f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
300895d67482SBill Paul {
300995d67482SBill Paul 	struct bge_softc *sc;
301095d67482SBill Paul 	struct mbuf *m_head = NULL;
301114bbd30fSGleb Smirnoff 	uint32_t prodidx;
3012303a718cSDag-Erling Smørgrav 	int count = 0;
301395d67482SBill Paul 
301495d67482SBill Paul 	sc = ifp->if_softc;
301595d67482SBill Paul 
3016dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
301795d67482SBill Paul 		return;
301895d67482SBill Paul 
301914bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
302095d67482SBill Paul 
302195d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
30224d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
302395d67482SBill Paul 		if (m_head == NULL)
302495d67482SBill Paul 			break;
302595d67482SBill Paul 
302695d67482SBill Paul 		/*
302795d67482SBill Paul 		 * XXX
3028b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
3029b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3030b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
3031b874fdd4SYaroslav Tykhiy 		 *
3032b874fdd4SYaroslav Tykhiy 		 * XXX
303395d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
303495d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
303595d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
303695d67482SBill Paul 		 * chain at once.
303795d67482SBill Paul 		 * (paranoia -- may not actually be needed)
303895d67482SBill Paul 		 */
303995d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
304095d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
304195d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
304295d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
30434d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
304413f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
304595d67482SBill Paul 				break;
304695d67482SBill Paul 			}
304795d67482SBill Paul 		}
304895d67482SBill Paul 
304995d67482SBill Paul 		/*
305095d67482SBill Paul 		 * Pack the data into the transmit ring. If we
305195d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
305295d67482SBill Paul 		 * for the NIC to drain the ring.
305395d67482SBill Paul 		 */
3054676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
3055676ad2c9SGleb Smirnoff 			if (m_head == NULL)
3056676ad2c9SGleb Smirnoff 				break;
30574d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
305813f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
305995d67482SBill Paul 			break;
306095d67482SBill Paul 		}
3061303a718cSDag-Erling Smørgrav 		++count;
306295d67482SBill Paul 
306395d67482SBill Paul 		/*
306495d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
306595d67482SBill Paul 		 * to him.
306695d67482SBill Paul 		 */
3067673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
306895d67482SBill Paul 	}
306995d67482SBill Paul 
30703f74909aSGleb Smirnoff 	if (count == 0)
30713f74909aSGleb Smirnoff 		/* No packets were dequeued. */
3072303a718cSDag-Erling Smørgrav 		return;
3073303a718cSDag-Erling Smørgrav 
30743f74909aSGleb Smirnoff 	/* Transmit. */
307595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
30763927098fSPaul Saab 	/* 5700 b2 errata */
3077e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
30783927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
307995d67482SBill Paul 
308014bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
308114bbd30fSGleb Smirnoff 
308295d67482SBill Paul 	/*
308395d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
308495d67482SBill Paul 	 */
308595d67482SBill Paul 	ifp->if_timer = 5;
308695d67482SBill Paul }
308795d67482SBill Paul 
30880f9bd73bSSam Leffler /*
30890f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
30900f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
30910f9bd73bSSam Leffler  */
309295d67482SBill Paul static void
30933f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
309495d67482SBill Paul {
30950f9bd73bSSam Leffler 	struct bge_softc *sc;
30960f9bd73bSSam Leffler 
30970f9bd73bSSam Leffler 	sc = ifp->if_softc;
30980f9bd73bSSam Leffler 	BGE_LOCK(sc);
30990f9bd73bSSam Leffler 	bge_start_locked(ifp);
31000f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
31010f9bd73bSSam Leffler }
31020f9bd73bSSam Leffler 
31030f9bd73bSSam Leffler static void
31043f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
31050f9bd73bSSam Leffler {
310695d67482SBill Paul 	struct ifnet *ifp;
31073f74909aSGleb Smirnoff 	uint16_t *m;
310895d67482SBill Paul 
31090f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
311095d67482SBill Paul 
3111fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
311295d67482SBill Paul 
311313f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
311495d67482SBill Paul 		return;
311595d67482SBill Paul 
311695d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
311795d67482SBill Paul 	bge_stop(sc);
311895d67482SBill Paul 	bge_reset(sc);
311995d67482SBill Paul 	bge_chipinit(sc);
312095d67482SBill Paul 
312195d67482SBill Paul 	/*
312295d67482SBill Paul 	 * Init the various state machines, ring
312395d67482SBill Paul 	 * control blocks and firmware.
312495d67482SBill Paul 	 */
312595d67482SBill Paul 	if (bge_blockinit(sc)) {
3126fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
312795d67482SBill Paul 		return;
312895d67482SBill Paul 	}
312995d67482SBill Paul 
3130fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
313195d67482SBill Paul 
313295d67482SBill Paul 	/* Specify MTU. */
313395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3134859c6c7dSBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
313595d67482SBill Paul 
313695d67482SBill Paul 	/* Load our MAC address. */
31373f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
313895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
313995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
314095d67482SBill Paul 
314195d67482SBill Paul 	/* Enable or disable promiscuous mode as needed. */
314295d67482SBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
314395d67482SBill Paul 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
314495d67482SBill Paul 	} else {
314595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
314695d67482SBill Paul 	}
314795d67482SBill Paul 
314895d67482SBill Paul 	/* Program multicast filter. */
314995d67482SBill Paul 	bge_setmulti(sc);
315095d67482SBill Paul 
315195d67482SBill Paul 	/* Init RX ring. */
315295d67482SBill Paul 	bge_init_rx_ring_std(sc);
315395d67482SBill Paul 
31540434d1b8SBill Paul 	/*
31550434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
31560434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
31570434d1b8SBill Paul 	 * entry of the ring.
31580434d1b8SBill Paul 	 */
31590434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
31603f74909aSGleb Smirnoff 		uint32_t		v, i;
31610434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
31620434d1b8SBill Paul 			DELAY(20);
31630434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
31640434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
31650434d1b8SBill Paul 				break;
31660434d1b8SBill Paul 		}
31670434d1b8SBill Paul 		if (i == 10)
3168fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
3169fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
31700434d1b8SBill Paul 	}
31710434d1b8SBill Paul 
317295d67482SBill Paul 	/* Init jumbo RX ring. */
317395d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
317495d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
317595d67482SBill Paul 
31763f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
317795d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
317895d67482SBill Paul 
317995d67482SBill Paul 	/* Init TX ring. */
318095d67482SBill Paul 	bge_init_tx_ring(sc);
318195d67482SBill Paul 
31823f74909aSGleb Smirnoff 	/* Turn on transmitter. */
318395d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
318495d67482SBill Paul 
31853f74909aSGleb Smirnoff 	/* Turn on receiver. */
318695d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
318795d67482SBill Paul 
318895d67482SBill Paul 	/* Tell firmware we're alive. */
318995d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
319095d67482SBill Paul 
319175719184SGleb Smirnoff #ifdef DEVICE_POLLING
319275719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
319375719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
319475719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
319575719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
319675719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
319775719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
319875719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
319975719184SGleb Smirnoff 	} else
320075719184SGleb Smirnoff #endif
320175719184SGleb Smirnoff 
320295d67482SBill Paul 	/* Enable host interrupts. */
320375719184SGleb Smirnoff 	{
320495d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
320595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
320695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
320775719184SGleb Smirnoff 	}
320895d67482SBill Paul 
320995d67482SBill Paul 	bge_ifmedia_upd(ifp);
321095d67482SBill Paul 
321113f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
321213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
321395d67482SBill Paul 
32140f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
32150f9bd73bSSam Leffler }
32160f9bd73bSSam Leffler 
32170f9bd73bSSam Leffler static void
32183f74909aSGleb Smirnoff bge_init(void *xsc)
32190f9bd73bSSam Leffler {
32200f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
32210f9bd73bSSam Leffler 
32220f9bd73bSSam Leffler 	BGE_LOCK(sc);
32230f9bd73bSSam Leffler 	bge_init_locked(sc);
32240f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
322595d67482SBill Paul }
322695d67482SBill Paul 
322795d67482SBill Paul /*
322895d67482SBill Paul  * Set media options.
322995d67482SBill Paul  */
323095d67482SBill Paul static int
32313f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
323295d67482SBill Paul {
323395d67482SBill Paul 	struct bge_softc *sc;
323495d67482SBill Paul 	struct mii_data *mii;
323595d67482SBill Paul 	struct ifmedia *ifm;
323695d67482SBill Paul 
323795d67482SBill Paul 	sc = ifp->if_softc;
323895d67482SBill Paul 	ifm = &sc->bge_ifmedia;
323995d67482SBill Paul 
324095d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
3241652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
324295d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
324395d67482SBill Paul 			return (EINVAL);
324495d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
324595d67482SBill Paul 		case IFM_AUTO:
3246ff50922bSDoug White 			/*
3247ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
3248ff50922bSDoug White 			 * mechanism for programming the autoneg
3249ff50922bSDoug White 			 * advertisement registers in TBI mode.
3250ff50922bSDoug White 			 */
3251c4529f41SMichael Reifenberger 			if (bge_fake_autoneg == 0 &&
3252c4529f41SMichael Reifenberger 			    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3253ff50922bSDoug White 				uint32_t sgdig;
3254ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3255ff50922bSDoug White 				sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3256ff50922bSDoug White 				sgdig |= BGE_SGDIGCFG_AUTO|
3257ff50922bSDoug White 				    BGE_SGDIGCFG_PAUSE_CAP|
3258ff50922bSDoug White 				    BGE_SGDIGCFG_ASYM_PAUSE;
3259ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3260ff50922bSDoug White 				    sgdig|BGE_SGDIGCFG_SEND);
3261ff50922bSDoug White 				DELAY(5);
3262ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3263ff50922bSDoug White 			}
326495d67482SBill Paul 			break;
326595d67482SBill Paul 		case IFM_1000_SX:
326695d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
326795d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
326895d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
326995d67482SBill Paul 			} else {
327095d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
327195d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
327295d67482SBill Paul 			}
327395d67482SBill Paul 			break;
327495d67482SBill Paul 		default:
327595d67482SBill Paul 			return (EINVAL);
327695d67482SBill Paul 		}
327795d67482SBill Paul 		return (0);
327895d67482SBill Paul 	}
327995d67482SBill Paul 
32801493e883SOleg Bulyzhin 	sc->bge_link_evt++;
328195d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
328295d67482SBill Paul 	if (mii->mii_instance) {
328395d67482SBill Paul 		struct mii_softc *miisc;
328495d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
328595d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
328695d67482SBill Paul 			mii_phy_reset(miisc);
328795d67482SBill Paul 	}
328895d67482SBill Paul 	mii_mediachg(mii);
328995d67482SBill Paul 
329095d67482SBill Paul 	return (0);
329195d67482SBill Paul }
329295d67482SBill Paul 
329395d67482SBill Paul /*
329495d67482SBill Paul  * Report current media status.
329595d67482SBill Paul  */
329695d67482SBill Paul static void
32973f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
329895d67482SBill Paul {
329995d67482SBill Paul 	struct bge_softc *sc;
330095d67482SBill Paul 	struct mii_data *mii;
330195d67482SBill Paul 
330295d67482SBill Paul 	sc = ifp->if_softc;
330395d67482SBill Paul 
3304652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
330595d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
330695d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
330795d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
330895d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
330995d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
33104c0da0ffSGleb Smirnoff 		else {
33114c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
33124c0da0ffSGleb Smirnoff 			return;
33134c0da0ffSGleb Smirnoff 		}
331495d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
331595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
331695d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
331795d67482SBill Paul 		else
331895d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
331995d67482SBill Paul 		return;
332095d67482SBill Paul 	}
332195d67482SBill Paul 
332295d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
332395d67482SBill Paul 	mii_pollstat(mii);
332495d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
332595d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
332695d67482SBill Paul }
332795d67482SBill Paul 
332895d67482SBill Paul static int
33293f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
333095d67482SBill Paul {
333195d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
333295d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
333395d67482SBill Paul 	struct mii_data *mii;
33343f74909aSGleb Smirnoff 	int mask, error = 0;
333595d67482SBill Paul 
333695d67482SBill Paul 	switch (command) {
333795d67482SBill Paul 	case SIOCSIFMTU:
33384c0da0ffSGleb Smirnoff 		if (ifr->ifr_mtu < ETHERMIN ||
33394c0da0ffSGleb Smirnoff 		    ((BGE_IS_JUMBO_CAPABLE(sc)) &&
33404c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > BGE_JUMBO_MTU) ||
33414c0da0ffSGleb Smirnoff 		    ((!BGE_IS_JUMBO_CAPABLE(sc)) &&
33424c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > ETHERMTU))
334395d67482SBill Paul 			error = EINVAL;
33444c0da0ffSGleb Smirnoff 		else if (ifp->if_mtu != ifr->ifr_mtu) {
334595d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
334613f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
334795d67482SBill Paul 			bge_init(sc);
334895d67482SBill Paul 		}
334995d67482SBill Paul 		break;
335095d67482SBill Paul 	case SIOCSIFFLAGS:
33510f9bd73bSSam Leffler 		BGE_LOCK(sc);
335295d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
335395d67482SBill Paul 			/*
335495d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
335595d67482SBill Paul 			 * then just use the 'set promisc mode' command
335695d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
335795d67482SBill Paul 			 * a full re-init means reloading the firmware and
335895d67482SBill Paul 			 * waiting for it to start up, which may take a
3359d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
336095d67482SBill Paul 			 */
336113f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
336295d67482SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
336395d67482SBill Paul 			    !(sc->bge_if_flags & IFF_PROMISC)) {
336495d67482SBill Paul 				BGE_SETBIT(sc, BGE_RX_MODE,
336595d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
336613f4c340SRobert Watson 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
336795d67482SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
336895d67482SBill Paul 			    sc->bge_if_flags & IFF_PROMISC) {
336995d67482SBill Paul 				BGE_CLRBIT(sc, BGE_RX_MODE,
337095d67482SBill Paul 				    BGE_RXMODE_RX_PROMISC);
3371d183af7fSRuslan Ermilov 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3372d183af7fSRuslan Ermilov 			    (ifp->if_flags ^ sc->bge_if_flags) & IFF_ALLMULTI) {
3373d183af7fSRuslan Ermilov 				bge_setmulti(sc);
337495d67482SBill Paul 			} else
33750f9bd73bSSam Leffler 				bge_init_locked(sc);
337695d67482SBill Paul 		} else {
337713f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
337895d67482SBill Paul 				bge_stop(sc);
337995d67482SBill Paul 			}
338095d67482SBill Paul 		}
338195d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
33820f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
338395d67482SBill Paul 		error = 0;
338495d67482SBill Paul 		break;
338595d67482SBill Paul 	case SIOCADDMULTI:
338695d67482SBill Paul 	case SIOCDELMULTI:
338713f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
33880f9bd73bSSam Leffler 			BGE_LOCK(sc);
338995d67482SBill Paul 			bge_setmulti(sc);
33900f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
339195d67482SBill Paul 			error = 0;
339295d67482SBill Paul 		}
339395d67482SBill Paul 		break;
339495d67482SBill Paul 	case SIOCSIFMEDIA:
339595d67482SBill Paul 	case SIOCGIFMEDIA:
3396652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
339795d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
339895d67482SBill Paul 			    &sc->bge_ifmedia, command);
339995d67482SBill Paul 		} else {
340095d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
340195d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
340295d67482SBill Paul 			    &mii->mii_media, command);
340395d67482SBill Paul 		}
340495d67482SBill Paul 		break;
340595d67482SBill Paul 	case SIOCSIFCAP:
340695d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
340775719184SGleb Smirnoff #ifdef DEVICE_POLLING
340875719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
340975719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
341075719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
341175719184SGleb Smirnoff 				if (error)
341275719184SGleb Smirnoff 					return (error);
341375719184SGleb Smirnoff 				BGE_LOCK(sc);
341475719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
341575719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
341675719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
341775719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
341875719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
341975719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
342075719184SGleb Smirnoff 				BGE_UNLOCK(sc);
342175719184SGleb Smirnoff 			} else {
342275719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
342375719184SGleb Smirnoff 				/* Enable interrupt even in error case */
342475719184SGleb Smirnoff 				BGE_LOCK(sc);
342575719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
342675719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
342775719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
342875719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
342975719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
343075719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
343175719184SGleb Smirnoff 				BGE_UNLOCK(sc);
343275719184SGleb Smirnoff 			}
343375719184SGleb Smirnoff 		}
343475719184SGleb Smirnoff #endif
3435d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
3436d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
3437d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
3438d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
3439b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
344095d67482SBill Paul 			else
3441b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
3442479b23b7SGleb Smirnoff 			VLAN_CAPABILITIES(ifp);
344395d67482SBill Paul 		}
344495d67482SBill Paul 		break;
344595d67482SBill Paul 	default:
3446673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
344795d67482SBill Paul 		break;
344895d67482SBill Paul 	}
344995d67482SBill Paul 
345095d67482SBill Paul 	return (error);
345195d67482SBill Paul }
345295d67482SBill Paul 
345395d67482SBill Paul static void
34543f74909aSGleb Smirnoff bge_watchdog(struct ifnet *ifp)
345595d67482SBill Paul {
345695d67482SBill Paul 	struct bge_softc *sc;
345795d67482SBill Paul 
345895d67482SBill Paul 	sc = ifp->if_softc;
345995d67482SBill Paul 
3460fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
346195d67482SBill Paul 
346213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
346395d67482SBill Paul 	bge_init(sc);
346495d67482SBill Paul 
346595d67482SBill Paul 	ifp->if_oerrors++;
346695d67482SBill Paul }
346795d67482SBill Paul 
346895d67482SBill Paul /*
346995d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
347095d67482SBill Paul  * RX and TX lists.
347195d67482SBill Paul  */
347295d67482SBill Paul static void
34733f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
347495d67482SBill Paul {
347595d67482SBill Paul 	struct ifnet *ifp;
347695d67482SBill Paul 	struct ifmedia_entry *ifm;
347795d67482SBill Paul 	struct mii_data *mii = NULL;
347895d67482SBill Paul 	int mtmp, itmp;
347995d67482SBill Paul 
34800f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
34810f9bd73bSSam Leffler 
3482fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
348395d67482SBill Paul 
3484652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0)
348595d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
348695d67482SBill Paul 
34870f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
348895d67482SBill Paul 
348995d67482SBill Paul 	/*
34903f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
349195d67482SBill Paul 	 */
349295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
349395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
349495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
34954c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
349695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
349795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
349895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
349995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
350095d67482SBill Paul 
350195d67482SBill Paul 	/*
35023f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
350395d67482SBill Paul 	 */
350495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
350595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
350695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
350795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
350895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
35094c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
351095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
351195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
351295d67482SBill Paul 
351395d67482SBill Paul 	/*
351495d67482SBill Paul 	 * Shut down all of the memory managers and related
351595d67482SBill Paul 	 * state machines.
351695d67482SBill Paul 	 */
351795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
351895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
35194c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
352095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
352195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
352295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
35234c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc))) {
352495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
352595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
35260434d1b8SBill Paul 	}
352795d67482SBill Paul 
352895d67482SBill Paul 	/* Disable host interrupts. */
352995d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
353095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
353195d67482SBill Paul 
353295d67482SBill Paul 	/*
353395d67482SBill Paul 	 * Tell firmware we're shutting down.
353495d67482SBill Paul 	 */
353595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
353695d67482SBill Paul 
353795d67482SBill Paul 	/* Free the RX lists. */
353895d67482SBill Paul 	bge_free_rx_ring_std(sc);
353995d67482SBill Paul 
354095d67482SBill Paul 	/* Free jumbo RX list. */
35414c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
354295d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
354395d67482SBill Paul 
354495d67482SBill Paul 	/* Free TX buffers. */
354595d67482SBill Paul 	bge_free_tx_ring(sc);
354695d67482SBill Paul 
354795d67482SBill Paul 	/*
354895d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
354995d67482SBill Paul 	 * unchanged so that things will be put back to normal when
355095d67482SBill Paul 	 * we bring the interface back up.
355195d67482SBill Paul 	 */
3552652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
355395d67482SBill Paul 		itmp = ifp->if_flags;
355495d67482SBill Paul 		ifp->if_flags |= IFF_UP;
3555dcc34049SPawel Jakub Dawidek 		/*
3556dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
3557dcc34049SPawel Jakub Dawidek 		 */
3558dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
355995d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
356095d67482SBill Paul 			mtmp = ifm->ifm_media;
356195d67482SBill Paul 			ifm->ifm_media = IFM_ETHER|IFM_NONE;
356295d67482SBill Paul 			mii_mediachg(mii);
356395d67482SBill Paul 			ifm->ifm_media = mtmp;
3564dcc34049SPawel Jakub Dawidek 		}
356595d67482SBill Paul 		ifp->if_flags = itmp;
356695d67482SBill Paul 	}
356795d67482SBill Paul 
356895d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
356995d67482SBill Paul 
35701493e883SOleg Bulyzhin 	/*
35711493e883SOleg Bulyzhin 	 * We can't just call bge_link_upd() cause chip is almost stopped so
35721493e883SOleg Bulyzhin 	 * bge_link_upd -> bge_tick_locked -> bge_stats_update sequence may
35731493e883SOleg Bulyzhin 	 * lead to hardware deadlock. So we just clearing MAC's link state
35741493e883SOleg Bulyzhin 	 * (PHY may still have link UP).
35751493e883SOleg Bulyzhin 	 */
35761493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
35771493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
35781493e883SOleg Bulyzhin 	sc->bge_link = 0;
357995d67482SBill Paul 
35801493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
358195d67482SBill Paul }
358295d67482SBill Paul 
358395d67482SBill Paul /*
358495d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
358595d67482SBill Paul  * get confused by errant DMAs when rebooting.
358695d67482SBill Paul  */
358795d67482SBill Paul static void
35883f74909aSGleb Smirnoff bge_shutdown(device_t dev)
358995d67482SBill Paul {
359095d67482SBill Paul 	struct bge_softc *sc;
359195d67482SBill Paul 
359295d67482SBill Paul 	sc = device_get_softc(dev);
359395d67482SBill Paul 
35940f9bd73bSSam Leffler 	BGE_LOCK(sc);
359595d67482SBill Paul 	bge_stop(sc);
359695d67482SBill Paul 	bge_reset(sc);
35970f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
359895d67482SBill Paul }
359914afefa3SPawel Jakub Dawidek 
360014afefa3SPawel Jakub Dawidek static int
360114afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
360214afefa3SPawel Jakub Dawidek {
360314afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
360414afefa3SPawel Jakub Dawidek 
360514afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
360614afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
360714afefa3SPawel Jakub Dawidek 	bge_stop(sc);
360814afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
360914afefa3SPawel Jakub Dawidek 
361014afefa3SPawel Jakub Dawidek 	return (0);
361114afefa3SPawel Jakub Dawidek }
361214afefa3SPawel Jakub Dawidek 
361314afefa3SPawel Jakub Dawidek static int
361414afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
361514afefa3SPawel Jakub Dawidek {
361614afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
361714afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
361814afefa3SPawel Jakub Dawidek 
361914afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
362014afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
362114afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
362214afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
362314afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
362414afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
362514afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
362614afefa3SPawel Jakub Dawidek 	}
362714afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
362814afefa3SPawel Jakub Dawidek 
362914afefa3SPawel Jakub Dawidek 	return (0);
363014afefa3SPawel Jakub Dawidek }
3631dab5cd05SOleg Bulyzhin 
3632dab5cd05SOleg Bulyzhin static void
36333f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
3634dab5cd05SOleg Bulyzhin {
36351f313773SOleg Bulyzhin 	struct mii_data *mii;
36361f313773SOleg Bulyzhin 	uint32_t link, status;
3637dab5cd05SOleg Bulyzhin 
3638dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
36391f313773SOleg Bulyzhin 
36403f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
36417b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
36427b97099dSOleg Bulyzhin 
3643dab5cd05SOleg Bulyzhin 	/*
3644dab5cd05SOleg Bulyzhin 	 * Process link state changes.
3645dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
3646dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
3647dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
3648dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
3649dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
3650dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
3651dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
3652dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
36531f313773SOleg Bulyzhin 	 *
36541f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
36554c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
3656dab5cd05SOleg Bulyzhin 	 */
3657dab5cd05SOleg Bulyzhin 
36581f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
36594c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
3660dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
3661dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
3662dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3663dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
36641f313773SOleg Bulyzhin 
36651f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
36661f313773SOleg Bulyzhin 			if (!sc->bge_link &&
36671f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
36681f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
36691f313773SOleg Bulyzhin 				sc->bge_link++;
36701f313773SOleg Bulyzhin 				if (bootverbose)
36711f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
36721f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
36731f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
36741f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
36751f313773SOleg Bulyzhin 				sc->bge_link = 0;
36761f313773SOleg Bulyzhin 				if (bootverbose)
36771f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
36781f313773SOleg Bulyzhin 			}
36791f313773SOleg Bulyzhin 
36803f74909aSGleb Smirnoff 			/* Clear the interrupt. */
3681dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
3682dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
3683dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
3684dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
3685dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
3686dab5cd05SOleg Bulyzhin 		}
3687dab5cd05SOleg Bulyzhin 		return;
3688dab5cd05SOleg Bulyzhin 	}
3689dab5cd05SOleg Bulyzhin 
3690652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
36911f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
36927b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
36937b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
36941f313773SOleg Bulyzhin 				sc->bge_link++;
36951f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
36961f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
36971f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
36981f313773SOleg Bulyzhin 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
36991f313773SOleg Bulyzhin 				if (bootverbose)
37001f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
37013f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
37023f74909aSGleb Smirnoff 				    LINK_STATE_UP);
37037b97099dSOleg Bulyzhin 			}
37041f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
3705dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
37061f313773SOleg Bulyzhin 			if (bootverbose)
37071f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
37087b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
37091f313773SOleg Bulyzhin 		}
37101493e883SOleg Bulyzhin 	/* Discard link events for MII/GMII cards if MI auto-polling disabled */
37111493e883SOleg Bulyzhin 	} else if (CSR_READ_4(sc, BGE_MI_MODE) & BGE_MIMODE_AUTOPOLL) {
37121f313773SOleg Bulyzhin 		/*
37131f313773SOleg Bulyzhin 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
37141f313773SOleg Bulyzhin 		 * in status word always set. Workaround this bug by reading
37151f313773SOleg Bulyzhin 		 * PHY link status directly.
37161f313773SOleg Bulyzhin 		 */
37171f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
37181f313773SOleg Bulyzhin 
37191f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
37201f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
3721dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3722dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
37231f313773SOleg Bulyzhin 
37241f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
37251f313773SOleg Bulyzhin 			if (!sc->bge_link &&
37261f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
37271f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
37281f313773SOleg Bulyzhin 				sc->bge_link++;
37291f313773SOleg Bulyzhin 				if (bootverbose)
37301f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
37311f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
37321f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
37331f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
37341f313773SOleg Bulyzhin 				sc->bge_link = 0;
37351f313773SOleg Bulyzhin 				if (bootverbose)
37361f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
37371f313773SOleg Bulyzhin 			}
37381f313773SOleg Bulyzhin 		}
3739dab5cd05SOleg Bulyzhin 	}
3740dab5cd05SOleg Bulyzhin 
37413f74909aSGleb Smirnoff 	/* Clear the attention. */
3742dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
3743dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
3744dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
3745dab5cd05SOleg Bulyzhin }
3746