xref: /freebsd/sys/dev/bge/if_bge.c (revision 7c929cf95bd72ff5e00efe9ce72fa21a2bafaf96)
1098ca2bdSWarner Losh /*-
295d67482SBill Paul  * Copyright (c) 2001 Wind River Systems
395d67482SBill Paul  * Copyright (c) 1997, 1998, 1999, 2001
495d67482SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
595d67482SBill Paul  *
695d67482SBill Paul  * Redistribution and use in source and binary forms, with or without
795d67482SBill Paul  * modification, are permitted provided that the following conditions
895d67482SBill Paul  * are met:
995d67482SBill Paul  * 1. Redistributions of source code must retain the above copyright
1095d67482SBill Paul  *    notice, this list of conditions and the following disclaimer.
1195d67482SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1295d67482SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1395d67482SBill Paul  *    documentation and/or other materials provided with the distribution.
1495d67482SBill Paul  * 3. All advertising materials mentioning features or use of this software
1595d67482SBill Paul  *    must display the following acknowledgement:
1695d67482SBill Paul  *	This product includes software developed by Bill Paul.
1795d67482SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1895d67482SBill Paul  *    may be used to endorse or promote products derived from this software
1995d67482SBill Paul  *    without specific prior written permission.
2095d67482SBill Paul  *
2195d67482SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2295d67482SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2395d67482SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2495d67482SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2595d67482SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2695d67482SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2795d67482SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2895d67482SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2995d67482SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3095d67482SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3195d67482SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3295d67482SBill Paul  */
3395d67482SBill Paul 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
3795d67482SBill Paul /*
3895d67482SBill Paul  * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
3995d67482SBill Paul  *
4095d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
4195d67482SBill Paul  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
4295d67482SBill Paul  * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
4395d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4495d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4595d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
4695d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
4795d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
4895d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
4995d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
5095d67482SBill Paul  * into the driver.
5195d67482SBill Paul  *
5295d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5395d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5495d67482SBill Paul  *
5595d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
5698b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
5795d67482SBill Paul  * does not support external SSRAM.
5895d67482SBill Paul  *
5995d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
6095d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
6195d67482SBill Paul  *
6295d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6395d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6495d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6595d67482SBill Paul  * result, this driver does not implement any support for the mini RX
6695d67482SBill Paul  * ring.
6795d67482SBill Paul  */
6895d67482SBill Paul 
6975719184SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
7075719184SGleb Smirnoff #include "opt_device_polling.h"
7175719184SGleb Smirnoff #endif
7275719184SGleb Smirnoff 
7395d67482SBill Paul #include <sys/param.h>
74f41ac2beSBill Paul #include <sys/endian.h>
7595d67482SBill Paul #include <sys/systm.h>
7695d67482SBill Paul #include <sys/sockio.h>
7795d67482SBill Paul #include <sys/mbuf.h>
7895d67482SBill Paul #include <sys/malloc.h>
7995d67482SBill Paul #include <sys/kernel.h>
80fe12f24bSPoul-Henning Kamp #include <sys/module.h>
8195d67482SBill Paul #include <sys/socket.h>
82f1a7e6d5SScott Long #include <sys/sysctl.h>
8395d67482SBill Paul 
8495d67482SBill Paul #include <net/if.h>
8595d67482SBill Paul #include <net/if_arp.h>
8695d67482SBill Paul #include <net/ethernet.h>
8795d67482SBill Paul #include <net/if_dl.h>
8895d67482SBill Paul #include <net/if_media.h>
8995d67482SBill Paul 
9095d67482SBill Paul #include <net/bpf.h>
9195d67482SBill Paul 
9295d67482SBill Paul #include <net/if_types.h>
9395d67482SBill Paul #include <net/if_vlan_var.h>
9495d67482SBill Paul 
9595d67482SBill Paul #include <netinet/in_systm.h>
9695d67482SBill Paul #include <netinet/in.h>
9795d67482SBill Paul #include <netinet/ip.h>
9895d67482SBill Paul 
9995d67482SBill Paul #include <machine/bus.h>
10095d67482SBill Paul #include <machine/resource.h>
10195d67482SBill Paul #include <sys/bus.h>
10295d67482SBill Paul #include <sys/rman.h>
10395d67482SBill Paul 
10495d67482SBill Paul #include <dev/mii/mii.h>
10595d67482SBill Paul #include <dev/mii/miivar.h>
1062d3ce713SDavid E. O'Brien #include "miidevs.h"
10795d67482SBill Paul #include <dev/mii/brgphyreg.h>
10895d67482SBill Paul 
1094fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1104fbd232cSWarner Losh #include <dev/pci/pcivar.h>
11195d67482SBill Paul 
11295d67482SBill Paul #include <dev/bge/if_bgereg.h>
11395d67482SBill Paul 
1145ddc5794SJohn Polstra #define BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
115d375e524SGleb Smirnoff #define ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
11695d67482SBill Paul 
11720aa3e48SJohn Polstra /*
11820aa3e48SJohn Polstra  * Disable the use of MSI until we sort out on which chip revisions support
11920aa3e48SJohn Polstra  * it properly.
12020aa3e48SJohn Polstra  */
12120aa3e48SJohn Polstra #define BGE_DISABLE_MSI		1
12220aa3e48SJohn Polstra 
123f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
124f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
12595d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12695d67482SBill Paul 
1277b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
12895d67482SBill Paul #include "miibus_if.h"
12995d67482SBill Paul 
13095d67482SBill Paul /*
13195d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
13295d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
13395d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13495d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13595d67482SBill Paul  */
1364c0da0ffSGleb Smirnoff static struct bge_type {
1374c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1384c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
1394c0da0ffSGleb Smirnoff } bge_devs[] = {
1404c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5700 },
1414c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5701 },
14295d67482SBill Paul 
1434c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1000 },
1444c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1002 },
1454c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC9100 },
1464c0da0ffSGleb Smirnoff 
1474c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	APPLE_DEVICE_BCM5701 },
1484c0da0ffSGleb Smirnoff 
1494c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5700 },
1504c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5701 },
1514c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702 },
1524c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702_ALT },
1534c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702X },
1544c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703 },
1554c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703_ALT },
1564c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703X },
1574c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704C },
1584c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S },
1594c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S_ALT },
1604c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705 },
1614c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705F },
1624c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705K },
1634c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M },
1644c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M_ALT },
1654c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714C },
1664c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714S },
1674c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715 },
1684c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715S },
1694c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1704c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
1714c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1724c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1734c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1744c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1754c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1764c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1774c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1784c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1794c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1804c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1819e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1829e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1839e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1849e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
1854c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
1864c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
1874c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
1884c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
1899e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
1909e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
1919e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
1924c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
1934c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
1944c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
1954c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
1964c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
1974c0da0ffSGleb Smirnoff 
1984c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
1994c0da0ffSGleb Smirnoff 
2004c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
2014c0da0ffSGleb Smirnoff 
2024c0da0ffSGleb Smirnoff 	{ 0, 0 }
20395d67482SBill Paul };
20495d67482SBill Paul 
2054c0da0ffSGleb Smirnoff static const struct bge_vendor {
2064c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2074c0da0ffSGleb Smirnoff 	const char	*v_name;
2084c0da0ffSGleb Smirnoff } bge_vendors[] = {
2094c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2104c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2114c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2124c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2134c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2144c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
2154c0da0ffSGleb Smirnoff 
2164c0da0ffSGleb Smirnoff 	{ 0, NULL }
2174c0da0ffSGleb Smirnoff };
2184c0da0ffSGleb Smirnoff 
2194c0da0ffSGleb Smirnoff static const struct bge_revision {
2204c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2214c0da0ffSGleb Smirnoff 	const char	*br_name;
2224c0da0ffSGleb Smirnoff } bge_revisions[] = {
2234c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2244c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2254c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2264c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2274c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2284c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2294c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2304c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2314c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2324c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2334c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2344c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2354c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2364c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2374c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2384c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2399e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2404c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2414c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2424c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2434c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2444c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2454c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2464c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2474c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2484c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2494c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2504c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2514c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2524c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2534c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2544c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2554c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
25642787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2574c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2584c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2594c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
2604c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
2614c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
2624c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
2634c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
2644c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
2656f8718a3SScott Long 	/* 5784 and 5787 share the same ASIC ID */
2666f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
2676f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
2686f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
2694c0da0ffSGleb Smirnoff 
2704c0da0ffSGleb Smirnoff 	{ 0, NULL }
2714c0da0ffSGleb Smirnoff };
2724c0da0ffSGleb Smirnoff 
2734c0da0ffSGleb Smirnoff /*
2744c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
2754c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
2764c0da0ffSGleb Smirnoff  */
2774c0da0ffSGleb Smirnoff static const struct bge_revision bge_majorrevs[] = {
2789e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
2799e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
2809e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
2819e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
2829e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
2839e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
2849e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
2859e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
2869e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
2879e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
2889e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
2896f8718a3SScott Long 	/* 5784 and 5787 share the same ASIC ID */
2906f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
2914c0da0ffSGleb Smirnoff 
2924c0da0ffSGleb Smirnoff 	{ 0, NULL }
2934c0da0ffSGleb Smirnoff };
2944c0da0ffSGleb Smirnoff 
2957ee00338SJung-uk Kim #define BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
2967ee00338SJung-uk Kim #define BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
2977ee00338SJung-uk Kim #define BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
2980dae9719SJung-uk Kim #define BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
2990dae9719SJung-uk Kim #define BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
3004c0da0ffSGleb Smirnoff 
3014c0da0ffSGleb Smirnoff const struct bge_revision * bge_lookup_rev(uint32_t);
3024c0da0ffSGleb Smirnoff const struct bge_vendor * bge_lookup_vendor(uint16_t);
303e51a25f8SAlfred Perlstein static int bge_probe(device_t);
304e51a25f8SAlfred Perlstein static int bge_attach(device_t);
305e51a25f8SAlfred Perlstein static int bge_detach(device_t);
30614afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
30714afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3083f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
309f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
310f41ac2beSBill Paul static int bge_dma_alloc(device_t);
311f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
312f41ac2beSBill Paul 
313e51a25f8SAlfred Perlstein static void bge_txeof(struct bge_softc *);
314e51a25f8SAlfred Perlstein static void bge_rxeof(struct bge_softc *);
31595d67482SBill Paul 
3168cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
317e51a25f8SAlfred Perlstein static void bge_tick(void *);
318e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
3193f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
320676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
32195d67482SBill Paul 
322e51a25f8SAlfred Perlstein static void bge_intr(void *);
3230f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
324e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
325e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
3260f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
327e51a25f8SAlfred Perlstein static void bge_init(void *);
328e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
329b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
330e51a25f8SAlfred Perlstein static void bge_shutdown(device_t);
33167d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
332e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
333e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
33495d67482SBill Paul 
3353f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
336e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
33795d67482SBill Paul 
3383e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
339e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
34095d67482SBill Paul 
341e51a25f8SAlfred Perlstein static int bge_newbuf_std(struct bge_softc *, int, struct mbuf *);
342e51a25f8SAlfred Perlstein static int bge_newbuf_jumbo(struct bge_softc *, int, struct mbuf *);
343e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
344e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
345e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
346e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
347e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
348e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
34995d67482SBill Paul 
350e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
351e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
35295d67482SBill Paul 
3533f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
354e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
35595d67482SBill Paul #ifdef notdef
3563f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
35795d67482SBill Paul #endif
3589ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
359e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
36095d67482SBill Paul 
361e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
362e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
363e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
36475719184SGleb Smirnoff #ifdef DEVICE_POLLING
3653f74909aSGleb Smirnoff static void bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
36675719184SGleb Smirnoff #endif
36795d67482SBill Paul 
3688cb1383cSDoug Ambrisko #define BGE_RESET_START 1
3698cb1383cSDoug Ambrisko #define BGE_RESET_STOP  2
3708cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
3718cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
3728cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
3738cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
374dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
37595d67482SBill Paul 
3766f8718a3SScott Long /*
3776f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
3786f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
3796f8718a3SScott Long  * traps on certain architectures.
3806f8718a3SScott Long  */
3816f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
3826f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
3836f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
3846f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
3856f8718a3SScott Long #endif
3866f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
3876f8718a3SScott Long 
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;
421f1a7e6d5SScott Long static int bge_allow_asf = 1;
422f1a7e6d5SScott Long 
423c4529f41SMichael Reifenberger TUNABLE_INT("hw.bge.fake_autoneg", &bge_fake_autoneg);
424f1a7e6d5SScott Long TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
425f1a7e6d5SScott Long 
426f1a7e6d5SScott Long SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
427f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, fake_autoneg, CTLFLAG_RD, &bge_fake_autoneg, 0,
428f1a7e6d5SScott Long 	"Enable fake autonegotiation for certain blade systems");
429f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
430f1a7e6d5SScott Long 	"Allow ASF mode if available");
431c4529f41SMichael Reifenberger 
4323f74909aSGleb Smirnoff static uint32_t
4333f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
43495d67482SBill Paul {
43595d67482SBill Paul 	device_t dev;
4366f8718a3SScott Long 	uint32_t val;
43795d67482SBill Paul 
43895d67482SBill Paul 	dev = sc->bge_dev;
43995d67482SBill Paul 
44095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
4416f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
4426f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
4436f8718a3SScott Long 	return (val);
44495d67482SBill Paul }
44595d67482SBill Paul 
44695d67482SBill Paul static void
4473f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
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_MEMWIN_BASEADDR, off, 4);
45495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
4556f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
45695d67482SBill Paul }
45795d67482SBill Paul 
45895d67482SBill Paul #ifdef notdef
4593f74909aSGleb Smirnoff static uint32_t
4603f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
46195d67482SBill Paul {
46295d67482SBill Paul 	device_t dev;
46395d67482SBill Paul 
46495d67482SBill Paul 	dev = sc->bge_dev;
46595d67482SBill Paul 
46695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
46795d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
46895d67482SBill Paul }
46995d67482SBill Paul #endif
47095d67482SBill Paul 
47195d67482SBill Paul static void
4723f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
47395d67482SBill Paul {
47495d67482SBill Paul 	device_t dev;
47595d67482SBill Paul 
47695d67482SBill Paul 	dev = sc->bge_dev;
47795d67482SBill Paul 
47895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
47995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
48095d67482SBill Paul }
48195d67482SBill Paul 
4826f8718a3SScott Long static void
4836f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
4846f8718a3SScott Long {
4856f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
4866f8718a3SScott Long }
4876f8718a3SScott Long 
488f41ac2beSBill Paul /*
489f41ac2beSBill Paul  * Map a single buffer address.
490f41ac2beSBill Paul  */
491f41ac2beSBill Paul 
492f41ac2beSBill Paul static void
4933f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
494f41ac2beSBill Paul {
495f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
496f41ac2beSBill Paul 
497f41ac2beSBill Paul 	if (error)
498f41ac2beSBill Paul 		return;
499f41ac2beSBill Paul 
500f41ac2beSBill Paul 	ctx = arg;
501f41ac2beSBill Paul 
502f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
503f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
504f41ac2beSBill Paul 		return;
505f41ac2beSBill Paul 	}
506f41ac2beSBill Paul 
507f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
508f41ac2beSBill Paul }
509f41ac2beSBill Paul 
51095d67482SBill Paul /*
51195d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
51295d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
51395d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
51495d67482SBill Paul  * access method.
51595d67482SBill Paul  */
5163f74909aSGleb Smirnoff static uint8_t
5173f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
51895d67482SBill Paul {
51995d67482SBill Paul 	int i;
5203f74909aSGleb Smirnoff 	uint32_t byte = 0;
52195d67482SBill Paul 
52295d67482SBill Paul 	/*
52395d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
52495d67482SBill Paul 	 * having to use the bitbang method.
52595d67482SBill Paul 	 */
52695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
52795d67482SBill Paul 
52895d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
52995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
53095d67482SBill Paul 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
53195d67482SBill Paul 	DELAY(20);
53295d67482SBill Paul 
53395d67482SBill Paul 	/* Issue the read EEPROM command. */
53495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
53595d67482SBill Paul 
53695d67482SBill Paul 	/* Wait for completion */
53795d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
53895d67482SBill Paul 		DELAY(10);
53995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
54095d67482SBill Paul 			break;
54195d67482SBill Paul 	}
54295d67482SBill Paul 
54395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
544fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
545f6789fbaSPyun YongHyeon 		return (1);
54695d67482SBill Paul 	}
54795d67482SBill Paul 
54895d67482SBill Paul 	/* Get result. */
54995d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
55095d67482SBill Paul 
55195d67482SBill Paul 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
55295d67482SBill Paul 
55395d67482SBill Paul 	return (0);
55495d67482SBill Paul }
55595d67482SBill Paul 
55695d67482SBill Paul /*
55795d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
55895d67482SBill Paul  */
55995d67482SBill Paul static int
5603f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
56195d67482SBill Paul {
5623f74909aSGleb Smirnoff 	int i, error = 0;
5633f74909aSGleb Smirnoff 	uint8_t byte = 0;
56495d67482SBill Paul 
56595d67482SBill Paul 	for (i = 0; i < cnt; i++) {
5663f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
5673f74909aSGleb Smirnoff 		if (error)
56895d67482SBill Paul 			break;
56995d67482SBill Paul 		*(dest + i) = byte;
57095d67482SBill Paul 	}
57195d67482SBill Paul 
5723f74909aSGleb Smirnoff 	return (error ? 1 : 0);
57395d67482SBill Paul }
57495d67482SBill Paul 
57595d67482SBill Paul static int
5763f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
57795d67482SBill Paul {
57895d67482SBill Paul 	struct bge_softc *sc;
5793f74909aSGleb Smirnoff 	uint32_t val, autopoll;
58095d67482SBill Paul 	int i;
58195d67482SBill Paul 
58295d67482SBill Paul 	sc = device_get_softc(dev);
58395d67482SBill Paul 
5840434d1b8SBill Paul 	/*
5850434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
5860434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
5870434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
5880434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
5890434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
5900434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
5910434d1b8SBill Paul 	 * special-cased.
5920434d1b8SBill Paul 	 */
593b1265c1aSJohn Polstra 	if (phy != 1)
59498b28ee5SBill Paul 		return (0);
59598b28ee5SBill Paul 
59637ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
59737ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
59837ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
59937ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
60037ceeb4dSPaul Saab 		DELAY(40);
60137ceeb4dSPaul Saab 	}
60237ceeb4dSPaul Saab 
60395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
60495d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
60595d67482SBill Paul 
60695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
60795d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
60895d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
60995d67482SBill Paul 			break;
61095d67482SBill Paul 	}
61195d67482SBill Paul 
61295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
6136b9f5c94SGleb Smirnoff 		device_printf(sc->bge_dev, "PHY read timed out\n");
61437ceeb4dSPaul Saab 		val = 0;
61537ceeb4dSPaul Saab 		goto done;
61695d67482SBill Paul 	}
61795d67482SBill Paul 
61895d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
61995d67482SBill Paul 
62037ceeb4dSPaul Saab done:
62137ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
62237ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
62337ceeb4dSPaul Saab 		DELAY(40);
62437ceeb4dSPaul Saab 	}
62537ceeb4dSPaul Saab 
62695d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
62795d67482SBill Paul 		return (0);
62895d67482SBill Paul 
62995d67482SBill Paul 	return (val & 0xFFFF);
63095d67482SBill Paul }
63195d67482SBill Paul 
63295d67482SBill Paul static int
6333f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
63495d67482SBill Paul {
63595d67482SBill Paul 	struct bge_softc *sc;
6363f74909aSGleb Smirnoff 	uint32_t autopoll;
63795d67482SBill Paul 	int i;
63895d67482SBill Paul 
63995d67482SBill Paul 	sc = device_get_softc(dev);
64095d67482SBill Paul 
64137ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
64237ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
64337ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
64437ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
64537ceeb4dSPaul Saab 		DELAY(40);
64637ceeb4dSPaul Saab 	}
64737ceeb4dSPaul Saab 
64895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
64995d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
65095d67482SBill Paul 
65195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
65295d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
65395d67482SBill Paul 			break;
65495d67482SBill Paul 	}
65595d67482SBill Paul 
65637ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
65737ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
65837ceeb4dSPaul Saab 		DELAY(40);
65937ceeb4dSPaul Saab 	}
66037ceeb4dSPaul Saab 
66195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
6626b9f5c94SGleb Smirnoff 		device_printf(sc->bge_dev, "PHY read timed out\n");
66395d67482SBill Paul 		return (0);
66495d67482SBill Paul 	}
66595d67482SBill Paul 
66695d67482SBill Paul 	return (0);
66795d67482SBill Paul }
66895d67482SBill Paul 
66995d67482SBill Paul static void
6703f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
67195d67482SBill Paul {
67295d67482SBill Paul 	struct bge_softc *sc;
67395d67482SBill Paul 	struct mii_data *mii;
67495d67482SBill Paul 	sc = device_get_softc(dev);
67595d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
67695d67482SBill Paul 
67795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
6783f74909aSGleb Smirnoff 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)
67995d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
6803f74909aSGleb Smirnoff 	else
68195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
68295d67482SBill Paul 
6833f74909aSGleb Smirnoff 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
68495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
6853f74909aSGleb Smirnoff 	else
68695d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
68795d67482SBill Paul }
68895d67482SBill Paul 
68995d67482SBill Paul /*
69095d67482SBill Paul  * Intialize a standard receive ring descriptor.
69195d67482SBill Paul  */
69295d67482SBill Paul static int
6933f74909aSGleb Smirnoff bge_newbuf_std(struct bge_softc *sc, int i, struct mbuf *m)
69495d67482SBill Paul {
69595d67482SBill Paul 	struct mbuf *m_new = NULL;
69695d67482SBill Paul 	struct bge_rx_bd *r;
697f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
698f41ac2beSBill Paul 	int error;
69995d67482SBill Paul 
70095d67482SBill Paul 	if (m == NULL) {
701c3a56752SGleb Smirnoff 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
702c3a56752SGleb Smirnoff 		if (m_new == NULL)
70395d67482SBill Paul 			return (ENOBUFS);
70495d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
70595d67482SBill Paul 	} else {
70695d67482SBill Paul 		m_new = m;
70795d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
70895d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
70995d67482SBill Paul 	}
71095d67482SBill Paul 
711652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
71295d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
71395d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
714f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
715f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
716f41ac2beSBill Paul 	ctx.sc = sc;
717f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
718f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
719f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
720f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
721f7cea149SGleb Smirnoff 		if (m == NULL) {
722f7cea149SGleb Smirnoff 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
723f41ac2beSBill Paul 			m_freem(m_new);
724f7cea149SGleb Smirnoff 		}
725f41ac2beSBill Paul 		return (ENOMEM);
726f41ac2beSBill Paul 	}
727e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(ctx.bge_busaddr);
728e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(ctx.bge_busaddr);
729e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
730e907febfSPyun YongHyeon 	r->bge_len = m_new->m_len;
731e907febfSPyun YongHyeon 	r->bge_idx = i;
732f41ac2beSBill Paul 
733f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
734f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
735f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
73695d67482SBill Paul 
73795d67482SBill Paul 	return (0);
73895d67482SBill Paul }
73995d67482SBill Paul 
74095d67482SBill Paul /*
74195d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
74295d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
74395d67482SBill Paul  */
74495d67482SBill Paul static int
7453f74909aSGleb Smirnoff bge_newbuf_jumbo(struct bge_softc *sc, int i, struct mbuf *m)
74695d67482SBill Paul {
7471be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
7481be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
74995d67482SBill Paul 	struct mbuf *m_new = NULL;
7501be6acb7SGleb Smirnoff 	int nsegs;
751f41ac2beSBill Paul 	int error;
75295d67482SBill Paul 
75395d67482SBill Paul 	if (m == NULL) {
754a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
7551be6acb7SGleb Smirnoff 		if (m_new == NULL)
75695d67482SBill Paul 			return (ENOBUFS);
75795d67482SBill Paul 
7581be6acb7SGleb Smirnoff 		m_cljget(m_new, M_DONTWAIT, MJUM9BYTES);
7591be6acb7SGleb Smirnoff 		if (!(m_new->m_flags & M_EXT)) {
76095d67482SBill Paul 			m_freem(m_new);
76195d67482SBill Paul 			return (ENOBUFS);
76295d67482SBill Paul 		}
7631be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
76495d67482SBill Paul 	} else {
76595d67482SBill Paul 		m_new = m;
7661be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
76795d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
76895d67482SBill Paul 	}
76995d67482SBill Paul 
770652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
77195d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
7721be6acb7SGleb Smirnoff 
7731be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
7741be6acb7SGleb Smirnoff 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
7751be6acb7SGleb Smirnoff 	    m_new, segs, &nsegs, BUS_DMA_NOWAIT);
7761be6acb7SGleb Smirnoff 	if (error) {
7771be6acb7SGleb Smirnoff 		if (m == NULL)
778f41ac2beSBill Paul 			m_freem(m_new);
7791be6acb7SGleb Smirnoff 		return (error);
780f7cea149SGleb Smirnoff 	}
7811be6acb7SGleb Smirnoff 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
7821be6acb7SGleb Smirnoff 
7831be6acb7SGleb Smirnoff 	/*
7841be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
7851be6acb7SGleb Smirnoff 	 */
7861be6acb7SGleb Smirnoff 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
7874e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING|BGE_RXBDFLAG_END;
7884e7ba1abSGleb Smirnoff 	r->bge_idx = i;
7894e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
7904e7ba1abSGleb Smirnoff 	switch (nsegs) {
7914e7ba1abSGleb Smirnoff 	case 4:
7924e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
7934e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
7944e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
7954e7ba1abSGleb Smirnoff 	case 3:
796e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
797e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
798e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
7994e7ba1abSGleb Smirnoff 	case 2:
8004e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
8014e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
8024e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
8034e7ba1abSGleb Smirnoff 	case 1:
8044e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
8054e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
8064e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
8074e7ba1abSGleb Smirnoff 		break;
8084e7ba1abSGleb Smirnoff 	default:
8094e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
8104e7ba1abSGleb Smirnoff 	}
811f41ac2beSBill Paul 
812f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
813f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
814f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
81595d67482SBill Paul 
81695d67482SBill Paul 	return (0);
81795d67482SBill Paul }
81895d67482SBill Paul 
81995d67482SBill Paul /*
82095d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
82195d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
82295d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
82395d67482SBill Paul  * the NIC.
82495d67482SBill Paul  */
82595d67482SBill Paul static int
8263f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
82795d67482SBill Paul {
82895d67482SBill Paul 	int i;
82995d67482SBill Paul 
83095d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
83195d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
83295d67482SBill Paul 			return (ENOBUFS);
83395d67482SBill Paul 	};
83495d67482SBill Paul 
835f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
836f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
837f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
838f41ac2beSBill Paul 
83995d67482SBill Paul 	sc->bge_std = i - 1;
84095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
84195d67482SBill Paul 
84295d67482SBill Paul 	return (0);
84395d67482SBill Paul }
84495d67482SBill Paul 
84595d67482SBill Paul static void
8463f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
84795d67482SBill Paul {
84895d67482SBill Paul 	int i;
84995d67482SBill Paul 
85095d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
85195d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
852e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
853e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
854e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
855f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
856f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
857e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
858e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
85995d67482SBill Paul 		}
860f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
86195d67482SBill Paul 		    sizeof(struct bge_rx_bd));
86295d67482SBill Paul 	}
86395d67482SBill Paul }
86495d67482SBill Paul 
86595d67482SBill Paul static int
8663f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
86795d67482SBill Paul {
86895d67482SBill Paul 	struct bge_rcb *rcb;
8691be6acb7SGleb Smirnoff 	int i;
87095d67482SBill Paul 
87195d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
87295d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
87395d67482SBill Paul 			return (ENOBUFS);
87495d67482SBill Paul 	};
87595d67482SBill Paul 
876f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
877f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
878f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
879f41ac2beSBill Paul 
88095d67482SBill Paul 	sc->bge_jumbo = i - 1;
88195d67482SBill Paul 
882f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
8831be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
8841be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
88567111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
88695d67482SBill Paul 
88795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
88895d67482SBill Paul 
88995d67482SBill Paul 	return (0);
89095d67482SBill Paul }
89195d67482SBill Paul 
89295d67482SBill Paul static void
8933f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
89495d67482SBill Paul {
89595d67482SBill Paul 	int i;
89695d67482SBill Paul 
89795d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
89895d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
899e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
900e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
901e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
902f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
903f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
904e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
905e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
90695d67482SBill Paul 		}
907f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
9081be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
90995d67482SBill Paul 	}
91095d67482SBill Paul }
91195d67482SBill Paul 
91295d67482SBill Paul static void
9133f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
91495d67482SBill Paul {
91595d67482SBill Paul 	int i;
91695d67482SBill Paul 
917f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
91895d67482SBill Paul 		return;
91995d67482SBill Paul 
92095d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
92195d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
922e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
923e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
924e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
925f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
926f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
927e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
928e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
92995d67482SBill Paul 		}
930f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
93195d67482SBill Paul 		    sizeof(struct bge_tx_bd));
93295d67482SBill Paul 	}
93395d67482SBill Paul }
93495d67482SBill Paul 
93595d67482SBill Paul static int
9363f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
93795d67482SBill Paul {
93895d67482SBill Paul 	sc->bge_txcnt = 0;
93995d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
9403927098fSPaul Saab 
94114bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
94214bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
94314bbd30fSGleb Smirnoff 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
94414bbd30fSGleb Smirnoff 
9453927098fSPaul Saab 	/* 5700 b2 errata */
946e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
94714bbd30fSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
9483927098fSPaul Saab 
94914bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
9503927098fSPaul Saab 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
9513927098fSPaul Saab 	/* 5700 b2 errata */
952e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
95395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
95495d67482SBill Paul 
95595d67482SBill Paul 	return (0);
95695d67482SBill Paul }
95795d67482SBill Paul 
95895d67482SBill Paul static void
9593e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
9603e9b1bcaSJung-uk Kim {
9613e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
9623e9b1bcaSJung-uk Kim 
9633e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
9643e9b1bcaSJung-uk Kim 
9653e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
9663e9b1bcaSJung-uk Kim 
9673e9b1bcaSJung-uk Kim 	/*
9683e9b1bcaSJung-uk Kim 	 * Enable or disable promiscuous mode as needed.
9693e9b1bcaSJung-uk Kim 	 * Do not strip VLAN tag when promiscuous mode is enabled.
9703e9b1bcaSJung-uk Kim 	 */
9713e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
9723e9b1bcaSJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC |
9733e9b1bcaSJung-uk Kim 		    BGE_RXMODE_RX_KEEP_VLAN_DIAG);
9743e9b1bcaSJung-uk Kim 	else
9753e9b1bcaSJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC |
9763e9b1bcaSJung-uk Kim 		    BGE_RXMODE_RX_KEEP_VLAN_DIAG);
9773e9b1bcaSJung-uk Kim }
9783e9b1bcaSJung-uk Kim 
9793e9b1bcaSJung-uk Kim static void
9803f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
98195d67482SBill Paul {
98295d67482SBill Paul 	struct ifnet *ifp;
98395d67482SBill Paul 	struct ifmultiaddr *ifma;
9843f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
98595d67482SBill Paul 	int h, i;
98695d67482SBill Paul 
9870f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
9880f9bd73bSSam Leffler 
989fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
99095d67482SBill Paul 
99195d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
99295d67482SBill Paul 		for (i = 0; i < 4; i++)
99395d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
99495d67482SBill Paul 		return;
99595d67482SBill Paul 	}
99695d67482SBill Paul 
99795d67482SBill Paul 	/* First, zot all the existing filters. */
99895d67482SBill Paul 	for (i = 0; i < 4; i++)
99995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
100095d67482SBill Paul 
100195d67482SBill Paul 	/* Now program new ones. */
100213b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
100395d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
100495d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
100595d67482SBill Paul 			continue;
10060e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
10070e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
100895d67482SBill Paul 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
100995d67482SBill Paul 	}
101013b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
101195d67482SBill Paul 
101295d67482SBill Paul 	for (i = 0; i < 4; i++)
101395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
101495d67482SBill Paul }
101595d67482SBill Paul 
10168cb1383cSDoug Ambrisko static void
10178cb1383cSDoug Ambrisko bge_sig_pre_reset(sc, type)
10188cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10198cb1383cSDoug Ambrisko 	int type;
10208cb1383cSDoug Ambrisko {
10218cb1383cSDoug Ambrisko 	/*
10228cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
10238cb1383cSDoug Ambrisko 	 */
10248cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
10258cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
10268cb1383cSDoug Ambrisko 
10278cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
10288cb1383cSDoug Ambrisko 		switch (type) {
10298cb1383cSDoug Ambrisko 		case BGE_RESET_START:
10308cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
10318cb1383cSDoug Ambrisko 			break;
10328cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
10338cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
10348cb1383cSDoug Ambrisko 			break;
10358cb1383cSDoug Ambrisko 		}
10368cb1383cSDoug Ambrisko 	}
10378cb1383cSDoug Ambrisko }
10388cb1383cSDoug Ambrisko 
10398cb1383cSDoug Ambrisko static void
10408cb1383cSDoug Ambrisko bge_sig_post_reset(sc, type)
10418cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10428cb1383cSDoug Ambrisko 	int type;
10438cb1383cSDoug Ambrisko {
10448cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
10458cb1383cSDoug Ambrisko 		switch (type) {
10468cb1383cSDoug Ambrisko 		case BGE_RESET_START:
10478cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000001);
10488cb1383cSDoug Ambrisko 			/* START DONE */
10498cb1383cSDoug Ambrisko 			break;
10508cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
10518cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000002);
10528cb1383cSDoug Ambrisko 			break;
10538cb1383cSDoug Ambrisko 		}
10548cb1383cSDoug Ambrisko 	}
10558cb1383cSDoug Ambrisko }
10568cb1383cSDoug Ambrisko 
10578cb1383cSDoug Ambrisko static void
10588cb1383cSDoug Ambrisko bge_sig_legacy(sc, type)
10598cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10608cb1383cSDoug Ambrisko 	int type;
10618cb1383cSDoug Ambrisko {
10628cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
10638cb1383cSDoug Ambrisko 		switch (type) {
10648cb1383cSDoug Ambrisko 		case BGE_RESET_START:
10658cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
10668cb1383cSDoug Ambrisko 			break;
10678cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
10688cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
10698cb1383cSDoug Ambrisko 			break;
10708cb1383cSDoug Ambrisko 		}
10718cb1383cSDoug Ambrisko 	}
10728cb1383cSDoug Ambrisko }
10738cb1383cSDoug Ambrisko 
10748cb1383cSDoug Ambrisko void bge_stop_fw(struct bge_softc *);
10758cb1383cSDoug Ambrisko void
10768cb1383cSDoug Ambrisko bge_stop_fw(sc)
10778cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10788cb1383cSDoug Ambrisko {
10798cb1383cSDoug Ambrisko 	int i;
10808cb1383cSDoug Ambrisko 
10818cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
10828cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW, BGE_FW_PAUSE);
10838cb1383cSDoug Ambrisko 		CSR_WRITE_4(sc, BGE_CPU_EVENT,
10848cb1383cSDoug Ambrisko 			    CSR_READ_4(sc, BGE_CPU_EVENT) != (1 << 14));
10858cb1383cSDoug Ambrisko 
10868cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
10878cb1383cSDoug Ambrisko 			if (!(CSR_READ_4(sc, BGE_CPU_EVENT) & (1 << 14)))
10888cb1383cSDoug Ambrisko 				break;
10898cb1383cSDoug Ambrisko 			DELAY(10);
10908cb1383cSDoug Ambrisko 		}
10918cb1383cSDoug Ambrisko 	}
10928cb1383cSDoug Ambrisko }
10938cb1383cSDoug Ambrisko 
109495d67482SBill Paul /*
109595d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
109695d67482SBill Paul  * self-test results.
109795d67482SBill Paul  */
109895d67482SBill Paul static int
10993f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
110095d67482SBill Paul {
11013f74909aSGleb Smirnoff 	uint32_t dma_rw_ctl;
110295d67482SBill Paul 	int i;
110395d67482SBill Paul 
11048cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
1105e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
110695d67482SBill Paul 
110795d67482SBill Paul 	/*
110895d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
110995d67482SBill Paul 	 * self-tests passed.
111095d67482SBill Paul 	 */
111195d67482SBill Paul 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
1112fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "RX CPU self-diagnostics failed!\n");
111395d67482SBill Paul 		return (ENODEV);
111495d67482SBill Paul 	}
111595d67482SBill Paul 
111695d67482SBill Paul 	/* Clear the MAC control register */
111795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
111895d67482SBill Paul 
111995d67482SBill Paul 	/*
112095d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
112195d67482SBill Paul 	 * internal memory.
112295d67482SBill Paul 	 */
112395d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
11243f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
112595d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
112695d67482SBill Paul 
112795d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
11283f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
112995d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
113095d67482SBill Paul 
113195d67482SBill Paul 	/* Set up the PCI DMA control register. */
1132652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
11334c0da0ffSGleb Smirnoff 		/* PCI Express bus */
1134e53d81eeSPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1135e53d81eeSPaul Saab 		    (0xf << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1136e53d81eeSPaul Saab 		    (0x2 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1137652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
11388287860eSJohn Polstra 		/* PCI-X bus */
11394c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
11404c0da0ffSGleb Smirnoff 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD;
11414c0da0ffSGleb Smirnoff 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_ONEDMA_ATONCE; /* XXX */
11424c0da0ffSGleb Smirnoff 			/* XXX magic values, Broadcom-supplied Linux driver */
11434c0da0ffSGleb Smirnoff 			if (sc->bge_asicrev == BGE_ASICREV_BCM5780)
11444c0da0ffSGleb Smirnoff 				dma_rw_ctl |= (1 << 20) | (1 << 18) |
11454c0da0ffSGleb Smirnoff 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE;
11464c0da0ffSGleb Smirnoff 			else
11474c0da0ffSGleb Smirnoff 				dma_rw_ctl |= (1 << 20) | (1 << 18) | (1 << 15);
11484c0da0ffSGleb Smirnoff 
11494c0da0ffSGleb Smirnoff 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
11505cba12d3SPaul Saab 			/*
11515cba12d3SPaul Saab 			 * The 5704 uses a different encoding of read/write
11525cba12d3SPaul Saab 			 * watermarks.
11535cba12d3SPaul Saab 			 */
11545cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
11555cba12d3SPaul Saab 			    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
11565cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
11575cba12d3SPaul Saab 		else
11585cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
11595cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
11605cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
11615cba12d3SPaul Saab 			    (0x0F);
11625cba12d3SPaul Saab 
11635cba12d3SPaul Saab 		/*
11645cba12d3SPaul Saab 		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
11655cba12d3SPaul Saab 		 * for hardware bugs.
11665cba12d3SPaul Saab 		 */
1167e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1168e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
11693f74909aSGleb Smirnoff 			uint32_t tmp;
11705cba12d3SPaul Saab 
11715cba12d3SPaul Saab 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
11725cba12d3SPaul Saab 			if (tmp == 0x6 || tmp == 0x7)
11735cba12d3SPaul Saab 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
11748287860eSJohn Polstra 		}
11754c0da0ffSGleb Smirnoff 	} else
11764c0da0ffSGleb Smirnoff 		/* Conventional PCI bus */
11774c0da0ffSGleb Smirnoff 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
11784c0da0ffSGleb Smirnoff 		    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
11794c0da0ffSGleb Smirnoff 		    (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
11804c0da0ffSGleb Smirnoff 		    (0x0F);
11815cba12d3SPaul Saab 
1182e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
11830434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
11844c0da0ffSGleb Smirnoff 	    sc->bge_asicrev == BGE_ASICREV_BCM5705)
11855cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
11865cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
118795d67482SBill Paul 
118895d67482SBill Paul 	/*
118995d67482SBill Paul 	 * Set up general mode register.
119095d67482SBill Paul 	 */
1191e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
119295d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1193ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
119495d67482SBill Paul 
119595d67482SBill Paul 	/*
11968cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
11978cb1383cSDoug Ambrisko 	 */
11988cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
11998cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
12008cb1383cSDoug Ambrisko 
12018cb1383cSDoug Ambrisko 	/*
1202ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1203ea13bdd5SJohn Polstra 	 * properly by these devices.
120495d67482SBill Paul 	 */
1205ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
120695d67482SBill Paul 
120795d67482SBill Paul #ifdef __brokenalpha__
120895d67482SBill Paul 	/*
120995d67482SBill Paul 	 * Must insure that we do not cross an 8K (bytes) boundary
121095d67482SBill Paul 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
121195d67482SBill Paul 	 * restriction on some ALPHA platforms with early revision
121295d67482SBill Paul 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
121395d67482SBill Paul 	 */
121462f1ea9cSJohn Polstra 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
121562f1ea9cSJohn Polstra 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
121695d67482SBill Paul #endif
121795d67482SBill Paul 
121895d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
121995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
122095d67482SBill Paul 
122195d67482SBill Paul 	return (0);
122295d67482SBill Paul }
122395d67482SBill Paul 
122495d67482SBill Paul static int
12253f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
122695d67482SBill Paul {
122795d67482SBill Paul 	struct bge_rcb *rcb;
1228e907febfSPyun YongHyeon 	bus_size_t vrcb;
1229e907febfSPyun YongHyeon 	bge_hostaddr taddr;
12306f8718a3SScott Long 	uint32_t val;
123195d67482SBill Paul 	int i;
123295d67482SBill Paul 
123395d67482SBill Paul 	/*
123495d67482SBill Paul 	 * Initialize the memory window pointer register so that
123595d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
123695d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
123795d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
123895d67482SBill Paul 	 */
123995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
124095d67482SBill Paul 
1241822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1242822f63fcSBill Paul 
12437ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
124495d67482SBill Paul 		/* Configure mbuf memory pool */
12450dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1246822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1247822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1248822f63fcSBill Paul 		else
124995d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
125095d67482SBill Paul 
125195d67482SBill Paul 		/* Configure DMA resource pool */
12520434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
12530434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
125495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
12550434d1b8SBill Paul 	}
125695d67482SBill Paul 
125795d67482SBill Paul 	/* Configure mbuf pool watermarks */
12587ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
12590434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
12600434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
12610434d1b8SBill Paul 	} else {
1262fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1263fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
12640434d1b8SBill Paul 	}
1265fa228020SPaul Saab 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
126695d67482SBill Paul 
126795d67482SBill Paul 	/* Configure DMA resource watermarks */
126895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
126995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
127095d67482SBill Paul 
127195d67482SBill Paul 	/* Enable buffer manager */
12727ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
127395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
127495d67482SBill Paul 		    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
127595d67482SBill Paul 
127695d67482SBill Paul 		/* Poll for buffer manager start indication */
127795d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
127895d67482SBill Paul 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
127995d67482SBill Paul 				break;
128095d67482SBill Paul 			DELAY(10);
128195d67482SBill Paul 		}
128295d67482SBill Paul 
128395d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1284fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1285fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
128695d67482SBill Paul 			return (ENXIO);
128795d67482SBill Paul 		}
12880434d1b8SBill Paul 	}
128995d67482SBill Paul 
129095d67482SBill Paul 	/* Enable flow-through queues */
129195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
129295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
129395d67482SBill Paul 
129495d67482SBill Paul 	/* Wait until queue initialization is complete */
129595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
129695d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
129795d67482SBill Paul 			break;
129895d67482SBill Paul 		DELAY(10);
129995d67482SBill Paul 	}
130095d67482SBill Paul 
130195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1302fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
130395d67482SBill Paul 		return (ENXIO);
130495d67482SBill Paul 	}
130595d67482SBill Paul 
130695d67482SBill Paul 	/* Initialize the standard RX ring control block */
1307f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1308f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1309f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1310f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1311f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1312f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1313f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
13147ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
13150434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
13160434d1b8SBill Paul 	else
13170434d1b8SBill Paul 		rcb->bge_maxlen_flags =
13180434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
131995d67482SBill Paul 	rcb->bge_nicaddr = BGE_STD_RX_RINGS;
132067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
132167111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1322f41ac2beSBill Paul 
132367111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
132467111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
132595d67482SBill Paul 
132695d67482SBill Paul 	/*
132795d67482SBill Paul 	 * Initialize the jumbo RX ring control block
132895d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
132995d67482SBill Paul 	 * field until we're actually ready to start
133095d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
133195d67482SBill Paul 	 * high enough to require it).
133295d67482SBill Paul 	 */
13334c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1334f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1335f41ac2beSBill Paul 
1336f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1337f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1338f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1339f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1340f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1341f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1342f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
13431be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
13441be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD|BGE_RCB_FLAG_RING_DISABLED);
134595d67482SBill Paul 		rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
134667111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
134767111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
134867111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
134967111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1350f41ac2beSBill Paul 
13510434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
13520434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
135367111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
135495d67482SBill Paul 
135595d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1356f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
135767111612SJohn Polstra 		rcb->bge_maxlen_flags =
135867111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
13590434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
13600434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
13610434d1b8SBill Paul 	}
136295d67482SBill Paul 
136395d67482SBill Paul 	/*
136495d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
136595d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
136695d67482SBill Paul 	 * each ring.
13679ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
13689ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
13699ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
13709ba784dbSScott Long 	 * are reports that it might not need to be so strict.
137195d67482SBill Paul 	 */
13725345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
13736f8718a3SScott Long 		val = 8;
13746f8718a3SScott Long 	else
13756f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
13766f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
137795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
137895d67482SBill Paul 
137995d67482SBill Paul 	/*
138095d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
138195d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
138295d67482SBill Paul 	 * These are located in NIC memory.
138395d67482SBill Paul 	 */
1384e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
138595d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1386e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1387e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1388e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1389e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
139095d67482SBill Paul 	}
139195d67482SBill Paul 
139295d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
1393e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1394e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1395e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1396e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1397e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1398e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
13997ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
1400e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1401e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
140295d67482SBill Paul 
140395d67482SBill Paul 	/* Disable all unused RX return rings */
1404e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
140595d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1406e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1407e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1408e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
14090434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1410e907febfSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED));
1411e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
141295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
14133f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1414e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
141595d67482SBill Paul 	}
141695d67482SBill Paul 
141795d67482SBill Paul 	/* Initialize RX ring indexes */
141895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
141995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
142095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
142195d67482SBill Paul 
142295d67482SBill Paul 	/*
142395d67482SBill Paul 	 * Set up RX return ring 0
142495d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
142595d67482SBill Paul 	 * The return rings live entirely within the host, so the
142695d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
142795d67482SBill Paul 	 */
1428e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1429e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1430e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1431e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1432e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1433e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1434e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
143595d67482SBill Paul 
143695d67482SBill Paul 	/* Set random backoff seed for TX */
143795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
14384a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
14394a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
14404a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
144195d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
144295d67482SBill Paul 
144395d67482SBill Paul 	/* Set inter-packet gap */
144495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
144595d67482SBill Paul 
144695d67482SBill Paul 	/*
144795d67482SBill Paul 	 * Specify which ring to use for packets that don't match
144895d67482SBill Paul 	 * any RX rules.
144995d67482SBill Paul 	 */
145095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
145195d67482SBill Paul 
145295d67482SBill Paul 	/*
145395d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
145495d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
145595d67482SBill Paul 	 */
145695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
145795d67482SBill Paul 
145895d67482SBill Paul 	/* Inialize RX list placement stats mask. */
145995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
146095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
146195d67482SBill Paul 
146295d67482SBill Paul 	/* Disable host coalescing until we get it set up */
146395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
146495d67482SBill Paul 
146595d67482SBill Paul 	/* Poll to make sure it's shut down. */
146695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
146795d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
146895d67482SBill Paul 			break;
146995d67482SBill Paul 		DELAY(10);
147095d67482SBill Paul 	}
147195d67482SBill Paul 
147295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1473fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1474fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
147595d67482SBill Paul 		return (ENXIO);
147695d67482SBill Paul 	}
147795d67482SBill Paul 
147895d67482SBill Paul 	/* Set up host coalescing defaults */
147995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
148095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
148195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
148295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
14837ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
148495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
148595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
14860434d1b8SBill Paul 	}
148795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
148895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
148995d67482SBill Paul 
149095d67482SBill Paul 	/* Set up address of statistics block */
14917ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
1492f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1493f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
149495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1495f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
14960434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
149795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
14980434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
14990434d1b8SBill Paul 	}
15000434d1b8SBill Paul 
15010434d1b8SBill Paul 	/* Set up address of status block */
1502f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1503f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
150495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1505f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1506f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1507f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
150895d67482SBill Paul 
150995d67482SBill Paul 	/* Turn on host coalescing state machine */
151095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
151195d67482SBill Paul 
151295d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
151395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
151495d67482SBill Paul 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
151595d67482SBill Paul 
151695d67482SBill Paul 	/* Turn on RX list placement state machine */
151795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
151895d67482SBill Paul 
151995d67482SBill Paul 	/* Turn on RX list selector state machine. */
15207ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
152195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
152295d67482SBill Paul 
152395d67482SBill Paul 	/* Turn on DMA, clear stats */
152495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
152595d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
152695d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
152795d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
1528652ae483SGleb Smirnoff 	    ((sc->bge_flags & BGE_FLAG_TBI) ?
1529652ae483SGleb Smirnoff 	    BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
153095d67482SBill Paul 
153195d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
153295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
153395d67482SBill Paul 
153495d67482SBill Paul #ifdef notdef
153595d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
153695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
153795d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
153895d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
153995d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
154095d67482SBill Paul #endif
154195d67482SBill Paul 
154295d67482SBill Paul 	/* Turn on DMA completion state machine */
15437ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
154495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
154595d67482SBill Paul 
15466f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS;
15476f8718a3SScott Long 
15486f8718a3SScott Long 	/* Enable host coalescing bug fix. */
15496f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
15506f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5787)
15516f8718a3SScott Long 			val |= (1 << 29);
15526f8718a3SScott Long 
155395d67482SBill Paul 	/* Turn on write DMA state machine */
15546f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
155595d67482SBill Paul 
155695d67482SBill Paul 	/* Turn on read DMA state machine */
155795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
155895d67482SBill Paul 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
155995d67482SBill Paul 
156095d67482SBill Paul 	/* Turn on RX data completion state machine */
156195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
156295d67482SBill Paul 
156395d67482SBill Paul 	/* Turn on RX BD initiator state machine */
156495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
156595d67482SBill Paul 
156695d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
156795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
156895d67482SBill Paul 
156995d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
15707ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
157195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
157295d67482SBill Paul 
157395d67482SBill Paul 	/* Turn on send BD completion state machine */
157495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
157595d67482SBill Paul 
157695d67482SBill Paul 	/* Turn on send data completion state machine */
157795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
157895d67482SBill Paul 
157995d67482SBill Paul 	/* Turn on send data initiator state machine */
158095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
158195d67482SBill Paul 
158295d67482SBill Paul 	/* Turn on send BD initiator state machine */
158395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
158495d67482SBill Paul 
158595d67482SBill Paul 	/* Turn on send BD selector state machine */
158695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
158795d67482SBill Paul 
158895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
158995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
159095d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
159195d67482SBill Paul 
159295d67482SBill Paul 	/* ack/clear link change events */
159395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
15940434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
15950434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1596f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
159795d67482SBill Paul 
159895d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
1599652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
160095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1601a1d52896SBill Paul 	} else {
160295d67482SBill Paul 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
16031f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
16044c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
1605a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1606a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1607a1d52896SBill Paul 	}
160895d67482SBill Paul 
16091f313773SOleg Bulyzhin 	/*
16101f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
16111f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
16121f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
16131f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
16141f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
16151f313773SOleg Bulyzhin 	 */
16161f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
16171f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
16181f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
16191f313773SOleg Bulyzhin 
162095d67482SBill Paul 	/* Enable link state change attentions. */
162195d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
162295d67482SBill Paul 
162395d67482SBill Paul 	return (0);
162495d67482SBill Paul }
162595d67482SBill Paul 
16264c0da0ffSGleb Smirnoff const struct bge_revision *
16274c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
16284c0da0ffSGleb Smirnoff {
16294c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
16304c0da0ffSGleb Smirnoff 
16314c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
16324c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
16334c0da0ffSGleb Smirnoff 			return (br);
16344c0da0ffSGleb Smirnoff 	}
16354c0da0ffSGleb Smirnoff 
16364c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
16374c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
16384c0da0ffSGleb Smirnoff 			return (br);
16394c0da0ffSGleb Smirnoff 	}
16404c0da0ffSGleb Smirnoff 
16414c0da0ffSGleb Smirnoff 	return (NULL);
16424c0da0ffSGleb Smirnoff }
16434c0da0ffSGleb Smirnoff 
16444c0da0ffSGleb Smirnoff const struct bge_vendor *
16454c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
16464c0da0ffSGleb Smirnoff {
16474c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
16484c0da0ffSGleb Smirnoff 
16494c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
16504c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
16514c0da0ffSGleb Smirnoff 			return (v);
16524c0da0ffSGleb Smirnoff 
16534c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
16544c0da0ffSGleb Smirnoff 	return (NULL);
16554c0da0ffSGleb Smirnoff }
16564c0da0ffSGleb Smirnoff 
165795d67482SBill Paul /*
165895d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
16594c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
16604c0da0ffSGleb Smirnoff  *
16614c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
16627c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
16637c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
16647c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
16657c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
166695d67482SBill Paul  */
166795d67482SBill Paul static int
16683f74909aSGleb Smirnoff bge_probe(device_t dev)
166995d67482SBill Paul {
16704c0da0ffSGleb Smirnoff 	struct bge_type *t = bge_devs;
16714c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
16727c929cf9SJung-uk Kim 	uint16_t vid, did;
167395d67482SBill Paul 
167495d67482SBill Paul 	sc->bge_dev = dev;
16757c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
16767c929cf9SJung-uk Kim 	did = pci_get_device(dev);
16774c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
16787c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
16797c929cf9SJung-uk Kim 			char model[64], buf[96];
16804c0da0ffSGleb Smirnoff 			const struct bge_revision *br;
16814c0da0ffSGleb Smirnoff 			const struct bge_vendor *v;
16827c929cf9SJung-uk Kim 			const char *pname;
16834c0da0ffSGleb Smirnoff 			uint32_t id;
16844c0da0ffSGleb Smirnoff 
16854c0da0ffSGleb Smirnoff 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
16864c0da0ffSGleb Smirnoff 			    BGE_PCIMISCCTL_ASICREV;
16874c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
16887c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
16897c929cf9SJung-uk Kim 			if (pci_get_vpd_ident(dev, &pname))
16907c929cf9SJung-uk Kim 				snprintf(model, 64, "%s %s",
16917c929cf9SJung-uk Kim 				    v->v_name,
16927c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
16937c929cf9SJung-uk Kim 					"NetXtreme Ethernet Controller");
16944c0da0ffSGleb Smirnoff 			else
16957c929cf9SJung-uk Kim 				snprintf(model, 64, "%s", pname);
16967c929cf9SJung-uk Kim 			snprintf(buf, 96, "%s, %sASIC rev. %#04x", model,
16977c929cf9SJung-uk Kim 			    br != NULL ? "" : "unknown ", id >> 16);
16984c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
16996d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
1700652ae483SGleb Smirnoff 				sc->bge_flags |= BGE_FLAG_NO3LED;
170195d67482SBill Paul 			return (0);
170295d67482SBill Paul 		}
170395d67482SBill Paul 		t++;
170495d67482SBill Paul 	}
170595d67482SBill Paul 
170695d67482SBill Paul 	return (ENXIO);
170795d67482SBill Paul }
170895d67482SBill Paul 
1709f41ac2beSBill Paul static void
17103f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
1711f41ac2beSBill Paul {
1712f41ac2beSBill Paul 	int i;
1713f41ac2beSBill Paul 
17143f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
1715f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1716f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1717f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1718f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1719f41ac2beSBill Paul 	}
1720f41ac2beSBill Paul 
17213f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
1722f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1723f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1724f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1725f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1726f41ac2beSBill Paul 	}
1727f41ac2beSBill Paul 
17283f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
1729f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1730f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1731f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1732f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1733f41ac2beSBill Paul 	}
1734f41ac2beSBill Paul 
1735f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1736f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1737f41ac2beSBill Paul 
1738f41ac2beSBill Paul 
17393f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
1740e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
1741e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1742e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
1743e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1744f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1745f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1746f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1747f41ac2beSBill Paul 
1748f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1749f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1750f41ac2beSBill Paul 
17513f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
1752e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1753e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1754e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1755e65bed95SPyun YongHyeon 
1756e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1757e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
1758f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1759f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1760f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1761f41ac2beSBill Paul 
1762f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1763f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1764f41ac2beSBill Paul 
17653f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
1766e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
1767e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1768e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
1769e65bed95SPyun YongHyeon 
1770e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1771e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
1772f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1773f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1774f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1775f41ac2beSBill Paul 
1776f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1777f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1778f41ac2beSBill Paul 
17793f74909aSGleb Smirnoff 	/* Destroy TX ring. */
1780e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
1781e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1782e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
1783e65bed95SPyun YongHyeon 
1784e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
1785f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1786f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1787f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1788f41ac2beSBill Paul 
1789f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1790f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1791f41ac2beSBill Paul 
17923f74909aSGleb Smirnoff 	/* Destroy status block. */
1793e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
1794e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1795e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
1796e65bed95SPyun YongHyeon 
1797e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
1798f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1799f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1800f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1801f41ac2beSBill Paul 
1802f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1803f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1804f41ac2beSBill Paul 
18053f74909aSGleb Smirnoff 	/* Destroy statistics block. */
1806e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
1807e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1808e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
1809e65bed95SPyun YongHyeon 
1810e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
1811f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1812f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1813f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1814f41ac2beSBill Paul 
1815f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1816f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1817f41ac2beSBill Paul 
18183f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
1819f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1820f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1821f41ac2beSBill Paul }
1822f41ac2beSBill Paul 
1823f41ac2beSBill Paul static int
18243f74909aSGleb Smirnoff bge_dma_alloc(device_t dev)
1825f41ac2beSBill Paul {
18263f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
1827f41ac2beSBill Paul 	struct bge_softc *sc;
18281be6acb7SGleb Smirnoff 	int i, error;
1829f41ac2beSBill Paul 
1830f41ac2beSBill Paul 	sc = device_get_softc(dev);
1831f41ac2beSBill Paul 
1832f41ac2beSBill Paul 	/*
1833f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1834f41ac2beSBill Paul 	 */
1835378f231eSJohn-Mark Gurney 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),/* parent */
1836706620f0SScott Long 			1, 0,			/* alignment, boundary */
1837f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
18382f28b973SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1839f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1840f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1841f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
18428a40c10eSScott Long 			0,			/* flags */
1843f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1844f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1845f41ac2beSBill Paul 
1846e65bed95SPyun YongHyeon 	if (error != 0) {
1847fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1848fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
1849e65bed95SPyun YongHyeon 		return (ENOMEM);
1850e65bed95SPyun YongHyeon 	}
1851e65bed95SPyun YongHyeon 
1852f41ac2beSBill Paul 	/*
1853f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1854f41ac2beSBill Paul 	 */
18558a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1856f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
18571be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
18581be6acb7SGleb Smirnoff 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
1859f41ac2beSBill Paul 
1860f41ac2beSBill Paul 	if (error) {
1861fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1862f41ac2beSBill Paul 		return (ENOMEM);
1863f41ac2beSBill Paul 	}
1864f41ac2beSBill Paul 
18653f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
1866f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1867f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1868f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1869f41ac2beSBill Paul 		if (error) {
1870fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1871fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1872f41ac2beSBill Paul 			return (ENOMEM);
1873f41ac2beSBill Paul 		}
1874f41ac2beSBill Paul 	}
1875f41ac2beSBill Paul 
18763f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
1877f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1878f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1879f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1880f41ac2beSBill Paul 		if (error) {
1881fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1882fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1883f41ac2beSBill Paul 			return (ENOMEM);
1884f41ac2beSBill Paul 		}
1885f41ac2beSBill Paul 	}
1886f41ac2beSBill Paul 
18873f74909aSGleb Smirnoff 	/* Create tag for standard RX ring. */
1888f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1889f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1890f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1891f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1892f41ac2beSBill Paul 
1893f41ac2beSBill Paul 	if (error) {
1894fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1895f41ac2beSBill Paul 		return (ENOMEM);
1896f41ac2beSBill Paul 	}
1897f41ac2beSBill Paul 
18983f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for standard RX ring. */
1899f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1900f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1901f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1902f41ac2beSBill Paul 	if (error)
1903f41ac2beSBill Paul 		return (ENOMEM);
1904f41ac2beSBill Paul 
1905f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1906f41ac2beSBill Paul 
19073f74909aSGleb Smirnoff 	/* Load the address of the standard RX ring. */
1908f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1909f41ac2beSBill Paul 	ctx.sc = sc;
1910f41ac2beSBill Paul 
1911f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1912f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1913f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1914f41ac2beSBill Paul 
1915f41ac2beSBill Paul 	if (error)
1916f41ac2beSBill Paul 		return (ENOMEM);
1917f41ac2beSBill Paul 
1918f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
1919f41ac2beSBill Paul 
19203f74909aSGleb Smirnoff 	/* Create tags for jumbo mbufs. */
19214c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1922f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
19238a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
19241be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
19251be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
1926f41ac2beSBill Paul 		if (error) {
1927fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
19283f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
1929f41ac2beSBill Paul 			return (ENOMEM);
1930f41ac2beSBill Paul 		}
1931f41ac2beSBill Paul 
19323f74909aSGleb Smirnoff 		/* Create tag for jumbo RX ring. */
1933f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1934f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1935f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
1936f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
1937f41ac2beSBill Paul 
1938f41ac2beSBill Paul 		if (error) {
1939fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
19403f74909aSGleb Smirnoff 			    "could not allocate jumbo ring dma tag\n");
1941f41ac2beSBill Paul 			return (ENOMEM);
1942f41ac2beSBill Paul 		}
1943f41ac2beSBill Paul 
19443f74909aSGleb Smirnoff 		/* Allocate DMA'able memory for jumbo RX ring. */
1945f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
19461be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
19471be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1948f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
1949f41ac2beSBill Paul 		if (error)
1950f41ac2beSBill Paul 			return (ENOMEM);
1951f41ac2beSBill Paul 
19523f74909aSGleb Smirnoff 		/* Load the address of the jumbo RX ring. */
1953f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
1954f41ac2beSBill Paul 		ctx.sc = sc;
1955f41ac2beSBill Paul 
1956f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1957f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1958f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
1959f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1960f41ac2beSBill Paul 
1961f41ac2beSBill Paul 		if (error)
1962f41ac2beSBill Paul 			return (ENOMEM);
1963f41ac2beSBill Paul 
1964f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
1965f41ac2beSBill Paul 
19663f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
1967f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1968f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
1969f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1970f41ac2beSBill Paul 			if (error) {
1971fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
19723f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
1973f41ac2beSBill Paul 				return (ENOMEM);
1974f41ac2beSBill Paul 			}
1975f41ac2beSBill Paul 		}
1976f41ac2beSBill Paul 
1977f41ac2beSBill Paul 	}
1978f41ac2beSBill Paul 
19793f74909aSGleb Smirnoff 	/* Create tag for RX return ring. */
1980f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1981f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1982f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
1983f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
1984f41ac2beSBill Paul 
1985f41ac2beSBill Paul 	if (error) {
1986fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1987f41ac2beSBill Paul 		return (ENOMEM);
1988f41ac2beSBill Paul 	}
1989f41ac2beSBill Paul 
19903f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for RX return ring. */
1991f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
1992f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
1993f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
1994f41ac2beSBill Paul 	if (error)
1995f41ac2beSBill Paul 		return (ENOMEM);
1996f41ac2beSBill Paul 
1997f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
1998f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
1999f41ac2beSBill Paul 
20003f74909aSGleb Smirnoff 	/* Load the address of the RX return ring. */
2001f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2002f41ac2beSBill Paul 	ctx.sc = sc;
2003f41ac2beSBill Paul 
2004f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
2005f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
2006f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
2007f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2008f41ac2beSBill Paul 
2009f41ac2beSBill Paul 	if (error)
2010f41ac2beSBill Paul 		return (ENOMEM);
2011f41ac2beSBill Paul 
2012f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
2013f41ac2beSBill Paul 
20143f74909aSGleb Smirnoff 	/* Create tag for TX ring. */
2015f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2016f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2017f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
2018f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
2019f41ac2beSBill Paul 
2020f41ac2beSBill Paul 	if (error) {
2021fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2022f41ac2beSBill Paul 		return (ENOMEM);
2023f41ac2beSBill Paul 	}
2024f41ac2beSBill Paul 
20253f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for TX ring. */
2026f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
2027f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
2028f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
2029f41ac2beSBill Paul 	if (error)
2030f41ac2beSBill Paul 		return (ENOMEM);
2031f41ac2beSBill Paul 
2032f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
2033f41ac2beSBill Paul 
20343f74909aSGleb Smirnoff 	/* Load the address of the TX ring. */
2035f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2036f41ac2beSBill Paul 	ctx.sc = sc;
2037f41ac2beSBill Paul 
2038f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
2039f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
2040f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2041f41ac2beSBill Paul 
2042f41ac2beSBill Paul 	if (error)
2043f41ac2beSBill Paul 		return (ENOMEM);
2044f41ac2beSBill Paul 
2045f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
2046f41ac2beSBill Paul 
20473f74909aSGleb Smirnoff 	/* Create tag for status block. */
2048f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2049f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2050f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
2051f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
2052f41ac2beSBill Paul 
2053f41ac2beSBill Paul 	if (error) {
2054fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2055f41ac2beSBill Paul 		return (ENOMEM);
2056f41ac2beSBill Paul 	}
2057f41ac2beSBill Paul 
20583f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for status block. */
2059f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
2060f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
2061f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
2062f41ac2beSBill Paul 	if (error)
2063f41ac2beSBill Paul 		return (ENOMEM);
2064f41ac2beSBill Paul 
2065f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2066f41ac2beSBill Paul 
20673f74909aSGleb Smirnoff 	/* Load the address of the status block. */
2068f41ac2beSBill Paul 	ctx.sc = sc;
2069f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2070f41ac2beSBill Paul 
2071f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2072f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2073f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2074f41ac2beSBill Paul 
2075f41ac2beSBill Paul 	if (error)
2076f41ac2beSBill Paul 		return (ENOMEM);
2077f41ac2beSBill Paul 
2078f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2079f41ac2beSBill Paul 
20803f74909aSGleb Smirnoff 	/* Create tag for statistics block. */
2081f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2082f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2083f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2084f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
2085f41ac2beSBill Paul 
2086f41ac2beSBill Paul 	if (error) {
2087fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2088f41ac2beSBill Paul 		return (ENOMEM);
2089f41ac2beSBill Paul 	}
2090f41ac2beSBill Paul 
20913f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for statistics block. */
2092f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2093f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2094f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
2095f41ac2beSBill Paul 	if (error)
2096f41ac2beSBill Paul 		return (ENOMEM);
2097f41ac2beSBill Paul 
2098f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2099f41ac2beSBill Paul 
21003f74909aSGleb Smirnoff 	/* Load the address of the statstics block. */
2101f41ac2beSBill Paul 	ctx.sc = sc;
2102f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2103f41ac2beSBill Paul 
2104f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2105f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2106f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2107f41ac2beSBill Paul 
2108f41ac2beSBill Paul 	if (error)
2109f41ac2beSBill Paul 		return (ENOMEM);
2110f41ac2beSBill Paul 
2111f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2112f41ac2beSBill Paul 
2113f41ac2beSBill Paul 	return (0);
2114f41ac2beSBill Paul }
2115f41ac2beSBill Paul 
211695d67482SBill Paul static int
21173f74909aSGleb Smirnoff bge_attach(device_t dev)
211895d67482SBill Paul {
211995d67482SBill Paul 	struct ifnet *ifp;
212095d67482SBill Paul 	struct bge_softc *sc;
21213f74909aSGleb Smirnoff 	uint32_t hwcfg = 0;
21223f74909aSGleb Smirnoff 	uint32_t mac_tmp = 0;
2123fc74a9f9SBrooks Davis 	u_char eaddr[6];
2124724bd939SJohn Polstra 	int error = 0, msicount, rid, trys, reg;
212595d67482SBill Paul 
212695d67482SBill Paul 	sc = device_get_softc(dev);
212795d67482SBill Paul 	sc->bge_dev = dev;
212895d67482SBill Paul 
212995d67482SBill Paul 	/*
213095d67482SBill Paul 	 * Map control/status registers.
213195d67482SBill Paul 	 */
213295d67482SBill Paul 	pci_enable_busmaster(dev);
213395d67482SBill Paul 
213495d67482SBill Paul 	rid = BGE_PCI_BAR0;
21355f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
21365f96beb9SNate Lawson 	    RF_ACTIVE|PCI_RF_DENSE);
213795d67482SBill Paul 
213895d67482SBill Paul 	if (sc->bge_res == NULL) {
2139fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
214095d67482SBill Paul 		error = ENXIO;
214195d67482SBill Paul 		goto fail;
214295d67482SBill Paul 	}
214395d67482SBill Paul 
214495d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
214595d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
214695d67482SBill Paul 
2147724bd939SJohn Polstra 	/*
2148724bd939SJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
2149724bd939SJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
2150724bd939SJohn Polstra 	 * normal operation.
2151724bd939SJohn Polstra 	 */
2152724bd939SJohn Polstra 	if ((msicount = pci_msi_count(dev)) > 1)
2153724bd939SJohn Polstra 		msicount = 1;
215420aa3e48SJohn Polstra #ifdef BGE_DISABLE_MSI
215520aa3e48SJohn Polstra 	msicount = 0;
215620aa3e48SJohn Polstra #endif
2157724bd939SJohn Polstra 	if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
2158724bd939SJohn Polstra 		rid = 1;
2159724bd939SJohn Polstra 		sc->bge_flags |= BGE_FLAG_MSI;
2160724bd939SJohn Polstra 	} else
216195d67482SBill Paul 		rid = 0;
216295d67482SBill Paul 
21635f96beb9SNate Lawson 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
216495d67482SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
216595d67482SBill Paul 
216695d67482SBill Paul 	if (sc->bge_irq == NULL) {
2167fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
216895d67482SBill Paul 		error = ENXIO;
216995d67482SBill Paul 		goto fail;
217095d67482SBill Paul 	}
217195d67482SBill Paul 
21720f9bd73bSSam Leffler 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
21730f9bd73bSSam Leffler 
2174e53d81eeSPaul Saab 	/* Save ASIC rev. */
2175e53d81eeSPaul Saab 
2176e53d81eeSPaul Saab 	sc->bge_chipid =
2177e53d81eeSPaul Saab 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2178e53d81eeSPaul Saab 	    BGE_PCIMISCCTL_ASICREV;
2179e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2180e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2181e53d81eeSPaul Saab 
21820dae9719SJung-uk Kim 	/* Save chipset family. */
21830dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
21840dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
21850dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
21860dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
21870dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
21887ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
21890dae9719SJung-uk Kim 		break;
21900dae9719SJung-uk Kim 
21910dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
21920dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
21930dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
21947ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5714_FAMILY /* | BGE_FLAG_JUMBO */;
21950dae9719SJung-uk Kim 		/* Fall through */
21960dae9719SJung-uk Kim 
21970dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
21980dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
21990dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5755:
22000dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5787:
22010dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
22020dae9719SJung-uk Kim 		/* Fall through */
22030dae9719SJung-uk Kim 
22040dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
22050dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
22060dae9719SJung-uk Kim 		break;
22070dae9719SJung-uk Kim 	}
22080dae9719SJung-uk Kim 
2209e53d81eeSPaul Saab   	/*
22106f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
2211e53d81eeSPaul Saab   	 */
22126f8718a3SScott Long #if __FreeBSD_version > 700010
22136f8718a3SScott Long 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
22144c0da0ffSGleb Smirnoff 		/*
22156f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
22166f8718a3SScott Long 		 * must be a PCI Express device.
22176f8718a3SScott Long 		 */
22186f8718a3SScott Long 		if (reg != 0)
22196f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIE;
22206f8718a3SScott Long 	} else if (pci_find_extcap(dev, PCIY_PCIX, &reg) == 0) {
22216f8718a3SScott Long 		if (reg != 0)
22226f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIX;
22236f8718a3SScott Long 	}
22246f8718a3SScott Long 
22256f8718a3SScott Long #else
22265345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc)) {
22276f8718a3SScott Long 		reg = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
22286f8718a3SScott Long 		if ((reg & 0xff) == BGE_PCIE_CAPID)
22296f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIE;
22306f8718a3SScott Long 	} else {
22316f8718a3SScott Long 		/*
22326f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
22336f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
22344c0da0ffSGleb Smirnoff 		 */
22354c0da0ffSGleb Smirnoff 		if ((pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
22364c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
2237652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
22386f8718a3SScott Long 	}
22396f8718a3SScott Long #endif
22404c0da0ffSGleb Smirnoff 
224195d67482SBill Paul 	/* Try to reset the chip. */
22428cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
22438cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
22448cb1383cSDoug Ambrisko 		bge_release_resources(sc);
22458cb1383cSDoug Ambrisko 		error = ENXIO;
22468cb1383cSDoug Ambrisko 		goto fail;
22478cb1383cSDoug Ambrisko 	}
22488cb1383cSDoug Ambrisko 
22498cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
2250f1a7e6d5SScott Long 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG)
2251f1a7e6d5SScott Long 	    == BGE_MAGIC_NUMBER)) {
22528cb1383cSDoug Ambrisko 		if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG)
22538cb1383cSDoug Ambrisko 		    & BGE_HWCFG_ASF) {
22548cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_ENABLE;
22558cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_STACKUP;
22568cb1383cSDoug Ambrisko 			if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
22578cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
22588cb1383cSDoug Ambrisko 			}
22598cb1383cSDoug Ambrisko 		}
22608cb1383cSDoug Ambrisko 	}
22618cb1383cSDoug Ambrisko 
22628cb1383cSDoug Ambrisko 	/* Try to reset the chip again the nice way. */
22638cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
22648cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
22658cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
22668cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
22678cb1383cSDoug Ambrisko 		bge_release_resources(sc);
22688cb1383cSDoug Ambrisko 		error = ENXIO;
22698cb1383cSDoug Ambrisko 		goto fail;
22708cb1383cSDoug Ambrisko 	}
22718cb1383cSDoug Ambrisko 
22728cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
22738cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
227495d67482SBill Paul 
227595d67482SBill Paul 	if (bge_chipinit(sc)) {
2276fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
227795d67482SBill Paul 		bge_release_resources(sc);
227895d67482SBill Paul 		error = ENXIO;
227995d67482SBill Paul 		goto fail;
228095d67482SBill Paul 	}
228195d67482SBill Paul 
228295d67482SBill Paul 	/*
228395d67482SBill Paul 	 * Get station address from the EEPROM.
228495d67482SBill Paul 	 */
2285fc74a9f9SBrooks Davis 	mac_tmp = bge_readmem_ind(sc, 0x0c14);
2286fc74a9f9SBrooks Davis 	if ((mac_tmp >> 16) == 0x484b) {
2287fc74a9f9SBrooks Davis 		eaddr[0] = (u_char)(mac_tmp >> 8);
2288fc74a9f9SBrooks Davis 		eaddr[1] = (u_char)mac_tmp;
2289fc74a9f9SBrooks Davis 		mac_tmp = bge_readmem_ind(sc, 0x0c18);
2290fc74a9f9SBrooks Davis 		eaddr[2] = (u_char)(mac_tmp >> 24);
2291fc74a9f9SBrooks Davis 		eaddr[3] = (u_char)(mac_tmp >> 16);
2292fc74a9f9SBrooks Davis 		eaddr[4] = (u_char)(mac_tmp >> 8);
2293fc74a9f9SBrooks Davis 		eaddr[5] = (u_char)mac_tmp;
2294fc74a9f9SBrooks Davis 	} else if (bge_read_eeprom(sc, eaddr,
229595d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2296fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to read station address\n");
229795d67482SBill Paul 		bge_release_resources(sc);
229895d67482SBill Paul 		error = ENXIO;
229995d67482SBill Paul 		goto fail;
230095d67482SBill Paul 	}
230195d67482SBill Paul 
2302f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
23037ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
2304f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2305f41ac2beSBill Paul 	else
2306f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2307f41ac2beSBill Paul 
2308f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2309fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2310fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2311f41ac2beSBill Paul 		bge_release_resources(sc);
2312f41ac2beSBill Paul 		error = ENXIO;
2313f41ac2beSBill Paul 		goto fail;
2314f41ac2beSBill Paul 	}
2315f41ac2beSBill Paul 
231695d67482SBill Paul 	/* Set default tuneable values. */
231795d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
231895d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
231995d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
23206f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
23216f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
232295d67482SBill Paul 
232395d67482SBill Paul 	/* Set up ifnet structure */
2324fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2325fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2326fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2327fc74a9f9SBrooks Davis 		bge_release_resources(sc);
2328fc74a9f9SBrooks Davis 		error = ENXIO;
2329fc74a9f9SBrooks Davis 		goto fail;
2330fc74a9f9SBrooks Davis 	}
233195d67482SBill Paul 	ifp->if_softc = sc;
23329bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
233395d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
233495d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
233595d67482SBill Paul 	ifp->if_start = bge_start;
233695d67482SBill Paul 	ifp->if_init = bge_init;
233795d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
23384d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
23394d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
23404d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
234195d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2342d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
2343479b23b7SGleb Smirnoff 	    IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM;
234495d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
234575719184SGleb Smirnoff #ifdef DEVICE_POLLING
234675719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
234775719184SGleb Smirnoff #endif
234895d67482SBill Paul 
2349a1d52896SBill Paul 	/*
2350d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2351d375e524SGleb Smirnoff 	 * to hardware bugs.
2352d375e524SGleb Smirnoff 	 */
2353d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2354d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2355d375e524SGleb Smirnoff 		ifp->if_capenable &= IFCAP_HWCSUM;
2356d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2357d375e524SGleb Smirnoff 	}
2358d375e524SGleb Smirnoff 
2359d375e524SGleb Smirnoff 	/*
2360a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
236141abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
236241abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
236341abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
236441abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
236541abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
236641abcc1bSPaul Saab 	 * SK-9D41.
2367a1d52896SBill Paul 	 */
236841abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
236941abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
237041abcc1bSPaul Saab 	else {
2371f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2372f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2373fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2374f6789fbaSPyun YongHyeon 			bge_release_resources(sc);
2375f6789fbaSPyun YongHyeon 			error = ENXIO;
2376f6789fbaSPyun YongHyeon 			goto fail;
2377f6789fbaSPyun YongHyeon 		}
237841abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
237941abcc1bSPaul Saab 	}
238041abcc1bSPaul Saab 
238141abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2382652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
2383a1d52896SBill Paul 
238495d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
238595d67482SBill Paul 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
2386652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
238795d67482SBill Paul 
2388652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
238995d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
239095d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
239195d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
239295d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia,
239395d67482SBill Paul 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
239495d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
239595d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2396da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
239795d67482SBill Paul 	} else {
239895d67482SBill Paul 		/*
23998cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
24008cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
24018cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
24028cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
24038cb1383cSDoug Ambrisko 		 * the PHY.
240495d67482SBill Paul 		 */
24058cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
24068cb1383cSDoug Ambrisko again:
24078cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
24088cb1383cSDoug Ambrisko 
24098cb1383cSDoug Ambrisko 		trys = 0;
241095d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
241195d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
24128cb1383cSDoug Ambrisko 			if (trys++ < 4) {
24138cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
24148cb1383cSDoug Ambrisko 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR, BMCR_RESET);
24158cb1383cSDoug Ambrisko 				goto again;
24168cb1383cSDoug Ambrisko 			}
24178cb1383cSDoug Ambrisko 
2418fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
241995d67482SBill Paul 			bge_release_resources(sc);
242095d67482SBill Paul 			error = ENXIO;
242195d67482SBill Paul 			goto fail;
242295d67482SBill Paul 		}
24238cb1383cSDoug Ambrisko 
24248cb1383cSDoug Ambrisko 		/*
24258cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
24268cb1383cSDoug Ambrisko 		 */
24278cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
24288cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
242995d67482SBill Paul 	}
243095d67482SBill Paul 
243195d67482SBill Paul 	/*
2432e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2433e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2434e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2435e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2436e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2437e255b776SJohn Polstra 	 * payloads by copying the received packets.
2438e255b776SJohn Polstra 	 */
2439652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
2440652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
2441652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
2442e255b776SJohn Polstra 
2443e255b776SJohn Polstra 	/*
244495d67482SBill Paul 	 * Call MI attach routine.
244595d67482SBill Paul 	 */
2446fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
2447b74e67fbSGleb Smirnoff 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
24480f9bd73bSSam Leffler 
24490f9bd73bSSam Leffler 	/*
24500f9bd73bSSam Leffler 	 * Hookup IRQ last.
24510f9bd73bSSam Leffler 	 */
24520f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
24530f9bd73bSSam Leffler 	   bge_intr, sc, &sc->bge_intrhand);
24540f9bd73bSSam Leffler 
24550f9bd73bSSam Leffler 	if (error) {
2456fc74a9f9SBrooks Davis 		bge_detach(dev);
2457fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
24580f9bd73bSSam Leffler 	}
245995d67482SBill Paul 
24606f8718a3SScott Long 	bge_add_sysctls(sc);
24616f8718a3SScott Long 
246295d67482SBill Paul fail:
246395d67482SBill Paul 	return (error);
246495d67482SBill Paul }
246595d67482SBill Paul 
246695d67482SBill Paul static int
24673f74909aSGleb Smirnoff bge_detach(device_t dev)
246895d67482SBill Paul {
246995d67482SBill Paul 	struct bge_softc *sc;
247095d67482SBill Paul 	struct ifnet *ifp;
247195d67482SBill Paul 
247295d67482SBill Paul 	sc = device_get_softc(dev);
2473fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
247495d67482SBill Paul 
247575719184SGleb Smirnoff #ifdef DEVICE_POLLING
247675719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
247775719184SGleb Smirnoff 		ether_poll_deregister(ifp);
247875719184SGleb Smirnoff #endif
247975719184SGleb Smirnoff 
24800f9bd73bSSam Leffler 	BGE_LOCK(sc);
248195d67482SBill Paul 	bge_stop(sc);
248295d67482SBill Paul 	bge_reset(sc);
24830f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
24840f9bd73bSSam Leffler 
24850f9bd73bSSam Leffler 	ether_ifdetach(ifp);
248695d67482SBill Paul 
2487652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
248895d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
248995d67482SBill Paul 	} else {
249095d67482SBill Paul 		bus_generic_detach(dev);
249195d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
249295d67482SBill Paul 	}
249395d67482SBill Paul 
249495d67482SBill Paul 	bge_release_resources(sc);
249595d67482SBill Paul 
249695d67482SBill Paul 	return (0);
249795d67482SBill Paul }
249895d67482SBill Paul 
249995d67482SBill Paul static void
25003f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
250195d67482SBill Paul {
250295d67482SBill Paul 	device_t dev;
250395d67482SBill Paul 
250495d67482SBill Paul 	dev = sc->bge_dev;
250595d67482SBill Paul 
250695d67482SBill Paul 	if (sc->bge_intrhand != NULL)
250795d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
250895d67482SBill Paul 
250995d67482SBill Paul 	if (sc->bge_irq != NULL)
2510724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
2511724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
2512724bd939SJohn Polstra 
2513724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
2514724bd939SJohn Polstra 		pci_release_msi(dev);
251595d67482SBill Paul 
251695d67482SBill Paul 	if (sc->bge_res != NULL)
251795d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
251895d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
251995d67482SBill Paul 
2520ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2521ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2522ad61f896SRuslan Ermilov 
2523f41ac2beSBill Paul 	bge_dma_free(sc);
252495d67482SBill Paul 
25250f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
25260f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
252795d67482SBill Paul }
252895d67482SBill Paul 
25298cb1383cSDoug Ambrisko static int
25303f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
253195d67482SBill Paul {
253295d67482SBill Paul 	device_t dev;
25333f74909aSGleb Smirnoff 	uint32_t cachesize, command, pcistate, reset;
25346f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
253595d67482SBill Paul 	int i, val = 0;
253695d67482SBill Paul 
253795d67482SBill Paul 	dev = sc->bge_dev;
253895d67482SBill Paul 
25399ba784dbSScott Long 	if (BGE_IS_5705_PLUS(sc) && !BGE_IS_5714_FAMILY(sc)) {
25406f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
25416f8718a3SScott Long 			write_op = bge_writemem_direct;
25426f8718a3SScott Long 		else
25436f8718a3SScott Long 			write_op = bge_writemem_ind;
25449ba784dbSScott Long 	} else
25456f8718a3SScott Long 		write_op = bge_writereg_ind;
25466f8718a3SScott Long 
254795d67482SBill Paul 	/* Save some important PCI state. */
254895d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
254995d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
255095d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
255195d67482SBill Paul 
255295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
255395d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2554e907febfSPyun YongHyeon 	BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
255595d67482SBill Paul 
25566f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
25576f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
25586f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
25596f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
25606f8718a3SScott Long 		if (bootverbose)
25619ba784dbSScott Long 			device_printf(sc->bge_dev, "Disabling fastboot\n");
25626f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
25636f8718a3SScott Long 	}
25646f8718a3SScott Long 
25656f8718a3SScott Long 	/*
25666f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
25676f8718a3SScott Long 	 * When firmware finishes its initialization it will
25686f8718a3SScott Long 	 * write ~BGE_MAGIC_NUMBER to the same location.
25696f8718a3SScott Long 	 */
25706f8718a3SScott Long 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
25716f8718a3SScott Long 
2572e53d81eeSPaul Saab 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2573e53d81eeSPaul Saab 
2574e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2575652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2576e53d81eeSPaul Saab 		if (CSR_READ_4(sc, 0x7e2c) == 0x60)	/* PCIE 1.0 */
2577e53d81eeSPaul Saab 			CSR_WRITE_4(sc, 0x7e2c, 0x20);
2578e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2579e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
2580e53d81eeSPaul Saab 			CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2581e53d81eeSPaul Saab 			reset |= (1<<29);
2582e53d81eeSPaul Saab 		}
2583e53d81eeSPaul Saab 	}
2584e53d81eeSPaul Saab 
258521c9e407SDavid Christensen 	/*
25866f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
25876f8718a3SScott Long 	 * powered up in D0 uninitialized.
25886f8718a3SScott Long 	 */
25895345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
25906f8718a3SScott Long 		reset |= 0x04000000;
25916f8718a3SScott Long 
259295d67482SBill Paul 	/* Issue global reset */
25936f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
259495d67482SBill Paul 
259595d67482SBill Paul 	DELAY(1000);
259695d67482SBill Paul 
2597e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2598652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2599e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2600e53d81eeSPaul Saab 			uint32_t v;
2601e53d81eeSPaul Saab 
2602e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
2603e53d81eeSPaul Saab 			v = pci_read_config(dev, 0xc4, 4);
2604e53d81eeSPaul Saab 			pci_write_config(dev, 0xc4, v | (1<<15), 4);
2605e53d81eeSPaul Saab 		}
26069ba784dbSScott Long 		/*
26079ba784dbSScott Long 		 * Set PCIE max payload size to 128 bytes and clear error
26089ba784dbSScott Long 		 * status.
26099ba784dbSScott Long 		 */
2610e53d81eeSPaul Saab 		pci_write_config(dev, 0xd8, 0xf5000, 4);
2611e53d81eeSPaul Saab 	}
2612e53d81eeSPaul Saab 
26133f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
261495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
261595d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2616e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
261795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
261895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
26196f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, (65 << 1));
262095d67482SBill Paul 
2621a7b0c314SPaul Saab 	/* Enable memory arbiter. */
26224c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
26234c0da0ffSGleb Smirnoff 		uint32_t val;
26244c0da0ffSGleb Smirnoff 
26254c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
26264c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
26274c0da0ffSGleb Smirnoff 	} else
2628a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2629a7b0c314SPaul Saab 
263095d67482SBill Paul 	/*
26316f8718a3SScott Long 	 * Poll until we see the 1's complement of the magic number.
263295d67482SBill Paul 	 * This indicates that the firmware initialization
263395d67482SBill Paul 	 * is complete.
263495d67482SBill Paul 	 */
263595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
263695d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
263795d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
263895d67482SBill Paul 			break;
263995d67482SBill Paul 		DELAY(10);
264095d67482SBill Paul 	}
264195d67482SBill Paul 
264295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
26439ba784dbSScott Long 		device_printf(sc->bge_dev, "firmware handshake timed out, "
26449ba784dbSScott Long 		    "found 0x%08x\n", val);
264595d67482SBill Paul 	}
264695d67482SBill Paul 
264795d67482SBill Paul 	/*
264895d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
264995d67482SBill Paul 	 * return to its original pre-reset state. This is a
265095d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
265195d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
265295d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
265395d67482SBill Paul 	 * results.
265495d67482SBill Paul 	 */
265595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
265695d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
265795d67482SBill Paul 			break;
265895d67482SBill Paul 		DELAY(10);
265995d67482SBill Paul 	}
266095d67482SBill Paul 
26616f8718a3SScott Long 	if (sc->bge_flags & BGE_FLAG_PCIE) {
26626f8718a3SScott Long 		reset = bge_readmem_ind(sc, 0x7c00);
26636f8718a3SScott Long 		bge_writemem_ind(sc, 0x7c00, reset | (1 << 25));
26646f8718a3SScott Long 	}
26656f8718a3SScott Long 
26663f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
2667e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
266895d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
266995d67482SBill Paul 
26708cb1383cSDoug Ambrisko 	/* Tell the ASF firmware we are up */
26718cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
26728cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
26738cb1383cSDoug Ambrisko 
267495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
267595d67482SBill Paul 
2676da3003f0SBill Paul 	/*
2677da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
2678da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
2679da3003f0SBill Paul 	 * to 1.2V.
2680da3003f0SBill Paul 	 */
2681652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
2682652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
2683da3003f0SBill Paul 		uint32_t serdescfg;
2684652ae483SGleb Smirnoff 
2685da3003f0SBill Paul 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2686da3003f0SBill Paul 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
2687da3003f0SBill Paul 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2688da3003f0SBill Paul 	}
2689da3003f0SBill Paul 
2690e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2691652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
2692652ae483SGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2693e53d81eeSPaul Saab 		uint32_t v;
2694e53d81eeSPaul Saab 
2695e53d81eeSPaul Saab 		v = CSR_READ_4(sc, 0x7c00);
2696e53d81eeSPaul Saab 		CSR_WRITE_4(sc, 0x7c00, v | (1<<25));
2697e53d81eeSPaul Saab 	}
269895d67482SBill Paul 	DELAY(10000);
26998cb1383cSDoug Ambrisko 
27008cb1383cSDoug Ambrisko 	return(0);
270195d67482SBill Paul }
270295d67482SBill Paul 
270395d67482SBill Paul /*
270495d67482SBill Paul  * Frame reception handling. This is called if there's a frame
270595d67482SBill Paul  * on the receive return list.
270695d67482SBill Paul  *
270795d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
27081be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
270995d67482SBill Paul  * 2) the frame is from the standard receive ring
271095d67482SBill Paul  */
271195d67482SBill Paul 
271295d67482SBill Paul static void
27133f74909aSGleb Smirnoff bge_rxeof(struct bge_softc *sc)
271495d67482SBill Paul {
271595d67482SBill Paul 	struct ifnet *ifp;
271695d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
271795d67482SBill Paul 
27180f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
27190f9bd73bSSam Leffler 
27203f74909aSGleb Smirnoff 	/* Nothing to do. */
2721cfcb5025SOleg Bulyzhin 	if (sc->bge_rx_saved_considx ==
2722cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx)
2723cfcb5025SOleg Bulyzhin 		return;
2724cfcb5025SOleg Bulyzhin 
2725fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
272695d67482SBill Paul 
2727f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2728e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
2729f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2730f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
27314c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
2732f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
27334c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTREAD);
2734f41ac2beSBill Paul 
273595d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2736f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
273795d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
27383f74909aSGleb Smirnoff 		uint32_t		rxidx;
273995d67482SBill Paul 		struct mbuf		*m = NULL;
27403f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
274195d67482SBill Paul 		int			have_tag = 0;
274295d67482SBill Paul 
274375719184SGleb Smirnoff #ifdef DEVICE_POLLING
274475719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
274575719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
274675719184SGleb Smirnoff 				break;
274775719184SGleb Smirnoff 			sc->rxcycles--;
274875719184SGleb Smirnoff 		}
274975719184SGleb Smirnoff #endif
275075719184SGleb Smirnoff 
275195d67482SBill Paul 		cur_rx =
2752f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
275395d67482SBill Paul 
275495d67482SBill Paul 		rxidx = cur_rx->bge_idx;
27550434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
275695d67482SBill Paul 
27573e9b1bcaSJung-uk Kim 		if (!(ifp->if_flags & IFF_PROMISC) &&
27583e9b1bcaSJung-uk Kim 		    (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG)) {
275995d67482SBill Paul 			have_tag = 1;
276095d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
276195d67482SBill Paul 		}
276295d67482SBill Paul 
276395d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
276495d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2765f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2766f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2767f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2768f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2769f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
277095d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
277195d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
277295d67482SBill Paul 			jumbocnt++;
277395d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
277495d67482SBill Paul 				ifp->if_ierrors++;
277595d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
277695d67482SBill Paul 				continue;
277795d67482SBill Paul 			}
277895d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
277995d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
278095d67482SBill Paul 				ifp->if_ierrors++;
278195d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
278295d67482SBill Paul 				continue;
278395d67482SBill Paul 			}
278495d67482SBill Paul 		} else {
278595d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2786f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2787f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2788f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2789f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2790f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
279195d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
279295d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
279395d67482SBill Paul 			stdcnt++;
279495d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
279595d67482SBill Paul 				ifp->if_ierrors++;
279695d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
279795d67482SBill Paul 				continue;
279895d67482SBill Paul 			}
279995d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
280095d67482SBill Paul 			    NULL) == ENOBUFS) {
280195d67482SBill Paul 				ifp->if_ierrors++;
280295d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
280395d67482SBill Paul 				continue;
280495d67482SBill Paul 			}
280595d67482SBill Paul 		}
280695d67482SBill Paul 
280795d67482SBill Paul 		ifp->if_ipackets++;
2808e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2809e255b776SJohn Polstra 		/*
2810e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
2811e65bed95SPyun YongHyeon 		 * the payload is aligned.
2812e255b776SJohn Polstra 		 */
2813652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
2814e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2815e255b776SJohn Polstra 			    cur_rx->bge_len);
2816e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2817e255b776SJohn Polstra 		}
2818e255b776SJohn Polstra #endif
2819473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
282095d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
282195d67482SBill Paul 
2822b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
282378178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
282495d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
282595d67482SBill Paul 				if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
282695d67482SBill Paul 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
282778178cd1SGleb Smirnoff 			}
2828d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
2829d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
283095d67482SBill Paul 				m->m_pkthdr.csum_data =
283195d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
2832ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
2833ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
283495d67482SBill Paul 			}
283595d67482SBill Paul 		}
283695d67482SBill Paul 
283795d67482SBill Paul 		/*
2838673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
2839673d9191SSam Leffler 		 * attach that information to the packet.
284095d67482SBill Paul 		 */
2841d147662cSGleb Smirnoff 		if (have_tag) {
284278ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
284378ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
2844d147662cSGleb Smirnoff 		}
284595d67482SBill Paul 
28460f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
2847673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
28480f9bd73bSSam Leffler 		BGE_LOCK(sc);
284995d67482SBill Paul 	}
285095d67482SBill Paul 
2851e65bed95SPyun YongHyeon 	if (stdcnt > 0)
2852f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2853e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
28544c0da0ffSGleb Smirnoff 
28554c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc) && jumbocnt > 0)
2856f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
28574c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
2858f41ac2beSBill Paul 
285995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
286095d67482SBill Paul 	if (stdcnt)
286195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
286295d67482SBill Paul 	if (jumbocnt)
286395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
28646b037352SJung-uk Kim #ifdef notyet
28656b037352SJung-uk Kim 	/*
28666b037352SJung-uk Kim 	 * This register wraps very quickly under heavy packet drops.
28676b037352SJung-uk Kim 	 * If you need correct statistics, you can enable this check.
28686b037352SJung-uk Kim 	 */
28696b037352SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
28706b037352SJung-uk Kim 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
28716b037352SJung-uk Kim #endif
287295d67482SBill Paul }
287395d67482SBill Paul 
287495d67482SBill Paul static void
28753f74909aSGleb Smirnoff bge_txeof(struct bge_softc *sc)
287695d67482SBill Paul {
287795d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
287895d67482SBill Paul 	struct ifnet *ifp;
287995d67482SBill Paul 
28800f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
28810f9bd73bSSam Leffler 
28823f74909aSGleb Smirnoff 	/* Nothing to do. */
2883cfcb5025SOleg Bulyzhin 	if (sc->bge_tx_saved_considx ==
2884cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx)
2885cfcb5025SOleg Bulyzhin 		return;
2886cfcb5025SOleg Bulyzhin 
2887fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
288895d67482SBill Paul 
2889e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
2890e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
2891e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
289295d67482SBill Paul 	/*
289395d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
289495d67482SBill Paul 	 * frames that have been sent.
289595d67482SBill Paul 	 */
289695d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
2897f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
28983f74909aSGleb Smirnoff 		uint32_t		idx = 0;
289995d67482SBill Paul 
290095d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
2901f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
290295d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
290395d67482SBill Paul 			ifp->if_opackets++;
290495d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
2905e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2906e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
2907e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
2908f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2909f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
2910e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
2911e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
291295d67482SBill Paul 		}
291395d67482SBill Paul 		sc->bge_txcnt--;
291495d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
2915b74e67fbSGleb Smirnoff 		sc->bge_timer = 0;
291695d67482SBill Paul 	}
291795d67482SBill Paul 
291895d67482SBill Paul 	if (cur_tx != NULL)
291913f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
292095d67482SBill Paul }
292195d67482SBill Paul 
292275719184SGleb Smirnoff #ifdef DEVICE_POLLING
292375719184SGleb Smirnoff static void
292475719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
292575719184SGleb Smirnoff {
292675719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
2927366454f2SOleg Bulyzhin 	uint32_t statusword;
292875719184SGleb Smirnoff 
29293f74909aSGleb Smirnoff 	BGE_LOCK(sc);
29303f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
29313f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
29323f74909aSGleb Smirnoff 		return;
29333f74909aSGleb Smirnoff 	}
293475719184SGleb Smirnoff 
2935dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2936e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2937dab5cd05SOleg Bulyzhin 
29383f74909aSGleb Smirnoff 	statusword = atomic_readandclear_32(
29393f74909aSGleb Smirnoff 	    &sc->bge_ldata.bge_status_block->bge_status);
2940dab5cd05SOleg Bulyzhin 
2941dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2942e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2943366454f2SOleg Bulyzhin 
2944366454f2SOleg Bulyzhin 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS cmd */
2945366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
2946366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
2947366454f2SOleg Bulyzhin 
2948366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
2949366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
29504c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
2951652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
2952366454f2SOleg Bulyzhin 			bge_link_upd(sc);
2953366454f2SOleg Bulyzhin 
2954366454f2SOleg Bulyzhin 	sc->rxcycles = count;
2955366454f2SOleg Bulyzhin 	bge_rxeof(sc);
2956366454f2SOleg Bulyzhin 	bge_txeof(sc);
2957366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2958366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
29593f74909aSGleb Smirnoff 
29603f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
296175719184SGleb Smirnoff }
296275719184SGleb Smirnoff #endif /* DEVICE_POLLING */
296375719184SGleb Smirnoff 
296495d67482SBill Paul static void
29653f74909aSGleb Smirnoff bge_intr(void *xsc)
296695d67482SBill Paul {
296795d67482SBill Paul 	struct bge_softc *sc;
296895d67482SBill Paul 	struct ifnet *ifp;
2969dab5cd05SOleg Bulyzhin 	uint32_t statusword;
297095d67482SBill Paul 
297195d67482SBill Paul 	sc = xsc;
2972f41ac2beSBill Paul 
29730f9bd73bSSam Leffler 	BGE_LOCK(sc);
29740f9bd73bSSam Leffler 
2975dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
2976dab5cd05SOleg Bulyzhin 
297775719184SGleb Smirnoff #ifdef DEVICE_POLLING
297875719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
297975719184SGleb Smirnoff 		BGE_UNLOCK(sc);
298075719184SGleb Smirnoff 		return;
298175719184SGleb Smirnoff 	}
298275719184SGleb Smirnoff #endif
298375719184SGleb Smirnoff 
2984f30cbfc6SScott Long 	/*
2985f30cbfc6SScott Long 	 * Do the mandatory PCI flush as well as get the link status.
2986f30cbfc6SScott Long 	 */
2987f30cbfc6SScott Long 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
2988f41ac2beSBill Paul 
298995d67482SBill Paul 	/* Ack interrupt and stop others from occuring. */
299095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
299195d67482SBill Paul 
2992f30cbfc6SScott Long 	/* Make sure the descriptor ring indexes are coherent. */
2993f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2994f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2995f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2996f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2997f30cbfc6SScott Long 
29981f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
29994c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3000f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
3001dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
300295d67482SBill Paul 
300313f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
30043f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
300595d67482SBill Paul 		bge_rxeof(sc);
300695d67482SBill Paul 
30073f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
300895d67482SBill Paul 		bge_txeof(sc);
300995d67482SBill Paul 	}
301095d67482SBill Paul 
301195d67482SBill Paul 	/* Re-enable interrupts. */
301295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
301395d67482SBill Paul 
301413f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
301513f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
30160f9bd73bSSam Leffler 		bge_start_locked(ifp);
30170f9bd73bSSam Leffler 
30180f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
301995d67482SBill Paul }
302095d67482SBill Paul 
302195d67482SBill Paul static void
30228cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
30238cb1383cSDoug Ambrisko {
30248cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
30258cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
30268cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
30278cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
30288cb1383cSDoug Ambrisko 		else {
30298cb1383cSDoug Ambrisko 			sc->bge_asf_count = 5;
30308cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW,
30318cb1383cSDoug Ambrisko 			    BGE_FW_DRV_ALIVE);
30328cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_LEN, 4);
30338cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_DATA, 3);
30348cb1383cSDoug Ambrisko 			CSR_WRITE_4(sc, BGE_CPU_EVENT,
30358cb1383cSDoug Ambrisko 			    CSR_READ_4(sc, BGE_CPU_EVENT) != (1 << 14));
30368cb1383cSDoug Ambrisko 		}
30378cb1383cSDoug Ambrisko 	}
30388cb1383cSDoug Ambrisko }
30398cb1383cSDoug Ambrisko 
30408cb1383cSDoug Ambrisko static void
3041b74e67fbSGleb Smirnoff bge_tick(void *xsc)
30420f9bd73bSSam Leffler {
3043b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
304495d67482SBill Paul 	struct mii_data *mii = NULL;
304595d67482SBill Paul 
30460f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
304795d67482SBill Paul 
30487ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
30490434d1b8SBill Paul 		bge_stats_update_regs(sc);
30500434d1b8SBill Paul 	else
305195d67482SBill Paul 		bge_stats_update(sc);
305295d67482SBill Paul 
3053652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
305495d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
30558cb1383cSDoug Ambrisko 		/* Don't mess with the PHY in IPMI/ASF mode */
30568cb1383cSDoug Ambrisko 		if (!((sc->bge_asf_mode & ASF_STACKUP) && (sc->bge_link)))
305795d67482SBill Paul 			mii_tick(mii);
30587b97099dSOleg Bulyzhin 	} else {
30597b97099dSOleg Bulyzhin 		/*
30607b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
30617b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
30627b97099dSOleg Bulyzhin 		 * and trigger interrupt.
30637b97099dSOleg Bulyzhin 		 */
30647b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
30653f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
30667b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
30677b97099dSOleg Bulyzhin #endif
30687b97099dSOleg Bulyzhin 		{
30697b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
30707b97099dSOleg Bulyzhin 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
30717b97099dSOleg Bulyzhin 		}
3072dab5cd05SOleg Bulyzhin 	}
307395d67482SBill Paul 
30748cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
3075b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
30768cb1383cSDoug Ambrisko 
3077dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
307895d67482SBill Paul }
307995d67482SBill Paul 
308095d67482SBill Paul static void
30813f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
30820434d1b8SBill Paul {
30833f74909aSGleb Smirnoff 	struct ifnet *ifp;
30840434d1b8SBill Paul 
3085fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
30860434d1b8SBill Paul 
30876b037352SJung-uk Kim 	ifp->if_collisions += CSR_READ_4(sc, BGE_MAC_STATS +
30887e6e2507SJung-uk Kim 	    offsetof(struct bge_mac_stats_regs, etherStatsCollisions));
30897e6e2507SJung-uk Kim 
30906b037352SJung-uk Kim 	ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
30910434d1b8SBill Paul }
30920434d1b8SBill Paul 
30930434d1b8SBill Paul static void
30943f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
309595d67482SBill Paul {
309695d67482SBill Paul 	struct ifnet *ifp;
3097e907febfSPyun YongHyeon 	bus_size_t stats;
30987e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
309995d67482SBill Paul 
3100fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
310195d67482SBill Paul 
3102e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
3103e907febfSPyun YongHyeon 
3104e907febfSPyun YongHyeon #define READ_STAT(sc, stats, stat) \
3105e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
310695d67482SBill Paul 
31078634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
31086b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
31096fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
31106fb34dd2SOleg Bulyzhin 
31116fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
31126b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
31136fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
31146fb34dd2SOleg Bulyzhin 
31156fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
31166b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
31176fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
311895d67482SBill Paul 
3119e907febfSPyun YongHyeon #undef READ_STAT
312095d67482SBill Paul }
312195d67482SBill Paul 
312295d67482SBill Paul /*
3123d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
3124d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
3125d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
3126d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
3127d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
3128d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
3129d375e524SGleb Smirnoff  */
3130d375e524SGleb Smirnoff static __inline int
3131d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
3132d375e524SGleb Smirnoff {
3133d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
3134d375e524SGleb Smirnoff 	struct mbuf *last;
3135d375e524SGleb Smirnoff 
3136d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
3137d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
3138d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
3139d375e524SGleb Smirnoff 		last = m;
3140d375e524SGleb Smirnoff 	} else {
3141d375e524SGleb Smirnoff 		/*
3142d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
3143d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
3144d375e524SGleb Smirnoff 		 */
3145d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
3146d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
3147d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
3148d375e524SGleb Smirnoff 			struct mbuf *n;
3149d375e524SGleb Smirnoff 
3150d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
3151d375e524SGleb Smirnoff 			if (n == NULL)
3152d375e524SGleb Smirnoff 				return (ENOBUFS);
3153d375e524SGleb Smirnoff 			n->m_len = 0;
3154d375e524SGleb Smirnoff 			last->m_next = n;
3155d375e524SGleb Smirnoff 			last = n;
3156d375e524SGleb Smirnoff 		}
3157d375e524SGleb Smirnoff 	}
3158d375e524SGleb Smirnoff 
3159d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
3160d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
3161d375e524SGleb Smirnoff 	last->m_len += padlen;
3162d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
3163d375e524SGleb Smirnoff 
3164d375e524SGleb Smirnoff 	return (0);
3165d375e524SGleb Smirnoff }
3166d375e524SGleb Smirnoff 
3167d375e524SGleb Smirnoff /*
316895d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
316995d67482SBill Paul  * pointers to descriptors.
317095d67482SBill Paul  */
317195d67482SBill Paul static int
3172676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
317395d67482SBill Paul {
31747e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
3175f41ac2beSBill Paul 	bus_dmamap_t		map;
3176676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
3177676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
31787e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
3179676ad2c9SGleb Smirnoff 	uint16_t		csum_flags;
31807e27542aSGleb Smirnoff 	int			nsegs, i, error;
318195d67482SBill Paul 
31826909dc43SGleb Smirnoff 	csum_flags = 0;
31836909dc43SGleb Smirnoff 	if (m->m_pkthdr.csum_flags) {
31846909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
31856909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
31866909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
31876909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
31886909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
31896909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
31906909dc43SGleb Smirnoff 				m_freem(m);
31916909dc43SGleb Smirnoff 				*m_head = NULL;
31926909dc43SGleb Smirnoff 				return (error);
31936909dc43SGleb Smirnoff 			}
31946909dc43SGleb Smirnoff 		}
31956909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
31966909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
31976909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
31986909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
31996909dc43SGleb Smirnoff 	}
32006909dc43SGleb Smirnoff 
32017e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
3202676ad2c9SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m, segs,
3203676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
32047e27542aSGleb Smirnoff 	if (error == EFBIG) {
3205676ad2c9SGleb Smirnoff 		m = m_defrag(m, M_DONTWAIT);
3206676ad2c9SGleb Smirnoff 		if (m == NULL) {
3207676ad2c9SGleb Smirnoff 			m_freem(*m_head);
3208676ad2c9SGleb Smirnoff 			*m_head = NULL;
32097e27542aSGleb Smirnoff 			return (ENOBUFS);
32107e27542aSGleb Smirnoff 		}
3211676ad2c9SGleb Smirnoff 		*m_head = m;
3212676ad2c9SGleb Smirnoff 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m,
3213676ad2c9SGleb Smirnoff 		    segs, &nsegs, BUS_DMA_NOWAIT);
3214676ad2c9SGleb Smirnoff 		if (error) {
3215676ad2c9SGleb Smirnoff 			m_freem(m);
3216676ad2c9SGleb Smirnoff 			*m_head = NULL;
32177e27542aSGleb Smirnoff 			return (error);
32187e27542aSGleb Smirnoff 		}
3219676ad2c9SGleb Smirnoff 	} else if (error != 0)
3220676ad2c9SGleb Smirnoff 		return (error);
32217e27542aSGleb Smirnoff 
322295d67482SBill Paul 	/*
322395d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
322495d67482SBill Paul 	 * of the end of the ring.
322595d67482SBill Paul 	 */
32267e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
32277e27542aSGleb Smirnoff 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
322895d67482SBill Paul 		return (ENOBUFS);
32297e27542aSGleb Smirnoff 	}
32307e27542aSGleb Smirnoff 
3231e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
3232e65bed95SPyun YongHyeon 
32337e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
32347e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
32357e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
32367e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
32377e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
32387e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
32397e27542aSGleb Smirnoff 		if (i == nsegs - 1)
32407e27542aSGleb Smirnoff 			break;
32417e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
32427e27542aSGleb Smirnoff 	}
32437e27542aSGleb Smirnoff 
32447e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
32457e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
3246676ad2c9SGleb Smirnoff 
32477e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
32487e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
324978ba57b9SAndre Oppermann 	if (m->m_flags & M_VLANTAG) {
32507e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
325178ba57b9SAndre Oppermann 		d->bge_vlan_tag = m->m_pkthdr.ether_vtag;
32527e27542aSGleb Smirnoff 	} else
32537e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
3254f41ac2beSBill Paul 
3255f41ac2beSBill Paul 	/*
3256f41ac2beSBill Paul 	 * Insure that the map for this transmission
3257f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
3258f41ac2beSBill Paul 	 * in this chain.
3259f41ac2beSBill Paul 	 */
32607e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
32617e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
3262676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
32637e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
326495d67482SBill Paul 
32657e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
32667e27542aSGleb Smirnoff 	*txidx = idx;
326795d67482SBill Paul 
326895d67482SBill Paul 	return (0);
326995d67482SBill Paul }
327095d67482SBill Paul 
327195d67482SBill Paul /*
327295d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
327395d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
327495d67482SBill Paul  */
327595d67482SBill Paul static void
32763f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
327795d67482SBill Paul {
327895d67482SBill Paul 	struct bge_softc *sc;
327995d67482SBill Paul 	struct mbuf *m_head = NULL;
328014bbd30fSGleb Smirnoff 	uint32_t prodidx;
3281303a718cSDag-Erling Smørgrav 	int count = 0;
328295d67482SBill Paul 
328395d67482SBill Paul 	sc = ifp->if_softc;
328495d67482SBill Paul 
3285dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
328695d67482SBill Paul 		return;
328795d67482SBill Paul 
328814bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
328995d67482SBill Paul 
329095d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
32914d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
329295d67482SBill Paul 		if (m_head == NULL)
329395d67482SBill Paul 			break;
329495d67482SBill Paul 
329595d67482SBill Paul 		/*
329695d67482SBill Paul 		 * XXX
3297b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
3298b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3299b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
3300b874fdd4SYaroslav Tykhiy 		 *
3301b874fdd4SYaroslav Tykhiy 		 * XXX
330295d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
330395d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
330495d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
330595d67482SBill Paul 		 * chain at once.
330695d67482SBill Paul 		 * (paranoia -- may not actually be needed)
330795d67482SBill Paul 		 */
330895d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
330995d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
331095d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
331195d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
33124d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
331313f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
331495d67482SBill Paul 				break;
331595d67482SBill Paul 			}
331695d67482SBill Paul 		}
331795d67482SBill Paul 
331895d67482SBill Paul 		/*
331995d67482SBill Paul 		 * Pack the data into the transmit ring. If we
332095d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
332195d67482SBill Paul 		 * for the NIC to drain the ring.
332295d67482SBill Paul 		 */
3323676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
3324676ad2c9SGleb Smirnoff 			if (m_head == NULL)
3325676ad2c9SGleb Smirnoff 				break;
33264d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
332713f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
332895d67482SBill Paul 			break;
332995d67482SBill Paul 		}
3330303a718cSDag-Erling Smørgrav 		++count;
333195d67482SBill Paul 
333295d67482SBill Paul 		/*
333395d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
333495d67482SBill Paul 		 * to him.
333595d67482SBill Paul 		 */
3336673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
333795d67482SBill Paul 	}
333895d67482SBill Paul 
33393f74909aSGleb Smirnoff 	if (count == 0)
33403f74909aSGleb Smirnoff 		/* No packets were dequeued. */
3341303a718cSDag-Erling Smørgrav 		return;
3342303a718cSDag-Erling Smørgrav 
33433f74909aSGleb Smirnoff 	/* Transmit. */
334495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
33453927098fSPaul Saab 	/* 5700 b2 errata */
3346e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
33473927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
334895d67482SBill Paul 
334914bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
335014bbd30fSGleb Smirnoff 
335195d67482SBill Paul 	/*
335295d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
335395d67482SBill Paul 	 */
3354b74e67fbSGleb Smirnoff 	sc->bge_timer = 5;
335595d67482SBill Paul }
335695d67482SBill Paul 
33570f9bd73bSSam Leffler /*
33580f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
33590f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
33600f9bd73bSSam Leffler  */
336195d67482SBill Paul static void
33623f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
336395d67482SBill Paul {
33640f9bd73bSSam Leffler 	struct bge_softc *sc;
33650f9bd73bSSam Leffler 
33660f9bd73bSSam Leffler 	sc = ifp->if_softc;
33670f9bd73bSSam Leffler 	BGE_LOCK(sc);
33680f9bd73bSSam Leffler 	bge_start_locked(ifp);
33690f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
33700f9bd73bSSam Leffler }
33710f9bd73bSSam Leffler 
33720f9bd73bSSam Leffler static void
33733f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
33740f9bd73bSSam Leffler {
337595d67482SBill Paul 	struct ifnet *ifp;
33763f74909aSGleb Smirnoff 	uint16_t *m;
337795d67482SBill Paul 
33780f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
337995d67482SBill Paul 
3380fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
338195d67482SBill Paul 
338213f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
338395d67482SBill Paul 		return;
338495d67482SBill Paul 
338595d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
338695d67482SBill Paul 	bge_stop(sc);
33878cb1383cSDoug Ambrisko 
33888cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
33898cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
339095d67482SBill Paul 	bge_reset(sc);
33918cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
33928cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
33938cb1383cSDoug Ambrisko 
339495d67482SBill Paul 	bge_chipinit(sc);
339595d67482SBill Paul 
339695d67482SBill Paul 	/*
339795d67482SBill Paul 	 * Init the various state machines, ring
339895d67482SBill Paul 	 * control blocks and firmware.
339995d67482SBill Paul 	 */
340095d67482SBill Paul 	if (bge_blockinit(sc)) {
3401fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
340295d67482SBill Paul 		return;
340395d67482SBill Paul 	}
340495d67482SBill Paul 
3405fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
340695d67482SBill Paul 
340795d67482SBill Paul 	/* Specify MTU. */
340895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3409859c6c7dSBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
341095d67482SBill Paul 
341195d67482SBill Paul 	/* Load our MAC address. */
34123f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
341395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
341495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
341595d67482SBill Paul 
34163e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
34173e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
341895d67482SBill Paul 
341995d67482SBill Paul 	/* Program multicast filter. */
342095d67482SBill Paul 	bge_setmulti(sc);
342195d67482SBill Paul 
342295d67482SBill Paul 	/* Init RX ring. */
342395d67482SBill Paul 	bge_init_rx_ring_std(sc);
342495d67482SBill Paul 
34250434d1b8SBill Paul 	/*
34260434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
34270434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
34280434d1b8SBill Paul 	 * entry of the ring.
34290434d1b8SBill Paul 	 */
34300434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
34313f74909aSGleb Smirnoff 		uint32_t		v, i;
34320434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
34330434d1b8SBill Paul 			DELAY(20);
34340434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
34350434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
34360434d1b8SBill Paul 				break;
34370434d1b8SBill Paul 		}
34380434d1b8SBill Paul 		if (i == 10)
3439fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
3440fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
34410434d1b8SBill Paul 	}
34420434d1b8SBill Paul 
344395d67482SBill Paul 	/* Init jumbo RX ring. */
344495d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
344595d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
344695d67482SBill Paul 
34473f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
344895d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
344995d67482SBill Paul 
34507e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
34517e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
34527e6e2507SJung-uk Kim 
345395d67482SBill Paul 	/* Init TX ring. */
345495d67482SBill Paul 	bge_init_tx_ring(sc);
345595d67482SBill Paul 
34563f74909aSGleb Smirnoff 	/* Turn on transmitter. */
345795d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
345895d67482SBill Paul 
34593f74909aSGleb Smirnoff 	/* Turn on receiver. */
346095d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
346195d67482SBill Paul 
346295d67482SBill Paul 	/* Tell firmware we're alive. */
346395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
346495d67482SBill Paul 
346575719184SGleb Smirnoff #ifdef DEVICE_POLLING
346675719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
346775719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
346875719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
346975719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
347075719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
347175719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
347275719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
347375719184SGleb Smirnoff 	} else
347475719184SGleb Smirnoff #endif
347575719184SGleb Smirnoff 
347695d67482SBill Paul 	/* Enable host interrupts. */
347775719184SGleb Smirnoff 	{
347895d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
347995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
348095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
348175719184SGleb Smirnoff 	}
348295d67482SBill Paul 
348367d5e043SOleg Bulyzhin 	bge_ifmedia_upd_locked(ifp);
348495d67482SBill Paul 
348513f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
348613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
348795d67482SBill Paul 
34880f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
34890f9bd73bSSam Leffler }
34900f9bd73bSSam Leffler 
34910f9bd73bSSam Leffler static void
34923f74909aSGleb Smirnoff bge_init(void *xsc)
34930f9bd73bSSam Leffler {
34940f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
34950f9bd73bSSam Leffler 
34960f9bd73bSSam Leffler 	BGE_LOCK(sc);
34970f9bd73bSSam Leffler 	bge_init_locked(sc);
34980f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
349995d67482SBill Paul }
350095d67482SBill Paul 
350195d67482SBill Paul /*
350295d67482SBill Paul  * Set media options.
350395d67482SBill Paul  */
350495d67482SBill Paul static int
35053f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
350695d67482SBill Paul {
350767d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
350867d5e043SOleg Bulyzhin 	int res;
350967d5e043SOleg Bulyzhin 
351067d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
351167d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
351267d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
351367d5e043SOleg Bulyzhin 
351467d5e043SOleg Bulyzhin 	return (res);
351567d5e043SOleg Bulyzhin }
351667d5e043SOleg Bulyzhin 
351767d5e043SOleg Bulyzhin static int
351867d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
351967d5e043SOleg Bulyzhin {
352067d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
352195d67482SBill Paul 	struct mii_data *mii;
352295d67482SBill Paul 	struct ifmedia *ifm;
352395d67482SBill Paul 
352467d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
352567d5e043SOleg Bulyzhin 
352695d67482SBill Paul 	ifm = &sc->bge_ifmedia;
352795d67482SBill Paul 
352895d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
3529652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
353095d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
353195d67482SBill Paul 			return (EINVAL);
353295d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
353395d67482SBill Paul 		case IFM_AUTO:
3534ff50922bSDoug White 			/*
3535ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
3536ff50922bSDoug White 			 * mechanism for programming the autoneg
3537ff50922bSDoug White 			 * advertisement registers in TBI mode.
3538ff50922bSDoug White 			 */
3539c4529f41SMichael Reifenberger 			if (bge_fake_autoneg == 0 &&
3540c4529f41SMichael Reifenberger 			    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3541ff50922bSDoug White 				uint32_t sgdig;
3542ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3543ff50922bSDoug White 				sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3544ff50922bSDoug White 				sgdig |= BGE_SGDIGCFG_AUTO|
3545ff50922bSDoug White 				    BGE_SGDIGCFG_PAUSE_CAP|
3546ff50922bSDoug White 				    BGE_SGDIGCFG_ASYM_PAUSE;
3547ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3548ff50922bSDoug White 				    sgdig|BGE_SGDIGCFG_SEND);
3549ff50922bSDoug White 				DELAY(5);
3550ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3551ff50922bSDoug White 			}
355295d67482SBill Paul 			break;
355395d67482SBill Paul 		case IFM_1000_SX:
355495d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
355595d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
355695d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
355795d67482SBill Paul 			} else {
355895d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
355995d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
356095d67482SBill Paul 			}
356195d67482SBill Paul 			break;
356295d67482SBill Paul 		default:
356395d67482SBill Paul 			return (EINVAL);
356495d67482SBill Paul 		}
356595d67482SBill Paul 		return (0);
356695d67482SBill Paul 	}
356795d67482SBill Paul 
35681493e883SOleg Bulyzhin 	sc->bge_link_evt++;
356995d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
357095d67482SBill Paul 	if (mii->mii_instance) {
357195d67482SBill Paul 		struct mii_softc *miisc;
357295d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
357395d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
357495d67482SBill Paul 			mii_phy_reset(miisc);
357595d67482SBill Paul 	}
357695d67482SBill Paul 	mii_mediachg(mii);
357795d67482SBill Paul 
357895d67482SBill Paul 	return (0);
357995d67482SBill Paul }
358095d67482SBill Paul 
358195d67482SBill Paul /*
358295d67482SBill Paul  * Report current media status.
358395d67482SBill Paul  */
358495d67482SBill Paul static void
35853f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
358695d67482SBill Paul {
358767d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
358895d67482SBill Paul 	struct mii_data *mii;
358995d67482SBill Paul 
359067d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
359195d67482SBill Paul 
3592652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
359395d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
359495d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
359595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
359695d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
359795d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
35984c0da0ffSGleb Smirnoff 		else {
35994c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
360067d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
36014c0da0ffSGleb Smirnoff 			return;
36024c0da0ffSGleb Smirnoff 		}
360395d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
360495d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
360595d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
360695d67482SBill Paul 		else
360795d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
360867d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
360995d67482SBill Paul 		return;
361095d67482SBill Paul 	}
361195d67482SBill Paul 
361295d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
361395d67482SBill Paul 	mii_pollstat(mii);
361495d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
361595d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
361667d5e043SOleg Bulyzhin 
361767d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
361895d67482SBill Paul }
361995d67482SBill Paul 
362095d67482SBill Paul static int
36213f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
362295d67482SBill Paul {
362395d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
362495d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
362595d67482SBill Paul 	struct mii_data *mii;
3626f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
362795d67482SBill Paul 
362895d67482SBill Paul 	switch (command) {
362995d67482SBill Paul 	case SIOCSIFMTU:
36304c0da0ffSGleb Smirnoff 		if (ifr->ifr_mtu < ETHERMIN ||
36314c0da0ffSGleb Smirnoff 		    ((BGE_IS_JUMBO_CAPABLE(sc)) &&
36324c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > BGE_JUMBO_MTU) ||
36334c0da0ffSGleb Smirnoff 		    ((!BGE_IS_JUMBO_CAPABLE(sc)) &&
36344c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > ETHERMTU))
363595d67482SBill Paul 			error = EINVAL;
36364c0da0ffSGleb Smirnoff 		else if (ifp->if_mtu != ifr->ifr_mtu) {
363795d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
363813f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
363995d67482SBill Paul 			bge_init(sc);
364095d67482SBill Paul 		}
364195d67482SBill Paul 		break;
364295d67482SBill Paul 	case SIOCSIFFLAGS:
36430f9bd73bSSam Leffler 		BGE_LOCK(sc);
364495d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
364595d67482SBill Paul 			/*
364695d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
364795d67482SBill Paul 			 * then just use the 'set promisc mode' command
364895d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
364995d67482SBill Paul 			 * a full re-init means reloading the firmware and
365095d67482SBill Paul 			 * waiting for it to start up, which may take a
3651d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
365295d67482SBill Paul 			 */
3653f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3654f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
36553e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
36563e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
3657f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
3658d183af7fSRuslan Ermilov 					bge_setmulti(sc);
365995d67482SBill Paul 			} else
36600f9bd73bSSam Leffler 				bge_init_locked(sc);
366195d67482SBill Paul 		} else {
366213f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
366395d67482SBill Paul 				bge_stop(sc);
366495d67482SBill Paul 			}
366595d67482SBill Paul 		}
366695d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
36670f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
366895d67482SBill Paul 		error = 0;
366995d67482SBill Paul 		break;
367095d67482SBill Paul 	case SIOCADDMULTI:
367195d67482SBill Paul 	case SIOCDELMULTI:
367213f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
36730f9bd73bSSam Leffler 			BGE_LOCK(sc);
367495d67482SBill Paul 			bge_setmulti(sc);
36750f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
367695d67482SBill Paul 			error = 0;
367795d67482SBill Paul 		}
367895d67482SBill Paul 		break;
367995d67482SBill Paul 	case SIOCSIFMEDIA:
368095d67482SBill Paul 	case SIOCGIFMEDIA:
3681652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
368295d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
368395d67482SBill Paul 			    &sc->bge_ifmedia, command);
368495d67482SBill Paul 		} else {
368595d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
368695d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
368795d67482SBill Paul 			    &mii->mii_media, command);
368895d67482SBill Paul 		}
368995d67482SBill Paul 		break;
369095d67482SBill Paul 	case SIOCSIFCAP:
369195d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
369275719184SGleb Smirnoff #ifdef DEVICE_POLLING
369375719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
369475719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
369575719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
369675719184SGleb Smirnoff 				if (error)
369775719184SGleb Smirnoff 					return (error);
369875719184SGleb Smirnoff 				BGE_LOCK(sc);
369975719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
370075719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
370175719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
370275719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
370375719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
370475719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
370575719184SGleb Smirnoff 				BGE_UNLOCK(sc);
370675719184SGleb Smirnoff 			} else {
370775719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
370875719184SGleb Smirnoff 				/* Enable interrupt even in error case */
370975719184SGleb Smirnoff 				BGE_LOCK(sc);
371075719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
371175719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
371275719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
371375719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
371475719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
371575719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
371675719184SGleb Smirnoff 				BGE_UNLOCK(sc);
371775719184SGleb Smirnoff 			}
371875719184SGleb Smirnoff 		}
371975719184SGleb Smirnoff #endif
3720d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
3721d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
3722d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
3723d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
3724b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
372595d67482SBill Paul 			else
3726b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
3727479b23b7SGleb Smirnoff 			VLAN_CAPABILITIES(ifp);
372895d67482SBill Paul 		}
372995d67482SBill Paul 		break;
373095d67482SBill Paul 	default:
3731673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
373295d67482SBill Paul 		break;
373395d67482SBill Paul 	}
373495d67482SBill Paul 
373595d67482SBill Paul 	return (error);
373695d67482SBill Paul }
373795d67482SBill Paul 
373895d67482SBill Paul static void
3739b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
374095d67482SBill Paul {
3741b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
374295d67482SBill Paul 
3743b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
3744b74e67fbSGleb Smirnoff 
3745b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
3746b74e67fbSGleb Smirnoff 		return;
3747b74e67fbSGleb Smirnoff 
3748b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
374995d67482SBill Paul 
3750fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
375195d67482SBill Paul 
375213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3753426742bfSGleb Smirnoff 	bge_init_locked(sc);
375495d67482SBill Paul 
375595d67482SBill Paul 	ifp->if_oerrors++;
375695d67482SBill Paul }
375795d67482SBill Paul 
375895d67482SBill Paul /*
375995d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
376095d67482SBill Paul  * RX and TX lists.
376195d67482SBill Paul  */
376295d67482SBill Paul static void
37633f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
376495d67482SBill Paul {
376595d67482SBill Paul 	struct ifnet *ifp;
376695d67482SBill Paul 	struct ifmedia_entry *ifm;
376795d67482SBill Paul 	struct mii_data *mii = NULL;
376895d67482SBill Paul 	int mtmp, itmp;
376995d67482SBill Paul 
37700f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
37710f9bd73bSSam Leffler 
3772fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
377395d67482SBill Paul 
3774652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0)
377595d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
377695d67482SBill Paul 
37770f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
377895d67482SBill Paul 
377995d67482SBill Paul 	/*
37803f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
378195d67482SBill Paul 	 */
378295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
378395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
378495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
37857ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
378695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
378795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
378895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
378995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
379095d67482SBill Paul 
379195d67482SBill Paul 	/*
37923f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
379395d67482SBill Paul 	 */
379495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
379595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
379695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
379795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
379895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
37997ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
380095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
380195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
380295d67482SBill Paul 
380395d67482SBill Paul 	/*
380495d67482SBill Paul 	 * Shut down all of the memory managers and related
380595d67482SBill Paul 	 * state machines.
380695d67482SBill Paul 	 */
380795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
380895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
38097ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
381095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
381195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
381295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
38137ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
381495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
381595d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
38160434d1b8SBill Paul 	}
381795d67482SBill Paul 
381895d67482SBill Paul 	/* Disable host interrupts. */
381995d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
382095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
382195d67482SBill Paul 
382295d67482SBill Paul 	/*
382395d67482SBill Paul 	 * Tell firmware we're shutting down.
382495d67482SBill Paul 	 */
38258cb1383cSDoug Ambrisko 
38268cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
38278cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
38288cb1383cSDoug Ambrisko 	bge_reset(sc);
38298cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
38308cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
38318cb1383cSDoug Ambrisko 
38328cb1383cSDoug Ambrisko 	/*
38338cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
38348cb1383cSDoug Ambrisko 	 */
38358cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
38368cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
38378cb1383cSDoug Ambrisko 	else
383895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
383995d67482SBill Paul 
384095d67482SBill Paul 	/* Free the RX lists. */
384195d67482SBill Paul 	bge_free_rx_ring_std(sc);
384295d67482SBill Paul 
384395d67482SBill Paul 	/* Free jumbo RX list. */
38444c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
384595d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
384695d67482SBill Paul 
384795d67482SBill Paul 	/* Free TX buffers. */
384895d67482SBill Paul 	bge_free_tx_ring(sc);
384995d67482SBill Paul 
385095d67482SBill Paul 	/*
385195d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
385295d67482SBill Paul 	 * unchanged so that things will be put back to normal when
385395d67482SBill Paul 	 * we bring the interface back up.
385495d67482SBill Paul 	 */
3855652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
385695d67482SBill Paul 		itmp = ifp->if_flags;
385795d67482SBill Paul 		ifp->if_flags |= IFF_UP;
3858dcc34049SPawel Jakub Dawidek 		/*
3859dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
3860dcc34049SPawel Jakub Dawidek 		 */
3861dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
386295d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
386395d67482SBill Paul 			mtmp = ifm->ifm_media;
386495d67482SBill Paul 			ifm->ifm_media = IFM_ETHER|IFM_NONE;
386595d67482SBill Paul 			mii_mediachg(mii);
386695d67482SBill Paul 			ifm->ifm_media = mtmp;
3867dcc34049SPawel Jakub Dawidek 		}
386895d67482SBill Paul 		ifp->if_flags = itmp;
386995d67482SBill Paul 	}
387095d67482SBill Paul 
387195d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
387295d67482SBill Paul 
38731493e883SOleg Bulyzhin 	/*
38741493e883SOleg Bulyzhin 	 * We can't just call bge_link_upd() cause chip is almost stopped so
38751493e883SOleg Bulyzhin 	 * bge_link_upd -> bge_tick_locked -> bge_stats_update sequence may
38761493e883SOleg Bulyzhin 	 * lead to hardware deadlock. So we just clearing MAC's link state
38771493e883SOleg Bulyzhin 	 * (PHY may still have link UP).
38781493e883SOleg Bulyzhin 	 */
38791493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
38801493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
38811493e883SOleg Bulyzhin 	sc->bge_link = 0;
388295d67482SBill Paul 
38831493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
388495d67482SBill Paul }
388595d67482SBill Paul 
388695d67482SBill Paul /*
388795d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
388895d67482SBill Paul  * get confused by errant DMAs when rebooting.
388995d67482SBill Paul  */
389095d67482SBill Paul static void
38913f74909aSGleb Smirnoff bge_shutdown(device_t dev)
389295d67482SBill Paul {
389395d67482SBill Paul 	struct bge_softc *sc;
389495d67482SBill Paul 
389595d67482SBill Paul 	sc = device_get_softc(dev);
389695d67482SBill Paul 
38970f9bd73bSSam Leffler 	BGE_LOCK(sc);
389895d67482SBill Paul 	bge_stop(sc);
389995d67482SBill Paul 	bge_reset(sc);
39000f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
390195d67482SBill Paul }
390214afefa3SPawel Jakub Dawidek 
390314afefa3SPawel Jakub Dawidek static int
390414afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
390514afefa3SPawel Jakub Dawidek {
390614afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
390714afefa3SPawel Jakub Dawidek 
390814afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
390914afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
391014afefa3SPawel Jakub Dawidek 	bge_stop(sc);
391114afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
391214afefa3SPawel Jakub Dawidek 
391314afefa3SPawel Jakub Dawidek 	return (0);
391414afefa3SPawel Jakub Dawidek }
391514afefa3SPawel Jakub Dawidek 
391614afefa3SPawel Jakub Dawidek static int
391714afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
391814afefa3SPawel Jakub Dawidek {
391914afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
392014afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
392114afefa3SPawel Jakub Dawidek 
392214afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
392314afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
392414afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
392514afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
392614afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
392714afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
392814afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
392914afefa3SPawel Jakub Dawidek 	}
393014afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
393114afefa3SPawel Jakub Dawidek 
393214afefa3SPawel Jakub Dawidek 	return (0);
393314afefa3SPawel Jakub Dawidek }
3934dab5cd05SOleg Bulyzhin 
3935dab5cd05SOleg Bulyzhin static void
39363f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
3937dab5cd05SOleg Bulyzhin {
39381f313773SOleg Bulyzhin 	struct mii_data *mii;
39391f313773SOleg Bulyzhin 	uint32_t link, status;
3940dab5cd05SOleg Bulyzhin 
3941dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
39421f313773SOleg Bulyzhin 
39433f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
39447b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
39457b97099dSOleg Bulyzhin 
3946dab5cd05SOleg Bulyzhin 	/*
3947dab5cd05SOleg Bulyzhin 	 * Process link state changes.
3948dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
3949dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
3950dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
3951dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
3952dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
3953dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
3954dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
3955dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
39561f313773SOleg Bulyzhin 	 *
39571f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
39584c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
3959dab5cd05SOleg Bulyzhin 	 */
3960dab5cd05SOleg Bulyzhin 
39611f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
39624c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
3963dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
3964dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
3965dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
3966b74e67fbSGleb Smirnoff 			bge_tick(sc);
39671f313773SOleg Bulyzhin 
39681f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
39691f313773SOleg Bulyzhin 			if (!sc->bge_link &&
39701f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
39711f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
39721f313773SOleg Bulyzhin 				sc->bge_link++;
39731f313773SOleg Bulyzhin 				if (bootverbose)
39741f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
39751f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
39761f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
39771f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
39781f313773SOleg Bulyzhin 				sc->bge_link = 0;
39791f313773SOleg Bulyzhin 				if (bootverbose)
39801f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
39811f313773SOleg Bulyzhin 			}
39821f313773SOleg Bulyzhin 
39833f74909aSGleb Smirnoff 			/* Clear the interrupt. */
3984dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
3985dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
3986dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
3987dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
3988dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
3989dab5cd05SOleg Bulyzhin 		}
3990dab5cd05SOleg Bulyzhin 		return;
3991dab5cd05SOleg Bulyzhin 	}
3992dab5cd05SOleg Bulyzhin 
3993652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
39941f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
39957b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
39967b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
39971f313773SOleg Bulyzhin 				sc->bge_link++;
39981f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
39991f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
40001f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
40011f313773SOleg Bulyzhin 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
40021f313773SOleg Bulyzhin 				if (bootverbose)
40031f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
40043f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
40053f74909aSGleb Smirnoff 				    LINK_STATE_UP);
40067b97099dSOleg Bulyzhin 			}
40071f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
4008dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
40091f313773SOleg Bulyzhin 			if (bootverbose)
40101f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
40117b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
40121f313773SOleg Bulyzhin 		}
40131493e883SOleg Bulyzhin 	/* Discard link events for MII/GMII cards if MI auto-polling disabled */
40141493e883SOleg Bulyzhin 	} else if (CSR_READ_4(sc, BGE_MI_MODE) & BGE_MIMODE_AUTOPOLL) {
40151f313773SOleg Bulyzhin 		/*
40161f313773SOleg Bulyzhin 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
40171f313773SOleg Bulyzhin 		 * in status word always set. Workaround this bug by reading
40181f313773SOleg Bulyzhin 		 * PHY link status directly.
40191f313773SOleg Bulyzhin 		 */
40201f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
40211f313773SOleg Bulyzhin 
40221f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
40231f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
4024dab5cd05SOleg Bulyzhin 			callout_stop(&sc->bge_stat_ch);
4025b74e67fbSGleb Smirnoff 			bge_tick(sc);
40261f313773SOleg Bulyzhin 
40271f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
40281f313773SOleg Bulyzhin 			if (!sc->bge_link &&
40291f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
40301f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
40311f313773SOleg Bulyzhin 				sc->bge_link++;
40321f313773SOleg Bulyzhin 				if (bootverbose)
40331f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
40341f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
40351f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
40361f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
40371f313773SOleg Bulyzhin 				sc->bge_link = 0;
40381f313773SOleg Bulyzhin 				if (bootverbose)
40391f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
40401f313773SOleg Bulyzhin 			}
40411f313773SOleg Bulyzhin 		}
4042dab5cd05SOleg Bulyzhin 	}
4043dab5cd05SOleg Bulyzhin 
40443f74909aSGleb Smirnoff 	/* Clear the attention. */
4045dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
4046dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
4047dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
4048dab5cd05SOleg Bulyzhin }
40496f8718a3SScott Long 
40506f8718a3SScott Long static void
40516f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
40526f8718a3SScott Long {
40536f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
40546f8718a3SScott Long 	struct sysctl_oid_list *children;
40556f8718a3SScott Long 
40566f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
40576f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
40586f8718a3SScott Long 
40596f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
40606f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
40616f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
40626f8718a3SScott Long 	    "Debug Information");
40636f8718a3SScott Long 
40646f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
40656f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
40666f8718a3SScott Long 	    "Register Read");
40676f8718a3SScott Long 
40686f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
40696f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
40706f8718a3SScott Long 	    "Memory Read");
40716f8718a3SScott Long 
40726f8718a3SScott Long 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "stat_IfHcInOctets",
40736f8718a3SScott Long 	    CTLFLAG_RD,
40746f8718a3SScott Long 	    &sc->bge_ldata.bge_stats->rxstats.ifHCInOctets.bge_addr_lo,
40756f8718a3SScott Long 	    "Bytes received");
40766f8718a3SScott Long 
40776f8718a3SScott Long 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "stat_IfHcOutOctets",
40786f8718a3SScott Long 	    CTLFLAG_RD,
40796f8718a3SScott Long 	    &sc->bge_ldata.bge_stats->txstats.ifHCOutOctets.bge_addr_lo,
40806f8718a3SScott Long 	    "Bytes received");
40816f8718a3SScott Long #endif
40826f8718a3SScott Long }
40836f8718a3SScott Long 
40846f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
40856f8718a3SScott Long static int
40866f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
40876f8718a3SScott Long {
40886f8718a3SScott Long 	struct bge_softc *sc;
40896f8718a3SScott Long 	uint16_t *sbdata;
40906f8718a3SScott Long 	int error;
40916f8718a3SScott Long 	int result;
40926f8718a3SScott Long 	int i, j;
40936f8718a3SScott Long 
40946f8718a3SScott Long 	result = -1;
40956f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
40966f8718a3SScott Long 	if (error || (req->newptr == NULL))
40976f8718a3SScott Long 		return (error);
40986f8718a3SScott Long 
40996f8718a3SScott Long 	if (result == 1) {
41006f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
41016f8718a3SScott Long 
41026f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
41036f8718a3SScott Long 		printf("Status Block:\n");
41046f8718a3SScott Long 		for (i = 0x0; i < (BGE_STATUS_BLK_SZ / 4); ) {
41056f8718a3SScott Long 			printf("%06x:", i);
41066f8718a3SScott Long 			for (j = 0; j < 8; j++) {
41076f8718a3SScott Long 				printf(" %04x", sbdata[i]);
41086f8718a3SScott Long 				i += 4;
41096f8718a3SScott Long 			}
41106f8718a3SScott Long 			printf("\n");
41116f8718a3SScott Long 		}
41126f8718a3SScott Long 
41136f8718a3SScott Long 		printf("Registers:\n");
41146f8718a3SScott Long 		for (i = 0x800; i < 0xa00; ) {
41156f8718a3SScott Long 			printf("%06x:", i);
41166f8718a3SScott Long 			for (j = 0; j < 8; j++) {
41176f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
41186f8718a3SScott Long 				i += 4;
41196f8718a3SScott Long 			}
41206f8718a3SScott Long 			printf("\n");
41216f8718a3SScott Long 		}
41226f8718a3SScott Long 
41236f8718a3SScott Long 		printf("Hardware Flags:\n");
41245345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
41256f8718a3SScott Long 			printf(" - 575X Plus\n");
41265345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
41276f8718a3SScott Long 			printf(" - 5705 Plus\n");
41285345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
41295345bad0SScott Long 			printf(" - 5714 Family\n");
41305345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
41315345bad0SScott Long 			printf(" - 5700 Family\n");
41326f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
41336f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
41346f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
41356f8718a3SScott Long 			printf(" - PCI-X Bus\n");
41366f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
41376f8718a3SScott Long 			printf(" - PCI Express Bus\n");
41386f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_NO3LED)
41396f8718a3SScott Long 			printf(" - No 3 LEDs\n");
41406f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
41416f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
41426f8718a3SScott Long 	}
41436f8718a3SScott Long 
41446f8718a3SScott Long 	return (error);
41456f8718a3SScott Long }
41466f8718a3SScott Long 
41476f8718a3SScott Long static int
41486f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
41496f8718a3SScott Long {
41506f8718a3SScott Long 	struct bge_softc *sc;
41516f8718a3SScott Long 	int error;
41526f8718a3SScott Long 	uint16_t result;
41536f8718a3SScott Long 	uint32_t val;
41546f8718a3SScott Long 
41556f8718a3SScott Long 	result = -1;
41566f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
41576f8718a3SScott Long 	if (error || (req->newptr == NULL))
41586f8718a3SScott Long 		return (error);
41596f8718a3SScott Long 
41606f8718a3SScott Long 	if (result < 0x8000) {
41616f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
41626f8718a3SScott Long 		val = CSR_READ_4(sc, result);
41636f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
41646f8718a3SScott Long 	}
41656f8718a3SScott Long 
41666f8718a3SScott Long 	return (error);
41676f8718a3SScott Long }
41686f8718a3SScott Long 
41696f8718a3SScott Long static int
41706f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
41716f8718a3SScott Long {
41726f8718a3SScott Long 	struct bge_softc *sc;
41736f8718a3SScott Long 	int error;
41746f8718a3SScott Long 	uint16_t result;
41756f8718a3SScott Long 	uint32_t val;
41766f8718a3SScott Long 
41776f8718a3SScott Long 	result = -1;
41786f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
41796f8718a3SScott Long 	if (error || (req->newptr == NULL))
41806f8718a3SScott Long 		return (error);
41816f8718a3SScott Long 
41826f8718a3SScott Long 	if (result < 0x8000) {
41836f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
41846f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
41856f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
41866f8718a3SScott Long 	}
41876f8718a3SScott Long 
41886f8718a3SScott Long 	return (error);
41896f8718a3SScott Long }
41906f8718a3SScott Long #endif
4191