Home | History | Annotate | Line # | Download | only in ic
tulip.c revision 1.61
      1 /*	$NetBSD: tulip.c,v 1.61 2000/04/07 18:58:15 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Device driver for the Digital Semiconductor ``Tulip'' (21x4x)
     42  * Ethernet controller family, and a variety of clone chips.
     43  */
     44 
     45 #include "opt_inet.h"
     46 #include "opt_ns.h"
     47 #include "bpfilter.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/callout.h>
     52 #include <sys/mbuf.h>
     53 #include <sys/malloc.h>
     54 #include <sys/kernel.h>
     55 #include <sys/socket.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/errno.h>
     58 #include <sys/device.h>
     59 
     60 #include <machine/endian.h>
     61 
     62 #include <vm/vm.h>		/* for PAGE_SIZE */
     63 
     64 #include <net/if.h>
     65 #include <net/if_dl.h>
     66 #include <net/if_media.h>
     67 #include <net/if_ether.h>
     68 
     69 #if NBPFILTER > 0
     70 #include <net/bpf.h>
     71 #endif
     72 
     73 #ifdef INET
     74 #include <netinet/in.h>
     75 #include <netinet/if_inarp.h>
     76 #endif
     77 
     78 #ifdef NS
     79 #include <netns/ns.h>
     80 #include <netns/ns_if.h>
     81 #endif
     82 
     83 #include <machine/bus.h>
     84 #include <machine/intr.h>
     85 
     86 #include <dev/mii/mii.h>
     87 #include <dev/mii/miivar.h>
     88 #include <dev/mii/mii_bitbang.h>
     89 
     90 #include <dev/ic/tulipreg.h>
     91 #include <dev/ic/tulipvar.h>
     92 
     93 const char *tlp_chip_names[] = TULIP_CHIP_NAMES;
     94 
     95 const struct tulip_txthresh_tab tlp_10_txthresh_tab[] =
     96     TLP_TXTHRESH_TAB_10;
     97 
     98 const struct tulip_txthresh_tab tlp_10_100_txthresh_tab[] =
     99     TLP_TXTHRESH_TAB_10_100;
    100 
    101 const struct tulip_txthresh_tab tlp_winb_txthresh_tab[] =
    102     TLP_TXTHRESH_TAB_WINB;
    103 
    104 void	tlp_start __P((struct ifnet *));
    105 void	tlp_watchdog __P((struct ifnet *));
    106 int	tlp_ioctl __P((struct ifnet *, u_long, caddr_t));
    107 
    108 void	tlp_shutdown __P((void *));
    109 
    110 void	tlp_reset __P((struct tulip_softc *));
    111 int	tlp_init __P((struct tulip_softc *));
    112 void	tlp_rxdrain __P((struct tulip_softc *));
    113 void	tlp_stop __P((struct tulip_softc *, int));
    114 int	tlp_add_rxbuf __P((struct tulip_softc *, int));
    115 void	tlp_idle __P((struct tulip_softc *, u_int32_t));
    116 void	tlp_srom_idle __P((struct tulip_softc *));
    117 int	tlp_srom_size __P((struct tulip_softc *));
    118 
    119 int	tlp_enable __P((struct tulip_softc *));
    120 void	tlp_disable __P((struct tulip_softc *));
    121 void	tlp_power __P((int, void *));
    122 
    123 void	tlp_filter_setup __P((struct tulip_softc *));
    124 void	tlp_winb_filter_setup __P((struct tulip_softc *));
    125 void	tlp_al981_filter_setup __P((struct tulip_softc *));
    126 
    127 void	tlp_rxintr __P((struct tulip_softc *));
    128 void	tlp_txintr __P((struct tulip_softc *));
    129 
    130 void	tlp_mii_tick __P((void *));
    131 void	tlp_mii_statchg __P((struct device *));
    132 void	tlp_winb_mii_statchg __P((struct device *));
    133 
    134 void	tlp_mii_getmedia __P((struct tulip_softc *, struct ifmediareq *));
    135 int	tlp_mii_setmedia __P((struct tulip_softc *));
    136 
    137 int	tlp_bitbang_mii_readreg __P((struct device *, int, int));
    138 void	tlp_bitbang_mii_writereg __P((struct device *, int, int, int));
    139 
    140 int	tlp_pnic_mii_readreg __P((struct device *, int, int));
    141 void	tlp_pnic_mii_writereg __P((struct device *, int, int, int));
    142 
    143 int	tlp_al981_mii_readreg __P((struct device *, int, int));
    144 void	tlp_al981_mii_writereg __P((struct device *, int, int, int));
    145 
    146 void	tlp_2114x_preinit __P((struct tulip_softc *));
    147 void	tlp_2114x_mii_preinit __P((struct tulip_softc *));
    148 void	tlp_pnic_preinit __P((struct tulip_softc *));
    149 
    150 void	tlp_21140_reset __P((struct tulip_softc *));
    151 void	tlp_21142_reset __P((struct tulip_softc *));
    152 void	tlp_pmac_reset __P((struct tulip_softc *));
    153 
    154 u_int32_t tlp_crc32 __P((const u_int8_t *, size_t));
    155 #define	tlp_mchash(addr, sz) (tlp_crc32((addr), ETHER_ADDR_LEN) & ((sz) - 1))
    156 
    157 /*
    158  * MII bit-bang glue.
    159  */
    160 u_int32_t tlp_sio_mii_bitbang_read __P((struct device *));
    161 void	tlp_sio_mii_bitbang_write __P((struct device *, u_int32_t));
    162 
    163 const struct mii_bitbang_ops tlp_sio_mii_bitbang_ops = {
    164 	tlp_sio_mii_bitbang_read,
    165 	tlp_sio_mii_bitbang_write,
    166 	{
    167 		MIIROM_MDO,		/* MII_BIT_MDO */
    168 		MIIROM_MDI,		/* MII_BIT_MDI */
    169 		MIIROM_MDC,		/* MII_BIT_MDC */
    170 		0,			/* MII_BIT_DIR_HOST_PHY */
    171 		MIIROM_MIIDIR,		/* MII_BIT_DIR_PHY_HOST */
    172 	}
    173 };
    174 
    175 #ifdef TLP_DEBUG
    176 #define	DPRINTF(sc, x)	if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
    177 				printf x
    178 #else
    179 #define	DPRINTF(sc, x)	/* nothing */
    180 #endif
    181 
    182 #ifdef TLP_STATS
    183 void	tlp_print_stats __P((struct tulip_softc *));
    184 #endif
    185 
    186 /*
    187  * Can be used to debug the SROM-related things, including contents.
    188  * Initialized so that it's patchable.
    189  */
    190 int	tlp_srom_debug = 0;
    191 
    192 /*
    193  * tlp_attach:
    194  *
    195  *	Attach a Tulip interface to the system.
    196  */
    197 void
    198 tlp_attach(sc, enaddr)
    199 	struct tulip_softc *sc;
    200 	const u_int8_t *enaddr;
    201 {
    202 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    203 	int i, error;
    204 
    205 	callout_init(&sc->sc_nway_callout);
    206 	callout_init(&sc->sc_tick_callout);
    207 
    208 	/*
    209 	 * NOTE: WE EXPECT THE FRONT-END TO INITIALIZE sc_regshift!
    210 	 */
    211 
    212 	/*
    213 	 * Setup the transmit threshold table.
    214 	 */
    215 	switch (sc->sc_chip) {
    216 	case TULIP_CHIP_DE425:
    217 	case TULIP_CHIP_21040:
    218 	case TULIP_CHIP_21041:
    219 		sc->sc_txth = tlp_10_txthresh_tab;
    220 		break;
    221 
    222 	default:
    223 		sc->sc_txth = tlp_10_100_txthresh_tab;
    224 		break;
    225 	}
    226 
    227 	/*
    228 	 * Setup the filter setup function.
    229 	 */
    230 	switch (sc->sc_chip) {
    231 	case TULIP_CHIP_WB89C840F:
    232 		sc->sc_filter_setup = tlp_winb_filter_setup;
    233 		break;
    234 
    235 	case TULIP_CHIP_AL981:
    236 		sc->sc_filter_setup = tlp_al981_filter_setup;
    237 		break;
    238 
    239 	default:
    240 		sc->sc_filter_setup = tlp_filter_setup;
    241 		break;
    242 	}
    243 
    244 	/*
    245 	 * Set up the media status change function.
    246 	 */
    247 	switch (sc->sc_chip) {
    248 	case TULIP_CHIP_WB89C840F:
    249 		sc->sc_statchg = tlp_winb_mii_statchg;
    250 		break;
    251 
    252 	default:
    253 		/*
    254 		 * We may override this if we have special media
    255 		 * handling requirements (e.g. flipping GPIO pins).
    256 		 *
    257 		 * The pure-MII statchg function covers the basics.
    258 		 */
    259 		sc->sc_statchg = tlp_mii_statchg;
    260 		break;
    261 	}
    262 
    263 	/*
    264 	 * Set up various chip-specific quirks.
    265 	 *
    266 	 * Note that wherever we can, we use the "ring" option for
    267 	 * transmit and receive descriptors.  This is because some
    268 	 * clone chips apparently have problems when using chaining,
    269 	 * although some *only* support chaining.
    270 	 *
    271 	 * What we do is always program the "next" pointer, and then
    272 	 * conditionally set the TDCTL_CH and TDCTL_ER bits in the
    273 	 * appropriate places.
    274 	 */
    275 	switch (sc->sc_chip) {
    276 	case TULIP_CHIP_21140:
    277 	case TULIP_CHIP_21140A:
    278 	case TULIP_CHIP_21142:
    279 	case TULIP_CHIP_21143:
    280 	case TULIP_CHIP_82C115:		/* 21143-like */
    281 	case TULIP_CHIP_MX98713:	/* 21140-like */
    282 	case TULIP_CHIP_MX98713A:	/* 21143-like */
    283 	case TULIP_CHIP_MX98715:	/* 21143-like */
    284 	case TULIP_CHIP_MX98715A:	/* 21143-like */
    285 	case TULIP_CHIP_MX98725:	/* 21143-like */
    286 		/*
    287 		 * Run these chips in ring mode.
    288 		 */
    289 		sc->sc_tdctl_ch = 0;
    290 		sc->sc_tdctl_er = TDCTL_ER;
    291 		sc->sc_preinit = tlp_2114x_preinit;
    292 		break;
    293 
    294 	case TULIP_CHIP_82C168:
    295 	case TULIP_CHIP_82C169:
    296 		/*
    297 		 * Run these chips in ring mode.
    298 		 */
    299 		sc->sc_tdctl_ch = 0;
    300 		sc->sc_tdctl_er = TDCTL_ER;
    301 		sc->sc_preinit = tlp_pnic_preinit;
    302 
    303 		/*
    304 		 * These chips seem to have busted DMA engines; just put them
    305 		 * in Store-and-Forward mode from the get-go.
    306 		 */
    307 		sc->sc_txthresh = TXTH_SF;
    308 		break;
    309 
    310 	case TULIP_CHIP_WB89C840F:
    311 		/*
    312 		 * Run this chip in chained mode.
    313 		 */
    314 		sc->sc_tdctl_ch = TDCTL_CH;
    315 		sc->sc_tdctl_er = 0;
    316 		sc->sc_flags |= TULIPF_IC_FS;
    317 		break;
    318 
    319 	default:
    320 		/*
    321 		 * Default to running in ring mode.
    322 		 */
    323 		sc->sc_tdctl_ch = 0;
    324 		sc->sc_tdctl_er = TDCTL_ER;
    325 	}
    326 
    327 	/*
    328 	 * Set up the MII bit-bang operations.
    329 	 */
    330 	switch (sc->sc_chip) {
    331 	case TULIP_CHIP_WB89C840F:	/* XXX direction bit different? */
    332 		sc->sc_bitbang_ops = &tlp_sio_mii_bitbang_ops;
    333 		break;
    334 
    335 	default:
    336 		sc->sc_bitbang_ops = &tlp_sio_mii_bitbang_ops;
    337 	}
    338 
    339 	SIMPLEQ_INIT(&sc->sc_txfreeq);
    340 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
    341 
    342 	/*
    343 	 * Allocate the control data structures, and create and load the
    344 	 * DMA map for it.
    345 	 */
    346 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    347 	    sizeof(struct tulip_control_data), PAGE_SIZE, 0, &sc->sc_cdseg,
    348 	    1, &sc->sc_cdnseg, 0)) != 0) {
    349 		printf("%s: unable to allocate control data, error = %d\n",
    350 		    sc->sc_dev.dv_xname, error);
    351 		goto fail_0;
    352 	}
    353 
    354 	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg,
    355 	    sizeof(struct tulip_control_data), (caddr_t *)&sc->sc_control_data,
    356 	    BUS_DMA_COHERENT)) != 0) {
    357 		printf("%s: unable to map control data, error = %d\n",
    358 		    sc->sc_dev.dv_xname, error);
    359 		goto fail_1;
    360 	}
    361 
    362 	if ((error = bus_dmamap_create(sc->sc_dmat,
    363 	    sizeof(struct tulip_control_data), 1,
    364 	    sizeof(struct tulip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    365 		printf("%s: unable to create control data DMA map, "
    366 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    367 		goto fail_2;
    368 	}
    369 
    370 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    371 	    sc->sc_control_data, sizeof(struct tulip_control_data), NULL,
    372 	    0)) != 0) {
    373 		printf("%s: unable to load control data DMA map, error = %d\n",
    374 		    sc->sc_dev.dv_xname, error);
    375 		goto fail_3;
    376 	}
    377 
    378 	/*
    379 	 * Create the transmit buffer DMA maps.
    380 	 *
    381 	 * Note that on the Xircom clone, transmit buffers must be
    382 	 * 4-byte aligned.  We're almost guaranteed to have to copy
    383 	 * the packet in that case, so we just limit ourselves to
    384 	 * one segment.
    385 	 */
    386 	switch (sc->sc_chip) {
    387 	case TULIP_CHIP_X3201_3:
    388 		sc->sc_ntxsegs = 1;
    389 		break;
    390 
    391 	default:
    392 		sc->sc_ntxsegs = TULIP_NTXSEGS;
    393 	}
    394 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
    395 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    396 		    sc->sc_ntxsegs, MCLBYTES, 0, 0,
    397 		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
    398 			printf("%s: unable to create tx DMA map %d, "
    399 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    400 			goto fail_4;
    401 		}
    402 	}
    403 
    404 	/*
    405 	 * Create the recieve buffer DMA maps.
    406 	 */
    407 	for (i = 0; i < TULIP_NRXDESC; i++) {
    408 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    409 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    410 			printf("%s: unable to create rx DMA map %d, "
    411 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    412 			goto fail_5;
    413 		}
    414 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    415 	}
    416 
    417 	/*
    418 	 * From this point forward, the attachment cannot fail.  A failure
    419 	 * before this point releases all resources that may have been
    420 	 * allocated.
    421 	 */
    422 	sc->sc_flags |= TULIPF_ATTACHED;
    423 
    424 	/*
    425 	 * Reset the chip to a known state.
    426 	 */
    427 	tlp_reset(sc);
    428 
    429 	/* Announce ourselves. */
    430 	printf("%s: %s%sEthernet address %s\n", sc->sc_dev.dv_xname,
    431 	    sc->sc_name[0] != '\0' ? sc->sc_name : "",
    432 	    sc->sc_name[0] != '\0' ? ", " : "",
    433 	    ether_sprintf(enaddr));
    434 
    435 	/*
    436 	 * Initialize our media structures.  This may probe the MII, if
    437 	 * present.
    438 	 */
    439 	(*sc->sc_mediasw->tmsw_init)(sc);
    440 
    441 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    442 	ifp->if_softc = sc;
    443 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    444 	ifp->if_ioctl = tlp_ioctl;
    445 	ifp->if_start = tlp_start;
    446 	ifp->if_watchdog = tlp_watchdog;
    447 
    448 	/*
    449 	 * Attach the interface.
    450 	 */
    451 	if_attach(ifp);
    452 	ether_ifattach(ifp, enaddr);
    453 #if NBPFILTER > 0
    454 	bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
    455 	    sizeof(struct ether_header));
    456 #endif
    457 
    458 	/*
    459 	 * Make sure the interface is shutdown during reboot.
    460 	 */
    461 	sc->sc_sdhook = shutdownhook_establish(tlp_shutdown, sc);
    462 	if (sc->sc_sdhook == NULL)
    463 		printf("%s: WARNING: unable to establish shutdown hook\n",
    464 		    sc->sc_dev.dv_xname);
    465 
    466 	/*
    467 	 * Add a suspend hook to make sure we come back up after a
    468 	 * resume.
    469 	 */
    470 	sc->sc_powerhook = powerhook_establish(tlp_power, sc);
    471 	if (sc->sc_powerhook == NULL)
    472 		printf("%s: WARNING: unable to establish power hook\n",
    473 		    sc->sc_dev.dv_xname);
    474 	return;
    475 
    476 	/*
    477 	 * Free any resources we've allocated during the failed attach
    478 	 * attempt.  Do this in reverse order and fall through.
    479 	 */
    480  fail_5:
    481 	for (i = 0; i < TULIP_NRXDESC; i++) {
    482 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    483 			bus_dmamap_destroy(sc->sc_dmat,
    484 			    sc->sc_rxsoft[i].rxs_dmamap);
    485 	}
    486  fail_4:
    487 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
    488 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
    489 			bus_dmamap_destroy(sc->sc_dmat,
    490 			    sc->sc_txsoft[i].txs_dmamap);
    491 	}
    492 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    493  fail_3:
    494 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    495  fail_2:
    496 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
    497 	    sizeof(struct tulip_control_data));
    498  fail_1:
    499 	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
    500  fail_0:
    501 	return;
    502 }
    503 
    504 /*
    505  * tlp_activate:
    506  *
    507  *	Handle device activation/deactivation requests.
    508  */
    509 int
    510 tlp_activate(self, act)
    511 	struct device *self;
    512 	enum devact act;
    513 {
    514 	struct tulip_softc *sc = (void *) self;
    515 	int s, error = 0;
    516 
    517 	s = splnet();
    518 	switch (act) {
    519 	case DVACT_ACTIVATE:
    520 		error = EOPNOTSUPP;
    521 		break;
    522 
    523 	case DVACT_DEACTIVATE:
    524 		if (sc->sc_flags & TULIPF_HAS_MII)
    525 			mii_activate(&sc->sc_mii, act, MII_PHY_ANY,
    526 			    MII_OFFSET_ANY);
    527 		if_deactivate(&sc->sc_ethercom.ec_if);
    528 		break;
    529 	}
    530 	splx(s);
    531 
    532 	return (error);
    533 }
    534 
    535 /*
    536  * tlp_detach:
    537  *
    538  *	Detach a Tulip interface.
    539  */
    540 int
    541 tlp_detach(sc)
    542 	struct tulip_softc *sc;
    543 {
    544 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    545 	struct tulip_rxsoft *rxs;
    546 	struct tulip_txsoft *txs;
    547 	int i;
    548 
    549 	/*
    550 	 * Suceed now if there isn't any work to do.
    551 	 */
    552 	if ((sc->sc_flags & TULIPF_ATTACHED) == 0)
    553 		return (0);
    554 
    555 	/* Unhook our tick handler. */
    556 	if (sc->sc_tick)
    557 		callout_stop(&sc->sc_tick_callout);
    558 
    559 	if (sc->sc_flags & TULIPF_HAS_MII) {
    560 		/* Detach all PHYs */
    561 		mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    562 	}
    563 
    564 	/* Delete all remaining media. */
    565 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
    566 
    567 #if NBPFILTER > 0
    568 	bpfdetach(ifp);
    569 #endif
    570 	ether_ifdetach(ifp);
    571 	if_detach(ifp);
    572 
    573 	for (i = 0; i < TULIP_NRXDESC; i++) {
    574 		rxs = &sc->sc_rxsoft[i];
    575 		if (rxs->rxs_mbuf != NULL) {
    576 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
    577 			m_freem(rxs->rxs_mbuf);
    578 			rxs->rxs_mbuf = NULL;
    579 		}
    580 		bus_dmamap_destroy(sc->sc_dmat, rxs->rxs_dmamap);
    581 	}
    582 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
    583 		txs = &sc->sc_txsoft[i];
    584 		if (txs->txs_mbuf != NULL) {
    585 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
    586 			m_freem(txs->txs_mbuf);
    587 			txs->txs_mbuf = NULL;
    588 		}
    589 		bus_dmamap_destroy(sc->sc_dmat, txs->txs_dmamap);
    590 	}
    591 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    592 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    593 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
    594 	    sizeof(struct tulip_control_data));
    595 	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
    596 
    597 	shutdownhook_disestablish(sc->sc_sdhook);
    598 	powerhook_disestablish(sc->sc_powerhook);
    599 
    600 	if (sc->sc_srom)
    601 		free(sc->sc_srom, M_DEVBUF);
    602 
    603 	return (0);
    604 }
    605 
    606 /*
    607  * tlp_shutdown:
    608  *
    609  *	Make sure the interface is stopped at reboot time.
    610  */
    611 void
    612 tlp_shutdown(arg)
    613 	void *arg;
    614 {
    615 	struct tulip_softc *sc = arg;
    616 
    617 	tlp_stop(sc, 1);
    618 }
    619 
    620 /*
    621  * tlp_start:		[ifnet interface function]
    622  *
    623  *	Start packet transmission on the interface.
    624  */
    625 void
    626 tlp_start(ifp)
    627 	struct ifnet *ifp;
    628 {
    629 	struct tulip_softc *sc = ifp->if_softc;
    630 	struct mbuf *m0, *m;
    631 	struct tulip_txsoft *txs, *last_txs;
    632 	bus_dmamap_t dmamap;
    633 	int error, firsttx, nexttx, lasttx, ofree, seg;
    634 
    635 	DPRINTF(sc, ("%s: tlp_start: sc_flags 0x%08x, if_flags 0x%08x\n",
    636 	    sc->sc_dev.dv_xname, sc->sc_flags, ifp->if_flags));
    637 
    638 	/*
    639 	 * If we want a filter setup, it means no more descriptors were
    640 	 * available for the setup routine.  Let it get a chance to wedge
    641 	 * itself into the ring.
    642 	 */
    643 	if (sc->sc_flags & TULIPF_WANT_SETUP)
    644 		ifp->if_flags |= IFF_OACTIVE;
    645 
    646 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    647 		return;
    648 
    649 	/*
    650 	 * Remember the previous number of free descriptors and
    651 	 * the first descriptor we'll use.
    652 	 */
    653 	ofree = sc->sc_txfree;
    654 	firsttx = sc->sc_txnext;
    655 
    656 	DPRINTF(sc, ("%s: tlp_start: txfree %d, txnext %d\n",
    657 	    sc->sc_dev.dv_xname, ofree, firsttx));
    658 
    659 	/*
    660 	 * Loop through the send queue, setting up transmit descriptors
    661 	 * until we drain the queue, or use up all available transmit
    662 	 * descriptors.
    663 	 */
    664 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
    665 	       sc->sc_txfree != 0) {
    666 		/*
    667 		 * Grab a packet off the queue.
    668 		 */
    669 		IF_DEQUEUE(&ifp->if_snd, m0);
    670 		if (m0 == NULL)
    671 			break;
    672 
    673 		dmamap = txs->txs_dmamap;
    674 
    675 		/*
    676 		 * Load the DMA map.  If this fails, the packet either
    677 		 * didn't fit in the alloted number of segments, or we were
    678 		 * short on resources.  In this case, we'll copy and try
    679 		 * again.
    680 		 *
    681 		 * Note that if we're only allowed 1 Tx segment, we
    682 		 * have an alignment restriction.  Do this test before
    683 		 * attempting to load the DMA map, because it's more
    684 		 * likely we'll trip the alignment test than the
    685 		 * more-than-one-segment test.
    686 		 */
    687 		if ((sc->sc_ntxsegs == 1 && (mtod(m0, bus_addr_t) & 3) != 0) ||
    688 		    bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    689 		      BUS_DMA_NOWAIT) != 0) {
    690 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    691 			if (m == NULL) {
    692 				printf("%s: unable to allocate Tx mbuf\n",
    693 				    sc->sc_dev.dv_xname);
    694 				IF_PREPEND(&ifp->if_snd, m0);
    695 				break;
    696 			}
    697 			if (m0->m_pkthdr.len > MHLEN) {
    698 				MCLGET(m, M_DONTWAIT);
    699 				if ((m->m_flags & M_EXT) == 0) {
    700 					printf("%s: unable to allocate Tx "
    701 					    "cluster\n", sc->sc_dev.dv_xname);
    702 					m_freem(m);
    703 					IF_PREPEND(&ifp->if_snd, m0);
    704 					break;
    705 				}
    706 			}
    707 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
    708 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    709 			m_freem(m0);
    710 			m0 = m;
    711 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    712 			    m0, BUS_DMA_NOWAIT);
    713 			if (error) {
    714 				printf("%s: unable to load Tx buffer, "
    715 				    "error = %d\n", sc->sc_dev.dv_xname, error);
    716 				IF_PREPEND(&ifp->if_snd, m0);
    717 				break;
    718 			}
    719 		}
    720 
    721 		/*
    722 		 * Ensure we have enough descriptors free to describe
    723 		 * the packet.
    724 		 */
    725 		if (dmamap->dm_nsegs > sc->sc_txfree) {
    726 			/*
    727 			 * Not enough free descriptors to transmit this
    728 			 * packet.  We haven't committed to anything yet,
    729 			 * so just unload the DMA map, put the packet
    730 			 * back on the queue, and punt.  Notify the upper
    731 			 * layer that there are no more slots left.
    732 			 *
    733 			 * XXX We could allocate an mbuf and copy, but
    734 			 * XXX it is worth it?
    735 			 */
    736 			ifp->if_flags |= IFF_OACTIVE;
    737 			bus_dmamap_unload(sc->sc_dmat, dmamap);
    738 			IF_PREPEND(&ifp->if_snd, m0);
    739 			break;
    740 		}
    741 
    742 		/*
    743 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    744 		 */
    745 
    746 		/* Sync the DMA map. */
    747 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    748 		    BUS_DMASYNC_PREWRITE);
    749 
    750 		/*
    751 		 * Initialize the transmit descriptors.
    752 		 */
    753 		for (nexttx = sc->sc_txnext, seg = 0;
    754 		     seg < dmamap->dm_nsegs;
    755 		     seg++, nexttx = TULIP_NEXTTX(nexttx)) {
    756 			/*
    757 			 * If this is the first descriptor we're
    758 			 * enqueueing, don't set the OWN bit just
    759 			 * yet.  That could cause a race condition.
    760 			 * We'll do it below.
    761 			 */
    762 			sc->sc_txdescs[nexttx].td_status =
    763 			    (nexttx == firsttx) ? 0 : htole32(TDSTAT_OWN);
    764 			sc->sc_txdescs[nexttx].td_bufaddr1 =
    765 			    htole32(dmamap->dm_segs[seg].ds_addr);
    766 			sc->sc_txdescs[nexttx].td_ctl =
    767 			    htole32((dmamap->dm_segs[seg].ds_len <<
    768 			        TDCTL_SIZE1_SHIFT) | sc->sc_tdctl_ch |
    769 				(nexttx == (TULIP_NTXDESC - 1) ?
    770 				 sc->sc_tdctl_er : 0));
    771 			lasttx = nexttx;
    772 		}
    773 
    774 		/* Set `first segment' and `last segment' appropriately. */
    775 		sc->sc_txdescs[sc->sc_txnext].td_ctl |= htole32(TDCTL_Tx_FS);
    776 		sc->sc_txdescs[lasttx].td_ctl |= htole32(TDCTL_Tx_LS);
    777 
    778 #ifdef TLP_DEBUG
    779 		if (ifp->if_flags & IFF_DEBUG) {
    780 			printf("     txsoft %p trainsmit chain:\n", txs);
    781 			for (seg = sc->sc_txnext;; seg = TULIP_NEXTTX(seg)) {
    782 				printf("     descriptor %d:\n", seg);
    783 				printf("       td_status:   0x%08x\n",
    784 				    le32toh(sc->sc_txdescs[seg].td_status));
    785 				printf("       td_ctl:      0x%08x\n",
    786 				    le32toh(sc->sc_txdescs[seg].td_ctl));
    787 				printf("       td_bufaddr1: 0x%08x\n",
    788 				    le32toh(sc->sc_txdescs[seg].td_bufaddr1));
    789 				printf("       td_bufaddr2: 0x%08x\n",
    790 				    le32toh(sc->sc_txdescs[seg].td_bufaddr2));
    791 				if (seg == lasttx)
    792 					break;
    793 			}
    794 		}
    795 #endif
    796 
    797 		/* Sync the descriptors we're using. */
    798 		TULIP_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
    799 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    800 
    801 		/*
    802 		 * Store a pointer to the packet so we can free it later,
    803 		 * and remember what txdirty will be once the packet is
    804 		 * done.
    805 		 */
    806 		txs->txs_mbuf = m0;
    807 		txs->txs_firstdesc = sc->sc_txnext;
    808 		txs->txs_lastdesc = lasttx;
    809 		txs->txs_ndescs = dmamap->dm_nsegs;
    810 
    811 		/* Advance the tx pointer. */
    812 		sc->sc_txfree -= dmamap->dm_nsegs;
    813 		sc->sc_txnext = nexttx;
    814 
    815 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
    816 		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
    817 
    818 		last_txs = txs;
    819 
    820 #if NBPFILTER > 0
    821 		/*
    822 		 * Pass the packet to any BPF listeners.
    823 		 */
    824 		if (ifp->if_bpf)
    825 			bpf_mtap(ifp->if_bpf, m0);
    826 #endif /* NBPFILTER > 0 */
    827 	}
    828 
    829 	if (txs == NULL || sc->sc_txfree == 0) {
    830 		/* No more slots left; notify upper layer. */
    831 		ifp->if_flags |= IFF_OACTIVE;
    832 	}
    833 
    834 	if (sc->sc_txfree != ofree) {
    835 		DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
    836 		    sc->sc_dev.dv_xname, lasttx, firsttx));
    837 		/*
    838 		 * Cause a transmit interrupt to happen on the
    839 		 * last packet we enqueued.
    840 		 */
    841 		sc->sc_txdescs[lasttx].td_ctl |= htole32(TDCTL_Tx_IC);
    842 		TULIP_CDTXSYNC(sc, lasttx, 1,
    843 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    844 
    845 		/*
    846 		 * Some clone chips want IC on the *first* segment in
    847 		 * the packet.  Appease them.
    848 		 */
    849 		if ((sc->sc_flags & TULIPF_IC_FS) != 0 &&
    850 		    last_txs->txs_firstdesc != lasttx) {
    851 			sc->sc_txdescs[last_txs->txs_firstdesc].td_ctl |=
    852 			    htole32(TDCTL_Tx_IC);
    853 			TULIP_CDTXSYNC(sc, last_txs->txs_firstdesc, 1,
    854 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    855 		}
    856 
    857 		/*
    858 		 * The entire packet chain is set up.  Give the
    859 		 * first descriptor to the chip now.
    860 		 */
    861 		sc->sc_txdescs[firsttx].td_status |= htole32(TDSTAT_OWN);
    862 		TULIP_CDTXSYNC(sc, firsttx, 1,
    863 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    864 
    865 		/* Wake up the transmitter. */
    866 		/* XXX USE AUTOPOLLING? */
    867 		TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
    868 
    869 		/* Set a watchdog timer in case the chip flakes out. */
    870 		ifp->if_timer = 5;
    871 	}
    872 }
    873 
    874 /*
    875  * tlp_watchdog:	[ifnet interface function]
    876  *
    877  *	Watchdog timer handler.
    878  */
    879 void
    880 tlp_watchdog(ifp)
    881 	struct ifnet *ifp;
    882 {
    883 	struct tulip_softc *sc = ifp->if_softc;
    884 	int doing_setup, doing_transmit;
    885 
    886 	doing_setup = (sc->sc_flags & TULIPF_DOING_SETUP);
    887 	doing_transmit = (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL);
    888 
    889 	if (doing_setup && doing_transmit) {
    890 		printf("%s: filter setup and transmit timeout\n",
    891 		    sc->sc_dev.dv_xname);
    892 		ifp->if_oerrors++;
    893 	} else if (doing_transmit) {
    894 		printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
    895 		ifp->if_oerrors++;
    896 	} else if (doing_setup)
    897 		printf("%s: filter setup timeout\n", sc->sc_dev.dv_xname);
    898 	else
    899 		printf("%s: spurious watchdog timeout\n", sc->sc_dev.dv_xname);
    900 
    901 	(void) tlp_init(sc);
    902 
    903 	/* Try to get more packets going. */
    904 	tlp_start(ifp);
    905 }
    906 
    907 /*
    908  * tlp_ioctl:		[ifnet interface function]
    909  *
    910  *	Handle control requests from the operator.
    911  */
    912 int
    913 tlp_ioctl(ifp, cmd, data)
    914 	struct ifnet *ifp;
    915 	u_long cmd;
    916 	caddr_t data;
    917 {
    918 	struct tulip_softc *sc = ifp->if_softc;
    919 	struct ifreq *ifr = (struct ifreq *)data;
    920 	struct ifaddr *ifa = (struct ifaddr *)data;
    921 	int s, error = 0;
    922 
    923 	s = splnet();
    924 
    925 	switch (cmd) {
    926 	case SIOCSIFADDR:
    927 		if ((error = tlp_enable(sc)) != 0)
    928 			break;
    929 		ifp->if_flags |= IFF_UP;
    930 
    931 		switch (ifa->ifa_addr->sa_family) {
    932 #ifdef INET
    933 		case AF_INET:
    934 			if ((error = tlp_init(sc)) != 0)
    935 				break;
    936 			arp_ifinit(ifp, ifa);
    937 			break;
    938 #endif /* INET */
    939 #ifdef NS
    940 		case AF_NS:
    941 		    {
    942 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    943 
    944 			if (ns_nullhost(*ina))
    945 				ina->x_host = *(union ns_host *)
    946 				    LLADDR(ifp->if_sadl);
    947 			else
    948 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
    949 				    ifp->if_addrlen);
    950 			/* Set new address. */
    951 			error = tlp_init(sc);
    952 			break;
    953 		    }
    954 #endif /* NS */
    955 		default:
    956 			error = tlp_init(sc);
    957 			break;
    958 		}
    959 		break;
    960 
    961 	case SIOCSIFMTU:
    962 		if (ifr->ifr_mtu > ETHERMTU)
    963 			error = EINVAL;
    964 		else
    965 			ifp->if_mtu = ifr->ifr_mtu;
    966 		break;
    967 
    968 	case SIOCSIFFLAGS:
    969 #ifdef TLP_STATS
    970 		if (ifp->if_flags & IFF_DEBUG)
    971 			tlp_print_stats(sc);
    972 #endif
    973 		if ((ifp->if_flags & IFF_UP) == 0 &&
    974 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    975 			/*
    976 			 * If interface is marked down and it is running, then
    977 			 * stop it.
    978 			 */
    979 			tlp_stop(sc, 1);
    980 			tlp_disable(sc);
    981 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    982 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    983 			/*
    984 			 * If interfase it marked up and it is stopped, then
    985 			 * start it.
    986 			 */
    987 			if ((error = tlp_enable(sc)) != 0)
    988 				break;
    989 			error = tlp_init(sc);
    990 		} else if ((ifp->if_flags & IFF_UP) != 0) {
    991 			/*
    992 			 * Reset the interface to pick up changes in any other
    993 			 * flags that affect the hardware state.
    994 			 */
    995 			if ((error = tlp_enable(sc)) != 0)
    996 				break;
    997 			error = tlp_init(sc);
    998 		}
    999 		break;
   1000 
   1001 	case SIOCADDMULTI:
   1002 	case SIOCDELMULTI:
   1003 		error = (cmd == SIOCADDMULTI) ?
   1004 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1005 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1006 
   1007 		if (TULIP_IS_ENABLED(sc) && error == ENETRESET) {
   1008 			/*
   1009 			 * Multicast list has changed.  Set the filter
   1010 			 * accordingly.
   1011 			 */
   1012 			(*sc->sc_filter_setup)(sc);
   1013 			error = 0;
   1014 		}
   1015 		break;
   1016 
   1017 	case SIOCSIFMEDIA:
   1018 	case SIOCGIFMEDIA:
   1019 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   1020 		break;
   1021 
   1022 	default:
   1023 		error = EINVAL;
   1024 		break;
   1025 	}
   1026 
   1027 	/* Try to get more packets going. */
   1028 	if (TULIP_IS_ENABLED(sc))
   1029 		tlp_start(ifp);
   1030 
   1031 	splx(s);
   1032 	return (error);
   1033 }
   1034 
   1035 /*
   1036  * tlp_intr:
   1037  *
   1038  *	Interrupt service routine.
   1039  */
   1040 int
   1041 tlp_intr(arg)
   1042 	void *arg;
   1043 {
   1044 	struct tulip_softc *sc = arg;
   1045 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1046 	u_int32_t status, rxstatus, txstatus;
   1047 	int handled = 0, txthresh;
   1048 
   1049 	DPRINTF(sc, ("%s: tlp_intr\n", sc->sc_dev.dv_xname));
   1050 
   1051 #ifdef DEBUG
   1052 	if (TULIP_IS_ENABLED(sc) == 0)
   1053 		panic("%s: tlp_intr: not enabled\n", sc->sc_dev.dv_xname);
   1054 #endif
   1055 
   1056 	/*
   1057 	 * If the interface isn't running, the interrupt couldn't
   1058 	 * possibly have come from us.
   1059 	 */
   1060 	if ((ifp->if_flags & IFF_RUNNING) == 0 ||
   1061 	    (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   1062 		return (0);
   1063 
   1064 	for (;;) {
   1065 		status = TULIP_READ(sc, CSR_STATUS);
   1066 		if (status)
   1067 			TULIP_WRITE(sc, CSR_STATUS, status);
   1068 
   1069 		if ((status & sc->sc_inten) == 0)
   1070 			break;
   1071 
   1072 		handled = 1;
   1073 
   1074 		rxstatus = status & sc->sc_rxint_mask;
   1075 		txstatus = status & sc->sc_txint_mask;
   1076 
   1077 		if (rxstatus) {
   1078 			/* Grab new any new packets. */
   1079 			tlp_rxintr(sc);
   1080 
   1081 			if (rxstatus & STATUS_RWT)
   1082 				printf("%s: receive watchdog timeout\n",
   1083 				    sc->sc_dev.dv_xname);
   1084 
   1085 			if (rxstatus & STATUS_RU) {
   1086 				printf("%s: receive ring overrun\n",
   1087 				    sc->sc_dev.dv_xname);
   1088 				/* Get the receive process going again. */
   1089 				tlp_idle(sc, OPMODE_SR);
   1090 				TULIP_WRITE(sc, CSR_RXLIST,
   1091 				    TULIP_CDRXADDR(sc, sc->sc_rxptr));
   1092 				TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   1093 				TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
   1094 				break;
   1095 			}
   1096 		}
   1097 
   1098 		if (txstatus) {
   1099 			/* Sweep up transmit descriptors. */
   1100 			tlp_txintr(sc);
   1101 
   1102 			if (txstatus & STATUS_TJT)
   1103 				printf("%s: transmit jabber timeout\n",
   1104 				    sc->sc_dev.dv_xname);
   1105 
   1106 			if (txstatus & STATUS_UNF) {
   1107 				/*
   1108 				 * Increase our transmit threshold if
   1109 				 * another is available.
   1110 				 */
   1111 				txthresh = sc->sc_txthresh + 1;
   1112 				if (sc->sc_txth[txthresh].txth_name != NULL) {
   1113 					/* Idle the transmit process. */
   1114 					tlp_idle(sc, OPMODE_ST);
   1115 
   1116 					sc->sc_txthresh = txthresh;
   1117 					sc->sc_opmode &= ~(OPMODE_TR|OPMODE_SF);
   1118 					sc->sc_opmode |=
   1119 					    sc->sc_txth[txthresh].txth_opmode;
   1120 					printf("%s: transmit underrun; new "
   1121 					    "threshold: %s\n",
   1122 					    sc->sc_dev.dv_xname,
   1123 					    sc->sc_txth[txthresh].txth_name);
   1124 
   1125 					/*
   1126 					 * Set the new threshold and restart
   1127 					 * the transmit process.
   1128 					 */
   1129 					TULIP_WRITE(sc, CSR_OPMODE,
   1130 					    sc->sc_opmode);
   1131 				}
   1132 					/*
   1133 					 * XXX Log every Nth underrun from
   1134 					 * XXX now on?
   1135 					 */
   1136 			}
   1137 		}
   1138 
   1139 		if (status & (STATUS_TPS|STATUS_RPS)) {
   1140 			if (status & STATUS_TPS)
   1141 				printf("%s: transmit process stopped\n",
   1142 				    sc->sc_dev.dv_xname);
   1143 			if (status & STATUS_RPS)
   1144 				printf("%s: receive process stopped\n",
   1145 				    sc->sc_dev.dv_xname);
   1146 			(void) tlp_init(sc);
   1147 			break;
   1148 		}
   1149 
   1150 		if (status & STATUS_SE) {
   1151 			const char *str;
   1152 			switch (status & STATUS_EB) {
   1153 			case STATUS_EB_PARITY:
   1154 				str = "parity error";
   1155 				break;
   1156 
   1157 			case STATUS_EB_MABT:
   1158 				str = "master abort";
   1159 				break;
   1160 
   1161 			case STATUS_EB_TABT:
   1162 				str = "target abort";
   1163 				break;
   1164 
   1165 			default:
   1166 				str = "unknown error";
   1167 				break;
   1168 			}
   1169 			printf("%s: fatal system error: %s\n",
   1170 			    sc->sc_dev.dv_xname, str);
   1171 			(void) tlp_init(sc);
   1172 			break;
   1173 		}
   1174 
   1175 		/*
   1176 		 * Not handled:
   1177 		 *
   1178 		 *	Transmit buffer unavailable -- normal
   1179 		 *	condition, nothing to do, really.
   1180 		 *
   1181 		 *	General purpose timer experied -- we don't
   1182 		 *	use the general purpose timer.
   1183 		 *
   1184 		 *	Early receive interrupt -- not available on
   1185 		 *	all chips, we just use RI.  We also only
   1186 		 *	use single-segment receive DMA, so this
   1187 		 *	is mostly useless.
   1188 		 */
   1189 	}
   1190 
   1191 	/* Try to get more packets going. */
   1192 	tlp_start(ifp);
   1193 
   1194 	return (handled);
   1195 }
   1196 
   1197 /*
   1198  * tlp_rxintr:
   1199  *
   1200  *	Helper; handle receive interrupts.
   1201  */
   1202 void
   1203 tlp_rxintr(sc)
   1204 	struct tulip_softc *sc;
   1205 {
   1206 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1207 	struct ether_header *eh;
   1208 	struct tulip_rxsoft *rxs;
   1209 	struct mbuf *m;
   1210 	u_int32_t rxstat;
   1211 	int i, len;
   1212 
   1213 	for (i = sc->sc_rxptr;; i = TULIP_NEXTRX(i)) {
   1214 		rxs = &sc->sc_rxsoft[i];
   1215 
   1216 		TULIP_CDRXSYNC(sc, i,
   1217 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1218 
   1219 		rxstat = le32toh(sc->sc_rxdescs[i].td_status);
   1220 
   1221 		if (rxstat & TDSTAT_OWN) {
   1222 			/*
   1223 			 * We have processed all of the receive buffers.
   1224 			 */
   1225 			break;
   1226 		}
   1227 
   1228 		/*
   1229 		 * Make sure the packet fit in one buffer.  This should
   1230 		 * always be the case.  But the Lite-On PNIC, rev 33
   1231 		 * has an awful receive engine bug, which may require
   1232 		 * a very icky work-around.
   1233 		 */
   1234 		if ((rxstat & (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) !=
   1235 		    (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) {
   1236 			printf("%s: incoming packet spilled, resetting\n",
   1237 			    sc->sc_dev.dv_xname);
   1238 			(void) tlp_init(sc);
   1239 			return;
   1240 		}
   1241 
   1242 		/*
   1243 		 * If any collisions were seen on the wire, count one.
   1244 		 */
   1245 		if (rxstat & TDSTAT_Rx_CS)
   1246 			ifp->if_collisions++;
   1247 
   1248 		/*
   1249 		 * If an error occured, update stats, clear the status
   1250 		 * word, and leave the packet buffer in place.  It will
   1251 		 * simply be reused the next time the ring comes around.
   1252 		 */
   1253 		if (rxstat & TDSTAT_ES) {
   1254 #define	PRINTERR(bit, str)						\
   1255 			if (rxstat & (bit))				\
   1256 				printf("%s: receive error: %s\n",	\
   1257 				    sc->sc_dev.dv_xname, str)
   1258 			ifp->if_ierrors++;
   1259 			PRINTERR(TDSTAT_Rx_DE, "descriptor error");
   1260 			PRINTERR(TDSTAT_Rx_RF, "runt frame");
   1261 			PRINTERR(TDSTAT_Rx_TL, "frame too long");
   1262 			PRINTERR(TDSTAT_Rx_RE, "MII error");
   1263 			PRINTERR(TDSTAT_Rx_DB, "dribbling bit");
   1264 			PRINTERR(TDSTAT_Rx_CE, "CRC error");
   1265 #undef PRINTERR
   1266 			TULIP_INIT_RXDESC(sc, i);
   1267 			continue;
   1268 		}
   1269 
   1270 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1271 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1272 
   1273 		/*
   1274 		 * No errors; receive the packet.  Note the Tulip
   1275 		 * includes the CRC with every packet; trim it.
   1276 		 */
   1277 		len = TDSTAT_Rx_LENGTH(rxstat) - ETHER_CRC_LEN;
   1278 
   1279 #ifdef __NO_STRICT_ALIGNMENT
   1280 		/*
   1281 		 * Allocate a new mbuf cluster.  If that fails, we are
   1282 		 * out of memory, and must drop the packet and recycle
   1283 		 * the buffer that's already attached to this descriptor.
   1284 		 */
   1285 		m = rxs->rxs_mbuf;
   1286 		if (tlp_add_rxbuf(sc, i) != 0) {
   1287 			ifp->if_ierrors++;
   1288 			TULIP_INIT_RXDESC(sc, i);
   1289 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1290 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1291 			continue;
   1292 		}
   1293 #else
   1294 		/*
   1295 		 * The Tulip's receive buffers must be 4-byte aligned.
   1296 		 * But this means that the data after the Ethernet header
   1297 		 * is misaligned.  We must allocate a new buffer and
   1298 		 * copy the data, shifted forward 2 bytes.
   1299 		 */
   1300 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1301 		if (m == NULL) {
   1302  dropit:
   1303 			ifp->if_ierrors++;
   1304 			TULIP_INIT_RXDESC(sc, i);
   1305 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1306 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1307 			continue;
   1308 		}
   1309 		if (len > (MHLEN - 2)) {
   1310 			MCLGET(m, M_DONTWAIT);
   1311 			if ((m->m_flags & M_EXT) == 0) {
   1312 				m_freem(m);
   1313 				goto dropit;
   1314 			}
   1315 		}
   1316 		m->m_data += 2;
   1317 
   1318 		/*
   1319 		 * Note that we use clusters for incoming frames, so the
   1320 		 * buffer is virtually contiguous.
   1321 		 */
   1322 		memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
   1323 
   1324 		/* Allow the receive descriptor to continue using its mbuf. */
   1325 		TULIP_INIT_RXDESC(sc, i);
   1326 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1327 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1328 #endif /* __NO_STRICT_ALIGNMENT */
   1329 
   1330 		ifp->if_ipackets++;
   1331 		eh = mtod(m, struct ether_header *);
   1332 		m->m_pkthdr.rcvif = ifp;
   1333 		m->m_pkthdr.len = m->m_len = len;
   1334 
   1335 #if NBPFILTER > 0
   1336 		/*
   1337 		 * Pass this up to any BPF listeners, but only
   1338 		 * pass it up the stack if its for us.
   1339 		 */
   1340 		if (ifp->if_bpf)
   1341 			bpf_mtap(ifp->if_bpf, m);
   1342 #endif /* NPBFILTER > 0 */
   1343 
   1344 		/*
   1345 		 * This test is outside the NBPFILTER block because
   1346 		 * on the 21140 we have to use Hash-Only mode due to
   1347 		 * a bug in the filter logic.
   1348 		 */
   1349 		if ((ifp->if_flags & IFF_PROMISC) != 0 ||
   1350 		    sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
   1351 			if (memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
   1352 				 ETHER_ADDR_LEN) != 0 &&
   1353 			    ETHER_IS_MULTICAST(eh->ether_dhost) == 0) {
   1354 				m_freem(m);
   1355 				continue;
   1356 			}
   1357 		}
   1358 
   1359 		/* Pass it on. */
   1360 		(*ifp->if_input)(ifp, m);
   1361 	}
   1362 
   1363 	/* Update the recieve pointer. */
   1364 	sc->sc_rxptr = i;
   1365 }
   1366 
   1367 /*
   1368  * tlp_txintr:
   1369  *
   1370  *	Helper; handle transmit interrupts.
   1371  */
   1372 void
   1373 tlp_txintr(sc)
   1374 	struct tulip_softc *sc;
   1375 {
   1376 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1377 	struct tulip_txsoft *txs;
   1378 	u_int32_t txstat;
   1379 
   1380 	DPRINTF(sc, ("%s: tlp_txintr: sc_flags 0x%08x\n",
   1381 	    sc->sc_dev.dv_xname, sc->sc_flags));
   1382 
   1383 	ifp->if_flags &= ~IFF_OACTIVE;
   1384 
   1385 	/*
   1386 	 * Go through our Tx list and free mbufs for those
   1387 	 * frames that have been transmitted.
   1388 	 */
   1389 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1390 		TULIP_CDTXSYNC(sc, txs->txs_lastdesc,
   1391 		    txs->txs_ndescs,
   1392 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1393 
   1394 #ifdef TLP_DEBUG
   1395 		if (ifp->if_flags & IFF_DEBUG) {
   1396 			int i;
   1397 			printf("    txsoft %p trainsmit chain:\n", txs);
   1398 			for (i = txs->txs_firstdesc;; i = TULIP_NEXTTX(i)) {
   1399 				printf("     descriptor %d:\n", i);
   1400 				printf("       td_status:   0x%08x\n",
   1401 				    le32toh(sc->sc_txdescs[i].td_status));
   1402 				printf("       td_ctl:      0x%08x\n",
   1403 				    le32toh(sc->sc_txdescs[i].td_ctl));
   1404 				printf("       td_bufaddr1: 0x%08x\n",
   1405 				    le32toh(sc->sc_txdescs[i].td_bufaddr1));
   1406 				printf("       td_bufaddr2: 0x%08x\n",
   1407 				    le32toh(sc->sc_txdescs[i].td_bufaddr2));
   1408 				if (i == txs->txs_lastdesc)
   1409 					break;
   1410 			}
   1411 		}
   1412 #endif
   1413 
   1414 		txstat = le32toh(sc->sc_txdescs[txs->txs_lastdesc].td_status);
   1415 		if (txstat & TDSTAT_OWN)
   1416 			break;
   1417 
   1418 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
   1419 
   1420 		sc->sc_txfree += txs->txs_ndescs;
   1421 
   1422 		if (txs->txs_mbuf == NULL) {
   1423 			/*
   1424 			 * If we didn't have an mbuf, it was the setup
   1425 			 * packet.
   1426 			 */
   1427 #ifdef DIAGNOSTIC
   1428 			if ((sc->sc_flags & TULIPF_DOING_SETUP) == 0)
   1429 				panic("tlp_txintr: null mbuf, not doing setup");
   1430 #endif
   1431 			TULIP_CDSPSYNC(sc, BUS_DMASYNC_POSTWRITE);
   1432 			sc->sc_flags &= ~TULIPF_DOING_SETUP;
   1433 			SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1434 			continue;
   1435 		}
   1436 
   1437 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   1438 		    0, txs->txs_dmamap->dm_mapsize,
   1439 		    BUS_DMASYNC_POSTWRITE);
   1440 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1441 		m_freem(txs->txs_mbuf);
   1442 		txs->txs_mbuf = NULL;
   1443 
   1444 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1445 
   1446 		/*
   1447 		 * Check for errors and collisions.
   1448 		 */
   1449 #ifdef TLP_STATS
   1450 		if (txstat & TDSTAT_Tx_UF)
   1451 			sc->sc_stats.ts_tx_uf++;
   1452 		if (txstat & TDSTAT_Tx_TO)
   1453 			sc->sc_stats.ts_tx_to++;
   1454 		if (txstat & TDSTAT_Tx_EC)
   1455 			sc->sc_stats.ts_tx_ec++;
   1456 		if (txstat & TDSTAT_Tx_LC)
   1457 			sc->sc_stats.ts_tx_lc++;
   1458 #endif
   1459 
   1460 		if (txstat & (TDSTAT_Tx_UF|TDSTAT_Tx_TO))
   1461 			ifp->if_oerrors++;
   1462 
   1463 		if (txstat & TDSTAT_Tx_EC)
   1464 			ifp->if_collisions += 16;
   1465 		else
   1466 			ifp->if_collisions += TDSTAT_Tx_COLLISIONS(txstat);
   1467 		if (txstat & TDSTAT_Tx_LC)
   1468 			ifp->if_collisions++;
   1469 
   1470 		ifp->if_opackets++;
   1471 	}
   1472 
   1473 	/*
   1474 	 * If there are no more pending transmissions, cancel the watchdog
   1475 	 * timer.
   1476 	 */
   1477 	if (txs == NULL && (sc->sc_flags & TULIPF_DOING_SETUP) == 0)
   1478 		ifp->if_timer = 0;
   1479 
   1480 	/*
   1481 	 * If we have a receive filter setup pending, do it now.
   1482 	 */
   1483 	if (sc->sc_flags & TULIPF_WANT_SETUP)
   1484 		(*sc->sc_filter_setup)(sc);
   1485 }
   1486 
   1487 #ifdef TLP_STATS
   1488 void
   1489 tlp_print_stats(sc)
   1490 	struct tulip_softc *sc;
   1491 {
   1492 
   1493 	printf("%s: tx_uf %lu, tx_to %lu, tx_ec %lu, tx_lc %lu\n",
   1494 	    sc->sc_dev.dv_xname,
   1495 	    sc->sc_stats.ts_tx_uf, sc->sc_stats.ts_tx_to,
   1496 	    sc->sc_stats.ts_tx_ec, sc->sc_stats.ts_tx_lc);
   1497 }
   1498 #endif
   1499 
   1500 /*
   1501  * tlp_reset:
   1502  *
   1503  *	Perform a soft reset on the Tulip.
   1504  */
   1505 void
   1506 tlp_reset(sc)
   1507 	struct tulip_softc *sc;
   1508 {
   1509 	int i;
   1510 
   1511 	TULIP_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR);
   1512 
   1513 	/*
   1514 	 * Xircom clone doesn't bring itself out of reset automatically.
   1515 	 * Instead, we have to wait at least 50 PCI cycles, and then
   1516 	 * clear SWR.
   1517 	 */
   1518 	if (sc->sc_chip == TULIP_CHIP_X3201_3) {
   1519 		delay(10);
   1520 		TULIP_WRITE(sc, CSR_BUSMODE, 0);
   1521 	}
   1522 
   1523 	for (i = 0; i < 1000; i++) {
   1524 		/*
   1525 		 * Wait at least 50 PCI cycles for the reset to
   1526 		 * complete before peeking at the Tulip again.
   1527 		 * 10 uSec is a bit longer than 50 PCI cycles
   1528 		 * (at 33MHz), but it doesn't hurt have the extra
   1529 		 * wait.
   1530 		 */
   1531 		delay(10);
   1532 		if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0)
   1533 			break;
   1534 	}
   1535 
   1536 	if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR))
   1537 		printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
   1538 
   1539 	delay(1000);
   1540 
   1541 	/*
   1542 	 * If the board has any GPIO reset sequences to issue, do them now.
   1543 	 */
   1544 	if (sc->sc_reset != NULL)
   1545 		(*sc->sc_reset)(sc);
   1546 }
   1547 
   1548 /*
   1549  * tlp_init:
   1550  *
   1551  *	Initialize the interface.  Must be called at splnet().
   1552  */
   1553 int
   1554 tlp_init(sc)
   1555 	struct tulip_softc *sc;
   1556 {
   1557 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1558 	struct tulip_txsoft *txs;
   1559 	struct tulip_rxsoft *rxs;
   1560 	int i, error = 0;
   1561 
   1562 	/*
   1563 	 * Cancel any pending I/O.
   1564 	 */
   1565 	tlp_stop(sc, 0);
   1566 
   1567 	/*
   1568 	 * Initialize `opmode' to 0, and call the pre-init routine, if
   1569 	 * any.  This is required because the 2114x and some of the
   1570 	 * clones require that the media-related bits in `opmode' be
   1571 	 * set before performing a soft-reset in order to get internal
   1572 	 * chip pathways are correct.  Yay!
   1573 	 */
   1574 	sc->sc_opmode = 0;
   1575 	if (sc->sc_preinit != NULL)
   1576 		(*sc->sc_preinit)(sc);
   1577 
   1578 	/*
   1579 	 * Reset the Tulip to a known state.
   1580 	 */
   1581 	tlp_reset(sc);
   1582 
   1583 	/*
   1584 	 * Initialize the BUSMODE register.
   1585 	 */
   1586 	sc->sc_busmode = BUSMODE_BAR;
   1587 	switch (sc->sc_chip) {
   1588 	case TULIP_CHIP_21140:
   1589 	case TULIP_CHIP_21140A:
   1590 	case TULIP_CHIP_21142:
   1591 	case TULIP_CHIP_21143:
   1592 	case TULIP_CHIP_82C115:
   1593 	case TULIP_CHIP_MX98725:
   1594 		/*
   1595 		 * If we're allowed to do so, use Memory Read Line
   1596 		 * and Memory Read Multiple.
   1597 		 *
   1598 		 * XXX Should we use Memory Write and Invalidate?
   1599 		 */
   1600 		if (sc->sc_flags & TULIPF_MRL)
   1601 			sc->sc_busmode |= BUSMODE_RLE;
   1602 		if (sc->sc_flags & TULIPF_MRM)
   1603 			sc->sc_busmode |= BUSMODE_RME;
   1604 #if 0
   1605 		if (sc->sc_flags & TULIPF_MWI)
   1606 			sc->sc_busmode |= BUSMODE_WLE;
   1607 #endif
   1608 
   1609 	default:
   1610 		/* Nothing. */
   1611 	}
   1612 	switch (sc->sc_cacheline) {
   1613 	default:
   1614 		/*
   1615 		 * Note: We must *always* set these bits; a cache
   1616 		 * alignment of 0 is RESERVED.
   1617 		 */
   1618 	case 8:
   1619 		sc->sc_busmode |= BUSMODE_CAL_8LW;
   1620 		break;
   1621 	case 16:
   1622 		sc->sc_busmode |= BUSMODE_CAL_16LW;
   1623 		break;
   1624 	case 32:
   1625 		sc->sc_busmode |= BUSMODE_CAL_32LW;
   1626 		break;
   1627 	}
   1628 	switch (sc->sc_chip) {
   1629 	case TULIP_CHIP_82C168:
   1630 	case TULIP_CHIP_82C169:
   1631 		sc->sc_busmode |= BUSMODE_PBL_16LW | BUSMODE_PNIC_MBO;
   1632 		break;
   1633 	default:
   1634 		sc->sc_busmode |= BUSMODE_PBL_DEFAULT;
   1635 		break;
   1636 	}
   1637 #if BYTE_ORDER == BIG_ENDIAN
   1638 	/*
   1639 	 * Can't use BUSMODE_BLE or BUSMODE_DBO; not all chips
   1640 	 * support them, and even on ones that do, it doesn't
   1641 	 * always work.
   1642 	 */
   1643 #endif
   1644 	TULIP_WRITE(sc, CSR_BUSMODE, sc->sc_busmode);
   1645 
   1646 	/*
   1647 	 * Initialize the OPMODE register.  We don't write it until
   1648 	 * we're ready to begin the transmit and receive processes.
   1649 	 *
   1650 	 * Media-related OPMODE bits are set in the media callbacks
   1651 	 * for each specific chip/board.
   1652 	 */
   1653 	sc->sc_opmode |= OPMODE_SR | OPMODE_ST |
   1654 	    sc->sc_txth[sc->sc_txthresh].txth_opmode;
   1655 
   1656 	/*
   1657 	 * Magical mystery initialization on the Macronix chips.
   1658 	 * The MX98713 uses its own magic value, the rest share
   1659 	 * a common one.
   1660 	 */
   1661 	switch (sc->sc_chip) {
   1662 	case TULIP_CHIP_MX98713:
   1663 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98713);
   1664 		break;
   1665 
   1666 	case TULIP_CHIP_MX98713A:
   1667 	case TULIP_CHIP_MX98715:
   1668 	case TULIP_CHIP_MX98715A:
   1669 	case TULIP_CHIP_MX98725:
   1670 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98715);
   1671 		break;
   1672 
   1673 	default:
   1674 		/* Nothing. */
   1675 	}
   1676 
   1677 	/*
   1678 	 * Initialize the transmit descriptor ring.
   1679 	 */
   1680 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
   1681 	for (i = 0; i < TULIP_NTXDESC; i++) {
   1682 		sc->sc_txdescs[i].td_ctl = htole32(sc->sc_tdctl_ch);
   1683 		sc->sc_txdescs[i].td_bufaddr2 =
   1684 		    htole32(TULIP_CDTXADDR(sc, TULIP_NEXTTX(i)));
   1685 	}
   1686 	sc->sc_txdescs[TULIP_NTXDESC - 1].td_ctl |= htole32(sc->sc_tdctl_er);
   1687 	TULIP_CDTXSYNC(sc, 0, TULIP_NTXDESC,
   1688 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1689 	sc->sc_txfree = TULIP_NTXDESC;
   1690 	sc->sc_txnext = 0;
   1691 
   1692 	/*
   1693 	 * Initialize the transmit job descriptors.
   1694 	 */
   1695 	SIMPLEQ_INIT(&sc->sc_txfreeq);
   1696 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
   1697 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
   1698 		txs = &sc->sc_txsoft[i];
   1699 		txs->txs_mbuf = NULL;
   1700 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1701 	}
   1702 
   1703 	/*
   1704 	 * Initialize the receive descriptor and receive job
   1705 	 * descriptor rings.
   1706 	 */
   1707 	for (i = 0; i < TULIP_NRXDESC; i++) {
   1708 		rxs = &sc->sc_rxsoft[i];
   1709 		if (rxs->rxs_mbuf == NULL) {
   1710 			if ((error = tlp_add_rxbuf(sc, i)) != 0) {
   1711 				printf("%s: unable to allocate or map rx "
   1712 				    "buffer %d, error = %d\n",
   1713 				    sc->sc_dev.dv_xname, i, error);
   1714 				/*
   1715 				 * XXX Should attempt to run with fewer receive
   1716 				 * XXX buffers instead of just failing.
   1717 				 */
   1718 				tlp_rxdrain(sc);
   1719 				goto out;
   1720 			}
   1721 		}
   1722 	}
   1723 	sc->sc_rxptr = 0;
   1724 
   1725 	/*
   1726 	 * Initialize the interrupt mask and enable interrupts.
   1727 	 */
   1728 	/* normal interrupts */
   1729 	sc->sc_inten =  STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
   1730 
   1731 	/* abnormal interrupts */
   1732 	sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
   1733 	    STATUS_RU | STATUS_RPS | STATUS_RWT | STATUS_SE | STATUS_AIS;
   1734 
   1735 	sc->sc_rxint_mask = STATUS_RI|STATUS_RU|STATUS_RWT;
   1736 	sc->sc_txint_mask = STATUS_TI|STATUS_UNF|STATUS_TJT;
   1737 
   1738 	switch (sc->sc_chip) {
   1739 	case TULIP_CHIP_WB89C840F:
   1740 		/*
   1741 		 * Clear bits that we don't want that happen to
   1742 		 * overlap or don't exist.
   1743 		 */
   1744 		sc->sc_inten &= ~(STATUS_WINB_REI|STATUS_RWT);
   1745 		break;
   1746 
   1747 	default:
   1748 		/* Nothing. */
   1749 	}
   1750 
   1751 	sc->sc_rxint_mask &= sc->sc_inten;
   1752 	sc->sc_txint_mask &= sc->sc_inten;
   1753 
   1754 	TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
   1755 	TULIP_WRITE(sc, CSR_STATUS, 0xffffffff);
   1756 
   1757 	/*
   1758 	 * Give the transmit and receive rings to the Tulip.
   1759 	 */
   1760 	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDTXADDR(sc, sc->sc_txnext));
   1761 	TULIP_WRITE(sc, CSR_RXLIST, TULIP_CDRXADDR(sc, sc->sc_rxptr));
   1762 
   1763 	/*
   1764 	 * On chips that do this differently, set the station address.
   1765 	 */
   1766 	switch (sc->sc_chip) {
   1767 	case TULIP_CHIP_WB89C840F:
   1768 	    {
   1769 		/* XXX Do this with stream writes? */
   1770 		bus_addr_t cpa = TULIP_CSR_OFFSET(sc, CSR_WINB_CPA0);
   1771 
   1772 		for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1773 			bus_space_write_1(sc->sc_st, sc->sc_sh,
   1774 			    cpa + i, LLADDR(ifp->if_sadl)[i]);
   1775 		}
   1776 		break;
   1777 	    }
   1778 
   1779 	case TULIP_CHIP_AL981:
   1780 	    {
   1781 		u_int32_t reg;
   1782 		u_int8_t *enaddr = LLADDR(ifp->if_sadl);
   1783 
   1784 		reg = enaddr[0] |
   1785 		      (enaddr[1] << 8) |
   1786 		      (enaddr[2] << 16) |
   1787 		      (enaddr[3] << 24);
   1788 		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR0, reg);
   1789 
   1790 		reg = enaddr[4] |
   1791 		      (enaddr[5] << 8);
   1792 		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR1, reg);
   1793 	    }
   1794 
   1795 	default:
   1796 		/* Nothing. */
   1797 	}
   1798 
   1799 	/*
   1800 	 * Set the receive filter.  This will start the transmit and
   1801 	 * receive processes.
   1802 	 */
   1803 	(*sc->sc_filter_setup)(sc);
   1804 
   1805 	/*
   1806 	 * Set the current media.
   1807 	 */
   1808 	(void) (*sc->sc_mediasw->tmsw_set)(sc);
   1809 
   1810 	/*
   1811 	 * Start the receive process.
   1812 	 */
   1813 	TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
   1814 
   1815 	if (sc->sc_tick != NULL) {
   1816 		/* Start the one second clock. */
   1817 		callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc);
   1818 	}
   1819 
   1820 	/*
   1821 	 * Note that the interface is now running.
   1822 	 */
   1823 	ifp->if_flags |= IFF_RUNNING;
   1824 	ifp->if_flags &= ~IFF_OACTIVE;
   1825 
   1826  out:
   1827 	if (error)
   1828 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1829 	return (error);
   1830 }
   1831 
   1832 /*
   1833  * tlp_enable:
   1834  *
   1835  *	Enable the Tulip chip.
   1836  */
   1837 int
   1838 tlp_enable(sc)
   1839 	struct tulip_softc *sc;
   1840 {
   1841 
   1842 	if (TULIP_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
   1843 		if ((*sc->sc_enable)(sc) != 0) {
   1844 			printf("%s: device enable failed\n",
   1845 			    sc->sc_dev.dv_xname);
   1846 			return (EIO);
   1847 		}
   1848 		sc->sc_flags |= TULIPF_ENABLED;
   1849 	}
   1850 	return (0);
   1851 }
   1852 
   1853 /*
   1854  * tlp_disable:
   1855  *
   1856  *	Disable the Tulip chip.
   1857  */
   1858 void
   1859 tlp_disable(sc)
   1860 	struct tulip_softc *sc;
   1861 {
   1862 
   1863 	if (TULIP_IS_ENABLED(sc) && sc->sc_disable != NULL) {
   1864 		(*sc->sc_disable)(sc);
   1865 		sc->sc_flags &= ~TULIPF_ENABLED;
   1866 	}
   1867 }
   1868 
   1869 /*
   1870  * tlp_power:
   1871  *
   1872  *	Power management (suspend/resume) hook.
   1873  */
   1874 void
   1875 tlp_power(why, arg)
   1876 	int why;
   1877 	void *arg;
   1878 {
   1879 	struct tulip_softc *sc = arg;
   1880 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1881 	int s;
   1882 
   1883 	s = splnet();
   1884 	if (why != PWR_RESUME) {
   1885 		tlp_stop(sc, 0);
   1886 		if (sc->sc_power != NULL)
   1887 			(*sc->sc_power)(sc, why);
   1888 	} else if (ifp->if_flags & IFF_UP) {
   1889 		if (sc->sc_power != NULL)
   1890 			(*sc->sc_power)(sc, why);
   1891 		tlp_init(sc);
   1892 	}
   1893 	splx(s);
   1894 }
   1895 
   1896 /*
   1897  * tlp_rxdrain:
   1898  *
   1899  *	Drain the receive queue.
   1900  */
   1901 void
   1902 tlp_rxdrain(sc)
   1903 	struct tulip_softc *sc;
   1904 {
   1905 	struct tulip_rxsoft *rxs;
   1906 	int i;
   1907 
   1908 	for (i = 0; i < TULIP_NRXDESC; i++) {
   1909 		rxs = &sc->sc_rxsoft[i];
   1910 		if (rxs->rxs_mbuf != NULL) {
   1911 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1912 			m_freem(rxs->rxs_mbuf);
   1913 			rxs->rxs_mbuf = NULL;
   1914 		}
   1915 	}
   1916 }
   1917 
   1918 /*
   1919  * tlp_stop:
   1920  *
   1921  *	Stop transmission on the interface.
   1922  */
   1923 void
   1924 tlp_stop(sc, drain)
   1925 	struct tulip_softc *sc;
   1926 	int drain;
   1927 {
   1928 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1929 	struct tulip_txsoft *txs;
   1930 
   1931 	if (sc->sc_tick != NULL) {
   1932 		/* Stop the one second clock. */
   1933 		callout_stop(&sc->sc_tick_callout);
   1934 	}
   1935 
   1936 	if (sc->sc_flags & TULIPF_HAS_MII) {
   1937 		/* Down the MII. */
   1938 		mii_down(&sc->sc_mii);
   1939 	}
   1940 
   1941 	/* Disable interrupts. */
   1942 	TULIP_WRITE(sc, CSR_INTEN, 0);
   1943 
   1944 	/* Stop the transmit and receive processes. */
   1945 	TULIP_WRITE(sc, CSR_OPMODE, 0);
   1946 	TULIP_WRITE(sc, CSR_RXLIST, 0);
   1947 	TULIP_WRITE(sc, CSR_TXLIST, 0);
   1948 
   1949 	/*
   1950 	 * Release any queued transmit buffers.
   1951 	 */
   1952 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1953 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
   1954 		if (txs->txs_mbuf != NULL) {
   1955 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1956 			m_freem(txs->txs_mbuf);
   1957 			txs->txs_mbuf = NULL;
   1958 		}
   1959 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1960 	}
   1961 
   1962 	if (drain) {
   1963 		/*
   1964 		 * Release the receive buffers.
   1965 		 */
   1966 		tlp_rxdrain(sc);
   1967 	}
   1968 
   1969 	sc->sc_flags &= ~(TULIPF_WANT_SETUP|TULIPF_DOING_SETUP);
   1970 
   1971 	/*
   1972 	 * Mark the interface down and cancel the watchdog timer.
   1973 	 */
   1974 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1975 	ifp->if_timer = 0;
   1976 }
   1977 
   1978 #define	SROM_EMIT(sc, x)						\
   1979 do {									\
   1980 	TULIP_WRITE((sc), CSR_MIIROM, (x));				\
   1981 	delay(2);							\
   1982 } while (0)
   1983 
   1984 /*
   1985  * tlp_srom_idle:
   1986  *
   1987  *	Put the SROM in idle state.
   1988  */
   1989 void
   1990 tlp_srom_idle(sc)
   1991 	struct tulip_softc *sc;
   1992 {
   1993 	u_int32_t miirom;
   1994 	int i;
   1995 
   1996 	miirom = MIIROM_SR;
   1997 	SROM_EMIT(sc, miirom);
   1998 
   1999 	miirom |= MIIROM_RD;
   2000 	SROM_EMIT(sc, miirom);
   2001 
   2002 	miirom |= MIIROM_SROMCS;
   2003 	SROM_EMIT(sc, miirom);
   2004 
   2005 	SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2006 
   2007 	/* Strobe the clock 32 times. */
   2008 	for (i = 0; i < 32; i++) {
   2009 		SROM_EMIT(sc, miirom);
   2010 		SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2011 	}
   2012 
   2013 	SROM_EMIT(sc, miirom);
   2014 
   2015 	miirom &= ~MIIROM_SROMCS;
   2016 	SROM_EMIT(sc, miirom);
   2017 
   2018 	SROM_EMIT(sc, 0);
   2019 }
   2020 
   2021 /*
   2022  * tlp_srom_size:
   2023  *
   2024  *	Determine the number of address bits in the SROM.
   2025  */
   2026 int
   2027 tlp_srom_size(sc)
   2028 	struct tulip_softc *sc;
   2029 {
   2030 	u_int32_t miirom;
   2031 	int x;
   2032 
   2033 	/* Select the SROM. */
   2034 	miirom = MIIROM_SR;
   2035 	SROM_EMIT(sc, miirom);
   2036 
   2037 	miirom |= MIIROM_RD;
   2038 	SROM_EMIT(sc, miirom);
   2039 
   2040 	/* Send CHIP SELECT for one clock tick. */
   2041 	miirom |= MIIROM_SROMCS;
   2042 	SROM_EMIT(sc, miirom);
   2043 
   2044 	/* Shift in the READ opcode. */
   2045 	for (x = 3; x > 0; x--) {
   2046 		if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
   2047 			miirom |= MIIROM_SROMDI;
   2048 		else
   2049 			miirom &= ~MIIROM_SROMDI;
   2050 		SROM_EMIT(sc, miirom);
   2051 		SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2052 		SROM_EMIT(sc, miirom);
   2053 	}
   2054 
   2055 	/* Shift in address and look for dummy 0 bit. */
   2056 	for (x = 1; x <= 12; x++) {
   2057 		miirom &= ~MIIROM_SROMDI;
   2058 		SROM_EMIT(sc, miirom);
   2059 		SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2060 		if (!TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
   2061 			break;
   2062 		SROM_EMIT(sc, miirom);
   2063 	}
   2064 
   2065 	/* Clear CHIP SELECT. */
   2066 	miirom &= ~MIIROM_SROMCS;
   2067 	SROM_EMIT(sc, miirom);
   2068 
   2069 	/* Deselect the SROM. */
   2070 	SROM_EMIT(sc, 0);
   2071 
   2072 	if (x < 4 || x > 12) {
   2073 		printf("%s: broken MicroWire interface detected; "
   2074 		    "setting SROM size to 1Kb\n", sc->sc_dev.dv_xname);
   2075 		return (6);
   2076 	} else {
   2077 		if (tlp_srom_debug)
   2078 			printf("%s: SROM size is 2^%d*16 bits (%d bytes)\n",
   2079 			    sc->sc_dev.dv_xname, x, (1 << (x + 4)) >> 3);
   2080 		return (x);
   2081 	}
   2082 }
   2083 
   2084 /*
   2085  * tlp_read_srom:
   2086  *
   2087  *	Read the Tulip SROM.
   2088  */
   2089 int
   2090 tlp_read_srom(sc)
   2091 	struct tulip_softc *sc;
   2092 {
   2093 	int size;
   2094 	u_int32_t miirom;
   2095 	u_int16_t datain;
   2096 	int i, x;
   2097 
   2098 	tlp_srom_idle(sc);
   2099 
   2100 	sc->sc_srom_addrbits = tlp_srom_size(sc);
   2101 	if (sc->sc_srom_addrbits == 0)
   2102 		return (0);
   2103 	size = TULIP_ROM_SIZE(sc->sc_srom_addrbits);
   2104 	sc->sc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
   2105 
   2106 	/* Select the SROM. */
   2107 	miirom = MIIROM_SR;
   2108 	SROM_EMIT(sc, miirom);
   2109 
   2110 	miirom |= MIIROM_RD;
   2111 	SROM_EMIT(sc, miirom);
   2112 
   2113 	for (i = 0; i < size; i += 2) {
   2114 		/* Send CHIP SELECT for one clock tick. */
   2115 		miirom |= MIIROM_SROMCS;
   2116 		SROM_EMIT(sc, miirom);
   2117 
   2118 		/* Shift in the READ opcode. */
   2119 		for (x = 3; x > 0; x--) {
   2120 			if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
   2121 				miirom |= MIIROM_SROMDI;
   2122 			else
   2123 				miirom &= ~MIIROM_SROMDI;
   2124 			SROM_EMIT(sc, miirom);
   2125 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2126 			SROM_EMIT(sc, miirom);
   2127 		}
   2128 
   2129 		/* Shift in address. */
   2130 		for (x = sc->sc_srom_addrbits; x > 0; x--) {
   2131 			if (i & (1 << x))
   2132 				miirom |= MIIROM_SROMDI;
   2133 			else
   2134 				miirom &= ~MIIROM_SROMDI;
   2135 			SROM_EMIT(sc, miirom);
   2136 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2137 			SROM_EMIT(sc, miirom);
   2138 		}
   2139 
   2140 		/* Shift out data. */
   2141 		miirom &= ~MIIROM_SROMDI;
   2142 		datain = 0;
   2143 		for (x = 16; x > 0; x--) {
   2144 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2145 			if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
   2146 				datain |= (1 << (x - 1));
   2147 			SROM_EMIT(sc, miirom);
   2148 		}
   2149 		sc->sc_srom[i] = datain & 0xff;
   2150 		sc->sc_srom[i + 1] = datain >> 8;
   2151 
   2152 		/* Clear CHIP SELECT. */
   2153 		miirom &= ~MIIROM_SROMCS;
   2154 		SROM_EMIT(sc, miirom);
   2155 	}
   2156 
   2157 	/* Deselect the SROM. */
   2158 	SROM_EMIT(sc, 0);
   2159 
   2160 	/* ...and idle it. */
   2161 	tlp_srom_idle(sc);
   2162 
   2163 	if (tlp_srom_debug) {
   2164 		printf("SROM CONTENTS:");
   2165 		for (i = 0; i < size; i++) {
   2166 			if ((i % 8) == 0)
   2167 				printf("\n\t");
   2168 			printf("0x%02x ", sc->sc_srom[i]);
   2169 		}
   2170 		printf("\n");
   2171 	}
   2172 
   2173 	return (1);
   2174 }
   2175 
   2176 #undef SROM_EMIT
   2177 
   2178 /*
   2179  * tlp_add_rxbuf:
   2180  *
   2181  *	Add a receive buffer to the indicated descriptor.
   2182  */
   2183 int
   2184 tlp_add_rxbuf(sc, idx)
   2185 	struct tulip_softc *sc;
   2186 	int idx;
   2187 {
   2188 	struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx];
   2189 	struct mbuf *m;
   2190 	int error;
   2191 
   2192 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2193 	if (m == NULL)
   2194 		return (ENOBUFS);
   2195 
   2196 	MCLGET(m, M_DONTWAIT);
   2197 	if ((m->m_flags & M_EXT) == 0) {
   2198 		m_freem(m);
   2199 		return (ENOBUFS);
   2200 	}
   2201 
   2202 	if (rxs->rxs_mbuf != NULL)
   2203 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   2204 
   2205 	rxs->rxs_mbuf = m;
   2206 
   2207 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
   2208 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
   2209 	if (error) {
   2210 		printf("%s: can't load rx DMA map %d, error = %d\n",
   2211 		    sc->sc_dev.dv_xname, idx, error);
   2212 		panic("tlp_add_rxbuf");	/* XXX */
   2213 	}
   2214 
   2215 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   2216 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   2217 
   2218 	TULIP_INIT_RXDESC(sc, idx);
   2219 
   2220 	return (0);
   2221 }
   2222 
   2223 /*
   2224  * tlp_crc32:
   2225  *
   2226  *	Compute the 32-bit CRC of the provided buffer.
   2227  */
   2228 u_int32_t
   2229 tlp_crc32(buf, len)
   2230 	const u_int8_t *buf;
   2231 	size_t len;
   2232 {
   2233 	static const u_int32_t crctab[] = {
   2234 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   2235 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   2236 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   2237 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   2238 	};
   2239 	u_int32_t crc;
   2240 	int i;
   2241 
   2242 	crc = 0xffffffff;
   2243 	for (i = 0; i < len; i++) {
   2244 		crc ^= buf[i];
   2245 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   2246 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   2247 	}
   2248 	return (crc);
   2249 }
   2250 
   2251 /*
   2252  * tlp_srom_crcok:
   2253  *
   2254  *	Check the CRC of the Tulip SROM.
   2255  */
   2256 int
   2257 tlp_srom_crcok(romdata)
   2258 	const u_int8_t *romdata;
   2259 {
   2260 	u_int32_t crc;
   2261 
   2262 	crc = tlp_crc32(romdata, TULIP_ROM_CRC32_CHECKSUM);
   2263 	crc = (crc & 0xffff) ^ 0xffff;
   2264 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM))
   2265 		return (1);
   2266 
   2267 	/*
   2268 	 * Try an alternate checksum.
   2269 	 */
   2270 	crc = tlp_crc32(romdata, TULIP_ROM_CRC32_CHECKSUM1);
   2271 	crc = (crc & 0xffff) ^ 0xffff;
   2272 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM1))
   2273 		return (1);
   2274 
   2275 	return (0);
   2276 }
   2277 
   2278 /*
   2279  * tlp_isv_srom:
   2280  *
   2281  *	Check to see if the SROM is in the new standardized format.
   2282  */
   2283 int
   2284 tlp_isv_srom(romdata)
   2285 	const u_int8_t *romdata;
   2286 {
   2287 	int i;
   2288 	u_int16_t cksum;
   2289 
   2290 	if (tlp_srom_crcok(romdata)) {
   2291 		/*
   2292 		 * SROM CRC checks out; must be in the new format.
   2293 		 */
   2294 		return (1);
   2295 	}
   2296 
   2297 	cksum = TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM);
   2298 	if (cksum == 0xffff || cksum == 0) {
   2299 		/*
   2300 		 * No checksum present.  Check the SROM ID; 18 bytes of 0
   2301 		 * followed by 1 (version) followed by the number of
   2302 		 * adapters which use this SROM (should be non-zero).
   2303 		 */
   2304 		for (i = 0; i < TULIP_ROM_SROM_FORMAT_VERION; i++) {
   2305 			if (romdata[i] != 0)
   2306 				return (0);
   2307 		}
   2308 		if (romdata[TULIP_ROM_SROM_FORMAT_VERION] != 1)
   2309 			return (0);
   2310 		if (romdata[TULIP_ROM_CHIP_COUNT] == 0)
   2311 			return (0);
   2312 		return (1);
   2313 	}
   2314 
   2315 	return (0);
   2316 }
   2317 
   2318 /*
   2319  * tlp_isv_srom_enaddr:
   2320  *
   2321  *	Get the Ethernet address from an ISV SROM.
   2322  */
   2323 int
   2324 tlp_isv_srom_enaddr(sc, enaddr)
   2325 	struct tulip_softc *sc;
   2326 	u_int8_t *enaddr;
   2327 {
   2328 	int i, devcnt;
   2329 
   2330 	if (tlp_isv_srom(sc->sc_srom) == 0)
   2331 		return (0);
   2332 
   2333 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   2334 	for (i = 0; i < devcnt; i++) {
   2335 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   2336 			break;
   2337 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   2338 		    sc->sc_devno)
   2339 			break;
   2340 	}
   2341 
   2342 	if (i == devcnt)
   2343 		return (0);
   2344 
   2345 	memcpy(enaddr, &sc->sc_srom[TULIP_ROM_IEEE_NETWORK_ADDRESS],
   2346 	    ETHER_ADDR_LEN);
   2347 	enaddr[5] += i;
   2348 
   2349 	return (1);
   2350 }
   2351 
   2352 /*
   2353  * tlp_parse_old_srom:
   2354  *
   2355  *	Parse old-format SROMs.
   2356  *
   2357  *	This routine is largely lifted from Matt Thomas's `de' driver.
   2358  */
   2359 int
   2360 tlp_parse_old_srom(sc, enaddr)
   2361 	struct tulip_softc *sc;
   2362 	u_int8_t *enaddr;
   2363 {
   2364 	static const u_int8_t testpat[] =
   2365 	    { 0xff, 0, 0x55, 0xaa, 0xff, 0, 0x55, 0xaa };
   2366 	int i;
   2367 	u_int32_t cksum;
   2368 
   2369 	if (memcmp(&sc->sc_srom[0], &sc->sc_srom[16], 8) != 0) {
   2370 		/*
   2371 		 * Some vendors (e.g. ZNYX) don't use the standard
   2372 		 * DEC Address ROM format, but rather just have an
   2373 		 * Ethernet address in the first 6 bytes, maybe a
   2374 		 * 2 byte checksum, and then all 0xff's.
   2375 		 *
   2376 		 * On the other hand, Cobalt Networks interfaces
   2377 		 * simply have the address in the first six bytes
   2378 		 * with the rest zeroed out.
   2379 		 */
   2380 		for (i = 8; i < 32; i++) {
   2381 			if (sc->sc_srom[i] != 0xff &&
   2382 			    sc->sc_srom[i] != 0)
   2383 				return (0);
   2384 		}
   2385 
   2386 		/*
   2387 		 * Sanity check the Ethernet address:
   2388 		 *
   2389 		 *	- Make sure it's not multicast or locally
   2390 		 *	  assigned
   2391 		 *	- Make sure it has a non-0 OUI
   2392 		 */
   2393 		if (sc->sc_srom[0] & 3)
   2394 			return (0);
   2395 		if (sc->sc_srom[0] == 0 && sc->sc_srom[1] == 0 &&
   2396 		    sc->sc_srom[2] == 0)
   2397 			return (0);
   2398 
   2399 		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   2400 		return (1);
   2401 	}
   2402 
   2403 	/*
   2404 	 * Standard DEC Address ROM test.
   2405 	 */
   2406 
   2407 	if (memcmp(&sc->sc_srom[24], testpat, 8) != 0)
   2408 		return (0);
   2409 
   2410 	for (i = 0; i < 8; i++) {
   2411 		if (sc->sc_srom[i] != sc->sc_srom[15 - i])
   2412 			return (0);
   2413 	}
   2414 
   2415 	memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   2416 
   2417 	cksum = *(u_int16_t *) &enaddr[0];
   2418 
   2419 	cksum <<= 1;
   2420 	if (cksum > 0xffff)
   2421 		cksum -= 0xffff;
   2422 
   2423 	cksum += *(u_int16_t *) &enaddr[2];
   2424 	if (cksum > 0xffff)
   2425 		cksum -= 0xffff;
   2426 
   2427 	cksum <<= 1;
   2428 	if (cksum > 0xffff)
   2429 		cksum -= 0xffff;
   2430 
   2431 	cksum += *(u_int16_t *) &enaddr[4];
   2432 	if (cksum >= 0xffff)
   2433 		cksum -= 0xffff;
   2434 
   2435 	if (cksum != *(u_int16_t *) &sc->sc_srom[6])
   2436 		return (0);
   2437 
   2438 	return (1);
   2439 }
   2440 
   2441 /*
   2442  * tlp_filter_setup:
   2443  *
   2444  *	Set the Tulip's receive filter.
   2445  */
   2446 void
   2447 tlp_filter_setup(sc)
   2448 	struct tulip_softc *sc;
   2449 {
   2450 	struct ethercom *ec = &sc->sc_ethercom;
   2451 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2452 	struct ether_multi *enm;
   2453 	struct ether_multistep step;
   2454 	__volatile u_int32_t *sp;
   2455 	struct tulip_txsoft *txs;
   2456 	u_int8_t enaddr[ETHER_ADDR_LEN];
   2457 	u_int32_t hash, hashsize;
   2458 	int cnt;
   2459 
   2460 	DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n",
   2461 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2462 
   2463 	memcpy(enaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   2464 
   2465 	/*
   2466 	 * If there are transmissions pending, wait until they have
   2467 	 * completed.
   2468 	 */
   2469 	if (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL ||
   2470 	    (sc->sc_flags & TULIPF_DOING_SETUP) != 0) {
   2471 		sc->sc_flags |= TULIPF_WANT_SETUP;
   2472 		DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n",
   2473 		    sc->sc_dev.dv_xname));
   2474 		return;
   2475 	}
   2476 	sc->sc_flags &= ~TULIPF_WANT_SETUP;
   2477 
   2478 	switch (sc->sc_chip) {
   2479 	case TULIP_CHIP_82C115:
   2480 		hashsize = TULIP_PNICII_HASHSIZE;
   2481 		break;
   2482 
   2483 	default:
   2484 		hashsize = TULIP_MCHASHSIZE;
   2485 	}
   2486 
   2487 	/*
   2488 	 * If we're running, idle the transmit and receive engines.  If
   2489 	 * we're NOT running, we're being called from tlp_init(), and our
   2490 	 * writing OPMODE will start the transmit and receive processes
   2491 	 * in motion.
   2492 	 */
   2493 	if (ifp->if_flags & IFF_RUNNING) {
   2494 		/*
   2495 		 * Actually, some chips seem to need a really hard
   2496 		 * kick in the head for this to work.  The genuine
   2497 		 * DEC chips can just be idled, but some of the
   2498 		 * clones seem to REALLY want a reset here.  Doing
   2499 		 * the reset will end up here again, but with
   2500 		 * IFF_RUNNING cleared.
   2501 		 */
   2502 		switch (sc->sc_chip) {
   2503 		case TULIP_CHIP_82C168:
   2504 		case TULIP_CHIP_82C169:
   2505 			tlp_init(sc);
   2506 			return;
   2507 
   2508 		default:
   2509 			tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2510 		}
   2511 	}
   2512 
   2513 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
   2514 
   2515 	if (ifp->if_flags & IFF_PROMISC) {
   2516 		sc->sc_opmode |= OPMODE_PR;
   2517 		goto allmulti;
   2518 	}
   2519 
   2520 	/*
   2521 	 * Try Perfect filtering first.
   2522 	 */
   2523 
   2524 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   2525 	sp = TULIP_CDSP(sc);
   2526 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2527 	cnt = 0;
   2528 	ETHER_FIRST_MULTI(step, ec, enm);
   2529 	while (enm != NULL) {
   2530 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2531 			/*
   2532 			 * We must listen to a range of multicast addresses.
   2533 			 * For now, just accept all multicasts, rather than
   2534 			 * trying to set only those filter bits needed to match
   2535 			 * the range.  (At this time, the only use of address
   2536 			 * ranges is for IP multicast routing, for which the
   2537 			 * range is big enough to require all bits set.)
   2538 			 */
   2539 			goto allmulti;
   2540 		}
   2541 		if (cnt == (TULIP_MAXADDRS - 2)) {
   2542 			/*
   2543 			 * We already have our multicast limit (still need
   2544 			 * our station address and broadcast).  Go to
   2545 			 * Hash-Perfect mode.
   2546 			 */
   2547 			goto hashperfect;
   2548 		}
   2549 		cnt++;
   2550 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 0);
   2551 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 1);
   2552 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 2);
   2553 		ETHER_NEXT_MULTI(step, enm);
   2554 	}
   2555 
   2556 	if (ifp->if_flags & IFF_BROADCAST) {
   2557 		/* ...and the broadcast address. */
   2558 		cnt++;
   2559 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2560 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2561 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2562 	}
   2563 
   2564 	/* Pad the rest with our station address. */
   2565 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   2566 		*sp++ = TULIP_SP_FIELD(enaddr, 0);
   2567 		*sp++ = TULIP_SP_FIELD(enaddr, 1);
   2568 		*sp++ = TULIP_SP_FIELD(enaddr, 2);
   2569 	}
   2570 	ifp->if_flags &= ~IFF_ALLMULTI;
   2571 	goto setit;
   2572 
   2573  hashperfect:
   2574 	/*
   2575 	 * Try Hash-Perfect mode.
   2576 	 */
   2577 
   2578 	/*
   2579 	 * Some 21140 chips have broken Hash-Perfect modes.  On these
   2580 	 * chips, we simply use Hash-Only mode, and put our station
   2581 	 * address into the filter.
   2582 	 */
   2583 	if (sc->sc_chip == TULIP_CHIP_21140)
   2584 		sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY;
   2585 	else
   2586 		sc->sc_filtmode = TDCTL_Tx_FT_HASH;
   2587 	sp = TULIP_CDSP(sc);
   2588 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2589 	ETHER_FIRST_MULTI(step, ec, enm);
   2590 	while (enm != NULL) {
   2591 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2592 			/*
   2593 			 * We must listen to a range of multicast addresses.
   2594 			 * For now, just accept all multicasts, rather than
   2595 			 * trying to set only those filter bits needed to match
   2596 			 * the range.  (At this time, the only use of address
   2597 			 * ranges is for IP multicast routing, for which the
   2598 			 * range is big enough to require all bits set.)
   2599 			 */
   2600 			goto allmulti;
   2601 		}
   2602 		hash = tlp_mchash(enm->enm_addrlo, hashsize);
   2603 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2604 		ETHER_NEXT_MULTI(step, enm);
   2605 	}
   2606 
   2607 	if (ifp->if_flags & IFF_BROADCAST) {
   2608 		/* ...and the broadcast address. */
   2609 		hash = tlp_mchash(etherbroadcastaddr, hashsize);
   2610 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2611 	}
   2612 
   2613 	if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
   2614 		/* ...and our station address. */
   2615 		hash = tlp_mchash(enaddr, hashsize);
   2616 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2617 	} else {
   2618 		/*
   2619 		 * Hash-Perfect mode; put our station address after
   2620 		 * the hash table.
   2621 		 */
   2622 		sp[39] = TULIP_SP_FIELD(enaddr, 0);
   2623 		sp[40] = TULIP_SP_FIELD(enaddr, 1);
   2624 		sp[41] = TULIP_SP_FIELD(enaddr, 2);
   2625 	}
   2626 	ifp->if_flags &= ~IFF_ALLMULTI;
   2627 	goto setit;
   2628 
   2629  allmulti:
   2630 	/*
   2631 	 * Use Perfect filter mode.  First address is the broadcast address,
   2632 	 * and pad the rest with our station address.  We'll set Pass-all-
   2633 	 * multicast in OPMODE below.
   2634 	 */
   2635 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   2636 	sp = TULIP_CDSP(sc);
   2637 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2638 	cnt = 0;
   2639 	if (ifp->if_flags & IFF_BROADCAST) {
   2640 		cnt++;
   2641 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2642 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2643 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2644 	}
   2645 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   2646 		*sp++ = TULIP_SP_FIELD(enaddr, 0);
   2647 		*sp++ = TULIP_SP_FIELD(enaddr, 1);
   2648 		*sp++ = TULIP_SP_FIELD(enaddr, 2);
   2649 	}
   2650 	ifp->if_flags |= IFF_ALLMULTI;
   2651 
   2652  setit:
   2653 	if (ifp->if_flags & IFF_ALLMULTI)
   2654 		sc->sc_opmode |= OPMODE_PM;
   2655 
   2656 	/* Sync the setup packet buffer. */
   2657 	TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE);
   2658 
   2659 	/*
   2660 	 * Fill in the setup packet descriptor.
   2661 	 */
   2662 	txs = SIMPLEQ_FIRST(&sc->sc_txfreeq);
   2663 
   2664 	txs->txs_firstdesc = sc->sc_txnext;
   2665 	txs->txs_lastdesc = sc->sc_txnext;
   2666 	txs->txs_ndescs = 1;
   2667 	txs->txs_mbuf = NULL;
   2668 
   2669 	sc->sc_txdescs[sc->sc_txnext].td_bufaddr1 =
   2670 	    htole32(TULIP_CDSPADDR(sc));
   2671 	sc->sc_txdescs[sc->sc_txnext].td_ctl =
   2672 	    htole32((TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) |
   2673 	    sc->sc_filtmode | TDCTL_Tx_SET | TDCTL_Tx_FS |
   2674 	    TDCTL_Tx_LS | TDCTL_Tx_IC | sc->sc_tdctl_ch |
   2675 	    (sc->sc_txnext == (TULIP_NTXDESC - 1) ? sc->sc_tdctl_er : 0));
   2676 	sc->sc_txdescs[sc->sc_txnext].td_status = htole32(TDSTAT_OWN);
   2677 	TULIP_CDTXSYNC(sc, sc->sc_txnext, txs->txs_ndescs,
   2678 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   2679 
   2680 	/* Advance the tx pointer. */
   2681 	sc->sc_txfree -= 1;
   2682 	sc->sc_txnext = TULIP_NEXTTX(sc->sc_txnext);
   2683 
   2684 	SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
   2685 	SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
   2686 
   2687 	/*
   2688 	 * Set the OPMODE register.  This will also resume the
   2689 	 * transmit transmit process we idled above.
   2690 	 */
   2691 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2692 
   2693 	sc->sc_flags |= TULIPF_DOING_SETUP;
   2694 
   2695 	/*
   2696 	 * Kick the transmitter; this will cause the Tulip to
   2697 	 * read the setup descriptor.
   2698 	 */
   2699 	/* XXX USE AUTOPOLLING? */
   2700 	TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
   2701 
   2702 	/* Set up a watchdog timer in case the chip flakes out. */
   2703 	ifp->if_timer = 5;
   2704 
   2705 	DPRINTF(sc, ("%s: tlp_filter_setup: returning\n", sc->sc_dev.dv_xname));
   2706 }
   2707 
   2708 /*
   2709  * tlp_winb_filter_setup:
   2710  *
   2711  *	Set the Winbond 89C840F's receive filter.
   2712  */
   2713 void
   2714 tlp_winb_filter_setup(sc)
   2715 	struct tulip_softc *sc;
   2716 {
   2717 	struct ethercom *ec = &sc->sc_ethercom;
   2718 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2719 	struct ether_multi *enm;
   2720 	struct ether_multistep step;
   2721 	u_int32_t hash, mchash[2];
   2722 
   2723 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n",
   2724 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2725 
   2726 	sc->sc_opmode &= ~(OPMODE_WINB_APP|OPMODE_WINB_AMP|OPMODE_WINB_ABP);
   2727 
   2728 	if (ifp->if_flags & IFF_MULTICAST)
   2729 		sc->sc_opmode |= OPMODE_WINB_AMP;
   2730 
   2731 	if (ifp->if_flags & IFF_BROADCAST)
   2732 		sc->sc_opmode |= OPMODE_WINB_ABP;
   2733 
   2734 	if (ifp->if_flags & IFF_PROMISC) {
   2735 		sc->sc_opmode |= OPMODE_WINB_APP;
   2736 		goto allmulti;
   2737 	}
   2738 
   2739 	mchash[0] = mchash[1] = 0;
   2740 
   2741 	ETHER_FIRST_MULTI(step, ec, enm);
   2742 	while (enm != NULL) {
   2743 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2744 			/*
   2745 			 * We must listen to a range of multicast addresses.
   2746 			 * For now, just accept all multicasts, rather than
   2747 			 * trying to set only those filter bits needed to match
   2748 			 * the range.  (At this time, the only use of address
   2749 			 * ranges is for IP multicast routing, for which the
   2750 			 * range is big enough to require all bits set.)
   2751 			 */
   2752 			goto allmulti;
   2753 		}
   2754 
   2755 		/*
   2756 		 * According to the FreeBSD `wb' driver, yes, you
   2757 		 * really do invert the hash.
   2758 		 */
   2759 		hash = (~(tlp_crc32(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26))
   2760 		    & 0x3f;
   2761 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
   2762 		ETHER_NEXT_MULTI(step, enm);
   2763 	}
   2764 	ifp->if_flags &= ~IFF_ALLMULTI;
   2765 	goto setit;
   2766 
   2767  allmulti:
   2768 	ifp->if_flags |= IFF_ALLMULTI;
   2769 	mchash[0] = mchash[1] = 0xffffffff;
   2770 
   2771  setit:
   2772 	TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]);
   2773 	TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]);
   2774 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2775 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n",
   2776 	    sc->sc_dev.dv_xname));
   2777 }
   2778 
   2779 /*
   2780  * tlp_al981_filter_setup:
   2781  *
   2782  *	Set the ADMtek AL981's receive filter.
   2783  */
   2784 void
   2785 tlp_al981_filter_setup(sc)
   2786 	struct tulip_softc *sc;
   2787 {
   2788 	struct ethercom *ec = &sc->sc_ethercom;
   2789 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2790 	struct ether_multi *enm;
   2791 	struct ether_multistep step;
   2792 	u_int32_t hash, mchash[2];
   2793 
   2794 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: sc_flags 0x%08x\n",
   2795 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2796 
   2797 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2798 
   2799 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
   2800 
   2801 	if (ifp->if_flags & IFF_PROMISC) {
   2802 		sc->sc_opmode |= OPMODE_PR;
   2803 		goto allmulti;
   2804 	}
   2805 
   2806 	mchash[0] = mchash[1] = 0;
   2807 
   2808 	ETHER_FIRST_MULTI(step, ec, enm);
   2809 	while (enm != NULL) {
   2810 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2811 			/*
   2812 			 * We must listen to a range of multicast addresses.
   2813 			 * For now, just accept all multicasts, rather than
   2814 			 * trying to set only those filter bits needed to match
   2815 			 * the range.  (At this time, the only use of address
   2816 			 * ranges is for IP multicast routing, for which the
   2817 			 * range is big enough to require all bits set.)
   2818 			 */
   2819 			goto allmulti;
   2820 		}
   2821 
   2822 		hash = (tlp_crc32(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26)
   2823 		    & 0x3f;
   2824 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
   2825 		ETHER_NEXT_MULTI(step, enm);
   2826 	}
   2827 	ifp->if_flags &= ~IFF_ALLMULTI;
   2828 	goto setit;
   2829 
   2830  allmulti:
   2831 	ifp->if_flags |= IFF_ALLMULTI;
   2832 	mchash[0] = mchash[1] = 0xffffffff;
   2833 
   2834  setit:
   2835 	TULIP_WRITE(sc, CSR_ADM_MAR0, mchash[0]);
   2836 	TULIP_WRITE(sc, CSR_ADM_MAR1, mchash[1]);
   2837 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2838 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: returning\n",
   2839 	    sc->sc_dev.dv_xname));
   2840 }
   2841 
   2842 /*
   2843  * tlp_idle:
   2844  *
   2845  *	Cause the transmit and/or receive processes to go idle.
   2846  */
   2847 void
   2848 tlp_idle(sc, bits)
   2849 	struct tulip_softc *sc;
   2850 	u_int32_t bits;
   2851 {
   2852 	static const char *tx_state_names[] = {
   2853 		"STOPPED",
   2854 		"RUNNING - FETCH",
   2855 		"RUNNING - WAIT",
   2856 		"RUNNING - READING",
   2857 		"-- RESERVED --",
   2858 		"RUNNING - SETUP",
   2859 		"SUSPENDED",
   2860 		"RUNNING - CLOSE",
   2861 	};
   2862 	static const char *rx_state_names[] = {
   2863 		"STOPPED",
   2864 		"RUNNING - FETCH",
   2865 		"RUNNING - CHECK",
   2866 		"RUNNING - WAIT",
   2867 		"SUSPENDED",
   2868 		"RUNNING - CLOSE",
   2869 		"RUNNING - FLUSH",
   2870 		"RUNNING - QUEUE",
   2871 	};
   2872 	u_int32_t csr, ackmask = 0;
   2873 	int i;
   2874 
   2875 	if (bits & OPMODE_ST)
   2876 		ackmask |= STATUS_TPS;
   2877 
   2878 	if (bits & OPMODE_SR)
   2879 		ackmask |= STATUS_RPS;
   2880 
   2881 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits);
   2882 
   2883 	for (i = 0; i < 1000; i++) {
   2884 		if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
   2885 			break;
   2886 		delay(10);
   2887 	}
   2888 
   2889 	csr = TULIP_READ(sc, CSR_STATUS);
   2890 	if ((csr & ackmask) != ackmask) {
   2891 		if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
   2892 		    (csr & STATUS_TS) != STATUS_TS_STOPPED)
   2893 			printf("%s: transmit process failed to idle: "
   2894 			    "state %s\n", sc->sc_dev.dv_xname,
   2895 			    tx_state_names[(csr & STATUS_TS) >> 20]);
   2896 		if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
   2897 		    (csr & STATUS_RS) != STATUS_RS_STOPPED)
   2898 			printf("%s: receive process failed to idle: "
   2899 			    "state %s\n", sc->sc_dev.dv_xname,
   2900 			    rx_state_names[(csr & STATUS_RS) >> 17]);
   2901 	}
   2902 	TULIP_WRITE(sc, CSR_STATUS, ackmask);
   2903 }
   2904 
   2905 /*****************************************************************************
   2906  * Generic media support functions.
   2907  *****************************************************************************/
   2908 
   2909 /*
   2910  * tlp_mediastatus:	[ifmedia interface function]
   2911  *
   2912  *	Query the current media.
   2913  */
   2914 void
   2915 tlp_mediastatus(ifp, ifmr)
   2916 	struct ifnet *ifp;
   2917 	struct ifmediareq *ifmr;
   2918 {
   2919 	struct tulip_softc *sc = ifp->if_softc;
   2920 
   2921 	if (TULIP_IS_ENABLED(sc) == 0) {
   2922 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
   2923 		ifmr->ifm_status = 0;
   2924 		return;
   2925 	}
   2926 
   2927 	(*sc->sc_mediasw->tmsw_get)(sc, ifmr);
   2928 }
   2929 
   2930 /*
   2931  * tlp_mediachange:	[ifmedia interface function]
   2932  *
   2933  *	Update the current media.
   2934  */
   2935 int
   2936 tlp_mediachange(ifp)
   2937 	struct ifnet *ifp;
   2938 {
   2939 	struct tulip_softc *sc = ifp->if_softc;
   2940 
   2941 	return ((*sc->sc_mediasw->tmsw_set)(sc));
   2942 }
   2943 
   2944 /*****************************************************************************
   2945  * Support functions for MII-attached media.
   2946  *****************************************************************************/
   2947 
   2948 /*
   2949  * tlp_mii_tick:
   2950  *
   2951  *	One second timer, used to tick the MII.
   2952  */
   2953 void
   2954 tlp_mii_tick(arg)
   2955 	void *arg;
   2956 {
   2957 	struct tulip_softc *sc = arg;
   2958 	int s;
   2959 
   2960 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   2961 		return;
   2962 
   2963 	s = splnet();
   2964 	mii_tick(&sc->sc_mii);
   2965 	splx(s);
   2966 
   2967 	callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc);
   2968 }
   2969 
   2970 /*
   2971  * tlp_mii_statchg:	[mii interface function]
   2972  *
   2973  *	Callback from PHY when media changes.
   2974  */
   2975 void
   2976 tlp_mii_statchg(self)
   2977 	struct device *self;
   2978 {
   2979 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2980 
   2981 	/* Idle the transmit and receive processes. */
   2982 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2983 
   2984 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_HBD);
   2985 
   2986 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
   2987 		sc->sc_opmode |= OPMODE_TTM;
   2988 	else
   2989 		sc->sc_opmode |= OPMODE_HBD;
   2990 
   2991 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   2992 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
   2993 
   2994 	/*
   2995 	 * Write new OPMODE bits.  This also restarts the transmit
   2996 	 * and receive processes.
   2997 	 */
   2998 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2999 }
   3000 
   3001 /*
   3002  * tlp_winb_mii_statchg: [mii interface function]
   3003  *
   3004  *	Callback from PHY when media changes.  This version is
   3005  *	for the Winbond 89C840F, which has different OPMODE bits.
   3006  */
   3007 void
   3008 tlp_winb_mii_statchg(self)
   3009 	struct device *self;
   3010 {
   3011 	struct tulip_softc *sc = (struct tulip_softc *)self;
   3012 
   3013 	/* Idle the transmit and receive processes. */
   3014 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   3015 
   3016 	sc->sc_opmode &= ~(OPMODE_WINB_FES|OPMODE_FD);
   3017 
   3018 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX)
   3019 		sc->sc_opmode |= OPMODE_WINB_FES;
   3020 
   3021 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   3022 		sc->sc_opmode |= OPMODE_FD;
   3023 
   3024 	/*
   3025 	 * Write new OPMODE bits.  This also restarts the transmit
   3026 	 * and receive processes.
   3027 	 */
   3028 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3029 }
   3030 
   3031 /*
   3032  * tlp_mii_getmedia:
   3033  *
   3034  *	Callback from ifmedia to request current media status.
   3035  */
   3036 void
   3037 tlp_mii_getmedia(sc, ifmr)
   3038 	struct tulip_softc *sc;
   3039 	struct ifmediareq *ifmr;
   3040 {
   3041 
   3042 	mii_pollstat(&sc->sc_mii);
   3043 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   3044 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   3045 }
   3046 
   3047 /*
   3048  * tlp_mii_setmedia:
   3049  *
   3050  *	Callback from ifmedia to request new media setting.
   3051  */
   3052 int
   3053 tlp_mii_setmedia(sc)
   3054 	struct tulip_softc *sc;
   3055 {
   3056 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   3057 
   3058 	if (ifp->if_flags & IFF_UP) {
   3059 		switch (sc->sc_chip) {
   3060 		case TULIP_CHIP_21142:
   3061 		case TULIP_CHIP_21143:
   3062 			/* Disable the internal Nway engine. */
   3063 			TULIP_WRITE(sc, CSR_SIATXRX, 0);
   3064 			break;
   3065 
   3066 		default:
   3067 			/* Nothing. */
   3068 		}
   3069 		mii_mediachg(&sc->sc_mii);
   3070 	}
   3071 	return (0);
   3072 }
   3073 
   3074 /*
   3075  * tlp_bitbang_mii_readreg:
   3076  *
   3077  *	Read a PHY register via bit-bang'ing the MII.
   3078  */
   3079 int
   3080 tlp_bitbang_mii_readreg(self, phy, reg)
   3081 	struct device *self;
   3082 	int phy, reg;
   3083 {
   3084 	struct tulip_softc *sc = (void *) self;
   3085 
   3086 	return (mii_bitbang_readreg(self, sc->sc_bitbang_ops, phy, reg));
   3087 }
   3088 
   3089 /*
   3090  * tlp_bitbang_mii_writereg:
   3091  *
   3092  *	Write a PHY register via bit-bang'ing the MII.
   3093  */
   3094 void
   3095 tlp_bitbang_mii_writereg(self, phy, reg, val)
   3096 	struct device *self;
   3097 	int phy, reg, val;
   3098 {
   3099 	struct tulip_softc *sc = (void *) self;
   3100 
   3101 	mii_bitbang_writereg(self, sc->sc_bitbang_ops, phy, reg, val);
   3102 }
   3103 
   3104 /*
   3105  * tlp_sio_mii_bitbang_read:
   3106  *
   3107  *	Read the MII serial port for the MII bit-bang module.
   3108  */
   3109 u_int32_t
   3110 tlp_sio_mii_bitbang_read(self)
   3111 	struct device *self;
   3112 {
   3113 	struct tulip_softc *sc = (void *) self;
   3114 
   3115 	return (TULIP_READ(sc, CSR_MIIROM));
   3116 }
   3117 
   3118 /*
   3119  * tlp_sio_mii_bitbang_write:
   3120  *
   3121  *	Write the MII serial port for the MII bit-bang module.
   3122  */
   3123 void
   3124 tlp_sio_mii_bitbang_write(self, val)
   3125 	struct device *self;
   3126 	u_int32_t val;
   3127 {
   3128 	struct tulip_softc *sc = (void *) self;
   3129 
   3130 	TULIP_WRITE(sc, CSR_MIIROM, val);
   3131 }
   3132 
   3133 /*
   3134  * tlp_pnic_mii_readreg:
   3135  *
   3136  *	Read a PHY register on the Lite-On PNIC.
   3137  */
   3138 int
   3139 tlp_pnic_mii_readreg(self, phy, reg)
   3140 	struct device *self;
   3141 	int phy, reg;
   3142 {
   3143 	struct tulip_softc *sc = (void *) self;
   3144 	u_int32_t val;
   3145 	int i;
   3146 
   3147 	TULIP_WRITE(sc, CSR_PNIC_MII,
   3148 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   3149 	    PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
   3150 	    (reg << PNIC_MII_REGSHIFT));
   3151 
   3152 	for (i = 0; i < 1000; i++) {
   3153 		delay(10);
   3154 		val = TULIP_READ(sc, CSR_PNIC_MII);
   3155 		if ((val & PNIC_MII_BUSY) == 0) {
   3156 			if ((val & PNIC_MII_DATA) == PNIC_MII_DATA)
   3157 				return (0);
   3158 			else
   3159 				return (val & PNIC_MII_DATA);
   3160 		}
   3161 	}
   3162 	printf("%s: MII read timed out\n", sc->sc_dev.dv_xname);
   3163 	return (0);
   3164 }
   3165 
   3166 /*
   3167  * tlp_pnic_mii_writereg:
   3168  *
   3169  *	Write a PHY register on the Lite-On PNIC.
   3170  */
   3171 void
   3172 tlp_pnic_mii_writereg(self, phy, reg, val)
   3173 	struct device *self;
   3174 	int phy, reg, val;
   3175 {
   3176 	struct tulip_softc *sc = (void *) self;
   3177 	int i;
   3178 
   3179 	TULIP_WRITE(sc, CSR_PNIC_MII,
   3180 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   3181 	    PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
   3182 	    (reg << PNIC_MII_REGSHIFT) | val);
   3183 
   3184 	for (i = 0; i < 1000; i++) {
   3185 		delay(10);
   3186 		if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
   3187 			return;
   3188 	}
   3189 	printf("%s: MII write timed out\n", sc->sc_dev.dv_xname);
   3190 }
   3191 
   3192 const bus_addr_t tlp_al981_phy_regmap[] = {
   3193 	CSR_ADM_BMCR,
   3194 	CSR_ADM_BMSR,
   3195 	CSR_ADM_PHYIDR1,
   3196 	CSR_ADM_PHYIDR2,
   3197 	CSR_ADM_ANAR,
   3198 	CSR_ADM_ANLPAR,
   3199 	CSR_ADM_ANER,
   3200 
   3201 	CSR_ADM_XMC,
   3202 	CSR_ADM_XCIIS,
   3203 	CSR_ADM_XIE,
   3204 	CSR_ADM_100CTR,
   3205 };
   3206 const int tlp_al981_phy_regmap_size = sizeof(tlp_al981_phy_regmap) /
   3207     sizeof(tlp_al981_phy_regmap[0]);
   3208 
   3209 /*
   3210  * tlp_al981_mii_readreg:
   3211  *
   3212  *	Read a PHY register on the ADMtek AL981.
   3213  */
   3214 int
   3215 tlp_al981_mii_readreg(self, phy, reg)
   3216 	struct device *self;
   3217 	int phy, reg;
   3218 {
   3219 	struct tulip_softc *sc = (struct tulip_softc *)self;
   3220 
   3221 	/* AL981 only has an internal PHY. */
   3222 	if (phy != 0)
   3223 		return (0);
   3224 
   3225 	if (reg >= tlp_al981_phy_regmap_size)
   3226 		return (0);
   3227 
   3228 	return (bus_space_read_4(sc->sc_st, sc->sc_sh,
   3229 	    tlp_al981_phy_regmap[reg]) & 0xffff);
   3230 }
   3231 
   3232 /*
   3233  * tlp_al981_mii_writereg:
   3234  *
   3235  *	Write a PHY register on the ADMtek AL981.
   3236  */
   3237 void
   3238 tlp_al981_mii_writereg(self, phy, reg, val)
   3239 	struct device *self;
   3240 	int phy, reg, val;
   3241 {
   3242 	struct tulip_softc *sc = (struct tulip_softc *)self;
   3243 
   3244 	/* AL981 only has an internal PHY. */
   3245 	if (phy != 0)
   3246 		return;
   3247 
   3248 	if (reg >= tlp_al981_phy_regmap_size)
   3249 		return;
   3250 
   3251 	bus_space_write_4(sc->sc_st, sc->sc_sh,
   3252 	    tlp_al981_phy_regmap[reg], val);
   3253 }
   3254 
   3255 /*****************************************************************************
   3256  * Chip-specific pre-init and reset functions.
   3257  *****************************************************************************/
   3258 
   3259 /*
   3260  * tlp_2114x_preinit:
   3261  *
   3262  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
   3263  */
   3264 void
   3265 tlp_2114x_preinit(sc)
   3266 	struct tulip_softc *sc;
   3267 {
   3268 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3269 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3270 
   3271 	/*
   3272 	 * Whether or not we're in MII or SIA/SYM mode, the media info
   3273 	 * contains the appropriate OPMODE bits.
   3274 	 *
   3275 	 * Note that if we have no media info, we are are doing
   3276 	 * non-MII `auto'.
   3277 	 *
   3278 	 * Also, we always set the Must-Be-One bit.
   3279 	 */
   3280 	if (tm == NULL) {
   3281 #ifdef DIAGNOSTIC
   3282 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   3283 			panic("tlp_2114x_preinit: not IFM_AUTO");
   3284 		if (sc->sc_nway_active == NULL)
   3285 			panic("tlp_2114x_preinit: nway_active NULL");
   3286 #endif
   3287 		tm = sc->sc_nway_active->ifm_aux;
   3288 	}
   3289 	sc->sc_opmode |= OPMODE_MBO | tm->tm_opmode;
   3290 
   3291 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3292 }
   3293 
   3294 /*
   3295  * tlp_2114x_mii_preinit:
   3296  *
   3297  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
   3298  *	This version is used by boards which only have MII and don't have
   3299  *	an ISV SROM.
   3300  */
   3301 void
   3302 tlp_2114x_mii_preinit(sc)
   3303 	struct tulip_softc *sc;
   3304 {
   3305 
   3306 	/*
   3307 	 * Always set the Must-Be-One bit, and Port Select (to select MII).
   3308 	 * We'll never be called during a media change.
   3309 	 */
   3310 	sc->sc_opmode |= OPMODE_MBO|OPMODE_PS;
   3311 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3312 }
   3313 
   3314 /*
   3315  * tlp_pnic_preinit:
   3316  *
   3317  *	Pre-init function for the Lite-On 82c168 and 82c169.
   3318  */
   3319 void
   3320 tlp_pnic_preinit(sc)
   3321 	struct tulip_softc *sc;
   3322 {
   3323 
   3324 	if (sc->sc_flags & TULIPF_HAS_MII) {
   3325 		/*
   3326 		 * MII case: just set the port-select bit; we will never
   3327 		 * be called during a media change.
   3328 		 */
   3329 		sc->sc_opmode |= OPMODE_PS;
   3330 	} else {
   3331 		/*
   3332 		 * ENDEC/PCS/Nway mode; enable the Tx backoff counter.
   3333 		 */
   3334 		sc->sc_opmode |= OPMODE_PNIC_TBEN;
   3335 	}
   3336 }
   3337 
   3338 /*
   3339  * tlp_21140_reset:
   3340  *
   3341  *	Issue a reset sequence on the 21140 via the GPIO facility.
   3342  */
   3343 void
   3344 tlp_21140_reset(sc)
   3345 	struct tulip_softc *sc;
   3346 {
   3347 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3348 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3349 	int i;
   3350 
   3351 	/* First, set the direction on the GPIO pins. */
   3352 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   3353 
   3354 	/* Now, issue the reset sequence. */
   3355 	for (i = 0; i < tm->tm_reset_length; i++) {
   3356 		delay(10);
   3357 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_reset_offset + i]);
   3358 	}
   3359 
   3360 	/* Now, issue the selection sequence. */
   3361 	for (i = 0; i < tm->tm_gp_length; i++) {
   3362 		delay(10);
   3363 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_gp_offset + i]);
   3364 	}
   3365 
   3366 	/* If there were no sequences, just lower the pins. */
   3367 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0)
   3368 		TULIP_WRITE(sc, CSR_GPP, 0);
   3369 }
   3370 
   3371 /*
   3372  * tlp_21142_reset:
   3373  *
   3374  *	Issue a reset sequence on the 21142 via the GPIO facility.
   3375  */
   3376 void
   3377 tlp_21142_reset(sc)
   3378 	struct tulip_softc *sc;
   3379 {
   3380 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3381 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3382 	const u_int8_t *ncp;
   3383 	int i;
   3384 
   3385 	ncp = &sc->sc_srom[tm->tm_reset_offset];
   3386 	for (i = 0; i < tm->tm_reset_length; i++, ncp += 2) {
   3387 		delay(10);
   3388 		TULIP_WRITE(sc, CSR_SIAGEN,
   3389 		    TULIP_ROM_GETW(ncp, 0) << 16);
   3390 	}
   3391 
   3392 	ncp = &sc->sc_srom[tm->tm_gp_offset];
   3393 	for (i = 0; i < tm->tm_gp_length; i++, ncp += 2) {
   3394 		delay(10);
   3395 		TULIP_WRITE(sc, CSR_SIAGEN,
   3396 		    TULIP_ROM_GETW(ncp, 0) << 16);
   3397 	}
   3398 
   3399 	/* If there were no sequences, just lower the pins. */
   3400 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   3401 		delay(10);
   3402 		TULIP_WRITE(sc, CSR_SIAGEN, 0);
   3403 	}
   3404 }
   3405 
   3406 /*
   3407  * tlp_pmac_reset:
   3408  *
   3409  *	Reset routine for Macronix chips.
   3410  */
   3411 void
   3412 tlp_pmac_reset(sc)
   3413 	struct tulip_softc *sc;
   3414 {
   3415 
   3416 	switch (sc->sc_chip) {
   3417 	case TULIP_CHIP_82C115:
   3418 	case TULIP_CHIP_MX98715:
   3419 	case TULIP_CHIP_MX98715A:
   3420 	case TULIP_CHIP_MX98725:
   3421 		/*
   3422 		 * Set the LED operating mode.  This information is located
   3423 		 * in the EEPROM at byte offset 0x77, per the MX98715A and
   3424 		 * MX98725 application notes.
   3425 		 */
   3426 		TULIP_WRITE(sc, CSR_MIIROM, sc->sc_srom[0x77] << 24);
   3427 		break;
   3428 
   3429 	default:
   3430 		/* Nothing. */
   3431 	}
   3432 }
   3433 
   3434 /*****************************************************************************
   3435  * Chip/board-specific media switches.  The ones here are ones that
   3436  * are potentially common to multiple front-ends.
   3437  *****************************************************************************/
   3438 
   3439 /*
   3440  * This table is a common place for all sorts of media information,
   3441  * keyed off of the SROM media code for that media.
   3442  *
   3443  * Note that we explicitly configure the 21142/21143 to always advertise
   3444  * NWay capabilities when using the UTP port.
   3445  * XXX Actually, we don't yet.
   3446  */
   3447 const struct tulip_srom_to_ifmedia tulip_srom_to_ifmedia_table[] = {
   3448 	{ TULIP_ROM_MB_MEDIA_TP,	IFM_10_T,	0,
   3449 	  "10baseT",
   3450 	  0,
   3451 	  { SIACONN_21040_10BASET,
   3452 	    SIATXRX_21040_10BASET,
   3453 	    SIAGEN_21040_10BASET },
   3454 
   3455 	  { SIACONN_21041_10BASET,
   3456 	    SIATXRX_21041_10BASET,
   3457 	    SIAGEN_21041_10BASET },
   3458 
   3459 	  { SIACONN_21142_10BASET,
   3460 	    SIATXRX_21142_10BASET,
   3461 	    SIAGEN_21142_10BASET } },
   3462 
   3463 	{ TULIP_ROM_MB_MEDIA_BNC,	IFM_10_2,	0,
   3464 	  "10base2",
   3465 	  0,
   3466 	  { 0,
   3467 	    0,
   3468 	    0 },
   3469 
   3470 	  { SIACONN_21041_BNC,
   3471 	    SIATXRX_21041_BNC,
   3472 	    SIAGEN_21041_BNC },
   3473 
   3474 	  { SIACONN_21142_BNC,
   3475 	    SIATXRX_21142_BNC,
   3476 	    SIAGEN_21142_BNC } },
   3477 
   3478 	{ TULIP_ROM_MB_MEDIA_AUI,	IFM_10_5,	0,
   3479 	  "10base5",
   3480 	  0,
   3481 	  { SIACONN_21040_AUI,
   3482 	    SIATXRX_21040_AUI,
   3483 	    SIAGEN_21040_AUI },
   3484 
   3485 	  { SIACONN_21041_AUI,
   3486 	    SIATXRX_21041_AUI,
   3487 	    SIAGEN_21041_AUI },
   3488 
   3489 	  { SIACONN_21142_AUI,
   3490 	    SIATXRX_21142_AUI,
   3491 	    SIAGEN_21142_AUI } },
   3492 
   3493 	{ TULIP_ROM_MB_MEDIA_100TX,	IFM_100_TX,	0,
   3494 	  "100baseTX",
   3495 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
   3496 	  { 0,
   3497 	    0,
   3498 	    0 },
   3499 
   3500 	  { 0,
   3501 	    0,
   3502 	    0 },
   3503 
   3504 	  { 0,
   3505 	    0,
   3506 	    SIAGEN_ABM } },
   3507 
   3508 	{ TULIP_ROM_MB_MEDIA_TP_FDX,	IFM_10_T,	IFM_FDX,
   3509 	  "10baseT-FDX",
   3510 	  OPMODE_FD|OPMODE_HBD,
   3511 	  { SIACONN_21040_10BASET_FDX,
   3512 	    SIATXRX_21040_10BASET_FDX,
   3513 	    SIAGEN_21040_10BASET_FDX },
   3514 
   3515 	  { SIACONN_21041_10BASET_FDX,
   3516 	    SIATXRX_21041_10BASET_FDX,
   3517 	    SIAGEN_21041_10BASET_FDX },
   3518 
   3519 	  { SIACONN_21142_10BASET_FDX,
   3520 	    SIATXRX_21142_10BASET_FDX,
   3521 	    SIAGEN_21142_10BASET_FDX } },
   3522 
   3523 	{ TULIP_ROM_MB_MEDIA_100TX_FDX,	IFM_100_TX,	IFM_FDX,
   3524 	  "100baseTX-FDX",
   3525 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_FD|OPMODE_HBD,
   3526 	  { 0,
   3527 	    0,
   3528 	    0 },
   3529 
   3530 	  { 0,
   3531 	    0,
   3532 	    0 },
   3533 
   3534 	  { 0,
   3535 	    0,
   3536 	    SIAGEN_ABM } },
   3537 
   3538 	{ TULIP_ROM_MB_MEDIA_100T4,	IFM_100_T4,	0,
   3539 	  "100baseT4",
   3540 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
   3541 	  { 0,
   3542 	    0,
   3543 	    0 },
   3544 
   3545 	  { 0,
   3546 	    0,
   3547 	    0 },
   3548 
   3549 	  { 0,
   3550 	    0,
   3551 	    SIAGEN_ABM } },
   3552 
   3553 	{ TULIP_ROM_MB_MEDIA_100FX,	IFM_100_FX,	0,
   3554 	  "100baseFX",
   3555 	  OPMODE_PS|OPMODE_PCS|OPMODE_HBD,
   3556 	  { 0,
   3557 	    0,
   3558 	    0 },
   3559 
   3560 	  { 0,
   3561 	    0,
   3562 	    0 },
   3563 
   3564 	  { 0,
   3565 	    0,
   3566 	    SIAGEN_ABM } },
   3567 
   3568 	{ TULIP_ROM_MB_MEDIA_100FX_FDX,	IFM_100_FX,	IFM_FDX,
   3569 	  "100baseFX-FDX",
   3570 	  OPMODE_PS|OPMODE_PCS|OPMODE_FD|OPMODE_HBD,
   3571 	  { 0,
   3572 	    0,
   3573 	    0 },
   3574 
   3575 	  { 0,
   3576 	    0,
   3577 	    0 },
   3578 
   3579 	  { 0,
   3580 	    0,
   3581 	    SIAGEN_ABM } },
   3582 
   3583 	{ 0,				0,		0,
   3584 	  NULL,
   3585 	  0,
   3586 	  { 0,
   3587 	    0,
   3588 	    0 },
   3589 
   3590 	  { 0,
   3591 	    0,
   3592 	    0 },
   3593 
   3594 	  { 0,
   3595 	    0,
   3596 	    0 } },
   3597 };
   3598 
   3599 const struct tulip_srom_to_ifmedia *tlp_srom_to_ifmedia __P((u_int8_t));
   3600 void	tlp_srom_media_info __P((struct tulip_softc *,
   3601 	    const struct tulip_srom_to_ifmedia *, struct tulip_21x4x_media *));
   3602 void	tlp_add_srom_media __P((struct tulip_softc *, int,
   3603 	    void (*)(struct tulip_softc *, struct ifmediareq *),
   3604 	    int (*)(struct tulip_softc *), const u_int8_t *, int));
   3605 void	tlp_print_media __P((struct tulip_softc *));
   3606 void	tlp_nway_activate __P((struct tulip_softc *, int));
   3607 void	tlp_get_minst __P((struct tulip_softc *));
   3608 
   3609 const struct tulip_srom_to_ifmedia *
   3610 tlp_srom_to_ifmedia(sm)
   3611 	u_int8_t sm;
   3612 {
   3613 	const struct tulip_srom_to_ifmedia *tsti;
   3614 
   3615 	for (tsti = tulip_srom_to_ifmedia_table;
   3616 	     tsti->tsti_name != NULL; tsti++) {
   3617 		if (tsti->tsti_srom == sm)
   3618 			return (tsti);
   3619 	}
   3620 
   3621 	return (NULL);
   3622 }
   3623 
   3624 void
   3625 tlp_srom_media_info(sc, tsti, tm)
   3626 	struct tulip_softc *sc;
   3627 	const struct tulip_srom_to_ifmedia *tsti;
   3628 	struct tulip_21x4x_media *tm;
   3629 {
   3630 
   3631 	tm->tm_name = tsti->tsti_name;
   3632 	tm->tm_opmode = tsti->tsti_opmode;
   3633 
   3634 	switch (sc->sc_chip) {
   3635 	case TULIP_CHIP_DE425:
   3636 	case TULIP_CHIP_21040:
   3637 		tm->tm_sia = tsti->tsti_21040;	/* struct assignment */
   3638 		break;
   3639 
   3640 	case TULIP_CHIP_21041:
   3641 		tm->tm_sia = tsti->tsti_21041;	/* struct assignment */
   3642 		break;
   3643 
   3644 	case TULIP_CHIP_21142:
   3645 	case TULIP_CHIP_21143:
   3646 	case TULIP_CHIP_82C115:
   3647 	case TULIP_CHIP_MX98715:
   3648 	case TULIP_CHIP_MX98715A:
   3649 	case TULIP_CHIP_MX98725:
   3650 		tm->tm_sia = tsti->tsti_21142;	/* struct assignment */
   3651 		break;
   3652 
   3653 	default:
   3654 		/* Nothing. */
   3655 	}
   3656 }
   3657 
   3658 void
   3659 tlp_add_srom_media(sc, type, get, set, list, cnt)
   3660 	struct tulip_softc *sc;
   3661 	int type;
   3662 	void (*get) __P((struct tulip_softc *, struct ifmediareq *));
   3663 	int (*set) __P((struct tulip_softc *));
   3664 	const u_int8_t *list;
   3665 	int cnt;
   3666 {
   3667 	struct tulip_21x4x_media *tm;
   3668 	const struct tulip_srom_to_ifmedia *tsti;
   3669 	int i;
   3670 
   3671 	for (i = 0; i < cnt; i++) {
   3672 		tsti = tlp_srom_to_ifmedia(list[i]);
   3673 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   3674 		memset(tm, 0, sizeof(*tm));
   3675 		tlp_srom_media_info(sc, tsti, tm);
   3676 		tm->tm_type = type;
   3677 		tm->tm_get = get;
   3678 		tm->tm_set = set;
   3679 
   3680 		ifmedia_add(&sc->sc_mii.mii_media,
   3681 		    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   3682 		    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   3683 	}
   3684 }
   3685 
   3686 void
   3687 tlp_print_media(sc)
   3688 	struct tulip_softc *sc;
   3689 {
   3690 	struct ifmedia_entry *ife;
   3691 	struct tulip_21x4x_media *tm;
   3692 	const char *sep = "";
   3693 
   3694 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
   3695 
   3696 	printf("%s: ", sc->sc_dev.dv_xname);
   3697 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   3698 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
   3699 		tm = ife->ifm_aux;
   3700 		if (tm == NULL) {
   3701 #ifdef DIAGNOSTIC
   3702 			if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   3703 				panic("tlp_print_media");
   3704 #endif
   3705 			PRINT("auto");
   3706 		} else if (tm->tm_type != TULIP_ROM_MB_21140_MII &&
   3707 			   tm->tm_type != TULIP_ROM_MB_21142_MII) {
   3708 			PRINT(tm->tm_name);
   3709 		}
   3710 	}
   3711 	printf("\n");
   3712 
   3713 #undef PRINT
   3714 }
   3715 
   3716 void
   3717 tlp_nway_activate(sc, media)
   3718 	struct tulip_softc *sc;
   3719 	int media;
   3720 {
   3721 	struct ifmedia_entry *ife;
   3722 
   3723 	ife = ifmedia_match(&sc->sc_mii.mii_media, media, 0);
   3724 #ifdef DIAGNOSTIC
   3725 	if (ife == NULL)
   3726 		panic("tlp_nway_activate");
   3727 #endif
   3728 	sc->sc_nway_active = ife;
   3729 }
   3730 
   3731 void
   3732 tlp_get_minst(sc)
   3733 	struct tulip_softc *sc;
   3734 {
   3735 
   3736 	if ((sc->sc_media_seen &
   3737 	    ~((1 << TULIP_ROM_MB_21140_MII) |
   3738 	      (1 << TULIP_ROM_MB_21142_MII))) == 0) {
   3739 		/*
   3740 		 * We have not yet seen any SIA/SYM media (but are
   3741 		 * about to; that's why we're called!), so assign
   3742 		 * the current media instance to be the `internal media'
   3743 		 * instance, and advance it so any MII media gets a
   3744 		 * fresh one (used to selecting/isolating a PHY).
   3745 		 */
   3746 		sc->sc_tlp_minst = sc->sc_mii.mii_instance++;
   3747 	}
   3748 }
   3749 
   3750 /*
   3751  * SIA Utility functions.
   3752  */
   3753 void	tlp_sia_update_link __P((struct tulip_softc *));
   3754 void	tlp_sia_get __P((struct tulip_softc *, struct ifmediareq *));
   3755 int	tlp_sia_set __P((struct tulip_softc *));
   3756 void	tlp_sia_fixup __P((struct tulip_softc *));
   3757 
   3758 void
   3759 tlp_sia_update_link(sc)
   3760 	struct tulip_softc *sc;
   3761 {
   3762 	struct ifmedia_entry *ife;
   3763 	struct tulip_21x4x_media *tm;
   3764 	u_int32_t siastat;
   3765 
   3766 	ife = TULIP_CURRENT_MEDIA(sc);
   3767 	tm = ife->ifm_aux;
   3768 
   3769 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
   3770 
   3771 	siastat = TULIP_READ(sc, CSR_SIASTAT);
   3772 
   3773 	/*
   3774 	 * Note that when we do SIA link tests, we are assuming that
   3775 	 * the chip is really in the mode that the current media setting
   3776 	 * reflects.  If we're not, then the link tests will not be
   3777 	 * accurate!
   3778 	 */
   3779 	switch (IFM_SUBTYPE(ife->ifm_media)) {
   3780 	case IFM_10_T:
   3781 		sc->sc_flags |= TULIPF_LINK_VALID;
   3782 		if ((siastat & SIASTAT_LS10) == 0)
   3783 			sc->sc_flags |= TULIPF_LINK_UP;
   3784 		break;
   3785 
   3786 	case IFM_100_TX:
   3787 	case IFM_100_T4:
   3788 		sc->sc_flags |= TULIPF_LINK_VALID;
   3789 		if ((siastat & SIASTAT_LS100) == 0)
   3790 			sc->sc_flags |= TULIPF_LINK_UP;
   3791 		break;
   3792 	}
   3793 
   3794 	switch (sc->sc_chip) {
   3795 	case TULIP_CHIP_21142:
   3796 	case TULIP_CHIP_21143:
   3797 		/*
   3798 		 * On these chips, we can tell more information about
   3799 		 * AUI/BNC.  Note that the AUI/BNC selection is made
   3800 		 * in a different register; for our purpose, it's all
   3801 		 * AUI.
   3802 		 */
   3803 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   3804 		case IFM_10_2:
   3805 		case IFM_10_5:
   3806 			sc->sc_flags |= TULIPF_LINK_VALID;
   3807 			if (siastat & SIASTAT_ARA) {
   3808 				TULIP_WRITE(sc, CSR_SIASTAT, SIASTAT_ARA);
   3809 				sc->sc_flags |= TULIPF_LINK_UP;
   3810 			}
   3811 			break;
   3812 
   3813 		default:
   3814 			/*
   3815 			 * If we're SYM media and can detect the link
   3816 			 * via the GPIO facility, prefer that status
   3817 			 * over LS100.
   3818 			 */
   3819 			if (tm->tm_type == TULIP_ROM_MB_21143_SYM &&
   3820 			    tm->tm_actmask != 0) {
   3821 				sc->sc_flags = (sc->sc_flags &
   3822 				    ~TULIPF_LINK_UP) | TULIPF_LINK_VALID;
   3823 				if (TULIP_ISSET(sc, CSR_SIAGEN,
   3824 				    tm->tm_actmask) == tm->tm_actdata)
   3825 					sc->sc_flags |= TULIPF_LINK_UP;
   3826 			}
   3827 		}
   3828 		break;
   3829 
   3830 	default:
   3831 		/* Nothing. */
   3832 	}
   3833 }
   3834 
   3835 void
   3836 tlp_sia_get(sc, ifmr)
   3837 	struct tulip_softc *sc;
   3838 	struct ifmediareq *ifmr;
   3839 {
   3840 	struct ifmedia_entry *ife;
   3841 
   3842 	ifmr->ifm_status = 0;
   3843 
   3844 	tlp_sia_update_link(sc);
   3845 
   3846 	ife = TULIP_CURRENT_MEDIA(sc);
   3847 
   3848 	if (sc->sc_flags & TULIPF_LINK_VALID)
   3849 		ifmr->ifm_status |= IFM_AVALID;
   3850 	if (sc->sc_flags & TULIPF_LINK_UP)
   3851 		ifmr->ifm_status |= IFM_ACTIVE;
   3852 	ifmr->ifm_active = ife->ifm_media;
   3853 }
   3854 
   3855 void
   3856 tlp_sia_fixup(sc)
   3857 	struct tulip_softc *sc;
   3858 {
   3859 	struct ifmedia_entry *ife;
   3860 	struct tulip_21x4x_media *tm;
   3861 	u_int32_t siaconn, siatxrx, siagen;
   3862 
   3863 	switch (sc->sc_chip) {
   3864 	case TULIP_CHIP_82C115:
   3865 	case TULIP_CHIP_MX98713A:
   3866 	case TULIP_CHIP_MX98715:
   3867 	case TULIP_CHIP_MX98715A:
   3868 	case TULIP_CHIP_MX98725:
   3869 		siaconn = PMAC_SIACONN_MASK;
   3870 		siatxrx = PMAC_SIATXRX_MASK;
   3871 		siagen  = PMAC_SIAGEN_MASK;
   3872 		break;
   3873 
   3874 	default:
   3875 		/* No fixups required on any other chips. */
   3876 		return;
   3877 	}
   3878 
   3879 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   3880 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
   3881 		tm = ife->ifm_aux;
   3882 		if (tm == NULL)
   3883 			continue;
   3884 
   3885 		tm->tm_siaconn &= siaconn;
   3886 		tm->tm_siatxrx &= siatxrx;
   3887 		tm->tm_siagen  &= siagen;
   3888 	}
   3889 }
   3890 
   3891 int
   3892 tlp_sia_set(sc)
   3893 	struct tulip_softc *sc;
   3894 {
   3895 	struct ifmedia_entry *ife;
   3896 	struct tulip_21x4x_media *tm;
   3897 
   3898 	ife = TULIP_CURRENT_MEDIA(sc);
   3899 	tm = ife->ifm_aux;
   3900 
   3901 	/*
   3902 	 * XXX This appears to be necessary on a bunch of the clone chips.
   3903 	 */
   3904 	delay(20000);
   3905 
   3906 	/*
   3907 	 * Idle the chip.
   3908 	 */
   3909 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   3910 
   3911 	/*
   3912 	 * Program the SIA.  It's important to write in this order,
   3913 	 * resetting the SIA first.
   3914 	 */
   3915 	TULIP_WRITE(sc, CSR_SIACONN, 0);		/* SRL bit clear */
   3916 	delay(1000);
   3917 
   3918 	TULIP_WRITE(sc, CSR_SIATXRX, tm->tm_siatxrx);
   3919 
   3920 	switch (sc->sc_chip) {
   3921 	case TULIP_CHIP_21142:
   3922 	case TULIP_CHIP_21143:
   3923 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpctl);
   3924 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpdata);
   3925 		break;
   3926 	default:
   3927 		TULIP_WRITE(sc, CSR_SIAGEN,  tm->tm_siagen);
   3928 	}
   3929 
   3930 	TULIP_WRITE(sc, CSR_SIACONN, tm->tm_siaconn);
   3931 
   3932 	/*
   3933 	 * Set the OPMODE bits for this media and write OPMODE.
   3934 	 * This will resume the transmit and receive processes.
   3935 	 */
   3936 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
   3937 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3938 
   3939 	return (0);
   3940 }
   3941 
   3942 /*
   3943  * 21140 GPIO utility functions.
   3944  */
   3945 void	tlp_21140_gpio_update_link __P((struct tulip_softc *));
   3946 void	tlp_21140_gpio_get __P((struct tulip_softc *sc,
   3947 	    struct ifmediareq *ifmr));
   3948 int	tlp_21140_gpio_set __P((struct tulip_softc *sc));
   3949 
   3950 void
   3951 tlp_21140_gpio_update_link(sc)
   3952 	struct tulip_softc *sc;
   3953 {
   3954 	struct ifmedia_entry *ife;
   3955 	struct tulip_21x4x_media *tm;
   3956 
   3957 	ife = TULIP_CURRENT_MEDIA(sc);
   3958 	tm = ife->ifm_aux;
   3959 
   3960 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
   3961 
   3962 	if (tm->tm_actmask != 0) {
   3963 		sc->sc_flags |= TULIPF_LINK_VALID;
   3964 		if (TULIP_ISSET(sc, CSR_GPP, tm->tm_actmask) ==
   3965 		    tm->tm_actdata)
   3966 			sc->sc_flags |= TULIPF_LINK_UP;
   3967 	}
   3968 }
   3969 
   3970 void
   3971 tlp_21140_gpio_get(sc, ifmr)
   3972 	struct tulip_softc *sc;
   3973 	struct ifmediareq *ifmr;
   3974 {
   3975 	struct ifmedia_entry *ife;
   3976 
   3977 	ifmr->ifm_status = 0;
   3978 
   3979 	tlp_21140_gpio_update_link(sc);
   3980 
   3981 	ife = TULIP_CURRENT_MEDIA(sc);
   3982 
   3983 	if (sc->sc_flags & TULIPF_LINK_VALID)
   3984 		ifmr->ifm_status |= IFM_AVALID;
   3985 	if (sc->sc_flags & TULIPF_LINK_UP)
   3986 		ifmr->ifm_status |= IFM_ACTIVE;
   3987 	ifmr->ifm_active = ife->ifm_media;
   3988 }
   3989 
   3990 int
   3991 tlp_21140_gpio_set(sc)
   3992 	struct tulip_softc *sc;
   3993 {
   3994 	struct ifmedia_entry *ife;
   3995 	struct tulip_21x4x_media *tm;
   3996 
   3997 	ife = TULIP_CURRENT_MEDIA(sc);
   3998 	tm = ife->ifm_aux;
   3999 
   4000 	/*
   4001 	 * Idle the chip.
   4002 	 */
   4003 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   4004 
   4005 	/*
   4006 	 * Set the GPIO pins for this media, to flip any
   4007 	 * relays, etc.
   4008 	 */
   4009 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   4010 	delay(10);
   4011 	TULIP_WRITE(sc, CSR_GPP, tm->tm_gpdata);
   4012 
   4013 	/*
   4014 	 * Set the OPMODE bits for this media and write OPMODE.
   4015 	 * This will resume the transmit and receive processes.
   4016 	 */
   4017 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
   4018 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   4019 
   4020 	return (0);
   4021 }
   4022 
   4023 /*
   4024  * 21040 and 21041 media switches.
   4025  */
   4026 void	tlp_21040_tmsw_init __P((struct tulip_softc *));
   4027 void	tlp_21040_tp_tmsw_init __P((struct tulip_softc *));
   4028 void	tlp_21040_auibnc_tmsw_init __P((struct tulip_softc *));
   4029 void	tlp_21041_tmsw_init __P((struct tulip_softc *));
   4030 
   4031 const struct tulip_mediasw tlp_21040_mediasw = {
   4032 	tlp_21040_tmsw_init, tlp_sia_get, tlp_sia_set
   4033 };
   4034 
   4035 const struct tulip_mediasw tlp_21040_tp_mediasw = {
   4036 	tlp_21040_tp_tmsw_init, tlp_sia_get, tlp_sia_set
   4037 };
   4038 
   4039 const struct tulip_mediasw tlp_21040_auibnc_mediasw = {
   4040 	tlp_21040_auibnc_tmsw_init, tlp_sia_get, tlp_sia_set
   4041 };
   4042 
   4043 const struct tulip_mediasw tlp_21041_mediasw = {
   4044 	tlp_21041_tmsw_init, tlp_sia_get, tlp_sia_set
   4045 };
   4046 
   4047 
   4048 void
   4049 tlp_21040_tmsw_init(sc)
   4050 	struct tulip_softc *sc;
   4051 {
   4052 	static const u_int8_t media[] = {
   4053 		TULIP_ROM_MB_MEDIA_TP,
   4054 		TULIP_ROM_MB_MEDIA_TP_FDX,
   4055 		TULIP_ROM_MB_MEDIA_AUI,
   4056 	};
   4057 	struct tulip_21x4x_media *tm;
   4058 
   4059 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4060 	    tlp_mediastatus);
   4061 
   4062 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 3);
   4063 
   4064 	/*
   4065 	 * No SROM type for External SIA.
   4066 	 */
   4067 	tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4068 	memset(tm, 0, sizeof(*tm));
   4069 	tm->tm_name = "manual";
   4070 	tm->tm_opmode = 0;
   4071 	tm->tm_siaconn = SIACONN_21040_EXTSIA;
   4072 	tm->tm_siatxrx = SIATXRX_21040_EXTSIA;
   4073 	tm->tm_siagen  = SIAGEN_21040_EXTSIA;
   4074 	ifmedia_add(&sc->sc_mii.mii_media,
   4075 	    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, sc->sc_tlp_minst), 0, tm);
   4076 
   4077 	/*
   4078 	 * XXX Autosense not yet supported.
   4079 	 */
   4080 
   4081 	/* XXX This should be auto-sense. */
   4082 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   4083 
   4084 	tlp_print_media(sc);
   4085 }
   4086 
   4087 void
   4088 tlp_21040_tp_tmsw_init(sc)
   4089 	struct tulip_softc *sc;
   4090 {
   4091 	static const u_int8_t media[] = {
   4092 		TULIP_ROM_MB_MEDIA_TP,
   4093 		TULIP_ROM_MB_MEDIA_TP_FDX,
   4094 	};
   4095 
   4096 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4097 	    tlp_mediastatus);
   4098 
   4099 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 2);
   4100 
   4101 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   4102 
   4103 	tlp_print_media(sc);
   4104 }
   4105 
   4106 void
   4107 tlp_21040_auibnc_tmsw_init(sc)
   4108 	struct tulip_softc *sc;
   4109 {
   4110 	static const u_int8_t media[] = {
   4111 		TULIP_ROM_MB_MEDIA_AUI,
   4112 	};
   4113 
   4114 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4115 	    tlp_mediastatus);
   4116 
   4117 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 1);
   4118 
   4119 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5);
   4120 
   4121 	tlp_print_media(sc);
   4122 }
   4123 
   4124 void
   4125 tlp_21041_tmsw_init(sc)
   4126 	struct tulip_softc *sc;
   4127 {
   4128 	static const u_int8_t media[] = {
   4129 		TULIP_ROM_MB_MEDIA_TP,
   4130 		TULIP_ROM_MB_MEDIA_TP_FDX,
   4131 		TULIP_ROM_MB_MEDIA_BNC,
   4132 		TULIP_ROM_MB_MEDIA_AUI,
   4133 	};
   4134 	int i, defmedia, devcnt, leaf_offset, mb_offset, m_cnt;
   4135 	const struct tulip_srom_to_ifmedia *tsti;
   4136 	struct tulip_21x4x_media *tm;
   4137 	u_int16_t romdef;
   4138 	u_int8_t mb;
   4139 
   4140 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4141 	    tlp_mediastatus);
   4142 
   4143 	if (tlp_isv_srom(sc->sc_srom) == 0) {
   4144  not_isv_srom:
   4145 		/*
   4146 		 * If we have a board without the standard 21041 SROM format,
   4147 		 * we just assume all media are present and try and pick a
   4148 		 * reasonable default.
   4149 		 */
   4150 		tlp_add_srom_media(sc, 0, NULL, NULL, media, 4);
   4151 
   4152 		/*
   4153 		 * XXX Autosense not yet supported.
   4154 		 */
   4155 
   4156 		/* XXX This should be auto-sense. */
   4157 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   4158 
   4159 		tlp_print_media(sc);
   4160 		return;
   4161 	}
   4162 
   4163 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   4164 	for (i = 0; i < devcnt; i++) {
   4165 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   4166 			break;
   4167 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   4168 		    sc->sc_devno)
   4169 			break;
   4170 	}
   4171 
   4172 	if (i == devcnt)
   4173 		goto not_isv_srom;
   4174 
   4175 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
   4176 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
   4177 	mb_offset = leaf_offset + TULIP_ROM_IL_MEDIAn_BLOCK_BASE;
   4178 	m_cnt = sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
   4179 
   4180 	for (; m_cnt != 0;
   4181 	     m_cnt--, mb_offset += TULIP_ROM_MB_SIZE(mb)) {
   4182 		mb = sc->sc_srom[mb_offset];
   4183 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4184 		memset(tm, 0, sizeof(*tm));
   4185 		switch (mb & TULIP_ROM_MB_MEDIA_CODE) {
   4186 		case TULIP_ROM_MB_MEDIA_TP_FDX:
   4187 		case TULIP_ROM_MB_MEDIA_TP:
   4188 		case TULIP_ROM_MB_MEDIA_BNC:
   4189 		case TULIP_ROM_MB_MEDIA_AUI:
   4190 			tsti = tlp_srom_to_ifmedia(mb &
   4191 			    TULIP_ROM_MB_MEDIA_CODE);
   4192 
   4193 			tlp_srom_media_info(sc, tsti, tm);
   4194 
   4195 			/*
   4196 			 * Override our default SIA settings if the
   4197 			 * SROM contains its own.
   4198 			 */
   4199 			if (mb & TULIP_ROM_MB_EXT) {
   4200 				tm->tm_siaconn = TULIP_ROM_GETW(sc->sc_srom,
   4201 				    mb_offset + TULIP_ROM_MB_CSR13);
   4202 				tm->tm_siatxrx = TULIP_ROM_GETW(sc->sc_srom,
   4203 				    mb_offset + TULIP_ROM_MB_CSR14);
   4204 				tm->tm_siagen = TULIP_ROM_GETW(sc->sc_srom,
   4205 				    mb_offset + TULIP_ROM_MB_CSR15);
   4206 			}
   4207 
   4208 			ifmedia_add(&sc->sc_mii.mii_media,
   4209 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4210 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4211 			break;
   4212 
   4213 		default:
   4214 			printf("%s: unknown media code 0x%02x\n",
   4215 			    sc->sc_dev.dv_xname,
   4216 			    mb & TULIP_ROM_MB_MEDIA_CODE);
   4217 			free(tm, M_DEVBUF);
   4218 		}
   4219 	}
   4220 
   4221 	/*
   4222 	 * XXX Autosense not yet supported.
   4223 	 */
   4224 
   4225 	romdef = TULIP_ROM_GETW(sc->sc_srom, leaf_offset +
   4226 	    TULIP_ROM_IL_SELECT_CONN_TYPE);
   4227 	switch (romdef) {
   4228 	case SELECT_CONN_TYPE_TP:
   4229 	case SELECT_CONN_TYPE_TP_AUTONEG:
   4230 	case SELECT_CONN_TYPE_TP_NOLINKPASS:
   4231 		defmedia = IFM_ETHER|IFM_10_T;
   4232 		break;
   4233 
   4234 	case SELECT_CONN_TYPE_TP_FDX:
   4235 		defmedia = IFM_ETHER|IFM_10_T|IFM_FDX;
   4236 		break;
   4237 
   4238 	case SELECT_CONN_TYPE_BNC:
   4239 		defmedia = IFM_ETHER|IFM_10_2;
   4240 		break;
   4241 
   4242 	case SELECT_CONN_TYPE_AUI:
   4243 		defmedia = IFM_ETHER|IFM_10_5;
   4244 		break;
   4245 #if 0 /* XXX */
   4246 	case SELECT_CONN_TYPE_ASENSE:
   4247 	case SELECT_CONN_TYPE_ASENSE_AUTONEG:
   4248 		defmedia = IFM_ETHER|IFM_AUTO;
   4249 		break;
   4250 #endif
   4251 	default:
   4252 		defmedia = 0;
   4253 	}
   4254 
   4255 	if (defmedia == 0) {
   4256 		/*
   4257 		 * XXX We should default to auto-sense.
   4258 		 */
   4259 		defmedia = IFM_ETHER|IFM_10_T;
   4260 	}
   4261 
   4262 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
   4263 
   4264 	tlp_print_media(sc);
   4265 }
   4266 
   4267 /*
   4268  * DECchip 2114x ISV media switch.
   4269  */
   4270 void	tlp_2114x_isv_tmsw_init __P((struct tulip_softc *));
   4271 void	tlp_2114x_isv_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
   4272 int	tlp_2114x_isv_tmsw_set __P((struct tulip_softc *));
   4273 
   4274 const struct tulip_mediasw tlp_2114x_isv_mediasw = {
   4275 	tlp_2114x_isv_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
   4276 };
   4277 
   4278 void
   4279 tlp_2114x_isv_tmsw_init(sc)
   4280 	struct tulip_softc *sc;
   4281 {
   4282 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4283 	struct ifmedia_entry *ife;
   4284 	struct mii_softc *phy;
   4285 	struct tulip_21x4x_media *tm;
   4286 	const struct tulip_srom_to_ifmedia *tsti;
   4287 	int i, devcnt, leaf_offset, m_cnt, type, length;
   4288 	int defmedia, miidef;
   4289 	u_int16_t word;
   4290 	u_int8_t *cp, *ncp;
   4291 
   4292 	defmedia = miidef = 0;
   4293 
   4294 	sc->sc_mii.mii_ifp = ifp;
   4295 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
   4296 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
   4297 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4298 
   4299 	/*
   4300 	 * Ignore `instance'; we may get a mixture of SIA and MII
   4301 	 * media, and `instance' is used to isolate or select the
   4302 	 * PHY on the MII as appropriate.  Note that duplicate media
   4303 	 * are disallowed, so ignoring `instance' is safe.
   4304 	 */
   4305 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, tlp_mediachange,
   4306 	    tlp_mediastatus);
   4307 
   4308 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   4309 	for (i = 0; i < devcnt; i++) {
   4310 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   4311 			break;
   4312 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   4313 		    sc->sc_devno)
   4314 			break;
   4315 	}
   4316 
   4317 	if (i == devcnt) {
   4318 		printf("%s: unable to locate info leaf in SROM\n",
   4319 		    sc->sc_dev.dv_xname);
   4320 		return;
   4321 	}
   4322 
   4323 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
   4324 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
   4325 
   4326 	/* XXX SELECT CONN TYPE */
   4327 
   4328 	cp = &sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
   4329 
   4330 	/*
   4331 	 * On some chips, the first thing in the Info Leaf is the
   4332 	 * GPIO pin direction data.
   4333 	 */
   4334 	switch (sc->sc_chip) {
   4335 	case TULIP_CHIP_21140:
   4336 	case TULIP_CHIP_21140A:
   4337 	case TULIP_CHIP_MX98713:
   4338 	case TULIP_CHIP_AX88140:
   4339 	case TULIP_CHIP_AX88141:
   4340 		sc->sc_gp_dir = *cp++;
   4341 		break;
   4342 
   4343 	default:
   4344 		/* Nothing. */
   4345 	}
   4346 
   4347 	/* Get the media count. */
   4348 	m_cnt = *cp++;
   4349 
   4350 	for (; m_cnt != 0; cp = ncp, m_cnt--) {
   4351 		/*
   4352 		 * Determine the type and length of this media block.
   4353 		 */
   4354 		if ((*cp & 0x80) == 0) {
   4355 			length = 4;
   4356 			type = TULIP_ROM_MB_21140_GPR;
   4357 		} else {
   4358 			length = (*cp++ & 0x7f) - 1;
   4359 			type = *cp++ & 0x3f;
   4360 		}
   4361 
   4362 		/* Compute the start of the next block. */
   4363 		ncp = cp + length;
   4364 
   4365 		/* Now, parse the block. */
   4366 		switch (type) {
   4367 		case TULIP_ROM_MB_21140_GPR:
   4368 			tlp_get_minst(sc);
   4369 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_GPR;
   4370 
   4371 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4372 			memset(tm, 0, sizeof(*tm));
   4373 
   4374 			tm->tm_type = TULIP_ROM_MB_21140_GPR;
   4375 			tm->tm_get = tlp_21140_gpio_get;
   4376 			tm->tm_set = tlp_21140_gpio_set;
   4377 
   4378 			/* First is the media type code. */
   4379 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4380 			    TULIP_ROM_MB_MEDIA_CODE);
   4381 			if (tsti == NULL) {
   4382 				/* Invalid media code. */
   4383 				free(tm, M_DEVBUF);
   4384 				break;
   4385 			}
   4386 
   4387 			/* Get defaults. */
   4388 			tlp_srom_media_info(sc, tsti, tm);
   4389 
   4390 			/* Next is any GPIO info for this media. */
   4391 			tm->tm_gpdata = cp[1];
   4392 
   4393 			/*
   4394 			 * Next is a word containing OPMODE information
   4395 			 * and info on how to detect if this media is
   4396 			 * active.
   4397 			 */
   4398 			word = TULIP_ROM_GETW(cp, 2);
   4399 			tm->tm_opmode = TULIP_ROM_MB_OPMODE(word);
   4400 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
   4401 				tm->tm_actmask =
   4402 				    TULIP_ROM_MB_BITPOS(word);
   4403 				tm->tm_actdata =
   4404 				    (word & TULIP_ROM_MB_POLARITY) ?
   4405 				    0 : tm->tm_actmask;
   4406 			}
   4407 
   4408 			ifmedia_add(&sc->sc_mii.mii_media,
   4409 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4410 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4411 			break;
   4412 
   4413 		case TULIP_ROM_MB_21140_MII:
   4414 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_MII;
   4415 
   4416 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4417 			memset(tm, 0, sizeof(*tm));
   4418 
   4419 			tm->tm_type = TULIP_ROM_MB_21140_MII;
   4420 			tm->tm_get = tlp_mii_getmedia;
   4421 			tm->tm_set = tlp_mii_setmedia;
   4422 			tm->tm_opmode = OPMODE_PS;
   4423 
   4424 			if (sc->sc_reset == NULL)
   4425 				sc->sc_reset = tlp_21140_reset;
   4426 
   4427 			/* First is the PHY number. */
   4428 			tm->tm_phyno = *cp++;
   4429 
   4430 			/* Next is the MII select sequence length and offset. */
   4431 			tm->tm_gp_length = *cp++;
   4432 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
   4433 			cp += tm->tm_gp_length;
   4434 
   4435 			/* Next is the MII reset sequence length and offset. */
   4436 			tm->tm_reset_length = *cp++;
   4437 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
   4438 			cp += tm->tm_reset_length;
   4439 
   4440 			/*
   4441 			 * The following items are left in the media block
   4442 			 * that we don't particularly care about:
   4443 			 *
   4444 			 *	capabilities		W
   4445 			 *	advertisement		W
   4446 			 *	full duplex		W
   4447 			 *	tx threshold		W
   4448 			 *
   4449 			 * These appear to be bits in the PHY registers,
   4450 			 * which our MII code handles on its own.
   4451 			 */
   4452 
   4453 			/*
   4454 			 * Before we probe the MII bus, we need to reset
   4455 			 * it and issue the selection sequence.
   4456 			 */
   4457 
   4458 			/* Set the direction of the pins... */
   4459 			TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   4460 
   4461 			for (i = 0; i < tm->tm_reset_length; i++) {
   4462 				delay(10);
   4463 				TULIP_WRITE(sc, CSR_GPP,
   4464 				    sc->sc_srom[tm->tm_reset_offset + i]);
   4465 			}
   4466 
   4467 			for (i = 0; i < tm->tm_gp_length; i++) {
   4468 				delay(10);
   4469 				TULIP_WRITE(sc, CSR_GPP,
   4470 				    sc->sc_srom[tm->tm_gp_offset + i]);
   4471 			}
   4472 
   4473 			/* If there were no sequences, just lower the pins. */
   4474 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   4475 				delay(10);
   4476 				TULIP_WRITE(sc, CSR_GPP, 0);
   4477 			}
   4478 
   4479 			/*
   4480 			 * Now, probe the MII for the PHY.  Note, we know
   4481 			 * the location of the PHY on the bus, but we don't
   4482 			 * particularly care; the MII code just likes to
   4483 			 * search the whole thing anyhow.
   4484 			 */
   4485 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
   4486 			    MII_PHY_ANY, tm->tm_phyno, 0);
   4487 
   4488 			/*
   4489 			 * Now, search for the PHY we hopefully just
   4490 			 * configured.  If it's not configured into the
   4491 			 * kernel, we lose.  The PHY's default media always
   4492 			 * takes priority.
   4493 			 */
   4494 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
   4495 			     phy != NULL;
   4496 			     phy = LIST_NEXT(phy, mii_list))
   4497 				if (phy->mii_offset == tm->tm_phyno)
   4498 					break;
   4499 			if (phy == NULL) {
   4500 				printf("%s: unable to configure MII\n",
   4501 				    sc->sc_dev.dv_xname);
   4502 				break;
   4503 			}
   4504 
   4505 			sc->sc_flags |= TULIPF_HAS_MII;
   4506 			sc->sc_tick = tlp_mii_tick;
   4507 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
   4508 			    phy->mii_inst);
   4509 
   4510 			/*
   4511 			 * Okay, now that we've found the PHY and the MII
   4512 			 * layer has added all of the media associated
   4513 			 * with that PHY, we need to traverse the media
   4514 			 * list, and add our `tm' to each entry's `aux'
   4515 			 * pointer.
   4516 			 *
   4517 			 * We do this by looking for media with our
   4518 			 * PHY's `instance'.
   4519 			 */
   4520 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   4521 			     ife != NULL;
   4522 			     ife = TAILQ_NEXT(ife, ifm_list)) {
   4523 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
   4524 					continue;
   4525 				ife->ifm_aux = tm;
   4526 			}
   4527 			break;
   4528 
   4529 		case TULIP_ROM_MB_21142_SIA:
   4530 			tlp_get_minst(sc);
   4531 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_SIA;
   4532 
   4533 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4534 			memset(tm, 0, sizeof(*tm));
   4535 
   4536 			tm->tm_type = TULIP_ROM_MB_21142_SIA;
   4537 			tm->tm_get = tlp_sia_get;
   4538 			tm->tm_set = tlp_sia_set;
   4539 
   4540 			/* First is the media type code. */
   4541 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4542 			    TULIP_ROM_MB_MEDIA_CODE);
   4543 			if (tsti == NULL) {
   4544 				/* Invalid media code. */
   4545 				free(tm, M_DEVBUF);
   4546 				break;
   4547 			}
   4548 
   4549 			/* Get defaults. */
   4550 			tlp_srom_media_info(sc, tsti, tm);
   4551 
   4552 			/*
   4553 			 * Override our default SIA settings if the
   4554 			 * SROM contains its own.
   4555 			 */
   4556 			if (cp[0] & 0x40) {
   4557 				tm->tm_siaconn = TULIP_ROM_GETW(cp, 1);
   4558 				tm->tm_siatxrx = TULIP_ROM_GETW(cp, 3);
   4559 				tm->tm_siagen  = TULIP_ROM_GETW(cp, 5);
   4560 				cp += 7;
   4561 			} else
   4562 				cp++;
   4563 
   4564 			/* Next is GPIO control/data. */
   4565 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 0);
   4566 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 2);
   4567 
   4568 			ifmedia_add(&sc->sc_mii.mii_media,
   4569 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4570 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4571 			break;
   4572 
   4573 		case TULIP_ROM_MB_21142_MII:
   4574 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_MII;
   4575 
   4576 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4577 			memset(tm, 0, sizeof(*tm));
   4578 
   4579 			tm->tm_type = TULIP_ROM_MB_21142_MII;
   4580 			tm->tm_get = tlp_mii_getmedia;
   4581 			tm->tm_set = tlp_mii_setmedia;
   4582 			tm->tm_opmode = OPMODE_PS;
   4583 
   4584 			if (sc->sc_reset == NULL)
   4585 				sc->sc_reset = tlp_21142_reset;
   4586 
   4587 			/* First is the PHY number. */
   4588 			tm->tm_phyno = *cp++;
   4589 
   4590 			/* Next is the MII select sequence length and offset. */
   4591 			tm->tm_gp_length = *cp++;
   4592 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
   4593 			cp += tm->tm_gp_length * 2;
   4594 
   4595 			/* Next is the MII reset sequence length and offset. */
   4596 			tm->tm_reset_length = *cp++;
   4597 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
   4598 			cp += tm->tm_reset_length * 2;
   4599 
   4600 			/*
   4601 			 * The following items are left in the media block
   4602 			 * that we don't particularly care about:
   4603 			 *
   4604 			 *	capabilities		W
   4605 			 *	advertisement		W
   4606 			 *	full duplex		W
   4607 			 *	tx threshold		W
   4608 			 *	MII interrupt		W
   4609 			 *
   4610 			 * These appear to be bits in the PHY registers,
   4611 			 * which our MII code handles on its own.
   4612 			 */
   4613 
   4614 			/*
   4615 			 * Before we probe the MII bus, we need to reset
   4616 			 * it and issue the selection sequence.
   4617 			 */
   4618 
   4619 			ncp = &sc->sc_srom[tm->tm_reset_offset];
   4620 			for (i = 0; i < tm->tm_reset_length; i++, ncp += 2) {
   4621 				delay(10);
   4622 				TULIP_WRITE(sc, CSR_SIAGEN,
   4623 				    TULIP_ROM_GETW(ncp, 0) << 16);
   4624 			}
   4625 
   4626 			ncp = &sc->sc_srom[tm->tm_gp_offset];
   4627 			for (i = 0; i < tm->tm_gp_length; i++, ncp += 2) {
   4628 				delay(10);
   4629 				TULIP_WRITE(sc, CSR_SIAGEN,
   4630 				    TULIP_ROM_GETW(ncp, 0) << 16);
   4631 			}
   4632 
   4633 			/* If there were no sequences, just lower the pins. */
   4634 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   4635 				delay(10);
   4636 				TULIP_WRITE(sc, CSR_SIAGEN, 0);
   4637 			}
   4638 
   4639 			/*
   4640 			 * Now, probe the MII for the PHY.  Note, we know
   4641 			 * the location of the PHY on the bus, but we don't
   4642 			 * particularly care; the MII code just likes to
   4643 			 * search the whole thing anyhow.
   4644 			 */
   4645 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
   4646 			    MII_PHY_ANY, tm->tm_phyno, 0);
   4647 
   4648 			/*
   4649 			 * Now, search for the PHY we hopefully just
   4650 			 * configured.  If it's not configured into the
   4651 			 * kernel, we lose.  The PHY's default media always
   4652 			 * takes priority.
   4653 			 */
   4654 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
   4655 			     phy != NULL;
   4656 			     phy = LIST_NEXT(phy, mii_list))
   4657 				if (phy->mii_offset == tm->tm_phyno)
   4658 					break;
   4659 			if (phy == NULL) {
   4660 				printf("%s: unable to configure MII\n",
   4661 				    sc->sc_dev.dv_xname);
   4662 				break;
   4663 			}
   4664 
   4665 			sc->sc_flags |= TULIPF_HAS_MII;
   4666 			sc->sc_tick = tlp_mii_tick;
   4667 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
   4668 			    phy->mii_inst);
   4669 
   4670 			/*
   4671 			 * Okay, now that we've found the PHY and the MII
   4672 			 * layer has added all of the media associated
   4673 			 * with that PHY, we need to traverse the media
   4674 			 * list, and add our `tm' to each entry's `aux'
   4675 			 * pointer.
   4676 			 *
   4677 			 * We do this by looking for media with our
   4678 			 * PHY's `instance'.
   4679 			 */
   4680 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   4681 			     ife != NULL;
   4682 			     ife = TAILQ_NEXT(ife, ifm_list)) {
   4683 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
   4684 					continue;
   4685 				ife->ifm_aux = tm;
   4686 			}
   4687 			break;
   4688 
   4689 		case TULIP_ROM_MB_21143_SYM:
   4690 			tlp_get_minst(sc);
   4691 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21143_SYM;
   4692 
   4693 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4694 			memset(tm, 0, sizeof(*tm));
   4695 
   4696 			tm->tm_type = TULIP_ROM_MB_21143_SYM;
   4697 			tm->tm_get = tlp_sia_get;
   4698 			tm->tm_set = tlp_sia_set;
   4699 
   4700 			/* First is the media type code. */
   4701 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4702 			    TULIP_ROM_MB_MEDIA_CODE);
   4703 			if (tsti == NULL) {
   4704 				/* Invalid media code. */
   4705 				free(tm, M_DEVBUF);
   4706 				break;
   4707 			}
   4708 
   4709 			/* Get defaults. */
   4710 			tlp_srom_media_info(sc, tsti, tm);
   4711 
   4712 			/* Next is GPIO control/data. */
   4713 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 1);
   4714 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 3);
   4715 
   4716 			/*
   4717 			 * Next is a word containing OPMODE information
   4718 			 * and info on how to detect if this media is
   4719 			 * active.
   4720 			 */
   4721 			word = TULIP_ROM_GETW(cp, 5);
   4722 			tm->tm_opmode = TULIP_ROM_MB_OPMODE(word);
   4723 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
   4724 				tm->tm_actmask =
   4725 				    TULIP_ROM_MB_BITPOS(word);
   4726 				tm->tm_actdata =
   4727 				    (word & TULIP_ROM_MB_POLARITY) ?
   4728 				    0 : tm->tm_actmask;
   4729 			}
   4730 
   4731 			ifmedia_add(&sc->sc_mii.mii_media,
   4732 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4733 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4734 			break;
   4735 
   4736 		case TULIP_ROM_MB_21143_RESET:
   4737 			printf("%s: 21143 reset block\n", sc->sc_dev.dv_xname);
   4738 			break;
   4739 
   4740 		default:
   4741 			printf("%s: unknown ISV media block type 0x%02x\n",
   4742 			    sc->sc_dev.dv_xname, type);
   4743 		}
   4744 	}
   4745 
   4746 	/*
   4747 	 * Deal with the case where no media is configured.
   4748 	 */
   4749 	if (TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list) == NULL) {
   4750 		printf("%s: no media found!\n", sc->sc_dev.dv_xname);
   4751 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   4752 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   4753 		return;
   4754 	}
   4755 
   4756 	/*
   4757 	 * Pick the default media.
   4758 	 */
   4759 	if (miidef != 0)
   4760 		defmedia = miidef;
   4761 	else {
   4762 		/*
   4763 		 * XXX Pick a better default.  Should come from SROM
   4764 		 * XXX on 21140[A], and should be "auto" on 21142,
   4765 		 * XXX 21143, and Macronix chips.
   4766 		 */
   4767 		defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
   4768 	}
   4769 
   4770 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
   4771 
   4772 	/*
   4773 	 * Display any non-MII media we've located.
   4774 	 */
   4775 	if (sc->sc_media_seen &
   4776 	    ~((1 << TULIP_ROM_MB_21140_MII) | (1 << TULIP_ROM_MB_21142_MII)))
   4777 		tlp_print_media(sc);
   4778 
   4779 	tlp_sia_fixup(sc);
   4780 }
   4781 
   4782 void
   4783 tlp_2114x_isv_tmsw_get(sc, ifmr)
   4784 	struct tulip_softc *sc;
   4785 	struct ifmediareq *ifmr;
   4786 {
   4787 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   4788 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   4789 
   4790 	/*
   4791 	 * We might be polling a non-MII autosense; check for that.
   4792 	 */
   4793 	if (tm == NULL) {
   4794 #ifdef DIAGNOSTIC
   4795 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   4796 			panic("tlp_2114x_isv_tmsw_get");
   4797 #endif
   4798 		tm = sc->sc_nway_active->ifm_aux;
   4799 	}
   4800 
   4801 	(*tm->tm_get)(sc, ifmr);
   4802 }
   4803 
   4804 int
   4805 tlp_2114x_isv_tmsw_set(sc)
   4806 	struct tulip_softc *sc;
   4807 {
   4808 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   4809 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   4810 
   4811 	/*
   4812 	 * We might be setting a non-MII autosense; check for that.
   4813 	 */
   4814 	if (tm == NULL) {
   4815 #ifdef DIAGNOSTIC
   4816 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   4817 			panic("tlp_2114x_isv_tmsw_set");
   4818 #endif
   4819 		/* XXX XXX XXX */
   4820 	}
   4821 
   4822 	/*
   4823 	 * Check to see if we need to reset the chip, and do it.  The
   4824 	 * reset path will get the OPMODE register right the next
   4825 	 * time through.
   4826 	 */
   4827 	if (TULIP_MEDIA_NEEDSRESET(sc, tm->tm_opmode))
   4828 		return (tlp_init(sc));
   4829 
   4830 	return ((*tm->tm_set)(sc));
   4831 }
   4832 
   4833 /*
   4834  * MII-on-SIO media switch.  Handles only MII attached to the SIO.
   4835  */
   4836 void	tlp_sio_mii_tmsw_init __P((struct tulip_softc *));
   4837 
   4838 const struct tulip_mediasw tlp_sio_mii_mediasw = {
   4839 	tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   4840 };
   4841 
   4842 void
   4843 tlp_sio_mii_tmsw_init(sc)
   4844 	struct tulip_softc *sc;
   4845 {
   4846 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4847 
   4848 	/*
   4849 	 * We don't attach any media info structures to the ifmedia
   4850 	 * entries, so if we're using a pre-init function that needs
   4851 	 * that info, override it to one that doesn't.
   4852 	 */
   4853 	if (sc->sc_preinit == tlp_2114x_preinit)
   4854 		sc->sc_preinit = tlp_2114x_mii_preinit;
   4855 
   4856 	sc->sc_mii.mii_ifp = ifp;
   4857 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
   4858 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
   4859 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4860 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4861 	    tlp_mediastatus);
   4862 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   4863 	    MII_OFFSET_ANY, 0);
   4864 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   4865 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   4866 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   4867 	} else {
   4868 		sc->sc_flags |= TULIPF_HAS_MII;
   4869 		sc->sc_tick = tlp_mii_tick;
   4870 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4871 	}
   4872 }
   4873 
   4874 /*
   4875  * Lite-On PNIC media switch.  Must handle MII or internal NWAY.
   4876  */
   4877 void	tlp_pnic_tmsw_init __P((struct tulip_softc *));
   4878 void	tlp_pnic_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
   4879 int	tlp_pnic_tmsw_set __P((struct tulip_softc *));
   4880 
   4881 const struct tulip_mediasw tlp_pnic_mediasw = {
   4882 	tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
   4883 };
   4884 
   4885 void	tlp_pnic_nway_statchg __P((struct device *));
   4886 void	tlp_pnic_nway_tick __P((void *));
   4887 int	tlp_pnic_nway_service __P((struct tulip_softc *, int));
   4888 void	tlp_pnic_nway_reset __P((struct tulip_softc *));
   4889 int	tlp_pnic_nway_auto __P((struct tulip_softc *, int));
   4890 void	tlp_pnic_nway_auto_timeout __P((void *));
   4891 void	tlp_pnic_nway_status __P((struct tulip_softc *));
   4892 void	tlp_pnic_nway_acomp __P((struct tulip_softc *));
   4893 
   4894 void
   4895 tlp_pnic_tmsw_init(sc)
   4896 	struct tulip_softc *sc;
   4897 {
   4898 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4899 	const char *sep = "";
   4900 
   4901 #define	ADD(m, c)	ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL)
   4902 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
   4903 
   4904 	sc->sc_mii.mii_ifp = ifp;
   4905 	sc->sc_mii.mii_readreg = tlp_pnic_mii_readreg;
   4906 	sc->sc_mii.mii_writereg = tlp_pnic_mii_writereg;
   4907 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4908 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4909 	    tlp_mediastatus);
   4910 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   4911 	    MII_OFFSET_ANY, 0);
   4912 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   4913 		/* XXX What about AUI/BNC support? */
   4914 		printf("%s: ", sc->sc_dev.dv_xname);
   4915 
   4916 		tlp_pnic_nway_reset(sc);
   4917 
   4918 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
   4919 		    PNIC_NWAY_TW|PNIC_NWAY_CAP10T);
   4920 		PRINT("10baseT");
   4921 
   4922 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
   4923 		    PNIC_NWAY_TW|PNIC_NWAY_FD|PNIC_NWAY_CAP10TFDX);
   4924 		PRINT("10baseT-FDX");
   4925 
   4926 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
   4927 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_CAP100TX);
   4928 		PRINT("100baseTX");
   4929 
   4930 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
   4931 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_FD|
   4932 		    PNIC_NWAY_CAP100TXFDX);
   4933 		PRINT("100baseTX-FDX");
   4934 
   4935 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
   4936 		    PNIC_NWAY_TW|PNIC_NWAY_RN|PNIC_NWAY_NW|
   4937 		    PNIC_NWAY_CAP10T|PNIC_NWAY_CAP10TFDX|
   4938 		    PNIC_NWAY_CAP100TXFDX|PNIC_NWAY_CAP100TX);
   4939 		PRINT("auto");
   4940 
   4941 		printf("\n");
   4942 
   4943 		sc->sc_statchg = tlp_pnic_nway_statchg;
   4944 		sc->sc_tick = tlp_pnic_nway_tick;
   4945 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4946 	} else {
   4947 		sc->sc_flags |= TULIPF_HAS_MII;
   4948 		sc->sc_tick = tlp_mii_tick;
   4949 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4950 	}
   4951 
   4952 #undef ADD
   4953 #undef PRINT
   4954 }
   4955 
   4956 void
   4957 tlp_pnic_tmsw_get(sc, ifmr)
   4958 	struct tulip_softc *sc;
   4959 	struct ifmediareq *ifmr;
   4960 {
   4961 	struct mii_data *mii = &sc->sc_mii;
   4962 
   4963 	if (sc->sc_flags & TULIPF_HAS_MII)
   4964 		tlp_mii_getmedia(sc, ifmr);
   4965 	else {
   4966 		mii->mii_media_status = 0;
   4967 		mii->mii_media_active = IFM_NONE;
   4968 		tlp_pnic_nway_service(sc, MII_POLLSTAT);
   4969 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
   4970 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
   4971 	}
   4972 }
   4973 
   4974 int
   4975 tlp_pnic_tmsw_set(sc)
   4976 	struct tulip_softc *sc;
   4977 {
   4978 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4979 	struct mii_data *mii = &sc->sc_mii;
   4980 
   4981 	if (sc->sc_flags & TULIPF_HAS_MII) {
   4982 		/*
   4983 		 * Make sure the built-in Tx jabber timer is disabled.
   4984 		 */
   4985 		TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);
   4986 
   4987 		return (tlp_mii_setmedia(sc));
   4988 	}
   4989 
   4990 	if (ifp->if_flags & IFF_UP) {
   4991 		mii->mii_media_status = 0;
   4992 		mii->mii_media_active = IFM_NONE;
   4993 		return (tlp_pnic_nway_service(sc, MII_MEDIACHG));
   4994 	}
   4995 
   4996 	return (0);
   4997 }
   4998 
   4999 void
   5000 tlp_pnic_nway_statchg(self)
   5001 	struct device *self;
   5002 {
   5003 	struct tulip_softc *sc = (struct tulip_softc *)self;
   5004 
   5005 	/* Idle the transmit and receive processes. */
   5006 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   5007 
   5008 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_PS|OPMODE_PCS|
   5009 	    OPMODE_SCR|OPMODE_HBD);
   5010 
   5011 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
   5012 		sc->sc_opmode |= OPMODE_TTM;
   5013 		TULIP_WRITE(sc, CSR_GPP,
   5014 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) |
   5015 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
   5016 	} else {
   5017 		sc->sc_opmode |= OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD;
   5018 		TULIP_WRITE(sc, CSR_GPP,
   5019 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) |
   5020 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
   5021 	}
   5022 
   5023 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   5024 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
   5025 
   5026 	/*
   5027 	 * Write new OPMODE bits.  This also restarts the transmit
   5028 	 * and receive processes.
   5029 	 */
   5030 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   5031 }
   5032 
   5033 void
   5034 tlp_pnic_nway_tick(arg)
   5035 	void *arg;
   5036 {
   5037 	struct tulip_softc *sc = arg;
   5038 	int s;
   5039 
   5040 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   5041 		return;
   5042 
   5043 	s = splnet();
   5044 	tlp_pnic_nway_service(sc, MII_TICK);
   5045 	splx(s);
   5046 
   5047 	callout_reset(&sc->sc_tick_callout, hz, tlp_pnic_nway_tick, sc);
   5048 }
   5049 
   5050 /*
   5051  * Support for the Lite-On PNIC internal NWay block.  This is constructed
   5052  * somewhat like a PHY driver for simplicity.
   5053  */
   5054 
   5055 int
   5056 tlp_pnic_nway_service(sc, cmd)
   5057 	struct tulip_softc *sc;
   5058 	int cmd;
   5059 {
   5060 	struct mii_data *mii = &sc->sc_mii;
   5061 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   5062 
   5063 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
   5064 		return (0);
   5065 
   5066 	switch (cmd) {
   5067 	case MII_POLLSTAT:
   5068 		/* Nothing special to do here. */
   5069 		break;
   5070 
   5071 	case MII_MEDIACHG:
   5072 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   5073 		case IFM_AUTO:
   5074 			(void) tlp_pnic_nway_auto(sc, 1);
   5075 			break;
   5076 		case IFM_100_T4:
   5077 			/*
   5078 			 * XXX Not supported as a manual setting right now.
   5079 			 */
   5080 			return (EINVAL);
   5081 		default:
   5082 			/*
   5083 			 * NWAY register data is stored in the ifmedia entry.
   5084 			 */
   5085 			TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   5086 		}
   5087 		break;
   5088 
   5089 	case MII_TICK:
   5090 		/*
   5091 		 * Only used for autonegotiation.
   5092 		 */
   5093 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   5094 			return (0);
   5095 
   5096 		/*
   5097 		 * Check to see if we have link.  If we do, we don't
   5098 		 * need to restart the autonegotiation process.
   5099 		 */
   5100 		if (sc->sc_flags & TULIPF_LINK_UP)
   5101 			return (0);
   5102 
   5103 		/*
   5104 		 * Only retry autonegotiation every 5 seconds.
   5105 		 */
   5106 		if (++sc->sc_nway_ticks != 5)
   5107 			return (0);
   5108 
   5109 		sc->sc_nway_ticks = 0;
   5110 		tlp_pnic_nway_reset(sc);
   5111 		if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
   5112 			return (0);
   5113 		break;
   5114 	}
   5115 
   5116 	/* Update the media status. */
   5117 	tlp_pnic_nway_status(sc);
   5118 
   5119 	/* Callback if something changed. */
   5120 	if ((sc->sc_nway_active == NULL ||
   5121 	     sc->sc_nway_active->ifm_media != mii->mii_media_active) ||
   5122 	    cmd == MII_MEDIACHG) {
   5123 		(*sc->sc_statchg)(&sc->sc_dev);
   5124 		tlp_nway_activate(sc, mii->mii_media_active);
   5125 	}
   5126 	return (0);
   5127 }
   5128 
   5129 void
   5130 tlp_pnic_nway_reset(sc)
   5131 	struct tulip_softc *sc;
   5132 {
   5133 
   5134 	TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
   5135 	delay(100);
   5136 	TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
   5137 }
   5138 
   5139 int
   5140 tlp_pnic_nway_auto(sc, waitfor)
   5141 	struct tulip_softc *sc;
   5142 	int waitfor;
   5143 {
   5144 	struct mii_data *mii = &sc->sc_mii;
   5145 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   5146 	u_int32_t reg;
   5147 	int i;
   5148 
   5149 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
   5150 		TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   5151 
   5152 	if (waitfor) {
   5153 		/* Wait 500ms for it to complete. */
   5154 		for (i = 0; i < 500; i++) {
   5155 			reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5156 			if (reg & PNIC_NWAY_LPAR_MASK) {
   5157 				tlp_pnic_nway_acomp(sc);
   5158 				return (0);
   5159 			}
   5160 			delay(1000);
   5161 		}
   5162 #if 0
   5163 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   5164 			printf("%s: autonegotiation failed to complete\n",
   5165 			    sc->sc_dev.dv_xname);
   5166 #endif
   5167 
   5168 		/*
   5169 		 * Don't need to worry about clearing DOINGAUTO.
   5170 		 * If that's set, a timeout is pending, and it will
   5171 		 * clear the flag.
   5172 		 */
   5173 		return (EIO);
   5174 	}
   5175 
   5176 	/*
   5177 	 * Just let it finish asynchronously.  This is for the benefit of
   5178 	 * the tick handler driving autonegotiation.  Don't want 500ms
   5179 	 * delays all the time while the system is running!
   5180 	 */
   5181 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
   5182 		sc->sc_flags |= TULIPF_DOINGAUTO;
   5183 		callout_reset(&sc->sc_nway_callout, hz >> 1,
   5184 		    tlp_pnic_nway_auto_timeout, sc);
   5185 	}
   5186 	return (EJUSTRETURN);
   5187 }
   5188 
   5189 void
   5190 tlp_pnic_nway_auto_timeout(arg)
   5191 	void *arg;
   5192 {
   5193 	struct tulip_softc *sc = arg;
   5194 	u_int32_t reg;
   5195 	int s;
   5196 
   5197 	s = splnet();
   5198 	sc->sc_flags &= ~TULIPF_DOINGAUTO;
   5199 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5200 #if 0
   5201 	if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   5202 		printf("%s: autonegotiation failed to complete\n",
   5203 		    sc->sc_dev.dv_xname);
   5204 #endif
   5205 
   5206 	tlp_pnic_nway_acomp(sc);
   5207 
   5208 	/* Update the media status. */
   5209 	(void) tlp_pnic_nway_service(sc, MII_POLLSTAT);
   5210 	splx(s);
   5211 }
   5212 
   5213 void
   5214 tlp_pnic_nway_status(sc)
   5215 	struct tulip_softc *sc;
   5216 {
   5217 	struct mii_data *mii = &sc->sc_mii;
   5218 	u_int32_t reg;
   5219 
   5220 	mii->mii_media_status = IFM_AVALID;
   5221 	mii->mii_media_active = IFM_ETHER;
   5222 
   5223 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5224 
   5225 	if (sc->sc_flags & TULIPF_LINK_UP)
   5226 		mii->mii_media_status |= IFM_ACTIVE;
   5227 
   5228 	if (reg & PNIC_NWAY_NW) {
   5229 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
   5230 			/* Erg, still trying, I guess... */
   5231 			mii->mii_media_active |= IFM_NONE;
   5232 			return;
   5233 		}
   5234 
   5235 #if 0
   5236 		if (reg & PNIC_NWAY_LPAR100T4)
   5237 			mii->mii_media_active |= IFM_100_T4;
   5238 		else
   5239 #endif
   5240 		if (reg & PNIC_NWAY_LPAR100TXFDX)
   5241 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
   5242 		else if (reg & PNIC_NWAY_LPAR100TX)
   5243 			mii->mii_media_active |= IFM_100_TX;
   5244 		else if (reg & PNIC_NWAY_LPAR10TFDX)
   5245 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
   5246 		else if (reg & PNIC_NWAY_LPAR10T)
   5247 			mii->mii_media_active |= IFM_10_T;
   5248 		else
   5249 			mii->mii_media_active |= IFM_NONE;
   5250 	} else {
   5251 		if (reg & PNIC_NWAY_100)
   5252 			mii->mii_media_active |= IFM_100_TX;
   5253 		else
   5254 			mii->mii_media_active |= IFM_10_T;
   5255 		if (reg & PNIC_NWAY_FD)
   5256 			mii->mii_media_active |= IFM_FDX;
   5257 	}
   5258 }
   5259 
   5260 void
   5261 tlp_pnic_nway_acomp(sc)
   5262 	struct tulip_softc *sc;
   5263 {
   5264 	u_int32_t reg;
   5265 
   5266 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5267 	reg &= ~(PNIC_NWAY_FD|PNIC_NWAY_100|PNIC_NWAY_RN);
   5268 
   5269 	if (reg & (PNIC_NWAY_LPAR100TXFDX|PNIC_NWAY_LPAR100TX))
   5270 		reg |= PNIC_NWAY_100;
   5271 	if (reg & (PNIC_NWAY_LPAR10TFDX|PNIC_NWAY_LPAR100TXFDX))
   5272 		reg |= PNIC_NWAY_FD;
   5273 
   5274 	TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
   5275 }
   5276 
   5277 /*
   5278  * Macronix PMAC and Lite-On PNIC-II media switch:
   5279  *
   5280  *	MX98713 and MX98713A		21140-like MII or GPIO media.
   5281  *
   5282  *	MX98713A			21143-like MII or SIA/SYM media.
   5283  *
   5284  *	MX98715, MX98715A, MX98725,	21143-like SIA/SYM media.
   5285  *	82C115
   5286  *
   5287  * So, what we do here is fake MII-on-SIO or ISV media info, and
   5288  * use the ISV media switch get/set functions to handle the rest.
   5289  */
   5290 
   5291 void	tlp_pmac_tmsw_init __P((struct tulip_softc *));
   5292 
   5293 const struct tulip_mediasw tlp_pmac_mediasw = {
   5294 	tlp_pmac_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
   5295 };
   5296 
   5297 const struct tulip_mediasw tlp_pmac_mii_mediasw = {
   5298 	tlp_pmac_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   5299 };
   5300 
   5301 void
   5302 tlp_pmac_tmsw_init(sc)
   5303 	struct tulip_softc *sc;
   5304 {
   5305 	static const u_int8_t media[] = {
   5306 		TULIP_ROM_MB_MEDIA_TP,
   5307 		TULIP_ROM_MB_MEDIA_TP_FDX,
   5308 		TULIP_ROM_MB_MEDIA_100TX,
   5309 		TULIP_ROM_MB_MEDIA_100TX_FDX,
   5310 	};
   5311 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   5312 
   5313 	sc->sc_mii.mii_ifp = ifp;
   5314 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
   5315 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
   5316 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   5317 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   5318 	    tlp_mediastatus);
   5319 	if (sc->sc_chip == TULIP_CHIP_MX98713 ||
   5320 	    sc->sc_chip == TULIP_CHIP_MX98713A) {
   5321 		mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
   5322 		    MII_PHY_ANY, MII_OFFSET_ANY, 0);
   5323 		if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL) {
   5324 			sc->sc_flags |= TULIPF_HAS_MII;
   5325 			sc->sc_tick = tlp_mii_tick;
   5326 			sc->sc_preinit = tlp_2114x_mii_preinit;
   5327 			sc->sc_mediasw = &tlp_pmac_mii_mediasw;
   5328 			ifmedia_set(&sc->sc_mii.mii_media,
   5329 			    IFM_ETHER|IFM_AUTO);
   5330 			return;
   5331 		}
   5332 	}
   5333 
   5334 	switch (sc->sc_chip) {
   5335 	case TULIP_CHIP_MX98713:
   5336 		tlp_add_srom_media(sc, TULIP_ROM_MB_21140_GPR,
   5337 		    tlp_21140_gpio_get, tlp_21140_gpio_set, media, 4);
   5338 
   5339 		/*
   5340 		 * XXX Should implement auto-sense for this someday,
   5341 		 * XXX when we do the same for the 21140.
   5342 		 */
   5343 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   5344 		break;
   5345 
   5346 	default:
   5347 		tlp_add_srom_media(sc, TULIP_ROM_MB_21142_SIA,
   5348 		    tlp_sia_get, tlp_sia_set, media, 2);
   5349 		tlp_add_srom_media(sc, TULIP_ROM_MB_21143_SYM,
   5350 		    tlp_sia_get, tlp_sia_set, media + 2, 2);
   5351 
   5352 		/*
   5353 		 * XXX Autonegotiation not yet supported.
   5354 		 */
   5355 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   5356 		break;
   5357 	}
   5358 
   5359 	tlp_print_media(sc);
   5360 	tlp_sia_fixup(sc);
   5361 
   5362 	/* Set the LED modes. */
   5363 	tlp_pmac_reset(sc);
   5364 
   5365 	sc->sc_reset = tlp_pmac_reset;
   5366 }
   5367 
   5368 /*
   5369  * ADMtek AL981 media switch.  Only has internal PHY.
   5370  */
   5371 void	tlp_al981_tmsw_init __P((struct tulip_softc *));
   5372 
   5373 const struct tulip_mediasw tlp_al981_mediasw = {
   5374 	tlp_al981_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   5375 };
   5376 
   5377 void
   5378 tlp_al981_tmsw_init(sc)
   5379 	struct tulip_softc *sc;
   5380 {
   5381 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   5382 
   5383 	sc->sc_mii.mii_ifp = ifp;
   5384 	sc->sc_mii.mii_readreg = tlp_al981_mii_readreg;
   5385 	sc->sc_mii.mii_writereg = tlp_al981_mii_writereg;
   5386 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   5387 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   5388 	    tlp_mediastatus);
   5389 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   5390 	    MII_OFFSET_ANY, 0);
   5391 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   5392 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   5393 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   5394 	} else {
   5395 		sc->sc_flags |= TULIPF_HAS_MII;
   5396 		sc->sc_tick = tlp_mii_tick;
   5397 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   5398 	}
   5399 }
   5400