Home | History | Annotate | Line # | Download | only in ic
elinkxl.c revision 1.59
      1 /*	$NetBSD: elinkxl.c,v 1.59 2001/11/10 22:48:09 perry Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Frank van der Linden.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include "bpfilter.h"
     40 #include "rnd.h"
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/callout.h>
     45 #include <sys/kernel.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/socket.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/errno.h>
     50 #include <sys/syslog.h>
     51 #include <sys/select.h>
     52 #include <sys/device.h>
     53 #if NRND > 0
     54 #include <sys/rnd.h>
     55 #endif
     56 
     57 #include <uvm/uvm_extern.h>
     58 
     59 #include <net/if.h>
     60 #include <net/if_dl.h>
     61 #include <net/if_ether.h>
     62 #include <net/if_media.h>
     63 
     64 #if NBPFILTER > 0
     65 #include <net/bpf.h>
     66 #include <net/bpfdesc.h>
     67 #endif
     68 
     69 #include <machine/cpu.h>
     70 #include <machine/bus.h>
     71 #include <machine/intr.h>
     72 #include <machine/endian.h>
     73 
     74 #include <dev/mii/miivar.h>
     75 #include <dev/mii/mii.h>
     76 #include <dev/mii/mii_bitbang.h>
     77 
     78 #include <dev/ic/elink3reg.h>
     79 /* #include <dev/ic/elink3var.h> */
     80 #include <dev/ic/elinkxlreg.h>
     81 #include <dev/ic/elinkxlvar.h>
     82 
     83 #ifdef DEBUG
     84 int exdebug = 0;
     85 #endif
     86 
     87 /* ifmedia callbacks */
     88 int ex_media_chg __P((struct ifnet *ifp));
     89 void ex_media_stat __P((struct ifnet *ifp, struct ifmediareq *req));
     90 
     91 void ex_probe_media __P((struct ex_softc *));
     92 void ex_set_filter __P((struct ex_softc *));
     93 void ex_set_media __P((struct ex_softc *));
     94 struct mbuf *ex_get __P((struct ex_softc *, int));
     95 u_int16_t ex_read_eeprom __P((struct ex_softc *, int));
     96 int ex_init __P((struct ifnet *));
     97 void ex_read __P((struct ex_softc *));
     98 void ex_reset __P((struct ex_softc *));
     99 void ex_set_mc __P((struct ex_softc *));
    100 void ex_getstats __P((struct ex_softc *));
    101 void ex_printstats __P((struct ex_softc *));
    102 void ex_tick __P((void *));
    103 
    104 int ex_enable __P((struct ex_softc *));
    105 void ex_disable __P((struct ex_softc *));
    106 void ex_power __P((int, void *));
    107 
    108 static int ex_eeprom_busy __P((struct ex_softc *));
    109 static int ex_add_rxbuf __P((struct ex_softc *, struct ex_rxdesc *));
    110 static void ex_init_txdescs __P((struct ex_softc *));
    111 
    112 static void ex_shutdown __P((void *));
    113 static void ex_start __P((struct ifnet *));
    114 static void ex_txstat __P((struct ex_softc *));
    115 
    116 int ex_mii_readreg __P((struct device *, int, int));
    117 void ex_mii_writereg __P((struct device *, int, int, int));
    118 void ex_mii_statchg __P((struct device *));
    119 
    120 void ex_probemedia __P((struct ex_softc *));
    121 
    122 /*
    123  * Structure to map media-present bits in boards to ifmedia codes and
    124  * printable media names.  Used for table-driven ifmedia initialization.
    125  */
    126 struct ex_media {
    127 	int	exm_mpbit;		/* media present bit */
    128 	const char *exm_name;		/* name of medium */
    129 	int	exm_ifmedia;		/* ifmedia word for medium */
    130 	int	exm_epmedia;		/* ELINKMEDIA_* constant */
    131 };
    132 
    133 /*
    134  * Media table for 3c90x chips.  Note that chips with MII have no
    135  * `native' media.
    136  */
    137 struct ex_media ex_native_media[] = {
    138 	{ ELINK_PCI_10BASE_T,	"10baseT",	IFM_ETHER|IFM_10_T,
    139 	  ELINKMEDIA_10BASE_T },
    140 	{ ELINK_PCI_10BASE_T,	"10baseT-FDX",	IFM_ETHER|IFM_10_T|IFM_FDX,
    141 	  ELINKMEDIA_10BASE_T },
    142 	{ ELINK_PCI_AUI,	"10base5",	IFM_ETHER|IFM_10_5,
    143 	  ELINKMEDIA_AUI },
    144 	{ ELINK_PCI_BNC,	"10base2",	IFM_ETHER|IFM_10_2,
    145 	  ELINKMEDIA_10BASE_2 },
    146 	{ ELINK_PCI_100BASE_TX,	"100baseTX",	IFM_ETHER|IFM_100_TX,
    147 	  ELINKMEDIA_100BASE_TX },
    148 	{ ELINK_PCI_100BASE_TX,	"100baseTX-FDX",IFM_ETHER|IFM_100_TX|IFM_FDX,
    149 	  ELINKMEDIA_100BASE_TX },
    150 	{ ELINK_PCI_100BASE_FX,	"100baseFX",	IFM_ETHER|IFM_100_FX,
    151 	  ELINKMEDIA_100BASE_FX },
    152 	{ ELINK_PCI_100BASE_MII,"manual",	IFM_ETHER|IFM_MANUAL,
    153 	  ELINKMEDIA_MII },
    154 	{ ELINK_PCI_100BASE_T4,	"100baseT4",	IFM_ETHER|IFM_100_T4,
    155 	  ELINKMEDIA_100BASE_T4 },
    156 	{ 0,			NULL,		0,
    157 	  0 },
    158 };
    159 
    160 /*
    161  * MII bit-bang glue.
    162  */
    163 u_int32_t ex_mii_bitbang_read __P((struct device *));
    164 void ex_mii_bitbang_write __P((struct device *, u_int32_t));
    165 
    166 const struct mii_bitbang_ops ex_mii_bitbang_ops = {
    167 	ex_mii_bitbang_read,
    168 	ex_mii_bitbang_write,
    169 	{
    170 		ELINK_PHY_DATA,		/* MII_BIT_MDO */
    171 		ELINK_PHY_DATA,		/* MII_BIT_MDI */
    172 		ELINK_PHY_CLK,		/* MII_BIT_MDC */
    173 		ELINK_PHY_DIR,		/* MII_BIT_DIR_HOST_PHY */
    174 		0,			/* MII_BIT_DIR_PHY_HOST */
    175 	}
    176 };
    177 
    178 /*
    179  * Back-end attach and configure.
    180  */
    181 void
    182 ex_config(sc)
    183 	struct ex_softc *sc;
    184 {
    185 	struct ifnet *ifp;
    186 	u_int16_t val;
    187 	u_int8_t macaddr[ETHER_ADDR_LEN] = {0};
    188 	bus_space_tag_t iot = sc->sc_iot;
    189 	bus_space_handle_t ioh = sc->sc_ioh;
    190 	int i, error, attach_stage;
    191 
    192 	callout_init(&sc->ex_mii_callout);
    193 
    194 	ex_reset(sc);
    195 
    196 	val = ex_read_eeprom(sc, EEPROM_OEM_ADDR0);
    197 	macaddr[0] = val >> 8;
    198 	macaddr[1] = val & 0xff;
    199 	val = ex_read_eeprom(sc, EEPROM_OEM_ADDR1);
    200 	macaddr[2] = val >> 8;
    201 	macaddr[3] = val & 0xff;
    202 	val = ex_read_eeprom(sc, EEPROM_OEM_ADDR2);
    203 	macaddr[4] = val >> 8;
    204 	macaddr[5] = val & 0xff;
    205 
    206 	printf("%s: MAC address %s\n", sc->sc_dev.dv_xname,
    207 	    ether_sprintf(macaddr));
    208 
    209 	if (sc->ex_conf & (EX_CONF_INV_LED_POLARITY|EX_CONF_PHY_POWER)) {
    210 		GO_WINDOW(2);
    211 		val = bus_space_read_2(iot, ioh, ELINK_W2_RESET_OPTIONS);
    212 		if (sc->ex_conf & EX_CONF_INV_LED_POLARITY)
    213 			val |= ELINK_RESET_OPT_LEDPOLAR;
    214 		if (sc->ex_conf & EX_CONF_PHY_POWER)
    215 			val |= ELINK_RESET_OPT_PHYPOWER;
    216 		bus_space_write_2(iot, ioh, ELINK_W2_RESET_OPTIONS, val);
    217 	}
    218 
    219 	attach_stage = 0;
    220 
    221 	/*
    222 	 * Allocate the upload descriptors, and create and load the DMA
    223 	 * map for them.
    224 	 */
    225 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    226 	    EX_NUPD * sizeof (struct ex_upd), PAGE_SIZE, 0, &sc->sc_useg, 1,
    227             &sc->sc_urseg, BUS_DMA_NOWAIT)) != 0) {
    228 		printf("%s: can't allocate upload descriptors, error = %d\n",
    229 		    sc->sc_dev.dv_xname, error);
    230 		goto fail;
    231 	}
    232 
    233 	attach_stage = 1;
    234 
    235 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_useg, sc->sc_urseg,
    236 	    EX_NUPD * sizeof (struct ex_upd), (caddr_t *)&sc->sc_upd,
    237 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    238 		printf("%s: can't map upload descriptors, error = %d\n",
    239 		    sc->sc_dev.dv_xname, error);
    240 		goto fail;
    241 	}
    242 
    243 	attach_stage = 2;
    244 
    245 	if ((error = bus_dmamap_create(sc->sc_dmat,
    246 	    EX_NUPD * sizeof (struct ex_upd), 1,
    247 	    EX_NUPD * sizeof (struct ex_upd), 0, BUS_DMA_NOWAIT,
    248 	    &sc->sc_upd_dmamap)) != 0) {
    249 		printf("%s: can't create upload desc. DMA map, error = %d\n",
    250 		    sc->sc_dev.dv_xname, error);
    251 		goto fail;
    252 	}
    253 
    254 	attach_stage = 3;
    255 
    256 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_upd_dmamap,
    257 	    sc->sc_upd, EX_NUPD * sizeof (struct ex_upd), NULL,
    258 	    BUS_DMA_NOWAIT)) != 0) {
    259 		printf("%s: can't load upload desc. DMA map, error = %d\n",
    260 		    sc->sc_dev.dv_xname, error);
    261 		goto fail;
    262 	}
    263 
    264 	attach_stage = 4;
    265 
    266 	/*
    267 	 * Allocate the download descriptors, and create and load the DMA
    268 	 * map for them.
    269 	 */
    270 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    271 	    EX_NDPD * sizeof (struct ex_dpd), PAGE_SIZE, 0, &sc->sc_dseg, 1,
    272 	    &sc->sc_drseg, BUS_DMA_NOWAIT)) != 0) {
    273 		printf("%s: can't allocate download descriptors, error = %d\n",
    274 		    sc->sc_dev.dv_xname, error);
    275 		goto fail;
    276 	}
    277 
    278 	attach_stage = 5;
    279 
    280 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dseg, sc->sc_drseg,
    281 	    EX_NDPD * sizeof (struct ex_dpd), (caddr_t *)&sc->sc_dpd,
    282 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    283 		printf("%s: can't map download descriptors, error = %d\n",
    284 		    sc->sc_dev.dv_xname, error);
    285 		goto fail;
    286 	}
    287 	memset(sc->sc_dpd, 0, EX_NDPD * sizeof (struct ex_dpd));
    288 
    289 	attach_stage = 6;
    290 
    291 	if ((error = bus_dmamap_create(sc->sc_dmat,
    292 	    EX_NDPD * sizeof (struct ex_dpd), 1,
    293 	    EX_NDPD * sizeof (struct ex_dpd), 0, BUS_DMA_NOWAIT,
    294 	    &sc->sc_dpd_dmamap)) != 0) {
    295 		printf("%s: can't create download desc. DMA map, error = %d\n",
    296 		    sc->sc_dev.dv_xname, error);
    297 		goto fail;
    298 	}
    299 
    300 	attach_stage = 7;
    301 
    302 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dpd_dmamap,
    303 	    sc->sc_dpd, EX_NDPD * sizeof (struct ex_dpd), NULL,
    304 	    BUS_DMA_NOWAIT)) != 0) {
    305 		printf("%s: can't load download desc. DMA map, error = %d\n",
    306 		    sc->sc_dev.dv_xname, error);
    307 		goto fail;
    308 	}
    309 
    310 	attach_stage = 8;
    311 
    312 
    313 	/*
    314 	 * Create the transmit buffer DMA maps.
    315 	 */
    316 	for (i = 0; i < EX_NDPD; i++) {
    317 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    318 		    EX_NTFRAGS, MCLBYTES, 0, BUS_DMA_NOWAIT,
    319 		    &sc->sc_tx_dmamaps[i])) != 0) {
    320 			printf("%s: can't create tx DMA map %d, error = %d\n",
    321 			    sc->sc_dev.dv_xname, i, error);
    322 			goto fail;
    323 		}
    324 	}
    325 
    326 	attach_stage = 9;
    327 
    328 	/*
    329 	 * Create the receive buffer DMA maps.
    330 	 */
    331 	for (i = 0; i < EX_NUPD; i++) {
    332 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    333 		    EX_NRFRAGS, MCLBYTES, 0, BUS_DMA_NOWAIT,
    334 		    &sc->sc_rx_dmamaps[i])) != 0) {
    335 			printf("%s: can't create rx DMA map %d, error = %d\n",
    336 			    sc->sc_dev.dv_xname, i, error);
    337 			goto fail;
    338 		}
    339 	}
    340 
    341 	attach_stage = 10;
    342 
    343 	/*
    344 	 * Create ring of upload descriptors, only once. The DMA engine
    345 	 * will loop over this when receiving packets, stalling if it
    346 	 * hits an UPD with a finished receive.
    347 	 */
    348 	for (i = 0; i < EX_NUPD; i++) {
    349 		sc->sc_rxdescs[i].rx_dmamap = sc->sc_rx_dmamaps[i];
    350 		sc->sc_rxdescs[i].rx_upd = &sc->sc_upd[i];
    351 		sc->sc_upd[i].upd_frags[0].fr_len =
    352 		    htole32((MCLBYTES - 2) | EX_FR_LAST);
    353 		if (ex_add_rxbuf(sc, &sc->sc_rxdescs[i]) != 0) {
    354 			printf("%s: can't allocate or map rx buffers\n",
    355 			    sc->sc_dev.dv_xname);
    356 			goto fail;
    357 		}
    358 	}
    359 
    360 	bus_dmamap_sync(sc->sc_dmat, sc->sc_upd_dmamap, 0,
    361 	    EX_NUPD * sizeof (struct ex_upd),
    362 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    363 
    364 	ex_init_txdescs(sc);
    365 
    366 	attach_stage = 11;
    367 
    368 
    369 	GO_WINDOW(3);
    370 	val = bus_space_read_2(iot, ioh, ELINK_W3_RESET_OPTIONS);
    371 	if (val & ELINK_MEDIACAP_MII)
    372 		sc->ex_conf |= EX_CONF_MII;
    373 
    374 	ifp = &sc->sc_ethercom.ec_if;
    375 
    376 	/*
    377 	 * Initialize our media structures and MII info.  We'll
    378 	 * probe the MII if we discover that we have one.
    379 	 */
    380 	sc->ex_mii.mii_ifp = ifp;
    381 	sc->ex_mii.mii_readreg = ex_mii_readreg;
    382 	sc->ex_mii.mii_writereg = ex_mii_writereg;
    383 	sc->ex_mii.mii_statchg = ex_mii_statchg;
    384 	ifmedia_init(&sc->ex_mii.mii_media, 0, ex_media_chg,
    385 	    ex_media_stat);
    386 
    387 	if (sc->ex_conf & EX_CONF_MII) {
    388 		/*
    389 		 * Find PHY, extract media information from it.
    390 		 * First, select the right transceiver.
    391 		 */
    392 		u_int32_t icfg;
    393 
    394 		GO_WINDOW(3);
    395 		icfg = bus_space_read_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG);
    396 		icfg &= ~(CONFIG_XCVR_SEL << 16);
    397 		if (val & (ELINK_MEDIACAP_MII | ELINK_MEDIACAP_100BASET4))
    398 			icfg |= ELINKMEDIA_MII << (CONFIG_XCVR_SEL_SHIFT + 16);
    399 		if (val & ELINK_MEDIACAP_100BASETX)
    400 			icfg |= ELINKMEDIA_AUTO << (CONFIG_XCVR_SEL_SHIFT + 16);
    401 		if (val & ELINK_MEDIACAP_100BASEFX)
    402 			icfg |= ELINKMEDIA_100BASE_FX
    403 				<< (CONFIG_XCVR_SEL_SHIFT + 16);
    404 		bus_space_write_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG, icfg);
    405 
    406 		mii_attach(&sc->sc_dev, &sc->ex_mii, 0xffffffff,
    407 		    MII_PHY_ANY, MII_OFFSET_ANY, 0);
    408 		if (LIST_FIRST(&sc->ex_mii.mii_phys) == NULL) {
    409 			ifmedia_add(&sc->ex_mii.mii_media, IFM_ETHER|IFM_NONE,
    410 			    0, NULL);
    411 			ifmedia_set(&sc->ex_mii.mii_media, IFM_ETHER|IFM_NONE);
    412 		} else {
    413 			ifmedia_set(&sc->ex_mii.mii_media, IFM_ETHER|IFM_AUTO);
    414 		}
    415 	} else
    416 		ex_probemedia(sc);
    417 
    418 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    419 	ifp->if_softc = sc;
    420 	ifp->if_start = ex_start;
    421 	ifp->if_ioctl = ex_ioctl;
    422 	ifp->if_watchdog = ex_watchdog;
    423 	ifp->if_init = ex_init;
    424 	ifp->if_stop = ex_stop;
    425 	ifp->if_flags =
    426 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    427 	IFQ_SET_READY(&ifp->if_snd);
    428 
    429 	/*
    430 	 * We can support 802.1Q VLAN-sized frames.
    431 	 */
    432 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    433 
    434 	/*
    435 	 * The 3c90xB has hardware IPv4/TCPv4/UDPv4 checksum support.
    436 	 */
    437 	if (sc->ex_conf & EX_CONF_90XB)
    438 		sc->sc_ethercom.ec_if.if_capabilities |= IFCAP_CSUM_IPv4 |
    439 		    IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
    440 
    441 	if_attach(ifp);
    442 	ether_ifattach(ifp, macaddr);
    443 
    444 	GO_WINDOW(1);
    445 
    446 	sc->tx_start_thresh = 20;
    447 	sc->tx_succ_ok = 0;
    448 
    449 	/* TODO: set queues to 0 */
    450 
    451 #if NRND > 0
    452 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    453 			  RND_TYPE_NET, 0);
    454 #endif
    455 
    456 	/*  Establish callback to reset card when we reboot. */
    457 	sc->sc_sdhook = shutdownhook_establish(ex_shutdown, sc);
    458 	if (sc->sc_sdhook == NULL)
    459 		printf("%s: WARNING: unable to establish shutdown hook\n",
    460 			sc->sc_dev.dv_xname);
    461 
    462 	/* Add a suspend hook to make sure we come back up after a resume. */
    463 	sc->sc_powerhook = powerhook_establish(ex_power, sc);
    464 	if (sc->sc_powerhook == NULL)
    465 		printf("%s: WARNING: unable to establish power hook\n",
    466 			sc->sc_dev.dv_xname);
    467 
    468 	/* The attach is successful. */
    469 	sc->ex_flags |= EX_FLAGS_ATTACHED;
    470 	return;
    471 
    472  fail:
    473 	/*
    474 	 * Free any resources we've allocated during the failed attach
    475 	 * attempt.  Do this in reverse order and fall though.
    476 	 */
    477 	switch (attach_stage) {
    478 	case 11:
    479 	    {
    480 		struct ex_rxdesc *rxd;
    481 
    482 		for (i = 0; i < EX_NUPD; i++) {
    483 			rxd = &sc->sc_rxdescs[i];
    484 			if (rxd->rx_mbhead != NULL) {
    485 				bus_dmamap_unload(sc->sc_dmat, rxd->rx_dmamap);
    486 				m_freem(rxd->rx_mbhead);
    487 			}
    488 		}
    489 	    }
    490 		/* FALLTHROUGH */
    491 
    492 	case 10:
    493 		for (i = 0; i < EX_NUPD; i++)
    494 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_rx_dmamaps[i]);
    495 		/* FALLTHROUGH */
    496 
    497 	case 9:
    498 		for (i = 0; i < EX_NDPD; i++)
    499 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_tx_dmamaps[i]);
    500 		/* FALLTHROUGH */
    501 	case 8:
    502 		bus_dmamap_unload(sc->sc_dmat, sc->sc_dpd_dmamap);
    503 		/* FALLTHROUGH */
    504 
    505 	case 7:
    506 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_dpd_dmamap);
    507 		/* FALLTHROUGH */
    508 
    509 	case 6:
    510 		bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_dpd,
    511 		    EX_NDPD * sizeof (struct ex_dpd));
    512 		/* FALLTHROUGH */
    513 
    514 	case 5:
    515 		bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_drseg);
    516 		break;
    517 
    518 	case 4:
    519 		bus_dmamap_unload(sc->sc_dmat, sc->sc_upd_dmamap);
    520 		/* FALLTHROUGH */
    521 
    522 	case 3:
    523 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_upd_dmamap);
    524 		/* FALLTHROUGH */
    525 
    526 	case 2:
    527 		bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_upd,
    528 		    EX_NUPD * sizeof (struct ex_upd));
    529 		/* FALLTHROUGH */
    530 
    531 	case 1:
    532 		bus_dmamem_free(sc->sc_dmat, &sc->sc_useg, sc->sc_urseg);
    533 		break;
    534 	}
    535 
    536 }
    537 
    538 /*
    539  * Find the media present on non-MII chips.
    540  */
    541 void
    542 ex_probemedia(sc)
    543 	struct ex_softc *sc;
    544 {
    545 	bus_space_tag_t iot = sc->sc_iot;
    546 	bus_space_handle_t ioh = sc->sc_ioh;
    547 	struct ifmedia *ifm = &sc->ex_mii.mii_media;
    548 	struct ex_media *exm;
    549 	u_int16_t config1, reset_options, default_media;
    550 	int defmedia = 0;
    551 	const char *sep = "", *defmedianame = NULL;
    552 
    553 	GO_WINDOW(3);
    554 	config1 = bus_space_read_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG + 2);
    555 	reset_options = bus_space_read_1(iot, ioh, ELINK_W3_RESET_OPTIONS);
    556 	GO_WINDOW(0);
    557 
    558 	default_media = (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
    559 
    560 	printf("%s: ", sc->sc_dev.dv_xname);
    561 
    562 	/* Sanity check that there are any media! */
    563 	if ((reset_options & ELINK_PCI_MEDIAMASK) == 0) {
    564 		printf("no media present!\n");
    565 		ifmedia_add(ifm, IFM_ETHER|IFM_NONE, 0, NULL);
    566 		ifmedia_set(ifm, IFM_ETHER|IFM_NONE);
    567 		return;
    568 	}
    569 
    570 #define	PRINT(str)	printf("%s%s", sep, str); sep = ", "
    571 
    572 	for (exm = ex_native_media; exm->exm_name != NULL; exm++) {
    573 		if (reset_options & exm->exm_mpbit) {
    574 			/*
    575 			 * Default media is a little complicated.  We
    576 			 * support full-duplex which uses the same
    577 			 * reset options bit.
    578 			 *
    579 			 * XXX Check EEPROM for default to FDX?
    580 			 */
    581 			if (exm->exm_epmedia == default_media) {
    582 				if ((exm->exm_ifmedia & IFM_FDX) == 0) {
    583 					defmedia = exm->exm_ifmedia;
    584 					defmedianame = exm->exm_name;
    585 				}
    586 			} else if (defmedia == 0) {
    587 				defmedia = exm->exm_ifmedia;
    588 				defmedianame = exm->exm_name;
    589 			}
    590 			ifmedia_add(ifm, exm->exm_ifmedia, exm->exm_epmedia,
    591 			    NULL);
    592 			PRINT(exm->exm_name);
    593 		}
    594 	}
    595 
    596 #undef PRINT
    597 
    598 #ifdef DIAGNOSTIC
    599 	if (defmedia == 0)
    600 		panic("ex_probemedia: impossible");
    601 #endif
    602 
    603 	printf(", default %s\n", defmedianame);
    604 	ifmedia_set(ifm, defmedia);
    605 }
    606 
    607 /*
    608  * Bring device up.
    609  */
    610 int
    611 ex_init(ifp)
    612 	struct ifnet *ifp;
    613 {
    614 	struct ex_softc *sc = ifp->if_softc;
    615 	bus_space_tag_t iot = sc->sc_iot;
    616 	bus_space_handle_t ioh = sc->sc_ioh;
    617 	int i;
    618 	int error = 0;
    619 
    620 	if ((error = ex_enable(sc)) != 0)
    621 		goto out;
    622 
    623 	ex_waitcmd(sc);
    624 	ex_stop(ifp, 0);
    625 
    626 	/*
    627 	 * Set the station address and clear the station mask. The latter
    628 	 * is needed for 90x cards, 0 is the default for 90xB cards.
    629 	 */
    630 	GO_WINDOW(2);
    631 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
    632 		bus_space_write_1(iot, ioh, ELINK_W2_ADDR_0 + i,
    633 		    LLADDR(ifp->if_sadl)[i]);
    634 		bus_space_write_1(iot, ioh, ELINK_W2_RECVMASK_0 + i, 0);
    635 	}
    636 
    637 	GO_WINDOW(3);
    638 
    639 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_RESET);
    640 	ex_waitcmd(sc);
    641 	bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_RESET);
    642 	ex_waitcmd(sc);
    643 
    644 	/*
    645 	 * Disable reclaim threshold for 90xB, set free threshold to
    646 	 * 6 * 256 = 1536 for 90x.
    647 	 */
    648 	if (sc->ex_conf & EX_CONF_90XB)
    649 		bus_space_write_2(iot, ioh, ELINK_COMMAND,
    650 		    ELINK_TXRECLTHRESH | 255);
    651 	else
    652 		bus_space_write_1(iot, ioh, ELINK_TXFREETHRESH, 6);
    653 
    654 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    655 	    SET_RX_EARLY_THRESH | ELINK_THRESH_DISABLE);
    656 
    657 	bus_space_write_4(iot, ioh, ELINK_DMACTRL,
    658 	    bus_space_read_4(iot, ioh, ELINK_DMACTRL) | ELINK_DMAC_UPRXEAREN);
    659 
    660 	bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_RD_0_MASK | S_MASK);
    661 	bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_INTR_MASK | S_MASK);
    662 
    663 	bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR | 0xff);
    664 	if (sc->intr_ack)
    665 	    (* sc->intr_ack)(sc);
    666 	ex_set_media(sc);
    667 	ex_set_mc(sc);
    668 
    669 
    670 	bus_space_write_2(iot, ioh, ELINK_COMMAND, STATS_ENABLE);
    671 	bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_ENABLE);
    672 	bus_space_write_4(iot, ioh, ELINK_UPLISTPTR, sc->sc_upddma);
    673 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_ENABLE);
    674 	bus_space_write_2(iot, ioh, ELINK_COMMAND, ELINK_UPUNSTALL);
    675 
    676 	if (sc->ex_conf & (EX_CONF_PHY_POWER | EX_CONF_INV_LED_POLARITY)) {
    677 		u_int16_t cbcard_config;
    678 
    679 		GO_WINDOW(2);
    680 		cbcard_config = bus_space_read_2(sc->sc_iot, sc->sc_ioh, 0x0c);
    681 		if (sc->ex_conf & EX_CONF_PHY_POWER) {
    682 			cbcard_config |= 0x4000; /* turn on PHY power */
    683 		}
    684 		if (sc->ex_conf & EX_CONF_INV_LED_POLARITY) {
    685 			cbcard_config |= 0x0010; /* invert LED polarity */
    686 		}
    687 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, 0x0c, cbcard_config);
    688 
    689 		GO_WINDOW(3);
    690 	}
    691 
    692 	ifp->if_flags |= IFF_RUNNING;
    693 	ifp->if_flags &= ~IFF_OACTIVE;
    694 	ex_start(ifp);
    695 
    696 	GO_WINDOW(1);
    697 
    698 	callout_reset(&sc->ex_mii_callout, hz, ex_tick, sc);
    699 
    700  out:
    701 	if (error) {
    702 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    703 		ifp->if_timer = 0;
    704 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
    705 	}
    706 	return (error);
    707 }
    708 
    709 #define	ex_mchash(addr)	(ether_crc32_be((addr), ETHER_ADDR_LEN) & 0xff)
    710 
    711 /*
    712  * Set multicast receive filter. Also take care of promiscuous mode
    713  * here (XXX).
    714  */
    715 void
    716 ex_set_mc(sc)
    717 	struct ex_softc *sc;
    718 {
    719 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    720 	struct ethercom *ec = &sc->sc_ethercom;
    721 	struct ether_multi *enm;
    722 	struct ether_multistep estep;
    723 	int i;
    724 	u_int16_t mask = FIL_INDIVIDUAL | FIL_BRDCST;
    725 
    726 	if (ifp->if_flags & IFF_PROMISC)
    727 		mask |= FIL_PROMISC;
    728 
    729 	if (!(ifp->if_flags & IFF_MULTICAST))
    730 		goto out;
    731 
    732 	if (!(sc->ex_conf & EX_CONF_90XB) || ifp->if_flags & IFF_ALLMULTI) {
    733 		mask |= (ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0;
    734 	} else {
    735 		ETHER_FIRST_MULTI(estep, ec, enm);
    736 		while (enm != NULL) {
    737 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    738 			    ETHER_ADDR_LEN) != 0)
    739 				goto out;
    740 			i = ex_mchash(enm->enm_addrlo);
    741 			bus_space_write_2(sc->sc_iot, sc->sc_ioh,
    742 			    ELINK_COMMAND, ELINK_SETHASHFILBIT | i);
    743 			ETHER_NEXT_MULTI(estep, enm);
    744 		}
    745 		mask |= FIL_MULTIHASH;
    746 	}
    747  out:
    748 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND,
    749 	    SET_RX_FILTER | mask);
    750 }
    751 
    752 
    753 static void
    754 ex_txstat(sc)
    755 	struct ex_softc *sc;
    756 {
    757 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    758 	bus_space_tag_t iot = sc->sc_iot;
    759 	bus_space_handle_t ioh = sc->sc_ioh;
    760 	int i;
    761 
    762 	/*
    763 	 * We need to read+write TX_STATUS until we get a 0 status
    764 	 * in order to turn off the interrupt flag.
    765 	 */
    766 	while ((i = bus_space_read_1(iot, ioh, ELINK_TXSTATUS)) & TXS_COMPLETE) {
    767 		bus_space_write_1(iot, ioh, ELINK_TXSTATUS, 0x0);
    768 
    769 		if (i & TXS_JABBER) {
    770 			++sc->sc_ethercom.ec_if.if_oerrors;
    771 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    772 				printf("%s: jabber (%x)\n",
    773 				       sc->sc_dev.dv_xname, i);
    774 			ex_init(ifp);
    775 			/* TODO: be more subtle here */
    776 		} else if (i & TXS_UNDERRUN) {
    777 			++sc->sc_ethercom.ec_if.if_oerrors;
    778 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    779 				printf("%s: fifo underrun (%x) @%d\n",
    780 				       sc->sc_dev.dv_xname, i,
    781 				       sc->tx_start_thresh);
    782 			if (sc->tx_succ_ok < 100)
    783 				    sc->tx_start_thresh = min(ETHER_MAX_LEN,
    784 					    sc->tx_start_thresh + 20);
    785 			sc->tx_succ_ok = 0;
    786 			ex_init(ifp);
    787 			/* TODO: be more subtle here */
    788 		} else if (i & TXS_MAX_COLLISION) {
    789 			++sc->sc_ethercom.ec_if.if_collisions;
    790 			bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_ENABLE);
    791 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
    792 		} else
    793 			sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
    794 	}
    795 }
    796 
    797 int
    798 ex_media_chg(ifp)
    799 	struct ifnet *ifp;
    800 {
    801 
    802 	if (ifp->if_flags & IFF_UP)
    803 		ex_init(ifp);
    804 	return 0;
    805 }
    806 
    807 void
    808 ex_set_media(sc)
    809 	struct ex_softc *sc;
    810 {
    811 	bus_space_tag_t iot = sc->sc_iot;
    812 	bus_space_handle_t ioh = sc->sc_ioh;
    813 	u_int32_t configreg;
    814 
    815 	if (((sc->ex_conf & EX_CONF_MII) &&
    816 	    (sc->ex_mii.mii_media_active & IFM_FDX))
    817 	    || (!(sc->ex_conf & EX_CONF_MII) &&
    818 	    (sc->ex_mii.mii_media.ifm_media & IFM_FDX))) {
    819 		bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL,
    820 		    MAC_CONTROL_FDX);
    821 	} else {
    822 		bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL, 0);
    823 	}
    824 
    825 	/*
    826 	 * If the device has MII, select it, and then tell the
    827 	 * PHY which media to use.
    828 	 */
    829 	if (sc->ex_conf & EX_CONF_MII) {
    830 		GO_WINDOW(3);
    831 
    832 		configreg = bus_space_read_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG);
    833 
    834 		configreg &= ~(CONFIG_MEDIAMASK << 16);
    835 		configreg |= (ELINKMEDIA_MII << (CONFIG_MEDIAMASK_SHIFT + 16));
    836 
    837 		bus_space_write_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG, configreg);
    838 		mii_mediachg(&sc->ex_mii);
    839 		return;
    840 	}
    841 
    842 	GO_WINDOW(4);
    843 	bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE, 0);
    844 	bus_space_write_2(iot, ioh, ELINK_COMMAND, STOP_TRANSCEIVER);
    845 	delay(800);
    846 
    847 	/*
    848 	 * Now turn on the selected media/transceiver.
    849 	 */
    850 	switch (IFM_SUBTYPE(sc->ex_mii.mii_media.ifm_cur->ifm_media)) {
    851 	case IFM_10_T:
    852 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
    853 		    JABBER_GUARD_ENABLE|LINKBEAT_ENABLE);
    854 		break;
    855 
    856 	case IFM_10_2:
    857 		bus_space_write_2(iot, ioh, ELINK_COMMAND, START_TRANSCEIVER);
    858 		DELAY(800);
    859 		break;
    860 
    861 	case IFM_100_TX:
    862 	case IFM_100_FX:
    863 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
    864 		    LINKBEAT_ENABLE);
    865 		DELAY(800);
    866 		break;
    867 
    868 	case IFM_10_5:
    869 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
    870 		    SQE_ENABLE);
    871 		DELAY(800);
    872 		break;
    873 
    874 	case IFM_MANUAL:
    875 		break;
    876 
    877 	case IFM_NONE:
    878 		return;
    879 
    880 	default:
    881 		panic("ex_set_media: impossible");
    882 	}
    883 
    884 	GO_WINDOW(3);
    885 	configreg = bus_space_read_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG);
    886 
    887 	configreg &= ~(CONFIG_MEDIAMASK << 16);
    888 	configreg |= (sc->ex_mii.mii_media.ifm_cur->ifm_data <<
    889 	    (CONFIG_MEDIAMASK_SHIFT + 16));
    890 
    891 	bus_space_write_4(iot, ioh, ELINK_W3_INTERNAL_CONFIG, configreg);
    892 }
    893 
    894 /*
    895  * Get currently-selected media from card.
    896  * (if_media callback, may be called before interface is brought up).
    897  */
    898 void
    899 ex_media_stat(ifp, req)
    900 	struct ifnet *ifp;
    901 	struct ifmediareq *req;
    902 {
    903 	struct ex_softc *sc = ifp->if_softc;
    904 
    905 	if (sc->ex_conf & EX_CONF_MII) {
    906 		mii_pollstat(&sc->ex_mii);
    907 		req->ifm_status = sc->ex_mii.mii_media_status;
    908 		req->ifm_active = sc->ex_mii.mii_media_active;
    909 	} else {
    910 		GO_WINDOW(4);
    911 		req->ifm_status = IFM_AVALID;
    912 		req->ifm_active = sc->ex_mii.mii_media.ifm_cur->ifm_media;
    913 		if (bus_space_read_2(sc->sc_iot, sc->sc_ioh,
    914 		    ELINK_W4_MEDIA_TYPE) & LINKBEAT_DETECT)
    915 			req->ifm_status |= IFM_ACTIVE;
    916                 GO_WINDOW(1);
    917 	}
    918 }
    919 
    920 
    921 
    922 /*
    923  * Start outputting on the interface.
    924  */
    925 static void
    926 ex_start(ifp)
    927 	struct ifnet *ifp;
    928 {
    929 	struct ex_softc *sc = ifp->if_softc;
    930 	bus_space_tag_t iot = sc->sc_iot;
    931 	bus_space_handle_t ioh = sc->sc_ioh;
    932 	volatile struct ex_fraghdr *fr = NULL;
    933 	volatile struct ex_dpd *dpd = NULL, *prevdpd = NULL;
    934 	struct ex_txdesc *txp;
    935 	struct mbuf *mb_head;
    936 	bus_dmamap_t dmamap;
    937 	int offset, totlen, segment, error;
    938 	u_int32_t csum_flags;
    939 
    940 	if (sc->tx_head || sc->tx_free == NULL)
    941 		return;
    942 
    943 	txp = NULL;
    944 
    945 	/*
    946 	 * We're finished if there is nothing more to add to the list or if
    947 	 * we're all filled up with buffers to transmit.
    948 	 */
    949 	while (sc->tx_free != NULL) {
    950 		/*
    951 		 * Grab a packet to transmit.
    952 		 */
    953 		IFQ_DEQUEUE(&ifp->if_snd, mb_head);
    954 		if (mb_head == NULL)
    955 			break;
    956 
    957 		/*
    958 		 * Get pointer to next available tx desc.
    959 		 */
    960 		txp = sc->tx_free;
    961 		dmamap = txp->tx_dmamap;
    962 
    963 		/*
    964 		 * Go through each of the mbufs in the chain and initialize
    965 		 * the transmit buffer descriptors with the physical address
    966 		 * and size of the mbuf.
    967 		 */
    968  reload:
    969 		error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    970 		    mb_head, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
    971 		switch (error) {
    972 		case 0:
    973 			/* Success. */
    974 			break;
    975 
    976 		case EFBIG:
    977 		    {
    978 			struct mbuf *mn;
    979 
    980 			/*
    981 			 * We ran out of segments.  We have to recopy this
    982 			 * mbuf chain first.  Bail out if we can't get the
    983 			 * new buffers.
    984 			 */
    985 			printf("%s: too many segments, ", sc->sc_dev.dv_xname);
    986 
    987 			MGETHDR(mn, M_DONTWAIT, MT_DATA);
    988 			if (mn == NULL) {
    989 				m_freem(mb_head);
    990 				printf("aborting\n");
    991 				goto out;
    992 			}
    993 			if (mb_head->m_pkthdr.len > MHLEN) {
    994 				MCLGET(mn, M_DONTWAIT);
    995 				if ((mn->m_flags & M_EXT) == 0) {
    996 					m_freem(mn);
    997 					m_freem(mb_head);
    998 					printf("aborting\n");
    999 					goto out;
   1000 				}
   1001 			}
   1002 			m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
   1003 			    mtod(mn, caddr_t));
   1004 			mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
   1005 			m_freem(mb_head);
   1006 			mb_head = mn;
   1007 			printf("retrying\n");
   1008 			goto reload;
   1009 		    }
   1010 
   1011 		default:
   1012 			/*
   1013 			 * Some other problem; report it.
   1014 			 */
   1015 			printf("%s: can't load mbuf chain, error = %d\n",
   1016 			    sc->sc_dev.dv_xname, error);
   1017 			m_freem(mb_head);
   1018 			goto out;
   1019 		}
   1020 
   1021 		/*
   1022 		 * remove our tx desc from freelist.
   1023 		 */
   1024 		sc->tx_free = txp->tx_next;
   1025 		txp->tx_next = NULL;
   1026 
   1027 		fr = &txp->tx_dpd->dpd_frags[0];
   1028 		totlen = 0;
   1029 		for (segment = 0; segment < dmamap->dm_nsegs; segment++, fr++) {
   1030 			fr->fr_addr = htole32(dmamap->dm_segs[segment].ds_addr);
   1031 			fr->fr_len = htole32(dmamap->dm_segs[segment].ds_len);
   1032 			totlen += dmamap->dm_segs[segment].ds_len;
   1033 		}
   1034 		fr--;
   1035 		fr->fr_len |= htole32(EX_FR_LAST);
   1036 		txp->tx_mbhead = mb_head;
   1037 
   1038 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
   1039 		    BUS_DMASYNC_PREWRITE);
   1040 
   1041 		dpd = txp->tx_dpd;
   1042 		dpd->dpd_nextptr = 0;
   1043 		dpd->dpd_fsh = htole32(totlen);
   1044 
   1045 		/* Byte-swap constants to compiler can optimize. */
   1046 
   1047 		if (sc->ex_conf & EX_CONF_90XB) {
   1048 			csum_flags = 0;
   1049 
   1050 			if (mb_head->m_pkthdr.csum_flags & M_CSUM_IPv4)
   1051 				csum_flags |= htole32(EX_DPD_IPCKSUM);
   1052 
   1053 			if (mb_head->m_pkthdr.csum_flags & M_CSUM_TCPv4)
   1054 				csum_flags |= htole32(EX_DPD_TCPCKSUM);
   1055 			else if (mb_head->m_pkthdr.csum_flags & M_CSUM_UDPv4)
   1056 				csum_flags |= htole32(EX_DPD_UDPCKSUM);
   1057 
   1058 			dpd->dpd_fsh |= csum_flags;
   1059 		} else {
   1060 			KDASSERT((mb_head->m_pkthdr.csum_flags &
   1061 			    (M_CSUM_IPv4|M_CSUM_TCPv4|M_CSUM_UDPv4)) == 0);
   1062 		}
   1063 
   1064 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
   1065 		    ((caddr_t)dpd - (caddr_t)sc->sc_dpd),
   1066 		    sizeof (struct ex_dpd),
   1067 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1068 
   1069 		/*
   1070 		 * No need to stall the download engine, we know it's
   1071 		 * not busy right now.
   1072 		 *
   1073 		 * Fix up pointers in both the "soft" tx and the physical
   1074 		 * tx list.
   1075 		 */
   1076 		if (sc->tx_head != NULL) {
   1077 			prevdpd = sc->tx_tail->tx_dpd;
   1078 			offset = ((caddr_t)prevdpd - (caddr_t)sc->sc_dpd);
   1079 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
   1080 			    offset, sizeof (struct ex_dpd),
   1081 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1082 			prevdpd->dpd_nextptr = htole32(DPD_DMADDR(sc, txp));
   1083 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
   1084 			    offset, sizeof (struct ex_dpd),
   1085 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1086 			sc->tx_tail->tx_next = txp;
   1087 			sc->tx_tail = txp;
   1088 		} else {
   1089 			sc->tx_tail = sc->tx_head = txp;
   1090 		}
   1091 
   1092 #if NBPFILTER > 0
   1093 		/*
   1094 		 * Pass packet to bpf if there is a listener.
   1095 		 */
   1096 		if (ifp->if_bpf)
   1097 			bpf_mtap(ifp->if_bpf, mb_head);
   1098 #endif
   1099 	}
   1100  out:
   1101 	if (sc->tx_head) {
   1102 		sc->tx_tail->tx_dpd->dpd_fsh |= htole32(EX_DPD_DNIND);
   1103 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
   1104 		    ((caddr_t)sc->tx_tail->tx_dpd - (caddr_t)sc->sc_dpd),
   1105 		    sizeof (struct ex_dpd),
   1106 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1107 		ifp->if_flags |= IFF_OACTIVE;
   1108 		bus_space_write_2(iot, ioh, ELINK_COMMAND, ELINK_DNUNSTALL);
   1109 		bus_space_write_4(iot, ioh, ELINK_DNLISTPTR,
   1110 		    DPD_DMADDR(sc, sc->tx_head));
   1111 
   1112 		/* trigger watchdog */
   1113 		ifp->if_timer = 5;
   1114 	}
   1115 }
   1116 
   1117 
   1118 int
   1119 ex_intr(arg)
   1120 	void *arg;
   1121 {
   1122 	struct ex_softc *sc = arg;
   1123 	bus_space_tag_t iot = sc->sc_iot;
   1124 	bus_space_handle_t ioh = sc->sc_ioh;
   1125 	u_int16_t stat;
   1126 	int ret = 0;
   1127 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1128 
   1129 	if ((ifp->if_flags & IFF_RUNNING) == 0 ||
   1130 	    (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   1131 		return (0);
   1132 
   1133 	for (;;) {
   1134 		stat = bus_space_read_2(iot, ioh, ELINK_STATUS);
   1135 
   1136 		if ((stat & S_MASK) == 0) {
   1137 			if ((stat & S_INTR_LATCH) == 0) {
   1138 #if 0
   1139 				printf("%s: intr latch cleared\n",
   1140 				       sc->sc_dev.dv_xname);
   1141 #endif
   1142 				break;
   1143 			}
   1144 		}
   1145 
   1146 		ret = 1;
   1147 
   1148 		/*
   1149 		 * Acknowledge interrupts.
   1150 		 */
   1151 		bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR |
   1152 				  (stat & (S_MASK | S_INTR_LATCH)));
   1153 		if (sc->intr_ack)
   1154 			(*sc->intr_ack)(sc);
   1155 
   1156 		if (stat & S_HOST_ERROR) {
   1157 			printf("%s: adapter failure (%x)\n",
   1158 			    sc->sc_dev.dv_xname, stat);
   1159 			ex_reset(sc);
   1160 			ex_init(ifp);
   1161 			return 1;
   1162 		}
   1163 		if (stat & S_TX_COMPLETE) {
   1164 			ex_txstat(sc);
   1165 		}
   1166 		if (stat & S_UPD_STATS) {
   1167 			ex_getstats(sc);
   1168 		}
   1169 		if (stat & S_DN_COMPLETE) {
   1170 			struct ex_txdesc *txp, *ptxp = NULL;
   1171 			bus_dmamap_t txmap;
   1172 
   1173 			/* reset watchdog timer, was set in ex_start() */
   1174 			ifp->if_timer = 0;
   1175 
   1176 			for (txp = sc->tx_head; txp != NULL;
   1177 			    txp = txp->tx_next) {
   1178 				bus_dmamap_sync(sc->sc_dmat,
   1179 				    sc->sc_dpd_dmamap,
   1180 				    (caddr_t)txp->tx_dpd - (caddr_t)sc->sc_dpd,
   1181 				    sizeof (struct ex_dpd),
   1182 				    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1183 				if (txp->tx_mbhead != NULL) {
   1184 					txmap = txp->tx_dmamap;
   1185 					bus_dmamap_sync(sc->sc_dmat, txmap,
   1186 					    0, txmap->dm_mapsize,
   1187 					    BUS_DMASYNC_POSTWRITE);
   1188 					bus_dmamap_unload(sc->sc_dmat, txmap);
   1189 					m_freem(txp->tx_mbhead);
   1190 					txp->tx_mbhead = NULL;
   1191 				}
   1192 				ptxp = txp;
   1193 			}
   1194 
   1195 			/*
   1196 			 * Move finished tx buffers back to the tx free list.
   1197 			 */
   1198 			if (sc->tx_free) {
   1199 				sc->tx_ftail->tx_next = sc->tx_head;
   1200 				sc->tx_ftail = ptxp;
   1201 			} else
   1202 				sc->tx_ftail = sc->tx_free = sc->tx_head;
   1203 
   1204 			sc->tx_head = sc->tx_tail = NULL;
   1205 			ifp->if_flags &= ~IFF_OACTIVE;
   1206 		}
   1207 
   1208 		if (stat & S_UP_COMPLETE) {
   1209 			struct ex_rxdesc *rxd;
   1210 			struct mbuf *m;
   1211 			struct ex_upd *upd;
   1212 			bus_dmamap_t rxmap;
   1213 			u_int32_t pktstat;
   1214 
   1215  rcvloop:
   1216 			rxd = sc->rx_head;
   1217 			rxmap = rxd->rx_dmamap;
   1218 			m = rxd->rx_mbhead;
   1219 			upd = rxd->rx_upd;
   1220 
   1221 			bus_dmamap_sync(sc->sc_dmat, rxmap, 0,
   1222 			    rxmap->dm_mapsize,
   1223 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1224 			bus_dmamap_sync(sc->sc_dmat, sc->sc_upd_dmamap,
   1225 			    ((caddr_t)upd - (caddr_t)sc->sc_upd),
   1226 			    sizeof (struct ex_upd),
   1227 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1228 			pktstat = le32toh(upd->upd_pktstatus);
   1229 
   1230 			if (pktstat & EX_UPD_COMPLETE) {
   1231 				/*
   1232 				 * Remove first packet from the chain.
   1233 				 */
   1234 				sc->rx_head = rxd->rx_next;
   1235 				rxd->rx_next = NULL;
   1236 
   1237 				/*
   1238 				 * Add a new buffer to the receive chain.
   1239 				 * If this fails, the old buffer is recycled
   1240 				 * instead.
   1241 				 */
   1242 				if (ex_add_rxbuf(sc, rxd) == 0) {
   1243 					u_int16_t total_len;
   1244 
   1245 					if (pktstat &
   1246 					    ((sc->sc_ethercom.ec_capenable &
   1247 					    ETHERCAP_VLAN_MTU) ?
   1248 					    EX_UPD_ERR_VLAN : EX_UPD_ERR)) {
   1249 						ifp->if_ierrors++;
   1250 						m_freem(m);
   1251 						goto rcvloop;
   1252 					}
   1253 
   1254 					total_len = pktstat & EX_UPD_PKTLENMASK;
   1255 					if (total_len <
   1256 					    sizeof(struct ether_header)) {
   1257 						m_freem(m);
   1258 						goto rcvloop;
   1259 					}
   1260 					m->m_pkthdr.rcvif = ifp;
   1261 					m->m_pkthdr.len = m->m_len = total_len;
   1262 #if NBPFILTER > 0
   1263 					if (ifp->if_bpf)
   1264 						bpf_mtap(ifp->if_bpf, m);
   1265 #endif
   1266 		/*
   1267 		 * Set the incoming checksum information for the packet.
   1268 		 */
   1269 		if ((sc->ex_conf & EX_CONF_90XB) != 0 &&
   1270 		    (pktstat & EX_UPD_IPCHECKED) != 0) {
   1271 			m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1272 			if (pktstat & EX_UPD_IPCKSUMERR)
   1273 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   1274 			if (pktstat & EX_UPD_TCPCHECKED) {
   1275 				m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
   1276 				if (pktstat & EX_UPD_TCPCKSUMERR)
   1277 					m->m_pkthdr.csum_flags |=
   1278 					    M_CSUM_TCP_UDP_BAD;
   1279 			} else if (pktstat & EX_UPD_UDPCHECKED) {
   1280 				m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
   1281 				if (pktstat & EX_UPD_UDPCKSUMERR)
   1282 					m->m_pkthdr.csum_flags |=
   1283 					    M_CSUM_TCP_UDP_BAD;
   1284 			}
   1285 		}
   1286 					(*ifp->if_input)(ifp, m);
   1287 				}
   1288 				goto rcvloop;
   1289 			}
   1290 			/*
   1291 			 * Just in case we filled up all UPDs and the DMA engine
   1292 			 * stalled. We could be more subtle about this.
   1293 			 */
   1294 			if (bus_space_read_4(iot, ioh, ELINK_UPLISTPTR) == 0) {
   1295 				printf("%s: uplistptr was 0\n",
   1296 				       sc->sc_dev.dv_xname);
   1297 				ex_init(ifp);
   1298 			} else if (bus_space_read_4(iot, ioh, ELINK_UPPKTSTATUS)
   1299 				   & 0x2000) {
   1300 				printf("%s: receive stalled\n",
   1301 				       sc->sc_dev.dv_xname);
   1302 				bus_space_write_2(iot, ioh, ELINK_COMMAND,
   1303 						  ELINK_UPUNSTALL);
   1304 			}
   1305 		}
   1306 	}
   1307 
   1308 	/* no more interrupts */
   1309 	if (ret && IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1310 		ex_start(ifp);
   1311 	return ret;
   1312 }
   1313 
   1314 int
   1315 ex_ioctl(ifp, cmd, data)
   1316 	struct ifnet *ifp;
   1317 	u_long cmd;
   1318 	caddr_t data;
   1319 {
   1320 	struct ex_softc *sc = ifp->if_softc;
   1321 	struct ifreq *ifr = (struct ifreq *)data;
   1322 	int s, error;
   1323 
   1324 	s = splnet();
   1325 
   1326 	switch (cmd) {
   1327 	case SIOCSIFMEDIA:
   1328 	case SIOCGIFMEDIA:
   1329 		error = ifmedia_ioctl(ifp, ifr, &sc->ex_mii.mii_media, cmd);
   1330 		break;
   1331 
   1332 	default:
   1333 		error = ether_ioctl(ifp, cmd, data);
   1334 		if (error == ENETRESET) {
   1335 			if (sc->enabled) {
   1336 			/*
   1337 			 * Multicast list has changed; set the hardware filter
   1338 			 * accordingly.
   1339 			 */
   1340 				ex_set_mc(sc);
   1341 			}
   1342 			error = 0;
   1343 		}
   1344 		break;
   1345 	}
   1346 
   1347 	splx(s);
   1348 	return (error);
   1349 }
   1350 
   1351 void
   1352 ex_getstats(sc)
   1353 	struct ex_softc *sc;
   1354 {
   1355 	bus_space_handle_t ioh = sc->sc_ioh;
   1356 	bus_space_tag_t iot = sc->sc_iot;
   1357 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1358 	u_int8_t upperok;
   1359 
   1360 	GO_WINDOW(6);
   1361 	upperok = bus_space_read_1(iot, ioh, UPPER_FRAMES_OK);
   1362 	ifp->if_ipackets += bus_space_read_1(iot, ioh, RX_FRAMES_OK);
   1363 	ifp->if_ipackets += (upperok & 0x03) << 8;
   1364 	ifp->if_opackets += bus_space_read_1(iot, ioh, TX_FRAMES_OK);
   1365 	ifp->if_opackets += (upperok & 0x30) << 4;
   1366 	ifp->if_ierrors += bus_space_read_1(iot, ioh, RX_OVERRUNS);
   1367 	ifp->if_collisions += bus_space_read_1(iot, ioh, TX_COLLISIONS);
   1368 	/*
   1369 	 * There seems to be no way to get the exact number of collisions,
   1370 	 * this is the number that occurred at the very least.
   1371 	 */
   1372 	ifp->if_collisions += 2 * bus_space_read_1(iot, ioh,
   1373 	    TX_AFTER_X_COLLISIONS);
   1374 	ifp->if_ibytes += bus_space_read_2(iot, ioh, RX_TOTAL_OK);
   1375 	ifp->if_obytes += bus_space_read_2(iot, ioh, TX_TOTAL_OK);
   1376 
   1377 	/*
   1378 	 * Clear the following to avoid stats overflow interrupts
   1379 	 */
   1380 	bus_space_read_1(iot, ioh, TX_DEFERRALS);
   1381 	bus_space_read_1(iot, ioh, TX_AFTER_1_COLLISION);
   1382 	bus_space_read_1(iot, ioh, TX_NO_SQE);
   1383 	bus_space_read_1(iot, ioh, TX_CD_LOST);
   1384 	GO_WINDOW(4);
   1385 	bus_space_read_1(iot, ioh, ELINK_W4_BADSSD);
   1386 	upperok = bus_space_read_1(iot, ioh, ELINK_W4_UBYTESOK);
   1387 	ifp->if_ibytes += (upperok & 0x0f) << 16;
   1388 	ifp->if_obytes += (upperok & 0xf0) << 12;
   1389 	GO_WINDOW(1);
   1390 }
   1391 
   1392 void
   1393 ex_printstats(sc)
   1394 	struct ex_softc *sc;
   1395 {
   1396 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1397 
   1398 	ex_getstats(sc);
   1399 	printf("in %llu out %llu ierror %llu oerror %llu ibytes %llu obytes "
   1400 	    "%llu\n", (unsigned long long)ifp->if_ipackets,
   1401 	    (unsigned long long)ifp->if_opackets,
   1402 	    (unsigned long long)ifp->if_ierrors,
   1403 	    (unsigned long long)ifp->if_oerrors,
   1404 	    (unsigned long long)ifp->if_ibytes,
   1405 	    (unsigned long long)ifp->if_obytes);
   1406 }
   1407 
   1408 void
   1409 ex_tick(arg)
   1410 	void *arg;
   1411 {
   1412 	struct ex_softc *sc = arg;
   1413 	int s;
   1414 
   1415 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   1416 		return;
   1417 
   1418 	s = splnet();
   1419 
   1420 	if (sc->ex_conf & EX_CONF_MII)
   1421 		mii_tick(&sc->ex_mii);
   1422 
   1423 	if (!(bus_space_read_2((sc)->sc_iot, (sc)->sc_ioh, ELINK_STATUS)
   1424 	    & S_COMMAND_IN_PROGRESS))
   1425 		ex_getstats(sc);
   1426 
   1427 	splx(s);
   1428 
   1429 	callout_reset(&sc->ex_mii_callout, hz, ex_tick, sc);
   1430 }
   1431 
   1432 void
   1433 ex_reset(sc)
   1434 	struct ex_softc *sc;
   1435 {
   1436 	u_int16_t val = GLOBAL_RESET;
   1437 
   1438 	if (sc->ex_conf & EX_CONF_RESETHACK)
   1439 		val |= 0x10;
   1440 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND, val);
   1441 	/*
   1442 	 * XXX apparently the command in progress bit can't be trusted
   1443 	 * during a reset, so we just always wait this long. Fortunately
   1444 	 * we normally only reset the chip during autoconfig.
   1445 	 */
   1446 	delay(100000);
   1447 	ex_waitcmd(sc);
   1448 }
   1449 
   1450 void
   1451 ex_watchdog(ifp)
   1452 	struct ifnet *ifp;
   1453 {
   1454 	struct ex_softc *sc = ifp->if_softc;
   1455 
   1456 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1457 	++sc->sc_ethercom.ec_if.if_oerrors;
   1458 
   1459 	ex_reset(sc);
   1460 	ex_init(ifp);
   1461 }
   1462 
   1463 void
   1464 ex_stop(ifp, disable)
   1465 	struct ifnet *ifp;
   1466 	int disable;
   1467 {
   1468 	struct ex_softc *sc = ifp->if_softc;
   1469 	bus_space_tag_t iot = sc->sc_iot;
   1470 	bus_space_handle_t ioh = sc->sc_ioh;
   1471 	struct ex_txdesc *tx;
   1472 	struct ex_rxdesc *rx;
   1473 	int i;
   1474 
   1475 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_DISABLE);
   1476 	bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_DISABLE);
   1477 	bus_space_write_2(iot, ioh, ELINK_COMMAND, STOP_TRANSCEIVER);
   1478 
   1479 	for (tx = sc->tx_head ; tx != NULL; tx = tx->tx_next) {
   1480 		if (tx->tx_mbhead == NULL)
   1481 			continue;
   1482 		m_freem(tx->tx_mbhead);
   1483 		tx->tx_mbhead = NULL;
   1484 		bus_dmamap_unload(sc->sc_dmat, tx->tx_dmamap);
   1485 		tx->tx_dpd->dpd_fsh = tx->tx_dpd->dpd_nextptr = 0;
   1486 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
   1487 		    ((caddr_t)tx->tx_dpd - (caddr_t)sc->sc_dpd),
   1488 		    sizeof (struct ex_dpd),
   1489 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1490 	}
   1491 	sc->tx_tail = sc->tx_head = NULL;
   1492 	ex_init_txdescs(sc);
   1493 
   1494 	sc->rx_tail = sc->rx_head = 0;
   1495 	for (i = 0; i < EX_NUPD; i++) {
   1496 		rx = &sc->sc_rxdescs[i];
   1497 		if (rx->rx_mbhead != NULL) {
   1498 			bus_dmamap_unload(sc->sc_dmat, rx->rx_dmamap);
   1499 			m_freem(rx->rx_mbhead);
   1500 			rx->rx_mbhead = NULL;
   1501 		}
   1502 		ex_add_rxbuf(sc, rx);
   1503 	}
   1504 
   1505 	bus_space_write_2(iot, ioh, ELINK_COMMAND, C_INTR_LATCH);
   1506 
   1507 	callout_stop(&sc->ex_mii_callout);
   1508 	if (sc->ex_conf & EX_CONF_MII)
   1509 		mii_down(&sc->ex_mii);
   1510 
   1511 	if (disable)
   1512 		ex_disable(sc);
   1513 
   1514 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1515 	ifp->if_timer = 0;
   1516 }
   1517 
   1518 static void
   1519 ex_init_txdescs(sc)
   1520 	struct ex_softc *sc;
   1521 {
   1522 	int i;
   1523 
   1524 	for (i = 0; i < EX_NDPD; i++) {
   1525 		sc->sc_txdescs[i].tx_dmamap = sc->sc_tx_dmamaps[i];
   1526 		sc->sc_txdescs[i].tx_dpd = &sc->sc_dpd[i];
   1527 		if (i < EX_NDPD - 1)
   1528 			sc->sc_txdescs[i].tx_next = &sc->sc_txdescs[i + 1];
   1529 		else
   1530 			sc->sc_txdescs[i].tx_next = NULL;
   1531 	}
   1532 	sc->tx_free = &sc->sc_txdescs[0];
   1533 	sc->tx_ftail = &sc->sc_txdescs[EX_NDPD-1];
   1534 }
   1535 
   1536 
   1537 int
   1538 ex_activate(self, act)
   1539 	struct device *self;
   1540 	enum devact act;
   1541 {
   1542 	struct ex_softc *sc = (void *) self;
   1543 	int s, error = 0;
   1544 
   1545 	s = splnet();
   1546 	switch (act) {
   1547 	case DVACT_ACTIVATE:
   1548 		error = EOPNOTSUPP;
   1549 		break;
   1550 
   1551 	case DVACT_DEACTIVATE:
   1552 		if (sc->ex_conf & EX_CONF_MII)
   1553 			mii_activate(&sc->ex_mii, act, MII_PHY_ANY,
   1554 			    MII_OFFSET_ANY);
   1555 		if_deactivate(&sc->sc_ethercom.ec_if);
   1556 		break;
   1557 	}
   1558 	splx(s);
   1559 
   1560 	return (error);
   1561 }
   1562 
   1563 int
   1564 ex_detach(sc)
   1565 	struct ex_softc *sc;
   1566 {
   1567 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1568 	struct ex_rxdesc *rxd;
   1569 	int i;
   1570 
   1571 	/* Succeed now if there's no work to do. */
   1572 	if ((sc->ex_flags & EX_FLAGS_ATTACHED) == 0)
   1573 		return (0);
   1574 
   1575 	/* Unhook our tick handler. */
   1576 	callout_stop(&sc->ex_mii_callout);
   1577 
   1578 	if (sc->ex_conf & EX_CONF_MII) {
   1579 		/* Detach all PHYs */
   1580 		mii_detach(&sc->ex_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   1581 	}
   1582 
   1583 	/* Delete all remaining media. */
   1584 	ifmedia_delete_instance(&sc->ex_mii.mii_media, IFM_INST_ANY);
   1585 
   1586 #if NRND > 0
   1587 	rnd_detach_source(&sc->rnd_source);
   1588 #endif
   1589 	ether_ifdetach(ifp);
   1590 	if_detach(ifp);
   1591 
   1592 	for (i = 0; i < EX_NUPD; i++) {
   1593 		rxd = &sc->sc_rxdescs[i];
   1594 		if (rxd->rx_mbhead != NULL) {
   1595 			bus_dmamap_unload(sc->sc_dmat, rxd->rx_dmamap);
   1596 			m_freem(rxd->rx_mbhead);
   1597 			rxd->rx_mbhead = NULL;
   1598 		}
   1599 	}
   1600 	for (i = 0; i < EX_NUPD; i++)
   1601 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_rx_dmamaps[i]);
   1602 	for (i = 0; i < EX_NDPD; i++)
   1603 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_tx_dmamaps[i]);
   1604 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dpd_dmamap);
   1605 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_dpd_dmamap);
   1606 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_dpd,
   1607 	    EX_NDPD * sizeof (struct ex_dpd));
   1608 	bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_drseg);
   1609 	bus_dmamap_unload(sc->sc_dmat, sc->sc_upd_dmamap);
   1610 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_upd_dmamap);
   1611 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_upd,
   1612 	    EX_NUPD * sizeof (struct ex_upd));
   1613 	bus_dmamem_free(sc->sc_dmat, &sc->sc_useg, sc->sc_urseg);
   1614 
   1615 	shutdownhook_disestablish(sc->sc_sdhook);
   1616 	powerhook_disestablish(sc->sc_powerhook);
   1617 
   1618 	return (0);
   1619 }
   1620 
   1621 /*
   1622  * Before reboots, reset card completely.
   1623  */
   1624 static void
   1625 ex_shutdown(arg)
   1626 	void *arg;
   1627 {
   1628 	struct ex_softc *sc = arg;
   1629 
   1630 	ex_stop(&sc->sc_ethercom.ec_if, 1);
   1631 }
   1632 
   1633 /*
   1634  * Read EEPROM data.
   1635  * XXX what to do if EEPROM doesn't unbusy?
   1636  */
   1637 u_int16_t
   1638 ex_read_eeprom(sc, offset)
   1639 	struct ex_softc *sc;
   1640 	int offset;
   1641 {
   1642 	bus_space_tag_t iot = sc->sc_iot;
   1643 	bus_space_handle_t ioh = sc->sc_ioh;
   1644 	u_int16_t data = 0, cmd = READ_EEPROM;
   1645 	int off;
   1646 
   1647 	off = sc->ex_conf & EX_CONF_EEPROM_OFF ? 0x30 : 0;
   1648 	cmd = sc->ex_conf & EX_CONF_EEPROM_8BIT ? READ_EEPROM8 : READ_EEPROM;
   1649 
   1650 	GO_WINDOW(0);
   1651 	if (ex_eeprom_busy(sc))
   1652 		goto out;
   1653 	bus_space_write_2(iot, ioh, ELINK_W0_EEPROM_COMMAND,
   1654 	    cmd | (off + (offset & 0x3f)));
   1655 	if (ex_eeprom_busy(sc))
   1656 		goto out;
   1657 	data = bus_space_read_2(iot, ioh, ELINK_W0_EEPROM_DATA);
   1658 out:
   1659 	return data;
   1660 }
   1661 
   1662 static int
   1663 ex_eeprom_busy(sc)
   1664 	struct ex_softc *sc;
   1665 {
   1666 	bus_space_tag_t iot = sc->sc_iot;
   1667 	bus_space_handle_t ioh = sc->sc_ioh;
   1668 	int i = 100;
   1669 
   1670 	while (i--) {
   1671 		if (!(bus_space_read_2(iot, ioh, ELINK_W0_EEPROM_COMMAND) &
   1672 		    EEPROM_BUSY))
   1673 			return 0;
   1674 		delay(100);
   1675 	}
   1676 	printf("\n%s: eeprom stays busy.\n", sc->sc_dev.dv_xname);
   1677 	return (1);
   1678 }
   1679 
   1680 /*
   1681  * Create a new rx buffer and add it to the 'soft' rx list.
   1682  */
   1683 static int
   1684 ex_add_rxbuf(sc, rxd)
   1685 	struct ex_softc *sc;
   1686 	struct ex_rxdesc *rxd;
   1687 {
   1688 	struct mbuf *m, *oldm;
   1689 	bus_dmamap_t rxmap;
   1690 	int error, rval = 0;
   1691 
   1692 	oldm = rxd->rx_mbhead;
   1693 	rxmap = rxd->rx_dmamap;
   1694 
   1695 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1696 	if (m != NULL) {
   1697 		MCLGET(m, M_DONTWAIT);
   1698 		if ((m->m_flags & M_EXT) == 0) {
   1699 			m_freem(m);
   1700 			if (oldm == NULL)
   1701 				return 1;
   1702 			m = oldm;
   1703 			m->m_data = m->m_ext.ext_buf;
   1704 			rval = 1;
   1705 		}
   1706 	} else {
   1707 		if (oldm == NULL)
   1708 			return 1;
   1709 		m = oldm;
   1710 		m->m_data = m->m_ext.ext_buf;
   1711 		rval = 1;
   1712 	}
   1713 
   1714 	/*
   1715 	 * Setup the DMA map for this receive buffer.
   1716 	 */
   1717 	if (m != oldm) {
   1718 		if (oldm != NULL)
   1719 			bus_dmamap_unload(sc->sc_dmat, rxmap);
   1720 		error = bus_dmamap_load(sc->sc_dmat, rxmap,
   1721 		    m->m_ext.ext_buf, MCLBYTES, NULL,
   1722 		    BUS_DMA_READ|BUS_DMA_NOWAIT);
   1723 		if (error) {
   1724 			printf("%s: can't load rx buffer, error = %d\n",
   1725 			    sc->sc_dev.dv_xname, error);
   1726 			panic("ex_add_rxbuf");	/* XXX */
   1727 		}
   1728 	}
   1729 
   1730 	/*
   1731 	 * Align for data after 14 byte header.
   1732 	 */
   1733 	m->m_data += 2;
   1734 
   1735 	rxd->rx_mbhead = m;
   1736 	rxd->rx_upd->upd_pktstatus = htole32(MCLBYTES - 2);
   1737 	rxd->rx_upd->upd_frags[0].fr_addr =
   1738 	    htole32(rxmap->dm_segs[0].ds_addr + 2);
   1739 	rxd->rx_upd->upd_nextptr = 0;
   1740 
   1741 	/*
   1742 	 * Attach it to the end of the list.
   1743 	 */
   1744 	if (sc->rx_head != NULL) {
   1745 		sc->rx_tail->rx_next = rxd;
   1746 		sc->rx_tail->rx_upd->upd_nextptr = htole32(sc->sc_upddma +
   1747 		    ((caddr_t)rxd->rx_upd - (caddr_t)sc->sc_upd));
   1748 		bus_dmamap_sync(sc->sc_dmat, sc->sc_upd_dmamap,
   1749 		    (caddr_t)sc->rx_tail->rx_upd - (caddr_t)sc->sc_upd,
   1750 		    sizeof (struct ex_upd),
   1751 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1752 	} else {
   1753 		sc->rx_head = rxd;
   1754 	}
   1755 	sc->rx_tail = rxd;
   1756 
   1757 	bus_dmamap_sync(sc->sc_dmat, rxmap, 0, rxmap->dm_mapsize,
   1758 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1759 	bus_dmamap_sync(sc->sc_dmat, sc->sc_upd_dmamap,
   1760 	    ((caddr_t)rxd->rx_upd - (caddr_t)sc->sc_upd),
   1761 	    sizeof (struct ex_upd), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1762 	return (rval);
   1763 }
   1764 
   1765 u_int32_t
   1766 ex_mii_bitbang_read(self)
   1767 	struct device *self;
   1768 {
   1769 	struct ex_softc *sc = (void *) self;
   1770 
   1771 	/* We're already in Window 4. */
   1772 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, ELINK_W4_PHYSMGMT));
   1773 }
   1774 
   1775 void
   1776 ex_mii_bitbang_write(self, val)
   1777 	struct device *self;
   1778 	u_int32_t val;
   1779 {
   1780 	struct ex_softc *sc = (void *) self;
   1781 
   1782 	/* We're already in Window 4. */
   1783 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_W4_PHYSMGMT, val);
   1784 }
   1785 
   1786 int
   1787 ex_mii_readreg(v, phy, reg)
   1788 	struct device *v;
   1789 	int phy, reg;
   1790 {
   1791 	struct ex_softc *sc = (struct ex_softc *)v;
   1792 	int val;
   1793 
   1794 	if ((sc->ex_conf & EX_CONF_INTPHY) && phy != ELINK_INTPHY_ID)
   1795 		return 0;
   1796 
   1797 	GO_WINDOW(4);
   1798 
   1799 	val = mii_bitbang_readreg(v, &ex_mii_bitbang_ops, phy, reg);
   1800 
   1801 	GO_WINDOW(1);
   1802 
   1803 	return (val);
   1804 }
   1805 
   1806 void
   1807 ex_mii_writereg(v, phy, reg, data)
   1808         struct device *v;
   1809         int phy;
   1810         int reg;
   1811         int data;
   1812 {
   1813 	struct ex_softc *sc = (struct ex_softc *)v;
   1814 
   1815 	GO_WINDOW(4);
   1816 
   1817 	mii_bitbang_writereg(v, &ex_mii_bitbang_ops, phy, reg, data);
   1818 
   1819 	GO_WINDOW(1);
   1820 }
   1821 
   1822 void
   1823 ex_mii_statchg(v)
   1824 	struct device *v;
   1825 {
   1826 	struct ex_softc *sc = (struct ex_softc *)v;
   1827 	bus_space_tag_t iot = sc->sc_iot;
   1828 	bus_space_handle_t ioh = sc->sc_ioh;
   1829 	int mctl;
   1830 
   1831 	GO_WINDOW(3);
   1832 	mctl = bus_space_read_2(iot, ioh, ELINK_W3_MAC_CONTROL);
   1833 	if (sc->ex_mii.mii_media_active & IFM_FDX)
   1834 		mctl |= MAC_CONTROL_FDX;
   1835 	else
   1836 		mctl &= ~MAC_CONTROL_FDX;
   1837 	bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL, mctl);
   1838 	GO_WINDOW(1);   /* back to operating window */
   1839 }
   1840 
   1841 int
   1842 ex_enable(sc)
   1843 	struct ex_softc *sc;
   1844 {
   1845 	if (sc->enabled == 0 && sc->enable != NULL) {
   1846 		if ((*sc->enable)(sc) != 0) {
   1847 			printf("%s: de/vice enable failed\n",
   1848 				sc->sc_dev.dv_xname);
   1849 			return (EIO);
   1850 		}
   1851 		sc->enabled = 1;
   1852 	}
   1853 	return (0);
   1854 }
   1855 
   1856 void
   1857 ex_disable(sc)
   1858 	struct ex_softc *sc;
   1859 {
   1860 	if (sc->enabled == 1 && sc->disable != NULL) {
   1861 		(*sc->disable)(sc);
   1862 		sc->enabled = 0;
   1863 	}
   1864 }
   1865 
   1866 void
   1867 ex_power(why, arg)
   1868 	int why;
   1869 	void *arg;
   1870 {
   1871 	struct ex_softc *sc = (void *)arg;
   1872 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1873 	int s;
   1874 
   1875 	s = splnet();
   1876 	switch (why) {
   1877 	case PWR_SUSPEND:
   1878 	case PWR_STANDBY:
   1879 		ex_stop(ifp, 0);
   1880 		if (sc->power != NULL)
   1881 			(*sc->power)(sc, why);
   1882 		break;
   1883 	case PWR_RESUME:
   1884 		if (ifp->if_flags & IFF_UP) {
   1885 			if (sc->power != NULL)
   1886 				(*sc->power)(sc, why);
   1887 			ex_init(ifp);
   1888 		}
   1889 		break;
   1890 	case PWR_SOFTSUSPEND:
   1891 	case PWR_SOFTSTANDBY:
   1892 	case PWR_SOFTRESUME:
   1893 		break;
   1894 	}
   1895 	splx(s);
   1896 }
   1897