xref: /freebsd/sys/dev/gem/if_gem.c (revision 08e0fdeb392eaf35f554dcfaed15a97da66dde02)
142c1b001SThomas Moestl /*
242c1b001SThomas Moestl  * Copyright (C) 2001 Eduardo Horvath.
342c1b001SThomas Moestl  * All rights reserved.
442c1b001SThomas Moestl  *
542c1b001SThomas Moestl  * Redistribution and use in source and binary forms, with or without
642c1b001SThomas Moestl  * modification, are permitted provided that the following conditions
742c1b001SThomas Moestl  * are met:
842c1b001SThomas Moestl  * 1. Redistributions of source code must retain the above copyright
942c1b001SThomas Moestl  *    notice, this list of conditions and the following disclaimer.
1042c1b001SThomas Moestl  * 2. Redistributions in binary form must reproduce the above copyright
1142c1b001SThomas Moestl  *    notice, this list of conditions and the following disclaimer in the
1242c1b001SThomas Moestl  *    documentation and/or other materials provided with the distribution.
1342c1b001SThomas Moestl  *
1442c1b001SThomas Moestl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
1542c1b001SThomas Moestl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1642c1b001SThomas Moestl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1742c1b001SThomas Moestl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
1842c1b001SThomas Moestl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1942c1b001SThomas Moestl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2042c1b001SThomas Moestl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2142c1b001SThomas Moestl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2242c1b001SThomas Moestl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2342c1b001SThomas Moestl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2442c1b001SThomas Moestl  * SUCH DAMAGE.
2542c1b001SThomas Moestl  *
26336cca9eSBenno Rice  *	from: NetBSD: gem.c,v 1.21 2002/06/01 23:50:58 lukem Exp
2742c1b001SThomas Moestl  *
2842c1b001SThomas Moestl  * $FreeBSD$
2942c1b001SThomas Moestl  */
3042c1b001SThomas Moestl 
3142c1b001SThomas Moestl /*
3242c1b001SThomas Moestl  * Driver for Sun GEM ethernet controllers.
3342c1b001SThomas Moestl  */
3442c1b001SThomas Moestl 
3542c1b001SThomas Moestl #define	GEM_DEBUG
3642c1b001SThomas Moestl 
3742c1b001SThomas Moestl #include <sys/param.h>
3842c1b001SThomas Moestl #include <sys/systm.h>
3942c1b001SThomas Moestl #include <sys/bus.h>
4042c1b001SThomas Moestl #include <sys/callout.h>
41a30d4b32SMike Barcroft #include <sys/endian.h>
4242c1b001SThomas Moestl #include <sys/mbuf.h>
4342c1b001SThomas Moestl #include <sys/malloc.h>
4442c1b001SThomas Moestl #include <sys/kernel.h>
4542c1b001SThomas Moestl #include <sys/socket.h>
4642c1b001SThomas Moestl #include <sys/sockio.h>
4742c1b001SThomas Moestl 
4808e0fdebSThomas Moestl #include <net/bpf.h>
4942c1b001SThomas Moestl #include <net/ethernet.h>
5042c1b001SThomas Moestl #include <net/if.h>
5142c1b001SThomas Moestl #include <net/if_arp.h>
5242c1b001SThomas Moestl #include <net/if_dl.h>
5342c1b001SThomas Moestl #include <net/if_media.h>
5442c1b001SThomas Moestl 
5542c1b001SThomas Moestl #include <machine/bus.h>
5642c1b001SThomas Moestl 
5742c1b001SThomas Moestl #include <dev/mii/mii.h>
5842c1b001SThomas Moestl #include <dev/mii/miivar.h>
5942c1b001SThomas Moestl 
6042c1b001SThomas Moestl #include <gem/if_gemreg.h>
6142c1b001SThomas Moestl #include <gem/if_gemvar.h>
6242c1b001SThomas Moestl 
6342c1b001SThomas Moestl #define TRIES	10000
6442c1b001SThomas Moestl 
65e51a25f8SAlfred Perlstein static void	gem_start(struct ifnet *);
66e51a25f8SAlfred Perlstein static void	gem_stop(struct ifnet *, int);
67e51a25f8SAlfred Perlstein static int	gem_ioctl(struct ifnet *, u_long, caddr_t);
68e51a25f8SAlfred Perlstein static void	gem_cddma_callback(void *, bus_dma_segment_t *, int, int);
69e51a25f8SAlfred Perlstein static void	gem_rxdma_callback(void *, bus_dma_segment_t *, int, int);
70e51a25f8SAlfred Perlstein static void	gem_txdma_callback(void *, bus_dma_segment_t *, int, int);
71e51a25f8SAlfred Perlstein static void	gem_tick(void *);
72e51a25f8SAlfred Perlstein static void	gem_watchdog(struct ifnet *);
73e51a25f8SAlfred Perlstein static void	gem_init(void *);
74e51a25f8SAlfred Perlstein static void	gem_init_regs(struct gem_softc *sc);
75e51a25f8SAlfred Perlstein static int	gem_ringsize(int sz);
76e51a25f8SAlfred Perlstein static int	gem_meminit(struct gem_softc *);
77e51a25f8SAlfred Perlstein static int	gem_dmamap_load_mbuf(struct gem_softc *, struct mbuf *,
78e51a25f8SAlfred Perlstein     bus_dmamap_callback_t *,  struct gem_txjob *, int);
79e51a25f8SAlfred Perlstein static void	gem_dmamap_unload_mbuf(struct gem_softc *, struct gem_txjob *);
80e51a25f8SAlfred Perlstein static void	gem_dmamap_commit_mbuf(struct gem_softc *, struct gem_txjob *);
81e51a25f8SAlfred Perlstein static void	gem_mifinit(struct gem_softc *);
82e51a25f8SAlfred Perlstein static int	gem_bitwait(struct gem_softc *sc, bus_addr_t r,
83e51a25f8SAlfred Perlstein     u_int32_t clr, u_int32_t set);
84e51a25f8SAlfred Perlstein static int	gem_reset_rx(struct gem_softc *);
85e51a25f8SAlfred Perlstein static int	gem_reset_tx(struct gem_softc *);
86e51a25f8SAlfred Perlstein static int	gem_disable_rx(struct gem_softc *);
87e51a25f8SAlfred Perlstein static int	gem_disable_tx(struct gem_softc *);
88e51a25f8SAlfred Perlstein static void	gem_rxdrain(struct gem_softc *);
89e51a25f8SAlfred Perlstein static int	gem_add_rxbuf(struct gem_softc *, int);
90e51a25f8SAlfred Perlstein static void	gem_setladrf(struct gem_softc *);
9142c1b001SThomas Moestl 
92e51a25f8SAlfred Perlstein struct mbuf	*gem_get(struct gem_softc *, int, int);
93e51a25f8SAlfred Perlstein static void	gem_eint(struct gem_softc *, u_int);
94e51a25f8SAlfred Perlstein static void	gem_rint(struct gem_softc *);
9511e3f060SJake Burkholder #if 0
960d80b9bdSThomas Moestl static void	gem_rint_timeout(void *);
9711e3f060SJake Burkholder #endif
98e51a25f8SAlfred Perlstein static void	gem_tint(struct gem_softc *);
9942c1b001SThomas Moestl #ifdef notyet
100e51a25f8SAlfred Perlstein static void	gem_power(int, void *);
10142c1b001SThomas Moestl #endif
10242c1b001SThomas Moestl 
10342c1b001SThomas Moestl devclass_t gem_devclass;
10442c1b001SThomas Moestl DRIVER_MODULE(miibus, gem, miibus_driver, miibus_devclass, 0, 0);
10542c1b001SThomas Moestl MODULE_DEPEND(gem, miibus, 1, 1, 1);
10642c1b001SThomas Moestl 
10742c1b001SThomas Moestl #ifdef GEM_DEBUG
10842c1b001SThomas Moestl #define	DPRINTF(sc, x)	if ((sc)->sc_arpcom.ac_if.if_flags & IFF_DEBUG) \
10942c1b001SThomas Moestl 				printf x
11042c1b001SThomas Moestl #include <sys/ktr.h>
11142c1b001SThomas Moestl #define	KTR_GEM		KTR_CT2
11242c1b001SThomas Moestl #else
11342c1b001SThomas Moestl #define	DPRINTF(sc, x)	/* nothing */
11442c1b001SThomas Moestl #endif
11542c1b001SThomas Moestl 
11642c1b001SThomas Moestl #define	GEM_NSEGS GEM_NTXSEGS
11742c1b001SThomas Moestl 
11842c1b001SThomas Moestl /*
11942c1b001SThomas Moestl  * gem_attach:
12042c1b001SThomas Moestl  *
12142c1b001SThomas Moestl  *	Attach a Gem interface to the system.
12242c1b001SThomas Moestl  */
12342c1b001SThomas Moestl int
12442c1b001SThomas Moestl gem_attach(sc)
12542c1b001SThomas Moestl 	struct gem_softc *sc;
12642c1b001SThomas Moestl {
12742c1b001SThomas Moestl 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
12842c1b001SThomas Moestl 	struct mii_softc *child;
12942c1b001SThomas Moestl 	int i, error;
130336cca9eSBenno Rice 	u_int32_t v;
13142c1b001SThomas Moestl 
13242c1b001SThomas Moestl 	/* Make sure the chip is stopped. */
13342c1b001SThomas Moestl 	ifp->if_softc = sc;
13442c1b001SThomas Moestl 	gem_reset(sc);
13542c1b001SThomas Moestl 
13642c1b001SThomas Moestl 	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
13742c1b001SThomas Moestl 	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, GEM_NSEGS,
13842c1b001SThomas Moestl 	    BUS_SPACE_MAXSIZE_32BIT, 0, &sc->sc_pdmatag);
13942c1b001SThomas Moestl 	if (error)
14042c1b001SThomas Moestl 		return (error);
14142c1b001SThomas Moestl 
14242c1b001SThomas Moestl 	error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0,
14342c1b001SThomas Moestl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MAXBSIZE,
14442c1b001SThomas Moestl 	    GEM_NSEGS, BUS_SPACE_MAXSIZE_32BIT, BUS_DMA_ALLOCNOW,
14542c1b001SThomas Moestl 	    &sc->sc_dmatag);
14642c1b001SThomas Moestl 	if (error)
14742c1b001SThomas Moestl 		goto fail_0;
14842c1b001SThomas Moestl 
14942c1b001SThomas Moestl 	error = bus_dma_tag_create(sc->sc_pdmatag, PAGE_SIZE, 0,
15042c1b001SThomas Moestl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
15142c1b001SThomas Moestl 	    sizeof(struct gem_control_data), 1,
15242c1b001SThomas Moestl 	    sizeof(struct gem_control_data), BUS_DMA_ALLOCNOW,
15342c1b001SThomas Moestl 	    &sc->sc_cdmatag);
15442c1b001SThomas Moestl 	if (error)
15542c1b001SThomas Moestl 		goto fail_1;
15642c1b001SThomas Moestl 
15742c1b001SThomas Moestl 	/*
15842c1b001SThomas Moestl 	 * Allocate the control data structures, and create and load the
15942c1b001SThomas Moestl 	 * DMA map for it.
16042c1b001SThomas Moestl 	 */
16142c1b001SThomas Moestl 	if ((error = bus_dmamem_alloc(sc->sc_cdmatag,
16242c1b001SThomas Moestl 	    (void **)&sc->sc_control_data, 0, &sc->sc_cddmamap))) {
16342c1b001SThomas Moestl 		device_printf(sc->sc_dev, "unable to allocate control data,"
16442c1b001SThomas Moestl 		    " error = %d\n", error);
16542c1b001SThomas Moestl 		goto fail_2;
16642c1b001SThomas Moestl 	}
16742c1b001SThomas Moestl 
16842c1b001SThomas Moestl 	sc->sc_cddma = 0;
16942c1b001SThomas Moestl 	if ((error = bus_dmamap_load(sc->sc_cdmatag, sc->sc_cddmamap,
17042c1b001SThomas Moestl 	    sc->sc_control_data, sizeof(struct gem_control_data),
17142c1b001SThomas Moestl 	    gem_cddma_callback, sc, 0)) != 0 || sc->sc_cddma == 0) {
17242c1b001SThomas Moestl 		device_printf(sc->sc_dev, "unable to load control data DMA "
17342c1b001SThomas Moestl 		    "map, error = %d\n", error);
17442c1b001SThomas Moestl 		goto fail_3;
17542c1b001SThomas Moestl 	}
17642c1b001SThomas Moestl 
17742c1b001SThomas Moestl 	/*
17842c1b001SThomas Moestl 	 * Initialize the transmit job descriptors.
17942c1b001SThomas Moestl 	 */
18042c1b001SThomas Moestl 	STAILQ_INIT(&sc->sc_txfreeq);
18142c1b001SThomas Moestl 	STAILQ_INIT(&sc->sc_txdirtyq);
18242c1b001SThomas Moestl 
18342c1b001SThomas Moestl 	/*
18442c1b001SThomas Moestl 	 * Create the transmit buffer DMA maps.
18542c1b001SThomas Moestl 	 */
18642c1b001SThomas Moestl 	error = ENOMEM;
18742c1b001SThomas Moestl 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
18842c1b001SThomas Moestl 		struct gem_txsoft *txs;
18942c1b001SThomas Moestl 
19042c1b001SThomas Moestl 		txs = &sc->sc_txsoft[i];
19142c1b001SThomas Moestl 		txs->txs_mbuf = NULL;
19242c1b001SThomas Moestl 		txs->txs_ndescs = 0;
19342c1b001SThomas Moestl 		if ((error = bus_dmamap_create(sc->sc_dmatag, 0,
19442c1b001SThomas Moestl 		    &txs->txs_dmamap)) != 0) {
19542c1b001SThomas Moestl 			device_printf(sc->sc_dev, "unable to create tx DMA map "
19642c1b001SThomas Moestl 			    "%d, error = %d\n", i, error);
19742c1b001SThomas Moestl 			goto fail_4;
19842c1b001SThomas Moestl 		}
19942c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
20042c1b001SThomas Moestl 	}
20142c1b001SThomas Moestl 
20242c1b001SThomas Moestl 	/*
20342c1b001SThomas Moestl 	 * Create the receive buffer DMA maps.
20442c1b001SThomas Moestl 	 */
20542c1b001SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
20642c1b001SThomas Moestl 		if ((error = bus_dmamap_create(sc->sc_dmatag, 0,
20742c1b001SThomas Moestl 		    &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
20842c1b001SThomas Moestl 			device_printf(sc->sc_dev, "unable to create rx DMA map "
20942c1b001SThomas Moestl 			    "%d, error = %d\n", i, error);
21042c1b001SThomas Moestl 			goto fail_5;
21142c1b001SThomas Moestl 		}
21242c1b001SThomas Moestl 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
21342c1b001SThomas Moestl 	}
21442c1b001SThomas Moestl 
21542c1b001SThomas Moestl 
21642c1b001SThomas Moestl 	gem_mifinit(sc);
21742c1b001SThomas Moestl 
21842c1b001SThomas Moestl 	if ((error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, gem_mediachange,
21942c1b001SThomas Moestl 	    gem_mediastatus)) != 0) {
22042c1b001SThomas Moestl 		device_printf(sc->sc_dev, "phy probe failed: %d\n", error);
22142c1b001SThomas Moestl 		goto fail_5;
22242c1b001SThomas Moestl 	}
22342c1b001SThomas Moestl 	sc->sc_mii = device_get_softc(sc->sc_miibus);
22442c1b001SThomas Moestl 
22542c1b001SThomas Moestl 	/*
22642c1b001SThomas Moestl 	 * From this point forward, the attachment cannot fail.  A failure
22742c1b001SThomas Moestl 	 * before this point releases all resources that may have been
22842c1b001SThomas Moestl 	 * allocated.
22942c1b001SThomas Moestl 	 */
23042c1b001SThomas Moestl 
23142c1b001SThomas Moestl 	/* Announce ourselves. */
23242c1b001SThomas Moestl 	device_printf(sc->sc_dev, "Ethernet address:");
23342c1b001SThomas Moestl 	for (i = 0; i < 6; i++)
23442c1b001SThomas Moestl 		printf("%c%02x", i > 0 ? ':' : ' ', sc->sc_arpcom.ac_enaddr[i]);
235336cca9eSBenno Rice 
236336cca9eSBenno Rice 	/* Get RX FIFO size */
237336cca9eSBenno Rice 	sc->sc_rxfifosize = 64 *
238336cca9eSBenno Rice 	    bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_FIFO_SIZE);
239336cca9eSBenno Rice 	printf(", %uKB RX fifo", sc->sc_rxfifosize / 1024);
240336cca9eSBenno Rice 
241336cca9eSBenno Rice 	/* Get TX FIFO size */
242336cca9eSBenno Rice 	v = bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_FIFO_SIZE);
243336cca9eSBenno Rice 	printf(", %uKB TX fifo\n", v / 16);
24442c1b001SThomas Moestl 
24542c1b001SThomas Moestl 	/* Initialize ifnet structure. */
24642c1b001SThomas Moestl 	ifp->if_softc = sc;
24742c1b001SThomas Moestl 	ifp->if_unit = device_get_unit(sc->sc_dev);
24842c1b001SThomas Moestl 	ifp->if_name = "gem";
24942c1b001SThomas Moestl 	ifp->if_mtu = ETHERMTU;
25042c1b001SThomas Moestl 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
25142c1b001SThomas Moestl 	ifp->if_start = gem_start;
25242c1b001SThomas Moestl 	ifp->if_ioctl = gem_ioctl;
25342c1b001SThomas Moestl 	ifp->if_watchdog = gem_watchdog;
25442c1b001SThomas Moestl 	ifp->if_init = gem_init;
25542c1b001SThomas Moestl 	ifp->if_output = ether_output;
25642c1b001SThomas Moestl 	ifp->if_snd.ifq_maxlen = GEM_TXQUEUELEN;
25742c1b001SThomas Moestl 	/*
25842c1b001SThomas Moestl 	 * Walk along the list of attached MII devices and
25942c1b001SThomas Moestl 	 * establish an `MII instance' to `phy number'
26042c1b001SThomas Moestl 	 * mapping. We'll use this mapping in media change
26142c1b001SThomas Moestl 	 * requests to determine which phy to use to program
26242c1b001SThomas Moestl 	 * the MIF configuration register.
26342c1b001SThomas Moestl 	 */
26442c1b001SThomas Moestl 	for (child = LIST_FIRST(&sc->sc_mii->mii_phys); child != NULL;
26542c1b001SThomas Moestl 	     child = LIST_NEXT(child, mii_list)) {
26642c1b001SThomas Moestl 		/*
26742c1b001SThomas Moestl 		 * Note: we support just two PHYs: the built-in
26842c1b001SThomas Moestl 		 * internal device and an external on the MII
26942c1b001SThomas Moestl 		 * connector.
27042c1b001SThomas Moestl 		 */
27142c1b001SThomas Moestl 		if (child->mii_phy > 1 || child->mii_inst > 1) {
27242c1b001SThomas Moestl 			device_printf(sc->sc_dev, "cannot accomodate "
27342c1b001SThomas Moestl 			    "MII device %s at phy %d, instance %d\n",
27442c1b001SThomas Moestl 			    device_get_name(child->mii_dev),
27542c1b001SThomas Moestl 			    child->mii_phy, child->mii_inst);
27642c1b001SThomas Moestl 			continue;
27742c1b001SThomas Moestl 		}
27842c1b001SThomas Moestl 
27942c1b001SThomas Moestl 		sc->sc_phys[child->mii_inst] = child->mii_phy;
28042c1b001SThomas Moestl 	}
28142c1b001SThomas Moestl 
28242c1b001SThomas Moestl 	/*
28342c1b001SThomas Moestl 	 * Now select and activate the PHY we will use.
28442c1b001SThomas Moestl 	 *
28542c1b001SThomas Moestl 	 * The order of preference is External (MDI1),
28642c1b001SThomas Moestl 	 * Internal (MDI0), Serial Link (no MII).
28742c1b001SThomas Moestl 	 */
28842c1b001SThomas Moestl 	if (sc->sc_phys[1]) {
28942c1b001SThomas Moestl #ifdef GEM_DEBUG
29042c1b001SThomas Moestl 		printf("using external phy\n");
29142c1b001SThomas Moestl #endif
29242c1b001SThomas Moestl 		sc->sc_mif_config |= GEM_MIF_CONFIG_PHY_SEL;
29342c1b001SThomas Moestl 	} else {
29442c1b001SThomas Moestl #ifdef GEM_DEBUG
29542c1b001SThomas Moestl 		printf("using internal phy\n");
29642c1b001SThomas Moestl #endif
29742c1b001SThomas Moestl 		sc->sc_mif_config &= ~GEM_MIF_CONFIG_PHY_SEL;
29842c1b001SThomas Moestl 	}
29942c1b001SThomas Moestl 	bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_MIF_CONFIG,
30042c1b001SThomas Moestl 	    sc->sc_mif_config);
30142c1b001SThomas Moestl 	/* Attach the interface. */
30242c1b001SThomas Moestl 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
30342c1b001SThomas Moestl 
30442c1b001SThomas Moestl #if notyet
30542c1b001SThomas Moestl 	/*
30642c1b001SThomas Moestl 	 * Add a suspend hook to make sure we come back up after a
30742c1b001SThomas Moestl 	 * resume.
30842c1b001SThomas Moestl 	 */
30942c1b001SThomas Moestl 	sc->sc_powerhook = powerhook_establish(gem_power, sc);
31042c1b001SThomas Moestl 	if (sc->sc_powerhook == NULL)
31142c1b001SThomas Moestl 		device_printf(sc->sc_dev, "WARNING: unable to establish power "
31242c1b001SThomas Moestl 		    "hook\n");
31342c1b001SThomas Moestl #endif
31442c1b001SThomas Moestl 
31542c1b001SThomas Moestl 	callout_init(&sc->sc_tick_ch, 0);
3160d80b9bdSThomas Moestl 	callout_init(&sc->sc_rx_ch, 0);
31742c1b001SThomas Moestl 	return (0);
31842c1b001SThomas Moestl 
31942c1b001SThomas Moestl 	/*
32042c1b001SThomas Moestl 	 * Free any resources we've allocated during the failed attach
32142c1b001SThomas Moestl 	 * attempt.  Do this in reverse order and fall through.
32242c1b001SThomas Moestl 	 */
32342c1b001SThomas Moestl fail_5:
32442c1b001SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
32542c1b001SThomas Moestl 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
32642c1b001SThomas Moestl 			bus_dmamap_destroy(sc->sc_dmatag,
32742c1b001SThomas Moestl 			    sc->sc_rxsoft[i].rxs_dmamap);
32842c1b001SThomas Moestl 	}
32942c1b001SThomas Moestl fail_4:
33042c1b001SThomas Moestl 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
33142c1b001SThomas Moestl 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
33242c1b001SThomas Moestl 			bus_dmamap_destroy(sc->sc_dmatag,
33342c1b001SThomas Moestl 			    sc->sc_txsoft[i].txs_dmamap);
33442c1b001SThomas Moestl 	}
33542c1b001SThomas Moestl 	bus_dmamap_unload(sc->sc_dmatag, sc->sc_cddmamap);
33642c1b001SThomas Moestl fail_3:
33742c1b001SThomas Moestl 	bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data,
33842c1b001SThomas Moestl 	    sc->sc_cddmamap);
33942c1b001SThomas Moestl fail_2:
34042c1b001SThomas Moestl 	bus_dma_tag_destroy(sc->sc_cdmatag);
34142c1b001SThomas Moestl fail_1:
34242c1b001SThomas Moestl 	bus_dma_tag_destroy(sc->sc_dmatag);
34342c1b001SThomas Moestl fail_0:
34442c1b001SThomas Moestl 	bus_dma_tag_destroy(sc->sc_pdmatag);
34542c1b001SThomas Moestl 	return (error);
34642c1b001SThomas Moestl }
34742c1b001SThomas Moestl 
34842c1b001SThomas Moestl static void
34942c1b001SThomas Moestl gem_cddma_callback(xsc, segs, nsegs, error)
35042c1b001SThomas Moestl 	void *xsc;
35142c1b001SThomas Moestl 	bus_dma_segment_t *segs;
35242c1b001SThomas Moestl 	int nsegs;
35342c1b001SThomas Moestl 	int error;
35442c1b001SThomas Moestl {
35542c1b001SThomas Moestl 	struct gem_softc *sc = (struct gem_softc *)xsc;
35642c1b001SThomas Moestl 
35742c1b001SThomas Moestl 	if (error != 0)
35842c1b001SThomas Moestl 		return;
35942c1b001SThomas Moestl 	if (nsegs != 1) {
36042c1b001SThomas Moestl 		/* can't happen... */
36142c1b001SThomas Moestl 		panic("gem_cddma_callback: bad control buffer segment count");
36242c1b001SThomas Moestl 	}
36342c1b001SThomas Moestl 	sc->sc_cddma = segs[0].ds_addr;
36442c1b001SThomas Moestl }
36542c1b001SThomas Moestl 
36642c1b001SThomas Moestl static void
36742c1b001SThomas Moestl gem_rxdma_callback(xsc, segs, nsegs, error)
36842c1b001SThomas Moestl 	void *xsc;
36942c1b001SThomas Moestl 	bus_dma_segment_t *segs;
37042c1b001SThomas Moestl 	int nsegs;
37142c1b001SThomas Moestl 	int error;
37242c1b001SThomas Moestl {
37342c1b001SThomas Moestl 	struct gem_rxsoft *rxs = (struct gem_rxsoft *)xsc;
37442c1b001SThomas Moestl 
37542c1b001SThomas Moestl 	if (error != 0)
37642c1b001SThomas Moestl 		return;
37742c1b001SThomas Moestl 	if (nsegs != 1) {
37842c1b001SThomas Moestl 		/* can't happen... */
37942c1b001SThomas Moestl 		panic("gem_rxdma_callback: bad control buffer segment count");
38042c1b001SThomas Moestl 	}
38142c1b001SThomas Moestl 	rxs->rxs_paddr = segs[0].ds_addr;
38242c1b001SThomas Moestl }
38342c1b001SThomas Moestl 
38442c1b001SThomas Moestl /*
38542c1b001SThomas Moestl  * This is called multiple times in our version of dmamap_load_mbuf, but should
38642c1b001SThomas Moestl  * be fit for a generic version that only calls it once.
38742c1b001SThomas Moestl  */
38842c1b001SThomas Moestl static void
38942c1b001SThomas Moestl gem_txdma_callback(xsc, segs, nsegs, error)
39042c1b001SThomas Moestl 	void *xsc;
39142c1b001SThomas Moestl 	bus_dma_segment_t *segs;
39242c1b001SThomas Moestl 	int nsegs;
39342c1b001SThomas Moestl 	int error;
39442c1b001SThomas Moestl {
39542c1b001SThomas Moestl 	struct gem_txdma *tx = (struct gem_txdma *)xsc;
39642c1b001SThomas Moestl 	int seg;
39742c1b001SThomas Moestl 
39842c1b001SThomas Moestl 	tx->txd_error = error;
39942c1b001SThomas Moestl 	if (error != 0)
40042c1b001SThomas Moestl 		return;
40142c1b001SThomas Moestl 	tx->txd_nsegs = nsegs;
40242c1b001SThomas Moestl 
40342c1b001SThomas Moestl 	/*
40442c1b001SThomas Moestl 	 * Initialize the transmit descriptors.
40542c1b001SThomas Moestl 	 */
40642c1b001SThomas Moestl 	for (seg = 0; seg < nsegs;
40742c1b001SThomas Moestl 	     seg++, tx->txd_nexttx = GEM_NEXTTX(tx->txd_nexttx)) {
40842c1b001SThomas Moestl 		uint64_t flags;
40942c1b001SThomas Moestl 
41042c1b001SThomas Moestl 		DPRINTF(tx->txd_sc, ("txdma_cb: mapping seg %d (txd %d), len "
41142c1b001SThomas Moestl 		    "%lx, addr %#lx (%#lx)\n",  seg, tx->txd_nexttx,
41242c1b001SThomas Moestl 		    segs[seg].ds_len, segs[seg].ds_addr,
41342c1b001SThomas Moestl 		    GEM_DMA_WRITE(tx->txd_sc, segs[seg].ds_addr)));
41442c1b001SThomas Moestl 		CTR5(KTR_GEM, "txdma_cb: mapping seg %d (txd %d), len "
41542c1b001SThomas Moestl 		    "%lx, addr %#lx (%#lx)",  seg, tx->txd_nexttx,
41642c1b001SThomas Moestl 		    segs[seg].ds_len, segs[seg].ds_addr,
41742c1b001SThomas Moestl 		    GEM_DMA_WRITE(tx->txd_sc, segs[seg].ds_addr));
41842c1b001SThomas Moestl 		/*
41942c1b001SThomas Moestl 		 * If this is the first descriptor we're
42042c1b001SThomas Moestl 		 * enqueueing, set the start of packet flag,
42142c1b001SThomas Moestl 		 * and the checksum stuff if we want the hardware
42242c1b001SThomas Moestl 		 * to do it.
42342c1b001SThomas Moestl 		 */
42442c1b001SThomas Moestl 		tx->txd_sc->sc_txdescs[tx->txd_nexttx].gd_addr =
42542c1b001SThomas Moestl 		    GEM_DMA_WRITE(tx->txd_sc, segs[seg].ds_addr);
42642c1b001SThomas Moestl 		flags = segs[seg].ds_len & GEM_TD_BUFSIZE;
42742c1b001SThomas Moestl 		if ((tx->txd_flags & GTXD_FIRST) != 0 && seg == 0) {
42842c1b001SThomas Moestl 			CTR2(KTR_GEM, "txdma_cb: start of packet at seg %d, "
42942c1b001SThomas Moestl 			    "tx %d", seg, tx->txd_nexttx);
43042c1b001SThomas Moestl 			flags |= GEM_TD_START_OF_PACKET;
431336cca9eSBenno Rice 			if (++tx->txd_sc->sc_txwin > GEM_NTXSEGS * 2 / 3) {
432336cca9eSBenno Rice 				tx->txd_sc->sc_txwin = 0;
433336cca9eSBenno Rice 				flags |= GEM_TD_INTERRUPT_ME;
434336cca9eSBenno Rice 			}
43542c1b001SThomas Moestl 		}
43642c1b001SThomas Moestl 		if ((tx->txd_flags & GTXD_LAST) != 0 && seg == nsegs - 1) {
43742c1b001SThomas Moestl 			CTR2(KTR_GEM, "txdma_cb: end of packet at seg %d, "
43842c1b001SThomas Moestl 			    "tx %d", seg, tx->txd_nexttx);
43942c1b001SThomas Moestl 			flags |= GEM_TD_END_OF_PACKET;
44042c1b001SThomas Moestl 		}
44142c1b001SThomas Moestl 		tx->txd_sc->sc_txdescs[tx->txd_nexttx].gd_flags =
44242c1b001SThomas Moestl 		    GEM_DMA_WRITE(tx->txd_sc, flags);
44342c1b001SThomas Moestl 		tx->txd_lasttx = tx->txd_nexttx;
44442c1b001SThomas Moestl 	}
44542c1b001SThomas Moestl }
44642c1b001SThomas Moestl 
44742c1b001SThomas Moestl static void
44842c1b001SThomas Moestl gem_tick(arg)
44942c1b001SThomas Moestl 	void *arg;
45042c1b001SThomas Moestl {
45142c1b001SThomas Moestl 	struct gem_softc *sc = arg;
45242c1b001SThomas Moestl 	int s;
45342c1b001SThomas Moestl 
45442c1b001SThomas Moestl 	s = splnet();
45542c1b001SThomas Moestl 	mii_tick(sc->sc_mii);
45642c1b001SThomas Moestl 	splx(s);
45742c1b001SThomas Moestl 
45842c1b001SThomas Moestl 	callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
45942c1b001SThomas Moestl }
46042c1b001SThomas Moestl 
46142c1b001SThomas Moestl static int
46242c1b001SThomas Moestl gem_bitwait(sc, r, clr, set)
46342c1b001SThomas Moestl 	struct gem_softc *sc;
46442c1b001SThomas Moestl 	bus_addr_t r;
46542c1b001SThomas Moestl 	u_int32_t clr;
46642c1b001SThomas Moestl 	u_int32_t set;
46742c1b001SThomas Moestl {
46842c1b001SThomas Moestl 	int i;
46942c1b001SThomas Moestl 	u_int32_t reg;
47042c1b001SThomas Moestl 
47142c1b001SThomas Moestl 	for (i = TRIES; i--; DELAY(100)) {
47242c1b001SThomas Moestl 		reg = bus_space_read_4(sc->sc_bustag, sc->sc_h, r);
47342c1b001SThomas Moestl 		if ((r & clr) == 0 && (r & set) == set)
47442c1b001SThomas Moestl 			return (1);
47542c1b001SThomas Moestl 	}
47642c1b001SThomas Moestl 	return (0);
47742c1b001SThomas Moestl }
47842c1b001SThomas Moestl 
47942c1b001SThomas Moestl void
48042c1b001SThomas Moestl gem_reset(sc)
48142c1b001SThomas Moestl 	struct gem_softc *sc;
48242c1b001SThomas Moestl {
48342c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
48442c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
48542c1b001SThomas Moestl 	int s;
48642c1b001SThomas Moestl 
48742c1b001SThomas Moestl 	s = splnet();
48842c1b001SThomas Moestl 	DPRINTF(sc, ("%s: gem_reset\n", device_get_name(sc->sc_dev)));
48942c1b001SThomas Moestl 	CTR1(KTR_GEM, "%s: gem_reset", device_get_name(sc->sc_dev));
49042c1b001SThomas Moestl 	gem_reset_rx(sc);
49142c1b001SThomas Moestl 	gem_reset_tx(sc);
49242c1b001SThomas Moestl 
49342c1b001SThomas Moestl 	/* Do a full reset */
49442c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX);
49542c1b001SThomas Moestl 	if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0))
49642c1b001SThomas Moestl 		device_printf(sc->sc_dev, "cannot reset device\n");
49742c1b001SThomas Moestl 	splx(s);
49842c1b001SThomas Moestl }
49942c1b001SThomas Moestl 
50042c1b001SThomas Moestl 
50142c1b001SThomas Moestl /*
50242c1b001SThomas Moestl  * gem_rxdrain:
50342c1b001SThomas Moestl  *
50442c1b001SThomas Moestl  *	Drain the receive queue.
50542c1b001SThomas Moestl  */
50642c1b001SThomas Moestl static void
50742c1b001SThomas Moestl gem_rxdrain(sc)
50842c1b001SThomas Moestl 	struct gem_softc *sc;
50942c1b001SThomas Moestl {
51042c1b001SThomas Moestl 	struct gem_rxsoft *rxs;
51142c1b001SThomas Moestl 	int i;
51242c1b001SThomas Moestl 
51342c1b001SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
51442c1b001SThomas Moestl 		rxs = &sc->sc_rxsoft[i];
51542c1b001SThomas Moestl 		if (rxs->rxs_mbuf != NULL) {
51642c1b001SThomas Moestl 			bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
51742c1b001SThomas Moestl 			m_freem(rxs->rxs_mbuf);
51842c1b001SThomas Moestl 			rxs->rxs_mbuf = NULL;
51942c1b001SThomas Moestl 		}
52042c1b001SThomas Moestl 	}
52142c1b001SThomas Moestl }
52242c1b001SThomas Moestl 
52342c1b001SThomas Moestl /*
52442c1b001SThomas Moestl  * Reset the whole thing.
52542c1b001SThomas Moestl  */
52642c1b001SThomas Moestl static void
52742c1b001SThomas Moestl gem_stop(ifp, disable)
52842c1b001SThomas Moestl 	struct ifnet *ifp;
52942c1b001SThomas Moestl 	int disable;
53042c1b001SThomas Moestl {
53142c1b001SThomas Moestl 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
53242c1b001SThomas Moestl 	struct gem_txsoft *txs;
53342c1b001SThomas Moestl 
53442c1b001SThomas Moestl 	DPRINTF(sc, ("%s: gem_stop\n", device_get_name(sc->sc_dev)));
53542c1b001SThomas Moestl 	CTR1(KTR_GEM, "%s: gem_stop", device_get_name(sc->sc_dev));
53642c1b001SThomas Moestl 
53742c1b001SThomas Moestl 	callout_stop(&sc->sc_tick_ch);
53842c1b001SThomas Moestl 
53942c1b001SThomas Moestl 	/* XXX - Should we reset these instead? */
54042c1b001SThomas Moestl 	gem_disable_tx(sc);
54142c1b001SThomas Moestl 	gem_disable_rx(sc);
54242c1b001SThomas Moestl 
54342c1b001SThomas Moestl 	/*
54442c1b001SThomas Moestl 	 * Release any queued transmit buffers.
54542c1b001SThomas Moestl 	 */
54642c1b001SThomas Moestl 	while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
54742c1b001SThomas Moestl 		STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
54842c1b001SThomas Moestl 		if (txs->txs_ndescs != 0) {
54942c1b001SThomas Moestl 			bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
55042c1b001SThomas Moestl 			if (txs->txs_mbuf != NULL) {
55142c1b001SThomas Moestl 				m_freem(txs->txs_mbuf);
55242c1b001SThomas Moestl 				txs->txs_mbuf = NULL;
55342c1b001SThomas Moestl 			}
55442c1b001SThomas Moestl 		}
55542c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
55642c1b001SThomas Moestl 	}
55742c1b001SThomas Moestl 
55842c1b001SThomas Moestl 	if (disable)
55942c1b001SThomas Moestl 		gem_rxdrain(sc);
56042c1b001SThomas Moestl 
56142c1b001SThomas Moestl 	/*
56242c1b001SThomas Moestl 	 * Mark the interface down and cancel the watchdog timer.
56342c1b001SThomas Moestl 	 */
56442c1b001SThomas Moestl 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
56542c1b001SThomas Moestl 	ifp->if_timer = 0;
56642c1b001SThomas Moestl }
56742c1b001SThomas Moestl 
56842c1b001SThomas Moestl /*
56942c1b001SThomas Moestl  * Reset the receiver
57042c1b001SThomas Moestl  */
57142c1b001SThomas Moestl int
57242c1b001SThomas Moestl gem_reset_rx(sc)
57342c1b001SThomas Moestl 	struct gem_softc *sc;
57442c1b001SThomas Moestl {
57542c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
57642c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
57742c1b001SThomas Moestl 
57842c1b001SThomas Moestl 	/*
57942c1b001SThomas Moestl 	 * Resetting while DMA is in progress can cause a bus hang, so we
58042c1b001SThomas Moestl 	 * disable DMA first.
58142c1b001SThomas Moestl 	 */
58242c1b001SThomas Moestl 	gem_disable_rx(sc);
58342c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RX_CONFIG, 0);
58442c1b001SThomas Moestl 	/* Wait till it finishes */
58542c1b001SThomas Moestl 	if (!gem_bitwait(sc, GEM_RX_CONFIG, 1, 0))
58642c1b001SThomas Moestl 		device_printf(sc->sc_dev, "cannot disable read dma\n");
58742c1b001SThomas Moestl 
58842c1b001SThomas Moestl 	/* Wait 5ms extra. */
58942c1b001SThomas Moestl 	DELAY(5000);
59042c1b001SThomas Moestl 
59142c1b001SThomas Moestl 	/* Finally, reset the ERX */
59242c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX);
59342c1b001SThomas Moestl 	/* Wait till it finishes */
59442c1b001SThomas Moestl 	if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_TX, 0)) {
59542c1b001SThomas Moestl 		device_printf(sc->sc_dev, "cannot reset receiver\n");
59642c1b001SThomas Moestl 		return (1);
59742c1b001SThomas Moestl 	}
59842c1b001SThomas Moestl 	return (0);
59942c1b001SThomas Moestl }
60042c1b001SThomas Moestl 
60142c1b001SThomas Moestl 
60242c1b001SThomas Moestl /*
60342c1b001SThomas Moestl  * Reset the transmitter
60442c1b001SThomas Moestl  */
60542c1b001SThomas Moestl static int
60642c1b001SThomas Moestl gem_reset_tx(sc)
60742c1b001SThomas Moestl 	struct gem_softc *sc;
60842c1b001SThomas Moestl {
60942c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
61042c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
61142c1b001SThomas Moestl 	int i;
61242c1b001SThomas Moestl 
61342c1b001SThomas Moestl 	/*
61442c1b001SThomas Moestl 	 * Resetting while DMA is in progress can cause a bus hang, so we
61542c1b001SThomas Moestl 	 * disable DMA first.
61642c1b001SThomas Moestl 	 */
61742c1b001SThomas Moestl 	gem_disable_tx(sc);
61842c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_TX_CONFIG, 0);
61942c1b001SThomas Moestl 	/* Wait till it finishes */
62042c1b001SThomas Moestl 	if (!gem_bitwait(sc, GEM_TX_CONFIG, 1, 0))
62142c1b001SThomas Moestl 		device_printf(sc->sc_dev, "cannot disable read dma\n");
62242c1b001SThomas Moestl 
62342c1b001SThomas Moestl 	/* Wait 5ms extra. */
62442c1b001SThomas Moestl 	DELAY(5000);
62542c1b001SThomas Moestl 
62642c1b001SThomas Moestl 	/* Finally, reset the ETX */
62742c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_TX);
62842c1b001SThomas Moestl 	/* Wait till it finishes */
62942c1b001SThomas Moestl 	for (i = TRIES; i--; DELAY(100))
63042c1b001SThomas Moestl 		if ((bus_space_read_4(t, h, GEM_RESET) & GEM_RESET_TX) == 0)
63142c1b001SThomas Moestl 			break;
63242c1b001SThomas Moestl 	if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_TX, 0)) {
63342c1b001SThomas Moestl 		device_printf(sc->sc_dev, "cannot reset receiver\n");
63442c1b001SThomas Moestl 		return (1);
63542c1b001SThomas Moestl 	}
63642c1b001SThomas Moestl 	return (0);
63742c1b001SThomas Moestl }
63842c1b001SThomas Moestl 
63942c1b001SThomas Moestl /*
64042c1b001SThomas Moestl  * disable receiver.
64142c1b001SThomas Moestl  */
64242c1b001SThomas Moestl static int
64342c1b001SThomas Moestl gem_disable_rx(sc)
64442c1b001SThomas Moestl 	struct gem_softc *sc;
64542c1b001SThomas Moestl {
64642c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
64742c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
64842c1b001SThomas Moestl 	u_int32_t cfg;
64942c1b001SThomas Moestl 
65042c1b001SThomas Moestl 	/* Flip the enable bit */
65142c1b001SThomas Moestl 	cfg = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
65242c1b001SThomas Moestl 	cfg &= ~GEM_MAC_RX_ENABLE;
65342c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, cfg);
65442c1b001SThomas Moestl 
65542c1b001SThomas Moestl 	/* Wait for it to finish */
65642c1b001SThomas Moestl 	return (gem_bitwait(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0));
65742c1b001SThomas Moestl }
65842c1b001SThomas Moestl 
65942c1b001SThomas Moestl /*
66042c1b001SThomas Moestl  * disable transmitter.
66142c1b001SThomas Moestl  */
66242c1b001SThomas Moestl static int
66342c1b001SThomas Moestl gem_disable_tx(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 	u_int32_t cfg;
66942c1b001SThomas Moestl 
67042c1b001SThomas Moestl 	/* Flip the enable bit */
67142c1b001SThomas Moestl 	cfg = bus_space_read_4(t, h, GEM_MAC_TX_CONFIG);
67242c1b001SThomas Moestl 	cfg &= ~GEM_MAC_TX_ENABLE;
67342c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_TX_CONFIG, cfg);
67442c1b001SThomas Moestl 
67542c1b001SThomas Moestl 	/* Wait for it to finish */
67642c1b001SThomas Moestl 	return (gem_bitwait(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0));
67742c1b001SThomas Moestl }
67842c1b001SThomas Moestl 
67942c1b001SThomas Moestl /*
68042c1b001SThomas Moestl  * Initialize interface.
68142c1b001SThomas Moestl  */
68242c1b001SThomas Moestl static int
68342c1b001SThomas Moestl gem_meminit(sc)
68442c1b001SThomas Moestl 	struct gem_softc *sc;
68542c1b001SThomas Moestl {
68642c1b001SThomas Moestl 	struct gem_rxsoft *rxs;
68742c1b001SThomas Moestl 	int i, error;
68842c1b001SThomas Moestl 
68942c1b001SThomas Moestl 	/*
69042c1b001SThomas Moestl 	 * Initialize the transmit descriptor ring.
69142c1b001SThomas Moestl 	 */
69242c1b001SThomas Moestl 	memset((void *)sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
69342c1b001SThomas Moestl 	for (i = 0; i < GEM_NTXDESC; i++) {
69442c1b001SThomas Moestl 		sc->sc_txdescs[i].gd_flags = 0;
69542c1b001SThomas Moestl 		sc->sc_txdescs[i].gd_addr = 0;
69642c1b001SThomas Moestl 	}
69742c1b001SThomas Moestl 	GEM_CDTXSYNC(sc, 0, GEM_NTXDESC,
69842c1b001SThomas Moestl 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
699336cca9eSBenno Rice 	sc->sc_txfree = GEM_NTXDESC-1;
70042c1b001SThomas Moestl 	sc->sc_txnext = 0;
701336cca9eSBenno Rice 	sc->sc_txwin = 0;
70242c1b001SThomas Moestl 
70342c1b001SThomas Moestl 	/*
70442c1b001SThomas Moestl 	 * Initialize the receive descriptor and receive job
70542c1b001SThomas Moestl 	 * descriptor rings.
70642c1b001SThomas Moestl 	 */
70742c1b001SThomas Moestl 	for (i = 0; i < GEM_NRXDESC; i++) {
70842c1b001SThomas Moestl 		rxs = &sc->sc_rxsoft[i];
70942c1b001SThomas Moestl 		if (rxs->rxs_mbuf == NULL) {
71042c1b001SThomas Moestl 			if ((error = gem_add_rxbuf(sc, i)) != 0) {
71142c1b001SThomas Moestl 				device_printf(sc->sc_dev, "unable to "
71242c1b001SThomas Moestl 				    "allocate or map rx buffer %d, error = "
71342c1b001SThomas Moestl 				    "%d\n", i, error);
71442c1b001SThomas Moestl 				/*
71542c1b001SThomas Moestl 				 * XXX Should attempt to run with fewer receive
71642c1b001SThomas Moestl 				 * XXX buffers instead of just failing.
71742c1b001SThomas Moestl 				 */
71842c1b001SThomas Moestl 				gem_rxdrain(sc);
71942c1b001SThomas Moestl 				return (1);
72042c1b001SThomas Moestl 			}
72142c1b001SThomas Moestl 		} else
72242c1b001SThomas Moestl 			GEM_INIT_RXDESC(sc, i);
72342c1b001SThomas Moestl 	}
72442c1b001SThomas Moestl 	sc->sc_rxptr = 0;
72542c1b001SThomas Moestl 
72642c1b001SThomas Moestl 	return (0);
72742c1b001SThomas Moestl }
72842c1b001SThomas Moestl 
72942c1b001SThomas Moestl static int
73042c1b001SThomas Moestl gem_ringsize(sz)
73142c1b001SThomas Moestl 	int sz;
73242c1b001SThomas Moestl {
73342c1b001SThomas Moestl 	int v = 0;
73442c1b001SThomas Moestl 
73542c1b001SThomas Moestl 	switch (sz) {
73642c1b001SThomas Moestl 	case 32:
73742c1b001SThomas Moestl 		v = GEM_RING_SZ_32;
73842c1b001SThomas Moestl 		break;
73942c1b001SThomas Moestl 	case 64:
74042c1b001SThomas Moestl 		v = GEM_RING_SZ_64;
74142c1b001SThomas Moestl 		break;
74242c1b001SThomas Moestl 	case 128:
74342c1b001SThomas Moestl 		v = GEM_RING_SZ_128;
74442c1b001SThomas Moestl 		break;
74542c1b001SThomas Moestl 	case 256:
74642c1b001SThomas Moestl 		v = GEM_RING_SZ_256;
74742c1b001SThomas Moestl 		break;
74842c1b001SThomas Moestl 	case 512:
74942c1b001SThomas Moestl 		v = GEM_RING_SZ_512;
75042c1b001SThomas Moestl 		break;
75142c1b001SThomas Moestl 	case 1024:
75242c1b001SThomas Moestl 		v = GEM_RING_SZ_1024;
75342c1b001SThomas Moestl 		break;
75442c1b001SThomas Moestl 	case 2048:
75542c1b001SThomas Moestl 		v = GEM_RING_SZ_2048;
75642c1b001SThomas Moestl 		break;
75742c1b001SThomas Moestl 	case 4096:
75842c1b001SThomas Moestl 		v = GEM_RING_SZ_4096;
75942c1b001SThomas Moestl 		break;
76042c1b001SThomas Moestl 	case 8192:
76142c1b001SThomas Moestl 		v = GEM_RING_SZ_8192;
76242c1b001SThomas Moestl 		break;
76342c1b001SThomas Moestl 	default:
76442c1b001SThomas Moestl 		printf("gem: invalid Receive Descriptor ring size\n");
76542c1b001SThomas Moestl 		break;
76642c1b001SThomas Moestl 	}
76742c1b001SThomas Moestl 	return (v);
76842c1b001SThomas Moestl }
76942c1b001SThomas Moestl 
77042c1b001SThomas Moestl /*
77142c1b001SThomas Moestl  * Initialization of interface; set up initialization block
77242c1b001SThomas Moestl  * and transmit/receive descriptor rings.
77342c1b001SThomas Moestl  */
77442c1b001SThomas Moestl static void
77542c1b001SThomas Moestl gem_init(xsc)
77642c1b001SThomas Moestl 	void *xsc;
77742c1b001SThomas Moestl {
77842c1b001SThomas Moestl 	struct gem_softc *sc = (struct gem_softc *)xsc;
77942c1b001SThomas Moestl 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
78042c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
78142c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
78242c1b001SThomas Moestl 	int s;
78342c1b001SThomas Moestl 	u_int32_t v;
78442c1b001SThomas Moestl 
78542c1b001SThomas Moestl 	s = splnet();
78642c1b001SThomas Moestl 
78742c1b001SThomas Moestl 	DPRINTF(sc, ("%s: gem_init: calling stop\n", device_get_name(sc->sc_dev)));
78842c1b001SThomas Moestl 	CTR1(KTR_GEM, "%s: gem_init: calling stop", device_get_name(sc->sc_dev));
78942c1b001SThomas Moestl 	/*
79042c1b001SThomas Moestl 	 * Initialization sequence. The numbered steps below correspond
79142c1b001SThomas Moestl 	 * to the sequence outlined in section 6.3.5.1 in the Ethernet
79242c1b001SThomas Moestl 	 * Channel Engine manual (part of the PCIO manual).
79342c1b001SThomas Moestl 	 * See also the STP2002-STQ document from Sun Microsystems.
79442c1b001SThomas Moestl 	 */
79542c1b001SThomas Moestl 
79642c1b001SThomas Moestl 	/* step 1 & 2. Reset the Ethernet Channel */
79742c1b001SThomas Moestl 	gem_stop(&sc->sc_arpcom.ac_if, 0);
79842c1b001SThomas Moestl 	gem_reset(sc);
79942c1b001SThomas Moestl 	DPRINTF(sc, ("%s: gem_init: restarting\n", device_get_name(sc->sc_dev)));
80042c1b001SThomas Moestl 	CTR1(KTR_GEM, "%s: gem_init: restarting", device_get_name(sc->sc_dev));
80142c1b001SThomas Moestl 
80242c1b001SThomas Moestl 	/* Re-initialize the MIF */
80342c1b001SThomas Moestl 	gem_mifinit(sc);
80442c1b001SThomas Moestl 
80542c1b001SThomas Moestl 	/* Call MI reset function if any */
80642c1b001SThomas Moestl 	if (sc->sc_hwreset)
80742c1b001SThomas Moestl 		(*sc->sc_hwreset)(sc);
80842c1b001SThomas Moestl 
80942c1b001SThomas Moestl 	/* step 3. Setup data structures in host memory */
81042c1b001SThomas Moestl 	gem_meminit(sc);
81142c1b001SThomas Moestl 
81242c1b001SThomas Moestl 	/* step 4. TX MAC registers & counters */
81342c1b001SThomas Moestl 	gem_init_regs(sc);
81442c1b001SThomas Moestl 	/* XXX: VLAN code from NetBSD temporarily removed. */
81542c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
81642c1b001SThomas Moestl             (ETHER_MAX_LEN + sizeof(struct ether_header)) | (0x2000<<16));
81742c1b001SThomas Moestl 
81842c1b001SThomas Moestl 	/* step 5. RX MAC registers & counters */
81942c1b001SThomas Moestl 	gem_setladrf(sc);
82042c1b001SThomas Moestl 
82142c1b001SThomas Moestl 	/* step 6 & 7. Program Descriptor Ring Base Addresses */
82242c1b001SThomas Moestl 	/* NOTE: we use only 32-bit DMA addresses here. */
82342c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_TX_RING_PTR_HI, 0);
82442c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0));
82542c1b001SThomas Moestl 
82642c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RX_RING_PTR_HI, 0);
82742c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0));
82842c1b001SThomas Moestl 	DPRINTF(sc, ("loading rx ring %lx, tx ring %lx, cddma %lx\n",
82942c1b001SThomas Moestl 	    GEM_CDRXADDR(sc, 0), GEM_CDTXADDR(sc, 0), sc->sc_cddma));
83042c1b001SThomas Moestl 	CTR3(KTR_GEM, "loading rx ring %lx, tx ring %lx, cddma %lx",
83142c1b001SThomas Moestl 	    GEM_CDRXADDR(sc, 0), GEM_CDTXADDR(sc, 0), sc->sc_cddma);
83242c1b001SThomas Moestl 
83342c1b001SThomas Moestl 	/* step 8. Global Configuration & Interrupt Mask */
83442c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_INTMASK,
83542c1b001SThomas Moestl 		      ~(GEM_INTR_TX_INTME|
83642c1b001SThomas Moestl 			GEM_INTR_TX_EMPTY|
83742c1b001SThomas Moestl 			GEM_INTR_RX_DONE|GEM_INTR_RX_NOBUF|
83842c1b001SThomas Moestl 			GEM_INTR_RX_TAG_ERR|GEM_INTR_PCS|
83942c1b001SThomas Moestl 			GEM_INTR_MAC_CONTROL|GEM_INTR_MIF|
84042c1b001SThomas Moestl 			GEM_INTR_BERR));
841336cca9eSBenno Rice 	bus_space_write_4(t, h, GEM_MAC_RX_MASK,
842336cca9eSBenno Rice 			GEM_MAC_RX_DONE|GEM_MAC_RX_FRAME_CNT);
84342c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_TX_MASK, 0xffff); /* XXXX */
84442c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_CONTROL_MASK, 0); /* XXXX */
84542c1b001SThomas Moestl 
84642c1b001SThomas Moestl 	/* step 9. ETX Configuration: use mostly default values */
84742c1b001SThomas Moestl 
84842c1b001SThomas Moestl 	/* Enable DMA */
84942c1b001SThomas Moestl 	v = gem_ringsize(GEM_NTXDESC /*XXX*/);
85042c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_TX_CONFIG,
85142c1b001SThomas Moestl 		v|GEM_TX_CONFIG_TXDMA_EN|
85242c1b001SThomas Moestl 		((0x400<<10)&GEM_TX_CONFIG_TXFIFO_TH));
85342c1b001SThomas Moestl 
85442c1b001SThomas Moestl 	/* step 10. ERX Configuration */
85542c1b001SThomas Moestl 
85642c1b001SThomas Moestl 	/* Encode Receive Descriptor ring size: four possible values */
85742c1b001SThomas Moestl 	v = gem_ringsize(GEM_NRXDESC /*XXX*/);
85842c1b001SThomas Moestl 
85942c1b001SThomas Moestl 	/* Enable DMA */
86042c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RX_CONFIG,
86142c1b001SThomas Moestl 		v|(GEM_THRSH_1024<<GEM_RX_CONFIG_FIFO_THRS_SHIFT)|
86242c1b001SThomas Moestl 		(2<<GEM_RX_CONFIG_FBOFF_SHFT)|GEM_RX_CONFIG_RXDMA_EN|
86342c1b001SThomas Moestl 		(0<<GEM_RX_CONFIG_CXM_START_SHFT));
86442c1b001SThomas Moestl 	/*
865336cca9eSBenno Rice 	 * The following value is for an OFF Threshold of about 3/4 full
866336cca9eSBenno Rice 	 * and an ON Threshold of 1/4 full.
86742c1b001SThomas Moestl 	 */
868336cca9eSBenno Rice 	bus_space_write_4(t, h, GEM_RX_PAUSE_THRESH,
869336cca9eSBenno Rice 	    (3 * sc->sc_rxfifosize / 256) |
870336cca9eSBenno Rice 	    (   (sc->sc_rxfifosize / 256) << 12));
871336cca9eSBenno Rice 	bus_space_write_4(t, h, GEM_RX_BLANKING, (6<<12)|6);
87242c1b001SThomas Moestl 
87342c1b001SThomas Moestl 	/* step 11. Configure Media */
874336cca9eSBenno Rice 	mii_mediachg(sc->sc_mii);
87542c1b001SThomas Moestl 
87642c1b001SThomas Moestl 	/* step 12. RX_MAC Configuration Register */
87742c1b001SThomas Moestl 	v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
87842c1b001SThomas Moestl 	v |= GEM_MAC_RX_ENABLE;
87942c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
88042c1b001SThomas Moestl 
88142c1b001SThomas Moestl 	/* step 14. Issue Transmit Pending command */
88242c1b001SThomas Moestl 
88342c1b001SThomas Moestl 	/* Call MI initialization function if any */
88442c1b001SThomas Moestl 	if (sc->sc_hwinit)
88542c1b001SThomas Moestl 		(*sc->sc_hwinit)(sc);
88642c1b001SThomas Moestl 
88742c1b001SThomas Moestl 	/* step 15.  Give the reciever a swift kick */
88842c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_RX_KICK, GEM_NRXDESC-4);
88942c1b001SThomas Moestl 
89042c1b001SThomas Moestl 	/* Start the one second timer. */
89142c1b001SThomas Moestl 	callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
89242c1b001SThomas Moestl 
89342c1b001SThomas Moestl 	ifp->if_flags |= IFF_RUNNING;
89442c1b001SThomas Moestl 	ifp->if_flags &= ~IFF_OACTIVE;
89542c1b001SThomas Moestl 	ifp->if_timer = 0;
896336cca9eSBenno Rice 	sc->sc_ifflags = ifp->if_flags;
89742c1b001SThomas Moestl 	splx(s);
89842c1b001SThomas Moestl }
89942c1b001SThomas Moestl 
90042c1b001SThomas Moestl /*
90142c1b001SThomas Moestl  * XXX: This is really a substitute for bus_dmamap_load_mbuf(), which FreeBSD
90242c1b001SThomas Moestl  * does not yet have, with some adaptions for this driver.
90342c1b001SThomas Moestl  * Some changes are mandated by the fact that multiple maps may needed to map
90442c1b001SThomas Moestl  * a single mbuf.
90542c1b001SThomas Moestl  * It should be removed once generic support is available.
90642c1b001SThomas Moestl  *
90742c1b001SThomas Moestl  * This is derived from NetBSD (syssrc/sys/arch/sparc64/sparc64/machdep.c), for
90842c1b001SThomas Moestl  * a copyright notice see sparc64/sparc64/bus_machdep.c.
90942c1b001SThomas Moestl  *
91042c1b001SThomas Moestl  * Not every error condition is passed to the callback in this version, and the
91142c1b001SThomas Moestl  * callback may be called more than once.
91242c1b001SThomas Moestl  * It also gropes in the entails of the callback arg...
91342c1b001SThomas Moestl  */
91442c1b001SThomas Moestl static int
91542c1b001SThomas Moestl gem_dmamap_load_mbuf(sc, m0, cb, txj, flags)
91642c1b001SThomas Moestl 	struct gem_softc *sc;
91742c1b001SThomas Moestl 	struct mbuf *m0;
91842c1b001SThomas Moestl 	bus_dmamap_callback_t *cb;
91942c1b001SThomas Moestl 	struct gem_txjob *txj;
92042c1b001SThomas Moestl 	int flags;
92142c1b001SThomas Moestl {
92242c1b001SThomas Moestl 	struct gem_txdma txd;
92342c1b001SThomas Moestl 	struct gem_txsoft *txs;
92442c1b001SThomas Moestl 	struct mbuf *m;
92542c1b001SThomas Moestl 	void  *vaddr;
92642c1b001SThomas Moestl 	int error, first = 1, len, totlen;
92742c1b001SThomas Moestl 
92842c1b001SThomas Moestl 	if ((m0->m_flags & M_PKTHDR) == 0)
92942c1b001SThomas Moestl 		panic("gem_dmamap_load_mbuf: no packet header");
93042c1b001SThomas Moestl 	totlen = m0->m_pkthdr.len;
93142c1b001SThomas Moestl 	len = 0;
93242c1b001SThomas Moestl 	txd.txd_sc = sc;
93342c1b001SThomas Moestl 	txd.txd_nexttx = txj->txj_nexttx;
93442c1b001SThomas Moestl 	txj->txj_nsegs = 0;
93542c1b001SThomas Moestl 	STAILQ_INIT(&txj->txj_txsq);
93642c1b001SThomas Moestl 	m = m0;
93742c1b001SThomas Moestl 	while (m != NULL && len < totlen) {
93842c1b001SThomas Moestl 		if (m->m_len == 0)
93942c1b001SThomas Moestl 			continue;
94042c1b001SThomas Moestl 		/* Get a work queue entry. */
94142c1b001SThomas Moestl 		if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) {
94242c1b001SThomas Moestl 			/*
94342c1b001SThomas Moestl 			 * Ran out of descriptors, return a value that
94442c1b001SThomas Moestl 			 * cannot be returned by bus_dmamap_load to notify
94542c1b001SThomas Moestl 			 * the caller.
94642c1b001SThomas Moestl 			 */
94742c1b001SThomas Moestl 			error = -1;
94842c1b001SThomas Moestl 			goto fail;
94942c1b001SThomas Moestl 		}
95042c1b001SThomas Moestl 		len += m->m_len;
95142c1b001SThomas Moestl 		txd.txd_flags = first ? GTXD_FIRST : 0;
95242c1b001SThomas Moestl 		if (m->m_next == NULL || len >= totlen)
95342c1b001SThomas Moestl 			txd.txd_flags |= GTXD_LAST;
95442c1b001SThomas Moestl 		vaddr = mtod(m, void *);
95542c1b001SThomas Moestl 		error = bus_dmamap_load(sc->sc_dmatag, txs->txs_dmamap, vaddr,
95642c1b001SThomas Moestl 		    m->m_len, cb, &txd, flags);
95742c1b001SThomas Moestl 		if (error != 0 || txd.txd_error != 0)
95842c1b001SThomas Moestl 			goto fail;
95942c1b001SThomas Moestl 		/* Sync the DMA map. */
96042c1b001SThomas Moestl 		bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap,
96142c1b001SThomas Moestl 		    BUS_DMASYNC_PREWRITE);
96242c1b001SThomas Moestl 		m = m->m_next;
96342c1b001SThomas Moestl 		/*
96442c1b001SThomas Moestl 		 * Store a pointer to the packet so we can free it later,
96542c1b001SThomas Moestl 		 * and remember what txdirty will be once the packet is
96642c1b001SThomas Moestl 		 * done.
96742c1b001SThomas Moestl 		 */
96842c1b001SThomas Moestl 		txs->txs_mbuf = first ? m0 : NULL;
96942c1b001SThomas Moestl 		txs->txs_firstdesc = txj->txj_nexttx;
97042c1b001SThomas Moestl 		txs->txs_lastdesc = txd.txd_lasttx;
97142c1b001SThomas Moestl 		txs->txs_ndescs = txd.txd_nsegs;
97242c1b001SThomas Moestl 		CTR3(KTR_GEM, "load_mbuf: setting firstdesc=%d, lastdesc=%d, "
97342c1b001SThomas Moestl 		    "ndescs=%d", txs->txs_firstdesc, txs->txs_lastdesc,
97442c1b001SThomas Moestl 		    txs->txs_ndescs);
97542c1b001SThomas Moestl 		STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
97642c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&txj->txj_txsq, txs, txs_q);
97742c1b001SThomas Moestl 		txj->txj_nexttx = txd.txd_nexttx;
97842c1b001SThomas Moestl 		txj->txj_nsegs += txd.txd_nsegs;
97942c1b001SThomas Moestl 		first = 0;
98042c1b001SThomas Moestl 	}
98142c1b001SThomas Moestl 	txj->txj_lasttx = txd.txd_lasttx;
98242c1b001SThomas Moestl 	return (0);
98342c1b001SThomas Moestl 
98442c1b001SThomas Moestl fail:
98542c1b001SThomas Moestl 	CTR1(KTR_GEM, "gem_dmamap_load_mbuf failed (%d)", error);
98642c1b001SThomas Moestl 	gem_dmamap_unload_mbuf(sc, txj);
98742c1b001SThomas Moestl 	return (error);
98842c1b001SThomas Moestl }
98942c1b001SThomas Moestl 
99042c1b001SThomas Moestl /*
99142c1b001SThomas Moestl  * Unload an mbuf using the txd the information was placed in.
99242c1b001SThomas Moestl  * The tx interrupt code frees the tx segments one by one, because the txd is
99342c1b001SThomas Moestl  * not available any more.
99442c1b001SThomas Moestl  */
99542c1b001SThomas Moestl static void
99642c1b001SThomas Moestl gem_dmamap_unload_mbuf(sc, txj)
99742c1b001SThomas Moestl 	struct gem_softc *sc;
99842c1b001SThomas Moestl 	struct gem_txjob *txj;
99942c1b001SThomas Moestl {
100042c1b001SThomas Moestl 	struct gem_txsoft *txs;
100142c1b001SThomas Moestl 
100242c1b001SThomas Moestl 	/* Readd the removed descriptors and unload the segments. */
100342c1b001SThomas Moestl 	while ((txs = STAILQ_FIRST(&txj->txj_txsq)) != NULL) {
100442c1b001SThomas Moestl 		bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
100542c1b001SThomas Moestl 		STAILQ_REMOVE_HEAD(&txj->txj_txsq, txs_q);
100642c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
100742c1b001SThomas Moestl 	}
100842c1b001SThomas Moestl }
100942c1b001SThomas Moestl 
101042c1b001SThomas Moestl static void
101142c1b001SThomas Moestl gem_dmamap_commit_mbuf(sc, txj)
101242c1b001SThomas Moestl 	struct gem_softc *sc;
101342c1b001SThomas Moestl 	struct gem_txjob *txj;
101442c1b001SThomas Moestl {
101542c1b001SThomas Moestl 	struct gem_txsoft *txs;
101642c1b001SThomas Moestl 
101742c1b001SThomas Moestl 	/* Commit the txjob by transfering the txsoft's to the txdirtyq. */
101842c1b001SThomas Moestl 	while ((txs = STAILQ_FIRST(&txj->txj_txsq)) != NULL) {
101942c1b001SThomas Moestl 		STAILQ_REMOVE_HEAD(&txj->txj_txsq, txs_q);
102042c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
102142c1b001SThomas Moestl 	}
102242c1b001SThomas Moestl }
102342c1b001SThomas Moestl 
102442c1b001SThomas Moestl static void
102542c1b001SThomas Moestl gem_init_regs(sc)
102642c1b001SThomas Moestl 	struct gem_softc *sc;
102742c1b001SThomas Moestl {
102842c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
102942c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
1030336cca9eSBenno Rice 	const u_char *laddr = sc->sc_arpcom.ac_enaddr;
1031336cca9eSBenno Rice 	u_int32_t v;
103242c1b001SThomas Moestl 
103342c1b001SThomas Moestl 	/* These regs are not cleared on reset */
103442c1b001SThomas Moestl 	if (!sc->sc_inited) {
103542c1b001SThomas Moestl 
103642c1b001SThomas Moestl 		/* Wooo.  Magic values. */
103742c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_IPG0, 0);
103842c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_IPG1, 8);
103942c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_IPG2, 4);
104042c1b001SThomas Moestl 
104142c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN);
104242c1b001SThomas Moestl 		/* Max frame and max burst size */
104342c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
1044336cca9eSBenno Rice 		    ETHER_MAX_LEN | (0x2000<<16));
1045336cca9eSBenno Rice 
104642c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_PREAMBLE_LEN, 0x7);
104742c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_JAM_SIZE, 0x4);
104842c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ATTEMPT_LIMIT, 0x10);
104942c1b001SThomas Moestl 		/* Dunno.... */
105042c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_CONTROL_TYPE, 0x8088);
105142c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_RANDOM_SEED,
1052336cca9eSBenno Rice 		    ((laddr[5]<<8)|laddr[4])&0x3ff);
1053336cca9eSBenno Rice 
105442c1b001SThomas Moestl 		/* Secondary MAC addr set to 0:0:0:0:0:0 */
105542c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR3, 0);
105642c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR4, 0);
105742c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR5, 0);
1058336cca9eSBenno Rice 
1059336cca9eSBenno Rice 		/* MAC control addr set to 01:80:c2:00:00:01 */
106042c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR6, 0x0001);
106142c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR7, 0xc200);
106242c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR8, 0x0180);
106342c1b001SThomas Moestl 
106442c1b001SThomas Moestl 		/* MAC filter addr set to 0:0:0:0:0:0 */
106542c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER0, 0);
106642c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER1, 0);
106742c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER2, 0);
106842c1b001SThomas Moestl 
106942c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK1_2, 0);
107042c1b001SThomas Moestl 		bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK0, 0);
107142c1b001SThomas Moestl 
107242c1b001SThomas Moestl 		sc->sc_inited = 1;
107342c1b001SThomas Moestl 	}
107442c1b001SThomas Moestl 
107542c1b001SThomas Moestl 	/* Counters need to be zeroed */
107642c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_NORM_COLL_CNT, 0);
107742c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_FIRST_COLL_CNT, 0);
107842c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_EXCESS_COLL_CNT, 0);
107942c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_LATE_COLL_CNT, 0);
108042c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_DEFER_TMR_CNT, 0);
108142c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_PEAK_ATTEMPTS, 0);
108242c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_FRAME_COUNT, 0);
108342c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_LEN_ERR_CNT, 0);
108442c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_ALIGN_ERR, 0);
108542c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_CRC_ERR_CNT, 0);
108642c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_CODE_VIOL, 0);
108742c1b001SThomas Moestl 
108842c1b001SThomas Moestl 	/* Un-pause stuff */
108942c1b001SThomas Moestl #if 0
109042c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0);
109142c1b001SThomas Moestl #else
109242c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0);
109342c1b001SThomas Moestl #endif
109442c1b001SThomas Moestl 
109542c1b001SThomas Moestl 	/*
109642c1b001SThomas Moestl 	 * Set the station address.
109742c1b001SThomas Moestl 	 */
1098336cca9eSBenno Rice 	bus_space_write_4(t, h, GEM_MAC_ADDR0, (laddr[4]<<8)|laddr[5]);
1099336cca9eSBenno Rice 	bus_space_write_4(t, h, GEM_MAC_ADDR1, (laddr[2]<<8)|laddr[3]);
1100336cca9eSBenno Rice 	bus_space_write_4(t, h, GEM_MAC_ADDR2, (laddr[0]<<8)|laddr[1]);
1101336cca9eSBenno Rice 
1102336cca9eSBenno Rice 	/*
1103336cca9eSBenno Rice 	 * Enable MII outputs.  Enable GMII if there is a gigabit PHY.
1104336cca9eSBenno Rice 	 */
1105336cca9eSBenno Rice 	sc->sc_mif_config = bus_space_read_4(t, h, GEM_MIF_CONFIG);
1106336cca9eSBenno Rice 	v = GEM_MAC_XIF_TX_MII_ENA;
1107336cca9eSBenno Rice 	if (sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) {
1108336cca9eSBenno Rice 		v |= GEM_MAC_XIF_FDPLX_LED;
1109336cca9eSBenno Rice 		if (sc->sc_flags & GEM_GIGABIT)
1110336cca9eSBenno Rice 			v |= GEM_MAC_XIF_GMII_MODE;
1111336cca9eSBenno Rice 	}
1112336cca9eSBenno Rice 	bus_space_write_4(t, h, GEM_MAC_XIF_CONFIG, v);
111342c1b001SThomas Moestl }
111442c1b001SThomas Moestl 
111542c1b001SThomas Moestl static void
111642c1b001SThomas Moestl gem_start(ifp)
111742c1b001SThomas Moestl 	struct ifnet *ifp;
111842c1b001SThomas Moestl {
111942c1b001SThomas Moestl 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
112042c1b001SThomas Moestl 	struct mbuf *m0 = NULL, *m;
112142c1b001SThomas Moestl 	struct gem_txjob txj;
112242c1b001SThomas Moestl 	int firsttx, ofree, seg, ntx, txmfail;
112342c1b001SThomas Moestl 
112442c1b001SThomas Moestl 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
112542c1b001SThomas Moestl 		return;
112642c1b001SThomas Moestl 
112742c1b001SThomas Moestl 	/*
112842c1b001SThomas Moestl 	 * Remember the previous number of free descriptors and
112942c1b001SThomas Moestl 	 * the first descriptor we'll use.
113042c1b001SThomas Moestl 	 */
113142c1b001SThomas Moestl 	ofree = sc->sc_txfree;
113242c1b001SThomas Moestl 	firsttx = sc->sc_txnext;
113342c1b001SThomas Moestl 
113442c1b001SThomas Moestl 	DPRINTF(sc, ("%s: gem_start: txfree %d, txnext %d\n",
113542c1b001SThomas Moestl 	    device_get_name(sc->sc_dev), ofree, firsttx));
113642c1b001SThomas Moestl 	CTR3(KTR_GEM, "%s: gem_start: txfree %d, txnext %d",
113742c1b001SThomas Moestl 	    device_get_name(sc->sc_dev), ofree, firsttx);
113842c1b001SThomas Moestl 
113942c1b001SThomas Moestl 	txj.txj_nexttx = firsttx;
114042c1b001SThomas Moestl 	txj.txj_lasttx = 0;
114142c1b001SThomas Moestl 	/*
114242c1b001SThomas Moestl 	 * Loop through the send queue, setting up transmit descriptors
114342c1b001SThomas Moestl 	 * until we drain the queue, or use up all available transmit
114442c1b001SThomas Moestl 	 * descriptors.
114542c1b001SThomas Moestl 	 */
114642c1b001SThomas Moestl 	txmfail = 0;
114742c1b001SThomas Moestl 	for (ntx = 0;; ntx++) {
114842c1b001SThomas Moestl 		/*
114942c1b001SThomas Moestl 		 * Grab a packet off the queue.
115042c1b001SThomas Moestl 		 */
115142c1b001SThomas Moestl 		IF_DEQUEUE(&ifp->if_snd, m0);
115242c1b001SThomas Moestl 		if (m0 == NULL)
115342c1b001SThomas Moestl 			break;
115442c1b001SThomas Moestl 		m = NULL;
115542c1b001SThomas Moestl 
115642c1b001SThomas Moestl 		/*
115742c1b001SThomas Moestl 		 * Load the DMA map.  If this fails, the packet either
115842c1b001SThomas Moestl 		 * didn't fit in the alloted number of segments, or we were
115942c1b001SThomas Moestl 		 * short on resources.  In this case, we'll copy and try
116042c1b001SThomas Moestl 		 * again.
116142c1b001SThomas Moestl 		 */
116242c1b001SThomas Moestl 		txmfail = gem_dmamap_load_mbuf(sc, m0,
116342c1b001SThomas Moestl 		    gem_txdma_callback, &txj, BUS_DMA_NOWAIT);
116442c1b001SThomas Moestl 		if (txmfail == -1) {
116542c1b001SThomas Moestl 			IF_PREPEND(&ifp->if_snd, m0);
116642c1b001SThomas Moestl 			break;
116742c1b001SThomas Moestl 		}
116842c1b001SThomas Moestl 		if (txmfail > 0) {
116942c1b001SThomas Moestl 			MGETHDR(m, M_DONTWAIT, MT_DATA);
117042c1b001SThomas Moestl 			if (m == NULL) {
117142c1b001SThomas Moestl 				device_printf(sc->sc_dev, "unable to "
117242c1b001SThomas Moestl 				    "allocate Tx mbuf\n");
117342c1b001SThomas Moestl 				/* Failed; requeue. */
117442c1b001SThomas Moestl 				IF_PREPEND(&ifp->if_snd, m0);
117542c1b001SThomas Moestl 				break;
117642c1b001SThomas Moestl 			}
117742c1b001SThomas Moestl 			if (m0->m_pkthdr.len > MHLEN) {
117842c1b001SThomas Moestl 				MCLGET(m, M_DONTWAIT);
117942c1b001SThomas Moestl 				if ((m->m_flags & M_EXT) == 0) {
118042c1b001SThomas Moestl 					device_printf(sc->sc_dev, "unable to "
118142c1b001SThomas Moestl 					    "allocate Tx cluster\n");
118242c1b001SThomas Moestl 					IF_PREPEND(&ifp->if_snd, m0);
118342c1b001SThomas Moestl 					m_freem(m);
118442c1b001SThomas Moestl 					break;
118542c1b001SThomas Moestl 				}
118642c1b001SThomas Moestl 			}
118742c1b001SThomas Moestl 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
118842c1b001SThomas Moestl 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
118942c1b001SThomas Moestl 			txmfail = gem_dmamap_load_mbuf(sc, m,
119042c1b001SThomas Moestl 			    gem_txdma_callback, &txj, BUS_DMA_NOWAIT);
119142c1b001SThomas Moestl 			if (txmfail != 0) {
119242c1b001SThomas Moestl 				if (txmfail > 0) {
119342c1b001SThomas Moestl 					device_printf(sc->sc_dev, "unable to "
119442c1b001SThomas Moestl 					    "load Tx buffer, error = %d\n",
119542c1b001SThomas Moestl 					    txmfail);
119642c1b001SThomas Moestl 				}
119742c1b001SThomas Moestl 				m_freem(m);
119842c1b001SThomas Moestl 				IF_PREPEND(&ifp->if_snd, m0);
119942c1b001SThomas Moestl 				break;
120042c1b001SThomas Moestl 			}
120142c1b001SThomas Moestl 		}
120242c1b001SThomas Moestl 
120342c1b001SThomas Moestl 		/*
120442c1b001SThomas Moestl 		 * Ensure we have enough descriptors free to describe
120542c1b001SThomas Moestl 		 * the packet.  Note, we always reserve one descriptor
120642c1b001SThomas Moestl 		 * at the end of the ring as a termination point, to
120742c1b001SThomas Moestl 		 * prevent wrap-around.
120842c1b001SThomas Moestl 		 */
120942c1b001SThomas Moestl 		if (txj.txj_nsegs > (sc->sc_txfree - 1)) {
121042c1b001SThomas Moestl 			/*
121142c1b001SThomas Moestl 			 * Not enough free descriptors to transmit this
121242c1b001SThomas Moestl 			 * packet.  We haven't committed to anything yet,
121342c1b001SThomas Moestl 			 * so just unload the DMA map, put the packet
121442c1b001SThomas Moestl 			 * back on the queue, and punt.  Notify the upper
121542c1b001SThomas Moestl 			 * layer that there are no more slots left.
121642c1b001SThomas Moestl 			 *
121742c1b001SThomas Moestl 			 * XXX We could allocate an mbuf and copy, but
121842c1b001SThomas Moestl 			 * XXX it is worth it?
121942c1b001SThomas Moestl 			 */
122042c1b001SThomas Moestl 			ifp->if_flags |= IFF_OACTIVE;
122142c1b001SThomas Moestl 			gem_dmamap_unload_mbuf(sc, &txj);
122242c1b001SThomas Moestl 			if (m != NULL)
122342c1b001SThomas Moestl 				m_freem(m);
122442c1b001SThomas Moestl 			IF_PREPEND(&ifp->if_snd, m0);
122542c1b001SThomas Moestl 			break;
122642c1b001SThomas Moestl 		}
122742c1b001SThomas Moestl 
122842c1b001SThomas Moestl 		/*
122942c1b001SThomas Moestl 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
123042c1b001SThomas Moestl 		 */
123108e0fdebSThomas Moestl 		if (ifp->if_bpf != NULL)
123208e0fdebSThomas Moestl 			bpf_mtap(ifp, m0);
123308e0fdebSThomas Moestl 
123408e0fdebSThomas Moestl 		if (m != NULL)
123508e0fdebSThomas Moestl 			m_freem(m0);
123642c1b001SThomas Moestl 
123742c1b001SThomas Moestl #ifdef GEM_DEBUG
123842c1b001SThomas Moestl 		if (ifp->if_flags & IFF_DEBUG) {
123942c1b001SThomas Moestl 			printf("     gem_start %p transmit chain:\n",
124042c1b001SThomas Moestl 			    STAILQ_FIRST(&txj.txj_txsq));
124142c1b001SThomas Moestl 			for (seg = sc->sc_txnext;; seg = GEM_NEXTTX(seg)) {
124242c1b001SThomas Moestl 				printf("descriptor %d:\t", seg);
124342c1b001SThomas Moestl 				printf("gd_flags:   0x%016llx\t", (long long)
124442c1b001SThomas Moestl 					GEM_DMA_READ(sc, sc->sc_txdescs[seg].gd_flags));
124542c1b001SThomas Moestl 				printf("gd_addr: 0x%016llx\n", (long long)
124642c1b001SThomas Moestl 					GEM_DMA_READ(sc, sc->sc_txdescs[seg].gd_addr));
124742c1b001SThomas Moestl 				if (seg == txj.txj_lasttx)
124842c1b001SThomas Moestl 					break;
124942c1b001SThomas Moestl 			}
125042c1b001SThomas Moestl 		}
125142c1b001SThomas Moestl #endif
125242c1b001SThomas Moestl 
125342c1b001SThomas Moestl 		/* Sync the descriptors we're using. */
125442c1b001SThomas Moestl 		GEM_CDTXSYNC(sc, sc->sc_txnext, txj.txj_nsegs,
125542c1b001SThomas Moestl 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
125642c1b001SThomas Moestl 
125742c1b001SThomas Moestl 		/* Advance the tx pointer. */
125842c1b001SThomas Moestl 		sc->sc_txfree -= txj.txj_nsegs;
125942c1b001SThomas Moestl 		sc->sc_txnext = txj.txj_nexttx;
126042c1b001SThomas Moestl 
126142c1b001SThomas Moestl 		gem_dmamap_commit_mbuf(sc, &txj);
126242c1b001SThomas Moestl 	}
126342c1b001SThomas Moestl 
126442c1b001SThomas Moestl 	if (txmfail == -1 || sc->sc_txfree == 0) {
126542c1b001SThomas Moestl 		ifp->if_flags |= IFF_OACTIVE;
126642c1b001SThomas Moestl 		/* No more slots left; notify upper layer. */
126742c1b001SThomas Moestl 	}
126842c1b001SThomas Moestl 
126942c1b001SThomas Moestl 	if (ntx > 0) {
127042c1b001SThomas Moestl 		DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
127142c1b001SThomas Moestl 		    device_get_name(sc->sc_dev), txj.txj_lasttx, firsttx));
127242c1b001SThomas Moestl 		CTR3(KTR_GEM, "%s: packets enqueued, IC on %d, OWN on %d",
127342c1b001SThomas Moestl 		    device_get_name(sc->sc_dev), txj.txj_lasttx, firsttx);
127442c1b001SThomas Moestl 		/*
127542c1b001SThomas Moestl 		 * The entire packet chain is set up.
127642c1b001SThomas Moestl 		 * Kick the transmitter.
127742c1b001SThomas Moestl 		 */
127842c1b001SThomas Moestl 		DPRINTF(sc, ("%s: gem_start: kicking tx %d\n",
127942c1b001SThomas Moestl 			device_get_name(sc->sc_dev), txj.txj_nexttx));
128042c1b001SThomas Moestl 		CTR3(KTR_GEM, "%s: gem_start: kicking tx %d=%d",
128142c1b001SThomas Moestl 		    device_get_name(sc->sc_dev), txj.txj_nexttx,
128242c1b001SThomas Moestl 		    sc->sc_txnext);
128342c1b001SThomas Moestl 		bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_TX_KICK,
128442c1b001SThomas Moestl 			sc->sc_txnext);
128542c1b001SThomas Moestl 
128642c1b001SThomas Moestl 		/* Set a watchdog timer in case the chip flakes out. */
128742c1b001SThomas Moestl 		ifp->if_timer = 5;
128842c1b001SThomas Moestl 		DPRINTF(sc, ("%s: gem_start: watchdog %d\n",
128942c1b001SThomas Moestl 			device_get_name(sc->sc_dev), ifp->if_timer));
129042c1b001SThomas Moestl 		CTR2(KTR_GEM, "%s: gem_start: watchdog %d",
129142c1b001SThomas Moestl 			device_get_name(sc->sc_dev), ifp->if_timer);
129242c1b001SThomas Moestl 	}
129342c1b001SThomas Moestl }
129442c1b001SThomas Moestl 
129542c1b001SThomas Moestl /*
129642c1b001SThomas Moestl  * Transmit interrupt.
129742c1b001SThomas Moestl  */
129842c1b001SThomas Moestl static void
129942c1b001SThomas Moestl gem_tint(sc)
130042c1b001SThomas Moestl 	struct gem_softc *sc;
130142c1b001SThomas Moestl {
130242c1b001SThomas Moestl 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
130342c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
130442c1b001SThomas Moestl 	bus_space_handle_t mac = sc->sc_h;
130542c1b001SThomas Moestl 	struct gem_txsoft *txs;
130642c1b001SThomas Moestl 	int txlast;
1307336cca9eSBenno Rice 	int progress = 0;
130842c1b001SThomas Moestl 
130942c1b001SThomas Moestl 
131042c1b001SThomas Moestl 	DPRINTF(sc, ("%s: gem_tint\n", device_get_name(sc->sc_dev)));
131142c1b001SThomas Moestl 	CTR1(KTR_GEM, "%s: gem_tint", device_get_name(sc->sc_dev));
131242c1b001SThomas Moestl 
131342c1b001SThomas Moestl 	/*
131442c1b001SThomas Moestl 	 * Unload collision counters
131542c1b001SThomas Moestl 	 */
131642c1b001SThomas Moestl 	ifp->if_collisions +=
131742c1b001SThomas Moestl 		bus_space_read_4(t, mac, GEM_MAC_NORM_COLL_CNT) +
131842c1b001SThomas Moestl 		bus_space_read_4(t, mac, GEM_MAC_FIRST_COLL_CNT) +
131942c1b001SThomas Moestl 		bus_space_read_4(t, mac, GEM_MAC_EXCESS_COLL_CNT) +
132042c1b001SThomas Moestl 		bus_space_read_4(t, mac, GEM_MAC_LATE_COLL_CNT);
132142c1b001SThomas Moestl 
132242c1b001SThomas Moestl 	/*
132342c1b001SThomas Moestl 	 * then clear the hardware counters.
132442c1b001SThomas Moestl 	 */
132542c1b001SThomas Moestl 	bus_space_write_4(t, mac, GEM_MAC_NORM_COLL_CNT, 0);
132642c1b001SThomas Moestl 	bus_space_write_4(t, mac, GEM_MAC_FIRST_COLL_CNT, 0);
132742c1b001SThomas Moestl 	bus_space_write_4(t, mac, GEM_MAC_EXCESS_COLL_CNT, 0);
132842c1b001SThomas Moestl 	bus_space_write_4(t, mac, GEM_MAC_LATE_COLL_CNT, 0);
132942c1b001SThomas Moestl 
133042c1b001SThomas Moestl 	/*
133142c1b001SThomas Moestl 	 * Go through our Tx list and free mbufs for those
133242c1b001SThomas Moestl 	 * frames that have been transmitted.
133342c1b001SThomas Moestl 	 */
133442c1b001SThomas Moestl 	while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
133542c1b001SThomas Moestl 		GEM_CDTXSYNC(sc, txs->txs_lastdesc,
133642c1b001SThomas Moestl 		    txs->txs_ndescs,
133742c1b001SThomas Moestl 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
133842c1b001SThomas Moestl 
133942c1b001SThomas Moestl #ifdef GEM_DEBUG
134042c1b001SThomas Moestl 		if (ifp->if_flags & IFF_DEBUG) {
134142c1b001SThomas Moestl 			int i;
134242c1b001SThomas Moestl 			printf("    txsoft %p transmit chain:\n", txs);
134342c1b001SThomas Moestl 			for (i = txs->txs_firstdesc;; i = GEM_NEXTTX(i)) {
134442c1b001SThomas Moestl 				printf("descriptor %d: ", i);
134542c1b001SThomas Moestl 				printf("gd_flags: 0x%016llx\t", (long long)
134642c1b001SThomas Moestl 					GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_flags));
134742c1b001SThomas Moestl 				printf("gd_addr: 0x%016llx\n", (long long)
134842c1b001SThomas Moestl 					GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_addr));
134942c1b001SThomas Moestl 				if (i == txs->txs_lastdesc)
135042c1b001SThomas Moestl 					break;
135142c1b001SThomas Moestl 			}
135242c1b001SThomas Moestl 		}
135342c1b001SThomas Moestl #endif
135442c1b001SThomas Moestl 
135542c1b001SThomas Moestl 		/*
135642c1b001SThomas Moestl 		 * In theory, we could harveast some descriptors before
135742c1b001SThomas Moestl 		 * the ring is empty, but that's a bit complicated.
135842c1b001SThomas Moestl 		 *
135942c1b001SThomas Moestl 		 * GEM_TX_COMPLETION points to the last descriptor
136042c1b001SThomas Moestl 		 * processed +1.
136142c1b001SThomas Moestl 		 */
136242c1b001SThomas Moestl 		txlast = bus_space_read_4(t, mac, GEM_TX_COMPLETION);
136342c1b001SThomas Moestl 		DPRINTF(sc,
136442c1b001SThomas Moestl 			("gem_tint: txs->txs_lastdesc = %d, txlast = %d\n",
136542c1b001SThomas Moestl 				txs->txs_lastdesc, txlast));
136642c1b001SThomas Moestl 		CTR3(KTR_GEM, "gem_tint: txs->txs_firstdesc = %d, "
136742c1b001SThomas Moestl 		    "txs->txs_lastdesc = %d, txlast = %d",
136842c1b001SThomas Moestl 		    txs->txs_firstdesc, txs->txs_lastdesc, txlast);
136942c1b001SThomas Moestl 		if (txs->txs_firstdesc <= txs->txs_lastdesc) {
137042c1b001SThomas Moestl 			if ((txlast >= txs->txs_firstdesc) &&
137142c1b001SThomas Moestl 				(txlast <= txs->txs_lastdesc))
137242c1b001SThomas Moestl 				break;
137342c1b001SThomas Moestl 		} else {
137442c1b001SThomas Moestl 			/* Ick -- this command wraps */
137542c1b001SThomas Moestl 			if ((txlast >= txs->txs_firstdesc) ||
137642c1b001SThomas Moestl 				(txlast <= txs->txs_lastdesc))
137742c1b001SThomas Moestl 				break;
137842c1b001SThomas Moestl 		}
137942c1b001SThomas Moestl 
138042c1b001SThomas Moestl 		DPRINTF(sc, ("gem_tint: releasing a desc\n"));
138142c1b001SThomas Moestl 		CTR0(KTR_GEM, "gem_tint: releasing a desc");
138242c1b001SThomas Moestl 		STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
138342c1b001SThomas Moestl 
138442c1b001SThomas Moestl 		sc->sc_txfree += txs->txs_ndescs;
138542c1b001SThomas Moestl 
138642c1b001SThomas Moestl 		bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap,
138742c1b001SThomas Moestl 		    BUS_DMASYNC_POSTWRITE);
138842c1b001SThomas Moestl 		bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
138942c1b001SThomas Moestl 		if (txs->txs_mbuf != NULL) {
139042c1b001SThomas Moestl 			m_freem(txs->txs_mbuf);
139142c1b001SThomas Moestl 			txs->txs_mbuf = NULL;
139242c1b001SThomas Moestl 		}
139342c1b001SThomas Moestl 
139442c1b001SThomas Moestl 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
139542c1b001SThomas Moestl 
139642c1b001SThomas Moestl 		ifp->if_opackets++;
1397336cca9eSBenno Rice 		progress = 1;
139842c1b001SThomas Moestl 	}
139942c1b001SThomas Moestl 
140042c1b001SThomas Moestl 	DPRINTF(sc, ("gem_tint: GEM_TX_STATE_MACHINE %x "
140142c1b001SThomas Moestl 		"GEM_TX_DATA_PTR %llx "
140242c1b001SThomas Moestl 		"GEM_TX_COMPLETION %x\n",
140342c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_STATE_MACHINE),
140442c1b001SThomas Moestl 		((long long) bus_space_read_4(sc->sc_bustag, sc->sc_h,
140542c1b001SThomas Moestl 			GEM_TX_DATA_PTR_HI) << 32) |
140642c1b001SThomas Moestl 			     bus_space_read_4(sc->sc_bustag, sc->sc_h,
140742c1b001SThomas Moestl 			GEM_TX_DATA_PTR_LO),
140842c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_COMPLETION)));
140942c1b001SThomas Moestl 	CTR3(KTR_GEM, "gem_tint: GEM_TX_STATE_MACHINE %x "
141042c1b001SThomas Moestl 		"GEM_TX_DATA_PTR %llx "
141142c1b001SThomas Moestl 		"GEM_TX_COMPLETION %x",
141242c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_STATE_MACHINE),
141342c1b001SThomas Moestl 		((long long) bus_space_read_4(sc->sc_bustag, sc->sc_h,
141442c1b001SThomas Moestl 			GEM_TX_DATA_PTR_HI) << 32) |
141542c1b001SThomas Moestl 			     bus_space_read_4(sc->sc_bustag, sc->sc_h,
141642c1b001SThomas Moestl 			GEM_TX_DATA_PTR_LO),
141742c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_COMPLETION));
141842c1b001SThomas Moestl 
1419336cca9eSBenno Rice 	if (progress) {
1420336cca9eSBenno Rice 		if (sc->sc_txfree == GEM_NTXDESC - 1)
1421336cca9eSBenno Rice 			sc->sc_txwin = 0;
142242c1b001SThomas Moestl 
1423336cca9eSBenno Rice 		/* Freed some descriptors, so reset IFF_OACTIVE and restart. */
1424336cca9eSBenno Rice 		ifp->if_flags &= ~IFF_OACTIVE;
1425336cca9eSBenno Rice 		gem_start(ifp);
1426336cca9eSBenno Rice 
1427336cca9eSBenno Rice 		if (STAILQ_EMPTY(&sc->sc_txdirtyq))
1428336cca9eSBenno Rice 			ifp->if_timer = 0;
1429336cca9eSBenno Rice 	}
143042c1b001SThomas Moestl 
143142c1b001SThomas Moestl 	DPRINTF(sc, ("%s: gem_tint: watchdog %d\n",
143242c1b001SThomas Moestl 		device_get_name(sc->sc_dev), ifp->if_timer));
143342c1b001SThomas Moestl 	CTR2(KTR_GEM, "%s: gem_tint: watchdog %d",
143442c1b001SThomas Moestl 		device_get_name(sc->sc_dev), ifp->if_timer);
143542c1b001SThomas Moestl }
143642c1b001SThomas Moestl 
143711e3f060SJake Burkholder #if 0
14380d80b9bdSThomas Moestl static void
14390d80b9bdSThomas Moestl gem_rint_timeout(arg)
14400d80b9bdSThomas Moestl 	void *arg;
14410d80b9bdSThomas Moestl {
14420d80b9bdSThomas Moestl 
14430d80b9bdSThomas Moestl 	gem_rint((struct gem_softc *)arg);
14440d80b9bdSThomas Moestl }
144511e3f060SJake Burkholder #endif
14460d80b9bdSThomas Moestl 
144742c1b001SThomas Moestl /*
144842c1b001SThomas Moestl  * Receive interrupt.
144942c1b001SThomas Moestl  */
145042c1b001SThomas Moestl static void
145142c1b001SThomas Moestl gem_rint(sc)
145242c1b001SThomas Moestl 	struct gem_softc *sc;
145342c1b001SThomas Moestl {
145442c1b001SThomas Moestl 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
145542c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
145642c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
145742c1b001SThomas Moestl 	struct ether_header *eh;
145842c1b001SThomas Moestl 	struct gem_rxsoft *rxs;
145942c1b001SThomas Moestl 	struct mbuf *m;
146042c1b001SThomas Moestl 	u_int64_t rxstat;
1461336cca9eSBenno Rice 	u_int32_t rxcomp;
1462336cca9eSBenno Rice 	int i, len, progress = 0;
146342c1b001SThomas Moestl 
14640d80b9bdSThomas Moestl 	callout_stop(&sc->sc_rx_ch);
146542c1b001SThomas Moestl 	DPRINTF(sc, ("%s: gem_rint\n", device_get_name(sc->sc_dev)));
146642c1b001SThomas Moestl 	CTR1(KTR_GEM, "%s: gem_rint", device_get_name(sc->sc_dev));
1467336cca9eSBenno Rice 
1468336cca9eSBenno Rice 	/*
1469336cca9eSBenno Rice 	 * Read the completion register once.  This limits
1470336cca9eSBenno Rice 	 * how long the following loop can execute.
1471336cca9eSBenno Rice 	 */
1472336cca9eSBenno Rice 	rxcomp = bus_space_read_4(t, h, GEM_RX_COMPLETION);
1473336cca9eSBenno Rice 
147442c1b001SThomas Moestl 	/*
147542c1b001SThomas Moestl 	 * XXXX Read the lastrx only once at the top for speed.
147642c1b001SThomas Moestl 	 */
147742c1b001SThomas Moestl 	DPRINTF(sc, ("gem_rint: sc->rxptr %d, complete %d\n",
1478336cca9eSBenno Rice 		sc->sc_rxptr, rxcomp));
147942c1b001SThomas Moestl 	CTR2(KTR_GEM, "gem_rint: sc->rxptr %d, complete %d",
1480336cca9eSBenno Rice 	    sc->sc_rxptr, rxcomp);
1481336cca9eSBenno Rice 	for (i = sc->sc_rxptr; i != rxcomp;
148242c1b001SThomas Moestl 	     i = GEM_NEXTRX(i)) {
148342c1b001SThomas Moestl 		rxs = &sc->sc_rxsoft[i];
148442c1b001SThomas Moestl 
148542c1b001SThomas Moestl 		GEM_CDRXSYNC(sc, i,
148642c1b001SThomas Moestl 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
148742c1b001SThomas Moestl 
148842c1b001SThomas Moestl 		rxstat = GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags);
148942c1b001SThomas Moestl 
149042c1b001SThomas Moestl 		if (rxstat & GEM_RD_OWN) {
1491336cca9eSBenno Rice #if 0 /* XXX: In case of emergency, re-enable this. */
149242c1b001SThomas Moestl 			/*
14930d80b9bdSThomas Moestl 			 * The descriptor is still marked as owned, although
14940d80b9bdSThomas Moestl 			 * it is supposed to have completed. This has been
14950d80b9bdSThomas Moestl 			 * observed on some machines. Just exiting here
14960d80b9bdSThomas Moestl 			 * might leave the packet sitting around until another
14970d80b9bdSThomas Moestl 			 * one arrives to trigger a new interrupt, which is
14980d80b9bdSThomas Moestl 			 * generally undesirable, so set up a timeout.
149942c1b001SThomas Moestl 			 */
15000d80b9bdSThomas Moestl 			callout_reset(&sc->sc_rx_ch, GEM_RXOWN_TICKS,
15010d80b9bdSThomas Moestl 			    gem_rint_timeout, sc);
1502336cca9eSBenno Rice #endif
150342c1b001SThomas Moestl 			break;
150442c1b001SThomas Moestl 		}
150542c1b001SThomas Moestl 
1506336cca9eSBenno Rice 		progress++;
1507336cca9eSBenno Rice 		ifp->if_ipackets++;
1508336cca9eSBenno Rice 
150942c1b001SThomas Moestl 		if (rxstat & GEM_RD_BAD_CRC) {
1510336cca9eSBenno Rice 			ifp->if_ierrors++;
151142c1b001SThomas Moestl 			device_printf(sc->sc_dev, "receive error: CRC error\n");
151242c1b001SThomas Moestl 			GEM_INIT_RXDESC(sc, i);
151342c1b001SThomas Moestl 			continue;
151442c1b001SThomas Moestl 		}
151542c1b001SThomas Moestl 
151642c1b001SThomas Moestl 		bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap,
151742c1b001SThomas Moestl 		    BUS_DMASYNC_POSTREAD);
151842c1b001SThomas Moestl #ifdef GEM_DEBUG
151942c1b001SThomas Moestl 		if (ifp->if_flags & IFF_DEBUG) {
152042c1b001SThomas Moestl 			printf("    rxsoft %p descriptor %d: ", rxs, i);
152142c1b001SThomas Moestl 			printf("gd_flags: 0x%016llx\t", (long long)
152242c1b001SThomas Moestl 				GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags));
152342c1b001SThomas Moestl 			printf("gd_addr: 0x%016llx\n", (long long)
152442c1b001SThomas Moestl 				GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_addr));
152542c1b001SThomas Moestl 		}
152642c1b001SThomas Moestl #endif
152742c1b001SThomas Moestl 
152842c1b001SThomas Moestl 		/*
152942c1b001SThomas Moestl 		 * No errors; receive the packet.  Note the Gem
153042c1b001SThomas Moestl 		 * includes the CRC with every packet.
153142c1b001SThomas Moestl 		 */
153242c1b001SThomas Moestl 		len = GEM_RD_BUFLEN(rxstat);
153342c1b001SThomas Moestl 
153442c1b001SThomas Moestl 		/*
153542c1b001SThomas Moestl 		 * Allocate a new mbuf cluster.  If that fails, we are
153642c1b001SThomas Moestl 		 * out of memory, and must drop the packet and recycle
153742c1b001SThomas Moestl 		 * the buffer that's already attached to this descriptor.
153842c1b001SThomas Moestl 		 */
153942c1b001SThomas Moestl 		m = rxs->rxs_mbuf;
154042c1b001SThomas Moestl 		if (gem_add_rxbuf(sc, i) != 0) {
154142c1b001SThomas Moestl 			ifp->if_ierrors++;
154242c1b001SThomas Moestl 			GEM_INIT_RXDESC(sc, i);
154342c1b001SThomas Moestl 			bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap,
154442c1b001SThomas Moestl 			    BUS_DMASYNC_PREREAD);
154542c1b001SThomas Moestl 			continue;
154642c1b001SThomas Moestl 		}
154742c1b001SThomas Moestl 		m->m_data += 2; /* We're already off by two */
154842c1b001SThomas Moestl 
154942c1b001SThomas Moestl 		eh = mtod(m, struct ether_header *);
155042c1b001SThomas Moestl 		m->m_pkthdr.rcvif = ifp;
155142c1b001SThomas Moestl 		m->m_pkthdr.len = m->m_len = len - ETHER_CRC_LEN;
155242c1b001SThomas Moestl 		m_adj(m, sizeof(struct ether_header));
155342c1b001SThomas Moestl 
155442c1b001SThomas Moestl 		/* Pass it on. */
155542c1b001SThomas Moestl 		ether_input(ifp, eh, m);
155642c1b001SThomas Moestl 	}
155742c1b001SThomas Moestl 
1558336cca9eSBenno Rice 	if (progress) {
155942c1b001SThomas Moestl 		/* Update the receive pointer. */
1560336cca9eSBenno Rice 		if (i == sc->sc_rxptr) {
1561336cca9eSBenno Rice 			device_printf(sc->sc_dev, "rint: ring wrap\n");
1562336cca9eSBenno Rice 		}
156342c1b001SThomas Moestl 		sc->sc_rxptr = i;
1564336cca9eSBenno Rice 		bus_space_write_4(t, h, GEM_RX_KICK, GEM_PREVRX(i));
1565336cca9eSBenno Rice 	}
156642c1b001SThomas Moestl 
156742c1b001SThomas Moestl 	DPRINTF(sc, ("gem_rint: done sc->rxptr %d, complete %d\n",
156842c1b001SThomas Moestl 		sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION)));
156942c1b001SThomas Moestl 	CTR2(KTR_GEM, "gem_rint: done sc->rxptr %d, complete %d",
157042c1b001SThomas Moestl 		sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION));
157142c1b001SThomas Moestl 
157242c1b001SThomas Moestl }
157342c1b001SThomas Moestl 
157442c1b001SThomas Moestl 
157542c1b001SThomas Moestl /*
157642c1b001SThomas Moestl  * gem_add_rxbuf:
157742c1b001SThomas Moestl  *
157842c1b001SThomas Moestl  *	Add a receive buffer to the indicated descriptor.
157942c1b001SThomas Moestl  */
158042c1b001SThomas Moestl static int
158142c1b001SThomas Moestl gem_add_rxbuf(sc, idx)
158242c1b001SThomas Moestl 	struct gem_softc *sc;
158342c1b001SThomas Moestl 	int idx;
158442c1b001SThomas Moestl {
158542c1b001SThomas Moestl 	struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx];
158642c1b001SThomas Moestl 	struct mbuf *m;
158742c1b001SThomas Moestl 	int error;
158842c1b001SThomas Moestl 
158942c1b001SThomas Moestl 	MGETHDR(m, M_DONTWAIT, MT_DATA);
159042c1b001SThomas Moestl 	if (m == NULL)
159142c1b001SThomas Moestl 		return (ENOBUFS);
159242c1b001SThomas Moestl 
159342c1b001SThomas Moestl 	MCLGET(m, M_DONTWAIT);
159442c1b001SThomas Moestl 	if ((m->m_flags & M_EXT) == 0) {
159542c1b001SThomas Moestl 		m_freem(m);
159642c1b001SThomas Moestl 		return (ENOBUFS);
159742c1b001SThomas Moestl 	}
159842c1b001SThomas Moestl 
159942c1b001SThomas Moestl #ifdef GEM_DEBUG
160042c1b001SThomas Moestl 	/* bzero the packet to check dma */
160142c1b001SThomas Moestl 	memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size);
160242c1b001SThomas Moestl #endif
160342c1b001SThomas Moestl 
160442c1b001SThomas Moestl 	if (rxs->rxs_mbuf != NULL)
160542c1b001SThomas Moestl 		bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
160642c1b001SThomas Moestl 
160742c1b001SThomas Moestl 	rxs->rxs_mbuf = m;
160842c1b001SThomas Moestl 
160942c1b001SThomas Moestl 	error = bus_dmamap_load(sc->sc_dmatag, rxs->rxs_dmamap,
161042c1b001SThomas Moestl 	    m->m_ext.ext_buf, m->m_ext.ext_size, gem_rxdma_callback, rxs,
161142c1b001SThomas Moestl 	    BUS_DMA_NOWAIT);
161242c1b001SThomas Moestl 	if (error != 0 || rxs->rxs_paddr == 0) {
161342c1b001SThomas Moestl 		device_printf(sc->sc_dev, "can't load rx DMA map %d, error = "
161442c1b001SThomas Moestl 		    "%d\n", idx, error);
161542c1b001SThomas Moestl 		panic("gem_add_rxbuf");	/* XXX */
161642c1b001SThomas Moestl 	}
161742c1b001SThomas Moestl 
161842c1b001SThomas Moestl 	bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, BUS_DMASYNC_PREREAD);
161942c1b001SThomas Moestl 
162042c1b001SThomas Moestl 	GEM_INIT_RXDESC(sc, idx);
162142c1b001SThomas Moestl 
162242c1b001SThomas Moestl 	return (0);
162342c1b001SThomas Moestl }
162442c1b001SThomas Moestl 
162542c1b001SThomas Moestl 
162642c1b001SThomas Moestl static void
162742c1b001SThomas Moestl gem_eint(sc, status)
162842c1b001SThomas Moestl 	struct gem_softc *sc;
162942c1b001SThomas Moestl 	u_int status;
163042c1b001SThomas Moestl {
163142c1b001SThomas Moestl 
163242c1b001SThomas Moestl 	if ((status & GEM_INTR_MIF) != 0) {
163342c1b001SThomas Moestl 		device_printf(sc->sc_dev, "XXXlink status changed\n");
163442c1b001SThomas Moestl 		return;
163542c1b001SThomas Moestl 	}
163642c1b001SThomas Moestl 
163742c1b001SThomas Moestl 	device_printf(sc->sc_dev, "status=%x\n", status);
163842c1b001SThomas Moestl }
163942c1b001SThomas Moestl 
164042c1b001SThomas Moestl 
164142c1b001SThomas Moestl void
164242c1b001SThomas Moestl gem_intr(v)
164342c1b001SThomas Moestl 	void *v;
164442c1b001SThomas Moestl {
164542c1b001SThomas Moestl 	struct gem_softc *sc = (struct gem_softc *)v;
164642c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
164742c1b001SThomas Moestl 	bus_space_handle_t seb = sc->sc_h;
164842c1b001SThomas Moestl 	u_int32_t status;
164942c1b001SThomas Moestl 
165042c1b001SThomas Moestl 	status = bus_space_read_4(t, seb, GEM_STATUS);
165142c1b001SThomas Moestl 	DPRINTF(sc, ("%s: gem_intr: cplt %x, status %x\n",
165242c1b001SThomas Moestl 		device_get_name(sc->sc_dev), (status>>19),
165342c1b001SThomas Moestl 		(u_int)status));
165442c1b001SThomas Moestl 	CTR3(KTR_GEM, "%s: gem_intr: cplt %x, status %x",
165542c1b001SThomas Moestl 		device_get_name(sc->sc_dev), (status>>19),
165642c1b001SThomas Moestl 		(u_int)status);
165742c1b001SThomas Moestl 
165842c1b001SThomas Moestl 	if ((status & (GEM_INTR_RX_TAG_ERR | GEM_INTR_BERR)) != 0)
165942c1b001SThomas Moestl 		gem_eint(sc, status);
166042c1b001SThomas Moestl 
166142c1b001SThomas Moestl 	if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0)
166242c1b001SThomas Moestl 		gem_tint(sc);
166342c1b001SThomas Moestl 
166442c1b001SThomas Moestl 	if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0)
166542c1b001SThomas Moestl 		gem_rint(sc);
166642c1b001SThomas Moestl 
166742c1b001SThomas Moestl 	/* We should eventually do more than just print out error stats. */
166842c1b001SThomas Moestl 	if (status & GEM_INTR_TX_MAC) {
166942c1b001SThomas Moestl 		int txstat = bus_space_read_4(t, seb, GEM_MAC_TX_STATUS);
167042c1b001SThomas Moestl 		if (txstat & ~GEM_MAC_TX_XMIT_DONE)
1671336cca9eSBenno Rice 			device_printf(sc->sc_dev, "MAC tx fault, status %x\n",
1672336cca9eSBenno Rice 			    txstat);
16739bb711b9SThomas Moestl 		if (txstat & (GEM_MAC_TX_UNDERRUN | GEM_MAC_TX_PKT_TOO_LONG))
16749bb711b9SThomas Moestl 			gem_init(sc);
167542c1b001SThomas Moestl 	}
167642c1b001SThomas Moestl 	if (status & GEM_INTR_RX_MAC) {
167742c1b001SThomas Moestl 		int rxstat = bus_space_read_4(t, seb, GEM_MAC_RX_STATUS);
167842c1b001SThomas Moestl 		if (rxstat & ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT))
1679336cca9eSBenno Rice 			device_printf(sc->sc_dev, "MAC rx fault, status %x\n",
1680336cca9eSBenno Rice 			    rxstat);
16819bb711b9SThomas Moestl 		if ((rxstat & GEM_MAC_RX_OVERFLOW) != 0)
16829bb711b9SThomas Moestl 			gem_init(sc);
168342c1b001SThomas Moestl 	}
168442c1b001SThomas Moestl }
168542c1b001SThomas Moestl 
168642c1b001SThomas Moestl 
168742c1b001SThomas Moestl static void
168842c1b001SThomas Moestl gem_watchdog(ifp)
168942c1b001SThomas Moestl 	struct ifnet *ifp;
169042c1b001SThomas Moestl {
169142c1b001SThomas Moestl 	struct gem_softc *sc = ifp->if_softc;
169242c1b001SThomas Moestl 
169342c1b001SThomas Moestl 	DPRINTF(sc, ("gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x "
169442c1b001SThomas Moestl 		"GEM_MAC_RX_CONFIG %x\n",
169542c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_CONFIG),
169642c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_STATUS),
169742c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_CONFIG)));
169842c1b001SThomas Moestl 	CTR3(KTR_GEM, "gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x "
169942c1b001SThomas Moestl 		"GEM_MAC_RX_CONFIG %x",
170042c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_CONFIG),
170142c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_STATUS),
170242c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_CONFIG));
170342c1b001SThomas Moestl 	CTR3(KTR_GEM, "gem_watchdog: GEM_TX_CONFIG %x GEM_MAC_TX_STATUS %x "
170442c1b001SThomas Moestl 		"GEM_MAC_TX_CONFIG %x",
170542c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_CONFIG),
170642c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_TX_STATUS),
170742c1b001SThomas Moestl 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_TX_CONFIG));
170842c1b001SThomas Moestl 
170942c1b001SThomas Moestl 	device_printf(sc->sc_dev, "device timeout\n");
171042c1b001SThomas Moestl 	++ifp->if_oerrors;
171142c1b001SThomas Moestl 
171242c1b001SThomas Moestl 	/* Try to get more packets going. */
171342c1b001SThomas Moestl 	gem_start(ifp);
171442c1b001SThomas Moestl }
171542c1b001SThomas Moestl 
171642c1b001SThomas Moestl /*
171742c1b001SThomas Moestl  * Initialize the MII Management Interface
171842c1b001SThomas Moestl  */
171942c1b001SThomas Moestl static void
172042c1b001SThomas Moestl gem_mifinit(sc)
172142c1b001SThomas Moestl 	struct gem_softc *sc;
172242c1b001SThomas Moestl {
172342c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
172442c1b001SThomas Moestl 	bus_space_handle_t mif = sc->sc_h;
172542c1b001SThomas Moestl 
172642c1b001SThomas Moestl 	/* Configure the MIF in frame mode */
172742c1b001SThomas Moestl 	sc->sc_mif_config = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
172842c1b001SThomas Moestl 	sc->sc_mif_config &= ~GEM_MIF_CONFIG_BB_ENA;
172942c1b001SThomas Moestl 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, sc->sc_mif_config);
173042c1b001SThomas Moestl }
173142c1b001SThomas Moestl 
173242c1b001SThomas Moestl /*
173342c1b001SThomas Moestl  * MII interface
173442c1b001SThomas Moestl  *
173542c1b001SThomas Moestl  * The GEM MII interface supports at least three different operating modes:
173642c1b001SThomas Moestl  *
173742c1b001SThomas Moestl  * Bitbang mode is implemented using data, clock and output enable registers.
173842c1b001SThomas Moestl  *
173942c1b001SThomas Moestl  * Frame mode is implemented by loading a complete frame into the frame
174042c1b001SThomas Moestl  * register and polling the valid bit for completion.
174142c1b001SThomas Moestl  *
174242c1b001SThomas Moestl  * Polling mode uses the frame register but completion is indicated by
174342c1b001SThomas Moestl  * an interrupt.
174442c1b001SThomas Moestl  *
174542c1b001SThomas Moestl  */
174642c1b001SThomas Moestl int
174742c1b001SThomas Moestl gem_mii_readreg(dev, phy, reg)
174842c1b001SThomas Moestl 	device_t dev;
174942c1b001SThomas Moestl 	int phy, reg;
175042c1b001SThomas Moestl {
175142c1b001SThomas Moestl 	struct gem_softc *sc = device_get_softc(dev);
175242c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
175342c1b001SThomas Moestl 	bus_space_handle_t mif = sc->sc_h;
175442c1b001SThomas Moestl 	int n;
175542c1b001SThomas Moestl 	u_int32_t v;
175642c1b001SThomas Moestl 
175742c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY
175842c1b001SThomas Moestl 	printf("gem_mii_readreg: phy %d reg %d\n", phy, reg);
175942c1b001SThomas Moestl #endif
176042c1b001SThomas Moestl 
176142c1b001SThomas Moestl #if 0
176242c1b001SThomas Moestl 	/* Select the desired PHY in the MIF configuration register */
176342c1b001SThomas Moestl 	v = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
176442c1b001SThomas Moestl 	/* Clear PHY select bit */
176542c1b001SThomas Moestl 	v &= ~GEM_MIF_CONFIG_PHY_SEL;
176642c1b001SThomas Moestl 	if (phy == GEM_PHYAD_EXTERNAL)
176742c1b001SThomas Moestl 		/* Set PHY select bit to get at external device */
176842c1b001SThomas Moestl 		v |= GEM_MIF_CONFIG_PHY_SEL;
176942c1b001SThomas Moestl 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, v);
177042c1b001SThomas Moestl #endif
177142c1b001SThomas Moestl 
177242c1b001SThomas Moestl 	/* Construct the frame command */
177342c1b001SThomas Moestl 	v = (reg << GEM_MIF_REG_SHIFT)	| (phy << GEM_MIF_PHY_SHIFT) |
177442c1b001SThomas Moestl 		GEM_MIF_FRAME_READ;
177542c1b001SThomas Moestl 
177642c1b001SThomas Moestl 	bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
177742c1b001SThomas Moestl 	for (n = 0; n < 100; n++) {
177842c1b001SThomas Moestl 		DELAY(1);
177942c1b001SThomas Moestl 		v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
178042c1b001SThomas Moestl 		if (v & GEM_MIF_FRAME_TA0)
178142c1b001SThomas Moestl 			return (v & GEM_MIF_FRAME_DATA);
178242c1b001SThomas Moestl 	}
178342c1b001SThomas Moestl 
178442c1b001SThomas Moestl 	device_printf(sc->sc_dev, "mii_read timeout\n");
178542c1b001SThomas Moestl 	return (0);
178642c1b001SThomas Moestl }
178742c1b001SThomas Moestl 
178842c1b001SThomas Moestl int
178942c1b001SThomas Moestl gem_mii_writereg(dev, phy, reg, val)
179042c1b001SThomas Moestl 	device_t dev;
179142c1b001SThomas Moestl 	int phy, reg, val;
179242c1b001SThomas Moestl {
179342c1b001SThomas Moestl 	struct gem_softc *sc = device_get_softc(dev);
179442c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
179542c1b001SThomas Moestl 	bus_space_handle_t mif = sc->sc_h;
179642c1b001SThomas Moestl 	int n;
179742c1b001SThomas Moestl 	u_int32_t v;
179842c1b001SThomas Moestl 
179942c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY
180042c1b001SThomas Moestl 	printf("gem_mii_writereg: phy %d reg %d val %x\n", phy, reg, val);
180142c1b001SThomas Moestl #endif
180242c1b001SThomas Moestl 
180342c1b001SThomas Moestl #if 0
180442c1b001SThomas Moestl 	/* Select the desired PHY in the MIF configuration register */
180542c1b001SThomas Moestl 	v = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
180642c1b001SThomas Moestl 	/* Clear PHY select bit */
180742c1b001SThomas Moestl 	v &= ~GEM_MIF_CONFIG_PHY_SEL;
180842c1b001SThomas Moestl 	if (phy == GEM_PHYAD_EXTERNAL)
180942c1b001SThomas Moestl 		/* Set PHY select bit to get at external device */
181042c1b001SThomas Moestl 		v |= GEM_MIF_CONFIG_PHY_SEL;
181142c1b001SThomas Moestl 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, v);
181242c1b001SThomas Moestl #endif
181342c1b001SThomas Moestl 	/* Construct the frame command */
181442c1b001SThomas Moestl 	v = GEM_MIF_FRAME_WRITE			|
181542c1b001SThomas Moestl 	    (phy << GEM_MIF_PHY_SHIFT)		|
181642c1b001SThomas Moestl 	    (reg << GEM_MIF_REG_SHIFT)		|
181742c1b001SThomas Moestl 	    (val & GEM_MIF_FRAME_DATA);
181842c1b001SThomas Moestl 
181942c1b001SThomas Moestl 	bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
182042c1b001SThomas Moestl 	for (n = 0; n < 100; n++) {
182142c1b001SThomas Moestl 		DELAY(1);
182242c1b001SThomas Moestl 		v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
182342c1b001SThomas Moestl 		if (v & GEM_MIF_FRAME_TA0)
182442c1b001SThomas Moestl 			return (1);
182542c1b001SThomas Moestl 	}
182642c1b001SThomas Moestl 
182742c1b001SThomas Moestl 	device_printf(sc->sc_dev, "mii_write timeout\n");
182842c1b001SThomas Moestl 	return (0);
182942c1b001SThomas Moestl }
183042c1b001SThomas Moestl 
183142c1b001SThomas Moestl void
183242c1b001SThomas Moestl gem_mii_statchg(dev)
183342c1b001SThomas Moestl 	device_t dev;
183442c1b001SThomas Moestl {
183542c1b001SThomas Moestl 	struct gem_softc *sc = device_get_softc(dev);
183642c1b001SThomas Moestl #ifdef GEM_DEBUG
183742c1b001SThomas Moestl 	int instance = IFM_INST(sc->sc_mii->mii_media.ifm_cur->ifm_media);
183842c1b001SThomas Moestl #endif
183942c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
184042c1b001SThomas Moestl 	bus_space_handle_t mac = sc->sc_h;
184142c1b001SThomas Moestl 	u_int32_t v;
184242c1b001SThomas Moestl 
184342c1b001SThomas Moestl #ifdef GEM_DEBUG
184442c1b001SThomas Moestl 	if (sc->sc_debug)
184542c1b001SThomas Moestl 		printf("gem_mii_statchg: status change: phy = %d\n",
184642c1b001SThomas Moestl 			sc->sc_phys[instance]);
184742c1b001SThomas Moestl #endif
184842c1b001SThomas Moestl 
184942c1b001SThomas Moestl 	/* Set tx full duplex options */
185042c1b001SThomas Moestl 	bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, 0);
185142c1b001SThomas Moestl 	DELAY(10000); /* reg must be cleared and delay before changing. */
185242c1b001SThomas Moestl 	v = GEM_MAC_TX_ENA_IPG0|GEM_MAC_TX_NGU|GEM_MAC_TX_NGU_LIMIT|
185342c1b001SThomas Moestl 		GEM_MAC_TX_ENABLE;
185442c1b001SThomas Moestl 	if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) {
185542c1b001SThomas Moestl 		v |= GEM_MAC_TX_IGN_CARRIER|GEM_MAC_TX_IGN_COLLIS;
185642c1b001SThomas Moestl 	}
185742c1b001SThomas Moestl 	bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, v);
185842c1b001SThomas Moestl 
185942c1b001SThomas Moestl 	/* XIF Configuration */
186042c1b001SThomas Moestl  /* We should really calculate all this rather than rely on defaults */
186142c1b001SThomas Moestl 	v = bus_space_read_4(t, mac, GEM_MAC_XIF_CONFIG);
186242c1b001SThomas Moestl 	v = GEM_MAC_XIF_LINK_LED;
186342c1b001SThomas Moestl 	v |= GEM_MAC_XIF_TX_MII_ENA;
1864336cca9eSBenno Rice 
186542c1b001SThomas Moestl 	/* If an external transceiver is connected, enable its MII drivers */
186642c1b001SThomas Moestl 	sc->sc_mif_config = bus_space_read_4(t, mac, GEM_MIF_CONFIG);
186742c1b001SThomas Moestl 	if ((sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) != 0) {
186842c1b001SThomas Moestl 		/* External MII needs echo disable if half duplex. */
186942c1b001SThomas Moestl 		if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0)
187042c1b001SThomas Moestl 			/* turn on full duplex LED */
187142c1b001SThomas Moestl 			v |= GEM_MAC_XIF_FDPLX_LED;
187242c1b001SThomas Moestl 		else
187342c1b001SThomas Moestl 	 		/* half duplex -- disable echo */
187442c1b001SThomas Moestl 	 		v |= GEM_MAC_XIF_ECHO_DISABL;
1875336cca9eSBenno Rice 
1876336cca9eSBenno Rice 		if (IFM_SUBTYPE(sc->sc_mii->mii_media_active) == IFM_1000_T)
1877336cca9eSBenno Rice 			v |= GEM_MAC_XIF_GMII_MODE;
1878336cca9eSBenno Rice 		else
1879336cca9eSBenno Rice 			v &= ~GEM_MAC_XIF_GMII_MODE;
188042c1b001SThomas Moestl 	} else {
188142c1b001SThomas Moestl 		/* Internal MII needs buf enable */
188242c1b001SThomas Moestl 		v |= GEM_MAC_XIF_MII_BUF_ENA;
188342c1b001SThomas Moestl 	}
188442c1b001SThomas Moestl 	bus_space_write_4(t, mac, GEM_MAC_XIF_CONFIG, v);
188542c1b001SThomas Moestl }
188642c1b001SThomas Moestl 
188742c1b001SThomas Moestl int
188842c1b001SThomas Moestl gem_mediachange(ifp)
188942c1b001SThomas Moestl 	struct ifnet *ifp;
189042c1b001SThomas Moestl {
189142c1b001SThomas Moestl 	struct gem_softc *sc = ifp->if_softc;
189242c1b001SThomas Moestl 
189342c1b001SThomas Moestl 	/* XXX Add support for serial media. */
189442c1b001SThomas Moestl 
189542c1b001SThomas Moestl 	return (mii_mediachg(sc->sc_mii));
189642c1b001SThomas Moestl }
189742c1b001SThomas Moestl 
189842c1b001SThomas Moestl void
189942c1b001SThomas Moestl gem_mediastatus(ifp, ifmr)
190042c1b001SThomas Moestl 	struct ifnet *ifp;
190142c1b001SThomas Moestl 	struct ifmediareq *ifmr;
190242c1b001SThomas Moestl {
190342c1b001SThomas Moestl 	struct gem_softc *sc = ifp->if_softc;
190442c1b001SThomas Moestl 
190542c1b001SThomas Moestl 	if ((ifp->if_flags & IFF_UP) == 0)
190642c1b001SThomas Moestl 		return;
190742c1b001SThomas Moestl 
190842c1b001SThomas Moestl 	mii_pollstat(sc->sc_mii);
190942c1b001SThomas Moestl 	ifmr->ifm_active = sc->sc_mii->mii_media_active;
191042c1b001SThomas Moestl 	ifmr->ifm_status = sc->sc_mii->mii_media_status;
191142c1b001SThomas Moestl }
191242c1b001SThomas Moestl 
191342c1b001SThomas Moestl /*
191442c1b001SThomas Moestl  * Process an ioctl request.
191542c1b001SThomas Moestl  */
191642c1b001SThomas Moestl static int
191742c1b001SThomas Moestl gem_ioctl(ifp, cmd, data)
191842c1b001SThomas Moestl 	struct ifnet *ifp;
191942c1b001SThomas Moestl 	u_long cmd;
192042c1b001SThomas Moestl 	caddr_t data;
192142c1b001SThomas Moestl {
192242c1b001SThomas Moestl 	struct gem_softc *sc = ifp->if_softc;
192342c1b001SThomas Moestl 	struct ifreq *ifr = (struct ifreq *)data;
192442c1b001SThomas Moestl 	int s, error = 0;
192542c1b001SThomas Moestl 
192642c1b001SThomas Moestl 	switch (cmd) {
192742c1b001SThomas Moestl 	case SIOCSIFADDR:
192842c1b001SThomas Moestl 	case SIOCGIFADDR:
192942c1b001SThomas Moestl 	case SIOCSIFMTU:
193042c1b001SThomas Moestl 		error = ether_ioctl(ifp, cmd, data);
193142c1b001SThomas Moestl 		break;
193242c1b001SThomas Moestl 	case SIOCSIFFLAGS:
193342c1b001SThomas Moestl 		if (ifp->if_flags & IFF_UP) {
1934336cca9eSBenno Rice 			if ((sc->sc_ifflags ^ ifp->if_flags) == IFF_PROMISC)
193542c1b001SThomas Moestl 				gem_setladrf(sc);
193642c1b001SThomas Moestl 			else
193742c1b001SThomas Moestl 				gem_init(sc);
193842c1b001SThomas Moestl 		} else {
193942c1b001SThomas Moestl 			if (ifp->if_flags & IFF_RUNNING)
194042c1b001SThomas Moestl 				gem_stop(ifp, 0);
194142c1b001SThomas Moestl 		}
1942336cca9eSBenno Rice 		sc->sc_ifflags = ifp->if_flags;
194342c1b001SThomas Moestl 		error = 0;
194442c1b001SThomas Moestl 		break;
194542c1b001SThomas Moestl 	case SIOCADDMULTI:
194642c1b001SThomas Moestl 	case SIOCDELMULTI:
194742c1b001SThomas Moestl 		gem_setladrf(sc);
194842c1b001SThomas Moestl 		error = 0;
194942c1b001SThomas Moestl 		break;
195042c1b001SThomas Moestl 	case SIOCGIFMEDIA:
195142c1b001SThomas Moestl 	case SIOCSIFMEDIA:
195242c1b001SThomas Moestl 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, cmd);
195342c1b001SThomas Moestl 		break;
195442c1b001SThomas Moestl 	default:
195542c1b001SThomas Moestl 		error = ENOTTY;
195642c1b001SThomas Moestl 		break;
195742c1b001SThomas Moestl 	}
195842c1b001SThomas Moestl 
195942c1b001SThomas Moestl 	/* Try to get things going again */
196042c1b001SThomas Moestl 	if (ifp->if_flags & IFF_UP)
196142c1b001SThomas Moestl 		gem_start(ifp);
196242c1b001SThomas Moestl 	splx(s);
196342c1b001SThomas Moestl 	return (error);
196442c1b001SThomas Moestl }
196542c1b001SThomas Moestl 
196642c1b001SThomas Moestl /*
196742c1b001SThomas Moestl  * Set up the logical address filter.
196842c1b001SThomas Moestl  */
196942c1b001SThomas Moestl static void
197042c1b001SThomas Moestl gem_setladrf(sc)
197142c1b001SThomas Moestl 	struct gem_softc *sc;
197242c1b001SThomas Moestl {
197342c1b001SThomas Moestl 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
197442c1b001SThomas Moestl 	struct ifmultiaddr *inm;
197542c1b001SThomas Moestl 	struct sockaddr_dl *sdl;
197642c1b001SThomas Moestl 	bus_space_tag_t t = sc->sc_bustag;
197742c1b001SThomas Moestl 	bus_space_handle_t h = sc->sc_h;
197842c1b001SThomas Moestl 	u_char *cp;
197942c1b001SThomas Moestl 	u_int32_t crc;
198042c1b001SThomas Moestl 	u_int32_t hash[16];
198142c1b001SThomas Moestl 	u_int32_t v;
198242c1b001SThomas Moestl 	int len;
1983336cca9eSBenno Rice 	int i;
198442c1b001SThomas Moestl 
198542c1b001SThomas Moestl 	/* Get current RX configuration */
198642c1b001SThomas Moestl 	v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
198742c1b001SThomas Moestl 
1988336cca9eSBenno Rice 	/*
1989336cca9eSBenno Rice 	 * Turn off promiscuous mode, promiscuous group mode (all multicast),
1990336cca9eSBenno Rice 	 * and hash filter.  Depending on the case, the right bit will be
1991336cca9eSBenno Rice 	 * enabled.
1992336cca9eSBenno Rice 	 */
1993336cca9eSBenno Rice 	v &= ~(GEM_MAC_RX_PROMISCUOUS|GEM_MAC_RX_HASH_FILTER|
1994336cca9eSBenno Rice 	    GEM_MAC_RX_PROMISC_GRP);
1995336cca9eSBenno Rice 
199642c1b001SThomas Moestl 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
1997336cca9eSBenno Rice 		/* Turn on promiscuous mode */
199842c1b001SThomas Moestl 		v |= GEM_MAC_RX_PROMISCUOUS;
199942c1b001SThomas Moestl 		goto chipit;
200042c1b001SThomas Moestl 	}
200142c1b001SThomas Moestl 	if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
200242c1b001SThomas Moestl 		hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
200342c1b001SThomas Moestl 		ifp->if_flags |= IFF_ALLMULTI;
2004336cca9eSBenno Rice 		v |= GEM_MAC_RX_PROMISC_GRP;
200542c1b001SThomas Moestl 		goto chipit;
200642c1b001SThomas Moestl 	}
200742c1b001SThomas Moestl 
200842c1b001SThomas Moestl 	/*
200942c1b001SThomas Moestl 	 * Set up multicast address filter by passing all multicast addresses
2010336cca9eSBenno Rice 	 * through a crc generator, and then using the high order 8 bits as an
2011336cca9eSBenno Rice 	 * index into the 256 bit logical address filter.  The high order 4
2012336cca9eSBenno Rice 	 * bits selects the word, while the other 4 bits select the bit within
2013336cca9eSBenno Rice 	 * the word (where bit 0 is the MSB).
201442c1b001SThomas Moestl 	 */
201542c1b001SThomas Moestl 
2016336cca9eSBenno Rice 	/* Clear hash table */
2017336cca9eSBenno Rice 	memset(hash, 0, sizeof(hash));
2018336cca9eSBenno Rice 
201942c1b001SThomas Moestl 	TAILQ_FOREACH(inm, &sc->sc_arpcom.ac_if.if_multiaddrs, ifma_link) {
202042c1b001SThomas Moestl 		if (inm->ifma_addr->sa_family != AF_LINK)
202142c1b001SThomas Moestl 			continue;
202242c1b001SThomas Moestl 		sdl = (struct sockaddr_dl *)inm->ifma_addr;
202342c1b001SThomas Moestl 		cp = LLADDR(sdl);
202442c1b001SThomas Moestl 		crc = 0xffffffff;
202542c1b001SThomas Moestl 		for (len = sdl->sdl_alen; --len >= 0;) {
202642c1b001SThomas Moestl 			int octet = *cp++;
202742c1b001SThomas Moestl 			int i;
202842c1b001SThomas Moestl 
202942c1b001SThomas Moestl #define MC_POLY_LE	0xedb88320UL	/* mcast crc, little endian */
203042c1b001SThomas Moestl 			for (i = 0; i < 8; i++) {
203142c1b001SThomas Moestl 				if ((crc & 1) ^ (octet & 1)) {
203242c1b001SThomas Moestl 					crc >>= 1;
203342c1b001SThomas Moestl 					crc ^= MC_POLY_LE;
203442c1b001SThomas Moestl 				} else {
203542c1b001SThomas Moestl 					crc >>= 1;
203642c1b001SThomas Moestl 				}
203742c1b001SThomas Moestl 				octet >>= 1;
203842c1b001SThomas Moestl 			}
203942c1b001SThomas Moestl 		}
204042c1b001SThomas Moestl 		/* Just want the 8 most significant bits. */
204142c1b001SThomas Moestl 		crc >>= 24;
204242c1b001SThomas Moestl 
204342c1b001SThomas Moestl 		/* Set the corresponding bit in the filter. */
2044336cca9eSBenno Rice 		hash[crc >> 4] |= 1 << (15 - (crc & 15));
2045336cca9eSBenno Rice 	}
2046336cca9eSBenno Rice 
2047336cca9eSBenno Rice 	v |= GEM_MAC_RX_HASH_FILTER;
2048336cca9eSBenno Rice 	ifp->if_flags &= ~IFF_ALLMULTI;
2049336cca9eSBenno Rice 
2050336cca9eSBenno Rice 	/* Now load the hash table into the chip (if we are using it) */
2051336cca9eSBenno Rice 	for (i = 0; i < 16; i++) {
2052336cca9eSBenno Rice 		bus_space_write_4(t, h,
2053336cca9eSBenno Rice 		    GEM_MAC_HASH0 + i * (GEM_MAC_HASH1-GEM_MAC_HASH0),
2054336cca9eSBenno Rice 		    hash[i]);
205542c1b001SThomas Moestl 	}
205642c1b001SThomas Moestl 
205742c1b001SThomas Moestl chipit:
205842c1b001SThomas Moestl 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
205942c1b001SThomas Moestl }
206042c1b001SThomas Moestl 
206142c1b001SThomas Moestl #if notyet
206242c1b001SThomas Moestl 
206342c1b001SThomas Moestl /*
206442c1b001SThomas Moestl  * gem_power:
206542c1b001SThomas Moestl  *
206642c1b001SThomas Moestl  *	Power management (suspend/resume) hook.
206742c1b001SThomas Moestl  */
206842c1b001SThomas Moestl void
206942c1b001SThomas Moestl static gem_power(why, arg)
207042c1b001SThomas Moestl 	int why;
207142c1b001SThomas Moestl 	void *arg;
207242c1b001SThomas Moestl {
207342c1b001SThomas Moestl 	struct gem_softc *sc = arg;
207442c1b001SThomas Moestl 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
207542c1b001SThomas Moestl 	int s;
207642c1b001SThomas Moestl 
207742c1b001SThomas Moestl 	s = splnet();
207842c1b001SThomas Moestl 	switch (why) {
207942c1b001SThomas Moestl 	case PWR_SUSPEND:
208042c1b001SThomas Moestl 	case PWR_STANDBY:
208142c1b001SThomas Moestl 		gem_stop(ifp, 1);
208242c1b001SThomas Moestl 		if (sc->sc_power != NULL)
208342c1b001SThomas Moestl 			(*sc->sc_power)(sc, why);
208442c1b001SThomas Moestl 		break;
208542c1b001SThomas Moestl 	case PWR_RESUME:
208642c1b001SThomas Moestl 		if (ifp->if_flags & IFF_UP) {
208742c1b001SThomas Moestl 			if (sc->sc_power != NULL)
208842c1b001SThomas Moestl 				(*sc->sc_power)(sc, why);
208942c1b001SThomas Moestl 			gem_init(ifp);
209042c1b001SThomas Moestl 		}
209142c1b001SThomas Moestl 		break;
209242c1b001SThomas Moestl 	case PWR_SOFTSUSPEND:
209342c1b001SThomas Moestl 	case PWR_SOFTSTANDBY:
209442c1b001SThomas Moestl 	case PWR_SOFTRESUME:
209542c1b001SThomas Moestl 		break;
209642c1b001SThomas Moestl 	}
209742c1b001SThomas Moestl 	splx(s);
209842c1b001SThomas Moestl }
209942c1b001SThomas Moestl #endif
2100