xref: /freebsd/sys/dev/bge/if_bge.c (revision 6098821ce7389bbe5e3f053ea846855d52e0953b)
1098ca2bdSWarner Losh /*-
295d67482SBill Paul  * Copyright (c) 2001 Wind River Systems
395d67482SBill Paul  * Copyright (c) 1997, 1998, 1999, 2001
495d67482SBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
595d67482SBill Paul  *
695d67482SBill Paul  * Redistribution and use in source and binary forms, with or without
795d67482SBill Paul  * modification, are permitted provided that the following conditions
895d67482SBill Paul  * are met:
995d67482SBill Paul  * 1. Redistributions of source code must retain the above copyright
1095d67482SBill Paul  *    notice, this list of conditions and the following disclaimer.
1195d67482SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1295d67482SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1395d67482SBill Paul  *    documentation and/or other materials provided with the distribution.
1495d67482SBill Paul  * 3. All advertising materials mentioning features or use of this software
1595d67482SBill Paul  *    must display the following acknowledgement:
1695d67482SBill Paul  *	This product includes software developed by Bill Paul.
1795d67482SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1895d67482SBill Paul  *    may be used to endorse or promote products derived from this software
1995d67482SBill Paul  *    without specific prior written permission.
2095d67482SBill Paul  *
2195d67482SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2295d67482SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2395d67482SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2495d67482SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2595d67482SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2695d67482SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2795d67482SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2895d67482SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2995d67482SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3095d67482SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3195d67482SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3295d67482SBill Paul  */
3395d67482SBill Paul 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
3795d67482SBill Paul /*
3895d67482SBill Paul  * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
3995d67482SBill Paul  *
4095d67482SBill Paul  * The Broadcom BCM5700 is based on technology originally developed by
4195d67482SBill Paul  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
4295d67482SBill Paul  * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
4395d67482SBill Paul  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
4495d67482SBill Paul  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
4595d67482SBill Paul  * frames, highly configurable RX filtering, and 16 RX and TX queues
4695d67482SBill Paul  * (which, along with RX filter rules, can be used for QOS applications).
4795d67482SBill Paul  * Other features, such as TCP segmentation, may be available as part
4895d67482SBill Paul  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
4995d67482SBill Paul  * firmware images can be stored in hardware and need not be compiled
5095d67482SBill Paul  * into the driver.
5195d67482SBill Paul  *
5295d67482SBill Paul  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
5395d67482SBill Paul  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
5495d67482SBill Paul  *
5595d67482SBill Paul  * The BCM5701 is a single-chip solution incorporating both the BCM5700
5698b28ee5SBill Paul  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
5795d67482SBill Paul  * does not support external SSRAM.
5895d67482SBill Paul  *
5995d67482SBill Paul  * Broadcom also produces a variation of the BCM5700 under the "Altima"
6095d67482SBill Paul  * brand name, which is functionally similar but lacks PCI-X support.
6195d67482SBill Paul  *
6295d67482SBill Paul  * Without external SSRAM, you can only have at most 4 TX rings,
6395d67482SBill Paul  * and the use of the mini RX ring is disabled. This seems to imply
6495d67482SBill Paul  * that these features are simply not available on the BCM5701. As a
6595d67482SBill Paul  * result, this driver does not implement any support for the mini RX
6695d67482SBill Paul  * ring.
6795d67482SBill Paul  */
6895d67482SBill Paul 
6975719184SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
7075719184SGleb Smirnoff #include "opt_device_polling.h"
7175719184SGleb Smirnoff #endif
7275719184SGleb Smirnoff 
7395d67482SBill Paul #include <sys/param.h>
74f41ac2beSBill Paul #include <sys/endian.h>
7595d67482SBill Paul #include <sys/systm.h>
7695d67482SBill Paul #include <sys/sockio.h>
7795d67482SBill Paul #include <sys/mbuf.h>
7895d67482SBill Paul #include <sys/malloc.h>
7995d67482SBill Paul #include <sys/kernel.h>
80fe12f24bSPoul-Henning Kamp #include <sys/module.h>
8195d67482SBill Paul #include <sys/socket.h>
82f1a7e6d5SScott Long #include <sys/sysctl.h>
8395d67482SBill Paul 
8495d67482SBill Paul #include <net/if.h>
8595d67482SBill Paul #include <net/if_arp.h>
8695d67482SBill Paul #include <net/ethernet.h>
8795d67482SBill Paul #include <net/if_dl.h>
8895d67482SBill Paul #include <net/if_media.h>
8995d67482SBill Paul 
9095d67482SBill Paul #include <net/bpf.h>
9195d67482SBill Paul 
9295d67482SBill Paul #include <net/if_types.h>
9395d67482SBill Paul #include <net/if_vlan_var.h>
9495d67482SBill Paul 
9595d67482SBill Paul #include <netinet/in_systm.h>
9695d67482SBill Paul #include <netinet/in.h>
9795d67482SBill Paul #include <netinet/ip.h>
9895d67482SBill Paul 
9995d67482SBill Paul #include <machine/bus.h>
10095d67482SBill Paul #include <machine/resource.h>
10195d67482SBill Paul #include <sys/bus.h>
10295d67482SBill Paul #include <sys/rman.h>
10395d67482SBill Paul 
10495d67482SBill Paul #include <dev/mii/mii.h>
10595d67482SBill Paul #include <dev/mii/miivar.h>
1062d3ce713SDavid E. O'Brien #include "miidevs.h"
10795d67482SBill Paul #include <dev/mii/brgphyreg.h>
10895d67482SBill Paul 
1094fbd232cSWarner Losh #include <dev/pci/pcireg.h>
1104fbd232cSWarner Losh #include <dev/pci/pcivar.h>
11195d67482SBill Paul 
11295d67482SBill Paul #include <dev/bge/if_bgereg.h>
11395d67482SBill Paul 
1145ddc5794SJohn Polstra #define	BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
115d375e524SGleb Smirnoff #define	ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
11695d67482SBill Paul 
117f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, pci, 1, 1, 1);
118f246e4a1SMatthew N. Dodd MODULE_DEPEND(bge, ether, 1, 1, 1);
11995d67482SBill Paul MODULE_DEPEND(bge, miibus, 1, 1, 1);
12095d67482SBill Paul 
1217b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
12295d67482SBill Paul #include "miibus_if.h"
12395d67482SBill Paul 
12495d67482SBill Paul /*
12595d67482SBill Paul  * Various supported device vendors/types and their names. Note: the
12695d67482SBill Paul  * spec seems to indicate that the hardware still has Alteon's vendor
12795d67482SBill Paul  * ID burned into it, though it will always be overriden by the vendor
12895d67482SBill Paul  * ID in the EEPROM. Just to be safe, we cover all possibilities.
12995d67482SBill Paul  */
1304c0da0ffSGleb Smirnoff static struct bge_type {
1314c0da0ffSGleb Smirnoff 	uint16_t	bge_vid;
1324c0da0ffSGleb Smirnoff 	uint16_t	bge_did;
1334c0da0ffSGleb Smirnoff } bge_devs[] = {
1344c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5700 },
1354c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5701 },
13695d67482SBill Paul 
1374c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1000 },
1384c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1002 },
1394c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC9100 },
1404c0da0ffSGleb Smirnoff 
1414c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	APPLE_DEVICE_BCM5701 },
1424c0da0ffSGleb Smirnoff 
1434c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5700 },
1444c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5701 },
1454c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702 },
1464c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702_ALT },
1474c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702X },
1484c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703 },
1494c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703_ALT },
1504c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703X },
1514c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704C },
1524c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S },
1534c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S_ALT },
1544c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705 },
1554c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705F },
1564c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705K },
1574c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M },
1584c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M_ALT },
1594c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714C },
1604c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714S },
1614c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715 },
1624c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715S },
1634c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
1644c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
1654c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
1664c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
1674c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
1684c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
1694c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
1704c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
1714c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
1724c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
1734c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
1744c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
1759e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
1769e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
1779e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
1789e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
1794c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
1804c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
1814c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
1824c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
1839e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
1849e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
1859e86676bSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
1864c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
1874c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
1884c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
1894c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
1904c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
1914c0da0ffSGleb Smirnoff 
1924c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
1934c0da0ffSGleb Smirnoff 
1944c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
1954c0da0ffSGleb Smirnoff 
1964c0da0ffSGleb Smirnoff 	{ 0, 0 }
19795d67482SBill Paul };
19895d67482SBill Paul 
1994c0da0ffSGleb Smirnoff static const struct bge_vendor {
2004c0da0ffSGleb Smirnoff 	uint16_t	v_id;
2014c0da0ffSGleb Smirnoff 	const char	*v_name;
2024c0da0ffSGleb Smirnoff } bge_vendors[] = {
2034c0da0ffSGleb Smirnoff 	{ ALTEON_VENDORID,	"Alteon" },
2044c0da0ffSGleb Smirnoff 	{ ALTIMA_VENDORID,	"Altima" },
2054c0da0ffSGleb Smirnoff 	{ APPLE_VENDORID,	"Apple" },
2064c0da0ffSGleb Smirnoff 	{ BCOM_VENDORID,	"Broadcom" },
2074c0da0ffSGleb Smirnoff 	{ SK_VENDORID,		"SysKonnect" },
2084c0da0ffSGleb Smirnoff 	{ TC_VENDORID,		"3Com" },
2094c0da0ffSGleb Smirnoff 
2104c0da0ffSGleb Smirnoff 	{ 0, NULL }
2114c0da0ffSGleb Smirnoff };
2124c0da0ffSGleb Smirnoff 
2134c0da0ffSGleb Smirnoff static const struct bge_revision {
2144c0da0ffSGleb Smirnoff 	uint32_t	br_chipid;
2154c0da0ffSGleb Smirnoff 	const char	*br_name;
2164c0da0ffSGleb Smirnoff } bge_revisions[] = {
2174c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
2184c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
2194c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
2204c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
2214c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
2224c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
2234c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
2244c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
2254c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
2264c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
2274c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
2284c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
2294c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
2304c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
2314c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
2324c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
2339e86676bSGleb Smirnoff 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
2344c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
2354c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
2364c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
2374c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
2384c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
2394c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
2404c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
2414c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
2424c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
2434c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
2444c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
2454c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
2464c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
2474c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
2484c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
2494c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
25042787b76SGleb Smirnoff 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
2514c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
2524c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
2534c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
2544c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
2554c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
2564c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
2574c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
2584c0da0ffSGleb Smirnoff 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
25981179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
2606f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
2616f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
2626f8718a3SScott Long 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
2634c0da0ffSGleb Smirnoff 
2644c0da0ffSGleb Smirnoff 	{ 0, NULL }
2654c0da0ffSGleb Smirnoff };
2664c0da0ffSGleb Smirnoff 
2674c0da0ffSGleb Smirnoff /*
2684c0da0ffSGleb Smirnoff  * Some defaults for major revisions, so that newer steppings
2694c0da0ffSGleb Smirnoff  * that we don't know about have a shot at working.
2704c0da0ffSGleb Smirnoff  */
2714c0da0ffSGleb Smirnoff static const struct bge_revision bge_majorrevs[] = {
2729e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
2739e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
2749e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
2759e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
2769e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
2779e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
2789e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
2799e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
2809e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
2819e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
2829e86676bSGleb Smirnoff 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
28381179070SJung-uk Kim 	/* 5754 and 5787 share the same ASIC ID */
2846f8718a3SScott Long 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
2854c0da0ffSGleb Smirnoff 
2864c0da0ffSGleb Smirnoff 	{ 0, NULL }
2874c0da0ffSGleb Smirnoff };
2884c0da0ffSGleb Smirnoff 
2896098821cSJung-uk Kim #define	BGE_IS_JUMBO_CAPABLE(sc)	\
2906098821cSJung-uk Kim 	((sc)->bge_flags & BGE_FLAG_JUMBO)
2916098821cSJung-uk Kim #define	BGE_IS_5700_FAMILY(sc)		\
2926098821cSJung-uk Kim 	((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
2936098821cSJung-uk Kim #define	BGE_IS_5705_PLUS(sc)		\
2946098821cSJung-uk Kim 	((sc)->bge_flags & BGE_FLAG_5705_PLUS)
2956098821cSJung-uk Kim #define	BGE_IS_5714_FAMILY(sc)		\
2966098821cSJung-uk Kim 	((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
2976098821cSJung-uk Kim #define	BGE_IS_575X_PLUS(sc)		\
2986098821cSJung-uk Kim 	((sc)->bge_flags & BGE_FLAG_575X_PLUS)
2994c0da0ffSGleb Smirnoff 
3004c0da0ffSGleb Smirnoff const struct bge_revision * bge_lookup_rev(uint32_t);
3014c0da0ffSGleb Smirnoff const struct bge_vendor * bge_lookup_vendor(uint16_t);
302e51a25f8SAlfred Perlstein static int bge_probe(device_t);
303e51a25f8SAlfred Perlstein static int bge_attach(device_t);
304e51a25f8SAlfred Perlstein static int bge_detach(device_t);
30514afefa3SPawel Jakub Dawidek static int bge_suspend(device_t);
30614afefa3SPawel Jakub Dawidek static int bge_resume(device_t);
3073f74909aSGleb Smirnoff static void bge_release_resources(struct bge_softc *);
308f41ac2beSBill Paul static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
309f41ac2beSBill Paul static int bge_dma_alloc(device_t);
310f41ac2beSBill Paul static void bge_dma_free(struct bge_softc *);
311f41ac2beSBill Paul 
312e51a25f8SAlfred Perlstein static void bge_txeof(struct bge_softc *);
313e51a25f8SAlfred Perlstein static void bge_rxeof(struct bge_softc *);
31495d67482SBill Paul 
3158cb1383cSDoug Ambrisko static void bge_asf_driver_up (struct bge_softc *);
316e51a25f8SAlfred Perlstein static void bge_tick(void *);
317e51a25f8SAlfred Perlstein static void bge_stats_update(struct bge_softc *);
3183f74909aSGleb Smirnoff static void bge_stats_update_regs(struct bge_softc *);
319676ad2c9SGleb Smirnoff static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
32095d67482SBill Paul 
321e51a25f8SAlfred Perlstein static void bge_intr(void *);
3220f9bd73bSSam Leffler static void bge_start_locked(struct ifnet *);
323e51a25f8SAlfred Perlstein static void bge_start(struct ifnet *);
324e51a25f8SAlfred Perlstein static int bge_ioctl(struct ifnet *, u_long, caddr_t);
3250f9bd73bSSam Leffler static void bge_init_locked(struct bge_softc *);
326e51a25f8SAlfred Perlstein static void bge_init(void *);
327e51a25f8SAlfred Perlstein static void bge_stop(struct bge_softc *);
328b74e67fbSGleb Smirnoff static void bge_watchdog(struct bge_softc *);
329e51a25f8SAlfred Perlstein static void bge_shutdown(device_t);
33067d5e043SOleg Bulyzhin static int bge_ifmedia_upd_locked(struct ifnet *);
331e51a25f8SAlfred Perlstein static int bge_ifmedia_upd(struct ifnet *);
332e51a25f8SAlfred Perlstein static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
33395d67482SBill Paul 
3343f74909aSGleb Smirnoff static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
335e51a25f8SAlfred Perlstein static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
33695d67482SBill Paul 
3373e9b1bcaSJung-uk Kim static void bge_setpromisc(struct bge_softc *);
338e51a25f8SAlfred Perlstein static void bge_setmulti(struct bge_softc *);
33995d67482SBill Paul 
340e51a25f8SAlfred Perlstein static int bge_newbuf_std(struct bge_softc *, int, struct mbuf *);
341e51a25f8SAlfred Perlstein static int bge_newbuf_jumbo(struct bge_softc *, int, struct mbuf *);
342e51a25f8SAlfred Perlstein static int bge_init_rx_ring_std(struct bge_softc *);
343e51a25f8SAlfred Perlstein static void bge_free_rx_ring_std(struct bge_softc *);
344e51a25f8SAlfred Perlstein static int bge_init_rx_ring_jumbo(struct bge_softc *);
345e51a25f8SAlfred Perlstein static void bge_free_rx_ring_jumbo(struct bge_softc *);
346e51a25f8SAlfred Perlstein static void bge_free_tx_ring(struct bge_softc *);
347e51a25f8SAlfred Perlstein static int bge_init_tx_ring(struct bge_softc *);
34895d67482SBill Paul 
349e51a25f8SAlfred Perlstein static int bge_chipinit(struct bge_softc *);
350e51a25f8SAlfred Perlstein static int bge_blockinit(struct bge_softc *);
35195d67482SBill Paul 
3523f74909aSGleb Smirnoff static uint32_t bge_readmem_ind(struct bge_softc *, int);
353e51a25f8SAlfred Perlstein static void bge_writemem_ind(struct bge_softc *, int, int);
35495d67482SBill Paul #ifdef notdef
3553f74909aSGleb Smirnoff static uint32_t bge_readreg_ind(struct bge_softc *, int);
35695d67482SBill Paul #endif
3579ba784dbSScott Long static void bge_writemem_direct(struct bge_softc *, int, int);
358e51a25f8SAlfred Perlstein static void bge_writereg_ind(struct bge_softc *, int, int);
35995d67482SBill Paul 
360e51a25f8SAlfred Perlstein static int bge_miibus_readreg(device_t, int, int);
361e51a25f8SAlfred Perlstein static int bge_miibus_writereg(device_t, int, int, int);
362e51a25f8SAlfred Perlstein static void bge_miibus_statchg(device_t);
36375719184SGleb Smirnoff #ifdef DEVICE_POLLING
3643f74909aSGleb Smirnoff static void bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
36575719184SGleb Smirnoff #endif
36695d67482SBill Paul 
3678cb1383cSDoug Ambrisko #define	BGE_RESET_START 1
3688cb1383cSDoug Ambrisko #define	BGE_RESET_STOP  2
3698cb1383cSDoug Ambrisko static void bge_sig_post_reset(struct bge_softc *, int);
3708cb1383cSDoug Ambrisko static void bge_sig_legacy(struct bge_softc *, int);
3718cb1383cSDoug Ambrisko static void bge_sig_pre_reset(struct bge_softc *, int);
3728cb1383cSDoug Ambrisko static int bge_reset(struct bge_softc *);
373dab5cd05SOleg Bulyzhin static void bge_link_upd(struct bge_softc *);
37495d67482SBill Paul 
3756f8718a3SScott Long /*
3766f8718a3SScott Long  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
3776f8718a3SScott Long  * leak information to untrusted users.  It is also known to cause alignment
3786f8718a3SScott Long  * traps on certain architectures.
3796f8718a3SScott Long  */
3806f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
3816f8718a3SScott Long static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
3826f8718a3SScott Long static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
3836f8718a3SScott Long static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
3846f8718a3SScott Long #endif
3856f8718a3SScott Long static void bge_add_sysctls(struct bge_softc *);
3866f8718a3SScott Long 
38795d67482SBill Paul static device_method_t bge_methods[] = {
38895d67482SBill Paul 	/* Device interface */
38995d67482SBill Paul 	DEVMETHOD(device_probe,		bge_probe),
39095d67482SBill Paul 	DEVMETHOD(device_attach,	bge_attach),
39195d67482SBill Paul 	DEVMETHOD(device_detach,	bge_detach),
39295d67482SBill Paul 	DEVMETHOD(device_shutdown,	bge_shutdown),
39314afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_suspend,	bge_suspend),
39414afefa3SPawel Jakub Dawidek 	DEVMETHOD(device_resume,	bge_resume),
39595d67482SBill Paul 
39695d67482SBill Paul 	/* bus interface */
39795d67482SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
39895d67482SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
39995d67482SBill Paul 
40095d67482SBill Paul 	/* MII interface */
40195d67482SBill Paul 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
40295d67482SBill Paul 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
40395d67482SBill Paul 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
40495d67482SBill Paul 
40595d67482SBill Paul 	{ 0, 0 }
40695d67482SBill Paul };
40795d67482SBill Paul 
40895d67482SBill Paul static driver_t bge_driver = {
40995d67482SBill Paul 	"bge",
41095d67482SBill Paul 	bge_methods,
41195d67482SBill Paul 	sizeof(struct bge_softc)
41295d67482SBill Paul };
41395d67482SBill Paul 
41495d67482SBill Paul static devclass_t bge_devclass;
41595d67482SBill Paul 
416f246e4a1SMatthew N. Dodd DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
41795d67482SBill Paul DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
41895d67482SBill Paul 
419c4529f41SMichael Reifenberger static int bge_fake_autoneg = 0;
420f1a7e6d5SScott Long static int bge_allow_asf = 1;
421f1a7e6d5SScott Long 
422c4529f41SMichael Reifenberger TUNABLE_INT("hw.bge.fake_autoneg", &bge_fake_autoneg);
423f1a7e6d5SScott Long TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
424f1a7e6d5SScott Long 
425f1a7e6d5SScott Long SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
426f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, fake_autoneg, CTLFLAG_RD, &bge_fake_autoneg, 0,
427f1a7e6d5SScott Long 	"Enable fake autonegotiation for certain blade systems");
428f1a7e6d5SScott Long SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
429f1a7e6d5SScott Long 	"Allow ASF mode if available");
430c4529f41SMichael Reifenberger 
4313f74909aSGleb Smirnoff static uint32_t
4323f74909aSGleb Smirnoff bge_readmem_ind(struct bge_softc *sc, int off)
43395d67482SBill Paul {
43495d67482SBill Paul 	device_t dev;
4356f8718a3SScott Long 	uint32_t val;
43695d67482SBill Paul 
43795d67482SBill Paul 	dev = sc->bge_dev;
43895d67482SBill Paul 
43995d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
4406f8718a3SScott Long 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
4416f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
4426f8718a3SScott Long 	return (val);
44395d67482SBill Paul }
44495d67482SBill Paul 
44595d67482SBill Paul static void
4463f74909aSGleb Smirnoff bge_writemem_ind(struct bge_softc *sc, int off, int val)
44795d67482SBill Paul {
44895d67482SBill Paul 	device_t dev;
44995d67482SBill Paul 
45095d67482SBill Paul 	dev = sc->bge_dev;
45195d67482SBill Paul 
45295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
45395d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
4546f8718a3SScott Long 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
45595d67482SBill Paul }
45695d67482SBill Paul 
45795d67482SBill Paul #ifdef notdef
4583f74909aSGleb Smirnoff static uint32_t
4593f74909aSGleb Smirnoff bge_readreg_ind(struct bge_softc *sc, int off)
46095d67482SBill Paul {
46195d67482SBill Paul 	device_t dev;
46295d67482SBill Paul 
46395d67482SBill Paul 	dev = sc->bge_dev;
46495d67482SBill Paul 
46595d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
46695d67482SBill Paul 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
46795d67482SBill Paul }
46895d67482SBill Paul #endif
46995d67482SBill Paul 
47095d67482SBill Paul static void
4713f74909aSGleb Smirnoff bge_writereg_ind(struct bge_softc *sc, int off, int val)
47295d67482SBill Paul {
47395d67482SBill Paul 	device_t dev;
47495d67482SBill Paul 
47595d67482SBill Paul 	dev = sc->bge_dev;
47695d67482SBill Paul 
47795d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
47895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
47995d67482SBill Paul }
48095d67482SBill Paul 
4816f8718a3SScott Long static void
4826f8718a3SScott Long bge_writemem_direct(struct bge_softc *sc, int off, int val)
4836f8718a3SScott Long {
4846f8718a3SScott Long 	CSR_WRITE_4(sc, off, val);
4856f8718a3SScott Long }
4866f8718a3SScott Long 
487f41ac2beSBill Paul /*
488f41ac2beSBill Paul  * Map a single buffer address.
489f41ac2beSBill Paul  */
490f41ac2beSBill Paul 
491f41ac2beSBill Paul static void
4923f74909aSGleb Smirnoff bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
493f41ac2beSBill Paul {
494f41ac2beSBill Paul 	struct bge_dmamap_arg *ctx;
495f41ac2beSBill Paul 
496f41ac2beSBill Paul 	if (error)
497f41ac2beSBill Paul 		return;
498f41ac2beSBill Paul 
499f41ac2beSBill Paul 	ctx = arg;
500f41ac2beSBill Paul 
501f41ac2beSBill Paul 	if (nseg > ctx->bge_maxsegs) {
502f41ac2beSBill Paul 		ctx->bge_maxsegs = 0;
503f41ac2beSBill Paul 		return;
504f41ac2beSBill Paul 	}
505f41ac2beSBill Paul 
506f41ac2beSBill Paul 	ctx->bge_busaddr = segs->ds_addr;
507f41ac2beSBill Paul }
508f41ac2beSBill Paul 
50995d67482SBill Paul /*
51095d67482SBill Paul  * Read a byte of data stored in the EEPROM at address 'addr.' The
51195d67482SBill Paul  * BCM570x supports both the traditional bitbang interface and an
51295d67482SBill Paul  * auto access interface for reading the EEPROM. We use the auto
51395d67482SBill Paul  * access method.
51495d67482SBill Paul  */
5153f74909aSGleb Smirnoff static uint8_t
5163f74909aSGleb Smirnoff bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
51795d67482SBill Paul {
51895d67482SBill Paul 	int i;
5193f74909aSGleb Smirnoff 	uint32_t byte = 0;
52095d67482SBill Paul 
52195d67482SBill Paul 	/*
52295d67482SBill Paul 	 * Enable use of auto EEPROM access so we can avoid
52395d67482SBill Paul 	 * having to use the bitbang method.
52495d67482SBill Paul 	 */
52595d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
52695d67482SBill Paul 
52795d67482SBill Paul 	/* Reset the EEPROM, load the clock period. */
52895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR,
52995d67482SBill Paul 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
53095d67482SBill Paul 	DELAY(20);
53195d67482SBill Paul 
53295d67482SBill Paul 	/* Issue the read EEPROM command. */
53395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
53495d67482SBill Paul 
53595d67482SBill Paul 	/* Wait for completion */
53695d67482SBill Paul 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
53795d67482SBill Paul 		DELAY(10);
53895d67482SBill Paul 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
53995d67482SBill Paul 			break;
54095d67482SBill Paul 	}
54195d67482SBill Paul 
54295d67482SBill Paul 	if (i == BGE_TIMEOUT) {
543fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
544f6789fbaSPyun YongHyeon 		return (1);
54595d67482SBill Paul 	}
54695d67482SBill Paul 
54795d67482SBill Paul 	/* Get result. */
54895d67482SBill Paul 	byte = CSR_READ_4(sc, BGE_EE_DATA);
54995d67482SBill Paul 
5506098821cSJung-uk Kim 	*dest = (byte >> ((addr % 4) * 8)) & 0xff;
55195d67482SBill Paul 
55295d67482SBill Paul 	return (0);
55395d67482SBill Paul }
55495d67482SBill Paul 
55595d67482SBill Paul /*
55695d67482SBill Paul  * Read a sequence of bytes from the EEPROM.
55795d67482SBill Paul  */
55895d67482SBill Paul static int
5593f74909aSGleb Smirnoff bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
56095d67482SBill Paul {
5613f74909aSGleb Smirnoff 	int i, error = 0;
5623f74909aSGleb Smirnoff 	uint8_t byte = 0;
56395d67482SBill Paul 
56495d67482SBill Paul 	for (i = 0; i < cnt; i++) {
5653f74909aSGleb Smirnoff 		error = bge_eeprom_getbyte(sc, off + i, &byte);
5663f74909aSGleb Smirnoff 		if (error)
56795d67482SBill Paul 			break;
56895d67482SBill Paul 		*(dest + i) = byte;
56995d67482SBill Paul 	}
57095d67482SBill Paul 
5713f74909aSGleb Smirnoff 	return (error ? 1 : 0);
57295d67482SBill Paul }
57395d67482SBill Paul 
57495d67482SBill Paul static int
5753f74909aSGleb Smirnoff bge_miibus_readreg(device_t dev, int phy, int reg)
57695d67482SBill Paul {
57795d67482SBill Paul 	struct bge_softc *sc;
5783f74909aSGleb Smirnoff 	uint32_t val, autopoll;
57995d67482SBill Paul 	int i;
58095d67482SBill Paul 
58195d67482SBill Paul 	sc = device_get_softc(dev);
58295d67482SBill Paul 
5830434d1b8SBill Paul 	/*
5840434d1b8SBill Paul 	 * Broadcom's own driver always assumes the internal
5850434d1b8SBill Paul 	 * PHY is at GMII address 1. On some chips, the PHY responds
5860434d1b8SBill Paul 	 * to accesses at all addresses, which could cause us to
5870434d1b8SBill Paul 	 * bogusly attach the PHY 32 times at probe type. Always
5880434d1b8SBill Paul 	 * restricting the lookup to address 1 is simpler than
5890434d1b8SBill Paul 	 * trying to figure out which chips revisions should be
5900434d1b8SBill Paul 	 * special-cased.
5910434d1b8SBill Paul 	 */
592b1265c1aSJohn Polstra 	if (phy != 1)
59398b28ee5SBill Paul 		return (0);
59498b28ee5SBill Paul 
59537ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
59637ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
59737ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
59837ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
59937ceeb4dSPaul Saab 		DELAY(40);
60037ceeb4dSPaul Saab 	}
60137ceeb4dSPaul Saab 
60295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
60395d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
60495d67482SBill Paul 
60595d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
60695d67482SBill Paul 		val = CSR_READ_4(sc, BGE_MI_COMM);
60795d67482SBill Paul 		if (!(val & BGE_MICOMM_BUSY))
60895d67482SBill Paul 			break;
60995d67482SBill Paul 	}
61095d67482SBill Paul 
61195d67482SBill Paul 	if (i == BGE_TIMEOUT) {
6126b9f5c94SGleb Smirnoff 		device_printf(sc->bge_dev, "PHY read timed out\n");
61337ceeb4dSPaul Saab 		val = 0;
61437ceeb4dSPaul Saab 		goto done;
61595d67482SBill Paul 	}
61695d67482SBill Paul 
61795d67482SBill Paul 	val = CSR_READ_4(sc, BGE_MI_COMM);
61895d67482SBill Paul 
61937ceeb4dSPaul Saab done:
62037ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
62137ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
62237ceeb4dSPaul Saab 		DELAY(40);
62337ceeb4dSPaul Saab 	}
62437ceeb4dSPaul Saab 
62595d67482SBill Paul 	if (val & BGE_MICOMM_READFAIL)
62695d67482SBill Paul 		return (0);
62795d67482SBill Paul 
6286098821cSJung-uk Kim 	return (val & 0xffff);
62995d67482SBill Paul }
63095d67482SBill Paul 
63195d67482SBill Paul static int
6323f74909aSGleb Smirnoff bge_miibus_writereg(device_t dev, int phy, int reg, int val)
63395d67482SBill Paul {
63495d67482SBill Paul 	struct bge_softc *sc;
6353f74909aSGleb Smirnoff 	uint32_t autopoll;
63695d67482SBill Paul 	int i;
63795d67482SBill Paul 
63895d67482SBill Paul 	sc = device_get_softc(dev);
63995d67482SBill Paul 
64037ceeb4dSPaul Saab 	/* Reading with autopolling on may trigger PCI errors */
64137ceeb4dSPaul Saab 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
64237ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
64337ceeb4dSPaul Saab 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
64437ceeb4dSPaul Saab 		DELAY(40);
64537ceeb4dSPaul Saab 	}
64637ceeb4dSPaul Saab 
64795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
64895d67482SBill Paul 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
64995d67482SBill Paul 
65095d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
65195d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
65295d67482SBill Paul 			break;
65395d67482SBill Paul 	}
65495d67482SBill Paul 
65537ceeb4dSPaul Saab 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
65637ceeb4dSPaul Saab 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
65737ceeb4dSPaul Saab 		DELAY(40);
65837ceeb4dSPaul Saab 	}
65937ceeb4dSPaul Saab 
66095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
6616b9f5c94SGleb Smirnoff 		device_printf(sc->bge_dev, "PHY read timed out\n");
66295d67482SBill Paul 		return (0);
66395d67482SBill Paul 	}
66495d67482SBill Paul 
66595d67482SBill Paul 	return (0);
66695d67482SBill Paul }
66795d67482SBill Paul 
66895d67482SBill Paul static void
6693f74909aSGleb Smirnoff bge_miibus_statchg(device_t dev)
67095d67482SBill Paul {
67195d67482SBill Paul 	struct bge_softc *sc;
67295d67482SBill Paul 	struct mii_data *mii;
67395d67482SBill Paul 	sc = device_get_softc(dev);
67495d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
67595d67482SBill Paul 
67695d67482SBill Paul 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
6773f74909aSGleb Smirnoff 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)
67895d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
6793f74909aSGleb Smirnoff 	else
68095d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
68195d67482SBill Paul 
6823f74909aSGleb Smirnoff 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
68395d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
6843f74909aSGleb Smirnoff 	else
68595d67482SBill Paul 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
68695d67482SBill Paul }
68795d67482SBill Paul 
68895d67482SBill Paul /*
68995d67482SBill Paul  * Intialize a standard receive ring descriptor.
69095d67482SBill Paul  */
69195d67482SBill Paul static int
6923f74909aSGleb Smirnoff bge_newbuf_std(struct bge_softc *sc, int i, struct mbuf *m)
69395d67482SBill Paul {
69495d67482SBill Paul 	struct mbuf *m_new = NULL;
69595d67482SBill Paul 	struct bge_rx_bd *r;
696f41ac2beSBill Paul 	struct bge_dmamap_arg ctx;
697f41ac2beSBill Paul 	int error;
69895d67482SBill Paul 
69995d67482SBill Paul 	if (m == NULL) {
700c3a56752SGleb Smirnoff 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
701c3a56752SGleb Smirnoff 		if (m_new == NULL)
70295d67482SBill Paul 			return (ENOBUFS);
70395d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
70495d67482SBill Paul 	} else {
70595d67482SBill Paul 		m_new = m;
70695d67482SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
70795d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
70895d67482SBill Paul 	}
70995d67482SBill Paul 
710652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
71195d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
71295d67482SBill Paul 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
713f41ac2beSBill Paul 	r = &sc->bge_ldata.bge_rx_std_ring[i];
714f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
715f41ac2beSBill Paul 	ctx.sc = sc;
716f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
717f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
718f41ac2beSBill Paul 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
719f41ac2beSBill Paul 	if (error || ctx.bge_maxsegs == 0) {
720f7cea149SGleb Smirnoff 		if (m == NULL) {
721f7cea149SGleb Smirnoff 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
722f41ac2beSBill Paul 			m_freem(m_new);
723f7cea149SGleb Smirnoff 		}
724f41ac2beSBill Paul 		return (ENOMEM);
725f41ac2beSBill Paul 	}
726e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(ctx.bge_busaddr);
727e907febfSPyun YongHyeon 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(ctx.bge_busaddr);
728e907febfSPyun YongHyeon 	r->bge_flags = BGE_RXBDFLAG_END;
729e907febfSPyun YongHyeon 	r->bge_len = m_new->m_len;
730e907febfSPyun YongHyeon 	r->bge_idx = i;
731f41ac2beSBill Paul 
732f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
733f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_dmamap[i],
734f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
73595d67482SBill Paul 
73695d67482SBill Paul 	return (0);
73795d67482SBill Paul }
73895d67482SBill Paul 
73995d67482SBill Paul /*
74095d67482SBill Paul  * Initialize a jumbo receive ring descriptor. This allocates
74195d67482SBill Paul  * a jumbo buffer from the pool managed internally by the driver.
74295d67482SBill Paul  */
74395d67482SBill Paul static int
7443f74909aSGleb Smirnoff bge_newbuf_jumbo(struct bge_softc *sc, int i, struct mbuf *m)
74595d67482SBill Paul {
7461be6acb7SGleb Smirnoff 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
7471be6acb7SGleb Smirnoff 	struct bge_extrx_bd *r;
74895d67482SBill Paul 	struct mbuf *m_new = NULL;
7491be6acb7SGleb Smirnoff 	int nsegs;
750f41ac2beSBill Paul 	int error;
75195d67482SBill Paul 
75295d67482SBill Paul 	if (m == NULL) {
753a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
7541be6acb7SGleb Smirnoff 		if (m_new == NULL)
75595d67482SBill Paul 			return (ENOBUFS);
75695d67482SBill Paul 
7571be6acb7SGleb Smirnoff 		m_cljget(m_new, M_DONTWAIT, MJUM9BYTES);
7581be6acb7SGleb Smirnoff 		if (!(m_new->m_flags & M_EXT)) {
75995d67482SBill Paul 			m_freem(m_new);
76095d67482SBill Paul 			return (ENOBUFS);
76195d67482SBill Paul 		}
7621be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
76395d67482SBill Paul 	} else {
76495d67482SBill Paul 		m_new = m;
7651be6acb7SGleb Smirnoff 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
76695d67482SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
76795d67482SBill Paul 	}
76895d67482SBill Paul 
769652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
77095d67482SBill Paul 		m_adj(m_new, ETHER_ALIGN);
7711be6acb7SGleb Smirnoff 
7721be6acb7SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
7731be6acb7SGleb Smirnoff 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
7741be6acb7SGleb Smirnoff 	    m_new, segs, &nsegs, BUS_DMA_NOWAIT);
7751be6acb7SGleb Smirnoff 	if (error) {
7761be6acb7SGleb Smirnoff 		if (m == NULL)
777f41ac2beSBill Paul 			m_freem(m_new);
7781be6acb7SGleb Smirnoff 		return (error);
779f7cea149SGleb Smirnoff 	}
7801be6acb7SGleb Smirnoff 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
7811be6acb7SGleb Smirnoff 
7821be6acb7SGleb Smirnoff 	/*
7831be6acb7SGleb Smirnoff 	 * Fill in the extended RX buffer descriptor.
7841be6acb7SGleb Smirnoff 	 */
7851be6acb7SGleb Smirnoff 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
7864e7ba1abSGleb Smirnoff 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
7874e7ba1abSGleb Smirnoff 	r->bge_idx = i;
7884e7ba1abSGleb Smirnoff 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
7894e7ba1abSGleb Smirnoff 	switch (nsegs) {
7904e7ba1abSGleb Smirnoff 	case 4:
7914e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
7924e7ba1abSGleb Smirnoff 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
7934e7ba1abSGleb Smirnoff 		r->bge_len3 = segs[3].ds_len;
7944e7ba1abSGleb Smirnoff 	case 3:
795e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
796e907febfSPyun YongHyeon 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
797e907febfSPyun YongHyeon 		r->bge_len2 = segs[2].ds_len;
7984e7ba1abSGleb Smirnoff 	case 2:
7994e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
8004e7ba1abSGleb Smirnoff 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
8014e7ba1abSGleb Smirnoff 		r->bge_len1 = segs[1].ds_len;
8024e7ba1abSGleb Smirnoff 	case 1:
8034e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
8044e7ba1abSGleb Smirnoff 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
8054e7ba1abSGleb Smirnoff 		r->bge_len0 = segs[0].ds_len;
8064e7ba1abSGleb Smirnoff 		break;
8074e7ba1abSGleb Smirnoff 	default:
8084e7ba1abSGleb Smirnoff 		panic("%s: %d segments\n", __func__, nsegs);
8094e7ba1abSGleb Smirnoff 	}
810f41ac2beSBill Paul 
811f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
812f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
813f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD);
81495d67482SBill Paul 
81595d67482SBill Paul 	return (0);
81695d67482SBill Paul }
81795d67482SBill Paul 
81895d67482SBill Paul /*
81995d67482SBill Paul  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
82095d67482SBill Paul  * that's 1MB or memory, which is a lot. For now, we fill only the first
82195d67482SBill Paul  * 256 ring entries and hope that our CPU is fast enough to keep up with
82295d67482SBill Paul  * the NIC.
82395d67482SBill Paul  */
82495d67482SBill Paul static int
8253f74909aSGleb Smirnoff bge_init_rx_ring_std(struct bge_softc *sc)
82695d67482SBill Paul {
82795d67482SBill Paul 	int i;
82895d67482SBill Paul 
82995d67482SBill Paul 	for (i = 0; i < BGE_SSLOTS; i++) {
83095d67482SBill Paul 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
83195d67482SBill Paul 			return (ENOBUFS);
83295d67482SBill Paul 	};
83395d67482SBill Paul 
834f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
835f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map,
836f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
837f41ac2beSBill Paul 
83895d67482SBill Paul 	sc->bge_std = i - 1;
83995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
84095d67482SBill Paul 
84195d67482SBill Paul 	return (0);
84295d67482SBill Paul }
84395d67482SBill Paul 
84495d67482SBill Paul static void
8453f74909aSGleb Smirnoff bge_free_rx_ring_std(struct bge_softc *sc)
84695d67482SBill Paul {
84795d67482SBill Paul 	int i;
84895d67482SBill Paul 
84995d67482SBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
85095d67482SBill Paul 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
851e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
852e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_std_dmamap[i],
853e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
854f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
855f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
856e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
857e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
85895d67482SBill Paul 		}
859f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
86095d67482SBill Paul 		    sizeof(struct bge_rx_bd));
86195d67482SBill Paul 	}
86295d67482SBill Paul }
86395d67482SBill Paul 
86495d67482SBill Paul static int
8653f74909aSGleb Smirnoff bge_init_rx_ring_jumbo(struct bge_softc *sc)
86695d67482SBill Paul {
86795d67482SBill Paul 	struct bge_rcb *rcb;
8681be6acb7SGleb Smirnoff 	int i;
86995d67482SBill Paul 
87095d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
87195d67482SBill Paul 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
87295d67482SBill Paul 			return (ENOBUFS);
87395d67482SBill Paul 	};
87495d67482SBill Paul 
875f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
876f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
877f41ac2beSBill Paul 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
878f41ac2beSBill Paul 
87995d67482SBill Paul 	sc->bge_jumbo = i - 1;
88095d67482SBill Paul 
881f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
8821be6acb7SGleb Smirnoff 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
8831be6acb7SGleb Smirnoff 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
88467111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
88595d67482SBill Paul 
88695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
88795d67482SBill Paul 
88895d67482SBill Paul 	return (0);
88995d67482SBill Paul }
89095d67482SBill Paul 
89195d67482SBill Paul static void
8923f74909aSGleb Smirnoff bge_free_rx_ring_jumbo(struct bge_softc *sc)
89395d67482SBill Paul {
89495d67482SBill Paul 	int i;
89595d67482SBill Paul 
89695d67482SBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
89795d67482SBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
898e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
899e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
900e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTREAD);
901f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
902f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
903e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
904e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
90595d67482SBill Paul 		}
906f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
9071be6acb7SGleb Smirnoff 		    sizeof(struct bge_extrx_bd));
90895d67482SBill Paul 	}
90995d67482SBill Paul }
91095d67482SBill Paul 
91195d67482SBill Paul static void
9123f74909aSGleb Smirnoff bge_free_tx_ring(struct bge_softc *sc)
91395d67482SBill Paul {
91495d67482SBill Paul 	int i;
91595d67482SBill Paul 
916f41ac2beSBill Paul 	if (sc->bge_ldata.bge_tx_ring == NULL)
91795d67482SBill Paul 		return;
91895d67482SBill Paul 
91995d67482SBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
92095d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
921e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
922e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[i],
923e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
924f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
925f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
926e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
927e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[i] = NULL;
92895d67482SBill Paul 		}
929f41ac2beSBill Paul 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
93095d67482SBill Paul 		    sizeof(struct bge_tx_bd));
93195d67482SBill Paul 	}
93295d67482SBill Paul }
93395d67482SBill Paul 
93495d67482SBill Paul static int
9353f74909aSGleb Smirnoff bge_init_tx_ring(struct bge_softc *sc)
93695d67482SBill Paul {
93795d67482SBill Paul 	sc->bge_txcnt = 0;
93895d67482SBill Paul 	sc->bge_tx_saved_considx = 0;
9393927098fSPaul Saab 
94014bbd30fSGleb Smirnoff 	/* Initialize transmit producer index for host-memory send ring. */
94114bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = 0;
94214bbd30fSGleb Smirnoff 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
94314bbd30fSGleb Smirnoff 
9443927098fSPaul Saab 	/* 5700 b2 errata */
945e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
94614bbd30fSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
9473927098fSPaul Saab 
94814bbd30fSGleb Smirnoff 	/* NIC-memory send ring not used; initialize to zero. */
9493927098fSPaul Saab 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
9503927098fSPaul Saab 	/* 5700 b2 errata */
951e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
95295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
95395d67482SBill Paul 
95495d67482SBill Paul 	return (0);
95595d67482SBill Paul }
95695d67482SBill Paul 
95795d67482SBill Paul static void
9583e9b1bcaSJung-uk Kim bge_setpromisc(struct bge_softc *sc)
9593e9b1bcaSJung-uk Kim {
9603e9b1bcaSJung-uk Kim 	struct ifnet *ifp;
9613e9b1bcaSJung-uk Kim 
9623e9b1bcaSJung-uk Kim 	BGE_LOCK_ASSERT(sc);
9633e9b1bcaSJung-uk Kim 
9643e9b1bcaSJung-uk Kim 	ifp = sc->bge_ifp;
9653e9b1bcaSJung-uk Kim 
96645ee6ab3SJung-uk Kim 	/* Enable or disable promiscuous mode as needed. */
9673e9b1bcaSJung-uk Kim 	if (ifp->if_flags & IFF_PROMISC)
96845ee6ab3SJung-uk Kim 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
9693e9b1bcaSJung-uk Kim 	else
97045ee6ab3SJung-uk Kim 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
9713e9b1bcaSJung-uk Kim }
9723e9b1bcaSJung-uk Kim 
9733e9b1bcaSJung-uk Kim static void
9743f74909aSGleb Smirnoff bge_setmulti(struct bge_softc *sc)
97595d67482SBill Paul {
97695d67482SBill Paul 	struct ifnet *ifp;
97795d67482SBill Paul 	struct ifmultiaddr *ifma;
9783f74909aSGleb Smirnoff 	uint32_t hashes[4] = { 0, 0, 0, 0 };
97995d67482SBill Paul 	int h, i;
98095d67482SBill Paul 
9810f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
9820f9bd73bSSam Leffler 
983fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
98495d67482SBill Paul 
98595d67482SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
98695d67482SBill Paul 		for (i = 0; i < 4; i++)
9876098821cSJung-uk Kim 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xffffffff);
98895d67482SBill Paul 		return;
98995d67482SBill Paul 	}
99095d67482SBill Paul 
99195d67482SBill Paul 	/* First, zot all the existing filters. */
99295d67482SBill Paul 	for (i = 0; i < 4; i++)
99395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
99495d67482SBill Paul 
99595d67482SBill Paul 	/* Now program new ones. */
99613b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
99795d67482SBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
99895d67482SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
99995d67482SBill Paul 			continue;
10000e939c0cSChristian Weisgerber 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
10016098821cSJung-uk Kim 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7f;
10026098821cSJung-uk Kim 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1f);
100395d67482SBill Paul 	}
100413b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
100595d67482SBill Paul 
100695d67482SBill Paul 	for (i = 0; i < 4; i++)
100795d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
100895d67482SBill Paul }
100995d67482SBill Paul 
10108cb1383cSDoug Ambrisko static void
10118cb1383cSDoug Ambrisko bge_sig_pre_reset(sc, type)
10128cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10138cb1383cSDoug Ambrisko 	int type;
10148cb1383cSDoug Ambrisko {
10158cb1383cSDoug Ambrisko 	/*
10168cb1383cSDoug Ambrisko 	 * Some chips don't like this so only do this if ASF is enabled
10178cb1383cSDoug Ambrisko 	 */
10188cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode)
10198cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
10208cb1383cSDoug Ambrisko 
10218cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
10228cb1383cSDoug Ambrisko 		switch (type) {
10238cb1383cSDoug Ambrisko 		case BGE_RESET_START:
10248cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
10258cb1383cSDoug Ambrisko 			break;
10268cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
10278cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
10288cb1383cSDoug Ambrisko 			break;
10298cb1383cSDoug Ambrisko 		}
10308cb1383cSDoug Ambrisko 	}
10318cb1383cSDoug Ambrisko }
10328cb1383cSDoug Ambrisko 
10338cb1383cSDoug Ambrisko static void
10348cb1383cSDoug Ambrisko bge_sig_post_reset(sc, type)
10358cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10368cb1383cSDoug Ambrisko 	int type;
10378cb1383cSDoug Ambrisko {
10388cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
10398cb1383cSDoug Ambrisko 		switch (type) {
10408cb1383cSDoug Ambrisko 		case BGE_RESET_START:
10418cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000001);
10428cb1383cSDoug Ambrisko 			/* START DONE */
10438cb1383cSDoug Ambrisko 			break;
10448cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
10458cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000002);
10468cb1383cSDoug Ambrisko 			break;
10478cb1383cSDoug Ambrisko 		}
10488cb1383cSDoug Ambrisko 	}
10498cb1383cSDoug Ambrisko }
10508cb1383cSDoug Ambrisko 
10518cb1383cSDoug Ambrisko static void
10528cb1383cSDoug Ambrisko bge_sig_legacy(sc, type)
10538cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10548cb1383cSDoug Ambrisko 	int type;
10558cb1383cSDoug Ambrisko {
10568cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
10578cb1383cSDoug Ambrisko 		switch (type) {
10588cb1383cSDoug Ambrisko 		case BGE_RESET_START:
10598cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
10608cb1383cSDoug Ambrisko 			break;
10618cb1383cSDoug Ambrisko 		case BGE_RESET_STOP:
10628cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
10638cb1383cSDoug Ambrisko 			break;
10648cb1383cSDoug Ambrisko 		}
10658cb1383cSDoug Ambrisko 	}
10668cb1383cSDoug Ambrisko }
10678cb1383cSDoug Ambrisko 
10688cb1383cSDoug Ambrisko void bge_stop_fw(struct bge_softc *);
10698cb1383cSDoug Ambrisko void
10708cb1383cSDoug Ambrisko bge_stop_fw(sc)
10718cb1383cSDoug Ambrisko 	struct bge_softc *sc;
10728cb1383cSDoug Ambrisko {
10738cb1383cSDoug Ambrisko 	int i;
10748cb1383cSDoug Ambrisko 
10758cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode) {
10768cb1383cSDoug Ambrisko 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW, BGE_FW_PAUSE);
10778cb1383cSDoug Ambrisko 		CSR_WRITE_4(sc, BGE_CPU_EVENT,
10788cb1383cSDoug Ambrisko 			    CSR_READ_4(sc, BGE_CPU_EVENT) != (1 << 14));
10798cb1383cSDoug Ambrisko 
10808cb1383cSDoug Ambrisko 		for (i = 0; i < 100; i++ ) {
10818cb1383cSDoug Ambrisko 			if (!(CSR_READ_4(sc, BGE_CPU_EVENT) & (1 << 14)))
10828cb1383cSDoug Ambrisko 				break;
10838cb1383cSDoug Ambrisko 			DELAY(10);
10848cb1383cSDoug Ambrisko 		}
10858cb1383cSDoug Ambrisko 	}
10868cb1383cSDoug Ambrisko }
10878cb1383cSDoug Ambrisko 
108895d67482SBill Paul /*
108995d67482SBill Paul  * Do endian, PCI and DMA initialization. Also check the on-board ROM
109095d67482SBill Paul  * self-test results.
109195d67482SBill Paul  */
109295d67482SBill Paul static int
10933f74909aSGleb Smirnoff bge_chipinit(struct bge_softc *sc)
109495d67482SBill Paul {
10953f74909aSGleb Smirnoff 	uint32_t dma_rw_ctl;
109695d67482SBill Paul 	int i;
109795d67482SBill Paul 
10988cb1383cSDoug Ambrisko 	/* Set endianness before we access any non-PCI registers. */
1099e907febfSPyun YongHyeon 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
110095d67482SBill Paul 
110195d67482SBill Paul 	/*
110295d67482SBill Paul 	 * Check the 'ROM failed' bit on the RX CPU to see if
110395d67482SBill Paul 	 * self-tests passed.
110495d67482SBill Paul 	 */
110595d67482SBill Paul 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
11066098821cSJung-uk Kim 		device_printf(sc->bge_dev,
11076098821cSJung-uk Kim 		    "RX CPU self-diagnostics failed!\n");
110895d67482SBill Paul 		return (ENODEV);
110995d67482SBill Paul 	}
111095d67482SBill Paul 
111195d67482SBill Paul 	/* Clear the MAC control register */
111295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
111395d67482SBill Paul 
111495d67482SBill Paul 	/*
111595d67482SBill Paul 	 * Clear the MAC statistics block in the NIC's
111695d67482SBill Paul 	 * internal memory.
111795d67482SBill Paul 	 */
111895d67482SBill Paul 	for (i = BGE_STATS_BLOCK;
11193f74909aSGleb Smirnoff 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
112095d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
112195d67482SBill Paul 
112295d67482SBill Paul 	for (i = BGE_STATUS_BLOCK;
11233f74909aSGleb Smirnoff 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
112495d67482SBill Paul 		BGE_MEMWIN_WRITE(sc, i, 0);
112595d67482SBill Paul 
112695d67482SBill Paul 	/* Set up the PCI DMA control register. */
1127652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
11284c0da0ffSGleb Smirnoff 		/* PCI Express bus */
1129e53d81eeSPaul Saab 		dma_rw_ctl = BGE_PCI_READ_CMD | BGE_PCI_WRITE_CMD |
1130797b2220SJung-uk Kim 		    BGE_PCIDMARWCTL_RD_WAT_SHIFT(0xf) |
1131797b2220SJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(0x2);
1132652ae483SGleb Smirnoff 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
11338287860eSJohn Polstra 		/* PCI-X bus */
11344c0da0ffSGleb Smirnoff 		if (BGE_IS_5714_FAMILY(sc)) {
11354c0da0ffSGleb Smirnoff 			dma_rw_ctl = BGE_PCI_READ_CMD | BGE_PCI_WRITE_CMD;
11364c0da0ffSGleb Smirnoff 			dma_rw_ctl &= ~BGE_PCIDMARWCTL_ONEDMA_ATONCE; /* XXX */
11374c0da0ffSGleb Smirnoff 			/* XXX magic values, Broadcom-supplied Linux driver */
11386098821cSJung-uk Kim 			dma_rw_ctl |= (1 << 20) | (1 << 18);
11394c0da0ffSGleb Smirnoff 			if (sc->bge_asicrev == BGE_ASICREV_BCM5780)
11406098821cSJung-uk Kim 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
11414c0da0ffSGleb Smirnoff 			else
11426098821cSJung-uk Kim 				dma_rw_ctl |= (1 << 15);
11434c0da0ffSGleb Smirnoff 
11444c0da0ffSGleb Smirnoff 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
11455cba12d3SPaul Saab 			/*
11465cba12d3SPaul Saab 			 * The 5704 uses a different encoding of read/write
11475cba12d3SPaul Saab 			 * watermarks.
11485cba12d3SPaul Saab 			 */
11495cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD | BGE_PCI_WRITE_CMD |
1150797b2220SJung-uk Kim 			    BGE_PCIDMARWCTL_RD_WAT_SHIFT(0x7) |
1151797b2220SJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(0x3);
11525cba12d3SPaul Saab 		else
11535cba12d3SPaul Saab 			dma_rw_ctl = BGE_PCI_READ_CMD | BGE_PCI_WRITE_CMD |
1154797b2220SJung-uk Kim 			    BGE_PCIDMARWCTL_RD_WAT_SHIFT(0x3) |
1155797b2220SJung-uk Kim 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(0x3) |
1156797b2220SJung-uk Kim 			    0x0f;
11575cba12d3SPaul Saab 
11585cba12d3SPaul Saab 		/*
11595cba12d3SPaul Saab 		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
11605cba12d3SPaul Saab 		 * for hardware bugs.
11615cba12d3SPaul Saab 		 */
1162e0ced696SPaul Saab 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1163e0ced696SPaul Saab 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
11643f74909aSGleb Smirnoff 			uint32_t tmp;
11655cba12d3SPaul Saab 
11665cba12d3SPaul Saab 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
11675cba12d3SPaul Saab 			if (tmp == 0x6 || tmp == 0x7)
11685cba12d3SPaul Saab 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
11698287860eSJohn Polstra 		}
11704c0da0ffSGleb Smirnoff 	} else
11714c0da0ffSGleb Smirnoff 		/* Conventional PCI bus */
11724c0da0ffSGleb Smirnoff 		dma_rw_ctl = BGE_PCI_READ_CMD | BGE_PCI_WRITE_CMD |
1173797b2220SJung-uk Kim 		    BGE_PCIDMARWCTL_RD_WAT_SHIFT(0x7) |
1174797b2220SJung-uk Kim 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(0x7) |
1175797b2220SJung-uk Kim 		    0x0f;
11765cba12d3SPaul Saab 
1177e0ced696SPaul Saab 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
11780434d1b8SBill Paul 	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
11794c0da0ffSGleb Smirnoff 	    sc->bge_asicrev == BGE_ASICREV_BCM5705)
11805cba12d3SPaul Saab 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
11815cba12d3SPaul Saab 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
118295d67482SBill Paul 
118395d67482SBill Paul 	/*
118495d67482SBill Paul 	 * Set up general mode register.
118595d67482SBill Paul 	 */
1186e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
118795d67482SBill Paul 	    BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1188ee7ef91cSOleg Bulyzhin 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
118995d67482SBill Paul 
119095d67482SBill Paul 	/*
11918cb1383cSDoug Ambrisko 	 * Tell the firmware the driver is running
11928cb1383cSDoug Ambrisko 	 */
11938cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
11948cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
11958cb1383cSDoug Ambrisko 
11968cb1383cSDoug Ambrisko 	/*
1197ea13bdd5SJohn Polstra 	 * Disable memory write invalidate.  Apparently it is not supported
1198ea13bdd5SJohn Polstra 	 * properly by these devices.
119995d67482SBill Paul 	 */
1200ea13bdd5SJohn Polstra 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
120195d67482SBill Paul 
120295d67482SBill Paul #ifdef __brokenalpha__
120395d67482SBill Paul 	/*
120495d67482SBill Paul 	 * Must insure that we do not cross an 8K (bytes) boundary
120595d67482SBill Paul 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
120695d67482SBill Paul 	 * restriction on some ALPHA platforms with early revision
120795d67482SBill Paul 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
120895d67482SBill Paul 	 */
120962f1ea9cSJohn Polstra 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
121062f1ea9cSJohn Polstra 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
121195d67482SBill Paul #endif
121295d67482SBill Paul 
121395d67482SBill Paul 	/* Set the timer prescaler (always 66Mhz) */
12146098821cSJung-uk Kim 	CSR_WRITE_4(sc, BGE_MISC_CFG, (65 << 1)); /* BGE_32BITTIME_66MHZ */
121595d67482SBill Paul 
121695d67482SBill Paul 	return (0);
121795d67482SBill Paul }
121895d67482SBill Paul 
121995d67482SBill Paul static int
12203f74909aSGleb Smirnoff bge_blockinit(struct bge_softc *sc)
122195d67482SBill Paul {
122295d67482SBill Paul 	struct bge_rcb *rcb;
1223e907febfSPyun YongHyeon 	bus_size_t vrcb;
1224e907febfSPyun YongHyeon 	bge_hostaddr taddr;
12256f8718a3SScott Long 	uint32_t val;
122695d67482SBill Paul 	int i;
122795d67482SBill Paul 
122895d67482SBill Paul 	/*
122995d67482SBill Paul 	 * Initialize the memory window pointer register so that
123095d67482SBill Paul 	 * we can access the first 32K of internal NIC RAM. This will
123195d67482SBill Paul 	 * allow us to set up the TX send ring RCBs and the RX return
123295d67482SBill Paul 	 * ring RCBs, plus other things which live in NIC memory.
123395d67482SBill Paul 	 */
123495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
123595d67482SBill Paul 
1236822f63fcSBill Paul 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1237822f63fcSBill Paul 
12387ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
123995d67482SBill Paul 		/* Configure mbuf memory pool */
12400dae9719SJung-uk Kim 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1241822f63fcSBill Paul 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1242822f63fcSBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1243822f63fcSBill Paul 		else
124495d67482SBill Paul 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
124595d67482SBill Paul 
124695d67482SBill Paul 		/* Configure DMA resource pool */
12470434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
12480434d1b8SBill Paul 		    BGE_DMA_DESCRIPTORS);
124995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
12500434d1b8SBill Paul 	}
125195d67482SBill Paul 
125295d67482SBill Paul 	/* Configure mbuf pool watermarks */
12537ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
12540434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
12550434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
12560434d1b8SBill Paul 	} else {
1257fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1258fa228020SPaul Saab 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
12590434d1b8SBill Paul 	}
1260fa228020SPaul Saab 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
126195d67482SBill Paul 
126295d67482SBill Paul 	/* Configure DMA resource watermarks */
126395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
126495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
126595d67482SBill Paul 
126695d67482SBill Paul 	/* Enable buffer manager */
12677ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
126895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
126995d67482SBill Paul 		    BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN);
127095d67482SBill Paul 
127195d67482SBill Paul 		/* Poll for buffer manager start indication */
127295d67482SBill Paul 		for (i = 0; i < BGE_TIMEOUT; i++) {
12736098821cSJung-uk Kim 			if (CSR_READ_4(sc, BGE_BMAN_MODE) &
12746098821cSJung-uk Kim 			    BGE_BMANMODE_ENABLE)
127595d67482SBill Paul 				break;
127695d67482SBill Paul 			DELAY(10);
127795d67482SBill Paul 		}
127895d67482SBill Paul 
127995d67482SBill Paul 		if (i == BGE_TIMEOUT) {
1280fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1281fe806fdaSPyun YongHyeon 			    "buffer manager failed to start\n");
128295d67482SBill Paul 			return (ENXIO);
128395d67482SBill Paul 		}
12840434d1b8SBill Paul 	}
128595d67482SBill Paul 
128695d67482SBill Paul 	/* Enable flow-through queues */
12876098821cSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xffffffff);
128895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
128995d67482SBill Paul 
129095d67482SBill Paul 	/* Wait until queue initialization is complete */
129195d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
129295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
129395d67482SBill Paul 			break;
129495d67482SBill Paul 		DELAY(10);
129595d67482SBill Paul 	}
129695d67482SBill Paul 
129795d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1298fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
129995d67482SBill Paul 		return (ENXIO);
130095d67482SBill Paul 	}
130195d67482SBill Paul 
130295d67482SBill Paul 	/* Initialize the standard RX ring control block */
1303f41ac2beSBill Paul 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1304f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_lo =
1305f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1306f41ac2beSBill Paul 	rcb->bge_hostaddr.bge_addr_hi =
1307f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1308f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1309f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
13107ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
13110434d1b8SBill Paul 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
13120434d1b8SBill Paul 	else
13130434d1b8SBill Paul 		rcb->bge_maxlen_flags =
13140434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
131595d67482SBill Paul 	rcb->bge_nicaddr = BGE_STD_RX_RINGS;
13166098821cSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI,
13176098821cSJung-uk Kim 	    rcb->bge_hostaddr.bge_addr_hi);
13186098821cSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO,
13196098821cSJung-uk Kim 	    rcb->bge_hostaddr.bge_addr_lo);
1320f41ac2beSBill Paul 
132167111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
132267111612SJohn Polstra 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
132395d67482SBill Paul 
132495d67482SBill Paul 	/*
132595d67482SBill Paul 	 * Initialize the jumbo RX ring control block
132695d67482SBill Paul 	 * We set the 'ring disabled' bit in the flags
132795d67482SBill Paul 	 * field until we're actually ready to start
132895d67482SBill Paul 	 * using this ring (i.e. once we set the MTU
132995d67482SBill Paul 	 * high enough to require it).
133095d67482SBill Paul 	 */
13314c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1332f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1333f41ac2beSBill Paul 
1334f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_lo =
1335f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1336f41ac2beSBill Paul 		rcb->bge_hostaddr.bge_addr_hi =
1337f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1338f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1339f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1340f41ac2beSBill Paul 		    BUS_DMASYNC_PREREAD);
13411be6acb7SGleb Smirnoff 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
13421be6acb7SGleb Smirnoff 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
134395d67482SBill Paul 		rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
134467111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
134567111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_hi);
134667111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
134767111612SJohn Polstra 		    rcb->bge_hostaddr.bge_addr_lo);
1348f41ac2beSBill Paul 
13490434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
13500434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
135167111612SJohn Polstra 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
135295d67482SBill Paul 
135395d67482SBill Paul 		/* Set up dummy disabled mini ring RCB */
1354f41ac2beSBill Paul 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
135567111612SJohn Polstra 		rcb->bge_maxlen_flags =
135667111612SJohn Polstra 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
13570434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
13580434d1b8SBill Paul 		    rcb->bge_maxlen_flags);
13590434d1b8SBill Paul 	}
136095d67482SBill Paul 
136195d67482SBill Paul 	/*
136295d67482SBill Paul 	 * Set the BD ring replentish thresholds. The recommended
136395d67482SBill Paul 	 * values are 1/8th the number of descriptors allocated to
136495d67482SBill Paul 	 * each ring.
13659ba784dbSScott Long 	 * XXX The 5754 requires a lower threshold, so it might be a
13669ba784dbSScott Long 	 * requirement of all 575x family chips.  The Linux driver sets
13679ba784dbSScott Long 	 * the lower threshold for all 5705 family chips as well, but there
13689ba784dbSScott Long 	 * are reports that it might not need to be so strict.
136995d67482SBill Paul 	 */
13705345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
13716f8718a3SScott Long 		val = 8;
13726f8718a3SScott Long 	else
13736f8718a3SScott Long 		val = BGE_STD_RX_RING_CNT / 8;
13746f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
137595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
137695d67482SBill Paul 
137795d67482SBill Paul 	/*
137895d67482SBill Paul 	 * Disable all unused send rings by setting the 'ring disabled'
137995d67482SBill Paul 	 * bit in the flags field of all the TX send ring control blocks.
138095d67482SBill Paul 	 * These are located in NIC memory.
138195d67482SBill Paul 	 */
1382e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
138395d67482SBill Paul 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1384e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1385e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1386e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1387e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
138895d67482SBill Paul 	}
138995d67482SBill Paul 
139095d67482SBill Paul 	/* Configure TX RCB 0 (we use only the first ring) */
1391e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1392e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1393e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1394e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1395e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1396e907febfSPyun YongHyeon 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
13977ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
1398e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1399e907febfSPyun YongHyeon 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
140095d67482SBill Paul 
140195d67482SBill Paul 	/* Disable all unused RX return rings */
1402e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
140395d67482SBill Paul 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1404e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1405e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1406e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
14070434d1b8SBill Paul 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1408e907febfSPyun YongHyeon 		    BGE_RCB_FLAG_RING_DISABLED));
1409e907febfSPyun YongHyeon 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
141095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
14113f74909aSGleb Smirnoff 		    (i * (sizeof(uint64_t))), 0);
1412e907febfSPyun YongHyeon 		vrcb += sizeof(struct bge_rcb);
141395d67482SBill Paul 	}
141495d67482SBill Paul 
141595d67482SBill Paul 	/* Initialize RX ring indexes */
141695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
141795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
141895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
141995d67482SBill Paul 
142095d67482SBill Paul 	/*
142195d67482SBill Paul 	 * Set up RX return ring 0
142295d67482SBill Paul 	 * Note that the NIC address for RX return rings is 0x00000000.
142395d67482SBill Paul 	 * The return rings live entirely within the host, so the
142495d67482SBill Paul 	 * nicaddr field in the RCB isn't used.
142595d67482SBill Paul 	 */
1426e907febfSPyun YongHyeon 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1427e907febfSPyun YongHyeon 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1428e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1429e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1430e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1431e907febfSPyun YongHyeon 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1432e907febfSPyun YongHyeon 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
143395d67482SBill Paul 
143495d67482SBill Paul 	/* Set random backoff seed for TX */
143595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
14364a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
14374a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
14384a0d6638SRuslan Ermilov 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
143995d67482SBill Paul 	    BGE_TX_BACKOFF_SEED_MASK);
144095d67482SBill Paul 
144195d67482SBill Paul 	/* Set inter-packet gap */
144295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
144395d67482SBill Paul 
144495d67482SBill Paul 	/*
144595d67482SBill Paul 	 * Specify which ring to use for packets that don't match
144695d67482SBill Paul 	 * any RX rules.
144795d67482SBill Paul 	 */
144895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
144995d67482SBill Paul 
145095d67482SBill Paul 	/*
145195d67482SBill Paul 	 * Configure number of RX lists. One interrupt distribution
145295d67482SBill Paul 	 * list, sixteen active lists, one bad frames class.
145395d67482SBill Paul 	 */
145495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
145595d67482SBill Paul 
145695d67482SBill Paul 	/* Inialize RX list placement stats mask. */
14576098821cSJung-uk Kim 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007fffff);
145895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
145995d67482SBill Paul 
146095d67482SBill Paul 	/* Disable host coalescing until we get it set up */
146195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
146295d67482SBill Paul 
146395d67482SBill Paul 	/* Poll to make sure it's shut down. */
146495d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
146595d67482SBill Paul 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
146695d67482SBill Paul 			break;
146795d67482SBill Paul 		DELAY(10);
146895d67482SBill Paul 	}
146995d67482SBill Paul 
147095d67482SBill Paul 	if (i == BGE_TIMEOUT) {
1471fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1472fe806fdaSPyun YongHyeon 		    "host coalescing engine failed to idle\n");
147395d67482SBill Paul 		return (ENXIO);
147495d67482SBill Paul 	}
147595d67482SBill Paul 
147695d67482SBill Paul 	/* Set up host coalescing defaults */
147795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
147895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
147995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
148095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
14817ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
148295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
148395d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
14840434d1b8SBill Paul 	}
1485b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
1486b64728e5SBruce Evans 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
148795d67482SBill Paul 
148895d67482SBill Paul 	/* Set up address of statistics block */
14897ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
1490f41ac2beSBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1491f41ac2beSBill Paul 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
149295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1493f41ac2beSBill Paul 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
14940434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
149595d67482SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
14960434d1b8SBill Paul 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
14970434d1b8SBill Paul 	}
14980434d1b8SBill Paul 
14990434d1b8SBill Paul 	/* Set up address of status block */
1500f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1501f41ac2beSBill Paul 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
150295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1503f41ac2beSBill Paul 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1504f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1505f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
150695d67482SBill Paul 
150795d67482SBill Paul 	/* Turn on host coalescing state machine */
150895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
150995d67482SBill Paul 
151095d67482SBill Paul 	/* Turn on RX BD completion state machine and enable attentions */
151195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
151295d67482SBill Paul 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
151395d67482SBill Paul 
151495d67482SBill Paul 	/* Turn on RX list placement state machine */
151595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
151695d67482SBill Paul 
151795d67482SBill Paul 	/* Turn on RX list selector state machine. */
15187ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
151995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
152095d67482SBill Paul 
152195d67482SBill Paul 	/* Turn on DMA, clear stats */
152295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB |
152395d67482SBill Paul 	    BGE_MACMODE_RXDMA_ENB | BGE_MACMODE_RX_STATS_CLEAR |
152495d67482SBill Paul 	    BGE_MACMODE_TX_STATS_CLEAR | BGE_MACMODE_RX_STATS_ENB |
152595d67482SBill Paul 	    BGE_MACMODE_TX_STATS_ENB | BGE_MACMODE_FRMHDR_DMA_ENB |
1526652ae483SGleb Smirnoff 	    ((sc->bge_flags & BGE_FLAG_TBI) ?
1527652ae483SGleb Smirnoff 	    BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
152895d67482SBill Paul 
152995d67482SBill Paul 	/* Set misc. local control, enable interrupts on attentions */
153095d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
153195d67482SBill Paul 
153295d67482SBill Paul #ifdef notdef
153395d67482SBill Paul 	/* Assert GPIO pins for PHY reset */
153495d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
153595d67482SBill Paul 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
153695d67482SBill Paul 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
153795d67482SBill Paul 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
153895d67482SBill Paul #endif
153995d67482SBill Paul 
154095d67482SBill Paul 	/* Turn on DMA completion state machine */
15417ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
154295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
154395d67482SBill Paul 
15446f8718a3SScott Long 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
15456f8718a3SScott Long 
15466f8718a3SScott Long 	/* Enable host coalescing bug fix. */
15476f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
15486f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5787)
15496f8718a3SScott Long 			val |= (1 << 29);
15506f8718a3SScott Long 
155195d67482SBill Paul 	/* Turn on write DMA state machine */
15526f8718a3SScott Long 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
155395d67482SBill Paul 
155495d67482SBill Paul 	/* Turn on read DMA state machine */
155595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
155695d67482SBill Paul 	    BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS);
155795d67482SBill Paul 
155895d67482SBill Paul 	/* Turn on RX data completion state machine */
155995d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
156095d67482SBill Paul 
156195d67482SBill Paul 	/* Turn on RX BD initiator state machine */
156295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
156395d67482SBill Paul 
156495d67482SBill Paul 	/* Turn on RX data and RX BD initiator state machine */
156595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
156695d67482SBill Paul 
156795d67482SBill Paul 	/* Turn on Mbuf cluster free state machine */
15687ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
156995d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
157095d67482SBill Paul 
157195d67482SBill Paul 	/* Turn on send BD completion state machine */
157295d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
157395d67482SBill Paul 
157495d67482SBill Paul 	/* Turn on send data completion state machine */
157595d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
157695d67482SBill Paul 
157795d67482SBill Paul 	/* Turn on send data initiator state machine */
157895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
157995d67482SBill Paul 
158095d67482SBill Paul 	/* Turn on send BD initiator state machine */
158195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
158295d67482SBill Paul 
158395d67482SBill Paul 	/* Turn on send BD selector state machine */
158495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
158595d67482SBill Paul 
15866098821cSJung-uk Kim 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007fffff);
158795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
158895d67482SBill Paul 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
158995d67482SBill Paul 
159095d67482SBill Paul 	/* ack/clear link change events */
159195d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
15920434d1b8SBill Paul 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
15930434d1b8SBill Paul 	    BGE_MACSTAT_LINK_CHANGED);
1594f41ac2beSBill Paul 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
159595d67482SBill Paul 
159695d67482SBill Paul 	/* Enable PHY auto polling (for MII/GMII only) */
1597652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
159895d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1599a1d52896SBill Paul 	} else {
16006098821cSJung-uk Kim 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL | (10 << 16));
16011f313773SOleg Bulyzhin 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
16024c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
1603a1d52896SBill Paul 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1604a1d52896SBill Paul 			    BGE_EVTENB_MI_INTERRUPT);
1605a1d52896SBill Paul 	}
160695d67482SBill Paul 
16071f313773SOleg Bulyzhin 	/*
16081f313773SOleg Bulyzhin 	 * Clear any pending link state attention.
16091f313773SOleg Bulyzhin 	 * Otherwise some link state change events may be lost until attention
16101f313773SOleg Bulyzhin 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
16111f313773SOleg Bulyzhin 	 * It's not necessary on newer BCM chips - perhaps enabling link
16121f313773SOleg Bulyzhin 	 * state change attentions implies clearing pending attention.
16131f313773SOleg Bulyzhin 	 */
16141f313773SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
16151f313773SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
16161f313773SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
16171f313773SOleg Bulyzhin 
161895d67482SBill Paul 	/* Enable link state change attentions. */
161995d67482SBill Paul 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
162095d67482SBill Paul 
162195d67482SBill Paul 	return (0);
162295d67482SBill Paul }
162395d67482SBill Paul 
16244c0da0ffSGleb Smirnoff const struct bge_revision *
16254c0da0ffSGleb Smirnoff bge_lookup_rev(uint32_t chipid)
16264c0da0ffSGleb Smirnoff {
16274c0da0ffSGleb Smirnoff 	const struct bge_revision *br;
16284c0da0ffSGleb Smirnoff 
16294c0da0ffSGleb Smirnoff 	for (br = bge_revisions; br->br_name != NULL; br++) {
16304c0da0ffSGleb Smirnoff 		if (br->br_chipid == chipid)
16314c0da0ffSGleb Smirnoff 			return (br);
16324c0da0ffSGleb Smirnoff 	}
16334c0da0ffSGleb Smirnoff 
16344c0da0ffSGleb Smirnoff 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
16354c0da0ffSGleb Smirnoff 		if (br->br_chipid == BGE_ASICREV(chipid))
16364c0da0ffSGleb Smirnoff 			return (br);
16374c0da0ffSGleb Smirnoff 	}
16384c0da0ffSGleb Smirnoff 
16394c0da0ffSGleb Smirnoff 	return (NULL);
16404c0da0ffSGleb Smirnoff }
16414c0da0ffSGleb Smirnoff 
16424c0da0ffSGleb Smirnoff const struct bge_vendor *
16434c0da0ffSGleb Smirnoff bge_lookup_vendor(uint16_t vid)
16444c0da0ffSGleb Smirnoff {
16454c0da0ffSGleb Smirnoff 	const struct bge_vendor *v;
16464c0da0ffSGleb Smirnoff 
16474c0da0ffSGleb Smirnoff 	for (v = bge_vendors; v->v_name != NULL; v++)
16484c0da0ffSGleb Smirnoff 		if (v->v_id == vid)
16494c0da0ffSGleb Smirnoff 			return (v);
16504c0da0ffSGleb Smirnoff 
16514c0da0ffSGleb Smirnoff 	panic("%s: unknown vendor %d", __func__, vid);
16524c0da0ffSGleb Smirnoff 	return (NULL);
16534c0da0ffSGleb Smirnoff }
16544c0da0ffSGleb Smirnoff 
165595d67482SBill Paul /*
165695d67482SBill Paul  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
16574c0da0ffSGleb Smirnoff  * against our list and return its name if we find a match.
16584c0da0ffSGleb Smirnoff  *
16594c0da0ffSGleb Smirnoff  * Note that since the Broadcom controller contains VPD support, we
16607c929cf9SJung-uk Kim  * try to get the device name string from the controller itself instead
16617c929cf9SJung-uk Kim  * of the compiled-in string. It guarantees we'll always announce the
16627c929cf9SJung-uk Kim  * right product name. We fall back to the compiled-in string when
16637c929cf9SJung-uk Kim  * VPD is unavailable or corrupt.
166495d67482SBill Paul  */
166595d67482SBill Paul static int
16663f74909aSGleb Smirnoff bge_probe(device_t dev)
166795d67482SBill Paul {
16684c0da0ffSGleb Smirnoff 	struct bge_type *t = bge_devs;
16694c0da0ffSGleb Smirnoff 	struct bge_softc *sc = device_get_softc(dev);
16707c929cf9SJung-uk Kim 	uint16_t vid, did;
167195d67482SBill Paul 
167295d67482SBill Paul 	sc->bge_dev = dev;
16737c929cf9SJung-uk Kim 	vid = pci_get_vendor(dev);
16747c929cf9SJung-uk Kim 	did = pci_get_device(dev);
16754c0da0ffSGleb Smirnoff 	while(t->bge_vid != 0) {
16767c929cf9SJung-uk Kim 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
16777c929cf9SJung-uk Kim 			char model[64], buf[96];
16784c0da0ffSGleb Smirnoff 			const struct bge_revision *br;
16794c0da0ffSGleb Smirnoff 			const struct bge_vendor *v;
16804c0da0ffSGleb Smirnoff 			uint32_t id;
16814c0da0ffSGleb Smirnoff 
16824c0da0ffSGleb Smirnoff 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
16834c0da0ffSGleb Smirnoff 			    BGE_PCIMISCCTL_ASICREV;
16844c0da0ffSGleb Smirnoff 			br = bge_lookup_rev(id);
16857c929cf9SJung-uk Kim 			v = bge_lookup_vendor(vid);
16864e35d186SJung-uk Kim 			{
16874e35d186SJung-uk Kim #if __FreeBSD_version > 700024
16884e35d186SJung-uk Kim 				const char *pname;
16894e35d186SJung-uk Kim 
16904e35d186SJung-uk Kim 				if (pci_get_vpd_ident(dev, &pname) == 0)
16914e35d186SJung-uk Kim 					snprintf(model, 64, "%s", pname);
16924e35d186SJung-uk Kim 				else
16934e35d186SJung-uk Kim #endif
16947c929cf9SJung-uk Kim 					snprintf(model, 64, "%s %s",
16957c929cf9SJung-uk Kim 					    v->v_name,
16967c929cf9SJung-uk Kim 					    br != NULL ? br->br_name :
16977c929cf9SJung-uk Kim 					    "NetXtreme Ethernet Controller");
16984e35d186SJung-uk Kim 			}
16997c929cf9SJung-uk Kim 			snprintf(buf, 96, "%s, %sASIC rev. %#04x", model,
17007c929cf9SJung-uk Kim 			    br != NULL ? "" : "unknown ", id >> 16);
17014c0da0ffSGleb Smirnoff 			device_set_desc_copy(dev, buf);
17026d2a9bd6SDoug Ambrisko 			if (pci_get_subvendor(dev) == DELL_VENDORID)
17035ee49a3aSJung-uk Kim 				sc->bge_flags |= BGE_FLAG_NO_3LED;
170408bf8bb7SJung-uk Kim 			if (did == BCOM_DEVICEID_BCM5755M)
170508bf8bb7SJung-uk Kim 				sc->bge_flags |= BGE_FLAG_ADJUST_TRIM;
170695d67482SBill Paul 			return (0);
170795d67482SBill Paul 		}
170895d67482SBill Paul 		t++;
170995d67482SBill Paul 	}
171095d67482SBill Paul 
171195d67482SBill Paul 	return (ENXIO);
171295d67482SBill Paul }
171395d67482SBill Paul 
1714f41ac2beSBill Paul static void
17153f74909aSGleb Smirnoff bge_dma_free(struct bge_softc *sc)
1716f41ac2beSBill Paul {
1717f41ac2beSBill Paul 	int i;
1718f41ac2beSBill Paul 
17193f74909aSGleb Smirnoff 	/* Destroy DMA maps for RX buffers. */
1720f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1721f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1722f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1723f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1724f41ac2beSBill Paul 	}
1725f41ac2beSBill Paul 
17263f74909aSGleb Smirnoff 	/* Destroy DMA maps for jumbo RX buffers. */
1727f41ac2beSBill Paul 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1728f41ac2beSBill Paul 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1729f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1730f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1731f41ac2beSBill Paul 	}
1732f41ac2beSBill Paul 
17333f74909aSGleb Smirnoff 	/* Destroy DMA maps for TX buffers. */
1734f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1735f41ac2beSBill Paul 		if (sc->bge_cdata.bge_tx_dmamap[i])
1736f41ac2beSBill Paul 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1737f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[i]);
1738f41ac2beSBill Paul 	}
1739f41ac2beSBill Paul 
1740f41ac2beSBill Paul 	if (sc->bge_cdata.bge_mtag)
1741f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1742f41ac2beSBill Paul 
1743f41ac2beSBill Paul 
17443f74909aSGleb Smirnoff 	/* Destroy standard RX ring. */
1745e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map)
1746e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1747e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map);
1748e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1749f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1750f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_std_ring,
1751f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_std_ring_map);
1752f41ac2beSBill Paul 
1753f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1754f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1755f41ac2beSBill Paul 
17563f74909aSGleb Smirnoff 	/* Destroy jumbo RX ring. */
1757e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1758e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1759e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1760e65bed95SPyun YongHyeon 
1761e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1762e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_jumbo_ring)
1763f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1764f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring,
1765f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1766f41ac2beSBill Paul 
1767f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1768f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1769f41ac2beSBill Paul 
17703f74909aSGleb Smirnoff 	/* Destroy RX return ring. */
1771e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map)
1772e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1773e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_return_ring_map);
1774e65bed95SPyun YongHyeon 
1775e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1776e65bed95SPyun YongHyeon 	    sc->bge_ldata.bge_rx_return_ring)
1777f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1778f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_return_ring,
1779f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_return_ring_map);
1780f41ac2beSBill Paul 
1781f41ac2beSBill Paul 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1782f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1783f41ac2beSBill Paul 
17843f74909aSGleb Smirnoff 	/* Destroy TX ring. */
1785e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map)
1786e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1787e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_tx_ring_map);
1788e65bed95SPyun YongHyeon 
1789e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
1790f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1791f41ac2beSBill Paul 		    sc->bge_ldata.bge_tx_ring,
1792f41ac2beSBill Paul 		    sc->bge_cdata.bge_tx_ring_map);
1793f41ac2beSBill Paul 
1794f41ac2beSBill Paul 	if (sc->bge_cdata.bge_tx_ring_tag)
1795f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1796f41ac2beSBill Paul 
17973f74909aSGleb Smirnoff 	/* Destroy status block. */
1798e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map)
1799e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1800e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_status_map);
1801e65bed95SPyun YongHyeon 
1802e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
1803f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1804f41ac2beSBill Paul 		    sc->bge_ldata.bge_status_block,
1805f41ac2beSBill Paul 		    sc->bge_cdata.bge_status_map);
1806f41ac2beSBill Paul 
1807f41ac2beSBill Paul 	if (sc->bge_cdata.bge_status_tag)
1808f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1809f41ac2beSBill Paul 
18103f74909aSGleb Smirnoff 	/* Destroy statistics block. */
1811e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map)
1812e65bed95SPyun YongHyeon 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1813e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_stats_map);
1814e65bed95SPyun YongHyeon 
1815e65bed95SPyun YongHyeon 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
1816f41ac2beSBill Paul 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1817f41ac2beSBill Paul 		    sc->bge_ldata.bge_stats,
1818f41ac2beSBill Paul 		    sc->bge_cdata.bge_stats_map);
1819f41ac2beSBill Paul 
1820f41ac2beSBill Paul 	if (sc->bge_cdata.bge_stats_tag)
1821f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1822f41ac2beSBill Paul 
18233f74909aSGleb Smirnoff 	/* Destroy the parent tag. */
1824f41ac2beSBill Paul 	if (sc->bge_cdata.bge_parent_tag)
1825f41ac2beSBill Paul 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1826f41ac2beSBill Paul }
1827f41ac2beSBill Paul 
1828f41ac2beSBill Paul static int
18293f74909aSGleb Smirnoff bge_dma_alloc(device_t dev)
1830f41ac2beSBill Paul {
18313f74909aSGleb Smirnoff 	struct bge_dmamap_arg ctx;
1832f41ac2beSBill Paul 	struct bge_softc *sc;
18331be6acb7SGleb Smirnoff 	int i, error;
1834f41ac2beSBill Paul 
1835f41ac2beSBill Paul 	sc = device_get_softc(dev);
1836f41ac2beSBill Paul 
1837f41ac2beSBill Paul 	/*
1838f41ac2beSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1839f41ac2beSBill Paul 	 */
1840378f231eSJohn-Mark Gurney 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev), /* parent */
1841706620f0SScott Long 			1, 0,			/* alignment, boundary */
1842f41ac2beSBill Paul 			BUS_SPACE_MAXADDR,	/* lowaddr */
18432f28b973SScott Long 			BUS_SPACE_MAXADDR,	/* highaddr */
1844f41ac2beSBill Paul 			NULL, NULL,		/* filter, filterarg */
1845f41ac2beSBill Paul 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1846f41ac2beSBill Paul 			BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
18478a40c10eSScott Long 			0,			/* flags */
1848f41ac2beSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1849f41ac2beSBill Paul 			&sc->bge_cdata.bge_parent_tag);
1850f41ac2beSBill Paul 
1851e65bed95SPyun YongHyeon 	if (error != 0) {
1852fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
1853fe806fdaSPyun YongHyeon 		    "could not allocate parent dma tag\n");
1854e65bed95SPyun YongHyeon 		return (ENOMEM);
1855e65bed95SPyun YongHyeon 	}
1856e65bed95SPyun YongHyeon 
1857f41ac2beSBill Paul 	/*
1858f41ac2beSBill Paul 	 * Create tag for RX mbufs.
1859f41ac2beSBill Paul 	 */
18608a2e22deSScott Long 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1861f41ac2beSBill Paul 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
18621be6acb7SGleb Smirnoff 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
18631be6acb7SGleb Smirnoff 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
1864f41ac2beSBill Paul 
1865f41ac2beSBill Paul 	if (error) {
1866fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1867f41ac2beSBill Paul 		return (ENOMEM);
1868f41ac2beSBill Paul 	}
1869f41ac2beSBill Paul 
18703f74909aSGleb Smirnoff 	/* Create DMA maps for RX buffers. */
1871f41ac2beSBill Paul 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1872f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1873f41ac2beSBill Paul 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1874f41ac2beSBill Paul 		if (error) {
1875fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1876fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1877f41ac2beSBill Paul 			return (ENOMEM);
1878f41ac2beSBill Paul 		}
1879f41ac2beSBill Paul 	}
1880f41ac2beSBill Paul 
18813f74909aSGleb Smirnoff 	/* Create DMA maps for TX buffers. */
1882f41ac2beSBill Paul 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1883f41ac2beSBill Paul 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1884f41ac2beSBill Paul 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1885f41ac2beSBill Paul 		if (error) {
1886fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
1887fe806fdaSPyun YongHyeon 			    "can't create DMA map for RX\n");
1888f41ac2beSBill Paul 			return (ENOMEM);
1889f41ac2beSBill Paul 		}
1890f41ac2beSBill Paul 	}
1891f41ac2beSBill Paul 
18923f74909aSGleb Smirnoff 	/* Create tag for standard RX ring. */
1893f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1894f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1895f41ac2beSBill Paul 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1896f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1897f41ac2beSBill Paul 
1898f41ac2beSBill Paul 	if (error) {
1899fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1900f41ac2beSBill Paul 		return (ENOMEM);
1901f41ac2beSBill Paul 	}
1902f41ac2beSBill Paul 
19033f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for standard RX ring. */
1904f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1905f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1906f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_std_ring_map);
1907f41ac2beSBill Paul 	if (error)
1908f41ac2beSBill Paul 		return (ENOMEM);
1909f41ac2beSBill Paul 
1910f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1911f41ac2beSBill Paul 
19123f74909aSGleb Smirnoff 	/* Load the address of the standard RX ring. */
1913f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
1914f41ac2beSBill Paul 	ctx.sc = sc;
1915f41ac2beSBill Paul 
1916f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1917f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1918f41ac2beSBill Paul 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1919f41ac2beSBill Paul 
1920f41ac2beSBill Paul 	if (error)
1921f41ac2beSBill Paul 		return (ENOMEM);
1922f41ac2beSBill Paul 
1923f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
1924f41ac2beSBill Paul 
19253f74909aSGleb Smirnoff 	/* Create tags for jumbo mbufs. */
19264c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1927f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
19288a2e22deSScott Long 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
19291be6acb7SGleb Smirnoff 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
19301be6acb7SGleb Smirnoff 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
1931f41ac2beSBill Paul 		if (error) {
1932fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
19333f74909aSGleb Smirnoff 			    "could not allocate jumbo dma tag\n");
1934f41ac2beSBill Paul 			return (ENOMEM);
1935f41ac2beSBill Paul 		}
1936f41ac2beSBill Paul 
19373f74909aSGleb Smirnoff 		/* Create tag for jumbo RX ring. */
1938f41ac2beSBill Paul 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1939f41ac2beSBill Paul 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1940f41ac2beSBill Paul 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
1941f41ac2beSBill Paul 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
1942f41ac2beSBill Paul 
1943f41ac2beSBill Paul 		if (error) {
1944fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev,
19453f74909aSGleb Smirnoff 			    "could not allocate jumbo ring dma tag\n");
1946f41ac2beSBill Paul 			return (ENOMEM);
1947f41ac2beSBill Paul 		}
1948f41ac2beSBill Paul 
19493f74909aSGleb Smirnoff 		/* Allocate DMA'able memory for jumbo RX ring. */
1950f41ac2beSBill Paul 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
19511be6acb7SGleb Smirnoff 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
19521be6acb7SGleb Smirnoff 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1953f41ac2beSBill Paul 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
1954f41ac2beSBill Paul 		if (error)
1955f41ac2beSBill Paul 			return (ENOMEM);
1956f41ac2beSBill Paul 
19573f74909aSGleb Smirnoff 		/* Load the address of the jumbo RX ring. */
1958f41ac2beSBill Paul 		ctx.bge_maxsegs = 1;
1959f41ac2beSBill Paul 		ctx.sc = sc;
1960f41ac2beSBill Paul 
1961f41ac2beSBill Paul 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1962f41ac2beSBill Paul 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1963f41ac2beSBill Paul 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
1964f41ac2beSBill Paul 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1965f41ac2beSBill Paul 
1966f41ac2beSBill Paul 		if (error)
1967f41ac2beSBill Paul 			return (ENOMEM);
1968f41ac2beSBill Paul 
1969f41ac2beSBill Paul 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
1970f41ac2beSBill Paul 
19713f74909aSGleb Smirnoff 		/* Create DMA maps for jumbo RX buffers. */
1972f41ac2beSBill Paul 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1973f41ac2beSBill Paul 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
1974f41ac2beSBill Paul 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1975f41ac2beSBill Paul 			if (error) {
1976fe806fdaSPyun YongHyeon 				device_printf(sc->bge_dev,
19773f74909aSGleb Smirnoff 				    "can't create DMA map for jumbo RX\n");
1978f41ac2beSBill Paul 				return (ENOMEM);
1979f41ac2beSBill Paul 			}
1980f41ac2beSBill Paul 		}
1981f41ac2beSBill Paul 
1982f41ac2beSBill Paul 	}
1983f41ac2beSBill Paul 
19843f74909aSGleb Smirnoff 	/* Create tag for RX return ring. */
1985f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1986f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1987f41ac2beSBill Paul 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
1988f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
1989f41ac2beSBill Paul 
1990f41ac2beSBill Paul 	if (error) {
1991fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
1992f41ac2beSBill Paul 		return (ENOMEM);
1993f41ac2beSBill Paul 	}
1994f41ac2beSBill Paul 
19953f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for RX return ring. */
1996f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
1997f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
1998f41ac2beSBill Paul 	    &sc->bge_cdata.bge_rx_return_ring_map);
1999f41ac2beSBill Paul 	if (error)
2000f41ac2beSBill Paul 		return (ENOMEM);
2001f41ac2beSBill Paul 
2002f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
2003f41ac2beSBill Paul 	    BGE_RX_RTN_RING_SZ(sc));
2004f41ac2beSBill Paul 
20053f74909aSGleb Smirnoff 	/* Load the address of the RX return ring. */
2006f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2007f41ac2beSBill Paul 	ctx.sc = sc;
2008f41ac2beSBill Paul 
2009f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
2010f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_return_ring_map,
2011f41ac2beSBill Paul 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
2012f41ac2beSBill Paul 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2013f41ac2beSBill Paul 
2014f41ac2beSBill Paul 	if (error)
2015f41ac2beSBill Paul 		return (ENOMEM);
2016f41ac2beSBill Paul 
2017f41ac2beSBill Paul 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
2018f41ac2beSBill Paul 
20193f74909aSGleb Smirnoff 	/* Create tag for TX ring. */
2020f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2021f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2022f41ac2beSBill Paul 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
2023f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_tag);
2024f41ac2beSBill Paul 
2025f41ac2beSBill Paul 	if (error) {
2026fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2027f41ac2beSBill Paul 		return (ENOMEM);
2028f41ac2beSBill Paul 	}
2029f41ac2beSBill Paul 
20303f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for TX ring. */
2031f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
2032f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
2033f41ac2beSBill Paul 	    &sc->bge_cdata.bge_tx_ring_map);
2034f41ac2beSBill Paul 	if (error)
2035f41ac2beSBill Paul 		return (ENOMEM);
2036f41ac2beSBill Paul 
2037f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
2038f41ac2beSBill Paul 
20393f74909aSGleb Smirnoff 	/* Load the address of the TX ring. */
2040f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2041f41ac2beSBill Paul 	ctx.sc = sc;
2042f41ac2beSBill Paul 
2043f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
2044f41ac2beSBill Paul 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
2045f41ac2beSBill Paul 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2046f41ac2beSBill Paul 
2047f41ac2beSBill Paul 	if (error)
2048f41ac2beSBill Paul 		return (ENOMEM);
2049f41ac2beSBill Paul 
2050f41ac2beSBill Paul 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
2051f41ac2beSBill Paul 
20523f74909aSGleb Smirnoff 	/* Create tag for status block. */
2053f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2054f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2055f41ac2beSBill Paul 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
2056f41ac2beSBill Paul 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
2057f41ac2beSBill Paul 
2058f41ac2beSBill Paul 	if (error) {
2059fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2060f41ac2beSBill Paul 		return (ENOMEM);
2061f41ac2beSBill Paul 	}
2062f41ac2beSBill Paul 
20633f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for status block. */
2064f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
2065f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
2066f41ac2beSBill Paul 	    &sc->bge_cdata.bge_status_map);
2067f41ac2beSBill Paul 	if (error)
2068f41ac2beSBill Paul 		return (ENOMEM);
2069f41ac2beSBill Paul 
2070f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2071f41ac2beSBill Paul 
20723f74909aSGleb Smirnoff 	/* Load the address of the status block. */
2073f41ac2beSBill Paul 	ctx.sc = sc;
2074f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2075f41ac2beSBill Paul 
2076f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2077f41ac2beSBill Paul 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2078f41ac2beSBill Paul 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2079f41ac2beSBill Paul 
2080f41ac2beSBill Paul 	if (error)
2081f41ac2beSBill Paul 		return (ENOMEM);
2082f41ac2beSBill Paul 
2083f41ac2beSBill Paul 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2084f41ac2beSBill Paul 
20853f74909aSGleb Smirnoff 	/* Create tag for statistics block. */
2086f41ac2beSBill Paul 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2087f41ac2beSBill Paul 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2088f41ac2beSBill Paul 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2089f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_tag);
2090f41ac2beSBill Paul 
2091f41ac2beSBill Paul 	if (error) {
2092fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2093f41ac2beSBill Paul 		return (ENOMEM);
2094f41ac2beSBill Paul 	}
2095f41ac2beSBill Paul 
20963f74909aSGleb Smirnoff 	/* Allocate DMA'able memory for statistics block. */
2097f41ac2beSBill Paul 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2098f41ac2beSBill Paul 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2099f41ac2beSBill Paul 	    &sc->bge_cdata.bge_stats_map);
2100f41ac2beSBill Paul 	if (error)
2101f41ac2beSBill Paul 		return (ENOMEM);
2102f41ac2beSBill Paul 
2103f41ac2beSBill Paul 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2104f41ac2beSBill Paul 
21053f74909aSGleb Smirnoff 	/* Load the address of the statstics block. */
2106f41ac2beSBill Paul 	ctx.sc = sc;
2107f41ac2beSBill Paul 	ctx.bge_maxsegs = 1;
2108f41ac2beSBill Paul 
2109f41ac2beSBill Paul 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2110f41ac2beSBill Paul 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2111f41ac2beSBill Paul 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2112f41ac2beSBill Paul 
2113f41ac2beSBill Paul 	if (error)
2114f41ac2beSBill Paul 		return (ENOMEM);
2115f41ac2beSBill Paul 
2116f41ac2beSBill Paul 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2117f41ac2beSBill Paul 
2118f41ac2beSBill Paul 	return (0);
2119f41ac2beSBill Paul }
2120f41ac2beSBill Paul 
21214e35d186SJung-uk Kim #if __FreeBSD_version > 700025
2122bf6ef57aSJohn Polstra /*
2123bf6ef57aSJohn Polstra  * Return true if this device has more than one port.
2124bf6ef57aSJohn Polstra  */
2125bf6ef57aSJohn Polstra static int
2126bf6ef57aSJohn Polstra bge_has_multiple_ports(struct bge_softc *sc)
2127bf6ef57aSJohn Polstra {
2128bf6ef57aSJohn Polstra 	device_t dev = sc->bge_dev;
2129bf6ef57aSJohn Polstra 	u_int b, s, f, fscan;
2130bf6ef57aSJohn Polstra 
2131bf6ef57aSJohn Polstra 	b = pci_get_bus(dev);
2132bf6ef57aSJohn Polstra 	s = pci_get_slot(dev);
2133bf6ef57aSJohn Polstra 	f = pci_get_function(dev);
2134bf6ef57aSJohn Polstra 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
2135bf6ef57aSJohn Polstra 		if (fscan != f && pci_find_bsf(b, s, fscan) != NULL)
2136bf6ef57aSJohn Polstra 			return (1);
2137bf6ef57aSJohn Polstra 	return (0);
2138bf6ef57aSJohn Polstra }
2139bf6ef57aSJohn Polstra 
2140bf6ef57aSJohn Polstra /*
2141bf6ef57aSJohn Polstra  * Return true if MSI can be used with this device.
2142bf6ef57aSJohn Polstra  */
2143bf6ef57aSJohn Polstra static int
2144bf6ef57aSJohn Polstra bge_can_use_msi(struct bge_softc *sc)
2145bf6ef57aSJohn Polstra {
2146bf6ef57aSJohn Polstra 	int can_use_msi = 0;
2147bf6ef57aSJohn Polstra 
2148bf6ef57aSJohn Polstra 	switch (sc->bge_asicrev) {
2149bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5714:
2150bf6ef57aSJohn Polstra 		/*
2151bf6ef57aSJohn Polstra 		 * Apparently, MSI doesn't work when this chip is configured
2152bf6ef57aSJohn Polstra 		 * in single-port mode.
2153bf6ef57aSJohn Polstra 		 */
2154bf6ef57aSJohn Polstra 		if (bge_has_multiple_ports(sc))
2155bf6ef57aSJohn Polstra 			can_use_msi = 1;
2156bf6ef57aSJohn Polstra 		break;
2157bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5750:
2158bf6ef57aSJohn Polstra 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
2159bf6ef57aSJohn Polstra 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
2160bf6ef57aSJohn Polstra 			can_use_msi = 1;
2161bf6ef57aSJohn Polstra 		break;
2162bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5752:
2163bf6ef57aSJohn Polstra 	case BGE_ASICREV_BCM5780:
2164bf6ef57aSJohn Polstra 		can_use_msi = 1;
2165bf6ef57aSJohn Polstra 		break;
2166bf6ef57aSJohn Polstra 	}
2167bf6ef57aSJohn Polstra 	return (can_use_msi);
2168bf6ef57aSJohn Polstra }
21694e35d186SJung-uk Kim #endif
2170bf6ef57aSJohn Polstra 
217195d67482SBill Paul static int
21723f74909aSGleb Smirnoff bge_attach(device_t dev)
217395d67482SBill Paul {
217495d67482SBill Paul 	struct ifnet *ifp;
217595d67482SBill Paul 	struct bge_softc *sc;
21763f74909aSGleb Smirnoff 	uint32_t hwcfg = 0;
21773f74909aSGleb Smirnoff 	uint32_t mac_tmp = 0;
2178fc74a9f9SBrooks Davis 	u_char eaddr[6];
21794e35d186SJung-uk Kim 	int error = 0, rid, trys, reg;
218095d67482SBill Paul 
218195d67482SBill Paul 	sc = device_get_softc(dev);
218295d67482SBill Paul 	sc->bge_dev = dev;
218395d67482SBill Paul 
218495d67482SBill Paul 	/*
218595d67482SBill Paul 	 * Map control/status registers.
218695d67482SBill Paul 	 */
218795d67482SBill Paul 	pci_enable_busmaster(dev);
218895d67482SBill Paul 
218995d67482SBill Paul 	rid = BGE_PCI_BAR0;
21905f96beb9SNate Lawson 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
21915f96beb9SNate Lawson 	    RF_ACTIVE | PCI_RF_DENSE);
219295d67482SBill Paul 
219395d67482SBill Paul 	if (sc->bge_res == NULL) {
2194fe806fdaSPyun YongHyeon 		device_printf (sc->bge_dev, "couldn't map memory\n");
219595d67482SBill Paul 		error = ENXIO;
219695d67482SBill Paul 		goto fail;
219795d67482SBill Paul 	}
219895d67482SBill Paul 
219995d67482SBill Paul 	sc->bge_btag = rman_get_bustag(sc->bge_res);
220095d67482SBill Paul 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
220195d67482SBill Paul 
2202e53d81eeSPaul Saab 	/* Save ASIC rev. */
2203e53d81eeSPaul Saab 
2204e53d81eeSPaul Saab 	sc->bge_chipid =
2205e53d81eeSPaul Saab 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2206e53d81eeSPaul Saab 	    BGE_PCIMISCCTL_ASICREV;
2207e53d81eeSPaul Saab 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2208e53d81eeSPaul Saab 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2209e53d81eeSPaul Saab 
22100dae9719SJung-uk Kim 	/* Save chipset family. */
22110dae9719SJung-uk Kim 	switch (sc->bge_asicrev) {
22120dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5700:
22130dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5701:
22140dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5703:
22150dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5704:
22167ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
22170dae9719SJung-uk Kim 		break;
22180dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714_A0:
22190dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5780:
22200dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5714:
22217ee00338SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5714_FAMILY /* | BGE_FLAG_JUMBO */;
22225ee49a3aSJung-uk Kim 		/* FALLTHRU */
22230dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5750:
22240dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5752:
22250dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5755:
22260dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5787:
22270dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
22285ee49a3aSJung-uk Kim 		/* FALLTHRU */
22290dae9719SJung-uk Kim 	case BGE_ASICREV_BCM5705:
22300dae9719SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
22310dae9719SJung-uk Kim 		break;
22320dae9719SJung-uk Kim 	}
22330dae9719SJung-uk Kim 
22345ee49a3aSJung-uk Kim 	/* Set various bug flags. */
22351ec4c3a8SJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
22361ec4c3a8SJung-uk Kim 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
22371ec4c3a8SJung-uk Kim 		sc->bge_flags |= BGE_FLAG_CRC_BUG;
22385ee49a3aSJung-uk Kim 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
22395ee49a3aSJung-uk Kim 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
22405ee49a3aSJung-uk Kim 		sc->bge_flags |= BGE_FLAG_ADC_BUG;
22415ee49a3aSJung-uk Kim 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
22425ee49a3aSJung-uk Kim 		sc->bge_flags |= BGE_FLAG_5704_A0_BUG;
224308bf8bb7SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc) &&
224408bf8bb7SJung-uk Kim 	    !(sc->bge_flags & BGE_FLAG_ADJUST_TRIM)) {
22455ee49a3aSJung-uk Kim 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
22465ee49a3aSJung-uk Kim 		    sc->bge_asicrev == BGE_ASICREV_BCM5787)
22475ee49a3aSJung-uk Kim 			sc->bge_flags |= BGE_FLAG_JITTER_BUG;
22485ee49a3aSJung-uk Kim 		else
22495ee49a3aSJung-uk Kim 			sc->bge_flags |= BGE_FLAG_BER_BUG;
22505ee49a3aSJung-uk Kim 	}
22515ee49a3aSJung-uk Kim 
2252e53d81eeSPaul Saab   	/*
22536f8718a3SScott Long 	 * Check if this is a PCI-X or PCI Express device.
2254e53d81eeSPaul Saab   	 */
22556f8718a3SScott Long #if __FreeBSD_version > 700010
22566f8718a3SScott Long 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
22574c0da0ffSGleb Smirnoff 		/*
22586f8718a3SScott Long 		 * Found a PCI Express capabilities register, this
22596f8718a3SScott Long 		 * must be a PCI Express device.
22606f8718a3SScott Long 		 */
22616f8718a3SScott Long 		if (reg != 0)
22626f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIE;
22636f8718a3SScott Long 	} else if (pci_find_extcap(dev, PCIY_PCIX, &reg) == 0) {
22646f8718a3SScott Long 		if (reg != 0)
22656f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIX;
22666f8718a3SScott Long 	}
22676f8718a3SScott Long 
22686f8718a3SScott Long #else
22695345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc)) {
22706f8718a3SScott Long 		reg = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
22716f8718a3SScott Long 		if ((reg & 0xff) == BGE_PCIE_CAPID)
22726f8718a3SScott Long 			sc->bge_flags |= BGE_FLAG_PCIE;
22736f8718a3SScott Long 	} else {
22746f8718a3SScott Long 		/*
22756f8718a3SScott Long 		 * Check if the device is in PCI-X Mode.
22766f8718a3SScott Long 		 * (This bit is not valid on PCI Express controllers.)
22774c0da0ffSGleb Smirnoff 		 */
22784c0da0ffSGleb Smirnoff 		if ((pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
22794c0da0ffSGleb Smirnoff 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
2280652ae483SGleb Smirnoff 			sc->bge_flags |= BGE_FLAG_PCIX;
22816f8718a3SScott Long 	}
22826f8718a3SScott Long #endif
22834c0da0ffSGleb Smirnoff 
22844e35d186SJung-uk Kim #if __FreeBSD_version > 700025
22854e35d186SJung-uk Kim 	{
22864e35d186SJung-uk Kim 		int msicount;
22874e35d186SJung-uk Kim 
2288bf6ef57aSJohn Polstra 		/*
2289bf6ef57aSJohn Polstra 		 * Allocate the interrupt, using MSI if possible. These devices
2290bf6ef57aSJohn Polstra 		 * support 8 MSI messages, but only the first one is used in
2291bf6ef57aSJohn Polstra 		 * normal operation.
2292bf6ef57aSJohn Polstra 		 */
2293bf6ef57aSJohn Polstra 		if (bge_can_use_msi(sc)) {
2294bf6ef57aSJohn Polstra 			msicount = pci_msi_count(dev);
2295bf6ef57aSJohn Polstra 			if (msicount > 1)
2296bf6ef57aSJohn Polstra 				msicount = 1;
2297bf6ef57aSJohn Polstra 		} else
2298bf6ef57aSJohn Polstra 			msicount = 0;
2299bf6ef57aSJohn Polstra 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
2300bf6ef57aSJohn Polstra 			rid = 1;
2301bf6ef57aSJohn Polstra 			sc->bge_flags |= BGE_FLAG_MSI;
2302bf6ef57aSJohn Polstra 		} else
2303bf6ef57aSJohn Polstra 			rid = 0;
23044e35d186SJung-uk Kim 	}
23054e35d186SJung-uk Kim #else
23064e35d186SJung-uk Kim 	rid = 0;
23074e35d186SJung-uk Kim #endif
2308bf6ef57aSJohn Polstra 
2309bf6ef57aSJohn Polstra 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2310bf6ef57aSJohn Polstra 	    RF_SHAREABLE | RF_ACTIVE);
2311bf6ef57aSJohn Polstra 
2312bf6ef57aSJohn Polstra 	if (sc->bge_irq == NULL) {
2313bf6ef57aSJohn Polstra 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
2314bf6ef57aSJohn Polstra 		error = ENXIO;
2315bf6ef57aSJohn Polstra 		goto fail;
2316bf6ef57aSJohn Polstra 	}
2317bf6ef57aSJohn Polstra 
2318bf6ef57aSJohn Polstra 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
2319bf6ef57aSJohn Polstra 
232095d67482SBill Paul 	/* Try to reset the chip. */
23218cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
23228cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
23238cb1383cSDoug Ambrisko 		bge_release_resources(sc);
23248cb1383cSDoug Ambrisko 		error = ENXIO;
23258cb1383cSDoug Ambrisko 		goto fail;
23268cb1383cSDoug Ambrisko 	}
23278cb1383cSDoug Ambrisko 
23288cb1383cSDoug Ambrisko 	sc->bge_asf_mode = 0;
2329f1a7e6d5SScott Long 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG)
2330f1a7e6d5SScott Long 	    == BGE_MAGIC_NUMBER)) {
23318cb1383cSDoug Ambrisko 		if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG)
23328cb1383cSDoug Ambrisko 		    & BGE_HWCFG_ASF) {
23338cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_ENABLE;
23348cb1383cSDoug Ambrisko 			sc->bge_asf_mode |= ASF_STACKUP;
23358cb1383cSDoug Ambrisko 			if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
23368cb1383cSDoug Ambrisko 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
23378cb1383cSDoug Ambrisko 			}
23388cb1383cSDoug Ambrisko 		}
23398cb1383cSDoug Ambrisko 	}
23408cb1383cSDoug Ambrisko 
23418cb1383cSDoug Ambrisko 	/* Try to reset the chip again the nice way. */
23428cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
23438cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
23448cb1383cSDoug Ambrisko 	if (bge_reset(sc)) {
23458cb1383cSDoug Ambrisko 		device_printf(sc->bge_dev, "chip reset failed\n");
23468cb1383cSDoug Ambrisko 		bge_release_resources(sc);
23478cb1383cSDoug Ambrisko 		error = ENXIO;
23488cb1383cSDoug Ambrisko 		goto fail;
23498cb1383cSDoug Ambrisko 	}
23508cb1383cSDoug Ambrisko 
23518cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
23528cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
235395d67482SBill Paul 
235495d67482SBill Paul 	if (bge_chipinit(sc)) {
2355fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "chip initialization failed\n");
235695d67482SBill Paul 		bge_release_resources(sc);
235795d67482SBill Paul 		error = ENXIO;
235895d67482SBill Paul 		goto fail;
235995d67482SBill Paul 	}
236095d67482SBill Paul 
236195d67482SBill Paul 	/*
236295d67482SBill Paul 	 * Get station address from the EEPROM.
236395d67482SBill Paul 	 */
2364fc74a9f9SBrooks Davis 	mac_tmp = bge_readmem_ind(sc, 0x0c14);
2365fc74a9f9SBrooks Davis 	if ((mac_tmp >> 16) == 0x484b) {
2366fc74a9f9SBrooks Davis 		eaddr[0] = (u_char)(mac_tmp >> 8);
2367fc74a9f9SBrooks Davis 		eaddr[1] = (u_char)mac_tmp;
2368fc74a9f9SBrooks Davis 		mac_tmp = bge_readmem_ind(sc, 0x0c18);
2369fc74a9f9SBrooks Davis 		eaddr[2] = (u_char)(mac_tmp >> 24);
2370fc74a9f9SBrooks Davis 		eaddr[3] = (u_char)(mac_tmp >> 16);
2371fc74a9f9SBrooks Davis 		eaddr[4] = (u_char)(mac_tmp >> 8);
2372fc74a9f9SBrooks Davis 		eaddr[5] = (u_char)mac_tmp;
2373fc74a9f9SBrooks Davis 	} else if (bge_read_eeprom(sc, eaddr,
237495d67482SBill Paul 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2375fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to read station address\n");
237695d67482SBill Paul 		bge_release_resources(sc);
237795d67482SBill Paul 		error = ENXIO;
237895d67482SBill Paul 		goto fail;
237995d67482SBill Paul 	}
238095d67482SBill Paul 
2381f41ac2beSBill Paul 	/* 5705 limits RX return ring to 512 entries. */
23827ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
2383f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2384f41ac2beSBill Paul 	else
2385f41ac2beSBill Paul 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2386f41ac2beSBill Paul 
2387f41ac2beSBill Paul 	if (bge_dma_alloc(dev)) {
2388fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev,
2389fe806fdaSPyun YongHyeon 		    "failed to allocate DMA resources\n");
2390f41ac2beSBill Paul 		bge_release_resources(sc);
2391f41ac2beSBill Paul 		error = ENXIO;
2392f41ac2beSBill Paul 		goto fail;
2393f41ac2beSBill Paul 	}
2394f41ac2beSBill Paul 
239595d67482SBill Paul 	/* Set default tuneable values. */
239695d67482SBill Paul 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
239795d67482SBill Paul 	sc->bge_rx_coal_ticks = 150;
239895d67482SBill Paul 	sc->bge_tx_coal_ticks = 150;
23996f8718a3SScott Long 	sc->bge_rx_max_coal_bds = 10;
24006f8718a3SScott Long 	sc->bge_tx_max_coal_bds = 10;
240195d67482SBill Paul 
240295d67482SBill Paul 	/* Set up ifnet structure */
2403fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2404fc74a9f9SBrooks Davis 	if (ifp == NULL) {
2405fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2406fc74a9f9SBrooks Davis 		bge_release_resources(sc);
2407fc74a9f9SBrooks Davis 		error = ENXIO;
2408fc74a9f9SBrooks Davis 		goto fail;
2409fc74a9f9SBrooks Davis 	}
241095d67482SBill Paul 	ifp->if_softc = sc;
24119bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
241295d67482SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
241395d67482SBill Paul 	ifp->if_ioctl = bge_ioctl;
241495d67482SBill Paul 	ifp->if_start = bge_start;
241595d67482SBill Paul 	ifp->if_init = bge_init;
241695d67482SBill Paul 	ifp->if_mtu = ETHERMTU;
24174d665c4dSDag-Erling Smørgrav 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
24184d665c4dSDag-Erling Smørgrav 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
24194d665c4dSDag-Erling Smørgrav 	IFQ_SET_READY(&ifp->if_snd);
242095d67482SBill Paul 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2421d375e524SGleb Smirnoff 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
24224e35d186SJung-uk Kim 	    IFCAP_VLAN_MTU;
24234e35d186SJung-uk Kim #ifdef IFCAP_VLAN_HWCSUM
24244e35d186SJung-uk Kim 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
24254e35d186SJung-uk Kim #endif
242695d67482SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
242775719184SGleb Smirnoff #ifdef DEVICE_POLLING
242875719184SGleb Smirnoff 	ifp->if_capabilities |= IFCAP_POLLING;
242975719184SGleb Smirnoff #endif
243095d67482SBill Paul 
2431a1d52896SBill Paul 	/*
2432d375e524SGleb Smirnoff 	 * 5700 B0 chips do not support checksumming correctly due
2433d375e524SGleb Smirnoff 	 * to hardware bugs.
2434d375e524SGleb Smirnoff 	 */
2435d375e524SGleb Smirnoff 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2436d375e524SGleb Smirnoff 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2437d375e524SGleb Smirnoff 		ifp->if_capenable &= IFCAP_HWCSUM;
2438d375e524SGleb Smirnoff 		ifp->if_hwassist = 0;
2439d375e524SGleb Smirnoff 	}
2440d375e524SGleb Smirnoff 
2441d375e524SGleb Smirnoff 	/*
2442a1d52896SBill Paul 	 * Figure out what sort of media we have by checking the
244341abcc1bSPaul Saab 	 * hardware config word in the first 32k of NIC internal memory,
244441abcc1bSPaul Saab 	 * or fall back to examining the EEPROM if necessary.
244541abcc1bSPaul Saab 	 * Note: on some BCM5700 cards, this value appears to be unset.
244641abcc1bSPaul Saab 	 * If that's the case, we have to rely on identifying the NIC
244741abcc1bSPaul Saab 	 * by its PCI subsystem ID, as we do below for the SysKonnect
244841abcc1bSPaul Saab 	 * SK-9D41.
2449a1d52896SBill Paul 	 */
245041abcc1bSPaul Saab 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
245141abcc1bSPaul Saab 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
245241abcc1bSPaul Saab 	else {
2453f6789fbaSPyun YongHyeon 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2454f6789fbaSPyun YongHyeon 		    sizeof(hwcfg))) {
2455fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2456f6789fbaSPyun YongHyeon 			bge_release_resources(sc);
2457f6789fbaSPyun YongHyeon 			error = ENXIO;
2458f6789fbaSPyun YongHyeon 			goto fail;
2459f6789fbaSPyun YongHyeon 		}
246041abcc1bSPaul Saab 		hwcfg = ntohl(hwcfg);
246141abcc1bSPaul Saab 	}
246241abcc1bSPaul Saab 
246341abcc1bSPaul Saab 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2464652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
2465a1d52896SBill Paul 
246695d67482SBill Paul 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
24676098821cSJung-uk Kim 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
24686098821cSJung-uk Kim 	    SK_SUBSYSID_9D41)
2469652ae483SGleb Smirnoff 		sc->bge_flags |= BGE_FLAG_TBI;
247095d67482SBill Paul 
2471652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
247295d67482SBill Paul 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
247395d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts);
24746098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0,
24756098821cSJung-uk Kim 		    NULL);
24766098821cSJung-uk Kim 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX | IFM_FDX,
24776098821cSJung-uk Kim 		    0, NULL);
247895d67482SBill Paul 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
247995d67482SBill Paul 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
2480da3003f0SBill Paul 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
248195d67482SBill Paul 	} else {
248295d67482SBill Paul 		/*
24838cb1383cSDoug Ambrisko 		 * Do transceiver setup and tell the firmware the
24848cb1383cSDoug Ambrisko 		 * driver is down so we can try to get access the
24858cb1383cSDoug Ambrisko 		 * probe if ASF is running.  Retry a couple of times
24868cb1383cSDoug Ambrisko 		 * if we get a conflict with the ASF firmware accessing
24878cb1383cSDoug Ambrisko 		 * the PHY.
248895d67482SBill Paul 		 */
24898cb1383cSDoug Ambrisko 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
24908cb1383cSDoug Ambrisko again:
24918cb1383cSDoug Ambrisko 		bge_asf_driver_up(sc);
24928cb1383cSDoug Ambrisko 
24938cb1383cSDoug Ambrisko 		trys = 0;
249495d67482SBill Paul 		if (mii_phy_probe(dev, &sc->bge_miibus,
249595d67482SBill Paul 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
24968cb1383cSDoug Ambrisko 			if (trys++ < 4) {
24978cb1383cSDoug Ambrisko 				device_printf(sc->bge_dev, "Try again\n");
24984e35d186SJung-uk Kim 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR,
24994e35d186SJung-uk Kim 				    BMCR_RESET);
25008cb1383cSDoug Ambrisko 				goto again;
25018cb1383cSDoug Ambrisko 			}
25028cb1383cSDoug Ambrisko 
2503fe806fdaSPyun YongHyeon 			device_printf(sc->bge_dev, "MII without any PHY!\n");
250495d67482SBill Paul 			bge_release_resources(sc);
250595d67482SBill Paul 			error = ENXIO;
250695d67482SBill Paul 			goto fail;
250795d67482SBill Paul 		}
25088cb1383cSDoug Ambrisko 
25098cb1383cSDoug Ambrisko 		/*
25108cb1383cSDoug Ambrisko 		 * Now tell the firmware we are going up after probing the PHY
25118cb1383cSDoug Ambrisko 		 */
25128cb1383cSDoug Ambrisko 		if (sc->bge_asf_mode & ASF_STACKUP)
25138cb1383cSDoug Ambrisko 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
251495d67482SBill Paul 	}
251595d67482SBill Paul 
251695d67482SBill Paul 	/*
2517e255b776SJohn Polstra 	 * When using the BCM5701 in PCI-X mode, data corruption has
2518e255b776SJohn Polstra 	 * been observed in the first few bytes of some received packets.
2519e255b776SJohn Polstra 	 * Aligning the packet buffer in memory eliminates the corruption.
2520e255b776SJohn Polstra 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2521e255b776SJohn Polstra 	 * which do not support unaligned accesses, we will realign the
2522e255b776SJohn Polstra 	 * payloads by copying the received packets.
2523e255b776SJohn Polstra 	 */
2524652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
2525652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_PCIX)
2526652ae483SGleb Smirnoff                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
2527e255b776SJohn Polstra 
2528e255b776SJohn Polstra 	/*
252995d67482SBill Paul 	 * Call MI attach routine.
253095d67482SBill Paul 	 */
2531fc74a9f9SBrooks Davis 	ether_ifattach(ifp, eaddr);
2532b74e67fbSGleb Smirnoff 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
25330f9bd73bSSam Leffler 
25340f9bd73bSSam Leffler 	/*
25350f9bd73bSSam Leffler 	 * Hookup IRQ last.
25360f9bd73bSSam Leffler 	 */
25374e35d186SJung-uk Kim #if __FreeBSD_version > 700030
25380f9bd73bSSam Leffler 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
2539ef544f63SPaolo Pisati 	   NULL, bge_intr, sc, &sc->bge_intrhand);
25404e35d186SJung-uk Kim #else
25414e35d186SJung-uk Kim 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
25424e35d186SJung-uk Kim 	   bge_intr, sc, &sc->bge_intrhand);
25434e35d186SJung-uk Kim #endif
25440f9bd73bSSam Leffler 
25450f9bd73bSSam Leffler 	if (error) {
2546fc74a9f9SBrooks Davis 		bge_detach(dev);
2547fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "couldn't set up irq\n");
25480f9bd73bSSam Leffler 	}
254995d67482SBill Paul 
25506f8718a3SScott Long 	bge_add_sysctls(sc);
25516f8718a3SScott Long 
255295d67482SBill Paul fail:
255395d67482SBill Paul 	return (error);
255495d67482SBill Paul }
255595d67482SBill Paul 
255695d67482SBill Paul static int
25573f74909aSGleb Smirnoff bge_detach(device_t dev)
255895d67482SBill Paul {
255995d67482SBill Paul 	struct bge_softc *sc;
256095d67482SBill Paul 	struct ifnet *ifp;
256195d67482SBill Paul 
256295d67482SBill Paul 	sc = device_get_softc(dev);
2563fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
256495d67482SBill Paul 
256575719184SGleb Smirnoff #ifdef DEVICE_POLLING
256675719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
256775719184SGleb Smirnoff 		ether_poll_deregister(ifp);
256875719184SGleb Smirnoff #endif
256975719184SGleb Smirnoff 
25700f9bd73bSSam Leffler 	BGE_LOCK(sc);
257195d67482SBill Paul 	bge_stop(sc);
257295d67482SBill Paul 	bge_reset(sc);
25730f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
25740f9bd73bSSam Leffler 
25755dda8085SOleg Bulyzhin 	callout_drain(&sc->bge_stat_ch);
25765dda8085SOleg Bulyzhin 
25770f9bd73bSSam Leffler 	ether_ifdetach(ifp);
257895d67482SBill Paul 
2579652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
258095d67482SBill Paul 		ifmedia_removeall(&sc->bge_ifmedia);
258195d67482SBill Paul 	} else {
258295d67482SBill Paul 		bus_generic_detach(dev);
258395d67482SBill Paul 		device_delete_child(dev, sc->bge_miibus);
258495d67482SBill Paul 	}
258595d67482SBill Paul 
258695d67482SBill Paul 	bge_release_resources(sc);
258795d67482SBill Paul 
258895d67482SBill Paul 	return (0);
258995d67482SBill Paul }
259095d67482SBill Paul 
259195d67482SBill Paul static void
25923f74909aSGleb Smirnoff bge_release_resources(struct bge_softc *sc)
259395d67482SBill Paul {
259495d67482SBill Paul 	device_t dev;
259595d67482SBill Paul 
259695d67482SBill Paul 	dev = sc->bge_dev;
259795d67482SBill Paul 
259895d67482SBill Paul 	if (sc->bge_intrhand != NULL)
259995d67482SBill Paul 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
260095d67482SBill Paul 
260195d67482SBill Paul 	if (sc->bge_irq != NULL)
2602724bd939SJohn Polstra 		bus_release_resource(dev, SYS_RES_IRQ,
2603724bd939SJohn Polstra 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
2604724bd939SJohn Polstra 
26054e35d186SJung-uk Kim #if __FreeBSD_version > 700025
2606724bd939SJohn Polstra 	if (sc->bge_flags & BGE_FLAG_MSI)
2607724bd939SJohn Polstra 		pci_release_msi(dev);
26084e35d186SJung-uk Kim #endif
260995d67482SBill Paul 
261095d67482SBill Paul 	if (sc->bge_res != NULL)
261195d67482SBill Paul 		bus_release_resource(dev, SYS_RES_MEMORY,
261295d67482SBill Paul 		    BGE_PCI_BAR0, sc->bge_res);
261395d67482SBill Paul 
2614ad61f896SRuslan Ermilov 	if (sc->bge_ifp != NULL)
2615ad61f896SRuslan Ermilov 		if_free(sc->bge_ifp);
2616ad61f896SRuslan Ermilov 
2617f41ac2beSBill Paul 	bge_dma_free(sc);
261895d67482SBill Paul 
26190f9bd73bSSam Leffler 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
26200f9bd73bSSam Leffler 		BGE_LOCK_DESTROY(sc);
262195d67482SBill Paul }
262295d67482SBill Paul 
26238cb1383cSDoug Ambrisko static int
26243f74909aSGleb Smirnoff bge_reset(struct bge_softc *sc)
262595d67482SBill Paul {
262695d67482SBill Paul 	device_t dev;
26273f74909aSGleb Smirnoff 	uint32_t cachesize, command, pcistate, reset;
26286f8718a3SScott Long 	void (*write_op)(struct bge_softc *, int, int);
262995d67482SBill Paul 	int i, val = 0;
263095d67482SBill Paul 
263195d67482SBill Paul 	dev = sc->bge_dev;
263295d67482SBill Paul 
2633464223f7SJung-uk Kim 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc)) {
26346f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
26356f8718a3SScott Long 			write_op = bge_writemem_direct;
26366f8718a3SScott Long 		else
26376f8718a3SScott Long 			write_op = bge_writemem_ind;
26389ba784dbSScott Long 	} else
26396f8718a3SScott Long 		write_op = bge_writereg_ind;
26406f8718a3SScott Long 
264195d67482SBill Paul 	/* Save some important PCI state. */
264295d67482SBill Paul 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
264395d67482SBill Paul 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
264495d67482SBill Paul 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
264595d67482SBill Paul 
264695d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
264795d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
2648e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
264995d67482SBill Paul 
26506f8718a3SScott Long 	/* Disable fastboot on controllers that support it. */
26516f8718a3SScott Long 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
26526f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
26536f8718a3SScott Long 	    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
26546f8718a3SScott Long 		if (bootverbose)
26559ba784dbSScott Long 			device_printf(sc->bge_dev, "Disabling fastboot\n");
26566f8718a3SScott Long 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
26576f8718a3SScott Long 	}
26586f8718a3SScott Long 
26596f8718a3SScott Long 	/*
26606f8718a3SScott Long 	 * Write the magic number to SRAM at offset 0xB50.
26616f8718a3SScott Long 	 * When firmware finishes its initialization it will
26626f8718a3SScott Long 	 * write ~BGE_MAGIC_NUMBER to the same location.
26636f8718a3SScott Long 	 */
26646f8718a3SScott Long 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
26656f8718a3SScott Long 
2666e53d81eeSPaul Saab 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | (65 << 1);
2667e53d81eeSPaul Saab 
2668e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2669652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2670e53d81eeSPaul Saab 		if (CSR_READ_4(sc, 0x7e2c) == 0x60)	/* PCIE 1.0 */
2671e53d81eeSPaul Saab 			CSR_WRITE_4(sc, 0x7e2c, 0x20);
2672e53d81eeSPaul Saab 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2673e53d81eeSPaul Saab 			/* Prevent PCIE link training during global reset */
2674e53d81eeSPaul Saab 			CSR_WRITE_4(sc, BGE_MISC_CFG, (1 << 29));
2675e53d81eeSPaul Saab 			reset |= (1 << 29);
2676e53d81eeSPaul Saab 		}
2677e53d81eeSPaul Saab 	}
2678e53d81eeSPaul Saab 
267921c9e407SDavid Christensen 	/*
26806f8718a3SScott Long 	 * Set GPHY Power Down Override to leave GPHY
26816f8718a3SScott Long 	 * powered up in D0 uninitialized.
26826f8718a3SScott Long 	 */
26835345bad0SScott Long 	if (BGE_IS_5705_PLUS(sc))
26846f8718a3SScott Long 		reset |= 0x04000000;
26856f8718a3SScott Long 
268695d67482SBill Paul 	/* Issue global reset */
26876f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, reset);
268895d67482SBill Paul 
268995d67482SBill Paul 	DELAY(1000);
269095d67482SBill Paul 
2691e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2692652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2693e53d81eeSPaul Saab 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2694e53d81eeSPaul Saab 			uint32_t v;
2695e53d81eeSPaul Saab 
2696e53d81eeSPaul Saab 			DELAY(500000); /* wait for link training to complete */
2697e53d81eeSPaul Saab 			v = pci_read_config(dev, 0xc4, 4);
2698e53d81eeSPaul Saab 			pci_write_config(dev, 0xc4, v | (1 << 15), 4);
2699e53d81eeSPaul Saab 		}
27009ba784dbSScott Long 		/*
27019ba784dbSScott Long 		 * Set PCIE max payload size to 128 bytes and clear error
27029ba784dbSScott Long 		 * status.
27039ba784dbSScott Long 		 */
2704e53d81eeSPaul Saab 		pci_write_config(dev, 0xd8, 0xf5000, 4);
2705e53d81eeSPaul Saab 	}
2706e53d81eeSPaul Saab 
27073f74909aSGleb Smirnoff 	/* Reset some of the PCI state that got zapped by reset. */
270895d67482SBill Paul 	pci_write_config(dev, BGE_PCI_MISC_CTL,
270995d67482SBill Paul 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
2710e907febfSPyun YongHyeon 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
271195d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
271295d67482SBill Paul 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
27136f8718a3SScott Long 	write_op(sc, BGE_MISC_CFG, (65 << 1));
271495d67482SBill Paul 
2715bf6ef57aSJohn Polstra 	/* Re-enable MSI, if neccesary, and enable the memory arbiter. */
27164c0da0ffSGleb Smirnoff 	if (BGE_IS_5714_FAMILY(sc)) {
27174c0da0ffSGleb Smirnoff 		uint32_t val;
27184c0da0ffSGleb Smirnoff 
2719bf6ef57aSJohn Polstra 		/* This chip disables MSI on reset. */
2720bf6ef57aSJohn Polstra 		if (sc->bge_flags & BGE_FLAG_MSI) {
2721bf6ef57aSJohn Polstra 			val = pci_read_config(dev, BGE_PCI_MSI_CTL, 2);
2722bf6ef57aSJohn Polstra 			pci_write_config(dev, BGE_PCI_MSI_CTL,
2723bf6ef57aSJohn Polstra 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
2724bf6ef57aSJohn Polstra 			val = CSR_READ_4(sc, BGE_MSI_MODE);
2725bf6ef57aSJohn Polstra 			CSR_WRITE_4(sc, BGE_MSI_MODE,
2726bf6ef57aSJohn Polstra 			    val | BGE_MSIMODE_ENABLE);
2727bf6ef57aSJohn Polstra 		}
27284c0da0ffSGleb Smirnoff 		val = CSR_READ_4(sc, BGE_MARB_MODE);
27294c0da0ffSGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
27304c0da0ffSGleb Smirnoff 	} else
2731a7b0c314SPaul Saab 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2732a7b0c314SPaul Saab 
273395d67482SBill Paul 	/*
27346f8718a3SScott Long 	 * Poll until we see the 1's complement of the magic number.
273595d67482SBill Paul 	 * This indicates that the firmware initialization
273695d67482SBill Paul 	 * is complete.
273795d67482SBill Paul 	 */
273895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
273995d67482SBill Paul 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
274095d67482SBill Paul 		if (val == ~BGE_MAGIC_NUMBER)
274195d67482SBill Paul 			break;
274295d67482SBill Paul 		DELAY(10);
274395d67482SBill Paul 	}
274495d67482SBill Paul 
274595d67482SBill Paul 	if (i == BGE_TIMEOUT) {
27469ba784dbSScott Long 		device_printf(sc->bge_dev, "firmware handshake timed out, "
27479ba784dbSScott Long 		    "found 0x%08x\n", val);
274895d67482SBill Paul 	}
274995d67482SBill Paul 
275095d67482SBill Paul 	/*
275195d67482SBill Paul 	 * XXX Wait for the value of the PCISTATE register to
275295d67482SBill Paul 	 * return to its original pre-reset state. This is a
275395d67482SBill Paul 	 * fairly good indicator of reset completion. If we don't
275495d67482SBill Paul 	 * wait for the reset to fully complete, trying to read
275595d67482SBill Paul 	 * from the device's non-PCI registers may yield garbage
275695d67482SBill Paul 	 * results.
275795d67482SBill Paul 	 */
275895d67482SBill Paul 	for (i = 0; i < BGE_TIMEOUT; i++) {
275995d67482SBill Paul 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
276095d67482SBill Paul 			break;
276195d67482SBill Paul 		DELAY(10);
276295d67482SBill Paul 	}
276395d67482SBill Paul 
27646f8718a3SScott Long 	if (sc->bge_flags & BGE_FLAG_PCIE) {
27656f8718a3SScott Long 		reset = bge_readmem_ind(sc, 0x7c00);
27666f8718a3SScott Long 		bge_writemem_ind(sc, 0x7c00, reset | (1 << 25));
27676f8718a3SScott Long 	}
27686f8718a3SScott Long 
27693f74909aSGleb Smirnoff 	/* Fix up byte swapping. */
2770e907febfSPyun YongHyeon 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
277195d67482SBill Paul 	    BGE_MODECTL_BYTESWAP_DATA);
277295d67482SBill Paul 
27738cb1383cSDoug Ambrisko 	/* Tell the ASF firmware we are up */
27748cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
27758cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
27768cb1383cSDoug Ambrisko 
277795d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
277895d67482SBill Paul 
2779da3003f0SBill Paul 	/*
2780da3003f0SBill Paul 	 * The 5704 in TBI mode apparently needs some special
2781da3003f0SBill Paul 	 * adjustment to insure the SERDES drive level is set
2782da3003f0SBill Paul 	 * to 1.2V.
2783da3003f0SBill Paul 	 */
2784652ae483SGleb Smirnoff 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
2785652ae483SGleb Smirnoff 	    sc->bge_flags & BGE_FLAG_TBI) {
2786da3003f0SBill Paul 		uint32_t serdescfg;
2787652ae483SGleb Smirnoff 
2788da3003f0SBill Paul 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
27896098821cSJung-uk Kim 		serdescfg = (serdescfg & ~0xfff) | 0x880;
2790da3003f0SBill Paul 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2791da3003f0SBill Paul 	}
2792da3003f0SBill Paul 
2793e53d81eeSPaul Saab 	/* XXX: Broadcom Linux driver. */
2794652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_PCIE &&
2795652ae483SGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2796e53d81eeSPaul Saab 		uint32_t v;
2797e53d81eeSPaul Saab 
2798e53d81eeSPaul Saab 		v = CSR_READ_4(sc, 0x7c00);
2799e53d81eeSPaul Saab 		CSR_WRITE_4(sc, 0x7c00, v | (1 << 25));
2800e53d81eeSPaul Saab 	}
280195d67482SBill Paul 	DELAY(10000);
28028cb1383cSDoug Ambrisko 
28038cb1383cSDoug Ambrisko 	return(0);
280495d67482SBill Paul }
280595d67482SBill Paul 
280695d67482SBill Paul /*
280795d67482SBill Paul  * Frame reception handling. This is called if there's a frame
280895d67482SBill Paul  * on the receive return list.
280995d67482SBill Paul  *
281095d67482SBill Paul  * Note: we have to be able to handle two possibilities here:
28111be6acb7SGleb Smirnoff  * 1) the frame is from the jumbo receive ring
281295d67482SBill Paul  * 2) the frame is from the standard receive ring
281395d67482SBill Paul  */
281495d67482SBill Paul 
281595d67482SBill Paul static void
28163f74909aSGleb Smirnoff bge_rxeof(struct bge_softc *sc)
281795d67482SBill Paul {
281895d67482SBill Paul 	struct ifnet *ifp;
281995d67482SBill Paul 	int stdcnt = 0, jumbocnt = 0;
282095d67482SBill Paul 
28210f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
28220f9bd73bSSam Leffler 
28233f74909aSGleb Smirnoff 	/* Nothing to do. */
2824cfcb5025SOleg Bulyzhin 	if (sc->bge_rx_saved_considx ==
2825cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx)
2826cfcb5025SOleg Bulyzhin 		return;
2827cfcb5025SOleg Bulyzhin 
2828fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
282995d67482SBill Paul 
2830f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2831e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
2832f41ac2beSBill Paul 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2833f41ac2beSBill Paul 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
28344c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
2835f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
28364c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTREAD);
2837f41ac2beSBill Paul 
283895d67482SBill Paul 	while(sc->bge_rx_saved_considx !=
2839f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
284095d67482SBill Paul 		struct bge_rx_bd	*cur_rx;
28413f74909aSGleb Smirnoff 		uint32_t		rxidx;
284295d67482SBill Paul 		struct mbuf		*m = NULL;
28433f74909aSGleb Smirnoff 		uint16_t		vlan_tag = 0;
284495d67482SBill Paul 		int			have_tag = 0;
284595d67482SBill Paul 
284675719184SGleb Smirnoff #ifdef DEVICE_POLLING
284775719184SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
284875719184SGleb Smirnoff 			if (sc->rxcycles <= 0)
284975719184SGleb Smirnoff 				break;
285075719184SGleb Smirnoff 			sc->rxcycles--;
285175719184SGleb Smirnoff 		}
285275719184SGleb Smirnoff #endif
285375719184SGleb Smirnoff 
285495d67482SBill Paul 		cur_rx =
2855f41ac2beSBill Paul 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
285695d67482SBill Paul 
285795d67482SBill Paul 		rxidx = cur_rx->bge_idx;
28580434d1b8SBill Paul 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
285995d67482SBill Paul 
286045ee6ab3SJung-uk Kim 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
286195d67482SBill Paul 			have_tag = 1;
286295d67482SBill Paul 			vlan_tag = cur_rx->bge_vlan_tag;
286395d67482SBill Paul 		}
286495d67482SBill Paul 
286595d67482SBill Paul 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
286695d67482SBill Paul 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2867f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2868f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2869f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2870f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2871f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
287295d67482SBill Paul 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
287395d67482SBill Paul 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
287495d67482SBill Paul 			jumbocnt++;
287595d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
287695d67482SBill Paul 				ifp->if_ierrors++;
287795d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
287895d67482SBill Paul 				continue;
287995d67482SBill Paul 			}
288095d67482SBill Paul 			if (bge_newbuf_jumbo(sc,
288195d67482SBill Paul 			    sc->bge_jumbo, NULL) == ENOBUFS) {
288295d67482SBill Paul 				ifp->if_ierrors++;
288395d67482SBill Paul 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
288495d67482SBill Paul 				continue;
288595d67482SBill Paul 			}
288695d67482SBill Paul 		} else {
288795d67482SBill Paul 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2888f41ac2beSBill Paul 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2889f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2890f41ac2beSBill Paul 			    BUS_DMASYNC_POSTREAD);
2891f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2892f41ac2beSBill Paul 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
289395d67482SBill Paul 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
289495d67482SBill Paul 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
289595d67482SBill Paul 			stdcnt++;
289695d67482SBill Paul 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
289795d67482SBill Paul 				ifp->if_ierrors++;
289895d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
289995d67482SBill Paul 				continue;
290095d67482SBill Paul 			}
290195d67482SBill Paul 			if (bge_newbuf_std(sc, sc->bge_std,
290295d67482SBill Paul 			    NULL) == ENOBUFS) {
290395d67482SBill Paul 				ifp->if_ierrors++;
290495d67482SBill Paul 				bge_newbuf_std(sc, sc->bge_std, m);
290595d67482SBill Paul 				continue;
290695d67482SBill Paul 			}
290795d67482SBill Paul 		}
290895d67482SBill Paul 
290995d67482SBill Paul 		ifp->if_ipackets++;
2910e65bed95SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2911e255b776SJohn Polstra 		/*
2912e65bed95SPyun YongHyeon 		 * For architectures with strict alignment we must make sure
2913e65bed95SPyun YongHyeon 		 * the payload is aligned.
2914e255b776SJohn Polstra 		 */
2915652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
2916e255b776SJohn Polstra 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2917e255b776SJohn Polstra 			    cur_rx->bge_len);
2918e255b776SJohn Polstra 			m->m_data += ETHER_ALIGN;
2919e255b776SJohn Polstra 		}
2920e255b776SJohn Polstra #endif
2921473851baSPaul Saab 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
292295d67482SBill Paul 		m->m_pkthdr.rcvif = ifp;
292395d67482SBill Paul 
2924b874fdd4SYaroslav Tykhiy 		if (ifp->if_capenable & IFCAP_RXCSUM) {
292578178cd1SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
292695d67482SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
292795d67482SBill Paul 				if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
29286098821cSJung-uk Kim 					m->m_pkthdr.csum_flags |=
29296098821cSJung-uk Kim 					    CSUM_IP_VALID;
293078178cd1SGleb Smirnoff 			}
2931d375e524SGleb Smirnoff 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
2932d375e524SGleb Smirnoff 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
293395d67482SBill Paul 				m->m_pkthdr.csum_data =
293495d67482SBill Paul 				    cur_rx->bge_tcp_udp_csum;
2935ee7ef91cSOleg Bulyzhin 				m->m_pkthdr.csum_flags |=
2936ee7ef91cSOleg Bulyzhin 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
293795d67482SBill Paul 			}
293895d67482SBill Paul 		}
293995d67482SBill Paul 
294095d67482SBill Paul 		/*
2941673d9191SSam Leffler 		 * If we received a packet with a vlan tag,
2942673d9191SSam Leffler 		 * attach that information to the packet.
294395d67482SBill Paul 		 */
2944d147662cSGleb Smirnoff 		if (have_tag) {
29454e35d186SJung-uk Kim #if __FreeBSD_version > 700022
294678ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag = vlan_tag;
294778ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
29484e35d186SJung-uk Kim #else
29494e35d186SJung-uk Kim 			VLAN_INPUT_TAG_NEW(ifp, m, vlan_tag);
29504e35d186SJung-uk Kim 			if (m == NULL)
29514e35d186SJung-uk Kim 				continue;
29524e35d186SJung-uk Kim #endif
2953d147662cSGleb Smirnoff 		}
295495d67482SBill Paul 
29550f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
2956673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
29570f9bd73bSSam Leffler 		BGE_LOCK(sc);
295895d67482SBill Paul 	}
295995d67482SBill Paul 
2960e65bed95SPyun YongHyeon 	if (stdcnt > 0)
2961f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2962e65bed95SPyun YongHyeon 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
29634c0da0ffSGleb Smirnoff 
29644c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc) && jumbocnt > 0)
2965f41ac2beSBill Paul 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
29664c0da0ffSGleb Smirnoff 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
2967f41ac2beSBill Paul 
296895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
296995d67482SBill Paul 	if (stdcnt)
297095d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
297195d67482SBill Paul 	if (jumbocnt)
297295d67482SBill Paul 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
29736b037352SJung-uk Kim #ifdef notyet
29746b037352SJung-uk Kim 	/*
29756b037352SJung-uk Kim 	 * This register wraps very quickly under heavy packet drops.
29766b037352SJung-uk Kim 	 * If you need correct statistics, you can enable this check.
29776b037352SJung-uk Kim 	 */
29786b037352SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
29796b037352SJung-uk Kim 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
29806b037352SJung-uk Kim #endif
298195d67482SBill Paul }
298295d67482SBill Paul 
298395d67482SBill Paul static void
29843f74909aSGleb Smirnoff bge_txeof(struct bge_softc *sc)
298595d67482SBill Paul {
298695d67482SBill Paul 	struct bge_tx_bd *cur_tx = NULL;
298795d67482SBill Paul 	struct ifnet *ifp;
298895d67482SBill Paul 
29890f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
29900f9bd73bSSam Leffler 
29913f74909aSGleb Smirnoff 	/* Nothing to do. */
2992cfcb5025SOleg Bulyzhin 	if (sc->bge_tx_saved_considx ==
2993cfcb5025SOleg Bulyzhin 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx)
2994cfcb5025SOleg Bulyzhin 		return;
2995cfcb5025SOleg Bulyzhin 
2996fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
299795d67482SBill Paul 
2998e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
2999e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_tx_ring_map,
3000e65bed95SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
300195d67482SBill Paul 	/*
300295d67482SBill Paul 	 * Go through our tx ring and free mbufs for those
300395d67482SBill Paul 	 * frames that have been sent.
300495d67482SBill Paul 	 */
300595d67482SBill Paul 	while (sc->bge_tx_saved_considx !=
3006f41ac2beSBill Paul 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
30073f74909aSGleb Smirnoff 		uint32_t		idx = 0;
300895d67482SBill Paul 
300995d67482SBill Paul 		idx = sc->bge_tx_saved_considx;
3010f41ac2beSBill Paul 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
301195d67482SBill Paul 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
301295d67482SBill Paul 			ifp->if_opackets++;
301395d67482SBill Paul 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
3014e65bed95SPyun YongHyeon 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
3015e65bed95SPyun YongHyeon 			    sc->bge_cdata.bge_tx_dmamap[idx],
3016e65bed95SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
3017f41ac2beSBill Paul 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
3018f41ac2beSBill Paul 			    sc->bge_cdata.bge_tx_dmamap[idx]);
3019e65bed95SPyun YongHyeon 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
3020e65bed95SPyun YongHyeon 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
302195d67482SBill Paul 		}
302295d67482SBill Paul 		sc->bge_txcnt--;
302395d67482SBill Paul 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
302495d67482SBill Paul 	}
302595d67482SBill Paul 
302695d67482SBill Paul 	if (cur_tx != NULL)
302713f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
30285b01e77cSBruce Evans 	if (sc->bge_txcnt == 0)
30295b01e77cSBruce Evans 		sc->bge_timer = 0;
303095d67482SBill Paul }
303195d67482SBill Paul 
303275719184SGleb Smirnoff #ifdef DEVICE_POLLING
303375719184SGleb Smirnoff static void
303475719184SGleb Smirnoff bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
303575719184SGleb Smirnoff {
303675719184SGleb Smirnoff 	struct bge_softc *sc = ifp->if_softc;
3037366454f2SOleg Bulyzhin 	uint32_t statusword;
303875719184SGleb Smirnoff 
30393f74909aSGleb Smirnoff 	BGE_LOCK(sc);
30403f74909aSGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
30413f74909aSGleb Smirnoff 		BGE_UNLOCK(sc);
30423f74909aSGleb Smirnoff 		return;
30433f74909aSGleb Smirnoff 	}
304475719184SGleb Smirnoff 
3045dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3046e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
3047dab5cd05SOleg Bulyzhin 
30483f74909aSGleb Smirnoff 	statusword = atomic_readandclear_32(
30493f74909aSGleb Smirnoff 	    &sc->bge_ldata.bge_status_block->bge_status);
3050dab5cd05SOleg Bulyzhin 
3051dab5cd05SOleg Bulyzhin 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3052e65bed95SPyun YongHyeon 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
3053366454f2SOleg Bulyzhin 
30546098821cSJung-uk Kim 	/*
30556098821cSJung-uk Kim 	 * Note link event. It will be processed
30566098821cSJung-uk Kim 	 * by POLL_AND_CHECK_STATUS command.
30576098821cSJung-uk Kim 	 */
3058366454f2SOleg Bulyzhin 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
3059366454f2SOleg Bulyzhin 		sc->bge_link_evt++;
3060366454f2SOleg Bulyzhin 
3061366454f2SOleg Bulyzhin 	if (cmd == POLL_AND_CHECK_STATUS)
3062366454f2SOleg Bulyzhin 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
30634c0da0ffSGleb Smirnoff 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3064652ae483SGleb Smirnoff 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
3065366454f2SOleg Bulyzhin 			bge_link_upd(sc);
3066366454f2SOleg Bulyzhin 
3067366454f2SOleg Bulyzhin 	sc->rxcycles = count;
3068366454f2SOleg Bulyzhin 	bge_rxeof(sc);
3069366454f2SOleg Bulyzhin 	bge_txeof(sc);
3070366454f2SOleg Bulyzhin 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3071366454f2SOleg Bulyzhin 		bge_start_locked(ifp);
30723f74909aSGleb Smirnoff 
30733f74909aSGleb Smirnoff 	BGE_UNLOCK(sc);
307475719184SGleb Smirnoff }
307575719184SGleb Smirnoff #endif /* DEVICE_POLLING */
307675719184SGleb Smirnoff 
307795d67482SBill Paul static void
30783f74909aSGleb Smirnoff bge_intr(void *xsc)
307995d67482SBill Paul {
308095d67482SBill Paul 	struct bge_softc *sc;
308195d67482SBill Paul 	struct ifnet *ifp;
3082dab5cd05SOleg Bulyzhin 	uint32_t statusword;
308395d67482SBill Paul 
308495d67482SBill Paul 	sc = xsc;
3085f41ac2beSBill Paul 
30860f9bd73bSSam Leffler 	BGE_LOCK(sc);
30870f9bd73bSSam Leffler 
3088dab5cd05SOleg Bulyzhin 	ifp = sc->bge_ifp;
3089dab5cd05SOleg Bulyzhin 
309075719184SGleb Smirnoff #ifdef DEVICE_POLLING
309175719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
309275719184SGleb Smirnoff 		BGE_UNLOCK(sc);
309375719184SGleb Smirnoff 		return;
309475719184SGleb Smirnoff 	}
309575719184SGleb Smirnoff #endif
309675719184SGleb Smirnoff 
3097f30cbfc6SScott Long 	/*
3098b848e032SBruce Evans 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
3099b848e032SBruce Evans 	 * disable interrupts by writing nonzero like we used to, since with
3100b848e032SBruce Evans 	 * our current organization this just gives complications and
3101b848e032SBruce Evans 	 * pessimizations for re-enabling interrupts.  We used to have races
3102b848e032SBruce Evans 	 * instead of the necessary complications.  Disabling interrupts
3103b848e032SBruce Evans 	 * would just reduce the chance of a status update while we are
3104b848e032SBruce Evans 	 * running (by switching to the interrupt-mode coalescence
3105b848e032SBruce Evans 	 * parameters), but this chance is already very low so it is more
3106b848e032SBruce Evans 	 * efficient to get another interrupt than prevent it.
3107b848e032SBruce Evans 	 *
3108b848e032SBruce Evans 	 * We do the ack first to ensure another interrupt if there is a
3109b848e032SBruce Evans 	 * status update after the ack.  We don't check for the status
3110b848e032SBruce Evans 	 * changing later because it is more efficient to get another
3111b848e032SBruce Evans 	 * interrupt than prevent it, not quite as above (not checking is
3112b848e032SBruce Evans 	 * a smaller optimization than not toggling the interrupt enable,
3113b848e032SBruce Evans 	 * since checking doesn't involve PCI accesses and toggling require
3114b848e032SBruce Evans 	 * the status check).  So toggling would probably be a pessimization
3115b848e032SBruce Evans 	 * even with MSI.  It would only be needed for using a task queue.
3116b848e032SBruce Evans 	 */
3117b848e032SBruce Evans 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
3118b848e032SBruce Evans 
3119b848e032SBruce Evans 	/*
3120f30cbfc6SScott Long 	 * Do the mandatory PCI flush as well as get the link status.
3121f30cbfc6SScott Long 	 */
3122f30cbfc6SScott Long 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
3123f41ac2beSBill Paul 
3124f30cbfc6SScott Long 	/* Make sure the descriptor ring indexes are coherent. */
3125f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3126f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
3127f30cbfc6SScott Long 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3128f30cbfc6SScott Long 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
3129f30cbfc6SScott Long 
31301f313773SOleg Bulyzhin 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
31314c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3132f30cbfc6SScott Long 	    statusword || sc->bge_link_evt)
3133dab5cd05SOleg Bulyzhin 		bge_link_upd(sc);
313495d67482SBill Paul 
313513f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
31363f74909aSGleb Smirnoff 		/* Check RX return ring producer/consumer. */
313795d67482SBill Paul 		bge_rxeof(sc);
313895d67482SBill Paul 
31393f74909aSGleb Smirnoff 		/* Check TX ring producer/consumer. */
314095d67482SBill Paul 		bge_txeof(sc);
314195d67482SBill Paul 	}
314295d67482SBill Paul 
314313f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
314413f4c340SRobert Watson 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
31450f9bd73bSSam Leffler 		bge_start_locked(ifp);
31460f9bd73bSSam Leffler 
31470f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
314895d67482SBill Paul }
314995d67482SBill Paul 
315095d67482SBill Paul static void
31518cb1383cSDoug Ambrisko bge_asf_driver_up(struct bge_softc *sc)
31528cb1383cSDoug Ambrisko {
31538cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP) {
31548cb1383cSDoug Ambrisko 		/* Send ASF heartbeat aprox. every 2s */
31558cb1383cSDoug Ambrisko 		if (sc->bge_asf_count)
31568cb1383cSDoug Ambrisko 			sc->bge_asf_count --;
31578cb1383cSDoug Ambrisko 		else {
31588cb1383cSDoug Ambrisko 			sc->bge_asf_count = 5;
31598cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW,
31608cb1383cSDoug Ambrisko 			    BGE_FW_DRV_ALIVE);
31618cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_LEN, 4);
31628cb1383cSDoug Ambrisko 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_DATA, 3);
31638cb1383cSDoug Ambrisko 			CSR_WRITE_4(sc, BGE_CPU_EVENT,
31648cb1383cSDoug Ambrisko 			    CSR_READ_4(sc, BGE_CPU_EVENT) != (1 << 14));
31658cb1383cSDoug Ambrisko 		}
31668cb1383cSDoug Ambrisko 	}
31678cb1383cSDoug Ambrisko }
31688cb1383cSDoug Ambrisko 
31698cb1383cSDoug Ambrisko static void
3170b74e67fbSGleb Smirnoff bge_tick(void *xsc)
31710f9bd73bSSam Leffler {
3172b74e67fbSGleb Smirnoff 	struct bge_softc *sc = xsc;
317395d67482SBill Paul 	struct mii_data *mii = NULL;
317495d67482SBill Paul 
31750f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
317695d67482SBill Paul 
31775dda8085SOleg Bulyzhin 	/* Synchronize with possible callout reset/stop. */
31785dda8085SOleg Bulyzhin 	if (callout_pending(&sc->bge_stat_ch) ||
31795dda8085SOleg Bulyzhin 	    !callout_active(&sc->bge_stat_ch))
31805dda8085SOleg Bulyzhin 	    	return;
31815dda8085SOleg Bulyzhin 
31827ee00338SJung-uk Kim 	if (BGE_IS_5705_PLUS(sc))
31830434d1b8SBill Paul 		bge_stats_update_regs(sc);
31840434d1b8SBill Paul 	else
318595d67482SBill Paul 		bge_stats_update(sc);
318695d67482SBill Paul 
3187652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
318895d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
31898cb1383cSDoug Ambrisko 		/* Don't mess with the PHY in IPMI/ASF mode */
31908cb1383cSDoug Ambrisko 		if (!((sc->bge_asf_mode & ASF_STACKUP) && (sc->bge_link)))
319195d67482SBill Paul 			mii_tick(mii);
31927b97099dSOleg Bulyzhin 	} else {
31937b97099dSOleg Bulyzhin 		/*
31947b97099dSOleg Bulyzhin 		 * Since in TBI mode auto-polling can't be used we should poll
31957b97099dSOleg Bulyzhin 		 * link status manually. Here we register pending link event
31967b97099dSOleg Bulyzhin 		 * and trigger interrupt.
31977b97099dSOleg Bulyzhin 		 */
31987b97099dSOleg Bulyzhin #ifdef DEVICE_POLLING
31993f74909aSGleb Smirnoff 		/* In polling mode we poll link state in bge_poll(). */
32007b97099dSOleg Bulyzhin 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
32017b97099dSOleg Bulyzhin #endif
32027b97099dSOleg Bulyzhin 		{
32037b97099dSOleg Bulyzhin 		sc->bge_link_evt++;
32047b97099dSOleg Bulyzhin 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
32057b97099dSOleg Bulyzhin 		}
3206dab5cd05SOleg Bulyzhin 	}
320795d67482SBill Paul 
32088cb1383cSDoug Ambrisko 	bge_asf_driver_up(sc);
3209b74e67fbSGleb Smirnoff 	bge_watchdog(sc);
32108cb1383cSDoug Ambrisko 
3211dab5cd05SOleg Bulyzhin 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
321295d67482SBill Paul }
321395d67482SBill Paul 
321495d67482SBill Paul static void
32153f74909aSGleb Smirnoff bge_stats_update_regs(struct bge_softc *sc)
32160434d1b8SBill Paul {
32173f74909aSGleb Smirnoff 	struct ifnet *ifp;
32180434d1b8SBill Paul 
3219fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
32200434d1b8SBill Paul 
32216b037352SJung-uk Kim 	ifp->if_collisions += CSR_READ_4(sc, BGE_MAC_STATS +
32227e6e2507SJung-uk Kim 	    offsetof(struct bge_mac_stats_regs, etherStatsCollisions));
32237e6e2507SJung-uk Kim 
32246b037352SJung-uk Kim 	ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
32250434d1b8SBill Paul }
32260434d1b8SBill Paul 
32270434d1b8SBill Paul static void
32283f74909aSGleb Smirnoff bge_stats_update(struct bge_softc *sc)
322995d67482SBill Paul {
323095d67482SBill Paul 	struct ifnet *ifp;
3231e907febfSPyun YongHyeon 	bus_size_t stats;
32327e6e2507SJung-uk Kim 	uint32_t cnt;	/* current register value */
323395d67482SBill Paul 
3234fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
323595d67482SBill Paul 
3236e907febfSPyun YongHyeon 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
3237e907febfSPyun YongHyeon 
3238e907febfSPyun YongHyeon #define	READ_STAT(sc, stats, stat) \
3239e907febfSPyun YongHyeon 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
324095d67482SBill Paul 
32418634dfffSJung-uk Kim 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
32426b037352SJung-uk Kim 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
32436fb34dd2SOleg Bulyzhin 	sc->bge_tx_collisions = cnt;
32446fb34dd2SOleg Bulyzhin 
32456fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
32466b037352SJung-uk Kim 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
32476fb34dd2SOleg Bulyzhin 	sc->bge_rx_discards = cnt;
32486fb34dd2SOleg Bulyzhin 
32496fb34dd2SOleg Bulyzhin 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
32506b037352SJung-uk Kim 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
32516fb34dd2SOleg Bulyzhin 	sc->bge_tx_discards = cnt;
325295d67482SBill Paul 
3253e907febfSPyun YongHyeon #undef	READ_STAT
325495d67482SBill Paul }
325595d67482SBill Paul 
325695d67482SBill Paul /*
3257d375e524SGleb Smirnoff  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
3258d375e524SGleb Smirnoff  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
3259d375e524SGleb Smirnoff  * but when such padded frames employ the bge IP/TCP checksum offload,
3260d375e524SGleb Smirnoff  * the hardware checksum assist gives incorrect results (possibly
3261d375e524SGleb Smirnoff  * from incorporating its own padding into the UDP/TCP checksum; who knows).
3262d375e524SGleb Smirnoff  * If we pad such runts with zeros, the onboard checksum comes out correct.
3263d375e524SGleb Smirnoff  */
3264d375e524SGleb Smirnoff static __inline int
3265d375e524SGleb Smirnoff bge_cksum_pad(struct mbuf *m)
3266d375e524SGleb Smirnoff {
3267d375e524SGleb Smirnoff 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
3268d375e524SGleb Smirnoff 	struct mbuf *last;
3269d375e524SGleb Smirnoff 
3270d375e524SGleb Smirnoff 	/* If there's only the packet-header and we can pad there, use it. */
3271d375e524SGleb Smirnoff 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
3272d375e524SGleb Smirnoff 	    M_TRAILINGSPACE(m) >= padlen) {
3273d375e524SGleb Smirnoff 		last = m;
3274d375e524SGleb Smirnoff 	} else {
3275d375e524SGleb Smirnoff 		/*
3276d375e524SGleb Smirnoff 		 * Walk packet chain to find last mbuf. We will either
3277d375e524SGleb Smirnoff 		 * pad there, or append a new mbuf and pad it.
3278d375e524SGleb Smirnoff 		 */
3279d375e524SGleb Smirnoff 		for (last = m; last->m_next != NULL; last = last->m_next);
3280d375e524SGleb Smirnoff 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
3281d375e524SGleb Smirnoff 			/* Allocate new empty mbuf, pad it. Compact later. */
3282d375e524SGleb Smirnoff 			struct mbuf *n;
3283d375e524SGleb Smirnoff 
3284d375e524SGleb Smirnoff 			MGET(n, M_DONTWAIT, MT_DATA);
3285d375e524SGleb Smirnoff 			if (n == NULL)
3286d375e524SGleb Smirnoff 				return (ENOBUFS);
3287d375e524SGleb Smirnoff 			n->m_len = 0;
3288d375e524SGleb Smirnoff 			last->m_next = n;
3289d375e524SGleb Smirnoff 			last = n;
3290d375e524SGleb Smirnoff 		}
3291d375e524SGleb Smirnoff 	}
3292d375e524SGleb Smirnoff 
3293d375e524SGleb Smirnoff 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
3294d375e524SGleb Smirnoff 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
3295d375e524SGleb Smirnoff 	last->m_len += padlen;
3296d375e524SGleb Smirnoff 	m->m_pkthdr.len += padlen;
3297d375e524SGleb Smirnoff 
3298d375e524SGleb Smirnoff 	return (0);
3299d375e524SGleb Smirnoff }
3300d375e524SGleb Smirnoff 
3301d375e524SGleb Smirnoff /*
330295d67482SBill Paul  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
330395d67482SBill Paul  * pointers to descriptors.
330495d67482SBill Paul  */
330595d67482SBill Paul static int
3306676ad2c9SGleb Smirnoff bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
330795d67482SBill Paul {
33087e27542aSGleb Smirnoff 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
3309f41ac2beSBill Paul 	bus_dmamap_t		map;
3310676ad2c9SGleb Smirnoff 	struct bge_tx_bd	*d;
3311676ad2c9SGleb Smirnoff 	struct mbuf		*m = *m_head;
33127e27542aSGleb Smirnoff 	uint32_t		idx = *txidx;
3313676ad2c9SGleb Smirnoff 	uint16_t		csum_flags;
33147e27542aSGleb Smirnoff 	int			nsegs, i, error;
331595d67482SBill Paul 
33166909dc43SGleb Smirnoff 	csum_flags = 0;
33176909dc43SGleb Smirnoff 	if (m->m_pkthdr.csum_flags) {
33186909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP)
33196909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
33206909dc43SGleb Smirnoff 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
33216909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
33226909dc43SGleb Smirnoff 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
33236909dc43SGleb Smirnoff 			    (error = bge_cksum_pad(m)) != 0) {
33246909dc43SGleb Smirnoff 				m_freem(m);
33256909dc43SGleb Smirnoff 				*m_head = NULL;
33266909dc43SGleb Smirnoff 				return (error);
33276909dc43SGleb Smirnoff 			}
33286909dc43SGleb Smirnoff 		}
33296909dc43SGleb Smirnoff 		if (m->m_flags & M_LASTFRAG)
33306909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
33316909dc43SGleb Smirnoff 		else if (m->m_flags & M_FRAG)
33326909dc43SGleb Smirnoff 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
33336909dc43SGleb Smirnoff 	}
33346909dc43SGleb Smirnoff 
33357e27542aSGleb Smirnoff 	map = sc->bge_cdata.bge_tx_dmamap[idx];
3336676ad2c9SGleb Smirnoff 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m, segs,
3337676ad2c9SGleb Smirnoff 	    &nsegs, BUS_DMA_NOWAIT);
33387e27542aSGleb Smirnoff 	if (error == EFBIG) {
3339676ad2c9SGleb Smirnoff 		m = m_defrag(m, M_DONTWAIT);
3340676ad2c9SGleb Smirnoff 		if (m == NULL) {
3341676ad2c9SGleb Smirnoff 			m_freem(*m_head);
3342676ad2c9SGleb Smirnoff 			*m_head = NULL;
33437e27542aSGleb Smirnoff 			return (ENOBUFS);
33447e27542aSGleb Smirnoff 		}
3345676ad2c9SGleb Smirnoff 		*m_head = m;
3346676ad2c9SGleb Smirnoff 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m,
3347676ad2c9SGleb Smirnoff 		    segs, &nsegs, BUS_DMA_NOWAIT);
3348676ad2c9SGleb Smirnoff 		if (error) {
3349676ad2c9SGleb Smirnoff 			m_freem(m);
3350676ad2c9SGleb Smirnoff 			*m_head = NULL;
33517e27542aSGleb Smirnoff 			return (error);
33527e27542aSGleb Smirnoff 		}
3353676ad2c9SGleb Smirnoff 	} else if (error != 0)
3354676ad2c9SGleb Smirnoff 		return (error);
33557e27542aSGleb Smirnoff 
335695d67482SBill Paul 	/*
335795d67482SBill Paul 	 * Sanity check: avoid coming within 16 descriptors
335895d67482SBill Paul 	 * of the end of the ring.
335995d67482SBill Paul 	 */
33607e27542aSGleb Smirnoff 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
33617e27542aSGleb Smirnoff 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
336295d67482SBill Paul 		return (ENOBUFS);
33637e27542aSGleb Smirnoff 	}
33647e27542aSGleb Smirnoff 
3365e65bed95SPyun YongHyeon 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
3366e65bed95SPyun YongHyeon 
33677e27542aSGleb Smirnoff 	for (i = 0; ; i++) {
33687e27542aSGleb Smirnoff 		d = &sc->bge_ldata.bge_tx_ring[idx];
33697e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
33707e27542aSGleb Smirnoff 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
33717e27542aSGleb Smirnoff 		d->bge_len = segs[i].ds_len;
33727e27542aSGleb Smirnoff 		d->bge_flags = csum_flags;
33737e27542aSGleb Smirnoff 		if (i == nsegs - 1)
33747e27542aSGleb Smirnoff 			break;
33757e27542aSGleb Smirnoff 		BGE_INC(idx, BGE_TX_RING_CNT);
33767e27542aSGleb Smirnoff 	}
33777e27542aSGleb Smirnoff 
33787e27542aSGleb Smirnoff 	/* Mark the last segment as end of packet... */
33797e27542aSGleb Smirnoff 	d->bge_flags |= BGE_TXBDFLAG_END;
3380676ad2c9SGleb Smirnoff 
33817e27542aSGleb Smirnoff 	/* ... and put VLAN tag into first segment.  */
33827e27542aSGleb Smirnoff 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
33834e35d186SJung-uk Kim #if __FreeBSD_version > 700022
338478ba57b9SAndre Oppermann 	if (m->m_flags & M_VLANTAG) {
33857e27542aSGleb Smirnoff 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
338678ba57b9SAndre Oppermann 		d->bge_vlan_tag = m->m_pkthdr.ether_vtag;
33877e27542aSGleb Smirnoff 	} else
33887e27542aSGleb Smirnoff 		d->bge_vlan_tag = 0;
33894e35d186SJung-uk Kim #else
33904e35d186SJung-uk Kim 	{
33914e35d186SJung-uk Kim 		struct m_tag		*mtag;
33924e35d186SJung-uk Kim 
33934e35d186SJung-uk Kim 		if ((mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m)) != NULL) {
33944e35d186SJung-uk Kim 			d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
33954e35d186SJung-uk Kim 			d->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
33964e35d186SJung-uk Kim 		} else
33974e35d186SJung-uk Kim 			d->bge_vlan_tag = 0;
33984e35d186SJung-uk Kim 	}
33994e35d186SJung-uk Kim #endif
3400f41ac2beSBill Paul 
3401f41ac2beSBill Paul 	/*
3402f41ac2beSBill Paul 	 * Insure that the map for this transmission
3403f41ac2beSBill Paul 	 * is placed at the array index of the last descriptor
3404f41ac2beSBill Paul 	 * in this chain.
3405f41ac2beSBill Paul 	 */
34067e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
34077e27542aSGleb Smirnoff 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
3408676ad2c9SGleb Smirnoff 	sc->bge_cdata.bge_tx_chain[idx] = m;
34097e27542aSGleb Smirnoff 	sc->bge_txcnt += nsegs;
341095d67482SBill Paul 
34117e27542aSGleb Smirnoff 	BGE_INC(idx, BGE_TX_RING_CNT);
34127e27542aSGleb Smirnoff 	*txidx = idx;
341395d67482SBill Paul 
341495d67482SBill Paul 	return (0);
341595d67482SBill Paul }
341695d67482SBill Paul 
341795d67482SBill Paul /*
341895d67482SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
341995d67482SBill Paul  * to the mbuf data regions directly in the transmit descriptors.
342095d67482SBill Paul  */
342195d67482SBill Paul static void
34223f74909aSGleb Smirnoff bge_start_locked(struct ifnet *ifp)
342395d67482SBill Paul {
342495d67482SBill Paul 	struct bge_softc *sc;
342595d67482SBill Paul 	struct mbuf *m_head = NULL;
342614bbd30fSGleb Smirnoff 	uint32_t prodidx;
3427303a718cSDag-Erling Smørgrav 	int count = 0;
342895d67482SBill Paul 
342995d67482SBill Paul 	sc = ifp->if_softc;
343095d67482SBill Paul 
3431dab5cd05SOleg Bulyzhin 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
343295d67482SBill Paul 		return;
343395d67482SBill Paul 
343414bbd30fSGleb Smirnoff 	prodidx = sc->bge_tx_prodidx;
343595d67482SBill Paul 
343695d67482SBill Paul 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
34374d665c4dSDag-Erling Smørgrav 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
343895d67482SBill Paul 		if (m_head == NULL)
343995d67482SBill Paul 			break;
344095d67482SBill Paul 
344195d67482SBill Paul 		/*
344295d67482SBill Paul 		 * XXX
3443b874fdd4SYaroslav Tykhiy 		 * The code inside the if() block is never reached since we
3444b874fdd4SYaroslav Tykhiy 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3445b874fdd4SYaroslav Tykhiy 		 * requests to checksum TCP/UDP in a fragmented packet.
3446b874fdd4SYaroslav Tykhiy 		 *
3447b874fdd4SYaroslav Tykhiy 		 * XXX
344895d67482SBill Paul 		 * safety overkill.  If this is a fragmented packet chain
344995d67482SBill Paul 		 * with delayed TCP/UDP checksums, then only encapsulate
345095d67482SBill Paul 		 * it if we have enough descriptors to handle the entire
345195d67482SBill Paul 		 * chain at once.
345295d67482SBill Paul 		 * (paranoia -- may not actually be needed)
345395d67482SBill Paul 		 */
345495d67482SBill Paul 		if (m_head->m_flags & M_FIRSTFRAG &&
345595d67482SBill Paul 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
345695d67482SBill Paul 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
345795d67482SBill Paul 			    m_head->m_pkthdr.csum_data + 16) {
34584d665c4dSDag-Erling Smørgrav 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
345913f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
346095d67482SBill Paul 				break;
346195d67482SBill Paul 			}
346295d67482SBill Paul 		}
346395d67482SBill Paul 
346495d67482SBill Paul 		/*
346595d67482SBill Paul 		 * Pack the data into the transmit ring. If we
346695d67482SBill Paul 		 * don't have room, set the OACTIVE flag and wait
346795d67482SBill Paul 		 * for the NIC to drain the ring.
346895d67482SBill Paul 		 */
3469676ad2c9SGleb Smirnoff 		if (bge_encap(sc, &m_head, &prodidx)) {
3470676ad2c9SGleb Smirnoff 			if (m_head == NULL)
3471676ad2c9SGleb Smirnoff 				break;
34724d665c4dSDag-Erling Smørgrav 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
347313f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
347495d67482SBill Paul 			break;
347595d67482SBill Paul 		}
3476303a718cSDag-Erling Smørgrav 		++count;
347795d67482SBill Paul 
347895d67482SBill Paul 		/*
347995d67482SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
348095d67482SBill Paul 		 * to him.
348195d67482SBill Paul 		 */
34824e35d186SJung-uk Kim #ifdef ETHER_BPF_MTAP
348345ee6ab3SJung-uk Kim 		ETHER_BPF_MTAP(ifp, m_head);
34844e35d186SJung-uk Kim #else
34854e35d186SJung-uk Kim 		BPF_MTAP(ifp, m_head);
34864e35d186SJung-uk Kim #endif
348795d67482SBill Paul 	}
348895d67482SBill Paul 
34893f74909aSGleb Smirnoff 	if (count == 0)
34903f74909aSGleb Smirnoff 		/* No packets were dequeued. */
3491303a718cSDag-Erling Smørgrav 		return;
3492303a718cSDag-Erling Smørgrav 
34933f74909aSGleb Smirnoff 	/* Transmit. */
349495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
34953927098fSPaul Saab 	/* 5700 b2 errata */
3496e0ced696SPaul Saab 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
34973927098fSPaul Saab 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
349895d67482SBill Paul 
349914bbd30fSGleb Smirnoff 	sc->bge_tx_prodidx = prodidx;
350014bbd30fSGleb Smirnoff 
350195d67482SBill Paul 	/*
350295d67482SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
350395d67482SBill Paul 	 */
3504b74e67fbSGleb Smirnoff 	sc->bge_timer = 5;
350595d67482SBill Paul }
350695d67482SBill Paul 
35070f9bd73bSSam Leffler /*
35080f9bd73bSSam Leffler  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
35090f9bd73bSSam Leffler  * to the mbuf data regions directly in the transmit descriptors.
35100f9bd73bSSam Leffler  */
351195d67482SBill Paul static void
35123f74909aSGleb Smirnoff bge_start(struct ifnet *ifp)
351395d67482SBill Paul {
35140f9bd73bSSam Leffler 	struct bge_softc *sc;
35150f9bd73bSSam Leffler 
35160f9bd73bSSam Leffler 	sc = ifp->if_softc;
35170f9bd73bSSam Leffler 	BGE_LOCK(sc);
35180f9bd73bSSam Leffler 	bge_start_locked(ifp);
35190f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
35200f9bd73bSSam Leffler }
35210f9bd73bSSam Leffler 
35220f9bd73bSSam Leffler static void
35233f74909aSGleb Smirnoff bge_init_locked(struct bge_softc *sc)
35240f9bd73bSSam Leffler {
352595d67482SBill Paul 	struct ifnet *ifp;
35263f74909aSGleb Smirnoff 	uint16_t *m;
352795d67482SBill Paul 
35280f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
352995d67482SBill Paul 
3530fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
353195d67482SBill Paul 
353213f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
353395d67482SBill Paul 		return;
353495d67482SBill Paul 
353595d67482SBill Paul 	/* Cancel pending I/O and flush buffers. */
353695d67482SBill Paul 	bge_stop(sc);
35378cb1383cSDoug Ambrisko 
35388cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
35398cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_START);
354095d67482SBill Paul 	bge_reset(sc);
35418cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_START);
35428cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_START);
35438cb1383cSDoug Ambrisko 
354495d67482SBill Paul 	bge_chipinit(sc);
354595d67482SBill Paul 
354695d67482SBill Paul 	/*
354795d67482SBill Paul 	 * Init the various state machines, ring
354895d67482SBill Paul 	 * control blocks and firmware.
354995d67482SBill Paul 	 */
355095d67482SBill Paul 	if (bge_blockinit(sc)) {
3551fe806fdaSPyun YongHyeon 		device_printf(sc->bge_dev, "initialization failure\n");
355295d67482SBill Paul 		return;
355395d67482SBill Paul 	}
355495d67482SBill Paul 
3555fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
355695d67482SBill Paul 
355795d67482SBill Paul 	/* Specify MTU. */
355895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3559859c6c7dSBill Paul 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
356095d67482SBill Paul 
356195d67482SBill Paul 	/* Load our MAC address. */
35623f74909aSGleb Smirnoff 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
356395d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
356495d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
356595d67482SBill Paul 
35663e9b1bcaSJung-uk Kim 	/* Program promiscuous mode. */
35673e9b1bcaSJung-uk Kim 	bge_setpromisc(sc);
356895d67482SBill Paul 
356995d67482SBill Paul 	/* Program multicast filter. */
357095d67482SBill Paul 	bge_setmulti(sc);
357195d67482SBill Paul 
357295d67482SBill Paul 	/* Init RX ring. */
357395d67482SBill Paul 	bge_init_rx_ring_std(sc);
357495d67482SBill Paul 
35750434d1b8SBill Paul 	/*
35760434d1b8SBill Paul 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
35770434d1b8SBill Paul 	 * memory to insure that the chip has in fact read the first
35780434d1b8SBill Paul 	 * entry of the ring.
35790434d1b8SBill Paul 	 */
35800434d1b8SBill Paul 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
35813f74909aSGleb Smirnoff 		uint32_t		v, i;
35820434d1b8SBill Paul 		for (i = 0; i < 10; i++) {
35830434d1b8SBill Paul 			DELAY(20);
35840434d1b8SBill Paul 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
35850434d1b8SBill Paul 			if (v == (MCLBYTES - ETHER_ALIGN))
35860434d1b8SBill Paul 				break;
35870434d1b8SBill Paul 		}
35880434d1b8SBill Paul 		if (i == 10)
3589fe806fdaSPyun YongHyeon 			device_printf (sc->bge_dev,
3590fe806fdaSPyun YongHyeon 			    "5705 A0 chip failed to load RX ring\n");
35910434d1b8SBill Paul 	}
35920434d1b8SBill Paul 
359395d67482SBill Paul 	/* Init jumbo RX ring. */
359495d67482SBill Paul 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
359595d67482SBill Paul 		bge_init_rx_ring_jumbo(sc);
359695d67482SBill Paul 
35973f74909aSGleb Smirnoff 	/* Init our RX return ring index. */
359895d67482SBill Paul 	sc->bge_rx_saved_considx = 0;
359995d67482SBill Paul 
36007e6e2507SJung-uk Kim 	/* Init our RX/TX stat counters. */
36017e6e2507SJung-uk Kim 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
36027e6e2507SJung-uk Kim 
360395d67482SBill Paul 	/* Init TX ring. */
360495d67482SBill Paul 	bge_init_tx_ring(sc);
360595d67482SBill Paul 
36063f74909aSGleb Smirnoff 	/* Turn on transmitter. */
360795d67482SBill Paul 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
360895d67482SBill Paul 
36093f74909aSGleb Smirnoff 	/* Turn on receiver. */
361095d67482SBill Paul 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
361195d67482SBill Paul 
361295d67482SBill Paul 	/* Tell firmware we're alive. */
361395d67482SBill Paul 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
361495d67482SBill Paul 
361575719184SGleb Smirnoff #ifdef DEVICE_POLLING
361675719184SGleb Smirnoff 	/* Disable interrupts if we are polling. */
361775719184SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
361875719184SGleb Smirnoff 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
361975719184SGleb Smirnoff 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
362075719184SGleb Smirnoff 		CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
362175719184SGleb Smirnoff 	} else
362275719184SGleb Smirnoff #endif
362375719184SGleb Smirnoff 
362495d67482SBill Paul 	/* Enable host interrupts. */
362575719184SGleb Smirnoff 	{
362695d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
362795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
362895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
362975719184SGleb Smirnoff 	}
363095d67482SBill Paul 
363167d5e043SOleg Bulyzhin 	bge_ifmedia_upd_locked(ifp);
363295d67482SBill Paul 
363313f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
363413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
363595d67482SBill Paul 
36360f9bd73bSSam Leffler 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
36370f9bd73bSSam Leffler }
36380f9bd73bSSam Leffler 
36390f9bd73bSSam Leffler static void
36403f74909aSGleb Smirnoff bge_init(void *xsc)
36410f9bd73bSSam Leffler {
36420f9bd73bSSam Leffler 	struct bge_softc *sc = xsc;
36430f9bd73bSSam Leffler 
36440f9bd73bSSam Leffler 	BGE_LOCK(sc);
36450f9bd73bSSam Leffler 	bge_init_locked(sc);
36460f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
364795d67482SBill Paul }
364895d67482SBill Paul 
364995d67482SBill Paul /*
365095d67482SBill Paul  * Set media options.
365195d67482SBill Paul  */
365295d67482SBill Paul static int
36533f74909aSGleb Smirnoff bge_ifmedia_upd(struct ifnet *ifp)
365495d67482SBill Paul {
365567d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
365667d5e043SOleg Bulyzhin 	int res;
365767d5e043SOleg Bulyzhin 
365867d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
365967d5e043SOleg Bulyzhin 	res = bge_ifmedia_upd_locked(ifp);
366067d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
366167d5e043SOleg Bulyzhin 
366267d5e043SOleg Bulyzhin 	return (res);
366367d5e043SOleg Bulyzhin }
366467d5e043SOleg Bulyzhin 
366567d5e043SOleg Bulyzhin static int
366667d5e043SOleg Bulyzhin bge_ifmedia_upd_locked(struct ifnet *ifp)
366767d5e043SOleg Bulyzhin {
366867d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
366995d67482SBill Paul 	struct mii_data *mii;
367095d67482SBill Paul 	struct ifmedia *ifm;
367195d67482SBill Paul 
367267d5e043SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
367367d5e043SOleg Bulyzhin 
367495d67482SBill Paul 	ifm = &sc->bge_ifmedia;
367595d67482SBill Paul 
367695d67482SBill Paul 	/* If this is a 1000baseX NIC, enable the TBI port. */
3677652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
367895d67482SBill Paul 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
367995d67482SBill Paul 			return (EINVAL);
368095d67482SBill Paul 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
368195d67482SBill Paul 		case IFM_AUTO:
3682ff50922bSDoug White 			/*
3683ff50922bSDoug White 			 * The BCM5704 ASIC appears to have a special
3684ff50922bSDoug White 			 * mechanism for programming the autoneg
3685ff50922bSDoug White 			 * advertisement registers in TBI mode.
3686ff50922bSDoug White 			 */
3687c4529f41SMichael Reifenberger 			if (bge_fake_autoneg == 0 &&
3688c4529f41SMichael Reifenberger 			    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3689ff50922bSDoug White 				uint32_t sgdig;
3690ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3691ff50922bSDoug White 				sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3692ff50922bSDoug White 				sgdig |= BGE_SGDIGCFG_AUTO |
3693ff50922bSDoug White 				    BGE_SGDIGCFG_PAUSE_CAP |
3694ff50922bSDoug White 				    BGE_SGDIGCFG_ASYM_PAUSE;
3695ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3696ff50922bSDoug White 				    sgdig | BGE_SGDIGCFG_SEND);
3697ff50922bSDoug White 				DELAY(5);
3698ff50922bSDoug White 				CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3699ff50922bSDoug White 			}
370095d67482SBill Paul 			break;
370195d67482SBill Paul 		case IFM_1000_SX:
370295d67482SBill Paul 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
370395d67482SBill Paul 				BGE_CLRBIT(sc, BGE_MAC_MODE,
370495d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
370595d67482SBill Paul 			} else {
370695d67482SBill Paul 				BGE_SETBIT(sc, BGE_MAC_MODE,
370795d67482SBill Paul 				    BGE_MACMODE_HALF_DUPLEX);
370895d67482SBill Paul 			}
370995d67482SBill Paul 			break;
371095d67482SBill Paul 		default:
371195d67482SBill Paul 			return (EINVAL);
371295d67482SBill Paul 		}
371395d67482SBill Paul 		return (0);
371495d67482SBill Paul 	}
371595d67482SBill Paul 
37161493e883SOleg Bulyzhin 	sc->bge_link_evt++;
371795d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
371895d67482SBill Paul 	if (mii->mii_instance) {
371995d67482SBill Paul 		struct mii_softc *miisc;
372095d67482SBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
372195d67482SBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
372295d67482SBill Paul 			mii_phy_reset(miisc);
372395d67482SBill Paul 	}
372495d67482SBill Paul 	mii_mediachg(mii);
372595d67482SBill Paul 
372695d67482SBill Paul 	return (0);
372795d67482SBill Paul }
372895d67482SBill Paul 
372995d67482SBill Paul /*
373095d67482SBill Paul  * Report current media status.
373195d67482SBill Paul  */
373295d67482SBill Paul static void
37333f74909aSGleb Smirnoff bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
373495d67482SBill Paul {
373567d5e043SOleg Bulyzhin 	struct bge_softc *sc = ifp->if_softc;
373695d67482SBill Paul 	struct mii_data *mii;
373795d67482SBill Paul 
373867d5e043SOleg Bulyzhin 	BGE_LOCK(sc);
373995d67482SBill Paul 
3740652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
374195d67482SBill Paul 		ifmr->ifm_status = IFM_AVALID;
374295d67482SBill Paul 		ifmr->ifm_active = IFM_ETHER;
374395d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_STS) &
374495d67482SBill Paul 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
374595d67482SBill Paul 			ifmr->ifm_status |= IFM_ACTIVE;
37464c0da0ffSGleb Smirnoff 		else {
37474c0da0ffSGleb Smirnoff 			ifmr->ifm_active |= IFM_NONE;
374867d5e043SOleg Bulyzhin 			BGE_UNLOCK(sc);
37494c0da0ffSGleb Smirnoff 			return;
37504c0da0ffSGleb Smirnoff 		}
375195d67482SBill Paul 		ifmr->ifm_active |= IFM_1000_SX;
375295d67482SBill Paul 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
375395d67482SBill Paul 			ifmr->ifm_active |= IFM_HDX;
375495d67482SBill Paul 		else
375595d67482SBill Paul 			ifmr->ifm_active |= IFM_FDX;
375667d5e043SOleg Bulyzhin 		BGE_UNLOCK(sc);
375795d67482SBill Paul 		return;
375895d67482SBill Paul 	}
375995d67482SBill Paul 
376095d67482SBill Paul 	mii = device_get_softc(sc->bge_miibus);
376195d67482SBill Paul 	mii_pollstat(mii);
376295d67482SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
376395d67482SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
376467d5e043SOleg Bulyzhin 
376567d5e043SOleg Bulyzhin 	BGE_UNLOCK(sc);
376695d67482SBill Paul }
376795d67482SBill Paul 
376895d67482SBill Paul static int
37693f74909aSGleb Smirnoff bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
377095d67482SBill Paul {
377195d67482SBill Paul 	struct bge_softc *sc = ifp->if_softc;
377295d67482SBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
377395d67482SBill Paul 	struct mii_data *mii;
3774f9004b6dSJung-uk Kim 	int flags, mask, error = 0;
377595d67482SBill Paul 
377695d67482SBill Paul 	switch (command) {
377795d67482SBill Paul 	case SIOCSIFMTU:
37784c0da0ffSGleb Smirnoff 		if (ifr->ifr_mtu < ETHERMIN ||
37794c0da0ffSGleb Smirnoff 		    ((BGE_IS_JUMBO_CAPABLE(sc)) &&
37804c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > BGE_JUMBO_MTU) ||
37814c0da0ffSGleb Smirnoff 		    ((!BGE_IS_JUMBO_CAPABLE(sc)) &&
37824c0da0ffSGleb Smirnoff 		    ifr->ifr_mtu > ETHERMTU))
378395d67482SBill Paul 			error = EINVAL;
37844c0da0ffSGleb Smirnoff 		else if (ifp->if_mtu != ifr->ifr_mtu) {
378595d67482SBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
378613f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
378795d67482SBill Paul 			bge_init(sc);
378895d67482SBill Paul 		}
378995d67482SBill Paul 		break;
379095d67482SBill Paul 	case SIOCSIFFLAGS:
37910f9bd73bSSam Leffler 		BGE_LOCK(sc);
379295d67482SBill Paul 		if (ifp->if_flags & IFF_UP) {
379395d67482SBill Paul 			/*
379495d67482SBill Paul 			 * If only the state of the PROMISC flag changed,
379595d67482SBill Paul 			 * then just use the 'set promisc mode' command
379695d67482SBill Paul 			 * instead of reinitializing the entire NIC. Doing
379795d67482SBill Paul 			 * a full re-init means reloading the firmware and
379895d67482SBill Paul 			 * waiting for it to start up, which may take a
3799d183af7fSRuslan Ermilov 			 * second or two.  Similarly for ALLMULTI.
380095d67482SBill Paul 			 */
3801f9004b6dSJung-uk Kim 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3802f9004b6dSJung-uk Kim 				flags = ifp->if_flags ^ sc->bge_if_flags;
38033e9b1bcaSJung-uk Kim 				if (flags & IFF_PROMISC)
38043e9b1bcaSJung-uk Kim 					bge_setpromisc(sc);
3805f9004b6dSJung-uk Kim 				if (flags & IFF_ALLMULTI)
3806d183af7fSRuslan Ermilov 					bge_setmulti(sc);
380795d67482SBill Paul 			} else
38080f9bd73bSSam Leffler 				bge_init_locked(sc);
380995d67482SBill Paul 		} else {
381013f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
381195d67482SBill Paul 				bge_stop(sc);
381295d67482SBill Paul 			}
381395d67482SBill Paul 		}
381495d67482SBill Paul 		sc->bge_if_flags = ifp->if_flags;
38150f9bd73bSSam Leffler 		BGE_UNLOCK(sc);
381695d67482SBill Paul 		error = 0;
381795d67482SBill Paul 		break;
381895d67482SBill Paul 	case SIOCADDMULTI:
381995d67482SBill Paul 	case SIOCDELMULTI:
382013f4c340SRobert Watson 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
38210f9bd73bSSam Leffler 			BGE_LOCK(sc);
382295d67482SBill Paul 			bge_setmulti(sc);
38230f9bd73bSSam Leffler 			BGE_UNLOCK(sc);
382495d67482SBill Paul 			error = 0;
382595d67482SBill Paul 		}
382695d67482SBill Paul 		break;
382795d67482SBill Paul 	case SIOCSIFMEDIA:
382895d67482SBill Paul 	case SIOCGIFMEDIA:
3829652ae483SGleb Smirnoff 		if (sc->bge_flags & BGE_FLAG_TBI) {
383095d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
383195d67482SBill Paul 			    &sc->bge_ifmedia, command);
383295d67482SBill Paul 		} else {
383395d67482SBill Paul 			mii = device_get_softc(sc->bge_miibus);
383495d67482SBill Paul 			error = ifmedia_ioctl(ifp, ifr,
383595d67482SBill Paul 			    &mii->mii_media, command);
383695d67482SBill Paul 		}
383795d67482SBill Paul 		break;
383895d67482SBill Paul 	case SIOCSIFCAP:
383995d67482SBill Paul 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
384075719184SGleb Smirnoff #ifdef DEVICE_POLLING
384175719184SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
384275719184SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
384375719184SGleb Smirnoff 				error = ether_poll_register(bge_poll, ifp);
384475719184SGleb Smirnoff 				if (error)
384575719184SGleb Smirnoff 					return (error);
384675719184SGleb Smirnoff 				BGE_LOCK(sc);
384775719184SGleb Smirnoff 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
384875719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
384975719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
385075719184SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
385175719184SGleb Smirnoff 				BGE_UNLOCK(sc);
385275719184SGleb Smirnoff 			} else {
385375719184SGleb Smirnoff 				error = ether_poll_deregister(ifp);
385475719184SGleb Smirnoff 				/* Enable interrupt even in error case */
385575719184SGleb Smirnoff 				BGE_LOCK(sc);
385675719184SGleb Smirnoff 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
385775719184SGleb Smirnoff 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
385875719184SGleb Smirnoff 				CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
385975719184SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
386075719184SGleb Smirnoff 				BGE_UNLOCK(sc);
386175719184SGleb Smirnoff 			}
386275719184SGleb Smirnoff 		}
386375719184SGleb Smirnoff #endif
3864d375e524SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
3865d375e524SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
3866d375e524SGleb Smirnoff 			if (IFCAP_HWCSUM & ifp->if_capenable &&
3867d375e524SGleb Smirnoff 			    IFCAP_HWCSUM & ifp->if_capabilities)
3868b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = BGE_CSUM_FEATURES;
386995d67482SBill Paul 			else
3870b874fdd4SYaroslav Tykhiy 				ifp->if_hwassist = 0;
38714e35d186SJung-uk Kim #ifdef VLAN_CAPABILITIES
3872479b23b7SGleb Smirnoff 			VLAN_CAPABILITIES(ifp);
38734e35d186SJung-uk Kim #endif
387495d67482SBill Paul 		}
387595d67482SBill Paul 		break;
387695d67482SBill Paul 	default:
3877673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
387895d67482SBill Paul 		break;
387995d67482SBill Paul 	}
388095d67482SBill Paul 
388195d67482SBill Paul 	return (error);
388295d67482SBill Paul }
388395d67482SBill Paul 
388495d67482SBill Paul static void
3885b74e67fbSGleb Smirnoff bge_watchdog(struct bge_softc *sc)
388695d67482SBill Paul {
3887b74e67fbSGleb Smirnoff 	struct ifnet *ifp;
388895d67482SBill Paul 
3889b74e67fbSGleb Smirnoff 	BGE_LOCK_ASSERT(sc);
3890b74e67fbSGleb Smirnoff 
3891b74e67fbSGleb Smirnoff 	if (sc->bge_timer == 0 || --sc->bge_timer)
3892b74e67fbSGleb Smirnoff 		return;
3893b74e67fbSGleb Smirnoff 
3894b74e67fbSGleb Smirnoff 	ifp = sc->bge_ifp;
389595d67482SBill Paul 
3896fe806fdaSPyun YongHyeon 	if_printf(ifp, "watchdog timeout -- resetting\n");
389795d67482SBill Paul 
389813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3899426742bfSGleb Smirnoff 	bge_init_locked(sc);
390095d67482SBill Paul 
390195d67482SBill Paul 	ifp->if_oerrors++;
390295d67482SBill Paul }
390395d67482SBill Paul 
390495d67482SBill Paul /*
390595d67482SBill Paul  * Stop the adapter and free any mbufs allocated to the
390695d67482SBill Paul  * RX and TX lists.
390795d67482SBill Paul  */
390895d67482SBill Paul static void
39093f74909aSGleb Smirnoff bge_stop(struct bge_softc *sc)
391095d67482SBill Paul {
391195d67482SBill Paul 	struct ifnet *ifp;
391295d67482SBill Paul 	struct ifmedia_entry *ifm;
391395d67482SBill Paul 	struct mii_data *mii = NULL;
391495d67482SBill Paul 	int mtmp, itmp;
391595d67482SBill Paul 
39160f9bd73bSSam Leffler 	BGE_LOCK_ASSERT(sc);
39170f9bd73bSSam Leffler 
3918fc74a9f9SBrooks Davis 	ifp = sc->bge_ifp;
391995d67482SBill Paul 
3920652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0)
392195d67482SBill Paul 		mii = device_get_softc(sc->bge_miibus);
392295d67482SBill Paul 
39230f9bd73bSSam Leffler 	callout_stop(&sc->bge_stat_ch);
392495d67482SBill Paul 
392595d67482SBill Paul 	/*
39263f74909aSGleb Smirnoff 	 * Disable all of the receiver blocks.
392795d67482SBill Paul 	 */
392895d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
392995d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
393095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
39317ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
393295d67482SBill Paul 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
393395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
393495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
393595d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
393695d67482SBill Paul 
393795d67482SBill Paul 	/*
39383f74909aSGleb Smirnoff 	 * Disable all of the transmit blocks.
393995d67482SBill Paul 	 */
394095d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
394195d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
394295d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
394395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
394495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
39457ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
394695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
394795d67482SBill Paul 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
394895d67482SBill Paul 
394995d67482SBill Paul 	/*
395095d67482SBill Paul 	 * Shut down all of the memory managers and related
395195d67482SBill Paul 	 * state machines.
395295d67482SBill Paul 	 */
395395d67482SBill Paul 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
395495d67482SBill Paul 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
39557ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc)))
395695d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
39576098821cSJung-uk Kim 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xffffffff);
395895d67482SBill Paul 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
39597ee00338SJung-uk Kim 	if (!(BGE_IS_5705_PLUS(sc))) {
396095d67482SBill Paul 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
396195d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
39620434d1b8SBill Paul 	}
396395d67482SBill Paul 
396495d67482SBill Paul 	/* Disable host interrupts. */
396595d67482SBill Paul 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
396695d67482SBill Paul 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
396795d67482SBill Paul 
396895d67482SBill Paul 	/*
396995d67482SBill Paul 	 * Tell firmware we're shutting down.
397095d67482SBill Paul 	 */
39718cb1383cSDoug Ambrisko 
39728cb1383cSDoug Ambrisko 	bge_stop_fw(sc);
39738cb1383cSDoug Ambrisko 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
39748cb1383cSDoug Ambrisko 	bge_reset(sc);
39758cb1383cSDoug Ambrisko 	bge_sig_legacy(sc, BGE_RESET_STOP);
39768cb1383cSDoug Ambrisko 	bge_sig_post_reset(sc, BGE_RESET_STOP);
39778cb1383cSDoug Ambrisko 
39788cb1383cSDoug Ambrisko 	/*
39798cb1383cSDoug Ambrisko 	 * Keep the ASF firmware running if up.
39808cb1383cSDoug Ambrisko 	 */
39818cb1383cSDoug Ambrisko 	if (sc->bge_asf_mode & ASF_STACKUP)
39828cb1383cSDoug Ambrisko 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
39838cb1383cSDoug Ambrisko 	else
398495d67482SBill Paul 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
398595d67482SBill Paul 
398695d67482SBill Paul 	/* Free the RX lists. */
398795d67482SBill Paul 	bge_free_rx_ring_std(sc);
398895d67482SBill Paul 
398995d67482SBill Paul 	/* Free jumbo RX list. */
39904c0da0ffSGleb Smirnoff 	if (BGE_IS_JUMBO_CAPABLE(sc))
399195d67482SBill Paul 		bge_free_rx_ring_jumbo(sc);
399295d67482SBill Paul 
399395d67482SBill Paul 	/* Free TX buffers. */
399495d67482SBill Paul 	bge_free_tx_ring(sc);
399595d67482SBill Paul 
399695d67482SBill Paul 	/*
399795d67482SBill Paul 	 * Isolate/power down the PHY, but leave the media selection
399895d67482SBill Paul 	 * unchanged so that things will be put back to normal when
399995d67482SBill Paul 	 * we bring the interface back up.
400095d67482SBill Paul 	 */
4001652ae483SGleb Smirnoff 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
400295d67482SBill Paul 		itmp = ifp->if_flags;
400395d67482SBill Paul 		ifp->if_flags |= IFF_UP;
4004dcc34049SPawel Jakub Dawidek 		/*
4005dcc34049SPawel Jakub Dawidek 		 * If we are called from bge_detach(), mii is already NULL.
4006dcc34049SPawel Jakub Dawidek 		 */
4007dcc34049SPawel Jakub Dawidek 		if (mii != NULL) {
400895d67482SBill Paul 			ifm = mii->mii_media.ifm_cur;
400995d67482SBill Paul 			mtmp = ifm->ifm_media;
401095d67482SBill Paul 			ifm->ifm_media = IFM_ETHER | IFM_NONE;
401195d67482SBill Paul 			mii_mediachg(mii);
401295d67482SBill Paul 			ifm->ifm_media = mtmp;
4013dcc34049SPawel Jakub Dawidek 		}
401495d67482SBill Paul 		ifp->if_flags = itmp;
401595d67482SBill Paul 	}
401695d67482SBill Paul 
401795d67482SBill Paul 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
401895d67482SBill Paul 
40195dda8085SOleg Bulyzhin 	/* Clear MAC's link state (PHY may still have link UP). */
40201493e883SOleg Bulyzhin 	if (bootverbose && sc->bge_link)
40211493e883SOleg Bulyzhin 		if_printf(sc->bge_ifp, "link DOWN\n");
40221493e883SOleg Bulyzhin 	sc->bge_link = 0;
402395d67482SBill Paul 
40241493e883SOleg Bulyzhin 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
402595d67482SBill Paul }
402695d67482SBill Paul 
402795d67482SBill Paul /*
402895d67482SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
402995d67482SBill Paul  * get confused by errant DMAs when rebooting.
403095d67482SBill Paul  */
403195d67482SBill Paul static void
40323f74909aSGleb Smirnoff bge_shutdown(device_t dev)
403395d67482SBill Paul {
403495d67482SBill Paul 	struct bge_softc *sc;
403595d67482SBill Paul 
403695d67482SBill Paul 	sc = device_get_softc(dev);
403795d67482SBill Paul 
40380f9bd73bSSam Leffler 	BGE_LOCK(sc);
403995d67482SBill Paul 	bge_stop(sc);
404095d67482SBill Paul 	bge_reset(sc);
40410f9bd73bSSam Leffler 	BGE_UNLOCK(sc);
404295d67482SBill Paul }
404314afefa3SPawel Jakub Dawidek 
404414afefa3SPawel Jakub Dawidek static int
404514afefa3SPawel Jakub Dawidek bge_suspend(device_t dev)
404614afefa3SPawel Jakub Dawidek {
404714afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
404814afefa3SPawel Jakub Dawidek 
404914afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
405014afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
405114afefa3SPawel Jakub Dawidek 	bge_stop(sc);
405214afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
405314afefa3SPawel Jakub Dawidek 
405414afefa3SPawel Jakub Dawidek 	return (0);
405514afefa3SPawel Jakub Dawidek }
405614afefa3SPawel Jakub Dawidek 
405714afefa3SPawel Jakub Dawidek static int
405814afefa3SPawel Jakub Dawidek bge_resume(device_t dev)
405914afefa3SPawel Jakub Dawidek {
406014afefa3SPawel Jakub Dawidek 	struct bge_softc *sc;
406114afefa3SPawel Jakub Dawidek 	struct ifnet *ifp;
406214afefa3SPawel Jakub Dawidek 
406314afefa3SPawel Jakub Dawidek 	sc = device_get_softc(dev);
406414afefa3SPawel Jakub Dawidek 	BGE_LOCK(sc);
406514afefa3SPawel Jakub Dawidek 	ifp = sc->bge_ifp;
406614afefa3SPawel Jakub Dawidek 	if (ifp->if_flags & IFF_UP) {
406714afefa3SPawel Jakub Dawidek 		bge_init_locked(sc);
406814afefa3SPawel Jakub Dawidek 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
406914afefa3SPawel Jakub Dawidek 			bge_start_locked(ifp);
407014afefa3SPawel Jakub Dawidek 	}
407114afefa3SPawel Jakub Dawidek 	BGE_UNLOCK(sc);
407214afefa3SPawel Jakub Dawidek 
407314afefa3SPawel Jakub Dawidek 	return (0);
407414afefa3SPawel Jakub Dawidek }
4075dab5cd05SOleg Bulyzhin 
4076dab5cd05SOleg Bulyzhin static void
40773f74909aSGleb Smirnoff bge_link_upd(struct bge_softc *sc)
4078dab5cd05SOleg Bulyzhin {
40791f313773SOleg Bulyzhin 	struct mii_data *mii;
40801f313773SOleg Bulyzhin 	uint32_t link, status;
4081dab5cd05SOleg Bulyzhin 
4082dab5cd05SOleg Bulyzhin 	BGE_LOCK_ASSERT(sc);
40831f313773SOleg Bulyzhin 
40843f74909aSGleb Smirnoff 	/* Clear 'pending link event' flag. */
40857b97099dSOleg Bulyzhin 	sc->bge_link_evt = 0;
40867b97099dSOleg Bulyzhin 
4087dab5cd05SOleg Bulyzhin 	/*
4088dab5cd05SOleg Bulyzhin 	 * Process link state changes.
4089dab5cd05SOleg Bulyzhin 	 * Grrr. The link status word in the status block does
4090dab5cd05SOleg Bulyzhin 	 * not work correctly on the BCM5700 rev AX and BX chips,
4091dab5cd05SOleg Bulyzhin 	 * according to all available information. Hence, we have
4092dab5cd05SOleg Bulyzhin 	 * to enable MII interrupts in order to properly obtain
4093dab5cd05SOleg Bulyzhin 	 * async link changes. Unfortunately, this also means that
4094dab5cd05SOleg Bulyzhin 	 * we have to read the MAC status register to detect link
4095dab5cd05SOleg Bulyzhin 	 * changes, thereby adding an additional register access to
4096dab5cd05SOleg Bulyzhin 	 * the interrupt handler.
40971f313773SOleg Bulyzhin 	 *
40981f313773SOleg Bulyzhin 	 * XXX: perhaps link state detection procedure used for
40994c0da0ffSGleb Smirnoff 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
4100dab5cd05SOleg Bulyzhin 	 */
4101dab5cd05SOleg Bulyzhin 
41021f313773SOleg Bulyzhin 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
41034c0da0ffSGleb Smirnoff 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
4104dab5cd05SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
4105dab5cd05SOleg Bulyzhin 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
41061f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
41075dda8085SOleg Bulyzhin 			mii_pollstat(mii);
41081f313773SOleg Bulyzhin 			if (!sc->bge_link &&
41091f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
41101f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
41111f313773SOleg Bulyzhin 				sc->bge_link++;
41121f313773SOleg Bulyzhin 				if (bootverbose)
41131f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
41141f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
41151f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
41161f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
41171f313773SOleg Bulyzhin 				sc->bge_link = 0;
41181f313773SOleg Bulyzhin 				if (bootverbose)
41191f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
41201f313773SOleg Bulyzhin 			}
41211f313773SOleg Bulyzhin 
41223f74909aSGleb Smirnoff 			/* Clear the interrupt. */
4123dab5cd05SOleg Bulyzhin 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
4124dab5cd05SOleg Bulyzhin 			    BGE_EVTENB_MI_INTERRUPT);
4125dab5cd05SOleg Bulyzhin 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
4126dab5cd05SOleg Bulyzhin 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
4127dab5cd05SOleg Bulyzhin 			    BRGPHY_INTRS);
4128dab5cd05SOleg Bulyzhin 		}
4129dab5cd05SOleg Bulyzhin 		return;
4130dab5cd05SOleg Bulyzhin 	}
4131dab5cd05SOleg Bulyzhin 
4132652ae483SGleb Smirnoff 	if (sc->bge_flags & BGE_FLAG_TBI) {
41331f313773SOleg Bulyzhin 		status = CSR_READ_4(sc, BGE_MAC_STS);
41347b97099dSOleg Bulyzhin 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
41357b97099dSOleg Bulyzhin 			if (!sc->bge_link) {
41361f313773SOleg Bulyzhin 				sc->bge_link++;
41371f313773SOleg Bulyzhin 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
41381f313773SOleg Bulyzhin 					BGE_CLRBIT(sc, BGE_MAC_MODE,
41391f313773SOleg Bulyzhin 					    BGE_MACMODE_TBI_SEND_CFGS);
41406098821cSJung-uk Kim 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xffffffff);
41411f313773SOleg Bulyzhin 				if (bootverbose)
41421f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
41433f74909aSGleb Smirnoff 				if_link_state_change(sc->bge_ifp,
41443f74909aSGleb Smirnoff 				    LINK_STATE_UP);
41457b97099dSOleg Bulyzhin 			}
41461f313773SOleg Bulyzhin 		} else if (sc->bge_link) {
4147dab5cd05SOleg Bulyzhin 			sc->bge_link = 0;
41481f313773SOleg Bulyzhin 			if (bootverbose)
41491f313773SOleg Bulyzhin 				if_printf(sc->bge_ifp, "link DOWN\n");
41507b97099dSOleg Bulyzhin 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
41511f313773SOleg Bulyzhin 		}
41526098821cSJung-uk Kim 	/*
41536098821cSJung-uk Kim 	 * Discard link events for MII/GMII cards
41546098821cSJung-uk Kim 	 * if MI auto-polling is disabled.
41556098821cSJung-uk Kim 	 */
41561493e883SOleg Bulyzhin 	} else if (CSR_READ_4(sc, BGE_MI_MODE) & BGE_MIMODE_AUTOPOLL) {
41571f313773SOleg Bulyzhin 		/*
41586098821cSJung-uk Kim 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED
41596098821cSJung-uk Kim 		 * bit in status word always set. Workaround this bug by
41606098821cSJung-uk Kim 		 * reading PHY link status directly.
41611f313773SOleg Bulyzhin 		 */
41621f313773SOleg Bulyzhin 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
41631f313773SOleg Bulyzhin 
41641f313773SOleg Bulyzhin 		if (link != sc->bge_link ||
41651f313773SOleg Bulyzhin 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
41661f313773SOleg Bulyzhin 			mii = device_get_softc(sc->bge_miibus);
41675dda8085SOleg Bulyzhin 			mii_pollstat(mii);
41681f313773SOleg Bulyzhin 			if (!sc->bge_link &&
41691f313773SOleg Bulyzhin 			    mii->mii_media_status & IFM_ACTIVE &&
41701f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
41711f313773SOleg Bulyzhin 				sc->bge_link++;
41721f313773SOleg Bulyzhin 				if (bootverbose)
41731f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link UP\n");
41741f313773SOleg Bulyzhin 			} else if (sc->bge_link &&
41751f313773SOleg Bulyzhin 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
41761f313773SOleg Bulyzhin 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
41771f313773SOleg Bulyzhin 				sc->bge_link = 0;
41781f313773SOleg Bulyzhin 				if (bootverbose)
41791f313773SOleg Bulyzhin 					if_printf(sc->bge_ifp, "link DOWN\n");
41801f313773SOleg Bulyzhin 			}
41811f313773SOleg Bulyzhin 		}
4182dab5cd05SOleg Bulyzhin 	}
4183dab5cd05SOleg Bulyzhin 
41843f74909aSGleb Smirnoff 	/* Clear the attention. */
4185dab5cd05SOleg Bulyzhin 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
4186dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
4187dab5cd05SOleg Bulyzhin 	    BGE_MACSTAT_LINK_CHANGED);
4188dab5cd05SOleg Bulyzhin }
41896f8718a3SScott Long 
41906f8718a3SScott Long static void
41916f8718a3SScott Long bge_add_sysctls(struct bge_softc *sc)
41926f8718a3SScott Long {
41936f8718a3SScott Long 	struct sysctl_ctx_list *ctx;
41946f8718a3SScott Long 	struct sysctl_oid_list *children;
41956f8718a3SScott Long 
41966f8718a3SScott Long 	ctx = device_get_sysctl_ctx(sc->bge_dev);
41976f8718a3SScott Long 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
41986f8718a3SScott Long 
41996f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
42006f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
42016f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
42026f8718a3SScott Long 	    "Debug Information");
42036f8718a3SScott Long 
42046f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
42056f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
42066f8718a3SScott Long 	    "Register Read");
42076f8718a3SScott Long 
42086f8718a3SScott Long 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
42096f8718a3SScott Long 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
42106f8718a3SScott Long 	    "Memory Read");
42116f8718a3SScott Long 
42126f8718a3SScott Long 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "stat_IfHcInOctets",
42136f8718a3SScott Long 	    CTLFLAG_RD,
42146f8718a3SScott Long 	    &sc->bge_ldata.bge_stats->rxstats.ifHCInOctets.bge_addr_lo,
42156f8718a3SScott Long 	    "Bytes received");
42166f8718a3SScott Long 
42176f8718a3SScott Long 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, "stat_IfHcOutOctets",
42186f8718a3SScott Long 	    CTLFLAG_RD,
42196f8718a3SScott Long 	    &sc->bge_ldata.bge_stats->txstats.ifHCOutOctets.bge_addr_lo,
42206f8718a3SScott Long 	    "Bytes received");
42216f8718a3SScott Long #endif
42226f8718a3SScott Long }
42236f8718a3SScott Long 
42246f8718a3SScott Long #ifdef BGE_REGISTER_DEBUG
42256f8718a3SScott Long static int
42266f8718a3SScott Long bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
42276f8718a3SScott Long {
42286f8718a3SScott Long 	struct bge_softc *sc;
42296f8718a3SScott Long 	uint16_t *sbdata;
42306f8718a3SScott Long 	int error;
42316f8718a3SScott Long 	int result;
42326f8718a3SScott Long 	int i, j;
42336f8718a3SScott Long 
42346f8718a3SScott Long 	result = -1;
42356f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
42366f8718a3SScott Long 	if (error || (req->newptr == NULL))
42376f8718a3SScott Long 		return (error);
42386f8718a3SScott Long 
42396f8718a3SScott Long 	if (result == 1) {
42406f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
42416f8718a3SScott Long 
42426f8718a3SScott Long 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
42436f8718a3SScott Long 		printf("Status Block:\n");
42446f8718a3SScott Long 		for (i = 0x0; i < (BGE_STATUS_BLK_SZ / 4); ) {
42456f8718a3SScott Long 			printf("%06x:", i);
42466f8718a3SScott Long 			for (j = 0; j < 8; j++) {
42476f8718a3SScott Long 				printf(" %04x", sbdata[i]);
42486f8718a3SScott Long 				i += 4;
42496f8718a3SScott Long 			}
42506f8718a3SScott Long 			printf("\n");
42516f8718a3SScott Long 		}
42526f8718a3SScott Long 
42536f8718a3SScott Long 		printf("Registers:\n");
42546f8718a3SScott Long 		for (i = 0x800; i < 0xa00; ) {
42556f8718a3SScott Long 			printf("%06x:", i);
42566f8718a3SScott Long 			for (j = 0; j < 8; j++) {
42576f8718a3SScott Long 				printf(" %08x", CSR_READ_4(sc, i));
42586f8718a3SScott Long 				i += 4;
42596f8718a3SScott Long 			}
42606f8718a3SScott Long 			printf("\n");
42616f8718a3SScott Long 		}
42626f8718a3SScott Long 
42636f8718a3SScott Long 		printf("Hardware Flags:\n");
42645345bad0SScott Long 		if (BGE_IS_575X_PLUS(sc))
42656f8718a3SScott Long 			printf(" - 575X Plus\n");
42665345bad0SScott Long 		if (BGE_IS_5705_PLUS(sc))
42676f8718a3SScott Long 			printf(" - 5705 Plus\n");
42685345bad0SScott Long 		if (BGE_IS_5714_FAMILY(sc))
42695345bad0SScott Long 			printf(" - 5714 Family\n");
42705345bad0SScott Long 		if (BGE_IS_5700_FAMILY(sc))
42715345bad0SScott Long 			printf(" - 5700 Family\n");
42726f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_JUMBO)
42736f8718a3SScott Long 			printf(" - Supports Jumbo Frames\n");
42746f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIX)
42756f8718a3SScott Long 			printf(" - PCI-X Bus\n");
42766f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_PCIE)
42776f8718a3SScott Long 			printf(" - PCI Express Bus\n");
42785ee49a3aSJung-uk Kim 		if (sc->bge_flags & BGE_FLAG_NO_3LED)
42796f8718a3SScott Long 			printf(" - No 3 LEDs\n");
42806f8718a3SScott Long 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
42816f8718a3SScott Long 			printf(" - RX Alignment Bug\n");
42826f8718a3SScott Long 	}
42836f8718a3SScott Long 
42846f8718a3SScott Long 	return (error);
42856f8718a3SScott Long }
42866f8718a3SScott Long 
42876f8718a3SScott Long static int
42886f8718a3SScott Long bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
42896f8718a3SScott Long {
42906f8718a3SScott Long 	struct bge_softc *sc;
42916f8718a3SScott Long 	int error;
42926f8718a3SScott Long 	uint16_t result;
42936f8718a3SScott Long 	uint32_t val;
42946f8718a3SScott Long 
42956f8718a3SScott Long 	result = -1;
42966f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
42976f8718a3SScott Long 	if (error || (req->newptr == NULL))
42986f8718a3SScott Long 		return (error);
42996f8718a3SScott Long 
43006f8718a3SScott Long 	if (result < 0x8000) {
43016f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
43026f8718a3SScott Long 		val = CSR_READ_4(sc, result);
43036f8718a3SScott Long 		printf("reg 0x%06X = 0x%08X\n", result, val);
43046f8718a3SScott Long 	}
43056f8718a3SScott Long 
43066f8718a3SScott Long 	return (error);
43076f8718a3SScott Long }
43086f8718a3SScott Long 
43096f8718a3SScott Long static int
43106f8718a3SScott Long bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
43116f8718a3SScott Long {
43126f8718a3SScott Long 	struct bge_softc *sc;
43136f8718a3SScott Long 	int error;
43146f8718a3SScott Long 	uint16_t result;
43156f8718a3SScott Long 	uint32_t val;
43166f8718a3SScott Long 
43176f8718a3SScott Long 	result = -1;
43186f8718a3SScott Long 	error = sysctl_handle_int(oidp, &result, 0, req);
43196f8718a3SScott Long 	if (error || (req->newptr == NULL))
43206f8718a3SScott Long 		return (error);
43216f8718a3SScott Long 
43226f8718a3SScott Long 	if (result < 0x8000) {
43236f8718a3SScott Long 		sc = (struct bge_softc *)arg1;
43246f8718a3SScott Long 		val = bge_readmem_ind(sc, result);
43256f8718a3SScott Long 		printf("mem 0x%06X = 0x%08X\n", result, val);
43266f8718a3SScott Long 	}
43276f8718a3SScott Long 
43286f8718a3SScott Long 	return (error);
43296f8718a3SScott Long }
43306f8718a3SScott Long #endif
4331