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