xref: /freebsd/sys/dev/gem/if_gem.c (revision 78d22f42a8cbf28d830117e7432990e1dc8a61e0)
1aad970f1SDavid E. O'Brien /*-
242c1b001SThomas Moestl  * Copyright (C) 2001 Eduardo Horvath.
3305f2c06SThomas Moestl  * Copyright (c) 2001-2003 Thomas Moestl
42a79fd39SMarius Strobl  * Copyright (c) 2007 Marius Strobl <marius@FreeBSD.org>
542c1b001SThomas Moestl  * All rights reserved.
642c1b001SThomas Moestl  *
742c1b001SThomas Moestl  * Redistribution and use in source and binary forms, with or without
842c1b001SThomas Moestl  * modification, are permitted provided that the following conditions
942c1b001SThomas Moestl  * are met:
1042c1b001SThomas Moestl  * 1. Redistributions of source code must retain the above copyright
1142c1b001SThomas Moestl  *    notice, this list of conditions and the following disclaimer.
1242c1b001SThomas Moestl  * 2. Redistributions in binary form must reproduce the above copyright
1342c1b001SThomas Moestl  *    notice, this list of conditions and the following disclaimer in the
1442c1b001SThomas Moestl  *    documentation and/or other materials provided with the distribution.
1542c1b001SThomas Moestl  *
1642c1b001SThomas Moestl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
1742c1b001SThomas Moestl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1842c1b001SThomas Moestl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1942c1b001SThomas Moestl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
2042c1b001SThomas Moestl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2142c1b001SThomas Moestl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2242c1b001SThomas Moestl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2342c1b001SThomas Moestl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2442c1b001SThomas Moestl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2542c1b001SThomas Moestl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2642c1b001SThomas Moestl  * SUCH DAMAGE.
2742c1b001SThomas Moestl  *
28336cca9eSBenno Rice  *	from: NetBSD: gem.c,v 1.21 2002/06/01 23:50:58 lukem Exp
2942c1b001SThomas Moestl  */
3042c1b001SThomas Moestl 
31aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
32aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
33aad970f1SDavid E. O'Brien 
3442c1b001SThomas Moestl /*
351ed3fed7SMarius Strobl  * Driver for Apple GMAC, Sun ERI and Sun GEM Ethernet controllers
3642c1b001SThomas Moestl  */
3742c1b001SThomas Moestl 
3818100346SThomas Moestl #if 0
3942c1b001SThomas Moestl #define	GEM_DEBUG
4018100346SThomas Moestl #endif
4142c1b001SThomas Moestl 
42c3d5598aSMarius Strobl #if 0	/* XXX: In case of emergency, re-enable this. */
43c3d5598aSMarius Strobl #define	GEM_RINT_TIMEOUT
44c3d5598aSMarius Strobl #endif
45c3d5598aSMarius Strobl 
4642c1b001SThomas Moestl #include <sys/param.h>
4742c1b001SThomas Moestl #include <sys/systm.h>
4842c1b001SThomas Moestl #include <sys/bus.h>
4942c1b001SThomas Moestl #include <sys/callout.h>
50a30d4b32SMike Barcroft #include <sys/endian.h>
5142c1b001SThomas Moestl #include <sys/mbuf.h>
5242c1b001SThomas Moestl #include <sys/malloc.h>
5342c1b001SThomas Moestl #include <sys/kernel.h>
548cfaff7dSMarius Strobl #include <sys/lock.h>
55186f2b9eSPoul-Henning Kamp #include <sys/module.h>
568cfaff7dSMarius Strobl #include <sys/mutex.h>
5742c1b001SThomas Moestl #include <sys/socket.h>
5842c1b001SThomas Moestl #include <sys/sockio.h>
59e1bb13cdSPoul-Henning Kamp #include <sys/rman.h>
6042c1b001SThomas Moestl 
6108e0fdebSThomas Moestl #include <net/bpf.h>
6242c1b001SThomas Moestl #include <net/ethernet.h>
6342c1b001SThomas Moestl #include <net/if.h>
6442c1b001SThomas Moestl #include <net/if_arp.h>
6542c1b001SThomas Moestl #include <net/if_dl.h>
6642c1b001SThomas Moestl #include <net/if_media.h>
67fc74a9f9SBrooks Davis #include <net/if_types.h>
6800d12766SMarius Strobl #include <net/if_vlan_var.h>
6942c1b001SThomas Moestl 
7012fb0330SPyun YongHyeon #include <netinet/in.h>
7112fb0330SPyun YongHyeon #include <netinet/in_systm.h>
7212fb0330SPyun YongHyeon #include <netinet/ip.h>
7312fb0330SPyun YongHyeon #include <netinet/tcp.h>
7412fb0330SPyun YongHyeon #include <netinet/udp.h>
7512fb0330SPyun YongHyeon 
7642c1b001SThomas Moestl #include <machine/bus.h>
7742c1b001SThomas Moestl 
7842c1b001SThomas Moestl #include <dev/mii/mii.h>
7942c1b001SThomas Moestl #include <dev/mii/miivar.h>
8042c1b001SThomas Moestl 
81681f7d03SWarner Losh #include <dev/gem/if_gemreg.h>
82681f7d03SWarner Losh #include <dev/gem/if_gemvar.h>
8342c1b001SThomas Moestl 
841ed3fed7SMarius Strobl CTASSERT(powerof2(GEM_NRXDESC) && GEM_NRXDESC >= 32 && GEM_NRXDESC <= 8192);
851ed3fed7SMarius Strobl CTASSERT(powerof2(GEM_NTXDESC) && GEM_NTXDESC >= 32 && GEM_NTXDESC <= 8192);
861ed3fed7SMarius Strobl 
8742c1b001SThomas Moestl #define	TRIES	10000
881ed3fed7SMarius Strobl 
8912fb0330SPyun YongHyeon /*
9078d22f42SMarius Strobl  * The hardware supports basic TCP/UDP checksum offloading.  However,
9112fb0330SPyun YongHyeon  * the hardware doesn't compensate the checksum for UDP datagram which
9212fb0330SPyun YongHyeon  * can yield to 0x0.  As a safe guard, UDP checksum offload is disabled
9312fb0330SPyun YongHyeon  * by default.  It can be reactivated by setting special link option
9412fb0330SPyun YongHyeon  * link0 with ifconfig(8).
9512fb0330SPyun YongHyeon  */
9612fb0330SPyun YongHyeon #define	GEM_CSUM_FEATURES	(CSUM_TCP)
9742c1b001SThomas Moestl 
982a79fd39SMarius Strobl static int	gem_add_rxbuf(struct gem_softc *sc, int idx);
99bd3d9826SMarius Strobl static int	gem_bitwait(struct gem_softc *sc, u_int bank, bus_addr_t r,
100bd3d9826SMarius Strobl 		    uint32_t clr, uint32_t set);
1012a79fd39SMarius Strobl static void	gem_cddma_callback(void *xsc, bus_dma_segment_t *segs,
1022a79fd39SMarius Strobl 		    int nsegs, int error);
1032a79fd39SMarius Strobl static int	gem_disable_rx(struct gem_softc *sc);
1042a79fd39SMarius Strobl static int	gem_disable_tx(struct gem_softc *sc);
1052a79fd39SMarius Strobl static void	gem_eint(struct gem_softc *sc, u_int status);
1062a79fd39SMarius Strobl static void	gem_init(void *xsc);
1072a79fd39SMarius Strobl static void	gem_init_locked(struct gem_softc *sc);
1082a79fd39SMarius Strobl static void	gem_init_regs(struct gem_softc *sc);
1092a79fd39SMarius Strobl static int	gem_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
1102a79fd39SMarius Strobl static int	gem_load_txmbuf(struct gem_softc *sc, struct mbuf **m_head);
1112a79fd39SMarius Strobl static int	gem_meminit(struct gem_softc *sc);
1122a79fd39SMarius Strobl static void	gem_mifinit(struct gem_softc *sc);
1132a79fd39SMarius Strobl static void	gem_reset(struct gem_softc *sc);
1142a79fd39SMarius Strobl static int	gem_reset_rx(struct gem_softc *sc);
1151ed3fed7SMarius Strobl static void	gem_reset_rxdma(struct gem_softc *sc);
1162a79fd39SMarius Strobl static int	gem_reset_tx(struct gem_softc *sc);
1172a79fd39SMarius Strobl static u_int	gem_ringsize(u_int sz);
1182a79fd39SMarius Strobl static void	gem_rint(struct gem_softc *sc);
119c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT
1202a79fd39SMarius Strobl static void	gem_rint_timeout(void *arg);
12111e3f060SJake Burkholder #endif
1222a79fd39SMarius Strobl static __inline void gem_rxcksum(struct mbuf *m, uint64_t flags);
1232a79fd39SMarius Strobl static void	gem_rxdrain(struct gem_softc *sc);
1242a79fd39SMarius Strobl static void	gem_setladrf(struct gem_softc *sc);
1252a79fd39SMarius Strobl static void	gem_start(struct ifnet *ifp);
1262a79fd39SMarius Strobl static void	gem_start_locked(struct ifnet *ifp);
1272a79fd39SMarius Strobl static void	gem_stop(struct ifnet *ifp, int disable);
1282a79fd39SMarius Strobl static void	gem_tick(void *arg);
1292a79fd39SMarius Strobl static void	gem_tint(struct gem_softc *sc);
1302a79fd39SMarius Strobl static int	gem_watchdog(struct gem_softc *sc);
13142c1b001SThomas Moestl 
13242c1b001SThomas Moestl devclass_t gem_devclass;
13342c1b001SThomas Moestl DRIVER_MODULE(miibus, gem, miibus_driver, miibus_devclass, 0, 0);
13442c1b001SThomas Moestl MODULE_DEPEND(gem, miibus, 1, 1, 1);
13542c1b001SThomas Moestl 
13642c1b001SThomas Moestl #ifdef GEM_DEBUG
13742c1b001SThomas Moestl #include <sys/ktr.h>
13842c1b001SThomas Moestl #define	KTR_GEM		KTR_CT2
13942c1b001SThomas Moestl #endif
14042c1b001SThomas Moestl 
141bd3d9826SMarius Strobl #define	GEM_BANK1_BITWAIT(sc, r, clr, set)				\
142bd3d9826SMarius Strobl 	gem_bitwait((sc), GEM_RES_BANK1, (r), (clr), (set))
143bd3d9826SMarius Strobl #define	GEM_BANK2_BITWAIT(sc, r, clr, set)				\
144bd3d9826SMarius Strobl 	gem_bitwait((sc), GEM_RES_BANK2, (r), (clr), (set))
145bd3d9826SMarius Strobl 
14642c1b001SThomas Moestl int
1472a79fd39SMarius Strobl gem_attach(struct gem_softc *sc)
14842c1b001SThomas Moestl {
1492a79fd39SMarius Strobl 	struct gem_txsoft *txs;
150fc74a9f9SBrooks Davis 	struct ifnet *ifp;
1512a79fd39SMarius Strobl 	int error, i;
1522a79fd39SMarius Strobl 	uint32_t v;
15342c1b001SThomas Moestl 
154fc74a9f9SBrooks Davis 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
155fc74a9f9SBrooks Davis 	if (ifp == NULL)
156fc74a9f9SBrooks Davis 		return (ENOSPC);
157fc74a9f9SBrooks Davis 
1581f317bf9SMarius Strobl 	callout_init_mtx(&sc->sc_tick_ch, &sc->sc_mtx, 0);
1591f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT
1601f317bf9SMarius Strobl 	callout_init_mtx(&sc->sc_rx_ch, &sc->sc_mtx, 0);
1611f317bf9SMarius Strobl #endif
1621f317bf9SMarius Strobl 
16342c1b001SThomas Moestl 	/* Make sure the chip is stopped. */
16442c1b001SThomas Moestl 	ifp->if_softc = sc;
16542c1b001SThomas Moestl 	gem_reset(sc);
16642c1b001SThomas Moestl 
167378f231eSJohn-Mark Gurney 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
168378f231eSJohn-Mark Gurney 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1692a79fd39SMarius Strobl 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0, NULL,
1702a79fd39SMarius Strobl 	    NULL, &sc->sc_pdmatag);
17142c1b001SThomas Moestl 	if (error)
172fc74a9f9SBrooks Davis 		goto fail_ifnet;
17342c1b001SThomas Moestl 
17442c1b001SThomas Moestl 	error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0,
17512fb0330SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
17612fb0330SPyun YongHyeon 	    1, MCLBYTES, BUS_DMA_ALLOCNOW, NULL, NULL, &sc->sc_rdmatag);
17742c1b001SThomas Moestl 	if (error)
178305f2c06SThomas Moestl 		goto fail_ptag;
179305f2c06SThomas Moestl 
180305f2c06SThomas Moestl 	error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0,
18112fb0330SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
18212fb0330SPyun YongHyeon 	    MCLBYTES * GEM_NTXSEGS, GEM_NTXSEGS, MCLBYTES,
183f6b1c44dSScott Long 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->sc_tdmatag);
184305f2c06SThomas Moestl 	if (error)
185305f2c06SThomas Moestl 		goto fail_rtag;
18642c1b001SThomas Moestl 
18742c1b001SThomas Moestl 	error = bus_dma_tag_create(sc->sc_pdmatag, PAGE_SIZE, 0,
18812fb0330SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
18942c1b001SThomas Moestl 	    sizeof(struct gem_control_data), 1,
19012fb0330SPyun YongHyeon 	    sizeof(struct gem_control_data), 0,
19112fb0330SPyun YongHyeon 	    NULL, NULL, &sc->sc_cdmatag);
19242c1b001SThomas Moestl 	if (error)
193305f2c06SThomas Moestl 		goto fail_ttag;
19442c1b001SThomas Moestl 
19542c1b001SThomas Moestl 	/*
1962a79fd39SMarius Strobl 	 * Allocate the control data structures, create and load the
19742c1b001SThomas Moestl 	 * DMA map for it.
19842c1b001SThomas Moestl 	 */
19942c1b001SThomas Moestl 	if ((error = bus_dmamem_alloc(sc->sc_cdmatag,
20012fb0330SPyun YongHyeon 	    (void **)&sc->sc_control_data,
20112fb0330SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
20212fb0330SPyun YongHyeon 	    &sc->sc_cddmamap))) {
2032a79fd39SMarius Strobl 		device_printf(sc->sc_dev,
2042a79fd39SMarius Strobl 		    "unable to allocate control data, error = %d\n", error);
205305f2c06SThomas Moestl 		goto fail_ctag;
20642c1b001SThomas Moestl 	}
20742c1b001SThomas Moestl 
20842c1b001SThomas Moestl 	sc->sc_cddma = 0;
20942c1b001SThomas Moestl 	if ((error = bus_dmamap_load(sc->sc_cdmatag, sc->sc_cddmamap,
21042c1b001SThomas Moestl 	    sc->sc_control_data, sizeof(struct gem_control_data),
21142c1b001SThomas Moestl 	    gem_cddma_callback, sc, 0)) != 0 || sc->sc_cddma == 0) {
2122a79fd39SMarius Strobl 		device_printf(sc->sc_dev,
2132a79fd39SMarius Strobl 		    "unable to load control data DMA map, error = %d\n",
2142a79fd39SMarius Strobl 		    error);
215305f2c06SThomas Moestl 		goto fail_cmem;
21642c1b001SThomas Moestl 	}
21742c1b001SThomas Moestl 
21842c1b001SThomas Moestl 	/*
21942c1b001SThomas Moestl 	 * Initialize the transmit job descriptors.
22042c1b001SThomas Moestl 	 */
22142c1b001SThomas Moestl 	STAILQ_INIT(&sc->sc_txfreeq);
22242c1b001SThomas Moestl 	STAILQ_INIT(&sc->sc_txdirtyq);
22342c1b001SThomas Moestl 
22442c1b001SThomas Moestl 	/*
22542c1b001SThomas Moestl 	 * Create the transmit buffer DMA maps.
22642c1b001SThomas Moestl 	 */
22742c1b001SThomas Moestl 	error = ENOMEM;
22842c1b001SThomas Moestl 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
22942c1b001SThomas Moestl 		txs = &sc->sc_txsoft[i];
23042c1b001SThomas Moestl 		txs->txs_mbuf = NULL;
23142c1b001SThomas Moestl 		txs->txs_ndescs = 0;
232305f2c06SThomas Moestl 		if ((error = bus_dmamap_create(sc->sc_tdmatag, 0,
23342c1b001SThomas Moestl 		    &txs->txs_dmamap)) != 0) {
2342a79fd39SMarius Strobl 			device_printf(sc->sc_dev,
2352a79fd39SMarius Strobl 			    "unable to create TX DMA map %d, error = %d\n",
2362a79fd39SMarius Strobl 			    i, error);
237305f2c06SThomas Moestl 			goto fail_txd;
23842c1b001SThomas Moestl 		}
23942c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
24042c1b001SThomas Moestl 	}
24142c1b001SThomas Moestl 
24242c1b001SThomas Moestl 	/*
24342c1b001SThomas Moestl 	 * Create the receive buffer DMA maps.
24442c1b001SThomas Moestl 	 */
24542c1b001SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
246305f2c06SThomas Moestl 		if ((error = bus_dmamap_create(sc->sc_rdmatag, 0,
24742c1b001SThomas Moestl 		    &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
2482a79fd39SMarius Strobl 			device_printf(sc->sc_dev,
2492a79fd39SMarius Strobl 			    "unable to create RX DMA map %d, error = %d\n",
2502a79fd39SMarius Strobl 			    i, error);
251305f2c06SThomas Moestl 			goto fail_rxd;
25242c1b001SThomas Moestl 		}
25342c1b001SThomas Moestl 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
25442c1b001SThomas Moestl 	}
25542c1b001SThomas Moestl 
2561ed3fed7SMarius Strobl 	/* Bad things will happen when touching this register on ERI. */
2571ed3fed7SMarius Strobl 	if (sc->sc_variant != GEM_SUN_ERI)
258bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MII_DATAPATH_MODE,
2591ed3fed7SMarius Strobl 		    GEM_MII_DATAPATH_MII);
2601ed3fed7SMarius Strobl 
26142c1b001SThomas Moestl 	gem_mifinit(sc);
26242c1b001SThomas Moestl 
2631ed3fed7SMarius Strobl 	/*
2641ed3fed7SMarius Strobl 	 * Look for an external PHY.
2651ed3fed7SMarius Strobl 	 */
2661ed3fed7SMarius Strobl 	error = ENXIO;
267bd3d9826SMarius Strobl 	v = GEM_BANK1_READ_4(sc, GEM_MIF_CONFIG);
2681ed3fed7SMarius Strobl 	if ((v & GEM_MIF_CONFIG_MDI1) != 0) {
2691ed3fed7SMarius Strobl 		v |= GEM_MIF_CONFIG_PHY_SEL;
270bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MIF_CONFIG, v);
2711ed3fed7SMarius Strobl 		switch (sc->sc_variant) {
2721ed3fed7SMarius Strobl 		case GEM_SUN_ERI:
2731ed3fed7SMarius Strobl 			sc->sc_phyad = GEM_PHYAD_EXTERNAL;
2741ed3fed7SMarius Strobl 			break;
2751ed3fed7SMarius Strobl 		default:
2761ed3fed7SMarius Strobl 			sc->sc_phyad = -1;
2771ed3fed7SMarius Strobl 			break;
2781ed3fed7SMarius Strobl 		}
2791ed3fed7SMarius Strobl 		error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus,
2801ed3fed7SMarius Strobl 		    gem_mediachange, gem_mediastatus);
2811ed3fed7SMarius Strobl 	}
2821ed3fed7SMarius Strobl 
2831ed3fed7SMarius Strobl 	/*
2841ed3fed7SMarius Strobl 	 * Fall back on an internal PHY if no external PHY was found.
2851ed3fed7SMarius Strobl 	 */
2861ed3fed7SMarius Strobl 	if (error != 0 && (v & GEM_MIF_CONFIG_MDI0) != 0) {
2871ed3fed7SMarius Strobl 		v &= ~GEM_MIF_CONFIG_PHY_SEL;
288bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MIF_CONFIG, v);
2891ed3fed7SMarius Strobl 		switch (sc->sc_variant) {
2901ed3fed7SMarius Strobl 		case GEM_SUN_ERI:
2911ed3fed7SMarius Strobl 		case GEM_APPLE_K2_GMAC:
2921ed3fed7SMarius Strobl 			sc->sc_phyad = GEM_PHYAD_INTERNAL;
2931ed3fed7SMarius Strobl 			break;
2941ed3fed7SMarius Strobl 		case GEM_APPLE_GMAC:
2951ed3fed7SMarius Strobl 			sc->sc_phyad = GEM_PHYAD_EXTERNAL;
2961ed3fed7SMarius Strobl 			break;
2971ed3fed7SMarius Strobl 		default:
2981ed3fed7SMarius Strobl 			sc->sc_phyad = -1;
2991ed3fed7SMarius Strobl 			break;
3001ed3fed7SMarius Strobl 		}
3011ed3fed7SMarius Strobl 		error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus,
3021ed3fed7SMarius Strobl 		    gem_mediachange, gem_mediastatus);
3031ed3fed7SMarius Strobl 	}
3041ed3fed7SMarius Strobl 
3051ed3fed7SMarius Strobl 	/*
3061ed3fed7SMarius Strobl 	 * Try the external PCS SERDES if we didn't find any PHYs.
3071ed3fed7SMarius Strobl 	 */
3081ed3fed7SMarius Strobl 	if (error != 0 && sc->sc_variant == GEM_SUN_GEM) {
309bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MII_DATAPATH_MODE,
3101ed3fed7SMarius Strobl 		    GEM_MII_DATAPATH_SERDES);
311bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MII_SLINK_CONTROL,
3121ed3fed7SMarius Strobl 		    GEM_MII_SLINK_LOOPBACK | GEM_MII_SLINK_EN_SYNC_D);
313bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG, GEM_MII_CONFIG_ENABLE);
3141ed3fed7SMarius Strobl 		sc->sc_flags |= GEM_SERDES;
3151ed3fed7SMarius Strobl 		sc->sc_phyad = GEM_PHYAD_EXTERNAL;
3161ed3fed7SMarius Strobl 		error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus,
3171ed3fed7SMarius Strobl 		    gem_mediachange, gem_mediastatus);
3181ed3fed7SMarius Strobl 	}
3191ed3fed7SMarius Strobl 
3201ed3fed7SMarius Strobl 	if (error != 0) {
3211ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "PHY probe failed: %d\n", error);
322305f2c06SThomas Moestl 		goto fail_rxd;
32342c1b001SThomas Moestl 	}
32442c1b001SThomas Moestl 	sc->sc_mii = device_get_softc(sc->sc_miibus);
32542c1b001SThomas Moestl 
32642c1b001SThomas Moestl 	/*
32742c1b001SThomas Moestl 	 * From this point forward, the attachment cannot fail.  A failure
32842c1b001SThomas Moestl 	 * before this point releases all resources that may have been
32942c1b001SThomas Moestl 	 * allocated.
33042c1b001SThomas Moestl 	 */
33142c1b001SThomas Moestl 
332801772ecSMarius Strobl 	/* Get RX FIFO size. */
333336cca9eSBenno Rice 	sc->sc_rxfifosize = 64 *
334bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_RX_FIFO_SIZE);
335336cca9eSBenno Rice 
336801772ecSMarius Strobl 	/* Get TX FIFO size. */
337bd3d9826SMarius Strobl 	v = GEM_BANK1_READ_4(sc, GEM_TX_FIFO_SIZE);
3383a5aee5aSThomas Moestl 	device_printf(sc->sc_dev, "%ukB RX FIFO, %ukB TX FIFO\n",
3393a5aee5aSThomas Moestl 	    sc->sc_rxfifosize / 1024, v / 16);
34042c1b001SThomas Moestl 
34112fb0330SPyun YongHyeon 	sc->sc_csum_features = GEM_CSUM_FEATURES;
34242c1b001SThomas Moestl 	/* Initialize ifnet structure. */
34342c1b001SThomas Moestl 	ifp->if_softc = sc;
3449bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(sc->sc_dev),
3459bf40edeSBrooks Davis 	    device_get_unit(sc->sc_dev));
3468cfaff7dSMarius Strobl 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
34742c1b001SThomas Moestl 	ifp->if_start = gem_start;
34842c1b001SThomas Moestl 	ifp->if_ioctl = gem_ioctl;
34942c1b001SThomas Moestl 	ifp->if_init = gem_init;
35012fb0330SPyun YongHyeon 	IFQ_SET_MAXLEN(&ifp->if_snd, GEM_TXQUEUELEN);
35112fb0330SPyun YongHyeon 	ifp->if_snd.ifq_drv_maxlen = GEM_TXQUEUELEN;
35212fb0330SPyun YongHyeon 	IFQ_SET_READY(&ifp->if_snd);
35342c1b001SThomas Moestl 
35442c1b001SThomas Moestl 	/* Attach the interface. */
355fc74a9f9SBrooks Davis 	ether_ifattach(ifp, sc->sc_enaddr);
35642c1b001SThomas Moestl 
35700d12766SMarius Strobl 	/*
35812fb0330SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames/checksum offloads.
35900d12766SMarius Strobl 	 */
36000d12766SMarius Strobl 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
36112fb0330SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_HWCSUM;
36212fb0330SPyun YongHyeon 	ifp->if_hwassist |= sc->sc_csum_features;
36312fb0330SPyun YongHyeon 	ifp->if_capenable |= IFCAP_VLAN_MTU | IFCAP_HWCSUM;
36400d12766SMarius Strobl 
36542c1b001SThomas Moestl 	return (0);
36642c1b001SThomas Moestl 
36742c1b001SThomas Moestl 	/*
36842c1b001SThomas Moestl 	 * Free any resources we've allocated during the failed attach
36942c1b001SThomas Moestl 	 * attempt.  Do this in reverse order and fall through.
37042c1b001SThomas Moestl 	 */
371305f2c06SThomas Moestl  fail_rxd:
3722a79fd39SMarius Strobl 	for (i = 0; i < GEM_NRXDESC; i++)
37342c1b001SThomas Moestl 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
374305f2c06SThomas Moestl 			bus_dmamap_destroy(sc->sc_rdmatag,
37542c1b001SThomas Moestl 			    sc->sc_rxsoft[i].rxs_dmamap);
376305f2c06SThomas Moestl  fail_txd:
3772a79fd39SMarius Strobl 	for (i = 0; i < GEM_TXQUEUELEN; i++)
37842c1b001SThomas Moestl 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
379305f2c06SThomas Moestl 			bus_dmamap_destroy(sc->sc_tdmatag,
38042c1b001SThomas Moestl 			    sc->sc_txsoft[i].txs_dmamap);
381305f2c06SThomas Moestl 	bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap);
382305f2c06SThomas Moestl  fail_cmem:
38342c1b001SThomas Moestl 	bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data,
38442c1b001SThomas Moestl 	    sc->sc_cddmamap);
385305f2c06SThomas Moestl  fail_ctag:
38642c1b001SThomas Moestl 	bus_dma_tag_destroy(sc->sc_cdmatag);
387305f2c06SThomas Moestl  fail_ttag:
388305f2c06SThomas Moestl 	bus_dma_tag_destroy(sc->sc_tdmatag);
389305f2c06SThomas Moestl  fail_rtag:
390305f2c06SThomas Moestl 	bus_dma_tag_destroy(sc->sc_rdmatag);
391305f2c06SThomas Moestl  fail_ptag:
39242c1b001SThomas Moestl 	bus_dma_tag_destroy(sc->sc_pdmatag);
393fc74a9f9SBrooks Davis  fail_ifnet:
394fc74a9f9SBrooks Davis 	if_free(ifp);
39542c1b001SThomas Moestl 	return (error);
39642c1b001SThomas Moestl }
39742c1b001SThomas Moestl 
398cbbdf236SThomas Moestl void
3992a79fd39SMarius Strobl gem_detach(struct gem_softc *sc)
400cbbdf236SThomas Moestl {
401fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
402cbbdf236SThomas Moestl 	int i;
403cbbdf236SThomas Moestl 
4048cfaff7dSMarius Strobl 	GEM_LOCK(sc);
40525bd46d0SBrooks Davis 	gem_stop(ifp, 1);
4068cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
4071f317bf9SMarius Strobl 	callout_drain(&sc->sc_tick_ch);
4081f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT
4091f317bf9SMarius Strobl 	callout_drain(&sc->sc_rx_ch);
4101f317bf9SMarius Strobl #endif
411cbbdf236SThomas Moestl 	ether_ifdetach(ifp);
412fc74a9f9SBrooks Davis 	if_free(ifp);
413cbbdf236SThomas Moestl 	device_delete_child(sc->sc_dev, sc->sc_miibus);
414cbbdf236SThomas Moestl 
4152a79fd39SMarius Strobl 	for (i = 0; i < GEM_NRXDESC; i++)
416cbbdf236SThomas Moestl 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
417cbbdf236SThomas Moestl 			bus_dmamap_destroy(sc->sc_rdmatag,
418cbbdf236SThomas Moestl 			    sc->sc_rxsoft[i].rxs_dmamap);
4192a79fd39SMarius Strobl 	for (i = 0; i < GEM_TXQUEUELEN; i++)
420cbbdf236SThomas Moestl 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
421cbbdf236SThomas Moestl 			bus_dmamap_destroy(sc->sc_tdmatag,
422cbbdf236SThomas Moestl 			    sc->sc_txsoft[i].txs_dmamap);
423ccb1212aSMarius Strobl 	GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
424cbbdf236SThomas Moestl 	bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap);
425cbbdf236SThomas Moestl 	bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data,
426cbbdf236SThomas Moestl 	    sc->sc_cddmamap);
427cbbdf236SThomas Moestl 	bus_dma_tag_destroy(sc->sc_cdmatag);
428cbbdf236SThomas Moestl 	bus_dma_tag_destroy(sc->sc_tdmatag);
429cbbdf236SThomas Moestl 	bus_dma_tag_destroy(sc->sc_rdmatag);
430cbbdf236SThomas Moestl 	bus_dma_tag_destroy(sc->sc_pdmatag);
431cbbdf236SThomas Moestl }
432cbbdf236SThomas Moestl 
433cbbdf236SThomas Moestl void
4342a79fd39SMarius Strobl gem_suspend(struct gem_softc *sc)
435cbbdf236SThomas Moestl {
436fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
437cbbdf236SThomas Moestl 
4388cfaff7dSMarius Strobl 	GEM_LOCK(sc);
439cbbdf236SThomas Moestl 	gem_stop(ifp, 0);
4408cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
441cbbdf236SThomas Moestl }
442cbbdf236SThomas Moestl 
443cbbdf236SThomas Moestl void
4442a79fd39SMarius Strobl gem_resume(struct gem_softc *sc)
445cbbdf236SThomas Moestl {
446fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
447cbbdf236SThomas Moestl 
4488cfaff7dSMarius Strobl 	GEM_LOCK(sc);
44900d12766SMarius Strobl 	/*
45000d12766SMarius Strobl 	 * On resume all registers have to be initialized again like
45100d12766SMarius Strobl 	 * after power-on.
45200d12766SMarius Strobl 	 */
4531ed3fed7SMarius Strobl 	sc->sc_flags &= ~GEM_INITED;
454cbbdf236SThomas Moestl 	if (ifp->if_flags & IFF_UP)
4558cfaff7dSMarius Strobl 		gem_init_locked(sc);
4568cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
457cbbdf236SThomas Moestl }
458cbbdf236SThomas Moestl 
45912fb0330SPyun YongHyeon static __inline void
46012fb0330SPyun YongHyeon gem_rxcksum(struct mbuf *m, uint64_t flags)
46112fb0330SPyun YongHyeon {
46212fb0330SPyun YongHyeon 	struct ether_header *eh;
46312fb0330SPyun YongHyeon 	struct ip *ip;
46412fb0330SPyun YongHyeon 	struct udphdr *uh;
4652a79fd39SMarius Strobl 	uint16_t *opts;
46612fb0330SPyun YongHyeon 	int32_t hlen, len, pktlen;
46712fb0330SPyun YongHyeon 	uint32_t temp32;
4682a79fd39SMarius Strobl 	uint16_t cksum;
46912fb0330SPyun YongHyeon 
47012fb0330SPyun YongHyeon 	pktlen = m->m_pkthdr.len;
47112fb0330SPyun YongHyeon 	if (pktlen < sizeof(struct ether_header) + sizeof(struct ip))
47212fb0330SPyun YongHyeon 		return;
47312fb0330SPyun YongHyeon 	eh = mtod(m, struct ether_header *);
47412fb0330SPyun YongHyeon 	if (eh->ether_type != htons(ETHERTYPE_IP))
47512fb0330SPyun YongHyeon 		return;
47612fb0330SPyun YongHyeon 	ip = (struct ip *)(eh + 1);
47712fb0330SPyun YongHyeon 	if (ip->ip_v != IPVERSION)
47812fb0330SPyun YongHyeon 		return;
47912fb0330SPyun YongHyeon 
48012fb0330SPyun YongHyeon 	hlen = ip->ip_hl << 2;
48112fb0330SPyun YongHyeon 	pktlen -= sizeof(struct ether_header);
48212fb0330SPyun YongHyeon 	if (hlen < sizeof(struct ip))
48312fb0330SPyun YongHyeon 		return;
48412fb0330SPyun YongHyeon 	if (ntohs(ip->ip_len) < hlen)
48512fb0330SPyun YongHyeon 		return;
48612fb0330SPyun YongHyeon 	if (ntohs(ip->ip_len) != pktlen)
48712fb0330SPyun YongHyeon 		return;
48812fb0330SPyun YongHyeon 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
4892a79fd39SMarius Strobl 		return;	/* Cannot handle fragmented packet. */
49012fb0330SPyun YongHyeon 
49112fb0330SPyun YongHyeon 	switch (ip->ip_p) {
49212fb0330SPyun YongHyeon 	case IPPROTO_TCP:
49312fb0330SPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct tcphdr)))
49412fb0330SPyun YongHyeon 			return;
49512fb0330SPyun YongHyeon 		break;
49612fb0330SPyun YongHyeon 	case IPPROTO_UDP:
49712fb0330SPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct udphdr)))
49812fb0330SPyun YongHyeon 			return;
49912fb0330SPyun YongHyeon 		uh = (struct udphdr *)((uint8_t *)ip + hlen);
50012fb0330SPyun YongHyeon 		if (uh->uh_sum == 0)
50112fb0330SPyun YongHyeon 			return; /* no checksum */
50212fb0330SPyun YongHyeon 		break;
50312fb0330SPyun YongHyeon 	default:
50412fb0330SPyun YongHyeon 		return;
50512fb0330SPyun YongHyeon 	}
50612fb0330SPyun YongHyeon 
50712fb0330SPyun YongHyeon 	cksum = ~(flags & GEM_RD_CHECKSUM);
50812fb0330SPyun YongHyeon 	/* checksum fixup for IP options */
50912fb0330SPyun YongHyeon 	len = hlen - sizeof(struct ip);
51012fb0330SPyun YongHyeon 	if (len > 0) {
51112fb0330SPyun YongHyeon 		opts = (uint16_t *)(ip + 1);
51212fb0330SPyun YongHyeon 		for (; len > 0; len -= sizeof(uint16_t), opts++) {
51312fb0330SPyun YongHyeon 			temp32 = cksum - *opts;
51412fb0330SPyun YongHyeon 			temp32 = (temp32 >> 16) + (temp32 & 65535);
51512fb0330SPyun YongHyeon 			cksum = temp32 & 65535;
51612fb0330SPyun YongHyeon 		}
51712fb0330SPyun YongHyeon 	}
51812fb0330SPyun YongHyeon 	m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
51912fb0330SPyun YongHyeon 	m->m_pkthdr.csum_data = cksum;
52012fb0330SPyun YongHyeon }
52112fb0330SPyun YongHyeon 
52242c1b001SThomas Moestl static void
5232a79fd39SMarius Strobl gem_cddma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
52442c1b001SThomas Moestl {
5252a79fd39SMarius Strobl 	struct gem_softc *sc = xsc;
52642c1b001SThomas Moestl 
52742c1b001SThomas Moestl 	if (error != 0)
52842c1b001SThomas Moestl 		return;
5292a79fd39SMarius Strobl 	if (nsegs != 1)
5301ed3fed7SMarius Strobl 		panic("%s: bad control buffer segment count", __func__);
53142c1b001SThomas Moestl 	sc->sc_cddma = segs[0].ds_addr;
53242c1b001SThomas Moestl }
53342c1b001SThomas Moestl 
53442c1b001SThomas Moestl static void
5352a79fd39SMarius Strobl gem_tick(void *arg)
53642c1b001SThomas Moestl {
53742c1b001SThomas Moestl 	struct gem_softc *sc = arg;
53812fb0330SPyun YongHyeon 	struct ifnet *ifp;
53978d22f42SMarius Strobl 	uint32_t v;
54042c1b001SThomas Moestl 
5411f317bf9SMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
54212fb0330SPyun YongHyeon 
54312fb0330SPyun YongHyeon 	ifp = sc->sc_ifp;
54412fb0330SPyun YongHyeon 	/*
54578d22f42SMarius Strobl 	 * Unload collision and error counters.
54612fb0330SPyun YongHyeon 	 */
54712fb0330SPyun YongHyeon 	ifp->if_collisions +=
548bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_NORM_COLL_CNT) +
54978d22f42SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_FIRST_COLL_CNT);
55078d22f42SMarius Strobl 	v = GEM_BANK1_READ_4(sc, GEM_MAC_EXCESS_COLL_CNT) +
551bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_LATE_COLL_CNT);
55278d22f42SMarius Strobl 	ifp->if_collisions += v;
55378d22f42SMarius Strobl 	ifp->if_oerrors += v;
55478d22f42SMarius Strobl 	ifp->if_ierrors +=
55578d22f42SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_LEN_ERR_CNT) +
55678d22f42SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_ALIGN_ERR) +
55778d22f42SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_CRC_ERR_CNT) +
55878d22f42SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_CODE_VIOL);
55912fb0330SPyun YongHyeon 
56012fb0330SPyun YongHyeon 	/*
561801772ecSMarius Strobl 	 * Then clear the hardware counters.
56212fb0330SPyun YongHyeon 	 */
563bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_NORM_COLL_CNT, 0);
564bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_FIRST_COLL_CNT, 0);
565bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_EXCESS_COLL_CNT, 0);
566bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_LATE_COLL_CNT, 0);
56778d22f42SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_LEN_ERR_CNT, 0);
56878d22f42SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_ALIGN_ERR, 0);
56978d22f42SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CRC_ERR_CNT, 0);
57078d22f42SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CODE_VIOL, 0);
57112fb0330SPyun YongHyeon 
57242c1b001SThomas Moestl 	mii_tick(sc->sc_mii);
57342c1b001SThomas Moestl 
5748cb37876SMarius Strobl 	if (gem_watchdog(sc) == EJUSTRETURN)
5758cb37876SMarius Strobl 		return;
5768cb37876SMarius Strobl 
57742c1b001SThomas Moestl 	callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
57842c1b001SThomas Moestl }
57942c1b001SThomas Moestl 
58042c1b001SThomas Moestl static int
581bd3d9826SMarius Strobl gem_bitwait(struct gem_softc *sc, u_int bank, bus_addr_t r, uint32_t clr,
582bd3d9826SMarius Strobl     uint32_t set)
58342c1b001SThomas Moestl {
58442c1b001SThomas Moestl 	int i;
5852a79fd39SMarius Strobl 	uint32_t reg;
58642c1b001SThomas Moestl 
58742c1b001SThomas Moestl 	for (i = TRIES; i--; DELAY(100)) {
588bd3d9826SMarius Strobl 		reg = GEM_BANKN_READ_M(bank, 4, sc, r);
589e87137e1SMarius Strobl 		if ((reg & clr) == 0 && (reg & set) == set)
59042c1b001SThomas Moestl 			return (1);
59142c1b001SThomas Moestl 	}
59242c1b001SThomas Moestl 	return (0);
59342c1b001SThomas Moestl }
59442c1b001SThomas Moestl 
5951ed3fed7SMarius Strobl static void
59642c1b001SThomas Moestl gem_reset(sc)
59742c1b001SThomas Moestl 	struct gem_softc *sc;
59842c1b001SThomas Moestl {
59942c1b001SThomas Moestl 
60018100346SThomas Moestl #ifdef GEM_DEBUG
60112fb0330SPyun YongHyeon 	CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__);
60218100346SThomas Moestl #endif
60342c1b001SThomas Moestl 	gem_reset_rx(sc);
60442c1b001SThomas Moestl 	gem_reset_tx(sc);
60542c1b001SThomas Moestl 
6062a79fd39SMarius Strobl 	/* Do a full reset. */
607bd3d9826SMarius Strobl 	GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX);
608ccb1212aSMarius Strobl 	GEM_BANK2_BARRIER(sc, GEM_RESET, 4,
609ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
610bd3d9826SMarius Strobl 	if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0))
61142c1b001SThomas Moestl 		device_printf(sc->sc_dev, "cannot reset device\n");
61242c1b001SThomas Moestl }
61342c1b001SThomas Moestl 
61442c1b001SThomas Moestl static void
6152a79fd39SMarius Strobl gem_rxdrain(struct gem_softc *sc)
61642c1b001SThomas Moestl {
61742c1b001SThomas Moestl 	struct gem_rxsoft *rxs;
61842c1b001SThomas Moestl 	int i;
61942c1b001SThomas Moestl 
62042c1b001SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
62142c1b001SThomas Moestl 		rxs = &sc->sc_rxsoft[i];
62242c1b001SThomas Moestl 		if (rxs->rxs_mbuf != NULL) {
623b2d59f42SThomas Moestl 			bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap,
624b2d59f42SThomas Moestl 			    BUS_DMASYNC_POSTREAD);
625305f2c06SThomas Moestl 			bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap);
62642c1b001SThomas Moestl 			m_freem(rxs->rxs_mbuf);
62742c1b001SThomas Moestl 			rxs->rxs_mbuf = NULL;
62842c1b001SThomas Moestl 		}
62942c1b001SThomas Moestl 	}
63042c1b001SThomas Moestl }
63142c1b001SThomas Moestl 
63242c1b001SThomas Moestl static void
6332a79fd39SMarius Strobl gem_stop(struct ifnet *ifp, int disable)
63442c1b001SThomas Moestl {
6352a79fd39SMarius Strobl 	struct gem_softc *sc = ifp->if_softc;
63642c1b001SThomas Moestl 	struct gem_txsoft *txs;
63742c1b001SThomas Moestl 
63818100346SThomas Moestl #ifdef GEM_DEBUG
63912fb0330SPyun YongHyeon 	CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__);
64018100346SThomas Moestl #endif
64142c1b001SThomas Moestl 
64242c1b001SThomas Moestl 	callout_stop(&sc->sc_tick_ch);
6431f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT
6441f317bf9SMarius Strobl 	callout_stop(&sc->sc_rx_ch);
6451f317bf9SMarius Strobl #endif
64642c1b001SThomas Moestl 
6472a79fd39SMarius Strobl 	/* XXX should we reset these instead? */
64842c1b001SThomas Moestl 	gem_disable_tx(sc);
64942c1b001SThomas Moestl 	gem_disable_rx(sc);
65042c1b001SThomas Moestl 
65142c1b001SThomas Moestl 	/*
65242c1b001SThomas Moestl 	 * Release any queued transmit buffers.
65342c1b001SThomas Moestl 	 */
65442c1b001SThomas Moestl 	while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
65542c1b001SThomas Moestl 		STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
65642c1b001SThomas Moestl 		if (txs->txs_ndescs != 0) {
657b2d59f42SThomas Moestl 			bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap,
658b2d59f42SThomas Moestl 			    BUS_DMASYNC_POSTWRITE);
659305f2c06SThomas Moestl 			bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap);
66042c1b001SThomas Moestl 			if (txs->txs_mbuf != NULL) {
66142c1b001SThomas Moestl 				m_freem(txs->txs_mbuf);
66242c1b001SThomas Moestl 				txs->txs_mbuf = NULL;
66342c1b001SThomas Moestl 			}
66442c1b001SThomas Moestl 		}
66542c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
66642c1b001SThomas Moestl 	}
66742c1b001SThomas Moestl 
66842c1b001SThomas Moestl 	if (disable)
66942c1b001SThomas Moestl 		gem_rxdrain(sc);
67042c1b001SThomas Moestl 
67142c1b001SThomas Moestl 	/*
67242c1b001SThomas Moestl 	 * Mark the interface down and cancel the watchdog timer.
67342c1b001SThomas Moestl 	 */
67413f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
6751ed3fed7SMarius Strobl 	sc->sc_flags &= ~GEM_LINK;
6768cb37876SMarius Strobl 	sc->sc_wdog_timer = 0;
67742c1b001SThomas Moestl }
67842c1b001SThomas Moestl 
6791ed3fed7SMarius Strobl static int
6802a79fd39SMarius Strobl gem_reset_rx(struct gem_softc *sc)
68142c1b001SThomas Moestl {
68242c1b001SThomas Moestl 
68342c1b001SThomas Moestl 	/*
68442c1b001SThomas Moestl 	 * Resetting while DMA is in progress can cause a bus hang, so we
68542c1b001SThomas Moestl 	 * disable DMA first.
68642c1b001SThomas Moestl 	 */
68742c1b001SThomas Moestl 	gem_disable_rx(sc);
688bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG, 0);
689ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_RX_CONFIG, 4,
690ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
691bd3d9826SMarius Strobl 	if (!GEM_BANK1_BITWAIT(sc, GEM_RX_CONFIG, GEM_RX_CONFIG_RXDMA_EN, 0))
6921ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "cannot disable RX DMA\n");
69342c1b001SThomas Moestl 
694801772ecSMarius Strobl 	/* Finally, reset the ERX. */
695bd3d9826SMarius Strobl 	GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_RX);
696ccb1212aSMarius Strobl 	GEM_BANK2_BARRIER(sc, GEM_RESET, 4,
697ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
698bd3d9826SMarius Strobl 	if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX,
699bd3d9826SMarius Strobl 	    0)) {
70042c1b001SThomas Moestl 		device_printf(sc->sc_dev, "cannot reset receiver\n");
70142c1b001SThomas Moestl 		return (1);
70242c1b001SThomas Moestl 	}
70342c1b001SThomas Moestl 	return (0);
70442c1b001SThomas Moestl }
70542c1b001SThomas Moestl 
7061ed3fed7SMarius Strobl /*
7071ed3fed7SMarius Strobl  * Reset the receiver DMA engine.
7081ed3fed7SMarius Strobl  *
7091ed3fed7SMarius Strobl  * Intended to be used in case of GEM_INTR_RX_TAG_ERR, GEM_MAC_RX_OVERFLOW
7101ed3fed7SMarius Strobl  * etc in order to reset the receiver DMA engine only and not do a full
7111ed3fed7SMarius Strobl  * reset which amongst others also downs the link and clears the FIFOs.
7121ed3fed7SMarius Strobl  */
7131ed3fed7SMarius Strobl static void
7141ed3fed7SMarius Strobl gem_reset_rxdma(struct gem_softc *sc)
7151ed3fed7SMarius Strobl {
7161ed3fed7SMarius Strobl 	int i;
7171ed3fed7SMarius Strobl 
7181ed3fed7SMarius Strobl 	if (gem_reset_rx(sc) != 0)
7191ed3fed7SMarius Strobl 		return (gem_init_locked(sc));
7201ed3fed7SMarius Strobl 	for (i = 0; i < GEM_NRXDESC; i++)
7211ed3fed7SMarius Strobl 		if (sc->sc_rxsoft[i].rxs_mbuf != NULL)
7221ed3fed7SMarius Strobl 			GEM_UPDATE_RXDESC(sc, i);
7231ed3fed7SMarius Strobl 	sc->sc_rxptr = 0;
724ccb1212aSMarius Strobl 	GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
7251ed3fed7SMarius Strobl 
7261ed3fed7SMarius Strobl 	/* NOTE: we use only 32-bit DMA addresses here. */
727bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_HI, 0);
728bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0));
729bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_KICK, GEM_NRXDESC - 4);
730bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG,
7311ed3fed7SMarius Strobl 	    gem_ringsize(GEM_NRXDESC /* XXX */) |
7321ed3fed7SMarius Strobl 	    ((ETHER_HDR_LEN + sizeof(struct ip)) <<
7331ed3fed7SMarius Strobl 	    GEM_RX_CONFIG_CXM_START_SHFT) |
7341ed3fed7SMarius Strobl 	    (GEM_THRSH_1024 << GEM_RX_CONFIG_FIFO_THRS_SHIFT) |
7351ed3fed7SMarius Strobl 	    (2 << GEM_RX_CONFIG_FBOFF_SHFT));
736bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_BLANKING,
7371ed3fed7SMarius Strobl 	    (6 << GEM_RX_BLANKING_TIME_SHIFT) | 6);
738bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_PAUSE_THRESH,
7392a79fd39SMarius Strobl 	    (3 * sc->sc_rxfifosize / 256) |
7402a79fd39SMarius Strobl 	    ((sc->sc_rxfifosize / 256) << 12));
741bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG,
742bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_RX_CONFIG) | GEM_RX_CONFIG_RXDMA_EN);
743bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_MASK,
7441ed3fed7SMarius Strobl 	    GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT);
745bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG,
746bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG) | GEM_MAC_RX_ENABLE);
7471ed3fed7SMarius Strobl }
74842c1b001SThomas Moestl 
74942c1b001SThomas Moestl static int
7502a79fd39SMarius Strobl gem_reset_tx(struct gem_softc *sc)
75142c1b001SThomas Moestl {
75242c1b001SThomas Moestl 
75342c1b001SThomas Moestl 	/*
75442c1b001SThomas Moestl 	 * Resetting while DMA is in progress can cause a bus hang, so we
75542c1b001SThomas Moestl 	 * disable DMA first.
75642c1b001SThomas Moestl 	 */
75742c1b001SThomas Moestl 	gem_disable_tx(sc);
758bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_TX_CONFIG, 0);
759ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_TX_CONFIG, 4,
760ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
761bd3d9826SMarius Strobl 	if (!GEM_BANK1_BITWAIT(sc, GEM_TX_CONFIG, GEM_TX_CONFIG_TXDMA_EN, 0))
7621ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "cannot disable TX DMA\n");
76342c1b001SThomas Moestl 
764801772ecSMarius Strobl 	/* Finally, reset the ETX. */
765bd3d9826SMarius Strobl 	GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_TX);
766ccb1212aSMarius Strobl 	GEM_BANK2_BARRIER(sc, GEM_RESET, 4,
767ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
768bd3d9826SMarius Strobl 	if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX,
769bd3d9826SMarius Strobl 	    0)) {
7701ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "cannot reset transmitter\n");
77142c1b001SThomas Moestl 		return (1);
77242c1b001SThomas Moestl 	}
77342c1b001SThomas Moestl 	return (0);
77442c1b001SThomas Moestl }
77542c1b001SThomas Moestl 
77642c1b001SThomas Moestl static int
7772a79fd39SMarius Strobl gem_disable_rx(struct gem_softc *sc)
77842c1b001SThomas Moestl {
77942c1b001SThomas Moestl 
780bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG,
781bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG) & ~GEM_MAC_RX_ENABLE);
782ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4,
783ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
784bd3d9826SMarius Strobl 	return (GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE,
785bd3d9826SMarius Strobl 	    0));
78642c1b001SThomas Moestl }
78742c1b001SThomas Moestl 
78842c1b001SThomas Moestl static int
7892a79fd39SMarius Strobl gem_disable_tx(struct gem_softc *sc)
79042c1b001SThomas Moestl {
79142c1b001SThomas Moestl 
792bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG,
793bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG) & ~GEM_MAC_TX_ENABLE);
794ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MAC_TX_CONFIG, 4,
795ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
796bd3d9826SMarius Strobl 	return (GEM_BANK1_BITWAIT(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE,
797bd3d9826SMarius Strobl 	    0));
79842c1b001SThomas Moestl }
79942c1b001SThomas Moestl 
80042c1b001SThomas Moestl static int
80142c1b001SThomas Moestl gem_meminit(sc)
80242c1b001SThomas Moestl 	struct gem_softc *sc;
80342c1b001SThomas Moestl {
80442c1b001SThomas Moestl 	struct gem_rxsoft *rxs;
8052a79fd39SMarius Strobl 	int error, i;
80642c1b001SThomas Moestl 
80742c1b001SThomas Moestl 	/*
80842c1b001SThomas Moestl 	 * Initialize the transmit descriptor ring.
80942c1b001SThomas Moestl 	 */
81042c1b001SThomas Moestl 	for (i = 0; i < GEM_NTXDESC; i++) {
81142c1b001SThomas Moestl 		sc->sc_txdescs[i].gd_flags = 0;
81242c1b001SThomas Moestl 		sc->sc_txdescs[i].gd_addr = 0;
81342c1b001SThomas Moestl 	}
814305f2c06SThomas Moestl 	sc->sc_txfree = GEM_MAXTXFREE;
81542c1b001SThomas Moestl 	sc->sc_txnext = 0;
816336cca9eSBenno Rice 	sc->sc_txwin = 0;
81742c1b001SThomas Moestl 
81842c1b001SThomas Moestl 	/*
81942c1b001SThomas Moestl 	 * Initialize the receive descriptor and receive job
82042c1b001SThomas Moestl 	 * descriptor rings.
82142c1b001SThomas Moestl 	 */
82242c1b001SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
82342c1b001SThomas Moestl 		rxs = &sc->sc_rxsoft[i];
82442c1b001SThomas Moestl 		if (rxs->rxs_mbuf == NULL) {
82542c1b001SThomas Moestl 			if ((error = gem_add_rxbuf(sc, i)) != 0) {
8262a79fd39SMarius Strobl 				device_printf(sc->sc_dev,
8272a79fd39SMarius Strobl 				    "unable to allocate or map RX buffer %d, "
8282a79fd39SMarius Strobl 				    "error = %d\n", i, error);
82942c1b001SThomas Moestl 				/*
8302a79fd39SMarius Strobl 				 * XXX we should attempt to run with fewer
8312a79fd39SMarius Strobl 				 * receive buffers instead of just failing.
83242c1b001SThomas Moestl 				 */
83342c1b001SThomas Moestl 				gem_rxdrain(sc);
83442c1b001SThomas Moestl 				return (1);
83542c1b001SThomas Moestl 			}
83642c1b001SThomas Moestl 		} else
83742c1b001SThomas Moestl 			GEM_INIT_RXDESC(sc, i);
83842c1b001SThomas Moestl 	}
83942c1b001SThomas Moestl 	sc->sc_rxptr = 0;
840ccb1212aSMarius Strobl 	GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
84142c1b001SThomas Moestl 
84242c1b001SThomas Moestl 	return (0);
84342c1b001SThomas Moestl }
84442c1b001SThomas Moestl 
8451ed3fed7SMarius Strobl static u_int
8462a79fd39SMarius Strobl gem_ringsize(u_int sz)
84742c1b001SThomas Moestl {
84842c1b001SThomas Moestl 
84942c1b001SThomas Moestl 	switch (sz) {
85042c1b001SThomas Moestl 	case 32:
8511ed3fed7SMarius Strobl 		return (GEM_RING_SZ_32);
85242c1b001SThomas Moestl 	case 64:
8531ed3fed7SMarius Strobl 		return (GEM_RING_SZ_64);
85442c1b001SThomas Moestl 	case 128:
8551ed3fed7SMarius Strobl 		return (GEM_RING_SZ_128);
85642c1b001SThomas Moestl 	case 256:
8571ed3fed7SMarius Strobl 		return (GEM_RING_SZ_256);
85842c1b001SThomas Moestl 	case 512:
8591ed3fed7SMarius Strobl 		return (GEM_RING_SZ_512);
86042c1b001SThomas Moestl 	case 1024:
8611ed3fed7SMarius Strobl 		return (GEM_RING_SZ_1024);
86242c1b001SThomas Moestl 	case 2048:
8631ed3fed7SMarius Strobl 		return (GEM_RING_SZ_2048);
86442c1b001SThomas Moestl 	case 4096:
8651ed3fed7SMarius Strobl 		return (GEM_RING_SZ_4096);
86642c1b001SThomas Moestl 	case 8192:
8671ed3fed7SMarius Strobl 		return (GEM_RING_SZ_8192);
86842c1b001SThomas Moestl 	default:
8691ed3fed7SMarius Strobl 		printf("%s: invalid ring size %d\n", __func__, sz);
8701ed3fed7SMarius Strobl 		return (GEM_RING_SZ_32);
87142c1b001SThomas Moestl 	}
87242c1b001SThomas Moestl }
87342c1b001SThomas Moestl 
87442c1b001SThomas Moestl static void
8752a79fd39SMarius Strobl gem_init(void *xsc)
87642c1b001SThomas Moestl {
8772a79fd39SMarius Strobl 	struct gem_softc *sc = xsc;
8788cfaff7dSMarius Strobl 
8798cfaff7dSMarius Strobl 	GEM_LOCK(sc);
8808cfaff7dSMarius Strobl 	gem_init_locked(sc);
8818cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
8828cfaff7dSMarius Strobl }
8838cfaff7dSMarius Strobl 
8848cfaff7dSMarius Strobl /*
8858cfaff7dSMarius Strobl  * Initialization of interface; set up initialization block
8868cfaff7dSMarius Strobl  * and transmit/receive descriptor rings.
8878cfaff7dSMarius Strobl  */
8888cfaff7dSMarius Strobl static void
8892a79fd39SMarius Strobl gem_init_locked(struct gem_softc *sc)
8908cfaff7dSMarius Strobl {
891fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
8922a79fd39SMarius Strobl 	uint32_t v;
89342c1b001SThomas Moestl 
8948cfaff7dSMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
89542c1b001SThomas Moestl 
89618100346SThomas Moestl #ifdef GEM_DEBUG
89712fb0330SPyun YongHyeon 	CTR2(KTR_GEM, "%s: %s: calling stop", device_get_name(sc->sc_dev),
89812fb0330SPyun YongHyeon 	    __func__);
89918100346SThomas Moestl #endif
90042c1b001SThomas Moestl 	/*
90142c1b001SThomas Moestl 	 * Initialization sequence.  The numbered steps below correspond
90242c1b001SThomas Moestl 	 * to the sequence outlined in section 6.3.5.1 in the Ethernet
90342c1b001SThomas Moestl 	 * Channel Engine manual (part of the PCIO manual).
90442c1b001SThomas Moestl 	 * See also the STP2002-STQ document from Sun Microsystems.
90542c1b001SThomas Moestl 	 */
90642c1b001SThomas Moestl 
9072a79fd39SMarius Strobl 	/* step 1 & 2.  Reset the Ethernet Channel. */
908ccb1212aSMarius Strobl 	gem_stop(ifp, 0);
90942c1b001SThomas Moestl 	gem_reset(sc);
91018100346SThomas Moestl #ifdef GEM_DEBUG
91112fb0330SPyun YongHyeon 	CTR2(KTR_GEM, "%s: %s: restarting", device_get_name(sc->sc_dev),
91212fb0330SPyun YongHyeon 	    __func__);
91318100346SThomas Moestl #endif
91442c1b001SThomas Moestl 
9152a79fd39SMarius Strobl 	/* Re-initialize the MIF. */
91642c1b001SThomas Moestl 	gem_mifinit(sc);
91742c1b001SThomas Moestl 
9182a79fd39SMarius Strobl 	/* step 3.  Setup data structures in host memory. */
9191ed3fed7SMarius Strobl 	if (gem_meminit(sc) != 0)
9201ed3fed7SMarius Strobl 		return;
92142c1b001SThomas Moestl 
92242c1b001SThomas Moestl 	/* step 4.  TX MAC registers & counters */
92342c1b001SThomas Moestl 	gem_init_regs(sc);
92442c1b001SThomas Moestl 
92542c1b001SThomas Moestl 	/* step 5.  RX MAC registers & counters */
92642c1b001SThomas Moestl 	gem_setladrf(sc);
92742c1b001SThomas Moestl 
9282a79fd39SMarius Strobl 	/* step 6 & 7.  Program Descriptor Ring Base Addresses. */
92942c1b001SThomas Moestl 	/* NOTE: we use only 32-bit DMA addresses here. */
930bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_TX_RING_PTR_HI, 0);
931bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0));
93242c1b001SThomas Moestl 
933bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_HI, 0);
934bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0));
93518100346SThomas Moestl #ifdef GEM_DEBUG
9362a79fd39SMarius Strobl 	CTR3(KTR_GEM, "loading RX ring %lx, TX ring %lx, cddma %lx",
93742c1b001SThomas Moestl 	    GEM_CDRXADDR(sc, 0), GEM_CDTXADDR(sc, 0), sc->sc_cddma);
93818100346SThomas Moestl #endif
93942c1b001SThomas Moestl 
94042c1b001SThomas Moestl 	/* step 8.  Global Configuration & Interrupt Mask */
941bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_INTMASK,
9421ed3fed7SMarius Strobl 	    ~(GEM_INTR_TX_INTME | GEM_INTR_TX_EMPTY | GEM_INTR_RX_DONE |
9431ed3fed7SMarius Strobl 	    GEM_INTR_RX_NOBUF | GEM_INTR_RX_TAG_ERR | GEM_INTR_PERR |
9441ed3fed7SMarius Strobl 	    GEM_INTR_BERR
9451ed3fed7SMarius Strobl #ifdef GEM_DEBUG
9461ed3fed7SMarius Strobl 	    | GEM_INTR_PCS | GEM_INTR_MIF
9471ed3fed7SMarius Strobl #endif
9481ed3fed7SMarius Strobl 	    ));
949bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_MASK,
950336cca9eSBenno Rice 	    GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT);
951bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_MASK,
9521ed3fed7SMarius Strobl 	    GEM_MAC_TX_XMIT_DONE | GEM_MAC_TX_DEFER_EXP);
9531ed3fed7SMarius Strobl #ifdef GEM_DEBUG
954bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_MASK,
9551ed3fed7SMarius Strobl 	    ~(GEM_MAC_PAUSED | GEM_MAC_PAUSE | GEM_MAC_RESUME));
9561ed3fed7SMarius Strobl #else
957bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_MASK,
9581ed3fed7SMarius Strobl 	    GEM_MAC_PAUSED | GEM_MAC_PAUSE | GEM_MAC_RESUME);
9591ed3fed7SMarius Strobl #endif
96042c1b001SThomas Moestl 
9612a79fd39SMarius Strobl 	/* step 9.  ETX Configuration: use mostly default values. */
96242c1b001SThomas Moestl 
9632a79fd39SMarius Strobl 	/* Enable DMA. */
96442c1b001SThomas Moestl 	v = gem_ringsize(GEM_NTXDESC /* XXX */);
965ccb1212aSMarius Strobl 	v |= ((sc->sc_variant == GEM_SUN_ERI ? 0x100 : 0x4ff) << 10) &
966ccb1212aSMarius Strobl 	    GEM_TX_CONFIG_TXFIFO_TH;
967ccb1212aSMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_TX_CONFIG, v | GEM_TX_CONFIG_TXDMA_EN);
96842c1b001SThomas Moestl 
96942c1b001SThomas Moestl 	/* step 10.  ERX Configuration */
97042c1b001SThomas Moestl 
9711ed3fed7SMarius Strobl 	/* Encode Receive Descriptor ring size. */
97242c1b001SThomas Moestl 	v = gem_ringsize(GEM_NRXDESC /* XXX */);
9732a79fd39SMarius Strobl 	/* RX TCP/UDP checksum offset */
97412fb0330SPyun YongHyeon 	v |= ((ETHER_HDR_LEN + sizeof(struct ip)) <<
97512fb0330SPyun YongHyeon 	    GEM_RX_CONFIG_CXM_START_SHFT);
97642c1b001SThomas Moestl 
9772a79fd39SMarius Strobl 	/* Enable DMA. */
978bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG,
97942c1b001SThomas Moestl 	    v | (GEM_THRSH_1024 << GEM_RX_CONFIG_FIFO_THRS_SHIFT) |
98012fb0330SPyun YongHyeon 	    (2 << GEM_RX_CONFIG_FBOFF_SHFT) | GEM_RX_CONFIG_RXDMA_EN);
9811ed3fed7SMarius Strobl 
982bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_BLANKING,
9831ed3fed7SMarius Strobl 	    (6 << GEM_RX_BLANKING_TIME_SHIFT) | 6);
9841ed3fed7SMarius Strobl 
98542c1b001SThomas Moestl 	/*
986336cca9eSBenno Rice 	 * The following value is for an OFF Threshold of about 3/4 full
987336cca9eSBenno Rice 	 * and an ON Threshold of 1/4 full.
98842c1b001SThomas Moestl 	 */
989bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_PAUSE_THRESH,
990336cca9eSBenno Rice 	    (3 * sc->sc_rxfifosize / 256) |
991336cca9eSBenno Rice 	    ((sc->sc_rxfifosize / 256) << 12));
99242c1b001SThomas Moestl 
9932a79fd39SMarius Strobl 	/* step 11.  Configure Media. */
99442c1b001SThomas Moestl 
99542c1b001SThomas Moestl 	/* step 12.  RX_MAC Configuration Register */
996bd3d9826SMarius Strobl 	v = GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG);
997ccb1212aSMarius Strobl 	v |= GEM_MAC_RX_ENABLE | GEM_MAC_RX_STRIP_CRC;
998bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 0);
999ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4,
1000ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
1001bd3d9826SMarius Strobl 	if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0))
1002ccb1212aSMarius Strobl 		device_printf(sc->sc_dev, "cannot configure RX MAC\n");
1003bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, v);
100442c1b001SThomas Moestl 
1005ccb1212aSMarius Strobl 	/* step 13. TX_MAC Configuration Register */
1006ccb1212aSMarius Strobl 	v = GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG);
1007ccb1212aSMarius Strobl 	v |= GEM_MAC_TX_ENABLE;
1008ccb1212aSMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, 0);
1009ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MAC_TX_CONFIG, 4,
1010ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
1011ccb1212aSMarius Strobl 	if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0))
1012ccb1212aSMarius Strobl 		device_printf(sc->sc_dev, "cannot configure TX MAC\n");
1013ccb1212aSMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, v);
1014ccb1212aSMarius Strobl 
10152a79fd39SMarius Strobl 	/* step 14.  Issue Transmit Pending command. */
101642c1b001SThomas Moestl 
10172a79fd39SMarius Strobl 	/* step 15.  Give the reciever a swift kick. */
1018bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_KICK, GEM_NRXDESC - 4);
101942c1b001SThomas Moestl 
102013f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
102113f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
10221ed3fed7SMarius Strobl 
10231ed3fed7SMarius Strobl 	mii_mediachg(sc->sc_mii);
10241ed3fed7SMarius Strobl 
10251ed3fed7SMarius Strobl 	/* Start the one second timer. */
10261ed3fed7SMarius Strobl 	sc->sc_wdog_timer = 0;
10271ed3fed7SMarius Strobl 	callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
102842c1b001SThomas Moestl }
102942c1b001SThomas Moestl 
103012fb0330SPyun YongHyeon static int
10312a79fd39SMarius Strobl gem_load_txmbuf(struct gem_softc *sc, struct mbuf **m_head)
103212fb0330SPyun YongHyeon {
103312fb0330SPyun YongHyeon 	bus_dma_segment_t txsegs[GEM_NTXSEGS];
10342a79fd39SMarius Strobl 	struct gem_txsoft *txs;
1035ccb1212aSMarius Strobl 	struct ip *ip;
103612fb0330SPyun YongHyeon 	struct mbuf *m;
10372a79fd39SMarius Strobl 	uint64_t cflags, flags;
1038ccb1212aSMarius Strobl 	int error, nexttx, nsegs, offset, seg;
103942c1b001SThomas Moestl 
104042c1b001SThomas Moestl 	/* Get a work queue entry. */
104142c1b001SThomas Moestl 	if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) {
1042305f2c06SThomas Moestl 		/* Ran out of descriptors. */
104312fb0330SPyun YongHyeon 		return (ENOBUFS);
1044305f2c06SThomas Moestl 	}
1045ccb1212aSMarius Strobl 
1046ccb1212aSMarius Strobl 	cflags = 0;
1047ccb1212aSMarius Strobl 	if (((*m_head)->m_pkthdr.csum_flags & sc->sc_csum_features) != 0) {
1048ccb1212aSMarius Strobl 		if (M_WRITABLE(*m_head) == 0) {
1049ccb1212aSMarius Strobl 			m = m_dup(*m_head, M_DONTWAIT);
1050ccb1212aSMarius Strobl 			m_freem(*m_head);
1051ccb1212aSMarius Strobl 			*m_head = m;
1052ccb1212aSMarius Strobl 			if (m == NULL)
1053ccb1212aSMarius Strobl 				return (ENOBUFS);
1054ccb1212aSMarius Strobl 		}
1055ccb1212aSMarius Strobl 		offset = sizeof(struct ether_header);
1056ccb1212aSMarius Strobl 		m = m_pullup(*m_head, offset + sizeof(struct ip));
1057ccb1212aSMarius Strobl 		if (m == NULL) {
1058ccb1212aSMarius Strobl 			*m_head = NULL;
1059ccb1212aSMarius Strobl 			return (ENOBUFS);
1060ccb1212aSMarius Strobl 		}
1061ccb1212aSMarius Strobl 		ip = (struct ip *)(mtod(m, caddr_t) + offset);
1062ccb1212aSMarius Strobl 		offset += (ip->ip_hl << 2);
1063ccb1212aSMarius Strobl 		cflags = offset << GEM_TD_CXSUM_STARTSHFT |
1064ccb1212aSMarius Strobl 		    ((offset + m->m_pkthdr.csum_data) <<
1065ccb1212aSMarius Strobl 		    GEM_TD_CXSUM_STUFFSHFT) | GEM_TD_CXSUM_ENABLE;
1066ccb1212aSMarius Strobl 		*m_head = m;
1067ccb1212aSMarius Strobl 	}
1068ccb1212aSMarius Strobl 
106912fb0330SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->sc_tdmatag, txs->txs_dmamap,
107012fb0330SPyun YongHyeon 	    *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
107112fb0330SPyun YongHyeon 	if (error == EFBIG) {
1072304a4c6fSJohn Baldwin 		m = m_collapse(*m_head, M_DONTWAIT, GEM_NTXSEGS);
107312fb0330SPyun YongHyeon 		if (m == NULL) {
107412fb0330SPyun YongHyeon 			m_freem(*m_head);
107512fb0330SPyun YongHyeon 			*m_head = NULL;
107612fb0330SPyun YongHyeon 			return (ENOBUFS);
107712fb0330SPyun YongHyeon 		}
107812fb0330SPyun YongHyeon 		*m_head = m;
10792a79fd39SMarius Strobl 		error = bus_dmamap_load_mbuf_sg(sc->sc_tdmatag,
10802a79fd39SMarius Strobl 		    txs->txs_dmamap, *m_head, txsegs, &nsegs,
10812a79fd39SMarius Strobl 		    BUS_DMA_NOWAIT);
108212fb0330SPyun YongHyeon 		if (error != 0) {
108312fb0330SPyun YongHyeon 			m_freem(*m_head);
108412fb0330SPyun YongHyeon 			*m_head = NULL;
108512fb0330SPyun YongHyeon 			return (error);
108612fb0330SPyun YongHyeon 		}
108712fb0330SPyun YongHyeon 	} else if (error != 0)
108812fb0330SPyun YongHyeon 		return (error);
1089801772ecSMarius Strobl 	/* If nsegs is wrong then the stack is corrupt. */
1090801772ecSMarius Strobl 	KASSERT(nsegs <= GEM_NTXSEGS,
1091801772ecSMarius Strobl 	    ("%s: too many DMA segments (%d)", __func__, nsegs));
109212fb0330SPyun YongHyeon 	if (nsegs == 0) {
109312fb0330SPyun YongHyeon 		m_freem(*m_head);
109412fb0330SPyun YongHyeon 		*m_head = NULL;
109512fb0330SPyun YongHyeon 		return (EIO);
109612fb0330SPyun YongHyeon 	}
109712fb0330SPyun YongHyeon 
109812fb0330SPyun YongHyeon 	/*
109912fb0330SPyun YongHyeon 	 * Ensure we have enough descriptors free to describe
110012fb0330SPyun YongHyeon 	 * the packet.  Note, we always reserve one descriptor
11012a79fd39SMarius Strobl 	 * at the end of the ring as a termination point, in
11022a79fd39SMarius Strobl 	 * order to prevent wrap-around.
110312fb0330SPyun YongHyeon 	 */
110412fb0330SPyun YongHyeon 	if (nsegs > sc->sc_txfree - 1) {
110512fb0330SPyun YongHyeon 		txs->txs_ndescs = 0;
110612fb0330SPyun YongHyeon 		bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap);
110712fb0330SPyun YongHyeon 		return (ENOBUFS);
110812fb0330SPyun YongHyeon 	}
110912fb0330SPyun YongHyeon 
111012fb0330SPyun YongHyeon 	txs->txs_ndescs = nsegs;
1111305f2c06SThomas Moestl 	txs->txs_firstdesc = sc->sc_txnext;
111212fb0330SPyun YongHyeon 	nexttx = txs->txs_firstdesc;
111312fb0330SPyun YongHyeon 	for (seg = 0; seg < nsegs; seg++, nexttx = GEM_NEXTTX(nexttx)) {
111412fb0330SPyun YongHyeon #ifdef GEM_DEBUG
11152a79fd39SMarius Strobl 		CTR6(KTR_GEM,
11162a79fd39SMarius Strobl 		    "%s: mapping seg %d (txd %d), len %lx, addr %#lx (%#lx)",
11172a79fd39SMarius Strobl 		    __func__, seg, nexttx, txsegs[seg].ds_len,
11182a79fd39SMarius Strobl 		    txsegs[seg].ds_addr,
111912fb0330SPyun YongHyeon 		    GEM_DMA_WRITE(sc, txsegs[seg].ds_addr));
112012fb0330SPyun YongHyeon #endif
112112fb0330SPyun YongHyeon 		sc->sc_txdescs[nexttx].gd_addr =
112212fb0330SPyun YongHyeon 		    GEM_DMA_WRITE(sc, txsegs[seg].ds_addr);
112312fb0330SPyun YongHyeon 		KASSERT(txsegs[seg].ds_len < GEM_TD_BUFSIZE,
112412fb0330SPyun YongHyeon 		    ("%s: segment size too large!", __func__));
112512fb0330SPyun YongHyeon 		flags = txsegs[seg].ds_len & GEM_TD_BUFSIZE;
112612fb0330SPyun YongHyeon 		sc->sc_txdescs[nexttx].gd_flags =
112712fb0330SPyun YongHyeon 		    GEM_DMA_WRITE(sc, flags | cflags);
112812fb0330SPyun YongHyeon 		txs->txs_lastdesc = nexttx;
112942c1b001SThomas Moestl 	}
1130305f2c06SThomas Moestl 
11312a79fd39SMarius Strobl 	/* Set EOP on the last descriptor. */
113212fb0330SPyun YongHyeon #ifdef GEM_DEBUG
11332a79fd39SMarius Strobl 	CTR3(KTR_GEM, "%s: end of packet at segment %d, TX %d",
11342a79fd39SMarius Strobl 	    __func__, seg, nexttx);
113512fb0330SPyun YongHyeon #endif
113612fb0330SPyun YongHyeon 	sc->sc_txdescs[txs->txs_lastdesc].gd_flags |=
113712fb0330SPyun YongHyeon 	    GEM_DMA_WRITE(sc, GEM_TD_END_OF_PACKET);
113812fb0330SPyun YongHyeon 
11392a79fd39SMarius Strobl 	/* Lastly set SOP on the first descriptor. */
114012fb0330SPyun YongHyeon #ifdef GEM_DEBUG
11412a79fd39SMarius Strobl 	CTR3(KTR_GEM, "%s: start of packet at segment %d, TX %d",
11422a79fd39SMarius Strobl 	    __func__, seg, nexttx);
114312fb0330SPyun YongHyeon #endif
114412fb0330SPyun YongHyeon 	if (++sc->sc_txwin > GEM_NTXSEGS * 2 / 3) {
114512fb0330SPyun YongHyeon 		sc->sc_txwin = 0;
114612fb0330SPyun YongHyeon 		flags |= GEM_TD_INTERRUPT_ME;
114712fb0330SPyun YongHyeon 		sc->sc_txdescs[txs->txs_firstdesc].gd_flags |=
114812fb0330SPyun YongHyeon 		    GEM_DMA_WRITE(sc, GEM_TD_INTERRUPT_ME |
114912fb0330SPyun YongHyeon 		    GEM_TD_START_OF_PACKET);
115012fb0330SPyun YongHyeon 	} else
115112fb0330SPyun YongHyeon 		sc->sc_txdescs[txs->txs_firstdesc].gd_flags |=
115212fb0330SPyun YongHyeon 		    GEM_DMA_WRITE(sc, GEM_TD_START_OF_PACKET);
115312fb0330SPyun YongHyeon 
115442c1b001SThomas Moestl 	/* Sync the DMA map. */
11552a79fd39SMarius Strobl 	bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap,
11562a79fd39SMarius Strobl 	    BUS_DMASYNC_PREWRITE);
1157305f2c06SThomas Moestl 
115818100346SThomas Moestl #ifdef GEM_DEBUG
115912fb0330SPyun YongHyeon 	CTR4(KTR_GEM, "%s: setting firstdesc=%d, lastdesc=%d, ndescs=%d",
11602a79fd39SMarius Strobl 	    __func__, txs->txs_firstdesc, txs->txs_lastdesc,
11612a79fd39SMarius Strobl 	    txs->txs_ndescs);
116218100346SThomas Moestl #endif
116342c1b001SThomas Moestl 	STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
1164305f2c06SThomas Moestl 	STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
116512fb0330SPyun YongHyeon 	txs->txs_mbuf = *m_head;
1166305f2c06SThomas Moestl 
1167305f2c06SThomas Moestl 	sc->sc_txnext = GEM_NEXTTX(txs->txs_lastdesc);
1168305f2c06SThomas Moestl 	sc->sc_txfree -= txs->txs_ndescs;
116942c1b001SThomas Moestl 
117012fb0330SPyun YongHyeon 	return (0);
117142c1b001SThomas Moestl }
117242c1b001SThomas Moestl 
117342c1b001SThomas Moestl static void
11742a79fd39SMarius Strobl gem_init_regs(struct gem_softc *sc)
117542c1b001SThomas Moestl {
11764a0d6638SRuslan Ermilov 	const u_char *laddr = IF_LLADDR(sc->sc_ifp);
117742c1b001SThomas Moestl 
11782a79fd39SMarius Strobl 	/* These registers are not cleared on reset. */
11791ed3fed7SMarius Strobl 	if ((sc->sc_flags & GEM_INITED) == 0) {
11802a79fd39SMarius Strobl 		/* magic values */
1181bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG0, 0);
1182bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG1, 8);
1183bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG2, 4);
118442c1b001SThomas Moestl 
1185bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN);
11862a79fd39SMarius Strobl 		/* max frame and max burst size */
1187bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_MAC_MAX_FRAME,
11881ed3fed7SMarius Strobl 		    (ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN) | (0x2000 << 16));
1189336cca9eSBenno Rice 
1190bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_PREAMBLE_LEN, 0x7);
1191bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_JAM_SIZE, 0x4);
1192bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ATTEMPT_LIMIT, 0x10);
11932a79fd39SMarius Strobl 		/* dunno... */
1194bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_TYPE, 0x8088);
1195bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_RANDOM_SEED,
1196336cca9eSBenno Rice 		    ((laddr[5] << 8) | laddr[4]) & 0x3ff);
1197336cca9eSBenno Rice 
11982a79fd39SMarius Strobl 		/* secondary MAC address: 0:0:0:0:0:0 */
1199bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR3, 0);
1200bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR4, 0);
1201bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR5, 0);
1202336cca9eSBenno Rice 
12032a79fd39SMarius Strobl 		/* MAC control address: 01:80:c2:00:00:01 */
1204bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR6, 0x0001);
1205bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR7, 0xc200);
1206bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR8, 0x0180);
120742c1b001SThomas Moestl 
12082a79fd39SMarius Strobl 		/* MAC filter address: 0:0:0:0:0:0 */
1209bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER0, 0);
1210bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER1, 0);
1211bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER2, 0);
121242c1b001SThomas Moestl 
1213bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADR_FLT_MASK1_2, 0);
1214bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADR_FLT_MASK0, 0);
121542c1b001SThomas Moestl 
12161ed3fed7SMarius Strobl 		sc->sc_flags |= GEM_INITED;
121742c1b001SThomas Moestl 	}
121842c1b001SThomas Moestl 
12192a79fd39SMarius Strobl 	/* Counters need to be zeroed. */
1220bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_NORM_COLL_CNT, 0);
1221bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_FIRST_COLL_CNT, 0);
1222bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_EXCESS_COLL_CNT, 0);
1223bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_LATE_COLL_CNT, 0);
1224bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_DEFER_TMR_CNT, 0);
1225bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_PEAK_ATTEMPTS, 0);
1226bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_FRAME_COUNT, 0);
1227bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_LEN_ERR_CNT, 0);
1228bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_ALIGN_ERR, 0);
1229bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CRC_ERR_CNT, 0);
1230bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CODE_VIOL, 0);
123142c1b001SThomas Moestl 
12321ed3fed7SMarius Strobl 	/* Set XOFF PAUSE time. */
1233bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0);
12341ed3fed7SMarius Strobl 
12351ed3fed7SMarius Strobl 	/*
12361ed3fed7SMarius Strobl 	 * Set the internal arbitration to "infinite" bursts of the
12371ed3fed7SMarius Strobl 	 * maximum length of 31 * 64 bytes so DMA transfers aren't
12381ed3fed7SMarius Strobl 	 * split up in cache line size chunks.  This greatly improves
12391ed3fed7SMarius Strobl 	 * especially RX performance.
12401ed3fed7SMarius Strobl 	 * Enable silicon bug workarounds for the Apple variants.
12411ed3fed7SMarius Strobl 	 */
1242bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_CONFIG,
12431ed3fed7SMarius Strobl 	    GEM_CONFIG_TXDMA_LIMIT | GEM_CONFIG_RXDMA_LIMIT |
12441ed3fed7SMarius Strobl 	    GEM_CONFIG_BURST_INF | (GEM_IS_APPLE(sc) ?
12451ed3fed7SMarius Strobl 	    GEM_CONFIG_RONPAULBIT | GEM_CONFIG_BUG2FIX : 0));
124642c1b001SThomas Moestl 
12472a79fd39SMarius Strobl 	/* Set the station address. */
1248bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR0, (laddr[4] << 8) | laddr[5]);
1249bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR1, (laddr[2] << 8) | laddr[3]);
1250bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR2, (laddr[0] << 8) | laddr[1]);
1251336cca9eSBenno Rice 
12521ed3fed7SMarius Strobl 	/* Enable MII outputs. */
1253bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_XIF_CONFIG, GEM_MAC_XIF_TX_MII_ENA);
125442c1b001SThomas Moestl }
125542c1b001SThomas Moestl 
125642c1b001SThomas Moestl static void
12572a79fd39SMarius Strobl gem_start(struct ifnet *ifp)
125842c1b001SThomas Moestl {
12592a79fd39SMarius Strobl 	struct gem_softc *sc = ifp->if_softc;
12608cfaff7dSMarius Strobl 
12618cfaff7dSMarius Strobl 	GEM_LOCK(sc);
12628cfaff7dSMarius Strobl 	gem_start_locked(ifp);
12638cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
12648cfaff7dSMarius Strobl }
12658cfaff7dSMarius Strobl 
12668cfaff7dSMarius Strobl static void
12672a79fd39SMarius Strobl gem_start_locked(struct ifnet *ifp)
12688cfaff7dSMarius Strobl {
12692a79fd39SMarius Strobl 	struct gem_softc *sc = ifp->if_softc;
127012fb0330SPyun YongHyeon 	struct mbuf *m;
12712a79fd39SMarius Strobl 	int ntx;
127242c1b001SThomas Moestl 
127313f4c340SRobert Watson 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
12741ed3fed7SMarius Strobl 	    IFF_DRV_RUNNING || (sc->sc_flags & GEM_LINK) == 0)
127542c1b001SThomas Moestl 		return;
127642c1b001SThomas Moestl 
127718100346SThomas Moestl #ifdef GEM_DEBUG
127812fb0330SPyun YongHyeon 	CTR4(KTR_GEM, "%s: %s: txfree %d, txnext %d",
12791ed3fed7SMarius Strobl 	    device_get_name(sc->sc_dev), __func__, sc->sc_txfree,
12801ed3fed7SMarius Strobl 	    sc->sc_txnext);
128118100346SThomas Moestl #endif
12822a79fd39SMarius Strobl 	ntx = 0;
128312fb0330SPyun YongHyeon 	for (; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) && sc->sc_txfree > 1;) {
128412fb0330SPyun YongHyeon 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
128512fb0330SPyun YongHyeon 		if (m == NULL)
128642c1b001SThomas Moestl 			break;
12871ed3fed7SMarius Strobl 		if (gem_load_txmbuf(sc, &m) != 0) {
128812fb0330SPyun YongHyeon 			if (m == NULL)
128912fb0330SPyun YongHyeon 				break;
129012fb0330SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
129112fb0330SPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, m);
129242c1b001SThomas Moestl 			break;
129342c1b001SThomas Moestl 		}
129418100346SThomas Moestl 		ntx++;
1295305f2c06SThomas Moestl 		/* Kick the transmitter. */
129618100346SThomas Moestl #ifdef GEM_DEBUG
12972a79fd39SMarius Strobl 		CTR3(KTR_GEM, "%s: %s: kicking TX %d",
129812fb0330SPyun YongHyeon 		    device_get_name(sc->sc_dev), __func__, sc->sc_txnext);
129918100346SThomas Moestl #endif
1300ccb1212aSMarius Strobl 		GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1301bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_TX_KICK, sc->sc_txnext);
130242c1b001SThomas Moestl 
130312fb0330SPyun YongHyeon 		BPF_MTAP(ifp, m);
1304305f2c06SThomas Moestl 	}
1305305f2c06SThomas Moestl 
1306305f2c06SThomas Moestl 	if (ntx > 0) {
130718100346SThomas Moestl #ifdef GEM_DEBUG
1308305f2c06SThomas Moestl 		CTR2(KTR_GEM, "%s: packets enqueued, OWN on %d",
13091ed3fed7SMarius Strobl 		    device_get_name(sc->sc_dev), sc->sc_txnext);
131018100346SThomas Moestl #endif
1311305f2c06SThomas Moestl 
131242c1b001SThomas Moestl 		/* Set a watchdog timer in case the chip flakes out. */
13138cb37876SMarius Strobl 		sc->sc_wdog_timer = 5;
131418100346SThomas Moestl #ifdef GEM_DEBUG
131512fb0330SPyun YongHyeon 		CTR3(KTR_GEM, "%s: %s: watchdog %d",
13162a79fd39SMarius Strobl 		    device_get_name(sc->sc_dev), __func__,
13172a79fd39SMarius Strobl 		    sc->sc_wdog_timer);
131818100346SThomas Moestl #endif
131942c1b001SThomas Moestl 	}
132042c1b001SThomas Moestl }
132142c1b001SThomas Moestl 
132242c1b001SThomas Moestl static void
13232a79fd39SMarius Strobl gem_tint(struct gem_softc *sc)
132442c1b001SThomas Moestl {
1325fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
132642c1b001SThomas Moestl 	struct gem_txsoft *txs;
13272a79fd39SMarius Strobl 	int txlast, progress;
132818100346SThomas Moestl #ifdef GEM_DEBUG
13292a79fd39SMarius Strobl 	int i;
13302a79fd39SMarius Strobl 
133112fb0330SPyun YongHyeon 	CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__);
133218100346SThomas Moestl #endif
133342c1b001SThomas Moestl 
133442c1b001SThomas Moestl 	/*
13352a79fd39SMarius Strobl 	 * Go through our TX list and free mbufs for those
133642c1b001SThomas Moestl 	 * frames that have been transmitted.
133742c1b001SThomas Moestl 	 */
13382a79fd39SMarius Strobl 	progress = 0;
1339b2d59f42SThomas Moestl 	GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD);
134042c1b001SThomas Moestl 	while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
134142c1b001SThomas Moestl 
134242c1b001SThomas Moestl #ifdef GEM_DEBUG
13432a79fd39SMarius Strobl 		if ((ifp->if_flags & IFF_DEBUG) != 0) {
134442c1b001SThomas Moestl 			printf("    txsoft %p transmit chain:\n", txs);
134542c1b001SThomas Moestl 			for (i = txs->txs_firstdesc;; i = GEM_NEXTTX(i)) {
134642c1b001SThomas Moestl 				printf("descriptor %d: ", i);
13472a79fd39SMarius Strobl 				printf("gd_flags: 0x%016llx\t",
13482a79fd39SMarius Strobl 				    (long long)GEM_DMA_READ(sc,
13492a79fd39SMarius Strobl 				    sc->sc_txdescs[i].gd_flags));
13502a79fd39SMarius Strobl 				printf("gd_addr: 0x%016llx\n",
13512a79fd39SMarius Strobl 				    (long long)GEM_DMA_READ(sc,
13522a79fd39SMarius Strobl 				    sc->sc_txdescs[i].gd_addr));
135342c1b001SThomas Moestl 				if (i == txs->txs_lastdesc)
135442c1b001SThomas Moestl 					break;
135542c1b001SThomas Moestl 			}
135642c1b001SThomas Moestl 		}
135742c1b001SThomas Moestl #endif
135842c1b001SThomas Moestl 
135942c1b001SThomas Moestl 		/*
13601ed3fed7SMarius Strobl 		 * In theory, we could harvest some descriptors before
136142c1b001SThomas Moestl 		 * the ring is empty, but that's a bit complicated.
136242c1b001SThomas Moestl 		 *
136342c1b001SThomas Moestl 		 * GEM_TX_COMPLETION points to the last descriptor
136442c1b001SThomas Moestl 		 * processed + 1.
136542c1b001SThomas Moestl 		 */
1366bd3d9826SMarius Strobl 		txlast = GEM_BANK1_READ_4(sc, GEM_TX_COMPLETION);
136718100346SThomas Moestl #ifdef GEM_DEBUG
136812fb0330SPyun YongHyeon 		CTR4(KTR_GEM, "%s: txs->txs_firstdesc = %d, "
136942c1b001SThomas Moestl 		    "txs->txs_lastdesc = %d, txlast = %d",
137012fb0330SPyun YongHyeon 		    __func__, txs->txs_firstdesc, txs->txs_lastdesc, txlast);
137118100346SThomas Moestl #endif
137242c1b001SThomas Moestl 		if (txs->txs_firstdesc <= txs->txs_lastdesc) {
137342c1b001SThomas Moestl 			if ((txlast >= txs->txs_firstdesc) &&
137442c1b001SThomas Moestl 			    (txlast <= txs->txs_lastdesc))
137542c1b001SThomas Moestl 				break;
137642c1b001SThomas Moestl 		} else {
13772a79fd39SMarius Strobl 			/* Ick -- this command wraps. */
137842c1b001SThomas Moestl 			if ((txlast >= txs->txs_firstdesc) ||
137942c1b001SThomas Moestl 			    (txlast <= txs->txs_lastdesc))
138042c1b001SThomas Moestl 				break;
138142c1b001SThomas Moestl 		}
138242c1b001SThomas Moestl 
138318100346SThomas Moestl #ifdef GEM_DEBUG
13842a79fd39SMarius Strobl 		CTR1(KTR_GEM, "%s: releasing a descriptor", __func__);
138518100346SThomas Moestl #endif
138642c1b001SThomas Moestl 		STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
138742c1b001SThomas Moestl 
138842c1b001SThomas Moestl 		sc->sc_txfree += txs->txs_ndescs;
138942c1b001SThomas Moestl 
1390305f2c06SThomas Moestl 		bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap,
139142c1b001SThomas Moestl 		    BUS_DMASYNC_POSTWRITE);
1392305f2c06SThomas Moestl 		bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap);
139342c1b001SThomas Moestl 		if (txs->txs_mbuf != NULL) {
139442c1b001SThomas Moestl 			m_freem(txs->txs_mbuf);
139542c1b001SThomas Moestl 			txs->txs_mbuf = NULL;
139642c1b001SThomas Moestl 		}
139742c1b001SThomas Moestl 
139842c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
139942c1b001SThomas Moestl 
140042c1b001SThomas Moestl 		ifp->if_opackets++;
1401336cca9eSBenno Rice 		progress = 1;
140242c1b001SThomas Moestl 	}
140342c1b001SThomas Moestl 
140418100346SThomas Moestl #ifdef GEM_DEBUG
14052a79fd39SMarius Strobl 	CTR4(KTR_GEM, "%s: GEM_TX_STATE_MACHINE %x GEM_TX_DATA_PTR %llx "
140642c1b001SThomas Moestl 	    "GEM_TX_COMPLETION %x",
1407bd3d9826SMarius Strobl 	    __func__, GEM_BANK1_READ_4(sc, GEM_TX_STATE_MACHINE),
1408bd3d9826SMarius Strobl 	    ((long long)GEM_BANK1_READ_4(sc, GEM_TX_DATA_PTR_HI) << 32) |
1409bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_TX_DATA_PTR_LO),
1410bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_TX_COMPLETION));
141118100346SThomas Moestl #endif
141242c1b001SThomas Moestl 
1413336cca9eSBenno Rice 	if (progress) {
1414336cca9eSBenno Rice 		if (sc->sc_txfree == GEM_NTXDESC - 1)
1415336cca9eSBenno Rice 			sc->sc_txwin = 0;
141642c1b001SThomas Moestl 
14172a79fd39SMarius Strobl 		/*
14182a79fd39SMarius Strobl 		 * We freed some descriptors, so reset IFF_DRV_OACTIVE
14192a79fd39SMarius Strobl 		 * and restart.
14202a79fd39SMarius Strobl 		 */
142113f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
14228cb37876SMarius Strobl 		sc->sc_wdog_timer = STAILQ_EMPTY(&sc->sc_txdirtyq) ? 0 : 5;
142312fb0330SPyun YongHyeon 
142412fb0330SPyun YongHyeon 		gem_start_locked(ifp);
1425336cca9eSBenno Rice 	}
142642c1b001SThomas Moestl 
142718100346SThomas Moestl #ifdef GEM_DEBUG
142812fb0330SPyun YongHyeon 	CTR3(KTR_GEM, "%s: %s: watchdog %d",
142912fb0330SPyun YongHyeon 	    device_get_name(sc->sc_dev), __func__, sc->sc_wdog_timer);
143018100346SThomas Moestl #endif
143142c1b001SThomas Moestl }
143242c1b001SThomas Moestl 
1433c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT
14340d80b9bdSThomas Moestl static void
14352a79fd39SMarius Strobl gem_rint_timeout(void *arg)
14360d80b9bdSThomas Moestl {
14372a79fd39SMarius Strobl 	struct gem_softc *sc = arg;
14380d80b9bdSThomas Moestl 
14391f317bf9SMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
14408cfaff7dSMarius Strobl 	gem_rint(sc);
14410d80b9bdSThomas Moestl }
144211e3f060SJake Burkholder #endif
14430d80b9bdSThomas Moestl 
144442c1b001SThomas Moestl static void
14452a79fd39SMarius Strobl gem_rint(struct gem_softc *sc)
144642c1b001SThomas Moestl {
1447fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
144842c1b001SThomas Moestl 	struct mbuf *m;
14492a79fd39SMarius Strobl 	uint64_t rxstat;
14502a79fd39SMarius Strobl 	uint32_t rxcomp;
145142c1b001SThomas Moestl 
1452c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT
14530d80b9bdSThomas Moestl 	callout_stop(&sc->sc_rx_ch);
1454c3d5598aSMarius Strobl #endif
145518100346SThomas Moestl #ifdef GEM_DEBUG
145612fb0330SPyun YongHyeon 	CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__);
145718100346SThomas Moestl #endif
1458336cca9eSBenno Rice 
1459336cca9eSBenno Rice 	/*
1460336cca9eSBenno Rice 	 * Read the completion register once.  This limits
1461336cca9eSBenno Rice 	 * how long the following loop can execute.
1462336cca9eSBenno Rice 	 */
1463bd3d9826SMarius Strobl 	rxcomp = GEM_BANK1_READ_4(sc, GEM_RX_COMPLETION);
1464336cca9eSBenno Rice 
146518100346SThomas Moestl #ifdef GEM_DEBUG
146612fb0330SPyun YongHyeon 	CTR3(KTR_GEM, "%s: sc->rxptr %d, complete %d",
146712fb0330SPyun YongHyeon 	    __func__, sc->sc_rxptr, rxcomp);
146818100346SThomas Moestl #endif
1469b2d59f42SThomas Moestl 	GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD);
14701ed3fed7SMarius Strobl 	for (; sc->sc_rxptr != rxcomp;) {
14711ed3fed7SMarius Strobl 		m = sc->sc_rxsoft[sc->sc_rxptr].rxs_mbuf;
14721ed3fed7SMarius Strobl 		rxstat = GEM_DMA_READ(sc,
14731ed3fed7SMarius Strobl 		    sc->sc_rxdescs[sc->sc_rxptr].gd_flags);
147442c1b001SThomas Moestl 
147542c1b001SThomas Moestl 		if (rxstat & GEM_RD_OWN) {
1476c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT
147742c1b001SThomas Moestl 			/*
14780d80b9bdSThomas Moestl 			 * The descriptor is still marked as owned, although
14790d80b9bdSThomas Moestl 			 * it is supposed to have completed.  This has been
14800d80b9bdSThomas Moestl 			 * observed on some machines.  Just exiting here
14810d80b9bdSThomas Moestl 			 * might leave the packet sitting around until another
14820d80b9bdSThomas Moestl 			 * one arrives to trigger a new interrupt, which is
14830d80b9bdSThomas Moestl 			 * generally undesirable, so set up a timeout.
148442c1b001SThomas Moestl 			 */
14850d80b9bdSThomas Moestl 			callout_reset(&sc->sc_rx_ch, GEM_RXOWN_TICKS,
14860d80b9bdSThomas Moestl 			    gem_rint_timeout, sc);
1487336cca9eSBenno Rice #endif
14881ed3fed7SMarius Strobl 			m = NULL;
14891ed3fed7SMarius Strobl 			goto kickit;
149042c1b001SThomas Moestl 		}
149142c1b001SThomas Moestl 
149242c1b001SThomas Moestl 		if (rxstat & GEM_RD_BAD_CRC) {
1493336cca9eSBenno Rice 			ifp->if_ierrors++;
149442c1b001SThomas Moestl 			device_printf(sc->sc_dev, "receive error: CRC error\n");
14951ed3fed7SMarius Strobl 			GEM_INIT_RXDESC(sc, sc->sc_rxptr);
14961ed3fed7SMarius Strobl 			m = NULL;
14971ed3fed7SMarius Strobl 			goto kickit;
149842c1b001SThomas Moestl 		}
149942c1b001SThomas Moestl 
150042c1b001SThomas Moestl #ifdef GEM_DEBUG
15012a79fd39SMarius Strobl 		if ((ifp->if_flags & IFF_DEBUG) != 0) {
15021ed3fed7SMarius Strobl 			printf("    rxsoft %p descriptor %d: ",
15031ed3fed7SMarius Strobl 			    &sc->sc_rxsoft[sc->sc_rxptr], sc->sc_rxptr);
15042a79fd39SMarius Strobl 			printf("gd_flags: 0x%016llx\t",
15052a79fd39SMarius Strobl 			    (long long)GEM_DMA_READ(sc,
15062a79fd39SMarius Strobl 			    sc->sc_rxdescs[sc->sc_rxptr].gd_flags));
15072a79fd39SMarius Strobl 			printf("gd_addr: 0x%016llx\n",
15082a79fd39SMarius Strobl 			    (long long)GEM_DMA_READ(sc,
15092a79fd39SMarius Strobl 			    sc->sc_rxdescs[sc->sc_rxptr].gd_addr));
151042c1b001SThomas Moestl 		}
151142c1b001SThomas Moestl #endif
151242c1b001SThomas Moestl 
151342c1b001SThomas Moestl 		/*
151442c1b001SThomas Moestl 		 * Allocate a new mbuf cluster.  If that fails, we are
151542c1b001SThomas Moestl 		 * out of memory, and must drop the packet and recycle
151642c1b001SThomas Moestl 		 * the buffer that's already attached to this descriptor.
151742c1b001SThomas Moestl 		 */
15181ed3fed7SMarius Strobl 		if (gem_add_rxbuf(sc, sc->sc_rxptr) != 0) {
151942c1b001SThomas Moestl 			ifp->if_ierrors++;
15201ed3fed7SMarius Strobl 			GEM_INIT_RXDESC(sc, sc->sc_rxptr);
15211ed3fed7SMarius Strobl 			m = NULL;
15221ed3fed7SMarius Strobl 		}
15231ed3fed7SMarius Strobl 
15241ed3fed7SMarius Strobl  kickit:
15251ed3fed7SMarius Strobl 		/*
15261ed3fed7SMarius Strobl 		 * Update the RX kick register.  This register has to point
15271ed3fed7SMarius Strobl 		 * to the descriptor after the last valid one (before the
15281ed3fed7SMarius Strobl 		 * current batch) and must be incremented in multiples of
15291ed3fed7SMarius Strobl 		 * 4 (because the DMA engine fetches/updates descriptors
15301ed3fed7SMarius Strobl 		 * in batches of 4).
15311ed3fed7SMarius Strobl 		 */
15321ed3fed7SMarius Strobl 		sc->sc_rxptr = GEM_NEXTRX(sc->sc_rxptr);
15331ed3fed7SMarius Strobl 		if ((sc->sc_rxptr % 4) == 0) {
1534ccb1212aSMarius Strobl 			GEM_CDSYNC(sc,
1535ccb1212aSMarius Strobl 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1536bd3d9826SMarius Strobl 			GEM_BANK1_WRITE_4(sc, GEM_RX_KICK,
15371ed3fed7SMarius Strobl 			    (sc->sc_rxptr + GEM_NRXDESC - 4) &
15381ed3fed7SMarius Strobl 			    GEM_NRXDESC_MASK);
15391ed3fed7SMarius Strobl 		}
15401ed3fed7SMarius Strobl 
15411ed3fed7SMarius Strobl 		if (m == NULL) {
15421ed3fed7SMarius Strobl 			if (rxstat & GEM_RD_OWN)
15431ed3fed7SMarius Strobl 				break;
154442c1b001SThomas Moestl 			continue;
154542c1b001SThomas Moestl 		}
154642c1b001SThomas Moestl 
15471ed3fed7SMarius Strobl 		ifp->if_ipackets++;
15481ed3fed7SMarius Strobl 		m->m_data += 2; /* We're already off by two */
154942c1b001SThomas Moestl 		m->m_pkthdr.rcvif = ifp;
15501ed3fed7SMarius Strobl 		m->m_pkthdr.len = m->m_len = GEM_RD_BUFLEN(rxstat);
155112fb0330SPyun YongHyeon 
155212fb0330SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
155312fb0330SPyun YongHyeon 			gem_rxcksum(m, rxstat);
155442c1b001SThomas Moestl 
155542c1b001SThomas Moestl 		/* Pass it on. */
15568cfaff7dSMarius Strobl 		GEM_UNLOCK(sc);
1557673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
15588cfaff7dSMarius Strobl 		GEM_LOCK(sc);
155942c1b001SThomas Moestl 	}
156042c1b001SThomas Moestl 
156118100346SThomas Moestl #ifdef GEM_DEBUG
156212fb0330SPyun YongHyeon 	CTR3(KTR_GEM, "%s: done sc->rxptr %d, complete %d", __func__,
1563bd3d9826SMarius Strobl 	    sc->sc_rxptr, GEM_BANK1_READ_4(sc, GEM_RX_COMPLETION));
156418100346SThomas Moestl #endif
156542c1b001SThomas Moestl }
156642c1b001SThomas Moestl 
156742c1b001SThomas Moestl static int
15682a79fd39SMarius Strobl gem_add_rxbuf(struct gem_softc *sc, int idx)
156942c1b001SThomas Moestl {
157042c1b001SThomas Moestl 	struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx];
157142c1b001SThomas Moestl 	struct mbuf *m;
1572c3d5598aSMarius Strobl 	bus_dma_segment_t segs[1];
1573c3d5598aSMarius Strobl 	int error, nsegs;
157442c1b001SThomas Moestl 
1575a163d034SWarner Losh 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
157642c1b001SThomas Moestl 	if (m == NULL)
157742c1b001SThomas Moestl 		return (ENOBUFS);
1578305f2c06SThomas Moestl 	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
157942c1b001SThomas Moestl 
158042c1b001SThomas Moestl #ifdef GEM_DEBUG
15812a79fd39SMarius Strobl 	/* Bzero the packet to check DMA. */
158242c1b001SThomas Moestl 	memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size);
158342c1b001SThomas Moestl #endif
158442c1b001SThomas Moestl 
1585b2d59f42SThomas Moestl 	if (rxs->rxs_mbuf != NULL) {
1586b2d59f42SThomas Moestl 		bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap,
1587b2d59f42SThomas Moestl 		    BUS_DMASYNC_POSTREAD);
1588305f2c06SThomas Moestl 		bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap);
1589b2d59f42SThomas Moestl 	}
159042c1b001SThomas Moestl 
1591c3d5598aSMarius Strobl 	error = bus_dmamap_load_mbuf_sg(sc->sc_rdmatag, rxs->rxs_dmamap,
1592c3d5598aSMarius Strobl 	    m, segs, &nsegs, BUS_DMA_NOWAIT);
1593c3d5598aSMarius Strobl 	if (error != 0) {
15942a79fd39SMarius Strobl 		device_printf(sc->sc_dev,
15952a79fd39SMarius Strobl 		    "cannot load RS DMA map %d, error = %d\n", idx, error);
1596c3d5598aSMarius Strobl 		m_freem(m);
15971ed3fed7SMarius Strobl 		return (error);
159842c1b001SThomas Moestl 	}
15992a79fd39SMarius Strobl 	/* If nsegs is wrong then the stack is corrupt. */
1600801772ecSMarius Strobl 	KASSERT(nsegs == 1,
1601801772ecSMarius Strobl 	    ("%s: too many DMA segments (%d)", __func__, nsegs));
16021ed3fed7SMarius Strobl 	rxs->rxs_mbuf = m;
1603c3d5598aSMarius Strobl 	rxs->rxs_paddr = segs[0].ds_addr;
160442c1b001SThomas Moestl 
16052a79fd39SMarius Strobl 	bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap,
16062a79fd39SMarius Strobl 	    BUS_DMASYNC_PREREAD);
160742c1b001SThomas Moestl 
160842c1b001SThomas Moestl 	GEM_INIT_RXDESC(sc, idx);
160942c1b001SThomas Moestl 
161042c1b001SThomas Moestl 	return (0);
161142c1b001SThomas Moestl }
161242c1b001SThomas Moestl 
161342c1b001SThomas Moestl static void
16142a79fd39SMarius Strobl gem_eint(struct gem_softc *sc, u_int status)
161542c1b001SThomas Moestl {
161642c1b001SThomas Moestl 
16171ed3fed7SMarius Strobl 	sc->sc_ifp->if_ierrors++;
16181ed3fed7SMarius Strobl 	if ((status & GEM_INTR_RX_TAG_ERR) != 0) {
16191ed3fed7SMarius Strobl 		gem_reset_rxdma(sc);
162042c1b001SThomas Moestl 		return;
162142c1b001SThomas Moestl 	}
162242c1b001SThomas Moestl 
16231ed3fed7SMarius Strobl 	device_printf(sc->sc_dev, "%s: status=%x\n", __func__, status);
162442c1b001SThomas Moestl }
162542c1b001SThomas Moestl 
162642c1b001SThomas Moestl void
16272a79fd39SMarius Strobl gem_intr(void *v)
162842c1b001SThomas Moestl {
16292a79fd39SMarius Strobl 	struct gem_softc *sc = v;
16301ed3fed7SMarius Strobl 	uint32_t status, status2;
163142c1b001SThomas Moestl 
16328cfaff7dSMarius Strobl 	GEM_LOCK(sc);
1633bd3d9826SMarius Strobl 	status = GEM_BANK1_READ_4(sc, GEM_STATUS);
16341ed3fed7SMarius Strobl 
163518100346SThomas Moestl #ifdef GEM_DEBUG
163612fb0330SPyun YongHyeon 	CTR4(KTR_GEM, "%s: %s: cplt %x, status %x",
163712fb0330SPyun YongHyeon 	    device_get_name(sc->sc_dev), __func__, (status >> 19),
163842c1b001SThomas Moestl 	    (u_int)status);
16391ed3fed7SMarius Strobl 
16401ed3fed7SMarius Strobl 	/*
16411ed3fed7SMarius Strobl 	 * PCS interrupts must be cleared, otherwise no traffic is passed!
16421ed3fed7SMarius Strobl 	 */
16431ed3fed7SMarius Strobl 	if ((status & GEM_INTR_PCS) != 0) {
16442a79fd39SMarius Strobl 		status2 =
1645bd3d9826SMarius Strobl 		    GEM_BANK1_READ_4(sc, GEM_MII_INTERRUP_STATUS) |
1646bd3d9826SMarius Strobl 		    GEM_BANK1_READ_4(sc, GEM_MII_INTERRUP_STATUS);
16471ed3fed7SMarius Strobl 		if ((status2 & GEM_MII_INTERRUP_LINK) != 0)
16481ed3fed7SMarius Strobl 			device_printf(sc->sc_dev,
16491ed3fed7SMarius Strobl 			    "%s: PCS link status changed\n", __func__);
16501ed3fed7SMarius Strobl 	}
16511ed3fed7SMarius Strobl 	if ((status & GEM_MAC_CONTROL_STATUS) != 0) {
1652bd3d9826SMarius Strobl 		status2 = GEM_BANK1_READ_4(sc, GEM_MAC_CONTROL_STATUS);
16531ed3fed7SMarius Strobl 		if ((status2 & GEM_MAC_PAUSED) != 0)
16541ed3fed7SMarius Strobl 			device_printf(sc->sc_dev,
16551ed3fed7SMarius Strobl 			    "%s: PAUSE received (PAUSE time %d slots)\n",
16561ed3fed7SMarius Strobl 			    __func__, GEM_MAC_PAUSE_TIME(status2));
16571ed3fed7SMarius Strobl 		if ((status2 & GEM_MAC_PAUSE) != 0)
16581ed3fed7SMarius Strobl 			device_printf(sc->sc_dev,
16591ed3fed7SMarius Strobl 			    "%s: transited to PAUSE state\n", __func__);
16601ed3fed7SMarius Strobl 		if ((status2 & GEM_MAC_RESUME) != 0)
16611ed3fed7SMarius Strobl 			device_printf(sc->sc_dev,
16621ed3fed7SMarius Strobl 			    "%s: transited to non-PAUSE state\n", __func__);
16631ed3fed7SMarius Strobl 	}
16641ed3fed7SMarius Strobl 	if ((status & GEM_INTR_MIF) != 0)
16651ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "%s: MIF interrupt\n", __func__);
166618100346SThomas Moestl #endif
166742c1b001SThomas Moestl 
16681ed3fed7SMarius Strobl 	if ((status &
16691ed3fed7SMarius Strobl 	    (GEM_INTR_RX_TAG_ERR | GEM_INTR_PERR | GEM_INTR_BERR)) != 0)
167042c1b001SThomas Moestl 		gem_eint(sc, status);
167142c1b001SThomas Moestl 
167242c1b001SThomas Moestl 	if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0)
167342c1b001SThomas Moestl 		gem_rint(sc);
167442c1b001SThomas Moestl 
16751ed3fed7SMarius Strobl 	if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0)
16761ed3fed7SMarius Strobl 		gem_tint(sc);
16771ed3fed7SMarius Strobl 
167842c1b001SThomas Moestl 	if (status & GEM_INTR_TX_MAC) {
1679bd3d9826SMarius Strobl 		status2 = GEM_BANK1_READ_4(sc, GEM_MAC_TX_STATUS);
16802a79fd39SMarius Strobl 		if ((status2 &
16812a79fd39SMarius Strobl 		    ~(GEM_MAC_TX_XMIT_DONE | GEM_MAC_TX_DEFER_EXP)) != 0)
16822a79fd39SMarius Strobl 			device_printf(sc->sc_dev,
16832a79fd39SMarius Strobl 			    "MAC TX fault, status %x\n", status2);
16842a79fd39SMarius Strobl 		if ((status2 &
16852a79fd39SMarius Strobl 		    (GEM_MAC_TX_UNDERRUN | GEM_MAC_TX_PKT_TOO_LONG)) != 0)
16868cfaff7dSMarius Strobl 			gem_init_locked(sc);
168742c1b001SThomas Moestl 	}
168842c1b001SThomas Moestl 	if (status & GEM_INTR_RX_MAC) {
1689bd3d9826SMarius Strobl 		status2 = GEM_BANK1_READ_4(sc, GEM_MAC_RX_STATUS);
169000d12766SMarius Strobl 		/*
16911ed3fed7SMarius Strobl 		 * At least with GEM_SUN_GEM and some GEM_SUN_ERI
16921ed3fed7SMarius Strobl 		 * revisions GEM_MAC_RX_OVERFLOW happen often due to a
16931ed3fed7SMarius Strobl 		 * silicon bug so handle them silently.  Moreover, it's
16941ed3fed7SMarius Strobl 		 * likely that the receiver has hung so we reset it.
169500d12766SMarius Strobl 		 */
16962a79fd39SMarius Strobl 		if ((status2 & GEM_MAC_RX_OVERFLOW) != 0) {
16971ed3fed7SMarius Strobl 			sc->sc_ifp->if_ierrors++;
16981ed3fed7SMarius Strobl 			gem_reset_rxdma(sc);
16992a79fd39SMarius Strobl 		} else if ((status2 &
17002a79fd39SMarius Strobl 		    ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT)) != 0)
17012a79fd39SMarius Strobl 			device_printf(sc->sc_dev,
17022a79fd39SMarius Strobl 			    "MAC RX fault, status %x\n", status2);
170342c1b001SThomas Moestl 	}
17048cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
170542c1b001SThomas Moestl }
170642c1b001SThomas Moestl 
17078cb37876SMarius Strobl static int
17082a79fd39SMarius Strobl gem_watchdog(struct gem_softc *sc)
170942c1b001SThomas Moestl {
1710ccb1212aSMarius Strobl 	struct ifnet *ifp = sc->sc_ifp;
171142c1b001SThomas Moestl 
17128cb37876SMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
17138cb37876SMarius Strobl 
171418100346SThomas Moestl #ifdef GEM_DEBUG
17152a79fd39SMarius Strobl 	CTR4(KTR_GEM,
17162a79fd39SMarius Strobl 	    "%s: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x GEM_MAC_RX_CONFIG %x",
1717bd3d9826SMarius Strobl 	    __func__, GEM_BANK1_READ_4(sc, GEM_RX_CONFIG),
1718bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_STATUS),
1719bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG));
17202a79fd39SMarius Strobl 	CTR4(KTR_GEM,
17212a79fd39SMarius Strobl 	    "%s: GEM_TX_CONFIG %x GEM_MAC_TX_STATUS %x GEM_MAC_TX_CONFIG %x",
1722bd3d9826SMarius Strobl 	    __func__, GEM_BANK1_READ_4(sc, GEM_TX_CONFIG),
1723bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_TX_STATUS),
1724bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG));
172518100346SThomas Moestl #endif
172642c1b001SThomas Moestl 
17278cb37876SMarius Strobl 	if (sc->sc_wdog_timer == 0 || --sc->sc_wdog_timer != 0)
17288cb37876SMarius Strobl 		return (0);
17298cb37876SMarius Strobl 
17301ed3fed7SMarius Strobl 	if ((sc->sc_flags & GEM_LINK) != 0)
173142c1b001SThomas Moestl 		device_printf(sc->sc_dev, "device timeout\n");
17321ed3fed7SMarius Strobl 	else if (bootverbose)
17331ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "device timeout (no link)\n");
1734ccb1212aSMarius Strobl 	++ifp->if_oerrors;
173542c1b001SThomas Moestl 
173642c1b001SThomas Moestl 	/* Try to get more packets going. */
17378cfaff7dSMarius Strobl 	gem_init_locked(sc);
1738ccb1212aSMarius Strobl 	gem_start_locked(ifp);
17398cb37876SMarius Strobl 	return (EJUSTRETURN);
174042c1b001SThomas Moestl }
174142c1b001SThomas Moestl 
174242c1b001SThomas Moestl static void
17432a79fd39SMarius Strobl gem_mifinit(struct gem_softc *sc)
174442c1b001SThomas Moestl {
174542c1b001SThomas Moestl 
1746801772ecSMarius Strobl 	/* Configure the MIF in frame mode. */
1747bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MIF_CONFIG,
1748bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MIF_CONFIG) & ~GEM_MIF_CONFIG_BB_ENA);
174942c1b001SThomas Moestl }
175042c1b001SThomas Moestl 
175142c1b001SThomas Moestl /*
175242c1b001SThomas Moestl  * MII interface
175342c1b001SThomas Moestl  *
175478d22f42SMarius Strobl  * The MII interface supports at least three different operating modes:
175542c1b001SThomas Moestl  *
175642c1b001SThomas Moestl  * Bitbang mode is implemented using data, clock and output enable registers.
175742c1b001SThomas Moestl  *
175842c1b001SThomas Moestl  * Frame mode is implemented by loading a complete frame into the frame
175942c1b001SThomas Moestl  * register and polling the valid bit for completion.
176042c1b001SThomas Moestl  *
176142c1b001SThomas Moestl  * Polling mode uses the frame register but completion is indicated by
176242c1b001SThomas Moestl  * an interrupt.
176342c1b001SThomas Moestl  *
176442c1b001SThomas Moestl  */
176542c1b001SThomas Moestl int
17662a79fd39SMarius Strobl gem_mii_readreg(device_t dev, int phy, int reg)
176742c1b001SThomas Moestl {
17682a79fd39SMarius Strobl 	struct gem_softc *sc;
176942c1b001SThomas Moestl 	int n;
17702a79fd39SMarius Strobl 	uint32_t v;
177142c1b001SThomas Moestl 
177242c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY
17731ed3fed7SMarius Strobl 	printf("%s: phy %d reg %d\n", __func__, phy, reg);
177442c1b001SThomas Moestl #endif
177542c1b001SThomas Moestl 
17762a79fd39SMarius Strobl 	sc = device_get_softc(dev);
17771ed3fed7SMarius Strobl 	if (sc->sc_phyad != -1 && phy != sc->sc_phyad)
17781ed3fed7SMarius Strobl 		return (0);
17791ed3fed7SMarius Strobl 
17801ed3fed7SMarius Strobl 	if ((sc->sc_flags & GEM_SERDES) != 0) {
17811ed3fed7SMarius Strobl 		switch (reg) {
17821ed3fed7SMarius Strobl 		case MII_BMCR:
17831ed3fed7SMarius Strobl 			reg = GEM_MII_CONTROL;
17841ed3fed7SMarius Strobl 			break;
17851ed3fed7SMarius Strobl 		case MII_BMSR:
17861ed3fed7SMarius Strobl 			reg = GEM_MII_STATUS;
17871ed3fed7SMarius Strobl 			break;
17881ed3fed7SMarius Strobl 		case MII_PHYIDR1:
17891ed3fed7SMarius Strobl 		case MII_PHYIDR2:
17901ed3fed7SMarius Strobl 			return (0);
17911ed3fed7SMarius Strobl 		case MII_ANAR:
17921ed3fed7SMarius Strobl 			reg = GEM_MII_ANAR;
17931ed3fed7SMarius Strobl 			break;
17941ed3fed7SMarius Strobl 		case MII_ANLPAR:
17951ed3fed7SMarius Strobl 			reg = GEM_MII_ANLPAR;
17961ed3fed7SMarius Strobl 			break;
17971ed3fed7SMarius Strobl 		case MII_EXTSR:
17981ed3fed7SMarius Strobl 			return (EXTSR_1000XFDX | EXTSR_1000XHDX);
17991ed3fed7SMarius Strobl 		default:
18001ed3fed7SMarius Strobl 			device_printf(sc->sc_dev,
18011ed3fed7SMarius Strobl 			    "%s: unhandled register %d\n", __func__, reg);
18021ed3fed7SMarius Strobl 			return (0);
18031ed3fed7SMarius Strobl 		}
1804bd3d9826SMarius Strobl 		return (GEM_BANK1_READ_4(sc, reg));
18051ed3fed7SMarius Strobl 	}
180642c1b001SThomas Moestl 
18072a79fd39SMarius Strobl 	/* Construct the frame command. */
18081ed3fed7SMarius Strobl 	v = GEM_MIF_FRAME_READ |
18091ed3fed7SMarius Strobl 	    (phy << GEM_MIF_PHY_SHIFT) |
18101ed3fed7SMarius Strobl 	    (reg << GEM_MIF_REG_SHIFT);
181142c1b001SThomas Moestl 
1812bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MIF_FRAME, v);
1813ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MIF_FRAME, 4,
1814ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
181542c1b001SThomas Moestl 	for (n = 0; n < 100; n++) {
181642c1b001SThomas Moestl 		DELAY(1);
1817bd3d9826SMarius Strobl 		v = GEM_BANK1_READ_4(sc, GEM_MIF_FRAME);
18181f317bf9SMarius Strobl 		if (v & GEM_MIF_FRAME_TA0)
181942c1b001SThomas Moestl 			return (v & GEM_MIF_FRAME_DATA);
182042c1b001SThomas Moestl 	}
182142c1b001SThomas Moestl 
18222a79fd39SMarius Strobl 	device_printf(sc->sc_dev, "%s: timed out\n", __func__);
182342c1b001SThomas Moestl 	return (0);
182442c1b001SThomas Moestl }
182542c1b001SThomas Moestl 
182642c1b001SThomas Moestl int
18272a79fd39SMarius Strobl gem_mii_writereg(device_t dev, int phy, int reg, int val)
182842c1b001SThomas Moestl {
18292a79fd39SMarius Strobl 	struct gem_softc *sc;
183042c1b001SThomas Moestl 	int n;
18312a79fd39SMarius Strobl 	uint32_t v;
183242c1b001SThomas Moestl 
183342c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY
18341ed3fed7SMarius Strobl 	printf("%s: phy %d reg %d val %x\n", phy, reg, val, __func__);
183542c1b001SThomas Moestl #endif
183642c1b001SThomas Moestl 
18372a79fd39SMarius Strobl 	sc = device_get_softc(dev);
18381ed3fed7SMarius Strobl 	if (sc->sc_phyad != -1 && phy != sc->sc_phyad)
18391ed3fed7SMarius Strobl 		return (0);
18401ed3fed7SMarius Strobl 
18411ed3fed7SMarius Strobl 	if ((sc->sc_flags & GEM_SERDES) != 0) {
18421ed3fed7SMarius Strobl 		switch (reg) {
18431ed3fed7SMarius Strobl 		case MII_BMSR:
18441ed3fed7SMarius Strobl 			reg = GEM_MII_STATUS;
18451ed3fed7SMarius Strobl 			break;
1846ccb1212aSMarius Strobl 		case MII_BMCR:
1847ccb1212aSMarius Strobl 			reg = GEM_MII_CONTROL;
1848ccb1212aSMarius Strobl 			if ((val & GEM_MII_CONTROL_RESET) == 0)
1849ccb1212aSMarius Strobl 				break;
1850ccb1212aSMarius Strobl 			GEM_BANK1_WRITE_4(sc, GEM_MII_CONTROL, val);
1851ccb1212aSMarius Strobl 			GEM_BANK1_BARRIER(sc, GEM_MII_CONTROL, 4,
1852ccb1212aSMarius Strobl 			    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
1853ccb1212aSMarius Strobl 			if (!GEM_BANK1_BITWAIT(sc, GEM_MII_CONTROL,
1854ccb1212aSMarius Strobl 			    GEM_MII_CONTROL_RESET, 0))
1855ccb1212aSMarius Strobl 				device_printf(sc->sc_dev,
1856ccb1212aSMarius Strobl 				    "cannot reset PCS\n");
1857ccb1212aSMarius Strobl 			/* FALLTHROUGH */
18581ed3fed7SMarius Strobl 		case MII_ANAR:
1859bd3d9826SMarius Strobl 			GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG, 0);
1860bd3d9826SMarius Strobl 			GEM_BANK1_BARRIER(sc, GEM_MII_CONFIG, 4,
18611ed3fed7SMarius Strobl 			    BUS_SPACE_BARRIER_WRITE);
1862bd3d9826SMarius Strobl 			GEM_BANK1_WRITE_4(sc, GEM_MII_ANAR, val);
1863bd3d9826SMarius Strobl 			GEM_BANK1_WRITE_4(sc, GEM_MII_SLINK_CONTROL,
18641ed3fed7SMarius Strobl 			    GEM_MII_SLINK_LOOPBACK | GEM_MII_SLINK_EN_SYNC_D);
1865bd3d9826SMarius Strobl 			GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG,
18661ed3fed7SMarius Strobl 			    GEM_MII_CONFIG_ENABLE);
18671ed3fed7SMarius Strobl 			return (0);
18681ed3fed7SMarius Strobl 		case MII_ANLPAR:
18691ed3fed7SMarius Strobl 			reg = GEM_MII_ANLPAR;
18701ed3fed7SMarius Strobl 			break;
18711ed3fed7SMarius Strobl 		default:
18721ed3fed7SMarius Strobl 			device_printf(sc->sc_dev,
18731ed3fed7SMarius Strobl 			    "%s: unhandled register %d\n", __func__, reg);
18741ed3fed7SMarius Strobl 			return (0);
18751ed3fed7SMarius Strobl 		}
1876bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, reg, val);
18771ed3fed7SMarius Strobl 		return (0);
18781ed3fed7SMarius Strobl 	}
18791ed3fed7SMarius Strobl 
18802a79fd39SMarius Strobl 	/* Construct the frame command. */
188142c1b001SThomas Moestl 	v = GEM_MIF_FRAME_WRITE |
188242c1b001SThomas Moestl 	    (phy << GEM_MIF_PHY_SHIFT) |
188342c1b001SThomas Moestl 	    (reg << GEM_MIF_REG_SHIFT) |
188442c1b001SThomas Moestl 	    (val & GEM_MIF_FRAME_DATA);
188542c1b001SThomas Moestl 
1886bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MIF_FRAME, v);
1887ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MIF_FRAME, 4,
1888ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
188942c1b001SThomas Moestl 	for (n = 0; n < 100; n++) {
189042c1b001SThomas Moestl 		DELAY(1);
1891bd3d9826SMarius Strobl 		v = GEM_BANK1_READ_4(sc, GEM_MIF_FRAME);
18921f317bf9SMarius Strobl 		if (v & GEM_MIF_FRAME_TA0)
189342c1b001SThomas Moestl 			return (1);
189442c1b001SThomas Moestl 	}
189542c1b001SThomas Moestl 
18962a79fd39SMarius Strobl 	device_printf(sc->sc_dev, "%s: timed out\n", __func__);
189742c1b001SThomas Moestl 	return (0);
189842c1b001SThomas Moestl }
189942c1b001SThomas Moestl 
190042c1b001SThomas Moestl void
19012a79fd39SMarius Strobl gem_mii_statchg(device_t dev)
190242c1b001SThomas Moestl {
19032a79fd39SMarius Strobl 	struct gem_softc *sc;
19041ed3fed7SMarius Strobl 	int gigabit;
19051ed3fed7SMarius Strobl 	uint32_t rxcfg, txcfg, v;
190642c1b001SThomas Moestl 
19072a79fd39SMarius Strobl 	sc = device_get_softc(dev);
19082a79fd39SMarius Strobl 
190942c1b001SThomas Moestl #ifdef GEM_DEBUG
19102a79fd39SMarius Strobl 	if ((sc->sc_ifp->if_flags & IFF_DEBUG) != 0)
19111ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "%s: status change: PHY = %d\n",
19121ed3fed7SMarius Strobl 		    __func__, sc->sc_phyad);
191342c1b001SThomas Moestl #endif
191442c1b001SThomas Moestl 
19151ed3fed7SMarius Strobl 	if ((sc->sc_mii->mii_media_status & IFM_ACTIVE) != 0 &&
19161ed3fed7SMarius Strobl 	    IFM_SUBTYPE(sc->sc_mii->mii_media_active) != IFM_NONE)
19171ed3fed7SMarius Strobl 		sc->sc_flags |= GEM_LINK;
19181ed3fed7SMarius Strobl 	else
19191ed3fed7SMarius Strobl 		sc->sc_flags &= ~GEM_LINK;
19201ed3fed7SMarius Strobl 
19211ed3fed7SMarius Strobl 	switch (IFM_SUBTYPE(sc->sc_mii->mii_media_active)) {
19221ed3fed7SMarius Strobl 	case IFM_1000_SX:
19231ed3fed7SMarius Strobl 	case IFM_1000_LX:
19241ed3fed7SMarius Strobl 	case IFM_1000_CX:
19251ed3fed7SMarius Strobl 	case IFM_1000_T:
19261ed3fed7SMarius Strobl 		gigabit = 1;
19271ed3fed7SMarius Strobl 		break;
19281ed3fed7SMarius Strobl 	default:
19291ed3fed7SMarius Strobl 		gigabit = 0;
193042c1b001SThomas Moestl 	}
19311ed3fed7SMarius Strobl 
19321ed3fed7SMarius Strobl 	/*
19331ed3fed7SMarius Strobl 	 * The configuration done here corresponds to the steps F) and
19341ed3fed7SMarius Strobl 	 * G) and as far as enabling of RX and TX MAC goes also step H)
19351ed3fed7SMarius Strobl 	 * of the initialization sequence outlined in section 3.2.1 of
19361ed3fed7SMarius Strobl 	 * the GEM Gigabit Ethernet ASIC Specification.
19371ed3fed7SMarius Strobl 	 */
19381ed3fed7SMarius Strobl 
1939bd3d9826SMarius Strobl 	rxcfg = GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG);
19401ed3fed7SMarius Strobl 	rxcfg &= ~(GEM_MAC_RX_CARR_EXTEND | GEM_MAC_RX_ENABLE);
19411ed3fed7SMarius Strobl 	txcfg = GEM_MAC_TX_ENA_IPG0 | GEM_MAC_TX_NGU | GEM_MAC_TX_NGU_LIMIT;
19421ed3fed7SMarius Strobl 	if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0)
19431ed3fed7SMarius Strobl 		txcfg |= GEM_MAC_TX_IGN_CARRIER | GEM_MAC_TX_IGN_COLLIS;
19441ed3fed7SMarius Strobl 	else if (gigabit != 0) {
19451ed3fed7SMarius Strobl 		rxcfg |= GEM_MAC_RX_CARR_EXTEND;
19461ed3fed7SMarius Strobl 		txcfg |= GEM_MAC_TX_CARR_EXTEND;
19471ed3fed7SMarius Strobl 	}
1948bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, 0);
1949ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MAC_TX_CONFIG, 4,
1950ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
1951bd3d9826SMarius Strobl 	if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0))
19521ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "cannot disable TX MAC\n");
1953bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, txcfg);
1954bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 0);
1955ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4,
1956ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
1957bd3d9826SMarius Strobl 	if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0))
19581ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "cannot disable RX MAC\n");
1959bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, rxcfg);
19601ed3fed7SMarius Strobl 
1961bd3d9826SMarius Strobl 	v = GEM_BANK1_READ_4(sc, GEM_MAC_CONTROL_CONFIG) &
19621ed3fed7SMarius Strobl 	    ~(GEM_MAC_CC_RX_PAUSE | GEM_MAC_CC_TX_PAUSE);
19631ed3fed7SMarius Strobl #ifdef notyet
19642a79fd39SMarius Strobl 	if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) &
19652a79fd39SMarius Strobl 	    IFM_ETH_RXPAUSE) != 0)
19661ed3fed7SMarius Strobl 		v |= GEM_MAC_CC_RX_PAUSE;
19672a79fd39SMarius Strobl 	if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) &
19682a79fd39SMarius Strobl 	    IFM_ETH_TXPAUSE) != 0)
19691ed3fed7SMarius Strobl 		v |= GEM_MAC_CC_TX_PAUSE;
19701ed3fed7SMarius Strobl #endif
1971bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_CONFIG, v);
19721ed3fed7SMarius Strobl 
19731ed3fed7SMarius Strobl 	if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) == 0 &&
19741ed3fed7SMarius Strobl 	    gigabit != 0)
1975bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_SLOT_TIME,
19761ed3fed7SMarius Strobl 		    GEM_MAC_SLOT_TIME_CARR_EXTEND);
19771ed3fed7SMarius Strobl 	else
1978bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_SLOT_TIME,
19791ed3fed7SMarius Strobl 		    GEM_MAC_SLOT_TIME_NORMAL);
198042c1b001SThomas Moestl 
198142c1b001SThomas Moestl 	/* XIF Configuration */
198242c1b001SThomas Moestl 	v = GEM_MAC_XIF_LINK_LED;
198342c1b001SThomas Moestl 	v |= GEM_MAC_XIF_TX_MII_ENA;
19841ed3fed7SMarius Strobl 	if ((sc->sc_flags & GEM_SERDES) == 0) {
1985bd3d9826SMarius Strobl 		if ((GEM_BANK1_READ_4(sc, GEM_MIF_CONFIG) &
198678d22f42SMarius Strobl 		    GEM_MIF_CONFIG_PHY_SEL) != 0) {
198742c1b001SThomas Moestl 			/* External MII needs echo disable if half duplex. */
198878d22f42SMarius Strobl 		    	if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) &
198978d22f42SMarius Strobl 			    IFM_FDX) == 0)
199042c1b001SThomas Moestl 				v |= GEM_MAC_XIF_ECHO_DISABL;
199178d22f42SMarius Strobl 		} else
19921ed3fed7SMarius Strobl 			/*
19931ed3fed7SMarius Strobl 			 * Internal MII needs buffer enable.
19941ed3fed7SMarius Strobl 			 * XXX buffer enable makes only sense for an
19951ed3fed7SMarius Strobl 			 * external PHY.
19961ed3fed7SMarius Strobl 			 */
199742c1b001SThomas Moestl 			v |= GEM_MAC_XIF_MII_BUF_ENA;
199842c1b001SThomas Moestl 	}
19991ed3fed7SMarius Strobl 	if (gigabit != 0)
20001ed3fed7SMarius Strobl 		v |= GEM_MAC_XIF_GMII_MODE;
20011ed3fed7SMarius Strobl 	if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0)
20021ed3fed7SMarius Strobl 		v |= GEM_MAC_XIF_FDPLX_LED;
2003bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_XIF_CONFIG, v);
20041ed3fed7SMarius Strobl 
20051ed3fed7SMarius Strobl 	if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
20061ed3fed7SMarius Strobl 	    (sc->sc_flags & GEM_LINK) != 0) {
2007bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG,
20081ed3fed7SMarius Strobl 		    txcfg | GEM_MAC_TX_ENABLE);
2009bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG,
20101ed3fed7SMarius Strobl 		    rxcfg | GEM_MAC_RX_ENABLE);
20111ed3fed7SMarius Strobl 	}
201242c1b001SThomas Moestl }
201342c1b001SThomas Moestl 
201442c1b001SThomas Moestl int
20152a79fd39SMarius Strobl gem_mediachange(struct ifnet *ifp)
201642c1b001SThomas Moestl {
201742c1b001SThomas Moestl 	struct gem_softc *sc = ifp->if_softc;
20181f317bf9SMarius Strobl 	int error;
201942c1b001SThomas Moestl 
20202a79fd39SMarius Strobl 	/* XXX add support for serial media. */
202142c1b001SThomas Moestl 
20221f317bf9SMarius Strobl 	GEM_LOCK(sc);
20231f317bf9SMarius Strobl 	error = mii_mediachg(sc->sc_mii);
20241f317bf9SMarius Strobl 	GEM_UNLOCK(sc);
20251f317bf9SMarius Strobl 	return (error);
202642c1b001SThomas Moestl }
202742c1b001SThomas Moestl 
202842c1b001SThomas Moestl void
20292a79fd39SMarius Strobl gem_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
203042c1b001SThomas Moestl {
203142c1b001SThomas Moestl 	struct gem_softc *sc = ifp->if_softc;
203242c1b001SThomas Moestl 
20338cfaff7dSMarius Strobl 	GEM_LOCK(sc);
20348cfaff7dSMarius Strobl 	if ((ifp->if_flags & IFF_UP) == 0) {
20358cfaff7dSMarius Strobl 		GEM_UNLOCK(sc);
203642c1b001SThomas Moestl 		return;
20378cfaff7dSMarius Strobl 	}
203842c1b001SThomas Moestl 
203942c1b001SThomas Moestl 	mii_pollstat(sc->sc_mii);
204042c1b001SThomas Moestl 	ifmr->ifm_active = sc->sc_mii->mii_media_active;
204142c1b001SThomas Moestl 	ifmr->ifm_status = sc->sc_mii->mii_media_status;
20428cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
204342c1b001SThomas Moestl }
204442c1b001SThomas Moestl 
204542c1b001SThomas Moestl static int
20462a79fd39SMarius Strobl gem_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
204742c1b001SThomas Moestl {
204842c1b001SThomas Moestl 	struct gem_softc *sc = ifp->if_softc;
204942c1b001SThomas Moestl 	struct ifreq *ifr = (struct ifreq *)data;
20502a79fd39SMarius Strobl 	int error;
20518cfaff7dSMarius Strobl 
20522a79fd39SMarius Strobl 	error = 0;
205342c1b001SThomas Moestl 	switch (cmd) {
205442c1b001SThomas Moestl 	case SIOCSIFFLAGS:
20551f317bf9SMarius Strobl 		GEM_LOCK(sc);
20562a79fd39SMarius Strobl 		if ((ifp->if_flags & IFF_UP) != 0) {
20571ed3fed7SMarius Strobl 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
20581ed3fed7SMarius Strobl 			    ((ifp->if_flags ^ sc->sc_ifflags) &
20591ed3fed7SMarius Strobl 			    (IFF_ALLMULTI | IFF_PROMISC)) != 0)
206042c1b001SThomas Moestl 				gem_setladrf(sc);
206142c1b001SThomas Moestl 			else
20628cfaff7dSMarius Strobl 				gem_init_locked(sc);
20632a79fd39SMarius Strobl 		} else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
206442c1b001SThomas Moestl 			gem_stop(ifp, 0);
206512fb0330SPyun YongHyeon 		if ((ifp->if_flags & IFF_LINK0) != 0)
206612fb0330SPyun YongHyeon 			sc->sc_csum_features |= CSUM_UDP;
206712fb0330SPyun YongHyeon 		else
206812fb0330SPyun YongHyeon 			sc->sc_csum_features &= ~CSUM_UDP;
206912fb0330SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
207012fb0330SPyun YongHyeon 			ifp->if_hwassist = sc->sc_csum_features;
2071336cca9eSBenno Rice 		sc->sc_ifflags = ifp->if_flags;
20721f317bf9SMarius Strobl 		GEM_UNLOCK(sc);
207342c1b001SThomas Moestl 		break;
207442c1b001SThomas Moestl 	case SIOCADDMULTI:
207542c1b001SThomas Moestl 	case SIOCDELMULTI:
20761f317bf9SMarius Strobl 		GEM_LOCK(sc);
207742c1b001SThomas Moestl 		gem_setladrf(sc);
20781f317bf9SMarius Strobl 		GEM_UNLOCK(sc);
207942c1b001SThomas Moestl 		break;
208042c1b001SThomas Moestl 	case SIOCGIFMEDIA:
208142c1b001SThomas Moestl 	case SIOCSIFMEDIA:
208242c1b001SThomas Moestl 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, cmd);
208342c1b001SThomas Moestl 		break;
208412fb0330SPyun YongHyeon 	case SIOCSIFCAP:
208512fb0330SPyun YongHyeon 		GEM_LOCK(sc);
208612fb0330SPyun YongHyeon 		ifp->if_capenable = ifr->ifr_reqcap;
208712fb0330SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
208812fb0330SPyun YongHyeon 			ifp->if_hwassist = sc->sc_csum_features;
208912fb0330SPyun YongHyeon 		else
209012fb0330SPyun YongHyeon 			ifp->if_hwassist = 0;
209112fb0330SPyun YongHyeon 		GEM_UNLOCK(sc);
209212fb0330SPyun YongHyeon 		break;
209342c1b001SThomas Moestl 	default:
20941f317bf9SMarius Strobl 		error = ether_ioctl(ifp, cmd, data);
209542c1b001SThomas Moestl 		break;
209642c1b001SThomas Moestl 	}
209742c1b001SThomas Moestl 
209842c1b001SThomas Moestl 	return (error);
209942c1b001SThomas Moestl }
210042c1b001SThomas Moestl 
210142c1b001SThomas Moestl static void
21022a79fd39SMarius Strobl gem_setladrf(struct gem_softc *sc)
210342c1b001SThomas Moestl {
2104fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
210542c1b001SThomas Moestl 	struct ifmultiaddr *inm;
2106336cca9eSBenno Rice 	int i;
21072a79fd39SMarius Strobl 	uint32_t hash[16];
21082a79fd39SMarius Strobl 	uint32_t crc, v;
210942c1b001SThomas Moestl 
21108cfaff7dSMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
21118cfaff7dSMarius Strobl 
21122a79fd39SMarius Strobl 	/* Get the current RX configuration. */
2113bd3d9826SMarius Strobl 	v = GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG);
211442c1b001SThomas Moestl 
2115336cca9eSBenno Rice 	/*
2116336cca9eSBenno Rice 	 * Turn off promiscuous mode, promiscuous group mode (all multicast),
2117336cca9eSBenno Rice 	 * and hash filter.  Depending on the case, the right bit will be
2118336cca9eSBenno Rice 	 * enabled.
2119336cca9eSBenno Rice 	 */
2120336cca9eSBenno Rice 	v &= ~(GEM_MAC_RX_PROMISCUOUS | GEM_MAC_RX_HASH_FILTER |
2121336cca9eSBenno Rice 	    GEM_MAC_RX_PROMISC_GRP);
2122336cca9eSBenno Rice 
2123bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, v);
2124ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4,
2125ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
2126bd3d9826SMarius Strobl 	if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_HASH_FILTER,
2127bd3d9826SMarius Strobl 	    0))
21281ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "cannot disable RX hash filter\n");
21291ed3fed7SMarius Strobl 
213042c1b001SThomas Moestl 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
213142c1b001SThomas Moestl 		v |= GEM_MAC_RX_PROMISCUOUS;
213242c1b001SThomas Moestl 		goto chipit;
213342c1b001SThomas Moestl 	}
213442c1b001SThomas Moestl 	if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
2135336cca9eSBenno Rice 		v |= GEM_MAC_RX_PROMISC_GRP;
213642c1b001SThomas Moestl 		goto chipit;
213742c1b001SThomas Moestl 	}
213842c1b001SThomas Moestl 
213942c1b001SThomas Moestl 	/*
21402a79fd39SMarius Strobl 	 * Set up multicast address filter by passing all multicast
21412a79fd39SMarius Strobl 	 * addresses through a crc generator, and then using the high
21422a79fd39SMarius Strobl 	 * order 8 bits as an index into the 256 bit logical address
21432a79fd39SMarius Strobl 	 * filter.  The high order 4 bits selects the word, while the
21442a79fd39SMarius Strobl 	 * other 4 bits select the bit within the word (where bit 0
21452a79fd39SMarius Strobl 	 * is the MSB).
214642c1b001SThomas Moestl 	 */
214742c1b001SThomas Moestl 
21482a79fd39SMarius Strobl 	/* Clear the hash table. */
2149336cca9eSBenno Rice 	memset(hash, 0, sizeof(hash));
2150336cca9eSBenno Rice 
215113b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
2152fc74a9f9SBrooks Davis 	TAILQ_FOREACH(inm, &ifp->if_multiaddrs, ifma_link) {
215342c1b001SThomas Moestl 		if (inm->ifma_addr->sa_family != AF_LINK)
215442c1b001SThomas Moestl 			continue;
2155c240bd8cSMarius Strobl 		crc = ether_crc32_le(LLADDR((struct sockaddr_dl *)
2156c240bd8cSMarius Strobl 		    inm->ifma_addr), ETHER_ADDR_LEN);
215742c1b001SThomas Moestl 
21582a79fd39SMarius Strobl 		/* We just want the 8 most significant bits. */
215942c1b001SThomas Moestl 		crc >>= 24;
216042c1b001SThomas Moestl 
216142c1b001SThomas Moestl 		/* Set the corresponding bit in the filter. */
2162336cca9eSBenno Rice 		hash[crc >> 4] |= 1 << (15 - (crc & 15));
2163336cca9eSBenno Rice 	}
216413b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
2165336cca9eSBenno Rice 
2166336cca9eSBenno Rice 	v |= GEM_MAC_RX_HASH_FILTER;
2167336cca9eSBenno Rice 
21682a79fd39SMarius Strobl 	/* Now load the hash table into the chip (if we are using it). */
21692a79fd39SMarius Strobl 	for (i = 0; i < 16; i++)
2170bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc,
2171336cca9eSBenno Rice 		    GEM_MAC_HASH0 + i * (GEM_MAC_HASH1 - GEM_MAC_HASH0),
2172336cca9eSBenno Rice 		    hash[i]);
217342c1b001SThomas Moestl 
217442c1b001SThomas Moestl  chipit:
2175bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, v);
217642c1b001SThomas Moestl }
2177