Home | History | Annotate | Line # | Download | only in ic
gem.c revision 1.68
      1 /*	$NetBSD: gem.c,v 1.68 2008/01/05 20:27:44 jdc Exp $ */
      2 
      3 /*
      4  *
      5  * Copyright (C) 2001 Eduardo Horvath.
      6  * Copyright (c) 2001-2003 Thomas Moestl
      7  * All rights reserved.
      8  *
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  */
     32 
     33 /*
     34  * Driver for Apple GMAC, Sun ERI and Sun GEM Ethernet controllers
     35  * See `GEM Gigabit Ethernet ASIC Specification'
     36  *   http://www.sun.com/processors/manuals/ge.pdf
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: gem.c,v 1.68 2008/01/05 20:27:44 jdc Exp $");
     41 
     42 #include "opt_inet.h"
     43 #include "bpfilter.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/callout.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/syslog.h>
     50 #include <sys/malloc.h>
     51 #include <sys/kernel.h>
     52 #include <sys/socket.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/errno.h>
     55 #include <sys/device.h>
     56 
     57 #include <machine/endian.h>
     58 
     59 #include <uvm/uvm_extern.h>
     60 
     61 #include <net/if.h>
     62 #include <net/if_dl.h>
     63 #include <net/if_media.h>
     64 #include <net/if_ether.h>
     65 
     66 #ifdef INET
     67 #include <netinet/in.h>
     68 #include <netinet/in_systm.h>
     69 #include <netinet/in_var.h>
     70 #include <netinet/ip.h>
     71 #include <netinet/tcp.h>
     72 #include <netinet/udp.h>
     73 #endif
     74 
     75 #if NBPFILTER > 0
     76 #include <net/bpf.h>
     77 #endif
     78 
     79 #include <sys/bus.h>
     80 #include <sys/intr.h>
     81 
     82 #include <dev/mii/mii.h>
     83 #include <dev/mii/miivar.h>
     84 #include <dev/mii/mii_bitbang.h>
     85 
     86 #include <dev/ic/gemreg.h>
     87 #include <dev/ic/gemvar.h>
     88 
     89 #define TRIES	10000
     90 
     91 static void	gem_start(struct ifnet *);
     92 static void	gem_stop(struct ifnet *, int);
     93 int		gem_ioctl(struct ifnet *, u_long, void *);
     94 void		gem_tick(void *);
     95 void		gem_watchdog(struct ifnet *);
     96 void		gem_shutdown(void *);
     97 void		gem_pcs_start(struct gem_softc *sc);
     98 void		gem_pcs_stop(struct gem_softc *sc, int);
     99 int		gem_init(struct ifnet *);
    100 void		gem_init_regs(struct gem_softc *sc);
    101 static int	gem_ringsize(int sz);
    102 static int	gem_meminit(struct gem_softc *);
    103 void		gem_mifinit(struct gem_softc *);
    104 static int	gem_bitwait(struct gem_softc *sc, bus_space_handle_t, int,
    105 		    u_int32_t, u_int32_t);
    106 void		gem_reset(struct gem_softc *);
    107 int		gem_reset_rx(struct gem_softc *sc);
    108 static void	gem_reset_rxdma(struct gem_softc *sc);
    109 static void	gem_rx_common(struct gem_softc *sc);
    110 int		gem_reset_tx(struct gem_softc *sc);
    111 int		gem_disable_rx(struct gem_softc *sc);
    112 int		gem_disable_tx(struct gem_softc *sc);
    113 static void	gem_rxdrain(struct gem_softc *sc);
    114 int		gem_add_rxbuf(struct gem_softc *sc, int idx);
    115 void		gem_setladrf(struct gem_softc *);
    116 
    117 /* MII methods & callbacks */
    118 static int	gem_mii_readreg(struct device *, int, int);
    119 static void	gem_mii_writereg(struct device *, int, int, int);
    120 static void	gem_mii_statchg(struct device *);
    121 
    122 void		gem_statuschange(struct gem_softc *);
    123 
    124 int		gem_mediachange(struct ifnet *);
    125 void		gem_mediastatus(struct ifnet *, struct ifmediareq *);
    126 
    127 struct mbuf	*gem_get(struct gem_softc *, int, int);
    128 int		gem_put(struct gem_softc *, int, struct mbuf *);
    129 void		gem_read(struct gem_softc *, int, int);
    130 int		gem_pint(struct gem_softc *);
    131 int		gem_eint(struct gem_softc *, u_int);
    132 int		gem_rint(struct gem_softc *);
    133 int		gem_tint(struct gem_softc *);
    134 void		gem_power(int, void *);
    135 
    136 #ifdef GEM_DEBUG
    137 static void gem_txsoft_print(const struct gem_softc *, int, int);
    138 #define	DPRINTF(sc, x)	if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
    139 				printf x
    140 #else
    141 #define	DPRINTF(sc, x)	/* nothing */
    142 #endif
    143 
    144 #define ETHER_MIN_TX (ETHERMIN + sizeof(struct ether_header))
    145 
    146 
    147 /*
    148  * gem_attach:
    149  *
    150  *	Attach a Gem interface to the system.
    151  */
    152 void
    153 gem_attach(sc, enaddr)
    154 	struct gem_softc *sc;
    155 	const uint8_t *enaddr;
    156 {
    157 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    158 	struct mii_data *mii = &sc->sc_mii;
    159 	bus_space_tag_t t = sc->sc_bustag;
    160 	bus_space_handle_t h = sc->sc_h1;
    161 	struct mii_softc *child;
    162 	struct ifmedia_entry *ifm;
    163 	int i, error;
    164 	u_int32_t v;
    165 	char *nullbuf;
    166 
    167 	/* Make sure the chip is stopped. */
    168 	ifp->if_softc = sc;
    169 	gem_reset(sc);
    170 
    171 	/*
    172 	 * Allocate the control data structures, and create and load the
    173 	 * DMA map for it. gem_control_data is 9216 bytes, we have space for
    174 	 * the padding buffer in the bus_dmamem_alloc()'d memory.
    175 	 */
    176 	if ((error = bus_dmamem_alloc(sc->sc_dmatag,
    177 	    sizeof(struct gem_control_data) + ETHER_MIN_TX, PAGE_SIZE,
    178 	    0, &sc->sc_cdseg, 1, &sc->sc_cdnseg, 0)) != 0) {
    179 		aprint_error(
    180 		   "%s: unable to allocate control data, error = %d\n",
    181 		    sc->sc_dev.dv_xname, error);
    182 		goto fail_0;
    183 	}
    184 
    185 	/* XXX should map this in with correct endianness */
    186 	if ((error = bus_dmamem_map(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg,
    187 	    sizeof(struct gem_control_data), (void **)&sc->sc_control_data,
    188 	    BUS_DMA_COHERENT)) != 0) {
    189 		aprint_error("%s: unable to map control data, error = %d\n",
    190 		    sc->sc_dev.dv_xname, error);
    191 		goto fail_1;
    192 	}
    193 
    194 	nullbuf =
    195 	    (char *)sc->sc_control_data + sizeof(struct gem_control_data);
    196 
    197 	if ((error = bus_dmamap_create(sc->sc_dmatag,
    198 	    sizeof(struct gem_control_data), 1,
    199 	    sizeof(struct gem_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    200 		aprint_error("%s: unable to create control data DMA map, "
    201 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    202 		goto fail_2;
    203 	}
    204 
    205 	if ((error = bus_dmamap_load(sc->sc_dmatag, sc->sc_cddmamap,
    206 	    sc->sc_control_data, sizeof(struct gem_control_data), NULL,
    207 	    0)) != 0) {
    208 		aprint_error(
    209 		    "%s: unable to load control data DMA map, error = %d\n",
    210 		    sc->sc_dev.dv_xname, error);
    211 		goto fail_3;
    212 	}
    213 
    214 	memset(nullbuf, 0, ETHER_MIN_TX);
    215 	if ((error = bus_dmamap_create(sc->sc_dmatag,
    216 	    ETHER_MIN_TX, 1, ETHER_MIN_TX, 0, 0, &sc->sc_nulldmamap)) != 0) {
    217 		aprint_error("%s: unable to create padding DMA map, "
    218 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    219 		goto fail_4;
    220 	}
    221 
    222 	if ((error = bus_dmamap_load(sc->sc_dmatag, sc->sc_nulldmamap,
    223 	    nullbuf, ETHER_MIN_TX, NULL, 0)) != 0) {
    224 		aprint_error(
    225 		    "%s: unable to load padding DMA map, error = %d\n",
    226 		    sc->sc_dev.dv_xname, error);
    227 		goto fail_5;
    228 	}
    229 
    230 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_nulldmamap, 0, ETHER_MIN_TX,
    231 	    BUS_DMASYNC_PREWRITE);
    232 
    233 	/*
    234 	 * Initialize the transmit job descriptors.
    235 	 */
    236 	SIMPLEQ_INIT(&sc->sc_txfreeq);
    237 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
    238 
    239 	/*
    240 	 * Create the transmit buffer DMA maps.
    241 	 */
    242 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
    243 		struct gem_txsoft *txs;
    244 
    245 		txs = &sc->sc_txsoft[i];
    246 		txs->txs_mbuf = NULL;
    247 		if ((error = bus_dmamap_create(sc->sc_dmatag,
    248 		    ETHER_MAX_LEN_JUMBO, GEM_NTXSEGS,
    249 		    ETHER_MAX_LEN_JUMBO, 0, 0,
    250 		    &txs->txs_dmamap)) != 0) {
    251 			aprint_error("%s: unable to create tx DMA map %d, "
    252 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    253 			goto fail_6;
    254 		}
    255 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
    256 	}
    257 
    258 	/*
    259 	 * Create the receive buffer DMA maps.
    260 	 */
    261 	for (i = 0; i < GEM_NRXDESC; i++) {
    262 		if ((error = bus_dmamap_create(sc->sc_dmatag, MCLBYTES, 1,
    263 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    264 			aprint_error("%s: unable to create rx DMA map %d, "
    265 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    266 			goto fail_7;
    267 		}
    268 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    269 	}
    270 
    271 	/* Initialize ifmedia structures and MII info */
    272 	mii->mii_ifp = ifp;
    273 	mii->mii_readreg = gem_mii_readreg;
    274 	mii->mii_writereg = gem_mii_writereg;
    275 	mii->mii_statchg = gem_mii_statchg;
    276 
    277 	ifmedia_init(&mii->mii_media, IFM_IMASK, gem_mediachange, gem_mediastatus);
    278 
    279 	/*
    280 	 * Initialization based  on `GEM Gigabit Ethernet ASIC Specification'
    281 	 * Section 3.2.1 `Initialization Sequence'.
    282 	 * However, we can't assume SERDES or Serialink if neither
    283 	 * GEM_MIF_CONFIG_MDI0 nor GEM_MIF_CONFIG_MDI1 are set
    284 	 * being set, as both are set on Sun X1141A (with SERDES).  So,
    285 	 * we rely on our bus attachment setting GEM_SERDES or GEM_SERIAL.
    286 	 */
    287 	gem_mifinit(sc);
    288 
    289 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0) {
    290 		mii_attach(&sc->sc_dev, mii, 0xffffffff,
    291 		    MII_PHY_ANY, MII_OFFSET_ANY, MIIF_FORCEANEG);
    292 		child = LIST_FIRST(&mii->mii_phys);
    293 		if (child == NULL) {
    294 				/* No PHY attached */
    295 				aprint_error("%s: PHY probe failed\n",
    296 				    sc->sc_dev.dv_xname);
    297 				goto fail_7;
    298 		} else {
    299 			/*
    300 			 * Walk along the list of attached MII devices and
    301 			 * establish an `MII instance' to `PHY number'
    302 			 * mapping.
    303 			 */
    304 			for (; child != NULL;
    305 			    child = LIST_NEXT(child, mii_list)) {
    306 				/*
    307 				 * Note: we support just one PHY: the internal
    308 				 * or external MII is already selected for us
    309 				 * by the GEM_MIF_CONFIG  register.
    310 				 */
    311 				if (child->mii_phy > 1 || child->mii_inst > 0) {
    312 					aprint_error(
    313 					    "%s: cannot accommodate MII device"
    314 					    " %s at PHY %d, instance %d\n",
    315 					       sc->sc_dev.dv_xname,
    316 					       child->mii_dev.dv_xname,
    317 					       child->mii_phy, child->mii_inst);
    318 					continue;
    319 				}
    320 				sc->sc_phys[child->mii_inst] = child->mii_phy;
    321 			}
    322 
    323 			if (sc->sc_mif_config & GEM_MIF_CONFIG_MDI0) {
    324 #ifdef GEM_DEBUG
    325 				aprint_debug("%s: using PHY at MDIO_0\n",
    326 				    sc->sc_dev.dv_xname);
    327 #endif
    328 			} else {
    329 #ifdef GEM_DEBUG
    330 				aprint_debug("%s: using PHY at MDIO_1\n",
    331 				    sc->sc_dev.dv_xname);
    332 #endif
    333 			}
    334 			if (sc->sc_variant != GEM_SUN_ERI)
    335 				bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
    336 				    GEM_MII_DATAPATH_MII);
    337 
    338 			/*
    339 			 * XXX - we can really do the following ONLY if the
    340 			 * PHY indeed has the auto negotiation capability!!
    341 			 */
    342 			ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
    343 		}
    344 	} else {
    345 		/* SERDES or Serialink */
    346 		if (sc->sc_flags & GEM_SERDES) {
    347 			bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
    348 			    GEM_MII_DATAPATH_SERDES);
    349 		} else {
    350 			sc->sc_flags |= GEM_SERIAL;
    351 			bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
    352 			    GEM_MII_DATAPATH_SERIAL);
    353 		}
    354 
    355 		aprint_normal("%s: using external PCS %s: ",
    356 		    sc->sc_dev.dv_xname,
    357 		    sc->sc_flags & GEM_SERDES ? "SERDES" : "Serialink");
    358 
    359 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_AUTO, 0, NULL);
    360 		/* Check for FDX and HDX capabilities */
    361 		sc->sc_mii_anar = bus_space_read_4(t, h, GEM_MII_ANAR);
    362 		if (sc->sc_mii_anar & GEM_MII_ANEG_FUL_DUPLX) {
    363 			ifmedia_add(&sc->sc_media,
    364 			    IFM_ETHER|IFM_1000_SX|IFM_MANUAL|IFM_FDX, 0, NULL);
    365 			aprint_normal("1000baseSX-FDX, ");
    366 		}
    367 		if (sc->sc_mii_anar & GEM_MII_ANEG_HLF_DUPLX) {
    368 			ifmedia_add(&sc->sc_media,
    369 			    IFM_ETHER|IFM_1000_SX|IFM_MANUAL|IFM_HDX, 0, NULL);
    370 			aprint_normal("1000baseSX-HDX, ");
    371 		}
    372 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
    373 		sc->sc_mii_media = IFM_AUTO;
    374 		aprint_normal("auto\n");
    375 
    376 		gem_pcs_stop(sc, 1);
    377 	}
    378 
    379 	/*
    380 	 * From this point forward, the attachment cannot fail.  A failure
    381 	 * before this point releases all resources that may have been
    382 	 * allocated.
    383 	 */
    384 
    385 	/* Announce ourselves. */
    386 	aprint_normal("%s: Ethernet address %s", sc->sc_dev.dv_xname,
    387 	    ether_sprintf(enaddr));
    388 
    389 	/* Get RX FIFO size */
    390 	sc->sc_rxfifosize = 64 *
    391 	    bus_space_read_4(t, h, GEM_RX_FIFO_SIZE);
    392 	aprint_normal(", %uKB RX fifo", sc->sc_rxfifosize / 1024);
    393 
    394 	/* Get TX FIFO size */
    395 	v = bus_space_read_4(t, h, GEM_TX_FIFO_SIZE);
    396 	aprint_normal(", %uKB TX fifo\n", v / 16);
    397 
    398 	/* Initialize ifnet structure. */
    399 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    400 	ifp->if_softc = sc;
    401 	ifp->if_flags =
    402 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    403 	sc->sc_if_flags = ifp->if_flags;
    404 	/* The GEM hardware supports basic TCP checksum offloading only. */
    405 	ifp->if_capabilities |=
    406 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx;
    407 	ifp->if_start = gem_start;
    408 	ifp->if_ioctl = gem_ioctl;
    409 	ifp->if_watchdog = gem_watchdog;
    410 	ifp->if_stop = gem_stop;
    411 	ifp->if_init = gem_init;
    412 	IFQ_SET_READY(&ifp->if_snd);
    413 
    414 	/*
    415 	 * If we support GigE media, we support jumbo frames too.
    416 	 * Unless we are Apple.
    417 	 */
    418 	TAILQ_FOREACH(ifm, &sc->sc_media.ifm_list, ifm_list) {
    419 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_1000_T ||
    420 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_1000_SX ||
    421 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_1000_LX ||
    422 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_1000_CX) {
    423 			if (sc->sc_variant != GEM_APPLE_GMAC)
    424 				sc->sc_ethercom.ec_capabilities
    425 				    |= ETHERCAP_JUMBO_MTU;
    426 
    427 			sc->sc_flags |= GEM_GIGABIT;
    428 			break;
    429 		}
    430 	}
    431 
    432 	/* claim 802.1q capability */
    433 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    434 
    435 	/* Attach the interface. */
    436 	if_attach(ifp);
    437 	ether_ifattach(ifp, enaddr);
    438 
    439 	sc->sc_sh = shutdownhook_establish(gem_shutdown, sc);
    440 	if (sc->sc_sh == NULL)
    441 		panic("gem_config: can't establish shutdownhook");
    442 
    443 #if NRND > 0
    444 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    445 			  RND_TYPE_NET, 0);
    446 #endif
    447 
    448 	evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR,
    449 	    NULL, sc->sc_dev.dv_xname, "interrupts");
    450 #ifdef GEM_COUNTERS
    451 	evcnt_attach_dynamic(&sc->sc_ev_txint, EVCNT_TYPE_INTR,
    452 	    &sc->sc_ev_intr, sc->sc_dev.dv_xname, "tx interrupts");
    453 	evcnt_attach_dynamic(&sc->sc_ev_rxint, EVCNT_TYPE_INTR,
    454 	    &sc->sc_ev_intr, sc->sc_dev.dv_xname, "rx interrupts");
    455 	evcnt_attach_dynamic(&sc->sc_ev_rxfull, EVCNT_TYPE_INTR,
    456 	    &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx ring full");
    457 	evcnt_attach_dynamic(&sc->sc_ev_rxnobuf, EVCNT_TYPE_INTR,
    458 	    &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx malloc failure");
    459 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[0], EVCNT_TYPE_INTR,
    460 	    &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx 0desc");
    461 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[1], EVCNT_TYPE_INTR,
    462 	    &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx 1desc");
    463 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[2], EVCNT_TYPE_INTR,
    464 	    &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx 2desc");
    465 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[3], EVCNT_TYPE_INTR,
    466 	    &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx 3desc");
    467 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[4], EVCNT_TYPE_INTR,
    468 	    &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx >3desc");
    469 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[5], EVCNT_TYPE_INTR,
    470 	    &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx >7desc");
    471 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[6], EVCNT_TYPE_INTR,
    472 	    &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx >15desc");
    473 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[7], EVCNT_TYPE_INTR,
    474 	    &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx >31desc");
    475 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[8], EVCNT_TYPE_INTR,
    476 	    &sc->sc_ev_rxint, sc->sc_dev.dv_xname, "rx >63desc");
    477 #endif
    478 
    479 #if notyet
    480 	/*
    481 	 * Add a suspend hook to make sure we come back up after a
    482 	 * resume.
    483 	 */
    484 	sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
    485 	    gem_power, sc);
    486 	if (sc->sc_powerhook == NULL)
    487 		aprint_error("%s: WARNING: unable to establish power hook\n",
    488 		    sc->sc_dev.dv_xname);
    489 #endif
    490 
    491 	callout_init(&sc->sc_tick_ch, 0);
    492 	return;
    493 
    494 	/*
    495 	 * Free any resources we've allocated during the failed attach
    496 	 * attempt.  Do this in reverse order and fall through.
    497 	 */
    498  fail_7:
    499 	for (i = 0; i < GEM_NRXDESC; i++) {
    500 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    501 			bus_dmamap_destroy(sc->sc_dmatag,
    502 			    sc->sc_rxsoft[i].rxs_dmamap);
    503 	}
    504  fail_6:
    505 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
    506 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
    507 			bus_dmamap_destroy(sc->sc_dmatag,
    508 			    sc->sc_txsoft[i].txs_dmamap);
    509 	}
    510 	bus_dmamap_unload(sc->sc_dmatag, sc->sc_cddmamap);
    511  fail_5:
    512 	bus_dmamap_destroy(sc->sc_dmatag, sc->sc_nulldmamap);
    513  fail_4:
    514 	bus_dmamem_unmap(sc->sc_dmatag, (void *)nullbuf, ETHER_MIN_TX);
    515  fail_3:
    516 	bus_dmamap_destroy(sc->sc_dmatag, sc->sc_cddmamap);
    517  fail_2:
    518 	bus_dmamem_unmap(sc->sc_dmatag, (void *)sc->sc_control_data,
    519 	    sizeof(struct gem_control_data));
    520  fail_1:
    521 	bus_dmamem_free(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg);
    522  fail_0:
    523 	return;
    524 }
    525 
    526 
    527 void
    528 gem_tick(arg)
    529 	void *arg;
    530 {
    531 	struct gem_softc *sc = arg;
    532 	int s;
    533 
    534 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) != 0) {
    535 		/*
    536 		 * We have to reset everything if we failed to get a
    537 		 * PCS interrupt.  Restarting the callout is handled
    538 		 * in gem_pcs_start().
    539 		 */
    540 		gem_init(&sc->sc_ethercom.ec_if);
    541 	} else {
    542 		s = splnet();
    543 		mii_tick(&sc->sc_mii);
    544 		splx(s);
    545 		callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
    546 	}
    547 }
    548 
    549 static int
    550 gem_bitwait(sc, h, r, clr, set)
    551 	struct gem_softc *sc;
    552 	bus_space_handle_t h;
    553 	int r;
    554 	u_int32_t clr;
    555 	u_int32_t set;
    556 {
    557 	int i;
    558 	u_int32_t reg;
    559 
    560 	for (i = TRIES; i--; DELAY(100)) {
    561 		reg = bus_space_read_4(sc->sc_bustag, h, r);
    562 		if ((reg & clr) == 0 && (reg & set) == set)
    563 			return (1);
    564 	}
    565 	return (0);
    566 }
    567 
    568 void
    569 gem_reset(sc)
    570 	struct gem_softc *sc;
    571 {
    572 	bus_space_tag_t t = sc->sc_bustag;
    573 	bus_space_handle_t h = sc->sc_h2;
    574 	int s;
    575 
    576 	s = splnet();
    577 	DPRINTF(sc, ("%s: gem_reset\n", sc->sc_dev.dv_xname));
    578 	gem_reset_rx(sc);
    579 	gem_reset_tx(sc);
    580 
    581 	/* Do a full reset */
    582 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX|GEM_RESET_TX);
    583 	if (!gem_bitwait(sc, h, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0))
    584 		printf("%s: cannot reset device\n", sc->sc_dev.dv_xname);
    585 	splx(s);
    586 }
    587 
    588 
    589 /*
    590  * gem_rxdrain:
    591  *
    592  *	Drain the receive queue.
    593  */
    594 static void
    595 gem_rxdrain(struct gem_softc *sc)
    596 {
    597 	struct gem_rxsoft *rxs;
    598 	int i;
    599 
    600 	for (i = 0; i < GEM_NRXDESC; i++) {
    601 		rxs = &sc->sc_rxsoft[i];
    602 		if (rxs->rxs_mbuf != NULL) {
    603 			bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
    604 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
    605 			bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
    606 			m_freem(rxs->rxs_mbuf);
    607 			rxs->rxs_mbuf = NULL;
    608 		}
    609 	}
    610 }
    611 
    612 /*
    613  * Reset the whole thing.
    614  */
    615 static void
    616 gem_stop(struct ifnet *ifp, int disable)
    617 {
    618 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
    619 	struct gem_txsoft *txs;
    620 
    621 	DPRINTF(sc, ("%s: gem_stop\n", sc->sc_dev.dv_xname));
    622 
    623 	callout_stop(&sc->sc_tick_ch);
    624 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) != 0)
    625 		gem_pcs_stop(sc, disable);
    626 	else
    627 		mii_down(&sc->sc_mii);
    628 
    629 	/* XXX - Should we reset these instead? */
    630 	gem_disable_tx(sc);
    631 	gem_disable_rx(sc);
    632 
    633 	/*
    634 	 * Release any queued transmit buffers.
    635 	 */
    636 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
    637 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
    638 		if (txs->txs_mbuf != NULL) {
    639 			bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap, 0,
    640 			    txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    641 			bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
    642 			m_freem(txs->txs_mbuf);
    643 			txs->txs_mbuf = NULL;
    644 		}
    645 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
    646 	}
    647 
    648 	if (disable) {
    649 		gem_rxdrain(sc);
    650 	}
    651 
    652 	/*
    653 	 * Mark the interface down and cancel the watchdog timer.
    654 	 */
    655 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    656 	sc->sc_if_flags = ifp->if_flags;
    657 	ifp->if_timer = 0;
    658 }
    659 
    660 
    661 /*
    662  * Reset the receiver
    663  */
    664 int
    665 gem_reset_rx(struct gem_softc *sc)
    666 {
    667 	bus_space_tag_t t = sc->sc_bustag;
    668 	bus_space_handle_t h = sc->sc_h1, h2 = sc->sc_h2;
    669 
    670 	/*
    671 	 * Resetting while DMA is in progress can cause a bus hang, so we
    672 	 * disable DMA first.
    673 	 */
    674 	gem_disable_rx(sc);
    675 	bus_space_write_4(t, h, GEM_RX_CONFIG, 0);
    676 	bus_space_barrier(t, h, GEM_RX_CONFIG, 4, BUS_SPACE_BARRIER_WRITE);
    677 	/* Wait till it finishes */
    678 	if (!gem_bitwait(sc, h, GEM_RX_CONFIG, 1, 0))
    679 		printf("%s: cannot disable read dma\n", sc->sc_dev.dv_xname);
    680 
    681 	/* Finally, reset the ERX */
    682 	bus_space_write_4(t, h2, GEM_RESET, GEM_RESET_RX);
    683 	bus_space_barrier(t, h, GEM_RESET, 4, BUS_SPACE_BARRIER_WRITE);
    684 	/* Wait till it finishes */
    685 	if (!gem_bitwait(sc, h2, GEM_RESET, GEM_RESET_RX, 0)) {
    686 		printf("%s: cannot reset receiver\n", sc->sc_dev.dv_xname);
    687 		return (1);
    688 	}
    689 	return (0);
    690 }
    691 
    692 
    693 /*
    694  * Reset the receiver DMA engine.
    695  *
    696  * Intended to be used in case of GEM_INTR_RX_TAG_ERR, GEM_MAC_RX_OVERFLOW
    697  * etc in order to reset the receiver DMA engine only and not do a full
    698  * reset which amongst others also downs the link and clears the FIFOs.
    699  */
    700 static void
    701 gem_reset_rxdma(struct gem_softc *sc)
    702 {
    703 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    704 	bus_space_tag_t t = sc->sc_bustag;
    705 	bus_space_handle_t h = sc->sc_h1;
    706 	int i;
    707 
    708 	if (gem_reset_rx(sc) != 0) {
    709 		gem_init(ifp);
    710 		return;
    711 	}
    712 	for (i = 0; i < GEM_NRXDESC; i++)
    713 		if (sc->sc_rxsoft[i].rxs_mbuf != NULL)
    714 			GEM_UPDATE_RXDESC(sc, i);
    715 	sc->sc_rxptr = 0;
    716 	GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE);
    717 	GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD);
    718 
    719 	/* Reprogram Descriptor Ring Base Addresses */
    720 	/* NOTE: we use only 32-bit DMA addresses here. */
    721 	bus_space_write_4(t, h, GEM_RX_RING_PTR_HI, 0);
    722 	bus_space_write_4(t, h, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0));
    723 
    724 	/* Redo ERX Configuration */
    725 	gem_rx_common(sc);
    726 
    727 	/* Give the reciever a swift kick */
    728 	bus_space_write_4(t, h, GEM_RX_KICK, GEM_NRXDESC - 4);
    729 }
    730 
    731 /*
    732  * Common RX configuration for gem_init() and gem_reset_rxdma().
    733  */
    734 static void
    735 gem_rx_common(struct gem_softc *sc)
    736 {
    737 	bus_space_tag_t t = sc->sc_bustag;
    738 	bus_space_handle_t h = sc->sc_h1;
    739 	u_int32_t v;
    740 
    741 	/* Encode Receive Descriptor ring size: four possible values */
    742 	v = gem_ringsize(GEM_NRXDESC /*XXX*/);
    743 
    744 	/* Set receive h/w checksum offset */
    745 #ifdef INET
    746 	v |= (ETHER_HDR_LEN + sizeof(struct ip) +
    747 	    ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
    748 	    ETHER_VLAN_ENCAP_LEN : 0)) << GEM_RX_CONFIG_CXM_START_SHFT;
    749 #endif
    750 
    751 	/* Enable RX DMA */
    752 	bus_space_write_4(t, h, GEM_RX_CONFIG,
    753 	    v | (GEM_THRSH_1024 << GEM_RX_CONFIG_FIFO_THRS_SHIFT) |
    754 	    (2 << GEM_RX_CONFIG_FBOFF_SHFT) | GEM_RX_CONFIG_RXDMA_EN);
    755 
    756 	/*
    757 	 * The following value is for an OFF Threshold of about 3/4 full
    758 	 * and an ON Threshold of 1/4 full.
    759 	 */
    760 	bus_space_write_4(t, h, GEM_RX_PAUSE_THRESH,
    761 	    (3 * sc->sc_rxfifosize / 256) |
    762 	    ((sc->sc_rxfifosize / 256) << 12));
    763 	bus_space_write_4(t, h, GEM_RX_BLANKING,
    764 	    (6 << GEM_RX_BLANKING_TIME_SHIFT) | 6);
    765 }
    766 
    767 /*
    768  * Reset the transmitter
    769  */
    770 int
    771 gem_reset_tx(struct gem_softc *sc)
    772 {
    773 	bus_space_tag_t t = sc->sc_bustag;
    774 	bus_space_handle_t h = sc->sc_h1, h2 = sc->sc_h2;
    775 
    776 	/*
    777 	 * Resetting while DMA is in progress can cause a bus hang, so we
    778 	 * disable DMA first.
    779 	 */
    780 	gem_disable_tx(sc);
    781 	bus_space_write_4(t, h, GEM_TX_CONFIG, 0);
    782 	bus_space_barrier(t, h, GEM_TX_CONFIG, 4, BUS_SPACE_BARRIER_WRITE);
    783 	/* Wait till it finishes */
    784 	if (!gem_bitwait(sc, h, GEM_TX_CONFIG, 1, 0))
    785 		printf("%s: cannot disable read dma\n", sc->sc_dev.dv_xname);
    786 	/* Wait 5ms extra. */
    787 	delay(5000);
    788 
    789 	/* Finally, reset the ETX */
    790 	bus_space_write_4(t, h2, GEM_RESET, GEM_RESET_TX);
    791 	bus_space_barrier(t, h, GEM_RESET, 4, BUS_SPACE_BARRIER_WRITE);
    792 	/* Wait till it finishes */
    793 	if (!gem_bitwait(sc, h2, GEM_RESET, GEM_RESET_TX, 0)) {
    794 		printf("%s: cannot reset receiver\n",
    795 			sc->sc_dev.dv_xname);
    796 		return (1);
    797 	}
    798 	return (0);
    799 }
    800 
    801 /*
    802  * disable receiver.
    803  */
    804 int
    805 gem_disable_rx(struct gem_softc *sc)
    806 {
    807 	bus_space_tag_t t = sc->sc_bustag;
    808 	bus_space_handle_t h = sc->sc_h1;
    809 	u_int32_t cfg;
    810 
    811 	/* Flip the enable bit */
    812 	cfg = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
    813 	cfg &= ~GEM_MAC_RX_ENABLE;
    814 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, cfg);
    815 	bus_space_barrier(t, h, GEM_MAC_RX_CONFIG, 4, BUS_SPACE_BARRIER_WRITE);
    816 	/* Wait for it to finish */
    817 	return (gem_bitwait(sc, h, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0));
    818 }
    819 
    820 /*
    821  * disable transmitter.
    822  */
    823 int
    824 gem_disable_tx(struct gem_softc *sc)
    825 {
    826 	bus_space_tag_t t = sc->sc_bustag;
    827 	bus_space_handle_t h = sc->sc_h1;
    828 	u_int32_t cfg;
    829 
    830 	/* Flip the enable bit */
    831 	cfg = bus_space_read_4(t, h, GEM_MAC_TX_CONFIG);
    832 	cfg &= ~GEM_MAC_TX_ENABLE;
    833 	bus_space_write_4(t, h, GEM_MAC_TX_CONFIG, cfg);
    834 	bus_space_barrier(t, h, GEM_MAC_TX_CONFIG, 4, BUS_SPACE_BARRIER_WRITE);
    835 	/* Wait for it to finish */
    836 	return (gem_bitwait(sc, h, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0));
    837 }
    838 
    839 /*
    840  * Initialize interface.
    841  */
    842 int
    843 gem_meminit(struct gem_softc *sc)
    844 {
    845 	struct gem_rxsoft *rxs;
    846 	int i, error;
    847 
    848 	/*
    849 	 * Initialize the transmit descriptor ring.
    850 	 */
    851 	memset((void *)sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
    852 	for (i = 0; i < GEM_NTXDESC; i++) {
    853 		sc->sc_txdescs[i].gd_flags = 0;
    854 		sc->sc_txdescs[i].gd_addr = 0;
    855 	}
    856 	GEM_CDTXSYNC(sc, 0, GEM_NTXDESC,
    857 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    858 	sc->sc_txfree = GEM_NTXDESC-1;
    859 	sc->sc_txnext = 0;
    860 	sc->sc_txwin = 0;
    861 
    862 	/*
    863 	 * Initialize the receive descriptor and receive job
    864 	 * descriptor rings.
    865 	 */
    866 	for (i = 0; i < GEM_NRXDESC; i++) {
    867 		rxs = &sc->sc_rxsoft[i];
    868 		if (rxs->rxs_mbuf == NULL) {
    869 			if ((error = gem_add_rxbuf(sc, i)) != 0) {
    870 				printf("%s: unable to allocate or map rx "
    871 				    "buffer %d, error = %d\n",
    872 				    sc->sc_dev.dv_xname, i, error);
    873 				/*
    874 				 * XXX Should attempt to run with fewer receive
    875 				 * XXX buffers instead of just failing.
    876 				 */
    877 				gem_rxdrain(sc);
    878 				return (1);
    879 			}
    880 		} else
    881 			GEM_INIT_RXDESC(sc, i);
    882 	}
    883 	sc->sc_rxptr = 0;
    884 	sc->sc_meminited = 1;
    885 	GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE);
    886 	GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD);
    887 
    888 	return (0);
    889 }
    890 
    891 static int
    892 gem_ringsize(int sz)
    893 {
    894 	switch (sz) {
    895 	case 32:
    896 		return GEM_RING_SZ_32;
    897 	case 64:
    898 		return GEM_RING_SZ_64;
    899 	case 128:
    900 		return GEM_RING_SZ_128;
    901 	case 256:
    902 		return GEM_RING_SZ_256;
    903 	case 512:
    904 		return GEM_RING_SZ_512;
    905 	case 1024:
    906 		return GEM_RING_SZ_1024;
    907 	case 2048:
    908 		return GEM_RING_SZ_2048;
    909 	case 4096:
    910 		return GEM_RING_SZ_4096;
    911 	case 8192:
    912 		return GEM_RING_SZ_8192;
    913 	default:
    914 		printf("gem: invalid Receive Descriptor ring size %d\n", sz);
    915 		return GEM_RING_SZ_32;
    916 	}
    917 }
    918 
    919 
    920 /*
    921  * Start PCS
    922  */
    923 void
    924 gem_pcs_start(struct gem_softc *sc)
    925 {
    926 	bus_space_tag_t t = sc->sc_bustag;
    927 	bus_space_handle_t h = sc->sc_h1;
    928 	uint32_t v;
    929 
    930 #ifdef GEM_DEBUG
    931 	aprint_debug("%s: gem_pcs_start()\n", sc->sc_dev.dv_xname);
    932 #endif
    933 
    934 	/*
    935 	 * Set up.  We must disable the MII before modifying the
    936 	 * GEM_MII_ANAR register
    937 	 */
    938 	if (sc->sc_flags & GEM_SERDES) {
    939 		bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
    940 		    GEM_MII_DATAPATH_SERDES);
    941 		bus_space_write_4(t, h, GEM_MII_SLINK_CONTROL,
    942 		    GEM_MII_SLINK_LOOPBACK);
    943 	} else {
    944 		bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
    945 		    GEM_MII_DATAPATH_SERIAL);
    946 		bus_space_write_4(t, h, GEM_MII_SLINK_CONTROL, 0);
    947 	}
    948 	bus_space_write_4(t, h, GEM_MII_CONFIG, 0);
    949 	v = bus_space_read_4(t, h, GEM_MII_ANAR);
    950 	v |= (GEM_MII_ANEG_SYM_PAUSE | GEM_MII_ANEG_ASYM_PAUSE);
    951 	if (sc->sc_mii_media == IFM_AUTO)
    952 		v |= (GEM_MII_ANEG_FUL_DUPLX | GEM_MII_ANEG_HLF_DUPLX);
    953 	else if (sc->sc_mii_media == IFM_FDX) {
    954 		v |= GEM_MII_ANEG_FUL_DUPLX;
    955 		v &= ~GEM_MII_ANEG_HLF_DUPLX;
    956 	} else if (sc->sc_mii_media == IFM_HDX) {
    957 		v &= ~GEM_MII_ANEG_FUL_DUPLX;
    958 		v |= GEM_MII_ANEG_HLF_DUPLX;
    959 	}
    960 
    961 	/* Configure link. */
    962 	bus_space_write_4(t, h, GEM_MII_ANAR, v);
    963 	bus_space_write_4(t, h, GEM_MII_CONTROL,
    964 	    GEM_MII_CONTROL_AUTONEG | GEM_MII_CONTROL_RAN);
    965 	bus_space_write_4(t, h, GEM_MII_CONFIG, GEM_MII_CONFIG_ENABLE);
    966 	gem_bitwait(sc, h, GEM_MII_STATUS, 0, GEM_MII_STATUS_ANEG_CPT);
    967 
    968 	/* Start the 10 second timer */
    969 	callout_reset(&sc->sc_tick_ch, hz * 10, gem_tick, sc);
    970 }
    971 
    972 /*
    973  * Stop PCS
    974  */
    975 void
    976 gem_pcs_stop(struct gem_softc *sc, int disable)
    977 {
    978 	bus_space_tag_t t = sc->sc_bustag;
    979 	bus_space_handle_t h = sc->sc_h1;
    980 
    981 #ifdef GEM_DEBUG
    982 	aprint_debug("%s: gem_pcs_stop()\n", sc->sc_dev.dv_xname);
    983 #endif
    984 
    985 	/* Tell link partner that we're going away */
    986 	bus_space_write_4(t, h, GEM_MII_ANAR, GEM_MII_ANEG_RF);
    987 
    988 	/*
    989 	 * Disable PCS MII.  The documentation suggests that setting
    990 	 * GEM_MII_CONFIG_ENABLE to zero and then restarting auto-
    991 	 * negotiation will shut down the link.  However, it appears
    992 	 * that we also need to unset the datapath mode.
    993 	 */
    994 	bus_space_write_4(t, h, GEM_MII_CONFIG, 0);
    995 	bus_space_write_4(t, h, GEM_MII_CONTROL,
    996 	    GEM_MII_CONTROL_AUTONEG | GEM_MII_CONTROL_RAN);
    997 	bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE, GEM_MII_DATAPATH_MII);
    998 	bus_space_write_4(t, h, GEM_MII_CONFIG, 0);
    999 
   1000 	if (disable) {
   1001 		if (sc->sc_flags & GEM_SERDES)
   1002 			bus_space_write_4(t, h, GEM_MII_SLINK_CONTROL,
   1003 				GEM_MII_SLINK_POWER_OFF);
   1004 		else
   1005 			bus_space_write_4(t, h, GEM_MII_SLINK_CONTROL,
   1006 			    GEM_MII_SLINK_LOOPBACK | GEM_MII_SLINK_POWER_OFF);
   1007 	}
   1008 
   1009 	sc->sc_flags &= ~GEM_LINK;
   1010 	sc->sc_mii.mii_media_active = IFM_ETHER | IFM_NONE;
   1011 	sc->sc_mii.mii_media_status = IFM_AVALID;
   1012 }
   1013 
   1014 
   1015 /*
   1016  * Initialization of interface; set up initialization block
   1017  * and transmit/receive descriptor rings.
   1018  */
   1019 int
   1020 gem_init(struct ifnet *ifp)
   1021 {
   1022 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
   1023 	bus_space_tag_t t = sc->sc_bustag;
   1024 	bus_space_handle_t h = sc->sc_h1;
   1025 	int s;
   1026 	u_int max_frame_size;
   1027 	u_int32_t v;
   1028 
   1029 	s = splnet();
   1030 
   1031 	DPRINTF(sc, ("%s: gem_init: calling stop\n", sc->sc_dev.dv_xname));
   1032 	/*
   1033 	 * Initialization sequence. The numbered steps below correspond
   1034 	 * to the sequence outlined in section 6.3.5.1 in the Ethernet
   1035 	 * Channel Engine manual (part of the PCIO manual).
   1036 	 * See also the STP2002-STQ document from Sun Microsystems.
   1037 	 */
   1038 
   1039 	/* step 1 & 2. Reset the Ethernet Channel */
   1040 	gem_stop(ifp, 0);
   1041 	gem_reset(sc);
   1042 	DPRINTF(sc, ("%s: gem_init: restarting\n", sc->sc_dev.dv_xname));
   1043 
   1044 	/* Re-initialize the MIF */
   1045 	gem_mifinit(sc);
   1046 
   1047 	/* Set up correct datapath for non-SERDES/Serialink */
   1048 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0 &&
   1049 	    sc->sc_variant != GEM_SUN_ERI)
   1050 		bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
   1051 		    GEM_MII_DATAPATH_MII);
   1052 
   1053 	/* Call MI reset function if any */
   1054 	if (sc->sc_hwreset)
   1055 		(*sc->sc_hwreset)(sc);
   1056 
   1057 	/* step 3. Setup data structures in host memory */
   1058 	if (gem_meminit(sc) != 0)
   1059 		return 1;
   1060 
   1061 	/* step 4. TX MAC registers & counters */
   1062 	gem_init_regs(sc);
   1063 	max_frame_size = max(sc->sc_ethercom.ec_if.if_mtu, ETHERMTU);
   1064 	max_frame_size += ETHER_HDR_LEN + ETHER_CRC_LEN;
   1065 	if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
   1066 		max_frame_size += ETHER_VLAN_ENCAP_LEN;
   1067 	bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
   1068 	    max_frame_size|/* burst size */(0x2000<<16));
   1069 
   1070 	/* step 5. RX MAC registers & counters */
   1071 	gem_setladrf(sc);
   1072 
   1073 	/* step 6 & 7. Program Descriptor Ring Base Addresses */
   1074 	/* NOTE: we use only 32-bit DMA addresses here. */
   1075 	bus_space_write_4(t, h, GEM_TX_RING_PTR_HI, 0);
   1076 	bus_space_write_4(t, h, GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0));
   1077 
   1078 	bus_space_write_4(t, h, GEM_RX_RING_PTR_HI, 0);
   1079 	bus_space_write_4(t, h, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0));
   1080 
   1081 	/* step 8. Global Configuration & Interrupt Mask */
   1082 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) != 0)
   1083 		v = GEM_INTR_PCS;
   1084 	else
   1085 		v = GEM_INTR_MIF;
   1086 	bus_space_write_4(t, h, GEM_INTMASK,
   1087 		      ~(GEM_INTR_TX_INTME |
   1088 			GEM_INTR_TX_EMPTY |
   1089 			GEM_INTR_TX_MAC |
   1090 			GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF|
   1091 			GEM_INTR_RX_TAG_ERR | GEM_INTR_MAC_CONTROL|
   1092 			GEM_INTR_BERR | v));
   1093 	bus_space_write_4(t, h, GEM_MAC_RX_MASK,
   1094 			GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT);
   1095 	bus_space_write_4(t, h, GEM_MAC_TX_MASK, 0xffff); /* XXX */
   1096 	bus_space_write_4(t, h, GEM_MAC_CONTROL_MASK,
   1097 	    GEM_MAC_PAUSED | GEM_MAC_PAUSE | GEM_MAC_RESUME);
   1098 
   1099 	/* step 9. ETX Configuration: use mostly default values */
   1100 
   1101 	/* Enable TX DMA */
   1102 	v = gem_ringsize(GEM_NTXDESC /*XXX*/);
   1103 	bus_space_write_4(t, h, GEM_TX_CONFIG,
   1104 		v|GEM_TX_CONFIG_TXDMA_EN|
   1105 		((0x4FF<<10)&GEM_TX_CONFIG_TXFIFO_TH));
   1106 	bus_space_write_4(t, h, GEM_TX_KICK, sc->sc_txnext);
   1107 
   1108 	/* step 10. ERX Configuration */
   1109 	gem_rx_common(sc);
   1110 
   1111 	/* step 11. Configure Media */
   1112 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0)
   1113 		mii_mediachg(&sc->sc_mii);
   1114 
   1115 	/* step 12. RX_MAC Configuration Register */
   1116 	v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
   1117 	v |= GEM_MAC_RX_ENABLE | GEM_MAC_RX_STRIP_CRC;
   1118 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
   1119 
   1120 	/* step 14. Issue Transmit Pending command */
   1121 
   1122 	/* Call MI initialization function if any */
   1123 	if (sc->sc_hwinit)
   1124 		(*sc->sc_hwinit)(sc);
   1125 
   1126 
   1127 	/* step 15.  Give the reciever a swift kick */
   1128 	bus_space_write_4(t, h, GEM_RX_KICK, GEM_NRXDESC-4);
   1129 
   1130 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) != 0)
   1131 		/* Configure PCS */
   1132 		gem_pcs_start(sc);
   1133 	else
   1134 		/* Start the one second timer. */
   1135 		callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
   1136 
   1137 	sc->sc_flags &= ~GEM_LINK;
   1138 	ifp->if_flags |= IFF_RUNNING;
   1139 	ifp->if_flags &= ~IFF_OACTIVE;
   1140 	ifp->if_timer = 0;
   1141 	sc->sc_if_flags = ifp->if_flags;
   1142 
   1143 	splx(s);
   1144 
   1145 	return (0);
   1146 }
   1147 
   1148 void
   1149 gem_init_regs(struct gem_softc *sc)
   1150 {
   1151 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1152 	bus_space_tag_t t = sc->sc_bustag;
   1153 	bus_space_handle_t h = sc->sc_h1;
   1154 	const u_char *laddr = CLLADDR(ifp->if_sadl);
   1155 	u_int32_t v;
   1156 
   1157 	/* These regs are not cleared on reset */
   1158 	if (!sc->sc_inited) {
   1159 
   1160 		/* Load recommended values */
   1161 		bus_space_write_4(t, h, GEM_MAC_IPG0, 0x00);
   1162 		bus_space_write_4(t, h, GEM_MAC_IPG1, 0x08);
   1163 		bus_space_write_4(t, h, GEM_MAC_IPG2, 0x04);
   1164 
   1165 		bus_space_write_4(t, h, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN);
   1166 		/* Max frame and max burst size */
   1167 		bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
   1168 		    ETHER_MAX_LEN | (0x2000<<16));
   1169 
   1170 		bus_space_write_4(t, h, GEM_MAC_PREAMBLE_LEN, 0x07);
   1171 		bus_space_write_4(t, h, GEM_MAC_JAM_SIZE, 0x04);
   1172 		bus_space_write_4(t, h, GEM_MAC_ATTEMPT_LIMIT, 0x10);
   1173 		bus_space_write_4(t, h, GEM_MAC_CONTROL_TYPE, 0x8088);
   1174 		bus_space_write_4(t, h, GEM_MAC_RANDOM_SEED,
   1175 		    ((laddr[5]<<8)|laddr[4])&0x3ff);
   1176 
   1177 		/* Secondary MAC addr set to 0:0:0:0:0:0 */
   1178 		bus_space_write_4(t, h, GEM_MAC_ADDR3, 0);
   1179 		bus_space_write_4(t, h, GEM_MAC_ADDR4, 0);
   1180 		bus_space_write_4(t, h, GEM_MAC_ADDR5, 0);
   1181 
   1182 		/* MAC control addr set to 01:80:c2:00:00:01 */
   1183 		bus_space_write_4(t, h, GEM_MAC_ADDR6, 0x0001);
   1184 		bus_space_write_4(t, h, GEM_MAC_ADDR7, 0xc200);
   1185 		bus_space_write_4(t, h, GEM_MAC_ADDR8, 0x0180);
   1186 
   1187 		/* MAC filter addr set to 0:0:0:0:0:0 */
   1188 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER0, 0);
   1189 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER1, 0);
   1190 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER2, 0);
   1191 
   1192 		bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK1_2, 0);
   1193 		bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK0, 0);
   1194 
   1195 		sc->sc_inited = 1;
   1196 	}
   1197 
   1198 	/* Counters need to be zeroed */
   1199 	bus_space_write_4(t, h, GEM_MAC_NORM_COLL_CNT, 0);
   1200 	bus_space_write_4(t, h, GEM_MAC_FIRST_COLL_CNT, 0);
   1201 	bus_space_write_4(t, h, GEM_MAC_EXCESS_COLL_CNT, 0);
   1202 	bus_space_write_4(t, h, GEM_MAC_LATE_COLL_CNT, 0);
   1203 	bus_space_write_4(t, h, GEM_MAC_DEFER_TMR_CNT, 0);
   1204 	bus_space_write_4(t, h, GEM_MAC_PEAK_ATTEMPTS, 0);
   1205 	bus_space_write_4(t, h, GEM_MAC_RX_FRAME_COUNT, 0);
   1206 	bus_space_write_4(t, h, GEM_MAC_RX_LEN_ERR_CNT, 0);
   1207 	bus_space_write_4(t, h, GEM_MAC_RX_ALIGN_ERR, 0);
   1208 	bus_space_write_4(t, h, GEM_MAC_RX_CRC_ERR_CNT, 0);
   1209 	bus_space_write_4(t, h, GEM_MAC_RX_CODE_VIOL, 0);
   1210 
   1211 	/* Set XOFF PAUSE time. */
   1212 	bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0);
   1213 
   1214 	/*
   1215 	 * Set the internal arbitration to "infinite" bursts of the
   1216 	 * maximum length of 31 * 64 bytes so DMA transfers aren't
   1217 	 * split up in cache line size chunks. This greatly improves
   1218 	 * especially RX performance.
   1219 	 * Enable silicon bug workarounds for the Apple variants.
   1220 	 */
   1221 	bus_space_write_4(t, h, GEM_CONFIG,
   1222 	    GEM_CONFIG_TXDMA_LIMIT | GEM_CONFIG_RXDMA_LIMIT |
   1223 	    GEM_CONFIG_BURST_INF | (GEM_IS_APPLE(sc) ?
   1224 	    GEM_CONFIG_RONPAULBIT | GEM_CONFIG_BUG2FIX : 0));
   1225 
   1226 	/*
   1227 	 * Set the station address.
   1228 	 */
   1229 	bus_space_write_4(t, h, GEM_MAC_ADDR0, (laddr[4]<<8)|laddr[5]);
   1230 	bus_space_write_4(t, h, GEM_MAC_ADDR1, (laddr[2]<<8)|laddr[3]);
   1231 	bus_space_write_4(t, h, GEM_MAC_ADDR2, (laddr[0]<<8)|laddr[1]);
   1232 
   1233 	/*
   1234 	 * Enable MII outputs.  Enable GMII if there is a gigabit PHY.
   1235 	 */
   1236 	v = GEM_MAC_XIF_TX_MII_ENA;
   1237 	if (sc->sc_flags & GEM_GIGABIT)
   1238 		v |= GEM_MAC_XIF_GMII_MODE;
   1239 	bus_space_write_4(t, h, GEM_MAC_XIF_CONFIG, v);
   1240 }
   1241 
   1242 #ifdef GEM_DEBUG
   1243 static void
   1244 gem_txsoft_print(const struct gem_softc *sc, int firstdesc, int lastdesc)
   1245 {
   1246 	int i;
   1247 
   1248 	for (i = firstdesc;; i = GEM_NEXTTX(i)) {
   1249 		printf("descriptor %d:\t", i);
   1250 		printf("gd_flags:   0x%016" PRIx64 "\t",
   1251 			GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_flags));
   1252 		printf("gd_addr: 0x%016" PRIx64 "\n",
   1253 			GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_addr));
   1254 		if (i == lastdesc)
   1255 			break;
   1256 	}
   1257 }
   1258 #endif
   1259 
   1260 static void
   1261 gem_start(ifp)
   1262 	struct ifnet *ifp;
   1263 {
   1264 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
   1265 	struct mbuf *m0, *m;
   1266 	struct gem_txsoft *txs;
   1267 	bus_dmamap_t dmamap;
   1268 	int error, firsttx, nexttx = -1, lasttx = -1, ofree, seg;
   1269 	uint64_t flags = 0;
   1270 
   1271 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1272 		return;
   1273 
   1274 	/*
   1275 	 * Remember the previous number of free descriptors and
   1276 	 * the first descriptor we'll use.
   1277 	 */
   1278 	ofree = sc->sc_txfree;
   1279 	firsttx = sc->sc_txnext;
   1280 
   1281 	DPRINTF(sc, ("%s: gem_start: txfree %d, txnext %d\n",
   1282 	    sc->sc_dev.dv_xname, ofree, firsttx));
   1283 
   1284 	/*
   1285 	 * Loop through the send queue, setting up transmit descriptors
   1286 	 * until we drain the queue, or use up all available transmit
   1287 	 * descriptors.
   1288 	 */
   1289 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
   1290 	    sc->sc_txfree != 0) {
   1291 		/*
   1292 		 * Grab a packet off the queue.
   1293 		 */
   1294 		IFQ_POLL(&ifp->if_snd, m0);
   1295 		if (m0 == NULL)
   1296 			break;
   1297 		m = NULL;
   1298 
   1299 		dmamap = txs->txs_dmamap;
   1300 
   1301 		/*
   1302 		 * Load the DMA map.  If this fails, the packet either
   1303 		 * didn't fit in the alloted number of segments, or we were
   1304 		 * short on resources.  In this case, we'll copy and try
   1305 		 * again.
   1306 		 */
   1307 		if (bus_dmamap_load_mbuf(sc->sc_dmatag, dmamap, m0,
   1308 		      BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0 ||
   1309 		      (m0->m_pkthdr.len < ETHER_MIN_TX &&
   1310 		       dmamap->dm_nsegs == GEM_NTXSEGS)) {
   1311 			if (m0->m_pkthdr.len > MCLBYTES) {
   1312 				printf("%s: unable to allocate jumbo Tx "
   1313 				    "cluster\n", sc->sc_dev.dv_xname);
   1314 				IFQ_DEQUEUE(&ifp->if_snd, m0);
   1315 				m_freem(m0);
   1316 				continue;
   1317 			}
   1318 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   1319 			if (m == NULL) {
   1320 				printf("%s: unable to allocate Tx mbuf\n",
   1321 				    sc->sc_dev.dv_xname);
   1322 				break;
   1323 			}
   1324 			MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
   1325 			if (m0->m_pkthdr.len > MHLEN) {
   1326 				MCLGET(m, M_DONTWAIT);
   1327 				if ((m->m_flags & M_EXT) == 0) {
   1328 					printf("%s: unable to allocate Tx "
   1329 					    "cluster\n", sc->sc_dev.dv_xname);
   1330 					m_freem(m);
   1331 					break;
   1332 				}
   1333 			}
   1334 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
   1335 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
   1336 			error = bus_dmamap_load_mbuf(sc->sc_dmatag, dmamap,
   1337 			    m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
   1338 			if (error) {
   1339 				printf("%s: unable to load Tx buffer, "
   1340 				    "error = %d\n", sc->sc_dev.dv_xname, error);
   1341 				break;
   1342 			}
   1343 		}
   1344 
   1345 		/*
   1346 		 * Ensure we have enough descriptors free to describe
   1347 		 * the packet.
   1348 		 */
   1349 		if (dmamap->dm_nsegs > ((m0->m_pkthdr.len < ETHER_MIN_TX) ?
   1350 		     (sc->sc_txfree - 1) : sc->sc_txfree)) {
   1351 			/*
   1352 			 * Not enough free descriptors to transmit this
   1353 			 * packet.  We haven't committed to anything yet,
   1354 			 * so just unload the DMA map, put the packet
   1355 			 * back on the queue, and punt.  Notify the upper
   1356 			 * layer that there are no more slots left.
   1357 			 *
   1358 			 * XXX We could allocate an mbuf and copy, but
   1359 			 * XXX it is worth it?
   1360 			 */
   1361 			ifp->if_flags |= IFF_OACTIVE;
   1362 			sc->sc_if_flags = ifp->if_flags;
   1363 			bus_dmamap_unload(sc->sc_dmatag, dmamap);
   1364 			if (m != NULL)
   1365 				m_freem(m);
   1366 			break;
   1367 		}
   1368 
   1369 		IFQ_DEQUEUE(&ifp->if_snd, m0);
   1370 		if (m != NULL) {
   1371 			m_freem(m0);
   1372 			m0 = m;
   1373 		}
   1374 
   1375 		/*
   1376 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
   1377 		 */
   1378 
   1379 		/* Sync the DMA map. */
   1380 		bus_dmamap_sync(sc->sc_dmatag, dmamap, 0, dmamap->dm_mapsize,
   1381 		    BUS_DMASYNC_PREWRITE);
   1382 
   1383 		/*
   1384 		 * Initialize the transmit descriptors.
   1385 		 */
   1386 		for (nexttx = sc->sc_txnext, seg = 0;
   1387 		     seg < dmamap->dm_nsegs;
   1388 		     seg++, nexttx = GEM_NEXTTX(nexttx)) {
   1389 
   1390 			/*
   1391 			 * If this is the first descriptor we're
   1392 			 * enqueueing, set the start of packet flag,
   1393 			 * and the checksum stuff if we want the hardware
   1394 			 * to do it.
   1395 			 */
   1396 			sc->sc_txdescs[nexttx].gd_addr =
   1397 			    GEM_DMA_WRITE(sc, dmamap->dm_segs[seg].ds_addr);
   1398 			flags = dmamap->dm_segs[seg].ds_len & GEM_TD_BUFSIZE;
   1399 			if (nexttx == firsttx) {
   1400 				flags |= GEM_TD_START_OF_PACKET;
   1401 				if (++sc->sc_txwin > GEM_NTXSEGS * 2 / 3) {
   1402 					sc->sc_txwin = 0;
   1403 					flags |= GEM_TD_INTERRUPT_ME;
   1404 				}
   1405 
   1406 #ifdef INET
   1407 				/* h/w checksum */
   1408 				if (ifp->if_csum_flags_tx & M_CSUM_TCPv4 &&
   1409 				    m0->m_pkthdr.csum_flags & M_CSUM_TCPv4) {
   1410 					struct ether_header *eh;
   1411 					uint16_t offset, start;
   1412 
   1413 					eh = mtod(m0, struct ether_header *);
   1414 					switch (ntohs(eh->ether_type)) {
   1415 					case ETHERTYPE_IP:
   1416 						start = ETHER_HDR_LEN;
   1417 						break;
   1418 					case ETHERTYPE_VLAN:
   1419 						start = ETHER_HDR_LEN +
   1420 							ETHER_VLAN_ENCAP_LEN;
   1421 						break;
   1422 					default:
   1423 						/* unsupported, drop it */
   1424 						m_free(m0);
   1425 						continue;
   1426 					}
   1427 					start += M_CSUM_DATA_IPv4_IPHL(m0->m_pkthdr.csum_data);
   1428 					offset = M_CSUM_DATA_IPv4_OFFSET(m0->m_pkthdr.csum_data) + start;
   1429 					flags |= (start <<
   1430 						  GEM_TD_CXSUM_STARTSHFT) |
   1431 						 (offset <<
   1432 						  GEM_TD_CXSUM_STUFFSHFT) |
   1433 						 GEM_TD_CXSUM_ENABLE;
   1434 				}
   1435 #endif
   1436 			}
   1437 			if (seg == dmamap->dm_nsegs - 1) {
   1438 				flags |= GEM_TD_END_OF_PACKET;
   1439 			} else {
   1440 				/* last flag set outside of loop */
   1441 				sc->sc_txdescs[nexttx].gd_flags =
   1442 					GEM_DMA_WRITE(sc, flags);
   1443 			}
   1444 			lasttx = nexttx;
   1445 		}
   1446 		if (m0->m_pkthdr.len < ETHER_MIN_TX) {
   1447 			/* add padding buffer at end of chain */
   1448 			flags &= ~GEM_TD_END_OF_PACKET;
   1449 			sc->sc_txdescs[lasttx].gd_flags =
   1450 			    GEM_DMA_WRITE(sc, flags);
   1451 
   1452 			sc->sc_txdescs[nexttx].gd_addr =
   1453 			    GEM_DMA_WRITE(sc,
   1454 			    sc->sc_nulldmamap->dm_segs[0].ds_addr);
   1455 			flags = ((ETHER_MIN_TX - m0->m_pkthdr.len) &
   1456 			    GEM_TD_BUFSIZE) | GEM_TD_END_OF_PACKET;
   1457 			lasttx = nexttx;
   1458 			nexttx = GEM_NEXTTX(nexttx);
   1459 			seg++;
   1460 		}
   1461 		sc->sc_txdescs[lasttx].gd_flags = GEM_DMA_WRITE(sc, flags);
   1462 
   1463 		KASSERT(lasttx != -1);
   1464 
   1465 		/*
   1466 		 * Store a pointer to the packet so we can free it later,
   1467 		 * and remember what txdirty will be once the packet is
   1468 		 * done.
   1469 		 */
   1470 		txs->txs_mbuf = m0;
   1471 		txs->txs_firstdesc = sc->sc_txnext;
   1472 		txs->txs_lastdesc = lasttx;
   1473 		txs->txs_ndescs = seg;
   1474 
   1475 #ifdef GEM_DEBUG
   1476 		if (ifp->if_flags & IFF_DEBUG) {
   1477 			printf("     gem_start %p transmit chain:\n", txs);
   1478 			gem_txsoft_print(sc, txs->txs_firstdesc,
   1479 			    txs->txs_lastdesc);
   1480 		}
   1481 #endif
   1482 
   1483 		/* Sync the descriptors we're using. */
   1484 		GEM_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_ndescs,
   1485 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1486 
   1487 		/* Advance the tx pointer. */
   1488 		sc->sc_txfree -= txs->txs_ndescs;
   1489 		sc->sc_txnext = nexttx;
   1490 
   1491 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
   1492 		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
   1493 
   1494 #if NBPFILTER > 0
   1495 		/*
   1496 		 * Pass the packet to any BPF listeners.
   1497 		 */
   1498 		if (ifp->if_bpf)
   1499 			bpf_mtap(ifp->if_bpf, m0);
   1500 #endif /* NBPFILTER > 0 */
   1501 	}
   1502 
   1503 	if (txs == NULL || sc->sc_txfree == 0) {
   1504 		/* No more slots left; notify upper layer. */
   1505 		ifp->if_flags |= IFF_OACTIVE;
   1506 		sc->sc_if_flags = ifp->if_flags;
   1507 	}
   1508 
   1509 	if (sc->sc_txfree != ofree) {
   1510 		DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
   1511 		    sc->sc_dev.dv_xname, lasttx, firsttx));
   1512 		/*
   1513 		 * The entire packet chain is set up.
   1514 		 * Kick the transmitter.
   1515 		 */
   1516 		DPRINTF(sc, ("%s: gem_start: kicking tx %d\n",
   1517 			sc->sc_dev.dv_xname, nexttx));
   1518 		bus_space_write_4(sc->sc_bustag, sc->sc_h1, GEM_TX_KICK,
   1519 			sc->sc_txnext);
   1520 
   1521 		/* Set a watchdog timer in case the chip flakes out. */
   1522 		ifp->if_timer = 5;
   1523 		DPRINTF(sc, ("%s: gem_start: watchdog %d\n",
   1524 			sc->sc_dev.dv_xname, ifp->if_timer));
   1525 	}
   1526 }
   1527 
   1528 /*
   1529  * Transmit interrupt.
   1530  */
   1531 int
   1532 gem_tint(sc)
   1533 	struct gem_softc *sc;
   1534 {
   1535 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1536 	bus_space_tag_t t = sc->sc_bustag;
   1537 	bus_space_handle_t mac = sc->sc_h1;
   1538 	struct gem_txsoft *txs;
   1539 	int txlast;
   1540 	int progress = 0;
   1541 
   1542 	DPRINTF(sc, ("%s: gem_tint\n", sc->sc_dev.dv_xname));
   1543 
   1544 	/*
   1545 	 * Unload collision counters
   1546 	 */
   1547 	ifp->if_collisions +=
   1548 		bus_space_read_4(t, mac, GEM_MAC_NORM_COLL_CNT) +
   1549 		bus_space_read_4(t, mac, GEM_MAC_FIRST_COLL_CNT) +
   1550 		bus_space_read_4(t, mac, GEM_MAC_EXCESS_COLL_CNT) +
   1551 		bus_space_read_4(t, mac, GEM_MAC_LATE_COLL_CNT);
   1552 
   1553 	/*
   1554 	 * then clear the hardware counters.
   1555 	 */
   1556 	bus_space_write_4(t, mac, GEM_MAC_NORM_COLL_CNT, 0);
   1557 	bus_space_write_4(t, mac, GEM_MAC_FIRST_COLL_CNT, 0);
   1558 	bus_space_write_4(t, mac, GEM_MAC_EXCESS_COLL_CNT, 0);
   1559 	bus_space_write_4(t, mac, GEM_MAC_LATE_COLL_CNT, 0);
   1560 
   1561 	/*
   1562 	 * Go through our Tx list and free mbufs for those
   1563 	 * frames that have been transmitted.
   1564 	 */
   1565 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1566 		/*
   1567 		 * In theory, we could harvest some descriptors before
   1568 		 * the ring is empty, but that's a bit complicated.
   1569 		 *
   1570 		 * GEM_TX_COMPLETION points to the last descriptor
   1571 		 * processed +1.
   1572 		 *
   1573 		 * Let's assume that the NIC writes back to the Tx
   1574 		 * descriptors before it updates the completion
   1575 		 * register.  If the NIC has posted writes to the
   1576 		 * Tx descriptors, PCI ordering requires that the
   1577 		 * posted writes flush to RAM before the register-read
   1578 		 * finishes.  So let's read the completion register,
   1579 		 * before syncing the descriptors, so that we
   1580 		 * examine Tx descriptors that are at least as
   1581 		 * current as the completion register.
   1582 		 */
   1583 		txlast = bus_space_read_4(t, mac, GEM_TX_COMPLETION);
   1584 		DPRINTF(sc,
   1585 			("gem_tint: txs->txs_lastdesc = %d, txlast = %d\n",
   1586 				txs->txs_lastdesc, txlast));
   1587 		if (txs->txs_firstdesc <= txs->txs_lastdesc) {
   1588 			if (txlast >= txs->txs_firstdesc &&
   1589 			    txlast <= txs->txs_lastdesc)
   1590 				break;
   1591 		} else if (txlast >= txs->txs_firstdesc ||
   1592 			   txlast <= txs->txs_lastdesc)
   1593 			break;
   1594 
   1595 		GEM_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_ndescs,
   1596 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1597 
   1598 #ifdef GEM_DEBUG	/* XXX DMA synchronization? */
   1599 		if (ifp->if_flags & IFF_DEBUG) {
   1600 			printf("    txsoft %p transmit chain:\n", txs);
   1601 			gem_txsoft_print(sc, txs->txs_firstdesc,
   1602 			    txs->txs_lastdesc);
   1603 		}
   1604 #endif
   1605 
   1606 
   1607 		DPRINTF(sc, ("gem_tint: releasing a desc\n"));
   1608 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
   1609 
   1610 		sc->sc_txfree += txs->txs_ndescs;
   1611 
   1612 		bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap,
   1613 		    0, txs->txs_dmamap->dm_mapsize,
   1614 		    BUS_DMASYNC_POSTWRITE);
   1615 		bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
   1616 		if (txs->txs_mbuf != NULL) {
   1617 			m_freem(txs->txs_mbuf);
   1618 			txs->txs_mbuf = NULL;
   1619 		}
   1620 
   1621 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1622 
   1623 		ifp->if_opackets++;
   1624 		progress = 1;
   1625 	}
   1626 
   1627 #if 0
   1628 	DPRINTF(sc, ("gem_tint: GEM_TX_STATE_MACHINE %x "
   1629 		"GEM_TX_DATA_PTR %" PRIx64 "GEM_TX_COMPLETION %" PRIx32 "\n",
   1630 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_TX_STATE_MACHINE),
   1631 		((uint64_t)bus_space_read_4(sc->sc_bustag, sc->sc_h1,
   1632 			GEM_TX_DATA_PTR_HI) << 32) |
   1633 			     bus_space_read_4(sc->sc_bustag, sc->sc_h1,
   1634 			GEM_TX_DATA_PTR_LO),
   1635 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_TX_COMPLETION)));
   1636 #endif
   1637 
   1638 	if (progress) {
   1639 		if (sc->sc_txfree == GEM_NTXDESC - 1)
   1640 			sc->sc_txwin = 0;
   1641 
   1642 		/* Freed some descriptors, so reset IFF_OACTIVE and restart. */
   1643 		ifp->if_flags &= ~IFF_OACTIVE;
   1644 		sc->sc_if_flags = ifp->if_flags;
   1645 		ifp->if_timer = SIMPLEQ_EMPTY(&sc->sc_txdirtyq) ? 0 : 5;
   1646 		gem_start(ifp);
   1647 	}
   1648 	DPRINTF(sc, ("%s: gem_tint: watchdog %d\n",
   1649 		sc->sc_dev.dv_xname, ifp->if_timer));
   1650 
   1651 	return (1);
   1652 }
   1653 
   1654 /*
   1655  * Receive interrupt.
   1656  */
   1657 int
   1658 gem_rint(sc)
   1659 	struct gem_softc *sc;
   1660 {
   1661 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1662 	bus_space_tag_t t = sc->sc_bustag;
   1663 	bus_space_handle_t h = sc->sc_h1;
   1664 	struct gem_rxsoft *rxs;
   1665 	struct mbuf *m;
   1666 	u_int64_t rxstat;
   1667 	u_int32_t rxcomp;
   1668 	int i, len, progress = 0;
   1669 
   1670 	DPRINTF(sc, ("%s: gem_rint\n", sc->sc_dev.dv_xname));
   1671 
   1672 	/*
   1673 	 * Ignore spurious interrupt that sometimes occurs before
   1674 	 * we are set up when we network boot.
   1675 	 */
   1676 	if (!sc->sc_meminited)
   1677 		return 1;
   1678 
   1679 	/*
   1680 	 * Read the completion register once.  This limits
   1681 	 * how long the following loop can execute.
   1682 	 */
   1683 	rxcomp = bus_space_read_4(t, h, GEM_RX_COMPLETION);
   1684 
   1685 	/*
   1686 	 * XXX Read the lastrx only once at the top for speed.
   1687 	 */
   1688 	DPRINTF(sc, ("gem_rint: sc->rxptr %d, complete %d\n",
   1689 		sc->sc_rxptr, rxcomp));
   1690 
   1691 	/*
   1692 	 * Go into the loop at least once.
   1693 	 */
   1694 	for (i = sc->sc_rxptr; i == sc->sc_rxptr || i != rxcomp;
   1695 	     i = GEM_NEXTRX(i)) {
   1696 		rxs = &sc->sc_rxsoft[i];
   1697 
   1698 		GEM_CDRXSYNC(sc, i,
   1699 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1700 
   1701 		rxstat = GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags);
   1702 
   1703 		if (rxstat & GEM_RD_OWN) {
   1704 			GEM_CDRXSYNC(sc, i, BUS_DMASYNC_PREREAD);
   1705 			/*
   1706 			 * We have processed all of the receive buffers.
   1707 			 */
   1708 			break;
   1709 		}
   1710 
   1711 		progress++;
   1712 		ifp->if_ipackets++;
   1713 
   1714 		if (rxstat & GEM_RD_BAD_CRC) {
   1715 			ifp->if_ierrors++;
   1716 			printf("%s: receive error: CRC error\n",
   1717 				sc->sc_dev.dv_xname);
   1718 			GEM_INIT_RXDESC(sc, i);
   1719 			continue;
   1720 		}
   1721 
   1722 		bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
   1723 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1724 #ifdef GEM_DEBUG
   1725 		if (ifp->if_flags & IFF_DEBUG) {
   1726 			printf("    rxsoft %p descriptor %d: ", rxs, i);
   1727 			printf("gd_flags: 0x%016llx\t", (long long)
   1728 				GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags));
   1729 			printf("gd_addr: 0x%016llx\n", (long long)
   1730 				GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_addr));
   1731 		}
   1732 #endif
   1733 
   1734 		/* No errors; receive the packet. */
   1735 		len = GEM_RD_BUFLEN(rxstat);
   1736 
   1737 		/*
   1738 		 * Allocate a new mbuf cluster.  If that fails, we are
   1739 		 * out of memory, and must drop the packet and recycle
   1740 		 * the buffer that's already attached to this descriptor.
   1741 		 */
   1742 		m = rxs->rxs_mbuf;
   1743 		if (gem_add_rxbuf(sc, i) != 0) {
   1744 			GEM_COUNTER_INCR(sc, sc_ev_rxnobuf);
   1745 			ifp->if_ierrors++;
   1746 			GEM_INIT_RXDESC(sc, i);
   1747 			bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
   1748 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1749 			continue;
   1750 		}
   1751 		m->m_data += 2; /* We're already off by two */
   1752 
   1753 		m->m_pkthdr.rcvif = ifp;
   1754 		m->m_pkthdr.len = m->m_len = len;
   1755 
   1756 #if NBPFILTER > 0
   1757 		/*
   1758 		 * Pass this up to any BPF listeners, but only
   1759 		 * pass it up the stack if it's for us.
   1760 		 */
   1761 		if (ifp->if_bpf)
   1762 			bpf_mtap(ifp->if_bpf, m);
   1763 #endif /* NBPFILTER > 0 */
   1764 
   1765 #ifdef INET
   1766 		/* hardware checksum */
   1767 		if (ifp->if_csum_flags_rx & M_CSUM_TCPv4) {
   1768 			struct ether_header *eh;
   1769 			struct ip *ip;
   1770 			int32_t hlen, pktlen;
   1771 
   1772 			if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) {
   1773 				pktlen = m->m_pkthdr.len - ETHER_HDR_LEN -
   1774 					 ETHER_VLAN_ENCAP_LEN;
   1775 				eh = (struct ether_header *) mtod(m, void *) +
   1776 					ETHER_VLAN_ENCAP_LEN;
   1777 			} else {
   1778 				pktlen = m->m_pkthdr.len - ETHER_HDR_LEN;
   1779 				eh = mtod(m, struct ether_header *);
   1780 			}
   1781 			if (ntohs(eh->ether_type) != ETHERTYPE_IP)
   1782 				goto swcsum;
   1783 			ip = (struct ip *) ((char *)eh + ETHER_HDR_LEN);
   1784 
   1785 			/* IPv4 only */
   1786 			if (ip->ip_v != IPVERSION)
   1787 				goto swcsum;
   1788 
   1789 			hlen = ip->ip_hl << 2;
   1790 			if (hlen < sizeof(struct ip))
   1791 				goto swcsum;
   1792 
   1793 			/*
   1794 			 * bail if too short, has random trailing garbage,
   1795 			 * truncated, fragment, or has ethernet pad.
   1796 			 */
   1797 			if ((ntohs(ip->ip_len) < hlen) ||
   1798 			    (ntohs(ip->ip_len) != pktlen) ||
   1799 			    (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)))
   1800 				goto swcsum;
   1801 
   1802 			switch (ip->ip_p) {
   1803 			case IPPROTO_TCP:
   1804 				if (! (ifp->if_csum_flags_rx & M_CSUM_TCPv4))
   1805 					goto swcsum;
   1806 				if (pktlen < (hlen + sizeof(struct tcphdr)))
   1807 					goto swcsum;
   1808 				m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
   1809 				break;
   1810 			case IPPROTO_UDP:
   1811 				/* FALLTHROUGH */
   1812 			default:
   1813 				goto swcsum;
   1814 			}
   1815 
   1816 			/* the uncomplemented sum is expected */
   1817 			m->m_pkthdr.csum_data = (~rxstat) & GEM_RD_CHECKSUM;
   1818 
   1819 			/* if the pkt had ip options, we have to deduct them */
   1820 			if (hlen > sizeof(struct ip)) {
   1821 				uint16_t *opts;
   1822 				uint32_t optsum, temp;
   1823 
   1824 				optsum = 0;
   1825 				temp = hlen - sizeof(struct ip);
   1826 				opts = (uint16_t *) ((char *) ip +
   1827 					sizeof(struct ip));
   1828 
   1829 				while (temp > 1) {
   1830 					optsum += ntohs(*opts++);
   1831 					temp -= 2;
   1832 				}
   1833 				while (optsum >> 16)
   1834 					optsum = (optsum >> 16) +
   1835 						 (optsum & 0xffff);
   1836 
   1837 				/* Deduct ip opts sum from hwsum (rfc 1624). */
   1838 				m->m_pkthdr.csum_data =
   1839 					~((~m->m_pkthdr.csum_data) - ~optsum);
   1840 
   1841 				while (m->m_pkthdr.csum_data >> 16)
   1842 					m->m_pkthdr.csum_data =
   1843 						(m->m_pkthdr.csum_data >> 16) +
   1844 						(m->m_pkthdr.csum_data &
   1845 						 0xffff);
   1846 			}
   1847 
   1848 			m->m_pkthdr.csum_flags |= M_CSUM_DATA |
   1849 						  M_CSUM_NO_PSEUDOHDR;
   1850 		} else
   1851 swcsum:
   1852 			m->m_pkthdr.csum_flags = 0;
   1853 #endif
   1854 		/* Pass it on. */
   1855 		(*ifp->if_input)(ifp, m);
   1856 	}
   1857 
   1858 	if (progress) {
   1859 		/* Update the receive pointer. */
   1860 		if (i == sc->sc_rxptr) {
   1861 			GEM_COUNTER_INCR(sc, sc_ev_rxfull);
   1862 #ifdef GEM_DEBUG
   1863 			if (ifp->if_flags & IFF_DEBUG)
   1864 				printf("%s: rint: ring wrap\n",
   1865 				    sc->sc_dev.dv_xname);
   1866 #endif
   1867 		}
   1868 		sc->sc_rxptr = i;
   1869 		bus_space_write_4(t, h, GEM_RX_KICK, GEM_PREVRX(i));
   1870 	}
   1871 #ifdef GEM_COUNTERS
   1872 	if (progress <= 4) {
   1873 		GEM_COUNTER_INCR(sc, sc_ev_rxhist[progress]);
   1874 	} else if (progress < 32) {
   1875 		if (progress < 16)
   1876 			GEM_COUNTER_INCR(sc, sc_ev_rxhist[5]);
   1877 		else
   1878 			GEM_COUNTER_INCR(sc, sc_ev_rxhist[6]);
   1879 
   1880 	} else {
   1881 		if (progress < 64)
   1882 			GEM_COUNTER_INCR(sc, sc_ev_rxhist[7]);
   1883 		else
   1884 			GEM_COUNTER_INCR(sc, sc_ev_rxhist[8]);
   1885 	}
   1886 #endif
   1887 
   1888 	DPRINTF(sc, ("gem_rint: done sc->rxptr %d, complete %d\n",
   1889 		sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION)));
   1890 
   1891 	return (1);
   1892 }
   1893 
   1894 
   1895 /*
   1896  * gem_add_rxbuf:
   1897  *
   1898  *	Add a receive buffer to the indicated descriptor.
   1899  */
   1900 int
   1901 gem_add_rxbuf(struct gem_softc *sc, int idx)
   1902 {
   1903 	struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx];
   1904 	struct mbuf *m;
   1905 	int error;
   1906 
   1907 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1908 	if (m == NULL)
   1909 		return (ENOBUFS);
   1910 
   1911 	MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
   1912 	MCLGET(m, M_DONTWAIT);
   1913 	if ((m->m_flags & M_EXT) == 0) {
   1914 		m_freem(m);
   1915 		return (ENOBUFS);
   1916 	}
   1917 
   1918 #ifdef GEM_DEBUG
   1919 /* bzero the packet to check DMA */
   1920 	memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size);
   1921 #endif
   1922 
   1923 	if (rxs->rxs_mbuf != NULL)
   1924 		bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
   1925 
   1926 	rxs->rxs_mbuf = m;
   1927 
   1928 	error = bus_dmamap_load(sc->sc_dmatag, rxs->rxs_dmamap,
   1929 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   1930 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
   1931 	if (error) {
   1932 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1933 		    sc->sc_dev.dv_xname, idx, error);
   1934 		panic("gem_add_rxbuf");	/* XXX */
   1935 	}
   1936 
   1937 	bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
   1938 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1939 
   1940 	GEM_INIT_RXDESC(sc, idx);
   1941 
   1942 	return (0);
   1943 }
   1944 
   1945 
   1946 int
   1947 gem_eint(struct gem_softc *sc, u_int status)
   1948 {
   1949 	char bits[128];
   1950 	u_int32_t v;
   1951 
   1952 	if ((status & GEM_INTR_MIF) != 0) {
   1953 		printf("%s: XXXlink status changed\n", sc->sc_dev.dv_xname);
   1954 		return (1);
   1955 	}
   1956 
   1957 	if ((status & GEM_INTR_RX_TAG_ERR) != 0) {
   1958 		gem_reset_rxdma(sc);
   1959 		return (1);
   1960 	}
   1961 
   1962 	if (status & GEM_INTR_BERR) {
   1963 		bus_space_read_4(sc->sc_bustag, sc->sc_h2, GEM_ERROR_STATUS);
   1964 		v = bus_space_read_4(sc->sc_bustag, sc->sc_h2,
   1965 		    GEM_ERROR_STATUS);
   1966 		printf("%s: bus error interrupt: 0x%02x\n",
   1967 		    sc->sc_dev.dv_xname, v);
   1968 		return (1);
   1969 	}
   1970 
   1971 	printf("%s: status=%s\n", sc->sc_dev.dv_xname,
   1972 		bitmask_snprintf(status, GEM_INTR_BITS, bits, sizeof(bits)));
   1973 	return (1);
   1974 }
   1975 
   1976 
   1977 /*
   1978  * PCS interrupts.
   1979  * We should receive these when the link status changes, but sometimes
   1980  * we don't receive them for link up.  We compensate for this in the
   1981  * gem_tick() callout.
   1982  */
   1983 int
   1984 gem_pint(struct gem_softc *sc)
   1985 {
   1986 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1987 	bus_space_tag_t t = sc->sc_bustag;
   1988 	bus_space_handle_t h = sc->sc_h1;
   1989 	u_int32_t v, v2;
   1990 
   1991 	/*
   1992 	 * Clear the PCS interrupt from GEM_STATUS.  The PCS register is
   1993 	 * latched, so we have to read it twice.  There is only one bit in
   1994 	 * use, so the value is meaningless.
   1995 	 */
   1996 	bus_space_read_4(t, h, GEM_MII_INTERRUP_STATUS);
   1997 	bus_space_read_4(t, h, GEM_MII_INTERRUP_STATUS);
   1998 
   1999 	if ((ifp->if_flags & IFF_UP) == 0)
   2000 		return 1;
   2001 
   2002 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0)
   2003 		return 1;
   2004 
   2005 	v = bus_space_read_4(t, h, GEM_MII_STATUS);
   2006 	/* If we see remote fault, our link partner is probably going away */
   2007 	if ((v & GEM_MII_STATUS_REM_FLT) != 0) {
   2008 		gem_bitwait(sc, h, GEM_MII_STATUS, GEM_MII_STATUS_REM_FLT, 0);
   2009 		v = bus_space_read_4(t, h, GEM_MII_STATUS);
   2010 	/* Otherwise, we may need to wait after auto-negotiation completes */
   2011 	} else if ((v & (GEM_MII_STATUS_LINK_STS | GEM_MII_STATUS_ANEG_CPT)) ==
   2012 	    GEM_MII_STATUS_ANEG_CPT) {
   2013 		gem_bitwait(sc, h, GEM_MII_STATUS, 0, GEM_MII_STATUS_LINK_STS);
   2014 		v = bus_space_read_4(t, h, GEM_MII_STATUS);
   2015 	}
   2016 	if ((v & GEM_MII_STATUS_LINK_STS) != 0) {
   2017 		if (sc->sc_flags & GEM_LINK) {
   2018 			return 1;
   2019 		}
   2020 		callout_stop(&sc->sc_tick_ch);
   2021 		v = bus_space_read_4(t, h, GEM_MII_ANAR);
   2022 		v2 = bus_space_read_4(t, h, GEM_MII_ANLPAR);
   2023 		sc->sc_mii.mii_media_active = IFM_ETHER | IFM_1000_SX;
   2024 		sc->sc_mii.mii_media_status = IFM_AVALID | IFM_ACTIVE;
   2025 		v &= v2;
   2026 		if (v & GEM_MII_ANEG_FUL_DUPLX) {
   2027 			sc->sc_mii.mii_media_active |= IFM_FDX;
   2028 #ifdef GEM_DEBUG
   2029 			aprint_debug("%s: link up: full duplex\n",
   2030 			    sc->sc_dev.dv_xname);
   2031 #endif
   2032 		} else if (v & GEM_MII_ANEG_HLF_DUPLX) {
   2033 			sc->sc_mii.mii_media_active |= IFM_HDX;
   2034 #ifdef GEM_DEBUG
   2035 			aprint_debug("%s: link up: half duplex\n",
   2036 			    sc->sc_dev.dv_xname);
   2037 #endif
   2038 		} else {
   2039 #ifdef GEM_DEBUG
   2040 			aprint_debug("%s: duplex mismatch\n",
   2041 			    sc->sc_dev.dv_xname);
   2042 #endif
   2043 		}
   2044 		gem_statuschange(sc);
   2045 	} else {
   2046 		if ((sc->sc_flags & GEM_LINK) == 0) {
   2047 			return 1;
   2048 		}
   2049 		sc->sc_mii.mii_media_active = IFM_ETHER | IFM_NONE;
   2050 		sc->sc_mii.mii_media_status = IFM_AVALID;
   2051 #ifdef GEM_DEBUG
   2052 			aprint_debug("%s: link down\n",
   2053 			    sc->sc_dev.dv_xname);
   2054 #endif
   2055 		gem_statuschange(sc);
   2056 
   2057 		/* Start the 10 second timer */
   2058 		callout_reset(&sc->sc_tick_ch, hz * 10, gem_tick, sc);
   2059 	}
   2060 	return 1;
   2061 }
   2062 
   2063 
   2064 
   2065 int
   2066 gem_intr(v)
   2067 	void *v;
   2068 {
   2069 	struct gem_softc *sc = (struct gem_softc *)v;
   2070 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2071 	bus_space_tag_t t = sc->sc_bustag;
   2072 	bus_space_handle_t h = sc->sc_h1;
   2073 	u_int32_t status;
   2074 	int r = 0;
   2075 #ifdef GEM_DEBUG
   2076 	char bits[128];
   2077 #endif
   2078 
   2079 	/* XXX We should probably mask out interrupts until we're done */
   2080 
   2081 	sc->sc_ev_intr.ev_count++;
   2082 
   2083 	status = bus_space_read_4(t, h, GEM_STATUS);
   2084 	DPRINTF(sc, ("%s: gem_intr: cplt 0x%x status %s\n",
   2085 		sc->sc_dev.dv_xname, (status >> 19),
   2086 		bitmask_snprintf(status, GEM_INTR_BITS, bits, sizeof(bits))));
   2087 
   2088 	if ((status & (GEM_INTR_RX_TAG_ERR | GEM_INTR_BERR)) != 0)
   2089 		r |= gem_eint(sc, status);
   2090 
   2091 	/* We don't bother with GEM_INTR_TX_DONE */
   2092 	if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0) {
   2093 		GEM_COUNTER_INCR(sc, sc_ev_txint);
   2094 		r |= gem_tint(sc);
   2095 	}
   2096 
   2097 	if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0) {
   2098 		GEM_COUNTER_INCR(sc, sc_ev_rxint);
   2099 		r |= gem_rint(sc);
   2100 	}
   2101 
   2102 	/* We should eventually do more than just print out error stats. */
   2103 	if (status & GEM_INTR_TX_MAC) {
   2104 		int txstat = bus_space_read_4(t, h, GEM_MAC_TX_STATUS);
   2105 		if (txstat & ~GEM_MAC_TX_XMIT_DONE)
   2106 			printf("%s: MAC tx fault, status %x\n",
   2107 			    sc->sc_dev.dv_xname, txstat);
   2108 		if (txstat & (GEM_MAC_TX_UNDERRUN | GEM_MAC_TX_PKT_TOO_LONG))
   2109 			gem_init(ifp);
   2110 	}
   2111 	if (status & GEM_INTR_RX_MAC) {
   2112 		int rxstat = bus_space_read_4(t, h, GEM_MAC_RX_STATUS);
   2113 		if (rxstat & ~GEM_MAC_RX_DONE)
   2114 			printf("%s: MAC rx fault, status %x\n",
   2115 			    sc->sc_dev.dv_xname, rxstat);
   2116 		/*
   2117 		 * At least with GEM_SUN_GEM and some GEM_SUN_ERI
   2118 		 * revisions GEM_MAC_RX_OVERFLOW happen often due to a
   2119 		 * silicon bug so handle them silently. Moreover, it's
   2120 		 * likely that the receiver has hung so we reset it.
   2121 		 */
   2122 		if (rxstat & GEM_MAC_RX_OVERFLOW) {
   2123 			ifp->if_ierrors++;
   2124 			gem_reset_rxdma(sc);
   2125 		} else if (rxstat & ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT))
   2126 			printf("%s: MAC rx fault, status %x\n",
   2127 			    sc->sc_dev.dv_xname, rxstat);
   2128 	}
   2129 	if (status & GEM_INTR_PCS) {
   2130 		r |= gem_pint(sc);
   2131 	}
   2132 
   2133 /* Do we need to do anything with these?
   2134 	if ((status & GEM_MAC_CONTROL_STATUS) != 0) {
   2135 		status2 = bus_read_4(sc->sc_res[0], GEM_MAC_CONTROL_STATUS);
   2136 		if ((status2 & GEM_MAC_PAUSED) != 0)
   2137 			aprintf_debug("%s: PAUSE received (%d slots)\n",
   2138 			    GEM_MAC_PAUSE_TIME(status2), sc->sc_dev.dv_xname);
   2139 		if ((status2 & GEM_MAC_PAUSE) != 0)
   2140 			aprintf_debug("%s: transited to PAUSE state\n",
   2141 			    sc->sc_dev.dv_xname);
   2142 		if ((status2 & GEM_MAC_RESUME) != 0)
   2143 			aprintf_debug("%s: transited to non-PAUSE state\n",
   2144 			    sc->sc_dev.dv_xname);
   2145 	}
   2146 	if ((status & GEM_INTR_MIF) != 0)
   2147 		aprintf_debug("%s: MIF interrupt\n", sc->sc_dev.dv_xname);
   2148 */
   2149 #if NRND > 0
   2150 	rnd_add_uint32(&sc->rnd_source, status);
   2151 #endif
   2152 	return (r);
   2153 }
   2154 
   2155 
   2156 void
   2157 gem_watchdog(ifp)
   2158 	struct ifnet *ifp;
   2159 {
   2160 	struct gem_softc *sc = ifp->if_softc;
   2161 
   2162 	DPRINTF(sc, ("gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x "
   2163 		"GEM_MAC_RX_CONFIG %x\n",
   2164 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_RX_CONFIG),
   2165 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_MAC_RX_STATUS),
   2166 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_MAC_RX_CONFIG)));
   2167 
   2168 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   2169 	++ifp->if_oerrors;
   2170 
   2171 	/* Try to get more packets going. */
   2172 	gem_start(ifp);
   2173 }
   2174 
   2175 /*
   2176  * Initialize the MII Management Interface
   2177  */
   2178 void
   2179 gem_mifinit(sc)
   2180 	struct gem_softc *sc;
   2181 {
   2182 	bus_space_tag_t t = sc->sc_bustag;
   2183 	bus_space_handle_t mif = sc->sc_h1;
   2184 
   2185 	/* Configure the MIF in frame mode */
   2186 	sc->sc_mif_config = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
   2187 	sc->sc_mif_config &= ~GEM_MIF_CONFIG_BB_ENA;
   2188 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, sc->sc_mif_config);
   2189 }
   2190 
   2191 /*
   2192  * MII interface
   2193  *
   2194  * The GEM MII interface supports at least three different operating modes:
   2195  *
   2196  * Bitbang mode is implemented using data, clock and output enable registers.
   2197  *
   2198  * Frame mode is implemented by loading a complete frame into the frame
   2199  * register and polling the valid bit for completion.
   2200  *
   2201  * Polling mode uses the frame register but completion is indicated by
   2202  * an interrupt.
   2203  *
   2204  */
   2205 static int
   2206 gem_mii_readreg(self, phy, reg)
   2207 	struct device *self;
   2208 	int phy, reg;
   2209 {
   2210 	struct gem_softc *sc = (void *)self;
   2211 	bus_space_tag_t t = sc->sc_bustag;
   2212 	bus_space_handle_t mif = sc->sc_h1;
   2213 	int n;
   2214 	u_int32_t v;
   2215 
   2216 #ifdef GEM_DEBUG1
   2217 	if (sc->sc_debug)
   2218 		printf("gem_mii_readreg: PHY %d reg %d\n", phy, reg);
   2219 #endif
   2220 
   2221 	/* Construct the frame command */
   2222 	v = (reg << GEM_MIF_REG_SHIFT)	| (phy << GEM_MIF_PHY_SHIFT) |
   2223 		GEM_MIF_FRAME_READ;
   2224 
   2225 	bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
   2226 	for (n = 0; n < 100; n++) {
   2227 		DELAY(1);
   2228 		v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
   2229 		if (v & GEM_MIF_FRAME_TA0)
   2230 			return (v & GEM_MIF_FRAME_DATA);
   2231 	}
   2232 
   2233 	printf("%s: mii_read timeout\n", sc->sc_dev.dv_xname);
   2234 	return (0);
   2235 }
   2236 
   2237 static void
   2238 gem_mii_writereg(self, phy, reg, val)
   2239 	struct device *self;
   2240 	int phy, reg, val;
   2241 {
   2242 	struct gem_softc *sc = (void *)self;
   2243 	bus_space_tag_t t = sc->sc_bustag;
   2244 	bus_space_handle_t mif = sc->sc_h1;
   2245 	int n;
   2246 	u_int32_t v;
   2247 
   2248 #ifdef GEM_DEBUG1
   2249 	if (sc->sc_debug)
   2250 		printf("gem_mii_writereg: PHY %d reg %d val %x\n",
   2251 			phy, reg, val);
   2252 #endif
   2253 
   2254 	/* Construct the frame command */
   2255 	v = GEM_MIF_FRAME_WRITE			|
   2256 	    (phy << GEM_MIF_PHY_SHIFT)		|
   2257 	    (reg << GEM_MIF_REG_SHIFT)		|
   2258 	    (val & GEM_MIF_FRAME_DATA);
   2259 
   2260 	bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
   2261 	for (n = 0; n < 100; n++) {
   2262 		DELAY(1);
   2263 		v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
   2264 		if (v & GEM_MIF_FRAME_TA0)
   2265 			return;
   2266 	}
   2267 
   2268 	printf("%s: mii_write timeout\n", sc->sc_dev.dv_xname);
   2269 }
   2270 
   2271 static void
   2272 gem_mii_statchg(dev)
   2273 	struct device *dev;
   2274 {
   2275 	struct gem_softc *sc = (void *)dev;
   2276 #ifdef GEM_DEBUG
   2277 	int instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
   2278 #endif
   2279 
   2280 #ifdef GEM_DEBUG
   2281 	if (sc->sc_debug)
   2282 		printf("gem_mii_statchg: status change: phy = %d\n",
   2283 			sc->sc_phys[instance]);
   2284 #endif
   2285 	gem_statuschange(sc);
   2286 }
   2287 
   2288 /*
   2289  * Common status change for gem_mii_statchg() and gem_pint()
   2290  */
   2291 void
   2292 gem_statuschange(struct gem_softc* sc)
   2293 {
   2294 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2295 	bus_space_tag_t t = sc->sc_bustag;
   2296 	bus_space_handle_t mac = sc->sc_h1;
   2297 	int gigabit;
   2298 	u_int32_t rxcfg, txcfg, v;
   2299 
   2300 	if ((sc->sc_mii.mii_media_status & IFM_ACTIVE) != 0 &&
   2301 	    IFM_SUBTYPE(sc->sc_mii.mii_media_active) != IFM_NONE)
   2302 		sc->sc_flags |= GEM_LINK;
   2303 	else
   2304 		sc->sc_flags &= ~GEM_LINK;
   2305 
   2306 	switch (IFM_SUBTYPE(sc->sc_mii.mii_media_active)) {
   2307 	case IFM_1000_SX:
   2308 	case IFM_1000_LX:
   2309 	case IFM_1000_CX:
   2310 	case IFM_1000_T:
   2311 		gigabit = 1;
   2312 		break;
   2313 	default:
   2314 		gigabit = 0;
   2315 	}
   2316 
   2317 	/*
   2318 	 * The configuration done here corresponds to the steps F) and
   2319 	 * G) and as far as enabling of RX and TX MAC goes also step H)
   2320 	 * of the initialization sequence outlined in section 3.2.1 of
   2321 	 * the GEM Gigabit Ethernet ASIC Specification.
   2322 	 */
   2323 
   2324 	rxcfg = bus_space_read_4(t, mac, GEM_MAC_RX_CONFIG);
   2325 	rxcfg &= ~(GEM_MAC_RX_CARR_EXTEND | GEM_MAC_RX_ENABLE);
   2326 	txcfg = GEM_MAC_TX_ENA_IPG0 | GEM_MAC_TX_NGU | GEM_MAC_TX_NGU_LIMIT;
   2327 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
   2328 		txcfg |= GEM_MAC_TX_IGN_CARRIER | GEM_MAC_TX_IGN_COLLIS;
   2329 	else if (gigabit) {
   2330 		rxcfg |= GEM_MAC_RX_CARR_EXTEND;
   2331 		txcfg |= GEM_MAC_RX_CARR_EXTEND;
   2332 	}
   2333 	bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, 0);
   2334 	bus_space_barrier(t, mac, GEM_MAC_TX_CONFIG, 4,
   2335 	    BUS_SPACE_BARRIER_WRITE);
   2336 	if (!gem_bitwait(sc, mac, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0))
   2337 		aprint_normal("%s: cannot disable TX MAC\n",
   2338 		    sc->sc_dev.dv_xname);
   2339 	bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, txcfg);
   2340 	bus_space_write_4(t, mac, GEM_MAC_RX_CONFIG, 0);
   2341 	bus_space_barrier(t, mac, GEM_MAC_RX_CONFIG, 4,
   2342 	    BUS_SPACE_BARRIER_WRITE);
   2343 	if (!gem_bitwait(sc, mac, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0))
   2344 		aprint_normal("%s: cannot disable RX MAC\n",
   2345 		    sc->sc_dev.dv_xname);
   2346 	bus_space_write_4(t, mac, GEM_MAC_RX_CONFIG, rxcfg);
   2347 
   2348 	v = bus_space_read_4(t, mac, GEM_MAC_CONTROL_CONFIG) &
   2349 	    ~(GEM_MAC_CC_RX_PAUSE | GEM_MAC_CC_TX_PAUSE);
   2350 	bus_space_write_4(t, mac, GEM_MAC_CONTROL_CONFIG, v);
   2351 
   2352 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) == 0 &&
   2353 	    gigabit != 0)
   2354 		bus_space_write_4(t, mac, GEM_MAC_SLOT_TIME,
   2355 		    GEM_MAC_SLOT_TIME_CARR_EXTEND);
   2356 	else
   2357 		bus_space_write_4(t, mac, GEM_MAC_SLOT_TIME,
   2358 		    GEM_MAC_SLOT_TIME_NORMAL);
   2359 
   2360 	/* XIF Configuration */
   2361 	if (sc->sc_flags & GEM_LINK)
   2362 		v = GEM_MAC_XIF_LINK_LED;
   2363 	else
   2364 		v = 0;
   2365 	v |= GEM_MAC_XIF_TX_MII_ENA;
   2366 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) == 0) {
   2367 		/* MII/GMII needs echo disable if half duplex. */
   2368 		if ((sc->sc_flags &(GEM_SERDES | GEM_SERIAL)) == 0)
   2369 			v |= GEM_MAC_XIF_ECHO_DISABL;
   2370 		v &= ~GEM_MAC_XIF_FDPLX_LED;
   2371 	} else {
   2372 		v |= GEM_MAC_XIF_MII_BUF_ENA;
   2373 		v |= GEM_MAC_XIF_FDPLX_LED;
   2374 	}
   2375 	if (gigabit != 0)
   2376 		v |= GEM_MAC_XIF_GMII_MODE;
   2377 	if ((ifp->if_flags & IFF_RUNNING) != 0 &&
   2378 	    (sc->sc_flags & GEM_LINK) != 0) {
   2379 		bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG,
   2380 		    txcfg | GEM_MAC_TX_ENABLE);
   2381 		bus_space_write_4(t, mac, GEM_MAC_RX_CONFIG,
   2382 		    rxcfg | GEM_MAC_RX_ENABLE);
   2383 	}
   2384 }
   2385 
   2386 int
   2387 gem_mediachange(ifp)
   2388 	struct ifnet *ifp;
   2389 {
   2390 	struct gem_softc *sc = ifp->if_softc;
   2391 	u_int s, t;
   2392 
   2393 	if (IFM_TYPE(sc->sc_media.ifm_media) != IFM_ETHER)
   2394 		return EINVAL;
   2395 
   2396 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) != 0) {
   2397 		s = IFM_SUBTYPE(sc->sc_media.ifm_media);
   2398 		if (s == IFM_AUTO) {
   2399 			if (sc->sc_mii_media != s) {
   2400 #ifdef GEM_DEBUG
   2401 				aprint_debug("%s: setting media to auto\n",
   2402 				    sc->sc_dev.dv_xname);
   2403 #endif
   2404 				sc->sc_mii_media = s;
   2405 				if (ifp->if_flags & IFF_UP) {
   2406 					gem_pcs_stop(sc, 0);
   2407 					gem_pcs_start(sc);
   2408 				}
   2409 			}
   2410 			return 0;
   2411 		}
   2412 		if (s == IFM_1000_SX) {
   2413 			t = IFM_OPTIONS(sc->sc_media.ifm_media);
   2414 			if (t == IFM_FDX || t == IFM_HDX) {
   2415 				if (sc->sc_mii_media != t) {
   2416 					sc->sc_mii_media = t;
   2417 #ifdef GEM_DEBUG
   2418 					aprint_debug("%s:"
   2419 					    " setting media to 1000baseSX-%s\n",
   2420 					    sc->sc_dev.dv_xname,
   2421 					    t == IFM_FDX ? "FDX" : "HDX");
   2422 #endif
   2423 					if (ifp->if_flags & IFF_UP) {
   2424 						gem_pcs_stop(sc, 0);
   2425 						gem_pcs_start(sc);
   2426 					}
   2427 				}
   2428 				return 0;
   2429 			}
   2430 		}
   2431 		return EINVAL;
   2432 	} else
   2433 		return (mii_mediachg(&sc->sc_mii));
   2434 }
   2435 
   2436 void
   2437 gem_mediastatus(ifp, ifmr)
   2438 	struct ifnet *ifp;
   2439 	struct ifmediareq *ifmr;
   2440 {
   2441 	struct gem_softc *sc = ifp->if_softc;
   2442 
   2443 	if ((ifp->if_flags & IFF_UP) == 0)
   2444 		return;
   2445 
   2446 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0)
   2447 		mii_pollstat(&sc->sc_mii);
   2448 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   2449 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   2450 }
   2451 
   2452 /*
   2453  * Process an ioctl request.
   2454  */
   2455 int
   2456 gem_ioctl(ifp, cmd, data)
   2457 	struct ifnet *ifp;
   2458 	u_long cmd;
   2459 	void *data;
   2460 {
   2461 	struct gem_softc *sc = ifp->if_softc;
   2462 	struct ifreq *ifr = (struct ifreq *)data;
   2463 	int s, error = 0;
   2464 
   2465 	s = splnet();
   2466 
   2467 	switch (cmd) {
   2468 	case SIOCGIFMEDIA:
   2469 	case SIOCSIFMEDIA:
   2470 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   2471 		break;
   2472 	case SIOCSIFFLAGS:
   2473 #define RESETIGN (IFF_CANTCHANGE|IFF_DEBUG)
   2474 		if (((ifp->if_flags & (IFF_UP|IFF_RUNNING))
   2475 		    == (IFF_UP|IFF_RUNNING))
   2476 		    && ((ifp->if_flags & (~RESETIGN))
   2477 		    == (sc->sc_if_flags & (~RESETIGN)))) {
   2478 			gem_setladrf(sc);
   2479 			break;
   2480 		}
   2481 #undef RESETIGN
   2482 		/*FALLTHROUGH*/
   2483 	default:
   2484 		error = ether_ioctl(ifp, cmd, data);
   2485 		if (error == ENETRESET) {
   2486 			/*
   2487 			 * Multicast list has changed; set the hardware filter
   2488 			 * accordingly.
   2489 			 */
   2490 			if (ifp->if_flags & IFF_RUNNING)
   2491 				gem_setladrf(sc);
   2492 			error = 0;
   2493 		}
   2494 		break;
   2495 	}
   2496 
   2497 	/* Try to get things going again */
   2498 	if (ifp->if_flags & IFF_UP)
   2499 		gem_start(ifp);
   2500 	splx(s);
   2501 	return (error);
   2502 }
   2503 
   2504 
   2505 void
   2506 gem_shutdown(arg)
   2507 	void *arg;
   2508 {
   2509 	struct gem_softc *sc = (struct gem_softc *)arg;
   2510 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2511 
   2512 	gem_stop(ifp, 1);
   2513 }
   2514 
   2515 /*
   2516  * Set up the logical address filter.
   2517  */
   2518 void
   2519 gem_setladrf(sc)
   2520 	struct gem_softc *sc;
   2521 {
   2522 	struct ethercom *ec = &sc->sc_ethercom;
   2523 	struct ifnet *ifp = &ec->ec_if;
   2524 	struct ether_multi *enm;
   2525 	struct ether_multistep step;
   2526 	bus_space_tag_t t = sc->sc_bustag;
   2527 	bus_space_handle_t h = sc->sc_h1;
   2528 	u_int32_t crc;
   2529 	u_int32_t hash[16];
   2530 	u_int32_t v;
   2531 	int i;
   2532 
   2533 	/* Get current RX configuration */
   2534 	v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
   2535 
   2536 	/*
   2537 	 * Turn off promiscuous mode, promiscuous group mode (all multicast),
   2538 	 * and hash filter.  Depending on the case, the right bit will be
   2539 	 * enabled.
   2540 	 */
   2541 	v &= ~(GEM_MAC_RX_PROMISCUOUS|GEM_MAC_RX_HASH_FILTER|
   2542 	    GEM_MAC_RX_PROMISC_GRP);
   2543 
   2544 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
   2545 		/* Turn on promiscuous mode */
   2546 		v |= GEM_MAC_RX_PROMISCUOUS;
   2547 		ifp->if_flags |= IFF_ALLMULTI;
   2548 		goto chipit;
   2549 	}
   2550 
   2551 	/*
   2552 	 * Set up multicast address filter by passing all multicast addresses
   2553 	 * through a crc generator, and then using the high order 8 bits as an
   2554 	 * index into the 256 bit logical address filter.  The high order 4
   2555 	 * bits selects the word, while the other 4 bits select the bit within
   2556 	 * the word (where bit 0 is the MSB).
   2557 	 */
   2558 
   2559 	/* Clear hash table */
   2560 	memset(hash, 0, sizeof(hash));
   2561 
   2562 	ETHER_FIRST_MULTI(step, ec, enm);
   2563 	while (enm != NULL) {
   2564 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2565 			/*
   2566 			 * We must listen to a range of multicast addresses.
   2567 			 * For now, just accept all multicasts, rather than
   2568 			 * trying to set only those filter bits needed to match
   2569 			 * the range.  (At this time, the only use of address
   2570 			 * ranges is for IP multicast routing, for which the
   2571 			 * range is big enough to require all bits set.)
   2572 			 * XXX should use the address filters for this
   2573 			 */
   2574 			ifp->if_flags |= IFF_ALLMULTI;
   2575 			v |= GEM_MAC_RX_PROMISC_GRP;
   2576 			goto chipit;
   2577 		}
   2578 
   2579 		/* Get the LE CRC32 of the address */
   2580 		crc = ether_crc32_le(enm->enm_addrlo, sizeof(enm->enm_addrlo));
   2581 
   2582 		/* Just want the 8 most significant bits. */
   2583 		crc >>= 24;
   2584 
   2585 		/* Set the corresponding bit in the filter. */
   2586 		hash[crc >> 4] |= 1 << (15 - (crc & 15));
   2587 
   2588 		ETHER_NEXT_MULTI(step, enm);
   2589 	}
   2590 
   2591 	v |= GEM_MAC_RX_HASH_FILTER;
   2592 	ifp->if_flags &= ~IFF_ALLMULTI;
   2593 
   2594 	/* Now load the hash table into the chip (if we are using it) */
   2595 	for (i = 0; i < 16; i++) {
   2596 		bus_space_write_4(t, h,
   2597 		    GEM_MAC_HASH0 + i * (GEM_MAC_HASH1-GEM_MAC_HASH0),
   2598 		    hash[i]);
   2599 	}
   2600 
   2601 chipit:
   2602 	sc->sc_if_flags = ifp->if_flags;
   2603 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
   2604 }
   2605 
   2606 #if notyet
   2607 
   2608 /*
   2609  * gem_power:
   2610  *
   2611  *	Power management (suspend/resume) hook.
   2612  */
   2613 void
   2614 gem_power(why, arg)
   2615 	int why;
   2616 	void *arg;
   2617 {
   2618 	struct gem_softc *sc = arg;
   2619 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2620 	int s;
   2621 
   2622 	s = splnet();
   2623 	switch (why) {
   2624 	case PWR_SUSPEND:
   2625 	case PWR_STANDBY:
   2626 		gem_stop(ifp, 1);
   2627 		if (sc->sc_power != NULL)
   2628 			(*sc->sc_power)(sc, why);
   2629 		break;
   2630 	case PWR_RESUME:
   2631 		if (ifp->if_flags & IFF_UP) {
   2632 			if (sc->sc_power != NULL)
   2633 				(*sc->sc_power)(sc, why);
   2634 			gem_init(ifp);
   2635 		}
   2636 		break;
   2637 	case PWR_SOFTSUSPEND:
   2638 	case PWR_SOFTSTANDBY:
   2639 	case PWR_SOFTRESUME:
   2640 		break;
   2641 	}
   2642 	splx(s);
   2643 }
   2644 #endif
   2645