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