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