xref: /freebsd/sys/dev/bge/if_bge.c (revision b64728e55e4e672b4623e78b2abcc61e6820404c)
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 
117f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
118f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
11995d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12095d67482SBill Paul 
1217b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
12295d67482SBill Paul #include "miibus_if.h"
12395d67482SBill Paul 
12495d67482SBill Paul /*
12595d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
12695d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
12795d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
12895d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
12995d67482SBill Paul  */
1304c0da0ffSGleb Smirnoff static struct bge_type {
1314c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1324c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
1334c0da0ffSGleb Smirnoff } bge_devs[] = {
1344c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5700 },
1354c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5701 },
13695d67482SBill Paul 
1374c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1000 },
1384c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1002 },
1394c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC9100 },
1404c0da0ffSGleb Smirnoff 
1414c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	APPLE_DEVICE_BCM5701 },
1424c0da0ffSGleb Smirnoff 
1434c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5700 },
1444c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5701 },
1454c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702 },
1464c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702_ALT },
1474c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702X },
1484c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703 },
1494c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703_ALT },
1504c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703X },
1514c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704C },
1524c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S },
1534c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S_ALT },
1544c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705 },
1554c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705F },
1564c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705K },
1574c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M },
1584c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M_ALT },
1594c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714C },
1604c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714S },
1614c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715 },
1624c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715S },
1634c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1644c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
1654c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1664c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1674c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1684c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1694c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1704c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1714c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1724c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1734c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1744c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1759e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1769e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1779e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1789e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
1794c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
1804c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
1814c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
1824c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
1839e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
1849e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
1859e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
1864c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
1874c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
1884c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
1894c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
1904c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
1914c0da0ffSGleb Smirnoff 
1924c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
1934c0da0ffSGleb Smirnoff 
1944c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
1954c0da0ffSGleb Smirnoff 
1964c0da0ffSGleb Smirnoff 	{ 0, 0 }
19795d67482SBill Paul };
19895d67482SBill Paul 
1994c0da0ffSGleb Smirnoff static const struct bge_vendor {
2004c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2014c0da0ffSGleb Smirnoff 	const char	*v_name;
2024c0da0ffSGleb Smirnoff } bge_vendors[] = {
2034c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2044c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2054c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2064c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2074c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2084c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
2094c0da0ffSGleb Smirnoff 
2104c0da0ffSGleb Smirnoff 	{ 0, NULL }
2114c0da0ffSGleb Smirnoff };
2124c0da0ffSGleb Smirnoff 
2134c0da0ffSGleb Smirnoff static const struct bge_revision {
2144c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2154c0da0ffSGleb Smirnoff 	const char	*br_name;
2164c0da0ffSGleb Smirnoff } bge_revisions[] = {
2174c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2184c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2194c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2204c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2214c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2224c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2234c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2244c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2254c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2264c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2274c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2284c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2294c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2304c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2314c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2324c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2339e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2344c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2354c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2364c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2374c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2384c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2394c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2404c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2414c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2424c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2434c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2444c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2454c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2464c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2474c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2484c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2494c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
25042787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2514c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2524c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2534c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
2544c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
2554c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
2564c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
2574c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
2584c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
2596f8718a3SScott Long 	/* 5784 and 5787 share the same ASIC ID */
2606f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
2616f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
2626f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
2634c0da0ffSGleb Smirnoff 
2644c0da0ffSGleb Smirnoff 	{ 0, NULL }
2654c0da0ffSGleb Smirnoff };
2664c0da0ffSGleb Smirnoff 
2674c0da0ffSGleb Smirnoff /*
2684c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
2694c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
2704c0da0ffSGleb Smirnoff  */
2714c0da0ffSGleb Smirnoff static const struct bge_revision bge_majorrevs[] = {
2729e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
2739e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
2749e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
2759e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
2769e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
2779e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
2789e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
2799e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
2809e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
2819e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
2829e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
2836f8718a3SScott Long 	/* 5784 and 5787 share the same ASIC ID */
2846f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
2854c0da0ffSGleb Smirnoff 
2864c0da0ffSGleb Smirnoff 	{ 0, NULL }
2874c0da0ffSGleb Smirnoff };
2884c0da0ffSGleb Smirnoff 
2897ee00338SJung-uk Kim #define BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
2907ee00338SJung-uk Kim #define BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
2917ee00338SJung-uk Kim #define BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
2920dae9719SJung-uk Kim #define BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
2930dae9719SJung-uk Kim #define BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
2944c0da0ffSGleb Smirnoff 
2954c0da0ffSGleb Smirnoff const struct bge_revision * bge_lookup_rev(uint32_t);
2964c0da0ffSGleb Smirnoff const struct bge_vendor * bge_lookup_vendor(uint16_t);
297e51a25f8SAlfred Perlstein static int bge_probe(device_t);
298e51a25f8SAlfred Perlstein static int bge_attach(device_t);
299e51a25f8SAlfred Perlstein static int bge_detach(device_t);
30014afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
30114afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3023f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
303f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
304f41ac2beSBill Paul static int bge_dma_alloc(device_t);
305f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
306f41ac2beSBill Paul 
307e51a25f8SAlfred Perlstein static void bge_txeof(struct bge_softc *);
308e51a25f8SAlfred Perlstein static void bge_rxeof(struct bge_softc *);
30995d67482SBill Paul 
3108cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
311e51a25f8SAlfred Perlstein static void bge_tick(void *);
312e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
3133f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
314676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
31595d67482SBill Paul 
316e51a25f8SAlfred Perlstein static void bge_intr(void *);
3170f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
318e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
319e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
3200f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
321e51a25f8SAlfred Perlstein static void bge_init(void *);
322e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
323b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
324e51a25f8SAlfred Perlstein static void bge_shutdown(device_t);
32567d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
326e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
327e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
32895d67482SBill Paul 
3293f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
330e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
33195d67482SBill Paul 
3323e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
333e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
33495d67482SBill Paul 
335e51a25f8SAlfred Perlstein static int bge_newbuf_std(struct bge_softc *, int, struct mbuf *);
336e51a25f8SAlfred Perlstein static int bge_newbuf_jumbo(struct bge_softc *, int, struct mbuf *);
337e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
338e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
339e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
340e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
341e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
342e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
34395d67482SBill Paul 
344e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
345e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
34695d67482SBill Paul 
3473f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
348e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
34995d67482SBill Paul #ifdef notdef
3503f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
35195d67482SBill Paul #endif
3529ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
353e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
35495d67482SBill Paul 
355e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
356e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
357e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
35875719184SGleb Smirnoff #ifdef DEVICE_POLLING
3593f74909aSGleb Smirnoff static void bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
36075719184SGleb Smirnoff #endif
36195d67482SBill Paul 
3628cb1383cSDoug Ambrisko #define BGE_RESET_START 1
3638cb1383cSDoug Ambrisko #define BGE_RESET_STOP  2
3648cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
3658cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
3668cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
3678cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
368dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
36995d67482SBill Paul 
3706f8718a3SScott Long /*
3716f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
3726f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
3736f8718a3SScott Long  * traps on certain architectures.
3746f8718a3SScott Long  */
3756f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
3766f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
3776f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
3786f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
3796f8718a3SScott Long #endif
3806f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
3816f8718a3SScott Long 
38295d67482SBill Paul static device_method_t bge_methods[] = {
38395d67482SBill Paul 	/* Device interface */
38495d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
38595d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
38695d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
38795d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
38814afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
38914afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
39095d67482SBill Paul 
39195d67482SBill Paul 	/* bus interface */
39295d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
39395d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
39495d67482SBill Paul 
39595d67482SBill Paul 	/* MII interface */
39695d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
39795d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
39895d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
39995d67482SBill Paul 
40095d67482SBill Paul 	{ 0, 0 }
40195d67482SBill Paul };
40295d67482SBill Paul 
40395d67482SBill Paul static driver_t bge_driver = {
40495d67482SBill Paul 	"bge",
40595d67482SBill Paul 	bge_methods,
40695d67482SBill Paul 	sizeof(struct bge_softc)
40795d67482SBill Paul };
40895d67482SBill Paul 
40995d67482SBill Paul static devclass_t bge_devclass;
41095d67482SBill Paul 
411f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
41295d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
41395d67482SBill Paul 
414c4529f41SMichael Reifenberger static int bge_fake_autoneg = 0;
415f1a7e6d5SScott Long static int bge_allow_asf = 1;
416f1a7e6d5SScott Long 
417c4529f41SMichael Reifenberger TUNABLE_INT("hw.bge.fake_autoneg", &bge_fake_autoneg);
418f1a7e6d5SScott Long TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
419f1a7e6d5SScott Long 
420f1a7e6d5SScott Long SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
421f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, fake_autoneg, CTLFLAG_RD, &bge_fake_autoneg, 0,
422f1a7e6d5SScott Long 	"Enable fake autonegotiation for certain blade systems");
423f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
424f1a7e6d5SScott Long 	"Allow ASF mode if available");
425c4529f41SMichael Reifenberger 
4263f74909aSGleb Smirnoff static uint32_t
4273f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
42895d67482SBill Paul {
42995d67482SBill Paul 	device_t dev;
4306f8718a3SScott Long 	uint32_t val;
43195d67482SBill Paul 
43295d67482SBill Paul 	dev = sc->bge_dev;
43395d67482SBill Paul 
43495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
4356f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
4366f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
4376f8718a3SScott Long 	return (val);
43895d67482SBill Paul }
43995d67482SBill Paul 
44095d67482SBill Paul static void
4413f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
44295d67482SBill Paul {
44395d67482SBill Paul 	device_t dev;
44495d67482SBill Paul 
44595d67482SBill Paul 	dev = sc->bge_dev;
44695d67482SBill Paul 
44795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
44895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
4496f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
45095d67482SBill Paul }
45195d67482SBill Paul 
45295d67482SBill Paul #ifdef notdef
4533f74909aSGleb Smirnoff static uint32_t
4543f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
45595d67482SBill Paul {
45695d67482SBill Paul 	device_t dev;
45795d67482SBill Paul 
45895d67482SBill Paul 	dev = sc->bge_dev;
45995d67482SBill Paul 
46095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
46195d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
46295d67482SBill Paul }
46395d67482SBill Paul #endif
46495d67482SBill Paul 
46595d67482SBill Paul static void
4663f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
46795d67482SBill Paul {
46895d67482SBill Paul 	device_t dev;
46995d67482SBill Paul 
47095d67482SBill Paul 	dev = sc->bge_dev;
47195d67482SBill Paul 
47295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
47395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
47495d67482SBill Paul }
47595d67482SBill Paul 
4766f8718a3SScott Long static void
4776f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
4786f8718a3SScott Long {
4796f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
4806f8718a3SScott Long }
4816f8718a3SScott Long 
482f41ac2beSBill Paul /*
483f41ac2beSBill Paul  * Map a single buffer address.
484f41ac2beSBill Paul  */
485f41ac2beSBill Paul 
486f41ac2beSBill Paul static void
4873f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
488f41ac2beSBill Paul {
489f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
490f41ac2beSBill Paul 
491f41ac2beSBill Paul 	if (error)
492f41ac2beSBill Paul 		return;
493f41ac2beSBill Paul 
494f41ac2beSBill Paul 	ctx = arg;
495f41ac2beSBill Paul 
496f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
497f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
498f41ac2beSBill Paul 		return;
499f41ac2beSBill Paul 	}
500f41ac2beSBill Paul 
501f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
502f41ac2beSBill Paul }
503f41ac2beSBill Paul 
50495d67482SBill Paul /*
50595d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
50695d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
50795d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
50895d67482SBill Paul  * access method.
50995d67482SBill Paul  */
5103f74909aSGleb Smirnoff static uint8_t
5113f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
51295d67482SBill Paul {
51395d67482SBill Paul 	int i;
5143f74909aSGleb Smirnoff 	uint32_t byte = 0;
51595d67482SBill Paul 
51695d67482SBill Paul 	/*
51795d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
51895d67482SBill Paul 	 * having to use the bitbang method.
51995d67482SBill Paul 	 */
52095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
52195d67482SBill Paul 
52295d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
52395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
52495d67482SBill Paul 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
52595d67482SBill Paul 	DELAY(20);
52695d67482SBill Paul 
52795d67482SBill Paul 	/* Issue the read EEPROM command. */
52895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
52995d67482SBill Paul 
53095d67482SBill Paul 	/* Wait for completion */
53195d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
53295d67482SBill Paul 		DELAY(10);
53395d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
53495d67482SBill Paul 			break;
53595d67482SBill Paul 	}
53695d67482SBill Paul 
53795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
538fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
539f6789fbaSPyun YongHyeon 		return (1);
54095d67482SBill Paul 	}
54195d67482SBill Paul 
54295d67482SBill Paul 	/* Get result. */
54395d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
54495d67482SBill Paul 
54595d67482SBill Paul 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
54695d67482SBill Paul 
54795d67482SBill Paul 	return (0);
54895d67482SBill Paul }
54995d67482SBill Paul 
55095d67482SBill Paul /*
55195d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
55295d67482SBill Paul  */
55395d67482SBill Paul static int
5543f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
55595d67482SBill Paul {
5563f74909aSGleb Smirnoff 	int i, error = 0;
5573f74909aSGleb Smirnoff 	uint8_t byte = 0;
55895d67482SBill Paul 
55995d67482SBill Paul 	for (i = 0; i < cnt; i++) {
5603f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
5613f74909aSGleb Smirnoff 		if (error)
56295d67482SBill Paul 			break;
56395d67482SBill Paul 		*(dest + i) = byte;
56495d67482SBill Paul 	}
56595d67482SBill Paul 
5663f74909aSGleb Smirnoff 	return (error ? 1 : 0);
56795d67482SBill Paul }
56895d67482SBill Paul 
56995d67482SBill Paul static int
5703f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
57195d67482SBill Paul {
57295d67482SBill Paul 	struct bge_softc *sc;
5733f74909aSGleb Smirnoff 	uint32_t val, autopoll;
57495d67482SBill Paul 	int i;
57595d67482SBill Paul 
57695d67482SBill Paul 	sc = device_get_softc(dev);
57795d67482SBill Paul 
5780434d1b8SBill Paul 	/*
5790434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
5800434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
5810434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
5820434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
5830434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
5840434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
5850434d1b8SBill Paul 	 * special-cased.
5860434d1b8SBill Paul 	 */
587b1265c1aSJohn Polstra 	if (phy != 1)
58898b28ee5SBill Paul 		return (0);
58998b28ee5SBill Paul 
59037ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
59137ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
59237ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
59337ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
59437ceeb4dSPaul Saab 		DELAY(40);
59537ceeb4dSPaul Saab 	}
59637ceeb4dSPaul Saab 
59795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
59895d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
59995d67482SBill Paul 
60095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
60195d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
60295d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
60395d67482SBill Paul 			break;
60495d67482SBill Paul 	}
60595d67482SBill Paul 
60695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
6076b9f5c94SGleb Smirnoff 		device_printf(sc->bge_dev, "PHY read timed out\n");
60837ceeb4dSPaul Saab 		val = 0;
60937ceeb4dSPaul Saab 		goto done;
61095d67482SBill Paul 	}
61195d67482SBill Paul 
61295d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
61395d67482SBill Paul 
61437ceeb4dSPaul Saab done:
61537ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
61637ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
61737ceeb4dSPaul Saab 		DELAY(40);
61837ceeb4dSPaul Saab 	}
61937ceeb4dSPaul Saab 
62095d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
62195d67482SBill Paul 		return (0);
62295d67482SBill Paul 
62395d67482SBill Paul 	return (val & 0xFFFF);
62495d67482SBill Paul }
62595d67482SBill Paul 
62695d67482SBill Paul static int
6273f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
62895d67482SBill Paul {
62995d67482SBill Paul 	struct bge_softc *sc;
6303f74909aSGleb Smirnoff 	uint32_t autopoll;
63195d67482SBill Paul 	int i;
63295d67482SBill Paul 
63395d67482SBill Paul 	sc = device_get_softc(dev);
63495d67482SBill Paul 
63537ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
63637ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
63737ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
63837ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
63937ceeb4dSPaul Saab 		DELAY(40);
64037ceeb4dSPaul Saab 	}
64137ceeb4dSPaul Saab 
64295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
64395d67482SBill Paul 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
64495d67482SBill Paul 
64595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
64695d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
64795d67482SBill Paul 			break;
64895d67482SBill Paul 	}
64995d67482SBill Paul 
65037ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
65137ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
65237ceeb4dSPaul Saab 		DELAY(40);
65337ceeb4dSPaul Saab 	}
65437ceeb4dSPaul Saab 
65595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
6566b9f5c94SGleb Smirnoff 		device_printf(sc->bge_dev, "PHY read timed out\n");
65795d67482SBill Paul 		return (0);
65895d67482SBill Paul 	}
65995d67482SBill Paul 
66095d67482SBill Paul 	return (0);
66195d67482SBill Paul }
66295d67482SBill Paul 
66395d67482SBill Paul static void
6643f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
66595d67482SBill Paul {
66695d67482SBill Paul 	struct bge_softc *sc;
66795d67482SBill Paul 	struct mii_data *mii;
66895d67482SBill Paul 	sc = device_get_softc(dev);
66995d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
67095d67482SBill Paul 
67195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
6723f74909aSGleb Smirnoff 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)
67395d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
6743f74909aSGleb Smirnoff 	else
67595d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
67695d67482SBill Paul 
6773f74909aSGleb Smirnoff 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
67895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
6793f74909aSGleb Smirnoff 	else
68095d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
68195d67482SBill Paul }
68295d67482SBill Paul 
68395d67482SBill Paul /*
68495d67482SBill Paul  * Intialize a standard receive ring descriptor.
68595d67482SBill Paul  */
68695d67482SBill Paul static int
6873f74909aSGleb Smirnoff bge_newbuf_std(struct bge_softc *sc, int i, struct mbuf *m)
68895d67482SBill Paul {
68995d67482SBill Paul 	struct mbuf *m_new = NULL;
69095d67482SBill Paul 	struct bge_rx_bd *r;
691f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
692f41ac2beSBill Paul 	int error;
69395d67482SBill Paul 
69495d67482SBill Paul 	if (m == NULL) {
695c3a56752SGleb Smirnoff 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
696c3a56752SGleb Smirnoff 		if (m_new == NULL)
69795d67482SBill Paul 			return (ENOBUFS);
69895d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
69995d67482SBill Paul 	} else {
70095d67482SBill Paul 		m_new = m;
70195d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
70295d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
70395d67482SBill Paul 	}
70495d67482SBill Paul 
705652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
70695d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
70795d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
708f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
709f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
710f41ac2beSBill Paul 	ctx.sc = sc;
711f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
712f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
713f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
714f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
715f7cea149SGleb Smirnoff 		if (m == NULL) {
716f7cea149SGleb Smirnoff 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
717f41ac2beSBill Paul 			m_freem(m_new);
718f7cea149SGleb Smirnoff 		}
719f41ac2beSBill Paul 		return (ENOMEM);
720f41ac2beSBill Paul 	}
721e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(ctx.bge_busaddr);
722e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(ctx.bge_busaddr);
723e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
724e907febfSPyun YongHyeon 	r->bge_len = m_new->m_len;
725e907febfSPyun YongHyeon 	r->bge_idx = i;
726f41ac2beSBill Paul 
727f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
728f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
729f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
73095d67482SBill Paul 
73195d67482SBill Paul 	return (0);
73295d67482SBill Paul }
73395d67482SBill Paul 
73495d67482SBill Paul /*
73595d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
73695d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
73795d67482SBill Paul  */
73895d67482SBill Paul static int
7393f74909aSGleb Smirnoff bge_newbuf_jumbo(struct bge_softc *sc, int i, struct mbuf *m)
74095d67482SBill Paul {
7411be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
7421be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
74395d67482SBill Paul 	struct mbuf *m_new = NULL;
7441be6acb7SGleb Smirnoff 	int nsegs;
745f41ac2beSBill Paul 	int error;
74695d67482SBill Paul 
74795d67482SBill Paul 	if (m == NULL) {
748a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
7491be6acb7SGleb Smirnoff 		if (m_new == NULL)
75095d67482SBill Paul 			return (ENOBUFS);
75195d67482SBill Paul 
7521be6acb7SGleb Smirnoff 		m_cljget(m_new, M_DONTWAIT, MJUM9BYTES);
7531be6acb7SGleb Smirnoff 		if (!(m_new->m_flags & M_EXT)) {
75495d67482SBill Paul 			m_freem(m_new);
75595d67482SBill Paul 			return (ENOBUFS);
75695d67482SBill Paul 		}
7571be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
75895d67482SBill Paul 	} else {
75995d67482SBill Paul 		m_new = m;
7601be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
76195d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
76295d67482SBill Paul 	}
76395d67482SBill Paul 
764652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
76595d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
7661be6acb7SGleb Smirnoff 
7671be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
7681be6acb7SGleb Smirnoff 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
7691be6acb7SGleb Smirnoff 	    m_new, segs, &nsegs, BUS_DMA_NOWAIT);
7701be6acb7SGleb Smirnoff 	if (error) {
7711be6acb7SGleb Smirnoff 		if (m == NULL)
772f41ac2beSBill Paul 			m_freem(m_new);
7731be6acb7SGleb Smirnoff 		return (error);
774f7cea149SGleb Smirnoff 	}
7751be6acb7SGleb Smirnoff 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
7761be6acb7SGleb Smirnoff 
7771be6acb7SGleb Smirnoff 	/*
7781be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
7791be6acb7SGleb Smirnoff 	 */
7801be6acb7SGleb Smirnoff 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
7814e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING|BGE_RXBDFLAG_END;
7824e7ba1abSGleb Smirnoff 	r->bge_idx = i;
7834e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
7844e7ba1abSGleb Smirnoff 	switch (nsegs) {
7854e7ba1abSGleb Smirnoff 	case 4:
7864e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
7874e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
7884e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
7894e7ba1abSGleb Smirnoff 	case 3:
790e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
791e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
792e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
7934e7ba1abSGleb Smirnoff 	case 2:
7944e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
7954e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
7964e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
7974e7ba1abSGleb Smirnoff 	case 1:
7984e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
7994e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
8004e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
8014e7ba1abSGleb Smirnoff 		break;
8024e7ba1abSGleb Smirnoff 	default:
8034e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
8044e7ba1abSGleb Smirnoff 	}
805f41ac2beSBill Paul 
806f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
807f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
808f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
80995d67482SBill Paul 
81095d67482SBill Paul 	return (0);
81195d67482SBill Paul }
81295d67482SBill Paul 
81395d67482SBill Paul /*
81495d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
81595d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
81695d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
81795d67482SBill Paul  * the NIC.
81895d67482SBill Paul  */
81995d67482SBill Paul static int
8203f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
82195d67482SBill Paul {
82295d67482SBill Paul 	int i;
82395d67482SBill Paul 
82495d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
82595d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
82695d67482SBill Paul 			return (ENOBUFS);
82795d67482SBill Paul 	};
82895d67482SBill Paul 
829f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
830f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
831f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
832f41ac2beSBill Paul 
83395d67482SBill Paul 	sc->bge_std = i - 1;
83495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
83595d67482SBill Paul 
83695d67482SBill Paul 	return (0);
83795d67482SBill Paul }
83895d67482SBill Paul 
83995d67482SBill Paul static void
8403f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
84195d67482SBill Paul {
84295d67482SBill Paul 	int i;
84395d67482SBill Paul 
84495d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
84595d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
846e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
847e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
848e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
849f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
850f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
851e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
852e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
85395d67482SBill Paul 		}
854f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
85595d67482SBill Paul 		    sizeof(struct bge_rx_bd));
85695d67482SBill Paul 	}
85795d67482SBill Paul }
85895d67482SBill Paul 
85995d67482SBill Paul static int
8603f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
86195d67482SBill Paul {
86295d67482SBill Paul 	struct bge_rcb *rcb;
8631be6acb7SGleb Smirnoff 	int i;
86495d67482SBill Paul 
86595d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
86695d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
86795d67482SBill Paul 			return (ENOBUFS);
86895d67482SBill Paul 	};
86995d67482SBill Paul 
870f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
871f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
872f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
873f41ac2beSBill Paul 
87495d67482SBill Paul 	sc->bge_jumbo = i - 1;
87595d67482SBill Paul 
876f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
8771be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
8781be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
87967111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
88095d67482SBill Paul 
88195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
88295d67482SBill Paul 
88395d67482SBill Paul 	return (0);
88495d67482SBill Paul }
88595d67482SBill Paul 
88695d67482SBill Paul static void
8873f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
88895d67482SBill Paul {
88995d67482SBill Paul 	int i;
89095d67482SBill Paul 
89195d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
89295d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
893e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
894e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
895e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
896f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
897f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
898e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
899e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
90095d67482SBill Paul 		}
901f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
9021be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
90395d67482SBill Paul 	}
90495d67482SBill Paul }
90595d67482SBill Paul 
90695d67482SBill Paul static void
9073f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
90895d67482SBill Paul {
90995d67482SBill Paul 	int i;
91095d67482SBill Paul 
911f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
91295d67482SBill Paul 		return;
91395d67482SBill Paul 
91495d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
91595d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
916e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
917e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
918e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
919f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
920f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
921e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
922e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
92395d67482SBill Paul 		}
924f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
92595d67482SBill Paul 		    sizeof(struct bge_tx_bd));
92695d67482SBill Paul 	}
92795d67482SBill Paul }
92895d67482SBill Paul 
92995d67482SBill Paul static int
9303f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
93195d67482SBill Paul {
93295d67482SBill Paul 	sc->bge_txcnt = 0;
93395d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
9343927098fSPaul Saab 
93514bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
93614bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
93714bbd30fSGleb Smirnoff 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
93814bbd30fSGleb Smirnoff 
9393927098fSPaul Saab 	/* 5700 b2 errata */
940e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
94114bbd30fSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
9423927098fSPaul Saab 
94314bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
9443927098fSPaul Saab 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
9453927098fSPaul Saab 	/* 5700 b2 errata */
946e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
94795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
94895d67482SBill Paul 
94995d67482SBill Paul 	return (0);
95095d67482SBill Paul }
95195d67482SBill Paul 
95295d67482SBill Paul static void
9533e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
9543e9b1bcaSJung-uk Kim {
9553e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
9563e9b1bcaSJung-uk Kim 
9573e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
9583e9b1bcaSJung-uk Kim 
9593e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
9603e9b1bcaSJung-uk Kim 
96145ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
9623e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
96345ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
9643e9b1bcaSJung-uk Kim 	else
96545ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
9663e9b1bcaSJung-uk Kim }
9673e9b1bcaSJung-uk Kim 
9683e9b1bcaSJung-uk Kim static void
9693f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
97095d67482SBill Paul {
97195d67482SBill Paul 	struct ifnet *ifp;
97295d67482SBill Paul 	struct ifmultiaddr *ifma;
9733f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
97495d67482SBill Paul 	int h, i;
97595d67482SBill Paul 
9760f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
9770f9bd73bSSam Leffler 
978fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
97995d67482SBill Paul 
98095d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
98195d67482SBill Paul 		for (i = 0; i < 4; i++)
98295d67482SBill Paul 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
98395d67482SBill Paul 		return;
98495d67482SBill Paul 	}
98595d67482SBill Paul 
98695d67482SBill Paul 	/* First, zot all the existing filters. */
98795d67482SBill Paul 	for (i = 0; i < 4; i++)
98895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
98995d67482SBill Paul 
99095d67482SBill Paul 	/* Now program new ones. */
99113b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
99295d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
99395d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
99495d67482SBill Paul 			continue;
9950e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
9960e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
99795d67482SBill Paul 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
99895d67482SBill Paul 	}
99913b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
100095d67482SBill Paul 
100195d67482SBill Paul 	for (i = 0; i < 4; i++)
100295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
100395d67482SBill Paul }
100495d67482SBill Paul 
10058cb1383cSDoug Ambrisko static void
10068cb1383cSDoug Ambrisko bge_sig_pre_reset(sc, type)
10078cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10088cb1383cSDoug Ambrisko 	int type;
10098cb1383cSDoug Ambrisko {
10108cb1383cSDoug Ambrisko 	/*
10118cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
10128cb1383cSDoug Ambrisko 	 */
10138cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
10148cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
10158cb1383cSDoug Ambrisko 
10168cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
10178cb1383cSDoug Ambrisko 		switch (type) {
10188cb1383cSDoug Ambrisko 		case BGE_RESET_START:
10198cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
10208cb1383cSDoug Ambrisko 			break;
10218cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
10228cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
10238cb1383cSDoug Ambrisko 			break;
10248cb1383cSDoug Ambrisko 		}
10258cb1383cSDoug Ambrisko 	}
10268cb1383cSDoug Ambrisko }
10278cb1383cSDoug Ambrisko 
10288cb1383cSDoug Ambrisko static void
10298cb1383cSDoug Ambrisko bge_sig_post_reset(sc, type)
10308cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10318cb1383cSDoug Ambrisko 	int type;
10328cb1383cSDoug Ambrisko {
10338cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
10348cb1383cSDoug Ambrisko 		switch (type) {
10358cb1383cSDoug Ambrisko 		case BGE_RESET_START:
10368cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000001);
10378cb1383cSDoug Ambrisko 			/* START DONE */
10388cb1383cSDoug Ambrisko 			break;
10398cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
10408cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000002);
10418cb1383cSDoug Ambrisko 			break;
10428cb1383cSDoug Ambrisko 		}
10438cb1383cSDoug Ambrisko 	}
10448cb1383cSDoug Ambrisko }
10458cb1383cSDoug Ambrisko 
10468cb1383cSDoug Ambrisko static void
10478cb1383cSDoug Ambrisko bge_sig_legacy(sc, type)
10488cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10498cb1383cSDoug Ambrisko 	int type;
10508cb1383cSDoug Ambrisko {
10518cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
10528cb1383cSDoug Ambrisko 		switch (type) {
10538cb1383cSDoug Ambrisko 		case BGE_RESET_START:
10548cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
10558cb1383cSDoug Ambrisko 			break;
10568cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
10578cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
10588cb1383cSDoug Ambrisko 			break;
10598cb1383cSDoug Ambrisko 		}
10608cb1383cSDoug Ambrisko 	}
10618cb1383cSDoug Ambrisko }
10628cb1383cSDoug Ambrisko 
10638cb1383cSDoug Ambrisko void bge_stop_fw(struct bge_softc *);
10648cb1383cSDoug Ambrisko void
10658cb1383cSDoug Ambrisko bge_stop_fw(sc)
10668cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10678cb1383cSDoug Ambrisko {
10688cb1383cSDoug Ambrisko 	int i;
10698cb1383cSDoug Ambrisko 
10708cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
10718cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW, BGE_FW_PAUSE);
10728cb1383cSDoug Ambrisko 		CSR_WRITE_4(sc, BGE_CPU_EVENT,
10738cb1383cSDoug Ambrisko 			    CSR_READ_4(sc, BGE_CPU_EVENT) != (1 << 14));
10748cb1383cSDoug Ambrisko 
10758cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
10768cb1383cSDoug Ambrisko 			if (!(CSR_READ_4(sc, BGE_CPU_EVENT) & (1 << 14)))
10778cb1383cSDoug Ambrisko 				break;
10788cb1383cSDoug Ambrisko 			DELAY(10);
10798cb1383cSDoug Ambrisko 		}
10808cb1383cSDoug Ambrisko 	}
10818cb1383cSDoug Ambrisko }
10828cb1383cSDoug Ambrisko 
108395d67482SBill Paul /*
108495d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
108595d67482SBill Paul  * self-test results.
108695d67482SBill Paul  */
108795d67482SBill Paul static int
10883f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
108995d67482SBill Paul {
10903f74909aSGleb Smirnoff 	uint32_t dma_rw_ctl;
109195d67482SBill Paul 	int i;
109295d67482SBill Paul 
10938cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
1094e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
109595d67482SBill Paul 
109695d67482SBill Paul 	/*
109795d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
109895d67482SBill Paul 	 * self-tests passed.
109995d67482SBill Paul 	 */
110095d67482SBill Paul 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
1101fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "RX CPU self-diagnostics failed!\n");
110295d67482SBill Paul 		return (ENODEV);
110395d67482SBill Paul 	}
110495d67482SBill Paul 
110595d67482SBill Paul 	/* Clear the MAC control register */
110695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
110795d67482SBill Paul 
110895d67482SBill Paul 	/*
110995d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
111095d67482SBill Paul 	 * internal memory.
111195d67482SBill Paul 	 */
111295d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
11133f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
111495d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
111595d67482SBill Paul 
111695d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
11173f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
111895d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
111995d67482SBill Paul 
112095d67482SBill Paul 	/* Set up the PCI DMA control register. */
1121652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
11224c0da0ffSGleb Smirnoff 		/* PCI Express bus */
1123e53d81eeSPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1124e53d81eeSPaul Saab 		    (0xf << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1125e53d81eeSPaul Saab 		    (0x2 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1126652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
11278287860eSJohn Polstra 		/* PCI-X bus */
11284c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
11294c0da0ffSGleb Smirnoff 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD;
11304c0da0ffSGleb Smirnoff 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_ONEDMA_ATONCE; /* XXX */
11314c0da0ffSGleb Smirnoff 			/* XXX magic values, Broadcom-supplied Linux driver */
11324c0da0ffSGleb Smirnoff 			if (sc->bge_asicrev == BGE_ASICREV_BCM5780)
11334c0da0ffSGleb Smirnoff 				dma_rw_ctl |= (1 << 20) | (1 << 18) |
11344c0da0ffSGleb Smirnoff 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE;
11354c0da0ffSGleb Smirnoff 			else
11364c0da0ffSGleb Smirnoff 				dma_rw_ctl |= (1 << 20) | (1 << 18) | (1 << 15);
11374c0da0ffSGleb Smirnoff 
11384c0da0ffSGleb Smirnoff 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
11395cba12d3SPaul Saab 			/*
11405cba12d3SPaul Saab 			 * The 5704 uses a different encoding of read/write
11415cba12d3SPaul Saab 			 * watermarks.
11425cba12d3SPaul Saab 			 */
11435cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
11445cba12d3SPaul Saab 			    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
11455cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
11465cba12d3SPaul Saab 		else
11475cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
11485cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
11495cba12d3SPaul Saab 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
11505cba12d3SPaul Saab 			    (0x0F);
11515cba12d3SPaul Saab 
11525cba12d3SPaul Saab 		/*
11535cba12d3SPaul Saab 		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
11545cba12d3SPaul Saab 		 * for hardware bugs.
11555cba12d3SPaul Saab 		 */
1156e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1157e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
11583f74909aSGleb Smirnoff 			uint32_t tmp;
11595cba12d3SPaul Saab 
11605cba12d3SPaul Saab 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
11615cba12d3SPaul Saab 			if (tmp == 0x6 || tmp == 0x7)
11625cba12d3SPaul Saab 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
11638287860eSJohn Polstra 		}
11644c0da0ffSGleb Smirnoff 	} else
11654c0da0ffSGleb Smirnoff 		/* Conventional PCI bus */
11664c0da0ffSGleb Smirnoff 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
11674c0da0ffSGleb Smirnoff 		    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
11684c0da0ffSGleb Smirnoff 		    (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
11694c0da0ffSGleb Smirnoff 		    (0x0F);
11705cba12d3SPaul Saab 
1171e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
11720434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
11734c0da0ffSGleb Smirnoff 	    sc->bge_asicrev == BGE_ASICREV_BCM5705)
11745cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
11755cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
117695d67482SBill Paul 
117795d67482SBill Paul 	/*
117895d67482SBill Paul 	 * Set up general mode register.
117995d67482SBill Paul 	 */
1180e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
118195d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1182ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
118395d67482SBill Paul 
118495d67482SBill Paul 	/*
11858cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
11868cb1383cSDoug Ambrisko 	 */
11878cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
11888cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
11898cb1383cSDoug Ambrisko 
11908cb1383cSDoug Ambrisko 	/*
1191ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1192ea13bdd5SJohn Polstra 	 * properly by these devices.
119395d67482SBill Paul 	 */
1194ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
119595d67482SBill Paul 
119695d67482SBill Paul #ifdef __brokenalpha__
119795d67482SBill Paul 	/*
119895d67482SBill Paul 	 * Must insure that we do not cross an 8K (bytes) boundary
119995d67482SBill Paul 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
120095d67482SBill Paul 	 * restriction on some ALPHA platforms with early revision
120195d67482SBill Paul 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
120295d67482SBill Paul 	 */
120362f1ea9cSJohn Polstra 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
120462f1ea9cSJohn Polstra 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
120595d67482SBill Paul #endif
120695d67482SBill Paul 
120795d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
120895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
120995d67482SBill Paul 
121095d67482SBill Paul 	return (0);
121195d67482SBill Paul }
121295d67482SBill Paul 
121395d67482SBill Paul static int
12143f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
121595d67482SBill Paul {
121695d67482SBill Paul 	struct bge_rcb *rcb;
1217e907febfSPyun YongHyeon 	bus_size_t vrcb;
1218e907febfSPyun YongHyeon 	bge_hostaddr taddr;
12196f8718a3SScott Long 	uint32_t val;
122095d67482SBill Paul 	int i;
122195d67482SBill Paul 
122295d67482SBill Paul 	/*
122395d67482SBill Paul 	 * Initialize the memory window pointer register so that
122495d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
122595d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
122695d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
122795d67482SBill Paul 	 */
122895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
122995d67482SBill Paul 
1230822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1231822f63fcSBill Paul 
12327ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
123395d67482SBill Paul 		/* Configure mbuf memory pool */
12340dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1235822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1236822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1237822f63fcSBill Paul 		else
123895d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
123995d67482SBill Paul 
124095d67482SBill Paul 		/* Configure DMA resource pool */
12410434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
12420434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
124395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
12440434d1b8SBill Paul 	}
124595d67482SBill Paul 
124695d67482SBill Paul 	/* Configure mbuf pool watermarks */
12477ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
12480434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
12490434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
12500434d1b8SBill Paul 	} else {
1251fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1252fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
12530434d1b8SBill Paul 	}
1254fa228020SPaul Saab 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
125595d67482SBill Paul 
125695d67482SBill Paul 	/* Configure DMA resource watermarks */
125795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
125895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
125995d67482SBill Paul 
126095d67482SBill Paul 	/* Enable buffer manager */
12617ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
126295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
126395d67482SBill Paul 		    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
126495d67482SBill Paul 
126595d67482SBill Paul 		/* Poll for buffer manager start indication */
126695d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
126795d67482SBill Paul 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
126895d67482SBill Paul 				break;
126995d67482SBill Paul 			DELAY(10);
127095d67482SBill Paul 		}
127195d67482SBill Paul 
127295d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1273fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1274fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
127595d67482SBill Paul 			return (ENXIO);
127695d67482SBill Paul 		}
12770434d1b8SBill Paul 	}
127895d67482SBill Paul 
127995d67482SBill Paul 	/* Enable flow-through queues */
128095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
128195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
128295d67482SBill Paul 
128395d67482SBill Paul 	/* Wait until queue initialization is complete */
128495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
128595d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
128695d67482SBill Paul 			break;
128795d67482SBill Paul 		DELAY(10);
128895d67482SBill Paul 	}
128995d67482SBill Paul 
129095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1291fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
129295d67482SBill Paul 		return (ENXIO);
129395d67482SBill Paul 	}
129495d67482SBill Paul 
129595d67482SBill Paul 	/* Initialize the standard RX ring control block */
1296f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1297f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1298f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1299f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1300f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1301f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1302f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
13037ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
13040434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
13050434d1b8SBill Paul 	else
13060434d1b8SBill Paul 		rcb->bge_maxlen_flags =
13070434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
130895d67482SBill Paul 	rcb->bge_nicaddr = BGE_STD_RX_RINGS;
130967111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
131067111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1311f41ac2beSBill Paul 
131267111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
131367111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
131495d67482SBill Paul 
131595d67482SBill Paul 	/*
131695d67482SBill Paul 	 * Initialize the jumbo RX ring control block
131795d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
131895d67482SBill Paul 	 * field until we're actually ready to start
131995d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
132095d67482SBill Paul 	 * high enough to require it).
132195d67482SBill Paul 	 */
13224c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1323f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1324f41ac2beSBill Paul 
1325f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1326f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1327f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1328f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1329f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1330f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1331f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
13321be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
13331be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD|BGE_RCB_FLAG_RING_DISABLED);
133495d67482SBill Paul 		rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
133567111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
133667111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
133767111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
133867111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1339f41ac2beSBill Paul 
13400434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
13410434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
134267111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
134395d67482SBill Paul 
134495d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1345f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
134667111612SJohn Polstra 		rcb->bge_maxlen_flags =
134767111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
13480434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
13490434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
13500434d1b8SBill Paul 	}
135195d67482SBill Paul 
135295d67482SBill Paul 	/*
135395d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
135495d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
135595d67482SBill Paul 	 * each ring.
13569ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
13579ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
13589ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
13599ba784dbSScott Long 	 * are reports that it might not need to be so strict.
136095d67482SBill Paul 	 */
13615345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
13626f8718a3SScott Long 		val = 8;
13636f8718a3SScott Long 	else
13646f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
13656f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
136695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
136795d67482SBill Paul 
136895d67482SBill Paul 	/*
136995d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
137095d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
137195d67482SBill Paul 	 * These are located in NIC memory.
137295d67482SBill Paul 	 */
1373e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
137495d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1375e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1376e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1377e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1378e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
137995d67482SBill Paul 	}
138095d67482SBill Paul 
138195d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
1382e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1383e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1384e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1385e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1386e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1387e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
13887ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
1389e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1390e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
139195d67482SBill Paul 
139295d67482SBill Paul 	/* Disable all unused RX return rings */
1393e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
139495d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1395e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1396e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1397e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
13980434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1399e907febfSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED));
1400e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
140195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
14023f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1403e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
140495d67482SBill Paul 	}
140595d67482SBill Paul 
140695d67482SBill Paul 	/* Initialize RX ring indexes */
140795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
140895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
140995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
141095d67482SBill Paul 
141195d67482SBill Paul 	/*
141295d67482SBill Paul 	 * Set up RX return ring 0
141395d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
141495d67482SBill Paul 	 * The return rings live entirely within the host, so the
141595d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
141695d67482SBill Paul 	 */
1417e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1418e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1419e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1420e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1421e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1422e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1423e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
142495d67482SBill Paul 
142595d67482SBill Paul 	/* Set random backoff seed for TX */
142695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
14274a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
14284a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
14294a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
143095d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
143195d67482SBill Paul 
143295d67482SBill Paul 	/* Set inter-packet gap */
143395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
143495d67482SBill Paul 
143595d67482SBill Paul 	/*
143695d67482SBill Paul 	 * Specify which ring to use for packets that don't match
143795d67482SBill Paul 	 * any RX rules.
143895d67482SBill Paul 	 */
143995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
144095d67482SBill Paul 
144195d67482SBill Paul 	/*
144295d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
144395d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
144495d67482SBill Paul 	 */
144595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
144695d67482SBill Paul 
144795d67482SBill Paul 	/* Inialize RX list placement stats mask. */
144895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
144995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
145095d67482SBill Paul 
145195d67482SBill Paul 	/* Disable host coalescing until we get it set up */
145295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
145395d67482SBill Paul 
145495d67482SBill Paul 	/* Poll to make sure it's shut down. */
145595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
145695d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
145795d67482SBill Paul 			break;
145895d67482SBill Paul 		DELAY(10);
145995d67482SBill Paul 	}
146095d67482SBill Paul 
146195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1462fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1463fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
146495d67482SBill Paul 		return (ENXIO);
146595d67482SBill Paul 	}
146695d67482SBill Paul 
146795d67482SBill Paul 	/* Set up host coalescing defaults */
146895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
146995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
147095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
147195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
14727ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
147395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
147495d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
14750434d1b8SBill Paul 	}
1476b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
1477b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
147895d67482SBill Paul 
147995d67482SBill Paul 	/* Set up address of statistics block */
14807ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
1481f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1482f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
148395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1484f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
14850434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
148695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
14870434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
14880434d1b8SBill Paul 	}
14890434d1b8SBill Paul 
14900434d1b8SBill Paul 	/* Set up address of status block */
1491f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1492f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
149395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1494f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1495f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1496f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
149795d67482SBill Paul 
149895d67482SBill Paul 	/* Turn on host coalescing state machine */
149995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
150095d67482SBill Paul 
150195d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
150295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
150395d67482SBill Paul 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
150495d67482SBill Paul 
150595d67482SBill Paul 	/* Turn on RX list placement state machine */
150695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
150795d67482SBill Paul 
150895d67482SBill Paul 	/* Turn on RX list selector state machine. */
15097ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
151095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
151195d67482SBill Paul 
151295d67482SBill Paul 	/* Turn on DMA, clear stats */
151395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
151495d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
151595d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
151695d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
1517652ae483SGleb Smirnoff 	    ((sc->bge_flags & BGE_FLAG_TBI) ?
1518652ae483SGleb Smirnoff 	    BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
151995d67482SBill Paul 
152095d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
152195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
152295d67482SBill Paul 
152395d67482SBill Paul #ifdef notdef
152495d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
152595d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
152695d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
152795d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
152895d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
152995d67482SBill Paul #endif
153095d67482SBill Paul 
153195d67482SBill Paul 	/* Turn on DMA completion state machine */
15327ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
153395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
153495d67482SBill Paul 
15356f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS;
15366f8718a3SScott Long 
15376f8718a3SScott Long 	/* Enable host coalescing bug fix. */
15386f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
15396f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5787)
15406f8718a3SScott Long 			val |= (1 << 29);
15416f8718a3SScott Long 
154295d67482SBill Paul 	/* Turn on write DMA state machine */
15436f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
154495d67482SBill Paul 
154595d67482SBill Paul 	/* Turn on read DMA state machine */
154695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
154795d67482SBill Paul 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
154895d67482SBill Paul 
154995d67482SBill Paul 	/* Turn on RX data completion state machine */
155095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
155195d67482SBill Paul 
155295d67482SBill Paul 	/* Turn on RX BD initiator state machine */
155395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
155495d67482SBill Paul 
155595d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
155695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
155795d67482SBill Paul 
155895d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
15597ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
156095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
156195d67482SBill Paul 
156295d67482SBill Paul 	/* Turn on send BD completion state machine */
156395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
156495d67482SBill Paul 
156595d67482SBill Paul 	/* Turn on send data completion state machine */
156695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
156795d67482SBill Paul 
156895d67482SBill Paul 	/* Turn on send data initiator state machine */
156995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
157095d67482SBill Paul 
157195d67482SBill Paul 	/* Turn on send BD initiator state machine */
157295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
157395d67482SBill Paul 
157495d67482SBill Paul 	/* Turn on send BD selector state machine */
157595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
157695d67482SBill Paul 
157795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
157895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
157995d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
158095d67482SBill Paul 
158195d67482SBill Paul 	/* ack/clear link change events */
158295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
15830434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
15840434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1585f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
158695d67482SBill Paul 
158795d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
1588652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
158995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1590a1d52896SBill Paul 	} else {
159195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
15921f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
15934c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
1594a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1595a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1596a1d52896SBill Paul 	}
159795d67482SBill Paul 
15981f313773SOleg Bulyzhin 	/*
15991f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
16001f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
16011f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
16021f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
16031f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
16041f313773SOleg Bulyzhin 	 */
16051f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
16061f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
16071f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
16081f313773SOleg Bulyzhin 
160995d67482SBill Paul 	/* Enable link state change attentions. */
161095d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
161195d67482SBill Paul 
161295d67482SBill Paul 	return (0);
161395d67482SBill Paul }
161495d67482SBill Paul 
16154c0da0ffSGleb Smirnoff const struct bge_revision *
16164c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
16174c0da0ffSGleb Smirnoff {
16184c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
16194c0da0ffSGleb Smirnoff 
16204c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
16214c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
16224c0da0ffSGleb Smirnoff 			return (br);
16234c0da0ffSGleb Smirnoff 	}
16244c0da0ffSGleb Smirnoff 
16254c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
16264c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
16274c0da0ffSGleb Smirnoff 			return (br);
16284c0da0ffSGleb Smirnoff 	}
16294c0da0ffSGleb Smirnoff 
16304c0da0ffSGleb Smirnoff 	return (NULL);
16314c0da0ffSGleb Smirnoff }
16324c0da0ffSGleb Smirnoff 
16334c0da0ffSGleb Smirnoff const struct bge_vendor *
16344c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
16354c0da0ffSGleb Smirnoff {
16364c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
16374c0da0ffSGleb Smirnoff 
16384c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
16394c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
16404c0da0ffSGleb Smirnoff 			return (v);
16414c0da0ffSGleb Smirnoff 
16424c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
16434c0da0ffSGleb Smirnoff 	return (NULL);
16444c0da0ffSGleb Smirnoff }
16454c0da0ffSGleb Smirnoff 
164695d67482SBill Paul /*
164795d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
16484c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
16494c0da0ffSGleb Smirnoff  *
16504c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
16517c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
16527c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
16537c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
16547c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
165595d67482SBill Paul  */
165695d67482SBill Paul static int
16573f74909aSGleb Smirnoff bge_probe(device_t dev)
165895d67482SBill Paul {
16594c0da0ffSGleb Smirnoff 	struct bge_type *t = bge_devs;
16604c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
16617c929cf9SJung-uk Kim 	uint16_t vid, did;
166295d67482SBill Paul 
166395d67482SBill Paul 	sc->bge_dev = dev;
16647c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
16657c929cf9SJung-uk Kim 	did = pci_get_device(dev);
16664c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
16677c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
16687c929cf9SJung-uk Kim 			char model[64], buf[96];
16694c0da0ffSGleb Smirnoff 			const struct bge_revision *br;
16704c0da0ffSGleb Smirnoff 			const struct bge_vendor *v;
16717c929cf9SJung-uk Kim 			const char *pname;
16724c0da0ffSGleb Smirnoff 			uint32_t id;
16734c0da0ffSGleb Smirnoff 
16744c0da0ffSGleb Smirnoff 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
16754c0da0ffSGleb Smirnoff 			    BGE_PCIMISCCTL_ASICREV;
16764c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
16777c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
16787c929cf9SJung-uk Kim 			if (pci_get_vpd_ident(dev, &pname))
16797c929cf9SJung-uk Kim 				snprintf(model, 64, "%s %s",
16807c929cf9SJung-uk Kim 				    v->v_name,
16817c929cf9SJung-uk Kim 				    br != NULL ? br->br_name :
16827c929cf9SJung-uk Kim 					"NetXtreme Ethernet Controller");
16834c0da0ffSGleb Smirnoff 			else
16847c929cf9SJung-uk Kim 				snprintf(model, 64, "%s", pname);
16857c929cf9SJung-uk Kim 			snprintf(buf, 96, "%s, %sASIC rev. %#04x", model,
16867c929cf9SJung-uk Kim 			    br != NULL ? "" : "unknown ", id >> 16);
16874c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
16886d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
1689652ae483SGleb Smirnoff 				sc->bge_flags |= BGE_FLAG_NO3LED;
169095d67482SBill Paul 			return (0);
169195d67482SBill Paul 		}
169295d67482SBill Paul 		t++;
169395d67482SBill Paul 	}
169495d67482SBill Paul 
169595d67482SBill Paul 	return (ENXIO);
169695d67482SBill Paul }
169795d67482SBill Paul 
1698f41ac2beSBill Paul static void
16993f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
1700f41ac2beSBill Paul {
1701f41ac2beSBill Paul 	int i;
1702f41ac2beSBill Paul 
17033f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
1704f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1705f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1706f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1707f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1708f41ac2beSBill Paul 	}
1709f41ac2beSBill Paul 
17103f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
1711f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1712f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1713f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1714f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1715f41ac2beSBill Paul 	}
1716f41ac2beSBill Paul 
17173f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
1718f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1719f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1720f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1721f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1722f41ac2beSBill Paul 	}
1723f41ac2beSBill Paul 
1724f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1725f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1726f41ac2beSBill Paul 
1727f41ac2beSBill Paul 
17283f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
1729e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
1730e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1731e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
1732e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1733f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1734f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1735f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1736f41ac2beSBill Paul 
1737f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1738f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1739f41ac2beSBill Paul 
17403f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
1741e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1742e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1743e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1744e65bed95SPyun YongHyeon 
1745e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1746e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
1747f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1748f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1749f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1750f41ac2beSBill Paul 
1751f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1752f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1753f41ac2beSBill Paul 
17543f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
1755e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
1756e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1757e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
1758e65bed95SPyun YongHyeon 
1759e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1760e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
1761f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1762f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1763f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1764f41ac2beSBill Paul 
1765f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1766f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1767f41ac2beSBill Paul 
17683f74909aSGleb Smirnoff 	/* Destroy TX ring. */
1769e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
1770e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1771e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
1772e65bed95SPyun YongHyeon 
1773e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
1774f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1775f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1776f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1777f41ac2beSBill Paul 
1778f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1779f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1780f41ac2beSBill Paul 
17813f74909aSGleb Smirnoff 	/* Destroy status block. */
1782e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
1783e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1784e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
1785e65bed95SPyun YongHyeon 
1786e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
1787f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1788f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1789f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1790f41ac2beSBill Paul 
1791f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1792f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1793f41ac2beSBill Paul 
17943f74909aSGleb Smirnoff 	/* Destroy statistics block. */
1795e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
1796e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1797e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
1798e65bed95SPyun YongHyeon 
1799e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
1800f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1801f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1802f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1803f41ac2beSBill Paul 
1804f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1805f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1806f41ac2beSBill Paul 
18073f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
1808f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1809f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1810f41ac2beSBill Paul }
1811f41ac2beSBill Paul 
1812f41ac2beSBill Paul static int
18133f74909aSGleb Smirnoff bge_dma_alloc(device_t dev)
1814f41ac2beSBill Paul {
18153f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
1816f41ac2beSBill Paul 	struct bge_softc *sc;
18171be6acb7SGleb Smirnoff 	int i, error;
1818f41ac2beSBill Paul 
1819f41ac2beSBill Paul 	sc = device_get_softc(dev);
1820f41ac2beSBill Paul 
1821f41ac2beSBill Paul 	/*
1822f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1823f41ac2beSBill Paul 	 */
1824378f231eSJohn-Mark Gurney 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),/* parent */
1825706620f0SScott Long 			1, 0,			/* alignment, boundary */
1826f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
18272f28b973SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1828f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1829f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1830f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
18318a40c10eSScott Long 			0,			/* flags */
1832f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1833f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1834f41ac2beSBill Paul 
1835e65bed95SPyun YongHyeon 	if (error != 0) {
1836fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1837fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
1838e65bed95SPyun YongHyeon 		return (ENOMEM);
1839e65bed95SPyun YongHyeon 	}
1840e65bed95SPyun YongHyeon 
1841f41ac2beSBill Paul 	/*
1842f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1843f41ac2beSBill Paul 	 */
18448a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1845f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
18461be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
18471be6acb7SGleb Smirnoff 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
1848f41ac2beSBill Paul 
1849f41ac2beSBill Paul 	if (error) {
1850fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1851f41ac2beSBill Paul 		return (ENOMEM);
1852f41ac2beSBill Paul 	}
1853f41ac2beSBill Paul 
18543f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
1855f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1856f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1857f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1858f41ac2beSBill Paul 		if (error) {
1859fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1860fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1861f41ac2beSBill Paul 			return (ENOMEM);
1862f41ac2beSBill Paul 		}
1863f41ac2beSBill Paul 	}
1864f41ac2beSBill Paul 
18653f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
1866f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1867f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1868f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_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 tag for standard RX ring. */
1877f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1878f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1879f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1880f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1881f41ac2beSBill Paul 
1882f41ac2beSBill Paul 	if (error) {
1883fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1884f41ac2beSBill Paul 		return (ENOMEM);
1885f41ac2beSBill Paul 	}
1886f41ac2beSBill Paul 
18873f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for standard RX ring. */
1888f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1889f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1890f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1891f41ac2beSBill Paul 	if (error)
1892f41ac2beSBill Paul 		return (ENOMEM);
1893f41ac2beSBill Paul 
1894f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1895f41ac2beSBill Paul 
18963f74909aSGleb Smirnoff 	/* Load the address of the standard RX ring. */
1897f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1898f41ac2beSBill Paul 	ctx.sc = sc;
1899f41ac2beSBill Paul 
1900f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1901f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1902f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1903f41ac2beSBill Paul 
1904f41ac2beSBill Paul 	if (error)
1905f41ac2beSBill Paul 		return (ENOMEM);
1906f41ac2beSBill Paul 
1907f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
1908f41ac2beSBill Paul 
19093f74909aSGleb Smirnoff 	/* Create tags for jumbo mbufs. */
19104c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1911f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
19128a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
19131be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
19141be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
1915f41ac2beSBill Paul 		if (error) {
1916fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
19173f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
1918f41ac2beSBill Paul 			return (ENOMEM);
1919f41ac2beSBill Paul 		}
1920f41ac2beSBill Paul 
19213f74909aSGleb Smirnoff 		/* Create tag for jumbo RX ring. */
1922f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1923f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1924f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
1925f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
1926f41ac2beSBill Paul 
1927f41ac2beSBill Paul 		if (error) {
1928fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
19293f74909aSGleb Smirnoff 			    "could not allocate jumbo ring dma tag\n");
1930f41ac2beSBill Paul 			return (ENOMEM);
1931f41ac2beSBill Paul 		}
1932f41ac2beSBill Paul 
19333f74909aSGleb Smirnoff 		/* Allocate DMA'able memory for jumbo RX ring. */
1934f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
19351be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
19361be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1937f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
1938f41ac2beSBill Paul 		if (error)
1939f41ac2beSBill Paul 			return (ENOMEM);
1940f41ac2beSBill Paul 
19413f74909aSGleb Smirnoff 		/* Load the address of the jumbo RX ring. */
1942f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
1943f41ac2beSBill Paul 		ctx.sc = sc;
1944f41ac2beSBill Paul 
1945f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1946f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1947f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
1948f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1949f41ac2beSBill Paul 
1950f41ac2beSBill Paul 		if (error)
1951f41ac2beSBill Paul 			return (ENOMEM);
1952f41ac2beSBill Paul 
1953f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
1954f41ac2beSBill Paul 
19553f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
1956f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1957f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
1958f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1959f41ac2beSBill Paul 			if (error) {
1960fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
19613f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
1962f41ac2beSBill Paul 				return (ENOMEM);
1963f41ac2beSBill Paul 			}
1964f41ac2beSBill Paul 		}
1965f41ac2beSBill Paul 
1966f41ac2beSBill Paul 	}
1967f41ac2beSBill Paul 
19683f74909aSGleb Smirnoff 	/* Create tag for RX return ring. */
1969f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1970f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1971f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
1972f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
1973f41ac2beSBill Paul 
1974f41ac2beSBill Paul 	if (error) {
1975fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1976f41ac2beSBill Paul 		return (ENOMEM);
1977f41ac2beSBill Paul 	}
1978f41ac2beSBill Paul 
19793f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for RX return ring. */
1980f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
1981f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
1982f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
1983f41ac2beSBill Paul 	if (error)
1984f41ac2beSBill Paul 		return (ENOMEM);
1985f41ac2beSBill Paul 
1986f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
1987f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
1988f41ac2beSBill Paul 
19893f74909aSGleb Smirnoff 	/* Load the address of the RX return ring. */
1990f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1991f41ac2beSBill Paul 	ctx.sc = sc;
1992f41ac2beSBill Paul 
1993f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
1994f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
1995f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
1996f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1997f41ac2beSBill Paul 
1998f41ac2beSBill Paul 	if (error)
1999f41ac2beSBill Paul 		return (ENOMEM);
2000f41ac2beSBill Paul 
2001f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
2002f41ac2beSBill Paul 
20033f74909aSGleb Smirnoff 	/* Create tag for TX ring. */
2004f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2005f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2006f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
2007f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
2008f41ac2beSBill Paul 
2009f41ac2beSBill Paul 	if (error) {
2010fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2011f41ac2beSBill Paul 		return (ENOMEM);
2012f41ac2beSBill Paul 	}
2013f41ac2beSBill Paul 
20143f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for TX ring. */
2015f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
2016f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
2017f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
2018f41ac2beSBill Paul 	if (error)
2019f41ac2beSBill Paul 		return (ENOMEM);
2020f41ac2beSBill Paul 
2021f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
2022f41ac2beSBill Paul 
20233f74909aSGleb Smirnoff 	/* Load the address of the TX ring. */
2024f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2025f41ac2beSBill Paul 	ctx.sc = sc;
2026f41ac2beSBill Paul 
2027f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
2028f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
2029f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2030f41ac2beSBill Paul 
2031f41ac2beSBill Paul 	if (error)
2032f41ac2beSBill Paul 		return (ENOMEM);
2033f41ac2beSBill Paul 
2034f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
2035f41ac2beSBill Paul 
20363f74909aSGleb Smirnoff 	/* Create tag for status block. */
2037f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2038f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2039f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
2040f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
2041f41ac2beSBill Paul 
2042f41ac2beSBill Paul 	if (error) {
2043fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2044f41ac2beSBill Paul 		return (ENOMEM);
2045f41ac2beSBill Paul 	}
2046f41ac2beSBill Paul 
20473f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for status block. */
2048f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
2049f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
2050f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
2051f41ac2beSBill Paul 	if (error)
2052f41ac2beSBill Paul 		return (ENOMEM);
2053f41ac2beSBill Paul 
2054f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2055f41ac2beSBill Paul 
20563f74909aSGleb Smirnoff 	/* Load the address of the status block. */
2057f41ac2beSBill Paul 	ctx.sc = sc;
2058f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2059f41ac2beSBill Paul 
2060f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2061f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2062f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2063f41ac2beSBill Paul 
2064f41ac2beSBill Paul 	if (error)
2065f41ac2beSBill Paul 		return (ENOMEM);
2066f41ac2beSBill Paul 
2067f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2068f41ac2beSBill Paul 
20693f74909aSGleb Smirnoff 	/* Create tag for statistics block. */
2070f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2071f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2072f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2073f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
2074f41ac2beSBill Paul 
2075f41ac2beSBill Paul 	if (error) {
2076fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2077f41ac2beSBill Paul 		return (ENOMEM);
2078f41ac2beSBill Paul 	}
2079f41ac2beSBill Paul 
20803f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for statistics block. */
2081f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2082f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2083f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
2084f41ac2beSBill Paul 	if (error)
2085f41ac2beSBill Paul 		return (ENOMEM);
2086f41ac2beSBill Paul 
2087f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2088f41ac2beSBill Paul 
20893f74909aSGleb Smirnoff 	/* Load the address of the statstics block. */
2090f41ac2beSBill Paul 	ctx.sc = sc;
2091f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2092f41ac2beSBill Paul 
2093f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2094f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2095f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2096f41ac2beSBill Paul 
2097f41ac2beSBill Paul 	if (error)
2098f41ac2beSBill Paul 		return (ENOMEM);
2099f41ac2beSBill Paul 
2100f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2101f41ac2beSBill Paul 
2102f41ac2beSBill Paul 	return (0);
2103f41ac2beSBill Paul }
2104f41ac2beSBill Paul 
2105bf6ef57aSJohn Polstra /*
2106bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
2107bf6ef57aSJohn Polstra  */
2108bf6ef57aSJohn Polstra static int
2109bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
2110bf6ef57aSJohn Polstra {
2111bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
2112bf6ef57aSJohn Polstra 	u_int b, s, f, fscan;
2113bf6ef57aSJohn Polstra 
2114bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
2115bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
2116bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
2117bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
2118bf6ef57aSJohn Polstra 		if (fscan != f && pci_find_bsf(b, s, fscan) != NULL)
2119bf6ef57aSJohn Polstra 			return (1);
2120bf6ef57aSJohn Polstra 	return (0);
2121bf6ef57aSJohn Polstra }
2122bf6ef57aSJohn Polstra 
2123bf6ef57aSJohn Polstra /*
2124bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
2125bf6ef57aSJohn Polstra  */
2126bf6ef57aSJohn Polstra static int
2127bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
2128bf6ef57aSJohn Polstra {
2129bf6ef57aSJohn Polstra 	int can_use_msi = 0;
2130bf6ef57aSJohn Polstra 
2131bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
2132bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
2133bf6ef57aSJohn Polstra 		/*
2134bf6ef57aSJohn Polstra 		 * Apparently, MSI doesn't work when this chip is configured
2135bf6ef57aSJohn Polstra 		 * in single-port mode.
2136bf6ef57aSJohn Polstra 		 */
2137bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
2138bf6ef57aSJohn Polstra 			can_use_msi = 1;
2139bf6ef57aSJohn Polstra 		break;
2140bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
2141bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
2142bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
2143bf6ef57aSJohn Polstra 			can_use_msi = 1;
2144bf6ef57aSJohn Polstra 		break;
2145bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5752:
2146bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5780:
2147bf6ef57aSJohn Polstra 		can_use_msi = 1;
2148bf6ef57aSJohn Polstra 		break;
2149bf6ef57aSJohn Polstra 	}
2150bf6ef57aSJohn Polstra 	return (can_use_msi);
2151bf6ef57aSJohn Polstra }
2152bf6ef57aSJohn Polstra 
215395d67482SBill Paul static int
21543f74909aSGleb Smirnoff bge_attach(device_t dev)
215595d67482SBill Paul {
215695d67482SBill Paul 	struct ifnet *ifp;
215795d67482SBill Paul 	struct bge_softc *sc;
21583f74909aSGleb Smirnoff 	uint32_t hwcfg = 0;
21593f74909aSGleb Smirnoff 	uint32_t mac_tmp = 0;
2160fc74a9f9SBrooks Davis 	u_char eaddr[6];
2161724bd939SJohn Polstra 	int error = 0, msicount, rid, trys, reg;
216295d67482SBill Paul 
216395d67482SBill Paul 	sc = device_get_softc(dev);
216495d67482SBill Paul 	sc->bge_dev = dev;
216595d67482SBill Paul 
216695d67482SBill Paul 	/*
216795d67482SBill Paul 	 * Map control/status registers.
216895d67482SBill Paul 	 */
216995d67482SBill Paul 	pci_enable_busmaster(dev);
217095d67482SBill Paul 
217195d67482SBill Paul 	rid = BGE_PCI_BAR0;
21725f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
21735f96beb9SNate Lawson 	    RF_ACTIVE|PCI_RF_DENSE);
217495d67482SBill Paul 
217595d67482SBill Paul 	if (sc->bge_res == NULL) {
2176fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
217795d67482SBill Paul 		error = ENXIO;
217895d67482SBill Paul 		goto fail;
217995d67482SBill Paul 	}
218095d67482SBill Paul 
218195d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
218295d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
218395d67482SBill Paul 
2184e53d81eeSPaul Saab 	/* Save ASIC rev. */
2185e53d81eeSPaul Saab 
2186e53d81eeSPaul Saab 	sc->bge_chipid =
2187e53d81eeSPaul Saab 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2188e53d81eeSPaul Saab 	    BGE_PCIMISCCTL_ASICREV;
2189e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2190e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2191e53d81eeSPaul Saab 
21920dae9719SJung-uk Kim 	/* Save chipset family. */
21930dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
21940dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
21950dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
21960dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
21970dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
21987ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
21990dae9719SJung-uk Kim 		break;
22000dae9719SJung-uk Kim 
22010dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
22020dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
22030dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
22047ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5714_FAMILY /* | BGE_FLAG_JUMBO */;
22050dae9719SJung-uk Kim 		/* Fall through */
22060dae9719SJung-uk Kim 
22070dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
22080dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
22090dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5755:
22100dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5787:
22110dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
22120dae9719SJung-uk Kim 		/* Fall through */
22130dae9719SJung-uk Kim 
22140dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
22150dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
22160dae9719SJung-uk Kim 		break;
22170dae9719SJung-uk Kim 	}
22180dae9719SJung-uk Kim 
2219e53d81eeSPaul Saab   	/*
22206f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
2221e53d81eeSPaul Saab   	 */
22226f8718a3SScott Long #if __FreeBSD_version > 700010
22236f8718a3SScott Long 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
22244c0da0ffSGleb Smirnoff 		/*
22256f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
22266f8718a3SScott Long 		 * must be a PCI Express device.
22276f8718a3SScott Long 		 */
22286f8718a3SScott Long 		if (reg != 0)
22296f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIE;
22306f8718a3SScott Long 	} else if (pci_find_extcap(dev, PCIY_PCIX, &reg) == 0) {
22316f8718a3SScott Long 		if (reg != 0)
22326f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIX;
22336f8718a3SScott Long 	}
22346f8718a3SScott Long 
22356f8718a3SScott Long #else
22365345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc)) {
22376f8718a3SScott Long 		reg = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
22386f8718a3SScott Long 		if ((reg & 0xff) == BGE_PCIE_CAPID)
22396f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIE;
22406f8718a3SScott Long 	} else {
22416f8718a3SScott Long 		/*
22426f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
22436f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
22444c0da0ffSGleb Smirnoff 		 */
22454c0da0ffSGleb Smirnoff 		if ((pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
22464c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
2247652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
22486f8718a3SScott Long 	}
22496f8718a3SScott Long #endif
22504c0da0ffSGleb Smirnoff 
2251bf6ef57aSJohn Polstra 	/*
2252bf6ef57aSJohn Polstra 	 * Allocate the interrupt, using MSI if possible.  These devices
2253bf6ef57aSJohn Polstra 	 * support 8 MSI messages, but only the first one is used in
2254bf6ef57aSJohn Polstra 	 * normal operation.
2255bf6ef57aSJohn Polstra 	 */
2256bf6ef57aSJohn Polstra 	if (bge_can_use_msi(sc)) {
2257bf6ef57aSJohn Polstra 		msicount = pci_msi_count(dev);
2258bf6ef57aSJohn Polstra 		if (msicount > 1)
2259bf6ef57aSJohn Polstra 			msicount = 1;
2260bf6ef57aSJohn Polstra 	} else
2261bf6ef57aSJohn Polstra 		msicount = 0;
2262bf6ef57aSJohn Polstra 	if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
2263bf6ef57aSJohn Polstra 		rid = 1;
2264bf6ef57aSJohn Polstra 		sc->bge_flags |= BGE_FLAG_MSI;
2265bf6ef57aSJohn Polstra 	} else
2266bf6ef57aSJohn Polstra 		rid = 0;
2267bf6ef57aSJohn Polstra 
2268bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2269bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
2270bf6ef57aSJohn Polstra 
2271bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
2272bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
2273bf6ef57aSJohn Polstra 		error = ENXIO;
2274bf6ef57aSJohn Polstra 		goto fail;
2275bf6ef57aSJohn Polstra 	}
2276bf6ef57aSJohn Polstra 
2277bf6ef57aSJohn Polstra 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
2278bf6ef57aSJohn Polstra 
227995d67482SBill Paul 	/* Try to reset the chip. */
22808cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
22818cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
22828cb1383cSDoug Ambrisko 		bge_release_resources(sc);
22838cb1383cSDoug Ambrisko 		error = ENXIO;
22848cb1383cSDoug Ambrisko 		goto fail;
22858cb1383cSDoug Ambrisko 	}
22868cb1383cSDoug Ambrisko 
22878cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
2288f1a7e6d5SScott Long 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG)
2289f1a7e6d5SScott Long 	    == BGE_MAGIC_NUMBER)) {
22908cb1383cSDoug Ambrisko 		if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG)
22918cb1383cSDoug Ambrisko 		    & BGE_HWCFG_ASF) {
22928cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_ENABLE;
22938cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_STACKUP;
22948cb1383cSDoug Ambrisko 			if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
22958cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
22968cb1383cSDoug Ambrisko 			}
22978cb1383cSDoug Ambrisko 		}
22988cb1383cSDoug Ambrisko 	}
22998cb1383cSDoug Ambrisko 
23008cb1383cSDoug Ambrisko 	/* Try to reset the chip again the nice way. */
23018cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
23028cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
23038cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
23048cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
23058cb1383cSDoug Ambrisko 		bge_release_resources(sc);
23068cb1383cSDoug Ambrisko 		error = ENXIO;
23078cb1383cSDoug Ambrisko 		goto fail;
23088cb1383cSDoug Ambrisko 	}
23098cb1383cSDoug Ambrisko 
23108cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
23118cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
231295d67482SBill Paul 
231395d67482SBill Paul 	if (bge_chipinit(sc)) {
2314fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
231595d67482SBill Paul 		bge_release_resources(sc);
231695d67482SBill Paul 		error = ENXIO;
231795d67482SBill Paul 		goto fail;
231895d67482SBill Paul 	}
231995d67482SBill Paul 
232095d67482SBill Paul 	/*
232195d67482SBill Paul 	 * Get station address from the EEPROM.
232295d67482SBill Paul 	 */
2323fc74a9f9SBrooks Davis 	mac_tmp = bge_readmem_ind(sc, 0x0c14);
2324fc74a9f9SBrooks Davis 	if ((mac_tmp >> 16) == 0x484b) {
2325fc74a9f9SBrooks Davis 		eaddr[0] = (u_char)(mac_tmp >> 8);
2326fc74a9f9SBrooks Davis 		eaddr[1] = (u_char)mac_tmp;
2327fc74a9f9SBrooks Davis 		mac_tmp = bge_readmem_ind(sc, 0x0c18);
2328fc74a9f9SBrooks Davis 		eaddr[2] = (u_char)(mac_tmp >> 24);
2329fc74a9f9SBrooks Davis 		eaddr[3] = (u_char)(mac_tmp >> 16);
2330fc74a9f9SBrooks Davis 		eaddr[4] = (u_char)(mac_tmp >> 8);
2331fc74a9f9SBrooks Davis 		eaddr[5] = (u_char)mac_tmp;
2332fc74a9f9SBrooks Davis 	} else if (bge_read_eeprom(sc, eaddr,
233395d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2334fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to read station address\n");
233595d67482SBill Paul 		bge_release_resources(sc);
233695d67482SBill Paul 		error = ENXIO;
233795d67482SBill Paul 		goto fail;
233895d67482SBill Paul 	}
233995d67482SBill Paul 
2340f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
23417ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
2342f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2343f41ac2beSBill Paul 	else
2344f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2345f41ac2beSBill Paul 
2346f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2347fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2348fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2349f41ac2beSBill Paul 		bge_release_resources(sc);
2350f41ac2beSBill Paul 		error = ENXIO;
2351f41ac2beSBill Paul 		goto fail;
2352f41ac2beSBill Paul 	}
2353f41ac2beSBill Paul 
235495d67482SBill Paul 	/* Set default tuneable values. */
235595d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
235695d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
235795d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
23586f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
23596f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
236095d67482SBill Paul 
236195d67482SBill Paul 	/* Set up ifnet structure */
2362fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2363fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2364fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2365fc74a9f9SBrooks Davis 		bge_release_resources(sc);
2366fc74a9f9SBrooks Davis 		error = ENXIO;
2367fc74a9f9SBrooks Davis 		goto fail;
2368fc74a9f9SBrooks Davis 	}
236995d67482SBill Paul 	ifp->if_softc = sc;
23709bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
237195d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
237295d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
237395d67482SBill Paul 	ifp->if_start = bge_start;
237495d67482SBill Paul 	ifp->if_init = bge_init;
237595d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
23764d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
23774d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
23784d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
237995d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2380d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
2381479b23b7SGleb Smirnoff 	    IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM;
238295d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
238375719184SGleb Smirnoff #ifdef DEVICE_POLLING
238475719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
238575719184SGleb Smirnoff #endif
238695d67482SBill Paul 
2387a1d52896SBill Paul 	/*
2388d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2389d375e524SGleb Smirnoff 	 * to hardware bugs.
2390d375e524SGleb Smirnoff 	 */
2391d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2392d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2393d375e524SGleb Smirnoff 		ifp->if_capenable &= IFCAP_HWCSUM;
2394d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2395d375e524SGleb Smirnoff 	}
2396d375e524SGleb Smirnoff 
2397d375e524SGleb Smirnoff 	/*
2398a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
239941abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
240041abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
240141abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
240241abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
240341abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
240441abcc1bSPaul Saab 	 * SK-9D41.
2405a1d52896SBill Paul 	 */
240641abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
240741abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
240841abcc1bSPaul Saab 	else {
2409f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2410f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2411fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2412f6789fbaSPyun YongHyeon 			bge_release_resources(sc);
2413f6789fbaSPyun YongHyeon 			error = ENXIO;
2414f6789fbaSPyun YongHyeon 			goto fail;
2415f6789fbaSPyun YongHyeon 		}
241641abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
241741abcc1bSPaul Saab 	}
241841abcc1bSPaul Saab 
241941abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2420652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
2421a1d52896SBill Paul 
242295d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
242395d67482SBill Paul 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
2424652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
242595d67482SBill Paul 
2426652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
242795d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
242895d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
242995d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
243095d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia,
243195d67482SBill Paul 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
243295d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
243395d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2434da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
243595d67482SBill Paul 	} else {
243695d67482SBill Paul 		/*
24378cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
24388cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
24398cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
24408cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
24418cb1383cSDoug Ambrisko 		 * the PHY.
244295d67482SBill Paul 		 */
24438cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
24448cb1383cSDoug Ambrisko again:
24458cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
24468cb1383cSDoug Ambrisko 
24478cb1383cSDoug Ambrisko 		trys = 0;
244895d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
244995d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
24508cb1383cSDoug Ambrisko 			if (trys++ < 4) {
24518cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
24528cb1383cSDoug Ambrisko 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR, BMCR_RESET);
24538cb1383cSDoug Ambrisko 				goto again;
24548cb1383cSDoug Ambrisko 			}
24558cb1383cSDoug Ambrisko 
2456fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
245795d67482SBill Paul 			bge_release_resources(sc);
245895d67482SBill Paul 			error = ENXIO;
245995d67482SBill Paul 			goto fail;
246095d67482SBill Paul 		}
24618cb1383cSDoug Ambrisko 
24628cb1383cSDoug Ambrisko 		/*
24638cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
24648cb1383cSDoug Ambrisko 		 */
24658cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
24668cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
246795d67482SBill Paul 	}
246895d67482SBill Paul 
246995d67482SBill Paul 	/*
2470e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2471e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2472e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2473e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2474e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2475e255b776SJohn Polstra 	 * payloads by copying the received packets.
2476e255b776SJohn Polstra 	 */
2477652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
2478652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
2479652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
2480e255b776SJohn Polstra 
2481e255b776SJohn Polstra 	/*
248295d67482SBill Paul 	 * Call MI attach routine.
248395d67482SBill Paul 	 */
2484fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
2485b74e67fbSGleb Smirnoff 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
24860f9bd73bSSam Leffler 
24870f9bd73bSSam Leffler 	/*
24880f9bd73bSSam Leffler 	 * Hookup IRQ last.
24890f9bd73bSSam Leffler 	 */
24900f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
24910f9bd73bSSam Leffler 	   bge_intr, sc, &sc->bge_intrhand);
24920f9bd73bSSam Leffler 
24930f9bd73bSSam Leffler 	if (error) {
2494fc74a9f9SBrooks Davis 		bge_detach(dev);
2495fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
24960f9bd73bSSam Leffler 	}
249795d67482SBill Paul 
24986f8718a3SScott Long 	bge_add_sysctls(sc);
24996f8718a3SScott Long 
250095d67482SBill Paul fail:
250195d67482SBill Paul 	return (error);
250295d67482SBill Paul }
250395d67482SBill Paul 
250495d67482SBill Paul static int
25053f74909aSGleb Smirnoff bge_detach(device_t dev)
250695d67482SBill Paul {
250795d67482SBill Paul 	struct bge_softc *sc;
250895d67482SBill Paul 	struct ifnet *ifp;
250995d67482SBill Paul 
251095d67482SBill Paul 	sc = device_get_softc(dev);
2511fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
251295d67482SBill Paul 
251375719184SGleb Smirnoff #ifdef DEVICE_POLLING
251475719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
251575719184SGleb Smirnoff 		ether_poll_deregister(ifp);
251675719184SGleb Smirnoff #endif
251775719184SGleb Smirnoff 
25180f9bd73bSSam Leffler 	BGE_LOCK(sc);
251995d67482SBill Paul 	bge_stop(sc);
252095d67482SBill Paul 	bge_reset(sc);
25210f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
25220f9bd73bSSam Leffler 
25235dda8085SOleg Bulyzhin 	callout_drain(&sc->bge_stat_ch);
25245dda8085SOleg Bulyzhin 
25250f9bd73bSSam Leffler 	ether_ifdetach(ifp);
252695d67482SBill Paul 
2527652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
252895d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
252995d67482SBill Paul 	} else {
253095d67482SBill Paul 		bus_generic_detach(dev);
253195d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
253295d67482SBill Paul 	}
253395d67482SBill Paul 
253495d67482SBill Paul 	bge_release_resources(sc);
253595d67482SBill Paul 
253695d67482SBill Paul 	return (0);
253795d67482SBill Paul }
253895d67482SBill Paul 
253995d67482SBill Paul static void
25403f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
254195d67482SBill Paul {
254295d67482SBill Paul 	device_t dev;
254395d67482SBill Paul 
254495d67482SBill Paul 	dev = sc->bge_dev;
254595d67482SBill Paul 
254695d67482SBill Paul 	if (sc->bge_intrhand != NULL)
254795d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
254895d67482SBill Paul 
254995d67482SBill Paul 	if (sc->bge_irq != NULL)
2550724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
2551724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
2552724bd939SJohn Polstra 
2553724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
2554724bd939SJohn Polstra 		pci_release_msi(dev);
255595d67482SBill Paul 
255695d67482SBill Paul 	if (sc->bge_res != NULL)
255795d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
255895d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
255995d67482SBill Paul 
2560ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2561ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2562ad61f896SRuslan Ermilov 
2563f41ac2beSBill Paul 	bge_dma_free(sc);
256495d67482SBill Paul 
25650f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
25660f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
256795d67482SBill Paul }
256895d67482SBill Paul 
25698cb1383cSDoug Ambrisko static int
25703f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
257195d67482SBill Paul {
257295d67482SBill Paul 	device_t dev;
25733f74909aSGleb Smirnoff 	uint32_t cachesize, command, pcistate, reset;
25746f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
257595d67482SBill Paul 	int i, val = 0;
257695d67482SBill Paul 
257795d67482SBill Paul 	dev = sc->bge_dev;
257895d67482SBill Paul 
25799ba784dbSScott Long 	if (BGE_IS_5705_PLUS(sc) && !BGE_IS_5714_FAMILY(sc)) {
25806f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
25816f8718a3SScott Long 			write_op = bge_writemem_direct;
25826f8718a3SScott Long 		else
25836f8718a3SScott Long 			write_op = bge_writemem_ind;
25849ba784dbSScott Long 	} else
25856f8718a3SScott Long 		write_op = bge_writereg_ind;
25866f8718a3SScott Long 
258795d67482SBill Paul 	/* Save some important PCI state. */
258895d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
258995d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
259095d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
259195d67482SBill Paul 
259295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
259395d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2594e907febfSPyun YongHyeon 	BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
259595d67482SBill Paul 
25966f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
25976f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
25986f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
25996f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
26006f8718a3SScott Long 		if (bootverbose)
26019ba784dbSScott Long 			device_printf(sc->bge_dev, "Disabling fastboot\n");
26026f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
26036f8718a3SScott Long 	}
26046f8718a3SScott Long 
26056f8718a3SScott Long 	/*
26066f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
26076f8718a3SScott Long 	 * When firmware finishes its initialization it will
26086f8718a3SScott Long 	 * write ~BGE_MAGIC_NUMBER to the same location.
26096f8718a3SScott Long 	 */
26106f8718a3SScott Long 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
26116f8718a3SScott Long 
2612e53d81eeSPaul Saab 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2613e53d81eeSPaul Saab 
2614e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2615652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2616e53d81eeSPaul Saab 		if (CSR_READ_4(sc, 0x7e2c) == 0x60)	/* PCIE 1.0 */
2617e53d81eeSPaul Saab 			CSR_WRITE_4(sc, 0x7e2c, 0x20);
2618e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2619e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
2620e53d81eeSPaul Saab 			CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2621e53d81eeSPaul Saab 			reset |= (1<<29);
2622e53d81eeSPaul Saab 		}
2623e53d81eeSPaul Saab 	}
2624e53d81eeSPaul Saab 
262521c9e407SDavid Christensen 	/*
26266f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
26276f8718a3SScott Long 	 * powered up in D0 uninitialized.
26286f8718a3SScott Long 	 */
26295345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
26306f8718a3SScott Long 		reset |= 0x04000000;
26316f8718a3SScott Long 
263295d67482SBill Paul 	/* Issue global reset */
26336f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
263495d67482SBill Paul 
263595d67482SBill Paul 	DELAY(1000);
263695d67482SBill Paul 
2637e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2638652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2639e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2640e53d81eeSPaul Saab 			uint32_t v;
2641e53d81eeSPaul Saab 
2642e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
2643e53d81eeSPaul Saab 			v = pci_read_config(dev, 0xc4, 4);
2644e53d81eeSPaul Saab 			pci_write_config(dev, 0xc4, v | (1<<15), 4);
2645e53d81eeSPaul Saab 		}
26469ba784dbSScott Long 		/*
26479ba784dbSScott Long 		 * Set PCIE max payload size to 128 bytes and clear error
26489ba784dbSScott Long 		 * status.
26499ba784dbSScott Long 		 */
2650e53d81eeSPaul Saab 		pci_write_config(dev, 0xd8, 0xf5000, 4);
2651e53d81eeSPaul Saab 	}
2652e53d81eeSPaul Saab 
26533f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
265495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
265595d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2656e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW, 4);
265795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
265895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
26596f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, (65 << 1));
266095d67482SBill Paul 
2661bf6ef57aSJohn Polstra 	/* Re-enable MSI, if neccesary, and enable the memory arbiter. */
26624c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
26634c0da0ffSGleb Smirnoff 		uint32_t val;
26644c0da0ffSGleb Smirnoff 
2665bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
2666bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
2667bf6ef57aSJohn Polstra 			val = pci_read_config(dev, BGE_PCI_MSI_CTL, 2);
2668bf6ef57aSJohn Polstra 			pci_write_config(dev, BGE_PCI_MSI_CTL,
2669bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
2670bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
2671bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
2672bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
2673bf6ef57aSJohn Polstra 		}
26744c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
26754c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
26764c0da0ffSGleb Smirnoff 	} else
2677a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2678a7b0c314SPaul Saab 
267995d67482SBill Paul 	/*
26806f8718a3SScott Long 	 * Poll until we see the 1's complement of the magic number.
268195d67482SBill Paul 	 * This indicates that the firmware initialization
268295d67482SBill Paul 	 * is complete.
268395d67482SBill Paul 	 */
268495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
268595d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
268695d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
268795d67482SBill Paul 			break;
268895d67482SBill Paul 		DELAY(10);
268995d67482SBill Paul 	}
269095d67482SBill Paul 
269195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
26929ba784dbSScott Long 		device_printf(sc->bge_dev, "firmware handshake timed out, "
26939ba784dbSScott Long 		    "found 0x%08x\n", val);
269495d67482SBill Paul 	}
269595d67482SBill Paul 
269695d67482SBill Paul 	/*
269795d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
269895d67482SBill Paul 	 * return to its original pre-reset state. This is a
269995d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
270095d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
270195d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
270295d67482SBill Paul 	 * results.
270395d67482SBill Paul 	 */
270495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
270595d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
270695d67482SBill Paul 			break;
270795d67482SBill Paul 		DELAY(10);
270895d67482SBill Paul 	}
270995d67482SBill Paul 
27106f8718a3SScott Long 	if (sc->bge_flags & BGE_FLAG_PCIE) {
27116f8718a3SScott Long 		reset = bge_readmem_ind(sc, 0x7c00);
27126f8718a3SScott Long 		bge_writemem_ind(sc, 0x7c00, reset | (1 << 25));
27136f8718a3SScott Long 	}
27146f8718a3SScott Long 
27153f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
2716e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
271795d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
271895d67482SBill Paul 
27198cb1383cSDoug Ambrisko 	/* Tell the ASF firmware we are up */
27208cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
27218cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
27228cb1383cSDoug Ambrisko 
272395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
272495d67482SBill Paul 
2725da3003f0SBill Paul 	/*
2726da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
2727da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
2728da3003f0SBill Paul 	 * to 1.2V.
2729da3003f0SBill Paul 	 */
2730652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
2731652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
2732da3003f0SBill Paul 		uint32_t serdescfg;
2733652ae483SGleb Smirnoff 
2734da3003f0SBill Paul 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2735da3003f0SBill Paul 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
2736da3003f0SBill Paul 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2737da3003f0SBill Paul 	}
2738da3003f0SBill Paul 
2739e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2740652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
2741652ae483SGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2742e53d81eeSPaul Saab 		uint32_t v;
2743e53d81eeSPaul Saab 
2744e53d81eeSPaul Saab 		v = CSR_READ_4(sc, 0x7c00);
2745e53d81eeSPaul Saab 		CSR_WRITE_4(sc, 0x7c00, v | (1<<25));
2746e53d81eeSPaul Saab 	}
274795d67482SBill Paul 	DELAY(10000);
27488cb1383cSDoug Ambrisko 
27498cb1383cSDoug Ambrisko 	return(0);
275095d67482SBill Paul }
275195d67482SBill Paul 
275295d67482SBill Paul /*
275395d67482SBill Paul  * Frame reception handling. This is called if there's a frame
275495d67482SBill Paul  * on the receive return list.
275595d67482SBill Paul  *
275695d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
27571be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
275895d67482SBill Paul  * 2) the frame is from the standard receive ring
275995d67482SBill Paul  */
276095d67482SBill Paul 
276195d67482SBill Paul static void
27623f74909aSGleb Smirnoff bge_rxeof(struct bge_softc *sc)
276395d67482SBill Paul {
276495d67482SBill Paul 	struct ifnet *ifp;
276595d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
276695d67482SBill Paul 
27670f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
27680f9bd73bSSam Leffler 
27693f74909aSGleb Smirnoff 	/* Nothing to do. */
2770cfcb5025SOleg Bulyzhin 	if (sc->bge_rx_saved_considx ==
2771cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx)
2772cfcb5025SOleg Bulyzhin 		return;
2773cfcb5025SOleg Bulyzhin 
2774fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
277595d67482SBill Paul 
2776f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2777e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
2778f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2779f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
27804c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
2781f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
27824c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTREAD);
2783f41ac2beSBill Paul 
278495d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2785f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
278695d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
27873f74909aSGleb Smirnoff 		uint32_t		rxidx;
278895d67482SBill Paul 		struct mbuf		*m = NULL;
27893f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
279095d67482SBill Paul 		int			have_tag = 0;
279195d67482SBill Paul 
279275719184SGleb Smirnoff #ifdef DEVICE_POLLING
279375719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
279475719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
279575719184SGleb Smirnoff 				break;
279675719184SGleb Smirnoff 			sc->rxcycles--;
279775719184SGleb Smirnoff 		}
279875719184SGleb Smirnoff #endif
279975719184SGleb Smirnoff 
280095d67482SBill Paul 		cur_rx =
2801f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
280295d67482SBill Paul 
280395d67482SBill Paul 		rxidx = cur_rx->bge_idx;
28040434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
280595d67482SBill Paul 
280645ee6ab3SJung-uk Kim 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
280795d67482SBill Paul 			have_tag = 1;
280895d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
280995d67482SBill Paul 		}
281095d67482SBill Paul 
281195d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
281295d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2813f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2814f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2815f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2816f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2817f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
281895d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
281995d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
282095d67482SBill Paul 			jumbocnt++;
282195d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
282295d67482SBill Paul 				ifp->if_ierrors++;
282395d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
282495d67482SBill Paul 				continue;
282595d67482SBill Paul 			}
282695d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
282795d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
282895d67482SBill Paul 				ifp->if_ierrors++;
282995d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
283095d67482SBill Paul 				continue;
283195d67482SBill Paul 			}
283295d67482SBill Paul 		} else {
283395d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2834f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2835f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2836f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2837f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2838f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
283995d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
284095d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
284195d67482SBill Paul 			stdcnt++;
284295d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
284395d67482SBill Paul 				ifp->if_ierrors++;
284495d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
284595d67482SBill Paul 				continue;
284695d67482SBill Paul 			}
284795d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
284895d67482SBill Paul 			    NULL) == ENOBUFS) {
284995d67482SBill Paul 				ifp->if_ierrors++;
285095d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
285195d67482SBill Paul 				continue;
285295d67482SBill Paul 			}
285395d67482SBill Paul 		}
285495d67482SBill Paul 
285595d67482SBill Paul 		ifp->if_ipackets++;
2856e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2857e255b776SJohn Polstra 		/*
2858e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
2859e65bed95SPyun YongHyeon 		 * the payload is aligned.
2860e255b776SJohn Polstra 		 */
2861652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
2862e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2863e255b776SJohn Polstra 			    cur_rx->bge_len);
2864e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2865e255b776SJohn Polstra 		}
2866e255b776SJohn Polstra #endif
2867473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
286895d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
286995d67482SBill Paul 
2870b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
287178178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
287295d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
287395d67482SBill Paul 				if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
287495d67482SBill Paul 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
287578178cd1SGleb Smirnoff 			}
2876d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
2877d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
287895d67482SBill Paul 				m->m_pkthdr.csum_data =
287995d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
2880ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
2881ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
288295d67482SBill Paul 			}
288395d67482SBill Paul 		}
288495d67482SBill Paul 
288595d67482SBill Paul 		/*
2886673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
2887673d9191SSam Leffler 		 * attach that information to the packet.
288895d67482SBill Paul 		 */
2889d147662cSGleb Smirnoff 		if (have_tag) {
289078ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
289178ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
2892d147662cSGleb Smirnoff 		}
289395d67482SBill Paul 
28940f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
2895673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
28960f9bd73bSSam Leffler 		BGE_LOCK(sc);
289795d67482SBill Paul 	}
289895d67482SBill Paul 
2899e65bed95SPyun YongHyeon 	if (stdcnt > 0)
2900f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2901e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
29024c0da0ffSGleb Smirnoff 
29034c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc) && jumbocnt > 0)
2904f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
29054c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
2906f41ac2beSBill Paul 
290795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
290895d67482SBill Paul 	if (stdcnt)
290995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
291095d67482SBill Paul 	if (jumbocnt)
291195d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
29126b037352SJung-uk Kim #ifdef notyet
29136b037352SJung-uk Kim 	/*
29146b037352SJung-uk Kim 	 * This register wraps very quickly under heavy packet drops.
29156b037352SJung-uk Kim 	 * If you need correct statistics, you can enable this check.
29166b037352SJung-uk Kim 	 */
29176b037352SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
29186b037352SJung-uk Kim 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
29196b037352SJung-uk Kim #endif
292095d67482SBill Paul }
292195d67482SBill Paul 
292295d67482SBill Paul static void
29233f74909aSGleb Smirnoff bge_txeof(struct bge_softc *sc)
292495d67482SBill Paul {
292595d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
292695d67482SBill Paul 	struct ifnet *ifp;
292795d67482SBill Paul 
29280f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
29290f9bd73bSSam Leffler 
29303f74909aSGleb Smirnoff 	/* Nothing to do. */
2931cfcb5025SOleg Bulyzhin 	if (sc->bge_tx_saved_considx ==
2932cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx)
2933cfcb5025SOleg Bulyzhin 		return;
2934cfcb5025SOleg Bulyzhin 
2935fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
293695d67482SBill Paul 
2937e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
2938e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
2939e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
294095d67482SBill Paul 	/*
294195d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
294295d67482SBill Paul 	 * frames that have been sent.
294395d67482SBill Paul 	 */
294495d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
2945f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
29463f74909aSGleb Smirnoff 		uint32_t		idx = 0;
294795d67482SBill Paul 
294895d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
2949f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
295095d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
295195d67482SBill Paul 			ifp->if_opackets++;
295295d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
2953e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2954e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
2955e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
2956f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2957f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
2958e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
2959e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
296095d67482SBill Paul 		}
296195d67482SBill Paul 		sc->bge_txcnt--;
296295d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
296395d67482SBill Paul 	}
296495d67482SBill Paul 
296595d67482SBill Paul 	if (cur_tx != NULL)
296613f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
29675b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
29685b01e77cSBruce Evans 		sc->bge_timer = 0;
296995d67482SBill Paul }
297095d67482SBill Paul 
297175719184SGleb Smirnoff #ifdef DEVICE_POLLING
297275719184SGleb Smirnoff static void
297375719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
297475719184SGleb Smirnoff {
297575719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
2976366454f2SOleg Bulyzhin 	uint32_t statusword;
297775719184SGleb Smirnoff 
29783f74909aSGleb Smirnoff 	BGE_LOCK(sc);
29793f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
29803f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
29813f74909aSGleb Smirnoff 		return;
29823f74909aSGleb Smirnoff 	}
298375719184SGleb Smirnoff 
2984dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2985e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
2986dab5cd05SOleg Bulyzhin 
29873f74909aSGleb Smirnoff 	statusword = atomic_readandclear_32(
29883f74909aSGleb Smirnoff 	    &sc->bge_ldata.bge_status_block->bge_status);
2989dab5cd05SOleg Bulyzhin 
2990dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2991e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
2992366454f2SOleg Bulyzhin 
2993366454f2SOleg Bulyzhin 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS cmd */
2994366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
2995366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
2996366454f2SOleg Bulyzhin 
2997366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
2998366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
29994c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3000652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
3001366454f2SOleg Bulyzhin 			bge_link_upd(sc);
3002366454f2SOleg Bulyzhin 
3003366454f2SOleg Bulyzhin 	sc->rxcycles = count;
3004366454f2SOleg Bulyzhin 	bge_rxeof(sc);
3005366454f2SOleg Bulyzhin 	bge_txeof(sc);
3006366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3007366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
30083f74909aSGleb Smirnoff 
30093f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
301075719184SGleb Smirnoff }
301175719184SGleb Smirnoff #endif /* DEVICE_POLLING */
301275719184SGleb Smirnoff 
301395d67482SBill Paul static void
30143f74909aSGleb Smirnoff bge_intr(void *xsc)
301595d67482SBill Paul {
301695d67482SBill Paul 	struct bge_softc *sc;
301795d67482SBill Paul 	struct ifnet *ifp;
3018dab5cd05SOleg Bulyzhin 	uint32_t statusword;
301995d67482SBill Paul 
302095d67482SBill Paul 	sc = xsc;
3021f41ac2beSBill Paul 
30220f9bd73bSSam Leffler 	BGE_LOCK(sc);
30230f9bd73bSSam Leffler 
3024dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
3025dab5cd05SOleg Bulyzhin 
302675719184SGleb Smirnoff #ifdef DEVICE_POLLING
302775719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
302875719184SGleb Smirnoff 		BGE_UNLOCK(sc);
302975719184SGleb Smirnoff 		return;
303075719184SGleb Smirnoff 	}
303175719184SGleb Smirnoff #endif
303275719184SGleb Smirnoff 
3033f30cbfc6SScott Long 	/*
3034b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
3035b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
3036b848e032SBruce Evans 	 * our current organization this just gives complications and
3037b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
3038b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
3039b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
3040b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
3041b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
3042b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
3043b848e032SBruce Evans 	 *
3044b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
3045b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
3046b848e032SBruce Evans 	 * changing later because it is more efficient to get another
3047b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
3048b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
3049b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
3050b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
3051b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
3052b848e032SBruce Evans 	 */
3053b848e032SBruce Evans 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
3054b848e032SBruce Evans 
3055b848e032SBruce Evans 	/*
3056f30cbfc6SScott Long 	 * Do the mandatory PCI flush as well as get the link status.
3057f30cbfc6SScott Long 	 */
3058f30cbfc6SScott Long 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
3059f41ac2beSBill Paul 
3060f30cbfc6SScott Long 	/* Make sure the descriptor ring indexes are coherent. */
3061f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3062f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
3063f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3064f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
3065f30cbfc6SScott Long 
30661f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
30674c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3068f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
3069dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
307095d67482SBill Paul 
307113f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
30723f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
307395d67482SBill Paul 		bge_rxeof(sc);
307495d67482SBill Paul 
30753f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
307695d67482SBill Paul 		bge_txeof(sc);
307795d67482SBill Paul 	}
307895d67482SBill Paul 
307913f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
308013f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
30810f9bd73bSSam Leffler 		bge_start_locked(ifp);
30820f9bd73bSSam Leffler 
30830f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
308495d67482SBill Paul }
308595d67482SBill Paul 
308695d67482SBill Paul static void
30878cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
30888cb1383cSDoug Ambrisko {
30898cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
30908cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
30918cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
30928cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
30938cb1383cSDoug Ambrisko 		else {
30948cb1383cSDoug Ambrisko 			sc->bge_asf_count = 5;
30958cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW,
30968cb1383cSDoug Ambrisko 			    BGE_FW_DRV_ALIVE);
30978cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_LEN, 4);
30988cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_DATA, 3);
30998cb1383cSDoug Ambrisko 			CSR_WRITE_4(sc, BGE_CPU_EVENT,
31008cb1383cSDoug Ambrisko 			    CSR_READ_4(sc, BGE_CPU_EVENT) != (1 << 14));
31018cb1383cSDoug Ambrisko 		}
31028cb1383cSDoug Ambrisko 	}
31038cb1383cSDoug Ambrisko }
31048cb1383cSDoug Ambrisko 
31058cb1383cSDoug Ambrisko static void
3106b74e67fbSGleb Smirnoff bge_tick(void *xsc)
31070f9bd73bSSam Leffler {
3108b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
310995d67482SBill Paul 	struct mii_data *mii = NULL;
311095d67482SBill Paul 
31110f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
311295d67482SBill Paul 
31135dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
31145dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
31155dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
31165dda8085SOleg Bulyzhin 	    	return;
31175dda8085SOleg Bulyzhin 
31187ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
31190434d1b8SBill Paul 		bge_stats_update_regs(sc);
31200434d1b8SBill Paul 	else
312195d67482SBill Paul 		bge_stats_update(sc);
312295d67482SBill Paul 
3123652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
312495d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
31258cb1383cSDoug Ambrisko 		/* Don't mess with the PHY in IPMI/ASF mode */
31268cb1383cSDoug Ambrisko 		if (!((sc->bge_asf_mode & ASF_STACKUP) && (sc->bge_link)))
312795d67482SBill Paul 			mii_tick(mii);
31287b97099dSOleg Bulyzhin 	} else {
31297b97099dSOleg Bulyzhin 		/*
31307b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
31317b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
31327b97099dSOleg Bulyzhin 		 * and trigger interrupt.
31337b97099dSOleg Bulyzhin 		 */
31347b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
31353f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
31367b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
31377b97099dSOleg Bulyzhin #endif
31387b97099dSOleg Bulyzhin 		{
31397b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
31407b97099dSOleg Bulyzhin 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
31417b97099dSOleg Bulyzhin 		}
3142dab5cd05SOleg Bulyzhin 	}
314395d67482SBill Paul 
31448cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
3145b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
31468cb1383cSDoug Ambrisko 
3147dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
314895d67482SBill Paul }
314995d67482SBill Paul 
315095d67482SBill Paul static void
31513f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
31520434d1b8SBill Paul {
31533f74909aSGleb Smirnoff 	struct ifnet *ifp;
31540434d1b8SBill Paul 
3155fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
31560434d1b8SBill Paul 
31576b037352SJung-uk Kim 	ifp->if_collisions += CSR_READ_4(sc, BGE_MAC_STATS +
31587e6e2507SJung-uk Kim 	    offsetof(struct bge_mac_stats_regs, etherStatsCollisions));
31597e6e2507SJung-uk Kim 
31606b037352SJung-uk Kim 	ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
31610434d1b8SBill Paul }
31620434d1b8SBill Paul 
31630434d1b8SBill Paul static void
31643f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
316595d67482SBill Paul {
316695d67482SBill Paul 	struct ifnet *ifp;
3167e907febfSPyun YongHyeon 	bus_size_t stats;
31687e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
316995d67482SBill Paul 
3170fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
317195d67482SBill Paul 
3172e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
3173e907febfSPyun YongHyeon 
3174e907febfSPyun YongHyeon #define READ_STAT(sc, stats, stat) \
3175e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
317695d67482SBill Paul 
31778634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
31786b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
31796fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
31806fb34dd2SOleg Bulyzhin 
31816fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
31826b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
31836fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
31846fb34dd2SOleg Bulyzhin 
31856fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
31866b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
31876fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
318895d67482SBill Paul 
3189e907febfSPyun YongHyeon #undef READ_STAT
319095d67482SBill Paul }
319195d67482SBill Paul 
319295d67482SBill Paul /*
3193d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
3194d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
3195d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
3196d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
3197d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
3198d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
3199d375e524SGleb Smirnoff  */
3200d375e524SGleb Smirnoff static __inline int
3201d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
3202d375e524SGleb Smirnoff {
3203d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
3204d375e524SGleb Smirnoff 	struct mbuf *last;
3205d375e524SGleb Smirnoff 
3206d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
3207d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
3208d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
3209d375e524SGleb Smirnoff 		last = m;
3210d375e524SGleb Smirnoff 	} else {
3211d375e524SGleb Smirnoff 		/*
3212d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
3213d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
3214d375e524SGleb Smirnoff 		 */
3215d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
3216d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
3217d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
3218d375e524SGleb Smirnoff 			struct mbuf *n;
3219d375e524SGleb Smirnoff 
3220d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
3221d375e524SGleb Smirnoff 			if (n == NULL)
3222d375e524SGleb Smirnoff 				return (ENOBUFS);
3223d375e524SGleb Smirnoff 			n->m_len = 0;
3224d375e524SGleb Smirnoff 			last->m_next = n;
3225d375e524SGleb Smirnoff 			last = n;
3226d375e524SGleb Smirnoff 		}
3227d375e524SGleb Smirnoff 	}
3228d375e524SGleb Smirnoff 
3229d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
3230d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
3231d375e524SGleb Smirnoff 	last->m_len += padlen;
3232d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
3233d375e524SGleb Smirnoff 
3234d375e524SGleb Smirnoff 	return (0);
3235d375e524SGleb Smirnoff }
3236d375e524SGleb Smirnoff 
3237d375e524SGleb Smirnoff /*
323895d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
323995d67482SBill Paul  * pointers to descriptors.
324095d67482SBill Paul  */
324195d67482SBill Paul static int
3242676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
324395d67482SBill Paul {
32447e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
3245f41ac2beSBill Paul 	bus_dmamap_t		map;
3246676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
3247676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
32487e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
3249676ad2c9SGleb Smirnoff 	uint16_t		csum_flags;
32507e27542aSGleb Smirnoff 	int			nsegs, i, error;
325195d67482SBill Paul 
32526909dc43SGleb Smirnoff 	csum_flags = 0;
32536909dc43SGleb Smirnoff 	if (m->m_pkthdr.csum_flags) {
32546909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
32556909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
32566909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
32576909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
32586909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
32596909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
32606909dc43SGleb Smirnoff 				m_freem(m);
32616909dc43SGleb Smirnoff 				*m_head = NULL;
32626909dc43SGleb Smirnoff 				return (error);
32636909dc43SGleb Smirnoff 			}
32646909dc43SGleb Smirnoff 		}
32656909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
32666909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
32676909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
32686909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
32696909dc43SGleb Smirnoff 	}
32706909dc43SGleb Smirnoff 
32717e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
3272676ad2c9SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m, segs,
3273676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
32747e27542aSGleb Smirnoff 	if (error == EFBIG) {
3275676ad2c9SGleb Smirnoff 		m = m_defrag(m, M_DONTWAIT);
3276676ad2c9SGleb Smirnoff 		if (m == NULL) {
3277676ad2c9SGleb Smirnoff 			m_freem(*m_head);
3278676ad2c9SGleb Smirnoff 			*m_head = NULL;
32797e27542aSGleb Smirnoff 			return (ENOBUFS);
32807e27542aSGleb Smirnoff 		}
3281676ad2c9SGleb Smirnoff 		*m_head = m;
3282676ad2c9SGleb Smirnoff 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m,
3283676ad2c9SGleb Smirnoff 		    segs, &nsegs, BUS_DMA_NOWAIT);
3284676ad2c9SGleb Smirnoff 		if (error) {
3285676ad2c9SGleb Smirnoff 			m_freem(m);
3286676ad2c9SGleb Smirnoff 			*m_head = NULL;
32877e27542aSGleb Smirnoff 			return (error);
32887e27542aSGleb Smirnoff 		}
3289676ad2c9SGleb Smirnoff 	} else if (error != 0)
3290676ad2c9SGleb Smirnoff 		return (error);
32917e27542aSGleb Smirnoff 
329295d67482SBill Paul 	/*
329395d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
329495d67482SBill Paul 	 * of the end of the ring.
329595d67482SBill Paul 	 */
32967e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
32977e27542aSGleb Smirnoff 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
329895d67482SBill Paul 		return (ENOBUFS);
32997e27542aSGleb Smirnoff 	}
33007e27542aSGleb Smirnoff 
3301e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
3302e65bed95SPyun YongHyeon 
33037e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
33047e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
33057e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
33067e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
33077e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
33087e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
33097e27542aSGleb Smirnoff 		if (i == nsegs - 1)
33107e27542aSGleb Smirnoff 			break;
33117e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
33127e27542aSGleb Smirnoff 	}
33137e27542aSGleb Smirnoff 
33147e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
33157e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
3316676ad2c9SGleb Smirnoff 
33177e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
33187e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
331978ba57b9SAndre Oppermann 	if (m->m_flags & M_VLANTAG) {
33207e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
332178ba57b9SAndre Oppermann 		d->bge_vlan_tag = m->m_pkthdr.ether_vtag;
33227e27542aSGleb Smirnoff 	} else
33237e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
3324f41ac2beSBill Paul 
3325f41ac2beSBill Paul 	/*
3326f41ac2beSBill Paul 	 * Insure that the map for this transmission
3327f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
3328f41ac2beSBill Paul 	 * in this chain.
3329f41ac2beSBill Paul 	 */
33307e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
33317e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
3332676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
33337e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
333495d67482SBill Paul 
33357e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
33367e27542aSGleb Smirnoff 	*txidx = idx;
333795d67482SBill Paul 
333895d67482SBill Paul 	return (0);
333995d67482SBill Paul }
334095d67482SBill Paul 
334195d67482SBill Paul /*
334295d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
334395d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
334495d67482SBill Paul  */
334595d67482SBill Paul static void
33463f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
334795d67482SBill Paul {
334895d67482SBill Paul 	struct bge_softc *sc;
334995d67482SBill Paul 	struct mbuf *m_head = NULL;
335014bbd30fSGleb Smirnoff 	uint32_t prodidx;
3351303a718cSDag-Erling Smørgrav 	int count = 0;
335295d67482SBill Paul 
335395d67482SBill Paul 	sc = ifp->if_softc;
335495d67482SBill Paul 
3355dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
335695d67482SBill Paul 		return;
335795d67482SBill Paul 
335814bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
335995d67482SBill Paul 
336095d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
33614d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
336295d67482SBill Paul 		if (m_head == NULL)
336395d67482SBill Paul 			break;
336495d67482SBill Paul 
336595d67482SBill Paul 		/*
336695d67482SBill Paul 		 * XXX
3367b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
3368b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3369b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
3370b874fdd4SYaroslav Tykhiy 		 *
3371b874fdd4SYaroslav Tykhiy 		 * XXX
337295d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
337395d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
337495d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
337595d67482SBill Paul 		 * chain at once.
337695d67482SBill Paul 		 * (paranoia -- may not actually be needed)
337795d67482SBill Paul 		 */
337895d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
337995d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
338095d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
338195d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
33824d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
338313f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
338495d67482SBill Paul 				break;
338595d67482SBill Paul 			}
338695d67482SBill Paul 		}
338795d67482SBill Paul 
338895d67482SBill Paul 		/*
338995d67482SBill Paul 		 * Pack the data into the transmit ring. If we
339095d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
339195d67482SBill Paul 		 * for the NIC to drain the ring.
339295d67482SBill Paul 		 */
3393676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
3394676ad2c9SGleb Smirnoff 			if (m_head == NULL)
3395676ad2c9SGleb Smirnoff 				break;
33964d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
339713f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
339895d67482SBill Paul 			break;
339995d67482SBill Paul 		}
3400303a718cSDag-Erling Smørgrav 		++count;
340195d67482SBill Paul 
340295d67482SBill Paul 		/*
340395d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
340495d67482SBill Paul 		 * to him.
340595d67482SBill Paul 		 */
340645ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
340795d67482SBill Paul 	}
340895d67482SBill Paul 
34093f74909aSGleb Smirnoff 	if (count == 0)
34103f74909aSGleb Smirnoff 		/* No packets were dequeued. */
3411303a718cSDag-Erling Smørgrav 		return;
3412303a718cSDag-Erling Smørgrav 
34133f74909aSGleb Smirnoff 	/* Transmit. */
341495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
34153927098fSPaul Saab 	/* 5700 b2 errata */
3416e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
34173927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
341895d67482SBill Paul 
341914bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
342014bbd30fSGleb Smirnoff 
342195d67482SBill Paul 	/*
342295d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
342395d67482SBill Paul 	 */
3424b74e67fbSGleb Smirnoff 	sc->bge_timer = 5;
342595d67482SBill Paul }
342695d67482SBill Paul 
34270f9bd73bSSam Leffler /*
34280f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
34290f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
34300f9bd73bSSam Leffler  */
343195d67482SBill Paul static void
34323f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
343395d67482SBill Paul {
34340f9bd73bSSam Leffler 	struct bge_softc *sc;
34350f9bd73bSSam Leffler 
34360f9bd73bSSam Leffler 	sc = ifp->if_softc;
34370f9bd73bSSam Leffler 	BGE_LOCK(sc);
34380f9bd73bSSam Leffler 	bge_start_locked(ifp);
34390f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
34400f9bd73bSSam Leffler }
34410f9bd73bSSam Leffler 
34420f9bd73bSSam Leffler static void
34433f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
34440f9bd73bSSam Leffler {
344595d67482SBill Paul 	struct ifnet *ifp;
34463f74909aSGleb Smirnoff 	uint16_t *m;
344795d67482SBill Paul 
34480f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
344995d67482SBill Paul 
3450fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
345195d67482SBill Paul 
345213f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
345395d67482SBill Paul 		return;
345495d67482SBill Paul 
345595d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
345695d67482SBill Paul 	bge_stop(sc);
34578cb1383cSDoug Ambrisko 
34588cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
34598cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
346095d67482SBill Paul 	bge_reset(sc);
34618cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
34628cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
34638cb1383cSDoug Ambrisko 
346495d67482SBill Paul 	bge_chipinit(sc);
346595d67482SBill Paul 
346695d67482SBill Paul 	/*
346795d67482SBill Paul 	 * Init the various state machines, ring
346895d67482SBill Paul 	 * control blocks and firmware.
346995d67482SBill Paul 	 */
347095d67482SBill Paul 	if (bge_blockinit(sc)) {
3471fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
347295d67482SBill Paul 		return;
347395d67482SBill Paul 	}
347495d67482SBill Paul 
3475fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
347695d67482SBill Paul 
347795d67482SBill Paul 	/* Specify MTU. */
347895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3479859c6c7dSBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
348095d67482SBill Paul 
348195d67482SBill Paul 	/* Load our MAC address. */
34823f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
348395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
348495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
348595d67482SBill Paul 
34863e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
34873e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
348895d67482SBill Paul 
348995d67482SBill Paul 	/* Program multicast filter. */
349095d67482SBill Paul 	bge_setmulti(sc);
349195d67482SBill Paul 
349295d67482SBill Paul 	/* Init RX ring. */
349395d67482SBill Paul 	bge_init_rx_ring_std(sc);
349495d67482SBill Paul 
34950434d1b8SBill Paul 	/*
34960434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
34970434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
34980434d1b8SBill Paul 	 * entry of the ring.
34990434d1b8SBill Paul 	 */
35000434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
35013f74909aSGleb Smirnoff 		uint32_t		v, i;
35020434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
35030434d1b8SBill Paul 			DELAY(20);
35040434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
35050434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
35060434d1b8SBill Paul 				break;
35070434d1b8SBill Paul 		}
35080434d1b8SBill Paul 		if (i == 10)
3509fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
3510fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
35110434d1b8SBill Paul 	}
35120434d1b8SBill Paul 
351395d67482SBill Paul 	/* Init jumbo RX ring. */
351495d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
351595d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
351695d67482SBill Paul 
35173f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
351895d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
351995d67482SBill Paul 
35207e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
35217e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
35227e6e2507SJung-uk Kim 
352395d67482SBill Paul 	/* Init TX ring. */
352495d67482SBill Paul 	bge_init_tx_ring(sc);
352595d67482SBill Paul 
35263f74909aSGleb Smirnoff 	/* Turn on transmitter. */
352795d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
352895d67482SBill Paul 
35293f74909aSGleb Smirnoff 	/* Turn on receiver. */
353095d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
353195d67482SBill Paul 
353295d67482SBill Paul 	/* Tell firmware we're alive. */
353395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
353495d67482SBill Paul 
353575719184SGleb Smirnoff #ifdef DEVICE_POLLING
353675719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
353775719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
353875719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
353975719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
354075719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
354175719184SGleb Smirnoff 	} else
354275719184SGleb Smirnoff #endif
354375719184SGleb Smirnoff 
354495d67482SBill Paul 	/* Enable host interrupts. */
354575719184SGleb Smirnoff 	{
354695d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
354795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
354895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
354975719184SGleb Smirnoff 	}
355095d67482SBill Paul 
355167d5e043SOleg Bulyzhin 	bge_ifmedia_upd_locked(ifp);
355295d67482SBill Paul 
355313f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
355413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
355595d67482SBill Paul 
35560f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
35570f9bd73bSSam Leffler }
35580f9bd73bSSam Leffler 
35590f9bd73bSSam Leffler static void
35603f74909aSGleb Smirnoff bge_init(void *xsc)
35610f9bd73bSSam Leffler {
35620f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
35630f9bd73bSSam Leffler 
35640f9bd73bSSam Leffler 	BGE_LOCK(sc);
35650f9bd73bSSam Leffler 	bge_init_locked(sc);
35660f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
356795d67482SBill Paul }
356895d67482SBill Paul 
356995d67482SBill Paul /*
357095d67482SBill Paul  * Set media options.
357195d67482SBill Paul  */
357295d67482SBill Paul static int
35733f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
357495d67482SBill Paul {
357567d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
357667d5e043SOleg Bulyzhin 	int res;
357767d5e043SOleg Bulyzhin 
357867d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
357967d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
358067d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
358167d5e043SOleg Bulyzhin 
358267d5e043SOleg Bulyzhin 	return (res);
358367d5e043SOleg Bulyzhin }
358467d5e043SOleg Bulyzhin 
358567d5e043SOleg Bulyzhin static int
358667d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
358767d5e043SOleg Bulyzhin {
358867d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
358995d67482SBill Paul 	struct mii_data *mii;
359095d67482SBill Paul 	struct ifmedia *ifm;
359195d67482SBill Paul 
359267d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
359367d5e043SOleg Bulyzhin 
359495d67482SBill Paul 	ifm = &sc->bge_ifmedia;
359595d67482SBill Paul 
359695d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
3597652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
359895d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
359995d67482SBill Paul 			return (EINVAL);
360095d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
360195d67482SBill Paul 		case IFM_AUTO:
3602ff50922bSDoug White 			/*
3603ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
3604ff50922bSDoug White 			 * mechanism for programming the autoneg
3605ff50922bSDoug White 			 * advertisement registers in TBI mode.
3606ff50922bSDoug White 			 */
3607c4529f41SMichael Reifenberger 			if (bge_fake_autoneg == 0 &&
3608c4529f41SMichael Reifenberger 			    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3609ff50922bSDoug White 				uint32_t sgdig;
3610ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3611ff50922bSDoug White 				sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3612ff50922bSDoug White 				sgdig |= BGE_SGDIGCFG_AUTO|
3613ff50922bSDoug White 				    BGE_SGDIGCFG_PAUSE_CAP|
3614ff50922bSDoug White 				    BGE_SGDIGCFG_ASYM_PAUSE;
3615ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3616ff50922bSDoug White 				    sgdig|BGE_SGDIGCFG_SEND);
3617ff50922bSDoug White 				DELAY(5);
3618ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3619ff50922bSDoug White 			}
362095d67482SBill Paul 			break;
362195d67482SBill Paul 		case IFM_1000_SX:
362295d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
362395d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
362495d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
362595d67482SBill Paul 			} else {
362695d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
362795d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
362895d67482SBill Paul 			}
362995d67482SBill Paul 			break;
363095d67482SBill Paul 		default:
363195d67482SBill Paul 			return (EINVAL);
363295d67482SBill Paul 		}
363395d67482SBill Paul 		return (0);
363495d67482SBill Paul 	}
363595d67482SBill Paul 
36361493e883SOleg Bulyzhin 	sc->bge_link_evt++;
363795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
363895d67482SBill Paul 	if (mii->mii_instance) {
363995d67482SBill Paul 		struct mii_softc *miisc;
364095d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
364195d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
364295d67482SBill Paul 			mii_phy_reset(miisc);
364395d67482SBill Paul 	}
364495d67482SBill Paul 	mii_mediachg(mii);
364595d67482SBill Paul 
364695d67482SBill Paul 	return (0);
364795d67482SBill Paul }
364895d67482SBill Paul 
364995d67482SBill Paul /*
365095d67482SBill Paul  * Report current media status.
365195d67482SBill Paul  */
365295d67482SBill Paul static void
36533f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
365495d67482SBill Paul {
365567d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
365695d67482SBill Paul 	struct mii_data *mii;
365795d67482SBill Paul 
365867d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
365995d67482SBill Paul 
3660652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
366195d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
366295d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
366395d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
366495d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
366595d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
36664c0da0ffSGleb Smirnoff 		else {
36674c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
366867d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
36694c0da0ffSGleb Smirnoff 			return;
36704c0da0ffSGleb Smirnoff 		}
367195d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
367295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
367395d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
367495d67482SBill Paul 		else
367595d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
367667d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
367795d67482SBill Paul 		return;
367895d67482SBill Paul 	}
367995d67482SBill Paul 
368095d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
368195d67482SBill Paul 	mii_pollstat(mii);
368295d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
368395d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
368467d5e043SOleg Bulyzhin 
368567d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
368695d67482SBill Paul }
368795d67482SBill Paul 
368895d67482SBill Paul static int
36893f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
369095d67482SBill Paul {
369195d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
369295d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
369395d67482SBill Paul 	struct mii_data *mii;
3694f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
369595d67482SBill Paul 
369695d67482SBill Paul 	switch (command) {
369795d67482SBill Paul 	case SIOCSIFMTU:
36984c0da0ffSGleb Smirnoff 		if (ifr->ifr_mtu < ETHERMIN ||
36994c0da0ffSGleb Smirnoff 		    ((BGE_IS_JUMBO_CAPABLE(sc)) &&
37004c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > BGE_JUMBO_MTU) ||
37014c0da0ffSGleb Smirnoff 		    ((!BGE_IS_JUMBO_CAPABLE(sc)) &&
37024c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > ETHERMTU))
370395d67482SBill Paul 			error = EINVAL;
37044c0da0ffSGleb Smirnoff 		else if (ifp->if_mtu != ifr->ifr_mtu) {
370595d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
370613f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
370795d67482SBill Paul 			bge_init(sc);
370895d67482SBill Paul 		}
370995d67482SBill Paul 		break;
371095d67482SBill Paul 	case SIOCSIFFLAGS:
37110f9bd73bSSam Leffler 		BGE_LOCK(sc);
371295d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
371395d67482SBill Paul 			/*
371495d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
371595d67482SBill Paul 			 * then just use the 'set promisc mode' command
371695d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
371795d67482SBill Paul 			 * a full re-init means reloading the firmware and
371895d67482SBill Paul 			 * waiting for it to start up, which may take a
3719d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
372095d67482SBill Paul 			 */
3721f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3722f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
37233e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
37243e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
3725f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
3726d183af7fSRuslan Ermilov 					bge_setmulti(sc);
372795d67482SBill Paul 			} else
37280f9bd73bSSam Leffler 				bge_init_locked(sc);
372995d67482SBill Paul 		} else {
373013f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
373195d67482SBill Paul 				bge_stop(sc);
373295d67482SBill Paul 			}
373395d67482SBill Paul 		}
373495d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
37350f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
373695d67482SBill Paul 		error = 0;
373795d67482SBill Paul 		break;
373895d67482SBill Paul 	case SIOCADDMULTI:
373995d67482SBill Paul 	case SIOCDELMULTI:
374013f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
37410f9bd73bSSam Leffler 			BGE_LOCK(sc);
374295d67482SBill Paul 			bge_setmulti(sc);
37430f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
374495d67482SBill Paul 			error = 0;
374595d67482SBill Paul 		}
374695d67482SBill Paul 		break;
374795d67482SBill Paul 	case SIOCSIFMEDIA:
374895d67482SBill Paul 	case SIOCGIFMEDIA:
3749652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
375095d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
375195d67482SBill Paul 			    &sc->bge_ifmedia, command);
375295d67482SBill Paul 		} else {
375395d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
375495d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
375595d67482SBill Paul 			    &mii->mii_media, command);
375695d67482SBill Paul 		}
375795d67482SBill Paul 		break;
375895d67482SBill Paul 	case SIOCSIFCAP:
375995d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
376075719184SGleb Smirnoff #ifdef DEVICE_POLLING
376175719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
376275719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
376375719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
376475719184SGleb Smirnoff 				if (error)
376575719184SGleb Smirnoff 					return (error);
376675719184SGleb Smirnoff 				BGE_LOCK(sc);
376775719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
376875719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
376975719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
377075719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
377175719184SGleb Smirnoff 				BGE_UNLOCK(sc);
377275719184SGleb Smirnoff 			} else {
377375719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
377475719184SGleb Smirnoff 				/* Enable interrupt even in error case */
377575719184SGleb Smirnoff 				BGE_LOCK(sc);
377675719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
377775719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
377875719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
377975719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
378075719184SGleb Smirnoff 				BGE_UNLOCK(sc);
378175719184SGleb Smirnoff 			}
378275719184SGleb Smirnoff 		}
378375719184SGleb Smirnoff #endif
3784d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
3785d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
3786d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
3787d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
3788b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
378995d67482SBill Paul 			else
3790b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
3791479b23b7SGleb Smirnoff 			VLAN_CAPABILITIES(ifp);
379295d67482SBill Paul 		}
379395d67482SBill Paul 		break;
379495d67482SBill Paul 	default:
3795673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
379695d67482SBill Paul 		break;
379795d67482SBill Paul 	}
379895d67482SBill Paul 
379995d67482SBill Paul 	return (error);
380095d67482SBill Paul }
380195d67482SBill Paul 
380295d67482SBill Paul static void
3803b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
380495d67482SBill Paul {
3805b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
380695d67482SBill Paul 
3807b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
3808b74e67fbSGleb Smirnoff 
3809b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
3810b74e67fbSGleb Smirnoff 		return;
3811b74e67fbSGleb Smirnoff 
3812b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
381395d67482SBill Paul 
3814fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
381595d67482SBill Paul 
381613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3817426742bfSGleb Smirnoff 	bge_init_locked(sc);
381895d67482SBill Paul 
381995d67482SBill Paul 	ifp->if_oerrors++;
382095d67482SBill Paul }
382195d67482SBill Paul 
382295d67482SBill Paul /*
382395d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
382495d67482SBill Paul  * RX and TX lists.
382595d67482SBill Paul  */
382695d67482SBill Paul static void
38273f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
382895d67482SBill Paul {
382995d67482SBill Paul 	struct ifnet *ifp;
383095d67482SBill Paul 	struct ifmedia_entry *ifm;
383195d67482SBill Paul 	struct mii_data *mii = NULL;
383295d67482SBill Paul 	int mtmp, itmp;
383395d67482SBill Paul 
38340f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
38350f9bd73bSSam Leffler 
3836fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
383795d67482SBill Paul 
3838652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0)
383995d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
384095d67482SBill Paul 
38410f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
384295d67482SBill Paul 
384395d67482SBill Paul 	/*
38443f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
384595d67482SBill Paul 	 */
384695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
384795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
384895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
38497ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
385095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
385195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
385295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
385395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
385495d67482SBill Paul 
385595d67482SBill Paul 	/*
38563f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
385795d67482SBill Paul 	 */
385895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
385995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
386095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
386195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
386295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
38637ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
386495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
386595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
386695d67482SBill Paul 
386795d67482SBill Paul 	/*
386895d67482SBill Paul 	 * Shut down all of the memory managers and related
386995d67482SBill Paul 	 * state machines.
387095d67482SBill Paul 	 */
387195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
387295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
38737ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
387495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
387595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
387695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
38777ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
387895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
387995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
38800434d1b8SBill Paul 	}
388195d67482SBill Paul 
388295d67482SBill Paul 	/* Disable host interrupts. */
388395d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
388495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
388595d67482SBill Paul 
388695d67482SBill Paul 	/*
388795d67482SBill Paul 	 * Tell firmware we're shutting down.
388895d67482SBill Paul 	 */
38898cb1383cSDoug Ambrisko 
38908cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
38918cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
38928cb1383cSDoug Ambrisko 	bge_reset(sc);
38938cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
38948cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
38958cb1383cSDoug Ambrisko 
38968cb1383cSDoug Ambrisko 	/*
38978cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
38988cb1383cSDoug Ambrisko 	 */
38998cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
39008cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
39018cb1383cSDoug Ambrisko 	else
390295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
390395d67482SBill Paul 
390495d67482SBill Paul 	/* Free the RX lists. */
390595d67482SBill Paul 	bge_free_rx_ring_std(sc);
390695d67482SBill Paul 
390795d67482SBill Paul 	/* Free jumbo RX list. */
39084c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
390995d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
391095d67482SBill Paul 
391195d67482SBill Paul 	/* Free TX buffers. */
391295d67482SBill Paul 	bge_free_tx_ring(sc);
391395d67482SBill Paul 
391495d67482SBill Paul 	/*
391595d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
391695d67482SBill Paul 	 * unchanged so that things will be put back to normal when
391795d67482SBill Paul 	 * we bring the interface back up.
391895d67482SBill Paul 	 */
3919652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
392095d67482SBill Paul 		itmp = ifp->if_flags;
392195d67482SBill Paul 		ifp->if_flags |= IFF_UP;
3922dcc34049SPawel Jakub Dawidek 		/*
3923dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
3924dcc34049SPawel Jakub Dawidek 		 */
3925dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
392695d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
392795d67482SBill Paul 			mtmp = ifm->ifm_media;
392895d67482SBill Paul 			ifm->ifm_media = IFM_ETHER|IFM_NONE;
392995d67482SBill Paul 			mii_mediachg(mii);
393095d67482SBill Paul 			ifm->ifm_media = mtmp;
3931dcc34049SPawel Jakub Dawidek 		}
393295d67482SBill Paul 		ifp->if_flags = itmp;
393395d67482SBill Paul 	}
393495d67482SBill Paul 
393595d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
393695d67482SBill Paul 
39375dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
39381493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
39391493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
39401493e883SOleg Bulyzhin 	sc->bge_link = 0;
394195d67482SBill Paul 
39421493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
394395d67482SBill Paul }
394495d67482SBill Paul 
394595d67482SBill Paul /*
394695d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
394795d67482SBill Paul  * get confused by errant DMAs when rebooting.
394895d67482SBill Paul  */
394995d67482SBill Paul static void
39503f74909aSGleb Smirnoff bge_shutdown(device_t dev)
395195d67482SBill Paul {
395295d67482SBill Paul 	struct bge_softc *sc;
395395d67482SBill Paul 
395495d67482SBill Paul 	sc = device_get_softc(dev);
395595d67482SBill Paul 
39560f9bd73bSSam Leffler 	BGE_LOCK(sc);
395795d67482SBill Paul 	bge_stop(sc);
395895d67482SBill Paul 	bge_reset(sc);
39590f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
396095d67482SBill Paul }
396114afefa3SPawel Jakub Dawidek 
396214afefa3SPawel Jakub Dawidek static int
396314afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
396414afefa3SPawel Jakub Dawidek {
396514afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
396614afefa3SPawel Jakub Dawidek 
396714afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
396814afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
396914afefa3SPawel Jakub Dawidek 	bge_stop(sc);
397014afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
397114afefa3SPawel Jakub Dawidek 
397214afefa3SPawel Jakub Dawidek 	return (0);
397314afefa3SPawel Jakub Dawidek }
397414afefa3SPawel Jakub Dawidek 
397514afefa3SPawel Jakub Dawidek static int
397614afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
397714afefa3SPawel Jakub Dawidek {
397814afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
397914afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
398014afefa3SPawel Jakub Dawidek 
398114afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
398214afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
398314afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
398414afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
398514afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
398614afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
398714afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
398814afefa3SPawel Jakub Dawidek 	}
398914afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
399014afefa3SPawel Jakub Dawidek 
399114afefa3SPawel Jakub Dawidek 	return (0);
399214afefa3SPawel Jakub Dawidek }
3993dab5cd05SOleg Bulyzhin 
3994dab5cd05SOleg Bulyzhin static void
39953f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
3996dab5cd05SOleg Bulyzhin {
39971f313773SOleg Bulyzhin 	struct mii_data *mii;
39981f313773SOleg Bulyzhin 	uint32_t link, status;
3999dab5cd05SOleg Bulyzhin 
4000dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
40011f313773SOleg Bulyzhin 
40023f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
40037b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
40047b97099dSOleg Bulyzhin 
4005dab5cd05SOleg Bulyzhin 	/*
4006dab5cd05SOleg Bulyzhin 	 * Process link state changes.
4007dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
4008dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
4009dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
4010dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
4011dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
4012dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
4013dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
4014dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
40151f313773SOleg Bulyzhin 	 *
40161f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
40174c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
4018dab5cd05SOleg Bulyzhin 	 */
4019dab5cd05SOleg Bulyzhin 
40201f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
40214c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
4022dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
4023dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
40241f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
40255dda8085SOleg Bulyzhin 			mii_pollstat(mii);
40261f313773SOleg Bulyzhin 			if (!sc->bge_link &&
40271f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
40281f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
40291f313773SOleg Bulyzhin 				sc->bge_link++;
40301f313773SOleg Bulyzhin 				if (bootverbose)
40311f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
40321f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
40331f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
40341f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
40351f313773SOleg Bulyzhin 				sc->bge_link = 0;
40361f313773SOleg Bulyzhin 				if (bootverbose)
40371f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
40381f313773SOleg Bulyzhin 			}
40391f313773SOleg Bulyzhin 
40403f74909aSGleb Smirnoff 			/* Clear the interrupt. */
4041dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
4042dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
4043dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
4044dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
4045dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
4046dab5cd05SOleg Bulyzhin 		}
4047dab5cd05SOleg Bulyzhin 		return;
4048dab5cd05SOleg Bulyzhin 	}
4049dab5cd05SOleg Bulyzhin 
4050652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
40511f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
40527b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
40537b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
40541f313773SOleg Bulyzhin 				sc->bge_link++;
40551f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
40561f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
40571f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
40581f313773SOleg Bulyzhin 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
40591f313773SOleg Bulyzhin 				if (bootverbose)
40601f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
40613f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
40623f74909aSGleb Smirnoff 				    LINK_STATE_UP);
40637b97099dSOleg Bulyzhin 			}
40641f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
4065dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
40661f313773SOleg Bulyzhin 			if (bootverbose)
40671f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
40687b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
40691f313773SOleg Bulyzhin 		}
40701493e883SOleg Bulyzhin 	/* Discard link events for MII/GMII cards if MI auto-polling disabled */
40711493e883SOleg Bulyzhin 	} else if (CSR_READ_4(sc, BGE_MI_MODE) & BGE_MIMODE_AUTOPOLL) {
40721f313773SOleg Bulyzhin 		/*
40731f313773SOleg Bulyzhin 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
40741f313773SOleg Bulyzhin 		 * in status word always set. Workaround this bug by reading
40751f313773SOleg Bulyzhin 		 * PHY link status directly.
40761f313773SOleg Bulyzhin 		 */
40771f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
40781f313773SOleg Bulyzhin 
40791f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
40801f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
40811f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
40825dda8085SOleg Bulyzhin 			mii_pollstat(mii);
40831f313773SOleg Bulyzhin 			if (!sc->bge_link &&
40841f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
40851f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
40861f313773SOleg Bulyzhin 				sc->bge_link++;
40871f313773SOleg Bulyzhin 				if (bootverbose)
40881f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
40891f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
40901f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
40911f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
40921f313773SOleg Bulyzhin 				sc->bge_link = 0;
40931f313773SOleg Bulyzhin 				if (bootverbose)
40941f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
40951f313773SOleg Bulyzhin 			}
40961f313773SOleg Bulyzhin 		}
4097dab5cd05SOleg Bulyzhin 	}
4098dab5cd05SOleg Bulyzhin 
40993f74909aSGleb Smirnoff 	/* Clear the attention. */
4100dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
4101dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
4102dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
4103dab5cd05SOleg Bulyzhin }
41046f8718a3SScott Long 
41056f8718a3SScott Long static void
41066f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
41076f8718a3SScott Long {
41086f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
41096f8718a3SScott Long 	struct sysctl_oid_list *children;
41106f8718a3SScott Long 
41116f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
41126f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
41136f8718a3SScott Long 
41146f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
41156f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
41166f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
41176f8718a3SScott Long 	    "Debug Information");
41186f8718a3SScott Long 
41196f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
41206f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
41216f8718a3SScott Long 	    "Register Read");
41226f8718a3SScott Long 
41236f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
41246f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
41256f8718a3SScott Long 	    "Memory Read");
41266f8718a3SScott Long 
41276f8718a3SScott Long 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "stat_IfHcInOctets",
41286f8718a3SScott Long 	    CTLFLAG_RD,
41296f8718a3SScott Long 	    &sc->bge_ldata.bge_stats->rxstats.ifHCInOctets.bge_addr_lo,
41306f8718a3SScott Long 	    "Bytes received");
41316f8718a3SScott Long 
41326f8718a3SScott Long 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "stat_IfHcOutOctets",
41336f8718a3SScott Long 	    CTLFLAG_RD,
41346f8718a3SScott Long 	    &sc->bge_ldata.bge_stats->txstats.ifHCOutOctets.bge_addr_lo,
41356f8718a3SScott Long 	    "Bytes received");
41366f8718a3SScott Long #endif
41376f8718a3SScott Long }
41386f8718a3SScott Long 
41396f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
41406f8718a3SScott Long static int
41416f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
41426f8718a3SScott Long {
41436f8718a3SScott Long 	struct bge_softc *sc;
41446f8718a3SScott Long 	uint16_t *sbdata;
41456f8718a3SScott Long 	int error;
41466f8718a3SScott Long 	int result;
41476f8718a3SScott Long 	int i, j;
41486f8718a3SScott Long 
41496f8718a3SScott Long 	result = -1;
41506f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
41516f8718a3SScott Long 	if (error || (req->newptr == NULL))
41526f8718a3SScott Long 		return (error);
41536f8718a3SScott Long 
41546f8718a3SScott Long 	if (result == 1) {
41556f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
41566f8718a3SScott Long 
41576f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
41586f8718a3SScott Long 		printf("Status Block:\n");
41596f8718a3SScott Long 		for (i = 0x0; i < (BGE_STATUS_BLK_SZ / 4); ) {
41606f8718a3SScott Long 			printf("%06x:", i);
41616f8718a3SScott Long 			for (j = 0; j < 8; j++) {
41626f8718a3SScott Long 				printf(" %04x", sbdata[i]);
41636f8718a3SScott Long 				i += 4;
41646f8718a3SScott Long 			}
41656f8718a3SScott Long 			printf("\n");
41666f8718a3SScott Long 		}
41676f8718a3SScott Long 
41686f8718a3SScott Long 		printf("Registers:\n");
41696f8718a3SScott Long 		for (i = 0x800; i < 0xa00; ) {
41706f8718a3SScott Long 			printf("%06x:", i);
41716f8718a3SScott Long 			for (j = 0; j < 8; j++) {
41726f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
41736f8718a3SScott Long 				i += 4;
41746f8718a3SScott Long 			}
41756f8718a3SScott Long 			printf("\n");
41766f8718a3SScott Long 		}
41776f8718a3SScott Long 
41786f8718a3SScott Long 		printf("Hardware Flags:\n");
41795345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
41806f8718a3SScott Long 			printf(" - 575X Plus\n");
41815345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
41826f8718a3SScott Long 			printf(" - 5705 Plus\n");
41835345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
41845345bad0SScott Long 			printf(" - 5714 Family\n");
41855345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
41865345bad0SScott Long 			printf(" - 5700 Family\n");
41876f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
41886f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
41896f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
41906f8718a3SScott Long 			printf(" - PCI-X Bus\n");
41916f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
41926f8718a3SScott Long 			printf(" - PCI Express Bus\n");
41936f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_NO3LED)
41946f8718a3SScott Long 			printf(" - No 3 LEDs\n");
41956f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
41966f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
41976f8718a3SScott Long 	}
41986f8718a3SScott Long 
41996f8718a3SScott Long 	return (error);
42006f8718a3SScott Long }
42016f8718a3SScott Long 
42026f8718a3SScott Long static int
42036f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
42046f8718a3SScott Long {
42056f8718a3SScott Long 	struct bge_softc *sc;
42066f8718a3SScott Long 	int error;
42076f8718a3SScott Long 	uint16_t result;
42086f8718a3SScott Long 	uint32_t val;
42096f8718a3SScott Long 
42106f8718a3SScott Long 	result = -1;
42116f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
42126f8718a3SScott Long 	if (error || (req->newptr == NULL))
42136f8718a3SScott Long 		return (error);
42146f8718a3SScott Long 
42156f8718a3SScott Long 	if (result < 0x8000) {
42166f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
42176f8718a3SScott Long 		val = CSR_READ_4(sc, result);
42186f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
42196f8718a3SScott Long 	}
42206f8718a3SScott Long 
42216f8718a3SScott Long 	return (error);
42226f8718a3SScott Long }
42236f8718a3SScott Long 
42246f8718a3SScott Long static int
42256f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
42266f8718a3SScott Long {
42276f8718a3SScott Long 	struct bge_softc *sc;
42286f8718a3SScott Long 	int error;
42296f8718a3SScott Long 	uint16_t result;
42306f8718a3SScott Long 	uint32_t val;
42316f8718a3SScott Long 
42326f8718a3SScott Long 	result = -1;
42336f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
42346f8718a3SScott Long 	if (error || (req->newptr == NULL))
42356f8718a3SScott Long 		return (error);
42366f8718a3SScott Long 
42376f8718a3SScott Long 	if (result < 0x8000) {
42386f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
42396f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
42406f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
42416f8718a3SScott Long 	}
42426f8718a3SScott Long 
42436f8718a3SScott Long 	return (error);
42446f8718a3SScott Long }
42456f8718a3SScott Long #endif
4246