xref: /freebsd/sys/dev/bge/if_bge.c (revision f9004b6d2dd4fa73ef30d02e23126a19f04cbfe2)
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 
3298cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
3300f9bd73bSSam Leffler static void bge_tick_locked(struct bge_softc *);
331e51a25f8SAlfred Perlstein static void bge_tick(void *);
332e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
3333f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
334676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
33595d67482SBill Paul 
336e51a25f8SAlfred Perlstein static void bge_intr(void *);
3370f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
338e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
339e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
3400f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
341e51a25f8SAlfred Perlstein static void bge_init(void *);
342e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
343e51a25f8SAlfred Perlstein static void bge_watchdog(struct ifnet *);
344e51a25f8SAlfred Perlstein static void bge_shutdown(device_t);
34567d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
346e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
347e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
34895d67482SBill Paul 
3493f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
350e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
35195d67482SBill Paul 
352e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
35395d67482SBill Paul 
354e51a25f8SAlfred Perlstein static int bge_newbuf_std(struct bge_softc *, int, struct mbuf *);
355e51a25f8SAlfred Perlstein static int bge_newbuf_jumbo(struct bge_softc *, int, struct mbuf *);
356e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
357e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
358e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
359e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
360e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
361e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
36295d67482SBill Paul 
363e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
364e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
36595d67482SBill Paul 
3663f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
367e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
36895d67482SBill Paul #ifdef notdef
3693f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
37095d67482SBill Paul #endif
371e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
37295d67482SBill Paul 
373e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
374e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
375e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
37675719184SGleb Smirnoff #ifdef DEVICE_POLLING
3773f74909aSGleb Smirnoff static void bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
37875719184SGleb Smirnoff #endif
37995d67482SBill Paul 
3808cb1383cSDoug Ambrisko #define BGE_RESET_START 1
3818cb1383cSDoug Ambrisko #define BGE_RESET_STOP  2
3828cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
3838cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
3848cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
3858cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
386dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
38795d67482SBill Paul 
38895d67482SBill Paul static device_method_t bge_methods[] = {
38995d67482SBill Paul 	/* Device interface */
39095d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
39195d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
39295d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
39395d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
39414afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
39514afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
39695d67482SBill Paul 
39795d67482SBill Paul 	/* bus interface */
39895d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
39995d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
40095d67482SBill Paul 
40195d67482SBill Paul 	/* MII interface */
40295d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
40395d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
40495d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
40595d67482SBill Paul 
40695d67482SBill Paul 	{ 0, 0 }
40795d67482SBill Paul };
40895d67482SBill Paul 
40995d67482SBill Paul static driver_t bge_driver = {
41095d67482SBill Paul 	"bge",
41195d67482SBill Paul 	bge_methods,
41295d67482SBill Paul 	sizeof(struct bge_softc)
41395d67482SBill Paul };
41495d67482SBill Paul 
41595d67482SBill Paul static devclass_t bge_devclass;
41695d67482SBill Paul 
417f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
41895d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
41995d67482SBill Paul 
420c4529f41SMichael Reifenberger static int bge_fake_autoneg = 0;
421c4529f41SMichael Reifenberger TUNABLE_INT("hw.bge.fake_autoneg", &bge_fake_autoneg);
422c4529f41SMichael Reifenberger 
4233f74909aSGleb Smirnoff static uint32_t
4243f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
42595d67482SBill Paul {
42695d67482SBill Paul 	device_t dev;
42795d67482SBill Paul 
42895d67482SBill Paul 	dev = sc->bge_dev;
42995d67482SBill Paul 
43095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
43195d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4));
43295d67482SBill Paul }
43395d67482SBill Paul 
43495d67482SBill Paul static void
4353f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
43695d67482SBill Paul {
43795d67482SBill Paul 	device_t dev;
43895d67482SBill Paul 
43995d67482SBill Paul 	dev = sc->bge_dev;
44095d67482SBill Paul 
44195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
44295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
44395d67482SBill Paul }
44495d67482SBill Paul 
44595d67482SBill Paul #ifdef notdef
4463f74909aSGleb Smirnoff static uint32_t
4473f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
44895d67482SBill Paul {
44995d67482SBill Paul 	device_t dev;
45095d67482SBill Paul 
45195d67482SBill Paul 	dev = sc->bge_dev;
45295d67482SBill Paul 
45395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
45495d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
45595d67482SBill Paul }
45695d67482SBill Paul #endif
45795d67482SBill Paul 
45895d67482SBill Paul static void
4593f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
46095d67482SBill Paul {
46195d67482SBill Paul 	device_t dev;
46295d67482SBill Paul 
46395d67482SBill Paul 	dev = sc->bge_dev;
46495d67482SBill Paul 
46595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
46695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
46795d67482SBill Paul }
46895d67482SBill Paul 
469f41ac2beSBill Paul /*
470f41ac2beSBill Paul  * Map a single buffer address.
471f41ac2beSBill Paul  */
472f41ac2beSBill Paul 
473f41ac2beSBill Paul static void
4743f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
475f41ac2beSBill Paul {
476f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
477f41ac2beSBill Paul 
478f41ac2beSBill Paul 	if (error)
479f41ac2beSBill Paul 		return;
480f41ac2beSBill Paul 
481f41ac2beSBill Paul 	ctx = arg;
482f41ac2beSBill Paul 
483f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
484f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
485f41ac2beSBill Paul 		return;
486f41ac2beSBill Paul 	}
487f41ac2beSBill Paul 
488f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
489f41ac2beSBill Paul }
490f41ac2beSBill Paul 
49195d67482SBill Paul /*
49295d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
49395d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
49495d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
49595d67482SBill Paul  * access method.
49695d67482SBill Paul  */
4973f74909aSGleb Smirnoff static uint8_t
4983f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
49995d67482SBill Paul {
50095d67482SBill Paul 	int i;
5013f74909aSGleb Smirnoff 	uint32_t byte = 0;
50295d67482SBill Paul 
50395d67482SBill Paul 	/*
50495d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
50595d67482SBill Paul 	 * having to use the bitbang method.
50695d67482SBill Paul 	 */
50795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
50895d67482SBill Paul 
50995d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
51095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
51195d67482SBill Paul 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
51295d67482SBill Paul 	DELAY(20);
51395d67482SBill Paul 
51495d67482SBill Paul 	/* Issue the read EEPROM command. */
51595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
51695d67482SBill Paul 
51795d67482SBill Paul 	/* Wait for completion */
51895d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
51995d67482SBill Paul 		DELAY(10);
52095d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
52195d67482SBill Paul 			break;
52295d67482SBill Paul 	}
52395d67482SBill Paul 
52495d67482SBill Paul 	if (i == BGE_TIMEOUT) {
525fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
526f6789fbaSPyun YongHyeon 		return (1);
52795d67482SBill Paul 	}
52895d67482SBill Paul 
52995d67482SBill Paul 	/* Get result. */
53095d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
53195d67482SBill Paul 
53295d67482SBill Paul 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
53395d67482SBill Paul 
53495d67482SBill Paul 	return (0);
53595d67482SBill Paul }
53695d67482SBill Paul 
53795d67482SBill Paul /*
53895d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
53995d67482SBill Paul  */
54095d67482SBill Paul static int
5413f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
54295d67482SBill Paul {
5433f74909aSGleb Smirnoff 	int i, error = 0;
5443f74909aSGleb Smirnoff 	uint8_t byte = 0;
54595d67482SBill Paul 
54695d67482SBill Paul 	for (i = 0; i < cnt; i++) {
5473f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
5483f74909aSGleb Smirnoff 		if (error)
54995d67482SBill Paul 			break;
55095d67482SBill Paul 		*(dest + i) = byte;
55195d67482SBill Paul 	}
55295d67482SBill Paul 
5533f74909aSGleb Smirnoff 	return (error ? 1 : 0);
55495d67482SBill Paul }
55595d67482SBill Paul 
55695d67482SBill Paul static int
5573f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
55895d67482SBill Paul {
55995d67482SBill Paul 	struct bge_softc *sc;
5603f74909aSGleb Smirnoff 	uint32_t val, autopoll;
56195d67482SBill Paul 	int i;
56295d67482SBill Paul 
56395d67482SBill Paul 	sc = device_get_softc(dev);
56495d67482SBill Paul 
5650434d1b8SBill Paul 	/*
5660434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
5670434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
5680434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
5690434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
5700434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
5710434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
5720434d1b8SBill Paul 	 * special-cased.
5730434d1b8SBill Paul 	 */
574b1265c1aSJohn Polstra 	if (phy != 1)
57598b28ee5SBill Paul 		return (0);
57698b28ee5SBill Paul 
57737ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
57837ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
57937ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
58037ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
58137ceeb4dSPaul Saab 		DELAY(40);
58237ceeb4dSPaul Saab 	}
58337ceeb4dSPaul Saab 
58495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
58595d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
58695d67482SBill Paul 
58795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
58895d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
58995d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
59095d67482SBill Paul 			break;
59195d67482SBill Paul 	}
59295d67482SBill Paul 
59395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
5946b9f5c94SGleb Smirnoff 		device_printf(sc->bge_dev, "PHY read timed out\n");
59537ceeb4dSPaul Saab 		val = 0;
59637ceeb4dSPaul Saab 		goto done;
59795d67482SBill Paul 	}
59895d67482SBill Paul 
59995d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
60095d67482SBill Paul 
60137ceeb4dSPaul Saab done:
60237ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
60337ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
60437ceeb4dSPaul Saab 		DELAY(40);
60537ceeb4dSPaul Saab 	}
60637ceeb4dSPaul Saab 
60795d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
60895d67482SBill Paul 		return (0);
60995d67482SBill Paul 
61095d67482SBill Paul 	return (val & 0xFFFF);
61195d67482SBill Paul }
61295d67482SBill Paul 
61395d67482SBill Paul static int
6143f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
61595d67482SBill Paul {
61695d67482SBill Paul 	struct bge_softc *sc;
6173f74909aSGleb Smirnoff 	uint32_t autopoll;
61895d67482SBill Paul 	int i;
61995d67482SBill Paul 
62095d67482SBill Paul 	sc = device_get_softc(dev);
62195d67482SBill Paul 
62237ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
62337ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
62437ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
62537ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
62637ceeb4dSPaul Saab 		DELAY(40);
62737ceeb4dSPaul Saab 	}
62837ceeb4dSPaul Saab 
62995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
63095d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
63195d67482SBill Paul 
63295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
63395d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
63495d67482SBill Paul 			break;
63595d67482SBill Paul 	}
63695d67482SBill Paul 
63737ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
63837ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
63937ceeb4dSPaul Saab 		DELAY(40);
64037ceeb4dSPaul Saab 	}
64137ceeb4dSPaul Saab 
64295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
6436b9f5c94SGleb Smirnoff 		device_printf(sc->bge_dev, "PHY read timed out\n");
64495d67482SBill Paul 		return (0);
64595d67482SBill Paul 	}
64695d67482SBill Paul 
64795d67482SBill Paul 	return (0);
64895d67482SBill Paul }
64995d67482SBill Paul 
65095d67482SBill Paul static void
6513f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
65295d67482SBill Paul {
65395d67482SBill Paul 	struct bge_softc *sc;
65495d67482SBill Paul 	struct mii_data *mii;
65595d67482SBill Paul 	sc = device_get_softc(dev);
65695d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
65795d67482SBill Paul 
65895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
6593f74909aSGleb Smirnoff 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)
66095d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
6613f74909aSGleb Smirnoff 	else
66295d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
66395d67482SBill Paul 
6643f74909aSGleb Smirnoff 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
66595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
6663f74909aSGleb Smirnoff 	else
66795d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
66895d67482SBill Paul }
66995d67482SBill Paul 
67095d67482SBill Paul /*
67195d67482SBill Paul  * Intialize a standard receive ring descriptor.
67295d67482SBill Paul  */
67395d67482SBill Paul static int
6743f74909aSGleb Smirnoff bge_newbuf_std(struct bge_softc *sc, int i, struct mbuf *m)
67595d67482SBill Paul {
67695d67482SBill Paul 	struct mbuf *m_new = NULL;
67795d67482SBill Paul 	struct bge_rx_bd *r;
678f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
679f41ac2beSBill Paul 	int error;
68095d67482SBill Paul 
68195d67482SBill Paul 	if (m == NULL) {
682c3a56752SGleb Smirnoff 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
683c3a56752SGleb Smirnoff 		if (m_new == NULL)
68495d67482SBill Paul 			return (ENOBUFS);
68595d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
68695d67482SBill Paul 	} else {
68795d67482SBill Paul 		m_new = m;
68895d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
68995d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
69095d67482SBill Paul 	}
69195d67482SBill Paul 
692652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
69395d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
69495d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
695f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
696f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
697f41ac2beSBill Paul 	ctx.sc = sc;
698f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
699f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
700f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
701f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
702f7cea149SGleb Smirnoff 		if (m == NULL) {
703f7cea149SGleb Smirnoff 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
704f41ac2beSBill Paul 			m_freem(m_new);
705f7cea149SGleb Smirnoff 		}
706f41ac2beSBill Paul 		return (ENOMEM);
707f41ac2beSBill Paul 	}
708e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(ctx.bge_busaddr);
709e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(ctx.bge_busaddr);
710e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
711e907febfSPyun YongHyeon 	r->bge_len = m_new->m_len;
712e907febfSPyun YongHyeon 	r->bge_idx = i;
713f41ac2beSBill Paul 
714f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
715f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
716f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
71795d67482SBill Paul 
71895d67482SBill Paul 	return (0);
71995d67482SBill Paul }
72095d67482SBill Paul 
72195d67482SBill Paul /*
72295d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
72395d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
72495d67482SBill Paul  */
72595d67482SBill Paul static int
7263f74909aSGleb Smirnoff bge_newbuf_jumbo(struct bge_softc *sc, int i, struct mbuf *m)
72795d67482SBill Paul {
7281be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
7291be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
73095d67482SBill Paul 	struct mbuf *m_new = NULL;
7311be6acb7SGleb Smirnoff 	int nsegs;
732f41ac2beSBill Paul 	int error;
73395d67482SBill Paul 
73495d67482SBill Paul 	if (m == NULL) {
735a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
7361be6acb7SGleb Smirnoff 		if (m_new == NULL)
73795d67482SBill Paul 			return (ENOBUFS);
73895d67482SBill Paul 
7391be6acb7SGleb Smirnoff 		m_cljget(m_new, M_DONTWAIT, MJUM9BYTES);
7401be6acb7SGleb Smirnoff 		if (!(m_new->m_flags & M_EXT)) {
74195d67482SBill Paul 			m_freem(m_new);
74295d67482SBill Paul 			return (ENOBUFS);
74395d67482SBill Paul 		}
7441be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
74595d67482SBill Paul 	} else {
74695d67482SBill Paul 		m_new = m;
7471be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
74895d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
74995d67482SBill Paul 	}
75095d67482SBill Paul 
751652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
75295d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
7531be6acb7SGleb Smirnoff 
7541be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
7551be6acb7SGleb Smirnoff 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
7561be6acb7SGleb Smirnoff 	    m_new, segs, &nsegs, BUS_DMA_NOWAIT);
7571be6acb7SGleb Smirnoff 	if (error) {
7581be6acb7SGleb Smirnoff 		if (m == NULL)
759f41ac2beSBill Paul 			m_freem(m_new);
7601be6acb7SGleb Smirnoff 		return (error);
761f7cea149SGleb Smirnoff 	}
7621be6acb7SGleb Smirnoff 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
7631be6acb7SGleb Smirnoff 
7641be6acb7SGleb Smirnoff 	/*
7651be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
7661be6acb7SGleb Smirnoff 	 */
7671be6acb7SGleb Smirnoff 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
7684e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING|BGE_RXBDFLAG_END;
7694e7ba1abSGleb Smirnoff 	r->bge_idx = i;
7704e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
7714e7ba1abSGleb Smirnoff 	switch (nsegs) {
7724e7ba1abSGleb Smirnoff 	case 4:
7734e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
7744e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
7754e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
7764e7ba1abSGleb Smirnoff 	case 3:
777e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
778e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
779e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
7804e7ba1abSGleb Smirnoff 	case 2:
7814e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
7824e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
7834e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
7844e7ba1abSGleb Smirnoff 	case 1:
7854e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
7864e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
7874e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
7884e7ba1abSGleb Smirnoff 		break;
7894e7ba1abSGleb Smirnoff 	default:
7904e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
7914e7ba1abSGleb Smirnoff 	}
792f41ac2beSBill Paul 
793f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
794f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
795f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
79695d67482SBill Paul 
79795d67482SBill Paul 	return (0);
79895d67482SBill Paul }
79995d67482SBill Paul 
80095d67482SBill Paul /*
80195d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
80295d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
80395d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
80495d67482SBill Paul  * the NIC.
80595d67482SBill Paul  */
80695d67482SBill Paul static int
8073f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
80895d67482SBill Paul {
80995d67482SBill Paul 	int i;
81095d67482SBill Paul 
81195d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
81295d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
81395d67482SBill Paul 			return (ENOBUFS);
81495d67482SBill Paul 	};
81595d67482SBill Paul 
816f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
817f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
818f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
819f41ac2beSBill Paul 
82095d67482SBill Paul 	sc->bge_std = i - 1;
82195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
82295d67482SBill Paul 
82395d67482SBill Paul 	return (0);
82495d67482SBill Paul }
82595d67482SBill Paul 
82695d67482SBill Paul static void
8273f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
82895d67482SBill Paul {
82995d67482SBill Paul 	int i;
83095d67482SBill Paul 
83195d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
83295d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
833e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
834e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
835e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
836f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
837f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
838e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
839e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
84095d67482SBill Paul 		}
841f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
84295d67482SBill Paul 		    sizeof(struct bge_rx_bd));
84395d67482SBill Paul 	}
84495d67482SBill Paul }
84595d67482SBill Paul 
84695d67482SBill Paul static int
8473f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
84895d67482SBill Paul {
84995d67482SBill Paul 	struct bge_rcb *rcb;
8501be6acb7SGleb Smirnoff 	int i;
85195d67482SBill Paul 
85295d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
85395d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
85495d67482SBill Paul 			return (ENOBUFS);
85595d67482SBill Paul 	};
85695d67482SBill Paul 
857f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
858f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
859f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
860f41ac2beSBill Paul 
86195d67482SBill Paul 	sc->bge_jumbo = i - 1;
86295d67482SBill Paul 
863f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
8641be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
8651be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
86667111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
86795d67482SBill Paul 
86895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
86995d67482SBill Paul 
87095d67482SBill Paul 	return (0);
87195d67482SBill Paul }
87295d67482SBill Paul 
87395d67482SBill Paul static void
8743f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
87595d67482SBill Paul {
87695d67482SBill Paul 	int i;
87795d67482SBill Paul 
87895d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
87995d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
880e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
881e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
882e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
883f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
884f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
885e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
886e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
88795d67482SBill Paul 		}
888f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
8891be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
89095d67482SBill Paul 	}
89195d67482SBill Paul }
89295d67482SBill Paul 
89395d67482SBill Paul static void
8943f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
89595d67482SBill Paul {
89695d67482SBill Paul 	int i;
89795d67482SBill Paul 
898f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
89995d67482SBill Paul 		return;
90095d67482SBill Paul 
90195d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
90295d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
903e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
904e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
905e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
906f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
907f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
908e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
909e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
91095d67482SBill Paul 		}
911f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
91295d67482SBill Paul 		    sizeof(struct bge_tx_bd));
91395d67482SBill Paul 	}
91495d67482SBill Paul }
91595d67482SBill Paul 
91695d67482SBill Paul static int
9173f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
91895d67482SBill Paul {
91995d67482SBill Paul 	sc->bge_txcnt = 0;
92095d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
9213927098fSPaul Saab 
92214bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
92314bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
92414bbd30fSGleb Smirnoff 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
92514bbd30fSGleb Smirnoff 
9263927098fSPaul Saab 	/* 5700 b2 errata */
927e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
92814bbd30fSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
9293927098fSPaul Saab 
93014bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
9313927098fSPaul Saab 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
9323927098fSPaul Saab 	/* 5700 b2 errata */
933e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
93495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
93595d67482SBill Paul 
93695d67482SBill Paul 	return (0);
93795d67482SBill Paul }
93895d67482SBill Paul 
93995d67482SBill Paul static void
9403f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
94195d67482SBill Paul {
94295d67482SBill Paul 	struct ifnet *ifp;
94395d67482SBill Paul 	struct ifmultiaddr *ifma;
9443f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
94595d67482SBill Paul 	int h, i;
94695d67482SBill Paul 
9470f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
9480f9bd73bSSam Leffler 
949fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
95095d67482SBill Paul 
95195d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
95295d67482SBill Paul 		for (i = 0; i < 4; i++)
95395d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
95495d67482SBill Paul 		return;
95595d67482SBill Paul 	}
95695d67482SBill Paul 
95795d67482SBill Paul 	/* First, zot all the existing filters. */
95895d67482SBill Paul 	for (i = 0; i < 4; i++)
95995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
96095d67482SBill Paul 
96195d67482SBill Paul 	/* Now program new ones. */
96213b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
96395d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
96495d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
96595d67482SBill Paul 			continue;
9660e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
9670e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
96895d67482SBill Paul 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
96995d67482SBill Paul 	}
97013b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
97195d67482SBill Paul 
97295d67482SBill Paul 	for (i = 0; i < 4; i++)
97395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
97495d67482SBill Paul }
97595d67482SBill Paul 
9768cb1383cSDoug Ambrisko static void
9778cb1383cSDoug Ambrisko bge_sig_pre_reset(sc, type)
9788cb1383cSDoug Ambrisko 	struct bge_softc *sc;
9798cb1383cSDoug Ambrisko 	int type;
9808cb1383cSDoug Ambrisko {
9818cb1383cSDoug Ambrisko 	/*
9828cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
9838cb1383cSDoug Ambrisko 	 */
9848cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
9858cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
9868cb1383cSDoug Ambrisko 
9878cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
9888cb1383cSDoug Ambrisko 		switch (type) {
9898cb1383cSDoug Ambrisko 		case BGE_RESET_START:
9908cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
9918cb1383cSDoug Ambrisko 			break;
9928cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
9938cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
9948cb1383cSDoug Ambrisko 			break;
9958cb1383cSDoug Ambrisko 		}
9968cb1383cSDoug Ambrisko 	}
9978cb1383cSDoug Ambrisko }
9988cb1383cSDoug Ambrisko 
9998cb1383cSDoug Ambrisko static void
10008cb1383cSDoug Ambrisko bge_sig_post_reset(sc, type)
10018cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10028cb1383cSDoug Ambrisko 	int type;
10038cb1383cSDoug Ambrisko {
10048cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
10058cb1383cSDoug Ambrisko 		switch (type) {
10068cb1383cSDoug Ambrisko 		case BGE_RESET_START:
10078cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000001);
10088cb1383cSDoug Ambrisko 			/* START DONE */
10098cb1383cSDoug Ambrisko 			break;
10108cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
10118cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000002);
10128cb1383cSDoug Ambrisko 			break;
10138cb1383cSDoug Ambrisko 		}
10148cb1383cSDoug Ambrisko 	}
10158cb1383cSDoug Ambrisko }
10168cb1383cSDoug Ambrisko 
10178cb1383cSDoug Ambrisko static void
10188cb1383cSDoug Ambrisko bge_sig_legacy(sc, type)
10198cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10208cb1383cSDoug Ambrisko 	int type;
10218cb1383cSDoug Ambrisko {
10228cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
10238cb1383cSDoug Ambrisko 		switch (type) {
10248cb1383cSDoug Ambrisko 		case BGE_RESET_START:
10258cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
10268cb1383cSDoug Ambrisko 			break;
10278cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
10288cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
10298cb1383cSDoug Ambrisko 			break;
10308cb1383cSDoug Ambrisko 		}
10318cb1383cSDoug Ambrisko 	}
10328cb1383cSDoug Ambrisko }
10338cb1383cSDoug Ambrisko 
10348cb1383cSDoug Ambrisko void bge_stop_fw(struct bge_softc *);
10358cb1383cSDoug Ambrisko void
10368cb1383cSDoug Ambrisko bge_stop_fw(sc)
10378cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10388cb1383cSDoug Ambrisko {
10398cb1383cSDoug Ambrisko 	int i;
10408cb1383cSDoug Ambrisko 
10418cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
10428cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW, BGE_FW_PAUSE);
10438cb1383cSDoug Ambrisko 		CSR_WRITE_4(sc, BGE_CPU_EVENT,
10448cb1383cSDoug Ambrisko 			    CSR_READ_4(sc, BGE_CPU_EVENT) != (1 << 14));
10458cb1383cSDoug Ambrisko 
10468cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
10478cb1383cSDoug Ambrisko 			if (!(CSR_READ_4(sc, BGE_CPU_EVENT) & (1 << 14)))
10488cb1383cSDoug Ambrisko 				break;
10498cb1383cSDoug Ambrisko 			DELAY(10);
10508cb1383cSDoug Ambrisko 		}
10518cb1383cSDoug Ambrisko 	}
10528cb1383cSDoug Ambrisko }
10538cb1383cSDoug Ambrisko 
105495d67482SBill Paul /*
105595d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
105695d67482SBill Paul  * self-test results.
105795d67482SBill Paul  */
105895d67482SBill Paul static int
10593f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
106095d67482SBill Paul {
10613f74909aSGleb Smirnoff 	uint32_t dma_rw_ctl;
106295d67482SBill Paul 	int i;
106395d67482SBill Paul 
10648cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
1065e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
106695d67482SBill Paul 
106795d67482SBill Paul 	/*
106895d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
106995d67482SBill Paul 	 * self-tests passed.
107095d67482SBill Paul 	 */
107195d67482SBill Paul 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
1072fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "RX CPU self-diagnostics failed!\n");
107395d67482SBill Paul 		return (ENODEV);
107495d67482SBill Paul 	}
107595d67482SBill Paul 
107695d67482SBill Paul 	/* Clear the MAC control register */
107795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
107895d67482SBill Paul 
107995d67482SBill Paul 	/*
108095d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
108195d67482SBill Paul 	 * internal memory.
108295d67482SBill Paul 	 */
108395d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
10843f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
108595d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
108695d67482SBill Paul 
108795d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
10883f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
108995d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
109095d67482SBill Paul 
109195d67482SBill Paul 	/* Set up the PCI DMA control register. */
1092652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
10934c0da0ffSGleb Smirnoff 		/* PCI Express bus */
1094e53d81eeSPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1095e53d81eeSPaul Saab 		    (0xf << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1096e53d81eeSPaul Saab 		    (0x2 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1097652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
10988287860eSJohn Polstra 		/* PCI-X bus */
10994c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
11004c0da0ffSGleb Smirnoff 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD;
11014c0da0ffSGleb Smirnoff 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_ONEDMA_ATONCE; /* XXX */
11024c0da0ffSGleb Smirnoff 			/* XXX magic values, Broadcom-supplied Linux driver */
11034c0da0ffSGleb Smirnoff 			if (sc->bge_asicrev == BGE_ASICREV_BCM5780)
11044c0da0ffSGleb Smirnoff 				dma_rw_ctl |= (1 << 20) | (1 << 18) |
11054c0da0ffSGleb Smirnoff 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE;
11064c0da0ffSGleb Smirnoff 			else
11074c0da0ffSGleb Smirnoff 				dma_rw_ctl |= (1 << 20) | (1 << 18) | (1 << 15);
11084c0da0ffSGleb Smirnoff 
11094c0da0ffSGleb Smirnoff 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
11105cba12d3SPaul Saab 			/*
11115cba12d3SPaul Saab 			 * The 5704 uses a different encoding of read/write
11125cba12d3SPaul Saab 			 * watermarks.
11135cba12d3SPaul Saab 			 */
11145cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
11155cba12d3SPaul Saab 			    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
11165cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
11175cba12d3SPaul Saab 		else
11185cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
11195cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
11205cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
11215cba12d3SPaul Saab 			    (0x0F);
11225cba12d3SPaul Saab 
11235cba12d3SPaul Saab 		/*
11245cba12d3SPaul Saab 		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
11255cba12d3SPaul Saab 		 * for hardware bugs.
11265cba12d3SPaul Saab 		 */
1127e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1128e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
11293f74909aSGleb Smirnoff 			uint32_t tmp;
11305cba12d3SPaul Saab 
11315cba12d3SPaul Saab 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
11325cba12d3SPaul Saab 			if (tmp == 0x6 || tmp == 0x7)
11335cba12d3SPaul Saab 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
11348287860eSJohn Polstra 		}
11354c0da0ffSGleb Smirnoff 	} else
11364c0da0ffSGleb Smirnoff 		/* Conventional PCI bus */
11374c0da0ffSGleb Smirnoff 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
11384c0da0ffSGleb Smirnoff 		    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
11394c0da0ffSGleb Smirnoff 		    (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
11404c0da0ffSGleb Smirnoff 		    (0x0F);
11415cba12d3SPaul Saab 
1142e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
11430434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
11444c0da0ffSGleb Smirnoff 	    sc->bge_asicrev == BGE_ASICREV_BCM5705)
11455cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
11465cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
114795d67482SBill Paul 
114895d67482SBill Paul 	/*
114995d67482SBill Paul 	 * Set up general mode register.
115095d67482SBill Paul 	 */
1151e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
115295d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1153ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
115495d67482SBill Paul 
115595d67482SBill Paul 	/*
11568cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
11578cb1383cSDoug Ambrisko 	 */
11588cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
11598cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
11608cb1383cSDoug Ambrisko 
11618cb1383cSDoug Ambrisko 	/*
1162ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1163ea13bdd5SJohn Polstra 	 * properly by these devices.
116495d67482SBill Paul 	 */
1165ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
116695d67482SBill Paul 
116795d67482SBill Paul #ifdef __brokenalpha__
116895d67482SBill Paul 	/*
116995d67482SBill Paul 	 * Must insure that we do not cross an 8K (bytes) boundary
117095d67482SBill Paul 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
117195d67482SBill Paul 	 * restriction on some ALPHA platforms with early revision
117295d67482SBill Paul 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
117395d67482SBill Paul 	 */
117462f1ea9cSJohn Polstra 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
117562f1ea9cSJohn Polstra 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
117695d67482SBill Paul #endif
117795d67482SBill Paul 
117895d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
117995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
118095d67482SBill Paul 
118195d67482SBill Paul 	return (0);
118295d67482SBill Paul }
118395d67482SBill Paul 
118495d67482SBill Paul static int
11853f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
118695d67482SBill Paul {
118795d67482SBill Paul 	struct bge_rcb *rcb;
1188e907febfSPyun YongHyeon 	bus_size_t vrcb;
1189e907febfSPyun YongHyeon 	bge_hostaddr taddr;
119095d67482SBill Paul 	int i;
119195d67482SBill Paul 
119295d67482SBill Paul 	/*
119395d67482SBill Paul 	 * Initialize the memory window pointer register so that
119495d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
119595d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
119695d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
119795d67482SBill Paul 	 */
119895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
119995d67482SBill Paul 
1200822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1201822f63fcSBill Paul 
12024c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc))) {
120395d67482SBill Paul 		/* Configure mbuf memory pool */
1204652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_EXTRAM) {
12050434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
12060434d1b8SBill Paul 			    BGE_EXT_SSRAM);
1207822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1208822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1209822f63fcSBill Paul 			else
121095d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
121195d67482SBill Paul 		} else {
12120434d1b8SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
12130434d1b8SBill Paul 			    BGE_BUFFPOOL_1);
1214822f63fcSBill Paul 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1215822f63fcSBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1216822f63fcSBill Paul 			else
121795d67482SBill Paul 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
121895d67482SBill Paul 		}
121995d67482SBill Paul 
122095d67482SBill Paul 		/* Configure DMA resource pool */
12210434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
12220434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
122395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
12240434d1b8SBill Paul 	}
122595d67482SBill Paul 
122695d67482SBill Paul 	/* Configure mbuf pool watermarks */
12274c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc))) {
12280434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
12290434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
12300434d1b8SBill Paul 	} else {
1231fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1232fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
12330434d1b8SBill Paul 	}
1234fa228020SPaul Saab 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
123595d67482SBill Paul 
123695d67482SBill Paul 	/* Configure DMA resource watermarks */
123795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
123895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
123995d67482SBill Paul 
124095d67482SBill Paul 	/* Enable buffer manager */
12414c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc))) {
124295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
124395d67482SBill Paul 		    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
124495d67482SBill Paul 
124595d67482SBill Paul 		/* Poll for buffer manager start indication */
124695d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
124795d67482SBill Paul 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
124895d67482SBill Paul 				break;
124995d67482SBill Paul 			DELAY(10);
125095d67482SBill Paul 		}
125195d67482SBill Paul 
125295d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1253fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1254fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
125595d67482SBill Paul 			return (ENXIO);
125695d67482SBill Paul 		}
12570434d1b8SBill Paul 	}
125895d67482SBill Paul 
125995d67482SBill Paul 	/* Enable flow-through queues */
126095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
126195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
126295d67482SBill Paul 
126395d67482SBill Paul 	/* Wait until queue initialization is complete */
126495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
126595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
126695d67482SBill Paul 			break;
126795d67482SBill Paul 		DELAY(10);
126895d67482SBill Paul 	}
126995d67482SBill Paul 
127095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1271fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
127295d67482SBill Paul 		return (ENXIO);
127395d67482SBill Paul 	}
127495d67482SBill Paul 
127595d67482SBill Paul 	/* Initialize the standard RX ring control block */
1276f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1277f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1278f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1279f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1280f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1281f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1282f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
12834c0da0ffSGleb Smirnoff 	if (BGE_IS_5705_OR_BEYOND(sc))
12840434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
12850434d1b8SBill Paul 	else
12860434d1b8SBill Paul 		rcb->bge_maxlen_flags =
12870434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
1288652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_EXTRAM)
128995d67482SBill Paul 		rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
129095d67482SBill Paul 	else
129195d67482SBill Paul 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
129267111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
129367111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1294f41ac2beSBill Paul 
129567111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
129667111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
129795d67482SBill Paul 
129895d67482SBill Paul 	/*
129995d67482SBill Paul 	 * Initialize the jumbo RX ring control block
130095d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
130195d67482SBill Paul 	 * field until we're actually ready to start
130295d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
130395d67482SBill Paul 	 * high enough to require it).
130495d67482SBill Paul 	 */
13054c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1306f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1307f41ac2beSBill Paul 
1308f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1309f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1310f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1311f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1312f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1313f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1314f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
13151be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
13161be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD|BGE_RCB_FLAG_RING_DISABLED);
1317652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_EXTRAM)
131895d67482SBill Paul 			rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
131995d67482SBill Paul 		else
132095d67482SBill Paul 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
132167111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
132267111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
132367111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
132467111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1325f41ac2beSBill Paul 
13260434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
13270434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
132867111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
132995d67482SBill Paul 
133095d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1331f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
133267111612SJohn Polstra 		rcb->bge_maxlen_flags =
133367111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
13340434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
13350434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
13360434d1b8SBill Paul 	}
133795d67482SBill Paul 
133895d67482SBill Paul 	/*
133995d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
134095d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
134195d67482SBill Paul 	 * each ring.
134295d67482SBill Paul 	 */
134395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
134495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
134595d67482SBill Paul 
134695d67482SBill Paul 	/*
134795d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
134895d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
134995d67482SBill Paul 	 * These are located in NIC memory.
135095d67482SBill Paul 	 */
1351e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
135295d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1353e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1354e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1355e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1356e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
135795d67482SBill Paul 	}
135895d67482SBill Paul 
135995d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
1360e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1361e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1362e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1363e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1364e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1365e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
13664c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
1367e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1368e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
136995d67482SBill Paul 
137095d67482SBill Paul 	/* Disable all unused RX return rings */
1371e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
137295d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1373e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1374e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1375e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
13760434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1377e907febfSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED));
1378e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
137995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
13803f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1381e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
138295d67482SBill Paul 	}
138395d67482SBill Paul 
138495d67482SBill Paul 	/* Initialize RX ring indexes */
138595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
138695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
138795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
138895d67482SBill Paul 
138995d67482SBill Paul 	/*
139095d67482SBill Paul 	 * Set up RX return ring 0
139195d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
139295d67482SBill Paul 	 * The return rings live entirely within the host, so the
139395d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
139495d67482SBill Paul 	 */
1395e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1396e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1397e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1398e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1399e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1400e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1401e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
140295d67482SBill Paul 
140395d67482SBill Paul 	/* Set random backoff seed for TX */
140495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
14054a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
14064a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
14074a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
140895d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
140995d67482SBill Paul 
141095d67482SBill Paul 	/* Set inter-packet gap */
141195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
141295d67482SBill Paul 
141395d67482SBill Paul 	/*
141495d67482SBill Paul 	 * Specify which ring to use for packets that don't match
141595d67482SBill Paul 	 * any RX rules.
141695d67482SBill Paul 	 */
141795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
141895d67482SBill Paul 
141995d67482SBill Paul 	/*
142095d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
142195d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
142295d67482SBill Paul 	 */
142395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
142495d67482SBill Paul 
142595d67482SBill Paul 	/* Inialize RX list placement stats mask. */
142695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
142795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
142895d67482SBill Paul 
142995d67482SBill Paul 	/* Disable host coalescing until we get it set up */
143095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
143195d67482SBill Paul 
143295d67482SBill Paul 	/* Poll to make sure it's shut down. */
143395d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
143495d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
143595d67482SBill Paul 			break;
143695d67482SBill Paul 		DELAY(10);
143795d67482SBill Paul 	}
143895d67482SBill Paul 
143995d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1440fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1441fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
144295d67482SBill Paul 		return (ENXIO);
144395d67482SBill Paul 	}
144495d67482SBill Paul 
144595d67482SBill Paul 	/* Set up host coalescing defaults */
144695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
144795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
144895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
144995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
14504c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc))) {
145195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
145295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
14530434d1b8SBill Paul 	}
145495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
145595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
145695d67482SBill Paul 
145795d67482SBill Paul 	/* Set up address of statistics block */
14584c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc))) {
1459f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1460f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
146195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1462f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
14630434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
146495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
14650434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
14660434d1b8SBill Paul 	}
14670434d1b8SBill Paul 
14680434d1b8SBill Paul 	/* Set up address of status block */
1469f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1470f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
147195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1472f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1473f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1474f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
147595d67482SBill Paul 
147695d67482SBill Paul 	/* Turn on host coalescing state machine */
147795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
147895d67482SBill Paul 
147995d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
148095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
148195d67482SBill Paul 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
148295d67482SBill Paul 
148395d67482SBill Paul 	/* Turn on RX list placement state machine */
148495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
148595d67482SBill Paul 
148695d67482SBill Paul 	/* Turn on RX list selector state machine. */
14874c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
148895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
148995d67482SBill Paul 
149095d67482SBill Paul 	/* Turn on DMA, clear stats */
149195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
149295d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
149395d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
149495d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
1495652ae483SGleb Smirnoff 	    ((sc->bge_flags & BGE_FLAG_TBI) ?
1496652ae483SGleb Smirnoff 	    BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
149795d67482SBill Paul 
149895d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
149995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
150095d67482SBill Paul 
150195d67482SBill Paul #ifdef notdef
150295d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
150395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
150495d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
150595d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
150695d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
150795d67482SBill Paul #endif
150895d67482SBill Paul 
150995d67482SBill Paul 	/* Turn on DMA completion state machine */
15104c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
151195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
151295d67482SBill Paul 
151395d67482SBill Paul 	/* Turn on write DMA state machine */
151495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_WDMA_MODE,
151595d67482SBill Paul 	    BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
151695d67482SBill Paul 
151795d67482SBill Paul 	/* Turn on read DMA state machine */
151895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
151995d67482SBill Paul 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
152095d67482SBill Paul 
152195d67482SBill Paul 	/* Turn on RX data completion state machine */
152295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
152395d67482SBill Paul 
152495d67482SBill Paul 	/* Turn on RX BD initiator state machine */
152595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
152695d67482SBill Paul 
152795d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
152895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
152995d67482SBill Paul 
153095d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
15314c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
153295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
153395d67482SBill Paul 
153495d67482SBill Paul 	/* Turn on send BD completion state machine */
153595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
153695d67482SBill Paul 
153795d67482SBill Paul 	/* Turn on send data completion state machine */
153895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
153995d67482SBill Paul 
154095d67482SBill Paul 	/* Turn on send data initiator state machine */
154195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
154295d67482SBill Paul 
154395d67482SBill Paul 	/* Turn on send BD initiator state machine */
154495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
154595d67482SBill Paul 
154695d67482SBill Paul 	/* Turn on send BD selector state machine */
154795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
154895d67482SBill Paul 
154995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
155095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
155195d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
155295d67482SBill Paul 
155395d67482SBill Paul 	/* ack/clear link change events */
155495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
15550434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
15560434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1557f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
155895d67482SBill Paul 
155995d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
1560652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
156195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1562a1d52896SBill Paul 	} else {
156395d67482SBill Paul 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
15641f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
15654c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
1566a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1567a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1568a1d52896SBill Paul 	}
156995d67482SBill Paul 
15701f313773SOleg Bulyzhin 	/*
15711f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
15721f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
15731f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
15741f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
15751f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
15761f313773SOleg Bulyzhin 	 */
15771f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
15781f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
15791f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
15801f313773SOleg Bulyzhin 
158195d67482SBill Paul 	/* Enable link state change attentions. */
158295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
158395d67482SBill Paul 
158495d67482SBill Paul 	return (0);
158595d67482SBill Paul }
158695d67482SBill Paul 
15874c0da0ffSGleb Smirnoff const struct bge_revision *
15884c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
15894c0da0ffSGleb Smirnoff {
15904c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
15914c0da0ffSGleb Smirnoff 
15924c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
15934c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
15944c0da0ffSGleb Smirnoff 			return (br);
15954c0da0ffSGleb Smirnoff 	}
15964c0da0ffSGleb Smirnoff 
15974c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
15984c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
15994c0da0ffSGleb Smirnoff 			return (br);
16004c0da0ffSGleb Smirnoff 	}
16014c0da0ffSGleb Smirnoff 
16024c0da0ffSGleb Smirnoff 	return (NULL);
16034c0da0ffSGleb Smirnoff }
16044c0da0ffSGleb Smirnoff 
16054c0da0ffSGleb Smirnoff const struct bge_vendor *
16064c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
16074c0da0ffSGleb Smirnoff {
16084c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
16094c0da0ffSGleb Smirnoff 
16104c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
16114c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
16124c0da0ffSGleb Smirnoff 			return (v);
16134c0da0ffSGleb Smirnoff 
16144c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
16154c0da0ffSGleb Smirnoff 	return (NULL);
16164c0da0ffSGleb Smirnoff }
16174c0da0ffSGleb Smirnoff 
161895d67482SBill Paul /*
161995d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
16204c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
16214c0da0ffSGleb Smirnoff  *
16224c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
162395d67482SBill Paul  * can get the device name string from the controller itself instead
162495d67482SBill Paul  * of the compiled-in string. This is a little slow, but it guarantees
16254c0da0ffSGleb Smirnoff  * we'll always announce the right product name. Unfortunately, this
16264c0da0ffSGleb Smirnoff  * is possible only later in bge_attach(), when we have established
16274c0da0ffSGleb Smirnoff  * access to EEPROM.
162895d67482SBill Paul  */
162995d67482SBill Paul static int
16303f74909aSGleb Smirnoff bge_probe(device_t dev)
163195d67482SBill Paul {
16324c0da0ffSGleb Smirnoff 	struct bge_type *t = bge_devs;
16334c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
163495d67482SBill Paul 
163595d67482SBill Paul 	bzero(sc, sizeof(struct bge_softc));
163695d67482SBill Paul 	sc->bge_dev = dev;
163795d67482SBill Paul 
16384c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
163995d67482SBill Paul 		if ((pci_get_vendor(dev) == t->bge_vid) &&
164095d67482SBill Paul 		    (pci_get_device(dev) == t->bge_did)) {
16414c0da0ffSGleb Smirnoff 			char buf[64];
16424c0da0ffSGleb Smirnoff 			const struct bge_revision *br;
16434c0da0ffSGleb Smirnoff 			const struct bge_vendor *v;
16444c0da0ffSGleb Smirnoff 			uint32_t id;
16454c0da0ffSGleb Smirnoff 
16464c0da0ffSGleb Smirnoff 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
16474c0da0ffSGleb Smirnoff 			    BGE_PCIMISCCTL_ASICREV;
16484c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
16494c0da0ffSGleb Smirnoff 			id >>= 16;
16504c0da0ffSGleb Smirnoff 			v = bge_lookup_vendor(t->bge_vid);
16514c0da0ffSGleb Smirnoff 			if (br == NULL)
16524c0da0ffSGleb Smirnoff 				snprintf(buf, 64, "%s unknown ASIC (%#04x)",
16534c0da0ffSGleb Smirnoff 				    v->v_name, id);
16544c0da0ffSGleb Smirnoff 			else
16554c0da0ffSGleb Smirnoff 				snprintf(buf, 64, "%s %s, ASIC rev. %#04x",
16564c0da0ffSGleb Smirnoff 				    v->v_name, br->br_name, id);
16574c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
16586d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
1659652ae483SGleb Smirnoff 				sc->bge_flags |= BGE_FLAG_NO3LED;
166095d67482SBill Paul 			return (0);
166195d67482SBill Paul 		}
166295d67482SBill Paul 		t++;
166395d67482SBill Paul 	}
166495d67482SBill Paul 
166595d67482SBill Paul 	return (ENXIO);
166695d67482SBill Paul }
166795d67482SBill Paul 
1668f41ac2beSBill Paul static void
16693f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
1670f41ac2beSBill Paul {
1671f41ac2beSBill Paul 	int i;
1672f41ac2beSBill Paul 
16733f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
1674f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1675f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1676f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1677f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1678f41ac2beSBill Paul 	}
1679f41ac2beSBill Paul 
16803f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
1681f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1682f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1683f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1684f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1685f41ac2beSBill Paul 	}
1686f41ac2beSBill Paul 
16873f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
1688f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1689f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1690f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1691f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1692f41ac2beSBill Paul 	}
1693f41ac2beSBill Paul 
1694f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1695f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1696f41ac2beSBill Paul 
1697f41ac2beSBill Paul 
16983f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
1699e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
1700e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1701e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
1702e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1703f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1704f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1705f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1706f41ac2beSBill Paul 
1707f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1708f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1709f41ac2beSBill Paul 
17103f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
1711e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1712e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1713e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1714e65bed95SPyun YongHyeon 
1715e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1716e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
1717f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1718f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1719f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1720f41ac2beSBill Paul 
1721f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1722f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1723f41ac2beSBill Paul 
17243f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
1725e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
1726e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1727e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
1728e65bed95SPyun YongHyeon 
1729e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1730e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
1731f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1732f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1733f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1734f41ac2beSBill Paul 
1735f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1736f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1737f41ac2beSBill Paul 
17383f74909aSGleb Smirnoff 	/* Destroy TX ring. */
1739e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
1740e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1741e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
1742e65bed95SPyun YongHyeon 
1743e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
1744f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1745f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1746f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1747f41ac2beSBill Paul 
1748f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1749f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1750f41ac2beSBill Paul 
17513f74909aSGleb Smirnoff 	/* Destroy status block. */
1752e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
1753e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1754e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
1755e65bed95SPyun YongHyeon 
1756e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
1757f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1758f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1759f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1760f41ac2beSBill Paul 
1761f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1762f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1763f41ac2beSBill Paul 
17643f74909aSGleb Smirnoff 	/* Destroy statistics block. */
1765e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
1766e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1767e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
1768e65bed95SPyun YongHyeon 
1769e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
1770f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1771f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1772f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1773f41ac2beSBill Paul 
1774f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1775f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1776f41ac2beSBill Paul 
17773f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
1778f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1779f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1780f41ac2beSBill Paul }
1781f41ac2beSBill Paul 
1782f41ac2beSBill Paul static int
17833f74909aSGleb Smirnoff bge_dma_alloc(device_t dev)
1784f41ac2beSBill Paul {
17853f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
1786f41ac2beSBill Paul 	struct bge_softc *sc;
17871be6acb7SGleb Smirnoff 	int i, error;
1788f41ac2beSBill Paul 
1789f41ac2beSBill Paul 	sc = device_get_softc(dev);
1790f41ac2beSBill Paul 
1791f41ac2beSBill Paul 	/*
1792f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1793f41ac2beSBill Paul 	 */
1794378f231eSJohn-Mark Gurney 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),/* parent */
1795f41ac2beSBill Paul 			PAGE_SIZE, 0,		/* alignment, boundary */
1796f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
17972f28b973SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1798f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1799f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1800f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
18018a40c10eSScott Long 			0,			/* flags */
1802f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1803f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1804f41ac2beSBill Paul 
1805e65bed95SPyun YongHyeon 	if (error != 0) {
1806fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1807fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
1808e65bed95SPyun YongHyeon 		return (ENOMEM);
1809e65bed95SPyun YongHyeon 	}
1810e65bed95SPyun YongHyeon 
1811f41ac2beSBill Paul 	/*
1812f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1813f41ac2beSBill Paul 	 */
18148a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1815f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
18161be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
18171be6acb7SGleb Smirnoff 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
1818f41ac2beSBill Paul 
1819f41ac2beSBill Paul 	if (error) {
1820fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1821f41ac2beSBill Paul 		return (ENOMEM);
1822f41ac2beSBill Paul 	}
1823f41ac2beSBill Paul 
18243f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
1825f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1826f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1827f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1828f41ac2beSBill Paul 		if (error) {
1829fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1830fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1831f41ac2beSBill Paul 			return (ENOMEM);
1832f41ac2beSBill Paul 		}
1833f41ac2beSBill Paul 	}
1834f41ac2beSBill Paul 
18353f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
1836f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1837f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1838f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1839f41ac2beSBill Paul 		if (error) {
1840fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1841fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1842f41ac2beSBill Paul 			return (ENOMEM);
1843f41ac2beSBill Paul 		}
1844f41ac2beSBill Paul 	}
1845f41ac2beSBill Paul 
18463f74909aSGleb Smirnoff 	/* Create tag for standard RX ring. */
1847f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1848f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1849f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1850f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1851f41ac2beSBill Paul 
1852f41ac2beSBill Paul 	if (error) {
1853fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1854f41ac2beSBill Paul 		return (ENOMEM);
1855f41ac2beSBill Paul 	}
1856f41ac2beSBill Paul 
18573f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for standard RX ring. */
1858f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1859f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1860f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1861f41ac2beSBill Paul 	if (error)
1862f41ac2beSBill Paul 		return (ENOMEM);
1863f41ac2beSBill Paul 
1864f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1865f41ac2beSBill Paul 
18663f74909aSGleb Smirnoff 	/* Load the address of the standard RX ring. */
1867f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1868f41ac2beSBill Paul 	ctx.sc = sc;
1869f41ac2beSBill Paul 
1870f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1871f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1872f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1873f41ac2beSBill Paul 
1874f41ac2beSBill Paul 	if (error)
1875f41ac2beSBill Paul 		return (ENOMEM);
1876f41ac2beSBill Paul 
1877f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
1878f41ac2beSBill Paul 
18793f74909aSGleb Smirnoff 	/* Create tags for jumbo mbufs. */
18804c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1881f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
18828a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
18831be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
18841be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
1885f41ac2beSBill Paul 		if (error) {
1886fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
18873f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
1888f41ac2beSBill Paul 			return (ENOMEM);
1889f41ac2beSBill Paul 		}
1890f41ac2beSBill Paul 
18913f74909aSGleb Smirnoff 		/* Create tag for jumbo RX ring. */
1892f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1893f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1894f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
1895f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
1896f41ac2beSBill Paul 
1897f41ac2beSBill Paul 		if (error) {
1898fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
18993f74909aSGleb Smirnoff 			    "could not allocate jumbo ring dma tag\n");
1900f41ac2beSBill Paul 			return (ENOMEM);
1901f41ac2beSBill Paul 		}
1902f41ac2beSBill Paul 
19033f74909aSGleb Smirnoff 		/* Allocate DMA'able memory for jumbo RX ring. */
1904f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
19051be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
19061be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1907f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
1908f41ac2beSBill Paul 		if (error)
1909f41ac2beSBill Paul 			return (ENOMEM);
1910f41ac2beSBill Paul 
19113f74909aSGleb Smirnoff 		/* Load the address of the jumbo RX ring. */
1912f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
1913f41ac2beSBill Paul 		ctx.sc = sc;
1914f41ac2beSBill Paul 
1915f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1916f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1917f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
1918f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1919f41ac2beSBill Paul 
1920f41ac2beSBill Paul 		if (error)
1921f41ac2beSBill Paul 			return (ENOMEM);
1922f41ac2beSBill Paul 
1923f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
1924f41ac2beSBill Paul 
19253f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
1926f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1927f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
1928f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1929f41ac2beSBill Paul 			if (error) {
1930fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
19313f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
1932f41ac2beSBill Paul 				return (ENOMEM);
1933f41ac2beSBill Paul 			}
1934f41ac2beSBill Paul 		}
1935f41ac2beSBill Paul 
1936f41ac2beSBill Paul 	}
1937f41ac2beSBill Paul 
19383f74909aSGleb Smirnoff 	/* Create tag for RX return ring. */
1939f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1940f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1941f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
1942f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
1943f41ac2beSBill Paul 
1944f41ac2beSBill Paul 	if (error) {
1945fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1946f41ac2beSBill Paul 		return (ENOMEM);
1947f41ac2beSBill Paul 	}
1948f41ac2beSBill Paul 
19493f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for RX return ring. */
1950f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
1951f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
1952f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
1953f41ac2beSBill Paul 	if (error)
1954f41ac2beSBill Paul 		return (ENOMEM);
1955f41ac2beSBill Paul 
1956f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
1957f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
1958f41ac2beSBill Paul 
19593f74909aSGleb Smirnoff 	/* Load the address of the RX return ring. */
1960f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1961f41ac2beSBill Paul 	ctx.sc = sc;
1962f41ac2beSBill Paul 
1963f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
1964f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
1965f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
1966f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1967f41ac2beSBill Paul 
1968f41ac2beSBill Paul 	if (error)
1969f41ac2beSBill Paul 		return (ENOMEM);
1970f41ac2beSBill Paul 
1971f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
1972f41ac2beSBill Paul 
19733f74909aSGleb Smirnoff 	/* Create tag for TX ring. */
1974f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1975f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1976f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
1977f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
1978f41ac2beSBill Paul 
1979f41ac2beSBill Paul 	if (error) {
1980fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1981f41ac2beSBill Paul 		return (ENOMEM);
1982f41ac2beSBill Paul 	}
1983f41ac2beSBill Paul 
19843f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for TX ring. */
1985f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
1986f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
1987f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
1988f41ac2beSBill Paul 	if (error)
1989f41ac2beSBill Paul 		return (ENOMEM);
1990f41ac2beSBill Paul 
1991f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1992f41ac2beSBill Paul 
19933f74909aSGleb Smirnoff 	/* Load the address of the TX ring. */
1994f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1995f41ac2beSBill Paul 	ctx.sc = sc;
1996f41ac2beSBill Paul 
1997f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
1998f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
1999f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2000f41ac2beSBill Paul 
2001f41ac2beSBill Paul 	if (error)
2002f41ac2beSBill Paul 		return (ENOMEM);
2003f41ac2beSBill Paul 
2004f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
2005f41ac2beSBill Paul 
20063f74909aSGleb Smirnoff 	/* Create tag for status block. */
2007f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2008f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2009f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
2010f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
2011f41ac2beSBill Paul 
2012f41ac2beSBill Paul 	if (error) {
2013fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2014f41ac2beSBill Paul 		return (ENOMEM);
2015f41ac2beSBill Paul 	}
2016f41ac2beSBill Paul 
20173f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for status block. */
2018f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
2019f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
2020f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
2021f41ac2beSBill Paul 	if (error)
2022f41ac2beSBill Paul 		return (ENOMEM);
2023f41ac2beSBill Paul 
2024f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2025f41ac2beSBill Paul 
20263f74909aSGleb Smirnoff 	/* Load the address of the status block. */
2027f41ac2beSBill Paul 	ctx.sc = sc;
2028f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2029f41ac2beSBill Paul 
2030f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2031f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2032f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2033f41ac2beSBill Paul 
2034f41ac2beSBill Paul 	if (error)
2035f41ac2beSBill Paul 		return (ENOMEM);
2036f41ac2beSBill Paul 
2037f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2038f41ac2beSBill Paul 
20393f74909aSGleb Smirnoff 	/* Create tag for statistics block. */
2040f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2041f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2042f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2043f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
2044f41ac2beSBill Paul 
2045f41ac2beSBill Paul 	if (error) {
2046fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2047f41ac2beSBill Paul 		return (ENOMEM);
2048f41ac2beSBill Paul 	}
2049f41ac2beSBill Paul 
20503f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for statistics block. */
2051f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2052f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2053f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
2054f41ac2beSBill Paul 	if (error)
2055f41ac2beSBill Paul 		return (ENOMEM);
2056f41ac2beSBill Paul 
2057f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2058f41ac2beSBill Paul 
20593f74909aSGleb Smirnoff 	/* Load the address of the statstics block. */
2060f41ac2beSBill Paul 	ctx.sc = sc;
2061f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2062f41ac2beSBill Paul 
2063f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2064f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2065f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2066f41ac2beSBill Paul 
2067f41ac2beSBill Paul 	if (error)
2068f41ac2beSBill Paul 		return (ENOMEM);
2069f41ac2beSBill Paul 
2070f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2071f41ac2beSBill Paul 
2072f41ac2beSBill Paul 	return (0);
2073f41ac2beSBill Paul }
2074f41ac2beSBill Paul 
207595d67482SBill Paul static int
20763f74909aSGleb Smirnoff bge_attach(device_t dev)
207795d67482SBill Paul {
207895d67482SBill Paul 	struct ifnet *ifp;
207995d67482SBill Paul 	struct bge_softc *sc;
20803f74909aSGleb Smirnoff 	uint32_t hwcfg = 0;
20813f74909aSGleb Smirnoff 	uint32_t mac_tmp = 0;
2082fc74a9f9SBrooks Davis 	u_char eaddr[6];
2083fe806fdaSPyun YongHyeon 	int error = 0, rid;
20848cb1383cSDoug Ambrisko 	int trys;
208595d67482SBill Paul 
208695d67482SBill Paul 	sc = device_get_softc(dev);
208795d67482SBill Paul 	sc->bge_dev = dev;
208895d67482SBill Paul 
208995d67482SBill Paul 	/*
209095d67482SBill Paul 	 * Map control/status registers.
209195d67482SBill Paul 	 */
209295d67482SBill Paul 	pci_enable_busmaster(dev);
209395d67482SBill Paul 
209495d67482SBill Paul 	rid = BGE_PCI_BAR0;
20955f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
20965f96beb9SNate Lawson 	    RF_ACTIVE|PCI_RF_DENSE);
209795d67482SBill Paul 
209895d67482SBill Paul 	if (sc->bge_res == NULL) {
2099fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
210095d67482SBill Paul 		error = ENXIO;
210195d67482SBill Paul 		goto fail;
210295d67482SBill Paul 	}
210395d67482SBill Paul 
210495d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
210595d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
210695d67482SBill Paul 
21073f74909aSGleb Smirnoff 	/* Allocate interrupt. */
210895d67482SBill Paul 	rid = 0;
210995d67482SBill Paul 
21105f96beb9SNate Lawson 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
211195d67482SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
211295d67482SBill Paul 
211395d67482SBill Paul 	if (sc->bge_irq == NULL) {
2114fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
211595d67482SBill Paul 		error = ENXIO;
211695d67482SBill Paul 		goto fail;
211795d67482SBill Paul 	}
211895d67482SBill Paul 
21190f9bd73bSSam Leffler 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
21200f9bd73bSSam Leffler 
2121e53d81eeSPaul Saab 	/* Save ASIC rev. */
2122e53d81eeSPaul Saab 
2123e53d81eeSPaul Saab 	sc->bge_chipid =
2124e53d81eeSPaul Saab 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2125e53d81eeSPaul Saab 	    BGE_PCIMISCCTL_ASICREV;
2126e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2127e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2128e53d81eeSPaul Saab 
2129e53d81eeSPaul Saab 	/*
2130e53d81eeSPaul Saab 	 * XXX: Broadcom Linux driver.  Not in specs or eratta.
2131e53d81eeSPaul Saab 	 * PCI-Express?
2132e53d81eeSPaul Saab 	 */
21334c0da0ffSGleb Smirnoff 	if (BGE_IS_5705_OR_BEYOND(sc)) {
21343f74909aSGleb Smirnoff 		uint32_t v;
2135e53d81eeSPaul Saab 
2136e53d81eeSPaul Saab 		v = pci_read_config(dev, BGE_PCI_MSI_CAPID, 4);
2137e53d81eeSPaul Saab 		if (((v >> 8) & 0xff) == BGE_PCIE_CAPID_REG) {
2138e53d81eeSPaul Saab 			v = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
2139e53d81eeSPaul Saab 			if ((v & 0xff) == BGE_PCIE_CAPID)
2140652ae483SGleb Smirnoff 				sc->bge_flags |= BGE_FLAG_PCIE;
2141e53d81eeSPaul Saab 		}
2142e53d81eeSPaul Saab 	}
2143e53d81eeSPaul Saab 
21444c0da0ffSGleb Smirnoff 	/*
21454c0da0ffSGleb Smirnoff 	 * PCI-X ?
21464c0da0ffSGleb Smirnoff 	 */
21474c0da0ffSGleb Smirnoff 	if ((pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
21484c0da0ffSGleb Smirnoff 	    BGE_PCISTATE_PCI_BUSMODE) == 0)
2149652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_PCIX;
21504c0da0ffSGleb Smirnoff 
215195d67482SBill Paul 	/* Try to reset the chip. */
21528cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
21538cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
21548cb1383cSDoug Ambrisko 		bge_release_resources(sc);
21558cb1383cSDoug Ambrisko 		error = ENXIO;
21568cb1383cSDoug Ambrisko 		goto fail;
21578cb1383cSDoug Ambrisko 	}
21588cb1383cSDoug Ambrisko 
21598cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
21608cb1383cSDoug Ambrisko 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG)
21618cb1383cSDoug Ambrisko 	    == BGE_MAGIC_NUMBER) {
21628cb1383cSDoug Ambrisko 		if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG)
21638cb1383cSDoug Ambrisko 		    & BGE_HWCFG_ASF) {
21648cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_ENABLE;
21658cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_STACKUP;
21668cb1383cSDoug Ambrisko 			if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
21678cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
21688cb1383cSDoug Ambrisko 			}
21698cb1383cSDoug Ambrisko 		}
21708cb1383cSDoug Ambrisko 	}
21718cb1383cSDoug Ambrisko 
21728cb1383cSDoug Ambrisko 	/* Try to reset the chip again the nice way. */
21738cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
21748cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
21758cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
21768cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
21778cb1383cSDoug Ambrisko 		bge_release_resources(sc);
21788cb1383cSDoug Ambrisko 		error = ENXIO;
21798cb1383cSDoug Ambrisko 		goto fail;
21808cb1383cSDoug Ambrisko 	}
21818cb1383cSDoug Ambrisko 
21828cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
21838cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
218495d67482SBill Paul 
218595d67482SBill Paul 	if (bge_chipinit(sc)) {
2186fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
218795d67482SBill Paul 		bge_release_resources(sc);
218895d67482SBill Paul 		error = ENXIO;
218995d67482SBill Paul 		goto fail;
219095d67482SBill Paul 	}
219195d67482SBill Paul 
219295d67482SBill Paul 	/*
219395d67482SBill Paul 	 * Get station address from the EEPROM.
219495d67482SBill Paul 	 */
2195fc74a9f9SBrooks Davis 	mac_tmp = bge_readmem_ind(sc, 0x0c14);
2196fc74a9f9SBrooks Davis 	if ((mac_tmp >> 16) == 0x484b) {
2197fc74a9f9SBrooks Davis 		eaddr[0] = (u_char)(mac_tmp >> 8);
2198fc74a9f9SBrooks Davis 		eaddr[1] = (u_char)mac_tmp;
2199fc74a9f9SBrooks Davis 		mac_tmp = bge_readmem_ind(sc, 0x0c18);
2200fc74a9f9SBrooks Davis 		eaddr[2] = (u_char)(mac_tmp >> 24);
2201fc74a9f9SBrooks Davis 		eaddr[3] = (u_char)(mac_tmp >> 16);
2202fc74a9f9SBrooks Davis 		eaddr[4] = (u_char)(mac_tmp >> 8);
2203fc74a9f9SBrooks Davis 		eaddr[5] = (u_char)mac_tmp;
2204fc74a9f9SBrooks Davis 	} else if (bge_read_eeprom(sc, eaddr,
220595d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2206fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to read station address\n");
220795d67482SBill Paul 		bge_release_resources(sc);
220895d67482SBill Paul 		error = ENXIO;
220995d67482SBill Paul 		goto fail;
221095d67482SBill Paul 	}
221195d67482SBill Paul 
2212f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
22134c0da0ffSGleb Smirnoff 	if (BGE_IS_5705_OR_BEYOND(sc))
2214f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2215f41ac2beSBill Paul 	else
2216f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2217f41ac2beSBill Paul 
2218f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2219fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2220fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2221f41ac2beSBill Paul 		bge_release_resources(sc);
2222f41ac2beSBill Paul 		error = ENXIO;
2223f41ac2beSBill Paul 		goto fail;
2224f41ac2beSBill Paul 	}
2225f41ac2beSBill Paul 
222695d67482SBill Paul 	/* Set default tuneable values. */
222795d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
222895d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
222995d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
223095d67482SBill Paul 	sc->bge_rx_max_coal_bds = 64;
223195d67482SBill Paul 	sc->bge_tx_max_coal_bds = 128;
223295d67482SBill Paul 
223395d67482SBill Paul 	/* Set up ifnet structure */
2234fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2235fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2236fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2237fc74a9f9SBrooks Davis 		bge_release_resources(sc);
2238fc74a9f9SBrooks Davis 		error = ENXIO;
2239fc74a9f9SBrooks Davis 		goto fail;
2240fc74a9f9SBrooks Davis 	}
224195d67482SBill Paul 	ifp->if_softc = sc;
22429bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
224395d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
224495d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
224595d67482SBill Paul 	ifp->if_start = bge_start;
224695d67482SBill Paul 	ifp->if_watchdog = bge_watchdog;
224795d67482SBill Paul 	ifp->if_init = bge_init;
224895d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
22494d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
22504d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
22514d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
225295d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2253d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
2254479b23b7SGleb Smirnoff 	    IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM;
225595d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
225675719184SGleb Smirnoff #ifdef DEVICE_POLLING
225775719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
225875719184SGleb Smirnoff #endif
225995d67482SBill Paul 
2260a1d52896SBill Paul 	/*
2261d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2262d375e524SGleb Smirnoff 	 * to hardware bugs.
2263d375e524SGleb Smirnoff 	 */
2264d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2265d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2266d375e524SGleb Smirnoff 		ifp->if_capenable &= IFCAP_HWCSUM;
2267d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2268d375e524SGleb Smirnoff 	}
2269d375e524SGleb Smirnoff 
2270d375e524SGleb Smirnoff 	/*
2271a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
227241abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
227341abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
227441abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
227541abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
227641abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
227741abcc1bSPaul Saab 	 * SK-9D41.
2278a1d52896SBill Paul 	 */
227941abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
228041abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
228141abcc1bSPaul Saab 	else {
2282f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2283f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2284fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2285f6789fbaSPyun YongHyeon 			bge_release_resources(sc);
2286f6789fbaSPyun YongHyeon 			error = ENXIO;
2287f6789fbaSPyun YongHyeon 			goto fail;
2288f6789fbaSPyun YongHyeon 		}
228941abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
229041abcc1bSPaul Saab 	}
229141abcc1bSPaul Saab 
229241abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2293652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
2294a1d52896SBill Paul 
229595d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
229695d67482SBill Paul 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
2297652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
229895d67482SBill Paul 
2299652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
230095d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
230195d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
230295d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
230395d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia,
230495d67482SBill Paul 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
230595d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
230695d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2307da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
230895d67482SBill Paul 	} else {
230995d67482SBill Paul 		/*
23108cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
23118cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
23128cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
23138cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
23148cb1383cSDoug Ambrisko 		 * the PHY.
231595d67482SBill Paul 		 */
23168cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
23178cb1383cSDoug Ambrisko again:
23188cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
23198cb1383cSDoug Ambrisko 
23208cb1383cSDoug Ambrisko 		trys = 0;
232195d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
232295d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
23238cb1383cSDoug Ambrisko 			if (trys++ < 4) {
23248cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
23258cb1383cSDoug Ambrisko 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR, BMCR_RESET);
23268cb1383cSDoug Ambrisko 				goto again;
23278cb1383cSDoug Ambrisko 			}
23288cb1383cSDoug Ambrisko 
2329fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
233095d67482SBill Paul 			bge_release_resources(sc);
233195d67482SBill Paul 			error = ENXIO;
233295d67482SBill Paul 			goto fail;
233395d67482SBill Paul 		}
23348cb1383cSDoug Ambrisko 
23358cb1383cSDoug Ambrisko 		/*
23368cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
23378cb1383cSDoug Ambrisko 		 */
23388cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
23398cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
234095d67482SBill Paul 	}
234195d67482SBill Paul 
234295d67482SBill Paul 	/*
2343e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2344e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2345e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2346e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2347e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2348e255b776SJohn Polstra 	 * payloads by copying the received packets.
2349e255b776SJohn Polstra 	 */
2350652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
2351652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
2352652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
2353e255b776SJohn Polstra 
2354e255b776SJohn Polstra 	/*
235595d67482SBill Paul 	 * Call MI attach routine.
235695d67482SBill Paul 	 */
2357fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
23580f9bd73bSSam Leffler 	callout_init(&sc->bge_stat_ch, CALLOUT_MPSAFE);
23590f9bd73bSSam Leffler 
23600f9bd73bSSam Leffler 	/*
23610f9bd73bSSam Leffler 	 * Hookup IRQ last.
23620f9bd73bSSam Leffler 	 */
23630f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
23640f9bd73bSSam Leffler 	   bge_intr, sc, &sc->bge_intrhand);
23650f9bd73bSSam Leffler 
23660f9bd73bSSam Leffler 	if (error) {
2367fc74a9f9SBrooks Davis 		bge_detach(dev);
2368fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
23690f9bd73bSSam Leffler 	}
237095d67482SBill Paul 
237195d67482SBill Paul fail:
237295d67482SBill Paul 	return (error);
237395d67482SBill Paul }
237495d67482SBill Paul 
237595d67482SBill Paul static int
23763f74909aSGleb Smirnoff bge_detach(device_t dev)
237795d67482SBill Paul {
237895d67482SBill Paul 	struct bge_softc *sc;
237995d67482SBill Paul 	struct ifnet *ifp;
238095d67482SBill Paul 
238195d67482SBill Paul 	sc = device_get_softc(dev);
2382fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
238395d67482SBill Paul 
238475719184SGleb Smirnoff #ifdef DEVICE_POLLING
238575719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
238675719184SGleb Smirnoff 		ether_poll_deregister(ifp);
238775719184SGleb Smirnoff #endif
238875719184SGleb Smirnoff 
23890f9bd73bSSam Leffler 	BGE_LOCK(sc);
239095d67482SBill Paul 	bge_stop(sc);
239195d67482SBill Paul 	bge_reset(sc);
23920f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
23930f9bd73bSSam Leffler 
23940f9bd73bSSam Leffler 	ether_ifdetach(ifp);
239595d67482SBill Paul 
2396652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
239795d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
239895d67482SBill Paul 	} else {
239995d67482SBill Paul 		bus_generic_detach(dev);
240095d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
240195d67482SBill Paul 	}
240295d67482SBill Paul 
240395d67482SBill Paul 	bge_release_resources(sc);
240495d67482SBill Paul 
240595d67482SBill Paul 	return (0);
240695d67482SBill Paul }
240795d67482SBill Paul 
240895d67482SBill Paul static void
24093f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
241095d67482SBill Paul {
241195d67482SBill Paul 	device_t dev;
241295d67482SBill Paul 
241395d67482SBill Paul 	dev = sc->bge_dev;
241495d67482SBill Paul 
241595d67482SBill Paul 	if (sc->bge_vpd_prodname != NULL)
241695d67482SBill Paul 		free(sc->bge_vpd_prodname, M_DEVBUF);
241795d67482SBill Paul 
241895d67482SBill Paul 	if (sc->bge_vpd_readonly != NULL)
241995d67482SBill Paul 		free(sc->bge_vpd_readonly, M_DEVBUF);
242095d67482SBill Paul 
242195d67482SBill Paul 	if (sc->bge_intrhand != NULL)
242295d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
242395d67482SBill Paul 
242495d67482SBill Paul 	if (sc->bge_irq != NULL)
242595d67482SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
242695d67482SBill Paul 
242795d67482SBill Paul 	if (sc->bge_res != NULL)
242895d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
242995d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
243095d67482SBill Paul 
2431ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2432ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2433ad61f896SRuslan Ermilov 
2434f41ac2beSBill Paul 	bge_dma_free(sc);
243595d67482SBill Paul 
24360f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
24370f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
243895d67482SBill Paul }
243995d67482SBill Paul 
24408cb1383cSDoug Ambrisko static int
24413f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
244295d67482SBill Paul {
244395d67482SBill Paul 	device_t dev;
24443f74909aSGleb Smirnoff 	uint32_t cachesize, command, pcistate, reset;
244595d67482SBill Paul 	int i, val = 0;
244695d67482SBill Paul 
244795d67482SBill Paul 	dev = sc->bge_dev;
244895d67482SBill Paul 
244995d67482SBill Paul 	/* Save some important PCI state. */
245095d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
245195d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
245295d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
245395d67482SBill Paul 
245495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
245595d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2456e907febfSPyun YongHyeon 	BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
245795d67482SBill Paul 
2458e53d81eeSPaul Saab 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2459e53d81eeSPaul Saab 
2460e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2461652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2462e53d81eeSPaul Saab 		if (CSR_READ_4(sc, 0x7e2c) == 0x60)	/* PCIE 1.0 */
2463e53d81eeSPaul Saab 			CSR_WRITE_4(sc, 0x7e2c, 0x20);
2464e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2465e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
2466e53d81eeSPaul Saab 			CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2467e53d81eeSPaul Saab 			reset |= (1<<29);
2468e53d81eeSPaul Saab 		}
2469e53d81eeSPaul Saab 	}
2470e53d81eeSPaul Saab 
247121c9e407SDavid Christensen 	/*
247221c9e407SDavid Christensen 	 * Write the magic number to the firmware mailbox at 0xb50
247321c9e407SDavid Christensen          * so that the driver can synchronize with the firmware.
247421c9e407SDavid Christensen 	 */
247521c9e407SDavid Christensen 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
247621c9e407SDavid Christensen 
247795d67482SBill Paul 	/* Issue global reset */
2478e53d81eeSPaul Saab 	bge_writereg_ind(sc, BGE_MISC_CFG, reset);
247995d67482SBill Paul 
248095d67482SBill Paul 	DELAY(1000);
248195d67482SBill Paul 
2482e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2483652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2484e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2485e53d81eeSPaul Saab 			uint32_t v;
2486e53d81eeSPaul Saab 
2487e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
2488e53d81eeSPaul Saab 			v = pci_read_config(dev, 0xc4, 4);
2489e53d81eeSPaul Saab 			pci_write_config(dev, 0xc4, v | (1<<15), 4);
2490e53d81eeSPaul Saab 		}
2491e53d81eeSPaul Saab 		/* Set PCIE max payload size and clear error status. */
2492e53d81eeSPaul Saab 		pci_write_config(dev, 0xd8, 0xf5000, 4);
2493e53d81eeSPaul Saab 	}
2494e53d81eeSPaul Saab 
24953f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
249695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
249795d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2498e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
249995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
250095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
250195d67482SBill Paul 	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
250295d67482SBill Paul 
2503a7b0c314SPaul Saab 	/* Enable memory arbiter. */
25044c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
25054c0da0ffSGleb Smirnoff 		uint32_t val;
25064c0da0ffSGleb Smirnoff 
25074c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
25084c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
25094c0da0ffSGleb Smirnoff 	} else
2510a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2511a7b0c314SPaul Saab 
251295d67482SBill Paul 	/*
251395d67482SBill Paul 	 * Poll the value location we just wrote until
251495d67482SBill Paul 	 * we see the 1's complement of the magic number.
251595d67482SBill Paul 	 * This indicates that the firmware initialization
251695d67482SBill Paul 	 * is complete.
251795d67482SBill Paul 	 */
251895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
251995d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
252095d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
252195d67482SBill Paul 			break;
252295d67482SBill Paul 		DELAY(10);
252395d67482SBill Paul 	}
252495d67482SBill Paul 
252595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
2526fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "firmware handshake timed out\n");
25278cb1383cSDoug Ambrisko 		return(0);
252895d67482SBill Paul 	}
252995d67482SBill Paul 
253095d67482SBill Paul 	/*
253195d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
253295d67482SBill Paul 	 * return to its original pre-reset state. This is a
253395d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
253495d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
253595d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
253695d67482SBill Paul 	 * results.
253795d67482SBill Paul 	 */
253895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
253995d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
254095d67482SBill Paul 			break;
254195d67482SBill Paul 		DELAY(10);
254295d67482SBill Paul 	}
254395d67482SBill Paul 
25443f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
2545e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
254695d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
254795d67482SBill Paul 
25488cb1383cSDoug Ambrisko 	/* Tell the ASF firmware we are up */
25498cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
25508cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
25518cb1383cSDoug Ambrisko 
255295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
255395d67482SBill Paul 
2554da3003f0SBill Paul 	/*
2555da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
2556da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
2557da3003f0SBill Paul 	 * to 1.2V.
2558da3003f0SBill Paul 	 */
2559652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
2560652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
2561da3003f0SBill Paul 		uint32_t serdescfg;
2562652ae483SGleb Smirnoff 
2563da3003f0SBill Paul 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2564da3003f0SBill Paul 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
2565da3003f0SBill Paul 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2566da3003f0SBill Paul 	}
2567da3003f0SBill Paul 
2568e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2569652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
2570652ae483SGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2571e53d81eeSPaul Saab 		uint32_t v;
2572e53d81eeSPaul Saab 
2573e53d81eeSPaul Saab 		v = CSR_READ_4(sc, 0x7c00);
2574e53d81eeSPaul Saab 		CSR_WRITE_4(sc, 0x7c00, v | (1<<25));
2575e53d81eeSPaul Saab 	}
257695d67482SBill Paul 	DELAY(10000);
25778cb1383cSDoug Ambrisko 
25788cb1383cSDoug Ambrisko 	return(0);
257995d67482SBill Paul }
258095d67482SBill Paul 
258195d67482SBill Paul /*
258295d67482SBill Paul  * Frame reception handling. This is called if there's a frame
258395d67482SBill Paul  * on the receive return list.
258495d67482SBill Paul  *
258595d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
25861be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
258795d67482SBill Paul  * 2) the frame is from the standard receive ring
258895d67482SBill Paul  */
258995d67482SBill Paul 
259095d67482SBill Paul static void
25913f74909aSGleb Smirnoff bge_rxeof(struct bge_softc *sc)
259295d67482SBill Paul {
259395d67482SBill Paul 	struct ifnet *ifp;
259495d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
259595d67482SBill Paul 
25960f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
25970f9bd73bSSam Leffler 
25983f74909aSGleb Smirnoff 	/* Nothing to do. */
2599cfcb5025SOleg Bulyzhin 	if (sc->bge_rx_saved_considx ==
2600cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx)
2601cfcb5025SOleg Bulyzhin 		return;
2602cfcb5025SOleg Bulyzhin 
2603fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
260495d67482SBill Paul 
2605f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2606e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
2607f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2608f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
26094c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
2610f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
26114c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTREAD);
2612f41ac2beSBill Paul 
261395d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2614f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
261595d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
26163f74909aSGleb Smirnoff 		uint32_t		rxidx;
261795d67482SBill Paul 		struct mbuf		*m = NULL;
26183f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
261995d67482SBill Paul 		int			have_tag = 0;
262095d67482SBill Paul 
262175719184SGleb Smirnoff #ifdef DEVICE_POLLING
262275719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
262375719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
262475719184SGleb Smirnoff 				break;
262575719184SGleb Smirnoff 			sc->rxcycles--;
262675719184SGleb Smirnoff 		}
262775719184SGleb Smirnoff #endif
262875719184SGleb Smirnoff 
262995d67482SBill Paul 		cur_rx =
2630f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
263195d67482SBill Paul 
263295d67482SBill Paul 		rxidx = cur_rx->bge_idx;
26330434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
263495d67482SBill Paul 
263595d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
263695d67482SBill Paul 			have_tag = 1;
263795d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
263895d67482SBill Paul 		}
263995d67482SBill Paul 
264095d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
264195d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2642f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2643f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2644f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2645f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2646f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
264795d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
264895d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
264995d67482SBill Paul 			jumbocnt++;
265095d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
265195d67482SBill Paul 				ifp->if_ierrors++;
265295d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
265395d67482SBill Paul 				continue;
265495d67482SBill Paul 			}
265595d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
265695d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
265795d67482SBill Paul 				ifp->if_ierrors++;
265895d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
265995d67482SBill Paul 				continue;
266095d67482SBill Paul 			}
266195d67482SBill Paul 		} else {
266295d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2663f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2664f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2665f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2666f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2667f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
266895d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
266995d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
267095d67482SBill Paul 			stdcnt++;
267195d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
267295d67482SBill Paul 				ifp->if_ierrors++;
267395d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
267495d67482SBill Paul 				continue;
267595d67482SBill Paul 			}
267695d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
267795d67482SBill Paul 			    NULL) == ENOBUFS) {
267895d67482SBill Paul 				ifp->if_ierrors++;
267995d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
268095d67482SBill Paul 				continue;
268195d67482SBill Paul 			}
268295d67482SBill Paul 		}
268395d67482SBill Paul 
268495d67482SBill Paul 		ifp->if_ipackets++;
2685e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2686e255b776SJohn Polstra 		/*
2687e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
2688e65bed95SPyun YongHyeon 		 * the payload is aligned.
2689e255b776SJohn Polstra 		 */
2690652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
2691e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2692e255b776SJohn Polstra 			    cur_rx->bge_len);
2693e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2694e255b776SJohn Polstra 		}
2695e255b776SJohn Polstra #endif
2696473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
269795d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
269895d67482SBill Paul 
2699b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
270078178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
270195d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
270295d67482SBill Paul 				if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
270395d67482SBill Paul 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
270478178cd1SGleb Smirnoff 			}
2705d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
2706d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
270795d67482SBill Paul 				m->m_pkthdr.csum_data =
270895d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
2709ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
2710ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
271195d67482SBill Paul 			}
271295d67482SBill Paul 		}
271395d67482SBill Paul 
271495d67482SBill Paul 		/*
2715673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
2716673d9191SSam Leffler 		 * attach that information to the packet.
271795d67482SBill Paul 		 */
2718d147662cSGleb Smirnoff 		if (have_tag) {
271978ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
272078ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
2721d147662cSGleb Smirnoff 		}
272295d67482SBill Paul 
27230f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
2724673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
27250f9bd73bSSam Leffler 		BGE_LOCK(sc);
272695d67482SBill Paul 	}
272795d67482SBill Paul 
2728e65bed95SPyun YongHyeon 	if (stdcnt > 0)
2729f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2730e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
27314c0da0ffSGleb Smirnoff 
27324c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc) && jumbocnt > 0)
2733f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
27344c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
2735f41ac2beSBill Paul 
273695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
273795d67482SBill Paul 	if (stdcnt)
273895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
273995d67482SBill Paul 	if (jumbocnt)
274095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
274195d67482SBill Paul }
274295d67482SBill Paul 
274395d67482SBill Paul static void
27443f74909aSGleb Smirnoff bge_txeof(struct bge_softc *sc)
274595d67482SBill Paul {
274695d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
274795d67482SBill Paul 	struct ifnet *ifp;
274895d67482SBill Paul 
27490f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
27500f9bd73bSSam Leffler 
27513f74909aSGleb Smirnoff 	/* Nothing to do. */
2752cfcb5025SOleg Bulyzhin 	if (sc->bge_tx_saved_considx ==
2753cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx)
2754cfcb5025SOleg Bulyzhin 		return;
2755cfcb5025SOleg Bulyzhin 
2756fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
275795d67482SBill Paul 
2758e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
2759e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
2760e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
276195d67482SBill Paul 	/*
276295d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
276395d67482SBill Paul 	 * frames that have been sent.
276495d67482SBill Paul 	 */
276595d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
2766f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
27673f74909aSGleb Smirnoff 		uint32_t		idx = 0;
276895d67482SBill Paul 
276995d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
2770f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
277195d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
277295d67482SBill Paul 			ifp->if_opackets++;
277395d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
2774e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2775e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
2776e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
2777f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2778f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
2779e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
2780e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
278195d67482SBill Paul 		}
278295d67482SBill Paul 		sc->bge_txcnt--;
278395d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
278495d67482SBill Paul 		ifp->if_timer = 0;
278595d67482SBill Paul 	}
278695d67482SBill Paul 
278795d67482SBill Paul 	if (cur_tx != NULL)
278813f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
278995d67482SBill Paul }
279095d67482SBill Paul 
279175719184SGleb Smirnoff #ifdef DEVICE_POLLING
279275719184SGleb Smirnoff static void
279375719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
279475719184SGleb Smirnoff {
279575719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
2796366454f2SOleg Bulyzhin 	uint32_t statusword;
279775719184SGleb Smirnoff 
27983f74909aSGleb Smirnoff 	BGE_LOCK(sc);
27993f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
28003f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
28013f74909aSGleb Smirnoff 		return;
28023f74909aSGleb Smirnoff 	}
280375719184SGleb Smirnoff 
2804dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2805e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2806dab5cd05SOleg Bulyzhin 
28073f74909aSGleb Smirnoff 	statusword = atomic_readandclear_32(
28083f74909aSGleb Smirnoff 	    &sc->bge_ldata.bge_status_block->bge_status);
2809dab5cd05SOleg Bulyzhin 
2810dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2811e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2812366454f2SOleg Bulyzhin 
2813366454f2SOleg Bulyzhin 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS cmd */
2814366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
2815366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
2816366454f2SOleg Bulyzhin 
2817366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
2818366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
28194c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
2820652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
2821366454f2SOleg Bulyzhin 			bge_link_upd(sc);
2822366454f2SOleg Bulyzhin 
2823366454f2SOleg Bulyzhin 	sc->rxcycles = count;
2824366454f2SOleg Bulyzhin 	bge_rxeof(sc);
2825366454f2SOleg Bulyzhin 	bge_txeof(sc);
2826366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2827366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
28283f74909aSGleb Smirnoff 
28293f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
283075719184SGleb Smirnoff }
283175719184SGleb Smirnoff #endif /* DEVICE_POLLING */
283275719184SGleb Smirnoff 
283395d67482SBill Paul static void
28343f74909aSGleb Smirnoff bge_intr(void *xsc)
283595d67482SBill Paul {
283695d67482SBill Paul 	struct bge_softc *sc;
283795d67482SBill Paul 	struct ifnet *ifp;
2838dab5cd05SOleg Bulyzhin 	uint32_t statusword;
283995d67482SBill Paul 
284095d67482SBill Paul 	sc = xsc;
2841f41ac2beSBill Paul 
28420f9bd73bSSam Leffler 	BGE_LOCK(sc);
28430f9bd73bSSam Leffler 
2844dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
2845dab5cd05SOleg Bulyzhin 
284675719184SGleb Smirnoff #ifdef DEVICE_POLLING
284775719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
284875719184SGleb Smirnoff 		BGE_UNLOCK(sc);
284975719184SGleb Smirnoff 		return;
285075719184SGleb Smirnoff 	}
285175719184SGleb Smirnoff #endif
285275719184SGleb Smirnoff 
2853f30cbfc6SScott Long 	/*
2854f30cbfc6SScott Long 	 * Do the mandatory PCI flush as well as get the link status.
2855f30cbfc6SScott Long 	 */
2856f30cbfc6SScott Long 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
2857f41ac2beSBill Paul 
285895d67482SBill Paul 	/* Ack interrupt and stop others from occuring. */
285995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
286095d67482SBill Paul 
2861f30cbfc6SScott Long 	/* Make sure the descriptor ring indexes are coherent. */
2862f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2863f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2864f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2865f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2866f30cbfc6SScott Long 
28671f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
28684c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
2869f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
2870dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
287195d67482SBill Paul 
287213f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
28733f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
287495d67482SBill Paul 		bge_rxeof(sc);
287595d67482SBill Paul 
28763f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
287795d67482SBill Paul 		bge_txeof(sc);
287895d67482SBill Paul 	}
287995d67482SBill Paul 
288095d67482SBill Paul 	/* Re-enable interrupts. */
288195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
288295d67482SBill Paul 
288313f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
288413f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
28850f9bd73bSSam Leffler 		bge_start_locked(ifp);
28860f9bd73bSSam Leffler 
28870f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
288895d67482SBill Paul }
288995d67482SBill Paul 
289095d67482SBill Paul static void
28918cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
28928cb1383cSDoug Ambrisko {
28938cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
28948cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
28958cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
28968cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
28978cb1383cSDoug Ambrisko 		else {
28988cb1383cSDoug Ambrisko 			sc->bge_asf_count = 5;
28998cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW,
29008cb1383cSDoug Ambrisko 			    BGE_FW_DRV_ALIVE);
29018cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_LEN, 4);
29028cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_DATA, 3);
29038cb1383cSDoug Ambrisko 			CSR_WRITE_4(sc, BGE_CPU_EVENT,
29048cb1383cSDoug Ambrisko 			    CSR_READ_4(sc, BGE_CPU_EVENT) != (1 << 14));
29058cb1383cSDoug Ambrisko 		}
29068cb1383cSDoug Ambrisko 	}
29078cb1383cSDoug Ambrisko }
29088cb1383cSDoug Ambrisko 
29098cb1383cSDoug Ambrisko static void
29103f74909aSGleb Smirnoff bge_tick_locked(struct bge_softc *sc)
29110f9bd73bSSam Leffler {
291295d67482SBill Paul 	struct mii_data *mii = NULL;
291395d67482SBill Paul 
29140f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
291595d67482SBill Paul 
29164c0da0ffSGleb Smirnoff 	if (BGE_IS_5705_OR_BEYOND(sc))
29170434d1b8SBill Paul 		bge_stats_update_regs(sc);
29180434d1b8SBill Paul 	else
291995d67482SBill Paul 		bge_stats_update(sc);
292095d67482SBill Paul 
2921652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
292295d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
29238cb1383cSDoug Ambrisko 		/* Don't mess with the PHY in IPMI/ASF mode */
29248cb1383cSDoug Ambrisko 		if (!((sc->bge_asf_mode & ASF_STACKUP) && (sc->bge_link)))
292595d67482SBill Paul 			mii_tick(mii);
29267b97099dSOleg Bulyzhin 	} else {
29277b97099dSOleg Bulyzhin 		/*
29287b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
29297b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
29307b97099dSOleg Bulyzhin 		 * and trigger interrupt.
29317b97099dSOleg Bulyzhin 		 */
29327b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
29333f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
29347b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
29357b97099dSOleg Bulyzhin #endif
29367b97099dSOleg Bulyzhin 		{
29377b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
29387b97099dSOleg Bulyzhin 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
29397b97099dSOleg Bulyzhin 		}
2940dab5cd05SOleg Bulyzhin 	}
294195d67482SBill Paul 
29428cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
29438cb1383cSDoug Ambrisko 
2944dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
294595d67482SBill Paul }
294695d67482SBill Paul 
294795d67482SBill Paul static void
29483f74909aSGleb Smirnoff bge_tick(void *xsc)
29490f9bd73bSSam Leffler {
29500f9bd73bSSam Leffler 	struct bge_softc *sc;
29510f9bd73bSSam Leffler 
29520f9bd73bSSam Leffler 	sc = xsc;
29530f9bd73bSSam Leffler 
29540f9bd73bSSam Leffler 	BGE_LOCK(sc);
29550f9bd73bSSam Leffler 	bge_tick_locked(sc);
29560f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
29570f9bd73bSSam Leffler }
29580f9bd73bSSam Leffler 
29590f9bd73bSSam Leffler static void
29603f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
29610434d1b8SBill Paul {
29620434d1b8SBill Paul 	struct bge_mac_stats_regs stats;
29633f74909aSGleb Smirnoff 	struct ifnet *ifp;
29643f74909aSGleb Smirnoff 	uint32_t *s;
29656fb34dd2SOleg Bulyzhin 	u_long cnt;	/* current register value */
29660434d1b8SBill Paul 	int i;
29670434d1b8SBill Paul 
2968fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
29690434d1b8SBill Paul 
29703f74909aSGleb Smirnoff 	s = (uint32_t *)&stats;
29710434d1b8SBill Paul 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
29720434d1b8SBill Paul 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
29730434d1b8SBill Paul 		s++;
29740434d1b8SBill Paul 	}
29750434d1b8SBill Paul 
29766fb34dd2SOleg Bulyzhin 	cnt = stats.dot3StatsSingleCollisionFrames +
29770434d1b8SBill Paul 	    stats.dot3StatsMultipleCollisionFrames +
29780434d1b8SBill Paul 	    stats.dot3StatsExcessiveCollisions +
29796fb34dd2SOleg Bulyzhin 	    stats.dot3StatsLateCollisions;
29806fb34dd2SOleg Bulyzhin 	ifp->if_collisions += cnt >= sc->bge_tx_collisions ?
29816fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_collisions : cnt;
29826fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
29830434d1b8SBill Paul }
29840434d1b8SBill Paul 
29850434d1b8SBill Paul static void
29863f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
298795d67482SBill Paul {
298895d67482SBill Paul 	struct ifnet *ifp;
2989e907febfSPyun YongHyeon 	bus_size_t stats;
29906fb34dd2SOleg Bulyzhin 	u_long cnt;	/* current register value */
299195d67482SBill Paul 
2992fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
299395d67482SBill Paul 
2994e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
2995e907febfSPyun YongHyeon 
2996e907febfSPyun YongHyeon #define READ_STAT(sc, stats, stat) \
2997e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
299895d67482SBill Paul 
29996fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats,
30006fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsSingleCollisionFrames.bge_addr_lo);
30016fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
30026fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsMultipleCollisionFrames.bge_addr_lo);
30036fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
30046fb34dd2SOleg Bulyzhin 	    txstats.dot3StatsExcessiveCollisions.bge_addr_lo);
30056fb34dd2SOleg Bulyzhin 	cnt += READ_STAT(sc, stats,
30066fb34dd2SOleg Bulyzhin 		txstats.dot3StatsLateCollisions.bge_addr_lo);
30076fb34dd2SOleg Bulyzhin 	ifp->if_collisions += cnt >= sc->bge_tx_collisions ?
30086fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_collisions : cnt;
30096fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
30106fb34dd2SOleg Bulyzhin 
30116fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
30126fb34dd2SOleg Bulyzhin 	ifp->if_ierrors += cnt >= sc->bge_rx_discards ?
30136fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_rx_discards : cnt;
30146fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
30156fb34dd2SOleg Bulyzhin 
30166fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
30176fb34dd2SOleg Bulyzhin 	ifp->if_oerrors += cnt >= sc->bge_tx_discards ?
30186fb34dd2SOleg Bulyzhin 	    cnt - sc->bge_tx_discards : cnt;
30196fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
302095d67482SBill Paul 
3021e907febfSPyun YongHyeon #undef READ_STAT
302295d67482SBill Paul }
302395d67482SBill Paul 
302495d67482SBill Paul /*
3025d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
3026d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
3027d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
3028d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
3029d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
3030d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
3031d375e524SGleb Smirnoff  */
3032d375e524SGleb Smirnoff static __inline int
3033d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
3034d375e524SGleb Smirnoff {
3035d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
3036d375e524SGleb Smirnoff 	struct mbuf *last;
3037d375e524SGleb Smirnoff 
3038d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
3039d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
3040d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
3041d375e524SGleb Smirnoff 		last = m;
3042d375e524SGleb Smirnoff 	} else {
3043d375e524SGleb Smirnoff 		/*
3044d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
3045d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
3046d375e524SGleb Smirnoff 		 */
3047d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
3048d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
3049d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
3050d375e524SGleb Smirnoff 			struct mbuf *n;
3051d375e524SGleb Smirnoff 
3052d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
3053d375e524SGleb Smirnoff 			if (n == NULL)
3054d375e524SGleb Smirnoff 				return (ENOBUFS);
3055d375e524SGleb Smirnoff 			n->m_len = 0;
3056d375e524SGleb Smirnoff 			last->m_next = n;
3057d375e524SGleb Smirnoff 			last = n;
3058d375e524SGleb Smirnoff 		}
3059d375e524SGleb Smirnoff 	}
3060d375e524SGleb Smirnoff 
3061d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
3062d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
3063d375e524SGleb Smirnoff 	last->m_len += padlen;
3064d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
3065d375e524SGleb Smirnoff 
3066d375e524SGleb Smirnoff 	return (0);
3067d375e524SGleb Smirnoff }
3068d375e524SGleb Smirnoff 
3069d375e524SGleb Smirnoff /*
307095d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
307195d67482SBill Paul  * pointers to descriptors.
307295d67482SBill Paul  */
307395d67482SBill Paul static int
3074676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
307595d67482SBill Paul {
30767e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
3077f41ac2beSBill Paul 	bus_dmamap_t		map;
3078676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
3079676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
30807e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
3081676ad2c9SGleb Smirnoff 	uint16_t		csum_flags;
30827e27542aSGleb Smirnoff 	int			nsegs, i, error;
308395d67482SBill Paul 
30846909dc43SGleb Smirnoff 	csum_flags = 0;
30856909dc43SGleb Smirnoff 	if (m->m_pkthdr.csum_flags) {
30866909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
30876909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
30886909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
30896909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
30906909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
30916909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
30926909dc43SGleb Smirnoff 				m_freem(m);
30936909dc43SGleb Smirnoff 				*m_head = NULL;
30946909dc43SGleb Smirnoff 				return (error);
30956909dc43SGleb Smirnoff 			}
30966909dc43SGleb Smirnoff 		}
30976909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
30986909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
30996909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
31006909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
31016909dc43SGleb Smirnoff 	}
31026909dc43SGleb Smirnoff 
31037e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
3104676ad2c9SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m, segs,
3105676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
31067e27542aSGleb Smirnoff 	if (error == EFBIG) {
3107676ad2c9SGleb Smirnoff 		m = m_defrag(m, M_DONTWAIT);
3108676ad2c9SGleb Smirnoff 		if (m == NULL) {
3109676ad2c9SGleb Smirnoff 			m_freem(*m_head);
3110676ad2c9SGleb Smirnoff 			*m_head = NULL;
31117e27542aSGleb Smirnoff 			return (ENOBUFS);
31127e27542aSGleb Smirnoff 		}
3113676ad2c9SGleb Smirnoff 		*m_head = m;
3114676ad2c9SGleb Smirnoff 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m,
3115676ad2c9SGleb Smirnoff 		    segs, &nsegs, BUS_DMA_NOWAIT);
3116676ad2c9SGleb Smirnoff 		if (error) {
3117676ad2c9SGleb Smirnoff 			m_freem(m);
3118676ad2c9SGleb Smirnoff 			*m_head = NULL;
31197e27542aSGleb Smirnoff 			return (error);
31207e27542aSGleb Smirnoff 		}
3121676ad2c9SGleb Smirnoff 	} else if (error != 0)
3122676ad2c9SGleb Smirnoff 		return (error);
31237e27542aSGleb Smirnoff 
312495d67482SBill Paul 	/*
312595d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
312695d67482SBill Paul 	 * of the end of the ring.
312795d67482SBill Paul 	 */
31287e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
31297e27542aSGleb Smirnoff 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
313095d67482SBill Paul 		return (ENOBUFS);
31317e27542aSGleb Smirnoff 	}
31327e27542aSGleb Smirnoff 
3133e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
3134e65bed95SPyun YongHyeon 
31357e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
31367e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
31377e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
31387e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
31397e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
31407e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
31417e27542aSGleb Smirnoff 		if (i == nsegs - 1)
31427e27542aSGleb Smirnoff 			break;
31437e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
31447e27542aSGleb Smirnoff 	}
31457e27542aSGleb Smirnoff 
31467e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
31477e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
3148676ad2c9SGleb Smirnoff 
31497e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
31507e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
315178ba57b9SAndre Oppermann 	if (m->m_flags & M_VLANTAG) {
31527e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
315378ba57b9SAndre Oppermann 		d->bge_vlan_tag = m->m_pkthdr.ether_vtag;
31547e27542aSGleb Smirnoff 	} else
31557e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
3156f41ac2beSBill Paul 
3157f41ac2beSBill Paul 	/*
3158f41ac2beSBill Paul 	 * Insure that the map for this transmission
3159f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
3160f41ac2beSBill Paul 	 * in this chain.
3161f41ac2beSBill Paul 	 */
31627e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
31637e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
3164676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
31657e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
316695d67482SBill Paul 
31677e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
31687e27542aSGleb Smirnoff 	*txidx = idx;
316995d67482SBill Paul 
317095d67482SBill Paul 	return (0);
317195d67482SBill Paul }
317295d67482SBill Paul 
317395d67482SBill Paul /*
317495d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
317595d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
317695d67482SBill Paul  */
317795d67482SBill Paul static void
31783f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
317995d67482SBill Paul {
318095d67482SBill Paul 	struct bge_softc *sc;
318195d67482SBill Paul 	struct mbuf *m_head = NULL;
318214bbd30fSGleb Smirnoff 	uint32_t prodidx;
3183303a718cSDag-Erling Smørgrav 	int count = 0;
318495d67482SBill Paul 
318595d67482SBill Paul 	sc = ifp->if_softc;
318695d67482SBill Paul 
3187dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
318895d67482SBill Paul 		return;
318995d67482SBill Paul 
319014bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
319195d67482SBill Paul 
319295d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
31934d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
319495d67482SBill Paul 		if (m_head == NULL)
319595d67482SBill Paul 			break;
319695d67482SBill Paul 
319795d67482SBill Paul 		/*
319895d67482SBill Paul 		 * XXX
3199b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
3200b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3201b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
3202b874fdd4SYaroslav Tykhiy 		 *
3203b874fdd4SYaroslav Tykhiy 		 * XXX
320495d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
320595d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
320695d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
320795d67482SBill Paul 		 * chain at once.
320895d67482SBill Paul 		 * (paranoia -- may not actually be needed)
320995d67482SBill Paul 		 */
321095d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
321195d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
321295d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
321395d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
32144d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
321513f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
321695d67482SBill Paul 				break;
321795d67482SBill Paul 			}
321895d67482SBill Paul 		}
321995d67482SBill Paul 
322095d67482SBill Paul 		/*
322195d67482SBill Paul 		 * Pack the data into the transmit ring. If we
322295d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
322395d67482SBill Paul 		 * for the NIC to drain the ring.
322495d67482SBill Paul 		 */
3225676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
3226676ad2c9SGleb Smirnoff 			if (m_head == NULL)
3227676ad2c9SGleb Smirnoff 				break;
32284d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
322913f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
323095d67482SBill Paul 			break;
323195d67482SBill Paul 		}
3232303a718cSDag-Erling Smørgrav 		++count;
323395d67482SBill Paul 
323495d67482SBill Paul 		/*
323595d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
323695d67482SBill Paul 		 * to him.
323795d67482SBill Paul 		 */
3238673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
323995d67482SBill Paul 	}
324095d67482SBill Paul 
32413f74909aSGleb Smirnoff 	if (count == 0)
32423f74909aSGleb Smirnoff 		/* No packets were dequeued. */
3243303a718cSDag-Erling Smørgrav 		return;
3244303a718cSDag-Erling Smørgrav 
32453f74909aSGleb Smirnoff 	/* Transmit. */
324695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
32473927098fSPaul Saab 	/* 5700 b2 errata */
3248e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
32493927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
325095d67482SBill Paul 
325114bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
325214bbd30fSGleb Smirnoff 
325395d67482SBill Paul 	/*
325495d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
325595d67482SBill Paul 	 */
325695d67482SBill Paul 	ifp->if_timer = 5;
325795d67482SBill Paul }
325895d67482SBill Paul 
32590f9bd73bSSam Leffler /*
32600f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
32610f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
32620f9bd73bSSam Leffler  */
326395d67482SBill Paul static void
32643f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
326595d67482SBill Paul {
32660f9bd73bSSam Leffler 	struct bge_softc *sc;
32670f9bd73bSSam Leffler 
32680f9bd73bSSam Leffler 	sc = ifp->if_softc;
32690f9bd73bSSam Leffler 	BGE_LOCK(sc);
32700f9bd73bSSam Leffler 	bge_start_locked(ifp);
32710f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
32720f9bd73bSSam Leffler }
32730f9bd73bSSam Leffler 
32740f9bd73bSSam Leffler static void
32753f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
32760f9bd73bSSam Leffler {
327795d67482SBill Paul 	struct ifnet *ifp;
32783f74909aSGleb Smirnoff 	uint16_t *m;
327995d67482SBill Paul 
32800f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
328195d67482SBill Paul 
3282fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
328395d67482SBill Paul 
328413f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
328595d67482SBill Paul 		return;
328695d67482SBill Paul 
328795d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
328895d67482SBill Paul 	bge_stop(sc);
32898cb1383cSDoug Ambrisko 
32908cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
32918cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
329295d67482SBill Paul 	bge_reset(sc);
32938cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
32948cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
32958cb1383cSDoug Ambrisko 
329695d67482SBill Paul 	bge_chipinit(sc);
329795d67482SBill Paul 
329895d67482SBill Paul 	/*
329995d67482SBill Paul 	 * Init the various state machines, ring
330095d67482SBill Paul 	 * control blocks and firmware.
330195d67482SBill Paul 	 */
330295d67482SBill Paul 	if (bge_blockinit(sc)) {
3303fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
330495d67482SBill Paul 		return;
330595d67482SBill Paul 	}
330695d67482SBill Paul 
3307fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
330895d67482SBill Paul 
330995d67482SBill Paul 	/* Specify MTU. */
331095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3311859c6c7dSBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
331295d67482SBill Paul 
331395d67482SBill Paul 	/* Load our MAC address. */
33143f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
331595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
331695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
331795d67482SBill Paul 
331895d67482SBill Paul 	/* Enable or disable promiscuous mode as needed. */
331995d67482SBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
332095d67482SBill Paul 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
332195d67482SBill Paul 	} else {
332295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
332395d67482SBill Paul 	}
332495d67482SBill Paul 
332595d67482SBill Paul 	/* Program multicast filter. */
332695d67482SBill Paul 	bge_setmulti(sc);
332795d67482SBill Paul 
332895d67482SBill Paul 	/* Init RX ring. */
332995d67482SBill Paul 	bge_init_rx_ring_std(sc);
333095d67482SBill Paul 
33310434d1b8SBill Paul 	/*
33320434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
33330434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
33340434d1b8SBill Paul 	 * entry of the ring.
33350434d1b8SBill Paul 	 */
33360434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
33373f74909aSGleb Smirnoff 		uint32_t		v, i;
33380434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
33390434d1b8SBill Paul 			DELAY(20);
33400434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
33410434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
33420434d1b8SBill Paul 				break;
33430434d1b8SBill Paul 		}
33440434d1b8SBill Paul 		if (i == 10)
3345fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
3346fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
33470434d1b8SBill Paul 	}
33480434d1b8SBill Paul 
334995d67482SBill Paul 	/* Init jumbo RX ring. */
335095d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
335195d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
335295d67482SBill Paul 
33533f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
335495d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
335595d67482SBill Paul 
335695d67482SBill Paul 	/* Init TX ring. */
335795d67482SBill Paul 	bge_init_tx_ring(sc);
335895d67482SBill Paul 
33593f74909aSGleb Smirnoff 	/* Turn on transmitter. */
336095d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
336195d67482SBill Paul 
33623f74909aSGleb Smirnoff 	/* Turn on receiver. */
336395d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
336495d67482SBill Paul 
336595d67482SBill Paul 	/* Tell firmware we're alive. */
336695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
336795d67482SBill Paul 
336875719184SGleb Smirnoff #ifdef DEVICE_POLLING
336975719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
337075719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
337175719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
337275719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
337375719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
337475719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
337575719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
337675719184SGleb Smirnoff 	} else
337775719184SGleb Smirnoff #endif
337875719184SGleb Smirnoff 
337995d67482SBill Paul 	/* Enable host interrupts. */
338075719184SGleb Smirnoff 	{
338195d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
338295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
338395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
338475719184SGleb Smirnoff 	}
338595d67482SBill Paul 
338667d5e043SOleg Bulyzhin 	bge_ifmedia_upd_locked(ifp);
338795d67482SBill Paul 
338813f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
338913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
339095d67482SBill Paul 
33910f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
33920f9bd73bSSam Leffler }
33930f9bd73bSSam Leffler 
33940f9bd73bSSam Leffler static void
33953f74909aSGleb Smirnoff bge_init(void *xsc)
33960f9bd73bSSam Leffler {
33970f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
33980f9bd73bSSam Leffler 
33990f9bd73bSSam Leffler 	BGE_LOCK(sc);
34000f9bd73bSSam Leffler 	bge_init_locked(sc);
34010f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
340295d67482SBill Paul }
340395d67482SBill Paul 
340495d67482SBill Paul /*
340595d67482SBill Paul  * Set media options.
340695d67482SBill Paul  */
340795d67482SBill Paul static int
34083f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
340995d67482SBill Paul {
341067d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
341167d5e043SOleg Bulyzhin 	int res;
341267d5e043SOleg Bulyzhin 
341367d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
341467d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
341567d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
341667d5e043SOleg Bulyzhin 
341767d5e043SOleg Bulyzhin 	return (res);
341867d5e043SOleg Bulyzhin }
341967d5e043SOleg Bulyzhin 
342067d5e043SOleg Bulyzhin static int
342167d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
342267d5e043SOleg Bulyzhin {
342367d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
342495d67482SBill Paul 	struct mii_data *mii;
342595d67482SBill Paul 	struct ifmedia *ifm;
342695d67482SBill Paul 
342767d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
342867d5e043SOleg Bulyzhin 
342995d67482SBill Paul 	ifm = &sc->bge_ifmedia;
343095d67482SBill Paul 
343195d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
3432652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
343395d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
343495d67482SBill Paul 			return (EINVAL);
343595d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
343695d67482SBill Paul 		case IFM_AUTO:
3437ff50922bSDoug White 			/*
3438ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
3439ff50922bSDoug White 			 * mechanism for programming the autoneg
3440ff50922bSDoug White 			 * advertisement registers in TBI mode.
3441ff50922bSDoug White 			 */
3442c4529f41SMichael Reifenberger 			if (bge_fake_autoneg == 0 &&
3443c4529f41SMichael Reifenberger 			    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3444ff50922bSDoug White 				uint32_t sgdig;
3445ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3446ff50922bSDoug White 				sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3447ff50922bSDoug White 				sgdig |= BGE_SGDIGCFG_AUTO|
3448ff50922bSDoug White 				    BGE_SGDIGCFG_PAUSE_CAP|
3449ff50922bSDoug White 				    BGE_SGDIGCFG_ASYM_PAUSE;
3450ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3451ff50922bSDoug White 				    sgdig|BGE_SGDIGCFG_SEND);
3452ff50922bSDoug White 				DELAY(5);
3453ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3454ff50922bSDoug White 			}
345595d67482SBill Paul 			break;
345695d67482SBill Paul 		case IFM_1000_SX:
345795d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
345895d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
345995d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
346095d67482SBill Paul 			} else {
346195d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
346295d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
346395d67482SBill Paul 			}
346495d67482SBill Paul 			break;
346595d67482SBill Paul 		default:
346695d67482SBill Paul 			return (EINVAL);
346795d67482SBill Paul 		}
346895d67482SBill Paul 		return (0);
346995d67482SBill Paul 	}
347095d67482SBill Paul 
34711493e883SOleg Bulyzhin 	sc->bge_link_evt++;
347295d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
347395d67482SBill Paul 	if (mii->mii_instance) {
347495d67482SBill Paul 		struct mii_softc *miisc;
347595d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
347695d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
347795d67482SBill Paul 			mii_phy_reset(miisc);
347895d67482SBill Paul 	}
347995d67482SBill Paul 	mii_mediachg(mii);
348095d67482SBill Paul 
348195d67482SBill Paul 	return (0);
348295d67482SBill Paul }
348395d67482SBill Paul 
348495d67482SBill Paul /*
348595d67482SBill Paul  * Report current media status.
348695d67482SBill Paul  */
348795d67482SBill Paul static void
34883f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
348995d67482SBill Paul {
349067d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
349195d67482SBill Paul 	struct mii_data *mii;
349295d67482SBill Paul 
349367d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
349495d67482SBill Paul 
3495652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
349695d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
349795d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
349895d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
349995d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
350095d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
35014c0da0ffSGleb Smirnoff 		else {
35024c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
350367d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
35044c0da0ffSGleb Smirnoff 			return;
35054c0da0ffSGleb Smirnoff 		}
350695d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
350795d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
350895d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
350995d67482SBill Paul 		else
351095d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
351167d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
351295d67482SBill Paul 		return;
351395d67482SBill Paul 	}
351495d67482SBill Paul 
351595d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
351695d67482SBill Paul 	mii_pollstat(mii);
351795d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
351895d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
351967d5e043SOleg Bulyzhin 
352067d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
352195d67482SBill Paul }
352295d67482SBill Paul 
352395d67482SBill Paul static int
35243f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
352595d67482SBill Paul {
352695d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
352795d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
352895d67482SBill Paul 	struct mii_data *mii;
3529f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
353095d67482SBill Paul 
353195d67482SBill Paul 	switch (command) {
353295d67482SBill Paul 	case SIOCSIFMTU:
35334c0da0ffSGleb Smirnoff 		if (ifr->ifr_mtu < ETHERMIN ||
35344c0da0ffSGleb Smirnoff 		    ((BGE_IS_JUMBO_CAPABLE(sc)) &&
35354c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > BGE_JUMBO_MTU) ||
35364c0da0ffSGleb Smirnoff 		    ((!BGE_IS_JUMBO_CAPABLE(sc)) &&
35374c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > ETHERMTU))
353895d67482SBill Paul 			error = EINVAL;
35394c0da0ffSGleb Smirnoff 		else if (ifp->if_mtu != ifr->ifr_mtu) {
354095d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
354113f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
354295d67482SBill Paul 			bge_init(sc);
354395d67482SBill Paul 		}
354495d67482SBill Paul 		break;
354595d67482SBill Paul 	case SIOCSIFFLAGS:
35460f9bd73bSSam Leffler 		BGE_LOCK(sc);
354795d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
354895d67482SBill Paul 			/*
354995d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
355095d67482SBill Paul 			 * then just use the 'set promisc mode' command
355195d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
355295d67482SBill Paul 			 * a full re-init means reloading the firmware and
355395d67482SBill Paul 			 * waiting for it to start up, which may take a
3554d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
355595d67482SBill Paul 			 */
3556f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3557f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
3558f9004b6dSJung-uk Kim 				if (flags & IFF_PROMISC) {
3559f9004b6dSJung-uk Kim 					if (ifp->if_flags & IFF_PROMISC)
356095d67482SBill Paul 						BGE_SETBIT(sc, BGE_RX_MODE,
356195d67482SBill Paul 						    BGE_RXMODE_RX_PROMISC);
3562f9004b6dSJung-uk Kim 					else
356395d67482SBill Paul 						BGE_CLRBIT(sc, BGE_RX_MODE,
356495d67482SBill Paul 						    BGE_RXMODE_RX_PROMISC);
3565f9004b6dSJung-uk Kim 				}
3566f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
3567d183af7fSRuslan Ermilov 					bge_setmulti(sc);
356895d67482SBill Paul 			} else
35690f9bd73bSSam Leffler 				bge_init_locked(sc);
357095d67482SBill Paul 		} else {
357113f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
357295d67482SBill Paul 				bge_stop(sc);
357395d67482SBill Paul 			}
357495d67482SBill Paul 		}
357595d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
35760f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
357795d67482SBill Paul 		error = 0;
357895d67482SBill Paul 		break;
357995d67482SBill Paul 	case SIOCADDMULTI:
358095d67482SBill Paul 	case SIOCDELMULTI:
358113f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
35820f9bd73bSSam Leffler 			BGE_LOCK(sc);
358395d67482SBill Paul 			bge_setmulti(sc);
35840f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
358595d67482SBill Paul 			error = 0;
358695d67482SBill Paul 		}
358795d67482SBill Paul 		break;
358895d67482SBill Paul 	case SIOCSIFMEDIA:
358995d67482SBill Paul 	case SIOCGIFMEDIA:
3590652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
359195d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
359295d67482SBill Paul 			    &sc->bge_ifmedia, command);
359395d67482SBill Paul 		} else {
359495d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
359595d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
359695d67482SBill Paul 			    &mii->mii_media, command);
359795d67482SBill Paul 		}
359895d67482SBill Paul 		break;
359995d67482SBill Paul 	case SIOCSIFCAP:
360095d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
360175719184SGleb Smirnoff #ifdef DEVICE_POLLING
360275719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
360375719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
360475719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
360575719184SGleb Smirnoff 				if (error)
360675719184SGleb Smirnoff 					return (error);
360775719184SGleb Smirnoff 				BGE_LOCK(sc);
360875719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
360975719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
361075719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
361175719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
361275719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
361375719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
361475719184SGleb Smirnoff 				BGE_UNLOCK(sc);
361575719184SGleb Smirnoff 			} else {
361675719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
361775719184SGleb Smirnoff 				/* Enable interrupt even in error case */
361875719184SGleb Smirnoff 				BGE_LOCK(sc);
361975719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
362075719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
362175719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
362275719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
362375719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
362475719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
362575719184SGleb Smirnoff 				BGE_UNLOCK(sc);
362675719184SGleb Smirnoff 			}
362775719184SGleb Smirnoff 		}
362875719184SGleb Smirnoff #endif
3629d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
3630d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
3631d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
3632d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
3633b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
363495d67482SBill Paul 			else
3635b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
3636479b23b7SGleb Smirnoff 			VLAN_CAPABILITIES(ifp);
363795d67482SBill Paul 		}
363895d67482SBill Paul 		break;
363995d67482SBill Paul 	default:
3640673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
364195d67482SBill Paul 		break;
364295d67482SBill Paul 	}
364395d67482SBill Paul 
364495d67482SBill Paul 	return (error);
364595d67482SBill Paul }
364695d67482SBill Paul 
364795d67482SBill Paul static void
36483f74909aSGleb Smirnoff bge_watchdog(struct ifnet *ifp)
364995d67482SBill Paul {
365095d67482SBill Paul 	struct bge_softc *sc;
365195d67482SBill Paul 
365295d67482SBill Paul 	sc = ifp->if_softc;
365395d67482SBill Paul 
3654fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
365595d67482SBill Paul 
365613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
365795d67482SBill Paul 	bge_init(sc);
365895d67482SBill Paul 
365995d67482SBill Paul 	ifp->if_oerrors++;
366095d67482SBill Paul }
366195d67482SBill Paul 
366295d67482SBill Paul /*
366395d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
366495d67482SBill Paul  * RX and TX lists.
366595d67482SBill Paul  */
366695d67482SBill Paul static void
36673f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
366895d67482SBill Paul {
366995d67482SBill Paul 	struct ifnet *ifp;
367095d67482SBill Paul 	struct ifmedia_entry *ifm;
367195d67482SBill Paul 	struct mii_data *mii = NULL;
367295d67482SBill Paul 	int mtmp, itmp;
367395d67482SBill Paul 
36740f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
36750f9bd73bSSam Leffler 
3676fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
367795d67482SBill Paul 
3678652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0)
367995d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
368095d67482SBill Paul 
36810f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
368295d67482SBill Paul 
368395d67482SBill Paul 	/*
36843f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
368595d67482SBill Paul 	 */
368695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
368795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
368895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
36894c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
369095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
369195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
369295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
369395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
369495d67482SBill Paul 
369595d67482SBill Paul 	/*
36963f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
369795d67482SBill Paul 	 */
369895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
369995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
370095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
370195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
370295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
37034c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
370495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
370595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
370695d67482SBill Paul 
370795d67482SBill Paul 	/*
370895d67482SBill Paul 	 * Shut down all of the memory managers and related
370995d67482SBill Paul 	 * state machines.
371095d67482SBill Paul 	 */
371195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
371295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
37134c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc)))
371495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
371595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
371695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
37174c0da0ffSGleb Smirnoff 	if (!(BGE_IS_5705_OR_BEYOND(sc))) {
371895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
371995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
37200434d1b8SBill Paul 	}
372195d67482SBill Paul 
372295d67482SBill Paul 	/* Disable host interrupts. */
372395d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
372495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
372595d67482SBill Paul 
372695d67482SBill Paul 	/*
372795d67482SBill Paul 	 * Tell firmware we're shutting down.
372895d67482SBill Paul 	 */
37298cb1383cSDoug Ambrisko 
37308cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
37318cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
37328cb1383cSDoug Ambrisko 	bge_reset(sc);
37338cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
37348cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
37358cb1383cSDoug Ambrisko 
37368cb1383cSDoug Ambrisko 	/*
37378cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
37388cb1383cSDoug Ambrisko 	 */
37398cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
37408cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
37418cb1383cSDoug Ambrisko 	else
374295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
374395d67482SBill Paul 
374495d67482SBill Paul 	/* Free the RX lists. */
374595d67482SBill Paul 	bge_free_rx_ring_std(sc);
374695d67482SBill Paul 
374795d67482SBill Paul 	/* Free jumbo RX list. */
37484c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
374995d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
375095d67482SBill Paul 
375195d67482SBill Paul 	/* Free TX buffers. */
375295d67482SBill Paul 	bge_free_tx_ring(sc);
375395d67482SBill Paul 
375495d67482SBill Paul 	/*
375595d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
375695d67482SBill Paul 	 * unchanged so that things will be put back to normal when
375795d67482SBill Paul 	 * we bring the interface back up.
375895d67482SBill Paul 	 */
3759652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
376095d67482SBill Paul 		itmp = ifp->if_flags;
376195d67482SBill Paul 		ifp->if_flags |= IFF_UP;
3762dcc34049SPawel Jakub Dawidek 		/*
3763dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
3764dcc34049SPawel Jakub Dawidek 		 */
3765dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
376695d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
376795d67482SBill Paul 			mtmp = ifm->ifm_media;
376895d67482SBill Paul 			ifm->ifm_media = IFM_ETHER|IFM_NONE;
376995d67482SBill Paul 			mii_mediachg(mii);
377095d67482SBill Paul 			ifm->ifm_media = mtmp;
3771dcc34049SPawel Jakub Dawidek 		}
377295d67482SBill Paul 		ifp->if_flags = itmp;
377395d67482SBill Paul 	}
377495d67482SBill Paul 
377595d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
377695d67482SBill Paul 
37771493e883SOleg Bulyzhin 	/*
37781493e883SOleg Bulyzhin 	 * We can't just call bge_link_upd() cause chip is almost stopped so
37791493e883SOleg Bulyzhin 	 * bge_link_upd -> bge_tick_locked -> bge_stats_update sequence may
37801493e883SOleg Bulyzhin 	 * lead to hardware deadlock. So we just clearing MAC's link state
37811493e883SOleg Bulyzhin 	 * (PHY may still have link UP).
37821493e883SOleg Bulyzhin 	 */
37831493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
37841493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
37851493e883SOleg Bulyzhin 	sc->bge_link = 0;
378695d67482SBill Paul 
37871493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
378895d67482SBill Paul }
378995d67482SBill Paul 
379095d67482SBill Paul /*
379195d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
379295d67482SBill Paul  * get confused by errant DMAs when rebooting.
379395d67482SBill Paul  */
379495d67482SBill Paul static void
37953f74909aSGleb Smirnoff bge_shutdown(device_t dev)
379695d67482SBill Paul {
379795d67482SBill Paul 	struct bge_softc *sc;
379895d67482SBill Paul 
379995d67482SBill Paul 	sc = device_get_softc(dev);
380095d67482SBill Paul 
38010f9bd73bSSam Leffler 	BGE_LOCK(sc);
380295d67482SBill Paul 	bge_stop(sc);
380395d67482SBill Paul 	bge_reset(sc);
38040f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
380595d67482SBill Paul }
380614afefa3SPawel Jakub Dawidek 
380714afefa3SPawel Jakub Dawidek static int
380814afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
380914afefa3SPawel Jakub Dawidek {
381014afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
381114afefa3SPawel Jakub Dawidek 
381214afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
381314afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
381414afefa3SPawel Jakub Dawidek 	bge_stop(sc);
381514afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
381614afefa3SPawel Jakub Dawidek 
381714afefa3SPawel Jakub Dawidek 	return (0);
381814afefa3SPawel Jakub Dawidek }
381914afefa3SPawel Jakub Dawidek 
382014afefa3SPawel Jakub Dawidek static int
382114afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
382214afefa3SPawel Jakub Dawidek {
382314afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
382414afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
382514afefa3SPawel Jakub Dawidek 
382614afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
382714afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
382814afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
382914afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
383014afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
383114afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
383214afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
383314afefa3SPawel Jakub Dawidek 	}
383414afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
383514afefa3SPawel Jakub Dawidek 
383614afefa3SPawel Jakub Dawidek 	return (0);
383714afefa3SPawel Jakub Dawidek }
3838dab5cd05SOleg Bulyzhin 
3839dab5cd05SOleg Bulyzhin static void
38403f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
3841dab5cd05SOleg Bulyzhin {
38421f313773SOleg Bulyzhin 	struct mii_data *mii;
38431f313773SOleg Bulyzhin 	uint32_t link, status;
3844dab5cd05SOleg Bulyzhin 
3845dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
38461f313773SOleg Bulyzhin 
38473f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
38487b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
38497b97099dSOleg Bulyzhin 
3850dab5cd05SOleg Bulyzhin 	/*
3851dab5cd05SOleg Bulyzhin 	 * Process link state changes.
3852dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
3853dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
3854dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
3855dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
3856dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
3857dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
3858dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
3859dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
38601f313773SOleg Bulyzhin 	 *
38611f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
38624c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
3863dab5cd05SOleg Bulyzhin 	 */
3864dab5cd05SOleg Bulyzhin 
38651f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
38664c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
3867dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
3868dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
3869dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3870dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
38711f313773SOleg Bulyzhin 
38721f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
38731f313773SOleg Bulyzhin 			if (!sc->bge_link &&
38741f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
38751f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
38761f313773SOleg Bulyzhin 				sc->bge_link++;
38771f313773SOleg Bulyzhin 				if (bootverbose)
38781f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
38791f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
38801f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
38811f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
38821f313773SOleg Bulyzhin 				sc->bge_link = 0;
38831f313773SOleg Bulyzhin 				if (bootverbose)
38841f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
38851f313773SOleg Bulyzhin 			}
38861f313773SOleg Bulyzhin 
38873f74909aSGleb Smirnoff 			/* Clear the interrupt. */
3888dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
3889dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
3890dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
3891dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
3892dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
3893dab5cd05SOleg Bulyzhin 		}
3894dab5cd05SOleg Bulyzhin 		return;
3895dab5cd05SOleg Bulyzhin 	}
3896dab5cd05SOleg Bulyzhin 
3897652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
38981f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
38997b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
39007b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
39011f313773SOleg Bulyzhin 				sc->bge_link++;
39021f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
39031f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
39041f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
39051f313773SOleg Bulyzhin 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
39061f313773SOleg Bulyzhin 				if (bootverbose)
39071f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
39083f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
39093f74909aSGleb Smirnoff 				    LINK_STATE_UP);
39107b97099dSOleg Bulyzhin 			}
39111f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
3912dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
39131f313773SOleg Bulyzhin 			if (bootverbose)
39141f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
39157b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
39161f313773SOleg Bulyzhin 		}
39171493e883SOleg Bulyzhin 	/* Discard link events for MII/GMII cards if MI auto-polling disabled */
39181493e883SOleg Bulyzhin 	} else if (CSR_READ_4(sc, BGE_MI_MODE) & BGE_MIMODE_AUTOPOLL) {
39191f313773SOleg Bulyzhin 		/*
39201f313773SOleg Bulyzhin 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
39211f313773SOleg Bulyzhin 		 * in status word always set. Workaround this bug by reading
39221f313773SOleg Bulyzhin 		 * PHY link status directly.
39231f313773SOleg Bulyzhin 		 */
39241f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
39251f313773SOleg Bulyzhin 
39261f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
39271f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
3928dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3929dab5cd05SOleg Bulyzhin 			bge_tick_locked(sc);
39301f313773SOleg Bulyzhin 
39311f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
39321f313773SOleg Bulyzhin 			if (!sc->bge_link &&
39331f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
39341f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
39351f313773SOleg Bulyzhin 				sc->bge_link++;
39361f313773SOleg Bulyzhin 				if (bootverbose)
39371f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
39381f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
39391f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
39401f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
39411f313773SOleg Bulyzhin 				sc->bge_link = 0;
39421f313773SOleg Bulyzhin 				if (bootverbose)
39431f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
39441f313773SOleg Bulyzhin 			}
39451f313773SOleg Bulyzhin 		}
3946dab5cd05SOleg Bulyzhin 	}
3947dab5cd05SOleg Bulyzhin 
39483f74909aSGleb Smirnoff 	/* Clear the attention. */
3949dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
3950dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
3951dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
3952dab5cd05SOleg Bulyzhin }
3953