xref: /freebsd/sys/dev/gem/if_gem.c (revision 00d12766ef59174c52e57686a63555490600b678)
1aad970f1SDavid E. O'Brien /*-
242c1b001SThomas Moestl  * Copyright (C) 2001 Eduardo Horvath.
3305f2c06SThomas Moestl  * Copyright (c) 2001-2003 Thomas Moestl
442c1b001SThomas Moestl  * All rights reserved.
542c1b001SThomas Moestl  *
642c1b001SThomas Moestl  * Redistribution and use in source and binary forms, with or without
742c1b001SThomas Moestl  * modification, are permitted provided that the following conditions
842c1b001SThomas Moestl  * are met:
942c1b001SThomas Moestl  * 1. Redistributions of source code must retain the above copyright
1042c1b001SThomas Moestl  *    notice, this list of conditions and the following disclaimer.
1142c1b001SThomas Moestl  * 2. Redistributions in binary form must reproduce the above copyright
1242c1b001SThomas Moestl  *    notice, this list of conditions and the following disclaimer in the
1342c1b001SThomas Moestl  *    documentation and/or other materials provided with the distribution.
1442c1b001SThomas Moestl  *
1542c1b001SThomas Moestl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
1642c1b001SThomas Moestl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1742c1b001SThomas Moestl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1842c1b001SThomas Moestl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
1942c1b001SThomas Moestl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2042c1b001SThomas Moestl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2142c1b001SThomas Moestl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2242c1b001SThomas Moestl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2342c1b001SThomas Moestl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2442c1b001SThomas Moestl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2542c1b001SThomas Moestl  * SUCH DAMAGE.
2642c1b001SThomas Moestl  *
27336cca9eSBenno Rice  *	from: NetBSD: gem.c,v 1.21 2002/06/01 23:50:58 lukem Exp
2842c1b001SThomas Moestl  */
2942c1b001SThomas Moestl 
30aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
31aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
32aad970f1SDavid E. O'Brien 
3342c1b001SThomas Moestl /*
3442c1b001SThomas Moestl  * Driver for Sun GEM ethernet controllers.
3542c1b001SThomas Moestl  */
3642c1b001SThomas Moestl 
3718100346SThomas Moestl #if 0
3842c1b001SThomas Moestl #define	GEM_DEBUG
3918100346SThomas Moestl #endif
4042c1b001SThomas Moestl 
41c3d5598aSMarius Strobl #if 0	/* XXX: In case of emergency, re-enable this. */
42c3d5598aSMarius Strobl #define	GEM_RINT_TIMEOUT
43c3d5598aSMarius Strobl #endif
44c3d5598aSMarius Strobl 
4542c1b001SThomas Moestl #include <sys/param.h>
4642c1b001SThomas Moestl #include <sys/systm.h>
4742c1b001SThomas Moestl #include <sys/bus.h>
4842c1b001SThomas Moestl #include <sys/callout.h>
49a30d4b32SMike Barcroft #include <sys/endian.h>
5042c1b001SThomas Moestl #include <sys/mbuf.h>
5142c1b001SThomas Moestl #include <sys/malloc.h>
5242c1b001SThomas Moestl #include <sys/kernel.h>
538cfaff7dSMarius Strobl #include <sys/lock.h>
54186f2b9eSPoul-Henning Kamp #include <sys/module.h>
558cfaff7dSMarius Strobl #include <sys/mutex.h>
5642c1b001SThomas Moestl #include <sys/socket.h>
5742c1b001SThomas Moestl #include <sys/sockio.h>
5842c1b001SThomas Moestl 
5908e0fdebSThomas Moestl #include <net/bpf.h>
6042c1b001SThomas Moestl #include <net/ethernet.h>
6142c1b001SThomas Moestl #include <net/if.h>
6242c1b001SThomas Moestl #include <net/if_arp.h>
6342c1b001SThomas Moestl #include <net/if_dl.h>
6442c1b001SThomas Moestl #include <net/if_media.h>
65fc74a9f9SBrooks Davis #include <net/if_types.h>
6600d12766SMarius Strobl #include <net/if_vlan_var.h>
6742c1b001SThomas Moestl 
6842c1b001SThomas Moestl #include <machine/bus.h>
6942c1b001SThomas Moestl 
7042c1b001SThomas Moestl #include <dev/mii/mii.h>
7142c1b001SThomas Moestl #include <dev/mii/miivar.h>
7242c1b001SThomas Moestl 
73681f7d03SWarner Losh #include <dev/gem/if_gemreg.h>
74681f7d03SWarner Losh #include <dev/gem/if_gemvar.h>
7542c1b001SThomas Moestl 
7642c1b001SThomas Moestl #define TRIES	10000
7742c1b001SThomas Moestl 
78e51a25f8SAlfred Perlstein static void	gem_start(struct ifnet *);
798cfaff7dSMarius Strobl static void	gem_start_locked(struct ifnet *);
80e51a25f8SAlfred Perlstein static void	gem_stop(struct ifnet *, int);
81e51a25f8SAlfred Perlstein static int	gem_ioctl(struct ifnet *, u_long, caddr_t);
82e51a25f8SAlfred Perlstein static void	gem_cddma_callback(void *, bus_dma_segment_t *, int, int);
83305f2c06SThomas Moestl static void	gem_txdma_callback(void *, bus_dma_segment_t *, int,
84305f2c06SThomas Moestl     bus_size_t, int);
85e51a25f8SAlfred Perlstein static void	gem_tick(void *);
86e51a25f8SAlfred Perlstein static void	gem_watchdog(struct ifnet *);
87e51a25f8SAlfred Perlstein static void	gem_init(void *);
888cfaff7dSMarius Strobl static void	gem_init_locked(struct gem_softc *sc);
89e51a25f8SAlfred Perlstein static void	gem_init_regs(struct gem_softc *sc);
90e51a25f8SAlfred Perlstein static int	gem_ringsize(int sz);
91e51a25f8SAlfred Perlstein static int	gem_meminit(struct gem_softc *);
92305f2c06SThomas Moestl static int	gem_load_txmbuf(struct gem_softc *, struct mbuf *);
93e51a25f8SAlfred Perlstein static void	gem_mifinit(struct gem_softc *);
94e51a25f8SAlfred Perlstein static int	gem_bitwait(struct gem_softc *sc, bus_addr_t r,
95e51a25f8SAlfred Perlstein     u_int32_t clr, u_int32_t set);
96e51a25f8SAlfred Perlstein static int	gem_reset_rx(struct gem_softc *);
97e51a25f8SAlfred Perlstein static int	gem_reset_tx(struct gem_softc *);
98e51a25f8SAlfred Perlstein static int	gem_disable_rx(struct gem_softc *);
99e51a25f8SAlfred Perlstein static int	gem_disable_tx(struct gem_softc *);
100e51a25f8SAlfred Perlstein static void	gem_rxdrain(struct gem_softc *);
101e51a25f8SAlfred Perlstein static int	gem_add_rxbuf(struct gem_softc *, int);
102e51a25f8SAlfred Perlstein static void	gem_setladrf(struct gem_softc *);
10342c1b001SThomas Moestl 
104e51a25f8SAlfred Perlstein struct mbuf	*gem_get(struct gem_softc *, int, int);
105e51a25f8SAlfred Perlstein static void	gem_eint(struct gem_softc *, u_int);
106e51a25f8SAlfred Perlstein static void	gem_rint(struct gem_softc *);
107c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT
1080d80b9bdSThomas Moestl static void	gem_rint_timeout(void *);
10911e3f060SJake Burkholder #endif
110e51a25f8SAlfred Perlstein static void	gem_tint(struct gem_softc *);
11142c1b001SThomas Moestl #ifdef notyet
112e51a25f8SAlfred Perlstein static void	gem_power(int, void *);
11342c1b001SThomas Moestl #endif
11442c1b001SThomas Moestl 
11542c1b001SThomas Moestl devclass_t gem_devclass;
11642c1b001SThomas Moestl DRIVER_MODULE(miibus, gem, miibus_driver, miibus_devclass, 0, 0);
11742c1b001SThomas Moestl MODULE_DEPEND(gem, miibus, 1, 1, 1);
11842c1b001SThomas Moestl 
11942c1b001SThomas Moestl #ifdef GEM_DEBUG
12042c1b001SThomas Moestl #include <sys/ktr.h>
12142c1b001SThomas Moestl #define	KTR_GEM		KTR_CT2
12242c1b001SThomas Moestl #endif
12342c1b001SThomas Moestl 
12418100346SThomas Moestl #define	GEM_NSEGS GEM_NTXDESC
12542c1b001SThomas Moestl 
12642c1b001SThomas Moestl /*
12742c1b001SThomas Moestl  * gem_attach:
12842c1b001SThomas Moestl  *
12942c1b001SThomas Moestl  *	Attach a Gem interface to the system.
13042c1b001SThomas Moestl  */
13142c1b001SThomas Moestl int
13242c1b001SThomas Moestl gem_attach(sc)
13342c1b001SThomas Moestl 	struct gem_softc *sc;
13442c1b001SThomas Moestl {
135fc74a9f9SBrooks Davis 	struct ifnet *ifp;
13642c1b001SThomas Moestl 	struct mii_softc *child;
13742c1b001SThomas Moestl 	int i, error;
138336cca9eSBenno Rice 	u_int32_t v;
13942c1b001SThomas Moestl 
1408cfaff7dSMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_NOTOWNED);
1418cfaff7dSMarius Strobl 
142fc74a9f9SBrooks Davis 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
143fc74a9f9SBrooks Davis 	if (ifp == NULL)
144fc74a9f9SBrooks Davis 		return (ENOSPC);
145fc74a9f9SBrooks Davis 
14642c1b001SThomas Moestl 	/* Make sure the chip is stopped. */
14742c1b001SThomas Moestl 	ifp->if_softc = sc;
1488cfaff7dSMarius Strobl 	GEM_LOCK(sc);
14942c1b001SThomas Moestl 	gem_reset(sc);
1508cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
15142c1b001SThomas Moestl 
15242c1b001SThomas Moestl 	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
15342c1b001SThomas Moestl 	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, GEM_NSEGS,
154f6b1c44dSScott Long 	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, &sc->sc_pdmatag);
15542c1b001SThomas Moestl 	if (error)
156fc74a9f9SBrooks Davis 		goto fail_ifnet;
15742c1b001SThomas Moestl 
15842c1b001SThomas Moestl 	error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0,
15942c1b001SThomas Moestl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MAXBSIZE,
160f6b1c44dSScott Long 	    1, BUS_SPACE_MAXSIZE_32BIT, BUS_DMA_ALLOCNOW, NULL, NULL,
161305f2c06SThomas Moestl 	    &sc->sc_rdmatag);
16242c1b001SThomas Moestl 	if (error)
163305f2c06SThomas Moestl 		goto fail_ptag;
164305f2c06SThomas Moestl 
165305f2c06SThomas Moestl 	error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0,
166305f2c06SThomas Moestl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
16718100346SThomas Moestl 	    GEM_TD_BUFSIZE, GEM_NTXDESC, BUS_SPACE_MAXSIZE_32BIT,
168f6b1c44dSScott Long 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->sc_tdmatag);
169305f2c06SThomas Moestl 	if (error)
170305f2c06SThomas Moestl 		goto fail_rtag;
17142c1b001SThomas Moestl 
17242c1b001SThomas Moestl 	error = bus_dma_tag_create(sc->sc_pdmatag, PAGE_SIZE, 0,
17342c1b001SThomas Moestl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
17442c1b001SThomas Moestl 	    sizeof(struct gem_control_data), 1,
17542c1b001SThomas Moestl 	    sizeof(struct gem_control_data), BUS_DMA_ALLOCNOW,
176f6b1c44dSScott Long 	    busdma_lock_mutex, &Giant, &sc->sc_cdmatag);
17742c1b001SThomas Moestl 	if (error)
178305f2c06SThomas Moestl 		goto fail_ttag;
17942c1b001SThomas Moestl 
18042c1b001SThomas Moestl 	/*
18142c1b001SThomas Moestl 	 * Allocate the control data structures, and create and load the
18242c1b001SThomas Moestl 	 * DMA map for it.
18342c1b001SThomas Moestl 	 */
18442c1b001SThomas Moestl 	if ((error = bus_dmamem_alloc(sc->sc_cdmatag,
18542c1b001SThomas Moestl 	    (void **)&sc->sc_control_data, 0, &sc->sc_cddmamap))) {
18642c1b001SThomas Moestl 		device_printf(sc->sc_dev, "unable to allocate control data,"
18742c1b001SThomas Moestl 		    " error = %d\n", error);
188305f2c06SThomas Moestl 		goto fail_ctag;
18942c1b001SThomas Moestl 	}
19042c1b001SThomas Moestl 
19142c1b001SThomas Moestl 	sc->sc_cddma = 0;
19242c1b001SThomas Moestl 	if ((error = bus_dmamap_load(sc->sc_cdmatag, sc->sc_cddmamap,
19342c1b001SThomas Moestl 	    sc->sc_control_data, sizeof(struct gem_control_data),
19442c1b001SThomas Moestl 	    gem_cddma_callback, sc, 0)) != 0 || sc->sc_cddma == 0) {
19542c1b001SThomas Moestl 		device_printf(sc->sc_dev, "unable to load control data DMA "
19642c1b001SThomas Moestl 		    "map, error = %d\n", error);
197305f2c06SThomas Moestl 		goto fail_cmem;
19842c1b001SThomas Moestl 	}
19942c1b001SThomas Moestl 
20042c1b001SThomas Moestl 	/*
20142c1b001SThomas Moestl 	 * Initialize the transmit job descriptors.
20242c1b001SThomas Moestl 	 */
20342c1b001SThomas Moestl 	STAILQ_INIT(&sc->sc_txfreeq);
20442c1b001SThomas Moestl 	STAILQ_INIT(&sc->sc_txdirtyq);
20542c1b001SThomas Moestl 
20642c1b001SThomas Moestl 	/*
20742c1b001SThomas Moestl 	 * Create the transmit buffer DMA maps.
20842c1b001SThomas Moestl 	 */
20942c1b001SThomas Moestl 	error = ENOMEM;
21042c1b001SThomas Moestl 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
21142c1b001SThomas Moestl 		struct gem_txsoft *txs;
21242c1b001SThomas Moestl 
21342c1b001SThomas Moestl 		txs = &sc->sc_txsoft[i];
21442c1b001SThomas Moestl 		txs->txs_mbuf = NULL;
21542c1b001SThomas Moestl 		txs->txs_ndescs = 0;
216305f2c06SThomas Moestl 		if ((error = bus_dmamap_create(sc->sc_tdmatag, 0,
21742c1b001SThomas Moestl 		    &txs->txs_dmamap)) != 0) {
21842c1b001SThomas Moestl 			device_printf(sc->sc_dev, "unable to create tx DMA map "
21942c1b001SThomas Moestl 			    "%d, error = %d\n", i, error);
220305f2c06SThomas Moestl 			goto fail_txd;
22142c1b001SThomas Moestl 		}
22242c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
22342c1b001SThomas Moestl 	}
22442c1b001SThomas Moestl 
22542c1b001SThomas Moestl 	/*
22642c1b001SThomas Moestl 	 * Create the receive buffer DMA maps.
22742c1b001SThomas Moestl 	 */
22842c1b001SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
229305f2c06SThomas Moestl 		if ((error = bus_dmamap_create(sc->sc_rdmatag, 0,
23042c1b001SThomas Moestl 		    &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
23142c1b001SThomas Moestl 			device_printf(sc->sc_dev, "unable to create rx DMA map "
23242c1b001SThomas Moestl 			    "%d, error = %d\n", i, error);
233305f2c06SThomas Moestl 			goto fail_rxd;
23442c1b001SThomas Moestl 		}
23542c1b001SThomas Moestl 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
23642c1b001SThomas Moestl 	}
23742c1b001SThomas Moestl 
2388cfaff7dSMarius Strobl 	GEM_LOCK(sc);
23942c1b001SThomas Moestl 	gem_mifinit(sc);
2408cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
24142c1b001SThomas Moestl 
24242c1b001SThomas Moestl 	if ((error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, gem_mediachange,
24342c1b001SThomas Moestl 	    gem_mediastatus)) != 0) {
24442c1b001SThomas Moestl 		device_printf(sc->sc_dev, "phy probe failed: %d\n", error);
245305f2c06SThomas Moestl 		goto fail_rxd;
24642c1b001SThomas Moestl 	}
24742c1b001SThomas Moestl 	sc->sc_mii = device_get_softc(sc->sc_miibus);
24842c1b001SThomas Moestl 
24942c1b001SThomas Moestl 	/*
25042c1b001SThomas Moestl 	 * From this point forward, the attachment cannot fail.  A failure
25142c1b001SThomas Moestl 	 * before this point releases all resources that may have been
25242c1b001SThomas Moestl 	 * allocated.
25342c1b001SThomas Moestl 	 */
25442c1b001SThomas Moestl 
255336cca9eSBenno Rice 	/* Get RX FIFO size */
256336cca9eSBenno Rice 	sc->sc_rxfifosize = 64 *
257336cca9eSBenno Rice 	    bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_FIFO_SIZE);
258336cca9eSBenno Rice 
259336cca9eSBenno Rice 	/* Get TX FIFO size */
260336cca9eSBenno Rice 	v = bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_FIFO_SIZE);
2613a5aee5aSThomas Moestl 	device_printf(sc->sc_dev, "%ukB RX FIFO, %ukB TX FIFO\n",
2623a5aee5aSThomas Moestl 	    sc->sc_rxfifosize / 1024, v / 16);
26342c1b001SThomas Moestl 
26442c1b001SThomas Moestl 	/* Initialize ifnet structure. */
26542c1b001SThomas Moestl 	ifp->if_softc = sc;
2669bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(sc->sc_dev),
2679bf40edeSBrooks Davis 	    device_get_unit(sc->sc_dev));
26842c1b001SThomas Moestl 	ifp->if_mtu = ETHERMTU;
2698cfaff7dSMarius Strobl 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
27042c1b001SThomas Moestl 	ifp->if_start = gem_start;
27142c1b001SThomas Moestl 	ifp->if_ioctl = gem_ioctl;
27242c1b001SThomas Moestl 	ifp->if_watchdog = gem_watchdog;
27342c1b001SThomas Moestl 	ifp->if_init = gem_init;
27442c1b001SThomas Moestl 	ifp->if_snd.ifq_maxlen = GEM_TXQUEUELEN;
27542c1b001SThomas Moestl 	/*
27642c1b001SThomas Moestl 	 * Walk along the list of attached MII devices and
27742c1b001SThomas Moestl 	 * establish an `MII instance' to `phy number'
27842c1b001SThomas Moestl 	 * mapping. We'll use this mapping in media change
27942c1b001SThomas Moestl 	 * requests to determine which phy to use to program
28042c1b001SThomas Moestl 	 * the MIF configuration register.
28142c1b001SThomas Moestl 	 */
28242c1b001SThomas Moestl 	for (child = LIST_FIRST(&sc->sc_mii->mii_phys); child != NULL;
28342c1b001SThomas Moestl 	     child = LIST_NEXT(child, mii_list)) {
28442c1b001SThomas Moestl 		/*
28542c1b001SThomas Moestl 		 * Note: we support just two PHYs: the built-in
28642c1b001SThomas Moestl 		 * internal device and an external on the MII
28742c1b001SThomas Moestl 		 * connector.
28842c1b001SThomas Moestl 		 */
28942c1b001SThomas Moestl 		if (child->mii_phy > 1 || child->mii_inst > 1) {
29042c1b001SThomas Moestl 			device_printf(sc->sc_dev, "cannot accomodate "
29142c1b001SThomas Moestl 			    "MII device %s at phy %d, instance %d\n",
29242c1b001SThomas Moestl 			    device_get_name(child->mii_dev),
29342c1b001SThomas Moestl 			    child->mii_phy, child->mii_inst);
29442c1b001SThomas Moestl 			continue;
29542c1b001SThomas Moestl 		}
29642c1b001SThomas Moestl 
29742c1b001SThomas Moestl 		sc->sc_phys[child->mii_inst] = child->mii_phy;
29842c1b001SThomas Moestl 	}
29942c1b001SThomas Moestl 
30042c1b001SThomas Moestl 	/*
30142c1b001SThomas Moestl 	 * Now select and activate the PHY we will use.
30242c1b001SThomas Moestl 	 *
30342c1b001SThomas Moestl 	 * The order of preference is External (MDI1),
30442c1b001SThomas Moestl 	 * Internal (MDI0), Serial Link (no MII).
30542c1b001SThomas Moestl 	 */
30642c1b001SThomas Moestl 	if (sc->sc_phys[1]) {
30742c1b001SThomas Moestl #ifdef GEM_DEBUG
30842c1b001SThomas Moestl 		printf("using external phy\n");
30942c1b001SThomas Moestl #endif
31042c1b001SThomas Moestl 		sc->sc_mif_config |= GEM_MIF_CONFIG_PHY_SEL;
31142c1b001SThomas Moestl 	} else {
31242c1b001SThomas Moestl #ifdef GEM_DEBUG
31342c1b001SThomas Moestl 		printf("using internal phy\n");
31442c1b001SThomas Moestl #endif
31542c1b001SThomas Moestl 		sc->sc_mif_config &= ~GEM_MIF_CONFIG_PHY_SEL;
31642c1b001SThomas Moestl 	}
31742c1b001SThomas Moestl 	bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_MIF_CONFIG,
31842c1b001SThomas Moestl 	    sc->sc_mif_config);
31942c1b001SThomas Moestl 	/* Attach the interface. */
320fc74a9f9SBrooks Davis 	ether_ifattach(ifp, sc->sc_enaddr);
32142c1b001SThomas Moestl 
32242c1b001SThomas Moestl #if notyet
32342c1b001SThomas Moestl 	/*
32442c1b001SThomas Moestl 	 * Add a suspend hook to make sure we come back up after a
32542c1b001SThomas Moestl 	 * resume.
32642c1b001SThomas Moestl 	 */
32742c1b001SThomas Moestl 	sc->sc_powerhook = powerhook_establish(gem_power, sc);
32842c1b001SThomas Moestl 	if (sc->sc_powerhook == NULL)
32942c1b001SThomas Moestl 		device_printf(sc->sc_dev, "WARNING: unable to establish power "
33042c1b001SThomas Moestl 		    "hook\n");
33142c1b001SThomas Moestl #endif
33242c1b001SThomas Moestl 
3338cfaff7dSMarius Strobl 	callout_init(&sc->sc_tick_ch, CALLOUT_MPSAFE);
334c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT
3358cfaff7dSMarius Strobl 	callout_init(&sc->sc_rx_ch, CALLOUT_MPSAFE);
336c3d5598aSMarius Strobl #endif
33700d12766SMarius Strobl 
33800d12766SMarius Strobl 	/*
33900d12766SMarius Strobl 	 * Tell the upper layer(s) we support long frames.
34000d12766SMarius Strobl 	 */
34100d12766SMarius Strobl 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
34200d12766SMarius Strobl 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
34300d12766SMarius Strobl 	ifp->if_capenable |= IFCAP_VLAN_MTU;
34400d12766SMarius Strobl 
34542c1b001SThomas Moestl 	return (0);
34642c1b001SThomas Moestl 
34742c1b001SThomas Moestl 	/*
34842c1b001SThomas Moestl 	 * Free any resources we've allocated during the failed attach
34942c1b001SThomas Moestl 	 * attempt.  Do this in reverse order and fall through.
35042c1b001SThomas Moestl 	 */
351305f2c06SThomas Moestl fail_rxd:
35242c1b001SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
35342c1b001SThomas Moestl 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
354305f2c06SThomas Moestl 			bus_dmamap_destroy(sc->sc_rdmatag,
35542c1b001SThomas Moestl 			    sc->sc_rxsoft[i].rxs_dmamap);
35642c1b001SThomas Moestl 	}
357305f2c06SThomas Moestl fail_txd:
35842c1b001SThomas Moestl 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
35942c1b001SThomas Moestl 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
360305f2c06SThomas Moestl 			bus_dmamap_destroy(sc->sc_tdmatag,
36142c1b001SThomas Moestl 			    sc->sc_txsoft[i].txs_dmamap);
36242c1b001SThomas Moestl 	}
363305f2c06SThomas Moestl 	bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap);
364305f2c06SThomas Moestl fail_cmem:
36542c1b001SThomas Moestl 	bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data,
36642c1b001SThomas Moestl 	    sc->sc_cddmamap);
367305f2c06SThomas Moestl fail_ctag:
36842c1b001SThomas Moestl 	bus_dma_tag_destroy(sc->sc_cdmatag);
369305f2c06SThomas Moestl fail_ttag:
370305f2c06SThomas Moestl 	bus_dma_tag_destroy(sc->sc_tdmatag);
371305f2c06SThomas Moestl fail_rtag:
372305f2c06SThomas Moestl 	bus_dma_tag_destroy(sc->sc_rdmatag);
373305f2c06SThomas Moestl fail_ptag:
37442c1b001SThomas Moestl 	bus_dma_tag_destroy(sc->sc_pdmatag);
375fc74a9f9SBrooks Davis fail_ifnet:
376fc74a9f9SBrooks Davis 	if_free(ifp);
37742c1b001SThomas Moestl 	return (error);
37842c1b001SThomas Moestl }
37942c1b001SThomas Moestl 
380cbbdf236SThomas Moestl void
381cbbdf236SThomas Moestl gem_detach(sc)
382cbbdf236SThomas Moestl 	struct gem_softc *sc;
383cbbdf236SThomas Moestl {
384fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
385cbbdf236SThomas Moestl 	int i;
386cbbdf236SThomas Moestl 
3878cfaff7dSMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_NOTOWNED);
3888cfaff7dSMarius Strobl 
3898cfaff7dSMarius Strobl 	GEM_LOCK(sc);
39025bd46d0SBrooks Davis 	gem_stop(ifp, 1);
3918cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
392cbbdf236SThomas Moestl 	ether_ifdetach(ifp);
393fc74a9f9SBrooks Davis 	if_free(ifp);
394cbbdf236SThomas Moestl 	device_delete_child(sc->sc_dev, sc->sc_miibus);
395cbbdf236SThomas Moestl 
396cbbdf236SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
397cbbdf236SThomas Moestl 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
398cbbdf236SThomas Moestl 			bus_dmamap_destroy(sc->sc_rdmatag,
399cbbdf236SThomas Moestl 			    sc->sc_rxsoft[i].rxs_dmamap);
400cbbdf236SThomas Moestl 	}
401cbbdf236SThomas Moestl 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
402cbbdf236SThomas Moestl 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
403cbbdf236SThomas Moestl 			bus_dmamap_destroy(sc->sc_tdmatag,
404cbbdf236SThomas Moestl 			    sc->sc_txsoft[i].txs_dmamap);
405cbbdf236SThomas Moestl 	}
406b2d59f42SThomas Moestl 	GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD);
407b2d59f42SThomas Moestl 	GEM_CDSYNC(sc, BUS_DMASYNC_POSTWRITE);
408cbbdf236SThomas Moestl 	bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap);
409cbbdf236SThomas Moestl 	bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data,
410cbbdf236SThomas Moestl 	    sc->sc_cddmamap);
411cbbdf236SThomas Moestl 	bus_dma_tag_destroy(sc->sc_cdmatag);
412cbbdf236SThomas Moestl 	bus_dma_tag_destroy(sc->sc_tdmatag);
413cbbdf236SThomas Moestl 	bus_dma_tag_destroy(sc->sc_rdmatag);
414cbbdf236SThomas Moestl 	bus_dma_tag_destroy(sc->sc_pdmatag);
415cbbdf236SThomas Moestl }
416cbbdf236SThomas Moestl 
417cbbdf236SThomas Moestl void
418cbbdf236SThomas Moestl gem_suspend(sc)
419cbbdf236SThomas Moestl 	struct gem_softc *sc;
420cbbdf236SThomas Moestl {
421fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
422cbbdf236SThomas Moestl 
4238cfaff7dSMarius Strobl 	GEM_LOCK(sc);
424cbbdf236SThomas Moestl 	gem_stop(ifp, 0);
4258cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
426cbbdf236SThomas Moestl }
427cbbdf236SThomas Moestl 
428cbbdf236SThomas Moestl void
429cbbdf236SThomas Moestl gem_resume(sc)
430cbbdf236SThomas Moestl 	struct gem_softc *sc;
431cbbdf236SThomas Moestl {
432fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
433cbbdf236SThomas Moestl 
4348cfaff7dSMarius Strobl 	GEM_LOCK(sc);
43500d12766SMarius Strobl 	/*
43600d12766SMarius Strobl 	 * On resume all registers have to be initialized again like
43700d12766SMarius Strobl 	 * after power-on.
43800d12766SMarius Strobl 	 */
43900d12766SMarius Strobl 	sc->sc_inited = 0;
440cbbdf236SThomas Moestl 	if (ifp->if_flags & IFF_UP)
4418cfaff7dSMarius Strobl 		gem_init_locked(sc);
4428cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
443cbbdf236SThomas Moestl }
444cbbdf236SThomas Moestl 
44542c1b001SThomas Moestl static void
44642c1b001SThomas Moestl gem_cddma_callback(xsc, segs, nsegs, error)
44742c1b001SThomas Moestl 	void *xsc;
44842c1b001SThomas Moestl 	bus_dma_segment_t *segs;
44942c1b001SThomas Moestl 	int nsegs;
45042c1b001SThomas Moestl 	int error;
45142c1b001SThomas Moestl {
45242c1b001SThomas Moestl 	struct gem_softc *sc = (struct gem_softc *)xsc;
45342c1b001SThomas Moestl 
45442c1b001SThomas Moestl 	if (error != 0)
45542c1b001SThomas Moestl 		return;
45642c1b001SThomas Moestl 	if (nsegs != 1) {
45742c1b001SThomas Moestl 		/* can't happen... */
45842c1b001SThomas Moestl 		panic("gem_cddma_callback: bad control buffer segment count");
45942c1b001SThomas Moestl 	}
46042c1b001SThomas Moestl 	sc->sc_cddma = segs[0].ds_addr;
46142c1b001SThomas Moestl }
46242c1b001SThomas Moestl 
46342c1b001SThomas Moestl static void
464305f2c06SThomas Moestl gem_txdma_callback(xsc, segs, nsegs, totsz, error)
46542c1b001SThomas Moestl 	void *xsc;
46642c1b001SThomas Moestl 	bus_dma_segment_t *segs;
46742c1b001SThomas Moestl 	int nsegs;
468305f2c06SThomas Moestl 	bus_size_t totsz;
46942c1b001SThomas Moestl 	int error;
47042c1b001SThomas Moestl {
471305f2c06SThomas Moestl 	struct gem_txdma *txd = (struct gem_txdma *)xsc;
472305f2c06SThomas Moestl 	struct gem_softc *sc = txd->txd_sc;
473305f2c06SThomas Moestl 	struct gem_txsoft *txs = txd->txd_txs;
474305f2c06SThomas Moestl 	bus_size_t len = 0;
475305f2c06SThomas Moestl 	uint64_t flags = 0;
476305f2c06SThomas Moestl 	int seg, nexttx;
47742c1b001SThomas Moestl 
47842c1b001SThomas Moestl 	if (error != 0)
47942c1b001SThomas Moestl 		return;
480305f2c06SThomas Moestl 	/*
481305f2c06SThomas Moestl 	 * Ensure we have enough descriptors free to describe
482305f2c06SThomas Moestl 	 * the packet.  Note, we always reserve one descriptor
483305f2c06SThomas Moestl 	 * at the end of the ring as a termination point, to
484305f2c06SThomas Moestl 	 * prevent wrap-around.
485305f2c06SThomas Moestl 	 */
486305f2c06SThomas Moestl 	if (nsegs > sc->sc_txfree - 1) {
487305f2c06SThomas Moestl 		txs->txs_ndescs = -1;
488305f2c06SThomas Moestl 		return;
489305f2c06SThomas Moestl 	}
490305f2c06SThomas Moestl 	txs->txs_ndescs = nsegs;
49142c1b001SThomas Moestl 
492305f2c06SThomas Moestl 	nexttx = txs->txs_firstdesc;
49342c1b001SThomas Moestl 	/*
49442c1b001SThomas Moestl 	 * Initialize the transmit descriptors.
49542c1b001SThomas Moestl 	 */
49642c1b001SThomas Moestl 	for (seg = 0; seg < nsegs;
497305f2c06SThomas Moestl 	     seg++, nexttx = GEM_NEXTTX(nexttx)) {
49818100346SThomas Moestl #ifdef GEM_DEBUG
49942c1b001SThomas Moestl 		CTR5(KTR_GEM, "txdma_cb: mapping seg %d (txd %d), len "
500305f2c06SThomas Moestl 		    "%lx, addr %#lx (%#lx)",  seg, nexttx,
50142c1b001SThomas Moestl 		    segs[seg].ds_len, segs[seg].ds_addr,
502305f2c06SThomas Moestl 		    GEM_DMA_WRITE(sc, segs[seg].ds_addr));
50318100346SThomas Moestl #endif
504305f2c06SThomas Moestl 
505305f2c06SThomas Moestl 		if (segs[seg].ds_len == 0)
506305f2c06SThomas Moestl 			continue;
507305f2c06SThomas Moestl 		sc->sc_txdescs[nexttx].gd_addr =
508305f2c06SThomas Moestl 		    GEM_DMA_WRITE(sc, segs[seg].ds_addr);
509305f2c06SThomas Moestl 		KASSERT(segs[seg].ds_len < GEM_TD_BUFSIZE,
510305f2c06SThomas Moestl 		    ("gem_txdma_callback: segment size too large!"));
51142c1b001SThomas Moestl 		flags = segs[seg].ds_len & GEM_TD_BUFSIZE;
512305f2c06SThomas Moestl 		if (len == 0) {
51318100346SThomas Moestl #ifdef GEM_DEBUG
51442c1b001SThomas Moestl 			CTR2(KTR_GEM, "txdma_cb: start of packet at seg %d, "
515305f2c06SThomas Moestl 			    "tx %d", seg, nexttx);
51618100346SThomas Moestl #endif
51742c1b001SThomas Moestl 			flags |= GEM_TD_START_OF_PACKET;
518305f2c06SThomas Moestl 			if (++sc->sc_txwin > GEM_NTXSEGS * 2 / 3) {
519305f2c06SThomas Moestl 				sc->sc_txwin = 0;
520336cca9eSBenno Rice 				flags |= GEM_TD_INTERRUPT_ME;
521336cca9eSBenno Rice 			}
52242c1b001SThomas Moestl 		}
523305f2c06SThomas Moestl 		if (len + segs[seg].ds_len == totsz) {
52418100346SThomas Moestl #ifdef GEM_DEBUG
52542c1b001SThomas Moestl 			CTR2(KTR_GEM, "txdma_cb: end of packet at seg %d, "
526305f2c06SThomas Moestl 			    "tx %d", seg, nexttx);
52718100346SThomas Moestl #endif
52842c1b001SThomas Moestl 			flags |= GEM_TD_END_OF_PACKET;
52942c1b001SThomas Moestl 		}
530305f2c06SThomas Moestl 		sc->sc_txdescs[nexttx].gd_flags = GEM_DMA_WRITE(sc, flags);
531305f2c06SThomas Moestl 		txs->txs_lastdesc = nexttx;
532305f2c06SThomas Moestl 		len += segs[seg].ds_len;
53342c1b001SThomas Moestl 	}
534305f2c06SThomas Moestl 	KASSERT((flags & GEM_TD_END_OF_PACKET) != 0,
535305f2c06SThomas Moestl 	    ("gem_txdma_callback: missed end of packet!"));
53642c1b001SThomas Moestl }
53742c1b001SThomas Moestl 
53842c1b001SThomas Moestl static void
53942c1b001SThomas Moestl gem_tick(arg)
54042c1b001SThomas Moestl 	void *arg;
54142c1b001SThomas Moestl {
54242c1b001SThomas Moestl 	struct gem_softc *sc = arg;
54342c1b001SThomas Moestl 
54442c1b001SThomas Moestl 	mii_tick(sc->sc_mii);
54542c1b001SThomas Moestl 
54642c1b001SThomas Moestl 	callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
54742c1b001SThomas Moestl }
54842c1b001SThomas Moestl 
54942c1b001SThomas Moestl static int
55042c1b001SThomas Moestl gem_bitwait(sc, r, clr, set)
55142c1b001SThomas Moestl 	struct gem_softc *sc;
55242c1b001SThomas Moestl 	bus_addr_t r;
55342c1b001SThomas Moestl 	u_int32_t clr;
55442c1b001SThomas Moestl 	u_int32_t set;
55542c1b001SThomas Moestl {
55642c1b001SThomas Moestl 	int i;
55742c1b001SThomas Moestl 	u_int32_t reg;
55842c1b001SThomas Moestl 
55942c1b001SThomas Moestl 	for (i = TRIES; i--; DELAY(100)) {
56042c1b001SThomas Moestl 		reg = bus_space_read_4(sc->sc_bustag, sc->sc_h, r);
56142c1b001SThomas Moestl 		if ((r & clr) == 0 && (r & set) == set)
56242c1b001SThomas Moestl 			return (1);
56342c1b001SThomas Moestl 	}
56442c1b001SThomas Moestl 	return (0);
56542c1b001SThomas Moestl }
56642c1b001SThomas Moestl 
56742c1b001SThomas Moestl void
56842c1b001SThomas Moestl gem_reset(sc)
56942c1b001SThomas Moestl 	struct gem_softc *sc;
57042c1b001SThomas Moestl {
57142c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
57242c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
57342c1b001SThomas Moestl 
57418100346SThomas Moestl #ifdef GEM_DEBUG
57542c1b001SThomas Moestl 	CTR1(KTR_GEM, "%s: gem_reset", device_get_name(sc->sc_dev));
57618100346SThomas Moestl #endif
57742c1b001SThomas Moestl 	gem_reset_rx(sc);
57842c1b001SThomas Moestl 	gem_reset_tx(sc);
57942c1b001SThomas Moestl 
58042c1b001SThomas Moestl 	/* Do a full reset */
58142c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX);
58242c1b001SThomas Moestl 	if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0))
58342c1b001SThomas Moestl 		device_printf(sc->sc_dev, "cannot reset device\n");
58442c1b001SThomas Moestl }
58542c1b001SThomas Moestl 
58642c1b001SThomas Moestl 
58742c1b001SThomas Moestl /*
58842c1b001SThomas Moestl  * gem_rxdrain:
58942c1b001SThomas Moestl  *
59042c1b001SThomas Moestl  *	Drain the receive queue.
59142c1b001SThomas Moestl  */
59242c1b001SThomas Moestl static void
59342c1b001SThomas Moestl gem_rxdrain(sc)
59442c1b001SThomas Moestl 	struct gem_softc *sc;
59542c1b001SThomas Moestl {
59642c1b001SThomas Moestl 	struct gem_rxsoft *rxs;
59742c1b001SThomas Moestl 	int i;
59842c1b001SThomas Moestl 
59942c1b001SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
60042c1b001SThomas Moestl 		rxs = &sc->sc_rxsoft[i];
60142c1b001SThomas Moestl 		if (rxs->rxs_mbuf != NULL) {
602b2d59f42SThomas Moestl 			bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap,
603b2d59f42SThomas Moestl 			    BUS_DMASYNC_POSTREAD);
604305f2c06SThomas Moestl 			bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap);
60542c1b001SThomas Moestl 			m_freem(rxs->rxs_mbuf);
60642c1b001SThomas Moestl 			rxs->rxs_mbuf = NULL;
60742c1b001SThomas Moestl 		}
60842c1b001SThomas Moestl 	}
60942c1b001SThomas Moestl }
61042c1b001SThomas Moestl 
61142c1b001SThomas Moestl /*
61242c1b001SThomas Moestl  * Reset the whole thing.
61342c1b001SThomas Moestl  */
61442c1b001SThomas Moestl static void
61542c1b001SThomas Moestl gem_stop(ifp, disable)
61642c1b001SThomas Moestl 	struct ifnet *ifp;
61742c1b001SThomas Moestl 	int disable;
61842c1b001SThomas Moestl {
61942c1b001SThomas Moestl 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
62042c1b001SThomas Moestl 	struct gem_txsoft *txs;
62142c1b001SThomas Moestl 
62218100346SThomas Moestl #ifdef GEM_DEBUG
62342c1b001SThomas Moestl 	CTR1(KTR_GEM, "%s: gem_stop", device_get_name(sc->sc_dev));
62418100346SThomas Moestl #endif
62542c1b001SThomas Moestl 
62642c1b001SThomas Moestl 	callout_stop(&sc->sc_tick_ch);
62742c1b001SThomas Moestl 
62842c1b001SThomas Moestl 	/* XXX - Should we reset these instead? */
62942c1b001SThomas Moestl 	gem_disable_tx(sc);
63042c1b001SThomas Moestl 	gem_disable_rx(sc);
63142c1b001SThomas Moestl 
63242c1b001SThomas Moestl 	/*
63342c1b001SThomas Moestl 	 * Release any queued transmit buffers.
63442c1b001SThomas Moestl 	 */
63542c1b001SThomas Moestl 	while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
63642c1b001SThomas Moestl 		STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
63742c1b001SThomas Moestl 		if (txs->txs_ndescs != 0) {
638b2d59f42SThomas Moestl 			bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap,
639b2d59f42SThomas Moestl 			    BUS_DMASYNC_POSTWRITE);
640305f2c06SThomas Moestl 			bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap);
64142c1b001SThomas Moestl 			if (txs->txs_mbuf != NULL) {
64242c1b001SThomas Moestl 				m_freem(txs->txs_mbuf);
64342c1b001SThomas Moestl 				txs->txs_mbuf = NULL;
64442c1b001SThomas Moestl 			}
64542c1b001SThomas Moestl 		}
64642c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
64742c1b001SThomas Moestl 	}
64842c1b001SThomas Moestl 
64942c1b001SThomas Moestl 	if (disable)
65042c1b001SThomas Moestl 		gem_rxdrain(sc);
65142c1b001SThomas Moestl 
65242c1b001SThomas Moestl 	/*
65342c1b001SThomas Moestl 	 * Mark the interface down and cancel the watchdog timer.
65442c1b001SThomas Moestl 	 */
65513f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
65642c1b001SThomas Moestl 	ifp->if_timer = 0;
65742c1b001SThomas Moestl }
65842c1b001SThomas Moestl 
65942c1b001SThomas Moestl /*
66042c1b001SThomas Moestl  * Reset the receiver
66142c1b001SThomas Moestl  */
66242c1b001SThomas Moestl int
66342c1b001SThomas Moestl gem_reset_rx(sc)
66442c1b001SThomas Moestl 	struct gem_softc *sc;
66542c1b001SThomas Moestl {
66642c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
66742c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
66842c1b001SThomas Moestl 
66942c1b001SThomas Moestl 	/*
67042c1b001SThomas Moestl 	 * Resetting while DMA is in progress can cause a bus hang, so we
67142c1b001SThomas Moestl 	 * disable DMA first.
67242c1b001SThomas Moestl 	 */
67342c1b001SThomas Moestl 	gem_disable_rx(sc);
67442c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RX_CONFIG, 0);
67542c1b001SThomas Moestl 	/* Wait till it finishes */
67642c1b001SThomas Moestl 	if (!gem_bitwait(sc, GEM_RX_CONFIG, 1, 0))
67742c1b001SThomas Moestl 		device_printf(sc->sc_dev, "cannot disable read dma\n");
67842c1b001SThomas Moestl 
67942c1b001SThomas Moestl 	/* Wait 5ms extra. */
68042c1b001SThomas Moestl 	DELAY(5000);
68142c1b001SThomas Moestl 
68242c1b001SThomas Moestl 	/* Finally, reset the ERX */
68342c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX);
68442c1b001SThomas Moestl 	/* Wait till it finishes */
68542c1b001SThomas Moestl 	if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_TX, 0)) {
68642c1b001SThomas Moestl 		device_printf(sc->sc_dev, "cannot reset receiver\n");
68742c1b001SThomas Moestl 		return (1);
68842c1b001SThomas Moestl 	}
68942c1b001SThomas Moestl 	return (0);
69042c1b001SThomas Moestl }
69142c1b001SThomas Moestl 
69242c1b001SThomas Moestl 
69342c1b001SThomas Moestl /*
69442c1b001SThomas Moestl  * Reset the transmitter
69542c1b001SThomas Moestl  */
69642c1b001SThomas Moestl static int
69742c1b001SThomas Moestl gem_reset_tx(sc)
69842c1b001SThomas Moestl 	struct gem_softc *sc;
69942c1b001SThomas Moestl {
70042c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
70142c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
70242c1b001SThomas Moestl 	int i;
70342c1b001SThomas Moestl 
70442c1b001SThomas Moestl 	/*
70542c1b001SThomas Moestl 	 * Resetting while DMA is in progress can cause a bus hang, so we
70642c1b001SThomas Moestl 	 * disable DMA first.
70742c1b001SThomas Moestl 	 */
70842c1b001SThomas Moestl 	gem_disable_tx(sc);
70942c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_TX_CONFIG, 0);
71042c1b001SThomas Moestl 	/* Wait till it finishes */
71142c1b001SThomas Moestl 	if (!gem_bitwait(sc, GEM_TX_CONFIG, 1, 0))
71242c1b001SThomas Moestl 		device_printf(sc->sc_dev, "cannot disable read dma\n");
71342c1b001SThomas Moestl 
71442c1b001SThomas Moestl 	/* Wait 5ms extra. */
71542c1b001SThomas Moestl 	DELAY(5000);
71642c1b001SThomas Moestl 
71742c1b001SThomas Moestl 	/* Finally, reset the ETX */
71842c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_TX);
71942c1b001SThomas Moestl 	/* Wait till it finishes */
72042c1b001SThomas Moestl 	for (i = TRIES; i--; DELAY(100))
72142c1b001SThomas Moestl 		if ((bus_space_read_4(t, h, GEM_RESET) & GEM_RESET_TX) == 0)
72242c1b001SThomas Moestl 			break;
72342c1b001SThomas Moestl 	if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_TX, 0)) {
72442c1b001SThomas Moestl 		device_printf(sc->sc_dev, "cannot reset receiver\n");
72542c1b001SThomas Moestl 		return (1);
72642c1b001SThomas Moestl 	}
72742c1b001SThomas Moestl 	return (0);
72842c1b001SThomas Moestl }
72942c1b001SThomas Moestl 
73042c1b001SThomas Moestl /*
73142c1b001SThomas Moestl  * disable receiver.
73242c1b001SThomas Moestl  */
73342c1b001SThomas Moestl static int
73442c1b001SThomas Moestl gem_disable_rx(sc)
73542c1b001SThomas Moestl 	struct gem_softc *sc;
73642c1b001SThomas Moestl {
73742c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
73842c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
73942c1b001SThomas Moestl 	u_int32_t cfg;
74042c1b001SThomas Moestl 
74142c1b001SThomas Moestl 	/* Flip the enable bit */
74242c1b001SThomas Moestl 	cfg = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
74342c1b001SThomas Moestl 	cfg &= ~GEM_MAC_RX_ENABLE;
74442c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, cfg);
74542c1b001SThomas Moestl 
74642c1b001SThomas Moestl 	/* Wait for it to finish */
74742c1b001SThomas Moestl 	return (gem_bitwait(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0));
74842c1b001SThomas Moestl }
74942c1b001SThomas Moestl 
75042c1b001SThomas Moestl /*
75142c1b001SThomas Moestl  * disable transmitter.
75242c1b001SThomas Moestl  */
75342c1b001SThomas Moestl static int
75442c1b001SThomas Moestl gem_disable_tx(sc)
75542c1b001SThomas Moestl 	struct gem_softc *sc;
75642c1b001SThomas Moestl {
75742c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
75842c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
75942c1b001SThomas Moestl 	u_int32_t cfg;
76042c1b001SThomas Moestl 
76142c1b001SThomas Moestl 	/* Flip the enable bit */
76242c1b001SThomas Moestl 	cfg = bus_space_read_4(t, h, GEM_MAC_TX_CONFIG);
76342c1b001SThomas Moestl 	cfg &= ~GEM_MAC_TX_ENABLE;
76442c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_TX_CONFIG, cfg);
76542c1b001SThomas Moestl 
76642c1b001SThomas Moestl 	/* Wait for it to finish */
76742c1b001SThomas Moestl 	return (gem_bitwait(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0));
76842c1b001SThomas Moestl }
76942c1b001SThomas Moestl 
77042c1b001SThomas Moestl /*
77142c1b001SThomas Moestl  * Initialize interface.
77242c1b001SThomas Moestl  */
77342c1b001SThomas Moestl static int
77442c1b001SThomas Moestl gem_meminit(sc)
77542c1b001SThomas Moestl 	struct gem_softc *sc;
77642c1b001SThomas Moestl {
77742c1b001SThomas Moestl 	struct gem_rxsoft *rxs;
77842c1b001SThomas Moestl 	int i, error;
77942c1b001SThomas Moestl 
78042c1b001SThomas Moestl 	/*
78142c1b001SThomas Moestl 	 * Initialize the transmit descriptor ring.
78242c1b001SThomas Moestl 	 */
78342c1b001SThomas Moestl 	for (i = 0; i < GEM_NTXDESC; i++) {
78442c1b001SThomas Moestl 		sc->sc_txdescs[i].gd_flags = 0;
78542c1b001SThomas Moestl 		sc->sc_txdescs[i].gd_addr = 0;
78642c1b001SThomas Moestl 	}
787305f2c06SThomas Moestl 	sc->sc_txfree = GEM_MAXTXFREE;
78842c1b001SThomas Moestl 	sc->sc_txnext = 0;
789336cca9eSBenno Rice 	sc->sc_txwin = 0;
79042c1b001SThomas Moestl 
79142c1b001SThomas Moestl 	/*
79242c1b001SThomas Moestl 	 * Initialize the receive descriptor and receive job
79342c1b001SThomas Moestl 	 * descriptor rings.
79442c1b001SThomas Moestl 	 */
79542c1b001SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
79642c1b001SThomas Moestl 		rxs = &sc->sc_rxsoft[i];
79742c1b001SThomas Moestl 		if (rxs->rxs_mbuf == NULL) {
79842c1b001SThomas Moestl 			if ((error = gem_add_rxbuf(sc, i)) != 0) {
79942c1b001SThomas Moestl 				device_printf(sc->sc_dev, "unable to "
80042c1b001SThomas Moestl 				    "allocate or map rx buffer %d, error = "
80142c1b001SThomas Moestl 				    "%d\n", i, error);
80242c1b001SThomas Moestl 				/*
80342c1b001SThomas Moestl 				 * XXX Should attempt to run with fewer receive
80442c1b001SThomas Moestl 				 * XXX buffers instead of just failing.
80542c1b001SThomas Moestl 				 */
80642c1b001SThomas Moestl 				gem_rxdrain(sc);
80742c1b001SThomas Moestl 				return (1);
80842c1b001SThomas Moestl 			}
80942c1b001SThomas Moestl 		} else
81042c1b001SThomas Moestl 			GEM_INIT_RXDESC(sc, i);
81142c1b001SThomas Moestl 	}
81242c1b001SThomas Moestl 	sc->sc_rxptr = 0;
813b2d59f42SThomas Moestl 	GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE);
814b2d59f42SThomas Moestl 	GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD);
81542c1b001SThomas Moestl 
81642c1b001SThomas Moestl 	return (0);
81742c1b001SThomas Moestl }
81842c1b001SThomas Moestl 
81942c1b001SThomas Moestl static int
82042c1b001SThomas Moestl gem_ringsize(sz)
82142c1b001SThomas Moestl 	int sz;
82242c1b001SThomas Moestl {
82342c1b001SThomas Moestl 	int v = 0;
82442c1b001SThomas Moestl 
82542c1b001SThomas Moestl 	switch (sz) {
82642c1b001SThomas Moestl 	case 32:
82742c1b001SThomas Moestl 		v = GEM_RING_SZ_32;
82842c1b001SThomas Moestl 		break;
82942c1b001SThomas Moestl 	case 64:
83042c1b001SThomas Moestl 		v = GEM_RING_SZ_64;
83142c1b001SThomas Moestl 		break;
83242c1b001SThomas Moestl 	case 128:
83342c1b001SThomas Moestl 		v = GEM_RING_SZ_128;
83442c1b001SThomas Moestl 		break;
83542c1b001SThomas Moestl 	case 256:
83642c1b001SThomas Moestl 		v = GEM_RING_SZ_256;
83742c1b001SThomas Moestl 		break;
83842c1b001SThomas Moestl 	case 512:
83942c1b001SThomas Moestl 		v = GEM_RING_SZ_512;
84042c1b001SThomas Moestl 		break;
84142c1b001SThomas Moestl 	case 1024:
84242c1b001SThomas Moestl 		v = GEM_RING_SZ_1024;
84342c1b001SThomas Moestl 		break;
84442c1b001SThomas Moestl 	case 2048:
84542c1b001SThomas Moestl 		v = GEM_RING_SZ_2048;
84642c1b001SThomas Moestl 		break;
84742c1b001SThomas Moestl 	case 4096:
84842c1b001SThomas Moestl 		v = GEM_RING_SZ_4096;
84942c1b001SThomas Moestl 		break;
85042c1b001SThomas Moestl 	case 8192:
85142c1b001SThomas Moestl 		v = GEM_RING_SZ_8192;
85242c1b001SThomas Moestl 		break;
85342c1b001SThomas Moestl 	default:
85442c1b001SThomas Moestl 		printf("gem: invalid Receive Descriptor ring size\n");
85542c1b001SThomas Moestl 		break;
85642c1b001SThomas Moestl 	}
85742c1b001SThomas Moestl 	return (v);
85842c1b001SThomas Moestl }
85942c1b001SThomas Moestl 
86042c1b001SThomas Moestl static void
86142c1b001SThomas Moestl gem_init(xsc)
86242c1b001SThomas Moestl 	void *xsc;
86342c1b001SThomas Moestl {
86442c1b001SThomas Moestl 	struct gem_softc *sc = (struct gem_softc *)xsc;
8658cfaff7dSMarius Strobl 
8668cfaff7dSMarius Strobl 	GEM_LOCK(sc);
8678cfaff7dSMarius Strobl 	gem_init_locked(sc);
8688cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
8698cfaff7dSMarius Strobl }
8708cfaff7dSMarius Strobl 
8718cfaff7dSMarius Strobl /*
8728cfaff7dSMarius Strobl  * Initialization of interface; set up initialization block
8738cfaff7dSMarius Strobl  * and transmit/receive descriptor rings.
8748cfaff7dSMarius Strobl  */
8758cfaff7dSMarius Strobl static void
8768cfaff7dSMarius Strobl gem_init_locked(sc)
8778cfaff7dSMarius Strobl 	struct gem_softc *sc;
8788cfaff7dSMarius Strobl {
879fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
88042c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
88142c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
88242c1b001SThomas Moestl 	u_int32_t v;
88342c1b001SThomas Moestl 
8848cfaff7dSMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
88542c1b001SThomas Moestl 
88618100346SThomas Moestl #ifdef GEM_DEBUG
88742c1b001SThomas Moestl 	CTR1(KTR_GEM, "%s: gem_init: calling stop", device_get_name(sc->sc_dev));
88818100346SThomas Moestl #endif
88942c1b001SThomas Moestl 	/*
89042c1b001SThomas Moestl 	 * Initialization sequence. The numbered steps below correspond
89142c1b001SThomas Moestl 	 * to the sequence outlined in section 6.3.5.1 in the Ethernet
89242c1b001SThomas Moestl 	 * Channel Engine manual (part of the PCIO manual).
89342c1b001SThomas Moestl 	 * See also the STP2002-STQ document from Sun Microsystems.
89442c1b001SThomas Moestl 	 */
89542c1b001SThomas Moestl 
89642c1b001SThomas Moestl 	/* step 1 & 2. Reset the Ethernet Channel */
897fc74a9f9SBrooks Davis 	gem_stop(sc->sc_ifp, 0);
89842c1b001SThomas Moestl 	gem_reset(sc);
89918100346SThomas Moestl #ifdef GEM_DEBUG
90042c1b001SThomas Moestl 	CTR1(KTR_GEM, "%s: gem_init: restarting", device_get_name(sc->sc_dev));
90118100346SThomas Moestl #endif
90242c1b001SThomas Moestl 
90342c1b001SThomas Moestl 	/* Re-initialize the MIF */
90442c1b001SThomas Moestl 	gem_mifinit(sc);
90542c1b001SThomas Moestl 
90642c1b001SThomas Moestl 	/* step 3. Setup data structures in host memory */
90742c1b001SThomas Moestl 	gem_meminit(sc);
90842c1b001SThomas Moestl 
90942c1b001SThomas Moestl 	/* step 4. TX MAC registers & counters */
91042c1b001SThomas Moestl 	gem_init_regs(sc);
91142c1b001SThomas Moestl 
91242c1b001SThomas Moestl 	/* step 5. RX MAC registers & counters */
91342c1b001SThomas Moestl 	gem_setladrf(sc);
91442c1b001SThomas Moestl 
91542c1b001SThomas Moestl 	/* step 6 & 7. Program Descriptor Ring Base Addresses */
91642c1b001SThomas Moestl 	/* NOTE: we use only 32-bit DMA addresses here. */
91742c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_TX_RING_PTR_HI, 0);
91842c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0));
91942c1b001SThomas Moestl 
92042c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RX_RING_PTR_HI, 0);
92142c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0));
92218100346SThomas Moestl #ifdef GEM_DEBUG
92342c1b001SThomas Moestl 	CTR3(KTR_GEM, "loading rx ring %lx, tx ring %lx, cddma %lx",
92442c1b001SThomas Moestl 	    GEM_CDRXADDR(sc, 0), GEM_CDTXADDR(sc, 0), sc->sc_cddma);
92518100346SThomas Moestl #endif
92642c1b001SThomas Moestl 
92742c1b001SThomas Moestl 	/* step 8. Global Configuration & Interrupt Mask */
92842c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_INTMASK,
92942c1b001SThomas Moestl 		      ~(GEM_INTR_TX_INTME|
93042c1b001SThomas Moestl 			GEM_INTR_TX_EMPTY|
93142c1b001SThomas Moestl 			GEM_INTR_RX_DONE|GEM_INTR_RX_NOBUF|
93242c1b001SThomas Moestl 			GEM_INTR_RX_TAG_ERR|GEM_INTR_PCS|
93342c1b001SThomas Moestl 			GEM_INTR_MAC_CONTROL|GEM_INTR_MIF|
93442c1b001SThomas Moestl 			GEM_INTR_BERR));
935336cca9eSBenno Rice 	bus_space_write_4(t, h, GEM_MAC_RX_MASK,
936336cca9eSBenno Rice 			GEM_MAC_RX_DONE|GEM_MAC_RX_FRAME_CNT);
93742c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_TX_MASK, 0xffff); /* XXXX */
93842c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_CONTROL_MASK, 0); /* XXXX */
93942c1b001SThomas Moestl 
94042c1b001SThomas Moestl 	/* step 9. ETX Configuration: use mostly default values */
94142c1b001SThomas Moestl 
94242c1b001SThomas Moestl 	/* Enable DMA */
94342c1b001SThomas Moestl 	v = gem_ringsize(GEM_NTXDESC /*XXX*/);
94442c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_TX_CONFIG,
94542c1b001SThomas Moestl 		v|GEM_TX_CONFIG_TXDMA_EN|
94642c1b001SThomas Moestl 		((0x400<<10)&GEM_TX_CONFIG_TXFIFO_TH));
94742c1b001SThomas Moestl 
94842c1b001SThomas Moestl 	/* step 10. ERX Configuration */
94942c1b001SThomas Moestl 
95042c1b001SThomas Moestl 	/* Encode Receive Descriptor ring size: four possible values */
95142c1b001SThomas Moestl 	v = gem_ringsize(GEM_NRXDESC /*XXX*/);
95242c1b001SThomas Moestl 
95342c1b001SThomas Moestl 	/* Enable DMA */
95442c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RX_CONFIG,
95542c1b001SThomas Moestl 		v|(GEM_THRSH_1024<<GEM_RX_CONFIG_FIFO_THRS_SHIFT)|
95642c1b001SThomas Moestl 		(2<<GEM_RX_CONFIG_FBOFF_SHFT)|GEM_RX_CONFIG_RXDMA_EN|
95742c1b001SThomas Moestl 		(0<<GEM_RX_CONFIG_CXM_START_SHFT));
95842c1b001SThomas Moestl 	/*
959336cca9eSBenno Rice 	 * The following value is for an OFF Threshold of about 3/4 full
960336cca9eSBenno Rice 	 * and an ON Threshold of 1/4 full.
96142c1b001SThomas Moestl 	 */
962336cca9eSBenno Rice 	bus_space_write_4(t, h, GEM_RX_PAUSE_THRESH,
963336cca9eSBenno Rice 	    (3 * sc->sc_rxfifosize / 256) |
964336cca9eSBenno Rice 	    (   (sc->sc_rxfifosize / 256) << 12));
965336cca9eSBenno Rice 	bus_space_write_4(t, h, GEM_RX_BLANKING, (6<<12)|6);
96642c1b001SThomas Moestl 
96742c1b001SThomas Moestl 	/* step 11. Configure Media */
9688cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
969336cca9eSBenno Rice 	mii_mediachg(sc->sc_mii);
9708cfaff7dSMarius Strobl 	GEM_LOCK(sc);
97142c1b001SThomas Moestl 
97242c1b001SThomas Moestl 	/* step 12. RX_MAC Configuration Register */
97342c1b001SThomas Moestl 	v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
97442c1b001SThomas Moestl 	v |= GEM_MAC_RX_ENABLE;
97542c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
97642c1b001SThomas Moestl 
97742c1b001SThomas Moestl 	/* step 14. Issue Transmit Pending command */
97842c1b001SThomas Moestl 
97942c1b001SThomas Moestl 	/* step 15.  Give the reciever a swift kick */
98042c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RX_KICK, GEM_NRXDESC-4);
98142c1b001SThomas Moestl 
98242c1b001SThomas Moestl 	/* Start the one second timer. */
98342c1b001SThomas Moestl 	callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
98442c1b001SThomas Moestl 
98513f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
98613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
98742c1b001SThomas Moestl 	ifp->if_timer = 0;
988336cca9eSBenno Rice 	sc->sc_ifflags = ifp->if_flags;
98942c1b001SThomas Moestl }
99042c1b001SThomas Moestl 
99142c1b001SThomas Moestl static int
992305f2c06SThomas Moestl gem_load_txmbuf(sc, m0)
99342c1b001SThomas Moestl 	struct gem_softc *sc;
99442c1b001SThomas Moestl 	struct mbuf *m0;
99542c1b001SThomas Moestl {
99642c1b001SThomas Moestl 	struct gem_txdma txd;
99742c1b001SThomas Moestl 	struct gem_txsoft *txs;
998305f2c06SThomas Moestl 	int error;
99942c1b001SThomas Moestl 
100042c1b001SThomas Moestl 	/* Get a work queue entry. */
100142c1b001SThomas Moestl 	if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) {
1002305f2c06SThomas Moestl 		/* Ran out of descriptors. */
1003305f2c06SThomas Moestl 		return (-1);
1004305f2c06SThomas Moestl 	}
1005305f2c06SThomas Moestl 	txd.txd_sc = sc;
1006305f2c06SThomas Moestl 	txd.txd_txs = txs;
1007305f2c06SThomas Moestl 	txs->txs_firstdesc = sc->sc_txnext;
1008305f2c06SThomas Moestl 	error = bus_dmamap_load_mbuf(sc->sc_tdmatag, txs->txs_dmamap, m0,
1009305f2c06SThomas Moestl 	    gem_txdma_callback, &txd, BUS_DMA_NOWAIT);
1010305f2c06SThomas Moestl 	if (error != 0)
1011305f2c06SThomas Moestl 		goto fail;
1012305f2c06SThomas Moestl 	if (txs->txs_ndescs == -1) {
101342c1b001SThomas Moestl 		error = -1;
101442c1b001SThomas Moestl 		goto fail;
101542c1b001SThomas Moestl 	}
1016305f2c06SThomas Moestl 
101742c1b001SThomas Moestl 	/* Sync the DMA map. */
1018305f2c06SThomas Moestl 	bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap,
101942c1b001SThomas Moestl 	    BUS_DMASYNC_PREWRITE);
1020305f2c06SThomas Moestl 
102118100346SThomas Moestl #ifdef GEM_DEBUG
102242c1b001SThomas Moestl 	CTR3(KTR_GEM, "load_mbuf: setting firstdesc=%d, lastdesc=%d, "
102342c1b001SThomas Moestl 	    "ndescs=%d", txs->txs_firstdesc, txs->txs_lastdesc,
102442c1b001SThomas Moestl 	    txs->txs_ndescs);
102518100346SThomas Moestl #endif
102642c1b001SThomas Moestl 	STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
1027305f2c06SThomas Moestl 	STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
1028c3d5598aSMarius Strobl 	txs->txs_mbuf = m0;
1029305f2c06SThomas Moestl 
1030305f2c06SThomas Moestl 	sc->sc_txnext = GEM_NEXTTX(txs->txs_lastdesc);
1031305f2c06SThomas Moestl 	sc->sc_txfree -= txs->txs_ndescs;
103242c1b001SThomas Moestl 	return (0);
103342c1b001SThomas Moestl 
103442c1b001SThomas Moestl fail:
103518100346SThomas Moestl #ifdef GEM_DEBUG
1036305f2c06SThomas Moestl 	CTR1(KTR_GEM, "gem_load_txmbuf failed (%d)", error);
103718100346SThomas Moestl #endif
1038305f2c06SThomas Moestl 	bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap);
103942c1b001SThomas Moestl 	return (error);
104042c1b001SThomas Moestl }
104142c1b001SThomas Moestl 
104242c1b001SThomas Moestl static void
104342c1b001SThomas Moestl gem_init_regs(sc)
104442c1b001SThomas Moestl 	struct gem_softc *sc;
104542c1b001SThomas Moestl {
104642c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
104742c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
1048fc74a9f9SBrooks Davis 	const u_char *laddr = IFP2ENADDR(sc->sc_ifp);
1049336cca9eSBenno Rice 	u_int32_t v;
105042c1b001SThomas Moestl 
105142c1b001SThomas Moestl 	/* These regs are not cleared on reset */
105242c1b001SThomas Moestl 	if (!sc->sc_inited) {
105342c1b001SThomas Moestl 
105442c1b001SThomas Moestl 		/* Wooo.  Magic values. */
105542c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_IPG0, 0);
105642c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_IPG1, 8);
105742c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_IPG2, 4);
105842c1b001SThomas Moestl 
105942c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN);
106042c1b001SThomas Moestl 		/* Max frame and max burst size */
106142c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
106200d12766SMarius Strobl 		    (ETHER_MAX_LEN + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN) |
106300d12766SMarius Strobl 		    (0x2000 << 16));
1064336cca9eSBenno Rice 
106542c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_PREAMBLE_LEN, 0x7);
106642c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_JAM_SIZE, 0x4);
106742c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ATTEMPT_LIMIT, 0x10);
106842c1b001SThomas Moestl 		/* Dunno.... */
106942c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_CONTROL_TYPE, 0x8088);
107042c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_RANDOM_SEED,
1071336cca9eSBenno Rice 		    ((laddr[5]<<8)|laddr[4])&0x3ff);
1072336cca9eSBenno Rice 
107342c1b001SThomas Moestl 		/* Secondary MAC addr set to 0:0:0:0:0:0 */
107442c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR3, 0);
107542c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR4, 0);
107642c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR5, 0);
1077336cca9eSBenno Rice 
1078336cca9eSBenno Rice 		/* MAC control addr set to 01:80:c2:00:00:01 */
107942c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR6, 0x0001);
108042c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR7, 0xc200);
108142c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR8, 0x0180);
108242c1b001SThomas Moestl 
108342c1b001SThomas Moestl 		/* MAC filter addr set to 0:0:0:0:0:0 */
108442c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER0, 0);
108542c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER1, 0);
108642c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER2, 0);
108742c1b001SThomas Moestl 
108842c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK1_2, 0);
108942c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK0, 0);
109042c1b001SThomas Moestl 
109142c1b001SThomas Moestl 		sc->sc_inited = 1;
109242c1b001SThomas Moestl 	}
109342c1b001SThomas Moestl 
109442c1b001SThomas Moestl 	/* Counters need to be zeroed */
109542c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_NORM_COLL_CNT, 0);
109642c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_FIRST_COLL_CNT, 0);
109742c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_EXCESS_COLL_CNT, 0);
109842c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_LATE_COLL_CNT, 0);
109942c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_DEFER_TMR_CNT, 0);
110042c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_PEAK_ATTEMPTS, 0);
110142c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_FRAME_COUNT, 0);
110242c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_LEN_ERR_CNT, 0);
110342c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_ALIGN_ERR, 0);
110442c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_CRC_ERR_CNT, 0);
110542c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_CODE_VIOL, 0);
110642c1b001SThomas Moestl 
110742c1b001SThomas Moestl 	/* Un-pause stuff */
110842c1b001SThomas Moestl #if 0
110942c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0);
111042c1b001SThomas Moestl #else
111142c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0);
111242c1b001SThomas Moestl #endif
111342c1b001SThomas Moestl 
111442c1b001SThomas Moestl 	/*
111542c1b001SThomas Moestl 	 * Set the station address.
111642c1b001SThomas Moestl 	 */
1117336cca9eSBenno Rice 	bus_space_write_4(t, h, GEM_MAC_ADDR0, (laddr[4]<<8)|laddr[5]);
1118336cca9eSBenno Rice 	bus_space_write_4(t, h, GEM_MAC_ADDR1, (laddr[2]<<8)|laddr[3]);
1119336cca9eSBenno Rice 	bus_space_write_4(t, h, GEM_MAC_ADDR2, (laddr[0]<<8)|laddr[1]);
1120336cca9eSBenno Rice 
1121336cca9eSBenno Rice 	/*
1122336cca9eSBenno Rice 	 * Enable MII outputs.  Enable GMII if there is a gigabit PHY.
1123336cca9eSBenno Rice 	 */
1124336cca9eSBenno Rice 	sc->sc_mif_config = bus_space_read_4(t, h, GEM_MIF_CONFIG);
1125336cca9eSBenno Rice 	v = GEM_MAC_XIF_TX_MII_ENA;
1126336cca9eSBenno Rice 	if (sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) {
1127336cca9eSBenno Rice 		v |= GEM_MAC_XIF_FDPLX_LED;
1128336cca9eSBenno Rice 		if (sc->sc_flags & GEM_GIGABIT)
1129336cca9eSBenno Rice 			v |= GEM_MAC_XIF_GMII_MODE;
1130336cca9eSBenno Rice 	}
1131336cca9eSBenno Rice 	bus_space_write_4(t, h, GEM_MAC_XIF_CONFIG, v);
113242c1b001SThomas Moestl }
113342c1b001SThomas Moestl 
113442c1b001SThomas Moestl static void
113542c1b001SThomas Moestl gem_start(ifp)
113642c1b001SThomas Moestl 	struct ifnet *ifp;
113742c1b001SThomas Moestl {
113842c1b001SThomas Moestl 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
11398cfaff7dSMarius Strobl 
11408cfaff7dSMarius Strobl 	GEM_LOCK(sc);
11418cfaff7dSMarius Strobl 	gem_start_locked(ifp);
11428cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
11438cfaff7dSMarius Strobl }
11448cfaff7dSMarius Strobl 
11458cfaff7dSMarius Strobl static void
11468cfaff7dSMarius Strobl gem_start_locked(ifp)
11478cfaff7dSMarius Strobl 	struct ifnet *ifp;
11488cfaff7dSMarius Strobl {
11498cfaff7dSMarius Strobl 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
1150305f2c06SThomas Moestl 	struct mbuf *m0 = NULL;
115118100346SThomas Moestl 	int firsttx, ntx = 0, ofree, txmfail;
115242c1b001SThomas Moestl 
115313f4c340SRobert Watson 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
115413f4c340SRobert Watson 	    IFF_DRV_RUNNING)
115542c1b001SThomas Moestl 		return;
115642c1b001SThomas Moestl 
115742c1b001SThomas Moestl 	/*
115842c1b001SThomas Moestl 	 * Remember the previous number of free descriptors and
115942c1b001SThomas Moestl 	 * the first descriptor we'll use.
116042c1b001SThomas Moestl 	 */
116142c1b001SThomas Moestl 	ofree = sc->sc_txfree;
116242c1b001SThomas Moestl 	firsttx = sc->sc_txnext;
116342c1b001SThomas Moestl 
116418100346SThomas Moestl #ifdef GEM_DEBUG
116542c1b001SThomas Moestl 	CTR3(KTR_GEM, "%s: gem_start: txfree %d, txnext %d",
116642c1b001SThomas Moestl 	    device_get_name(sc->sc_dev), ofree, firsttx);
116718100346SThomas Moestl #endif
116842c1b001SThomas Moestl 
116942c1b001SThomas Moestl 	/*
117042c1b001SThomas Moestl 	 * Loop through the send queue, setting up transmit descriptors
117142c1b001SThomas Moestl 	 * until we drain the queue, or use up all available transmit
117242c1b001SThomas Moestl 	 * descriptors.
117342c1b001SThomas Moestl 	 */
117442c1b001SThomas Moestl 	txmfail = 0;
117518100346SThomas Moestl 	do {
117642c1b001SThomas Moestl 		/*
117742c1b001SThomas Moestl 		 * Grab a packet off the queue.
117842c1b001SThomas Moestl 		 */
117942c1b001SThomas Moestl 		IF_DEQUEUE(&ifp->if_snd, m0);
118042c1b001SThomas Moestl 		if (m0 == NULL)
118142c1b001SThomas Moestl 			break;
118242c1b001SThomas Moestl 
1183305f2c06SThomas Moestl 		txmfail = gem_load_txmbuf(sc, m0);
1184305f2c06SThomas Moestl 		if (txmfail > 0) {
1185305f2c06SThomas Moestl 			/* Drop the mbuf and complain. */
1186305f2c06SThomas Moestl 			printf("gem_start: error %d while loading mbuf dma "
1187305f2c06SThomas Moestl 			    "map\n", txmfail);
1188305f2c06SThomas Moestl 			continue;
1189305f2c06SThomas Moestl 		}
1190305f2c06SThomas Moestl 		/* Not enough descriptors. */
119142c1b001SThomas Moestl 		if (txmfail == -1) {
1192305f2c06SThomas Moestl 			if (sc->sc_txfree == GEM_MAXTXFREE)
1193305f2c06SThomas Moestl 				panic("gem_start: mbuf chain too long!");
119442c1b001SThomas Moestl 			IF_PREPEND(&ifp->if_snd, m0);
119542c1b001SThomas Moestl 			break;
119642c1b001SThomas Moestl 		}
119742c1b001SThomas Moestl 
119818100346SThomas Moestl 		ntx++;
1199305f2c06SThomas Moestl 		/* Kick the transmitter. */
120018100346SThomas Moestl #ifdef GEM_DEBUG
1201305f2c06SThomas Moestl 		CTR2(KTR_GEM, "%s: gem_start: kicking tx %d",
1202305f2c06SThomas Moestl 		    device_get_name(sc->sc_dev), sc->sc_txnext);
120318100346SThomas Moestl #endif
120442c1b001SThomas Moestl 		bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_TX_KICK,
120542c1b001SThomas Moestl 			sc->sc_txnext);
120642c1b001SThomas Moestl 
1207305f2c06SThomas Moestl 		if (ifp->if_bpf != NULL)
1208305f2c06SThomas Moestl 			bpf_mtap(ifp->if_bpf, m0);
120918100346SThomas Moestl 	} while (1);
1210305f2c06SThomas Moestl 
1211305f2c06SThomas Moestl 	if (txmfail == -1 || sc->sc_txfree == 0) {
1212305f2c06SThomas Moestl 		/* No more slots left; notify upper layer. */
121313f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1214305f2c06SThomas Moestl 	}
1215305f2c06SThomas Moestl 
1216305f2c06SThomas Moestl 	if (ntx > 0) {
1217b2d59f42SThomas Moestl 		GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE);
1218b2d59f42SThomas Moestl 
121918100346SThomas Moestl #ifdef GEM_DEBUG
1220305f2c06SThomas Moestl 		CTR2(KTR_GEM, "%s: packets enqueued, OWN on %d",
1221305f2c06SThomas Moestl 		    device_get_name(sc->sc_dev), firsttx);
122218100346SThomas Moestl #endif
1223305f2c06SThomas Moestl 
122442c1b001SThomas Moestl 		/* Set a watchdog timer in case the chip flakes out. */
122542c1b001SThomas Moestl 		ifp->if_timer = 5;
122618100346SThomas Moestl #ifdef GEM_DEBUG
122742c1b001SThomas Moestl 		CTR2(KTR_GEM, "%s: gem_start: watchdog %d",
122842c1b001SThomas Moestl 			device_get_name(sc->sc_dev), ifp->if_timer);
122918100346SThomas Moestl #endif
123042c1b001SThomas Moestl 	}
123142c1b001SThomas Moestl }
123242c1b001SThomas Moestl 
123342c1b001SThomas Moestl /*
123442c1b001SThomas Moestl  * Transmit interrupt.
123542c1b001SThomas Moestl  */
123642c1b001SThomas Moestl static void
123742c1b001SThomas Moestl gem_tint(sc)
123842c1b001SThomas Moestl 	struct gem_softc *sc;
123942c1b001SThomas Moestl {
1240fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
124142c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
124242c1b001SThomas Moestl 	bus_space_handle_t mac = sc->sc_h;
124342c1b001SThomas Moestl 	struct gem_txsoft *txs;
124442c1b001SThomas Moestl 	int txlast;
1245336cca9eSBenno Rice 	int progress = 0;
124642c1b001SThomas Moestl 
124742c1b001SThomas Moestl 
124818100346SThomas Moestl #ifdef GEM_DEBUG
124942c1b001SThomas Moestl 	CTR1(KTR_GEM, "%s: gem_tint", device_get_name(sc->sc_dev));
125018100346SThomas Moestl #endif
125142c1b001SThomas Moestl 
125242c1b001SThomas Moestl 	/*
125342c1b001SThomas Moestl 	 * Unload collision counters
125442c1b001SThomas Moestl 	 */
125542c1b001SThomas Moestl 	ifp->if_collisions +=
125642c1b001SThomas Moestl 		bus_space_read_4(t, mac, GEM_MAC_NORM_COLL_CNT) +
125742c1b001SThomas Moestl 		bus_space_read_4(t, mac, GEM_MAC_FIRST_COLL_CNT) +
125842c1b001SThomas Moestl 		bus_space_read_4(t, mac, GEM_MAC_EXCESS_COLL_CNT) +
125942c1b001SThomas Moestl 		bus_space_read_4(t, mac, GEM_MAC_LATE_COLL_CNT);
126042c1b001SThomas Moestl 
126142c1b001SThomas Moestl 	/*
126242c1b001SThomas Moestl 	 * then clear the hardware counters.
126342c1b001SThomas Moestl 	 */
126442c1b001SThomas Moestl 	bus_space_write_4(t, mac, GEM_MAC_NORM_COLL_CNT, 0);
126542c1b001SThomas Moestl 	bus_space_write_4(t, mac, GEM_MAC_FIRST_COLL_CNT, 0);
126642c1b001SThomas Moestl 	bus_space_write_4(t, mac, GEM_MAC_EXCESS_COLL_CNT, 0);
126742c1b001SThomas Moestl 	bus_space_write_4(t, mac, GEM_MAC_LATE_COLL_CNT, 0);
126842c1b001SThomas Moestl 
126942c1b001SThomas Moestl 	/*
127042c1b001SThomas Moestl 	 * Go through our Tx list and free mbufs for those
127142c1b001SThomas Moestl 	 * frames that have been transmitted.
127242c1b001SThomas Moestl 	 */
1273b2d59f42SThomas Moestl 	GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD);
127442c1b001SThomas Moestl 	while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
127542c1b001SThomas Moestl 
127642c1b001SThomas Moestl #ifdef GEM_DEBUG
127742c1b001SThomas Moestl 		if (ifp->if_flags & IFF_DEBUG) {
127842c1b001SThomas Moestl 			int i;
127942c1b001SThomas Moestl 			printf("    txsoft %p transmit chain:\n", txs);
128042c1b001SThomas Moestl 			for (i = txs->txs_firstdesc;; i = GEM_NEXTTX(i)) {
128142c1b001SThomas Moestl 				printf("descriptor %d: ", i);
128242c1b001SThomas Moestl 				printf("gd_flags: 0x%016llx\t", (long long)
128342c1b001SThomas Moestl 					GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_flags));
128442c1b001SThomas Moestl 				printf("gd_addr: 0x%016llx\n", (long long)
128542c1b001SThomas Moestl 					GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_addr));
128642c1b001SThomas Moestl 				if (i == txs->txs_lastdesc)
128742c1b001SThomas Moestl 					break;
128842c1b001SThomas Moestl 			}
128942c1b001SThomas Moestl 		}
129042c1b001SThomas Moestl #endif
129142c1b001SThomas Moestl 
129242c1b001SThomas Moestl 		/*
129342c1b001SThomas Moestl 		 * In theory, we could harveast some descriptors before
129442c1b001SThomas Moestl 		 * the ring is empty, but that's a bit complicated.
129542c1b001SThomas Moestl 		 *
129642c1b001SThomas Moestl 		 * GEM_TX_COMPLETION points to the last descriptor
129742c1b001SThomas Moestl 		 * processed +1.
129842c1b001SThomas Moestl 		 */
129942c1b001SThomas Moestl 		txlast = bus_space_read_4(t, mac, GEM_TX_COMPLETION);
130018100346SThomas Moestl #ifdef GEM_DEBUG
130142c1b001SThomas Moestl 		CTR3(KTR_GEM, "gem_tint: txs->txs_firstdesc = %d, "
130242c1b001SThomas Moestl 		    "txs->txs_lastdesc = %d, txlast = %d",
130342c1b001SThomas Moestl 		    txs->txs_firstdesc, txs->txs_lastdesc, txlast);
130418100346SThomas Moestl #endif
130542c1b001SThomas Moestl 		if (txs->txs_firstdesc <= txs->txs_lastdesc) {
130642c1b001SThomas Moestl 			if ((txlast >= txs->txs_firstdesc) &&
130742c1b001SThomas Moestl 				(txlast <= txs->txs_lastdesc))
130842c1b001SThomas Moestl 				break;
130942c1b001SThomas Moestl 		} else {
131042c1b001SThomas Moestl 			/* Ick -- this command wraps */
131142c1b001SThomas Moestl 			if ((txlast >= txs->txs_firstdesc) ||
131242c1b001SThomas Moestl 				(txlast <= txs->txs_lastdesc))
131342c1b001SThomas Moestl 				break;
131442c1b001SThomas Moestl 		}
131542c1b001SThomas Moestl 
131618100346SThomas Moestl #ifdef GEM_DEBUG
131742c1b001SThomas Moestl 		CTR0(KTR_GEM, "gem_tint: releasing a desc");
131818100346SThomas Moestl #endif
131942c1b001SThomas Moestl 		STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
132042c1b001SThomas Moestl 
132142c1b001SThomas Moestl 		sc->sc_txfree += txs->txs_ndescs;
132242c1b001SThomas Moestl 
1323305f2c06SThomas Moestl 		bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap,
132442c1b001SThomas Moestl 		    BUS_DMASYNC_POSTWRITE);
1325305f2c06SThomas Moestl 		bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap);
132642c1b001SThomas Moestl 		if (txs->txs_mbuf != NULL) {
132742c1b001SThomas Moestl 			m_freem(txs->txs_mbuf);
132842c1b001SThomas Moestl 			txs->txs_mbuf = NULL;
132942c1b001SThomas Moestl 		}
133042c1b001SThomas Moestl 
133142c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
133242c1b001SThomas Moestl 
133342c1b001SThomas Moestl 		ifp->if_opackets++;
1334336cca9eSBenno Rice 		progress = 1;
133542c1b001SThomas Moestl 	}
133642c1b001SThomas Moestl 
133718100346SThomas Moestl #ifdef GEM_DEBUG
133842c1b001SThomas Moestl 	CTR3(KTR_GEM, "gem_tint: GEM_TX_STATE_MACHINE %x "
133942c1b001SThomas Moestl 		"GEM_TX_DATA_PTR %llx "
134042c1b001SThomas Moestl 		"GEM_TX_COMPLETION %x",
134142c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_STATE_MACHINE),
134242c1b001SThomas Moestl 		((long long) bus_space_read_4(sc->sc_bustag, sc->sc_h,
134342c1b001SThomas Moestl 			GEM_TX_DATA_PTR_HI) << 32) |
134442c1b001SThomas Moestl 			     bus_space_read_4(sc->sc_bustag, sc->sc_h,
134542c1b001SThomas Moestl 			GEM_TX_DATA_PTR_LO),
134642c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_COMPLETION));
134718100346SThomas Moestl #endif
134842c1b001SThomas Moestl 
1349336cca9eSBenno Rice 	if (progress) {
1350336cca9eSBenno Rice 		if (sc->sc_txfree == GEM_NTXDESC - 1)
1351336cca9eSBenno Rice 			sc->sc_txwin = 0;
135242c1b001SThomas Moestl 
135313f4c340SRobert Watson 		/* Freed some descriptors, so reset IFF_DRV_OACTIVE and restart. */
135413f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
13558cfaff7dSMarius Strobl 		gem_start_locked(ifp);
1356336cca9eSBenno Rice 
1357336cca9eSBenno Rice 		if (STAILQ_EMPTY(&sc->sc_txdirtyq))
1358336cca9eSBenno Rice 			ifp->if_timer = 0;
1359336cca9eSBenno Rice 	}
136042c1b001SThomas Moestl 
136118100346SThomas Moestl #ifdef GEM_DEBUG
136242c1b001SThomas Moestl 	CTR2(KTR_GEM, "%s: gem_tint: watchdog %d",
136342c1b001SThomas Moestl 		device_get_name(sc->sc_dev), ifp->if_timer);
136418100346SThomas Moestl #endif
136542c1b001SThomas Moestl }
136642c1b001SThomas Moestl 
1367c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT
13680d80b9bdSThomas Moestl static void
13690d80b9bdSThomas Moestl gem_rint_timeout(arg)
13700d80b9bdSThomas Moestl 	void *arg;
13710d80b9bdSThomas Moestl {
13728cfaff7dSMarius Strobl 	struct gem_softc *sc = (struct gem_softc *)arg;
13730d80b9bdSThomas Moestl 
13748cfaff7dSMarius Strobl 	GEM_LOCK(sc);
13758cfaff7dSMarius Strobl 	gem_rint(sc);
13768cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
13770d80b9bdSThomas Moestl }
137811e3f060SJake Burkholder #endif
13790d80b9bdSThomas Moestl 
138042c1b001SThomas Moestl /*
138142c1b001SThomas Moestl  * Receive interrupt.
138242c1b001SThomas Moestl  */
138342c1b001SThomas Moestl static void
138442c1b001SThomas Moestl gem_rint(sc)
138542c1b001SThomas Moestl 	struct gem_softc *sc;
138642c1b001SThomas Moestl {
1387fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
138842c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
138942c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
139042c1b001SThomas Moestl 	struct gem_rxsoft *rxs;
139142c1b001SThomas Moestl 	struct mbuf *m;
139242c1b001SThomas Moestl 	u_int64_t rxstat;
1393336cca9eSBenno Rice 	u_int32_t rxcomp;
1394336cca9eSBenno Rice 	int i, len, progress = 0;
139542c1b001SThomas Moestl 
1396c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT
13970d80b9bdSThomas Moestl 	callout_stop(&sc->sc_rx_ch);
1398c3d5598aSMarius Strobl #endif
139918100346SThomas Moestl #ifdef GEM_DEBUG
140042c1b001SThomas Moestl 	CTR1(KTR_GEM, "%s: gem_rint", device_get_name(sc->sc_dev));
140118100346SThomas Moestl #endif
1402336cca9eSBenno Rice 
1403336cca9eSBenno Rice 	/*
1404336cca9eSBenno Rice 	 * Read the completion register once.  This limits
1405336cca9eSBenno Rice 	 * how long the following loop can execute.
1406336cca9eSBenno Rice 	 */
1407336cca9eSBenno Rice 	rxcomp = bus_space_read_4(t, h, GEM_RX_COMPLETION);
1408336cca9eSBenno Rice 
140918100346SThomas Moestl #ifdef GEM_DEBUG
141042c1b001SThomas Moestl 	CTR2(KTR_GEM, "gem_rint: sc->rxptr %d, complete %d",
1411336cca9eSBenno Rice 	    sc->sc_rxptr, rxcomp);
141218100346SThomas Moestl #endif
1413b2d59f42SThomas Moestl 	GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD);
1414336cca9eSBenno Rice 	for (i = sc->sc_rxptr; i != rxcomp;
141542c1b001SThomas Moestl 	     i = GEM_NEXTRX(i)) {
141642c1b001SThomas Moestl 		rxs = &sc->sc_rxsoft[i];
141742c1b001SThomas Moestl 
141842c1b001SThomas Moestl 		rxstat = GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags);
141942c1b001SThomas Moestl 
142042c1b001SThomas Moestl 		if (rxstat & GEM_RD_OWN) {
1421c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT
142242c1b001SThomas Moestl 			/*
14230d80b9bdSThomas Moestl 			 * The descriptor is still marked as owned, although
14240d80b9bdSThomas Moestl 			 * it is supposed to have completed. This has been
14250d80b9bdSThomas Moestl 			 * observed on some machines. Just exiting here
14260d80b9bdSThomas Moestl 			 * might leave the packet sitting around until another
14270d80b9bdSThomas Moestl 			 * one arrives to trigger a new interrupt, which is
14280d80b9bdSThomas Moestl 			 * generally undesirable, so set up a timeout.
142942c1b001SThomas Moestl 			 */
14300d80b9bdSThomas Moestl 			callout_reset(&sc->sc_rx_ch, GEM_RXOWN_TICKS,
14310d80b9bdSThomas Moestl 			    gem_rint_timeout, sc);
1432336cca9eSBenno Rice #endif
143342c1b001SThomas Moestl 			break;
143442c1b001SThomas Moestl 		}
143542c1b001SThomas Moestl 
1436336cca9eSBenno Rice 		progress++;
1437336cca9eSBenno Rice 		ifp->if_ipackets++;
1438336cca9eSBenno Rice 
143942c1b001SThomas Moestl 		if (rxstat & GEM_RD_BAD_CRC) {
1440336cca9eSBenno Rice 			ifp->if_ierrors++;
144142c1b001SThomas Moestl 			device_printf(sc->sc_dev, "receive error: CRC error\n");
144242c1b001SThomas Moestl 			GEM_INIT_RXDESC(sc, i);
144342c1b001SThomas Moestl 			continue;
144442c1b001SThomas Moestl 		}
144542c1b001SThomas Moestl 
144642c1b001SThomas Moestl #ifdef GEM_DEBUG
144742c1b001SThomas Moestl 		if (ifp->if_flags & IFF_DEBUG) {
144842c1b001SThomas Moestl 			printf("    rxsoft %p descriptor %d: ", rxs, i);
144942c1b001SThomas Moestl 			printf("gd_flags: 0x%016llx\t", (long long)
145042c1b001SThomas Moestl 				GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags));
145142c1b001SThomas Moestl 			printf("gd_addr: 0x%016llx\n", (long long)
145242c1b001SThomas Moestl 				GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_addr));
145342c1b001SThomas Moestl 		}
145442c1b001SThomas Moestl #endif
145542c1b001SThomas Moestl 
145642c1b001SThomas Moestl 		/*
145742c1b001SThomas Moestl 		 * No errors; receive the packet.  Note the Gem
145842c1b001SThomas Moestl 		 * includes the CRC with every packet.
145942c1b001SThomas Moestl 		 */
146042c1b001SThomas Moestl 		len = GEM_RD_BUFLEN(rxstat);
146142c1b001SThomas Moestl 
146242c1b001SThomas Moestl 		/*
146342c1b001SThomas Moestl 		 * Allocate a new mbuf cluster.  If that fails, we are
146442c1b001SThomas Moestl 		 * out of memory, and must drop the packet and recycle
146542c1b001SThomas Moestl 		 * the buffer that's already attached to this descriptor.
146642c1b001SThomas Moestl 		 */
146742c1b001SThomas Moestl 		m = rxs->rxs_mbuf;
146842c1b001SThomas Moestl 		if (gem_add_rxbuf(sc, i) != 0) {
146942c1b001SThomas Moestl 			ifp->if_ierrors++;
147042c1b001SThomas Moestl 			GEM_INIT_RXDESC(sc, i);
147142c1b001SThomas Moestl 			continue;
147242c1b001SThomas Moestl 		}
147342c1b001SThomas Moestl 		m->m_data += 2; /* We're already off by two */
147442c1b001SThomas Moestl 
147542c1b001SThomas Moestl 		m->m_pkthdr.rcvif = ifp;
147642c1b001SThomas Moestl 		m->m_pkthdr.len = m->m_len = len - ETHER_CRC_LEN;
147742c1b001SThomas Moestl 
147842c1b001SThomas Moestl 		/* Pass it on. */
14798cfaff7dSMarius Strobl 		GEM_UNLOCK(sc);
1480673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
14818cfaff7dSMarius Strobl 		GEM_LOCK(sc);
148242c1b001SThomas Moestl 	}
148342c1b001SThomas Moestl 
1484336cca9eSBenno Rice 	if (progress) {
1485b2d59f42SThomas Moestl 		GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE);
148642c1b001SThomas Moestl 		/* Update the receive pointer. */
1487336cca9eSBenno Rice 		if (i == sc->sc_rxptr) {
1488336cca9eSBenno Rice 			device_printf(sc->sc_dev, "rint: ring wrap\n");
1489336cca9eSBenno Rice 		}
149042c1b001SThomas Moestl 		sc->sc_rxptr = i;
1491336cca9eSBenno Rice 		bus_space_write_4(t, h, GEM_RX_KICK, GEM_PREVRX(i));
1492336cca9eSBenno Rice 	}
149342c1b001SThomas Moestl 
149418100346SThomas Moestl #ifdef GEM_DEBUG
149542c1b001SThomas Moestl 	CTR2(KTR_GEM, "gem_rint: done sc->rxptr %d, complete %d",
149642c1b001SThomas Moestl 		sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION));
149718100346SThomas Moestl #endif
149842c1b001SThomas Moestl }
149942c1b001SThomas Moestl 
150042c1b001SThomas Moestl 
150142c1b001SThomas Moestl /*
150242c1b001SThomas Moestl  * gem_add_rxbuf:
150342c1b001SThomas Moestl  *
150442c1b001SThomas Moestl  *	Add a receive buffer to the indicated descriptor.
150542c1b001SThomas Moestl  */
150642c1b001SThomas Moestl static int
150742c1b001SThomas Moestl gem_add_rxbuf(sc, idx)
150842c1b001SThomas Moestl 	struct gem_softc *sc;
150942c1b001SThomas Moestl 	int idx;
151042c1b001SThomas Moestl {
151142c1b001SThomas Moestl 	struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx];
151242c1b001SThomas Moestl 	struct mbuf *m;
1513c3d5598aSMarius Strobl 	bus_dma_segment_t segs[1];
1514c3d5598aSMarius Strobl 	int error, nsegs;
151542c1b001SThomas Moestl 
1516a163d034SWarner Losh 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
151742c1b001SThomas Moestl 	if (m == NULL)
151842c1b001SThomas Moestl 		return (ENOBUFS);
1519305f2c06SThomas Moestl 	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
152042c1b001SThomas Moestl 
152142c1b001SThomas Moestl #ifdef GEM_DEBUG
152242c1b001SThomas Moestl 	/* bzero the packet to check dma */
152342c1b001SThomas Moestl 	memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size);
152442c1b001SThomas Moestl #endif
152542c1b001SThomas Moestl 
1526b2d59f42SThomas Moestl 	if (rxs->rxs_mbuf != NULL) {
1527b2d59f42SThomas Moestl 		bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap,
1528b2d59f42SThomas Moestl 		    BUS_DMASYNC_POSTREAD);
1529305f2c06SThomas Moestl 		bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap);
1530b2d59f42SThomas Moestl 	}
153142c1b001SThomas Moestl 
153242c1b001SThomas Moestl 	rxs->rxs_mbuf = m;
153342c1b001SThomas Moestl 
1534c3d5598aSMarius Strobl 	error = bus_dmamap_load_mbuf_sg(sc->sc_rdmatag, rxs->rxs_dmamap,
1535c3d5598aSMarius Strobl 	    m, segs, &nsegs, BUS_DMA_NOWAIT);
1536c3d5598aSMarius Strobl 	/* If nsegs is wrong then the stack is corrupt. */
1537c3d5598aSMarius Strobl 	KASSERT(nsegs == 1, ("Too many segments returned!"));
1538c3d5598aSMarius Strobl 	if (error != 0) {
153942c1b001SThomas Moestl 		device_printf(sc->sc_dev, "can't load rx DMA map %d, error = "
154042c1b001SThomas Moestl 		    "%d\n", idx, error);
1541c3d5598aSMarius Strobl 		m_freem(m);
1542c3d5598aSMarius Strobl 		return (ENOBUFS);
154342c1b001SThomas Moestl 	}
1544c3d5598aSMarius Strobl 	rxs->rxs_paddr = segs[0].ds_addr;
154542c1b001SThomas Moestl 
1546305f2c06SThomas Moestl 	bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, BUS_DMASYNC_PREREAD);
154742c1b001SThomas Moestl 
154842c1b001SThomas Moestl 	GEM_INIT_RXDESC(sc, idx);
154942c1b001SThomas Moestl 
155042c1b001SThomas Moestl 	return (0);
155142c1b001SThomas Moestl }
155242c1b001SThomas Moestl 
155342c1b001SThomas Moestl 
155442c1b001SThomas Moestl static void
155542c1b001SThomas Moestl gem_eint(sc, status)
155642c1b001SThomas Moestl 	struct gem_softc *sc;
155742c1b001SThomas Moestl 	u_int status;
155842c1b001SThomas Moestl {
155942c1b001SThomas Moestl 
156042c1b001SThomas Moestl 	if ((status & GEM_INTR_MIF) != 0) {
156142c1b001SThomas Moestl 		device_printf(sc->sc_dev, "XXXlink status changed\n");
156242c1b001SThomas Moestl 		return;
156342c1b001SThomas Moestl 	}
156442c1b001SThomas Moestl 
156542c1b001SThomas Moestl 	device_printf(sc->sc_dev, "status=%x\n", status);
156642c1b001SThomas Moestl }
156742c1b001SThomas Moestl 
156842c1b001SThomas Moestl 
156942c1b001SThomas Moestl void
157042c1b001SThomas Moestl gem_intr(v)
157142c1b001SThomas Moestl 	void *v;
157242c1b001SThomas Moestl {
157342c1b001SThomas Moestl 	struct gem_softc *sc = (struct gem_softc *)v;
157442c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
157542c1b001SThomas Moestl 	bus_space_handle_t seb = sc->sc_h;
157642c1b001SThomas Moestl 	u_int32_t status;
157742c1b001SThomas Moestl 
15788cfaff7dSMarius Strobl 	GEM_LOCK(sc);
157942c1b001SThomas Moestl 	status = bus_space_read_4(t, seb, GEM_STATUS);
158018100346SThomas Moestl #ifdef GEM_DEBUG
158142c1b001SThomas Moestl 	CTR3(KTR_GEM, "%s: gem_intr: cplt %x, status %x",
158242c1b001SThomas Moestl 		device_get_name(sc->sc_dev), (status>>19),
158342c1b001SThomas Moestl 		(u_int)status);
158418100346SThomas Moestl #endif
158542c1b001SThomas Moestl 
158642c1b001SThomas Moestl 	if ((status & (GEM_INTR_RX_TAG_ERR | GEM_INTR_BERR)) != 0)
158742c1b001SThomas Moestl 		gem_eint(sc, status);
158842c1b001SThomas Moestl 
158942c1b001SThomas Moestl 	if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0)
159042c1b001SThomas Moestl 		gem_tint(sc);
159142c1b001SThomas Moestl 
159242c1b001SThomas Moestl 	if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0)
159342c1b001SThomas Moestl 		gem_rint(sc);
159442c1b001SThomas Moestl 
159542c1b001SThomas Moestl 	/* We should eventually do more than just print out error stats. */
159642c1b001SThomas Moestl 	if (status & GEM_INTR_TX_MAC) {
159742c1b001SThomas Moestl 		int txstat = bus_space_read_4(t, seb, GEM_MAC_TX_STATUS);
159842c1b001SThomas Moestl 		if (txstat & ~GEM_MAC_TX_XMIT_DONE)
1599336cca9eSBenno Rice 			device_printf(sc->sc_dev, "MAC tx fault, status %x\n",
1600336cca9eSBenno Rice 			    txstat);
16019bb711b9SThomas Moestl 		if (txstat & (GEM_MAC_TX_UNDERRUN | GEM_MAC_TX_PKT_TOO_LONG))
16028cfaff7dSMarius Strobl 			gem_init_locked(sc);
160342c1b001SThomas Moestl 	}
160442c1b001SThomas Moestl 	if (status & GEM_INTR_RX_MAC) {
160542c1b001SThomas Moestl 		int rxstat = bus_space_read_4(t, seb, GEM_MAC_RX_STATUS);
160600d12766SMarius Strobl 		/*
160700d12766SMarius Strobl 		 * On some chip revisions GEM_MAC_RX_OVERFLOW happen often
160800d12766SMarius Strobl 		 * due to a silicon bug so handle them silently.
160900d12766SMarius Strobl 		 */
161000d12766SMarius Strobl 		if (rxstat & GEM_MAC_RX_OVERFLOW)
161100d12766SMarius Strobl 			gem_init_locked(sc);
161200d12766SMarius Strobl 		else if (rxstat & ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT))
1613336cca9eSBenno Rice 			device_printf(sc->sc_dev, "MAC rx fault, status %x\n",
1614336cca9eSBenno Rice 			    rxstat);
161542c1b001SThomas Moestl 	}
16168cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
161742c1b001SThomas Moestl }
161842c1b001SThomas Moestl 
161942c1b001SThomas Moestl 
162042c1b001SThomas Moestl static void
162142c1b001SThomas Moestl gem_watchdog(ifp)
162242c1b001SThomas Moestl 	struct ifnet *ifp;
162342c1b001SThomas Moestl {
162442c1b001SThomas Moestl 	struct gem_softc *sc = ifp->if_softc;
162542c1b001SThomas Moestl 
16268cfaff7dSMarius Strobl 	GEM_LOCK(sc);
162718100346SThomas Moestl #ifdef GEM_DEBUG
162842c1b001SThomas Moestl 	CTR3(KTR_GEM, "gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x "
162942c1b001SThomas Moestl 		"GEM_MAC_RX_CONFIG %x",
163042c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_CONFIG),
163142c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_STATUS),
163242c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_CONFIG));
163342c1b001SThomas Moestl 	CTR3(KTR_GEM, "gem_watchdog: GEM_TX_CONFIG %x GEM_MAC_TX_STATUS %x "
163442c1b001SThomas Moestl 		"GEM_MAC_TX_CONFIG %x",
163542c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_CONFIG),
163642c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_TX_STATUS),
163742c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_TX_CONFIG));
163818100346SThomas Moestl #endif
163942c1b001SThomas Moestl 
164042c1b001SThomas Moestl 	device_printf(sc->sc_dev, "device timeout\n");
164142c1b001SThomas Moestl 	++ifp->if_oerrors;
164242c1b001SThomas Moestl 
164342c1b001SThomas Moestl 	/* Try to get more packets going. */
16448cfaff7dSMarius Strobl 	gem_init_locked(sc);
16458cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
164642c1b001SThomas Moestl }
164742c1b001SThomas Moestl 
164842c1b001SThomas Moestl /*
164942c1b001SThomas Moestl  * Initialize the MII Management Interface
165042c1b001SThomas Moestl  */
165142c1b001SThomas Moestl static void
165242c1b001SThomas Moestl gem_mifinit(sc)
165342c1b001SThomas Moestl 	struct gem_softc *sc;
165442c1b001SThomas Moestl {
165542c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
165642c1b001SThomas Moestl 	bus_space_handle_t mif = sc->sc_h;
165742c1b001SThomas Moestl 
16588cfaff7dSMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
16598cfaff7dSMarius Strobl 
166042c1b001SThomas Moestl 	/* Configure the MIF in frame mode */
166142c1b001SThomas Moestl 	sc->sc_mif_config = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
166242c1b001SThomas Moestl 	sc->sc_mif_config &= ~GEM_MIF_CONFIG_BB_ENA;
166342c1b001SThomas Moestl 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, sc->sc_mif_config);
166442c1b001SThomas Moestl }
166542c1b001SThomas Moestl 
166642c1b001SThomas Moestl /*
166742c1b001SThomas Moestl  * MII interface
166842c1b001SThomas Moestl  *
166942c1b001SThomas Moestl  * The GEM MII interface supports at least three different operating modes:
167042c1b001SThomas Moestl  *
167142c1b001SThomas Moestl  * Bitbang mode is implemented using data, clock and output enable registers.
167242c1b001SThomas Moestl  *
167342c1b001SThomas Moestl  * Frame mode is implemented by loading a complete frame into the frame
167442c1b001SThomas Moestl  * register and polling the valid bit for completion.
167542c1b001SThomas Moestl  *
167642c1b001SThomas Moestl  * Polling mode uses the frame register but completion is indicated by
167742c1b001SThomas Moestl  * an interrupt.
167842c1b001SThomas Moestl  *
167942c1b001SThomas Moestl  */
168042c1b001SThomas Moestl int
168142c1b001SThomas Moestl gem_mii_readreg(dev, phy, reg)
168242c1b001SThomas Moestl 	device_t dev;
168342c1b001SThomas Moestl 	int phy, reg;
168442c1b001SThomas Moestl {
168542c1b001SThomas Moestl 	struct gem_softc *sc = device_get_softc(dev);
168642c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
168742c1b001SThomas Moestl 	bus_space_handle_t mif = sc->sc_h;
168842c1b001SThomas Moestl 	int n;
168942c1b001SThomas Moestl 	u_int32_t v;
169042c1b001SThomas Moestl 
16918cfaff7dSMarius Strobl 	GEM_LOCK(sc);
169242c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY
169342c1b001SThomas Moestl 	printf("gem_mii_readreg: phy %d reg %d\n", phy, reg);
169442c1b001SThomas Moestl #endif
169542c1b001SThomas Moestl 
169642c1b001SThomas Moestl #if 0
169742c1b001SThomas Moestl 	/* Select the desired PHY in the MIF configuration register */
169842c1b001SThomas Moestl 	v = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
169942c1b001SThomas Moestl 	/* Clear PHY select bit */
170042c1b001SThomas Moestl 	v &= ~GEM_MIF_CONFIG_PHY_SEL;
170142c1b001SThomas Moestl 	if (phy == GEM_PHYAD_EXTERNAL)
170242c1b001SThomas Moestl 		/* Set PHY select bit to get at external device */
170342c1b001SThomas Moestl 		v |= GEM_MIF_CONFIG_PHY_SEL;
170442c1b001SThomas Moestl 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, v);
170542c1b001SThomas Moestl #endif
170642c1b001SThomas Moestl 
170742c1b001SThomas Moestl 	/* Construct the frame command */
170842c1b001SThomas Moestl 	v = (reg << GEM_MIF_REG_SHIFT)	| (phy << GEM_MIF_PHY_SHIFT) |
170942c1b001SThomas Moestl 		GEM_MIF_FRAME_READ;
171042c1b001SThomas Moestl 
171142c1b001SThomas Moestl 	bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
171242c1b001SThomas Moestl 	for (n = 0; n < 100; n++) {
171342c1b001SThomas Moestl 		DELAY(1);
171442c1b001SThomas Moestl 		v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
17158cfaff7dSMarius Strobl 		if (v & GEM_MIF_FRAME_TA0) {
17168cfaff7dSMarius Strobl 			GEM_UNLOCK(sc);
171742c1b001SThomas Moestl 			return (v & GEM_MIF_FRAME_DATA);
171842c1b001SThomas Moestl 		}
17198cfaff7dSMarius Strobl 	}
172042c1b001SThomas Moestl 
172142c1b001SThomas Moestl 	device_printf(sc->sc_dev, "mii_read timeout\n");
17228cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
172342c1b001SThomas Moestl 	return (0);
172442c1b001SThomas Moestl }
172542c1b001SThomas Moestl 
172642c1b001SThomas Moestl int
172742c1b001SThomas Moestl gem_mii_writereg(dev, phy, reg, val)
172842c1b001SThomas Moestl 	device_t dev;
172942c1b001SThomas Moestl 	int phy, reg, val;
173042c1b001SThomas Moestl {
173142c1b001SThomas Moestl 	struct gem_softc *sc = device_get_softc(dev);
173242c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
173342c1b001SThomas Moestl 	bus_space_handle_t mif = sc->sc_h;
173442c1b001SThomas Moestl 	int n;
173542c1b001SThomas Moestl 	u_int32_t v;
173642c1b001SThomas Moestl 
17378cfaff7dSMarius Strobl 	GEM_LOCK(sc);
173842c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY
173942c1b001SThomas Moestl 	printf("gem_mii_writereg: phy %d reg %d val %x\n", phy, reg, val);
174042c1b001SThomas Moestl #endif
174142c1b001SThomas Moestl 
174242c1b001SThomas Moestl #if 0
174342c1b001SThomas Moestl 	/* Select the desired PHY in the MIF configuration register */
174442c1b001SThomas Moestl 	v = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
174542c1b001SThomas Moestl 	/* Clear PHY select bit */
174642c1b001SThomas Moestl 	v &= ~GEM_MIF_CONFIG_PHY_SEL;
174742c1b001SThomas Moestl 	if (phy == GEM_PHYAD_EXTERNAL)
174842c1b001SThomas Moestl 		/* Set PHY select bit to get at external device */
174942c1b001SThomas Moestl 		v |= GEM_MIF_CONFIG_PHY_SEL;
175042c1b001SThomas Moestl 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, v);
175142c1b001SThomas Moestl #endif
175242c1b001SThomas Moestl 	/* Construct the frame command */
175342c1b001SThomas Moestl 	v = GEM_MIF_FRAME_WRITE			|
175442c1b001SThomas Moestl 	    (phy << GEM_MIF_PHY_SHIFT)		|
175542c1b001SThomas Moestl 	    (reg << GEM_MIF_REG_SHIFT)		|
175642c1b001SThomas Moestl 	    (val & GEM_MIF_FRAME_DATA);
175742c1b001SThomas Moestl 
175842c1b001SThomas Moestl 	bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
175942c1b001SThomas Moestl 	for (n = 0; n < 100; n++) {
176042c1b001SThomas Moestl 		DELAY(1);
176142c1b001SThomas Moestl 		v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
17628cfaff7dSMarius Strobl 		if (v & GEM_MIF_FRAME_TA0) {
17638cfaff7dSMarius Strobl 			GEM_UNLOCK(sc);
176442c1b001SThomas Moestl 			return (1);
176542c1b001SThomas Moestl 		}
17668cfaff7dSMarius Strobl 	}
176742c1b001SThomas Moestl 
176842c1b001SThomas Moestl 	device_printf(sc->sc_dev, "mii_write timeout\n");
17698cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
177042c1b001SThomas Moestl 	return (0);
177142c1b001SThomas Moestl }
177242c1b001SThomas Moestl 
177342c1b001SThomas Moestl void
177442c1b001SThomas Moestl gem_mii_statchg(dev)
177542c1b001SThomas Moestl 	device_t dev;
177642c1b001SThomas Moestl {
177742c1b001SThomas Moestl 	struct gem_softc *sc = device_get_softc(dev);
177842c1b001SThomas Moestl #ifdef GEM_DEBUG
17798cfaff7dSMarius Strobl 	int instance;
178042c1b001SThomas Moestl #endif
178142c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
178242c1b001SThomas Moestl 	bus_space_handle_t mac = sc->sc_h;
178342c1b001SThomas Moestl 	u_int32_t v;
178442c1b001SThomas Moestl 
17858cfaff7dSMarius Strobl 	GEM_LOCK(sc);
178642c1b001SThomas Moestl #ifdef GEM_DEBUG
17878cfaff7dSMarius Strobl 	instance = IFM_INST(sc->sc_mii->mii_media.ifm_cur->ifm_media);
178842c1b001SThomas Moestl 	if (sc->sc_debug)
178942c1b001SThomas Moestl 		printf("gem_mii_statchg: status change: phy = %d\n",
179042c1b001SThomas Moestl 			sc->sc_phys[instance]);
179142c1b001SThomas Moestl #endif
179242c1b001SThomas Moestl 
179342c1b001SThomas Moestl 	/* Set tx full duplex options */
179442c1b001SThomas Moestl 	bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, 0);
179542c1b001SThomas Moestl 	DELAY(10000); /* reg must be cleared and delay before changing. */
179642c1b001SThomas Moestl 	v = GEM_MAC_TX_ENA_IPG0|GEM_MAC_TX_NGU|GEM_MAC_TX_NGU_LIMIT|
179742c1b001SThomas Moestl 		GEM_MAC_TX_ENABLE;
179842c1b001SThomas Moestl 	if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) {
179942c1b001SThomas Moestl 		v |= GEM_MAC_TX_IGN_CARRIER|GEM_MAC_TX_IGN_COLLIS;
180042c1b001SThomas Moestl 	}
180142c1b001SThomas Moestl 	bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, v);
180242c1b001SThomas Moestl 
180342c1b001SThomas Moestl 	/* XIF Configuration */
180442c1b001SThomas Moestl 	v = GEM_MAC_XIF_LINK_LED;
180542c1b001SThomas Moestl 	v |= GEM_MAC_XIF_TX_MII_ENA;
1806336cca9eSBenno Rice 
180742c1b001SThomas Moestl 	/* If an external transceiver is connected, enable its MII drivers */
180842c1b001SThomas Moestl 	sc->sc_mif_config = bus_space_read_4(t, mac, GEM_MIF_CONFIG);
180942c1b001SThomas Moestl 	if ((sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) != 0) {
181042c1b001SThomas Moestl 		/* External MII needs echo disable if half duplex. */
181142c1b001SThomas Moestl 		if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0)
181242c1b001SThomas Moestl 			/* turn on full duplex LED */
181342c1b001SThomas Moestl 			v |= GEM_MAC_XIF_FDPLX_LED;
181442c1b001SThomas Moestl 		else
181542c1b001SThomas Moestl 	 		/* half duplex -- disable echo */
181642c1b001SThomas Moestl 	 		v |= GEM_MAC_XIF_ECHO_DISABL;
1817336cca9eSBenno Rice 
1818336cca9eSBenno Rice 		if (IFM_SUBTYPE(sc->sc_mii->mii_media_active) == IFM_1000_T)
1819336cca9eSBenno Rice 			v |= GEM_MAC_XIF_GMII_MODE;
1820336cca9eSBenno Rice 		else
1821336cca9eSBenno Rice 			v &= ~GEM_MAC_XIF_GMII_MODE;
182242c1b001SThomas Moestl 	} else {
182342c1b001SThomas Moestl 		/* Internal MII needs buf enable */
182442c1b001SThomas Moestl 		v |= GEM_MAC_XIF_MII_BUF_ENA;
182542c1b001SThomas Moestl 	}
182642c1b001SThomas Moestl 	bus_space_write_4(t, mac, GEM_MAC_XIF_CONFIG, v);
18278cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
182842c1b001SThomas Moestl }
182942c1b001SThomas Moestl 
183042c1b001SThomas Moestl int
183142c1b001SThomas Moestl gem_mediachange(ifp)
183242c1b001SThomas Moestl 	struct ifnet *ifp;
183342c1b001SThomas Moestl {
183442c1b001SThomas Moestl 	struct gem_softc *sc = ifp->if_softc;
183542c1b001SThomas Moestl 
183642c1b001SThomas Moestl 	/* XXX Add support for serial media. */
183742c1b001SThomas Moestl 
183842c1b001SThomas Moestl 	return (mii_mediachg(sc->sc_mii));
183942c1b001SThomas Moestl }
184042c1b001SThomas Moestl 
184142c1b001SThomas Moestl void
184242c1b001SThomas Moestl gem_mediastatus(ifp, ifmr)
184342c1b001SThomas Moestl 	struct ifnet *ifp;
184442c1b001SThomas Moestl 	struct ifmediareq *ifmr;
184542c1b001SThomas Moestl {
184642c1b001SThomas Moestl 	struct gem_softc *sc = ifp->if_softc;
184742c1b001SThomas Moestl 
18488cfaff7dSMarius Strobl 	GEM_LOCK(sc);
18498cfaff7dSMarius Strobl 	if ((ifp->if_flags & IFF_UP) == 0) {
18508cfaff7dSMarius Strobl 		GEM_UNLOCK(sc);
185142c1b001SThomas Moestl 		return;
18528cfaff7dSMarius Strobl 	}
185342c1b001SThomas Moestl 
18548cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
185542c1b001SThomas Moestl 	mii_pollstat(sc->sc_mii);
18568cfaff7dSMarius Strobl 	GEM_LOCK(sc);
185742c1b001SThomas Moestl 	ifmr->ifm_active = sc->sc_mii->mii_media_active;
185842c1b001SThomas Moestl 	ifmr->ifm_status = sc->sc_mii->mii_media_status;
18598cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
186042c1b001SThomas Moestl }
186142c1b001SThomas Moestl 
186242c1b001SThomas Moestl /*
186342c1b001SThomas Moestl  * Process an ioctl request.
186442c1b001SThomas Moestl  */
186542c1b001SThomas Moestl static int
186642c1b001SThomas Moestl gem_ioctl(ifp, cmd, data)
186742c1b001SThomas Moestl 	struct ifnet *ifp;
186842c1b001SThomas Moestl 	u_long cmd;
186942c1b001SThomas Moestl 	caddr_t data;
187042c1b001SThomas Moestl {
187142c1b001SThomas Moestl 	struct gem_softc *sc = ifp->if_softc;
187242c1b001SThomas Moestl 	struct ifreq *ifr = (struct ifreq *)data;
18738cfaff7dSMarius Strobl 	int error = 0;
18748cfaff7dSMarius Strobl 
18758cfaff7dSMarius Strobl 	GEM_LOCK(sc);
187642c1b001SThomas Moestl 
187742c1b001SThomas Moestl 	switch (cmd) {
187842c1b001SThomas Moestl 	case SIOCSIFADDR:
187942c1b001SThomas Moestl 	case SIOCGIFADDR:
188042c1b001SThomas Moestl 	case SIOCSIFMTU:
18818cfaff7dSMarius Strobl 		GEM_UNLOCK(sc);
188242c1b001SThomas Moestl 		error = ether_ioctl(ifp, cmd, data);
18838cfaff7dSMarius Strobl 		GEM_LOCK(sc);
188442c1b001SThomas Moestl 		break;
188542c1b001SThomas Moestl 	case SIOCSIFFLAGS:
188642c1b001SThomas Moestl 		if (ifp->if_flags & IFF_UP) {
1887336cca9eSBenno Rice 			if ((sc->sc_ifflags ^ ifp->if_flags) == IFF_PROMISC)
188842c1b001SThomas Moestl 				gem_setladrf(sc);
188942c1b001SThomas Moestl 			else
18908cfaff7dSMarius Strobl 				gem_init_locked(sc);
189142c1b001SThomas Moestl 		} else {
189213f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
189342c1b001SThomas Moestl 				gem_stop(ifp, 0);
189442c1b001SThomas Moestl 		}
1895336cca9eSBenno Rice 		sc->sc_ifflags = ifp->if_flags;
189642c1b001SThomas Moestl 		error = 0;
189742c1b001SThomas Moestl 		break;
189842c1b001SThomas Moestl 	case SIOCADDMULTI:
189942c1b001SThomas Moestl 	case SIOCDELMULTI:
190042c1b001SThomas Moestl 		gem_setladrf(sc);
190142c1b001SThomas Moestl 		error = 0;
190242c1b001SThomas Moestl 		break;
190342c1b001SThomas Moestl 	case SIOCGIFMEDIA:
190442c1b001SThomas Moestl 	case SIOCSIFMEDIA:
19058cfaff7dSMarius Strobl 		GEM_UNLOCK(sc);
190642c1b001SThomas Moestl 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, cmd);
19078cfaff7dSMarius Strobl 		GEM_LOCK(sc);
190842c1b001SThomas Moestl 		break;
190942c1b001SThomas Moestl 	default:
1910305f2c06SThomas Moestl 		error = ENOTTY;
191142c1b001SThomas Moestl 		break;
191242c1b001SThomas Moestl 	}
191342c1b001SThomas Moestl 
191442c1b001SThomas Moestl 	/* Try to get things going again */
191542c1b001SThomas Moestl 	if (ifp->if_flags & IFF_UP)
19168cfaff7dSMarius Strobl 		gem_start_locked(ifp);
19178cfaff7dSMarius Strobl 	GEM_UNLOCK(sc);
191842c1b001SThomas Moestl 	return (error);
191942c1b001SThomas Moestl }
192042c1b001SThomas Moestl 
192142c1b001SThomas Moestl /*
192242c1b001SThomas Moestl  * Set up the logical address filter.
192342c1b001SThomas Moestl  */
192442c1b001SThomas Moestl static void
192542c1b001SThomas Moestl gem_setladrf(sc)
192642c1b001SThomas Moestl 	struct gem_softc *sc;
192742c1b001SThomas Moestl {
1928fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->sc_ifp;
192942c1b001SThomas Moestl 	struct ifmultiaddr *inm;
193042c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
193142c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
193242c1b001SThomas Moestl 	u_int32_t crc;
193342c1b001SThomas Moestl 	u_int32_t hash[16];
193442c1b001SThomas Moestl 	u_int32_t v;
1935336cca9eSBenno Rice 	int i;
193642c1b001SThomas Moestl 
19378cfaff7dSMarius Strobl 	GEM_LOCK_ASSERT(sc, MA_OWNED);
19388cfaff7dSMarius Strobl 
193942c1b001SThomas Moestl 	/* Get current RX configuration */
194042c1b001SThomas Moestl 	v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
194142c1b001SThomas Moestl 
1942336cca9eSBenno Rice 	/*
1943336cca9eSBenno Rice 	 * Turn off promiscuous mode, promiscuous group mode (all multicast),
1944336cca9eSBenno Rice 	 * and hash filter.  Depending on the case, the right bit will be
1945336cca9eSBenno Rice 	 * enabled.
1946336cca9eSBenno Rice 	 */
1947336cca9eSBenno Rice 	v &= ~(GEM_MAC_RX_PROMISCUOUS|GEM_MAC_RX_HASH_FILTER|
1948336cca9eSBenno Rice 	    GEM_MAC_RX_PROMISC_GRP);
1949336cca9eSBenno Rice 
195042c1b001SThomas Moestl 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
1951336cca9eSBenno Rice 		/* Turn on promiscuous mode */
195242c1b001SThomas Moestl 		v |= GEM_MAC_RX_PROMISCUOUS;
195342c1b001SThomas Moestl 		goto chipit;
195442c1b001SThomas Moestl 	}
195542c1b001SThomas Moestl 	if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
195642c1b001SThomas Moestl 		hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
195742c1b001SThomas Moestl 		ifp->if_flags |= IFF_ALLMULTI;
1958336cca9eSBenno Rice 		v |= GEM_MAC_RX_PROMISC_GRP;
195942c1b001SThomas Moestl 		goto chipit;
196042c1b001SThomas Moestl 	}
196142c1b001SThomas Moestl 
196242c1b001SThomas Moestl 	/*
196342c1b001SThomas Moestl 	 * Set up multicast address filter by passing all multicast addresses
1964336cca9eSBenno Rice 	 * through a crc generator, and then using the high order 8 bits as an
1965336cca9eSBenno Rice 	 * index into the 256 bit logical address filter.  The high order 4
1966336cca9eSBenno Rice 	 * bits selects the word, while the other 4 bits select the bit within
1967336cca9eSBenno Rice 	 * the word (where bit 0 is the MSB).
196842c1b001SThomas Moestl 	 */
196942c1b001SThomas Moestl 
1970336cca9eSBenno Rice 	/* Clear hash table */
1971336cca9eSBenno Rice 	memset(hash, 0, sizeof(hash));
1972336cca9eSBenno Rice 
197313b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
1974fc74a9f9SBrooks Davis 	TAILQ_FOREACH(inm, &ifp->if_multiaddrs, ifma_link) {
197542c1b001SThomas Moestl 		if (inm->ifma_addr->sa_family != AF_LINK)
197642c1b001SThomas Moestl 			continue;
1977c240bd8cSMarius Strobl 		crc = ether_crc32_le(LLADDR((struct sockaddr_dl *)
1978c240bd8cSMarius Strobl 		    inm->ifma_addr), ETHER_ADDR_LEN);
197942c1b001SThomas Moestl 
198042c1b001SThomas Moestl 		/* Just want the 8 most significant bits. */
198142c1b001SThomas Moestl 		crc >>= 24;
198242c1b001SThomas Moestl 
198342c1b001SThomas Moestl 		/* Set the corresponding bit in the filter. */
1984336cca9eSBenno Rice 		hash[crc >> 4] |= 1 << (15 - (crc & 15));
1985336cca9eSBenno Rice 	}
198613b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
1987336cca9eSBenno Rice 
1988336cca9eSBenno Rice 	v |= GEM_MAC_RX_HASH_FILTER;
1989336cca9eSBenno Rice 	ifp->if_flags &= ~IFF_ALLMULTI;
1990336cca9eSBenno Rice 
1991336cca9eSBenno Rice 	/* Now load the hash table into the chip (if we are using it) */
1992336cca9eSBenno Rice 	for (i = 0; i < 16; i++) {
1993336cca9eSBenno Rice 		bus_space_write_4(t, h,
1994336cca9eSBenno Rice 		    GEM_MAC_HASH0 + i * (GEM_MAC_HASH1-GEM_MAC_HASH0),
1995336cca9eSBenno Rice 		    hash[i]);
199642c1b001SThomas Moestl 	}
199742c1b001SThomas Moestl 
199842c1b001SThomas Moestl chipit:
199942c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
200042c1b001SThomas Moestl }
2001