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