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