xref: /freebsd/sys/dev/bge/if_bge.c (revision bcc20328f5861ca39cea96fe123f16cc49d70a63)
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 
10908013fd3SMarius Strobl #ifdef __sparc64__
11008013fd3SMarius Strobl #include <dev/ofw/ofw_bus.h>
11108013fd3SMarius Strobl #include <dev/ofw/openfirm.h>
11208013fd3SMarius Strobl #include <machine/ofw_machdep.h>
11308013fd3SMarius Strobl #include <machine/ver.h>
11408013fd3SMarius Strobl #endif
11508013fd3SMarius Strobl 
1164fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1174fbd232cSWarner Losh #include <dev/pci/pcivar.h>
11895d67482SBill Paul 
11995d67482SBill Paul #include <dev/bge/if_bgereg.h>
12095d67482SBill Paul 
1215ddc5794SJohn Polstra #define	BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
122d375e524SGleb Smirnoff #define	ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
12395d67482SBill Paul 
124f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
125f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
12695d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12795d67482SBill Paul 
1287b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
12995d67482SBill Paul #include "miibus_if.h"
13095d67482SBill Paul 
13195d67482SBill Paul /*
13295d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
13395d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
13495d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
13595d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
13695d67482SBill Paul  */
1374c0da0ffSGleb Smirnoff static struct bge_type {
1384c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1394c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
1404c0da0ffSGleb Smirnoff } bge_devs[] = {
1414c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5700 },
1424c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5701 },
14395d67482SBill Paul 
1444c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1000 },
1454c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1002 },
1464c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC9100 },
1474c0da0ffSGleb Smirnoff 
1484c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	APPLE_DEVICE_BCM5701 },
1494c0da0ffSGleb Smirnoff 
1504c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5700 },
1514c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5701 },
1524c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702 },
1534c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702_ALT },
1544c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702X },
1554c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703 },
1564c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703_ALT },
1574c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703X },
1584c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704C },
1594c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S },
1604c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S_ALT },
1614c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705 },
1624c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705F },
1634c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705K },
1644c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M },
1654c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M_ALT },
1664c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714C },
1674c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714S },
1684c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715 },
1694c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715S },
1704c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1714c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
172effef978SRemko Lodder 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5722 },
1734c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1744c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1754c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1764c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1774c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1784c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1794c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1804c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1814c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1824c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1839e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1849e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1859e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1869e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
1874c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
1884c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
1894c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
1904c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
1919e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
1929e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
1939e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
1944c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
1954c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
1964c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
1974c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
1984c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
1994c0da0ffSGleb Smirnoff 
2004c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
2014c0da0ffSGleb Smirnoff 
2024c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
2034c0da0ffSGleb Smirnoff 
2044c0da0ffSGleb Smirnoff 	{ 0, 0 }
20595d67482SBill Paul };
20695d67482SBill Paul 
2074c0da0ffSGleb Smirnoff static const struct bge_vendor {
2084c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2094c0da0ffSGleb Smirnoff 	const char	*v_name;
2104c0da0ffSGleb Smirnoff } bge_vendors[] = {
2114c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2124c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2134c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2144c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2154c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2164c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
2174c0da0ffSGleb Smirnoff 
2184c0da0ffSGleb Smirnoff 	{ 0, NULL }
2194c0da0ffSGleb Smirnoff };
2204c0da0ffSGleb Smirnoff 
2214c0da0ffSGleb Smirnoff static const struct bge_revision {
2224c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2234c0da0ffSGleb Smirnoff 	const char	*br_name;
2244c0da0ffSGleb Smirnoff } bge_revisions[] = {
2254c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2264c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2274c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2284c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2294c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2304c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2314c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2324c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2334c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2344c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2354c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2364c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2374c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2384c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2394c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2404c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2419e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2424c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2434c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2444c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2454c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2464c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2474c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2484c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2494c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2504c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2514c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2524c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2534c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2544c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2554c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2564c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2574c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
25842787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2594c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2604c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2614c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
2624c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
2634c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
2644c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
2654c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
2664c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
2670c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5715_A3,	"BCM5715 A3" },
2680c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A0,	"BCM5755 A0" },
2690c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A1,	"BCM5755 A1" },
2700c4a1ef8SJung-uk Kim 	{ BGE_CHIPID_BCM5755_A2,	"BCM5755 A2" },
271bcc20328SJohn Baldwin 	{ BGE_CHIPID_BCM5722_A0,	"BCM5722 A0" },
27281179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
2736f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
2746f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
2756f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
2764c0da0ffSGleb Smirnoff 
2774c0da0ffSGleb Smirnoff 	{ 0, NULL }
2784c0da0ffSGleb Smirnoff };
2794c0da0ffSGleb Smirnoff 
2804c0da0ffSGleb Smirnoff /*
2814c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
2824c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
2834c0da0ffSGleb Smirnoff  */
2844c0da0ffSGleb Smirnoff static const struct bge_revision bge_majorrevs[] = {
2859e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
2869e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
2879e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
2889e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
2899e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
2909e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
2919e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
2929e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
2939e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
2949e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
2959e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
29681179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
2976f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
2984c0da0ffSGleb Smirnoff 
2994c0da0ffSGleb Smirnoff 	{ 0, NULL }
3004c0da0ffSGleb Smirnoff };
3014c0da0ffSGleb Smirnoff 
3020c8aa4eaSJung-uk Kim #define	BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
3030c8aa4eaSJung-uk Kim #define	BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
3040c8aa4eaSJung-uk Kim #define	BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
3050c8aa4eaSJung-uk Kim #define	BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
3060c8aa4eaSJung-uk Kim #define	BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
3074c0da0ffSGleb Smirnoff 
3084c0da0ffSGleb Smirnoff const struct bge_revision * bge_lookup_rev(uint32_t);
3094c0da0ffSGleb Smirnoff const struct bge_vendor * bge_lookup_vendor(uint16_t);
310e51a25f8SAlfred Perlstein static int bge_probe(device_t);
311e51a25f8SAlfred Perlstein static int bge_attach(device_t);
312e51a25f8SAlfred Perlstein static int bge_detach(device_t);
31314afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
31414afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3153f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
316f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
317f41ac2beSBill Paul static int bge_dma_alloc(device_t);
318f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
319f41ac2beSBill Paul 
320e51a25f8SAlfred Perlstein static void bge_txeof(struct bge_softc *);
321e51a25f8SAlfred Perlstein static void bge_rxeof(struct bge_softc *);
32295d67482SBill Paul 
3238cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
324e51a25f8SAlfred Perlstein static void bge_tick(void *);
325e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
3263f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
327676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
32895d67482SBill Paul 
329e51a25f8SAlfred Perlstein static void bge_intr(void *);
3300f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
331e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
332e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
3330f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
334e51a25f8SAlfred Perlstein static void bge_init(void *);
335e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
336b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
337e51a25f8SAlfred Perlstein static void bge_shutdown(device_t);
33867d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
339e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
340e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
34195d67482SBill Paul 
3423f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
343e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
34495d67482SBill Paul 
3453e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
346e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
347cb2eacc7SYaroslav Tykhiy static void bge_setvlan(struct bge_softc *);
34895d67482SBill Paul 
349e51a25f8SAlfred Perlstein static int bge_newbuf_std(struct bge_softc *, int, struct mbuf *);
350e51a25f8SAlfred Perlstein static int bge_newbuf_jumbo(struct bge_softc *, int, struct mbuf *);
351e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
352e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
353e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
354e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
355e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
356e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
35795d67482SBill Paul 
358e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
359e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
36095d67482SBill Paul 
36108013fd3SMarius Strobl static int bge_has_eeprom(struct bge_softc *);
3623f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
363e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
36495d67482SBill Paul #ifdef notdef
3653f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
36695d67482SBill Paul #endif
3679ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
368e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
36995d67482SBill Paul 
370e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
371e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
372e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
37375719184SGleb Smirnoff #ifdef DEVICE_POLLING
3743f74909aSGleb Smirnoff static void bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
37575719184SGleb Smirnoff #endif
37695d67482SBill Paul 
3778cb1383cSDoug Ambrisko #define	BGE_RESET_START 1
3788cb1383cSDoug Ambrisko #define	BGE_RESET_STOP  2
3798cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
3808cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
3818cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
3828cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
383dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
38495d67482SBill Paul 
3856f8718a3SScott Long /*
3866f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
3876f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
3886f8718a3SScott Long  * traps on certain architectures.
3896f8718a3SScott Long  */
3906f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
3916f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
3926f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
3936f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
3946f8718a3SScott Long #endif
3956f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
396763757b2SScott Long static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
3976f8718a3SScott Long 
39895d67482SBill Paul static device_method_t bge_methods[] = {
39995d67482SBill Paul 	/* Device interface */
40095d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
40195d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
40295d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
40395d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
40414afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
40514afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
40695d67482SBill Paul 
40795d67482SBill Paul 	/* bus interface */
40895d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
40995d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
41095d67482SBill Paul 
41195d67482SBill Paul 	/* MII interface */
41295d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
41395d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
41495d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
41595d67482SBill Paul 
41695d67482SBill Paul 	{ 0, 0 }
41795d67482SBill Paul };
41895d67482SBill Paul 
41995d67482SBill Paul static driver_t bge_driver = {
42095d67482SBill Paul 	"bge",
42195d67482SBill Paul 	bge_methods,
42295d67482SBill Paul 	sizeof(struct bge_softc)
42395d67482SBill Paul };
42495d67482SBill Paul 
42595d67482SBill Paul static devclass_t bge_devclass;
42695d67482SBill Paul 
427f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
42895d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
42995d67482SBill Paul 
430f1a7e6d5SScott Long static int bge_allow_asf = 1;
431f1a7e6d5SScott Long 
432f1a7e6d5SScott Long TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
433f1a7e6d5SScott Long 
434f1a7e6d5SScott Long SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
435f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
436f1a7e6d5SScott Long 	"Allow ASF mode if available");
437c4529f41SMichael Reifenberger 
43808013fd3SMarius Strobl #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
43908013fd3SMarius Strobl #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
44008013fd3SMarius Strobl #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
44108013fd3SMarius Strobl #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
44208013fd3SMarius Strobl #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
44308013fd3SMarius Strobl 
44408013fd3SMarius Strobl static int
44508013fd3SMarius Strobl bge_has_eeprom(struct bge_softc *sc)
44608013fd3SMarius Strobl {
44708013fd3SMarius Strobl #ifdef __sparc64__
44808013fd3SMarius Strobl 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
44908013fd3SMarius Strobl 	device_t dev;
45008013fd3SMarius Strobl 	uint32_t subvendor;
45108013fd3SMarius Strobl 
45208013fd3SMarius Strobl 	dev = sc->bge_dev;
45308013fd3SMarius Strobl 
45408013fd3SMarius Strobl 	/*
45508013fd3SMarius Strobl 	 * The on-board BGEs found in sun4u machines aren't fitted with
45608013fd3SMarius Strobl 	 * an EEPROM which means that we have to obtain the MAC address
45708013fd3SMarius Strobl 	 * via OFW and that some tests will always fail. We distinguish
45808013fd3SMarius Strobl 	 * such BGEs by the subvendor ID, which also has to be obtained
45908013fd3SMarius Strobl 	 * from OFW instead of the PCI configuration space as the latter
46008013fd3SMarius Strobl 	 * indicates Broadcom as the subvendor of the netboot interface.
46108013fd3SMarius Strobl 	 * For early Blade 1500 and 2500 we even have to check the OFW
46208013fd3SMarius Strobl 	 * device path as the subvendor ID always defaults to Broadcom
46308013fd3SMarius Strobl 	 * there.
46408013fd3SMarius Strobl 	 */
46508013fd3SMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
46608013fd3SMarius Strobl 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
46708013fd3SMarius Strobl 	    subvendor == SUN_VENDORID)
46808013fd3SMarius Strobl 		return (0);
46908013fd3SMarius Strobl 	memset(buf, 0, sizeof(buf));
47008013fd3SMarius Strobl 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
47108013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
47208013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
47308013fd3SMarius Strobl 			return (0);
47408013fd3SMarius Strobl 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
47508013fd3SMarius Strobl 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
47608013fd3SMarius Strobl 			return (0);
47708013fd3SMarius Strobl 	}
47808013fd3SMarius Strobl #endif
47908013fd3SMarius Strobl 	return (1);
48008013fd3SMarius Strobl }
48108013fd3SMarius Strobl 
4823f74909aSGleb Smirnoff static uint32_t
4833f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
48495d67482SBill Paul {
48595d67482SBill Paul 	device_t dev;
4866f8718a3SScott Long 	uint32_t val;
48795d67482SBill Paul 
48895d67482SBill Paul 	dev = sc->bge_dev;
48995d67482SBill Paul 
49095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
4916f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
4926f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
4936f8718a3SScott Long 	return (val);
49495d67482SBill Paul }
49595d67482SBill Paul 
49695d67482SBill Paul static void
4973f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
49895d67482SBill Paul {
49995d67482SBill Paul 	device_t dev;
50095d67482SBill Paul 
50195d67482SBill Paul 	dev = sc->bge_dev;
50295d67482SBill Paul 
50395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
50495d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
5056f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
50695d67482SBill Paul }
50795d67482SBill Paul 
50895d67482SBill Paul #ifdef notdef
5093f74909aSGleb Smirnoff static uint32_t
5103f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
51195d67482SBill Paul {
51295d67482SBill Paul 	device_t dev;
51395d67482SBill Paul 
51495d67482SBill Paul 	dev = sc->bge_dev;
51595d67482SBill Paul 
51695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
51795d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
51895d67482SBill Paul }
51995d67482SBill Paul #endif
52095d67482SBill Paul 
52195d67482SBill Paul static void
5223f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
52395d67482SBill Paul {
52495d67482SBill Paul 	device_t dev;
52595d67482SBill Paul 
52695d67482SBill Paul 	dev = sc->bge_dev;
52795d67482SBill Paul 
52895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
52995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
53095d67482SBill Paul }
53195d67482SBill Paul 
5326f8718a3SScott Long static void
5336f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
5346f8718a3SScott Long {
5356f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
5366f8718a3SScott Long }
5376f8718a3SScott Long 
538f41ac2beSBill Paul /*
539f41ac2beSBill Paul  * Map a single buffer address.
540f41ac2beSBill Paul  */
541f41ac2beSBill Paul 
542f41ac2beSBill Paul static void
5433f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
544f41ac2beSBill Paul {
545f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
546f41ac2beSBill Paul 
547f41ac2beSBill Paul 	if (error)
548f41ac2beSBill Paul 		return;
549f41ac2beSBill Paul 
550f41ac2beSBill Paul 	ctx = arg;
551f41ac2beSBill Paul 
552f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
553f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
554f41ac2beSBill Paul 		return;
555f41ac2beSBill Paul 	}
556f41ac2beSBill Paul 
557f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
558f41ac2beSBill Paul }
559f41ac2beSBill Paul 
56095d67482SBill Paul /*
56195d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
56295d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
56395d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
56495d67482SBill Paul  * access method.
56595d67482SBill Paul  */
5663f74909aSGleb Smirnoff static uint8_t
5673f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
56895d67482SBill Paul {
56995d67482SBill Paul 	int i;
5703f74909aSGleb Smirnoff 	uint32_t byte = 0;
57195d67482SBill Paul 
57295d67482SBill Paul 	/*
57395d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
57495d67482SBill Paul 	 * having to use the bitbang method.
57595d67482SBill Paul 	 */
57695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
57795d67482SBill Paul 
57895d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
57995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
58095d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
58195d67482SBill Paul 	DELAY(20);
58295d67482SBill Paul 
58395d67482SBill Paul 	/* Issue the read EEPROM command. */
58495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
58595d67482SBill Paul 
58695d67482SBill Paul 	/* Wait for completion */
58795d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
58895d67482SBill Paul 		DELAY(10);
58995d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
59095d67482SBill Paul 			break;
59195d67482SBill Paul 	}
59295d67482SBill Paul 
593d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT * 10) {
594fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
595f6789fbaSPyun YongHyeon 		return (1);
59695d67482SBill Paul 	}
59795d67482SBill Paul 
59895d67482SBill Paul 	/* Get result. */
59995d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
60095d67482SBill Paul 
6010c8aa4eaSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
60295d67482SBill Paul 
60395d67482SBill Paul 	return (0);
60495d67482SBill Paul }
60595d67482SBill Paul 
60695d67482SBill Paul /*
60795d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
60895d67482SBill Paul  */
60995d67482SBill Paul static int
6103f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
61195d67482SBill Paul {
6123f74909aSGleb Smirnoff 	int i, error = 0;
6133f74909aSGleb Smirnoff 	uint8_t byte = 0;
61495d67482SBill Paul 
61595d67482SBill Paul 	for (i = 0; i < cnt; i++) {
6163f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
6173f74909aSGleb Smirnoff 		if (error)
61895d67482SBill Paul 			break;
61995d67482SBill Paul 		*(dest + i) = byte;
62095d67482SBill Paul 	}
62195d67482SBill Paul 
6223f74909aSGleb Smirnoff 	return (error ? 1 : 0);
62395d67482SBill Paul }
62495d67482SBill Paul 
62595d67482SBill Paul static int
6263f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
62795d67482SBill Paul {
62895d67482SBill Paul 	struct bge_softc *sc;
6293f74909aSGleb Smirnoff 	uint32_t val, autopoll;
63095d67482SBill Paul 	int i;
63195d67482SBill Paul 
63295d67482SBill Paul 	sc = device_get_softc(dev);
63395d67482SBill Paul 
6340434d1b8SBill Paul 	/*
6350434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
6360434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
6370434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
6380434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
6390434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
6400434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
6410434d1b8SBill Paul 	 * special-cased.
6420434d1b8SBill Paul 	 */
643b1265c1aSJohn Polstra 	if (phy != 1)
64498b28ee5SBill Paul 		return (0);
64598b28ee5SBill Paul 
64637ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
64737ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
64837ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
64937ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
65037ceeb4dSPaul Saab 		DELAY(40);
65137ceeb4dSPaul Saab 	}
65237ceeb4dSPaul Saab 
65395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
65495d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
65595d67482SBill Paul 
65695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
657d5d23857SJung-uk Kim 		DELAY(10);
65895d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
65995d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
66095d67482SBill Paul 			break;
66195d67482SBill Paul 	}
66295d67482SBill Paul 
66395d67482SBill Paul 	if (i == BGE_TIMEOUT) {
6646b9f5c94SGleb Smirnoff 		device_printf(sc->bge_dev, "PHY read timed out\n");
66537ceeb4dSPaul Saab 		val = 0;
66637ceeb4dSPaul Saab 		goto done;
66795d67482SBill Paul 	}
66895d67482SBill Paul 
66995d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
67095d67482SBill Paul 
67137ceeb4dSPaul Saab done:
67237ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
67337ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
67437ceeb4dSPaul Saab 		DELAY(40);
67537ceeb4dSPaul Saab 	}
67637ceeb4dSPaul Saab 
67795d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
67895d67482SBill Paul 		return (0);
67995d67482SBill Paul 
6800c8aa4eaSJung-uk Kim 	return (val & 0xFFFF);
68195d67482SBill Paul }
68295d67482SBill Paul 
68395d67482SBill Paul static int
6843f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
68595d67482SBill Paul {
68695d67482SBill Paul 	struct bge_softc *sc;
6873f74909aSGleb Smirnoff 	uint32_t autopoll;
68895d67482SBill Paul 	int i;
68995d67482SBill Paul 
69095d67482SBill Paul 	sc = device_get_softc(dev);
69195d67482SBill Paul 
69237ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
69337ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
69437ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
69537ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
69637ceeb4dSPaul Saab 		DELAY(40);
69737ceeb4dSPaul Saab 	}
69837ceeb4dSPaul Saab 
69995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
70095d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
70195d67482SBill Paul 
70295d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
703d5d23857SJung-uk Kim 		DELAY(10);
70495d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
70595d67482SBill Paul 			break;
706d5d23857SJung-uk Kim 	}
707d5d23857SJung-uk Kim 
708d5d23857SJung-uk Kim 	if (i == BGE_TIMEOUT) {
709d5d23857SJung-uk Kim 		device_printf(sc->bge_dev, "PHY write timed out\n");
710d5d23857SJung-uk Kim 		return (0);
71195d67482SBill Paul 	}
71295d67482SBill Paul 
71337ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
71437ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
71537ceeb4dSPaul Saab 		DELAY(40);
71637ceeb4dSPaul Saab 	}
71737ceeb4dSPaul Saab 
71895d67482SBill Paul 	return (0);
71995d67482SBill Paul }
72095d67482SBill Paul 
72195d67482SBill Paul static void
7223f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
72395d67482SBill Paul {
72495d67482SBill Paul 	struct bge_softc *sc;
72595d67482SBill Paul 	struct mii_data *mii;
72695d67482SBill Paul 	sc = device_get_softc(dev);
72795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
72895d67482SBill Paul 
72995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
7303f74909aSGleb Smirnoff 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)
73195d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
7323f74909aSGleb Smirnoff 	else
73395d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
73495d67482SBill Paul 
7353f74909aSGleb Smirnoff 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
73695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
7373f74909aSGleb Smirnoff 	else
73895d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
73995d67482SBill Paul }
74095d67482SBill Paul 
74195d67482SBill Paul /*
74295d67482SBill Paul  * Intialize a standard receive ring descriptor.
74395d67482SBill Paul  */
74495d67482SBill Paul static int
7453f74909aSGleb Smirnoff bge_newbuf_std(struct bge_softc *sc, int i, struct mbuf *m)
74695d67482SBill Paul {
74795d67482SBill Paul 	struct mbuf *m_new = NULL;
74895d67482SBill Paul 	struct bge_rx_bd *r;
749f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
750f41ac2beSBill Paul 	int error;
75195d67482SBill Paul 
75295d67482SBill Paul 	if (m == NULL) {
753c3a56752SGleb Smirnoff 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
754c3a56752SGleb Smirnoff 		if (m_new == NULL)
75595d67482SBill Paul 			return (ENOBUFS);
75695d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
75795d67482SBill Paul 	} else {
75895d67482SBill Paul 		m_new = m;
75995d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
76095d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
76195d67482SBill Paul 	}
76295d67482SBill Paul 
763652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
76495d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
76595d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
766f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
767f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
768f41ac2beSBill Paul 	ctx.sc = sc;
769f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
770f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
771f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
772f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
773f7cea149SGleb Smirnoff 		if (m == NULL) {
774f7cea149SGleb Smirnoff 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
775f41ac2beSBill Paul 			m_freem(m_new);
776f7cea149SGleb Smirnoff 		}
777f41ac2beSBill Paul 		return (ENOMEM);
778f41ac2beSBill Paul 	}
779e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(ctx.bge_busaddr);
780e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(ctx.bge_busaddr);
781e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
782e907febfSPyun YongHyeon 	r->bge_len = m_new->m_len;
783e907febfSPyun YongHyeon 	r->bge_idx = i;
784f41ac2beSBill Paul 
785f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
786f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
787f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
78895d67482SBill Paul 
78995d67482SBill Paul 	return (0);
79095d67482SBill Paul }
79195d67482SBill Paul 
79295d67482SBill Paul /*
79395d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
79495d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
79595d67482SBill Paul  */
79695d67482SBill Paul static int
7973f74909aSGleb Smirnoff bge_newbuf_jumbo(struct bge_softc *sc, int i, struct mbuf *m)
79895d67482SBill Paul {
7991be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
8001be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
80195d67482SBill Paul 	struct mbuf *m_new = NULL;
8021be6acb7SGleb Smirnoff 	int nsegs;
803f41ac2beSBill Paul 	int error;
80495d67482SBill Paul 
80595d67482SBill Paul 	if (m == NULL) {
806a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
8071be6acb7SGleb Smirnoff 		if (m_new == NULL)
80895d67482SBill Paul 			return (ENOBUFS);
80995d67482SBill Paul 
8101be6acb7SGleb Smirnoff 		m_cljget(m_new, M_DONTWAIT, MJUM9BYTES);
8111be6acb7SGleb Smirnoff 		if (!(m_new->m_flags & M_EXT)) {
81295d67482SBill Paul 			m_freem(m_new);
81395d67482SBill Paul 			return (ENOBUFS);
81495d67482SBill Paul 		}
8151be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
81695d67482SBill Paul 	} else {
81795d67482SBill Paul 		m_new = m;
8181be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
81995d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
82095d67482SBill Paul 	}
82195d67482SBill Paul 
822652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
82395d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
8241be6acb7SGleb Smirnoff 
8251be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
8261be6acb7SGleb Smirnoff 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
8271be6acb7SGleb Smirnoff 	    m_new, segs, &nsegs, BUS_DMA_NOWAIT);
8281be6acb7SGleb Smirnoff 	if (error) {
8291be6acb7SGleb Smirnoff 		if (m == NULL)
830f41ac2beSBill Paul 			m_freem(m_new);
8311be6acb7SGleb Smirnoff 		return (error);
832f7cea149SGleb Smirnoff 	}
8331be6acb7SGleb Smirnoff 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
8341be6acb7SGleb Smirnoff 
8351be6acb7SGleb Smirnoff 	/*
8361be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
8371be6acb7SGleb Smirnoff 	 */
8381be6acb7SGleb Smirnoff 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
8394e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
8404e7ba1abSGleb Smirnoff 	r->bge_idx = i;
8414e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
8424e7ba1abSGleb Smirnoff 	switch (nsegs) {
8434e7ba1abSGleb Smirnoff 	case 4:
8444e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
8454e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
8464e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
8474e7ba1abSGleb Smirnoff 	case 3:
848e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
849e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
850e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
8514e7ba1abSGleb Smirnoff 	case 2:
8524e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
8534e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
8544e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
8554e7ba1abSGleb Smirnoff 	case 1:
8564e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
8574e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
8584e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
8594e7ba1abSGleb Smirnoff 		break;
8604e7ba1abSGleb Smirnoff 	default:
8614e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
8624e7ba1abSGleb Smirnoff 	}
863f41ac2beSBill Paul 
864f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
865f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
866f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
86795d67482SBill Paul 
86895d67482SBill Paul 	return (0);
86995d67482SBill Paul }
87095d67482SBill Paul 
87195d67482SBill Paul /*
87295d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
87395d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
87495d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
87595d67482SBill Paul  * the NIC.
87695d67482SBill Paul  */
87795d67482SBill Paul static int
8783f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
87995d67482SBill Paul {
88095d67482SBill Paul 	int i;
88195d67482SBill Paul 
88295d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
88395d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
88495d67482SBill Paul 			return (ENOBUFS);
88595d67482SBill Paul 	};
88695d67482SBill Paul 
887f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
888f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
889f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
890f41ac2beSBill Paul 
89195d67482SBill Paul 	sc->bge_std = i - 1;
89295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
89395d67482SBill Paul 
89495d67482SBill Paul 	return (0);
89595d67482SBill Paul }
89695d67482SBill Paul 
89795d67482SBill Paul static void
8983f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
89995d67482SBill Paul {
90095d67482SBill Paul 	int i;
90195d67482SBill Paul 
90295d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
90395d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
904e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
905e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
906e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
907f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
908f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
909e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
910e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
91195d67482SBill Paul 		}
912f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
91395d67482SBill Paul 		    sizeof(struct bge_rx_bd));
91495d67482SBill Paul 	}
91595d67482SBill Paul }
91695d67482SBill Paul 
91795d67482SBill Paul static int
9183f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
91995d67482SBill Paul {
92095d67482SBill Paul 	struct bge_rcb *rcb;
9211be6acb7SGleb Smirnoff 	int i;
92295d67482SBill Paul 
92395d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
92495d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
92595d67482SBill Paul 			return (ENOBUFS);
92695d67482SBill Paul 	};
92795d67482SBill Paul 
928f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
929f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
930f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
931f41ac2beSBill Paul 
93295d67482SBill Paul 	sc->bge_jumbo = i - 1;
93395d67482SBill Paul 
934f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
9351be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
9361be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
93767111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
93895d67482SBill Paul 
93995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
94095d67482SBill Paul 
94195d67482SBill Paul 	return (0);
94295d67482SBill Paul }
94395d67482SBill Paul 
94495d67482SBill Paul static void
9453f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
94695d67482SBill Paul {
94795d67482SBill Paul 	int i;
94895d67482SBill Paul 
94995d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
95095d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
951e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
952e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
953e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
954f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
955f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
956e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
957e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
95895d67482SBill Paul 		}
959f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
9601be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
96195d67482SBill Paul 	}
96295d67482SBill Paul }
96395d67482SBill Paul 
96495d67482SBill Paul static void
9653f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
96695d67482SBill Paul {
96795d67482SBill Paul 	int i;
96895d67482SBill Paul 
969f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
97095d67482SBill Paul 		return;
97195d67482SBill Paul 
97295d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
97395d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
974e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
975e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
976e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
977f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
978f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
979e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
980e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
98195d67482SBill Paul 		}
982f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
98395d67482SBill Paul 		    sizeof(struct bge_tx_bd));
98495d67482SBill Paul 	}
98595d67482SBill Paul }
98695d67482SBill Paul 
98795d67482SBill Paul static int
9883f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
98995d67482SBill Paul {
99095d67482SBill Paul 	sc->bge_txcnt = 0;
99195d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
9923927098fSPaul Saab 
99314bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
99414bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
99514bbd30fSGleb Smirnoff 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
99614bbd30fSGleb Smirnoff 
9973927098fSPaul Saab 	/* 5700 b2 errata */
998e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
99914bbd30fSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
10003927098fSPaul Saab 
100114bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
10023927098fSPaul Saab 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
10033927098fSPaul Saab 	/* 5700 b2 errata */
1004e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
100595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
100695d67482SBill Paul 
100795d67482SBill Paul 	return (0);
100895d67482SBill Paul }
100995d67482SBill Paul 
101095d67482SBill Paul static void
10113e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
10123e9b1bcaSJung-uk Kim {
10133e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
10143e9b1bcaSJung-uk Kim 
10153e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
10163e9b1bcaSJung-uk Kim 
10173e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
10183e9b1bcaSJung-uk Kim 
101945ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
10203e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
102145ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
10223e9b1bcaSJung-uk Kim 	else
102345ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
10243e9b1bcaSJung-uk Kim }
10253e9b1bcaSJung-uk Kim 
10263e9b1bcaSJung-uk Kim static void
10273f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
102895d67482SBill Paul {
102995d67482SBill Paul 	struct ifnet *ifp;
103095d67482SBill Paul 	struct ifmultiaddr *ifma;
10313f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
103295d67482SBill Paul 	int h, i;
103395d67482SBill Paul 
10340f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
10350f9bd73bSSam Leffler 
1036fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
103795d67482SBill Paul 
103895d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
103995d67482SBill Paul 		for (i = 0; i < 4; i++)
10400c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
104195d67482SBill Paul 		return;
104295d67482SBill Paul 	}
104395d67482SBill Paul 
104495d67482SBill Paul 	/* First, zot all the existing filters. */
104595d67482SBill Paul 	for (i = 0; i < 4; i++)
104695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
104795d67482SBill Paul 
104895d67482SBill Paul 	/* Now program new ones. */
104913b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
105095d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
105195d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
105295d67482SBill Paul 			continue;
10530e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
10540c8aa4eaSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
10550c8aa4eaSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
105695d67482SBill Paul 	}
105713b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
105895d67482SBill Paul 
105995d67482SBill Paul 	for (i = 0; i < 4; i++)
106095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
106195d67482SBill Paul }
106295d67482SBill Paul 
10638cb1383cSDoug Ambrisko static void
1064cb2eacc7SYaroslav Tykhiy bge_setvlan(struct bge_softc *sc)
1065cb2eacc7SYaroslav Tykhiy {
1066cb2eacc7SYaroslav Tykhiy 	struct ifnet *ifp;
1067cb2eacc7SYaroslav Tykhiy 
1068cb2eacc7SYaroslav Tykhiy 	BGE_LOCK_ASSERT(sc);
1069cb2eacc7SYaroslav Tykhiy 
1070cb2eacc7SYaroslav Tykhiy 	ifp = sc->bge_ifp;
1071cb2eacc7SYaroslav Tykhiy 
1072cb2eacc7SYaroslav Tykhiy 	/* Enable or disable VLAN tag stripping as needed. */
1073cb2eacc7SYaroslav Tykhiy 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1074cb2eacc7SYaroslav Tykhiy 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1075cb2eacc7SYaroslav Tykhiy 	else
1076cb2eacc7SYaroslav Tykhiy 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1077cb2eacc7SYaroslav Tykhiy }
1078cb2eacc7SYaroslav Tykhiy 
1079cb2eacc7SYaroslav Tykhiy static void
10808cb1383cSDoug Ambrisko bge_sig_pre_reset(sc, type)
10818cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10828cb1383cSDoug Ambrisko 	int type;
10838cb1383cSDoug Ambrisko {
10848cb1383cSDoug Ambrisko 	/*
10858cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
10868cb1383cSDoug Ambrisko 	 */
10878cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
10888cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
10898cb1383cSDoug Ambrisko 
10908cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
10918cb1383cSDoug Ambrisko 		switch (type) {
10928cb1383cSDoug Ambrisko 		case BGE_RESET_START:
10938cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
10948cb1383cSDoug Ambrisko 			break;
10958cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
10968cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
10978cb1383cSDoug Ambrisko 			break;
10988cb1383cSDoug Ambrisko 		}
10998cb1383cSDoug Ambrisko 	}
11008cb1383cSDoug Ambrisko }
11018cb1383cSDoug Ambrisko 
11028cb1383cSDoug Ambrisko static void
11038cb1383cSDoug Ambrisko bge_sig_post_reset(sc, type)
11048cb1383cSDoug Ambrisko 	struct bge_softc *sc;
11058cb1383cSDoug Ambrisko 	int type;
11068cb1383cSDoug Ambrisko {
11078cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
11088cb1383cSDoug Ambrisko 		switch (type) {
11098cb1383cSDoug Ambrisko 		case BGE_RESET_START:
11108cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000001);
11118cb1383cSDoug Ambrisko 			/* START DONE */
11128cb1383cSDoug Ambrisko 			break;
11138cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
11148cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000002);
11158cb1383cSDoug Ambrisko 			break;
11168cb1383cSDoug Ambrisko 		}
11178cb1383cSDoug Ambrisko 	}
11188cb1383cSDoug Ambrisko }
11198cb1383cSDoug Ambrisko 
11208cb1383cSDoug Ambrisko static void
11218cb1383cSDoug Ambrisko bge_sig_legacy(sc, type)
11228cb1383cSDoug Ambrisko 	struct bge_softc *sc;
11238cb1383cSDoug Ambrisko 	int type;
11248cb1383cSDoug Ambrisko {
11258cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
11268cb1383cSDoug Ambrisko 		switch (type) {
11278cb1383cSDoug Ambrisko 		case BGE_RESET_START:
11288cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
11298cb1383cSDoug Ambrisko 			break;
11308cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
11318cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
11328cb1383cSDoug Ambrisko 			break;
11338cb1383cSDoug Ambrisko 		}
11348cb1383cSDoug Ambrisko 	}
11358cb1383cSDoug Ambrisko }
11368cb1383cSDoug Ambrisko 
11378cb1383cSDoug Ambrisko void bge_stop_fw(struct bge_softc *);
11388cb1383cSDoug Ambrisko void
11398cb1383cSDoug Ambrisko bge_stop_fw(sc)
11408cb1383cSDoug Ambrisko 	struct bge_softc *sc;
11418cb1383cSDoug Ambrisko {
11428cb1383cSDoug Ambrisko 	int i;
11438cb1383cSDoug Ambrisko 
11448cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
11458cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW, BGE_FW_PAUSE);
11468cb1383cSDoug Ambrisko 		CSR_WRITE_4(sc, BGE_CPU_EVENT,
114739153c5aSJung-uk Kim 		    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
11488cb1383cSDoug Ambrisko 
11498cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
11508cb1383cSDoug Ambrisko 			if (!(CSR_READ_4(sc, BGE_CPU_EVENT) & (1 << 14)))
11518cb1383cSDoug Ambrisko 				break;
11528cb1383cSDoug Ambrisko 			DELAY(10);
11538cb1383cSDoug Ambrisko 		}
11548cb1383cSDoug Ambrisko 	}
11558cb1383cSDoug Ambrisko }
11568cb1383cSDoug Ambrisko 
115795d67482SBill Paul /*
115895d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
115995d67482SBill Paul  * self-test results.
116095d67482SBill Paul  */
116195d67482SBill Paul static int
11623f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
116395d67482SBill Paul {
11643f74909aSGleb Smirnoff 	uint32_t dma_rw_ctl;
116595d67482SBill Paul 	int i;
116695d67482SBill Paul 
11678cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
1168e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
116995d67482SBill Paul 
117095d67482SBill Paul 	/*
117195d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
117208013fd3SMarius Strobl 	 * self-tests passed. Skip this check when there's no
117308013fd3SMarius Strobl 	 * EEPROM fitted, since in that case it will always
117408013fd3SMarius Strobl 	 * fail.
117595d67482SBill Paul 	 */
117608013fd3SMarius Strobl 	if ((sc->bge_flags & BGE_FLAG_EEPROM) &&
117708013fd3SMarius Strobl 	    CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
11780c8aa4eaSJung-uk Kim 		device_printf(sc->bge_dev, "RX CPU self-diagnostics failed!\n");
117995d67482SBill Paul 		return (ENODEV);
118095d67482SBill Paul 	}
118195d67482SBill Paul 
118295d67482SBill Paul 	/* Clear the MAC control register */
118395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
118495d67482SBill Paul 
118595d67482SBill Paul 	/*
118695d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
118795d67482SBill Paul 	 * internal memory.
118895d67482SBill Paul 	 */
118995d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
11903f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
119195d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
119295d67482SBill Paul 
119395d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
11943f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
119595d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
119695d67482SBill Paul 
1197186f842bSJung-uk Kim 	/*
1198186f842bSJung-uk Kim 	 * Set up the PCI DMA control register.
1199186f842bSJung-uk Kim 	 */
1200186f842bSJung-uk Kim 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1201186f842bSJung-uk Kim 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1202652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
1203186f842bSJung-uk Kim 		/* Read watermark not used, 128 bytes for write. */
1204186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1205652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
12064c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
1207186f842bSJung-uk Kim 			/* 256 bytes for read and write. */
1208186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1209186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1210186f842bSJung-uk Kim 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1211186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1212186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1213186f842bSJung-uk Kim 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1214186f842bSJung-uk Kim 			/* 1536 bytes for read, 384 bytes for write. */
1215186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1216186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1217186f842bSJung-uk Kim 		} else {
1218186f842bSJung-uk Kim 			/* 384 bytes for read and write. */
1219186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1220186f842bSJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
12210c8aa4eaSJung-uk Kim 			    0x0F;
1222186f842bSJung-uk Kim 		}
1223e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1224e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
12253f74909aSGleb Smirnoff 			uint32_t tmp;
12265cba12d3SPaul Saab 
1227186f842bSJung-uk Kim 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
12280c8aa4eaSJung-uk Kim 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1229186f842bSJung-uk Kim 			if (tmp == 6 || tmp == 7)
1230186f842bSJung-uk Kim 				dma_rw_ctl |=
1231186f842bSJung-uk Kim 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
12325cba12d3SPaul Saab 
1233186f842bSJung-uk Kim 			/* Set PCI-X DMA write workaround. */
1234186f842bSJung-uk Kim 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1235186f842bSJung-uk Kim 		}
1236186f842bSJung-uk Kim 	} else {
1237186f842bSJung-uk Kim 		/* Conventional PCI bus: 256 bytes for read and write. */
1238186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1239186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1240186f842bSJung-uk Kim 
1241186f842bSJung-uk Kim 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1242186f842bSJung-uk Kim 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1243186f842bSJung-uk Kim 			dma_rw_ctl |= 0x0F;
1244186f842bSJung-uk Kim 	}
1245186f842bSJung-uk Kim 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1246186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1247186f842bSJung-uk Kim 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1248186f842bSJung-uk Kim 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1249e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1250186f842bSJung-uk Kim 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
12515cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
12525cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
125395d67482SBill Paul 
125495d67482SBill Paul 	/*
125595d67482SBill Paul 	 * Set up general mode register.
125695d67482SBill Paul 	 */
1257e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
125895d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1259ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
126095d67482SBill Paul 
126195d67482SBill Paul 	/*
12628cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
12638cb1383cSDoug Ambrisko 	 */
12648cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
12658cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
12668cb1383cSDoug Ambrisko 
12678cb1383cSDoug Ambrisko 	/*
1268ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1269ea13bdd5SJohn Polstra 	 * properly by these devices.
127095d67482SBill Paul 	 */
1271ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
127295d67482SBill Paul 
127395d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
12740c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
127595d67482SBill Paul 
127695d67482SBill Paul 	return (0);
127795d67482SBill Paul }
127895d67482SBill Paul 
127995d67482SBill Paul static int
12803f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
128195d67482SBill Paul {
128295d67482SBill Paul 	struct bge_rcb *rcb;
1283e907febfSPyun YongHyeon 	bus_size_t vrcb;
1284e907febfSPyun YongHyeon 	bge_hostaddr taddr;
12856f8718a3SScott Long 	uint32_t val;
128695d67482SBill Paul 	int i;
128795d67482SBill Paul 
128895d67482SBill Paul 	/*
128995d67482SBill Paul 	 * Initialize the memory window pointer register so that
129095d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
129195d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
129295d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
129395d67482SBill Paul 	 */
129495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
129595d67482SBill Paul 
1296822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1297822f63fcSBill Paul 
12987ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
129995d67482SBill Paul 		/* Configure mbuf memory pool */
13000dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1301822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1302822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1303822f63fcSBill Paul 		else
130495d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
130595d67482SBill Paul 
130695d67482SBill Paul 		/* Configure DMA resource pool */
13070434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
13080434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
130995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
13100434d1b8SBill Paul 	}
131195d67482SBill Paul 
131295d67482SBill Paul 	/* Configure mbuf pool watermarks */
1313e4be3198SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc)) {
13140434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
13150434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
13160434d1b8SBill Paul 	} else {
1317fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1318fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
13190434d1b8SBill Paul 	}
1320fa228020SPaul Saab 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
132195d67482SBill Paul 
132295d67482SBill Paul 	/* Configure DMA resource watermarks */
132395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
132495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
132595d67482SBill Paul 
132695d67482SBill Paul 	/* Enable buffer manager */
13277ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
132895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
132995d67482SBill Paul 		    BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN);
133095d67482SBill Paul 
133195d67482SBill Paul 		/* Poll for buffer manager start indication */
133295d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
1333d5d23857SJung-uk Kim 			DELAY(10);
13340c8aa4eaSJung-uk Kim 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
133595d67482SBill Paul 				break;
133695d67482SBill Paul 		}
133795d67482SBill Paul 
133895d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1339fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1340fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
134195d67482SBill Paul 			return (ENXIO);
134295d67482SBill Paul 		}
13430434d1b8SBill Paul 	}
134495d67482SBill Paul 
134595d67482SBill Paul 	/* Enable flow-through queues */
13460c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
134795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
134895d67482SBill Paul 
134995d67482SBill Paul 	/* Wait until queue initialization is complete */
135095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1351d5d23857SJung-uk Kim 		DELAY(10);
135295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
135395d67482SBill Paul 			break;
135495d67482SBill Paul 	}
135595d67482SBill Paul 
135695d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1357fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
135895d67482SBill Paul 		return (ENXIO);
135995d67482SBill Paul 	}
136095d67482SBill Paul 
136195d67482SBill Paul 	/* Initialize the standard RX ring control block */
1362f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1363f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1364f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1365f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1366f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1367f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1368f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
13697ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
13700434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
13710434d1b8SBill Paul 	else
13720434d1b8SBill Paul 		rcb->bge_maxlen_flags =
13730434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
137495d67482SBill Paul 	rcb->bge_nicaddr = BGE_STD_RX_RINGS;
13750c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
13760c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1377f41ac2beSBill Paul 
137867111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
137967111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
138095d67482SBill Paul 
138195d67482SBill Paul 	/*
138295d67482SBill Paul 	 * Initialize the jumbo RX ring control block
138395d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
138495d67482SBill Paul 	 * field until we're actually ready to start
138595d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
138695d67482SBill Paul 	 * high enough to require it).
138795d67482SBill Paul 	 */
13884c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1389f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1390f41ac2beSBill Paul 
1391f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1392f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1393f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1394f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1395f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1396f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1397f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
13981be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
13991be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
140095d67482SBill Paul 		rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
140167111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
140267111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
140367111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
140467111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1405f41ac2beSBill Paul 
14060434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
14070434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
140867111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
140995d67482SBill Paul 
141095d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1411f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
141267111612SJohn Polstra 		rcb->bge_maxlen_flags =
141367111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
14140434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
14150434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
14160434d1b8SBill Paul 	}
141795d67482SBill Paul 
141895d67482SBill Paul 	/*
141995d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
142095d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
142195d67482SBill Paul 	 * each ring.
14229ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
14239ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
14249ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
14259ba784dbSScott Long 	 * are reports that it might not need to be so strict.
142695d67482SBill Paul 	 */
14275345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
14286f8718a3SScott Long 		val = 8;
14296f8718a3SScott Long 	else
14306f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
14316f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
143295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
143395d67482SBill Paul 
143495d67482SBill Paul 	/*
143595d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
143695d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
143795d67482SBill Paul 	 * These are located in NIC memory.
143895d67482SBill Paul 	 */
1439e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
144095d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1441e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1442e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1443e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1444e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
144595d67482SBill Paul 	}
144695d67482SBill Paul 
144795d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
1448e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1449e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1450e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1451e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1452e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1453e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
14547ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
1455e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1456e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
145795d67482SBill Paul 
145895d67482SBill Paul 	/* Disable all unused RX return rings */
1459e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
146095d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1461e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1462e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1463e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
14640434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1465e907febfSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED));
1466e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
146795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
14683f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1469e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
147095d67482SBill Paul 	}
147195d67482SBill Paul 
147295d67482SBill Paul 	/* Initialize RX ring indexes */
147395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
147495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
147595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
147695d67482SBill Paul 
147795d67482SBill Paul 	/*
147895d67482SBill Paul 	 * Set up RX return ring 0
147995d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
148095d67482SBill Paul 	 * The return rings live entirely within the host, so the
148195d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
148295d67482SBill Paul 	 */
1483e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1484e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1485e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1486e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1487e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1488e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1489e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
149095d67482SBill Paul 
149195d67482SBill Paul 	/* Set random backoff seed for TX */
149295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
14934a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
14944a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
14954a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
149695d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
149795d67482SBill Paul 
149895d67482SBill Paul 	/* Set inter-packet gap */
149995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
150095d67482SBill Paul 
150195d67482SBill Paul 	/*
150295d67482SBill Paul 	 * Specify which ring to use for packets that don't match
150395d67482SBill Paul 	 * any RX rules.
150495d67482SBill Paul 	 */
150595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
150695d67482SBill Paul 
150795d67482SBill Paul 	/*
150895d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
150995d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
151095d67482SBill Paul 	 */
151195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
151295d67482SBill Paul 
151395d67482SBill Paul 	/* Inialize RX list placement stats mask. */
15140c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
151595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
151695d67482SBill Paul 
151795d67482SBill Paul 	/* Disable host coalescing until we get it set up */
151895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
151995d67482SBill Paul 
152095d67482SBill Paul 	/* Poll to make sure it's shut down. */
152195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
1522d5d23857SJung-uk Kim 		DELAY(10);
152395d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
152495d67482SBill Paul 			break;
152595d67482SBill Paul 	}
152695d67482SBill Paul 
152795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1528fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1529fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
153095d67482SBill Paul 		return (ENXIO);
153195d67482SBill Paul 	}
153295d67482SBill Paul 
153395d67482SBill Paul 	/* Set up host coalescing defaults */
153495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
153595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
153695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
153795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
15387ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
153995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
154095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
15410434d1b8SBill Paul 	}
1542b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
1543b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
154495d67482SBill Paul 
154595d67482SBill Paul 	/* Set up address of statistics block */
15467ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
1547f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1548f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
154995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1550f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
15510434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
155295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
15530434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
15540434d1b8SBill Paul 	}
15550434d1b8SBill Paul 
15560434d1b8SBill Paul 	/* Set up address of status block */
1557f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1558f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
155995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1560f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1561f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1562f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
156395d67482SBill Paul 
156495d67482SBill Paul 	/* Turn on host coalescing state machine */
156595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
156695d67482SBill Paul 
156795d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
156895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
156995d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
157095d67482SBill Paul 
157195d67482SBill Paul 	/* Turn on RX list placement state machine */
157295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
157395d67482SBill Paul 
157495d67482SBill Paul 	/* Turn on RX list selector state machine. */
15757ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
157695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
157795d67482SBill Paul 
157895d67482SBill Paul 	/* Turn on DMA, clear stats */
157995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB |
158095d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB | BGE_MACMODE_RX_STATS_CLEAR |
158195d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR | BGE_MACMODE_RX_STATS_ENB |
158295d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB | BGE_MACMODE_FRMHDR_DMA_ENB |
1583652ae483SGleb Smirnoff 	    ((sc->bge_flags & BGE_FLAG_TBI) ?
1584652ae483SGleb Smirnoff 	    BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
158595d67482SBill Paul 
158695d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
158795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
158895d67482SBill Paul 
158995d67482SBill Paul #ifdef notdef
159095d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
159195d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
159295d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
159395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
159495d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
159595d67482SBill Paul #endif
159695d67482SBill Paul 
159795d67482SBill Paul 	/* Turn on DMA completion state machine */
15987ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
159995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
160095d67482SBill Paul 
16016f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
16026f8718a3SScott Long 
16036f8718a3SScott Long 	/* Enable host coalescing bug fix. */
16046f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
16056f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5787)
16060c8aa4eaSJung-uk Kim 			val |= 1 << 29;
16076f8718a3SScott Long 
160895d67482SBill Paul 	/* Turn on write DMA state machine */
16096f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
161095d67482SBill Paul 
161195d67482SBill Paul 	/* Turn on read DMA state machine */
161295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
161395d67482SBill Paul 	    BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS);
161495d67482SBill Paul 
161595d67482SBill Paul 	/* Turn on RX data completion state machine */
161695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
161795d67482SBill Paul 
161895d67482SBill Paul 	/* Turn on RX BD initiator state machine */
161995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
162095d67482SBill Paul 
162195d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
162295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
162395d67482SBill Paul 
162495d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
16257ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
162695d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
162795d67482SBill Paul 
162895d67482SBill Paul 	/* Turn on send BD completion state machine */
162995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
163095d67482SBill Paul 
163195d67482SBill Paul 	/* Turn on send data completion state machine */
163295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
163395d67482SBill Paul 
163495d67482SBill Paul 	/* Turn on send data initiator state machine */
163595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
163695d67482SBill Paul 
163795d67482SBill Paul 	/* Turn on send BD initiator state machine */
163895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
163995d67482SBill Paul 
164095d67482SBill Paul 	/* Turn on send BD selector state machine */
164195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
164295d67482SBill Paul 
16430c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
164495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
164595d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
164695d67482SBill Paul 
164795d67482SBill Paul 	/* ack/clear link change events */
164895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
16490434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
16500434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1651f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
165295d67482SBill Paul 
165395d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
1654652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
165595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1656a1d52896SBill Paul 	} else {
16576098821cSJung-uk Kim 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL | (10 << 16));
16581f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
16594c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
1660a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1661a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1662a1d52896SBill Paul 	}
166395d67482SBill Paul 
16641f313773SOleg Bulyzhin 	/*
16651f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
16661f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
16671f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
16681f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
16691f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
16701f313773SOleg Bulyzhin 	 */
16711f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
16721f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
16731f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
16741f313773SOleg Bulyzhin 
167595d67482SBill Paul 	/* Enable link state change attentions. */
167695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
167795d67482SBill Paul 
167895d67482SBill Paul 	return (0);
167995d67482SBill Paul }
168095d67482SBill Paul 
16814c0da0ffSGleb Smirnoff const struct bge_revision *
16824c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
16834c0da0ffSGleb Smirnoff {
16844c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
16854c0da0ffSGleb Smirnoff 
16864c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
16874c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
16884c0da0ffSGleb Smirnoff 			return (br);
16894c0da0ffSGleb Smirnoff 	}
16904c0da0ffSGleb Smirnoff 
16914c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
16924c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
16934c0da0ffSGleb Smirnoff 			return (br);
16944c0da0ffSGleb Smirnoff 	}
16954c0da0ffSGleb Smirnoff 
16964c0da0ffSGleb Smirnoff 	return (NULL);
16974c0da0ffSGleb Smirnoff }
16984c0da0ffSGleb Smirnoff 
16994c0da0ffSGleb Smirnoff const struct bge_vendor *
17004c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
17014c0da0ffSGleb Smirnoff {
17024c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
17034c0da0ffSGleb Smirnoff 
17044c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
17054c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
17064c0da0ffSGleb Smirnoff 			return (v);
17074c0da0ffSGleb Smirnoff 
17084c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
17094c0da0ffSGleb Smirnoff 	return (NULL);
17104c0da0ffSGleb Smirnoff }
17114c0da0ffSGleb Smirnoff 
171295d67482SBill Paul /*
171395d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
17144c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
17154c0da0ffSGleb Smirnoff  *
17164c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
17177c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
17187c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
17197c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
17207c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
172195d67482SBill Paul  */
172295d67482SBill Paul static int
17233f74909aSGleb Smirnoff bge_probe(device_t dev)
172495d67482SBill Paul {
17254c0da0ffSGleb Smirnoff 	struct bge_type *t = bge_devs;
17264c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
17277c929cf9SJung-uk Kim 	uint16_t vid, did;
172895d67482SBill Paul 
172995d67482SBill Paul 	sc->bge_dev = dev;
17307c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
17317c929cf9SJung-uk Kim 	did = pci_get_device(dev);
17324c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
17337c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
17347c929cf9SJung-uk Kim 			char model[64], buf[96];
17354c0da0ffSGleb Smirnoff 			const struct bge_revision *br;
17364c0da0ffSGleb Smirnoff 			const struct bge_vendor *v;
17374c0da0ffSGleb Smirnoff 			uint32_t id;
17384c0da0ffSGleb Smirnoff 
17394c0da0ffSGleb Smirnoff 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
17404c0da0ffSGleb Smirnoff 			    BGE_PCIMISCCTL_ASICREV;
17414c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
17427c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
17434e35d186SJung-uk Kim 			{
17444e35d186SJung-uk Kim #if __FreeBSD_version > 700024
17454e35d186SJung-uk Kim 				const char *pname;
17464e35d186SJung-uk Kim 
17474e35d186SJung-uk Kim 				if (pci_get_vpd_ident(dev, &pname) == 0)
17484e35d186SJung-uk Kim 					snprintf(model, 64, "%s", pname);
17494e35d186SJung-uk Kim 				else
17504e35d186SJung-uk Kim #endif
17517c929cf9SJung-uk Kim 					snprintf(model, 64, "%s %s",
17527c929cf9SJung-uk Kim 					    v->v_name,
17537c929cf9SJung-uk Kim 					    br != NULL ? br->br_name :
17547c929cf9SJung-uk Kim 					    "NetXtreme Ethernet Controller");
17554e35d186SJung-uk Kim 			}
17567c929cf9SJung-uk Kim 			snprintf(buf, 96, "%s, %sASIC rev. %#04x", model,
17577c929cf9SJung-uk Kim 			    br != NULL ? "" : "unknown ", id >> 16);
17584c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
17596d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
17605ee49a3aSJung-uk Kim 				sc->bge_flags |= BGE_FLAG_NO_3LED;
176108bf8bb7SJung-uk Kim 			if (did == BCOM_DEVICEID_BCM5755M)
176208bf8bb7SJung-uk Kim 				sc->bge_flags |= BGE_FLAG_ADJUST_TRIM;
176395d67482SBill Paul 			return (0);
176495d67482SBill Paul 		}
176595d67482SBill Paul 		t++;
176695d67482SBill Paul 	}
176795d67482SBill Paul 
176895d67482SBill Paul 	return (ENXIO);
176995d67482SBill Paul }
177095d67482SBill Paul 
1771f41ac2beSBill Paul static void
17723f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
1773f41ac2beSBill Paul {
1774f41ac2beSBill Paul 	int i;
1775f41ac2beSBill Paul 
17763f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
1777f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1778f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1779f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1780f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1781f41ac2beSBill Paul 	}
1782f41ac2beSBill Paul 
17833f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
1784f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1785f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1786f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1787f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1788f41ac2beSBill Paul 	}
1789f41ac2beSBill Paul 
17903f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
1791f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1792f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1793f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1794f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1795f41ac2beSBill Paul 	}
1796f41ac2beSBill Paul 
1797f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1798f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1799f41ac2beSBill Paul 
1800f41ac2beSBill Paul 
18013f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
1802e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
1803e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1804e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
1805e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1806f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1807f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1808f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1809f41ac2beSBill Paul 
1810f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1811f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1812f41ac2beSBill Paul 
18133f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
1814e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1815e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1816e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1817e65bed95SPyun YongHyeon 
1818e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1819e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
1820f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1821f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1822f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1823f41ac2beSBill Paul 
1824f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1825f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1826f41ac2beSBill Paul 
18273f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
1828e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
1829e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1830e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
1831e65bed95SPyun YongHyeon 
1832e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1833e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
1834f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1835f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1836f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1837f41ac2beSBill Paul 
1838f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1839f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1840f41ac2beSBill Paul 
18413f74909aSGleb Smirnoff 	/* Destroy TX ring. */
1842e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
1843e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1844e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
1845e65bed95SPyun YongHyeon 
1846e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
1847f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1848f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1849f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1850f41ac2beSBill Paul 
1851f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1852f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1853f41ac2beSBill Paul 
18543f74909aSGleb Smirnoff 	/* Destroy status block. */
1855e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
1856e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1857e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
1858e65bed95SPyun YongHyeon 
1859e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
1860f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1861f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1862f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1863f41ac2beSBill Paul 
1864f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1865f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1866f41ac2beSBill Paul 
18673f74909aSGleb Smirnoff 	/* Destroy statistics block. */
1868e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
1869e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1870e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
1871e65bed95SPyun YongHyeon 
1872e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
1873f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1874f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1875f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1876f41ac2beSBill Paul 
1877f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1878f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1879f41ac2beSBill Paul 
18803f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
1881f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1882f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1883f41ac2beSBill Paul }
1884f41ac2beSBill Paul 
1885f41ac2beSBill Paul static int
18863f74909aSGleb Smirnoff bge_dma_alloc(device_t dev)
1887f41ac2beSBill Paul {
18883f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
1889f41ac2beSBill Paul 	struct bge_softc *sc;
18901be6acb7SGleb Smirnoff 	int i, error;
1891f41ac2beSBill Paul 
1892f41ac2beSBill Paul 	sc = device_get_softc(dev);
1893f41ac2beSBill Paul 
1894f41ac2beSBill Paul 	/*
1895f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1896f41ac2beSBill Paul 	 */
1897378f231eSJohn-Mark Gurney 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev), /* parent */
1898706620f0SScott Long 			1, 0,			/* alignment, boundary */
1899f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
19002f28b973SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1901f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1902f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1903f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
19048a40c10eSScott Long 			0,			/* flags */
1905f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1906f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1907f41ac2beSBill Paul 
1908e65bed95SPyun YongHyeon 	if (error != 0) {
1909fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1910fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
1911e65bed95SPyun YongHyeon 		return (ENOMEM);
1912e65bed95SPyun YongHyeon 	}
1913e65bed95SPyun YongHyeon 
1914f41ac2beSBill Paul 	/*
1915f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1916f41ac2beSBill Paul 	 */
19178a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1918f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
19191be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
19201be6acb7SGleb Smirnoff 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
1921f41ac2beSBill Paul 
1922f41ac2beSBill Paul 	if (error) {
1923fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1924f41ac2beSBill Paul 		return (ENOMEM);
1925f41ac2beSBill Paul 	}
1926f41ac2beSBill Paul 
19273f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
1928f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1929f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1930f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1931f41ac2beSBill Paul 		if (error) {
1932fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1933fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1934f41ac2beSBill Paul 			return (ENOMEM);
1935f41ac2beSBill Paul 		}
1936f41ac2beSBill Paul 	}
1937f41ac2beSBill Paul 
19383f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
1939f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1940f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1941f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1942f41ac2beSBill Paul 		if (error) {
1943fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1944fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1945f41ac2beSBill Paul 			return (ENOMEM);
1946f41ac2beSBill Paul 		}
1947f41ac2beSBill Paul 	}
1948f41ac2beSBill Paul 
19493f74909aSGleb Smirnoff 	/* Create tag for standard RX ring. */
1950f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1951f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1952f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1953f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1954f41ac2beSBill Paul 
1955f41ac2beSBill Paul 	if (error) {
1956fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1957f41ac2beSBill Paul 		return (ENOMEM);
1958f41ac2beSBill Paul 	}
1959f41ac2beSBill Paul 
19603f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for standard RX ring. */
1961f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1962f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1963f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1964f41ac2beSBill Paul 	if (error)
1965f41ac2beSBill Paul 		return (ENOMEM);
1966f41ac2beSBill Paul 
1967f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1968f41ac2beSBill Paul 
19693f74909aSGleb Smirnoff 	/* Load the address of the standard RX ring. */
1970f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1971f41ac2beSBill Paul 	ctx.sc = sc;
1972f41ac2beSBill Paul 
1973f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1974f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1975f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1976f41ac2beSBill Paul 
1977f41ac2beSBill Paul 	if (error)
1978f41ac2beSBill Paul 		return (ENOMEM);
1979f41ac2beSBill Paul 
1980f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
1981f41ac2beSBill Paul 
19823f74909aSGleb Smirnoff 	/* Create tags for jumbo mbufs. */
19834c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1984f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
19858a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
19861be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
19871be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
1988f41ac2beSBill Paul 		if (error) {
1989fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
19903f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
1991f41ac2beSBill Paul 			return (ENOMEM);
1992f41ac2beSBill Paul 		}
1993f41ac2beSBill Paul 
19943f74909aSGleb Smirnoff 		/* Create tag for jumbo RX ring. */
1995f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1996f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1997f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
1998f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
1999f41ac2beSBill Paul 
2000f41ac2beSBill Paul 		if (error) {
2001fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
20023f74909aSGleb Smirnoff 			    "could not allocate jumbo ring dma tag\n");
2003f41ac2beSBill Paul 			return (ENOMEM);
2004f41ac2beSBill Paul 		}
2005f41ac2beSBill Paul 
20063f74909aSGleb Smirnoff 		/* Allocate DMA'able memory for jumbo RX ring. */
2007f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
20081be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
20091be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
2010f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
2011f41ac2beSBill Paul 		if (error)
2012f41ac2beSBill Paul 			return (ENOMEM);
2013f41ac2beSBill Paul 
20143f74909aSGleb Smirnoff 		/* Load the address of the jumbo RX ring. */
2015f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
2016f41ac2beSBill Paul 		ctx.sc = sc;
2017f41ac2beSBill Paul 
2018f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2019f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2020f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
2021f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2022f41ac2beSBill Paul 
2023f41ac2beSBill Paul 		if (error)
2024f41ac2beSBill Paul 			return (ENOMEM);
2025f41ac2beSBill Paul 
2026f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
2027f41ac2beSBill Paul 
20283f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
2029f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2030f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2031f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2032f41ac2beSBill Paul 			if (error) {
2033fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
20343f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
2035f41ac2beSBill Paul 				return (ENOMEM);
2036f41ac2beSBill Paul 			}
2037f41ac2beSBill Paul 		}
2038f41ac2beSBill Paul 
2039f41ac2beSBill Paul 	}
2040f41ac2beSBill Paul 
20413f74909aSGleb Smirnoff 	/* Create tag for RX return ring. */
2042f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2043f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2044f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
2045f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
2046f41ac2beSBill Paul 
2047f41ac2beSBill Paul 	if (error) {
2048fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2049f41ac2beSBill Paul 		return (ENOMEM);
2050f41ac2beSBill Paul 	}
2051f41ac2beSBill Paul 
20523f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for RX return ring. */
2053f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
2054f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
2055f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
2056f41ac2beSBill Paul 	if (error)
2057f41ac2beSBill Paul 		return (ENOMEM);
2058f41ac2beSBill Paul 
2059f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
2060f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
2061f41ac2beSBill Paul 
20623f74909aSGleb Smirnoff 	/* Load the address of the RX return ring. */
2063f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2064f41ac2beSBill Paul 	ctx.sc = sc;
2065f41ac2beSBill Paul 
2066f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
2067f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
2068f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
2069f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2070f41ac2beSBill Paul 
2071f41ac2beSBill Paul 	if (error)
2072f41ac2beSBill Paul 		return (ENOMEM);
2073f41ac2beSBill Paul 
2074f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
2075f41ac2beSBill Paul 
20763f74909aSGleb Smirnoff 	/* Create tag for TX ring. */
2077f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2078f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2079f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
2080f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
2081f41ac2beSBill Paul 
2082f41ac2beSBill Paul 	if (error) {
2083fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2084f41ac2beSBill Paul 		return (ENOMEM);
2085f41ac2beSBill Paul 	}
2086f41ac2beSBill Paul 
20873f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for TX ring. */
2088f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
2089f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
2090f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
2091f41ac2beSBill Paul 	if (error)
2092f41ac2beSBill Paul 		return (ENOMEM);
2093f41ac2beSBill Paul 
2094f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
2095f41ac2beSBill Paul 
20963f74909aSGleb Smirnoff 	/* Load the address of the TX ring. */
2097f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2098f41ac2beSBill Paul 	ctx.sc = sc;
2099f41ac2beSBill Paul 
2100f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
2101f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
2102f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2103f41ac2beSBill Paul 
2104f41ac2beSBill Paul 	if (error)
2105f41ac2beSBill Paul 		return (ENOMEM);
2106f41ac2beSBill Paul 
2107f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
2108f41ac2beSBill Paul 
21093f74909aSGleb Smirnoff 	/* Create tag for status block. */
2110f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2111f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2112f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
2113f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
2114f41ac2beSBill Paul 
2115f41ac2beSBill Paul 	if (error) {
2116fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2117f41ac2beSBill Paul 		return (ENOMEM);
2118f41ac2beSBill Paul 	}
2119f41ac2beSBill Paul 
21203f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for status block. */
2121f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
2122f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
2123f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
2124f41ac2beSBill Paul 	if (error)
2125f41ac2beSBill Paul 		return (ENOMEM);
2126f41ac2beSBill Paul 
2127f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2128f41ac2beSBill Paul 
21293f74909aSGleb Smirnoff 	/* Load the address of the status block. */
2130f41ac2beSBill Paul 	ctx.sc = sc;
2131f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2132f41ac2beSBill Paul 
2133f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2134f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2135f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2136f41ac2beSBill Paul 
2137f41ac2beSBill Paul 	if (error)
2138f41ac2beSBill Paul 		return (ENOMEM);
2139f41ac2beSBill Paul 
2140f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2141f41ac2beSBill Paul 
21423f74909aSGleb Smirnoff 	/* Create tag for statistics block. */
2143f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2144f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2145f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2146f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
2147f41ac2beSBill Paul 
2148f41ac2beSBill Paul 	if (error) {
2149fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2150f41ac2beSBill Paul 		return (ENOMEM);
2151f41ac2beSBill Paul 	}
2152f41ac2beSBill Paul 
21533f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for statistics block. */
2154f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2155f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2156f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
2157f41ac2beSBill Paul 	if (error)
2158f41ac2beSBill Paul 		return (ENOMEM);
2159f41ac2beSBill Paul 
2160f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2161f41ac2beSBill Paul 
21623f74909aSGleb Smirnoff 	/* Load the address of the statstics block. */
2163f41ac2beSBill Paul 	ctx.sc = sc;
2164f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2165f41ac2beSBill Paul 
2166f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2167f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2168f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2169f41ac2beSBill Paul 
2170f41ac2beSBill Paul 	if (error)
2171f41ac2beSBill Paul 		return (ENOMEM);
2172f41ac2beSBill Paul 
2173f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2174f41ac2beSBill Paul 
2175f41ac2beSBill Paul 	return (0);
2176f41ac2beSBill Paul }
2177f41ac2beSBill Paul 
21780a55a034SJung-uk Kim #if __FreeBSD_version > 602105
2179bf6ef57aSJohn Polstra /*
2180bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
2181bf6ef57aSJohn Polstra  */
2182bf6ef57aSJohn Polstra static int
2183bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
2184bf6ef57aSJohn Polstra {
2185bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
218655aaf894SMarius Strobl 	u_int b, d, f, fscan, s;
2187bf6ef57aSJohn Polstra 
218855aaf894SMarius Strobl 	d = pci_get_domain(dev);
2189bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
2190bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
2191bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
2192bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
219355aaf894SMarius Strobl 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
2194bf6ef57aSJohn Polstra 			return (1);
2195bf6ef57aSJohn Polstra 	return (0);
2196bf6ef57aSJohn Polstra }
2197bf6ef57aSJohn Polstra 
2198bf6ef57aSJohn Polstra /*
2199bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
2200bf6ef57aSJohn Polstra  */
2201bf6ef57aSJohn Polstra static int
2202bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
2203bf6ef57aSJohn Polstra {
2204bf6ef57aSJohn Polstra 	int can_use_msi = 0;
2205bf6ef57aSJohn Polstra 
2206bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
2207bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
2208bf6ef57aSJohn Polstra 		/*
2209bf6ef57aSJohn Polstra 		 * Apparently, MSI doesn't work when this chip is configured
2210bf6ef57aSJohn Polstra 		 * in single-port mode.
2211bf6ef57aSJohn Polstra 		 */
2212bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
2213bf6ef57aSJohn Polstra 			can_use_msi = 1;
2214bf6ef57aSJohn Polstra 		break;
2215bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
2216bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
2217bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
2218bf6ef57aSJohn Polstra 			can_use_msi = 1;
2219bf6ef57aSJohn Polstra 		break;
2220bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5752:
2221bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5780:
2222bf6ef57aSJohn Polstra 		can_use_msi = 1;
2223bf6ef57aSJohn Polstra 		break;
2224bf6ef57aSJohn Polstra 	}
2225bf6ef57aSJohn Polstra 	return (can_use_msi);
2226bf6ef57aSJohn Polstra }
22274e35d186SJung-uk Kim #endif
2228bf6ef57aSJohn Polstra 
222995d67482SBill Paul static int
22303f74909aSGleb Smirnoff bge_attach(device_t dev)
223195d67482SBill Paul {
223295d67482SBill Paul 	struct ifnet *ifp;
223395d67482SBill Paul 	struct bge_softc *sc;
22343f74909aSGleb Smirnoff 	uint32_t hwcfg = 0;
22353f74909aSGleb Smirnoff 	uint32_t mac_tmp = 0;
223608013fd3SMarius Strobl 	u_char eaddr[ETHER_ADDR_LEN];
223708013fd3SMarius Strobl 	int error, reg, rid, trys;
223895d67482SBill Paul 
223995d67482SBill Paul 	sc = device_get_softc(dev);
224095d67482SBill Paul 	sc->bge_dev = dev;
224195d67482SBill Paul 
224295d67482SBill Paul 	/*
224395d67482SBill Paul 	 * Map control/status registers.
224495d67482SBill Paul 	 */
224595d67482SBill Paul 	pci_enable_busmaster(dev);
224695d67482SBill Paul 
224795d67482SBill Paul 	rid = BGE_PCI_BAR0;
22485f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
22495f96beb9SNate Lawson 	    RF_ACTIVE | PCI_RF_DENSE);
225095d67482SBill Paul 
225195d67482SBill Paul 	if (sc->bge_res == NULL) {
2252fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
225395d67482SBill Paul 		error = ENXIO;
225495d67482SBill Paul 		goto fail;
225595d67482SBill Paul 	}
225695d67482SBill Paul 
225795d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
225895d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
225995d67482SBill Paul 
2260e53d81eeSPaul Saab 	/* Save ASIC rev. */
2261e53d81eeSPaul Saab 
2262e53d81eeSPaul Saab 	sc->bge_chipid =
2263e53d81eeSPaul Saab 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2264e53d81eeSPaul Saab 	    BGE_PCIMISCCTL_ASICREV;
2265e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2266e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2267e53d81eeSPaul Saab 
226886543395SJung-uk Kim 	/*
226986543395SJung-uk Kim 	 * Don't enable Ethernet@WireSpeed for the 5700 or the
227086543395SJung-uk Kim 	 * 5705 A0 and A1 chips.
227186543395SJung-uk Kim 	 */
227286543395SJung-uk Kim 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
227386543395SJung-uk Kim 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
227486543395SJung-uk Kim 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)
227586543395SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_WIRESPEED;
227686543395SJung-uk Kim 
227708013fd3SMarius Strobl 	if (bge_has_eeprom(sc))
227808013fd3SMarius Strobl 		sc->bge_flags |= BGE_FLAG_EEPROM;
227908013fd3SMarius Strobl 
22800dae9719SJung-uk Kim 	/* Save chipset family. */
22810dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
22820dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
22830dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
22840dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
22850dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
22867ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
22870dae9719SJung-uk Kim 		break;
22880dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
22890dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
22900dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
22917ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5714_FAMILY /* | BGE_FLAG_JUMBO */;
22925ee49a3aSJung-uk Kim 		/* FALLTHRU */
22930dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
22940dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
22950dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5755:
22960dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5787:
22970dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
22985ee49a3aSJung-uk Kim 		/* FALLTHRU */
22990dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
23000dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
23010dae9719SJung-uk Kim 		break;
23020dae9719SJung-uk Kim 	}
23030dae9719SJung-uk Kim 
23045ee49a3aSJung-uk Kim 	/* Set various bug flags. */
23051ec4c3a8SJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
23061ec4c3a8SJung-uk Kim 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
23071ec4c3a8SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_CRC_BUG;
23085ee49a3aSJung-uk Kim 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
23095ee49a3aSJung-uk Kim 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
23105ee49a3aSJung-uk Kim 		sc->bge_flags |= BGE_FLAG_ADC_BUG;
23115ee49a3aSJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
23125ee49a3aSJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5704_A0_BUG;
231308bf8bb7SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc) &&
231408bf8bb7SJung-uk Kim 	    !(sc->bge_flags & BGE_FLAG_ADJUST_TRIM)) {
23155ee49a3aSJung-uk Kim 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
23165ee49a3aSJung-uk Kim 		    sc->bge_asicrev == BGE_ASICREV_BCM5787)
23175ee49a3aSJung-uk Kim 			sc->bge_flags |= BGE_FLAG_JITTER_BUG;
23185ee49a3aSJung-uk Kim 		else
23195ee49a3aSJung-uk Kim 			sc->bge_flags |= BGE_FLAG_BER_BUG;
23205ee49a3aSJung-uk Kim 	}
23215ee49a3aSJung-uk Kim 
2322e53d81eeSPaul Saab   	/*
23236f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
2324e53d81eeSPaul Saab   	 */
2325fe09b799SJung-uk Kim #if __FreeBSD_version > 602101
23266f8718a3SScott Long 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
23274c0da0ffSGleb Smirnoff 		/*
23286f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
23296f8718a3SScott Long 		 * must be a PCI Express device.
23306f8718a3SScott Long 		 */
23316f8718a3SScott Long 		if (reg != 0)
23326f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIE;
23336f8718a3SScott Long 	} else if (pci_find_extcap(dev, PCIY_PCIX, &reg) == 0) {
23346f8718a3SScott Long 		if (reg != 0)
23356f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIX;
23366f8718a3SScott Long 	}
23376f8718a3SScott Long 
23386f8718a3SScott Long #else
23395345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc)) {
23406f8718a3SScott Long 		reg = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
23410c8aa4eaSJung-uk Kim 		if ((reg & 0xFF) == BGE_PCIE_CAPID)
23426f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIE;
23436f8718a3SScott Long 	} else {
23446f8718a3SScott Long 		/*
23456f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
23466f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
23474c0da0ffSGleb Smirnoff 		 */
23484c0da0ffSGleb Smirnoff 		if ((pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
23494c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
2350652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
23516f8718a3SScott Long 	}
23526f8718a3SScott Long #endif
23534c0da0ffSGleb Smirnoff 
23540a55a034SJung-uk Kim #if __FreeBSD_version > 602105
23554e35d186SJung-uk Kim 	{
23564e35d186SJung-uk Kim 		int msicount;
23574e35d186SJung-uk Kim 
2358bf6ef57aSJohn Polstra 		/*
2359bf6ef57aSJohn Polstra 		 * Allocate the interrupt, using MSI if possible.  These devices
2360bf6ef57aSJohn Polstra 		 * support 8 MSI messages, but only the first one is used in
2361bf6ef57aSJohn Polstra 		 * normal operation.
2362bf6ef57aSJohn Polstra 		 */
2363bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
2364bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
2365bf6ef57aSJohn Polstra 			if (msicount > 1)
2366bf6ef57aSJohn Polstra 				msicount = 1;
2367bf6ef57aSJohn Polstra 		} else
2368bf6ef57aSJohn Polstra 			msicount = 0;
2369bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
2370bf6ef57aSJohn Polstra 			rid = 1;
2371bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
2372bf6ef57aSJohn Polstra 		} else
2373bf6ef57aSJohn Polstra 			rid = 0;
23744e35d186SJung-uk Kim 	}
23754e35d186SJung-uk Kim #else
23764e35d186SJung-uk Kim 	rid = 0;
23774e35d186SJung-uk Kim #endif
2378bf6ef57aSJohn Polstra 
2379bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2380bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
2381bf6ef57aSJohn Polstra 
2382bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
2383bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
2384bf6ef57aSJohn Polstra 		error = ENXIO;
2385bf6ef57aSJohn Polstra 		goto fail;
2386bf6ef57aSJohn Polstra 	}
2387bf6ef57aSJohn Polstra 
2388bf6ef57aSJohn Polstra 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
2389bf6ef57aSJohn Polstra 
239095d67482SBill Paul 	/* Try to reset the chip. */
23918cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
23928cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
23938cb1383cSDoug Ambrisko 		error = ENXIO;
23948cb1383cSDoug Ambrisko 		goto fail;
23958cb1383cSDoug Ambrisko 	}
23968cb1383cSDoug Ambrisko 
23978cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
2398f1a7e6d5SScott Long 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG)
2399f1a7e6d5SScott Long 	    == BGE_MAGIC_NUMBER)) {
24008cb1383cSDoug Ambrisko 		if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG)
24018cb1383cSDoug Ambrisko 		    & BGE_HWCFG_ASF) {
24028cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_ENABLE;
24038cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_STACKUP;
24048cb1383cSDoug Ambrisko 			if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
24058cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
24068cb1383cSDoug Ambrisko 			}
24078cb1383cSDoug Ambrisko 		}
24088cb1383cSDoug Ambrisko 	}
24098cb1383cSDoug Ambrisko 
24108cb1383cSDoug Ambrisko 	/* Try to reset the chip again the nice way. */
24118cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
24128cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
24138cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
24148cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
24158cb1383cSDoug Ambrisko 		error = ENXIO;
24168cb1383cSDoug Ambrisko 		goto fail;
24178cb1383cSDoug Ambrisko 	}
24188cb1383cSDoug Ambrisko 
24198cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
24208cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
242195d67482SBill Paul 
242295d67482SBill Paul 	if (bge_chipinit(sc)) {
2423fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
242495d67482SBill Paul 		error = ENXIO;
242595d67482SBill Paul 		goto fail;
242695d67482SBill Paul 	}
242795d67482SBill Paul 
242808013fd3SMarius Strobl #ifdef __sparc64__
242908013fd3SMarius Strobl 	if ((sc->bge_flags & BGE_FLAG_EEPROM) == 0)
243008013fd3SMarius Strobl 		OF_getetheraddr(dev, eaddr);
243108013fd3SMarius Strobl 	else
243208013fd3SMarius Strobl #endif
243308013fd3SMarius Strobl 	{
24340c8aa4eaSJung-uk Kim 		mac_tmp = bge_readmem_ind(sc, 0x0C14);
24350c8aa4eaSJung-uk Kim 		if ((mac_tmp >> 16) == 0x484B) {
2436fc74a9f9SBrooks Davis 			eaddr[0] = (u_char)(mac_tmp >> 8);
2437fc74a9f9SBrooks Davis 			eaddr[1] = (u_char)mac_tmp;
24380c8aa4eaSJung-uk Kim 			mac_tmp = bge_readmem_ind(sc, 0x0C18);
2439fc74a9f9SBrooks Davis 			eaddr[2] = (u_char)(mac_tmp >> 24);
2440fc74a9f9SBrooks Davis 			eaddr[3] = (u_char)(mac_tmp >> 16);
2441fc74a9f9SBrooks Davis 			eaddr[4] = (u_char)(mac_tmp >> 8);
2442fc74a9f9SBrooks Davis 			eaddr[5] = (u_char)mac_tmp;
2443fc74a9f9SBrooks Davis 		} else if (bge_read_eeprom(sc, eaddr,
244495d67482SBill Paul 		    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
244508013fd3SMarius Strobl 			device_printf(sc->bge_dev,
244608013fd3SMarius Strobl 			    "failed to read station address\n");
244795d67482SBill Paul 			error = ENXIO;
244895d67482SBill Paul 			goto fail;
244995d67482SBill Paul 		}
245008013fd3SMarius Strobl 	}
245195d67482SBill Paul 
2452f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
24537ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
2454f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2455f41ac2beSBill Paul 	else
2456f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2457f41ac2beSBill Paul 
2458f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2459fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2460fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2461f41ac2beSBill Paul 		error = ENXIO;
2462f41ac2beSBill Paul 		goto fail;
2463f41ac2beSBill Paul 	}
2464f41ac2beSBill Paul 
246595d67482SBill Paul 	/* Set default tuneable values. */
246695d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
246795d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
246895d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
24696f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
24706f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
247195d67482SBill Paul 
247295d67482SBill Paul 	/* Set up ifnet structure */
2473fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2474fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2475fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2476fc74a9f9SBrooks Davis 		error = ENXIO;
2477fc74a9f9SBrooks Davis 		goto fail;
2478fc74a9f9SBrooks Davis 	}
247995d67482SBill Paul 	ifp->if_softc = sc;
24809bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
248195d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
248295d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
248395d67482SBill Paul 	ifp->if_start = bge_start;
248495d67482SBill Paul 	ifp->if_init = bge_init;
248595d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
24864d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
24874d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
24884d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
248995d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2490d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
24914e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
24924e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
24934e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
24944e35d186SJung-uk Kim #endif
249595d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
249675719184SGleb Smirnoff #ifdef DEVICE_POLLING
249775719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
249875719184SGleb Smirnoff #endif
249995d67482SBill Paul 
2500a1d52896SBill Paul 	/*
2501d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2502d375e524SGleb Smirnoff 	 * to hardware bugs.
2503d375e524SGleb Smirnoff 	 */
2504d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2505d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2506d375e524SGleb Smirnoff 		ifp->if_capenable &= IFCAP_HWCSUM;
2507d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2508d375e524SGleb Smirnoff 	}
2509d375e524SGleb Smirnoff 
2510d375e524SGleb Smirnoff 	/*
2511a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
251241abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
251341abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
251441abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
251541abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
251641abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
251741abcc1bSPaul Saab 	 * SK-9D41.
2518a1d52896SBill Paul 	 */
251941abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
252041abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
252108013fd3SMarius Strobl 	else if (sc->bge_flags & BGE_FLAG_EEPROM) {
2522f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2523f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2524fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2525f6789fbaSPyun YongHyeon 			error = ENXIO;
2526f6789fbaSPyun YongHyeon 			goto fail;
2527f6789fbaSPyun YongHyeon 		}
252841abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
252941abcc1bSPaul Saab 	}
253041abcc1bSPaul Saab 
253141abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2532652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
2533a1d52896SBill Paul 
253495d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
25350c8aa4eaSJung-uk Kim 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
2536652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
253795d67482SBill Paul 
2538652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
25390c8aa4eaSJung-uk Kim 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
25400c8aa4eaSJung-uk Kim 		    bge_ifmedia_sts);
25410c8aa4eaSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
25426098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
25436098821cSJung-uk Kim 		    0, NULL);
254495d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
254595d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
2546da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
254795d67482SBill Paul 	} else {
254895d67482SBill Paul 		/*
25498cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
25508cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
25518cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
25528cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
25538cb1383cSDoug Ambrisko 		 * the PHY.
255495d67482SBill Paul 		 */
25558cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
25568cb1383cSDoug Ambrisko again:
25578cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
25588cb1383cSDoug Ambrisko 
25598cb1383cSDoug Ambrisko 		trys = 0;
256095d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
256195d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
25628cb1383cSDoug Ambrisko 			if (trys++ < 4) {
25638cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
25644e35d186SJung-uk Kim 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR,
25654e35d186SJung-uk Kim 				    BMCR_RESET);
25668cb1383cSDoug Ambrisko 				goto again;
25678cb1383cSDoug Ambrisko 			}
25688cb1383cSDoug Ambrisko 
2569fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
257095d67482SBill Paul 			error = ENXIO;
257195d67482SBill Paul 			goto fail;
257295d67482SBill Paul 		}
25738cb1383cSDoug Ambrisko 
25748cb1383cSDoug Ambrisko 		/*
25758cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
25768cb1383cSDoug Ambrisko 		 */
25778cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
25788cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
257995d67482SBill Paul 	}
258095d67482SBill Paul 
258195d67482SBill Paul 	/*
2582e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2583e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2584e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2585e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2586e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2587e255b776SJohn Polstra 	 * payloads by copying the received packets.
2588e255b776SJohn Polstra 	 */
2589652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
2590652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
2591652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
2592e255b776SJohn Polstra 
2593e255b776SJohn Polstra 	/*
259495d67482SBill Paul 	 * Call MI attach routine.
259595d67482SBill Paul 	 */
2596fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
2597b74e67fbSGleb Smirnoff 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
25980f9bd73bSSam Leffler 
25990f9bd73bSSam Leffler 	/*
26000f9bd73bSSam Leffler 	 * Hookup IRQ last.
26010f9bd73bSSam Leffler 	 */
26024e35d186SJung-uk Kim #if __FreeBSD_version > 700030
26030f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
2604ef544f63SPaolo Pisati 	   NULL, bge_intr, sc, &sc->bge_intrhand);
26054e35d186SJung-uk Kim #else
26064e35d186SJung-uk Kim 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
26074e35d186SJung-uk Kim 	   bge_intr, sc, &sc->bge_intrhand);
26084e35d186SJung-uk Kim #endif
26090f9bd73bSSam Leffler 
26100f9bd73bSSam Leffler 	if (error) {
2611fc74a9f9SBrooks Davis 		bge_detach(dev);
2612fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
26130f9bd73bSSam Leffler 	}
261495d67482SBill Paul 
26156f8718a3SScott Long 	bge_add_sysctls(sc);
26166f8718a3SScott Long 
261708013fd3SMarius Strobl 	return (0);
261808013fd3SMarius Strobl 
261995d67482SBill Paul fail:
262008013fd3SMarius Strobl 	bge_release_resources(sc);
262108013fd3SMarius Strobl 
262295d67482SBill Paul 	return (error);
262395d67482SBill Paul }
262495d67482SBill Paul 
262595d67482SBill Paul static int
26263f74909aSGleb Smirnoff bge_detach(device_t dev)
262795d67482SBill Paul {
262895d67482SBill Paul 	struct bge_softc *sc;
262995d67482SBill Paul 	struct ifnet *ifp;
263095d67482SBill Paul 
263195d67482SBill Paul 	sc = device_get_softc(dev);
2632fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
263395d67482SBill Paul 
263475719184SGleb Smirnoff #ifdef DEVICE_POLLING
263575719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
263675719184SGleb Smirnoff 		ether_poll_deregister(ifp);
263775719184SGleb Smirnoff #endif
263875719184SGleb Smirnoff 
26390f9bd73bSSam Leffler 	BGE_LOCK(sc);
264095d67482SBill Paul 	bge_stop(sc);
264195d67482SBill Paul 	bge_reset(sc);
26420f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
26430f9bd73bSSam Leffler 
26445dda8085SOleg Bulyzhin 	callout_drain(&sc->bge_stat_ch);
26455dda8085SOleg Bulyzhin 
26460f9bd73bSSam Leffler 	ether_ifdetach(ifp);
264795d67482SBill Paul 
2648652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
264995d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
265095d67482SBill Paul 	} else {
265195d67482SBill Paul 		bus_generic_detach(dev);
265295d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
265395d67482SBill Paul 	}
265495d67482SBill Paul 
265595d67482SBill Paul 	bge_release_resources(sc);
265695d67482SBill Paul 
265795d67482SBill Paul 	return (0);
265895d67482SBill Paul }
265995d67482SBill Paul 
266095d67482SBill Paul static void
26613f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
266295d67482SBill Paul {
266395d67482SBill Paul 	device_t dev;
266495d67482SBill Paul 
266595d67482SBill Paul 	dev = sc->bge_dev;
266695d67482SBill Paul 
266795d67482SBill Paul 	if (sc->bge_intrhand != NULL)
266895d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
266995d67482SBill Paul 
267095d67482SBill Paul 	if (sc->bge_irq != NULL)
2671724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
2672724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
2673724bd939SJohn Polstra 
26740a55a034SJung-uk Kim #if __FreeBSD_version > 602105
2675724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
2676724bd939SJohn Polstra 		pci_release_msi(dev);
26774e35d186SJung-uk Kim #endif
267895d67482SBill Paul 
267995d67482SBill Paul 	if (sc->bge_res != NULL)
268095d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
268195d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
268295d67482SBill Paul 
2683ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2684ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2685ad61f896SRuslan Ermilov 
2686f41ac2beSBill Paul 	bge_dma_free(sc);
268795d67482SBill Paul 
26880f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
26890f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
269095d67482SBill Paul }
269195d67482SBill Paul 
26928cb1383cSDoug Ambrisko static int
26933f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
269495d67482SBill Paul {
269595d67482SBill Paul 	device_t dev;
26963f74909aSGleb Smirnoff 	uint32_t cachesize, command, pcistate, reset;
26976f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
269895d67482SBill Paul 	int i, val = 0;
269995d67482SBill Paul 
270095d67482SBill Paul 	dev = sc->bge_dev;
270195d67482SBill Paul 
2702464223f7SJung-uk Kim 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc)) {
27036f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
27046f8718a3SScott Long 			write_op = bge_writemem_direct;
27056f8718a3SScott Long 		else
27066f8718a3SScott Long 			write_op = bge_writemem_ind;
27079ba784dbSScott Long 	} else
27086f8718a3SScott Long 		write_op = bge_writereg_ind;
27096f8718a3SScott Long 
271095d67482SBill Paul 	/* Save some important PCI state. */
271195d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
271295d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
271395d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
271495d67482SBill Paul 
271595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
271695d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
2717e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
271895d67482SBill Paul 
27196f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
27206f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
27216f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
27226f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
27236f8718a3SScott Long 		if (bootverbose)
27249ba784dbSScott Long 			device_printf(sc->bge_dev, "Disabling fastboot\n");
27256f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
27266f8718a3SScott Long 	}
27276f8718a3SScott Long 
27286f8718a3SScott Long 	/*
27296f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
27306f8718a3SScott Long 	 * When firmware finishes its initialization it will
27316f8718a3SScott Long 	 * write ~BGE_MAGIC_NUMBER to the same location.
27326f8718a3SScott Long 	 */
27336f8718a3SScott Long 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
27346f8718a3SScott Long 
27350c8aa4eaSJung-uk Kim 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
2736e53d81eeSPaul Saab 
2737e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2738652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
27390c8aa4eaSJung-uk Kim 		if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
27400c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, 0x7E2C, 0x20);
2741e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2742e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
27430c8aa4eaSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
27440c8aa4eaSJung-uk Kim 			reset |= 1 << 29;
2745e53d81eeSPaul Saab 		}
2746e53d81eeSPaul Saab 	}
2747e53d81eeSPaul Saab 
274821c9e407SDavid Christensen 	/*
27496f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
27506f8718a3SScott Long 	 * powered up in D0 uninitialized.
27516f8718a3SScott Long 	 */
27525345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
27536f8718a3SScott Long 		reset |= 0x04000000;
27546f8718a3SScott Long 
275595d67482SBill Paul 	/* Issue global reset */
27566f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
275795d67482SBill Paul 
275895d67482SBill Paul 	DELAY(1000);
275995d67482SBill Paul 
2760e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2761652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2762e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2763e53d81eeSPaul Saab 			uint32_t v;
2764e53d81eeSPaul Saab 
2765e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
27660c8aa4eaSJung-uk Kim 			v = pci_read_config(dev, 0xC4, 4);
27670c8aa4eaSJung-uk Kim 			pci_write_config(dev, 0xC4, v | (1 << 15), 4);
2768e53d81eeSPaul Saab 		}
27699ba784dbSScott Long 		/*
27709ba784dbSScott Long 		 * Set PCIE max payload size to 128 bytes and clear error
27719ba784dbSScott Long 		 * status.
27729ba784dbSScott Long 		 */
27730c8aa4eaSJung-uk Kim 		pci_write_config(dev, 0xD8, 0xF5000, 4);
2774e53d81eeSPaul Saab 	}
2775e53d81eeSPaul Saab 
27763f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
277795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
277895d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
2779e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
278095d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
278195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
27820c8aa4eaSJung-uk Kim 	write_op(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
278395d67482SBill Paul 
2784bf6ef57aSJohn Polstra 	/* Re-enable MSI, if neccesary, and enable the memory arbiter. */
27854c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
27864c0da0ffSGleb Smirnoff 		uint32_t val;
27874c0da0ffSGleb Smirnoff 
2788bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
2789bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
2790bf6ef57aSJohn Polstra 			val = pci_read_config(dev, BGE_PCI_MSI_CTL, 2);
2791bf6ef57aSJohn Polstra 			pci_write_config(dev, BGE_PCI_MSI_CTL,
2792bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
2793bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
2794bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
2795bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
2796bf6ef57aSJohn Polstra 		}
27974c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
27984c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
27994c0da0ffSGleb Smirnoff 	} else
2800a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2801a7b0c314SPaul Saab 
280295d67482SBill Paul 	/*
28036f8718a3SScott Long 	 * Poll until we see the 1's complement of the magic number.
280408013fd3SMarius Strobl 	 * This indicates that the firmware initialization is complete.
280508013fd3SMarius Strobl 	 * We expect this to fail if no EEPROM is fitted though.
280695d67482SBill Paul 	 */
280795d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
2808d5d23857SJung-uk Kim 		DELAY(10);
280995d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
281095d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
281195d67482SBill Paul 			break;
281295d67482SBill Paul 	}
281395d67482SBill Paul 
281408013fd3SMarius Strobl 	if ((sc->bge_flags & BGE_FLAG_EEPROM) && i == BGE_TIMEOUT)
28159ba784dbSScott Long 		device_printf(sc->bge_dev, "firmware handshake timed out, "
28169ba784dbSScott Long 		    "found 0x%08x\n", val);
281795d67482SBill Paul 
281895d67482SBill Paul 	/*
281995d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
282095d67482SBill Paul 	 * return to its original pre-reset state. This is a
282195d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
282295d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
282395d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
282495d67482SBill Paul 	 * results.
282595d67482SBill Paul 	 */
282695d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
282795d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
282895d67482SBill Paul 			break;
282995d67482SBill Paul 		DELAY(10);
283095d67482SBill Paul 	}
283195d67482SBill Paul 
28326f8718a3SScott Long 	if (sc->bge_flags & BGE_FLAG_PCIE) {
28330c8aa4eaSJung-uk Kim 		reset = bge_readmem_ind(sc, 0x7C00);
28340c8aa4eaSJung-uk Kim 		bge_writemem_ind(sc, 0x7C00, reset | (1 << 25));
28356f8718a3SScott Long 	}
28366f8718a3SScott Long 
28373f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
2838e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
283995d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
284095d67482SBill Paul 
28418cb1383cSDoug Ambrisko 	/* Tell the ASF firmware we are up */
28428cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
28438cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
28448cb1383cSDoug Ambrisko 
284595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
284695d67482SBill Paul 
2847da3003f0SBill Paul 	/*
2848da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
2849da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
2850da3003f0SBill Paul 	 * to 1.2V.
2851da3003f0SBill Paul 	 */
2852652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
2853652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
2854da3003f0SBill Paul 		uint32_t serdescfg;
2855652ae483SGleb Smirnoff 
2856da3003f0SBill Paul 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
28570c8aa4eaSJung-uk Kim 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
2858da3003f0SBill Paul 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2859da3003f0SBill Paul 	}
2860da3003f0SBill Paul 
2861e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2862652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
2863652ae483SGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2864e53d81eeSPaul Saab 		uint32_t v;
2865e53d81eeSPaul Saab 
28660c8aa4eaSJung-uk Kim 		v = CSR_READ_4(sc, 0x7C00);
28670c8aa4eaSJung-uk Kim 		CSR_WRITE_4(sc, 0x7C00, v | (1 << 25));
2868e53d81eeSPaul Saab 	}
286995d67482SBill Paul 	DELAY(10000);
28708cb1383cSDoug Ambrisko 
28718cb1383cSDoug Ambrisko 	return(0);
287295d67482SBill Paul }
287395d67482SBill Paul 
287495d67482SBill Paul /*
287595d67482SBill Paul  * Frame reception handling. This is called if there's a frame
287695d67482SBill Paul  * on the receive return list.
287795d67482SBill Paul  *
287895d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
28791be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
288095d67482SBill Paul  * 2) the frame is from the standard receive ring
288195d67482SBill Paul  */
288295d67482SBill Paul 
288395d67482SBill Paul static void
28843f74909aSGleb Smirnoff bge_rxeof(struct bge_softc *sc)
288595d67482SBill Paul {
288695d67482SBill Paul 	struct ifnet *ifp;
288795d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
288895d67482SBill Paul 
28890f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
28900f9bd73bSSam Leffler 
28913f74909aSGleb Smirnoff 	/* Nothing to do. */
2892cfcb5025SOleg Bulyzhin 	if (sc->bge_rx_saved_considx ==
2893cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx)
2894cfcb5025SOleg Bulyzhin 		return;
2895cfcb5025SOleg Bulyzhin 
2896fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
289795d67482SBill Paul 
2898f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2899e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
2900f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2901f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
29024c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
2903f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
29044c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTREAD);
2905f41ac2beSBill Paul 
290695d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2907f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
290895d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
29093f74909aSGleb Smirnoff 		uint32_t		rxidx;
291095d67482SBill Paul 		struct mbuf		*m = NULL;
29113f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
291295d67482SBill Paul 		int			have_tag = 0;
291395d67482SBill Paul 
291475719184SGleb Smirnoff #ifdef DEVICE_POLLING
291575719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
291675719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
291775719184SGleb Smirnoff 				break;
291875719184SGleb Smirnoff 			sc->rxcycles--;
291975719184SGleb Smirnoff 		}
292075719184SGleb Smirnoff #endif
292175719184SGleb Smirnoff 
292295d67482SBill Paul 		cur_rx =
2923f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
292495d67482SBill Paul 
292595d67482SBill Paul 		rxidx = cur_rx->bge_idx;
29260434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
292795d67482SBill Paul 
2928cb2eacc7SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
2929cb2eacc7SYaroslav Tykhiy 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
293095d67482SBill Paul 			have_tag = 1;
293195d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
293295d67482SBill Paul 		}
293395d67482SBill Paul 
293495d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
293595d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2936f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2937f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2938f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2939f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2940f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
294195d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
294295d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
294395d67482SBill Paul 			jumbocnt++;
294495d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
294595d67482SBill Paul 				ifp->if_ierrors++;
294695d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
294795d67482SBill Paul 				continue;
294895d67482SBill Paul 			}
294995d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
295095d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
295195d67482SBill Paul 				ifp->if_ierrors++;
295295d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
295395d67482SBill Paul 				continue;
295495d67482SBill Paul 			}
295595d67482SBill Paul 		} else {
295695d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2957f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2958f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2959f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2960f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2961f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
296295d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
296395d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
296495d67482SBill Paul 			stdcnt++;
296595d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
296695d67482SBill Paul 				ifp->if_ierrors++;
296795d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
296895d67482SBill Paul 				continue;
296995d67482SBill Paul 			}
297095d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
297195d67482SBill Paul 			    NULL) == ENOBUFS) {
297295d67482SBill Paul 				ifp->if_ierrors++;
297395d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
297495d67482SBill Paul 				continue;
297595d67482SBill Paul 			}
297695d67482SBill Paul 		}
297795d67482SBill Paul 
297895d67482SBill Paul 		ifp->if_ipackets++;
2979e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2980e255b776SJohn Polstra 		/*
2981e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
2982e65bed95SPyun YongHyeon 		 * the payload is aligned.
2983e255b776SJohn Polstra 		 */
2984652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
2985e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2986e255b776SJohn Polstra 			    cur_rx->bge_len);
2987e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2988e255b776SJohn Polstra 		}
2989e255b776SJohn Polstra #endif
2990473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
299195d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
299295d67482SBill Paul 
2993b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
299478178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
299595d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
29960c8aa4eaSJung-uk Kim 				if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
29970c8aa4eaSJung-uk Kim 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
299878178cd1SGleb Smirnoff 			}
2999d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
3000d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
300195d67482SBill Paul 				m->m_pkthdr.csum_data =
300295d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
3003ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
3004ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
300595d67482SBill Paul 			}
300695d67482SBill Paul 		}
300795d67482SBill Paul 
300895d67482SBill Paul 		/*
3009673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
3010673d9191SSam Leffler 		 * attach that information to the packet.
301195d67482SBill Paul 		 */
3012d147662cSGleb Smirnoff 		if (have_tag) {
30134e35d186SJung-uk Kim #if __FreeBSD_version > 700022
301478ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
301578ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
30164e35d186SJung-uk Kim #else
30174e35d186SJung-uk Kim 			VLAN_INPUT_TAG_NEW(ifp, m, vlan_tag);
30184e35d186SJung-uk Kim 			if (m == NULL)
30194e35d186SJung-uk Kim 				continue;
30204e35d186SJung-uk Kim #endif
3021d147662cSGleb Smirnoff 		}
302295d67482SBill Paul 
30230f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
3024673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
30250f9bd73bSSam Leffler 		BGE_LOCK(sc);
302695d67482SBill Paul 	}
302795d67482SBill Paul 
3028e65bed95SPyun YongHyeon 	if (stdcnt > 0)
3029f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
3030e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
30314c0da0ffSGleb Smirnoff 
30324c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc) && jumbocnt > 0)
3033f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
30344c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
3035f41ac2beSBill Paul 
303695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
303795d67482SBill Paul 	if (stdcnt)
303895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
303995d67482SBill Paul 	if (jumbocnt)
304095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
30416b037352SJung-uk Kim #ifdef notyet
30426b037352SJung-uk Kim 	/*
30436b037352SJung-uk Kim 	 * This register wraps very quickly under heavy packet drops.
30446b037352SJung-uk Kim 	 * If you need correct statistics, you can enable this check.
30456b037352SJung-uk Kim 	 */
30466b037352SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
30476b037352SJung-uk Kim 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
30486b037352SJung-uk Kim #endif
304995d67482SBill Paul }
305095d67482SBill Paul 
305195d67482SBill Paul static void
30523f74909aSGleb Smirnoff bge_txeof(struct bge_softc *sc)
305395d67482SBill Paul {
305495d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
305595d67482SBill Paul 	struct ifnet *ifp;
305695d67482SBill Paul 
30570f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
30580f9bd73bSSam Leffler 
30593f74909aSGleb Smirnoff 	/* Nothing to do. */
3060cfcb5025SOleg Bulyzhin 	if (sc->bge_tx_saved_considx ==
3061cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx)
3062cfcb5025SOleg Bulyzhin 		return;
3063cfcb5025SOleg Bulyzhin 
3064fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
306595d67482SBill Paul 
3066e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
3067e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
3068e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
306995d67482SBill Paul 	/*
307095d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
307195d67482SBill Paul 	 * frames that have been sent.
307295d67482SBill Paul 	 */
307395d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
3074f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
30753f74909aSGleb Smirnoff 		uint32_t		idx = 0;
307695d67482SBill Paul 
307795d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
3078f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
307995d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
308095d67482SBill Paul 			ifp->if_opackets++;
308195d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
3082e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
3083e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
3084e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
3085f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
3086f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
3087e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
3088e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
308995d67482SBill Paul 		}
309095d67482SBill Paul 		sc->bge_txcnt--;
309195d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
309295d67482SBill Paul 	}
309395d67482SBill Paul 
309495d67482SBill Paul 	if (cur_tx != NULL)
309513f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
30965b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
30975b01e77cSBruce Evans 		sc->bge_timer = 0;
309895d67482SBill Paul }
309995d67482SBill Paul 
310075719184SGleb Smirnoff #ifdef DEVICE_POLLING
310175719184SGleb Smirnoff static void
310275719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
310375719184SGleb Smirnoff {
310475719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
3105366454f2SOleg Bulyzhin 	uint32_t statusword;
310675719184SGleb Smirnoff 
31073f74909aSGleb Smirnoff 	BGE_LOCK(sc);
31083f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
31093f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
31103f74909aSGleb Smirnoff 		return;
31113f74909aSGleb Smirnoff 	}
311275719184SGleb Smirnoff 
3113dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3114e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
3115dab5cd05SOleg Bulyzhin 
31163f74909aSGleb Smirnoff 	statusword = atomic_readandclear_32(
31173f74909aSGleb Smirnoff 	    &sc->bge_ldata.bge_status_block->bge_status);
3118dab5cd05SOleg Bulyzhin 
3119dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3120e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
3121366454f2SOleg Bulyzhin 
31220c8aa4eaSJung-uk Kim 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
3123366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
3124366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
3125366454f2SOleg Bulyzhin 
3126366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
3127366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
31284c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3129652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
3130366454f2SOleg Bulyzhin 			bge_link_upd(sc);
3131366454f2SOleg Bulyzhin 
3132366454f2SOleg Bulyzhin 	sc->rxcycles = count;
3133366454f2SOleg Bulyzhin 	bge_rxeof(sc);
3134366454f2SOleg Bulyzhin 	bge_txeof(sc);
3135366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3136366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
31373f74909aSGleb Smirnoff 
31383f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
313975719184SGleb Smirnoff }
314075719184SGleb Smirnoff #endif /* DEVICE_POLLING */
314175719184SGleb Smirnoff 
314295d67482SBill Paul static void
31433f74909aSGleb Smirnoff bge_intr(void *xsc)
314495d67482SBill Paul {
314595d67482SBill Paul 	struct bge_softc *sc;
314695d67482SBill Paul 	struct ifnet *ifp;
3147dab5cd05SOleg Bulyzhin 	uint32_t statusword;
314895d67482SBill Paul 
314995d67482SBill Paul 	sc = xsc;
3150f41ac2beSBill Paul 
31510f9bd73bSSam Leffler 	BGE_LOCK(sc);
31520f9bd73bSSam Leffler 
3153dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
3154dab5cd05SOleg Bulyzhin 
315575719184SGleb Smirnoff #ifdef DEVICE_POLLING
315675719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
315775719184SGleb Smirnoff 		BGE_UNLOCK(sc);
315875719184SGleb Smirnoff 		return;
315975719184SGleb Smirnoff 	}
316075719184SGleb Smirnoff #endif
316175719184SGleb Smirnoff 
3162f30cbfc6SScott Long 	/*
3163b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
3164b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
3165b848e032SBruce Evans 	 * our current organization this just gives complications and
3166b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
3167b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
3168b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
3169b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
3170b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
3171b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
3172b848e032SBruce Evans 	 *
3173b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
3174b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
3175b848e032SBruce Evans 	 * changing later because it is more efficient to get another
3176b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
3177b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
3178b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
3179b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
3180b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
3181b848e032SBruce Evans 	 */
3182b848e032SBruce Evans 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
3183b848e032SBruce Evans 
3184b848e032SBruce Evans 	/*
3185f30cbfc6SScott Long 	 * Do the mandatory PCI flush as well as get the link status.
3186f30cbfc6SScott Long 	 */
3187f30cbfc6SScott Long 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
3188f41ac2beSBill Paul 
3189f30cbfc6SScott Long 	/* Make sure the descriptor ring indexes are coherent. */
3190f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3191f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
3192f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3193f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
3194f30cbfc6SScott Long 
31951f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
31964c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3197f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
3198dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
319995d67482SBill Paul 
320013f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
32013f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
320295d67482SBill Paul 		bge_rxeof(sc);
320395d67482SBill Paul 
32043f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
320595d67482SBill Paul 		bge_txeof(sc);
320695d67482SBill Paul 	}
320795d67482SBill Paul 
320813f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
320913f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
32100f9bd73bSSam Leffler 		bge_start_locked(ifp);
32110f9bd73bSSam Leffler 
32120f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
321395d67482SBill Paul }
321495d67482SBill Paul 
321595d67482SBill Paul static void
32168cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
32178cb1383cSDoug Ambrisko {
32188cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
32198cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
32208cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
32218cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
32228cb1383cSDoug Ambrisko 		else {
32238cb1383cSDoug Ambrisko 			sc->bge_asf_count = 5;
32248cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW,
32258cb1383cSDoug Ambrisko 			    BGE_FW_DRV_ALIVE);
32268cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_LEN, 4);
32278cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_DATA, 3);
32288cb1383cSDoug Ambrisko 			CSR_WRITE_4(sc, BGE_CPU_EVENT,
322939153c5aSJung-uk Kim 			    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
32308cb1383cSDoug Ambrisko 		}
32318cb1383cSDoug Ambrisko 	}
32328cb1383cSDoug Ambrisko }
32338cb1383cSDoug Ambrisko 
32348cb1383cSDoug Ambrisko static void
3235b74e67fbSGleb Smirnoff bge_tick(void *xsc)
32360f9bd73bSSam Leffler {
3237b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
323895d67482SBill Paul 	struct mii_data *mii = NULL;
323995d67482SBill Paul 
32400f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
324195d67482SBill Paul 
32425dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
32435dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
32445dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
32455dda8085SOleg Bulyzhin 	    	return;
32465dda8085SOleg Bulyzhin 
32477ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
32480434d1b8SBill Paul 		bge_stats_update_regs(sc);
32490434d1b8SBill Paul 	else
325095d67482SBill Paul 		bge_stats_update(sc);
325195d67482SBill Paul 
3252652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
325395d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
32548cb1383cSDoug Ambrisko 		/* Don't mess with the PHY in IPMI/ASF mode */
32558cb1383cSDoug Ambrisko 		if (!((sc->bge_asf_mode & ASF_STACKUP) && (sc->bge_link)))
325695d67482SBill Paul 			mii_tick(mii);
32577b97099dSOleg Bulyzhin 	} else {
32587b97099dSOleg Bulyzhin 		/*
32597b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
32607b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
32617b97099dSOleg Bulyzhin 		 * and trigger interrupt.
32627b97099dSOleg Bulyzhin 		 */
32637b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
32643f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
32657b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
32667b97099dSOleg Bulyzhin #endif
32677b97099dSOleg Bulyzhin 		{
32687b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
32697b97099dSOleg Bulyzhin 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
32707b97099dSOleg Bulyzhin 		}
3271dab5cd05SOleg Bulyzhin 	}
327295d67482SBill Paul 
32738cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
3274b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
32758cb1383cSDoug Ambrisko 
3276dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
327795d67482SBill Paul }
327895d67482SBill Paul 
327995d67482SBill Paul static void
32803f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
32810434d1b8SBill Paul {
32823f74909aSGleb Smirnoff 	struct ifnet *ifp;
32830434d1b8SBill Paul 
3284fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
32850434d1b8SBill Paul 
32866b037352SJung-uk Kim 	ifp->if_collisions += CSR_READ_4(sc, BGE_MAC_STATS +
32877e6e2507SJung-uk Kim 	    offsetof(struct bge_mac_stats_regs, etherStatsCollisions));
32887e6e2507SJung-uk Kim 
32896b037352SJung-uk Kim 	ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
32900434d1b8SBill Paul }
32910434d1b8SBill Paul 
32920434d1b8SBill Paul static void
32933f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
329495d67482SBill Paul {
329595d67482SBill Paul 	struct ifnet *ifp;
3296e907febfSPyun YongHyeon 	bus_size_t stats;
32977e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
329895d67482SBill Paul 
3299fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
330095d67482SBill Paul 
3301e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
3302e907febfSPyun YongHyeon 
3303e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
3304e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
330595d67482SBill Paul 
33068634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
33076b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
33086fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
33096fb34dd2SOleg Bulyzhin 
33106fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
33116b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
33126fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
33136fb34dd2SOleg Bulyzhin 
33146fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
33156b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
33166fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
331795d67482SBill Paul 
3318e907febfSPyun YongHyeon #undef	READ_STAT
331995d67482SBill Paul }
332095d67482SBill Paul 
332195d67482SBill Paul /*
3322d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
3323d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
3324d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
3325d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
3326d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
3327d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
3328d375e524SGleb Smirnoff  */
3329d375e524SGleb Smirnoff static __inline int
3330d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
3331d375e524SGleb Smirnoff {
3332d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
3333d375e524SGleb Smirnoff 	struct mbuf *last;
3334d375e524SGleb Smirnoff 
3335d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
3336d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
3337d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
3338d375e524SGleb Smirnoff 		last = m;
3339d375e524SGleb Smirnoff 	} else {
3340d375e524SGleb Smirnoff 		/*
3341d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
3342d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
3343d375e524SGleb Smirnoff 		 */
3344d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
3345d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
3346d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
3347d375e524SGleb Smirnoff 			struct mbuf *n;
3348d375e524SGleb Smirnoff 
3349d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
3350d375e524SGleb Smirnoff 			if (n == NULL)
3351d375e524SGleb Smirnoff 				return (ENOBUFS);
3352d375e524SGleb Smirnoff 			n->m_len = 0;
3353d375e524SGleb Smirnoff 			last->m_next = n;
3354d375e524SGleb Smirnoff 			last = n;
3355d375e524SGleb Smirnoff 		}
3356d375e524SGleb Smirnoff 	}
3357d375e524SGleb Smirnoff 
3358d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
3359d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
3360d375e524SGleb Smirnoff 	last->m_len += padlen;
3361d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
3362d375e524SGleb Smirnoff 
3363d375e524SGleb Smirnoff 	return (0);
3364d375e524SGleb Smirnoff }
3365d375e524SGleb Smirnoff 
3366d375e524SGleb Smirnoff /*
336795d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
336895d67482SBill Paul  * pointers to descriptors.
336995d67482SBill Paul  */
337095d67482SBill Paul static int
3371676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
337295d67482SBill Paul {
33737e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
3374f41ac2beSBill Paul 	bus_dmamap_t		map;
3375676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
3376676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
33777e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
3378676ad2c9SGleb Smirnoff 	uint16_t		csum_flags;
33797e27542aSGleb Smirnoff 	int			nsegs, i, error;
338095d67482SBill Paul 
33816909dc43SGleb Smirnoff 	csum_flags = 0;
33826909dc43SGleb Smirnoff 	if (m->m_pkthdr.csum_flags) {
33836909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
33846909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
33856909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
33866909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
33876909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
33886909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
33896909dc43SGleb Smirnoff 				m_freem(m);
33906909dc43SGleb Smirnoff 				*m_head = NULL;
33916909dc43SGleb Smirnoff 				return (error);
33926909dc43SGleb Smirnoff 			}
33936909dc43SGleb Smirnoff 		}
33946909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
33956909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
33966909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
33976909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
33986909dc43SGleb Smirnoff 	}
33996909dc43SGleb Smirnoff 
34007e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
3401676ad2c9SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m, segs,
3402676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
34037e27542aSGleb Smirnoff 	if (error == EFBIG) {
3404676ad2c9SGleb Smirnoff 		m = m_defrag(m, M_DONTWAIT);
3405676ad2c9SGleb Smirnoff 		if (m == NULL) {
3406676ad2c9SGleb Smirnoff 			m_freem(*m_head);
3407676ad2c9SGleb Smirnoff 			*m_head = NULL;
34087e27542aSGleb Smirnoff 			return (ENOBUFS);
34097e27542aSGleb Smirnoff 		}
3410676ad2c9SGleb Smirnoff 		*m_head = m;
3411676ad2c9SGleb Smirnoff 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m,
3412676ad2c9SGleb Smirnoff 		    segs, &nsegs, BUS_DMA_NOWAIT);
3413676ad2c9SGleb Smirnoff 		if (error) {
3414676ad2c9SGleb Smirnoff 			m_freem(m);
3415676ad2c9SGleb Smirnoff 			*m_head = NULL;
34167e27542aSGleb Smirnoff 			return (error);
34177e27542aSGleb Smirnoff 		}
3418676ad2c9SGleb Smirnoff 	} else if (error != 0)
3419676ad2c9SGleb Smirnoff 		return (error);
34207e27542aSGleb Smirnoff 
342195d67482SBill Paul 	/*
342295d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
342395d67482SBill Paul 	 * of the end of the ring.
342495d67482SBill Paul 	 */
34257e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
34267e27542aSGleb Smirnoff 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
342795d67482SBill Paul 		return (ENOBUFS);
34287e27542aSGleb Smirnoff 	}
34297e27542aSGleb Smirnoff 
3430e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
3431e65bed95SPyun YongHyeon 
34327e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
34337e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
34347e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
34357e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
34367e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
34377e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
34387e27542aSGleb Smirnoff 		if (i == nsegs - 1)
34397e27542aSGleb Smirnoff 			break;
34407e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
34417e27542aSGleb Smirnoff 	}
34427e27542aSGleb Smirnoff 
34437e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
34447e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
3445676ad2c9SGleb Smirnoff 
34467e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
34477e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
34484e35d186SJung-uk Kim #if __FreeBSD_version > 700022
344978ba57b9SAndre Oppermann 	if (m->m_flags & M_VLANTAG) {
34507e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
345178ba57b9SAndre Oppermann 		d->bge_vlan_tag = m->m_pkthdr.ether_vtag;
34527e27542aSGleb Smirnoff 	} else
34537e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
34544e35d186SJung-uk Kim #else
34554e35d186SJung-uk Kim 	{
34564e35d186SJung-uk Kim 		struct m_tag		*mtag;
34574e35d186SJung-uk Kim 
34584e35d186SJung-uk Kim 		if ((mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m)) != NULL) {
34594e35d186SJung-uk Kim 			d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
34604e35d186SJung-uk Kim 			d->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
34614e35d186SJung-uk Kim 		} else
34624e35d186SJung-uk Kim 			d->bge_vlan_tag = 0;
34634e35d186SJung-uk Kim 	}
34644e35d186SJung-uk Kim #endif
3465f41ac2beSBill Paul 
3466f41ac2beSBill Paul 	/*
3467f41ac2beSBill Paul 	 * Insure that the map for this transmission
3468f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
3469f41ac2beSBill Paul 	 * in this chain.
3470f41ac2beSBill Paul 	 */
34717e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
34727e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
3473676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
34747e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
347595d67482SBill Paul 
34767e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
34777e27542aSGleb Smirnoff 	*txidx = idx;
347895d67482SBill Paul 
347995d67482SBill Paul 	return (0);
348095d67482SBill Paul }
348195d67482SBill Paul 
348295d67482SBill Paul /*
348395d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
348495d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
348595d67482SBill Paul  */
348695d67482SBill Paul static void
34873f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
348895d67482SBill Paul {
348995d67482SBill Paul 	struct bge_softc *sc;
349095d67482SBill Paul 	struct mbuf *m_head = NULL;
349114bbd30fSGleb Smirnoff 	uint32_t prodidx;
3492303a718cSDag-Erling Smørgrav 	int count = 0;
349395d67482SBill Paul 
349495d67482SBill Paul 	sc = ifp->if_softc;
349595d67482SBill Paul 
3496dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
349795d67482SBill Paul 		return;
349895d67482SBill Paul 
349914bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
350095d67482SBill Paul 
350195d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
35024d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
350395d67482SBill Paul 		if (m_head == NULL)
350495d67482SBill Paul 			break;
350595d67482SBill Paul 
350695d67482SBill Paul 		/*
350795d67482SBill Paul 		 * XXX
3508b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
3509b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3510b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
3511b874fdd4SYaroslav Tykhiy 		 *
3512b874fdd4SYaroslav Tykhiy 		 * XXX
351395d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
351495d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
351595d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
351695d67482SBill Paul 		 * chain at once.
351795d67482SBill Paul 		 * (paranoia -- may not actually be needed)
351895d67482SBill Paul 		 */
351995d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
352095d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
352195d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
352295d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
35234d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
352413f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
352595d67482SBill Paul 				break;
352695d67482SBill Paul 			}
352795d67482SBill Paul 		}
352895d67482SBill Paul 
352995d67482SBill Paul 		/*
353095d67482SBill Paul 		 * Pack the data into the transmit ring. If we
353195d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
353295d67482SBill Paul 		 * for the NIC to drain the ring.
353395d67482SBill Paul 		 */
3534676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
3535676ad2c9SGleb Smirnoff 			if (m_head == NULL)
3536676ad2c9SGleb Smirnoff 				break;
35374d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
353813f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
353995d67482SBill Paul 			break;
354095d67482SBill Paul 		}
3541303a718cSDag-Erling Smørgrav 		++count;
354295d67482SBill Paul 
354395d67482SBill Paul 		/*
354495d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
354595d67482SBill Paul 		 * to him.
354695d67482SBill Paul 		 */
35474e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
354845ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
35494e35d186SJung-uk Kim #else
35504e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
35514e35d186SJung-uk Kim #endif
355295d67482SBill Paul 	}
355395d67482SBill Paul 
35543f74909aSGleb Smirnoff 	if (count == 0)
35553f74909aSGleb Smirnoff 		/* No packets were dequeued. */
3556303a718cSDag-Erling Smørgrav 		return;
3557303a718cSDag-Erling Smørgrav 
35583f74909aSGleb Smirnoff 	/* Transmit. */
355995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
35603927098fSPaul Saab 	/* 5700 b2 errata */
3561e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
35623927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
356395d67482SBill Paul 
356414bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
356514bbd30fSGleb Smirnoff 
356695d67482SBill Paul 	/*
356795d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
356895d67482SBill Paul 	 */
3569b74e67fbSGleb Smirnoff 	sc->bge_timer = 5;
357095d67482SBill Paul }
357195d67482SBill Paul 
35720f9bd73bSSam Leffler /*
35730f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
35740f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
35750f9bd73bSSam Leffler  */
357695d67482SBill Paul static void
35773f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
357895d67482SBill Paul {
35790f9bd73bSSam Leffler 	struct bge_softc *sc;
35800f9bd73bSSam Leffler 
35810f9bd73bSSam Leffler 	sc = ifp->if_softc;
35820f9bd73bSSam Leffler 	BGE_LOCK(sc);
35830f9bd73bSSam Leffler 	bge_start_locked(ifp);
35840f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
35850f9bd73bSSam Leffler }
35860f9bd73bSSam Leffler 
35870f9bd73bSSam Leffler static void
35883f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
35890f9bd73bSSam Leffler {
359095d67482SBill Paul 	struct ifnet *ifp;
35913f74909aSGleb Smirnoff 	uint16_t *m;
359295d67482SBill Paul 
35930f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
359495d67482SBill Paul 
3595fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
359695d67482SBill Paul 
359713f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
359895d67482SBill Paul 		return;
359995d67482SBill Paul 
360095d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
360195d67482SBill Paul 	bge_stop(sc);
36028cb1383cSDoug Ambrisko 
36038cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
36048cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
360595d67482SBill Paul 	bge_reset(sc);
36068cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
36078cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
36088cb1383cSDoug Ambrisko 
360995d67482SBill Paul 	bge_chipinit(sc);
361095d67482SBill Paul 
361195d67482SBill Paul 	/*
361295d67482SBill Paul 	 * Init the various state machines, ring
361395d67482SBill Paul 	 * control blocks and firmware.
361495d67482SBill Paul 	 */
361595d67482SBill Paul 	if (bge_blockinit(sc)) {
3616fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
361795d67482SBill Paul 		return;
361895d67482SBill Paul 	}
361995d67482SBill Paul 
3620fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
362195d67482SBill Paul 
362295d67482SBill Paul 	/* Specify MTU. */
362395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3624cb2eacc7SYaroslav Tykhiy 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
3625cb2eacc7SYaroslav Tykhiy 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
362695d67482SBill Paul 
362795d67482SBill Paul 	/* Load our MAC address. */
36283f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
362995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
363095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
363195d67482SBill Paul 
36323e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
36333e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
363495d67482SBill Paul 
363595d67482SBill Paul 	/* Program multicast filter. */
363695d67482SBill Paul 	bge_setmulti(sc);
363795d67482SBill Paul 
3638cb2eacc7SYaroslav Tykhiy 	/* Program VLAN tag stripping. */
3639cb2eacc7SYaroslav Tykhiy 	bge_setvlan(sc);
3640cb2eacc7SYaroslav Tykhiy 
364195d67482SBill Paul 	/* Init RX ring. */
364295d67482SBill Paul 	bge_init_rx_ring_std(sc);
364395d67482SBill Paul 
36440434d1b8SBill Paul 	/*
36450434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
36460434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
36470434d1b8SBill Paul 	 * entry of the ring.
36480434d1b8SBill Paul 	 */
36490434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
36503f74909aSGleb Smirnoff 		uint32_t		v, i;
36510434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
36520434d1b8SBill Paul 			DELAY(20);
36530434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
36540434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
36550434d1b8SBill Paul 				break;
36560434d1b8SBill Paul 		}
36570434d1b8SBill Paul 		if (i == 10)
3658fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
3659fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
36600434d1b8SBill Paul 	}
36610434d1b8SBill Paul 
366295d67482SBill Paul 	/* Init jumbo RX ring. */
366395d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
366495d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
366595d67482SBill Paul 
36663f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
366795d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
366895d67482SBill Paul 
36697e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
36707e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
36717e6e2507SJung-uk Kim 
367295d67482SBill Paul 	/* Init TX ring. */
367395d67482SBill Paul 	bge_init_tx_ring(sc);
367495d67482SBill Paul 
36753f74909aSGleb Smirnoff 	/* Turn on transmitter. */
367695d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
367795d67482SBill Paul 
36783f74909aSGleb Smirnoff 	/* Turn on receiver. */
367995d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
368095d67482SBill Paul 
368195d67482SBill Paul 	/* Tell firmware we're alive. */
368295d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
368395d67482SBill Paul 
368475719184SGleb Smirnoff #ifdef DEVICE_POLLING
368575719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
368675719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
368775719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
368875719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
368975719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
369075719184SGleb Smirnoff 	} else
369175719184SGleb Smirnoff #endif
369275719184SGleb Smirnoff 
369395d67482SBill Paul 	/* Enable host interrupts. */
369475719184SGleb Smirnoff 	{
369595d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
369695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
369795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
369875719184SGleb Smirnoff 	}
369995d67482SBill Paul 
370067d5e043SOleg Bulyzhin 	bge_ifmedia_upd_locked(ifp);
370195d67482SBill Paul 
370213f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
370313f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
370495d67482SBill Paul 
37050f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
37060f9bd73bSSam Leffler }
37070f9bd73bSSam Leffler 
37080f9bd73bSSam Leffler static void
37093f74909aSGleb Smirnoff bge_init(void *xsc)
37100f9bd73bSSam Leffler {
37110f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
37120f9bd73bSSam Leffler 
37130f9bd73bSSam Leffler 	BGE_LOCK(sc);
37140f9bd73bSSam Leffler 	bge_init_locked(sc);
37150f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
371695d67482SBill Paul }
371795d67482SBill Paul 
371895d67482SBill Paul /*
371995d67482SBill Paul  * Set media options.
372095d67482SBill Paul  */
372195d67482SBill Paul static int
37223f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
372395d67482SBill Paul {
372467d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
372567d5e043SOleg Bulyzhin 	int res;
372667d5e043SOleg Bulyzhin 
372767d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
372867d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
372967d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
373067d5e043SOleg Bulyzhin 
373167d5e043SOleg Bulyzhin 	return (res);
373267d5e043SOleg Bulyzhin }
373367d5e043SOleg Bulyzhin 
373467d5e043SOleg Bulyzhin static int
373567d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
373667d5e043SOleg Bulyzhin {
373767d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
373895d67482SBill Paul 	struct mii_data *mii;
373995d67482SBill Paul 	struct ifmedia *ifm;
374095d67482SBill Paul 
374167d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
374267d5e043SOleg Bulyzhin 
374395d67482SBill Paul 	ifm = &sc->bge_ifmedia;
374495d67482SBill Paul 
374595d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
3746652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
374795d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
374895d67482SBill Paul 			return (EINVAL);
374995d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
375095d67482SBill Paul 		case IFM_AUTO:
3751ff50922bSDoug White 			/*
3752ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
3753ff50922bSDoug White 			 * mechanism for programming the autoneg
3754ff50922bSDoug White 			 * advertisement registers in TBI mode.
3755ff50922bSDoug White 			 */
37560f89fde2SJung-uk Kim 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3757ff50922bSDoug White 				uint32_t sgdig;
37580f89fde2SJung-uk Kim 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
37590f89fde2SJung-uk Kim 				if (sgdig & BGE_SGDIGSTS_DONE) {
3760ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3761ff50922bSDoug White 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3762ff50922bSDoug White 					sgdig |= BGE_SGDIGCFG_AUTO |
3763ff50922bSDoug White 					    BGE_SGDIGCFG_PAUSE_CAP |
3764ff50922bSDoug White 					    BGE_SGDIGCFG_ASYM_PAUSE;
3765ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3766ff50922bSDoug White 					    sgdig | BGE_SGDIGCFG_SEND);
3767ff50922bSDoug White 					DELAY(5);
3768ff50922bSDoug White 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3769ff50922bSDoug White 				}
37700f89fde2SJung-uk Kim 			}
377195d67482SBill Paul 			break;
377295d67482SBill Paul 		case IFM_1000_SX:
377395d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
377495d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
377595d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
377695d67482SBill Paul 			} else {
377795d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
377895d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
377995d67482SBill Paul 			}
378095d67482SBill Paul 			break;
378195d67482SBill Paul 		default:
378295d67482SBill Paul 			return (EINVAL);
378395d67482SBill Paul 		}
378495d67482SBill Paul 		return (0);
378595d67482SBill Paul 	}
378695d67482SBill Paul 
37871493e883SOleg Bulyzhin 	sc->bge_link_evt++;
378895d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
378995d67482SBill Paul 	if (mii->mii_instance) {
379095d67482SBill Paul 		struct mii_softc *miisc;
379195d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
379295d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
379395d67482SBill Paul 			mii_phy_reset(miisc);
379495d67482SBill Paul 	}
379595d67482SBill Paul 	mii_mediachg(mii);
379695d67482SBill Paul 
379795d67482SBill Paul 	return (0);
379895d67482SBill Paul }
379995d67482SBill Paul 
380095d67482SBill Paul /*
380195d67482SBill Paul  * Report current media status.
380295d67482SBill Paul  */
380395d67482SBill Paul static void
38043f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
380595d67482SBill Paul {
380667d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
380795d67482SBill Paul 	struct mii_data *mii;
380895d67482SBill Paul 
380967d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
381095d67482SBill Paul 
3811652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
381295d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
381395d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
381495d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
381595d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
381695d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
38174c0da0ffSGleb Smirnoff 		else {
38184c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
381967d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
38204c0da0ffSGleb Smirnoff 			return;
38214c0da0ffSGleb Smirnoff 		}
382295d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
382395d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
382495d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
382595d67482SBill Paul 		else
382695d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
382767d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
382895d67482SBill Paul 		return;
382995d67482SBill Paul 	}
383095d67482SBill Paul 
383195d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
383295d67482SBill Paul 	mii_pollstat(mii);
383395d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
383495d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
383567d5e043SOleg Bulyzhin 
383667d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
383795d67482SBill Paul }
383895d67482SBill Paul 
383995d67482SBill Paul static int
38403f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
384195d67482SBill Paul {
384295d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
384395d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
384495d67482SBill Paul 	struct mii_data *mii;
3845f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
384695d67482SBill Paul 
384795d67482SBill Paul 	switch (command) {
384895d67482SBill Paul 	case SIOCSIFMTU:
38494c0da0ffSGleb Smirnoff 		if (ifr->ifr_mtu < ETHERMIN ||
38504c0da0ffSGleb Smirnoff 		    ((BGE_IS_JUMBO_CAPABLE(sc)) &&
38514c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > BGE_JUMBO_MTU) ||
38524c0da0ffSGleb Smirnoff 		    ((!BGE_IS_JUMBO_CAPABLE(sc)) &&
38534c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > ETHERMTU))
385495d67482SBill Paul 			error = EINVAL;
38554c0da0ffSGleb Smirnoff 		else if (ifp->if_mtu != ifr->ifr_mtu) {
385695d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
385713f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
385895d67482SBill Paul 			bge_init(sc);
385995d67482SBill Paul 		}
386095d67482SBill Paul 		break;
386195d67482SBill Paul 	case SIOCSIFFLAGS:
38620f9bd73bSSam Leffler 		BGE_LOCK(sc);
386395d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
386495d67482SBill Paul 			/*
386595d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
386695d67482SBill Paul 			 * then just use the 'set promisc mode' command
386795d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
386895d67482SBill Paul 			 * a full re-init means reloading the firmware and
386995d67482SBill Paul 			 * waiting for it to start up, which may take a
3870d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
387195d67482SBill Paul 			 */
3872f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3873f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
38743e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
38753e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
3876f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
3877d183af7fSRuslan Ermilov 					bge_setmulti(sc);
387895d67482SBill Paul 			} else
38790f9bd73bSSam Leffler 				bge_init_locked(sc);
388095d67482SBill Paul 		} else {
388113f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
388295d67482SBill Paul 				bge_stop(sc);
388395d67482SBill Paul 			}
388495d67482SBill Paul 		}
388595d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
38860f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
388795d67482SBill Paul 		error = 0;
388895d67482SBill Paul 		break;
388995d67482SBill Paul 	case SIOCADDMULTI:
389095d67482SBill Paul 	case SIOCDELMULTI:
389113f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
38920f9bd73bSSam Leffler 			BGE_LOCK(sc);
389395d67482SBill Paul 			bge_setmulti(sc);
38940f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
389595d67482SBill Paul 			error = 0;
389695d67482SBill Paul 		}
389795d67482SBill Paul 		break;
389895d67482SBill Paul 	case SIOCSIFMEDIA:
389995d67482SBill Paul 	case SIOCGIFMEDIA:
3900652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
390195d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
390295d67482SBill Paul 			    &sc->bge_ifmedia, command);
390395d67482SBill Paul 		} else {
390495d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
390595d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
390695d67482SBill Paul 			    &mii->mii_media, command);
390795d67482SBill Paul 		}
390895d67482SBill Paul 		break;
390995d67482SBill Paul 	case SIOCSIFCAP:
391095d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
391175719184SGleb Smirnoff #ifdef DEVICE_POLLING
391275719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
391375719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
391475719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
391575719184SGleb Smirnoff 				if (error)
391675719184SGleb Smirnoff 					return (error);
391775719184SGleb Smirnoff 				BGE_LOCK(sc);
391875719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
391975719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
392075719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
392175719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
392275719184SGleb Smirnoff 				BGE_UNLOCK(sc);
392375719184SGleb Smirnoff 			} else {
392475719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
392575719184SGleb Smirnoff 				/* Enable interrupt even in error case */
392675719184SGleb Smirnoff 				BGE_LOCK(sc);
392775719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
392875719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
392975719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
393075719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
393175719184SGleb Smirnoff 				BGE_UNLOCK(sc);
393275719184SGleb Smirnoff 			}
393375719184SGleb Smirnoff 		}
393475719184SGleb Smirnoff #endif
3935d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
3936d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
3937d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
3938d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
3939b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
394095d67482SBill Paul 			else
3941b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
39424e35d186SJung-uk Kim #ifdef VLAN_CAPABILITIES
3943479b23b7SGleb Smirnoff 			VLAN_CAPABILITIES(ifp);
39444e35d186SJung-uk Kim #endif
394595d67482SBill Paul 		}
3946cb2eacc7SYaroslav Tykhiy 
3947cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_MTU) {
3948cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
3949cb2eacc7SYaroslav Tykhiy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3950cb2eacc7SYaroslav Tykhiy 			bge_init(sc);
3951cb2eacc7SYaroslav Tykhiy 		}
3952cb2eacc7SYaroslav Tykhiy 
3953cb2eacc7SYaroslav Tykhiy 		if (mask & IFCAP_VLAN_HWTAGGING) {
3954cb2eacc7SYaroslav Tykhiy 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
3955cb2eacc7SYaroslav Tykhiy 			BGE_LOCK(sc);
3956cb2eacc7SYaroslav Tykhiy 			bge_setvlan(sc);
3957cb2eacc7SYaroslav Tykhiy 			BGE_UNLOCK(sc);
3958cb2eacc7SYaroslav Tykhiy #ifdef VLAN_CAPABILITIES
3959cb2eacc7SYaroslav Tykhiy 			VLAN_CAPABILITIES(ifp);
3960cb2eacc7SYaroslav Tykhiy #endif
3961cb2eacc7SYaroslav Tykhiy 		}
3962cb2eacc7SYaroslav Tykhiy 
396395d67482SBill Paul 		break;
396495d67482SBill Paul 	default:
3965673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
396695d67482SBill Paul 		break;
396795d67482SBill Paul 	}
396895d67482SBill Paul 
396995d67482SBill Paul 	return (error);
397095d67482SBill Paul }
397195d67482SBill Paul 
397295d67482SBill Paul static void
3973b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
397495d67482SBill Paul {
3975b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
397695d67482SBill Paul 
3977b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
3978b74e67fbSGleb Smirnoff 
3979b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
3980b74e67fbSGleb Smirnoff 		return;
3981b74e67fbSGleb Smirnoff 
3982b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
398395d67482SBill Paul 
3984fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
398595d67482SBill Paul 
398613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3987426742bfSGleb Smirnoff 	bge_init_locked(sc);
398895d67482SBill Paul 
398995d67482SBill Paul 	ifp->if_oerrors++;
399095d67482SBill Paul }
399195d67482SBill Paul 
399295d67482SBill Paul /*
399395d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
399495d67482SBill Paul  * RX and TX lists.
399595d67482SBill Paul  */
399695d67482SBill Paul static void
39973f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
399895d67482SBill Paul {
399995d67482SBill Paul 	struct ifnet *ifp;
400095d67482SBill Paul 	struct ifmedia_entry *ifm;
400195d67482SBill Paul 	struct mii_data *mii = NULL;
400295d67482SBill Paul 	int mtmp, itmp;
400395d67482SBill Paul 
40040f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
40050f9bd73bSSam Leffler 
4006fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
400795d67482SBill Paul 
4008652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0)
400995d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
401095d67482SBill Paul 
40110f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
401295d67482SBill Paul 
401395d67482SBill Paul 	/*
40143f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
401595d67482SBill Paul 	 */
401695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
401795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
401895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
40197ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
402095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
402195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
402295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
402395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
402495d67482SBill Paul 
402595d67482SBill Paul 	/*
40263f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
402795d67482SBill Paul 	 */
402895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
402995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
403095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
403195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
403295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
40337ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
403495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
403595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
403695d67482SBill Paul 
403795d67482SBill Paul 	/*
403895d67482SBill Paul 	 * Shut down all of the memory managers and related
403995d67482SBill Paul 	 * state machines.
404095d67482SBill Paul 	 */
404195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
404295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
40437ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
404495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
40450c8aa4eaSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
404695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
40477ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
404895d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
404995d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
40500434d1b8SBill Paul 	}
405195d67482SBill Paul 
405295d67482SBill Paul 	/* Disable host interrupts. */
405395d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
405495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
405595d67482SBill Paul 
405695d67482SBill Paul 	/*
405795d67482SBill Paul 	 * Tell firmware we're shutting down.
405895d67482SBill Paul 	 */
40598cb1383cSDoug Ambrisko 
40608cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
40618cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
40628cb1383cSDoug Ambrisko 	bge_reset(sc);
40638cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
40648cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
40658cb1383cSDoug Ambrisko 
40668cb1383cSDoug Ambrisko 	/*
40678cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
40688cb1383cSDoug Ambrisko 	 */
40698cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
40708cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
40718cb1383cSDoug Ambrisko 	else
407295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
407395d67482SBill Paul 
407495d67482SBill Paul 	/* Free the RX lists. */
407595d67482SBill Paul 	bge_free_rx_ring_std(sc);
407695d67482SBill Paul 
407795d67482SBill Paul 	/* Free jumbo RX list. */
40784c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
407995d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
408095d67482SBill Paul 
408195d67482SBill Paul 	/* Free TX buffers. */
408295d67482SBill Paul 	bge_free_tx_ring(sc);
408395d67482SBill Paul 
408495d67482SBill Paul 	/*
408595d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
408695d67482SBill Paul 	 * unchanged so that things will be put back to normal when
408795d67482SBill Paul 	 * we bring the interface back up.
408895d67482SBill Paul 	 */
4089652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
409095d67482SBill Paul 		itmp = ifp->if_flags;
409195d67482SBill Paul 		ifp->if_flags |= IFF_UP;
4092dcc34049SPawel Jakub Dawidek 		/*
4093dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
4094dcc34049SPawel Jakub Dawidek 		 */
4095dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
409695d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
409795d67482SBill Paul 			mtmp = ifm->ifm_media;
409895d67482SBill Paul 			ifm->ifm_media = IFM_ETHER | IFM_NONE;
409995d67482SBill Paul 			mii_mediachg(mii);
410095d67482SBill Paul 			ifm->ifm_media = mtmp;
4101dcc34049SPawel Jakub Dawidek 		}
410295d67482SBill Paul 		ifp->if_flags = itmp;
410395d67482SBill Paul 	}
410495d67482SBill Paul 
410595d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
410695d67482SBill Paul 
41075dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
41081493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
41091493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
41101493e883SOleg Bulyzhin 	sc->bge_link = 0;
411195d67482SBill Paul 
41121493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
411395d67482SBill Paul }
411495d67482SBill Paul 
411595d67482SBill Paul /*
411695d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
411795d67482SBill Paul  * get confused by errant DMAs when rebooting.
411895d67482SBill Paul  */
411995d67482SBill Paul static void
41203f74909aSGleb Smirnoff bge_shutdown(device_t dev)
412195d67482SBill Paul {
412295d67482SBill Paul 	struct bge_softc *sc;
412395d67482SBill Paul 
412495d67482SBill Paul 	sc = device_get_softc(dev);
412595d67482SBill Paul 
41260f9bd73bSSam Leffler 	BGE_LOCK(sc);
412795d67482SBill Paul 	bge_stop(sc);
412895d67482SBill Paul 	bge_reset(sc);
41290f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
413095d67482SBill Paul }
413114afefa3SPawel Jakub Dawidek 
413214afefa3SPawel Jakub Dawidek static int
413314afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
413414afefa3SPawel Jakub Dawidek {
413514afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
413614afefa3SPawel Jakub Dawidek 
413714afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
413814afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
413914afefa3SPawel Jakub Dawidek 	bge_stop(sc);
414014afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
414114afefa3SPawel Jakub Dawidek 
414214afefa3SPawel Jakub Dawidek 	return (0);
414314afefa3SPawel Jakub Dawidek }
414414afefa3SPawel Jakub Dawidek 
414514afefa3SPawel Jakub Dawidek static int
414614afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
414714afefa3SPawel Jakub Dawidek {
414814afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
414914afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
415014afefa3SPawel Jakub Dawidek 
415114afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
415214afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
415314afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
415414afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
415514afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
415614afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
415714afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
415814afefa3SPawel Jakub Dawidek 	}
415914afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
416014afefa3SPawel Jakub Dawidek 
416114afefa3SPawel Jakub Dawidek 	return (0);
416214afefa3SPawel Jakub Dawidek }
4163dab5cd05SOleg Bulyzhin 
4164dab5cd05SOleg Bulyzhin static void
41653f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
4166dab5cd05SOleg Bulyzhin {
41671f313773SOleg Bulyzhin 	struct mii_data *mii;
41681f313773SOleg Bulyzhin 	uint32_t link, status;
4169dab5cd05SOleg Bulyzhin 
4170dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
41711f313773SOleg Bulyzhin 
41723f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
41737b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
41747b97099dSOleg Bulyzhin 
4175dab5cd05SOleg Bulyzhin 	/*
4176dab5cd05SOleg Bulyzhin 	 * Process link state changes.
4177dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
4178dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
4179dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
4180dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
4181dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
4182dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
4183dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
4184dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
41851f313773SOleg Bulyzhin 	 *
41861f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
41874c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
4188dab5cd05SOleg Bulyzhin 	 */
4189dab5cd05SOleg Bulyzhin 
41901f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
41914c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
4192dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
4193dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
41941f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
41955dda8085SOleg Bulyzhin 			mii_pollstat(mii);
41961f313773SOleg Bulyzhin 			if (!sc->bge_link &&
41971f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
41981f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
41991f313773SOleg Bulyzhin 				sc->bge_link++;
42001f313773SOleg Bulyzhin 				if (bootverbose)
42011f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
42021f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
42031f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
42041f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
42051f313773SOleg Bulyzhin 				sc->bge_link = 0;
42061f313773SOleg Bulyzhin 				if (bootverbose)
42071f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
42081f313773SOleg Bulyzhin 			}
42091f313773SOleg Bulyzhin 
42103f74909aSGleb Smirnoff 			/* Clear the interrupt. */
4211dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
4212dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
4213dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
4214dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
4215dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
4216dab5cd05SOleg Bulyzhin 		}
4217dab5cd05SOleg Bulyzhin 		return;
4218dab5cd05SOleg Bulyzhin 	}
4219dab5cd05SOleg Bulyzhin 
4220652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
42211f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
42227b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
42237b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
42241f313773SOleg Bulyzhin 				sc->bge_link++;
42251f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
42261f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
42271f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
42280c8aa4eaSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
42291f313773SOleg Bulyzhin 				if (bootverbose)
42301f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
42313f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
42323f74909aSGleb Smirnoff 				    LINK_STATE_UP);
42337b97099dSOleg Bulyzhin 			}
42341f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
4235dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
42361f313773SOleg Bulyzhin 			if (bootverbose)
42371f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
42387b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
42391f313773SOleg Bulyzhin 		}
42401493e883SOleg Bulyzhin 	} else if (CSR_READ_4(sc, BGE_MI_MODE) & BGE_MIMODE_AUTOPOLL) {
42411f313773SOleg Bulyzhin 		/*
42420c8aa4eaSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
42430c8aa4eaSJung-uk Kim 		 * in status word always set. Workaround this bug by reading
42440c8aa4eaSJung-uk Kim 		 * PHY link status directly.
42451f313773SOleg Bulyzhin 		 */
42461f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
42471f313773SOleg Bulyzhin 
42481f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
42491f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
42501f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
42515dda8085SOleg Bulyzhin 			mii_pollstat(mii);
42521f313773SOleg Bulyzhin 			if (!sc->bge_link &&
42531f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
42541f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
42551f313773SOleg Bulyzhin 				sc->bge_link++;
42561f313773SOleg Bulyzhin 				if (bootverbose)
42571f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
42581f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
42591f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
42601f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
42611f313773SOleg Bulyzhin 				sc->bge_link = 0;
42621f313773SOleg Bulyzhin 				if (bootverbose)
42631f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
42641f313773SOleg Bulyzhin 			}
42651f313773SOleg Bulyzhin 		}
42660c8aa4eaSJung-uk Kim 	} else {
42670c8aa4eaSJung-uk Kim 		/*
42680c8aa4eaSJung-uk Kim 		 * Discard link events for MII/GMII controllers
42690c8aa4eaSJung-uk Kim 		 * if MI auto-polling is disabled.
42700c8aa4eaSJung-uk Kim 		 */
4271dab5cd05SOleg Bulyzhin 	}
4272dab5cd05SOleg Bulyzhin 
42733f74909aSGleb Smirnoff 	/* Clear the attention. */
4274dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
4275dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
4276dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
4277dab5cd05SOleg Bulyzhin }
42786f8718a3SScott Long 
4279763757b2SScott Long #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
428006e83c7eSScott Long 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
4281763757b2SScott Long 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
4282763757b2SScott Long 	    desc)
4283763757b2SScott Long 
42846f8718a3SScott Long static void
42856f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
42866f8718a3SScott Long {
42876f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
4288763757b2SScott Long 	struct sysctl_oid_list *children, *schildren;
4289763757b2SScott Long 	struct sysctl_oid *tree;
42906f8718a3SScott Long 
42916f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
42926f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
42936f8718a3SScott Long 
42946f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
42956f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
42966f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
42976f8718a3SScott Long 	    "Debug Information");
42986f8718a3SScott Long 
42996f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
43006f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
43016f8718a3SScott Long 	    "Register Read");
43026f8718a3SScott Long 
43036f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
43046f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
43056f8718a3SScott Long 	    "Memory Read");
43066f8718a3SScott Long 
43076f8718a3SScott Long #endif
4308763757b2SScott Long 
4309d949071dSJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
4310d949071dSJung-uk Kim 		return;
4311d949071dSJung-uk Kim 
4312763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats", CTLFLAG_RD,
4313763757b2SScott Long 	    NULL, "BGE Statistics");
4314763757b2SScott Long 	schildren = children = SYSCTL_CHILDREN(tree);
4315763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
4316763757b2SScott Long 	    children, COSFramesDroppedDueToFilters,
4317763757b2SScott Long 	    "FramesDroppedDueToFilters");
4318763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
4319763757b2SScott Long 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
4320763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
4321763757b2SScott Long 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
4322763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
4323763757b2SScott Long 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
432406e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
432506e83c7eSScott Long 	    children, ifInDiscards, "InputDiscards");
432606e83c7eSScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
432706e83c7eSScott Long 	    children, ifInErrors, "InputErrors");
4328763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
4329763757b2SScott Long 	    children, nicRecvThresholdHit, "RecvThresholdHit");
4330763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
4331763757b2SScott Long 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
4332763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
4333763757b2SScott Long 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
4334763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
4335763757b2SScott Long 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
4336763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
4337763757b2SScott Long 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
4338763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
4339763757b2SScott Long 	    children, nicRingStatusUpdate, "RingStatusUpdate");
4340763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
4341763757b2SScott Long 	    children, nicInterrupts, "Interrupts");
4342763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
4343763757b2SScott Long 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
4344763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
4345763757b2SScott Long 	    children, nicSendThresholdHit, "SendThresholdHit");
4346763757b2SScott Long 
4347763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
4348763757b2SScott Long 	    NULL, "BGE RX Statistics");
4349763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
4350763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
4351763757b2SScott Long 	    children, rxstats.ifHCInOctets, "Octets");
4352763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
4353763757b2SScott Long 	    children, rxstats.etherStatsFragments, "Fragments");
4354763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
4355763757b2SScott Long 	    children, rxstats.ifHCInUcastPkts, "UcastPkts");
4356763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
4357763757b2SScott Long 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
4358763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
4359763757b2SScott Long 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
4360763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
4361763757b2SScott Long 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
4362763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
4363763757b2SScott Long 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
4364763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
4365763757b2SScott Long 	    children, rxstats.xoffPauseFramesReceived,
4366763757b2SScott Long 	    "xoffPauseFramesReceived");
4367763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
4368763757b2SScott Long 	    children, rxstats.macControlFramesReceived,
4369763757b2SScott Long 	    "ControlFramesReceived");
4370763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
4371763757b2SScott Long 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
4372763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
4373763757b2SScott Long 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
4374763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
4375763757b2SScott Long 	    children, rxstats.etherStatsJabbers, "Jabbers");
4376763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
4377763757b2SScott Long 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
4378763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
437906e83c7eSScott Long 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
4380763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
438106e83c7eSScott Long 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
4382763757b2SScott Long 
4383763757b2SScott Long 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
4384763757b2SScott Long 	    NULL, "BGE TX Statistics");
4385763757b2SScott Long 	children = SYSCTL_CHILDREN(tree);
4386763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
4387763757b2SScott Long 	    children, txstats.ifHCOutOctets, "Octets");
4388763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
4389763757b2SScott Long 	    children, txstats.etherStatsCollisions, "Collisions");
4390763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
4391763757b2SScott Long 	    children, txstats.outXonSent, "XonSent");
4392763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
4393763757b2SScott Long 	    children, txstats.outXoffSent, "XoffSent");
4394763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
4395763757b2SScott Long 	    children, txstats.flowControlDone, "flowControlDone");
4396763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
4397763757b2SScott Long 	    children, txstats.dot3StatsInternalMacTransmitErrors,
4398763757b2SScott Long 	    "InternalMacTransmitErrors");
4399763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
4400763757b2SScott Long 	    children, txstats.dot3StatsSingleCollisionFrames,
4401763757b2SScott Long 	    "SingleCollisionFrames");
4402763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
4403763757b2SScott Long 	    children, txstats.dot3StatsMultipleCollisionFrames,
4404763757b2SScott Long 	    "MultipleCollisionFrames");
4405763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
4406763757b2SScott Long 	    children, txstats.dot3StatsDeferredTransmissions,
4407763757b2SScott Long 	    "DeferredTransmissions");
4408763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
4409763757b2SScott Long 	    children, txstats.dot3StatsExcessiveCollisions,
4410763757b2SScott Long 	    "ExcessiveCollisions");
4411763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
441206e83c7eSScott Long 	    children, txstats.dot3StatsLateCollisions,
441306e83c7eSScott Long 	    "LateCollisions");
4414763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
4415763757b2SScott Long 	    children, txstats.ifHCOutUcastPkts, "UcastPkts");
4416763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
4417763757b2SScott Long 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
4418763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
4419763757b2SScott Long 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
4420763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
4421763757b2SScott Long 	    children, txstats.dot3StatsCarrierSenseErrors,
4422763757b2SScott Long 	    "CarrierSenseErrors");
4423763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
4424763757b2SScott Long 	    children, txstats.ifOutDiscards, "Discards");
4425763757b2SScott Long 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
4426763757b2SScott Long 	    children, txstats.ifOutErrors, "Errors");
4427763757b2SScott Long }
4428763757b2SScott Long 
4429763757b2SScott Long static int
4430763757b2SScott Long bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
4431763757b2SScott Long {
4432763757b2SScott Long 	struct bge_softc *sc;
443306e83c7eSScott Long 	uint32_t result;
4434d949071dSJung-uk Kim 	int offset;
4435763757b2SScott Long 
4436763757b2SScott Long 	sc = (struct bge_softc *)arg1;
4437763757b2SScott Long 	offset = arg2;
4438d949071dSJung-uk Kim 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
4439d949071dSJung-uk Kim 	    offsetof(bge_hostaddr, bge_addr_lo));
4440041b706bSDavid Malone 	return (sysctl_handle_int(oidp, &result, 0, req));
44416f8718a3SScott Long }
44426f8718a3SScott Long 
44436f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
44446f8718a3SScott Long static int
44456f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
44466f8718a3SScott Long {
44476f8718a3SScott Long 	struct bge_softc *sc;
44486f8718a3SScott Long 	uint16_t *sbdata;
44496f8718a3SScott Long 	int error;
44506f8718a3SScott Long 	int result;
44516f8718a3SScott Long 	int i, j;
44526f8718a3SScott Long 
44536f8718a3SScott Long 	result = -1;
44546f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
44556f8718a3SScott Long 	if (error || (req->newptr == NULL))
44566f8718a3SScott Long 		return (error);
44576f8718a3SScott Long 
44586f8718a3SScott Long 	if (result == 1) {
44596f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
44606f8718a3SScott Long 
44616f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
44626f8718a3SScott Long 		printf("Status Block:\n");
44636f8718a3SScott Long 		for (i = 0x0; i < (BGE_STATUS_BLK_SZ / 4); ) {
44646f8718a3SScott Long 			printf("%06x:", i);
44656f8718a3SScott Long 			for (j = 0; j < 8; j++) {
44666f8718a3SScott Long 				printf(" %04x", sbdata[i]);
44676f8718a3SScott Long 				i += 4;
44686f8718a3SScott Long 			}
44696f8718a3SScott Long 			printf("\n");
44706f8718a3SScott Long 		}
44716f8718a3SScott Long 
44726f8718a3SScott Long 		printf("Registers:\n");
44730c8aa4eaSJung-uk Kim 		for (i = 0x800; i < 0xA00; ) {
44746f8718a3SScott Long 			printf("%06x:", i);
44756f8718a3SScott Long 			for (j = 0; j < 8; j++) {
44766f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
44776f8718a3SScott Long 				i += 4;
44786f8718a3SScott Long 			}
44796f8718a3SScott Long 			printf("\n");
44806f8718a3SScott Long 		}
44816f8718a3SScott Long 
44826f8718a3SScott Long 		printf("Hardware Flags:\n");
44835345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
44846f8718a3SScott Long 			printf(" - 575X Plus\n");
44855345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
44866f8718a3SScott Long 			printf(" - 5705 Plus\n");
44875345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
44885345bad0SScott Long 			printf(" - 5714 Family\n");
44895345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
44905345bad0SScott Long 			printf(" - 5700 Family\n");
44916f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
44926f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
44936f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
44946f8718a3SScott Long 			printf(" - PCI-X Bus\n");
44956f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
44966f8718a3SScott Long 			printf(" - PCI Express Bus\n");
44975ee49a3aSJung-uk Kim 		if (sc->bge_flags & BGE_FLAG_NO_3LED)
44986f8718a3SScott Long 			printf(" - No 3 LEDs\n");
44996f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
45006f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
45016f8718a3SScott Long 	}
45026f8718a3SScott Long 
45036f8718a3SScott Long 	return (error);
45046f8718a3SScott Long }
45056f8718a3SScott Long 
45066f8718a3SScott Long static int
45076f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
45086f8718a3SScott Long {
45096f8718a3SScott Long 	struct bge_softc *sc;
45106f8718a3SScott Long 	int error;
45116f8718a3SScott Long 	uint16_t result;
45126f8718a3SScott Long 	uint32_t val;
45136f8718a3SScott Long 
45146f8718a3SScott Long 	result = -1;
45156f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
45166f8718a3SScott Long 	if (error || (req->newptr == NULL))
45176f8718a3SScott Long 		return (error);
45186f8718a3SScott Long 
45196f8718a3SScott Long 	if (result < 0x8000) {
45206f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
45216f8718a3SScott Long 		val = CSR_READ_4(sc, result);
45226f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
45236f8718a3SScott Long 	}
45246f8718a3SScott Long 
45256f8718a3SScott Long 	return (error);
45266f8718a3SScott Long }
45276f8718a3SScott Long 
45286f8718a3SScott Long static int
45296f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
45306f8718a3SScott Long {
45316f8718a3SScott Long 	struct bge_softc *sc;
45326f8718a3SScott Long 	int error;
45336f8718a3SScott Long 	uint16_t result;
45346f8718a3SScott Long 	uint32_t val;
45356f8718a3SScott Long 
45366f8718a3SScott Long 	result = -1;
45376f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
45386f8718a3SScott Long 	if (error || (req->newptr == NULL))
45396f8718a3SScott Long 		return (error);
45406f8718a3SScott Long 
45416f8718a3SScott Long 	if (result < 0x8000) {
45426f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
45436f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
45446f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
45456f8718a3SScott Long 	}
45466f8718a3SScott Long 
45476f8718a3SScott Long 	return (error);
45486f8718a3SScott Long }
45496f8718a3SScott Long #endif
4550