xref: /freebsd-src/sys/dev/gem/if_gem_pci.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1098ca2bdSWarner Losh /*-
2*b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
442c1b001SThomas Moestl  * Copyright (C) 2001 Eduardo Horvath.
52a79fd39SMarius Strobl  * Copyright (c) 2007 Marius Strobl <marius@FreeBSD.org>
642c1b001SThomas Moestl  * All rights reserved.
742c1b001SThomas Moestl  *
842c1b001SThomas Moestl  *
942c1b001SThomas Moestl  * Redistribution and use in source and binary forms, with or without
1042c1b001SThomas Moestl  * modification, are permitted provided that the following conditions
1142c1b001SThomas Moestl  * are met:
1242c1b001SThomas Moestl  * 1. Redistributions of source code must retain the above copyright
1342c1b001SThomas Moestl  *    notice, this list of conditions and the following disclaimer.
1442c1b001SThomas Moestl  * 2. Redistributions in binary form must reproduce the above copyright
1542c1b001SThomas Moestl  *    notice, this list of conditions and the following disclaimer in the
1642c1b001SThomas Moestl  *    documentation and/or other materials provided with the distribution.
1742c1b001SThomas Moestl  *
1842c1b001SThomas Moestl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
1942c1b001SThomas Moestl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2042c1b001SThomas Moestl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2142c1b001SThomas Moestl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
2242c1b001SThomas Moestl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2342c1b001SThomas Moestl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2442c1b001SThomas Moestl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2542c1b001SThomas Moestl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2642c1b001SThomas Moestl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2742c1b001SThomas Moestl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2842c1b001SThomas Moestl  * SUCH DAMAGE.
2942c1b001SThomas Moestl  *
3042c1b001SThomas Moestl  *	from: NetBSD: if_gem_pci.c,v 1.7 2001/10/18 15:09:15 thorpej Exp
3142c1b001SThomas Moestl  */
3242c1b001SThomas Moestl 
33aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
3442c1b001SThomas Moestl /*
358defc88cSMarius Strobl  * PCI bindings for Apple GMAC and Sun GEM Ethernet controllers
3642c1b001SThomas Moestl  */
3742c1b001SThomas Moestl 
3842c1b001SThomas Moestl #include <sys/param.h>
3942c1b001SThomas Moestl #include <sys/systm.h>
4042c1b001SThomas Moestl #include <sys/bus.h>
4142c1b001SThomas Moestl #include <sys/kernel.h>
428cfaff7dSMarius Strobl #include <sys/lock.h>
43186f2b9eSPoul-Henning Kamp #include <sys/module.h>
448cfaff7dSMarius Strobl #include <sys/mutex.h>
4542c1b001SThomas Moestl #include <sys/resource.h>
461ed3fed7SMarius Strobl #include <sys/rman.h>
4742c1b001SThomas Moestl #include <sys/socket.h>
4842c1b001SThomas Moestl 
4942c1b001SThomas Moestl #include <net/ethernet.h>
5042c1b001SThomas Moestl #include <net/if.h>
5142c1b001SThomas Moestl 
5242c1b001SThomas Moestl #include <machine/bus.h>
5358aa35d4SWarner Losh #if defined(__powerpc__)
5465f2c0ffSMarius Strobl #include <dev/ofw/ofw_bus.h>
5511a91bffSMarcel Moolenaar #include <dev/ofw/openfirm.h>
5642c1b001SThomas Moestl #include <machine/ofw_machdep.h>
571ed3fed7SMarius Strobl #endif
581ed3fed7SMarius Strobl #include <machine/resource.h>
5942c1b001SThomas Moestl 
6042c1b001SThomas Moestl #include <dev/gem/if_gemreg.h>
6142c1b001SThomas Moestl #include <dev/gem/if_gemvar.h>
6242c1b001SThomas Moestl 
6342c1b001SThomas Moestl #include <dev/pci/pcireg.h>
641ed3fed7SMarius Strobl #include <dev/pci/pcivar.h>
6542c1b001SThomas Moestl 
6642c1b001SThomas Moestl #include "miibus_if.h"
6742c1b001SThomas Moestl 
682a79fd39SMarius Strobl static int	gem_pci_attach(device_t dev);
692a79fd39SMarius Strobl static int	gem_pci_detach(device_t dev);
702a79fd39SMarius Strobl static int	gem_pci_probe(device_t dev);
712a79fd39SMarius Strobl static int	gem_pci_resume(device_t dev);
722a79fd39SMarius Strobl static int	gem_pci_suspend(device_t dev);
7342c1b001SThomas Moestl 
74b2b71ff0SWarner Losh static const struct gem_pci_dev {
75b2b71ff0SWarner Losh 	uint32_t	gpd_devid;
76b2b71ff0SWarner Losh 	int		gpd_variant;
77b2b71ff0SWarner Losh 	const char	*gpd_desc;
78b2b71ff0SWarner Losh } gem_pci_devlist[] = {
79b2b71ff0SWarner Losh 	{ 0x2bad108e, GEM_SUN_GEM,	"Sun GEM Gigabit Ethernet" },
80b2b71ff0SWarner Losh 	{ 0x0021106b, GEM_APPLE_GMAC,	"Apple UniNorth GMAC Ethernet" },
81b2b71ff0SWarner Losh 	{ 0x0024106b, GEM_APPLE_GMAC,	"Apple Pangea GMAC Ethernet" },
82b2b71ff0SWarner Losh 	{ 0x0032106b, GEM_APPLE_GMAC,	"Apple UniNorth2 GMAC Ethernet" },
83b2b71ff0SWarner Losh 	{ 0x004c106b, GEM_APPLE_K2_GMAC,"Apple K2 GMAC Ethernet" },
84b2b71ff0SWarner Losh 	{ 0x0051106b, GEM_APPLE_GMAC,	"Apple Shasta GMAC Ethernet" },
85b2b71ff0SWarner Losh 	{ 0x006b106b, GEM_APPLE_GMAC,	"Apple Intrepid 2 GMAC Ethernet" },
86b2b71ff0SWarner Losh 	{ 0, 0, NULL }
87b2b71ff0SWarner Losh };
88b2b71ff0SWarner Losh 
8942c1b001SThomas Moestl static device_method_t gem_pci_methods[] = {
9042c1b001SThomas Moestl 	/* Device interface */
9142c1b001SThomas Moestl 	DEVMETHOD(device_probe,		gem_pci_probe),
9242c1b001SThomas Moestl 	DEVMETHOD(device_attach,	gem_pci_attach),
93cbbdf236SThomas Moestl 	DEVMETHOD(device_detach,	gem_pci_detach),
94cbbdf236SThomas Moestl 	DEVMETHOD(device_suspend,	gem_pci_suspend),
95cbbdf236SThomas Moestl 	DEVMETHOD(device_resume,	gem_pci_resume),
96cbbdf236SThomas Moestl 	/* Use the suspend handler here, it is all that is required. */
97cbbdf236SThomas Moestl 	DEVMETHOD(device_shutdown,	gem_pci_suspend),
9842c1b001SThomas Moestl 
9942c1b001SThomas Moestl 	/* MII interface */
10042c1b001SThomas Moestl 	DEVMETHOD(miibus_readreg,	gem_mii_readreg),
10142c1b001SThomas Moestl 	DEVMETHOD(miibus_writereg,	gem_mii_writereg),
10242c1b001SThomas Moestl 	DEVMETHOD(miibus_statchg,	gem_mii_statchg),
10342c1b001SThomas Moestl 
1044b7ec270SMarius Strobl 	DEVMETHOD_END
10542c1b001SThomas Moestl };
10642c1b001SThomas Moestl 
10742c1b001SThomas Moestl static driver_t gem_pci_driver = {
10842c1b001SThomas Moestl 	"gem",
10942c1b001SThomas Moestl 	gem_pci_methods,
110e1bb13cdSPoul-Henning Kamp 	sizeof(struct gem_softc)
11142c1b001SThomas Moestl };
11242c1b001SThomas Moestl 
1136f70f80cSJohn Baldwin DRIVER_MODULE(gem, pci, gem_pci_driver, 0, 0);
114b2b71ff0SWarner Losh MODULE_PNP_INFO("W32:vendor/device", pci, gem, gem_pci_devlist,
115329e817fSWarner Losh     nitems(gem_pci_devlist) - 1);
116f246e4a1SMatthew N. Dodd MODULE_DEPEND(gem, pci, 1, 1, 1);
117f246e4a1SMatthew N. Dodd MODULE_DEPEND(gem, ether, 1, 1, 1);
11842c1b001SThomas Moestl 
11942c1b001SThomas Moestl static int
gem_pci_probe(device_t dev)1202a79fd39SMarius Strobl gem_pci_probe(device_t dev)
12142c1b001SThomas Moestl {
12242c1b001SThomas Moestl 	int i;
12342c1b001SThomas Moestl 
12442c1b001SThomas Moestl 	for (i = 0; gem_pci_devlist[i].gpd_desc != NULL; i++) {
1251ed3fed7SMarius Strobl 		if (pci_get_devid(dev) == gem_pci_devlist[i].gpd_devid) {
12642c1b001SThomas Moestl 			device_set_desc(dev, gem_pci_devlist[i].gpd_desc);
127538565c4SWarner Losh 			return (BUS_PROBE_DEFAULT);
12842c1b001SThomas Moestl 		}
12942c1b001SThomas Moestl 	}
13042c1b001SThomas Moestl 
13142c1b001SThomas Moestl 	return (ENXIO);
13242c1b001SThomas Moestl }
13342c1b001SThomas Moestl 
134e1bb13cdSPoul-Henning Kamp static struct resource_spec gem_pci_res_spec[] = {
135bd3d9826SMarius Strobl 	{ SYS_RES_IRQ, 0, RF_SHAREABLE | RF_ACTIVE },	/* GEM_RES_INTR */
1368defc88cSMarius Strobl 	{ SYS_RES_MEMORY, PCIR_BAR(0), RF_ACTIVE },	/* GEM_RES_MEM */
137e1bb13cdSPoul-Henning Kamp 	{ -1, 0 }
138e1bb13cdSPoul-Henning Kamp };
139e1bb13cdSPoul-Henning Kamp 
14065f2c0ffSMarius Strobl #define	GEM_SHARED_PINS		"shared-pins"
14165f2c0ffSMarius Strobl #define	GEM_SHARED_PINS_SERDES	"serdes"
14265f2c0ffSMarius Strobl 
14342c1b001SThomas Moestl static int
gem_pci_attach(device_t dev)1442a79fd39SMarius Strobl gem_pci_attach(device_t dev)
14542c1b001SThomas Moestl {
1461ed3fed7SMarius Strobl 	struct gem_softc *sc;
1471ed3fed7SMarius Strobl 	int i;
14858aa35d4SWarner Losh #if defined(__powerpc__)
14965f2c0ffSMarius Strobl 	char buf[sizeof(GEM_SHARED_PINS)];
15065f2c0ffSMarius Strobl #else
1511ed3fed7SMarius Strobl 	int j;
1521ed3fed7SMarius Strobl #endif
1531ed3fed7SMarius Strobl 
1541ed3fed7SMarius Strobl 	sc = device_get_softc(dev);
1551ed3fed7SMarius Strobl 	sc->sc_variant = GEM_UNKNOWN;
1561ed3fed7SMarius Strobl 	for (i = 0; gem_pci_devlist[i].gpd_desc != NULL; i++) {
1571ed3fed7SMarius Strobl 		if (pci_get_devid(dev) == gem_pci_devlist[i].gpd_devid) {
1581ed3fed7SMarius Strobl 			sc->sc_variant = gem_pci_devlist[i].gpd_variant;
1591ed3fed7SMarius Strobl 			break;
1601ed3fed7SMarius Strobl 		}
1611ed3fed7SMarius Strobl 	}
1621ed3fed7SMarius Strobl 	if (sc->sc_variant == GEM_UNKNOWN) {
1631ed3fed7SMarius Strobl 		device_printf(dev, "unknown adaptor\n");
1641ed3fed7SMarius Strobl 		return (ENXIO);
1651ed3fed7SMarius Strobl 	}
166e64a9d37SThomas Moestl 
16749921f1eSThomas Moestl 	pci_enable_busmaster(dev);
16842c1b001SThomas Moestl 
16942c1b001SThomas Moestl 	sc->sc_dev = dev;
17042c1b001SThomas Moestl 
171e1bb13cdSPoul-Henning Kamp 	if (bus_alloc_resources(dev, gem_pci_res_spec, sc->sc_res)) {
172e1bb13cdSPoul-Henning Kamp 		device_printf(dev, "failed to allocate resources\n");
173e1bb13cdSPoul-Henning Kamp 		bus_release_resources(dev, gem_pci_res_spec, sc->sc_res);
174e1bb13cdSPoul-Henning Kamp 		return (ENXIO);
175e1bb13cdSPoul-Henning Kamp 	}
176e1bb13cdSPoul-Henning Kamp 
1778cfaff7dSMarius Strobl 	GEM_LOCK_INIT(sc, device_get_nameunit(dev));
1788cfaff7dSMarius Strobl 
1799ba2b298SMarius Strobl 	/* Determine whether we're running at 66MHz. */
1808defc88cSMarius Strobl 	if ((GEM_READ_4(sc, GEM_PCI_BIF_CONFIG) & GEM_PCI_BIF_CNF_M66EN) != 0)
1819ba2b298SMarius Strobl 		sc->sc_flags |= GEM_PCI66;
1829ba2b298SMarius Strobl 
18358aa35d4SWarner Losh #if defined(__powerpc__)
184fc74a9f9SBrooks Davis 	OF_getetheraddr(dev, sc->sc_enaddr);
18565f2c0ffSMarius Strobl 	if (OF_getprop(ofw_bus_get_node(dev), GEM_SHARED_PINS, buf,
18665f2c0ffSMarius Strobl 	    sizeof(buf)) > 0) {
18765f2c0ffSMarius Strobl 		buf[sizeof(buf) - 1] = '\0';
18865f2c0ffSMarius Strobl 		if (strcmp(buf, GEM_SHARED_PINS_SERDES) == 0)
18965f2c0ffSMarius Strobl 			sc->sc_flags |= GEM_SERDES;
19065f2c0ffSMarius Strobl 	}
1911ed3fed7SMarius Strobl #else
1921ed3fed7SMarius Strobl 	/*
1931ed3fed7SMarius Strobl 	 * Dig out VPD (vital product data) and read NA (network address).
1949ba2b298SMarius Strobl 	 * The VPD resides in the PCI Expansion ROM (PCI FCode) and can't
1959ba2b298SMarius Strobl 	 * be accessed via the PCI capability pointer.
1961ed3fed7SMarius Strobl 	 * ``Writing FCode 3.x Programs'' (newer ones, dated 1997 and later)
1971ed3fed7SMarius Strobl 	 * chapter 2 describes the data structure.
1981ed3fed7SMarius Strobl 	 */
1991ed3fed7SMarius Strobl 
2001ed3fed7SMarius Strobl #define	PCI_ROMHDR_SIZE			0x1c
2011ed3fed7SMarius Strobl #define	PCI_ROMHDR_SIG			0x00
2021ed3fed7SMarius Strobl #define	PCI_ROMHDR_SIG_MAGIC		0xaa55		/* little endian */
2031ed3fed7SMarius Strobl #define	PCI_ROMHDR_PTR_DATA		0x18
2041ed3fed7SMarius Strobl #define	PCI_ROM_SIZE			0x18
2051ed3fed7SMarius Strobl #define	PCI_ROM_SIG			0x00
2061ed3fed7SMarius Strobl #define	PCI_ROM_SIG_MAGIC		0x52494350	/* "PCIR", endian */
2071ed3fed7SMarius Strobl 							/* reversed */
2081ed3fed7SMarius Strobl #define	PCI_ROM_VENDOR			0x04
2091ed3fed7SMarius Strobl #define	PCI_ROM_DEVICE			0x06
2101ed3fed7SMarius Strobl #define	PCI_ROM_PTR_VPD			0x08
2111ed3fed7SMarius Strobl #define	PCI_VPDRES_BYTE0		0x00
2121ed3fed7SMarius Strobl #define	PCI_VPDRES_ISLARGE(x)		((x) & 0x80)
2131ed3fed7SMarius Strobl #define	PCI_VPDRES_LARGE_NAME(x)	((x) & 0x7f)
2141ed3fed7SMarius Strobl #define	PCI_VPDRES_LARGE_LEN_LSB	0x01
2151ed3fed7SMarius Strobl #define	PCI_VPDRES_LARGE_LEN_MSB	0x02
2169ba2b298SMarius Strobl #define	PCI_VPDRES_LARGE_SIZE		0x03
2179ba2b298SMarius Strobl #define	PCI_VPDRES_TYPE_VPD		0x10		/* large */
2181ed3fed7SMarius Strobl #define	PCI_VPD_KEY0			0x00
2191ed3fed7SMarius Strobl #define	PCI_VPD_KEY1			0x01
2201ed3fed7SMarius Strobl #define	PCI_VPD_LEN			0x02
2219ba2b298SMarius Strobl #define	PCI_VPD_SIZE			0x03
2221ed3fed7SMarius Strobl 
223bd3d9826SMarius Strobl #define	GEM_ROM_READ_1(sc, offs)					\
2248defc88cSMarius Strobl 	GEM_READ_1((sc), GEM_PCI_ROM_OFFSET + (offs))
225bd3d9826SMarius Strobl #define	GEM_ROM_READ_2(sc, offs)					\
2268defc88cSMarius Strobl 	GEM_READ_2((sc), GEM_PCI_ROM_OFFSET + (offs))
227bd3d9826SMarius Strobl #define	GEM_ROM_READ_4(sc, offs)					\
2288defc88cSMarius Strobl 	GEM_READ_4((sc), GEM_PCI_ROM_OFFSET + (offs))
2291ed3fed7SMarius Strobl 
2301ed3fed7SMarius Strobl 	/* Read PCI Expansion ROM header. */
2311ed3fed7SMarius Strobl 	if (GEM_ROM_READ_2(sc, PCI_ROMHDR_SIG) != PCI_ROMHDR_SIG_MAGIC ||
2322a79fd39SMarius Strobl 	    (i = GEM_ROM_READ_2(sc, PCI_ROMHDR_PTR_DATA)) <
2332a79fd39SMarius Strobl 	    PCI_ROMHDR_SIZE) {
2341ed3fed7SMarius Strobl 		device_printf(dev, "unexpected PCI Expansion ROM header\n");
2351ed3fed7SMarius Strobl 		goto fail;
2361ed3fed7SMarius Strobl 	}
2371ed3fed7SMarius Strobl 
2381ed3fed7SMarius Strobl 	/* Read PCI Expansion ROM data. */
2391ed3fed7SMarius Strobl 	if (GEM_ROM_READ_4(sc, i + PCI_ROM_SIG) != PCI_ROM_SIG_MAGIC ||
2401ed3fed7SMarius Strobl 	    GEM_ROM_READ_2(sc, i + PCI_ROM_VENDOR) != pci_get_vendor(dev) ||
2411ed3fed7SMarius Strobl 	    GEM_ROM_READ_2(sc, i + PCI_ROM_DEVICE) != pci_get_device(dev) ||
2422a79fd39SMarius Strobl 	    (j = GEM_ROM_READ_2(sc, i + PCI_ROM_PTR_VPD)) <
2432a79fd39SMarius Strobl 	    i + PCI_ROM_SIZE) {
2441ed3fed7SMarius Strobl 		device_printf(dev, "unexpected PCI Expansion ROM data\n");
2451ed3fed7SMarius Strobl 		goto fail;
2461ed3fed7SMarius Strobl 	}
2471ed3fed7SMarius Strobl 
2481ed3fed7SMarius Strobl 	/*
2491ed3fed7SMarius Strobl 	 * Read PCI VPD.
2501ed3fed7SMarius Strobl 	 * SUNW,pci-gem cards have a single large resource VPD-R tag
2511ed3fed7SMarius Strobl 	 * containing one NA.  The VPD used is not in PCI 2.2 standard
2521ed3fed7SMarius Strobl 	 * format however.  The length in the resource header is in big
2531ed3fed7SMarius Strobl 	 * endian and the end tag is non-standard (0x79) and followed
2541ed3fed7SMarius Strobl 	 * by an all-zero "checksum" byte.  Sun calls this a "Fresh
2551ed3fed7SMarius Strobl 	 * Choice Ethernet" VPD...
2561ed3fed7SMarius Strobl 	 */
2572a79fd39SMarius Strobl 	if (PCI_VPDRES_ISLARGE(GEM_ROM_READ_1(sc,
2582a79fd39SMarius Strobl 	    j + PCI_VPDRES_BYTE0)) == 0 ||
2592a79fd39SMarius Strobl 	    PCI_VPDRES_LARGE_NAME(GEM_ROM_READ_1(sc,
260bd3d9826SMarius Strobl 	    j + PCI_VPDRES_BYTE0)) != PCI_VPDRES_TYPE_VPD ||
2619ba2b298SMarius Strobl 	    ((GEM_ROM_READ_1(sc, j + PCI_VPDRES_LARGE_LEN_LSB) << 8) |
2621ed3fed7SMarius Strobl 	    GEM_ROM_READ_1(sc, j + PCI_VPDRES_LARGE_LEN_MSB)) !=
2631ed3fed7SMarius Strobl 	    PCI_VPD_SIZE + ETHER_ADDR_LEN ||
2649ba2b298SMarius Strobl 	    GEM_ROM_READ_1(sc, j + PCI_VPDRES_LARGE_SIZE + PCI_VPD_KEY0) !=
2651ed3fed7SMarius Strobl 	    0x4e /* N */ ||
2669ba2b298SMarius Strobl 	    GEM_ROM_READ_1(sc, j + PCI_VPDRES_LARGE_SIZE + PCI_VPD_KEY1) !=
2671ed3fed7SMarius Strobl 	    0x41 /* A */ ||
2689ba2b298SMarius Strobl 	    GEM_ROM_READ_1(sc, j + PCI_VPDRES_LARGE_SIZE + PCI_VPD_LEN) !=
2691ed3fed7SMarius Strobl 	    ETHER_ADDR_LEN ||
2709ba2b298SMarius Strobl 	    GEM_ROM_READ_1(sc, j + PCI_VPDRES_LARGE_SIZE + PCI_VPD_SIZE +
2711ed3fed7SMarius Strobl 	    ETHER_ADDR_LEN) != 0x79) {
2721ed3fed7SMarius Strobl 		device_printf(dev, "unexpected PCI VPD\n");
2731ed3fed7SMarius Strobl 		goto fail;
2741ed3fed7SMarius Strobl 	}
2758defc88cSMarius Strobl 	bus_read_region_1(sc->sc_res[GEM_RES_MEM],
2769ba2b298SMarius Strobl 	    GEM_PCI_ROM_OFFSET + j + PCI_VPDRES_LARGE_SIZE + PCI_VPD_SIZE,
277bd3d9826SMarius Strobl 	    sc->sc_enaddr, ETHER_ADDR_LEN);
2781ed3fed7SMarius Strobl #endif
279635dc3f7SNathan Whitehorn 	/*
280635dc3f7SNathan Whitehorn 	 * The Xserve G5 has a fake GMAC with an all-zero MAC address.
281635dc3f7SNathan Whitehorn 	 * Check for this, and don't attach in this case.
282635dc3f7SNathan Whitehorn 	 */
283635dc3f7SNathan Whitehorn 
284635dc3f7SNathan Whitehorn 	for (i = 0; i < ETHER_ADDR_LEN && sc->sc_enaddr[i] == 0; i++) {}
285635dc3f7SNathan Whitehorn 	if (i == ETHER_ADDR_LEN) {
286635dc3f7SNathan Whitehorn 		device_printf(dev, "invalid MAC address\n");
287635dc3f7SNathan Whitehorn 		goto fail;
288635dc3f7SNathan Whitehorn 	}
28942c1b001SThomas Moestl 
29042c1b001SThomas Moestl 	if (gem_attach(sc) != 0) {
2912a79fd39SMarius Strobl 		device_printf(dev, "could not be attached\n");
292e1bb13cdSPoul-Henning Kamp 		goto fail;
29342c1b001SThomas Moestl 	}
29442c1b001SThomas Moestl 
295bd3d9826SMarius Strobl 	if (bus_setup_intr(dev, sc->sc_res[GEM_RES_INTR], INTR_TYPE_NET |
296bd3d9826SMarius Strobl 	    INTR_MPSAFE, NULL, gem_intr, sc, &sc->sc_ih) != 0) {
29742c1b001SThomas Moestl 		device_printf(dev, "failed to set up interrupt\n");
2983437dbefSThomas Moestl 		gem_detach(sc);
299e1bb13cdSPoul-Henning Kamp 		goto fail;
30042c1b001SThomas Moestl 	}
30142c1b001SThomas Moestl 	return (0);
30242c1b001SThomas Moestl 
303e1bb13cdSPoul-Henning Kamp  fail:
3048cfaff7dSMarius Strobl 	GEM_LOCK_DESTROY(sc);
3051ed3fed7SMarius Strobl 	bus_release_resources(dev, gem_pci_res_spec, sc->sc_res);
30642c1b001SThomas Moestl 	return (ENXIO);
30742c1b001SThomas Moestl }
308cbbdf236SThomas Moestl 
309cbbdf236SThomas Moestl static int
gem_pci_detach(device_t dev)3102a79fd39SMarius Strobl gem_pci_detach(device_t dev)
311cbbdf236SThomas Moestl {
3122a79fd39SMarius Strobl 	struct gem_softc *sc;
313cbbdf236SThomas Moestl 
3142a79fd39SMarius Strobl 	sc = device_get_softc(dev);
315bd3d9826SMarius Strobl 	bus_teardown_intr(dev, sc->sc_res[GEM_RES_INTR], sc->sc_ih);
3168cfaff7dSMarius Strobl 	gem_detach(sc);
3178cfaff7dSMarius Strobl 	GEM_LOCK_DESTROY(sc);
318e1bb13cdSPoul-Henning Kamp 	bus_release_resources(dev, gem_pci_res_spec, sc->sc_res);
319cbbdf236SThomas Moestl 	return (0);
320cbbdf236SThomas Moestl }
321cbbdf236SThomas Moestl 
322cbbdf236SThomas Moestl static int
gem_pci_suspend(device_t dev)3232a79fd39SMarius Strobl gem_pci_suspend(device_t dev)
324cbbdf236SThomas Moestl {
325cbbdf236SThomas Moestl 
3269ba2b298SMarius Strobl 	gem_suspend(device_get_softc(dev));
327cbbdf236SThomas Moestl 	return (0);
328cbbdf236SThomas Moestl }
329cbbdf236SThomas Moestl 
330cbbdf236SThomas Moestl static int
gem_pci_resume(device_t dev)3312a79fd39SMarius Strobl gem_pci_resume(device_t dev)
332cbbdf236SThomas Moestl {
333cbbdf236SThomas Moestl 
3349ba2b298SMarius Strobl 	gem_resume(device_get_softc(dev));
335cbbdf236SThomas Moestl 	return (0);
336cbbdf236SThomas Moestl }
337