xref: /freebsd/sys/dev/gem/if_gem.c (revision 3e38757d4c52f7c2b33e4ab667ebc55e334a6ca0)
1aad970f1SDavid E. O'Brien /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
3718cf2ccSPedro F. Giffuni  *
442c1b001SThomas Moestl  * Copyright (C) 2001 Eduardo Horvath.
5305f2c06SThomas Moestl  * Copyright (c) 2001-2003 Thomas Moestl
62a79fd39SMarius Strobl  * Copyright (c) 2007 Marius Strobl <marius@FreeBSD.org>
742c1b001SThomas Moestl  * All rights reserved.
842c1b001SThomas Moestl  *
942c1b001SThomas Moestl  * Redistribution and use in source and binary forms, with or without
1042c1b001SThomas Moestl  * modification, are permitted provided that the following conditions
1142c1b001SThomas Moestl  * are met:
1242c1b001SThomas Moestl  * 1. Redistributions of source code must retain the above copyright
1342c1b001SThomas Moestl  *    notice, this list of conditions and the following disclaimer.
1442c1b001SThomas Moestl  * 2. Redistributions in binary form must reproduce the above copyright
1542c1b001SThomas Moestl  *    notice, this list of conditions and the following disclaimer in the
1642c1b001SThomas Moestl  *    documentation and/or other materials provided with the distribution.
1742c1b001SThomas Moestl  *
1842c1b001SThomas Moestl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
1942c1b001SThomas Moestl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2042c1b001SThomas Moestl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2142c1b001SThomas Moestl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
2242c1b001SThomas Moestl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2342c1b001SThomas Moestl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2442c1b001SThomas Moestl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2542c1b001SThomas Moestl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2642c1b001SThomas Moestl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2742c1b001SThomas Moestl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2842c1b001SThomas Moestl  * SUCH DAMAGE.
2942c1b001SThomas Moestl  *
30336cca9eSBenno Rice  *	from: NetBSD: gem.c,v 1.21 2002/06/01 23:50:58 lukem Exp
3142c1b001SThomas Moestl  */
3242c1b001SThomas Moestl 
33aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
34aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
35aad970f1SDavid E. O'Brien 
3642c1b001SThomas Moestl /*
371ed3fed7SMarius Strobl  * Driver for Apple GMAC, Sun ERI and Sun GEM Ethernet controllers
3842c1b001SThomas Moestl  */
3942c1b001SThomas Moestl 
4018100346SThomas Moestl #if 0
4142c1b001SThomas Moestl #define	GEM_DEBUG
4218100346SThomas Moestl #endif
4342c1b001SThomas Moestl 
44c3d5598aSMarius Strobl #if 0	/* XXX: In case of emergency, re-enable this. */
45c3d5598aSMarius Strobl #define	GEM_RINT_TIMEOUT
46c3d5598aSMarius Strobl #endif
47c3d5598aSMarius Strobl 
4842c1b001SThomas Moestl #include <sys/param.h>
4942c1b001SThomas Moestl #include <sys/systm.h>
5042c1b001SThomas Moestl #include <sys/bus.h>
5142c1b001SThomas Moestl #include <sys/callout.h>
52a30d4b32SMike Barcroft #include <sys/endian.h>
5342c1b001SThomas Moestl #include <sys/mbuf.h>
5442c1b001SThomas Moestl #include <sys/malloc.h>
5542c1b001SThomas Moestl #include <sys/kernel.h>
568cfaff7dSMarius Strobl #include <sys/lock.h>
57186f2b9eSPoul-Henning Kamp #include <sys/module.h>
588cfaff7dSMarius Strobl #include <sys/mutex.h>
5942c1b001SThomas Moestl #include <sys/socket.h>
6042c1b001SThomas Moestl #include <sys/sockio.h>
61e1bb13cdSPoul-Henning Kamp #include <sys/rman.h>
6242c1b001SThomas Moestl 
6308e0fdebSThomas Moestl #include <net/bpf.h>
6442c1b001SThomas Moestl #include <net/ethernet.h>
6542c1b001SThomas Moestl #include <net/if.h>
6676039bc8SGleb Smirnoff #include <net/if_var.h>
6742c1b001SThomas Moestl #include <net/if_arp.h>
6842c1b001SThomas Moestl #include <net/if_dl.h>
6942c1b001SThomas Moestl #include <net/if_media.h>
70fc74a9f9SBrooks Davis #include <net/if_types.h>
7100d12766SMarius Strobl #include <net/if_vlan_var.h>
7242c1b001SThomas Moestl 
7312fb0330SPyun YongHyeon #include <netinet/in.h>
7412fb0330SPyun YongHyeon #include <netinet/in_systm.h>
7512fb0330SPyun YongHyeon #include <netinet/ip.h>
7612fb0330SPyun YongHyeon #include <netinet/tcp.h>
7712fb0330SPyun YongHyeon #include <netinet/udp.h>
7812fb0330SPyun YongHyeon 
7942c1b001SThomas Moestl #include <machine/bus.h>
8042c1b001SThomas Moestl 
8142c1b001SThomas Moestl #include <dev/mii/mii.h>
8242c1b001SThomas Moestl #include <dev/mii/miivar.h>
8342c1b001SThomas Moestl 
84681f7d03SWarner Losh #include <dev/gem/if_gemreg.h>
85681f7d03SWarner Losh #include <dev/gem/if_gemvar.h>
8642c1b001SThomas Moestl 
871ed3fed7SMarius Strobl CTASSERT(powerof2(GEM_NRXDESC) && GEM_NRXDESC >= 32 && GEM_NRXDESC <= 8192);
881ed3fed7SMarius Strobl CTASSERT(powerof2(GEM_NTXDESC) && GEM_NTXDESC >= 32 && GEM_NTXDESC <= 8192);
891ed3fed7SMarius Strobl 
909ba2b298SMarius Strobl #define	GEM_TRIES	10000
911ed3fed7SMarius Strobl 
9212fb0330SPyun YongHyeon /*
9378d22f42SMarius Strobl  * The hardware supports basic TCP/UDP checksum offloading.  However,
9412fb0330SPyun YongHyeon  * the hardware doesn't compensate the checksum for UDP datagram which
9512fb0330SPyun YongHyeon  * can yield to 0x0.  As a safe guard, UDP checksum offload is disabled
9612fb0330SPyun YongHyeon  * by default.  It can be reactivated by setting special link option
9712fb0330SPyun YongHyeon  * link0 with ifconfig(8).
9812fb0330SPyun YongHyeon  */
9912fb0330SPyun YongHyeon #define	GEM_CSUM_FEATURES	(CSUM_TCP)
10042c1b001SThomas Moestl 
1012a79fd39SMarius Strobl static int	gem_add_rxbuf(struct gem_softc *sc, int idx);
102bd3d9826SMarius Strobl static int	gem_bitwait(struct gem_softc *sc, u_int bank, bus_addr_t r,
103bd3d9826SMarius Strobl 		    uint32_t clr, uint32_t set);
1042a79fd39SMarius Strobl static void	gem_cddma_callback(void *xsc, bus_dma_segment_t *segs,
1052a79fd39SMarius Strobl 		    int nsegs, int error);
1062a79fd39SMarius Strobl static int	gem_disable_rx(struct gem_softc *sc);
1072a79fd39SMarius Strobl static int	gem_disable_tx(struct gem_softc *sc);
1082a79fd39SMarius Strobl static void	gem_eint(struct gem_softc *sc, u_int status);
1092a79fd39SMarius Strobl static void	gem_init(void *xsc);
1102a79fd39SMarius Strobl static void	gem_init_locked(struct gem_softc *sc);
1112a79fd39SMarius Strobl static void	gem_init_regs(struct gem_softc *sc);
1122a79fd39SMarius Strobl static int	gem_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
1132a79fd39SMarius Strobl static int	gem_load_txmbuf(struct gem_softc *sc, struct mbuf **m_head);
1142a79fd39SMarius Strobl static int	gem_meminit(struct gem_softc *sc);
1152a79fd39SMarius Strobl static void	gem_mifinit(struct gem_softc *sc);
1162a79fd39SMarius Strobl static void	gem_reset(struct gem_softc *sc);
1172a79fd39SMarius Strobl static int	gem_reset_rx(struct gem_softc *sc);
1181ed3fed7SMarius Strobl static void	gem_reset_rxdma(struct gem_softc *sc);
1192a79fd39SMarius Strobl static int	gem_reset_tx(struct gem_softc *sc);
1202a79fd39SMarius Strobl static u_int	gem_ringsize(u_int sz);
1212a79fd39SMarius Strobl static void	gem_rint(struct gem_softc *sc);
122c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT
1232a79fd39SMarius Strobl static void	gem_rint_timeout(void *arg);
12411e3f060SJake Burkholder #endif
1259ba2b298SMarius Strobl static inline void gem_rxcksum(struct mbuf *m, uint64_t flags);
1262a79fd39SMarius Strobl static void	gem_rxdrain(struct gem_softc *sc);
1275ed0b954SMarius Strobl static void	gem_setladrf(struct gem_softc *sc);
1282a79fd39SMarius Strobl static void	gem_start(struct ifnet *ifp);
1292a79fd39SMarius Strobl static void	gem_start_locked(struct ifnet *ifp);
1302a79fd39SMarius Strobl static void	gem_stop(struct ifnet *ifp, int disable);
1312a79fd39SMarius Strobl static void	gem_tick(void *arg);
1322a79fd39SMarius Strobl static void	gem_tint(struct gem_softc *sc);
1339ba2b298SMarius Strobl static inline void gem_txkick(struct gem_softc *sc);
1342a79fd39SMarius Strobl static int	gem_watchdog(struct gem_softc *sc);
13542c1b001SThomas Moestl 
13642c1b001SThomas Moestl devclass_t gem_devclass;
137*3e38757dSJohn Baldwin DRIVER_MODULE(miibus, gem, miibus_driver, 0, 0);
13842c1b001SThomas Moestl MODULE_DEPEND(gem, miibus, 1, 1, 1);
13942c1b001SThomas Moestl 
14042c1b001SThomas Moestl #ifdef GEM_DEBUG
14142c1b001SThomas Moestl #include <sys/ktr.h>
142651aa2d8SAttilio Rao #define	KTR_GEM		KTR_SPARE2
14342c1b001SThomas Moestl #endif
14442c1b001SThomas Moestl 
145bd3d9826SMarius Strobl #define	GEM_BANK1_BITWAIT(sc, r, clr, set)				\
146bd3d9826SMarius Strobl 	gem_bitwait((sc), GEM_RES_BANK1, (r), (clr), (set))
147bd3d9826SMarius Strobl #define	GEM_BANK2_BITWAIT(sc, r, clr, set)				\
148bd3d9826SMarius Strobl 	gem_bitwait((sc), GEM_RES_BANK2, (r), (clr), (set))
149bd3d9826SMarius Strobl 
15042c1b001SThomas Moestl int
1512a79fd39SMarius Strobl gem_attach(struct gem_softc *sc)
15242c1b001SThomas Moestl {
1532a79fd39SMarius Strobl 	struct gem_txsoft *txs;
154fc74a9f9SBrooks Davis 	struct ifnet *ifp;
1558e5d93dbSMarius Strobl 	int error, i, phy;
1562a79fd39SMarius Strobl 	uint32_t v;
15742c1b001SThomas Moestl 
1589ba2b298SMarius Strobl 	if (bootverbose)
1599ba2b298SMarius Strobl 		device_printf(sc->sc_dev, "flags=0x%x\n", sc->sc_flags);
1609ba2b298SMarius Strobl 
1619ba2b298SMarius Strobl 	/* Set up ifnet structure. */
162fc74a9f9SBrooks Davis 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
163fc74a9f9SBrooks Davis 	if (ifp == NULL)
164fc74a9f9SBrooks Davis 		return (ENOSPC);
1659ba2b298SMarius Strobl 	sc->sc_csum_features = GEM_CSUM_FEATURES;
1669ba2b298SMarius Strobl 	ifp->if_softc = sc;
1679ba2b298SMarius Strobl 	if_initname(ifp, device_get_name(sc->sc_dev),
1689ba2b298SMarius Strobl 	    device_get_unit(sc->sc_dev));
1699ba2b298SMarius Strobl 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1709ba2b298SMarius Strobl 	ifp->if_start = gem_start;
1719ba2b298SMarius Strobl 	ifp->if_ioctl = gem_ioctl;
1729ba2b298SMarius Strobl 	ifp->if_init = gem_init;
1739ba2b298SMarius Strobl 	IFQ_SET_MAXLEN(&ifp->if_snd, GEM_TXQUEUELEN);
1749ba2b298SMarius Strobl 	ifp->if_snd.ifq_drv_maxlen = GEM_TXQUEUELEN;
1759ba2b298SMarius Strobl 	IFQ_SET_READY(&ifp->if_snd);
176fc74a9f9SBrooks Davis 
1771f317bf9SMarius Strobl 	callout_init_mtx(&sc->sc_tick_ch, &sc->sc_mtx, 0);
1781f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT
1791f317bf9SMarius Strobl 	callout_init_mtx(&sc->sc_rx_ch, &sc->sc_mtx, 0);
1801f317bf9SMarius Strobl #endif
1811f317bf9SMarius Strobl 
18242c1b001SThomas Moestl 	/* Make sure the chip is stopped. */
18342c1b001SThomas Moestl 	gem_reset(sc);
18442c1b001SThomas Moestl 
185378f231eSJohn-Mark Gurney 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
186378f231eSJohn-Mark Gurney 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1872a79fd39SMarius Strobl 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0, NULL,
1882a79fd39SMarius Strobl 	    NULL, &sc->sc_pdmatag);
1899ba2b298SMarius Strobl 	if (error != 0)
190fc74a9f9SBrooks Davis 		goto fail_ifnet;
19142c1b001SThomas Moestl 
19242c1b001SThomas Moestl 	error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0,
19312fb0330SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
19412fb0330SPyun YongHyeon 	    1, MCLBYTES, BUS_DMA_ALLOCNOW, NULL, NULL, &sc->sc_rdmatag);
1959ba2b298SMarius Strobl 	if (error != 0)
196305f2c06SThomas Moestl 		goto fail_ptag;
197305f2c06SThomas Moestl 
198305f2c06SThomas Moestl 	error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0,
19912fb0330SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
20012fb0330SPyun YongHyeon 	    MCLBYTES * GEM_NTXSEGS, GEM_NTXSEGS, MCLBYTES,
201f6b1c44dSScott Long 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->sc_tdmatag);
2029ba2b298SMarius Strobl 	if (error != 0)
203305f2c06SThomas Moestl 		goto fail_rtag;
20442c1b001SThomas Moestl 
20542c1b001SThomas Moestl 	error = bus_dma_tag_create(sc->sc_pdmatag, PAGE_SIZE, 0,
20612fb0330SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
20742c1b001SThomas Moestl 	    sizeof(struct gem_control_data), 1,
20812fb0330SPyun YongHyeon 	    sizeof(struct gem_control_data), 0,
20912fb0330SPyun YongHyeon 	    NULL, NULL, &sc->sc_cdmatag);
2109ba2b298SMarius Strobl 	if (error != 0)
211305f2c06SThomas Moestl 		goto fail_ttag;
21242c1b001SThomas Moestl 
21342c1b001SThomas Moestl 	/*
2142a79fd39SMarius Strobl 	 * Allocate the control data structures, create and load the
21542c1b001SThomas Moestl 	 * DMA map for it.
21642c1b001SThomas Moestl 	 */
21742c1b001SThomas Moestl 	if ((error = bus_dmamem_alloc(sc->sc_cdmatag,
21812fb0330SPyun YongHyeon 	    (void **)&sc->sc_control_data,
21912fb0330SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
2209ba2b298SMarius Strobl 	    &sc->sc_cddmamap)) != 0) {
2212a79fd39SMarius Strobl 		device_printf(sc->sc_dev,
2222a79fd39SMarius Strobl 		    "unable to allocate control data, error = %d\n", error);
223305f2c06SThomas Moestl 		goto fail_ctag;
22442c1b001SThomas Moestl 	}
22542c1b001SThomas Moestl 
22642c1b001SThomas Moestl 	sc->sc_cddma = 0;
22742c1b001SThomas Moestl 	if ((error = bus_dmamap_load(sc->sc_cdmatag, sc->sc_cddmamap,
22842c1b001SThomas Moestl 	    sc->sc_control_data, sizeof(struct gem_control_data),
22942c1b001SThomas Moestl 	    gem_cddma_callback, sc, 0)) != 0 || sc->sc_cddma == 0) {
2302a79fd39SMarius Strobl 		device_printf(sc->sc_dev,
2312a79fd39SMarius Strobl 		    "unable to load control data DMA map, error = %d\n",
2322a79fd39SMarius Strobl 		    error);
233305f2c06SThomas Moestl 		goto fail_cmem;
23442c1b001SThomas Moestl 	}
23542c1b001SThomas Moestl 
23642c1b001SThomas Moestl 	/*
23742c1b001SThomas Moestl 	 * Initialize the transmit job descriptors.
23842c1b001SThomas Moestl 	 */
23942c1b001SThomas Moestl 	STAILQ_INIT(&sc->sc_txfreeq);
24042c1b001SThomas Moestl 	STAILQ_INIT(&sc->sc_txdirtyq);
24142c1b001SThomas Moestl 
24242c1b001SThomas Moestl 	/*
24342c1b001SThomas Moestl 	 * Create the transmit buffer DMA maps.
24442c1b001SThomas Moestl 	 */
24542c1b001SThomas Moestl 	error = ENOMEM;
24642c1b001SThomas Moestl 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
24742c1b001SThomas Moestl 		txs = &sc->sc_txsoft[i];
24842c1b001SThomas Moestl 		txs->txs_mbuf = NULL;
24942c1b001SThomas Moestl 		txs->txs_ndescs = 0;
250305f2c06SThomas Moestl 		if ((error = bus_dmamap_create(sc->sc_tdmatag, 0,
25142c1b001SThomas Moestl 		    &txs->txs_dmamap)) != 0) {
2522a79fd39SMarius Strobl 			device_printf(sc->sc_dev,
2532a79fd39SMarius Strobl 			    "unable to create TX DMA map %d, error = %d\n",
2542a79fd39SMarius Strobl 			    i, error);
255305f2c06SThomas Moestl 			goto fail_txd;
25642c1b001SThomas Moestl 		}
25742c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
25842c1b001SThomas Moestl 	}
25942c1b001SThomas Moestl 
26042c1b001SThomas Moestl 	/*
26142c1b001SThomas Moestl 	 * Create the receive buffer DMA maps.
26242c1b001SThomas Moestl 	 */
26342c1b001SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
264305f2c06SThomas Moestl 		if ((error = bus_dmamap_create(sc->sc_rdmatag, 0,
26542c1b001SThomas Moestl 		    &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
2662a79fd39SMarius Strobl 			device_printf(sc->sc_dev,
2672a79fd39SMarius Strobl 			    "unable to create RX DMA map %d, error = %d\n",
2682a79fd39SMarius Strobl 			    i, error);
269305f2c06SThomas Moestl 			goto fail_rxd;
27042c1b001SThomas Moestl 		}
27142c1b001SThomas Moestl 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
27242c1b001SThomas Moestl 	}
27342c1b001SThomas Moestl 
27465f2c0ffSMarius Strobl 	/* Bypass probing PHYs if we already know for sure to use a SERDES. */
27565f2c0ffSMarius Strobl 	if ((sc->sc_flags & GEM_SERDES) != 0)
27665f2c0ffSMarius Strobl 		goto serdes;
27765f2c0ffSMarius Strobl 
2781ed3fed7SMarius Strobl 	/* Bad things will happen when touching this register on ERI. */
27965f2c0ffSMarius Strobl 	if (sc->sc_variant != GEM_SUN_ERI) {
280bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MII_DATAPATH_MODE,
2811ed3fed7SMarius Strobl 		    GEM_MII_DATAPATH_MII);
28265f2c0ffSMarius Strobl 		GEM_BANK1_BARRIER(sc, GEM_MII_DATAPATH_MODE, 4,
28365f2c0ffSMarius Strobl 		    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
28465f2c0ffSMarius Strobl 	}
2851ed3fed7SMarius Strobl 
28642c1b001SThomas Moestl 	gem_mifinit(sc);
28742c1b001SThomas Moestl 
2881ed3fed7SMarius Strobl 	/*
2891ed3fed7SMarius Strobl 	 * Look for an external PHY.
2901ed3fed7SMarius Strobl 	 */
2911ed3fed7SMarius Strobl 	error = ENXIO;
292bd3d9826SMarius Strobl 	v = GEM_BANK1_READ_4(sc, GEM_MIF_CONFIG);
2931ed3fed7SMarius Strobl 	if ((v & GEM_MIF_CONFIG_MDI1) != 0) {
2941ed3fed7SMarius Strobl 		v |= GEM_MIF_CONFIG_PHY_SEL;
295bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MIF_CONFIG, v);
29665f2c0ffSMarius Strobl 		GEM_BANK1_BARRIER(sc, GEM_MIF_CONFIG, 4,
29765f2c0ffSMarius Strobl 		    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
2981ed3fed7SMarius Strobl 		switch (sc->sc_variant) {
2991ed3fed7SMarius Strobl 		case GEM_SUN_ERI:
3008e5d93dbSMarius Strobl 			phy = GEM_PHYAD_EXTERNAL;
3011ed3fed7SMarius Strobl 			break;
3021ed3fed7SMarius Strobl 		default:
3038e5d93dbSMarius Strobl 			phy = MII_PHY_ANY;
3041ed3fed7SMarius Strobl 			break;
3051ed3fed7SMarius Strobl 		}
3068e5d93dbSMarius Strobl 		error = mii_attach(sc->sc_dev, &sc->sc_miibus, ifp,
3078e5d93dbSMarius Strobl 		    gem_mediachange, gem_mediastatus, BMSR_DEFCAPMASK, phy,
3089a68cbd3SMarius Strobl 		    MII_OFFSET_ANY, MIIF_DOPAUSE);
3091ed3fed7SMarius Strobl 	}
3101ed3fed7SMarius Strobl 
3111ed3fed7SMarius Strobl 	/*
3121ed3fed7SMarius Strobl 	 * Fall back on an internal PHY if no external PHY was found.
3139e48f1e7SMarius Strobl 	 * Note that with Apple (K2) GMACs GEM_MIF_CONFIG_MDI0 can't be
3149e48f1e7SMarius Strobl 	 * trusted when the firmware has powered down the chip.
3151ed3fed7SMarius Strobl 	 */
3169e48f1e7SMarius Strobl 	if (error != 0 &&
3179e48f1e7SMarius Strobl 	    ((v & GEM_MIF_CONFIG_MDI0) != 0 || GEM_IS_APPLE(sc))) {
3181ed3fed7SMarius Strobl 		v &= ~GEM_MIF_CONFIG_PHY_SEL;
319bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MIF_CONFIG, v);
32065f2c0ffSMarius Strobl 		GEM_BANK1_BARRIER(sc, GEM_MIF_CONFIG, 4,
32165f2c0ffSMarius Strobl 		    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
3221ed3fed7SMarius Strobl 		switch (sc->sc_variant) {
3231ed3fed7SMarius Strobl 		case GEM_SUN_ERI:
3241ed3fed7SMarius Strobl 		case GEM_APPLE_K2_GMAC:
3258e5d93dbSMarius Strobl 			phy = GEM_PHYAD_INTERNAL;
3261ed3fed7SMarius Strobl 			break;
3271ed3fed7SMarius Strobl 		case GEM_APPLE_GMAC:
3288e5d93dbSMarius Strobl 			phy = GEM_PHYAD_EXTERNAL;
3291ed3fed7SMarius Strobl 			break;
3301ed3fed7SMarius Strobl 		default:
3318e5d93dbSMarius Strobl 			phy = MII_PHY_ANY;
3321ed3fed7SMarius Strobl 			break;
3331ed3fed7SMarius Strobl 		}
3348e5d93dbSMarius Strobl 		error = mii_attach(sc->sc_dev, &sc->sc_miibus, ifp,
3358e5d93dbSMarius Strobl 		    gem_mediachange, gem_mediastatus, BMSR_DEFCAPMASK, phy,
3369a68cbd3SMarius Strobl 		    MII_OFFSET_ANY, MIIF_DOPAUSE);
3371ed3fed7SMarius Strobl 	}
3381ed3fed7SMarius Strobl 
3391ed3fed7SMarius Strobl 	/*
3401ed3fed7SMarius Strobl 	 * Try the external PCS SERDES if we didn't find any PHYs.
3411ed3fed7SMarius Strobl 	 */
3421ed3fed7SMarius Strobl 	if (error != 0 && sc->sc_variant == GEM_SUN_GEM) {
34365f2c0ffSMarius Strobl  serdes:
344bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MII_DATAPATH_MODE,
3451ed3fed7SMarius Strobl 		    GEM_MII_DATAPATH_SERDES);
34665f2c0ffSMarius Strobl 		GEM_BANK1_BARRIER(sc, GEM_MII_DATAPATH_MODE, 4,
34765f2c0ffSMarius Strobl 		    BUS_SPACE_BARRIER_WRITE);
348bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MII_SLINK_CONTROL,
3491ed3fed7SMarius Strobl 		    GEM_MII_SLINK_LOOPBACK | GEM_MII_SLINK_EN_SYNC_D);
35065f2c0ffSMarius Strobl 		GEM_BANK1_BARRIER(sc, GEM_MII_SLINK_CONTROL, 4,
35165f2c0ffSMarius Strobl 		    BUS_SPACE_BARRIER_WRITE);
352bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG, GEM_MII_CONFIG_ENABLE);
35365f2c0ffSMarius Strobl 		GEM_BANK1_BARRIER(sc, GEM_MII_CONFIG, 4,
35465f2c0ffSMarius Strobl 		    BUS_SPACE_BARRIER_WRITE);
3551ed3fed7SMarius Strobl 		sc->sc_flags |= GEM_SERDES;
3568e5d93dbSMarius Strobl 		error = mii_attach(sc->sc_dev, &sc->sc_miibus, ifp,
3578e5d93dbSMarius Strobl 		    gem_mediachange, gem_mediastatus, BMSR_DEFCAPMASK,
3589a68cbd3SMarius Strobl 		    GEM_PHYAD_EXTERNAL, MII_OFFSET_ANY, MIIF_DOPAUSE);
3591ed3fed7SMarius Strobl 	}
3601ed3fed7SMarius Strobl 	if (error != 0) {
3618e5d93dbSMarius Strobl 		device_printf(sc->sc_dev, "attaching PHYs failed\n");
362305f2c06SThomas Moestl 		goto fail_rxd;
36342c1b001SThomas Moestl 	}
36442c1b001SThomas Moestl 	sc->sc_mii = device_get_softc(sc->sc_miibus);
36542c1b001SThomas Moestl 
36642c1b001SThomas Moestl 	/*
36742c1b001SThomas Moestl 	 * From this point forward, the attachment cannot fail.  A failure
36842c1b001SThomas Moestl 	 * before this point releases all resources that may have been
36942c1b001SThomas Moestl 	 * allocated.
37042c1b001SThomas Moestl 	 */
37142c1b001SThomas Moestl 
372801772ecSMarius Strobl 	/* Get RX FIFO size. */
373336cca9eSBenno Rice 	sc->sc_rxfifosize = 64 *
374bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_RX_FIFO_SIZE);
375336cca9eSBenno Rice 
376801772ecSMarius Strobl 	/* Get TX FIFO size. */
377bd3d9826SMarius Strobl 	v = GEM_BANK1_READ_4(sc, GEM_TX_FIFO_SIZE);
3783a5aee5aSThomas Moestl 	device_printf(sc->sc_dev, "%ukB RX FIFO, %ukB TX FIFO\n",
3793a5aee5aSThomas Moestl 	    sc->sc_rxfifosize / 1024, v / 16);
38042c1b001SThomas Moestl 
38142c1b001SThomas Moestl 	/* Attach the interface. */
382fc74a9f9SBrooks Davis 	ether_ifattach(ifp, sc->sc_enaddr);
38342c1b001SThomas Moestl 
38400d12766SMarius Strobl 	/*
38512fb0330SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames/checksum offloads.
38600d12766SMarius Strobl 	 */
3871bffa951SGleb Smirnoff 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
38812fb0330SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_HWCSUM;
38912fb0330SPyun YongHyeon 	ifp->if_hwassist |= sc->sc_csum_features;
39012fb0330SPyun YongHyeon 	ifp->if_capenable |= IFCAP_VLAN_MTU | IFCAP_HWCSUM;
39100d12766SMarius Strobl 
39242c1b001SThomas Moestl 	return (0);
39342c1b001SThomas Moestl 
39442c1b001SThomas Moestl 	/*
39542c1b001SThomas Moestl 	 * Free any resources we've allocated during the failed attach
39642c1b001SThomas Moestl 	 * attempt.  Do this in reverse order and fall through.
39742c1b001SThomas Moestl 	 */
398305f2c06SThomas Moestl  fail_rxd:
3992a79fd39SMarius Strobl 	for (i = 0; i < GEM_NRXDESC; i++)
40042c1b001SThomas Moestl 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
401305f2c06SThomas Moestl 			bus_dmamap_destroy(sc->sc_rdmatag,
40242c1b001SThomas Moestl 			    sc->sc_rxsoft[i].rxs_dmamap);
403305f2c06SThomas Moestl  fail_txd:
4042a79fd39SMarius Strobl 	for (i = 0; i < GEM_TXQUEUELEN; i++)
40542c1b001SThomas Moestl 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
406305f2c06SThomas Moestl 			bus_dmamap_destroy(sc->sc_tdmatag,
40742c1b001SThomas Moestl 			    sc->sc_txsoft[i].txs_dmamap);
408305f2c06SThomas Moestl 	bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap);
409305f2c06SThomas Moestl  fail_cmem:
41042c1b001SThomas Moestl 	bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data,
41142c1b001SThomas Moestl 	    sc->sc_cddmamap);
412305f2c06SThomas Moestl  fail_ctag:
41342c1b001SThomas Moestl 	bus_dma_tag_destroy(sc->sc_cdmatag);
414305f2c06SThomas Moestl  fail_ttag:
415305f2c06SThomas Moestl 	bus_dma_tag_destroy(sc->sc_tdmatag);
416305f2c06SThomas Moestl  fail_rtag:
417305f2c06SThomas Moestl 	bus_dma_tag_destroy(sc->sc_rdmatag);
418305f2c06SThomas Moestl  fail_ptag:
41942c1b001SThomas Moestl 	bus_dma_tag_destroy(sc->sc_pdmatag);
420fc74a9f9SBrooks Davis  fail_ifnet:
421fc74a9f9SBrooks Davis 	if_free(ifp);
42242c1b001SThomas Moestl 	return (error);
42342c1b001SThomas Moestl }
42442c1b001SThomas Moestl 
425cbbdf236SThomas Moestl void
4262a79fd39SMarius Strobl gem_detach(struct gem_softc *sc)
427cbbdf236SThomas Moestl {
428fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
429cbbdf236SThomas Moestl 	int i;
430cbbdf236SThomas Moestl 
431b3a1f860SMarius Strobl 	ether_ifdetach(ifp);
4328cfaff7dSMarius Strobl 	GEM_LOCK(sc);
43325bd46d0SBrooks Davis 	gem_stop(ifp, 1);
4348cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
4351f317bf9SMarius Strobl 	callout_drain(&sc->sc_tick_ch);
4361f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT
4371f317bf9SMarius Strobl 	callout_drain(&sc->sc_rx_ch);
4381f317bf9SMarius Strobl #endif
439fc74a9f9SBrooks Davis 	if_free(ifp);
440cbbdf236SThomas Moestl 	device_delete_child(sc->sc_dev, sc->sc_miibus);
441cbbdf236SThomas Moestl 
4422a79fd39SMarius Strobl 	for (i = 0; i < GEM_NRXDESC; i++)
443cbbdf236SThomas Moestl 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
444cbbdf236SThomas Moestl 			bus_dmamap_destroy(sc->sc_rdmatag,
445cbbdf236SThomas Moestl 			    sc->sc_rxsoft[i].rxs_dmamap);
4462a79fd39SMarius Strobl 	for (i = 0; i < GEM_TXQUEUELEN; i++)
447cbbdf236SThomas Moestl 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
448cbbdf236SThomas Moestl 			bus_dmamap_destroy(sc->sc_tdmatag,
449cbbdf236SThomas Moestl 			    sc->sc_txsoft[i].txs_dmamap);
450ccb1212aSMarius Strobl 	GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
451cbbdf236SThomas Moestl 	bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap);
452cbbdf236SThomas Moestl 	bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data,
453cbbdf236SThomas Moestl 	    sc->sc_cddmamap);
454cbbdf236SThomas Moestl 	bus_dma_tag_destroy(sc->sc_cdmatag);
455cbbdf236SThomas Moestl 	bus_dma_tag_destroy(sc->sc_tdmatag);
456cbbdf236SThomas Moestl 	bus_dma_tag_destroy(sc->sc_rdmatag);
457cbbdf236SThomas Moestl 	bus_dma_tag_destroy(sc->sc_pdmatag);
458cbbdf236SThomas Moestl }
459cbbdf236SThomas Moestl 
460cbbdf236SThomas Moestl void
4612a79fd39SMarius Strobl gem_suspend(struct gem_softc *sc)
462cbbdf236SThomas Moestl {
463fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
464cbbdf236SThomas Moestl 
4658cfaff7dSMarius Strobl 	GEM_LOCK(sc);
466cbbdf236SThomas Moestl 	gem_stop(ifp, 0);
4678cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
468cbbdf236SThomas Moestl }
469cbbdf236SThomas Moestl 
470cbbdf236SThomas Moestl void
4712a79fd39SMarius Strobl gem_resume(struct gem_softc *sc)
472cbbdf236SThomas Moestl {
473fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
474cbbdf236SThomas Moestl 
4758cfaff7dSMarius Strobl 	GEM_LOCK(sc);
47600d12766SMarius Strobl 	/*
47700d12766SMarius Strobl 	 * On resume all registers have to be initialized again like
47800d12766SMarius Strobl 	 * after power-on.
47900d12766SMarius Strobl 	 */
4801ed3fed7SMarius Strobl 	sc->sc_flags &= ~GEM_INITED;
481cbbdf236SThomas Moestl 	if (ifp->if_flags & IFF_UP)
4828cfaff7dSMarius Strobl 		gem_init_locked(sc);
4838cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
484cbbdf236SThomas Moestl }
485cbbdf236SThomas Moestl 
4869ba2b298SMarius Strobl static inline void
48712fb0330SPyun YongHyeon gem_rxcksum(struct mbuf *m, uint64_t flags)
48812fb0330SPyun YongHyeon {
48912fb0330SPyun YongHyeon 	struct ether_header *eh;
49012fb0330SPyun YongHyeon 	struct ip *ip;
49112fb0330SPyun YongHyeon 	struct udphdr *uh;
4922a79fd39SMarius Strobl 	uint16_t *opts;
49312fb0330SPyun YongHyeon 	int32_t hlen, len, pktlen;
49412fb0330SPyun YongHyeon 	uint32_t temp32;
4952a79fd39SMarius Strobl 	uint16_t cksum;
49612fb0330SPyun YongHyeon 
49712fb0330SPyun YongHyeon 	pktlen = m->m_pkthdr.len;
49812fb0330SPyun YongHyeon 	if (pktlen < sizeof(struct ether_header) + sizeof(struct ip))
49912fb0330SPyun YongHyeon 		return;
50012fb0330SPyun YongHyeon 	eh = mtod(m, struct ether_header *);
50112fb0330SPyun YongHyeon 	if (eh->ether_type != htons(ETHERTYPE_IP))
50212fb0330SPyun YongHyeon 		return;
50312fb0330SPyun YongHyeon 	ip = (struct ip *)(eh + 1);
50412fb0330SPyun YongHyeon 	if (ip->ip_v != IPVERSION)
50512fb0330SPyun YongHyeon 		return;
50612fb0330SPyun YongHyeon 
50712fb0330SPyun YongHyeon 	hlen = ip->ip_hl << 2;
50812fb0330SPyun YongHyeon 	pktlen -= sizeof(struct ether_header);
50912fb0330SPyun YongHyeon 	if (hlen < sizeof(struct ip))
51012fb0330SPyun YongHyeon 		return;
51112fb0330SPyun YongHyeon 	if (ntohs(ip->ip_len) < hlen)
51212fb0330SPyun YongHyeon 		return;
51312fb0330SPyun YongHyeon 	if (ntohs(ip->ip_len) != pktlen)
51412fb0330SPyun YongHyeon 		return;
51512fb0330SPyun YongHyeon 	if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
5162a79fd39SMarius Strobl 		return;	/* Cannot handle fragmented packet. */
51712fb0330SPyun YongHyeon 
51812fb0330SPyun YongHyeon 	switch (ip->ip_p) {
51912fb0330SPyun YongHyeon 	case IPPROTO_TCP:
52012fb0330SPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct tcphdr)))
52112fb0330SPyun YongHyeon 			return;
52212fb0330SPyun YongHyeon 		break;
52312fb0330SPyun YongHyeon 	case IPPROTO_UDP:
52412fb0330SPyun YongHyeon 		if (pktlen < (hlen + sizeof(struct udphdr)))
52512fb0330SPyun YongHyeon 			return;
52612fb0330SPyun YongHyeon 		uh = (struct udphdr *)((uint8_t *)ip + hlen);
52712fb0330SPyun YongHyeon 		if (uh->uh_sum == 0)
52812fb0330SPyun YongHyeon 			return; /* no checksum */
52912fb0330SPyun YongHyeon 		break;
53012fb0330SPyun YongHyeon 	default:
53112fb0330SPyun YongHyeon 		return;
53212fb0330SPyun YongHyeon 	}
53312fb0330SPyun YongHyeon 
53412fb0330SPyun YongHyeon 	cksum = ~(flags & GEM_RD_CHECKSUM);
53512fb0330SPyun YongHyeon 	/* checksum fixup for IP options */
53612fb0330SPyun YongHyeon 	len = hlen - sizeof(struct ip);
53712fb0330SPyun YongHyeon 	if (len > 0) {
53812fb0330SPyun YongHyeon 		opts = (uint16_t *)(ip + 1);
53912fb0330SPyun YongHyeon 		for (; len > 0; len -= sizeof(uint16_t), opts++) {
54012fb0330SPyun YongHyeon 			temp32 = cksum - *opts;
54112fb0330SPyun YongHyeon 			temp32 = (temp32 >> 16) + (temp32 & 65535);
54212fb0330SPyun YongHyeon 			cksum = temp32 & 65535;
54312fb0330SPyun YongHyeon 		}
54412fb0330SPyun YongHyeon 	}
54512fb0330SPyun YongHyeon 	m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
54612fb0330SPyun YongHyeon 	m->m_pkthdr.csum_data = cksum;
54712fb0330SPyun YongHyeon }
54812fb0330SPyun YongHyeon 
54942c1b001SThomas Moestl static void
5502a79fd39SMarius Strobl gem_cddma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
55142c1b001SThomas Moestl {
5522a79fd39SMarius Strobl 	struct gem_softc *sc = xsc;
55342c1b001SThomas Moestl 
55442c1b001SThomas Moestl 	if (error != 0)
55542c1b001SThomas Moestl 		return;
5562a79fd39SMarius Strobl 	if (nsegs != 1)
5571ed3fed7SMarius Strobl 		panic("%s: bad control buffer segment count", __func__);
55842c1b001SThomas Moestl 	sc->sc_cddma = segs[0].ds_addr;
55942c1b001SThomas Moestl }
56042c1b001SThomas Moestl 
56142c1b001SThomas Moestl static void
5622a79fd39SMarius Strobl gem_tick(void *arg)
56342c1b001SThomas Moestl {
56442c1b001SThomas Moestl 	struct gem_softc *sc = arg;
5659ba2b298SMarius Strobl 	struct ifnet *ifp = sc->sc_ifp;
56678d22f42SMarius Strobl 	uint32_t v;
56742c1b001SThomas Moestl 
5681f317bf9SMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
56912fb0330SPyun YongHyeon 
57012fb0330SPyun YongHyeon 	/*
57178d22f42SMarius Strobl 	 * Unload collision and error counters.
57212fb0330SPyun YongHyeon 	 */
5738da56a6fSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS,
574bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_NORM_COLL_CNT) +
5758da56a6fSGleb Smirnoff 	    GEM_BANK1_READ_4(sc, GEM_MAC_FIRST_COLL_CNT));
57678d22f42SMarius Strobl 	v = GEM_BANK1_READ_4(sc, GEM_MAC_EXCESS_COLL_CNT) +
577bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_LATE_COLL_CNT);
5788da56a6fSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, v);
5798da56a6fSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, v);
5808da56a6fSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS,
58178d22f42SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_LEN_ERR_CNT) +
58278d22f42SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_ALIGN_ERR) +
58378d22f42SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_CRC_ERR_CNT) +
5848da56a6fSGleb Smirnoff 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_CODE_VIOL));
58512fb0330SPyun YongHyeon 
58612fb0330SPyun YongHyeon 	/*
587801772ecSMarius Strobl 	 * Then clear the hardware counters.
58812fb0330SPyun YongHyeon 	 */
589bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_NORM_COLL_CNT, 0);
590bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_FIRST_COLL_CNT, 0);
591bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_EXCESS_COLL_CNT, 0);
592bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_LATE_COLL_CNT, 0);
59378d22f42SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_LEN_ERR_CNT, 0);
59478d22f42SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_ALIGN_ERR, 0);
59578d22f42SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CRC_ERR_CNT, 0);
59678d22f42SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CODE_VIOL, 0);
59712fb0330SPyun YongHyeon 
59842c1b001SThomas Moestl 	mii_tick(sc->sc_mii);
59942c1b001SThomas Moestl 
6008cb37876SMarius Strobl 	if (gem_watchdog(sc) == EJUSTRETURN)
6018cb37876SMarius Strobl 		return;
6028cb37876SMarius Strobl 
60342c1b001SThomas Moestl 	callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
60442c1b001SThomas Moestl }
60542c1b001SThomas Moestl 
60642c1b001SThomas Moestl static int
607bd3d9826SMarius Strobl gem_bitwait(struct gem_softc *sc, u_int bank, bus_addr_t r, uint32_t clr,
608bd3d9826SMarius Strobl     uint32_t set)
60942c1b001SThomas Moestl {
61042c1b001SThomas Moestl 	int i;
6112a79fd39SMarius Strobl 	uint32_t reg;
61242c1b001SThomas Moestl 
6139ba2b298SMarius Strobl 	for (i = GEM_TRIES; i--; DELAY(100)) {
614bd3d9826SMarius Strobl 		reg = GEM_BANKN_READ_M(bank, 4, sc, r);
615e87137e1SMarius Strobl 		if ((reg & clr) == 0 && (reg & set) == set)
61642c1b001SThomas Moestl 			return (1);
61742c1b001SThomas Moestl 	}
61842c1b001SThomas Moestl 	return (0);
61942c1b001SThomas Moestl }
62042c1b001SThomas Moestl 
6211ed3fed7SMarius Strobl static void
6229ba2b298SMarius Strobl gem_reset(struct gem_softc *sc)
62342c1b001SThomas Moestl {
62442c1b001SThomas Moestl 
62518100346SThomas Moestl #ifdef GEM_DEBUG
62612fb0330SPyun YongHyeon 	CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__);
62718100346SThomas Moestl #endif
62842c1b001SThomas Moestl 	gem_reset_rx(sc);
62942c1b001SThomas Moestl 	gem_reset_tx(sc);
63042c1b001SThomas Moestl 
6312a79fd39SMarius Strobl 	/* Do a full reset. */
6329f9cc2edSMarius Strobl 	GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX |
6339f9cc2edSMarius Strobl 	    (sc->sc_variant == GEM_SUN_ERI ? GEM_ERI_CACHE_LINE_SIZE <<
6349f9cc2edSMarius Strobl 	    GEM_RESET_CLSZ_SHFT : 0));
635ccb1212aSMarius Strobl 	GEM_BANK2_BARRIER(sc, GEM_RESET, 4,
636ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
637bd3d9826SMarius Strobl 	if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0))
63842c1b001SThomas Moestl 		device_printf(sc->sc_dev, "cannot reset device\n");
63942c1b001SThomas Moestl }
64042c1b001SThomas Moestl 
64142c1b001SThomas Moestl static void
6422a79fd39SMarius Strobl gem_rxdrain(struct gem_softc *sc)
64342c1b001SThomas Moestl {
64442c1b001SThomas Moestl 	struct gem_rxsoft *rxs;
64542c1b001SThomas Moestl 	int i;
64642c1b001SThomas Moestl 
64742c1b001SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
64842c1b001SThomas Moestl 		rxs = &sc->sc_rxsoft[i];
64942c1b001SThomas Moestl 		if (rxs->rxs_mbuf != NULL) {
650b2d59f42SThomas Moestl 			bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap,
651b2d59f42SThomas Moestl 			    BUS_DMASYNC_POSTREAD);
652305f2c06SThomas Moestl 			bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap);
65342c1b001SThomas Moestl 			m_freem(rxs->rxs_mbuf);
65442c1b001SThomas Moestl 			rxs->rxs_mbuf = NULL;
65542c1b001SThomas Moestl 		}
65642c1b001SThomas Moestl 	}
65742c1b001SThomas Moestl }
65842c1b001SThomas Moestl 
65942c1b001SThomas Moestl static void
6602a79fd39SMarius Strobl gem_stop(struct ifnet *ifp, int disable)
66142c1b001SThomas Moestl {
6622a79fd39SMarius Strobl 	struct gem_softc *sc = ifp->if_softc;
66342c1b001SThomas Moestl 	struct gem_txsoft *txs;
66442c1b001SThomas Moestl 
66518100346SThomas Moestl #ifdef GEM_DEBUG
66612fb0330SPyun YongHyeon 	CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__);
66718100346SThomas Moestl #endif
66842c1b001SThomas Moestl 
66942c1b001SThomas Moestl 	callout_stop(&sc->sc_tick_ch);
6701f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT
6711f317bf9SMarius Strobl 	callout_stop(&sc->sc_rx_ch);
6721f317bf9SMarius Strobl #endif
67342c1b001SThomas Moestl 
6749ba2b298SMarius Strobl 	gem_reset_tx(sc);
6759ba2b298SMarius Strobl 	gem_reset_rx(sc);
67642c1b001SThomas Moestl 
67742c1b001SThomas Moestl 	/*
67842c1b001SThomas Moestl 	 * Release any queued transmit buffers.
67942c1b001SThomas Moestl 	 */
68042c1b001SThomas Moestl 	while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
68142c1b001SThomas Moestl 		STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
68242c1b001SThomas Moestl 		if (txs->txs_ndescs != 0) {
683b2d59f42SThomas Moestl 			bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap,
684b2d59f42SThomas Moestl 			    BUS_DMASYNC_POSTWRITE);
685305f2c06SThomas Moestl 			bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap);
68642c1b001SThomas Moestl 			if (txs->txs_mbuf != NULL) {
68742c1b001SThomas Moestl 				m_freem(txs->txs_mbuf);
68842c1b001SThomas Moestl 				txs->txs_mbuf = NULL;
68942c1b001SThomas Moestl 			}
69042c1b001SThomas Moestl 		}
69142c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
69242c1b001SThomas Moestl 	}
69342c1b001SThomas Moestl 
69442c1b001SThomas Moestl 	if (disable)
69542c1b001SThomas Moestl 		gem_rxdrain(sc);
69642c1b001SThomas Moestl 
69742c1b001SThomas Moestl 	/*
69842c1b001SThomas Moestl 	 * Mark the interface down and cancel the watchdog timer.
69942c1b001SThomas Moestl 	 */
70013f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
7011ed3fed7SMarius Strobl 	sc->sc_flags &= ~GEM_LINK;
7028cb37876SMarius Strobl 	sc->sc_wdog_timer = 0;
70342c1b001SThomas Moestl }
70442c1b001SThomas Moestl 
7051ed3fed7SMarius Strobl static int
7062a79fd39SMarius Strobl gem_reset_rx(struct gem_softc *sc)
70742c1b001SThomas Moestl {
70842c1b001SThomas Moestl 
70942c1b001SThomas Moestl 	/*
71042c1b001SThomas Moestl 	 * Resetting while DMA is in progress can cause a bus hang, so we
71142c1b001SThomas Moestl 	 * disable DMA first.
71242c1b001SThomas Moestl 	 */
713c0e3e9d4SMarius Strobl 	(void)gem_disable_rx(sc);
714bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG, 0);
715ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_RX_CONFIG, 4,
716ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
717bd3d9826SMarius Strobl 	if (!GEM_BANK1_BITWAIT(sc, GEM_RX_CONFIG, GEM_RX_CONFIG_RXDMA_EN, 0))
7181ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "cannot disable RX DMA\n");
71942c1b001SThomas Moestl 
7209a68cbd3SMarius Strobl 	/* Wait 5ms extra. */
7219a68cbd3SMarius Strobl 	DELAY(5000);
7229a68cbd3SMarius Strobl 
723c0e3e9d4SMarius Strobl 	/* Reset the ERX. */
7249f9cc2edSMarius Strobl 	GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_RX |
7259f9cc2edSMarius Strobl 	    (sc->sc_variant == GEM_SUN_ERI ? GEM_ERI_CACHE_LINE_SIZE <<
7269f9cc2edSMarius Strobl 	    GEM_RESET_CLSZ_SHFT : 0));
727ccb1212aSMarius Strobl 	GEM_BANK2_BARRIER(sc, GEM_RESET, 4,
728ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
7299f9cc2edSMarius Strobl 	if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_RX, 0)) {
73042c1b001SThomas Moestl 		device_printf(sc->sc_dev, "cannot reset receiver\n");
73142c1b001SThomas Moestl 		return (1);
73242c1b001SThomas Moestl 	}
733c0e3e9d4SMarius Strobl 
734c0e3e9d4SMarius Strobl 	/* Finally, reset RX MAC. */
735c0e3e9d4SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RXRESET, 1);
736c0e3e9d4SMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MAC_RXRESET, 4,
737c0e3e9d4SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
738c0e3e9d4SMarius Strobl 	if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_RXRESET, 1, 0)) {
739c0e3e9d4SMarius Strobl 		device_printf(sc->sc_dev, "cannot reset RX MAC\n");
740c0e3e9d4SMarius Strobl 		return (1);
741c0e3e9d4SMarius Strobl 	}
742c0e3e9d4SMarius Strobl 
74342c1b001SThomas Moestl 	return (0);
74442c1b001SThomas Moestl }
74542c1b001SThomas Moestl 
7461ed3fed7SMarius Strobl /*
7471ed3fed7SMarius Strobl  * Reset the receiver DMA engine.
7481ed3fed7SMarius Strobl  *
7491ed3fed7SMarius Strobl  * Intended to be used in case of GEM_INTR_RX_TAG_ERR, GEM_MAC_RX_OVERFLOW
7501ed3fed7SMarius Strobl  * etc in order to reset the receiver DMA engine only and not do a full
7511ed3fed7SMarius Strobl  * reset which amongst others also downs the link and clears the FIFOs.
7521ed3fed7SMarius Strobl  */
7531ed3fed7SMarius Strobl static void
7541ed3fed7SMarius Strobl gem_reset_rxdma(struct gem_softc *sc)
7551ed3fed7SMarius Strobl {
7561ed3fed7SMarius Strobl 	int i;
7571ed3fed7SMarius Strobl 
75883242185SPyun YongHyeon 	if (gem_reset_rx(sc) != 0) {
75983242185SPyun YongHyeon 		sc->sc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
7601ed3fed7SMarius Strobl 		return (gem_init_locked(sc));
76183242185SPyun YongHyeon 	}
7621ed3fed7SMarius Strobl 	for (i = 0; i < GEM_NRXDESC; i++)
7631ed3fed7SMarius Strobl 		if (sc->sc_rxsoft[i].rxs_mbuf != NULL)
7641ed3fed7SMarius Strobl 			GEM_UPDATE_RXDESC(sc, i);
7651ed3fed7SMarius Strobl 	sc->sc_rxptr = 0;
7669ba2b298SMarius Strobl 	GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
7671ed3fed7SMarius Strobl 
7681ed3fed7SMarius Strobl 	/* NOTE: we use only 32-bit DMA addresses here. */
769bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_HI, 0);
770bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0));
771bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_KICK, GEM_NRXDESC - 4);
772bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG,
7731ed3fed7SMarius Strobl 	    gem_ringsize(GEM_NRXDESC /* XXX */) |
7741ed3fed7SMarius Strobl 	    ((ETHER_HDR_LEN + sizeof(struct ip)) <<
7751ed3fed7SMarius Strobl 	    GEM_RX_CONFIG_CXM_START_SHFT) |
7761ed3fed7SMarius Strobl 	    (GEM_THRSH_1024 << GEM_RX_CONFIG_FIFO_THRS_SHIFT) |
7779ba2b298SMarius Strobl 	    (ETHER_ALIGN << GEM_RX_CONFIG_FBOFF_SHFT));
7785ed0b954SMarius Strobl 	/* Adjusting for the SBus clock probably isn't worth the fuzz. */
779bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_BLANKING,
7809ba2b298SMarius Strobl 	    ((6 * (sc->sc_flags & GEM_PCI66) != 0 ? 2 : 1) <<
7819ba2b298SMarius Strobl 	    GEM_RX_BLANKING_TIME_SHIFT) | 6);
782bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_PAUSE_THRESH,
7832a79fd39SMarius Strobl 	    (3 * sc->sc_rxfifosize / 256) |
7842a79fd39SMarius Strobl 	    ((sc->sc_rxfifosize / 256) << 12));
785bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG,
786bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_RX_CONFIG) | GEM_RX_CONFIG_RXDMA_EN);
787bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_MASK,
7881ed3fed7SMarius Strobl 	    GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT);
7895ed0b954SMarius Strobl 	/*
7905ed0b954SMarius Strobl 	 * Clear the RX filter and reprogram it.  This will also set the
7915ed0b954SMarius Strobl 	 * current RX MAC configuration and enable it.
7925ed0b954SMarius Strobl 	 */
7935ed0b954SMarius Strobl 	gem_setladrf(sc);
7941ed3fed7SMarius Strobl }
79542c1b001SThomas Moestl 
79642c1b001SThomas Moestl static int
7972a79fd39SMarius Strobl gem_reset_tx(struct gem_softc *sc)
79842c1b001SThomas Moestl {
79942c1b001SThomas Moestl 
80042c1b001SThomas Moestl 	/*
80142c1b001SThomas Moestl 	 * Resetting while DMA is in progress can cause a bus hang, so we
80242c1b001SThomas Moestl 	 * disable DMA first.
80342c1b001SThomas Moestl 	 */
804c0e3e9d4SMarius Strobl 	(void)gem_disable_tx(sc);
805bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_TX_CONFIG, 0);
806ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_TX_CONFIG, 4,
807ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
808bd3d9826SMarius Strobl 	if (!GEM_BANK1_BITWAIT(sc, GEM_TX_CONFIG, GEM_TX_CONFIG_TXDMA_EN, 0))
8091ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "cannot disable TX DMA\n");
81042c1b001SThomas Moestl 
8119a68cbd3SMarius Strobl 	/* Wait 5ms extra. */
8129a68cbd3SMarius Strobl 	DELAY(5000);
8139a68cbd3SMarius Strobl 
814801772ecSMarius Strobl 	/* Finally, reset the ETX. */
8159f9cc2edSMarius Strobl 	GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_TX |
8169f9cc2edSMarius Strobl 	    (sc->sc_variant == GEM_SUN_ERI ? GEM_ERI_CACHE_LINE_SIZE <<
8179f9cc2edSMarius Strobl 	    GEM_RESET_CLSZ_SHFT : 0));
818ccb1212aSMarius Strobl 	GEM_BANK2_BARRIER(sc, GEM_RESET, 4,
819ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
8209f9cc2edSMarius Strobl 	if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_TX, 0)) {
8211ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "cannot reset transmitter\n");
82242c1b001SThomas Moestl 		return (1);
82342c1b001SThomas Moestl 	}
82442c1b001SThomas Moestl 	return (0);
82542c1b001SThomas Moestl }
82642c1b001SThomas Moestl 
82742c1b001SThomas Moestl static int
8282a79fd39SMarius Strobl gem_disable_rx(struct gem_softc *sc)
82942c1b001SThomas Moestl {
83042c1b001SThomas Moestl 
831bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG,
832bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG) & ~GEM_MAC_RX_ENABLE);
833ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4,
834ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
835c0e3e9d4SMarius Strobl 	if (GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0))
836c0e3e9d4SMarius Strobl 		return (1);
837c0e3e9d4SMarius Strobl 	device_printf(sc->sc_dev, "cannot disable RX MAC\n");
838c0e3e9d4SMarius Strobl 	return (0);
83942c1b001SThomas Moestl }
84042c1b001SThomas Moestl 
84142c1b001SThomas Moestl static int
8422a79fd39SMarius Strobl gem_disable_tx(struct gem_softc *sc)
84342c1b001SThomas Moestl {
84442c1b001SThomas Moestl 
845bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG,
846bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG) & ~GEM_MAC_TX_ENABLE);
847ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MAC_TX_CONFIG, 4,
848ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
849c0e3e9d4SMarius Strobl 	if (GEM_BANK1_BITWAIT(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0))
850c0e3e9d4SMarius Strobl 		return (1);
851c0e3e9d4SMarius Strobl 	device_printf(sc->sc_dev, "cannot disable TX MAC\n");
852c0e3e9d4SMarius Strobl 	return (0);
85342c1b001SThomas Moestl }
85442c1b001SThomas Moestl 
85542c1b001SThomas Moestl static int
8569ba2b298SMarius Strobl gem_meminit(struct gem_softc *sc)
85742c1b001SThomas Moestl {
85842c1b001SThomas Moestl 	struct gem_rxsoft *rxs;
8592a79fd39SMarius Strobl 	int error, i;
86042c1b001SThomas Moestl 
8619ba2b298SMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
8629ba2b298SMarius Strobl 
86342c1b001SThomas Moestl 	/*
86442c1b001SThomas Moestl 	 * Initialize the transmit descriptor ring.
86542c1b001SThomas Moestl 	 */
86642c1b001SThomas Moestl 	for (i = 0; i < GEM_NTXDESC; i++) {
86742c1b001SThomas Moestl 		sc->sc_txdescs[i].gd_flags = 0;
86842c1b001SThomas Moestl 		sc->sc_txdescs[i].gd_addr = 0;
86942c1b001SThomas Moestl 	}
870305f2c06SThomas Moestl 	sc->sc_txfree = GEM_MAXTXFREE;
87142c1b001SThomas Moestl 	sc->sc_txnext = 0;
872336cca9eSBenno Rice 	sc->sc_txwin = 0;
87342c1b001SThomas Moestl 
87442c1b001SThomas Moestl 	/*
87542c1b001SThomas Moestl 	 * Initialize the receive descriptor and receive job
87642c1b001SThomas Moestl 	 * descriptor rings.
87742c1b001SThomas Moestl 	 */
87842c1b001SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
87942c1b001SThomas Moestl 		rxs = &sc->sc_rxsoft[i];
88042c1b001SThomas Moestl 		if (rxs->rxs_mbuf == NULL) {
88142c1b001SThomas Moestl 			if ((error = gem_add_rxbuf(sc, i)) != 0) {
8822a79fd39SMarius Strobl 				device_printf(sc->sc_dev,
8832a79fd39SMarius Strobl 				    "unable to allocate or map RX buffer %d, "
8842a79fd39SMarius Strobl 				    "error = %d\n", i, error);
88542c1b001SThomas Moestl 				/*
8862a79fd39SMarius Strobl 				 * XXX we should attempt to run with fewer
8872a79fd39SMarius Strobl 				 * receive buffers instead of just failing.
88842c1b001SThomas Moestl 				 */
88942c1b001SThomas Moestl 				gem_rxdrain(sc);
89042c1b001SThomas Moestl 				return (1);
89142c1b001SThomas Moestl 			}
89242c1b001SThomas Moestl 		} else
89342c1b001SThomas Moestl 			GEM_INIT_RXDESC(sc, i);
89442c1b001SThomas Moestl 	}
89542c1b001SThomas Moestl 	sc->sc_rxptr = 0;
8969ba2b298SMarius Strobl 
8979ba2b298SMarius Strobl 	GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
89842c1b001SThomas Moestl 
89942c1b001SThomas Moestl 	return (0);
90042c1b001SThomas Moestl }
90142c1b001SThomas Moestl 
9021ed3fed7SMarius Strobl static u_int
9032a79fd39SMarius Strobl gem_ringsize(u_int sz)
90442c1b001SThomas Moestl {
90542c1b001SThomas Moestl 
90642c1b001SThomas Moestl 	switch (sz) {
90742c1b001SThomas Moestl 	case 32:
9081ed3fed7SMarius Strobl 		return (GEM_RING_SZ_32);
90942c1b001SThomas Moestl 	case 64:
9101ed3fed7SMarius Strobl 		return (GEM_RING_SZ_64);
91142c1b001SThomas Moestl 	case 128:
9121ed3fed7SMarius Strobl 		return (GEM_RING_SZ_128);
91342c1b001SThomas Moestl 	case 256:
9141ed3fed7SMarius Strobl 		return (GEM_RING_SZ_256);
91542c1b001SThomas Moestl 	case 512:
9161ed3fed7SMarius Strobl 		return (GEM_RING_SZ_512);
91742c1b001SThomas Moestl 	case 1024:
9181ed3fed7SMarius Strobl 		return (GEM_RING_SZ_1024);
91942c1b001SThomas Moestl 	case 2048:
9201ed3fed7SMarius Strobl 		return (GEM_RING_SZ_2048);
92142c1b001SThomas Moestl 	case 4096:
9221ed3fed7SMarius Strobl 		return (GEM_RING_SZ_4096);
92342c1b001SThomas Moestl 	case 8192:
9241ed3fed7SMarius Strobl 		return (GEM_RING_SZ_8192);
92542c1b001SThomas Moestl 	default:
9261ed3fed7SMarius Strobl 		printf("%s: invalid ring size %d\n", __func__, sz);
9271ed3fed7SMarius Strobl 		return (GEM_RING_SZ_32);
92842c1b001SThomas Moestl 	}
92942c1b001SThomas Moestl }
93042c1b001SThomas Moestl 
93142c1b001SThomas Moestl static void
9322a79fd39SMarius Strobl gem_init(void *xsc)
93342c1b001SThomas Moestl {
9342a79fd39SMarius Strobl 	struct gem_softc *sc = xsc;
9358cfaff7dSMarius Strobl 
9368cfaff7dSMarius Strobl 	GEM_LOCK(sc);
9378cfaff7dSMarius Strobl 	gem_init_locked(sc);
9388cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
9398cfaff7dSMarius Strobl }
9408cfaff7dSMarius Strobl 
9418cfaff7dSMarius Strobl /*
9428cfaff7dSMarius Strobl  * Initialization of interface; set up initialization block
9438cfaff7dSMarius Strobl  * and transmit/receive descriptor rings.
9448cfaff7dSMarius Strobl  */
9458cfaff7dSMarius Strobl static void
9462a79fd39SMarius Strobl gem_init_locked(struct gem_softc *sc)
9478cfaff7dSMarius Strobl {
948fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
9492a79fd39SMarius Strobl 	uint32_t v;
95042c1b001SThomas Moestl 
9518cfaff7dSMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
95242c1b001SThomas Moestl 
95383242185SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
95483242185SPyun YongHyeon 		return;
95583242185SPyun YongHyeon 
95618100346SThomas Moestl #ifdef GEM_DEBUG
95712fb0330SPyun YongHyeon 	CTR2(KTR_GEM, "%s: %s: calling stop", device_get_name(sc->sc_dev),
95812fb0330SPyun YongHyeon 	    __func__);
95918100346SThomas Moestl #endif
96042c1b001SThomas Moestl 	/*
96142c1b001SThomas Moestl 	 * Initialization sequence.  The numbered steps below correspond
96242c1b001SThomas Moestl 	 * to the sequence outlined in section 6.3.5.1 in the Ethernet
96342c1b001SThomas Moestl 	 * Channel Engine manual (part of the PCIO manual).
96442c1b001SThomas Moestl 	 * See also the STP2002-STQ document from Sun Microsystems.
96542c1b001SThomas Moestl 	 */
96642c1b001SThomas Moestl 
9672a79fd39SMarius Strobl 	/* step 1 & 2.  Reset the Ethernet Channel. */
968ccb1212aSMarius Strobl 	gem_stop(ifp, 0);
96942c1b001SThomas Moestl 	gem_reset(sc);
97018100346SThomas Moestl #ifdef GEM_DEBUG
97112fb0330SPyun YongHyeon 	CTR2(KTR_GEM, "%s: %s: restarting", device_get_name(sc->sc_dev),
97212fb0330SPyun YongHyeon 	    __func__);
97318100346SThomas Moestl #endif
97442c1b001SThomas Moestl 
97565f2c0ffSMarius Strobl 	if ((sc->sc_flags & GEM_SERDES) == 0)
9762a79fd39SMarius Strobl 		/* Re-initialize the MIF. */
97742c1b001SThomas Moestl 		gem_mifinit(sc);
97842c1b001SThomas Moestl 
9792a79fd39SMarius Strobl 	/* step 3.  Setup data structures in host memory. */
9801ed3fed7SMarius Strobl 	if (gem_meminit(sc) != 0)
9811ed3fed7SMarius Strobl 		return;
98242c1b001SThomas Moestl 
98342c1b001SThomas Moestl 	/* step 4.  TX MAC registers & counters */
98442c1b001SThomas Moestl 	gem_init_regs(sc);
98542c1b001SThomas Moestl 
98642c1b001SThomas Moestl 	/* step 5.  RX MAC registers & counters */
98742c1b001SThomas Moestl 
9882a79fd39SMarius Strobl 	/* step 6 & 7.  Program Descriptor Ring Base Addresses. */
98942c1b001SThomas Moestl 	/* NOTE: we use only 32-bit DMA addresses here. */
990bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_TX_RING_PTR_HI, 0);
991bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0));
99242c1b001SThomas Moestl 
993bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_HI, 0);
994bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0));
99518100346SThomas Moestl #ifdef GEM_DEBUG
9962a79fd39SMarius Strobl 	CTR3(KTR_GEM, "loading RX ring %lx, TX ring %lx, cddma %lx",
99742c1b001SThomas Moestl 	    GEM_CDRXADDR(sc, 0), GEM_CDTXADDR(sc, 0), sc->sc_cddma);
99818100346SThomas Moestl #endif
99942c1b001SThomas Moestl 
100042c1b001SThomas Moestl 	/* step 8.  Global Configuration & Interrupt Mask */
10019ba2b298SMarius Strobl 
10029ba2b298SMarius Strobl 	/*
10039ba2b298SMarius Strobl 	 * Set the internal arbitration to "infinite" bursts of the
10049ba2b298SMarius Strobl 	 * maximum length of 31 * 64 bytes so DMA transfers aren't
10059ba2b298SMarius Strobl 	 * split up in cache line size chunks.  This greatly improves
10069ba2b298SMarius Strobl 	 * RX performance.
10079ba2b298SMarius Strobl 	 * Enable silicon bug workarounds for the Apple variants.
10089ba2b298SMarius Strobl 	 */
10099ba2b298SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_CONFIG,
10109ba2b298SMarius Strobl 	    GEM_CONFIG_TXDMA_LIMIT | GEM_CONFIG_RXDMA_LIMIT |
10119ba2b298SMarius Strobl 	    ((sc->sc_flags & GEM_PCI) != 0 ? GEM_CONFIG_BURST_INF :
10129ba2b298SMarius Strobl 	    GEM_CONFIG_BURST_64) | (GEM_IS_APPLE(sc) ?
10139ba2b298SMarius Strobl 	    GEM_CONFIG_RONPAULBIT | GEM_CONFIG_BUG2FIX : 0));
10149ba2b298SMarius Strobl 
1015bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_INTMASK,
10161ed3fed7SMarius Strobl 	    ~(GEM_INTR_TX_INTME | GEM_INTR_TX_EMPTY | GEM_INTR_RX_DONE |
10171ed3fed7SMarius Strobl 	    GEM_INTR_RX_NOBUF | GEM_INTR_RX_TAG_ERR | GEM_INTR_PERR |
10181ed3fed7SMarius Strobl 	    GEM_INTR_BERR
10191ed3fed7SMarius Strobl #ifdef GEM_DEBUG
10201ed3fed7SMarius Strobl 	    | GEM_INTR_PCS | GEM_INTR_MIF
10211ed3fed7SMarius Strobl #endif
10221ed3fed7SMarius Strobl 	    ));
1023bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_MASK,
1024336cca9eSBenno Rice 	    GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT);
1025bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_MASK,
10269ba2b298SMarius Strobl 	    GEM_MAC_TX_XMIT_DONE | GEM_MAC_TX_DEFER_EXP |
10279ba2b298SMarius Strobl 	    GEM_MAC_TX_PEAK_EXP);
10281ed3fed7SMarius Strobl #ifdef GEM_DEBUG
1029bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_MASK,
10301ed3fed7SMarius Strobl 	    ~(GEM_MAC_PAUSED | GEM_MAC_PAUSE | GEM_MAC_RESUME));
10311ed3fed7SMarius Strobl #else
1032bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_MASK,
10331ed3fed7SMarius Strobl 	    GEM_MAC_PAUSED | GEM_MAC_PAUSE | GEM_MAC_RESUME);
10341ed3fed7SMarius Strobl #endif
103542c1b001SThomas Moestl 
10362a79fd39SMarius Strobl 	/* step 9.  ETX Configuration: use mostly default values. */
103742c1b001SThomas Moestl 
10382a79fd39SMarius Strobl 	/* Enable DMA. */
10399ba2b298SMarius Strobl 	v = gem_ringsize(GEM_NTXDESC);
10409ba2b298SMarius Strobl 	/* Set TX FIFO threshold and enable DMA. */
1041ccb1212aSMarius Strobl 	v |= ((sc->sc_variant == GEM_SUN_ERI ? 0x100 : 0x4ff) << 10) &
1042ccb1212aSMarius Strobl 	    GEM_TX_CONFIG_TXFIFO_TH;
1043ccb1212aSMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_TX_CONFIG, v | GEM_TX_CONFIG_TXDMA_EN);
104442c1b001SThomas Moestl 
104542c1b001SThomas Moestl 	/* step 10.  ERX Configuration */
104642c1b001SThomas Moestl 
10471ed3fed7SMarius Strobl 	/* Encode Receive Descriptor ring size. */
104842c1b001SThomas Moestl 	v = gem_ringsize(GEM_NRXDESC /* XXX */);
10492a79fd39SMarius Strobl 	/* RX TCP/UDP checksum offset */
105012fb0330SPyun YongHyeon 	v |= ((ETHER_HDR_LEN + sizeof(struct ip)) <<
105112fb0330SPyun YongHyeon 	    GEM_RX_CONFIG_CXM_START_SHFT);
10529ba2b298SMarius Strobl 	/* Set RX FIFO threshold, set first byte offset and enable DMA. */
1053bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG,
105442c1b001SThomas Moestl 	    v | (GEM_THRSH_1024 << GEM_RX_CONFIG_FIFO_THRS_SHIFT) |
10559ba2b298SMarius Strobl 	    (ETHER_ALIGN << GEM_RX_CONFIG_FBOFF_SHFT) |
10569ba2b298SMarius Strobl 	    GEM_RX_CONFIG_RXDMA_EN);
10571ed3fed7SMarius Strobl 
10585ed0b954SMarius Strobl 	/* Adjusting for the SBus clock probably isn't worth the fuzz. */
1059bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_BLANKING,
10609ba2b298SMarius Strobl 	    ((6 * (sc->sc_flags & GEM_PCI66) != 0 ? 2 : 1) <<
10619ba2b298SMarius Strobl 	    GEM_RX_BLANKING_TIME_SHIFT) | 6);
10621ed3fed7SMarius Strobl 
106342c1b001SThomas Moestl 	/*
1064336cca9eSBenno Rice 	 * The following value is for an OFF Threshold of about 3/4 full
1065336cca9eSBenno Rice 	 * and an ON Threshold of 1/4 full.
106642c1b001SThomas Moestl 	 */
1067bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_PAUSE_THRESH,
1068336cca9eSBenno Rice 	    (3 * sc->sc_rxfifosize / 256) |
1069336cca9eSBenno Rice 	    ((sc->sc_rxfifosize / 256) << 12));
107042c1b001SThomas Moestl 
10712a79fd39SMarius Strobl 	/* step 11.  Configure Media. */
107242c1b001SThomas Moestl 
107342c1b001SThomas Moestl 	/* step 12.  RX_MAC Configuration Register */
1074bd3d9826SMarius Strobl 	v = GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG);
10755ed0b954SMarius Strobl 	v &= ~GEM_MAC_RX_ENABLE;
10765ed0b954SMarius Strobl 	v |= GEM_MAC_RX_STRIP_CRC;
10775ed0b954SMarius Strobl 	sc->sc_mac_rxcfg = v;
10785ed0b954SMarius Strobl 	/*
10795ed0b954SMarius Strobl 	 * Clear the RX filter and reprogram it.  This will also set the
10805ed0b954SMarius Strobl 	 * current RX MAC configuration and enable it.
10815ed0b954SMarius Strobl 	 */
10825ed0b954SMarius Strobl 	gem_setladrf(sc);
108342c1b001SThomas Moestl 
1084ccb1212aSMarius Strobl 	/* step 13.  TX_MAC Configuration Register */
1085ccb1212aSMarius Strobl 	v = GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG);
1086ccb1212aSMarius Strobl 	v |= GEM_MAC_TX_ENABLE;
1087c0e3e9d4SMarius Strobl 	(void)gem_disable_tx(sc);
1088ccb1212aSMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, v);
1089ccb1212aSMarius Strobl 
10902a79fd39SMarius Strobl 	/* step 14.  Issue Transmit Pending command. */
109142c1b001SThomas Moestl 
1092af5ac863SMarius Strobl 	/* step 15.  Give the receiver a swift kick. */
1093bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_RX_KICK, GEM_NRXDESC - 4);
109442c1b001SThomas Moestl 
109513f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
109613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
10971ed3fed7SMarius Strobl 
10981ed3fed7SMarius Strobl 	mii_mediachg(sc->sc_mii);
10991ed3fed7SMarius Strobl 
11001ed3fed7SMarius Strobl 	/* Start the one second timer. */
11011ed3fed7SMarius Strobl 	sc->sc_wdog_timer = 0;
11021ed3fed7SMarius Strobl 	callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
110342c1b001SThomas Moestl }
110442c1b001SThomas Moestl 
110512fb0330SPyun YongHyeon static int
11062a79fd39SMarius Strobl gem_load_txmbuf(struct gem_softc *sc, struct mbuf **m_head)
110712fb0330SPyun YongHyeon {
110812fb0330SPyun YongHyeon 	bus_dma_segment_t txsegs[GEM_NTXSEGS];
11092a79fd39SMarius Strobl 	struct gem_txsoft *txs;
1110ccb1212aSMarius Strobl 	struct ip *ip;
111112fb0330SPyun YongHyeon 	struct mbuf *m;
11122a79fd39SMarius Strobl 	uint64_t cflags, flags;
1113ccb1212aSMarius Strobl 	int error, nexttx, nsegs, offset, seg;
111442c1b001SThomas Moestl 
11159ba2b298SMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
11169ba2b298SMarius Strobl 
111742c1b001SThomas Moestl 	/* Get a work queue entry. */
111842c1b001SThomas Moestl 	if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) {
1119305f2c06SThomas Moestl 		/* Ran out of descriptors. */
112012fb0330SPyun YongHyeon 		return (ENOBUFS);
1121305f2c06SThomas Moestl 	}
1122ccb1212aSMarius Strobl 
1123ccb1212aSMarius Strobl 	cflags = 0;
1124ccb1212aSMarius Strobl 	if (((*m_head)->m_pkthdr.csum_flags & sc->sc_csum_features) != 0) {
1125ccb1212aSMarius Strobl 		if (M_WRITABLE(*m_head) == 0) {
1126c6499eccSGleb Smirnoff 			m = m_dup(*m_head, M_NOWAIT);
1127ccb1212aSMarius Strobl 			m_freem(*m_head);
1128ccb1212aSMarius Strobl 			*m_head = m;
1129ccb1212aSMarius Strobl 			if (m == NULL)
1130ccb1212aSMarius Strobl 				return (ENOBUFS);
1131ccb1212aSMarius Strobl 		}
1132ccb1212aSMarius Strobl 		offset = sizeof(struct ether_header);
1133ccb1212aSMarius Strobl 		m = m_pullup(*m_head, offset + sizeof(struct ip));
1134ccb1212aSMarius Strobl 		if (m == NULL) {
1135ccb1212aSMarius Strobl 			*m_head = NULL;
1136ccb1212aSMarius Strobl 			return (ENOBUFS);
1137ccb1212aSMarius Strobl 		}
1138ccb1212aSMarius Strobl 		ip = (struct ip *)(mtod(m, caddr_t) + offset);
1139ccb1212aSMarius Strobl 		offset += (ip->ip_hl << 2);
1140ccb1212aSMarius Strobl 		cflags = offset << GEM_TD_CXSUM_STARTSHFT |
1141ccb1212aSMarius Strobl 		    ((offset + m->m_pkthdr.csum_data) <<
1142ccb1212aSMarius Strobl 		    GEM_TD_CXSUM_STUFFSHFT) | GEM_TD_CXSUM_ENABLE;
1143ccb1212aSMarius Strobl 		*m_head = m;
1144ccb1212aSMarius Strobl 	}
1145ccb1212aSMarius Strobl 
114612fb0330SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->sc_tdmatag, txs->txs_dmamap,
114712fb0330SPyun YongHyeon 	    *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
114812fb0330SPyun YongHyeon 	if (error == EFBIG) {
1149c6499eccSGleb Smirnoff 		m = m_collapse(*m_head, M_NOWAIT, GEM_NTXSEGS);
115012fb0330SPyun YongHyeon 		if (m == NULL) {
115112fb0330SPyun YongHyeon 			m_freem(*m_head);
115212fb0330SPyun YongHyeon 			*m_head = NULL;
115312fb0330SPyun YongHyeon 			return (ENOBUFS);
115412fb0330SPyun YongHyeon 		}
115512fb0330SPyun YongHyeon 		*m_head = m;
11562a79fd39SMarius Strobl 		error = bus_dmamap_load_mbuf_sg(sc->sc_tdmatag,
11572a79fd39SMarius Strobl 		    txs->txs_dmamap, *m_head, txsegs, &nsegs,
11582a79fd39SMarius Strobl 		    BUS_DMA_NOWAIT);
115912fb0330SPyun YongHyeon 		if (error != 0) {
116012fb0330SPyun YongHyeon 			m_freem(*m_head);
116112fb0330SPyun YongHyeon 			*m_head = NULL;
116212fb0330SPyun YongHyeon 			return (error);
116312fb0330SPyun YongHyeon 		}
116412fb0330SPyun YongHyeon 	} else if (error != 0)
116512fb0330SPyun YongHyeon 		return (error);
1166801772ecSMarius Strobl 	/* If nsegs is wrong then the stack is corrupt. */
1167801772ecSMarius Strobl 	KASSERT(nsegs <= GEM_NTXSEGS,
1168801772ecSMarius Strobl 	    ("%s: too many DMA segments (%d)", __func__, nsegs));
116912fb0330SPyun YongHyeon 	if (nsegs == 0) {
117012fb0330SPyun YongHyeon 		m_freem(*m_head);
117112fb0330SPyun YongHyeon 		*m_head = NULL;
117212fb0330SPyun YongHyeon 		return (EIO);
117312fb0330SPyun YongHyeon 	}
117412fb0330SPyun YongHyeon 
117512fb0330SPyun YongHyeon 	/*
117612fb0330SPyun YongHyeon 	 * Ensure we have enough descriptors free to describe
117712fb0330SPyun YongHyeon 	 * the packet.  Note, we always reserve one descriptor
11782a79fd39SMarius Strobl 	 * at the end of the ring as a termination point, in
11792a79fd39SMarius Strobl 	 * order to prevent wrap-around.
118012fb0330SPyun YongHyeon 	 */
118112fb0330SPyun YongHyeon 	if (nsegs > sc->sc_txfree - 1) {
118212fb0330SPyun YongHyeon 		txs->txs_ndescs = 0;
118312fb0330SPyun YongHyeon 		bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap);
118412fb0330SPyun YongHyeon 		return (ENOBUFS);
118512fb0330SPyun YongHyeon 	}
118612fb0330SPyun YongHyeon 
118712fb0330SPyun YongHyeon 	txs->txs_ndescs = nsegs;
1188305f2c06SThomas Moestl 	txs->txs_firstdesc = sc->sc_txnext;
118912fb0330SPyun YongHyeon 	nexttx = txs->txs_firstdesc;
119012fb0330SPyun YongHyeon 	for (seg = 0; seg < nsegs; seg++, nexttx = GEM_NEXTTX(nexttx)) {
119112fb0330SPyun YongHyeon #ifdef GEM_DEBUG
11922a79fd39SMarius Strobl 		CTR6(KTR_GEM,
11932a79fd39SMarius Strobl 		    "%s: mapping seg %d (txd %d), len %lx, addr %#lx (%#lx)",
11942a79fd39SMarius Strobl 		    __func__, seg, nexttx, txsegs[seg].ds_len,
11952a79fd39SMarius Strobl 		    txsegs[seg].ds_addr,
119612fb0330SPyun YongHyeon 		    GEM_DMA_WRITE(sc, txsegs[seg].ds_addr));
119712fb0330SPyun YongHyeon #endif
119812fb0330SPyun YongHyeon 		sc->sc_txdescs[nexttx].gd_addr =
119912fb0330SPyun YongHyeon 		    GEM_DMA_WRITE(sc, txsegs[seg].ds_addr);
120012fb0330SPyun YongHyeon 		KASSERT(txsegs[seg].ds_len < GEM_TD_BUFSIZE,
120112fb0330SPyun YongHyeon 		    ("%s: segment size too large!", __func__));
120212fb0330SPyun YongHyeon 		flags = txsegs[seg].ds_len & GEM_TD_BUFSIZE;
120312fb0330SPyun YongHyeon 		sc->sc_txdescs[nexttx].gd_flags =
120412fb0330SPyun YongHyeon 		    GEM_DMA_WRITE(sc, flags | cflags);
120512fb0330SPyun YongHyeon 		txs->txs_lastdesc = nexttx;
120642c1b001SThomas Moestl 	}
1207305f2c06SThomas Moestl 
12082a79fd39SMarius Strobl 	/* Set EOP on the last descriptor. */
120912fb0330SPyun YongHyeon #ifdef GEM_DEBUG
12102a79fd39SMarius Strobl 	CTR3(KTR_GEM, "%s: end of packet at segment %d, TX %d",
12112a79fd39SMarius Strobl 	    __func__, seg, nexttx);
121212fb0330SPyun YongHyeon #endif
121312fb0330SPyun YongHyeon 	sc->sc_txdescs[txs->txs_lastdesc].gd_flags |=
121412fb0330SPyun YongHyeon 	    GEM_DMA_WRITE(sc, GEM_TD_END_OF_PACKET);
121512fb0330SPyun YongHyeon 
12162a79fd39SMarius Strobl 	/* Lastly set SOP on the first descriptor. */
121712fb0330SPyun YongHyeon #ifdef GEM_DEBUG
12182a79fd39SMarius Strobl 	CTR3(KTR_GEM, "%s: start of packet at segment %d, TX %d",
12192a79fd39SMarius Strobl 	    __func__, seg, nexttx);
122012fb0330SPyun YongHyeon #endif
122112fb0330SPyun YongHyeon 	if (++sc->sc_txwin > GEM_NTXSEGS * 2 / 3) {
122212fb0330SPyun YongHyeon 		sc->sc_txwin = 0;
122312fb0330SPyun YongHyeon 		sc->sc_txdescs[txs->txs_firstdesc].gd_flags |=
122412fb0330SPyun YongHyeon 		    GEM_DMA_WRITE(sc, GEM_TD_INTERRUPT_ME |
122512fb0330SPyun YongHyeon 		    GEM_TD_START_OF_PACKET);
122612fb0330SPyun YongHyeon 	} else
122712fb0330SPyun YongHyeon 		sc->sc_txdescs[txs->txs_firstdesc].gd_flags |=
122812fb0330SPyun YongHyeon 		    GEM_DMA_WRITE(sc, GEM_TD_START_OF_PACKET);
122912fb0330SPyun YongHyeon 
123042c1b001SThomas Moestl 	/* Sync the DMA map. */
12312a79fd39SMarius Strobl 	bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap,
12322a79fd39SMarius Strobl 	    BUS_DMASYNC_PREWRITE);
1233305f2c06SThomas Moestl 
123418100346SThomas Moestl #ifdef GEM_DEBUG
123512fb0330SPyun YongHyeon 	CTR4(KTR_GEM, "%s: setting firstdesc=%d, lastdesc=%d, ndescs=%d",
12362a79fd39SMarius Strobl 	    __func__, txs->txs_firstdesc, txs->txs_lastdesc,
12372a79fd39SMarius Strobl 	    txs->txs_ndescs);
123818100346SThomas Moestl #endif
123942c1b001SThomas Moestl 	STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
1240305f2c06SThomas Moestl 	STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
124112fb0330SPyun YongHyeon 	txs->txs_mbuf = *m_head;
1242305f2c06SThomas Moestl 
1243305f2c06SThomas Moestl 	sc->sc_txnext = GEM_NEXTTX(txs->txs_lastdesc);
1244305f2c06SThomas Moestl 	sc->sc_txfree -= txs->txs_ndescs;
124542c1b001SThomas Moestl 
124612fb0330SPyun YongHyeon 	return (0);
124742c1b001SThomas Moestl }
124842c1b001SThomas Moestl 
124942c1b001SThomas Moestl static void
12502a79fd39SMarius Strobl gem_init_regs(struct gem_softc *sc)
125142c1b001SThomas Moestl {
12524a0d6638SRuslan Ermilov 	const u_char *laddr = IF_LLADDR(sc->sc_ifp);
125342c1b001SThomas Moestl 
12549ba2b298SMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
12559ba2b298SMarius Strobl 
12562a79fd39SMarius Strobl 	/* These registers are not cleared on reset. */
12571ed3fed7SMarius Strobl 	if ((sc->sc_flags & GEM_INITED) == 0) {
12582a79fd39SMarius Strobl 		/* magic values */
1259bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG0, 0);
1260bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG1, 8);
1261bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG2, 4);
126242c1b001SThomas Moestl 
12639ba2b298SMarius Strobl 		/* min frame length */
1264bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN);
12659ba2b298SMarius Strobl 		/* max frame length and max burst size */
1266bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_MAC_MAX_FRAME,
12671ed3fed7SMarius Strobl 		    (ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN) | (0x2000 << 16));
1268336cca9eSBenno Rice 
12699ba2b298SMarius Strobl 		/* more magic values */
1270bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_PREAMBLE_LEN, 0x7);
1271bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_JAM_SIZE, 0x4);
1272bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ATTEMPT_LIMIT, 0x10);
12739a68cbd3SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_TYPE, 0x8808);
12749ba2b298SMarius Strobl 
12759ba2b298SMarius Strobl 		/* random number seed */
1276bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_RANDOM_SEED,
1277336cca9eSBenno Rice 		    ((laddr[5] << 8) | laddr[4]) & 0x3ff);
1278336cca9eSBenno Rice 
12792a79fd39SMarius Strobl 		/* secondary MAC address: 0:0:0:0:0:0 */
1280bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR3, 0);
1281bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR4, 0);
1282bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR5, 0);
1283336cca9eSBenno Rice 
12842a79fd39SMarius Strobl 		/* MAC control address: 01:80:c2:00:00:01 */
1285bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR6, 0x0001);
1286bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR7, 0xc200);
1287bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR8, 0x0180);
128842c1b001SThomas Moestl 
12892a79fd39SMarius Strobl 		/* MAC filter address: 0:0:0:0:0:0 */
1290bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER0, 0);
1291bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER1, 0);
1292bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER2, 0);
1293bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADR_FLT_MASK1_2, 0);
1294bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_ADR_FLT_MASK0, 0);
129542c1b001SThomas Moestl 
12961ed3fed7SMarius Strobl 		sc->sc_flags |= GEM_INITED;
129742c1b001SThomas Moestl 	}
129842c1b001SThomas Moestl 
12992a79fd39SMarius Strobl 	/* Counters need to be zeroed. */
1300bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_NORM_COLL_CNT, 0);
1301bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_FIRST_COLL_CNT, 0);
1302bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_EXCESS_COLL_CNT, 0);
1303bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_LATE_COLL_CNT, 0);
1304bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_DEFER_TMR_CNT, 0);
1305bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_PEAK_ATTEMPTS, 0);
1306bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_FRAME_COUNT, 0);
1307bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_LEN_ERR_CNT, 0);
1308bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_ALIGN_ERR, 0);
1309bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CRC_ERR_CNT, 0);
1310bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CODE_VIOL, 0);
131142c1b001SThomas Moestl 
13121ed3fed7SMarius Strobl 	/* Set XOFF PAUSE time. */
1313bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0);
13141ed3fed7SMarius Strobl 
13152a79fd39SMarius Strobl 	/* Set the station address. */
1316bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR0, (laddr[4] << 8) | laddr[5]);
1317bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR1, (laddr[2] << 8) | laddr[3]);
1318bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR2, (laddr[0] << 8) | laddr[1]);
1319336cca9eSBenno Rice 
13201ed3fed7SMarius Strobl 	/* Enable MII outputs. */
1321bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_XIF_CONFIG, GEM_MAC_XIF_TX_MII_ENA);
132242c1b001SThomas Moestl }
132342c1b001SThomas Moestl 
132442c1b001SThomas Moestl static void
13252a79fd39SMarius Strobl gem_start(struct ifnet *ifp)
132642c1b001SThomas Moestl {
13272a79fd39SMarius Strobl 	struct gem_softc *sc = ifp->if_softc;
13288cfaff7dSMarius Strobl 
13298cfaff7dSMarius Strobl 	GEM_LOCK(sc);
13308cfaff7dSMarius Strobl 	gem_start_locked(ifp);
13318cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
13328cfaff7dSMarius Strobl }
13338cfaff7dSMarius Strobl 
13349ba2b298SMarius Strobl static inline void
13359ba2b298SMarius Strobl gem_txkick(struct gem_softc *sc)
13369ba2b298SMarius Strobl {
13379ba2b298SMarius Strobl 
13389ba2b298SMarius Strobl 	/*
13399ba2b298SMarius Strobl 	 * Update the TX kick register.  This register has to point to the
13409ba2b298SMarius Strobl 	 * descriptor after the last valid one and for optimum performance
13419ba2b298SMarius Strobl 	 * should be incremented in multiples of 4 (the DMA engine fetches/
13429ba2b298SMarius Strobl 	 * updates descriptors in batches of 4).
13439ba2b298SMarius Strobl 	 */
13449ba2b298SMarius Strobl #ifdef GEM_DEBUG
13459ba2b298SMarius Strobl 	CTR3(KTR_GEM, "%s: %s: kicking TX %d",
13469ba2b298SMarius Strobl 	    device_get_name(sc->sc_dev), __func__, sc->sc_txnext);
13479ba2b298SMarius Strobl #endif
13489ba2b298SMarius Strobl 	GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
13499ba2b298SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_TX_KICK, sc->sc_txnext);
13509ba2b298SMarius Strobl }
13519ba2b298SMarius Strobl 
13528cfaff7dSMarius Strobl static void
13532a79fd39SMarius Strobl gem_start_locked(struct ifnet *ifp)
13548cfaff7dSMarius Strobl {
13552a79fd39SMarius Strobl 	struct gem_softc *sc = ifp->if_softc;
135612fb0330SPyun YongHyeon 	struct mbuf *m;
13579ba2b298SMarius Strobl 	int kicked, ntx;
13589ba2b298SMarius Strobl 
13599ba2b298SMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
136042c1b001SThomas Moestl 
136113f4c340SRobert Watson 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
13621ed3fed7SMarius Strobl 	    IFF_DRV_RUNNING || (sc->sc_flags & GEM_LINK) == 0)
136342c1b001SThomas Moestl 		return;
136442c1b001SThomas Moestl 
136518100346SThomas Moestl #ifdef GEM_DEBUG
136612fb0330SPyun YongHyeon 	CTR4(KTR_GEM, "%s: %s: txfree %d, txnext %d",
13671ed3fed7SMarius Strobl 	    device_get_name(sc->sc_dev), __func__, sc->sc_txfree,
13681ed3fed7SMarius Strobl 	    sc->sc_txnext);
136918100346SThomas Moestl #endif
13702a79fd39SMarius Strobl 	ntx = 0;
13719ba2b298SMarius Strobl 	kicked = 0;
137212fb0330SPyun YongHyeon 	for (; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) && sc->sc_txfree > 1;) {
137312fb0330SPyun YongHyeon 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
137412fb0330SPyun YongHyeon 		if (m == NULL)
137542c1b001SThomas Moestl 			break;
13761ed3fed7SMarius Strobl 		if (gem_load_txmbuf(sc, &m) != 0) {
137712fb0330SPyun YongHyeon 			if (m == NULL)
137812fb0330SPyun YongHyeon 				break;
137912fb0330SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
138012fb0330SPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, m);
138142c1b001SThomas Moestl 			break;
138242c1b001SThomas Moestl 		}
13839ba2b298SMarius Strobl 		if ((sc->sc_txnext % 4) == 0) {
13849ba2b298SMarius Strobl 			gem_txkick(sc);
13859ba2b298SMarius Strobl 			kicked = 1;
13869ba2b298SMarius Strobl 		} else
13879ba2b298SMarius Strobl 			kicked = 0;
138818100346SThomas Moestl 		ntx++;
138912fb0330SPyun YongHyeon 		BPF_MTAP(ifp, m);
1390305f2c06SThomas Moestl 	}
1391305f2c06SThomas Moestl 
1392305f2c06SThomas Moestl 	if (ntx > 0) {
13939ba2b298SMarius Strobl 		if (kicked == 0)
13949ba2b298SMarius Strobl 			gem_txkick(sc);
139518100346SThomas Moestl #ifdef GEM_DEBUG
1396305f2c06SThomas Moestl 		CTR2(KTR_GEM, "%s: packets enqueued, OWN on %d",
13971ed3fed7SMarius Strobl 		    device_get_name(sc->sc_dev), sc->sc_txnext);
139818100346SThomas Moestl #endif
1399305f2c06SThomas Moestl 
140042c1b001SThomas Moestl 		/* Set a watchdog timer in case the chip flakes out. */
14018cb37876SMarius Strobl 		sc->sc_wdog_timer = 5;
140218100346SThomas Moestl #ifdef GEM_DEBUG
140312fb0330SPyun YongHyeon 		CTR3(KTR_GEM, "%s: %s: watchdog %d",
14042a79fd39SMarius Strobl 		    device_get_name(sc->sc_dev), __func__,
14052a79fd39SMarius Strobl 		    sc->sc_wdog_timer);
140618100346SThomas Moestl #endif
140742c1b001SThomas Moestl 	}
140842c1b001SThomas Moestl }
140942c1b001SThomas Moestl 
141042c1b001SThomas Moestl static void
14112a79fd39SMarius Strobl gem_tint(struct gem_softc *sc)
141242c1b001SThomas Moestl {
1413fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
141442c1b001SThomas Moestl 	struct gem_txsoft *txs;
14159ba2b298SMarius Strobl 	int progress;
14169ba2b298SMarius Strobl 	uint32_t txlast;
141718100346SThomas Moestl #ifdef GEM_DEBUG
14182a79fd39SMarius Strobl 	int i;
14192a79fd39SMarius Strobl 
14209ba2b298SMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
14219ba2b298SMarius Strobl 
142212fb0330SPyun YongHyeon 	CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__);
142318100346SThomas Moestl #endif
142442c1b001SThomas Moestl 
142542c1b001SThomas Moestl 	/*
14262a79fd39SMarius Strobl 	 * Go through our TX list and free mbufs for those
142742c1b001SThomas Moestl 	 * frames that have been transmitted.
142842c1b001SThomas Moestl 	 */
14292a79fd39SMarius Strobl 	progress = 0;
1430b2d59f42SThomas Moestl 	GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD);
143142c1b001SThomas Moestl 	while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
143242c1b001SThomas Moestl #ifdef GEM_DEBUG
14332a79fd39SMarius Strobl 		if ((ifp->if_flags & IFF_DEBUG) != 0) {
143442c1b001SThomas Moestl 			printf("    txsoft %p transmit chain:\n", txs);
143542c1b001SThomas Moestl 			for (i = txs->txs_firstdesc;; i = GEM_NEXTTX(i)) {
143642c1b001SThomas Moestl 				printf("descriptor %d: ", i);
14372a79fd39SMarius Strobl 				printf("gd_flags: 0x%016llx\t",
14382a79fd39SMarius Strobl 				    (long long)GEM_DMA_READ(sc,
14392a79fd39SMarius Strobl 				    sc->sc_txdescs[i].gd_flags));
14402a79fd39SMarius Strobl 				printf("gd_addr: 0x%016llx\n",
14412a79fd39SMarius Strobl 				    (long long)GEM_DMA_READ(sc,
14422a79fd39SMarius Strobl 				    sc->sc_txdescs[i].gd_addr));
144342c1b001SThomas Moestl 				if (i == txs->txs_lastdesc)
144442c1b001SThomas Moestl 					break;
144542c1b001SThomas Moestl 			}
144642c1b001SThomas Moestl 		}
144742c1b001SThomas Moestl #endif
144842c1b001SThomas Moestl 
144942c1b001SThomas Moestl 		/*
14501ed3fed7SMarius Strobl 		 * In theory, we could harvest some descriptors before
145142c1b001SThomas Moestl 		 * the ring is empty, but that's a bit complicated.
145242c1b001SThomas Moestl 		 *
145342c1b001SThomas Moestl 		 * GEM_TX_COMPLETION points to the last descriptor
145442c1b001SThomas Moestl 		 * processed + 1.
145542c1b001SThomas Moestl 		 */
1456bd3d9826SMarius Strobl 		txlast = GEM_BANK1_READ_4(sc, GEM_TX_COMPLETION);
145718100346SThomas Moestl #ifdef GEM_DEBUG
145812fb0330SPyun YongHyeon 		CTR4(KTR_GEM, "%s: txs->txs_firstdesc = %d, "
145942c1b001SThomas Moestl 		    "txs->txs_lastdesc = %d, txlast = %d",
146012fb0330SPyun YongHyeon 		    __func__, txs->txs_firstdesc, txs->txs_lastdesc, txlast);
146118100346SThomas Moestl #endif
146242c1b001SThomas Moestl 		if (txs->txs_firstdesc <= txs->txs_lastdesc) {
146342c1b001SThomas Moestl 			if ((txlast >= txs->txs_firstdesc) &&
146442c1b001SThomas Moestl 			    (txlast <= txs->txs_lastdesc))
146542c1b001SThomas Moestl 				break;
146642c1b001SThomas Moestl 		} else {
14672a79fd39SMarius Strobl 			/* Ick -- this command wraps. */
146842c1b001SThomas Moestl 			if ((txlast >= txs->txs_firstdesc) ||
146942c1b001SThomas Moestl 			    (txlast <= txs->txs_lastdesc))
147042c1b001SThomas Moestl 				break;
147142c1b001SThomas Moestl 		}
147242c1b001SThomas Moestl 
147318100346SThomas Moestl #ifdef GEM_DEBUG
14742a79fd39SMarius Strobl 		CTR1(KTR_GEM, "%s: releasing a descriptor", __func__);
147518100346SThomas Moestl #endif
147642c1b001SThomas Moestl 		STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
147742c1b001SThomas Moestl 
147842c1b001SThomas Moestl 		sc->sc_txfree += txs->txs_ndescs;
147942c1b001SThomas Moestl 
1480305f2c06SThomas Moestl 		bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap,
148142c1b001SThomas Moestl 		    BUS_DMASYNC_POSTWRITE);
1482305f2c06SThomas Moestl 		bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap);
148342c1b001SThomas Moestl 		if (txs->txs_mbuf != NULL) {
148442c1b001SThomas Moestl 			m_freem(txs->txs_mbuf);
148542c1b001SThomas Moestl 			txs->txs_mbuf = NULL;
148642c1b001SThomas Moestl 		}
148742c1b001SThomas Moestl 
148842c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
148942c1b001SThomas Moestl 
14908da56a6fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1491336cca9eSBenno Rice 		progress = 1;
149242c1b001SThomas Moestl 	}
149342c1b001SThomas Moestl 
149418100346SThomas Moestl #ifdef GEM_DEBUG
14952a79fd39SMarius Strobl 	CTR4(KTR_GEM, "%s: GEM_TX_STATE_MACHINE %x GEM_TX_DATA_PTR %llx "
149642c1b001SThomas Moestl 	    "GEM_TX_COMPLETION %x",
1497bd3d9826SMarius Strobl 	    __func__, GEM_BANK1_READ_4(sc, GEM_TX_STATE_MACHINE),
1498bd3d9826SMarius Strobl 	    ((long long)GEM_BANK1_READ_4(sc, GEM_TX_DATA_PTR_HI) << 32) |
1499bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_TX_DATA_PTR_LO),
1500bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_TX_COMPLETION));
150118100346SThomas Moestl #endif
150242c1b001SThomas Moestl 
1503336cca9eSBenno Rice 	if (progress) {
1504336cca9eSBenno Rice 		if (sc->sc_txfree == GEM_NTXDESC - 1)
1505336cca9eSBenno Rice 			sc->sc_txwin = 0;
150642c1b001SThomas Moestl 
15072a79fd39SMarius Strobl 		/*
15082a79fd39SMarius Strobl 		 * We freed some descriptors, so reset IFF_DRV_OACTIVE
15092a79fd39SMarius Strobl 		 * and restart.
15102a79fd39SMarius Strobl 		 */
151113f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
15129ba2b298SMarius Strobl 		if (STAILQ_EMPTY(&sc->sc_txdirtyq))
15139ba2b298SMarius Strobl 		    sc->sc_wdog_timer = 0;
151412fb0330SPyun YongHyeon 		gem_start_locked(ifp);
1515336cca9eSBenno Rice 	}
151642c1b001SThomas Moestl 
151718100346SThomas Moestl #ifdef GEM_DEBUG
151812fb0330SPyun YongHyeon 	CTR3(KTR_GEM, "%s: %s: watchdog %d",
151912fb0330SPyun YongHyeon 	    device_get_name(sc->sc_dev), __func__, sc->sc_wdog_timer);
152018100346SThomas Moestl #endif
152142c1b001SThomas Moestl }
152242c1b001SThomas Moestl 
1523c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT
15240d80b9bdSThomas Moestl static void
15252a79fd39SMarius Strobl gem_rint_timeout(void *arg)
15260d80b9bdSThomas Moestl {
15272a79fd39SMarius Strobl 	struct gem_softc *sc = arg;
15280d80b9bdSThomas Moestl 
15291f317bf9SMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
15309ba2b298SMarius Strobl 
15318cfaff7dSMarius Strobl 	gem_rint(sc);
15320d80b9bdSThomas Moestl }
153311e3f060SJake Burkholder #endif
15340d80b9bdSThomas Moestl 
153542c1b001SThomas Moestl static void
15362a79fd39SMarius Strobl gem_rint(struct gem_softc *sc)
153742c1b001SThomas Moestl {
1538fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
153942c1b001SThomas Moestl 	struct mbuf *m;
15402a79fd39SMarius Strobl 	uint64_t rxstat;
15412a79fd39SMarius Strobl 	uint32_t rxcomp;
154242c1b001SThomas Moestl 
15439ba2b298SMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
15449ba2b298SMarius Strobl 
1545c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT
15460d80b9bdSThomas Moestl 	callout_stop(&sc->sc_rx_ch);
1547c3d5598aSMarius Strobl #endif
154818100346SThomas Moestl #ifdef GEM_DEBUG
154912fb0330SPyun YongHyeon 	CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__);
155018100346SThomas Moestl #endif
1551336cca9eSBenno Rice 
1552336cca9eSBenno Rice 	/*
1553336cca9eSBenno Rice 	 * Read the completion register once.  This limits
1554336cca9eSBenno Rice 	 * how long the following loop can execute.
1555336cca9eSBenno Rice 	 */
1556bd3d9826SMarius Strobl 	rxcomp = GEM_BANK1_READ_4(sc, GEM_RX_COMPLETION);
155718100346SThomas Moestl #ifdef GEM_DEBUG
15589ba2b298SMarius Strobl 	CTR3(KTR_GEM, "%s: sc->sc_rxptr %d, complete %d",
155912fb0330SPyun YongHyeon 	    __func__, sc->sc_rxptr, rxcomp);
156018100346SThomas Moestl #endif
15619ba2b298SMarius Strobl 	GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
15621ed3fed7SMarius Strobl 	for (; sc->sc_rxptr != rxcomp;) {
15631ed3fed7SMarius Strobl 		m = sc->sc_rxsoft[sc->sc_rxptr].rxs_mbuf;
15641ed3fed7SMarius Strobl 		rxstat = GEM_DMA_READ(sc,
15651ed3fed7SMarius Strobl 		    sc->sc_rxdescs[sc->sc_rxptr].gd_flags);
156642c1b001SThomas Moestl 
156742c1b001SThomas Moestl 		if (rxstat & GEM_RD_OWN) {
1568c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT
156942c1b001SThomas Moestl 			/*
15700d80b9bdSThomas Moestl 			 * The descriptor is still marked as owned, although
15710d80b9bdSThomas Moestl 			 * it is supposed to have completed.  This has been
15720d80b9bdSThomas Moestl 			 * observed on some machines.  Just exiting here
15730d80b9bdSThomas Moestl 			 * might leave the packet sitting around until another
15740d80b9bdSThomas Moestl 			 * one arrives to trigger a new interrupt, which is
15750d80b9bdSThomas Moestl 			 * generally undesirable, so set up a timeout.
157642c1b001SThomas Moestl 			 */
15770d80b9bdSThomas Moestl 			callout_reset(&sc->sc_rx_ch, GEM_RXOWN_TICKS,
15780d80b9bdSThomas Moestl 			    gem_rint_timeout, sc);
1579336cca9eSBenno Rice #endif
15801ed3fed7SMarius Strobl 			m = NULL;
15811ed3fed7SMarius Strobl 			goto kickit;
158242c1b001SThomas Moestl 		}
158342c1b001SThomas Moestl 
158442c1b001SThomas Moestl 		if (rxstat & GEM_RD_BAD_CRC) {
15858da56a6fSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
158642c1b001SThomas Moestl 			device_printf(sc->sc_dev, "receive error: CRC error\n");
15871ed3fed7SMarius Strobl 			GEM_INIT_RXDESC(sc, sc->sc_rxptr);
15881ed3fed7SMarius Strobl 			m = NULL;
15891ed3fed7SMarius Strobl 			goto kickit;
159042c1b001SThomas Moestl 		}
159142c1b001SThomas Moestl 
159242c1b001SThomas Moestl #ifdef GEM_DEBUG
15932a79fd39SMarius Strobl 		if ((ifp->if_flags & IFF_DEBUG) != 0) {
15941ed3fed7SMarius Strobl 			printf("    rxsoft %p descriptor %d: ",
15951ed3fed7SMarius Strobl 			    &sc->sc_rxsoft[sc->sc_rxptr], sc->sc_rxptr);
15962a79fd39SMarius Strobl 			printf("gd_flags: 0x%016llx\t",
15972a79fd39SMarius Strobl 			    (long long)GEM_DMA_READ(sc,
15982a79fd39SMarius Strobl 			    sc->sc_rxdescs[sc->sc_rxptr].gd_flags));
15992a79fd39SMarius Strobl 			printf("gd_addr: 0x%016llx\n",
16002a79fd39SMarius Strobl 			    (long long)GEM_DMA_READ(sc,
16012a79fd39SMarius Strobl 			    sc->sc_rxdescs[sc->sc_rxptr].gd_addr));
160242c1b001SThomas Moestl 		}
160342c1b001SThomas Moestl #endif
160442c1b001SThomas Moestl 
160542c1b001SThomas Moestl 		/*
160642c1b001SThomas Moestl 		 * Allocate a new mbuf cluster.  If that fails, we are
160742c1b001SThomas Moestl 		 * out of memory, and must drop the packet and recycle
160842c1b001SThomas Moestl 		 * the buffer that's already attached to this descriptor.
160942c1b001SThomas Moestl 		 */
16101ed3fed7SMarius Strobl 		if (gem_add_rxbuf(sc, sc->sc_rxptr) != 0) {
16118da56a6fSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
16121ed3fed7SMarius Strobl 			GEM_INIT_RXDESC(sc, sc->sc_rxptr);
16131ed3fed7SMarius Strobl 			m = NULL;
16141ed3fed7SMarius Strobl 		}
16151ed3fed7SMarius Strobl 
16161ed3fed7SMarius Strobl  kickit:
16171ed3fed7SMarius Strobl 		/*
16181ed3fed7SMarius Strobl 		 * Update the RX kick register.  This register has to point
16191ed3fed7SMarius Strobl 		 * to the descriptor after the last valid one (before the
16209ba2b298SMarius Strobl 		 * current batch) and for optimum performance should be
16219ba2b298SMarius Strobl 		 * incremented in multiples of 4 (the DMA engine fetches/
16229ba2b298SMarius Strobl 		 * updates descriptors in batches of 4).
16231ed3fed7SMarius Strobl 		 */
16241ed3fed7SMarius Strobl 		sc->sc_rxptr = GEM_NEXTRX(sc->sc_rxptr);
16251ed3fed7SMarius Strobl 		if ((sc->sc_rxptr % 4) == 0) {
1626ccb1212aSMarius Strobl 			GEM_CDSYNC(sc,
1627ccb1212aSMarius Strobl 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1628bd3d9826SMarius Strobl 			GEM_BANK1_WRITE_4(sc, GEM_RX_KICK,
16291ed3fed7SMarius Strobl 			    (sc->sc_rxptr + GEM_NRXDESC - 4) &
16301ed3fed7SMarius Strobl 			    GEM_NRXDESC_MASK);
16311ed3fed7SMarius Strobl 		}
16321ed3fed7SMarius Strobl 
16331ed3fed7SMarius Strobl 		if (m == NULL) {
16341ed3fed7SMarius Strobl 			if (rxstat & GEM_RD_OWN)
16351ed3fed7SMarius Strobl 				break;
163642c1b001SThomas Moestl 			continue;
163742c1b001SThomas Moestl 		}
163842c1b001SThomas Moestl 
16398da56a6fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
16409ba2b298SMarius Strobl 		m->m_data += ETHER_ALIGN; /* first byte offset */
164142c1b001SThomas Moestl 		m->m_pkthdr.rcvif = ifp;
16421ed3fed7SMarius Strobl 		m->m_pkthdr.len = m->m_len = GEM_RD_BUFLEN(rxstat);
164312fb0330SPyun YongHyeon 
164412fb0330SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
164512fb0330SPyun YongHyeon 			gem_rxcksum(m, rxstat);
164642c1b001SThomas Moestl 
164742c1b001SThomas Moestl 		/* Pass it on. */
16488cfaff7dSMarius Strobl 		GEM_UNLOCK(sc);
1649673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
16508cfaff7dSMarius Strobl 		GEM_LOCK(sc);
165142c1b001SThomas Moestl 	}
165242c1b001SThomas Moestl 
165318100346SThomas Moestl #ifdef GEM_DEBUG
16549ba2b298SMarius Strobl 	CTR3(KTR_GEM, "%s: done sc->sc_rxptr %d, complete %d", __func__,
1655bd3d9826SMarius Strobl 	    sc->sc_rxptr, GEM_BANK1_READ_4(sc, GEM_RX_COMPLETION));
165618100346SThomas Moestl #endif
165742c1b001SThomas Moestl }
165842c1b001SThomas Moestl 
165942c1b001SThomas Moestl static int
16602a79fd39SMarius Strobl gem_add_rxbuf(struct gem_softc *sc, int idx)
166142c1b001SThomas Moestl {
166242c1b001SThomas Moestl 	struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx];
166342c1b001SThomas Moestl 	struct mbuf *m;
1664c3d5598aSMarius Strobl 	bus_dma_segment_t segs[1];
1665c3d5598aSMarius Strobl 	int error, nsegs;
166642c1b001SThomas Moestl 
16679ba2b298SMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
16689ba2b298SMarius Strobl 
1669c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
167042c1b001SThomas Moestl 	if (m == NULL)
167142c1b001SThomas Moestl 		return (ENOBUFS);
1672305f2c06SThomas Moestl 	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
167342c1b001SThomas Moestl 
167442c1b001SThomas Moestl #ifdef GEM_DEBUG
16752a79fd39SMarius Strobl 	/* Bzero the packet to check DMA. */
167642c1b001SThomas Moestl 	memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size);
167742c1b001SThomas Moestl #endif
167842c1b001SThomas Moestl 
1679b2d59f42SThomas Moestl 	if (rxs->rxs_mbuf != NULL) {
1680b2d59f42SThomas Moestl 		bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap,
1681b2d59f42SThomas Moestl 		    BUS_DMASYNC_POSTREAD);
1682305f2c06SThomas Moestl 		bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap);
1683b2d59f42SThomas Moestl 	}
168442c1b001SThomas Moestl 
1685c3d5598aSMarius Strobl 	error = bus_dmamap_load_mbuf_sg(sc->sc_rdmatag, rxs->rxs_dmamap,
1686c3d5598aSMarius Strobl 	    m, segs, &nsegs, BUS_DMA_NOWAIT);
1687c3d5598aSMarius Strobl 	if (error != 0) {
16882a79fd39SMarius Strobl 		device_printf(sc->sc_dev,
16892a79fd39SMarius Strobl 		    "cannot load RS DMA map %d, error = %d\n", idx, error);
1690c3d5598aSMarius Strobl 		m_freem(m);
16911ed3fed7SMarius Strobl 		return (error);
169242c1b001SThomas Moestl 	}
16932a79fd39SMarius Strobl 	/* If nsegs is wrong then the stack is corrupt. */
1694801772ecSMarius Strobl 	KASSERT(nsegs == 1,
1695801772ecSMarius Strobl 	    ("%s: too many DMA segments (%d)", __func__, nsegs));
16961ed3fed7SMarius Strobl 	rxs->rxs_mbuf = m;
1697c3d5598aSMarius Strobl 	rxs->rxs_paddr = segs[0].ds_addr;
169842c1b001SThomas Moestl 
16992a79fd39SMarius Strobl 	bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap,
17002a79fd39SMarius Strobl 	    BUS_DMASYNC_PREREAD);
170142c1b001SThomas Moestl 
170242c1b001SThomas Moestl 	GEM_INIT_RXDESC(sc, idx);
170342c1b001SThomas Moestl 
170442c1b001SThomas Moestl 	return (0);
170542c1b001SThomas Moestl }
170642c1b001SThomas Moestl 
170742c1b001SThomas Moestl static void
17082a79fd39SMarius Strobl gem_eint(struct gem_softc *sc, u_int status)
170942c1b001SThomas Moestl {
171042c1b001SThomas Moestl 
17118da56a6fSGleb Smirnoff 	if_inc_counter(sc->sc_ifp, IFCOUNTER_IERRORS, 1);
17121ed3fed7SMarius Strobl 	if ((status & GEM_INTR_RX_TAG_ERR) != 0) {
17131ed3fed7SMarius Strobl 		gem_reset_rxdma(sc);
171442c1b001SThomas Moestl 		return;
171542c1b001SThomas Moestl 	}
171642c1b001SThomas Moestl 
17179ba2b298SMarius Strobl 	device_printf(sc->sc_dev, "%s: status 0x%x", __func__, status);
17189ba2b298SMarius Strobl 	if ((status & GEM_INTR_BERR) != 0) {
17199ba2b298SMarius Strobl 		if ((sc->sc_flags & GEM_PCI) != 0)
17209ba2b298SMarius Strobl 			printf(", PCI bus error 0x%x\n",
17219ba2b298SMarius Strobl 			    GEM_BANK1_READ_4(sc, GEM_PCI_ERROR_STATUS));
17229ba2b298SMarius Strobl 		else
17239ba2b298SMarius Strobl 			printf(", SBus error 0x%x\n",
17249ba2b298SMarius Strobl 			    GEM_BANK1_READ_4(sc, GEM_SBUS_STATUS));
17259ba2b298SMarius Strobl 	}
172642c1b001SThomas Moestl }
172742c1b001SThomas Moestl 
172842c1b001SThomas Moestl void
17292a79fd39SMarius Strobl gem_intr(void *v)
173042c1b001SThomas Moestl {
17312a79fd39SMarius Strobl 	struct gem_softc *sc = v;
17321ed3fed7SMarius Strobl 	uint32_t status, status2;
173342c1b001SThomas Moestl 
17348cfaff7dSMarius Strobl 	GEM_LOCK(sc);
1735bd3d9826SMarius Strobl 	status = GEM_BANK1_READ_4(sc, GEM_STATUS);
17361ed3fed7SMarius Strobl 
173718100346SThomas Moestl #ifdef GEM_DEBUG
173812fb0330SPyun YongHyeon 	CTR4(KTR_GEM, "%s: %s: cplt %x, status %x",
17399ba2b298SMarius Strobl 	    device_get_name(sc->sc_dev), __func__,
17409ba2b298SMarius Strobl 	    (status >> GEM_STATUS_TX_COMPLETION_SHFT), (u_int)status);
17411ed3fed7SMarius Strobl 
17421ed3fed7SMarius Strobl 	/*
17431ed3fed7SMarius Strobl 	 * PCS interrupts must be cleared, otherwise no traffic is passed!
17441ed3fed7SMarius Strobl 	 */
17451ed3fed7SMarius Strobl 	if ((status & GEM_INTR_PCS) != 0) {
17462a79fd39SMarius Strobl 		status2 =
1747bd3d9826SMarius Strobl 		    GEM_BANK1_READ_4(sc, GEM_MII_INTERRUP_STATUS) |
1748bd3d9826SMarius Strobl 		    GEM_BANK1_READ_4(sc, GEM_MII_INTERRUP_STATUS);
17491ed3fed7SMarius Strobl 		if ((status2 & GEM_MII_INTERRUP_LINK) != 0)
17501ed3fed7SMarius Strobl 			device_printf(sc->sc_dev,
17511ed3fed7SMarius Strobl 			    "%s: PCS link status changed\n", __func__);
17521ed3fed7SMarius Strobl 	}
17531ed3fed7SMarius Strobl 	if ((status & GEM_MAC_CONTROL_STATUS) != 0) {
1754bd3d9826SMarius Strobl 		status2 = GEM_BANK1_READ_4(sc, GEM_MAC_CONTROL_STATUS);
17551ed3fed7SMarius Strobl 		if ((status2 & GEM_MAC_PAUSED) != 0)
17561ed3fed7SMarius Strobl 			device_printf(sc->sc_dev,
17571ed3fed7SMarius Strobl 			    "%s: PAUSE received (PAUSE time %d slots)\n",
17581ed3fed7SMarius Strobl 			    __func__, GEM_MAC_PAUSE_TIME(status2));
17591ed3fed7SMarius Strobl 		if ((status2 & GEM_MAC_PAUSE) != 0)
17601ed3fed7SMarius Strobl 			device_printf(sc->sc_dev,
17611ed3fed7SMarius Strobl 			    "%s: transited to PAUSE state\n", __func__);
17621ed3fed7SMarius Strobl 		if ((status2 & GEM_MAC_RESUME) != 0)
17631ed3fed7SMarius Strobl 			device_printf(sc->sc_dev,
17641ed3fed7SMarius Strobl 			    "%s: transited to non-PAUSE state\n", __func__);
17651ed3fed7SMarius Strobl 	}
17661ed3fed7SMarius Strobl 	if ((status & GEM_INTR_MIF) != 0)
17671ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "%s: MIF interrupt\n", __func__);
176818100346SThomas Moestl #endif
176942c1b001SThomas Moestl 
17709ba2b298SMarius Strobl 	if (__predict_false(status &
17711ed3fed7SMarius Strobl 	    (GEM_INTR_RX_TAG_ERR | GEM_INTR_PERR | GEM_INTR_BERR)) != 0)
177242c1b001SThomas Moestl 		gem_eint(sc, status);
177342c1b001SThomas Moestl 
177442c1b001SThomas Moestl 	if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0)
177542c1b001SThomas Moestl 		gem_rint(sc);
177642c1b001SThomas Moestl 
17771ed3fed7SMarius Strobl 	if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0)
17781ed3fed7SMarius Strobl 		gem_tint(sc);
17791ed3fed7SMarius Strobl 
17809ba2b298SMarius Strobl 	if (__predict_false((status & GEM_INTR_TX_MAC) != 0)) {
1781bd3d9826SMarius Strobl 		status2 = GEM_BANK1_READ_4(sc, GEM_MAC_TX_STATUS);
17822a79fd39SMarius Strobl 		if ((status2 &
17839ba2b298SMarius Strobl 		    ~(GEM_MAC_TX_XMIT_DONE | GEM_MAC_TX_DEFER_EXP |
17849ba2b298SMarius Strobl 		    GEM_MAC_TX_PEAK_EXP)) != 0)
17852a79fd39SMarius Strobl 			device_printf(sc->sc_dev,
17862a79fd39SMarius Strobl 			    "MAC TX fault, status %x\n", status2);
17872a79fd39SMarius Strobl 		if ((status2 &
17889ba2b298SMarius Strobl 		    (GEM_MAC_TX_UNDERRUN | GEM_MAC_TX_PKT_TOO_LONG)) != 0) {
17898da56a6fSGleb Smirnoff 			if_inc_counter(sc->sc_ifp, IFCOUNTER_OERRORS, 1);
179083242185SPyun YongHyeon 			sc->sc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
17918cfaff7dSMarius Strobl 			gem_init_locked(sc);
179242c1b001SThomas Moestl 		}
17939ba2b298SMarius Strobl 	}
17949ba2b298SMarius Strobl 	if (__predict_false((status & GEM_INTR_RX_MAC) != 0)) {
1795bd3d9826SMarius Strobl 		status2 = GEM_BANK1_READ_4(sc, GEM_MAC_RX_STATUS);
179600d12766SMarius Strobl 		/*
17971ed3fed7SMarius Strobl 		 * At least with GEM_SUN_GEM and some GEM_SUN_ERI
17981ed3fed7SMarius Strobl 		 * revisions GEM_MAC_RX_OVERFLOW happen often due to a
17991ed3fed7SMarius Strobl 		 * silicon bug so handle them silently.  Moreover, it's
18001ed3fed7SMarius Strobl 		 * likely that the receiver has hung so we reset it.
180100d12766SMarius Strobl 		 */
18022a79fd39SMarius Strobl 		if ((status2 & GEM_MAC_RX_OVERFLOW) != 0) {
18038da56a6fSGleb Smirnoff 			if_inc_counter(sc->sc_ifp, IFCOUNTER_IERRORS, 1);
18041ed3fed7SMarius Strobl 			gem_reset_rxdma(sc);
18052a79fd39SMarius Strobl 		} else if ((status2 &
18062a79fd39SMarius Strobl 		    ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT)) != 0)
18072a79fd39SMarius Strobl 			device_printf(sc->sc_dev,
18082a79fd39SMarius Strobl 			    "MAC RX fault, status %x\n", status2);
180942c1b001SThomas Moestl 	}
18108cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
181142c1b001SThomas Moestl }
181242c1b001SThomas Moestl 
18138cb37876SMarius Strobl static int
18142a79fd39SMarius Strobl gem_watchdog(struct gem_softc *sc)
181542c1b001SThomas Moestl {
1816ccb1212aSMarius Strobl 	struct ifnet *ifp = sc->sc_ifp;
181742c1b001SThomas Moestl 
18188cb37876SMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
18198cb37876SMarius Strobl 
182018100346SThomas Moestl #ifdef GEM_DEBUG
18212a79fd39SMarius Strobl 	CTR4(KTR_GEM,
18222a79fd39SMarius Strobl 	    "%s: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x GEM_MAC_RX_CONFIG %x",
1823bd3d9826SMarius Strobl 	    __func__, GEM_BANK1_READ_4(sc, GEM_RX_CONFIG),
1824bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_STATUS),
1825bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG));
18262a79fd39SMarius Strobl 	CTR4(KTR_GEM,
18272a79fd39SMarius Strobl 	    "%s: GEM_TX_CONFIG %x GEM_MAC_TX_STATUS %x GEM_MAC_TX_CONFIG %x",
1828bd3d9826SMarius Strobl 	    __func__, GEM_BANK1_READ_4(sc, GEM_TX_CONFIG),
1829bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_TX_STATUS),
1830bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG));
183118100346SThomas Moestl #endif
183242c1b001SThomas Moestl 
18338cb37876SMarius Strobl 	if (sc->sc_wdog_timer == 0 || --sc->sc_wdog_timer != 0)
18348cb37876SMarius Strobl 		return (0);
18358cb37876SMarius Strobl 
18361ed3fed7SMarius Strobl 	if ((sc->sc_flags & GEM_LINK) != 0)
183742c1b001SThomas Moestl 		device_printf(sc->sc_dev, "device timeout\n");
18381ed3fed7SMarius Strobl 	else if (bootverbose)
18391ed3fed7SMarius Strobl 		device_printf(sc->sc_dev, "device timeout (no link)\n");
18408da56a6fSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
184142c1b001SThomas Moestl 
184242c1b001SThomas Moestl 	/* Try to get more packets going. */
184383242185SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
18448cfaff7dSMarius Strobl 	gem_init_locked(sc);
1845ccb1212aSMarius Strobl 	gem_start_locked(ifp);
18468cb37876SMarius Strobl 	return (EJUSTRETURN);
184742c1b001SThomas Moestl }
184842c1b001SThomas Moestl 
184942c1b001SThomas Moestl static void
18502a79fd39SMarius Strobl gem_mifinit(struct gem_softc *sc)
185142c1b001SThomas Moestl {
185242c1b001SThomas Moestl 
1853801772ecSMarius Strobl 	/* Configure the MIF in frame mode. */
1854bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MIF_CONFIG,
1855bd3d9826SMarius Strobl 	    GEM_BANK1_READ_4(sc, GEM_MIF_CONFIG) & ~GEM_MIF_CONFIG_BB_ENA);
185665f2c0ffSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MIF_CONFIG, 4,
185765f2c0ffSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
185842c1b001SThomas Moestl }
185942c1b001SThomas Moestl 
186042c1b001SThomas Moestl /*
186142c1b001SThomas Moestl  * MII interface
186242c1b001SThomas Moestl  *
186378d22f42SMarius Strobl  * The MII interface supports at least three different operating modes:
186442c1b001SThomas Moestl  *
186542c1b001SThomas Moestl  * Bitbang mode is implemented using data, clock and output enable registers.
186642c1b001SThomas Moestl  *
186742c1b001SThomas Moestl  * Frame mode is implemented by loading a complete frame into the frame
186842c1b001SThomas Moestl  * register and polling the valid bit for completion.
186942c1b001SThomas Moestl  *
187042c1b001SThomas Moestl  * Polling mode uses the frame register but completion is indicated by
187142c1b001SThomas Moestl  * an interrupt.
187242c1b001SThomas Moestl  *
187342c1b001SThomas Moestl  */
187442c1b001SThomas Moestl int
18752a79fd39SMarius Strobl gem_mii_readreg(device_t dev, int phy, int reg)
187642c1b001SThomas Moestl {
18772a79fd39SMarius Strobl 	struct gem_softc *sc;
187842c1b001SThomas Moestl 	int n;
18792a79fd39SMarius Strobl 	uint32_t v;
188042c1b001SThomas Moestl 
188142c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY
18821ed3fed7SMarius Strobl 	printf("%s: phy %d reg %d\n", __func__, phy, reg);
188342c1b001SThomas Moestl #endif
188442c1b001SThomas Moestl 
18852a79fd39SMarius Strobl 	sc = device_get_softc(dev);
18861ed3fed7SMarius Strobl 	if ((sc->sc_flags & GEM_SERDES) != 0) {
18871ed3fed7SMarius Strobl 		switch (reg) {
18881ed3fed7SMarius Strobl 		case MII_BMCR:
18891ed3fed7SMarius Strobl 			reg = GEM_MII_CONTROL;
18901ed3fed7SMarius Strobl 			break;
18911ed3fed7SMarius Strobl 		case MII_BMSR:
18921ed3fed7SMarius Strobl 			reg = GEM_MII_STATUS;
18931ed3fed7SMarius Strobl 			break;
18941ed3fed7SMarius Strobl 		case MII_PHYIDR1:
18951ed3fed7SMarius Strobl 		case MII_PHYIDR2:
18961ed3fed7SMarius Strobl 			return (0);
18971ed3fed7SMarius Strobl 		case MII_ANAR:
18981ed3fed7SMarius Strobl 			reg = GEM_MII_ANAR;
18991ed3fed7SMarius Strobl 			break;
19001ed3fed7SMarius Strobl 		case MII_ANLPAR:
19011ed3fed7SMarius Strobl 			reg = GEM_MII_ANLPAR;
19021ed3fed7SMarius Strobl 			break;
19031ed3fed7SMarius Strobl 		case MII_EXTSR:
19041ed3fed7SMarius Strobl 			return (EXTSR_1000XFDX | EXTSR_1000XHDX);
19051ed3fed7SMarius Strobl 		default:
19061ed3fed7SMarius Strobl 			device_printf(sc->sc_dev,
19071ed3fed7SMarius Strobl 			    "%s: unhandled register %d\n", __func__, reg);
19081ed3fed7SMarius Strobl 			return (0);
19091ed3fed7SMarius Strobl 		}
1910bd3d9826SMarius Strobl 		return (GEM_BANK1_READ_4(sc, reg));
19111ed3fed7SMarius Strobl 	}
191242c1b001SThomas Moestl 
19132a79fd39SMarius Strobl 	/* Construct the frame command. */
19141ed3fed7SMarius Strobl 	v = GEM_MIF_FRAME_READ |
19151ed3fed7SMarius Strobl 	    (phy << GEM_MIF_PHY_SHIFT) |
19161ed3fed7SMarius Strobl 	    (reg << GEM_MIF_REG_SHIFT);
191742c1b001SThomas Moestl 
1918bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MIF_FRAME, v);
1919ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MIF_FRAME, 4,
1920ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
192142c1b001SThomas Moestl 	for (n = 0; n < 100; n++) {
192242c1b001SThomas Moestl 		DELAY(1);
1923bd3d9826SMarius Strobl 		v = GEM_BANK1_READ_4(sc, GEM_MIF_FRAME);
19241f317bf9SMarius Strobl 		if (v & GEM_MIF_FRAME_TA0)
192542c1b001SThomas Moestl 			return (v & GEM_MIF_FRAME_DATA);
192642c1b001SThomas Moestl 	}
192742c1b001SThomas Moestl 
19282a79fd39SMarius Strobl 	device_printf(sc->sc_dev, "%s: timed out\n", __func__);
192942c1b001SThomas Moestl 	return (0);
193042c1b001SThomas Moestl }
193142c1b001SThomas Moestl 
193242c1b001SThomas Moestl int
19332a79fd39SMarius Strobl gem_mii_writereg(device_t dev, int phy, int reg, int val)
193442c1b001SThomas Moestl {
19352a79fd39SMarius Strobl 	struct gem_softc *sc;
193642c1b001SThomas Moestl 	int n;
19372a79fd39SMarius Strobl 	uint32_t v;
193842c1b001SThomas Moestl 
193942c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY
19401ed3fed7SMarius Strobl 	printf("%s: phy %d reg %d val %x\n", phy, reg, val, __func__);
194142c1b001SThomas Moestl #endif
194242c1b001SThomas Moestl 
19432a79fd39SMarius Strobl 	sc = device_get_softc(dev);
19441ed3fed7SMarius Strobl 	if ((sc->sc_flags & GEM_SERDES) != 0) {
19451ed3fed7SMarius Strobl 		switch (reg) {
19461ed3fed7SMarius Strobl 		case MII_BMSR:
19471ed3fed7SMarius Strobl 			reg = GEM_MII_STATUS;
19481ed3fed7SMarius Strobl 			break;
1949ccb1212aSMarius Strobl 		case MII_BMCR:
1950ccb1212aSMarius Strobl 			reg = GEM_MII_CONTROL;
1951ccb1212aSMarius Strobl 			if ((val & GEM_MII_CONTROL_RESET) == 0)
1952ccb1212aSMarius Strobl 				break;
1953ccb1212aSMarius Strobl 			GEM_BANK1_WRITE_4(sc, GEM_MII_CONTROL, val);
1954ccb1212aSMarius Strobl 			GEM_BANK1_BARRIER(sc, GEM_MII_CONTROL, 4,
1955ccb1212aSMarius Strobl 			    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
1956ccb1212aSMarius Strobl 			if (!GEM_BANK1_BITWAIT(sc, GEM_MII_CONTROL,
1957ccb1212aSMarius Strobl 			    GEM_MII_CONTROL_RESET, 0))
1958ccb1212aSMarius Strobl 				device_printf(sc->sc_dev,
1959ccb1212aSMarius Strobl 				    "cannot reset PCS\n");
1960ccb1212aSMarius Strobl 			/* FALLTHROUGH */
19611ed3fed7SMarius Strobl 		case MII_ANAR:
1962bd3d9826SMarius Strobl 			GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG, 0);
1963bd3d9826SMarius Strobl 			GEM_BANK1_BARRIER(sc, GEM_MII_CONFIG, 4,
19641ed3fed7SMarius Strobl 			    BUS_SPACE_BARRIER_WRITE);
1965bd3d9826SMarius Strobl 			GEM_BANK1_WRITE_4(sc, GEM_MII_ANAR, val);
196665f2c0ffSMarius Strobl 			GEM_BANK1_BARRIER(sc, GEM_MII_ANAR, 4,
196765f2c0ffSMarius Strobl 			    BUS_SPACE_BARRIER_WRITE);
1968bd3d9826SMarius Strobl 			GEM_BANK1_WRITE_4(sc, GEM_MII_SLINK_CONTROL,
19691ed3fed7SMarius Strobl 			    GEM_MII_SLINK_LOOPBACK | GEM_MII_SLINK_EN_SYNC_D);
197065f2c0ffSMarius Strobl 			GEM_BANK1_BARRIER(sc, GEM_MII_SLINK_CONTROL, 4,
197165f2c0ffSMarius Strobl 			    BUS_SPACE_BARRIER_WRITE);
1972bd3d9826SMarius Strobl 			GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG,
19731ed3fed7SMarius Strobl 			    GEM_MII_CONFIG_ENABLE);
197465f2c0ffSMarius Strobl 			GEM_BANK1_BARRIER(sc, GEM_MII_CONFIG, 4,
197565f2c0ffSMarius Strobl 			    BUS_SPACE_BARRIER_WRITE);
19761ed3fed7SMarius Strobl 			return (0);
19771ed3fed7SMarius Strobl 		case MII_ANLPAR:
19781ed3fed7SMarius Strobl 			reg = GEM_MII_ANLPAR;
19791ed3fed7SMarius Strobl 			break;
19801ed3fed7SMarius Strobl 		default:
19811ed3fed7SMarius Strobl 			device_printf(sc->sc_dev,
19821ed3fed7SMarius Strobl 			    "%s: unhandled register %d\n", __func__, reg);
19831ed3fed7SMarius Strobl 			return (0);
19841ed3fed7SMarius Strobl 		}
1985bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, reg, val);
198665f2c0ffSMarius Strobl 		GEM_BANK1_BARRIER(sc, reg, 4,
198765f2c0ffSMarius Strobl 		    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
19881ed3fed7SMarius Strobl 		return (0);
19891ed3fed7SMarius Strobl 	}
19901ed3fed7SMarius Strobl 
19912a79fd39SMarius Strobl 	/* Construct the frame command. */
199242c1b001SThomas Moestl 	v = GEM_MIF_FRAME_WRITE |
199342c1b001SThomas Moestl 	    (phy << GEM_MIF_PHY_SHIFT) |
199442c1b001SThomas Moestl 	    (reg << GEM_MIF_REG_SHIFT) |
199542c1b001SThomas Moestl 	    (val & GEM_MIF_FRAME_DATA);
199642c1b001SThomas Moestl 
1997bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MIF_FRAME, v);
1998ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MIF_FRAME, 4,
1999ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
200042c1b001SThomas Moestl 	for (n = 0; n < 100; n++) {
200142c1b001SThomas Moestl 		DELAY(1);
2002bd3d9826SMarius Strobl 		v = GEM_BANK1_READ_4(sc, GEM_MIF_FRAME);
20031f317bf9SMarius Strobl 		if (v & GEM_MIF_FRAME_TA0)
200442c1b001SThomas Moestl 			return (1);
200542c1b001SThomas Moestl 	}
200642c1b001SThomas Moestl 
20072a79fd39SMarius Strobl 	device_printf(sc->sc_dev, "%s: timed out\n", __func__);
200842c1b001SThomas Moestl 	return (0);
200942c1b001SThomas Moestl }
201042c1b001SThomas Moestl 
201142c1b001SThomas Moestl void
20122a79fd39SMarius Strobl gem_mii_statchg(device_t dev)
201342c1b001SThomas Moestl {
20142a79fd39SMarius Strobl 	struct gem_softc *sc;
20151ed3fed7SMarius Strobl 	int gigabit;
20161ed3fed7SMarius Strobl 	uint32_t rxcfg, txcfg, v;
201742c1b001SThomas Moestl 
20182a79fd39SMarius Strobl 	sc = device_get_softc(dev);
20192a79fd39SMarius Strobl 
20209ba2b298SMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
20219ba2b298SMarius Strobl 
202242c1b001SThomas Moestl #ifdef GEM_DEBUG
20232a79fd39SMarius Strobl 	if ((sc->sc_ifp->if_flags & IFF_DEBUG) != 0)
20248e5d93dbSMarius Strobl 		device_printf(sc->sc_dev, "%s: status change\n", __func__);
202542c1b001SThomas Moestl #endif
202642c1b001SThomas Moestl 
20271ed3fed7SMarius Strobl 	if ((sc->sc_mii->mii_media_status & IFM_ACTIVE) != 0 &&
20281ed3fed7SMarius Strobl 	    IFM_SUBTYPE(sc->sc_mii->mii_media_active) != IFM_NONE)
20291ed3fed7SMarius Strobl 		sc->sc_flags |= GEM_LINK;
20301ed3fed7SMarius Strobl 	else
20311ed3fed7SMarius Strobl 		sc->sc_flags &= ~GEM_LINK;
20321ed3fed7SMarius Strobl 
20331ed3fed7SMarius Strobl 	switch (IFM_SUBTYPE(sc->sc_mii->mii_media_active)) {
20341ed3fed7SMarius Strobl 	case IFM_1000_SX:
20351ed3fed7SMarius Strobl 	case IFM_1000_LX:
20361ed3fed7SMarius Strobl 	case IFM_1000_CX:
20371ed3fed7SMarius Strobl 	case IFM_1000_T:
20381ed3fed7SMarius Strobl 		gigabit = 1;
20391ed3fed7SMarius Strobl 		break;
20401ed3fed7SMarius Strobl 	default:
20411ed3fed7SMarius Strobl 		gigabit = 0;
204242c1b001SThomas Moestl 	}
20431ed3fed7SMarius Strobl 
20441ed3fed7SMarius Strobl 	/*
20451ed3fed7SMarius Strobl 	 * The configuration done here corresponds to the steps F) and
20461ed3fed7SMarius Strobl 	 * G) and as far as enabling of RX and TX MAC goes also step H)
20471ed3fed7SMarius Strobl 	 * of the initialization sequence outlined in section 3.2.1 of
20481ed3fed7SMarius Strobl 	 * the GEM Gigabit Ethernet ASIC Specification.
20491ed3fed7SMarius Strobl 	 */
20501ed3fed7SMarius Strobl 
2051c0e3e9d4SMarius Strobl 	rxcfg = sc->sc_mac_rxcfg;
2052c0e3e9d4SMarius Strobl 	rxcfg &= ~GEM_MAC_RX_CARR_EXTEND;
20531ed3fed7SMarius Strobl 	txcfg = GEM_MAC_TX_ENA_IPG0 | GEM_MAC_TX_NGU | GEM_MAC_TX_NGU_LIMIT;
20541ed3fed7SMarius Strobl 	if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0)
20551ed3fed7SMarius Strobl 		txcfg |= GEM_MAC_TX_IGN_CARRIER | GEM_MAC_TX_IGN_COLLIS;
20561ed3fed7SMarius Strobl 	else if (gigabit != 0) {
20571ed3fed7SMarius Strobl 		rxcfg |= GEM_MAC_RX_CARR_EXTEND;
20581ed3fed7SMarius Strobl 		txcfg |= GEM_MAC_TX_CARR_EXTEND;
20591ed3fed7SMarius Strobl 	}
2060c0e3e9d4SMarius Strobl 	(void)gem_disable_tx(sc);
2061bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, txcfg);
2062c0e3e9d4SMarius Strobl 	(void)gem_disable_rx(sc);
2063bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, rxcfg);
20641ed3fed7SMarius Strobl 
2065bd3d9826SMarius Strobl 	v = GEM_BANK1_READ_4(sc, GEM_MAC_CONTROL_CONFIG) &
20661ed3fed7SMarius Strobl 	    ~(GEM_MAC_CC_RX_PAUSE | GEM_MAC_CC_TX_PAUSE);
20672a79fd39SMarius Strobl 	if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) &
20682a79fd39SMarius Strobl 	    IFM_ETH_RXPAUSE) != 0)
20691ed3fed7SMarius Strobl 		v |= GEM_MAC_CC_RX_PAUSE;
20702a79fd39SMarius Strobl 	if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) &
20712a79fd39SMarius Strobl 	    IFM_ETH_TXPAUSE) != 0)
20721ed3fed7SMarius Strobl 		v |= GEM_MAC_CC_TX_PAUSE;
2073bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_CONFIG, v);
20741ed3fed7SMarius Strobl 
20751ed3fed7SMarius Strobl 	if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) == 0 &&
20761ed3fed7SMarius Strobl 	    gigabit != 0)
2077bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_SLOT_TIME,
20781ed3fed7SMarius Strobl 		    GEM_MAC_SLOT_TIME_CARR_EXTEND);
20791ed3fed7SMarius Strobl 	else
2080bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_SLOT_TIME,
20811ed3fed7SMarius Strobl 		    GEM_MAC_SLOT_TIME_NORMAL);
208242c1b001SThomas Moestl 
208342c1b001SThomas Moestl 	/* XIF Configuration */
208442c1b001SThomas Moestl 	v = GEM_MAC_XIF_LINK_LED;
208542c1b001SThomas Moestl 	v |= GEM_MAC_XIF_TX_MII_ENA;
20861ed3fed7SMarius Strobl 	if ((sc->sc_flags & GEM_SERDES) == 0) {
2087bd3d9826SMarius Strobl 		if ((GEM_BANK1_READ_4(sc, GEM_MIF_CONFIG) &
208878d22f42SMarius Strobl 		    GEM_MIF_CONFIG_PHY_SEL) != 0) {
208942c1b001SThomas Moestl 			/* External MII needs echo disable if half duplex. */
209078d22f42SMarius Strobl 			if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) &
209178d22f42SMarius Strobl 			    IFM_FDX) == 0)
209242c1b001SThomas Moestl 				v |= GEM_MAC_XIF_ECHO_DISABL;
209378d22f42SMarius Strobl 		} else
20941ed3fed7SMarius Strobl 			/*
20951ed3fed7SMarius Strobl 			 * Internal MII needs buffer enable.
20961ed3fed7SMarius Strobl 			 * XXX buffer enable makes only sense for an
20971ed3fed7SMarius Strobl 			 * external PHY.
20981ed3fed7SMarius Strobl 			 */
209942c1b001SThomas Moestl 			v |= GEM_MAC_XIF_MII_BUF_ENA;
210042c1b001SThomas Moestl 	}
21011ed3fed7SMarius Strobl 	if (gigabit != 0)
21021ed3fed7SMarius Strobl 		v |= GEM_MAC_XIF_GMII_MODE;
21031ed3fed7SMarius Strobl 	if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0)
21041ed3fed7SMarius Strobl 		v |= GEM_MAC_XIF_FDPLX_LED;
2105bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_XIF_CONFIG, v);
21061ed3fed7SMarius Strobl 
2107c0e3e9d4SMarius Strobl 	sc->sc_mac_rxcfg = rxcfg;
21081ed3fed7SMarius Strobl 	if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
21091ed3fed7SMarius Strobl 	    (sc->sc_flags & GEM_LINK) != 0) {
2110bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG,
21111ed3fed7SMarius Strobl 		    txcfg | GEM_MAC_TX_ENABLE);
2112bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG,
21131ed3fed7SMarius Strobl 		    rxcfg | GEM_MAC_RX_ENABLE);
21141ed3fed7SMarius Strobl 	}
211542c1b001SThomas Moestl }
211642c1b001SThomas Moestl 
211742c1b001SThomas Moestl int
21182a79fd39SMarius Strobl gem_mediachange(struct ifnet *ifp)
211942c1b001SThomas Moestl {
212042c1b001SThomas Moestl 	struct gem_softc *sc = ifp->if_softc;
21211f317bf9SMarius Strobl 	int error;
212242c1b001SThomas Moestl 
21232a79fd39SMarius Strobl 	/* XXX add support for serial media. */
212442c1b001SThomas Moestl 
21251f317bf9SMarius Strobl 	GEM_LOCK(sc);
21261f317bf9SMarius Strobl 	error = mii_mediachg(sc->sc_mii);
21271f317bf9SMarius Strobl 	GEM_UNLOCK(sc);
21281f317bf9SMarius Strobl 	return (error);
212942c1b001SThomas Moestl }
213042c1b001SThomas Moestl 
213142c1b001SThomas Moestl void
21322a79fd39SMarius Strobl gem_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
213342c1b001SThomas Moestl {
213442c1b001SThomas Moestl 	struct gem_softc *sc = ifp->if_softc;
213542c1b001SThomas Moestl 
21368cfaff7dSMarius Strobl 	GEM_LOCK(sc);
21378cfaff7dSMarius Strobl 	if ((ifp->if_flags & IFF_UP) == 0) {
21388cfaff7dSMarius Strobl 		GEM_UNLOCK(sc);
213942c1b001SThomas Moestl 		return;
21408cfaff7dSMarius Strobl 	}
214142c1b001SThomas Moestl 
214242c1b001SThomas Moestl 	mii_pollstat(sc->sc_mii);
214342c1b001SThomas Moestl 	ifmr->ifm_active = sc->sc_mii->mii_media_active;
214442c1b001SThomas Moestl 	ifmr->ifm_status = sc->sc_mii->mii_media_status;
21458cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
214642c1b001SThomas Moestl }
214742c1b001SThomas Moestl 
214842c1b001SThomas Moestl static int
21492a79fd39SMarius Strobl gem_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
215042c1b001SThomas Moestl {
215142c1b001SThomas Moestl 	struct gem_softc *sc = ifp->if_softc;
215242c1b001SThomas Moestl 	struct ifreq *ifr = (struct ifreq *)data;
21532a79fd39SMarius Strobl 	int error;
21548cfaff7dSMarius Strobl 
21552a79fd39SMarius Strobl 	error = 0;
215642c1b001SThomas Moestl 	switch (cmd) {
215742c1b001SThomas Moestl 	case SIOCSIFFLAGS:
21581f317bf9SMarius Strobl 		GEM_LOCK(sc);
21592a79fd39SMarius Strobl 		if ((ifp->if_flags & IFF_UP) != 0) {
21601ed3fed7SMarius Strobl 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
21611ed3fed7SMarius Strobl 			    ((ifp->if_flags ^ sc->sc_ifflags) &
21621ed3fed7SMarius Strobl 			    (IFF_ALLMULTI | IFF_PROMISC)) != 0)
21635ed0b954SMarius Strobl 				gem_setladrf(sc);
216442c1b001SThomas Moestl 			else
21658cfaff7dSMarius Strobl 				gem_init_locked(sc);
21662a79fd39SMarius Strobl 		} else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
216742c1b001SThomas Moestl 			gem_stop(ifp, 0);
216812fb0330SPyun YongHyeon 		if ((ifp->if_flags & IFF_LINK0) != 0)
216912fb0330SPyun YongHyeon 			sc->sc_csum_features |= CSUM_UDP;
217012fb0330SPyun YongHyeon 		else
217112fb0330SPyun YongHyeon 			sc->sc_csum_features &= ~CSUM_UDP;
217212fb0330SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
217312fb0330SPyun YongHyeon 			ifp->if_hwassist = sc->sc_csum_features;
2174336cca9eSBenno Rice 		sc->sc_ifflags = ifp->if_flags;
21751f317bf9SMarius Strobl 		GEM_UNLOCK(sc);
217642c1b001SThomas Moestl 		break;
217742c1b001SThomas Moestl 	case SIOCADDMULTI:
217842c1b001SThomas Moestl 	case SIOCDELMULTI:
21791f317bf9SMarius Strobl 		GEM_LOCK(sc);
2180c0e3e9d4SMarius Strobl 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
21815ed0b954SMarius Strobl 			gem_setladrf(sc);
21821f317bf9SMarius Strobl 		GEM_UNLOCK(sc);
218342c1b001SThomas Moestl 		break;
218442c1b001SThomas Moestl 	case SIOCGIFMEDIA:
218542c1b001SThomas Moestl 	case SIOCSIFMEDIA:
218642c1b001SThomas Moestl 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, cmd);
218742c1b001SThomas Moestl 		break;
218812fb0330SPyun YongHyeon 	case SIOCSIFCAP:
218912fb0330SPyun YongHyeon 		GEM_LOCK(sc);
219012fb0330SPyun YongHyeon 		ifp->if_capenable = ifr->ifr_reqcap;
219112fb0330SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
219212fb0330SPyun YongHyeon 			ifp->if_hwassist = sc->sc_csum_features;
219312fb0330SPyun YongHyeon 		else
219412fb0330SPyun YongHyeon 			ifp->if_hwassist = 0;
219512fb0330SPyun YongHyeon 		GEM_UNLOCK(sc);
219612fb0330SPyun YongHyeon 		break;
219742c1b001SThomas Moestl 	default:
21981f317bf9SMarius Strobl 		error = ether_ioctl(ifp, cmd, data);
219942c1b001SThomas Moestl 		break;
220042c1b001SThomas Moestl 	}
220142c1b001SThomas Moestl 
220242c1b001SThomas Moestl 	return (error);
220342c1b001SThomas Moestl }
220442c1b001SThomas Moestl 
220599e76377SGleb Smirnoff static u_int
220699e76377SGleb Smirnoff gem_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
220799e76377SGleb Smirnoff {
220899e76377SGleb Smirnoff 	uint32_t crc, *hash = arg;
220999e76377SGleb Smirnoff 
221099e76377SGleb Smirnoff 	crc = ether_crc32_le(LLADDR(sdl), ETHER_ADDR_LEN);
221199e76377SGleb Smirnoff 	/* We just want the 8 most significant bits. */
221299e76377SGleb Smirnoff 	crc >>= 24;
221399e76377SGleb Smirnoff 	/* Set the corresponding bit in the filter. */
221499e76377SGleb Smirnoff 	hash[crc >> 4] |= 1 << (15 - (crc & 15));
221599e76377SGleb Smirnoff 
221699e76377SGleb Smirnoff 	return (1);
221799e76377SGleb Smirnoff }
221899e76377SGleb Smirnoff 
221942c1b001SThomas Moestl static void
22205ed0b954SMarius Strobl gem_setladrf(struct gem_softc *sc)
222142c1b001SThomas Moestl {
2222fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
2223336cca9eSBenno Rice 	int i;
22242a79fd39SMarius Strobl 	uint32_t hash[16];
222599e76377SGleb Smirnoff 	uint32_t v;
222642c1b001SThomas Moestl 
22278cfaff7dSMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
22288cfaff7dSMarius Strobl 
2229336cca9eSBenno Rice 	/*
2230c0e3e9d4SMarius Strobl 	 * Turn off the RX MAC and the hash filter as required by the Sun GEM
2231c0e3e9d4SMarius Strobl 	 * programming restrictions.
2232336cca9eSBenno Rice 	 */
22332b2f3c09SMarius Strobl 	v = sc->sc_mac_rxcfg & ~GEM_MAC_RX_HASH_FILTER;
2234bd3d9826SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, v);
2235ccb1212aSMarius Strobl 	GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4,
2236ccb1212aSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
2237c0e3e9d4SMarius Strobl 	if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_HASH_FILTER |
2238c0e3e9d4SMarius Strobl 	    GEM_MAC_RX_ENABLE, 0))
2239c0e3e9d4SMarius Strobl 		device_printf(sc->sc_dev,
2240c0e3e9d4SMarius Strobl 		    "cannot disable RX MAC or hash filter\n");
22411ed3fed7SMarius Strobl 
2242c0e3e9d4SMarius Strobl 	v &= ~(GEM_MAC_RX_PROMISCUOUS | GEM_MAC_RX_PROMISC_GRP);
224342c1b001SThomas Moestl 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
224442c1b001SThomas Moestl 		v |= GEM_MAC_RX_PROMISCUOUS;
224542c1b001SThomas Moestl 		goto chipit;
224642c1b001SThomas Moestl 	}
224742c1b001SThomas Moestl 	if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
2248336cca9eSBenno Rice 		v |= GEM_MAC_RX_PROMISC_GRP;
224942c1b001SThomas Moestl 		goto chipit;
225042c1b001SThomas Moestl 	}
225142c1b001SThomas Moestl 
225242c1b001SThomas Moestl 	/*
22532a79fd39SMarius Strobl 	 * Set up multicast address filter by passing all multicast
22542a79fd39SMarius Strobl 	 * addresses through a crc generator, and then using the high
22552a79fd39SMarius Strobl 	 * order 8 bits as an index into the 256 bit logical address
22562a79fd39SMarius Strobl 	 * filter.  The high order 4 bits selects the word, while the
22572a79fd39SMarius Strobl 	 * other 4 bits select the bit within the word (where bit 0
22582a79fd39SMarius Strobl 	 * is the MSB).
225942c1b001SThomas Moestl 	 */
226042c1b001SThomas Moestl 
2261336cca9eSBenno Rice 	memset(hash, 0, sizeof(hash));
226299e76377SGleb Smirnoff 	if_foreach_llmaddr(ifp, gem_hash_maddr, hash);
2263336cca9eSBenno Rice 
2264336cca9eSBenno Rice 	v |= GEM_MAC_RX_HASH_FILTER;
2265336cca9eSBenno Rice 
22662a79fd39SMarius Strobl 	/* Now load the hash table into the chip (if we are using it). */
22672a79fd39SMarius Strobl 	for (i = 0; i < 16; i++)
2268bd3d9826SMarius Strobl 		GEM_BANK1_WRITE_4(sc,
2269336cca9eSBenno Rice 		    GEM_MAC_HASH0 + i * (GEM_MAC_HASH1 - GEM_MAC_HASH0),
2270336cca9eSBenno Rice 		    hash[i]);
227142c1b001SThomas Moestl 
227242c1b001SThomas Moestl  chipit:
2273c0e3e9d4SMarius Strobl 	sc->sc_mac_rxcfg = v;
22745ed0b954SMarius Strobl 	GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, v | GEM_MAC_RX_ENABLE);
227542c1b001SThomas Moestl }
2276