Home | History | Annotate | Line # | Download | only in ic
gem.c revision 1.1
      1  1.1  eeh /*	$NetBSD: gem.c,v 1.1 2001/09/16 00:11:43 eeh Exp $ */
      2  1.1  eeh 
      3  1.1  eeh /*
      4  1.1  eeh  *
      5  1.1  eeh  * Copyright (C) 2001 Eduardo Horvath.
      6  1.1  eeh  * All rights reserved.
      7  1.1  eeh  *
      8  1.1  eeh  *
      9  1.1  eeh  * Redistribution and use in source and binary forms, with or without
     10  1.1  eeh  * modification, are permitted provided that the following conditions
     11  1.1  eeh  * are met:
     12  1.1  eeh  * 1. Redistributions of source code must retain the above copyright
     13  1.1  eeh  *    notice, this list of conditions and the following disclaimer.
     14  1.1  eeh  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  eeh  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  eeh  *    documentation and/or other materials provided with the distribution.
     17  1.1  eeh  *
     18  1.1  eeh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
     19  1.1  eeh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  1.1  eeh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  1.1  eeh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
     22  1.1  eeh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1  eeh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  1.1  eeh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1  eeh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1  eeh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1  eeh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1  eeh  * SUCH DAMAGE.
     29  1.1  eeh  *
     30  1.1  eeh  */
     31  1.1  eeh 
     32  1.1  eeh /*
     33  1.1  eeh  * Driver for Sun GEM ethernet controllers.
     34  1.1  eeh  */
     35  1.1  eeh 
     36  1.1  eeh #define	GEM_DEBUG
     37  1.1  eeh int gem_opdebug = 0;
     38  1.1  eeh 
     39  1.1  eeh #include "bpfilter.h"
     40  1.1  eeh 
     41  1.1  eeh #include <sys/param.h>
     42  1.1  eeh #include <sys/systm.h>
     43  1.1  eeh #include <sys/callout.h>
     44  1.1  eeh #include <sys/mbuf.h>
     45  1.1  eeh #include <sys/syslog.h>
     46  1.1  eeh #include <sys/malloc.h>
     47  1.1  eeh #include <sys/kernel.h>
     48  1.1  eeh #include <sys/socket.h>
     49  1.1  eeh #include <sys/ioctl.h>
     50  1.1  eeh #include <sys/errno.h>
     51  1.1  eeh #include <sys/device.h>
     52  1.1  eeh 
     53  1.1  eeh #include <machine/endian.h>
     54  1.1  eeh 
     55  1.1  eeh #include <uvm/uvm_extern.h>
     56  1.1  eeh 
     57  1.1  eeh #include <net/if.h>
     58  1.1  eeh #include <net/if_dl.h>
     59  1.1  eeh #include <net/if_media.h>
     60  1.1  eeh #include <net/if_ether.h>
     61  1.1  eeh 
     62  1.1  eeh #if NBPFILTER > 0
     63  1.1  eeh #include <net/bpf.h>
     64  1.1  eeh #endif
     65  1.1  eeh 
     66  1.1  eeh #include <machine/bus.h>
     67  1.1  eeh #include <machine/intr.h>
     68  1.1  eeh 
     69  1.1  eeh #include <dev/mii/mii.h>
     70  1.1  eeh #include <dev/mii/miivar.h>
     71  1.1  eeh #include <dev/mii/mii_bitbang.h>
     72  1.1  eeh 
     73  1.1  eeh #include <dev/ic/gemreg.h>
     74  1.1  eeh #include <dev/ic/gemvar.h>
     75  1.1  eeh 
     76  1.1  eeh #define TRIES	10000
     77  1.1  eeh 
     78  1.1  eeh void		gem_start __P((struct ifnet *));
     79  1.1  eeh void		gem_stop __P((struct ifnet *, int));
     80  1.1  eeh int		gem_ioctl __P((struct ifnet *, u_long, caddr_t));
     81  1.1  eeh void		gem_tick __P((void *));
     82  1.1  eeh void		gem_watchdog __P((struct ifnet *));
     83  1.1  eeh void		gem_shutdown __P((void *));
     84  1.1  eeh int		gem_init __P((struct ifnet *));
     85  1.1  eeh void		gem_init_regs(struct gem_softc *sc);
     86  1.1  eeh static int	gem_ringsize(int sz);
     87  1.1  eeh int		gem_meminit __P((struct gem_softc *));
     88  1.1  eeh void		gem_mifinit __P((struct gem_softc *));
     89  1.1  eeh void		gem_reset __P((struct gem_softc *));
     90  1.1  eeh int		gem_reset_rx(struct gem_softc *sc);
     91  1.1  eeh int		gem_reset_tx(struct gem_softc *sc);
     92  1.1  eeh int		gem_disable_rx(struct gem_softc *sc);
     93  1.1  eeh int		gem_disable_tx(struct gem_softc *sc);
     94  1.1  eeh void		gem_rxdrain(struct gem_softc *sc);
     95  1.1  eeh int		gem_add_rxbuf(struct gem_softc *sc, int idx);
     96  1.1  eeh void		gem_setladrf __P((struct gem_softc *));
     97  1.1  eeh 
     98  1.1  eeh /* MII methods & callbacks */
     99  1.1  eeh static int	gem_mii_readreg __P((struct device *, int, int));
    100  1.1  eeh static void	gem_mii_writereg __P((struct device *, int, int, int));
    101  1.1  eeh static void	gem_mii_statchg __P((struct device *));
    102  1.1  eeh 
    103  1.1  eeh int		gem_mediachange __P((struct ifnet *));
    104  1.1  eeh void		gem_mediastatus __P((struct ifnet *, struct ifmediareq *));
    105  1.1  eeh 
    106  1.1  eeh struct mbuf	*gem_get __P((struct gem_softc *, int, int));
    107  1.1  eeh int		gem_put __P((struct gem_softc *, int, struct mbuf *));
    108  1.1  eeh void		gem_read __P((struct gem_softc *, int, int));
    109  1.1  eeh int		gem_eint __P((struct gem_softc *, u_int));
    110  1.1  eeh int		gem_rint __P((struct gem_softc *));
    111  1.1  eeh int		gem_tint __P((struct gem_softc *));
    112  1.1  eeh void		gem_power __P((int, void *));
    113  1.1  eeh 
    114  1.1  eeh static int	ether_cmp __P((u_char *, u_char *));
    115  1.1  eeh 
    116  1.1  eeh /* Default buffer copy routines */
    117  1.1  eeh void	gem_copytobuf_contig __P((struct gem_softc *, void *, int, int));
    118  1.1  eeh void	gem_copyfrombuf_contig __P((struct gem_softc *, void *, int, int));
    119  1.1  eeh void	gem_zerobuf_contig __P((struct gem_softc *, int, int));
    120  1.1  eeh 
    121  1.1  eeh 
    122  1.1  eeh #ifdef GEM_DEBUG
    123  1.1  eeh #define	DPRINTF(sc, x)	if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
    124  1.1  eeh 				printf x
    125  1.1  eeh #else
    126  1.1  eeh #define	DPRINTF(sc, x)	/* nothing */
    127  1.1  eeh #endif
    128  1.1  eeh 
    129  1.1  eeh 
    130  1.1  eeh /*
    131  1.1  eeh  * gem_config:
    132  1.1  eeh  *
    133  1.1  eeh  *	Attach a Gem interface to the system.
    134  1.1  eeh  */
    135  1.1  eeh void
    136  1.1  eeh gem_config(sc)
    137  1.1  eeh 	struct gem_softc *sc;
    138  1.1  eeh {
    139  1.1  eeh 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    140  1.1  eeh 	struct mii_data *mii = &sc->sc_mii;
    141  1.1  eeh 	struct mii_softc *child;
    142  1.1  eeh 	int i, error;
    143  1.1  eeh 
    144  1.1  eeh 	/* Make sure the chip is stopped. */
    145  1.1  eeh 	ifp->if_softc = sc;
    146  1.1  eeh 	gem_reset(sc);
    147  1.1  eeh 
    148  1.1  eeh 	/*
    149  1.1  eeh 	 * Allocate the control data structures, and create and load the
    150  1.1  eeh 	 * DMA map for it.
    151  1.1  eeh 	 */
    152  1.1  eeh 	if ((error = bus_dmamem_alloc(sc->sc_dmatag,
    153  1.1  eeh 	    sizeof(struct gem_control_data), PAGE_SIZE, 0, &sc->sc_cdseg,
    154  1.1  eeh 	    1, &sc->sc_cdnseg, 0)) != 0) {
    155  1.1  eeh 		printf("%s: unable to allocate control data, error = %d\n",
    156  1.1  eeh 		    sc->sc_dev.dv_xname, error);
    157  1.1  eeh 		goto fail_0;
    158  1.1  eeh 	}
    159  1.1  eeh 
    160  1.1  eeh /* XXX should map this in with correct endianness */
    161  1.1  eeh 	if ((error = bus_dmamem_map(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg,
    162  1.1  eeh 	    sizeof(struct gem_control_data), (caddr_t *)&sc->sc_control_data,
    163  1.1  eeh 	    BUS_DMA_COHERENT)) != 0) {
    164  1.1  eeh 		printf("%s: unable to map control data, error = %d\n",
    165  1.1  eeh 		    sc->sc_dev.dv_xname, error);
    166  1.1  eeh 		goto fail_1;
    167  1.1  eeh 	}
    168  1.1  eeh 
    169  1.1  eeh 	if ((error = bus_dmamap_create(sc->sc_dmatag,
    170  1.1  eeh 	    sizeof(struct gem_control_data), 1,
    171  1.1  eeh 	    sizeof(struct gem_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    172  1.1  eeh 		printf("%s: unable to create control data DMA map, "
    173  1.1  eeh 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    174  1.1  eeh 		goto fail_2;
    175  1.1  eeh 	}
    176  1.1  eeh 
    177  1.1  eeh 	if ((error = bus_dmamap_load(sc->sc_dmatag, sc->sc_cddmamap,
    178  1.1  eeh 	    sc->sc_control_data, sizeof(struct gem_control_data), NULL,
    179  1.1  eeh 	    0)) != 0) {
    180  1.1  eeh 		printf("%s: unable to load control data DMA map, error = %d\n",
    181  1.1  eeh 		    sc->sc_dev.dv_xname, error);
    182  1.1  eeh 		goto fail_3;
    183  1.1  eeh 	}
    184  1.1  eeh 
    185  1.1  eeh 	/*
    186  1.1  eeh 	 * Initialize the transmit job descriptors.
    187  1.1  eeh 	 */
    188  1.1  eeh 	SIMPLEQ_INIT(&sc->sc_txfreeq);
    189  1.1  eeh 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
    190  1.1  eeh 
    191  1.1  eeh 	/*
    192  1.1  eeh 	 * Create the transmit buffer DMA maps.
    193  1.1  eeh 	 */
    194  1.1  eeh 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
    195  1.1  eeh 		struct gem_txsoft *txs;
    196  1.1  eeh 
    197  1.1  eeh 		txs = &sc->sc_txsoft[i];
    198  1.1  eeh 		txs->txs_mbuf = NULL;
    199  1.1  eeh 		if ((error = bus_dmamap_create(sc->sc_dmatag, MCLBYTES,
    200  1.1  eeh 		    GEM_NTXSEGS, MCLBYTES, 0, 0,
    201  1.1  eeh 		    &txs->txs_dmamap)) != 0) {
    202  1.1  eeh 			printf("%s: unable to create tx DMA map %d, "
    203  1.1  eeh 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    204  1.1  eeh 			goto fail_4;
    205  1.1  eeh 		}
    206  1.1  eeh 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
    207  1.1  eeh 	}
    208  1.1  eeh 
    209  1.1  eeh 	/*
    210  1.1  eeh 	 * Create the receive buffer DMA maps.
    211  1.1  eeh 	 */
    212  1.1  eeh 	for (i = 0; i < GEM_NRXDESC; i++) {
    213  1.1  eeh 		if ((error = bus_dmamap_create(sc->sc_dmatag, MCLBYTES, 1,
    214  1.1  eeh 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    215  1.1  eeh 			printf("%s: unable to create rx DMA map %d, "
    216  1.1  eeh 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    217  1.1  eeh 			goto fail_5;
    218  1.1  eeh 		}
    219  1.1  eeh 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    220  1.1  eeh 	}
    221  1.1  eeh 
    222  1.1  eeh 	/*
    223  1.1  eeh 	 * From this point forward, the attachment cannot fail.  A failure
    224  1.1  eeh 	 * before this point releases all resources that may have been
    225  1.1  eeh 	 * allocated.
    226  1.1  eeh 	 */
    227  1.1  eeh 	sc->sc_flags |= GEMF_ATTACHED;
    228  1.1  eeh 
    229  1.1  eeh 	/* Announce ourselves. */
    230  1.1  eeh 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    231  1.1  eeh 	    ether_sprintf(sc->sc_enaddr));
    232  1.1  eeh 
    233  1.1  eeh 	/* Initialize ifnet structure. */
    234  1.1  eeh 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    235  1.1  eeh 	ifp->if_softc = sc;
    236  1.1  eeh 	ifp->if_flags =
    237  1.1  eeh 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    238  1.1  eeh 	ifp->if_start = gem_start;
    239  1.1  eeh 	ifp->if_ioctl = gem_ioctl;
    240  1.1  eeh 	ifp->if_watchdog = gem_watchdog;
    241  1.1  eeh 	ifp->if_stop = gem_stop;
    242  1.1  eeh 	ifp->if_init = gem_init;
    243  1.1  eeh 	IFQ_SET_READY(&ifp->if_snd);
    244  1.1  eeh 
    245  1.1  eeh 	/* Initialize ifmedia structures and MII info */
    246  1.1  eeh 	mii->mii_ifp = ifp;
    247  1.1  eeh 	mii->mii_readreg = gem_mii_readreg;
    248  1.1  eeh 	mii->mii_writereg = gem_mii_writereg;
    249  1.1  eeh 	mii->mii_statchg = gem_mii_statchg;
    250  1.1  eeh 
    251  1.1  eeh 	ifmedia_init(&mii->mii_media, 0, gem_mediachange, gem_mediastatus);
    252  1.1  eeh 
    253  1.1  eeh 	gem_mifinit(sc);
    254  1.1  eeh 
    255  1.1  eeh 	mii_attach(&sc->sc_dev, mii, 0xffffffff,
    256  1.1  eeh 			MII_PHY_ANY, MII_OFFSET_ANY, 0);
    257  1.1  eeh 
    258  1.1  eeh 	child = LIST_FIRST(&mii->mii_phys);
    259  1.1  eeh 	if (child == NULL) {
    260  1.1  eeh 		/* No PHY attached */
    261  1.1  eeh 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    262  1.1  eeh 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    263  1.1  eeh 	} else {
    264  1.1  eeh 		/*
    265  1.1  eeh 		 * Walk along the list of attached MII devices and
    266  1.1  eeh 		 * establish an `MII instance' to `phy number'
    267  1.1  eeh 		 * mapping. We'll use this mapping in media change
    268  1.1  eeh 		 * requests to determine which phy to use to program
    269  1.1  eeh 		 * the MIF configuration register.
    270  1.1  eeh 		 */
    271  1.1  eeh 		for (; child != NULL; child = LIST_NEXT(child, mii_list)) {
    272  1.1  eeh 			/*
    273  1.1  eeh 			 * Note: we support just two PHYs: the built-in
    274  1.1  eeh 			 * internal device and an external on the MII
    275  1.1  eeh 			 * connector.
    276  1.1  eeh 			 */
    277  1.1  eeh 			if (child->mii_phy > 1 || child->mii_inst > 1) {
    278  1.1  eeh 				printf("%s: cannot accomodate MII device %s"
    279  1.1  eeh 				       " at phy %d, instance %d\n",
    280  1.1  eeh 				       sc->sc_dev.dv_xname,
    281  1.1  eeh 				       child->mii_dev.dv_xname,
    282  1.1  eeh 				       child->mii_phy, child->mii_inst);
    283  1.1  eeh 				continue;
    284  1.1  eeh 			}
    285  1.1  eeh 
    286  1.1  eeh 			sc->sc_phys[child->mii_inst] = child->mii_phy;
    287  1.1  eeh 		}
    288  1.1  eeh 
    289  1.1  eeh 		/*
    290  1.1  eeh 		 * Now select and activate the PHY we will use.
    291  1.1  eeh 		 *
    292  1.1  eeh 		 * The order of preference is External (MDI1),
    293  1.1  eeh 		 * Internal (MDI0), Serial Link (no MII).
    294  1.1  eeh 		 */
    295  1.1  eeh 		if (sc->sc_phys[1]) {
    296  1.1  eeh #ifdef DEBUG
    297  1.1  eeh 			printf("using external phy\n");
    298  1.1  eeh #endif
    299  1.1  eeh 			sc->sc_mif_config |= GEM_MIF_CONFIG_PHY_SEL;
    300  1.1  eeh 		} else {
    301  1.1  eeh #ifdef DEBUG
    302  1.1  eeh 			printf("using internal phy\n");
    303  1.1  eeh #endif
    304  1.1  eeh 			sc->sc_mif_config &= ~GEM_MIF_CONFIG_PHY_SEL;
    305  1.1  eeh 		}
    306  1.1  eeh 		bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_MIF_CONFIG,
    307  1.1  eeh 			sc->sc_mif_config);
    308  1.1  eeh 
    309  1.1  eeh 		/*
    310  1.1  eeh 		 * XXX - we can really do the following ONLY if the
    311  1.1  eeh 		 * phy indeed has the auto negotiation capability!!
    312  1.1  eeh 		 */
    313  1.1  eeh 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
    314  1.1  eeh 	}
    315  1.1  eeh 
    316  1.1  eeh 	/* claim 802.1q capability */
    317  1.1  eeh 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    318  1.1  eeh 
    319  1.1  eeh 	/* Attach the interface. */
    320  1.1  eeh 	if_attach(ifp);
    321  1.1  eeh 	ether_ifattach(ifp, sc->sc_enaddr);
    322  1.1  eeh 
    323  1.1  eeh 	sc->sc_sh = shutdownhook_establish(gem_shutdown, sc);
    324  1.1  eeh 	if (sc->sc_sh == NULL)
    325  1.1  eeh 		panic("gem_config: can't establish shutdownhook");
    326  1.1  eeh 
    327  1.1  eeh #if NRND > 0
    328  1.1  eeh 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    329  1.1  eeh 			  RND_TYPE_NET, 0);
    330  1.1  eeh #endif
    331  1.1  eeh 
    332  1.1  eeh 
    333  1.1  eeh #if notyet
    334  1.1  eeh 	/*
    335  1.1  eeh 	 * Add a suspend hook to make sure we come back up after a
    336  1.1  eeh 	 * resume.
    337  1.1  eeh 	 */
    338  1.1  eeh 	sc->sc_powerhook = powerhook_establish(gem_power, sc);
    339  1.1  eeh 	if (sc->sc_powerhook == NULL)
    340  1.1  eeh 		printf("%s: WARNING: unable to establish power hook\n",
    341  1.1  eeh 		    sc->sc_dev.dv_xname);
    342  1.1  eeh #endif
    343  1.1  eeh 
    344  1.1  eeh 	callout_init(&sc->sc_tick_ch);
    345  1.1  eeh 	return;
    346  1.1  eeh 
    347  1.1  eeh 	/*
    348  1.1  eeh 	 * Free any resources we've allocated during the failed attach
    349  1.1  eeh 	 * attempt.  Do this in reverse order and fall through.
    350  1.1  eeh 	 */
    351  1.1  eeh  fail_5:
    352  1.1  eeh 	for (i = 0; i < GEM_NRXDESC; i++) {
    353  1.1  eeh 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    354  1.1  eeh 			bus_dmamap_destroy(sc->sc_dmatag,
    355  1.1  eeh 			    sc->sc_rxsoft[i].rxs_dmamap);
    356  1.1  eeh 	}
    357  1.1  eeh  fail_4:
    358  1.1  eeh 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
    359  1.1  eeh 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
    360  1.1  eeh 			bus_dmamap_destroy(sc->sc_dmatag,
    361  1.1  eeh 			    sc->sc_txsoft[i].txs_dmamap);
    362  1.1  eeh 	}
    363  1.1  eeh 	bus_dmamap_unload(sc->sc_dmatag, sc->sc_cddmamap);
    364  1.1  eeh  fail_3:
    365  1.1  eeh 	bus_dmamap_destroy(sc->sc_dmatag, sc->sc_cddmamap);
    366  1.1  eeh  fail_2:
    367  1.1  eeh 	bus_dmamem_unmap(sc->sc_dmatag, (caddr_t)sc->sc_control_data,
    368  1.1  eeh 	    sizeof(struct gem_control_data));
    369  1.1  eeh  fail_1:
    370  1.1  eeh 	bus_dmamem_free(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg);
    371  1.1  eeh  fail_0:
    372  1.1  eeh 	return;
    373  1.1  eeh }
    374  1.1  eeh 
    375  1.1  eeh 
    376  1.1  eeh void
    377  1.1  eeh gem_tick(arg)
    378  1.1  eeh 	void *arg;
    379  1.1  eeh {
    380  1.1  eeh 	struct gem_softc *sc = arg;
    381  1.1  eeh 	int s;
    382  1.1  eeh 
    383  1.1  eeh 	s = splnet();
    384  1.1  eeh 	mii_tick(&sc->sc_mii);
    385  1.1  eeh 	splx(s);
    386  1.1  eeh 
    387  1.1  eeh 	callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
    388  1.1  eeh 
    389  1.1  eeh }
    390  1.1  eeh 
    391  1.1  eeh void
    392  1.1  eeh gem_reset(sc)
    393  1.1  eeh 	struct gem_softc *sc;
    394  1.1  eeh {
    395  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
    396  1.1  eeh 	bus_space_handle_t h = sc->sc_h;
    397  1.1  eeh 	int i;
    398  1.1  eeh 	int s;
    399  1.1  eeh 
    400  1.1  eeh 	s = splnet();
    401  1.1  eeh 	DPRINTF(sc, ("%s: gem_reset\n", sc->sc_dev.dv_xname));
    402  1.1  eeh 	gem_reset_rx(sc);
    403  1.1  eeh 	gem_reset_tx(sc);
    404  1.1  eeh 
    405  1.1  eeh 	/* Do a full reset */
    406  1.1  eeh 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX|GEM_RESET_TX);
    407  1.1  eeh 	for (i=TRIES; i--; delay(100))
    408  1.1  eeh 		if ((bus_space_read_4(t, h, GEM_RESET) &
    409  1.1  eeh 			(GEM_RESET_RX|GEM_RESET_TX)) == 0)
    410  1.1  eeh 			break;
    411  1.1  eeh 	if ((bus_space_read_4(t, h, GEM_RESET) &
    412  1.1  eeh 		(GEM_RESET_RX|GEM_RESET_TX)) != 0) {
    413  1.1  eeh 		printf("%s: cannot reset device\n",
    414  1.1  eeh 			sc->sc_dev.dv_xname);
    415  1.1  eeh 	}
    416  1.1  eeh 	splx(s);
    417  1.1  eeh }
    418  1.1  eeh 
    419  1.1  eeh 
    420  1.1  eeh /*
    421  1.1  eeh  * gem_rxdrain:
    422  1.1  eeh  *
    423  1.1  eeh  *	Drain the receive queue.
    424  1.1  eeh  */
    425  1.1  eeh void
    426  1.1  eeh gem_rxdrain(struct gem_softc *sc)
    427  1.1  eeh {
    428  1.1  eeh 	struct gem_rxsoft *rxs;
    429  1.1  eeh 	int i;
    430  1.1  eeh 
    431  1.1  eeh 	for (i = 0; i < GEM_NRXDESC; i++) {
    432  1.1  eeh 		rxs = &sc->sc_rxsoft[i];
    433  1.1  eeh 		if (rxs->rxs_mbuf != NULL) {
    434  1.1  eeh 			bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
    435  1.1  eeh 			m_freem(rxs->rxs_mbuf);
    436  1.1  eeh 			rxs->rxs_mbuf = NULL;
    437  1.1  eeh 		}
    438  1.1  eeh 	}
    439  1.1  eeh }
    440  1.1  eeh 
    441  1.1  eeh /*
    442  1.1  eeh  * Reset the whole thing.
    443  1.1  eeh  */
    444  1.1  eeh void
    445  1.1  eeh gem_stop(struct ifnet *ifp, int disable)
    446  1.1  eeh {
    447  1.1  eeh 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
    448  1.1  eeh 	struct gem_txsoft *txs;
    449  1.1  eeh 
    450  1.1  eeh if (gem_opdebug) printf("in stop %d\n", disable);
    451  1.1  eeh 	DPRINTF(sc, ("%s: gem_stop\n", sc->sc_dev.dv_xname));
    452  1.1  eeh 
    453  1.1  eeh 	callout_stop(&sc->sc_tick_ch);
    454  1.1  eeh 	mii_down(&sc->sc_mii);
    455  1.1  eeh 
    456  1.1  eeh 	/* XXX - Should we reset these instead? */
    457  1.1  eeh 	gem_disable_rx(sc);
    458  1.1  eeh 	gem_disable_rx(sc);
    459  1.1  eeh 
    460  1.1  eeh 	/*
    461  1.1  eeh 	 * Release any queued transmit buffers.
    462  1.1  eeh 	 */
    463  1.1  eeh 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
    464  1.1  eeh 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
    465  1.1  eeh 		if (txs->txs_mbuf != NULL) {
    466  1.1  eeh 			bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
    467  1.1  eeh 			m_freem(txs->txs_mbuf);
    468  1.1  eeh 			txs->txs_mbuf = NULL;
    469  1.1  eeh 		}
    470  1.1  eeh 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
    471  1.1  eeh 	}
    472  1.1  eeh 
    473  1.1  eeh 	if (disable) {
    474  1.1  eeh 		gem_rxdrain(sc);
    475  1.1  eeh 	}
    476  1.1  eeh 
    477  1.1  eeh 	/*
    478  1.1  eeh 	 * Mark the interface down and cancel the watchdog timer.
    479  1.1  eeh 	 */
    480  1.1  eeh 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    481  1.1  eeh 	ifp->if_timer = 0;
    482  1.1  eeh }
    483  1.1  eeh 
    484  1.1  eeh 
    485  1.1  eeh /*
    486  1.1  eeh  * Reset the receiver
    487  1.1  eeh  */
    488  1.1  eeh int
    489  1.1  eeh gem_reset_rx(struct gem_softc *sc)
    490  1.1  eeh {
    491  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
    492  1.1  eeh 	bus_space_handle_t h = sc->sc_h;
    493  1.1  eeh 	int i;
    494  1.1  eeh 
    495  1.1  eeh 
    496  1.1  eeh 	/*
    497  1.1  eeh 	 * Resetting while DMA is in progress can cause a bus hang, so we
    498  1.1  eeh 	 * disable DMA first.
    499  1.1  eeh 	 */
    500  1.1  eeh 	gem_disable_rx(sc);
    501  1.1  eeh 	bus_space_write_4(t, h, GEM_RX_CONFIG, 0);
    502  1.1  eeh 	/* Wait till it finishes */
    503  1.1  eeh 	for (i=TRIES; i--; delay(100))
    504  1.1  eeh 		if ((bus_space_read_4(t, h, GEM_RX_CONFIG) & 1) == 0)
    505  1.1  eeh 			break;
    506  1.1  eeh 	if ((bus_space_read_4(t, h, GEM_RX_CONFIG) & 1) != 0)
    507  1.1  eeh 		printf("%s: cannot disable read dma\n",
    508  1.1  eeh 			sc->sc_dev.dv_xname);
    509  1.1  eeh 
    510  1.1  eeh 	/* Wait 5ms extra. */
    511  1.1  eeh 	delay(5000);
    512  1.1  eeh 
    513  1.1  eeh 	/* Finally, reset the ERX */
    514  1.1  eeh 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX);
    515  1.1  eeh 	/* Wait till it finishes */
    516  1.1  eeh 	for (i=TRIES; i--; delay(100))
    517  1.1  eeh 		if ((bus_space_read_4(t, h, GEM_RESET) & GEM_RESET_RX) == 0)
    518  1.1  eeh 			break;
    519  1.1  eeh 	if ((bus_space_read_4(t, h, GEM_RESET) & GEM_RESET_RX) != 0) {
    520  1.1  eeh 		printf("%s: cannot reset receiver\n",
    521  1.1  eeh 			sc->sc_dev.dv_xname);
    522  1.1  eeh 		return (1);
    523  1.1  eeh 	}
    524  1.1  eeh 	return (0);
    525  1.1  eeh }
    526  1.1  eeh 
    527  1.1  eeh 
    528  1.1  eeh /*
    529  1.1  eeh  * Reset the transmitter
    530  1.1  eeh  */
    531  1.1  eeh int
    532  1.1  eeh gem_reset_tx(struct gem_softc *sc)
    533  1.1  eeh {
    534  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
    535  1.1  eeh 	bus_space_handle_t h = sc->sc_h;
    536  1.1  eeh 	int i;
    537  1.1  eeh 
    538  1.1  eeh 	/*
    539  1.1  eeh 	 * Resetting while DMA is in progress can cause a bus hang, so we
    540  1.1  eeh 	 * disable DMA first.
    541  1.1  eeh 	 */
    542  1.1  eeh 	gem_disable_tx(sc);
    543  1.1  eeh 	bus_space_write_4(t, h, GEM_TX_CONFIG, 0);
    544  1.1  eeh 	/* Wait till it finishes */
    545  1.1  eeh 	for (i=TRIES; i--; delay(100))
    546  1.1  eeh 		if ((bus_space_read_4(t, h, GEM_TX_CONFIG) & 1) == 0)
    547  1.1  eeh 			break;
    548  1.1  eeh 	if ((bus_space_read_4(t, h, GEM_TX_CONFIG) & 1) != 0)
    549  1.1  eeh 		printf("%s: cannot disable read dma\n",
    550  1.1  eeh 			sc->sc_dev.dv_xname);
    551  1.1  eeh 
    552  1.1  eeh 	/* Wait 5ms extra. */
    553  1.1  eeh 	delay(5000);
    554  1.1  eeh 
    555  1.1  eeh 	/* Finally, reset the ETX */
    556  1.1  eeh 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_TX);
    557  1.1  eeh 	/* Wait till it finishes */
    558  1.1  eeh 	for (i=TRIES; i--; delay(100))
    559  1.1  eeh 		if ((bus_space_read_4(t, h, GEM_RESET) & GEM_RESET_TX) == 0)
    560  1.1  eeh 			break;
    561  1.1  eeh 	if ((bus_space_read_4(t, h, GEM_RESET) & GEM_RESET_TX) != 0) {
    562  1.1  eeh 		printf("%s: cannot reset receiver\n",
    563  1.1  eeh 			sc->sc_dev.dv_xname);
    564  1.1  eeh 		return (1);
    565  1.1  eeh 	}
    566  1.1  eeh 	return (0);
    567  1.1  eeh }
    568  1.1  eeh 
    569  1.1  eeh /*
    570  1.1  eeh  * disable receiver.
    571  1.1  eeh  */
    572  1.1  eeh int
    573  1.1  eeh gem_disable_rx(struct gem_softc *sc)
    574  1.1  eeh {
    575  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
    576  1.1  eeh 	bus_space_handle_t h = sc->sc_h;
    577  1.1  eeh 	int i;
    578  1.1  eeh 	u_int32_t cfg;
    579  1.1  eeh 
    580  1.1  eeh 	/* Flip the enable bit */
    581  1.1  eeh 	cfg = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
    582  1.1  eeh 	cfg &= ~GEM_MAC_RX_ENABLE;
    583  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, cfg);
    584  1.1  eeh 
    585  1.1  eeh 	/* Wait for it to finish */
    586  1.1  eeh 	for (i=TRIES; i--; delay(100))
    587  1.1  eeh 		if ((bus_space_read_4(t, h, GEM_MAC_RX_CONFIG) &
    588  1.1  eeh 			GEM_MAC_RX_ENABLE) == 0)
    589  1.1  eeh 			return (0);
    590  1.1  eeh 	return (1);
    591  1.1  eeh }
    592  1.1  eeh 
    593  1.1  eeh /*
    594  1.1  eeh  * disable transmitter.
    595  1.1  eeh  */
    596  1.1  eeh int
    597  1.1  eeh gem_disable_tx(struct gem_softc *sc)
    598  1.1  eeh {
    599  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
    600  1.1  eeh 	bus_space_handle_t h = sc->sc_h;
    601  1.1  eeh 	int i;
    602  1.1  eeh 	u_int32_t cfg;
    603  1.1  eeh 
    604  1.1  eeh 	/* Flip the enable bit */
    605  1.1  eeh 	cfg = bus_space_read_4(t, h, GEM_MAC_TX_CONFIG);
    606  1.1  eeh 	cfg &= ~GEM_MAC_TX_ENABLE;
    607  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_TX_CONFIG, cfg);
    608  1.1  eeh 
    609  1.1  eeh 	/* Wait for it to finish */
    610  1.1  eeh 	for (i=TRIES; i--; delay(100))
    611  1.1  eeh 		if ((bus_space_read_4(t, h, GEM_MAC_TX_CONFIG) &
    612  1.1  eeh 			GEM_MAC_TX_ENABLE) == 0)
    613  1.1  eeh 			return (0);
    614  1.1  eeh 	return (1);
    615  1.1  eeh }
    616  1.1  eeh 
    617  1.1  eeh /*
    618  1.1  eeh  * Initialize interface.
    619  1.1  eeh  */
    620  1.1  eeh int
    621  1.1  eeh gem_meminit(struct gem_softc *sc)
    622  1.1  eeh {
    623  1.1  eeh 	struct gem_rxsoft *rxs;
    624  1.1  eeh 	int i, error;
    625  1.1  eeh 
    626  1.1  eeh 	/*
    627  1.1  eeh 	 * Initialize the transmit descriptor ring.
    628  1.1  eeh 	 */
    629  1.1  eeh 	memset((void *)sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
    630  1.1  eeh 	for (i = 0; i < GEM_NTXDESC; i++) {
    631  1.1  eeh 		sc->sc_txdescs[i].gd_flags = 0;
    632  1.1  eeh 		sc->sc_txdescs[i].gd_addr = 0;
    633  1.1  eeh 	}
    634  1.1  eeh 	GEM_CDTXSYNC(sc, 0, GEM_NTXDESC,
    635  1.1  eeh 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    636  1.1  eeh 	sc->sc_txfree = GEM_NTXDESC;
    637  1.1  eeh 	sc->sc_txnext = 0;
    638  1.1  eeh 
    639  1.1  eeh 	/*
    640  1.1  eeh 	 * Initialize the receive descriptor and receive job
    641  1.1  eeh 	 * descriptor rings.
    642  1.1  eeh 	 */
    643  1.1  eeh 	for (i = 0; i < GEM_NRXDESC; i++) {
    644  1.1  eeh 		rxs = &sc->sc_rxsoft[i];
    645  1.1  eeh 		if (rxs->rxs_mbuf == NULL) {
    646  1.1  eeh 			if ((error = gem_add_rxbuf(sc, i)) != 0) {
    647  1.1  eeh 				printf("%s: unable to allocate or map rx "
    648  1.1  eeh 				    "buffer %d, error = %d\n",
    649  1.1  eeh 				    sc->sc_dev.dv_xname, i, error);
    650  1.1  eeh 				/*
    651  1.1  eeh 				 * XXX Should attempt to run with fewer receive
    652  1.1  eeh 				 * XXX buffers instead of just failing.
    653  1.1  eeh 				 */
    654  1.1  eeh 				gem_rxdrain(sc);
    655  1.1  eeh 				return (1);
    656  1.1  eeh 			}
    657  1.1  eeh 		} else
    658  1.1  eeh 			GEM_INIT_RXDESC(sc, i);
    659  1.1  eeh 	}
    660  1.1  eeh 	sc->sc_rxptr = 0;
    661  1.1  eeh 
    662  1.1  eeh 	return (0);
    663  1.1  eeh }
    664  1.1  eeh 
    665  1.1  eeh static int
    666  1.1  eeh gem_ringsize(int sz)
    667  1.1  eeh {
    668  1.1  eeh 	int v;
    669  1.1  eeh 
    670  1.1  eeh 	switch (sz) {
    671  1.1  eeh 	case 32:
    672  1.1  eeh 		v = GEM_RING_SZ_32;
    673  1.1  eeh 		break;
    674  1.1  eeh 	case 64:
    675  1.1  eeh 		v = GEM_RING_SZ_64;
    676  1.1  eeh 		break;
    677  1.1  eeh 	case 128:
    678  1.1  eeh 		v = GEM_RING_SZ_128;
    679  1.1  eeh 		break;
    680  1.1  eeh 	case 256:
    681  1.1  eeh 		v = GEM_RING_SZ_256;
    682  1.1  eeh 		break;
    683  1.1  eeh 	case 512:
    684  1.1  eeh 		v = GEM_RING_SZ_512;
    685  1.1  eeh 		break;
    686  1.1  eeh 	case 1024:
    687  1.1  eeh 		v = GEM_RING_SZ_1024;
    688  1.1  eeh 		break;
    689  1.1  eeh 	case 2048:
    690  1.1  eeh 		v = GEM_RING_SZ_2048;
    691  1.1  eeh 		break;
    692  1.1  eeh 	case 4096:
    693  1.1  eeh 		v = GEM_RING_SZ_4096;
    694  1.1  eeh 		break;
    695  1.1  eeh 	case 8192:
    696  1.1  eeh 		v = GEM_RING_SZ_8192;
    697  1.1  eeh 		break;
    698  1.1  eeh 	default:
    699  1.1  eeh 		printf("gem: invalid Receive Descriptor ring size\n");
    700  1.1  eeh 		break;
    701  1.1  eeh 	}
    702  1.1  eeh 	return (v);
    703  1.1  eeh }
    704  1.1  eeh 
    705  1.1  eeh /*
    706  1.1  eeh  * Initialization of interface; set up initialization block
    707  1.1  eeh  * and transmit/receive descriptor rings.
    708  1.1  eeh  */
    709  1.1  eeh int
    710  1.1  eeh gem_init(struct ifnet *ifp)
    711  1.1  eeh {
    712  1.1  eeh 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
    713  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
    714  1.1  eeh 	bus_space_handle_t h = sc->sc_h;
    715  1.1  eeh 	int s;
    716  1.1  eeh 	u_int32_t v;
    717  1.1  eeh 
    718  1.1  eeh 	s = splnet();
    719  1.1  eeh 
    720  1.1  eeh 	DPRINTF(sc, ("%s: gem_init: calling stop\n", sc->sc_dev.dv_xname));
    721  1.1  eeh 	/*
    722  1.1  eeh 	 * Initialization sequence. The numbered steps below correspond
    723  1.1  eeh 	 * to the sequence outlined in section 6.3.5.1 in the Ethernet
    724  1.1  eeh 	 * Channel Engine manual (part of the PCIO manual).
    725  1.1  eeh 	 * See also the STP2002-STQ document from Sun Microsystems.
    726  1.1  eeh 	 */
    727  1.1  eeh 
    728  1.1  eeh 	/* step 1 & 2. Reset the Ethernet Channel */
    729  1.1  eeh 	gem_stop(ifp, 0);
    730  1.1  eeh if (gem_opdebug) printf("in init\n");
    731  1.1  eeh 	gem_reset(sc);
    732  1.1  eeh 	DPRINTF(sc, ("%s: gem_init: restarting\n", sc->sc_dev.dv_xname));
    733  1.1  eeh 
    734  1.1  eeh 	/* Re-initialize the MIF */
    735  1.1  eeh 	gem_mifinit(sc);
    736  1.1  eeh 
    737  1.1  eeh 	/* Call MI reset function if any */
    738  1.1  eeh 	if (sc->sc_hwreset)
    739  1.1  eeh 		(*sc->sc_hwreset)(sc);
    740  1.1  eeh 
    741  1.1  eeh 	/* step 3. Setup data structures in host memory */
    742  1.1  eeh 	gem_meminit(sc);
    743  1.1  eeh 
    744  1.1  eeh 	/* step 4. TX MAC registers & counters */
    745  1.1  eeh 	gem_init_regs(sc);
    746  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
    747  1.1  eeh 	    ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
    748  1.1  eeh 	    ETHER_VLAN_ENCAP_LEN + ETHER_MAX_LEN  + sizeof(struct ether_header):
    749  1.1  eeh             ETHER_MAX_LEN + sizeof(struct ether_header)) | (0x2000<<16));
    750  1.1  eeh 
    751  1.1  eeh 	/* step 5. RX MAC registers & counters */
    752  1.1  eeh 	gem_setladrf(sc);
    753  1.1  eeh 
    754  1.1  eeh 	/* step 6 & 7. Program Descriptor Ring Base Addresses */
    755  1.1  eeh 	bus_space_write_8(t, h, GEM_TX_RING_PTR,
    756  1.1  eeh 		GEM_CDTXADDR(sc, 0));
    757  1.1  eeh 	/* Yeeech.  The following has endianness issues. */
    758  1.1  eeh 	bus_space_write_4(t, h, GEM_RX_RING_PTR_HI,
    759  1.1  eeh 		(((uint64_t)GEM_CDRXADDR(sc, 0))>>32));
    760  1.1  eeh 	bus_space_write_4(t, h, GEM_RX_RING_PTR_LO,
    761  1.1  eeh 		GEM_CDRXADDR(sc, 0));
    762  1.1  eeh 
    763  1.1  eeh 	/* step 8. Global Configuration & Interrupt Mask */
    764  1.1  eeh 	bus_space_write_4(t, h, GEM_INTMASK,
    765  1.1  eeh 		      ~(GEM_INTR_TX_INTME|
    766  1.1  eeh 			GEM_INTR_TX_EMPTY|
    767  1.1  eeh 			GEM_INTR_RX_DONE|GEM_INTR_RX_NOBUF|
    768  1.1  eeh 			GEM_INTR_RX_TAG_ERR|GEM_INTR_PCS|
    769  1.1  eeh 			GEM_INTR_MAC_CONTROL|GEM_INTR_MIF|
    770  1.1  eeh 			GEM_INTR_BERR));
    771  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_RX_MASK, 0); /* XXXX */
    772  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_TX_MASK, 0xffff); /* XXXX */
    773  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_CONTROL_MASK, 0); /* XXXX */
    774  1.1  eeh #if 0
    775  1.1  eeh 	if (!sc->sc_pci) {
    776  1.1  eeh 		/* Config SBus */
    777  1.1  eeh 		switch (sc->sc_burst) {
    778  1.1  eeh 		default:
    779  1.1  eeh 			v = 0;
    780  1.1  eeh 			break;
    781  1.1  eeh 		case 16:
    782  1.1  eeh 			v = GEM_SEB_CFG_BURST16;
    783  1.1  eeh 			break;
    784  1.1  eeh 		case 32:
    785  1.1  eeh 			v = GEM_SEB_CFG_BURST32;
    786  1.1  eeh 			break;
    787  1.1  eeh 		case 64:
    788  1.1  eeh 			v = GEM_SEB_CFG_BURST64;
    789  1.1  eeh 			break;
    790  1.1  eeh 		}
    791  1.1  eeh 		bus_space_write_4(t, seb, GEM_SEBI_CFG,
    792  1.1  eeh 			v|GE_SIOCFG_PARITY|GE_SIOCFG_BMODE64);
    793  1.1  eeh 	}
    794  1.1  eeh #endif
    795  1.1  eeh 	/* step 9. ETX Configuration: use mostly default values */
    796  1.1  eeh 
    797  1.1  eeh 	/* Enable DMA */
    798  1.1  eeh 	v = gem_ringsize(GEM_NTXDESC /*XXX*/);
    799  1.1  eeh 	bus_space_write_4(t, h, GEM_TX_CONFIG,
    800  1.1  eeh 		v|GEM_TX_CONFIG_TXDMA_EN|
    801  1.1  eeh 		((0x400<<10)&GEM_TX_CONFIG_TXFIFO_TH));
    802  1.1  eeh 	bus_space_write_4(t, h, GEM_TX_KICK, sc->sc_txnext);
    803  1.1  eeh 
    804  1.1  eeh 	/* step 10. ERX Configuration */
    805  1.1  eeh 
    806  1.1  eeh 	/* Encode Receive Descriptor ring size: four possible values */
    807  1.1  eeh 	v = gem_ringsize(GEM_NRXDESC /*XXX*/);
    808  1.1  eeh 
    809  1.1  eeh 	/* Enable DMA */
    810  1.1  eeh 	bus_space_write_4(t, h, GEM_RX_CONFIG,
    811  1.1  eeh 		v|(GEM_THRSH_1024<<GEM_RX_CONFIG_FIFO_THRS_SHIFT)|
    812  1.1  eeh 		(2<<GEM_RX_CONFIG_FBOFF_SHFT)|GEM_RX_CONFIG_RXDMA_EN|
    813  1.1  eeh 		(0<<GEM_RX_CONFIG_CXM_START_SHFT));
    814  1.1  eeh 	/*
    815  1.1  eeh 	 * The following value is for an OFF Threshold of about 15.5 Kbytes
    816  1.1  eeh 	 * and an ON Threshold of 4K bytes.
    817  1.1  eeh 	 */
    818  1.1  eeh 	bus_space_write_4(t, h, GEM_RX_PAUSE_THRESH, 0xf8 | (0x40 << 12));
    819  1.1  eeh 	bus_space_write_4(t, h, GEM_RX_BLANKING, (2<<12)|6);
    820  1.1  eeh 
    821  1.1  eeh 	/* step 11. Configure Media */
    822  1.1  eeh 	gem_mii_statchg(&sc->sc_dev);
    823  1.1  eeh 
    824  1.1  eeh /* XXXX Serial link needs a whole different setup. */
    825  1.1  eeh 
    826  1.1  eeh 
    827  1.1  eeh 	/* step 12. RX_MAC Configuration Register */
    828  1.1  eeh 	v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
    829  1.1  eeh 	v |= GEM_MAC_RX_ENABLE;
    830  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
    831  1.1  eeh 
    832  1.1  eeh 	/* step 14. Issue Transmit Pending command */
    833  1.1  eeh 
    834  1.1  eeh 	/* Call MI initialization function if any */
    835  1.1  eeh 	if (sc->sc_hwinit)
    836  1.1  eeh 		(*sc->sc_hwinit)(sc);
    837  1.1  eeh 
    838  1.1  eeh 
    839  1.1  eeh 	/* step 15.  Give the reciever a swift kick */
    840  1.1  eeh 	bus_space_write_4(t, h, GEM_RX_KICK, GEM_NRXDESC-4);
    841  1.1  eeh 
    842  1.1  eeh 	/* Start the one second timer. */
    843  1.1  eeh 	callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
    844  1.1  eeh 
    845  1.1  eeh 	ifp->if_flags |= IFF_RUNNING;
    846  1.1  eeh 	ifp->if_flags &= ~IFF_OACTIVE;
    847  1.1  eeh 	ifp->if_timer = 0;
    848  1.1  eeh 	splx(s);
    849  1.1  eeh 
    850  1.1  eeh 	return (0);
    851  1.1  eeh }
    852  1.1  eeh 
    853  1.1  eeh /*
    854  1.1  eeh  * Compare two Ether/802 addresses for equality, inlined and unrolled for
    855  1.1  eeh  * speed.
    856  1.1  eeh  */
    857  1.1  eeh static __inline__ int
    858  1.1  eeh ether_cmp(a, b)
    859  1.1  eeh 	u_char *a, *b;
    860  1.1  eeh {
    861  1.1  eeh 
    862  1.1  eeh 	if (a[5] != b[5] || a[4] != b[4] || a[3] != b[3] ||
    863  1.1  eeh 	    a[2] != b[2] || a[1] != b[1] || a[0] != b[0])
    864  1.1  eeh 		return (0);
    865  1.1  eeh 	return (1);
    866  1.1  eeh }
    867  1.1  eeh 
    868  1.1  eeh 
    869  1.1  eeh void
    870  1.1  eeh gem_init_regs(struct gem_softc *sc)
    871  1.1  eeh {
    872  1.1  eeh 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    873  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
    874  1.1  eeh 	bus_space_handle_t h = sc->sc_h;
    875  1.1  eeh 
    876  1.1  eeh 	/* These regs are not cleared on reset */
    877  1.1  eeh 	sc->sc_inited = 0;
    878  1.1  eeh 	if (!sc->sc_inited) {
    879  1.1  eeh 
    880  1.1  eeh 		/* Wooo.  Magic values. */
    881  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_IPG0, 0);
    882  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_IPG1, 8);
    883  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_IPG2, 4);
    884  1.1  eeh 
    885  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN);
    886  1.1  eeh 		/* Max frame and max burst size */
    887  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
    888  1.1  eeh 			(ifp->if_mtu+18) | (0x2000<<16)/* Burst size */);
    889  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_PREAMBLE_LEN, 0x7);
    890  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_JAM_SIZE, 0x4);
    891  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_ATTEMPT_LIMIT, 0x10);
    892  1.1  eeh 		/* Dunno.... */
    893  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_CONTROL_TYPE, 0x8088);
    894  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_RANDOM_SEED,
    895  1.1  eeh 			((sc->sc_enaddr[5]<<8)|sc->sc_enaddr[4])&0x3ff);
    896  1.1  eeh 		/* Secondary MAC addr set to 0:0:0:0:0:0 */
    897  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR3, 0);
    898  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR4, 0);
    899  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR5, 0);
    900  1.1  eeh 		/* MAC control addr set to 0:1:c2:0:1:80 */
    901  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR6, 0x0001);
    902  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR7, 0xc200);
    903  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR8, 0x0180);
    904  1.1  eeh 
    905  1.1  eeh 		/* MAC filter addr set to 0:0:0:0:0:0 */
    906  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER0, 0);
    907  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER1, 0);
    908  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER2, 0);
    909  1.1  eeh 
    910  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK1_2, 0);
    911  1.1  eeh 		bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK0, 0);
    912  1.1  eeh 
    913  1.1  eeh 		sc->sc_inited = 1;
    914  1.1  eeh 	}
    915  1.1  eeh 
    916  1.1  eeh 	/* Counters need to be zeroed */
    917  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_NORM_COLL_CNT, 0);
    918  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_FIRST_COLL_CNT, 0);
    919  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_EXCESS_COLL_CNT, 0);
    920  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_LATE_COLL_CNT, 0);
    921  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_DEFER_TMR_CNT, 0);
    922  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_PEAK_ATTEMPTS, 0);
    923  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_RX_FRAME_COUNT, 0);
    924  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_RX_LEN_ERR_CNT, 0);
    925  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_RX_ALIGN_ERR, 0);
    926  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_RX_CRC_ERR_CNT, 0);
    927  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_RX_CODE_VIOL, 0);
    928  1.1  eeh 
    929  1.1  eeh 	/* Un-pause stuff */
    930  1.1  eeh #if 0
    931  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0);
    932  1.1  eeh #else
    933  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0);
    934  1.1  eeh #endif
    935  1.1  eeh 
    936  1.1  eeh 	/*
    937  1.1  eeh 	 * Set the station address.
    938  1.1  eeh 	 */
    939  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_ADDR0,
    940  1.1  eeh 		(sc->sc_enaddr[4]<<8) | sc->sc_enaddr[5]);
    941  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_ADDR1,
    942  1.1  eeh 		(sc->sc_enaddr[2]<<8) | sc->sc_enaddr[3]);
    943  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_ADDR2,
    944  1.1  eeh 		(sc->sc_enaddr[0]<<8) | sc->sc_enaddr[1]);
    945  1.1  eeh 
    946  1.1  eeh }
    947  1.1  eeh 
    948  1.1  eeh 
    949  1.1  eeh 
    950  1.1  eeh void
    951  1.1  eeh gem_start(ifp)
    952  1.1  eeh 	struct ifnet *ifp;
    953  1.1  eeh {
    954  1.1  eeh 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
    955  1.1  eeh 	struct mbuf *m0, *m;
    956  1.1  eeh 	struct gem_txsoft *txs, *last_txs;
    957  1.1  eeh 	bus_dmamap_t dmamap;
    958  1.1  eeh 	int error, firsttx, nexttx, lasttx, ofree, seg;
    959  1.1  eeh 
    960  1.1  eeh if (gem_opdebug) printf("in start free %x next %x kick %x\n",
    961  1.1  eeh sc->sc_txfree, sc->sc_txnext,
    962  1.1  eeh bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_KICK));
    963  1.1  eeh 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    964  1.1  eeh 		return;
    965  1.1  eeh 
    966  1.1  eeh 	/*
    967  1.1  eeh 	 * Remember the previous number of free descriptors and
    968  1.1  eeh 	 * the first descriptor we'll use.
    969  1.1  eeh 	 */
    970  1.1  eeh 	ofree = sc->sc_txfree;
    971  1.1  eeh 	firsttx = sc->sc_txnext;
    972  1.1  eeh 
    973  1.1  eeh 	DPRINTF(sc, ("%s: gem_start: txfree %d, txnext %d\n",
    974  1.1  eeh 	    sc->sc_dev.dv_xname, ofree, firsttx));
    975  1.1  eeh 
    976  1.1  eeh 	/*
    977  1.1  eeh 	 * Loop through the send queue, setting up transmit descriptors
    978  1.1  eeh 	 * until we drain the queue, or use up all available transmit
    979  1.1  eeh 	 * descriptors.
    980  1.1  eeh 	 */
    981  1.1  eeh 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
    982  1.1  eeh 	       sc->sc_txfree != 0) {
    983  1.1  eeh 		/*
    984  1.1  eeh 		 * Grab a packet off the queue.
    985  1.1  eeh 		 */
    986  1.1  eeh 		IFQ_POLL(&ifp->if_snd, m0);
    987  1.1  eeh 		if (m0 == NULL)
    988  1.1  eeh 			break;
    989  1.1  eeh 		m = NULL;
    990  1.1  eeh 
    991  1.1  eeh 		dmamap = txs->txs_dmamap;
    992  1.1  eeh 
    993  1.1  eeh 		/*
    994  1.1  eeh 		 * Load the DMA map.  If this fails, the packet either
    995  1.1  eeh 		 * didn't fit in the alloted number of segments, or we were
    996  1.1  eeh 		 * short on resources.  In this case, we'll copy and try
    997  1.1  eeh 		 * again.
    998  1.1  eeh 		 */
    999  1.1  eeh 		if (bus_dmamap_load_mbuf(sc->sc_dmatag, dmamap, m0,
   1000  1.1  eeh 		      BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
   1001  1.1  eeh 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   1002  1.1  eeh 			if (m == NULL) {
   1003  1.1  eeh 				printf("%s: unable to allocate Tx mbuf\n",
   1004  1.1  eeh 				    sc->sc_dev.dv_xname);
   1005  1.1  eeh 				break;
   1006  1.1  eeh 			}
   1007  1.1  eeh 			if (m0->m_pkthdr.len > MHLEN) {
   1008  1.1  eeh 				MCLGET(m, M_DONTWAIT);
   1009  1.1  eeh 				if ((m->m_flags & M_EXT) == 0) {
   1010  1.1  eeh 					printf("%s: unable to allocate Tx "
   1011  1.1  eeh 					    "cluster\n", sc->sc_dev.dv_xname);
   1012  1.1  eeh 					m_freem(m);
   1013  1.1  eeh 					break;
   1014  1.1  eeh 				}
   1015  1.1  eeh 			}
   1016  1.1  eeh 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
   1017  1.1  eeh 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
   1018  1.1  eeh 			error = bus_dmamap_load_mbuf(sc->sc_dmatag, dmamap,
   1019  1.1  eeh 			    m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
   1020  1.1  eeh 			if (error) {
   1021  1.1  eeh 				printf("%s: unable to load Tx buffer, "
   1022  1.1  eeh 				    "error = %d\n", sc->sc_dev.dv_xname, error);
   1023  1.1  eeh 				break;
   1024  1.1  eeh 			}
   1025  1.1  eeh 		}
   1026  1.1  eeh 
   1027  1.1  eeh 		/*
   1028  1.1  eeh 		 * Ensure we have enough descriptors free to describe
   1029  1.1  eeh 		 * the packet.
   1030  1.1  eeh 		 */
   1031  1.1  eeh 		if (dmamap->dm_nsegs > sc->sc_txfree) {
   1032  1.1  eeh 			/*
   1033  1.1  eeh 			 * Not enough free descriptors to transmit this
   1034  1.1  eeh 			 * packet.  We haven't committed to anything yet,
   1035  1.1  eeh 			 * so just unload the DMA map, put the packet
   1036  1.1  eeh 			 * back on the queue, and punt.  Notify the upper
   1037  1.1  eeh 			 * layer that there are no more slots left.
   1038  1.1  eeh 			 *
   1039  1.1  eeh 			 * XXX We could allocate an mbuf and copy, but
   1040  1.1  eeh 			 * XXX it is worth it?
   1041  1.1  eeh 			 */
   1042  1.1  eeh 			ifp->if_flags |= IFF_OACTIVE;
   1043  1.1  eeh 			bus_dmamap_unload(sc->sc_dmatag, dmamap);
   1044  1.1  eeh 			if (m != NULL)
   1045  1.1  eeh 				m_freem(m);
   1046  1.1  eeh 			break;
   1047  1.1  eeh 		}
   1048  1.1  eeh 
   1049  1.1  eeh 		IFQ_DEQUEUE(&ifp->if_snd, m0);
   1050  1.1  eeh 		if (m != NULL) {
   1051  1.1  eeh 			m_freem(m0);
   1052  1.1  eeh 			m0 = m;
   1053  1.1  eeh 		}
   1054  1.1  eeh 
   1055  1.1  eeh 		/*
   1056  1.1  eeh 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
   1057  1.1  eeh 		 */
   1058  1.1  eeh 
   1059  1.1  eeh 		/* Sync the DMA map. */
   1060  1.1  eeh 		bus_dmamap_sync(sc->sc_dmatag, dmamap, 0, dmamap->dm_mapsize,
   1061  1.1  eeh 		    BUS_DMASYNC_PREWRITE);
   1062  1.1  eeh 
   1063  1.1  eeh 		/*
   1064  1.1  eeh 		 * Initialize the transmit descriptors.
   1065  1.1  eeh 		 */
   1066  1.1  eeh 		for (nexttx = sc->sc_txnext, seg = 0;
   1067  1.1  eeh 		     seg < dmamap->dm_nsegs;
   1068  1.1  eeh 		     seg++, nexttx = GEM_NEXTTX(nexttx)) {
   1069  1.1  eeh 			uint64_t flags;
   1070  1.1  eeh 
   1071  1.1  eeh 			/*
   1072  1.1  eeh 			 * If this is the first descriptor we're
   1073  1.1  eeh 			 * enqueueing, set the start of packet flag,
   1074  1.1  eeh 			 * and the checksum stuff if we want the hardware
   1075  1.1  eeh 			 * to do it.
   1076  1.1  eeh 			 */
   1077  1.1  eeh 			sc->sc_txdescs[nexttx].gd_addr =
   1078  1.1  eeh 			    htole64(dmamap->dm_segs[seg].ds_addr);
   1079  1.1  eeh 			flags = dmamap->dm_segs[seg].ds_len & GEM_TD_BUFSIZE;
   1080  1.1  eeh 			if (nexttx == firsttx) {
   1081  1.1  eeh 				flags |= GEM_TD_START_OF_PACKET;
   1082  1.1  eeh 			}
   1083  1.1  eeh 			if (seg == dmamap->dm_nsegs - 1) {
   1084  1.1  eeh 				flags |= GEM_TD_END_OF_PACKET;
   1085  1.1  eeh 			}
   1086  1.1  eeh 			sc->sc_txdescs[nexttx].gd_flags =
   1087  1.1  eeh 				htole64(flags);
   1088  1.1  eeh 			lasttx = nexttx;
   1089  1.1  eeh 		}
   1090  1.1  eeh 
   1091  1.1  eeh #ifdef GEM_DEBUG
   1092  1.1  eeh 		if (ifp->if_flags & IFF_DEBUG) {
   1093  1.1  eeh 			printf("     gem_start %p transmit chain:\n", txs);
   1094  1.1  eeh 			for (seg = sc->sc_txnext;; seg = GEM_NEXTTX(seg)) {
   1095  1.1  eeh 				printf("descriptor %d:\t", seg);
   1096  1.1  eeh 				printf("gd_flags:   0x%016llx\t", (long long)
   1097  1.1  eeh 					le64toh(sc->sc_txdescs[seg].gd_flags));
   1098  1.1  eeh 				printf("gd_addr: 0x%016llx\n", (long long)
   1099  1.1  eeh 					le64toh(sc->sc_txdescs[seg].gd_addr));
   1100  1.1  eeh 				if (seg == lasttx)
   1101  1.1  eeh 					break;
   1102  1.1  eeh 			}
   1103  1.1  eeh 		}
   1104  1.1  eeh #endif
   1105  1.1  eeh 
   1106  1.1  eeh 		/* Sync the descriptors we're using. */
   1107  1.1  eeh 		GEM_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
   1108  1.1  eeh 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1109  1.1  eeh 
   1110  1.1  eeh 		/*
   1111  1.1  eeh 		 * Store a pointer to the packet so we can free it later,
   1112  1.1  eeh 		 * and remember what txdirty will be once the packet is
   1113  1.1  eeh 		 * done.
   1114  1.1  eeh 		 */
   1115  1.1  eeh 		txs->txs_mbuf = m0;
   1116  1.1  eeh 		txs->txs_firstdesc = sc->sc_txnext;
   1117  1.1  eeh 		txs->txs_lastdesc = lasttx;
   1118  1.1  eeh 		txs->txs_ndescs = dmamap->dm_nsegs;
   1119  1.1  eeh 
   1120  1.1  eeh 		/* Advance the tx pointer. */
   1121  1.1  eeh 		sc->sc_txfree -= dmamap->dm_nsegs;
   1122  1.1  eeh 		sc->sc_txnext = nexttx;
   1123  1.1  eeh 
   1124  1.1  eeh 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
   1125  1.1  eeh 		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
   1126  1.1  eeh 
   1127  1.1  eeh 		last_txs = txs;
   1128  1.1  eeh 
   1129  1.1  eeh #if NBPFILTER > 0
   1130  1.1  eeh 		/*
   1131  1.1  eeh 		 * Pass the packet to any BPF listeners.
   1132  1.1  eeh 		 */
   1133  1.1  eeh 		if (ifp->if_bpf)
   1134  1.1  eeh 			bpf_mtap(ifp->if_bpf, m0);
   1135  1.1  eeh #endif /* NBPFILTER > 0 */
   1136  1.1  eeh 	}
   1137  1.1  eeh 
   1138  1.1  eeh 	if (txs == NULL || sc->sc_txfree == 0) {
   1139  1.1  eeh 		/* No more slots left; notify upper layer. */
   1140  1.1  eeh 		ifp->if_flags |= IFF_OACTIVE;
   1141  1.1  eeh 	}
   1142  1.1  eeh 
   1143  1.1  eeh 	if (sc->sc_txfree != ofree) {
   1144  1.1  eeh 		DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
   1145  1.1  eeh 		    sc->sc_dev.dv_xname, lasttx, firsttx));
   1146  1.1  eeh #if 0
   1147  1.1  eeh 		/*
   1148  1.1  eeh 		 * Cause a transmit interrupt to happen on the
   1149  1.1  eeh 		 * last packet we enqueued.
   1150  1.1  eeh 		 */
   1151  1.1  eeh 		sc->sc_txdescs[lasttx].gd_flags |= htole64(GEM_TD_INTERRUPT_ME);
   1152  1.1  eeh 		GEM_CDTXSYNC(sc, lasttx, 1,
   1153  1.1  eeh 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1154  1.1  eeh #endif
   1155  1.1  eeh 		/*
   1156  1.1  eeh 		 * The entire packet chain is set up.
   1157  1.1  eeh 		 * Kick the transmitter.
   1158  1.1  eeh 		 */
   1159  1.1  eeh 		DPRINTF(sc, ("%s: gem_start: kicking tx %d\n",
   1160  1.1  eeh 			sc->sc_dev.dv_xname, nexttx));
   1161  1.1  eeh if (gem_opdebug) {
   1162  1.1  eeh 	int i;
   1163  1.1  eeh 	int64_t pa;
   1164  1.1  eeh 	i = bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_KICK);
   1165  1.1  eeh 	printf("GEM_TX_KICK %x GEM_TX_DATA_PTR %llx GEM_TX_RING_PTR %llx\n",
   1166  1.1  eeh 		i,
   1167  1.1  eeh 		(long long)bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_DATA_PTR),
   1168  1.1  eeh 		(long long)bus_space_read_8(sc->sc_bustag, sc->sc_h, GEM_TX_RING_PTR));
   1169  1.1  eeh 	printf("descriptor %d: ", (i = lasttx));
   1170  1.1  eeh 	printf("gd_flags: 0x%016llx\t", (long long)
   1171  1.1  eeh 		le64toh(sc->sc_txdescs[i].gd_flags));
   1172  1.1  eeh 		pa = le64toh(sc->sc_txdescs[i].gd_addr);
   1173  1.1  eeh 	printf("gd_addr: 0x%016llx\n", (long long) pa);
   1174  1.1  eeh 	printf("GEM_TX_CONFIG %x GEM_MAC_XIF_CONFIG %x GEM_MAC_TX_CONFIG %x\n",
   1175  1.1  eeh 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_CONFIG),
   1176  1.1  eeh 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_XIF_CONFIG),
   1177  1.1  eeh 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_TX_CONFIG));
   1178  1.1  eeh }
   1179  1.1  eeh 		bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_TX_KICK,
   1180  1.1  eeh 			sc->sc_txnext);
   1181  1.1  eeh if (gem_opdebug) printf("gem_start: txkick %x\n",
   1182  1.1  eeh 	bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_KICK));
   1183  1.1  eeh 
   1184  1.1  eeh 		/* Set a watchdog timer in case the chip flakes out. */
   1185  1.1  eeh 		ifp->if_timer = 5;
   1186  1.1  eeh 		DPRINTF(sc, ("%s: gem_start: watchdog %d\n",
   1187  1.1  eeh 			sc->sc_dev.dv_xname, ifp->if_timer));
   1188  1.1  eeh 	}
   1189  1.1  eeh }
   1190  1.1  eeh 
   1191  1.1  eeh /*
   1192  1.1  eeh  * Transmit interrupt.
   1193  1.1  eeh  */
   1194  1.1  eeh int
   1195  1.1  eeh gem_tint(sc)
   1196  1.1  eeh 	struct gem_softc *sc;
   1197  1.1  eeh {
   1198  1.1  eeh 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1199  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
   1200  1.1  eeh 	bus_space_handle_t mac = sc->sc_h;
   1201  1.1  eeh 	struct gem_txsoft *txs;
   1202  1.1  eeh 	int txlast;
   1203  1.1  eeh 
   1204  1.1  eeh 
   1205  1.1  eeh 	DPRINTF(sc, ("%s: gem_tint: sc_flags 0x%08x\n",
   1206  1.1  eeh 	    sc->sc_dev.dv_xname, sc->sc_flags));
   1207  1.1  eeh 
   1208  1.1  eeh 	/*
   1209  1.1  eeh 	 * Unload collision counters
   1210  1.1  eeh 	 */
   1211  1.1  eeh 	ifp->if_collisions +=
   1212  1.1  eeh 		bus_space_read_4(t, mac, GEM_MAC_NORM_COLL_CNT) +
   1213  1.1  eeh 		bus_space_read_4(t, mac, GEM_MAC_FIRST_COLL_CNT) +
   1214  1.1  eeh 		bus_space_read_4(t, mac, GEM_MAC_EXCESS_COLL_CNT) +
   1215  1.1  eeh 		bus_space_read_4(t, mac, GEM_MAC_LATE_COLL_CNT);
   1216  1.1  eeh 
   1217  1.1  eeh 	/*
   1218  1.1  eeh 	 * then clear the hardware counters.
   1219  1.1  eeh 	 */
   1220  1.1  eeh 	bus_space_write_4(t, mac, GEM_MAC_NORM_COLL_CNT, 0);
   1221  1.1  eeh 	bus_space_write_4(t, mac, GEM_MAC_FIRST_COLL_CNT, 0);
   1222  1.1  eeh 	bus_space_write_4(t, mac, GEM_MAC_EXCESS_COLL_CNT, 0);
   1223  1.1  eeh 	bus_space_write_4(t, mac, GEM_MAC_LATE_COLL_CNT, 0);
   1224  1.1  eeh 
   1225  1.1  eeh 	/*
   1226  1.1  eeh 	 * Go through our Tx list and free mbufs for those
   1227  1.1  eeh 	 * frames that have been transmitted.
   1228  1.1  eeh 	 */
   1229  1.1  eeh 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1230  1.1  eeh 		GEM_CDTXSYNC(sc, txs->txs_lastdesc,
   1231  1.1  eeh 		    txs->txs_ndescs,
   1232  1.1  eeh 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1233  1.1  eeh 
   1234  1.1  eeh #ifdef GEM_DEBUG
   1235  1.1  eeh 		if (ifp->if_flags & IFF_DEBUG) {
   1236  1.1  eeh 			int i;
   1237  1.1  eeh 			printf("    txsoft %p transmit chain:\n", txs);
   1238  1.1  eeh 			for (i = txs->txs_firstdesc;; i = GEM_NEXTTX(i)) {
   1239  1.1  eeh 				printf("descriptor %d: ", i);
   1240  1.1  eeh 				printf("gd_flags: 0x%016llx\t", (long long)
   1241  1.1  eeh 					le64toh(sc->sc_txdescs[i].gd_flags));
   1242  1.1  eeh 				printf("gd_addr: 0x%016llx\n", (long long)
   1243  1.1  eeh 					le64toh(sc->sc_txdescs[i].gd_addr));
   1244  1.1  eeh 				if (i == txs->txs_lastdesc)
   1245  1.1  eeh 					break;
   1246  1.1  eeh 			}
   1247  1.1  eeh 		}
   1248  1.1  eeh #endif
   1249  1.1  eeh 
   1250  1.1  eeh 		/*
   1251  1.1  eeh 		 * In theory, we could harveast some descriptors before
   1252  1.1  eeh 		 * the ring is empty, but that's a bit complicated.
   1253  1.1  eeh 		 *
   1254  1.1  eeh 		 * GEM_TX_COMPLETION points to the last descriptor
   1255  1.1  eeh 		 * processed +1.
   1256  1.1  eeh 		 */
   1257  1.1  eeh 		txlast = bus_space_read_4(t, mac, GEM_TX_COMPLETION);
   1258  1.1  eeh 		DPRINTF(sc,
   1259  1.1  eeh 			("gem_tint: txs->txs_lastdesc = %d, txlast = %d\n",
   1260  1.1  eeh 				txs->txs_lastdesc, txlast));
   1261  1.1  eeh 		if (txs->txs_firstdesc <= txs->txs_lastdesc) {
   1262  1.1  eeh 			if ((txlast >= txs->txs_firstdesc) &&
   1263  1.1  eeh 				(txlast <= txs->txs_lastdesc))
   1264  1.1  eeh 				break;
   1265  1.1  eeh 		} else {
   1266  1.1  eeh 			/* Ick -- this command wraps */
   1267  1.1  eeh 			if ((txlast >= txs->txs_firstdesc) ||
   1268  1.1  eeh 				(txlast <= txs->txs_lastdesc))
   1269  1.1  eeh 				break;
   1270  1.1  eeh 		}
   1271  1.1  eeh 
   1272  1.1  eeh 		DPRINTF(sc, ("gem_tint: releasing a desc\n"));
   1273  1.1  eeh 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
   1274  1.1  eeh 
   1275  1.1  eeh 		sc->sc_txfree += txs->txs_ndescs;
   1276  1.1  eeh 
   1277  1.1  eeh 		if (txs->txs_mbuf == NULL) {
   1278  1.1  eeh #ifdef DIAGNOSTIC
   1279  1.1  eeh 				panic("gem_txintr: null mbuf");
   1280  1.1  eeh #endif
   1281  1.1  eeh 		}
   1282  1.1  eeh 
   1283  1.1  eeh 		bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap,
   1284  1.1  eeh 		    0, txs->txs_dmamap->dm_mapsize,
   1285  1.1  eeh 		    BUS_DMASYNC_POSTWRITE);
   1286  1.1  eeh 		bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
   1287  1.1  eeh 		m_freem(txs->txs_mbuf);
   1288  1.1  eeh 		txs->txs_mbuf = NULL;
   1289  1.1  eeh 
   1290  1.1  eeh 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1291  1.1  eeh 
   1292  1.1  eeh 		ifp->if_opackets++;
   1293  1.1  eeh 	}
   1294  1.1  eeh 
   1295  1.1  eeh 	DPRINTF(sc, ("gem_tint: GEM_TX_STATE_MACHINE %x "
   1296  1.1  eeh 		"GEM_TX_DATA_PTR %llx "
   1297  1.1  eeh 		"GEM_TX_COMPLETION %x\n",
   1298  1.1  eeh 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_STATE_MACHINE),
   1299  1.1  eeh 		(long long)bus_space_read_8(sc->sc_bustag, sc->sc_h,
   1300  1.1  eeh 			GEM_TX_DATA_PTR),
   1301  1.1  eeh 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_COMPLETION)));
   1302  1.1  eeh 
   1303  1.1  eeh 	gem_start(ifp);
   1304  1.1  eeh 
   1305  1.1  eeh 	if (SIMPLEQ_FIRST(&sc->sc_txdirtyq) == NULL)
   1306  1.1  eeh 		ifp->if_timer = 0;
   1307  1.1  eeh 	DPRINTF(sc, ("%s: gem_tint: watchdog %d\n",
   1308  1.1  eeh 		sc->sc_dev.dv_xname, ifp->if_timer));
   1309  1.1  eeh 
   1310  1.1  eeh 	return (1);
   1311  1.1  eeh }
   1312  1.1  eeh 
   1313  1.1  eeh /*
   1314  1.1  eeh  * Receive interrupt.
   1315  1.1  eeh  */
   1316  1.1  eeh int
   1317  1.1  eeh gem_rint(sc)
   1318  1.1  eeh 	struct gem_softc *sc;
   1319  1.1  eeh {
   1320  1.1  eeh 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1321  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
   1322  1.1  eeh 	bus_space_handle_t h = sc->sc_h;
   1323  1.1  eeh 	struct ether_header *eh;
   1324  1.1  eeh 	struct gem_rxsoft *rxs;
   1325  1.1  eeh 	struct mbuf *m;
   1326  1.1  eeh 	u_int64_t rxstat;
   1327  1.1  eeh 	int i, len;
   1328  1.1  eeh 
   1329  1.1  eeh 	DPRINTF(sc, ("%s: gem_rint: sc_flags 0x%08x\n",
   1330  1.1  eeh 		sc->sc_dev.dv_xname, sc->sc_flags));
   1331  1.1  eeh 	/*
   1332  1.1  eeh 	 * XXXX Read the lastrx only once at the top for speed.
   1333  1.1  eeh 	 */
   1334  1.1  eeh 	DPRINTF(sc, ("gem_rint: sc->rxptr %d, complete %d\n",
   1335  1.1  eeh 		sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION)));
   1336  1.1  eeh 	for (i = sc->sc_rxptr; i != bus_space_read_4(t, h, GEM_RX_COMPLETION);
   1337  1.1  eeh 	     i = GEM_NEXTRX(i)) {
   1338  1.1  eeh 		rxs = &sc->sc_rxsoft[i];
   1339  1.1  eeh 
   1340  1.1  eeh 		GEM_CDRXSYNC(sc, i,
   1341  1.1  eeh 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1342  1.1  eeh 
   1343  1.1  eeh 		rxstat = le64toh(sc->sc_rxdescs[i].gd_flags);
   1344  1.1  eeh 
   1345  1.1  eeh 		if (rxstat & GEM_RD_OWN) {
   1346  1.1  eeh 			printf("gem_rint: completed descriptor "
   1347  1.1  eeh 				"still owned %d\n", i);
   1348  1.1  eeh 			/*
   1349  1.1  eeh 			 * We have processed all of the receive buffers.
   1350  1.1  eeh 			 */
   1351  1.1  eeh 			break;
   1352  1.1  eeh 		}
   1353  1.1  eeh 
   1354  1.1  eeh 		if (rxstat & GEM_RD_BAD_CRC) {
   1355  1.1  eeh 			printf("%s: receive error: CRC error\n",
   1356  1.1  eeh 				sc->sc_dev.dv_xname);
   1357  1.1  eeh 			GEM_INIT_RXDESC(sc, i);
   1358  1.1  eeh 			continue;
   1359  1.1  eeh 		}
   1360  1.1  eeh 
   1361  1.1  eeh 		bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
   1362  1.1  eeh 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1363  1.1  eeh #ifdef GEM_DEBUG
   1364  1.1  eeh 		if (ifp->if_flags & IFF_DEBUG) {
   1365  1.1  eeh 			printf("    rxsoft %p descriptor %d: ", rxs, i);
   1366  1.1  eeh 			printf("gd_flags: 0x%016llx\t", (long long)
   1367  1.1  eeh 				le64toh(sc->sc_rxdescs[i].gd_flags));
   1368  1.1  eeh 			printf("gd_addr: 0x%016llx\n", (long long)
   1369  1.1  eeh 				le64toh(sc->sc_rxdescs[i].gd_addr));
   1370  1.1  eeh 		}
   1371  1.1  eeh #endif
   1372  1.1  eeh 
   1373  1.1  eeh 		/*
   1374  1.1  eeh 		 * No errors; receive the packet.  Note the Gem
   1375  1.1  eeh 		 * includes the CRC with every packet.
   1376  1.1  eeh 		 */
   1377  1.1  eeh 		len = GEM_RD_BUFLEN(rxstat);
   1378  1.1  eeh 
   1379  1.1  eeh 		/*
   1380  1.1  eeh 		 * We align the mbuf data in gem_add_rxbuf() so
   1381  1.1  eeh 		 * we can use __NO_STRICT_ALIGNMENT here
   1382  1.1  eeh 		 */
   1383  1.1  eeh #define __NO_STRICT_ALIGNMENT
   1384  1.1  eeh #ifdef __NO_STRICT_ALIGNMENT
   1385  1.1  eeh 		/*
   1386  1.1  eeh 		 * Allocate a new mbuf cluster.  If that fails, we are
   1387  1.1  eeh 		 * out of memory, and must drop the packet and recycle
   1388  1.1  eeh 		 * the buffer that's already attached to this descriptor.
   1389  1.1  eeh 		 */
   1390  1.1  eeh 		m = rxs->rxs_mbuf;
   1391  1.1  eeh 		if (gem_add_rxbuf(sc, i) != 0) {
   1392  1.1  eeh 			ifp->if_ierrors++;
   1393  1.1  eeh 			GEM_INIT_RXDESC(sc, i);
   1394  1.1  eeh 			bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
   1395  1.1  eeh 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1396  1.1  eeh 			continue;
   1397  1.1  eeh 		}
   1398  1.1  eeh 		m->m_data += 2; /* We're already off by two */
   1399  1.1  eeh #else
   1400  1.1  eeh 		/*
   1401  1.1  eeh 		 * The Gem's receive buffers must be 4-byte aligned.
   1402  1.1  eeh 		 * But this means that the data after the Ethernet header
   1403  1.1  eeh 		 * is misaligned.  We must allocate a new buffer and
   1404  1.1  eeh 		 * copy the data, shifted forward 2 bytes.
   1405  1.1  eeh 		 */
   1406  1.1  eeh 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1407  1.1  eeh 		if (m == NULL) {
   1408  1.1  eeh  dropit:
   1409  1.1  eeh 			ifp->if_ierrors++;
   1410  1.1  eeh 			GEM_INIT_RXDESC(sc, i);
   1411  1.1  eeh 			bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
   1412  1.1  eeh 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1413  1.1  eeh 			continue;
   1414  1.1  eeh 		}
   1415  1.1  eeh 		if (len > (MHLEN - 2)) {
   1416  1.1  eeh 			MCLGET(m, M_DONTWAIT);
   1417  1.1  eeh 			if ((m->m_flags & M_EXT) == 0) {
   1418  1.1  eeh 				m_freem(m);
   1419  1.1  eeh 				goto dropit;
   1420  1.1  eeh 			}
   1421  1.1  eeh 		}
   1422  1.1  eeh 		m->m_data += 2;
   1423  1.1  eeh 
   1424  1.1  eeh 		/*
   1425  1.1  eeh 		 * Note that we use clusters for incoming frames, so the
   1426  1.1  eeh 		 * buffer is virtually contiguous.
   1427  1.1  eeh 		 */
   1428  1.1  eeh 		memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
   1429  1.1  eeh 
   1430  1.1  eeh 		/* Allow the receive descriptor to continue using its mbuf. */
   1431  1.1  eeh 		GEM_INIT_RXDESC(sc, i);
   1432  1.1  eeh 		bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
   1433  1.1  eeh 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1434  1.1  eeh #endif /* __NO_STRICT_ALIGNMENT */
   1435  1.1  eeh 
   1436  1.1  eeh 		ifp->if_ipackets++;
   1437  1.1  eeh 		eh = mtod(m, struct ether_header *);
   1438  1.1  eeh 		m->m_flags |= M_HASFCS;
   1439  1.1  eeh 		m->m_pkthdr.rcvif = ifp;
   1440  1.1  eeh 		m->m_pkthdr.len = m->m_len = len;
   1441  1.1  eeh 
   1442  1.1  eeh #if NBPFILTER > 0
   1443  1.1  eeh 		/*
   1444  1.1  eeh 		 * Pass this up to any BPF listeners, but only
   1445  1.1  eeh 		 * pass it up the stack if its for us.
   1446  1.1  eeh 		 */
   1447  1.1  eeh 		if (ifp->if_bpf)
   1448  1.1  eeh 			bpf_mtap(ifp->if_bpf, m);
   1449  1.1  eeh #endif /* NPBFILTER > 0 */
   1450  1.1  eeh 
   1451  1.1  eeh #if 0
   1452  1.1  eeh 		/*
   1453  1.1  eeh 		 * We sometimes have to run the 21140 in Hash-Only
   1454  1.1  eeh 		 * mode.  If we're in that mode, and not in promiscuous
   1455  1.1  eeh 		 * mode, and we have a unicast packet that isn't for
   1456  1.1  eeh 		 * us, then drop it.
   1457  1.1  eeh 		 */
   1458  1.1  eeh 		if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY &&
   1459  1.1  eeh 		    (ifp->if_flags & IFF_PROMISC) == 0 &&
   1460  1.1  eeh 		    ETHER_IS_MULTICAST(eh->ether_dhost) == 0 &&
   1461  1.1  eeh 		    memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
   1462  1.1  eeh 			   ETHER_ADDR_LEN) != 0) {
   1463  1.1  eeh 			m_freem(m);
   1464  1.1  eeh 			continue;
   1465  1.1  eeh 		}
   1466  1.1  eeh #endif
   1467  1.1  eeh 
   1468  1.1  eeh 		/* Pass it on. */
   1469  1.1  eeh 		(*ifp->if_input)(ifp, m);
   1470  1.1  eeh 	}
   1471  1.1  eeh 
   1472  1.1  eeh 	/* Update the receive pointer. */
   1473  1.1  eeh 	sc->sc_rxptr = i;
   1474  1.1  eeh 	bus_space_write_4(t, h, GEM_RX_KICK, i);
   1475  1.1  eeh 
   1476  1.1  eeh 	DPRINTF(sc, ("gem_rint: done sc->rxptr %d, complete %d\n",
   1477  1.1  eeh 		sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION)));
   1478  1.1  eeh 
   1479  1.1  eeh 	return (1);
   1480  1.1  eeh }
   1481  1.1  eeh 
   1482  1.1  eeh 
   1483  1.1  eeh /*
   1484  1.1  eeh  * gem_add_rxbuf:
   1485  1.1  eeh  *
   1486  1.1  eeh  *	Add a receive buffer to the indicated descriptor.
   1487  1.1  eeh  */
   1488  1.1  eeh int
   1489  1.1  eeh gem_add_rxbuf(struct gem_softc *sc, int idx)
   1490  1.1  eeh {
   1491  1.1  eeh 	struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx];
   1492  1.1  eeh 	struct mbuf *m;
   1493  1.1  eeh 	int error;
   1494  1.1  eeh 
   1495  1.1  eeh 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1496  1.1  eeh 	if (m == NULL)
   1497  1.1  eeh 		return (ENOBUFS);
   1498  1.1  eeh 
   1499  1.1  eeh 	MCLGET(m, M_DONTWAIT);
   1500  1.1  eeh 	if ((m->m_flags & M_EXT) == 0) {
   1501  1.1  eeh 		m_freem(m);
   1502  1.1  eeh 		return (ENOBUFS);
   1503  1.1  eeh 	}
   1504  1.1  eeh 
   1505  1.1  eeh #ifdef GEM_DEBUG
   1506  1.1  eeh /* bzero the packet to check dma */
   1507  1.1  eeh 	memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size);
   1508  1.1  eeh #endif
   1509  1.1  eeh 
   1510  1.1  eeh 	if (rxs->rxs_mbuf != NULL)
   1511  1.1  eeh 		bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
   1512  1.1  eeh 
   1513  1.1  eeh 	rxs->rxs_mbuf = m;
   1514  1.1  eeh 
   1515  1.1  eeh 	error = bus_dmamap_load(sc->sc_dmatag, rxs->rxs_dmamap,
   1516  1.1  eeh 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   1517  1.1  eeh 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
   1518  1.1  eeh 	if (error) {
   1519  1.1  eeh 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1520  1.1  eeh 		    sc->sc_dev.dv_xname, idx, error);
   1521  1.1  eeh 		panic("gem_add_rxbuf");	/* XXX */
   1522  1.1  eeh 	}
   1523  1.1  eeh 
   1524  1.1  eeh 	bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
   1525  1.1  eeh 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1526  1.1  eeh 
   1527  1.1  eeh 	GEM_INIT_RXDESC(sc, idx);
   1528  1.1  eeh 
   1529  1.1  eeh 	return (0);
   1530  1.1  eeh }
   1531  1.1  eeh 
   1532  1.1  eeh 
   1533  1.1  eeh int
   1534  1.1  eeh gem_eint(sc, status)
   1535  1.1  eeh 	struct gem_softc *sc;
   1536  1.1  eeh 	u_int status;
   1537  1.1  eeh {
   1538  1.1  eeh 	char bits[128];
   1539  1.1  eeh 
   1540  1.1  eeh 	if ((status & GEM_INTR_MIF) != 0) {
   1541  1.1  eeh 		printf("%s: XXXlink status changed\n", sc->sc_dev.dv_xname);
   1542  1.1  eeh 		return (1);
   1543  1.1  eeh 	}
   1544  1.1  eeh 
   1545  1.1  eeh 	printf("%s: status=%s\n", sc->sc_dev.dv_xname,
   1546  1.1  eeh 		bitmask_snprintf(status, GEM_INTR_BITS, bits, sizeof(bits)));
   1547  1.1  eeh 	return (1);
   1548  1.1  eeh }
   1549  1.1  eeh 
   1550  1.1  eeh 
   1551  1.1  eeh int
   1552  1.1  eeh gem_intr(v)
   1553  1.1  eeh 	void *v;
   1554  1.1  eeh {
   1555  1.1  eeh 	struct gem_softc *sc = (struct gem_softc *)v;
   1556  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
   1557  1.1  eeh 	bus_space_handle_t seb = sc->sc_h;
   1558  1.1  eeh 	u_int32_t status;
   1559  1.1  eeh 	int r = 0;
   1560  1.1  eeh 
   1561  1.1  eeh 	char bits[128];
   1562  1.1  eeh 
   1563  1.1  eeh 	status = bus_space_read_4(t, seb, GEM_STATUS);
   1564  1.1  eeh 	DPRINTF(sc, ("%s: gem_intr: cplt %xstatus %s\n",
   1565  1.1  eeh 		sc->sc_dev.dv_xname, (status>>19),
   1566  1.1  eeh 		bitmask_snprintf(status, GEM_INTR_BITS, bits, sizeof(bits))));
   1567  1.1  eeh if (gem_opdebug) printf("%s: gem_intr: cplt %x status %s\n",
   1568  1.1  eeh 	sc->sc_dev.dv_xname, (status>>19),
   1569  1.1  eeh 	bitmask_snprintf(status, GEM_INTR_BITS, bits, sizeof(bits)));
   1570  1.1  eeh if (gem_opdebug && (status & GEM_INTR_TX_DONE)) {
   1571  1.1  eeh 	int i;
   1572  1.1  eeh 	int64_t pa;
   1573  1.1  eeh 	i = bus_space_read_4(t, seb, GEM_TX_KICK);
   1574  1.1  eeh 	printf("GEM_TX_KICK %x GEM_TX_DATA_PTR %llx GEM_TX_RING_PTR %llx\n",
   1575  1.1  eeh 		i, (long long)bus_space_read_4(t, seb, GEM_TX_DATA_PTR),
   1576  1.1  eeh 		(long long)bus_space_read_8(t, seb, GEM_TX_RING_PTR));
   1577  1.1  eeh 	printf("descriptor %d: ", --i);
   1578  1.1  eeh 	printf("gd_flags: 0x%016llx\t", (long long)
   1579  1.1  eeh 		le64toh(sc->sc_txdescs[i].gd_flags));
   1580  1.1  eeh 		pa = le64toh(sc->sc_txdescs[i].gd_addr);
   1581  1.1  eeh 	printf("gd_addr: 0x%016llx\n", (long long) pa);
   1582  1.1  eeh 	printf("GEM_TX_CONFIG %x GEM_MAC_XIF_CONFIG %x GEM_MAC_TX_CONFIG %x "
   1583  1.1  eeh 		"GEM_MAC_TX_STATUS %x\n",
   1584  1.1  eeh 		bus_space_read_4(t, seb, GEM_TX_CONFIG),
   1585  1.1  eeh 		bus_space_read_4(t, seb, GEM_MAC_XIF_CONFIG),
   1586  1.1  eeh 		bus_space_read_4(t, seb, GEM_MAC_TX_CONFIG),
   1587  1.1  eeh 		bus_space_read_4(t, seb, GEM_MAC_TX_STATUS));
   1588  1.1  eeh }
   1589  1.1  eeh 
   1590  1.1  eeh 	if ((status & (GEM_INTR_RX_TAG_ERR | GEM_INTR_BERR)) != 0)
   1591  1.1  eeh 		r |= gem_eint(sc, status);
   1592  1.1  eeh 
   1593  1.1  eeh 	if ((status &
   1594  1.1  eeh 		(GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME))
   1595  1.1  eeh 		!= 0)
   1596  1.1  eeh 		r |= gem_tint(sc);
   1597  1.1  eeh 
   1598  1.1  eeh 	if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0)
   1599  1.1  eeh 		r |= gem_rint(sc);
   1600  1.1  eeh 
   1601  1.1  eeh 	/* We should eventually do more than just print out error stats. */
   1602  1.1  eeh 	if (status & GEM_INTR_TX_MAC) {
   1603  1.1  eeh 		int txstat = bus_space_read_4(t, seb, GEM_MAC_TX_STATUS);
   1604  1.1  eeh 		if (txstat & ~GEM_MAC_TX_XMIT_DONE)
   1605  1.1  eeh 			printf("MAC tx fault, status %x\n", txstat);
   1606  1.1  eeh 	}
   1607  1.1  eeh 	if (status & GEM_INTR_RX_MAC) {
   1608  1.1  eeh 		int rxstat = bus_space_read_4(t, seb, GEM_MAC_RX_STATUS);
   1609  1.1  eeh 		if (rxstat & ~GEM_MAC_RX_DONE)
   1610  1.1  eeh 			printf("MAC rx fault, status %x\n", rxstat);
   1611  1.1  eeh 	}
   1612  1.1  eeh 	return (r);
   1613  1.1  eeh }
   1614  1.1  eeh 
   1615  1.1  eeh 
   1616  1.1  eeh void
   1617  1.1  eeh gem_watchdog(ifp)
   1618  1.1  eeh 	struct ifnet *ifp;
   1619  1.1  eeh {
   1620  1.1  eeh 	struct gem_softc *sc = ifp->if_softc;
   1621  1.1  eeh 
   1622  1.1  eeh 	DPRINTF(sc, ("gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x "
   1623  1.1  eeh 		"GEM_MAC_RX_CONFIG %x\n",
   1624  1.1  eeh 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_CONFIG),
   1625  1.1  eeh 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_STATUS),
   1626  1.1  eeh 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_CONFIG)));
   1627  1.1  eeh 
   1628  1.1  eeh 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1629  1.1  eeh 	++ifp->if_oerrors;
   1630  1.1  eeh 
   1631  1.1  eeh 	/* Try to get more packets going. */
   1632  1.1  eeh //	gem_reset(sc);
   1633  1.1  eeh 	gem_start(ifp);
   1634  1.1  eeh }
   1635  1.1  eeh 
   1636  1.1  eeh /*
   1637  1.1  eeh  * Initialize the MII Management Interface
   1638  1.1  eeh  */
   1639  1.1  eeh void
   1640  1.1  eeh gem_mifinit(sc)
   1641  1.1  eeh 	struct gem_softc *sc;
   1642  1.1  eeh {
   1643  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
   1644  1.1  eeh 	bus_space_handle_t mif = sc->sc_h;
   1645  1.1  eeh 
   1646  1.1  eeh 	/* Configure the MIF in frame mode */
   1647  1.1  eeh 	sc->sc_mif_config = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
   1648  1.1  eeh 	sc->sc_mif_config &= ~GEM_MIF_CONFIG_BB_ENA;
   1649  1.1  eeh 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, sc->sc_mif_config);
   1650  1.1  eeh }
   1651  1.1  eeh 
   1652  1.1  eeh /*
   1653  1.1  eeh  * MII interface
   1654  1.1  eeh  *
   1655  1.1  eeh  * The GEM MII interface supports at least three different operating modes:
   1656  1.1  eeh  *
   1657  1.1  eeh  * Bitbang mode is implemented using data, clock and output enable registers.
   1658  1.1  eeh  *
   1659  1.1  eeh  * Frame mode is implemented by loading a complete frame into the frame
   1660  1.1  eeh  * register and polling the valid bit for completion.
   1661  1.1  eeh  *
   1662  1.1  eeh  * Polling mode uses the frame register but completion is indicated by
   1663  1.1  eeh  * an interrupt.
   1664  1.1  eeh  *
   1665  1.1  eeh  */
   1666  1.1  eeh static int
   1667  1.1  eeh gem_mii_readreg(self, phy, reg)
   1668  1.1  eeh 	struct device *self;
   1669  1.1  eeh 	int phy, reg;
   1670  1.1  eeh {
   1671  1.1  eeh 	struct gem_softc *sc = (void *)self;
   1672  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
   1673  1.1  eeh 	bus_space_handle_t mif = sc->sc_h;
   1674  1.1  eeh 	int n;
   1675  1.1  eeh 	u_int32_t v;
   1676  1.1  eeh 
   1677  1.1  eeh #ifdef GEM_DEBUG1
   1678  1.1  eeh 	if (sc->sc_debug)
   1679  1.1  eeh 		printf("gem_mii_readreg: phy %d reg %d\n", phy, reg);
   1680  1.1  eeh #endif
   1681  1.1  eeh 
   1682  1.1  eeh #if 0
   1683  1.1  eeh 	/* Select the desired PHY in the MIF configuration register */
   1684  1.1  eeh 	v = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
   1685  1.1  eeh 	/* Clear PHY select bit */
   1686  1.1  eeh 	v &= ~GEM_MIF_CONFIG_PHY_SEL;
   1687  1.1  eeh 	if (phy == GEM_PHYAD_EXTERNAL)
   1688  1.1  eeh 		/* Set PHY select bit to get at external device */
   1689  1.1  eeh 		v |= GEM_MIF_CONFIG_PHY_SEL;
   1690  1.1  eeh 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, v);
   1691  1.1  eeh #endif
   1692  1.1  eeh 
   1693  1.1  eeh 	/* Construct the frame command */
   1694  1.1  eeh 	v = (reg << GEM_MIF_REG_SHIFT)	| (phy << GEM_MIF_PHY_SHIFT) |
   1695  1.1  eeh 		GEM_MIF_FRAME_READ;
   1696  1.1  eeh 
   1697  1.1  eeh 	bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
   1698  1.1  eeh 	for (n = 0; n < 100; n++) {
   1699  1.1  eeh 		DELAY(1);
   1700  1.1  eeh 		v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
   1701  1.1  eeh 		if (v & GEM_MIF_FRAME_TA0)
   1702  1.1  eeh 			return (v & GEM_MIF_FRAME_DATA);
   1703  1.1  eeh 	}
   1704  1.1  eeh 
   1705  1.1  eeh 	printf("%s: mii_read timeout\n", sc->sc_dev.dv_xname);
   1706  1.1  eeh 	return (0);
   1707  1.1  eeh }
   1708  1.1  eeh 
   1709  1.1  eeh static void
   1710  1.1  eeh gem_mii_writereg(self, phy, reg, val)
   1711  1.1  eeh 	struct device *self;
   1712  1.1  eeh 	int phy, reg, val;
   1713  1.1  eeh {
   1714  1.1  eeh 	struct gem_softc *sc = (void *)self;
   1715  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
   1716  1.1  eeh 	bus_space_handle_t mif = sc->sc_h;
   1717  1.1  eeh 	int n;
   1718  1.1  eeh 	u_int32_t v;
   1719  1.1  eeh 
   1720  1.1  eeh #ifdef GEM_DEBUG1
   1721  1.1  eeh 	if (sc->sc_debug)
   1722  1.1  eeh 		printf("gem_mii_writereg: phy %d reg %d val %x\n",
   1723  1.1  eeh 			phy, reg, val);
   1724  1.1  eeh #endif
   1725  1.1  eeh 
   1726  1.1  eeh #if 0
   1727  1.1  eeh 	/* Select the desired PHY in the MIF configuration register */
   1728  1.1  eeh 	v = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
   1729  1.1  eeh 	/* Clear PHY select bit */
   1730  1.1  eeh 	v &= ~GEM_MIF_CONFIG_PHY_SEL;
   1731  1.1  eeh 	if (phy == GEM_PHYAD_EXTERNAL)
   1732  1.1  eeh 		/* Set PHY select bit to get at external device */
   1733  1.1  eeh 		v |= GEM_MIF_CONFIG_PHY_SEL;
   1734  1.1  eeh 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, v);
   1735  1.1  eeh #endif
   1736  1.1  eeh 	/* Construct the frame command */
   1737  1.1  eeh 	v = GEM_MIF_FRAME_WRITE			|
   1738  1.1  eeh 	    (phy << GEM_MIF_PHY_SHIFT)		|
   1739  1.1  eeh 	    (reg << GEM_MIF_REG_SHIFT)		|
   1740  1.1  eeh 	    (val & GEM_MIF_FRAME_DATA);
   1741  1.1  eeh 
   1742  1.1  eeh 	bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
   1743  1.1  eeh 	for (n = 0; n < 100; n++) {
   1744  1.1  eeh 		DELAY(1);
   1745  1.1  eeh 		v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
   1746  1.1  eeh 		if (v & GEM_MIF_FRAME_TA0)
   1747  1.1  eeh 			return;
   1748  1.1  eeh 	}
   1749  1.1  eeh 
   1750  1.1  eeh 	printf("%s: mii_write timeout\n", sc->sc_dev.dv_xname);
   1751  1.1  eeh }
   1752  1.1  eeh 
   1753  1.1  eeh static void
   1754  1.1  eeh gem_mii_statchg(dev)
   1755  1.1  eeh 	struct device *dev;
   1756  1.1  eeh {
   1757  1.1  eeh 	struct gem_softc *sc = (void *)dev;
   1758  1.1  eeh 	int instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
   1759  1.1  eeh 	int phy = sc->sc_phys[instance];
   1760  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
   1761  1.1  eeh 	bus_space_handle_t mac = sc->sc_h;
   1762  1.1  eeh 	u_int32_t v;
   1763  1.1  eeh 
   1764  1.1  eeh #ifdef GEM_DEBUG
   1765  1.1  eeh 	if (sc->sc_debug)
   1766  1.1  eeh 		printf("gem_mii_statchg: status change: phy = %d\n", phy);
   1767  1.1  eeh #endif
   1768  1.1  eeh 
   1769  1.1  eeh 
   1770  1.1  eeh 	/* Set tx full duplex options */
   1771  1.1  eeh 	bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, 0);
   1772  1.1  eeh 	delay(10000); /* reg must be cleared and delay before changing. */
   1773  1.1  eeh 	v = GEM_MAC_TX_ENA_IPG0|GEM_MAC_TX_NGU|GEM_MAC_TX_NGU_LIMIT|
   1774  1.1  eeh 		GEM_MAC_TX_ENABLE;
   1775  1.1  eeh 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0) {
   1776  1.1  eeh 		v |= GEM_MAC_TX_IGN_CARRIER|GEM_MAC_TX_IGN_COLLIS;
   1777  1.1  eeh 	}
   1778  1.1  eeh 	bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, v);
   1779  1.1  eeh 
   1780  1.1  eeh 	/* XIF Configuration */
   1781  1.1  eeh  /* We should really calculate all this rather than rely on defaults */
   1782  1.1  eeh 	v = bus_space_read_4(t, mac, GEM_MAC_XIF_CONFIG);
   1783  1.1  eeh 	v = GEM_MAC_XIF_LINK_LED;
   1784  1.1  eeh 	v |= GEM_MAC_XIF_TX_MII_ENA;
   1785  1.1  eeh 	/* If an external transceiver is connected, enable its MII drivers */
   1786  1.1  eeh 	sc->sc_mif_config = bus_space_read_4(t, mac, GEM_MIF_CONFIG);
   1787  1.1  eeh 	if ((sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) != 0) {
   1788  1.1  eeh 		/* External MII needs echo disable if half duplex. */
   1789  1.1  eeh 		if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
   1790  1.1  eeh 			/* turn on full duplex LED */
   1791  1.1  eeh 			v |= GEM_MAC_XIF_FDPLX_LED;
   1792  1.1  eeh  			else
   1793  1.1  eeh 	 			/* half duplex -- disable echo */
   1794  1.1  eeh 		 		v |= GEM_MAC_XIF_ECHO_DISABL;
   1795  1.1  eeh 	} else
   1796  1.1  eeh 		/* Internal MII needs buf enable */
   1797  1.1  eeh 		v |= GEM_MAC_XIF_MII_BUF_ENA;
   1798  1.1  eeh 	bus_space_write_4(t, mac, GEM_MAC_XIF_CONFIG, v);
   1799  1.1  eeh }
   1800  1.1  eeh 
   1801  1.1  eeh int
   1802  1.1  eeh gem_mediachange(ifp)
   1803  1.1  eeh 	struct ifnet *ifp;
   1804  1.1  eeh {
   1805  1.1  eeh 	struct gem_softc *sc = ifp->if_softc;
   1806  1.1  eeh 
   1807  1.1  eeh 	if (IFM_TYPE(sc->sc_media.ifm_media) != IFM_ETHER)
   1808  1.1  eeh 		return (EINVAL);
   1809  1.1  eeh 
   1810  1.1  eeh 	return (mii_mediachg(&sc->sc_mii));
   1811  1.1  eeh }
   1812  1.1  eeh 
   1813  1.1  eeh void
   1814  1.1  eeh gem_mediastatus(ifp, ifmr)
   1815  1.1  eeh 	struct ifnet *ifp;
   1816  1.1  eeh 	struct ifmediareq *ifmr;
   1817  1.1  eeh {
   1818  1.1  eeh 	struct gem_softc *sc = ifp->if_softc;
   1819  1.1  eeh 
   1820  1.1  eeh 	if ((ifp->if_flags & IFF_UP) == 0)
   1821  1.1  eeh 		return;
   1822  1.1  eeh 
   1823  1.1  eeh 	mii_pollstat(&sc->sc_mii);
   1824  1.1  eeh 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   1825  1.1  eeh 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   1826  1.1  eeh }
   1827  1.1  eeh 
   1828  1.1  eeh int gem_ioctldebug = 0;
   1829  1.1  eeh /*
   1830  1.1  eeh  * Process an ioctl request.
   1831  1.1  eeh  */
   1832  1.1  eeh int
   1833  1.1  eeh gem_ioctl(ifp, cmd, data)
   1834  1.1  eeh 	struct ifnet *ifp;
   1835  1.1  eeh 	u_long cmd;
   1836  1.1  eeh 	caddr_t data;
   1837  1.1  eeh {
   1838  1.1  eeh 	struct gem_softc *sc = ifp->if_softc;
   1839  1.1  eeh 	struct ifreq *ifr = (struct ifreq *)data;
   1840  1.1  eeh 	int s, error = 0;
   1841  1.1  eeh 
   1842  1.1  eeh 
   1843  1.1  eeh 	switch (cmd) {
   1844  1.1  eeh 	case SIOCGIFMEDIA:
   1845  1.1  eeh 	case SIOCSIFMEDIA:
   1846  1.1  eeh 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1847  1.1  eeh 		break;
   1848  1.1  eeh 
   1849  1.1  eeh 	default:
   1850  1.1  eeh 		error = ether_ioctl(ifp, cmd, data);
   1851  1.1  eeh 		if (error == ENETRESET) {
   1852  1.1  eeh 			/*
   1853  1.1  eeh 			 * Multicast list has changed; set the hardware filter
   1854  1.1  eeh 			 * accordingly.
   1855  1.1  eeh 			 */
   1856  1.1  eeh if (gem_ioctldebug) printf("reset1\n");
   1857  1.1  eeh 			gem_init(ifp);
   1858  1.1  eeh 			delay(50000);
   1859  1.1  eeh 			error = 0;
   1860  1.1  eeh 		}
   1861  1.1  eeh 		break;
   1862  1.1  eeh 	}
   1863  1.1  eeh 
   1864  1.1  eeh 	/* Try to get things going again */
   1865  1.1  eeh 	if (ifp->if_flags & IFF_UP) {
   1866  1.1  eeh if (gem_ioctldebug) printf("start\n");
   1867  1.1  eeh 		gem_start(ifp);
   1868  1.1  eeh 	}
   1869  1.1  eeh 	splx(s);
   1870  1.1  eeh 	return (error);
   1871  1.1  eeh }
   1872  1.1  eeh 
   1873  1.1  eeh 
   1874  1.1  eeh void
   1875  1.1  eeh gem_shutdown(arg)
   1876  1.1  eeh 	void *arg;
   1877  1.1  eeh {
   1878  1.1  eeh 	struct gem_softc *sc = (struct gem_softc *)arg;
   1879  1.1  eeh 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1880  1.1  eeh 
   1881  1.1  eeh 	gem_stop(ifp, 1);
   1882  1.1  eeh }
   1883  1.1  eeh 
   1884  1.1  eeh /*
   1885  1.1  eeh  * Set up the logical address filter.
   1886  1.1  eeh  */
   1887  1.1  eeh void
   1888  1.1  eeh gem_setladrf(sc)
   1889  1.1  eeh 	struct gem_softc *sc;
   1890  1.1  eeh {
   1891  1.1  eeh 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1892  1.1  eeh 	struct ether_multi *enm;
   1893  1.1  eeh 	struct ether_multistep step;
   1894  1.1  eeh 	struct ethercom *ec = &sc->sc_ethercom;
   1895  1.1  eeh 	bus_space_tag_t t = sc->sc_bustag;
   1896  1.1  eeh 	bus_space_handle_t h = sc->sc_h;
   1897  1.1  eeh 	u_char *cp;
   1898  1.1  eeh 	u_int32_t crc;
   1899  1.1  eeh 	u_int32_t hash[16];
   1900  1.1  eeh 	u_int32_t v;
   1901  1.1  eeh 	int len;
   1902  1.1  eeh 
   1903  1.1  eeh 	/* Clear hash table */
   1904  1.1  eeh 	memset(hash, 0, sizeof(hash));
   1905  1.1  eeh 
   1906  1.1  eeh 	/* Get current RX configuration */
   1907  1.1  eeh 	v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
   1908  1.1  eeh 
   1909  1.1  eeh 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
   1910  1.1  eeh 		/* Turn on promiscuous mode; turn off the hash filter */
   1911  1.1  eeh 		v |= GEM_MAC_RX_PROMISCUOUS;
   1912  1.1  eeh 		v &= ~GEM_MAC_RX_HASH_FILTER;
   1913  1.1  eeh 		ifp->if_flags |= IFF_ALLMULTI;
   1914  1.1  eeh 		goto chipit;
   1915  1.1  eeh 	}
   1916  1.1  eeh 
   1917  1.1  eeh 	/* Turn off promiscuous mode; turn on the hash filter */
   1918  1.1  eeh 	v &= ~GEM_MAC_RX_PROMISCUOUS;
   1919  1.1  eeh 	v |= GEM_MAC_RX_HASH_FILTER;
   1920  1.1  eeh 
   1921  1.1  eeh 	/*
   1922  1.1  eeh 	 * Set up multicast address filter by passing all multicast addresses
   1923  1.1  eeh 	 * through a crc generator, and then using the high order 6 bits as an
   1924  1.1  eeh 	 * index into the 256 bit logical address filter.  The high order bit
   1925  1.1  eeh 	 * selects the word, while the rest of the bits select the bit within
   1926  1.1  eeh 	 * the word.
   1927  1.1  eeh 	 */
   1928  1.1  eeh 
   1929  1.1  eeh 	ETHER_FIRST_MULTI(step, ec, enm);
   1930  1.1  eeh 	while (enm != NULL) {
   1931  1.1  eeh 		if (ether_cmp(enm->enm_addrlo, enm->enm_addrhi)) {
   1932  1.1  eeh 			/*
   1933  1.1  eeh 			 * We must listen to a range of multicast addresses.
   1934  1.1  eeh 			 * For now, just accept all multicasts, rather than
   1935  1.1  eeh 			 * trying to set only those filter bits needed to match
   1936  1.1  eeh 			 * the range.  (At this time, the only use of address
   1937  1.1  eeh 			 * ranges is for IP multicast routing, for which the
   1938  1.1  eeh 			 * range is big enough to require all bits set.)
   1939  1.1  eeh 			 */
   1940  1.1  eeh 			hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
   1941  1.1  eeh 			ifp->if_flags |= IFF_ALLMULTI;
   1942  1.1  eeh 			goto chipit;
   1943  1.1  eeh 		}
   1944  1.1  eeh 
   1945  1.1  eeh 		cp = enm->enm_addrlo;
   1946  1.1  eeh 		crc = 0xffffffff;
   1947  1.1  eeh 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
   1948  1.1  eeh 			int octet = *cp++;
   1949  1.1  eeh 			int i;
   1950  1.1  eeh 
   1951  1.1  eeh #define MC_POLY_LE	0xedb88320UL	/* mcast crc, little endian */
   1952  1.1  eeh 			for (i = 0; i < 8; i++) {
   1953  1.1  eeh 				if ((crc & 1) ^ (octet & 1)) {
   1954  1.1  eeh 					crc >>= 1;
   1955  1.1  eeh 					crc ^= MC_POLY_LE;
   1956  1.1  eeh 				} else {
   1957  1.1  eeh 					crc >>= 1;
   1958  1.1  eeh 				}
   1959  1.1  eeh 				octet >>= 1;
   1960  1.1  eeh 			}
   1961  1.1  eeh 		}
   1962  1.1  eeh 		/* Just want the 8 most significant bits. */
   1963  1.1  eeh 		crc >>= 24;
   1964  1.1  eeh 
   1965  1.1  eeh 		/* Set the corresponding bit in the filter. */
   1966  1.1  eeh 		hash[crc >> 4] |= 1 << (crc & 0xf);
   1967  1.1  eeh 
   1968  1.1  eeh 		ETHER_NEXT_MULTI(step, enm);
   1969  1.1  eeh 	}
   1970  1.1  eeh 
   1971  1.1  eeh 	ifp->if_flags &= ~IFF_ALLMULTI;
   1972  1.1  eeh 
   1973  1.1  eeh chipit:
   1974  1.1  eeh 	/* Now load the hash table into the chip */
   1975  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH0, hash[0]);
   1976  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH1, hash[1]);
   1977  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH2, hash[2]);
   1978  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH3, hash[3]);
   1979  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH4, hash[4]);
   1980  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH5, hash[5]);
   1981  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH6, hash[6]);
   1982  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH7, hash[7]);
   1983  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH8, hash[8]);
   1984  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH9, hash[9]);
   1985  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH10, hash[10]);
   1986  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH11, hash[11]);
   1987  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH12, hash[12]);
   1988  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH13, hash[13]);
   1989  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH14, hash[14]);
   1990  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_HASH15, hash[15]);
   1991  1.1  eeh 
   1992  1.1  eeh 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
   1993  1.1  eeh }
   1994  1.1  eeh 
   1995  1.1  eeh #if notyet
   1996  1.1  eeh 
   1997  1.1  eeh /*
   1998  1.1  eeh  * gem_power:
   1999  1.1  eeh  *
   2000  1.1  eeh  *	Power management (suspend/resume) hook.
   2001  1.1  eeh  */
   2002  1.1  eeh void
   2003  1.1  eeh gem_power(why, arg)
   2004  1.1  eeh 	int why;
   2005  1.1  eeh 	void *arg;
   2006  1.1  eeh {
   2007  1.1  eeh 	struct gem_softc *sc = arg;
   2008  1.1  eeh 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2009  1.1  eeh 	int s;
   2010  1.1  eeh 
   2011  1.1  eeh 	s = splnet();
   2012  1.1  eeh 	switch (why) {
   2013  1.1  eeh 	case PWR_SUSPEND:
   2014  1.1  eeh 	case PWR_STANDBY:
   2015  1.1  eeh 		gem_stop(ifp, 1);
   2016  1.1  eeh 		if (sc->sc_power != NULL)
   2017  1.1  eeh 			(*sc->sc_power)(sc, why);
   2018  1.1  eeh 		break;
   2019  1.1  eeh 	case PWR_RESUME:
   2020  1.1  eeh 		if (ifp->if_flags & IFF_UP) {
   2021  1.1  eeh 			if (sc->sc_power != NULL)
   2022  1.1  eeh 				(*sc->sc_power)(sc, why);
   2023  1.1  eeh 			gem_init(ifp);
   2024  1.1  eeh 		}
   2025  1.1  eeh 		break;
   2026  1.1  eeh 	case PWR_SOFTSUSPEND:
   2027  1.1  eeh 	case PWR_SOFTSTANDBY:
   2028  1.1  eeh 	case PWR_SOFTRESUME:
   2029  1.1  eeh 		break;
   2030  1.1  eeh 	}
   2031  1.1  eeh 	splx(s);
   2032  1.1  eeh }
   2033  1.1  eeh #endif
   2034