Home | History | Annotate | Line # | Download | only in ic
tulip.c revision 1.64
      1 /*	$NetBSD: tulip.c,v 1.64 2000/05/18 03:02:45 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 #define	tlp_mchash(addr, sz)						\
    155 	(ether_crc32_le((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 				if (sc->sc_tdctl_er != TDCTL_ER) {
   1090 					tlp_idle(sc, OPMODE_SR);
   1091 					TULIP_WRITE(sc, CSR_RXLIST,
   1092 					    TULIP_CDRXADDR(sc, sc->sc_rxptr));
   1093 					TULIP_WRITE(sc, CSR_OPMODE,
   1094 					    sc->sc_opmode);
   1095 				}
   1096 				TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
   1097 				break;
   1098 			}
   1099 		}
   1100 
   1101 		if (txstatus) {
   1102 			/* Sweep up transmit descriptors. */
   1103 			tlp_txintr(sc);
   1104 
   1105 			if (txstatus & STATUS_TJT)
   1106 				printf("%s: transmit jabber timeout\n",
   1107 				    sc->sc_dev.dv_xname);
   1108 
   1109 			if (txstatus & STATUS_UNF) {
   1110 				/*
   1111 				 * Increase our transmit threshold if
   1112 				 * another is available.
   1113 				 */
   1114 				txthresh = sc->sc_txthresh + 1;
   1115 				if (sc->sc_txth[txthresh].txth_name != NULL) {
   1116 					/* Idle the transmit process. */
   1117 					tlp_idle(sc, OPMODE_ST);
   1118 
   1119 					sc->sc_txthresh = txthresh;
   1120 					sc->sc_opmode &= ~(OPMODE_TR|OPMODE_SF);
   1121 					sc->sc_opmode |=
   1122 					    sc->sc_txth[txthresh].txth_opmode;
   1123 					printf("%s: transmit underrun; new "
   1124 					    "threshold: %s\n",
   1125 					    sc->sc_dev.dv_xname,
   1126 					    sc->sc_txth[txthresh].txth_name);
   1127 
   1128 					/*
   1129 					 * Set the new threshold and restart
   1130 					 * the transmit process.
   1131 					 */
   1132 					TULIP_WRITE(sc, CSR_OPMODE,
   1133 					    sc->sc_opmode);
   1134 				}
   1135 					/*
   1136 					 * XXX Log every Nth underrun from
   1137 					 * XXX now on?
   1138 					 */
   1139 			}
   1140 		}
   1141 
   1142 		if (status & (STATUS_TPS|STATUS_RPS)) {
   1143 			if (status & STATUS_TPS)
   1144 				printf("%s: transmit process stopped\n",
   1145 				    sc->sc_dev.dv_xname);
   1146 			if (status & STATUS_RPS)
   1147 				printf("%s: receive process stopped\n",
   1148 				    sc->sc_dev.dv_xname);
   1149 			(void) tlp_init(sc);
   1150 			break;
   1151 		}
   1152 
   1153 		if (status & STATUS_SE) {
   1154 			const char *str;
   1155 			switch (status & STATUS_EB) {
   1156 			case STATUS_EB_PARITY:
   1157 				str = "parity error";
   1158 				break;
   1159 
   1160 			case STATUS_EB_MABT:
   1161 				str = "master abort";
   1162 				break;
   1163 
   1164 			case STATUS_EB_TABT:
   1165 				str = "target abort";
   1166 				break;
   1167 
   1168 			default:
   1169 				str = "unknown error";
   1170 				break;
   1171 			}
   1172 			printf("%s: fatal system error: %s\n",
   1173 			    sc->sc_dev.dv_xname, str);
   1174 			(void) tlp_init(sc);
   1175 			break;
   1176 		}
   1177 
   1178 		/*
   1179 		 * Not handled:
   1180 		 *
   1181 		 *	Transmit buffer unavailable -- normal
   1182 		 *	condition, nothing to do, really.
   1183 		 *
   1184 		 *	General purpose timer experied -- we don't
   1185 		 *	use the general purpose timer.
   1186 		 *
   1187 		 *	Early receive interrupt -- not available on
   1188 		 *	all chips, we just use RI.  We also only
   1189 		 *	use single-segment receive DMA, so this
   1190 		 *	is mostly useless.
   1191 		 */
   1192 	}
   1193 
   1194 	/* Try to get more packets going. */
   1195 	tlp_start(ifp);
   1196 
   1197 	return (handled);
   1198 }
   1199 
   1200 /*
   1201  * tlp_rxintr:
   1202  *
   1203  *	Helper; handle receive interrupts.
   1204  */
   1205 void
   1206 tlp_rxintr(sc)
   1207 	struct tulip_softc *sc;
   1208 {
   1209 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1210 	struct ether_header *eh;
   1211 	struct tulip_rxsoft *rxs;
   1212 	struct mbuf *m;
   1213 	u_int32_t rxstat;
   1214 	int i, len;
   1215 
   1216 	for (i = sc->sc_rxptr;; i = TULIP_NEXTRX(i)) {
   1217 		rxs = &sc->sc_rxsoft[i];
   1218 
   1219 		TULIP_CDRXSYNC(sc, i,
   1220 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1221 
   1222 		rxstat = le32toh(sc->sc_rxdescs[i].td_status);
   1223 
   1224 		if (rxstat & TDSTAT_OWN) {
   1225 			/*
   1226 			 * We have processed all of the receive buffers.
   1227 			 */
   1228 			break;
   1229 		}
   1230 
   1231 		/*
   1232 		 * Make sure the packet fit in one buffer.  This should
   1233 		 * always be the case.  But the Lite-On PNIC, rev 33
   1234 		 * has an awful receive engine bug, which may require
   1235 		 * a very icky work-around.
   1236 		 */
   1237 		if ((rxstat & (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) !=
   1238 		    (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) {
   1239 			printf("%s: incoming packet spilled, resetting\n",
   1240 			    sc->sc_dev.dv_xname);
   1241 			(void) tlp_init(sc);
   1242 			return;
   1243 		}
   1244 
   1245 		/*
   1246 		 * If any collisions were seen on the wire, count one.
   1247 		 */
   1248 		if (rxstat & TDSTAT_Rx_CS)
   1249 			ifp->if_collisions++;
   1250 
   1251 		/*
   1252 		 * If an error occured, update stats, clear the status
   1253 		 * word, and leave the packet buffer in place.  It will
   1254 		 * simply be reused the next time the ring comes around.
   1255 		 */
   1256 		if (rxstat & TDSTAT_ES) {
   1257 #define	PRINTERR(bit, str)						\
   1258 			if (rxstat & (bit))				\
   1259 				printf("%s: receive error: %s\n",	\
   1260 				    sc->sc_dev.dv_xname, str)
   1261 			ifp->if_ierrors++;
   1262 			PRINTERR(TDSTAT_Rx_DE, "descriptor error");
   1263 			PRINTERR(TDSTAT_Rx_RF, "runt frame");
   1264 			PRINTERR(TDSTAT_Rx_TL, "frame too long");
   1265 			PRINTERR(TDSTAT_Rx_RE, "MII error");
   1266 			PRINTERR(TDSTAT_Rx_DB, "dribbling bit");
   1267 			PRINTERR(TDSTAT_Rx_CE, "CRC error");
   1268 #undef PRINTERR
   1269 			TULIP_INIT_RXDESC(sc, i);
   1270 			continue;
   1271 		}
   1272 
   1273 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1274 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1275 
   1276 		/*
   1277 		 * No errors; receive the packet.  Note the Tulip
   1278 		 * includes the CRC with every packet; trim it.
   1279 		 */
   1280 		len = TDSTAT_Rx_LENGTH(rxstat) - ETHER_CRC_LEN;
   1281 
   1282 #ifdef __NO_STRICT_ALIGNMENT
   1283 		/*
   1284 		 * Allocate a new mbuf cluster.  If that fails, we are
   1285 		 * out of memory, and must drop the packet and recycle
   1286 		 * the buffer that's already attached to this descriptor.
   1287 		 */
   1288 		m = rxs->rxs_mbuf;
   1289 		if (tlp_add_rxbuf(sc, i) != 0) {
   1290 			ifp->if_ierrors++;
   1291 			TULIP_INIT_RXDESC(sc, i);
   1292 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1293 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1294 			continue;
   1295 		}
   1296 #else
   1297 		/*
   1298 		 * The Tulip's receive buffers must be 4-byte aligned.
   1299 		 * But this means that the data after the Ethernet header
   1300 		 * is misaligned.  We must allocate a new buffer and
   1301 		 * copy the data, shifted forward 2 bytes.
   1302 		 */
   1303 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1304 		if (m == NULL) {
   1305  dropit:
   1306 			ifp->if_ierrors++;
   1307 			TULIP_INIT_RXDESC(sc, i);
   1308 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1309 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1310 			continue;
   1311 		}
   1312 		if (len > (MHLEN - 2)) {
   1313 			MCLGET(m, M_DONTWAIT);
   1314 			if ((m->m_flags & M_EXT) == 0) {
   1315 				m_freem(m);
   1316 				goto dropit;
   1317 			}
   1318 		}
   1319 		m->m_data += 2;
   1320 
   1321 		/*
   1322 		 * Note that we use clusters for incoming frames, so the
   1323 		 * buffer is virtually contiguous.
   1324 		 */
   1325 		memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
   1326 
   1327 		/* Allow the receive descriptor to continue using its mbuf. */
   1328 		TULIP_INIT_RXDESC(sc, i);
   1329 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1330 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1331 #endif /* __NO_STRICT_ALIGNMENT */
   1332 
   1333 		ifp->if_ipackets++;
   1334 		eh = mtod(m, struct ether_header *);
   1335 		m->m_pkthdr.rcvif = ifp;
   1336 		m->m_pkthdr.len = m->m_len = len;
   1337 
   1338 #if NBPFILTER > 0
   1339 		/*
   1340 		 * Pass this up to any BPF listeners, but only
   1341 		 * pass it up the stack if its for us.
   1342 		 */
   1343 		if (ifp->if_bpf)
   1344 			bpf_mtap(ifp->if_bpf, m);
   1345 #endif /* NPBFILTER > 0 */
   1346 
   1347 		/*
   1348 		 * This test is outside the NBPFILTER block because
   1349 		 * on the 21140 we have to use Hash-Only mode due to
   1350 		 * a bug in the filter logic.
   1351 		 */
   1352 		if ((ifp->if_flags & IFF_PROMISC) != 0 ||
   1353 		    sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
   1354 			if (memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
   1355 				 ETHER_ADDR_LEN) != 0 &&
   1356 			    ETHER_IS_MULTICAST(eh->ether_dhost) == 0) {
   1357 				m_freem(m);
   1358 				continue;
   1359 			}
   1360 		}
   1361 
   1362 		/* Pass it on. */
   1363 		(*ifp->if_input)(ifp, m);
   1364 	}
   1365 
   1366 	/* Update the recieve pointer. */
   1367 	sc->sc_rxptr = i;
   1368 }
   1369 
   1370 /*
   1371  * tlp_txintr:
   1372  *
   1373  *	Helper; handle transmit interrupts.
   1374  */
   1375 void
   1376 tlp_txintr(sc)
   1377 	struct tulip_softc *sc;
   1378 {
   1379 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1380 	struct tulip_txsoft *txs;
   1381 	u_int32_t txstat;
   1382 
   1383 	DPRINTF(sc, ("%s: tlp_txintr: sc_flags 0x%08x\n",
   1384 	    sc->sc_dev.dv_xname, sc->sc_flags));
   1385 
   1386 	ifp->if_flags &= ~IFF_OACTIVE;
   1387 
   1388 	/*
   1389 	 * Go through our Tx list and free mbufs for those
   1390 	 * frames that have been transmitted.
   1391 	 */
   1392 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1393 		TULIP_CDTXSYNC(sc, txs->txs_lastdesc,
   1394 		    txs->txs_ndescs,
   1395 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1396 
   1397 #ifdef TLP_DEBUG
   1398 		if (ifp->if_flags & IFF_DEBUG) {
   1399 			int i;
   1400 			printf("    txsoft %p trainsmit chain:\n", txs);
   1401 			for (i = txs->txs_firstdesc;; i = TULIP_NEXTTX(i)) {
   1402 				printf("     descriptor %d:\n", i);
   1403 				printf("       td_status:   0x%08x\n",
   1404 				    le32toh(sc->sc_txdescs[i].td_status));
   1405 				printf("       td_ctl:      0x%08x\n",
   1406 				    le32toh(sc->sc_txdescs[i].td_ctl));
   1407 				printf("       td_bufaddr1: 0x%08x\n",
   1408 				    le32toh(sc->sc_txdescs[i].td_bufaddr1));
   1409 				printf("       td_bufaddr2: 0x%08x\n",
   1410 				    le32toh(sc->sc_txdescs[i].td_bufaddr2));
   1411 				if (i == txs->txs_lastdesc)
   1412 					break;
   1413 			}
   1414 		}
   1415 #endif
   1416 
   1417 		txstat = le32toh(sc->sc_txdescs[txs->txs_lastdesc].td_status);
   1418 		if (txstat & TDSTAT_OWN)
   1419 			break;
   1420 
   1421 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
   1422 
   1423 		sc->sc_txfree += txs->txs_ndescs;
   1424 
   1425 		if (txs->txs_mbuf == NULL) {
   1426 			/*
   1427 			 * If we didn't have an mbuf, it was the setup
   1428 			 * packet.
   1429 			 */
   1430 #ifdef DIAGNOSTIC
   1431 			if ((sc->sc_flags & TULIPF_DOING_SETUP) == 0)
   1432 				panic("tlp_txintr: null mbuf, not doing setup");
   1433 #endif
   1434 			TULIP_CDSPSYNC(sc, BUS_DMASYNC_POSTWRITE);
   1435 			sc->sc_flags &= ~TULIPF_DOING_SETUP;
   1436 			SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1437 			continue;
   1438 		}
   1439 
   1440 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   1441 		    0, txs->txs_dmamap->dm_mapsize,
   1442 		    BUS_DMASYNC_POSTWRITE);
   1443 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1444 		m_freem(txs->txs_mbuf);
   1445 		txs->txs_mbuf = NULL;
   1446 
   1447 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1448 
   1449 		/*
   1450 		 * Check for errors and collisions.
   1451 		 */
   1452 #ifdef TLP_STATS
   1453 		if (txstat & TDSTAT_Tx_UF)
   1454 			sc->sc_stats.ts_tx_uf++;
   1455 		if (txstat & TDSTAT_Tx_TO)
   1456 			sc->sc_stats.ts_tx_to++;
   1457 		if (txstat & TDSTAT_Tx_EC)
   1458 			sc->sc_stats.ts_tx_ec++;
   1459 		if (txstat & TDSTAT_Tx_LC)
   1460 			sc->sc_stats.ts_tx_lc++;
   1461 #endif
   1462 
   1463 		if (txstat & (TDSTAT_Tx_UF|TDSTAT_Tx_TO))
   1464 			ifp->if_oerrors++;
   1465 
   1466 		if (txstat & TDSTAT_Tx_EC)
   1467 			ifp->if_collisions += 16;
   1468 		else
   1469 			ifp->if_collisions += TDSTAT_Tx_COLLISIONS(txstat);
   1470 		if (txstat & TDSTAT_Tx_LC)
   1471 			ifp->if_collisions++;
   1472 
   1473 		ifp->if_opackets++;
   1474 	}
   1475 
   1476 	/*
   1477 	 * If there are no more pending transmissions, cancel the watchdog
   1478 	 * timer.
   1479 	 */
   1480 	if (txs == NULL && (sc->sc_flags & TULIPF_DOING_SETUP) == 0)
   1481 		ifp->if_timer = 0;
   1482 
   1483 	/*
   1484 	 * If we have a receive filter setup pending, do it now.
   1485 	 */
   1486 	if (sc->sc_flags & TULIPF_WANT_SETUP)
   1487 		(*sc->sc_filter_setup)(sc);
   1488 }
   1489 
   1490 #ifdef TLP_STATS
   1491 void
   1492 tlp_print_stats(sc)
   1493 	struct tulip_softc *sc;
   1494 {
   1495 
   1496 	printf("%s: tx_uf %lu, tx_to %lu, tx_ec %lu, tx_lc %lu\n",
   1497 	    sc->sc_dev.dv_xname,
   1498 	    sc->sc_stats.ts_tx_uf, sc->sc_stats.ts_tx_to,
   1499 	    sc->sc_stats.ts_tx_ec, sc->sc_stats.ts_tx_lc);
   1500 }
   1501 #endif
   1502 
   1503 /*
   1504  * tlp_reset:
   1505  *
   1506  *	Perform a soft reset on the Tulip.
   1507  */
   1508 void
   1509 tlp_reset(sc)
   1510 	struct tulip_softc *sc;
   1511 {
   1512 	int i;
   1513 
   1514 	TULIP_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR);
   1515 
   1516 	/*
   1517 	 * Xircom clone doesn't bring itself out of reset automatically.
   1518 	 * Instead, we have to wait at least 50 PCI cycles, and then
   1519 	 * clear SWR.
   1520 	 */
   1521 	if (sc->sc_chip == TULIP_CHIP_X3201_3) {
   1522 		delay(10);
   1523 		TULIP_WRITE(sc, CSR_BUSMODE, 0);
   1524 	}
   1525 
   1526 	for (i = 0; i < 1000; i++) {
   1527 		/*
   1528 		 * Wait at least 50 PCI cycles for the reset to
   1529 		 * complete before peeking at the Tulip again.
   1530 		 * 10 uSec is a bit longer than 50 PCI cycles
   1531 		 * (at 33MHz), but it doesn't hurt have the extra
   1532 		 * wait.
   1533 		 */
   1534 		delay(10);
   1535 		if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0)
   1536 			break;
   1537 	}
   1538 
   1539 	if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR))
   1540 		printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
   1541 
   1542 	delay(1000);
   1543 
   1544 	/*
   1545 	 * If the board has any GPIO reset sequences to issue, do them now.
   1546 	 */
   1547 	if (sc->sc_reset != NULL)
   1548 		(*sc->sc_reset)(sc);
   1549 }
   1550 
   1551 /*
   1552  * tlp_init:
   1553  *
   1554  *	Initialize the interface.  Must be called at splnet().
   1555  */
   1556 int
   1557 tlp_init(sc)
   1558 	struct tulip_softc *sc;
   1559 {
   1560 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1561 	struct tulip_txsoft *txs;
   1562 	struct tulip_rxsoft *rxs;
   1563 	int i, error = 0;
   1564 
   1565 	/*
   1566 	 * Cancel any pending I/O.
   1567 	 */
   1568 	tlp_stop(sc, 0);
   1569 
   1570 	/*
   1571 	 * Initialize `opmode' to 0, and call the pre-init routine, if
   1572 	 * any.  This is required because the 2114x and some of the
   1573 	 * clones require that the media-related bits in `opmode' be
   1574 	 * set before performing a soft-reset in order to get internal
   1575 	 * chip pathways are correct.  Yay!
   1576 	 */
   1577 	sc->sc_opmode = 0;
   1578 	if (sc->sc_preinit != NULL)
   1579 		(*sc->sc_preinit)(sc);
   1580 
   1581 	/*
   1582 	 * Reset the Tulip to a known state.
   1583 	 */
   1584 	tlp_reset(sc);
   1585 
   1586 	/*
   1587 	 * Initialize the BUSMODE register.
   1588 	 */
   1589 	sc->sc_busmode = BUSMODE_BAR;
   1590 	switch (sc->sc_chip) {
   1591 	case TULIP_CHIP_21140:
   1592 	case TULIP_CHIP_21140A:
   1593 	case TULIP_CHIP_21142:
   1594 	case TULIP_CHIP_21143:
   1595 	case TULIP_CHIP_82C115:
   1596 	case TULIP_CHIP_MX98725:
   1597 		/*
   1598 		 * If we're allowed to do so, use Memory Read Line
   1599 		 * and Memory Read Multiple.
   1600 		 *
   1601 		 * XXX Should we use Memory Write and Invalidate?
   1602 		 */
   1603 		if (sc->sc_flags & TULIPF_MRL)
   1604 			sc->sc_busmode |= BUSMODE_RLE;
   1605 		if (sc->sc_flags & TULIPF_MRM)
   1606 			sc->sc_busmode |= BUSMODE_RME;
   1607 #if 0
   1608 		if (sc->sc_flags & TULIPF_MWI)
   1609 			sc->sc_busmode |= BUSMODE_WLE;
   1610 #endif
   1611 
   1612 	default:
   1613 		/* Nothing. */
   1614 	}
   1615 	switch (sc->sc_cacheline) {
   1616 	default:
   1617 		/*
   1618 		 * Note: We must *always* set these bits; a cache
   1619 		 * alignment of 0 is RESERVED.
   1620 		 */
   1621 	case 8:
   1622 		sc->sc_busmode |= BUSMODE_CAL_8LW;
   1623 		break;
   1624 	case 16:
   1625 		sc->sc_busmode |= BUSMODE_CAL_16LW;
   1626 		break;
   1627 	case 32:
   1628 		sc->sc_busmode |= BUSMODE_CAL_32LW;
   1629 		break;
   1630 	}
   1631 	switch (sc->sc_chip) {
   1632 	case TULIP_CHIP_82C168:
   1633 	case TULIP_CHIP_82C169:
   1634 		sc->sc_busmode |= BUSMODE_PBL_16LW | BUSMODE_PNIC_MBO;
   1635 		break;
   1636 	default:
   1637 		sc->sc_busmode |= BUSMODE_PBL_DEFAULT;
   1638 		break;
   1639 	}
   1640 #if BYTE_ORDER == BIG_ENDIAN
   1641 	/*
   1642 	 * Can't use BUSMODE_BLE or BUSMODE_DBO; not all chips
   1643 	 * support them, and even on ones that do, it doesn't
   1644 	 * always work.
   1645 	 */
   1646 #endif
   1647 	TULIP_WRITE(sc, CSR_BUSMODE, sc->sc_busmode);
   1648 
   1649 	/*
   1650 	 * Initialize the OPMODE register.  We don't write it until
   1651 	 * we're ready to begin the transmit and receive processes.
   1652 	 *
   1653 	 * Media-related OPMODE bits are set in the media callbacks
   1654 	 * for each specific chip/board.
   1655 	 */
   1656 	sc->sc_opmode |= OPMODE_SR | OPMODE_ST |
   1657 	    sc->sc_txth[sc->sc_txthresh].txth_opmode;
   1658 
   1659 	/*
   1660 	 * Magical mystery initialization on the Macronix chips.
   1661 	 * The MX98713 uses its own magic value, the rest share
   1662 	 * a common one.
   1663 	 */
   1664 	switch (sc->sc_chip) {
   1665 	case TULIP_CHIP_MX98713:
   1666 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98713);
   1667 		break;
   1668 
   1669 	case TULIP_CHIP_MX98713A:
   1670 	case TULIP_CHIP_MX98715:
   1671 	case TULIP_CHIP_MX98715A:
   1672 	case TULIP_CHIP_MX98725:
   1673 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98715);
   1674 		break;
   1675 
   1676 	default:
   1677 		/* Nothing. */
   1678 	}
   1679 
   1680 	/*
   1681 	 * Initialize the transmit descriptor ring.
   1682 	 */
   1683 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
   1684 	for (i = 0; i < TULIP_NTXDESC; i++) {
   1685 		sc->sc_txdescs[i].td_ctl = htole32(sc->sc_tdctl_ch);
   1686 		sc->sc_txdescs[i].td_bufaddr2 =
   1687 		    htole32(TULIP_CDTXADDR(sc, TULIP_NEXTTX(i)));
   1688 	}
   1689 	sc->sc_txdescs[TULIP_NTXDESC - 1].td_ctl |= htole32(sc->sc_tdctl_er);
   1690 	TULIP_CDTXSYNC(sc, 0, TULIP_NTXDESC,
   1691 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1692 	sc->sc_txfree = TULIP_NTXDESC;
   1693 	sc->sc_txnext = 0;
   1694 
   1695 	/*
   1696 	 * Initialize the transmit job descriptors.
   1697 	 */
   1698 	SIMPLEQ_INIT(&sc->sc_txfreeq);
   1699 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
   1700 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
   1701 		txs = &sc->sc_txsoft[i];
   1702 		txs->txs_mbuf = NULL;
   1703 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1704 	}
   1705 
   1706 	/*
   1707 	 * Initialize the receive descriptor and receive job
   1708 	 * descriptor rings.
   1709 	 */
   1710 	for (i = 0; i < TULIP_NRXDESC; i++) {
   1711 		rxs = &sc->sc_rxsoft[i];
   1712 		if (rxs->rxs_mbuf == NULL) {
   1713 			if ((error = tlp_add_rxbuf(sc, i)) != 0) {
   1714 				printf("%s: unable to allocate or map rx "
   1715 				    "buffer %d, error = %d\n",
   1716 				    sc->sc_dev.dv_xname, i, error);
   1717 				/*
   1718 				 * XXX Should attempt to run with fewer receive
   1719 				 * XXX buffers instead of just failing.
   1720 				 */
   1721 				tlp_rxdrain(sc);
   1722 				goto out;
   1723 			}
   1724 		}
   1725 	}
   1726 	sc->sc_rxptr = 0;
   1727 
   1728 	/*
   1729 	 * Initialize the interrupt mask and enable interrupts.
   1730 	 */
   1731 	/* normal interrupts */
   1732 	sc->sc_inten =  STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
   1733 
   1734 	/* abnormal interrupts */
   1735 	sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
   1736 	    STATUS_RU | STATUS_RPS | STATUS_RWT | STATUS_SE | STATUS_AIS;
   1737 
   1738 	sc->sc_rxint_mask = STATUS_RI|STATUS_RU|STATUS_RWT;
   1739 	sc->sc_txint_mask = STATUS_TI|STATUS_UNF|STATUS_TJT;
   1740 
   1741 	switch (sc->sc_chip) {
   1742 	case TULIP_CHIP_WB89C840F:
   1743 		/*
   1744 		 * Clear bits that we don't want that happen to
   1745 		 * overlap or don't exist.
   1746 		 */
   1747 		sc->sc_inten &= ~(STATUS_WINB_REI|STATUS_RWT);
   1748 		break;
   1749 
   1750 	default:
   1751 		/* Nothing. */
   1752 	}
   1753 
   1754 	sc->sc_rxint_mask &= sc->sc_inten;
   1755 	sc->sc_txint_mask &= sc->sc_inten;
   1756 
   1757 	TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
   1758 	TULIP_WRITE(sc, CSR_STATUS, 0xffffffff);
   1759 
   1760 	/*
   1761 	 * Give the transmit and receive rings to the Tulip.
   1762 	 */
   1763 	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDTXADDR(sc, sc->sc_txnext));
   1764 	TULIP_WRITE(sc, CSR_RXLIST, TULIP_CDRXADDR(sc, sc->sc_rxptr));
   1765 
   1766 	/*
   1767 	 * On chips that do this differently, set the station address.
   1768 	 */
   1769 	switch (sc->sc_chip) {
   1770 	case TULIP_CHIP_WB89C840F:
   1771 	    {
   1772 		/* XXX Do this with stream writes? */
   1773 		bus_addr_t cpa = TULIP_CSR_OFFSET(sc, CSR_WINB_CPA0);
   1774 
   1775 		for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1776 			bus_space_write_1(sc->sc_st, sc->sc_sh,
   1777 			    cpa + i, LLADDR(ifp->if_sadl)[i]);
   1778 		}
   1779 		break;
   1780 	    }
   1781 
   1782 	case TULIP_CHIP_AL981:
   1783 	    {
   1784 		u_int32_t reg;
   1785 		u_int8_t *enaddr = LLADDR(ifp->if_sadl);
   1786 
   1787 		reg = enaddr[0] |
   1788 		      (enaddr[1] << 8) |
   1789 		      (enaddr[2] << 16) |
   1790 		      (enaddr[3] << 24);
   1791 		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR0, reg);
   1792 
   1793 		reg = enaddr[4] |
   1794 		      (enaddr[5] << 8);
   1795 		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR1, reg);
   1796 	    }
   1797 
   1798 	default:
   1799 		/* Nothing. */
   1800 	}
   1801 
   1802 	/*
   1803 	 * Set the receive filter.  This will start the transmit and
   1804 	 * receive processes.
   1805 	 */
   1806 	(*sc->sc_filter_setup)(sc);
   1807 
   1808 	/*
   1809 	 * Set the current media.
   1810 	 */
   1811 	(void) (*sc->sc_mediasw->tmsw_set)(sc);
   1812 
   1813 	/*
   1814 	 * Start the receive process.
   1815 	 */
   1816 	TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
   1817 
   1818 	if (sc->sc_tick != NULL) {
   1819 		/* Start the one second clock. */
   1820 		callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc);
   1821 	}
   1822 
   1823 	/*
   1824 	 * Note that the interface is now running.
   1825 	 */
   1826 	ifp->if_flags |= IFF_RUNNING;
   1827 	ifp->if_flags &= ~IFF_OACTIVE;
   1828 
   1829  out:
   1830 	if (error)
   1831 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1832 	return (error);
   1833 }
   1834 
   1835 /*
   1836  * tlp_enable:
   1837  *
   1838  *	Enable the Tulip chip.
   1839  */
   1840 int
   1841 tlp_enable(sc)
   1842 	struct tulip_softc *sc;
   1843 {
   1844 
   1845 	if (TULIP_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
   1846 		if ((*sc->sc_enable)(sc) != 0) {
   1847 			printf("%s: device enable failed\n",
   1848 			    sc->sc_dev.dv_xname);
   1849 			return (EIO);
   1850 		}
   1851 		sc->sc_flags |= TULIPF_ENABLED;
   1852 	}
   1853 	return (0);
   1854 }
   1855 
   1856 /*
   1857  * tlp_disable:
   1858  *
   1859  *	Disable the Tulip chip.
   1860  */
   1861 void
   1862 tlp_disable(sc)
   1863 	struct tulip_softc *sc;
   1864 {
   1865 
   1866 	if (TULIP_IS_ENABLED(sc) && sc->sc_disable != NULL) {
   1867 		(*sc->sc_disable)(sc);
   1868 		sc->sc_flags &= ~TULIPF_ENABLED;
   1869 	}
   1870 }
   1871 
   1872 /*
   1873  * tlp_power:
   1874  *
   1875  *	Power management (suspend/resume) hook.
   1876  */
   1877 void
   1878 tlp_power(why, arg)
   1879 	int why;
   1880 	void *arg;
   1881 {
   1882 	struct tulip_softc *sc = arg;
   1883 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1884 	int s;
   1885 
   1886 	s = splnet();
   1887 	if (why != PWR_RESUME) {
   1888 		tlp_stop(sc, 0);
   1889 		if (sc->sc_power != NULL)
   1890 			(*sc->sc_power)(sc, why);
   1891 	} else if (ifp->if_flags & IFF_UP) {
   1892 		if (sc->sc_power != NULL)
   1893 			(*sc->sc_power)(sc, why);
   1894 		tlp_init(sc);
   1895 	}
   1896 	splx(s);
   1897 }
   1898 
   1899 /*
   1900  * tlp_rxdrain:
   1901  *
   1902  *	Drain the receive queue.
   1903  */
   1904 void
   1905 tlp_rxdrain(sc)
   1906 	struct tulip_softc *sc;
   1907 {
   1908 	struct tulip_rxsoft *rxs;
   1909 	int i;
   1910 
   1911 	for (i = 0; i < TULIP_NRXDESC; i++) {
   1912 		rxs = &sc->sc_rxsoft[i];
   1913 		if (rxs->rxs_mbuf != NULL) {
   1914 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1915 			m_freem(rxs->rxs_mbuf);
   1916 			rxs->rxs_mbuf = NULL;
   1917 		}
   1918 	}
   1919 }
   1920 
   1921 /*
   1922  * tlp_stop:
   1923  *
   1924  *	Stop transmission on the interface.
   1925  */
   1926 void
   1927 tlp_stop(sc, drain)
   1928 	struct tulip_softc *sc;
   1929 	int drain;
   1930 {
   1931 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1932 	struct tulip_txsoft *txs;
   1933 
   1934 	if (sc->sc_tick != NULL) {
   1935 		/* Stop the one second clock. */
   1936 		callout_stop(&sc->sc_tick_callout);
   1937 	}
   1938 
   1939 	if (sc->sc_flags & TULIPF_HAS_MII) {
   1940 		/* Down the MII. */
   1941 		mii_down(&sc->sc_mii);
   1942 	}
   1943 
   1944 	/* Disable interrupts. */
   1945 	TULIP_WRITE(sc, CSR_INTEN, 0);
   1946 
   1947 	/* Stop the transmit and receive processes. */
   1948 	TULIP_WRITE(sc, CSR_OPMODE, 0);
   1949 	TULIP_WRITE(sc, CSR_RXLIST, 0);
   1950 	TULIP_WRITE(sc, CSR_TXLIST, 0);
   1951 
   1952 	/*
   1953 	 * Release any queued transmit buffers.
   1954 	 */
   1955 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1956 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
   1957 		if (txs->txs_mbuf != NULL) {
   1958 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1959 			m_freem(txs->txs_mbuf);
   1960 			txs->txs_mbuf = NULL;
   1961 		}
   1962 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1963 	}
   1964 
   1965 	if (drain) {
   1966 		/*
   1967 		 * Release the receive buffers.
   1968 		 */
   1969 		tlp_rxdrain(sc);
   1970 	}
   1971 
   1972 	sc->sc_flags &= ~(TULIPF_WANT_SETUP|TULIPF_DOING_SETUP);
   1973 
   1974 	/*
   1975 	 * Mark the interface down and cancel the watchdog timer.
   1976 	 */
   1977 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1978 	ifp->if_timer = 0;
   1979 }
   1980 
   1981 #define	SROM_EMIT(sc, x)						\
   1982 do {									\
   1983 	TULIP_WRITE((sc), CSR_MIIROM, (x));				\
   1984 	delay(2);							\
   1985 } while (0)
   1986 
   1987 /*
   1988  * tlp_srom_idle:
   1989  *
   1990  *	Put the SROM in idle state.
   1991  */
   1992 void
   1993 tlp_srom_idle(sc)
   1994 	struct tulip_softc *sc;
   1995 {
   1996 	u_int32_t miirom;
   1997 	int i;
   1998 
   1999 	miirom = MIIROM_SR;
   2000 	SROM_EMIT(sc, miirom);
   2001 
   2002 	miirom |= MIIROM_RD;
   2003 	SROM_EMIT(sc, miirom);
   2004 
   2005 	miirom |= MIIROM_SROMCS;
   2006 	SROM_EMIT(sc, miirom);
   2007 
   2008 	SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2009 
   2010 	/* Strobe the clock 32 times. */
   2011 	for (i = 0; i < 32; i++) {
   2012 		SROM_EMIT(sc, miirom);
   2013 		SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2014 	}
   2015 
   2016 	SROM_EMIT(sc, miirom);
   2017 
   2018 	miirom &= ~MIIROM_SROMCS;
   2019 	SROM_EMIT(sc, miirom);
   2020 
   2021 	SROM_EMIT(sc, 0);
   2022 }
   2023 
   2024 /*
   2025  * tlp_srom_size:
   2026  *
   2027  *	Determine the number of address bits in the SROM.
   2028  */
   2029 int
   2030 tlp_srom_size(sc)
   2031 	struct tulip_softc *sc;
   2032 {
   2033 	u_int32_t miirom;
   2034 	int x;
   2035 
   2036 	/* Select the SROM. */
   2037 	miirom = MIIROM_SR;
   2038 	SROM_EMIT(sc, miirom);
   2039 
   2040 	miirom |= MIIROM_RD;
   2041 	SROM_EMIT(sc, miirom);
   2042 
   2043 	/* Send CHIP SELECT for one clock tick. */
   2044 	miirom |= MIIROM_SROMCS;
   2045 	SROM_EMIT(sc, miirom);
   2046 
   2047 	/* Shift in the READ opcode. */
   2048 	for (x = 3; x > 0; x--) {
   2049 		if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
   2050 			miirom |= MIIROM_SROMDI;
   2051 		else
   2052 			miirom &= ~MIIROM_SROMDI;
   2053 		SROM_EMIT(sc, miirom);
   2054 		SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2055 		SROM_EMIT(sc, miirom);
   2056 	}
   2057 
   2058 	/* Shift in address and look for dummy 0 bit. */
   2059 	for (x = 1; x <= 12; x++) {
   2060 		miirom &= ~MIIROM_SROMDI;
   2061 		SROM_EMIT(sc, miirom);
   2062 		SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2063 		if (!TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
   2064 			break;
   2065 		SROM_EMIT(sc, miirom);
   2066 	}
   2067 
   2068 	/* Clear CHIP SELECT. */
   2069 	miirom &= ~MIIROM_SROMCS;
   2070 	SROM_EMIT(sc, miirom);
   2071 
   2072 	/* Deselect the SROM. */
   2073 	SROM_EMIT(sc, 0);
   2074 
   2075 	if (x < 4 || x > 12) {
   2076 		printf("%s: broken MicroWire interface detected; "
   2077 		    "setting SROM size to 1Kb\n", sc->sc_dev.dv_xname);
   2078 		return (6);
   2079 	} else {
   2080 		if (tlp_srom_debug)
   2081 			printf("%s: SROM size is 2^%d*16 bits (%d bytes)\n",
   2082 			    sc->sc_dev.dv_xname, x, (1 << (x + 4)) >> 3);
   2083 		return (x);
   2084 	}
   2085 }
   2086 
   2087 /*
   2088  * tlp_read_srom:
   2089  *
   2090  *	Read the Tulip SROM.
   2091  */
   2092 int
   2093 tlp_read_srom(sc)
   2094 	struct tulip_softc *sc;
   2095 {
   2096 	int size;
   2097 	u_int32_t miirom;
   2098 	u_int16_t datain;
   2099 	int i, x;
   2100 
   2101 	tlp_srom_idle(sc);
   2102 
   2103 	sc->sc_srom_addrbits = tlp_srom_size(sc);
   2104 	if (sc->sc_srom_addrbits == 0)
   2105 		return (0);
   2106 	size = TULIP_ROM_SIZE(sc->sc_srom_addrbits);
   2107 	sc->sc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
   2108 
   2109 	/* Select the SROM. */
   2110 	miirom = MIIROM_SR;
   2111 	SROM_EMIT(sc, miirom);
   2112 
   2113 	miirom |= MIIROM_RD;
   2114 	SROM_EMIT(sc, miirom);
   2115 
   2116 	for (i = 0; i < size; i += 2) {
   2117 		/* Send CHIP SELECT for one clock tick. */
   2118 		miirom |= MIIROM_SROMCS;
   2119 		SROM_EMIT(sc, miirom);
   2120 
   2121 		/* Shift in the READ opcode. */
   2122 		for (x = 3; x > 0; x--) {
   2123 			if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
   2124 				miirom |= MIIROM_SROMDI;
   2125 			else
   2126 				miirom &= ~MIIROM_SROMDI;
   2127 			SROM_EMIT(sc, miirom);
   2128 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2129 			SROM_EMIT(sc, miirom);
   2130 		}
   2131 
   2132 		/* Shift in address. */
   2133 		for (x = sc->sc_srom_addrbits; x > 0; x--) {
   2134 			if (i & (1 << x))
   2135 				miirom |= MIIROM_SROMDI;
   2136 			else
   2137 				miirom &= ~MIIROM_SROMDI;
   2138 			SROM_EMIT(sc, miirom);
   2139 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2140 			SROM_EMIT(sc, miirom);
   2141 		}
   2142 
   2143 		/* Shift out data. */
   2144 		miirom &= ~MIIROM_SROMDI;
   2145 		datain = 0;
   2146 		for (x = 16; x > 0; x--) {
   2147 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2148 			if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
   2149 				datain |= (1 << (x - 1));
   2150 			SROM_EMIT(sc, miirom);
   2151 		}
   2152 		sc->sc_srom[i] = datain & 0xff;
   2153 		sc->sc_srom[i + 1] = datain >> 8;
   2154 
   2155 		/* Clear CHIP SELECT. */
   2156 		miirom &= ~MIIROM_SROMCS;
   2157 		SROM_EMIT(sc, miirom);
   2158 	}
   2159 
   2160 	/* Deselect the SROM. */
   2161 	SROM_EMIT(sc, 0);
   2162 
   2163 	/* ...and idle it. */
   2164 	tlp_srom_idle(sc);
   2165 
   2166 	if (tlp_srom_debug) {
   2167 		printf("SROM CONTENTS:");
   2168 		for (i = 0; i < size; i++) {
   2169 			if ((i % 8) == 0)
   2170 				printf("\n\t");
   2171 			printf("0x%02x ", sc->sc_srom[i]);
   2172 		}
   2173 		printf("\n");
   2174 	}
   2175 
   2176 	return (1);
   2177 }
   2178 
   2179 #undef SROM_EMIT
   2180 
   2181 /*
   2182  * tlp_add_rxbuf:
   2183  *
   2184  *	Add a receive buffer to the indicated descriptor.
   2185  */
   2186 int
   2187 tlp_add_rxbuf(sc, idx)
   2188 	struct tulip_softc *sc;
   2189 	int idx;
   2190 {
   2191 	struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx];
   2192 	struct mbuf *m;
   2193 	int error;
   2194 
   2195 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2196 	if (m == NULL)
   2197 		return (ENOBUFS);
   2198 
   2199 	MCLGET(m, M_DONTWAIT);
   2200 	if ((m->m_flags & M_EXT) == 0) {
   2201 		m_freem(m);
   2202 		return (ENOBUFS);
   2203 	}
   2204 
   2205 	if (rxs->rxs_mbuf != NULL)
   2206 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   2207 
   2208 	rxs->rxs_mbuf = m;
   2209 
   2210 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
   2211 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
   2212 	if (error) {
   2213 		printf("%s: can't load rx DMA map %d, error = %d\n",
   2214 		    sc->sc_dev.dv_xname, idx, error);
   2215 		panic("tlp_add_rxbuf");	/* XXX */
   2216 	}
   2217 
   2218 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   2219 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   2220 
   2221 	TULIP_INIT_RXDESC(sc, idx);
   2222 
   2223 	return (0);
   2224 }
   2225 
   2226 /*
   2227  * tlp_srom_crcok:
   2228  *
   2229  *	Check the CRC of the Tulip SROM.
   2230  */
   2231 int
   2232 tlp_srom_crcok(romdata)
   2233 	const u_int8_t *romdata;
   2234 {
   2235 	u_int32_t crc;
   2236 
   2237 	crc = ether_crc32_le(romdata, TULIP_ROM_CRC32_CHECKSUM);
   2238 	crc = (crc & 0xffff) ^ 0xffff;
   2239 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM))
   2240 		return (1);
   2241 
   2242 	/*
   2243 	 * Try an alternate checksum.
   2244 	 */
   2245 	crc = ether_crc32_le(romdata, TULIP_ROM_CRC32_CHECKSUM1);
   2246 	crc = (crc & 0xffff) ^ 0xffff;
   2247 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM1))
   2248 		return (1);
   2249 
   2250 	return (0);
   2251 }
   2252 
   2253 /*
   2254  * tlp_isv_srom:
   2255  *
   2256  *	Check to see if the SROM is in the new standardized format.
   2257  */
   2258 int
   2259 tlp_isv_srom(romdata)
   2260 	const u_int8_t *romdata;
   2261 {
   2262 	int i;
   2263 	u_int16_t cksum;
   2264 
   2265 	if (tlp_srom_crcok(romdata)) {
   2266 		/*
   2267 		 * SROM CRC checks out; must be in the new format.
   2268 		 */
   2269 		return (1);
   2270 	}
   2271 
   2272 	cksum = TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM);
   2273 	if (cksum == 0xffff || cksum == 0) {
   2274 		/*
   2275 		 * No checksum present.  Check the SROM ID; 18 bytes of 0
   2276 		 * followed by 1 (version) followed by the number of
   2277 		 * adapters which use this SROM (should be non-zero).
   2278 		 */
   2279 		for (i = 0; i < TULIP_ROM_SROM_FORMAT_VERION; i++) {
   2280 			if (romdata[i] != 0)
   2281 				return (0);
   2282 		}
   2283 		if (romdata[TULIP_ROM_SROM_FORMAT_VERION] != 1)
   2284 			return (0);
   2285 		if (romdata[TULIP_ROM_CHIP_COUNT] == 0)
   2286 			return (0);
   2287 		return (1);
   2288 	}
   2289 
   2290 	return (0);
   2291 }
   2292 
   2293 /*
   2294  * tlp_isv_srom_enaddr:
   2295  *
   2296  *	Get the Ethernet address from an ISV SROM.
   2297  */
   2298 int
   2299 tlp_isv_srom_enaddr(sc, enaddr)
   2300 	struct tulip_softc *sc;
   2301 	u_int8_t *enaddr;
   2302 {
   2303 	int i, devcnt;
   2304 
   2305 	if (tlp_isv_srom(sc->sc_srom) == 0)
   2306 		return (0);
   2307 
   2308 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   2309 	for (i = 0; i < devcnt; i++) {
   2310 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   2311 			break;
   2312 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   2313 		    sc->sc_devno)
   2314 			break;
   2315 	}
   2316 
   2317 	if (i == devcnt)
   2318 		return (0);
   2319 
   2320 	memcpy(enaddr, &sc->sc_srom[TULIP_ROM_IEEE_NETWORK_ADDRESS],
   2321 	    ETHER_ADDR_LEN);
   2322 	enaddr[5] += i;
   2323 
   2324 	return (1);
   2325 }
   2326 
   2327 /*
   2328  * tlp_parse_old_srom:
   2329  *
   2330  *	Parse old-format SROMs.
   2331  *
   2332  *	This routine is largely lifted from Matt Thomas's `de' driver.
   2333  */
   2334 int
   2335 tlp_parse_old_srom(sc, enaddr)
   2336 	struct tulip_softc *sc;
   2337 	u_int8_t *enaddr;
   2338 {
   2339 	static const u_int8_t testpat[] =
   2340 	    { 0xff, 0, 0x55, 0xaa, 0xff, 0, 0x55, 0xaa };
   2341 	int i;
   2342 	u_int32_t cksum;
   2343 
   2344 	if (memcmp(&sc->sc_srom[0], &sc->sc_srom[16], 8) != 0) {
   2345 		/*
   2346 		 * Some vendors (e.g. ZNYX) don't use the standard
   2347 		 * DEC Address ROM format, but rather just have an
   2348 		 * Ethernet address in the first 6 bytes, maybe a
   2349 		 * 2 byte checksum, and then all 0xff's.
   2350 		 *
   2351 		 * On the other hand, Cobalt Networks interfaces
   2352 		 * simply have the address in the first six bytes
   2353 		 * with the rest zeroed out.
   2354 		 */
   2355 		for (i = 8; i < 32; i++) {
   2356 			if (sc->sc_srom[i] != 0xff &&
   2357 			    sc->sc_srom[i] != 0)
   2358 				return (0);
   2359 		}
   2360 
   2361 		/*
   2362 		 * Sanity check the Ethernet address:
   2363 		 *
   2364 		 *	- Make sure it's not multicast or locally
   2365 		 *	  assigned
   2366 		 *	- Make sure it has a non-0 OUI
   2367 		 */
   2368 		if (sc->sc_srom[0] & 3)
   2369 			return (0);
   2370 		if (sc->sc_srom[0] == 0 && sc->sc_srom[1] == 0 &&
   2371 		    sc->sc_srom[2] == 0)
   2372 			return (0);
   2373 
   2374 		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   2375 		return (1);
   2376 	}
   2377 
   2378 	/*
   2379 	 * Standard DEC Address ROM test.
   2380 	 */
   2381 
   2382 	if (memcmp(&sc->sc_srom[24], testpat, 8) != 0)
   2383 		return (0);
   2384 
   2385 	for (i = 0; i < 8; i++) {
   2386 		if (sc->sc_srom[i] != sc->sc_srom[15 - i])
   2387 			return (0);
   2388 	}
   2389 
   2390 	memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   2391 
   2392 	cksum = *(u_int16_t *) &enaddr[0];
   2393 
   2394 	cksum <<= 1;
   2395 	if (cksum > 0xffff)
   2396 		cksum -= 0xffff;
   2397 
   2398 	cksum += *(u_int16_t *) &enaddr[2];
   2399 	if (cksum > 0xffff)
   2400 		cksum -= 0xffff;
   2401 
   2402 	cksum <<= 1;
   2403 	if (cksum > 0xffff)
   2404 		cksum -= 0xffff;
   2405 
   2406 	cksum += *(u_int16_t *) &enaddr[4];
   2407 	if (cksum >= 0xffff)
   2408 		cksum -= 0xffff;
   2409 
   2410 	if (cksum != *(u_int16_t *) &sc->sc_srom[6])
   2411 		return (0);
   2412 
   2413 	return (1);
   2414 }
   2415 
   2416 /*
   2417  * tlp_filter_setup:
   2418  *
   2419  *	Set the Tulip's receive filter.
   2420  */
   2421 void
   2422 tlp_filter_setup(sc)
   2423 	struct tulip_softc *sc;
   2424 {
   2425 	struct ethercom *ec = &sc->sc_ethercom;
   2426 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2427 	struct ether_multi *enm;
   2428 	struct ether_multistep step;
   2429 	__volatile u_int32_t *sp;
   2430 	struct tulip_txsoft *txs;
   2431 	u_int8_t enaddr[ETHER_ADDR_LEN];
   2432 	u_int32_t hash, hashsize;
   2433 	int cnt;
   2434 
   2435 	DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n",
   2436 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2437 
   2438 	memcpy(enaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   2439 
   2440 	/*
   2441 	 * If there are transmissions pending, wait until they have
   2442 	 * completed.
   2443 	 */
   2444 	if (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL ||
   2445 	    (sc->sc_flags & TULIPF_DOING_SETUP) != 0) {
   2446 		sc->sc_flags |= TULIPF_WANT_SETUP;
   2447 		DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n",
   2448 		    sc->sc_dev.dv_xname));
   2449 		return;
   2450 	}
   2451 	sc->sc_flags &= ~TULIPF_WANT_SETUP;
   2452 
   2453 	switch (sc->sc_chip) {
   2454 	case TULIP_CHIP_82C115:
   2455 		hashsize = TULIP_PNICII_HASHSIZE;
   2456 		break;
   2457 
   2458 	default:
   2459 		hashsize = TULIP_MCHASHSIZE;
   2460 	}
   2461 
   2462 	/*
   2463 	 * If we're running, idle the transmit and receive engines.  If
   2464 	 * we're NOT running, we're being called from tlp_init(), and our
   2465 	 * writing OPMODE will start the transmit and receive processes
   2466 	 * in motion.
   2467 	 */
   2468 	if (ifp->if_flags & IFF_RUNNING) {
   2469 		/*
   2470 		 * Actually, some chips seem to need a really hard
   2471 		 * kick in the head for this to work.  The genuine
   2472 		 * DEC chips can just be idled, but some of the
   2473 		 * clones seem to REALLY want a reset here.  Doing
   2474 		 * the reset will end up here again, but with
   2475 		 * IFF_RUNNING cleared.
   2476 		 */
   2477 		switch (sc->sc_chip) {
   2478 		case TULIP_CHIP_82C168:
   2479 		case TULIP_CHIP_82C169:
   2480 			tlp_init(sc);
   2481 			return;
   2482 
   2483 		default:
   2484 			tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2485 		}
   2486 	}
   2487 
   2488 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
   2489 
   2490 	if (ifp->if_flags & IFF_PROMISC) {
   2491 		sc->sc_opmode |= OPMODE_PR;
   2492 		goto allmulti;
   2493 	}
   2494 
   2495 	/*
   2496 	 * Try Perfect filtering first.
   2497 	 */
   2498 
   2499 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   2500 	sp = TULIP_CDSP(sc);
   2501 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2502 	cnt = 0;
   2503 	ETHER_FIRST_MULTI(step, ec, enm);
   2504 	while (enm != NULL) {
   2505 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2506 			/*
   2507 			 * We must listen to a range of multicast addresses.
   2508 			 * For now, just accept all multicasts, rather than
   2509 			 * trying to set only those filter bits needed to match
   2510 			 * the range.  (At this time, the only use of address
   2511 			 * ranges is for IP multicast routing, for which the
   2512 			 * range is big enough to require all bits set.)
   2513 			 */
   2514 			goto allmulti;
   2515 		}
   2516 		if (cnt == (TULIP_MAXADDRS - 2)) {
   2517 			/*
   2518 			 * We already have our multicast limit (still need
   2519 			 * our station address and broadcast).  Go to
   2520 			 * Hash-Perfect mode.
   2521 			 */
   2522 			goto hashperfect;
   2523 		}
   2524 		cnt++;
   2525 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 0);
   2526 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 1);
   2527 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 2);
   2528 		ETHER_NEXT_MULTI(step, enm);
   2529 	}
   2530 
   2531 	if (ifp->if_flags & IFF_BROADCAST) {
   2532 		/* ...and the broadcast address. */
   2533 		cnt++;
   2534 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2535 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2536 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2537 	}
   2538 
   2539 	/* Pad the rest with our station address. */
   2540 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   2541 		*sp++ = TULIP_SP_FIELD(enaddr, 0);
   2542 		*sp++ = TULIP_SP_FIELD(enaddr, 1);
   2543 		*sp++ = TULIP_SP_FIELD(enaddr, 2);
   2544 	}
   2545 	ifp->if_flags &= ~IFF_ALLMULTI;
   2546 	goto setit;
   2547 
   2548  hashperfect:
   2549 	/*
   2550 	 * Try Hash-Perfect mode.
   2551 	 */
   2552 
   2553 	/*
   2554 	 * Some 21140 chips have broken Hash-Perfect modes.  On these
   2555 	 * chips, we simply use Hash-Only mode, and put our station
   2556 	 * address into the filter.
   2557 	 */
   2558 	if (sc->sc_chip == TULIP_CHIP_21140)
   2559 		sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY;
   2560 	else
   2561 		sc->sc_filtmode = TDCTL_Tx_FT_HASH;
   2562 	sp = TULIP_CDSP(sc);
   2563 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2564 	ETHER_FIRST_MULTI(step, ec, enm);
   2565 	while (enm != NULL) {
   2566 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2567 			/*
   2568 			 * We must listen to a range of multicast addresses.
   2569 			 * For now, just accept all multicasts, rather than
   2570 			 * trying to set only those filter bits needed to match
   2571 			 * the range.  (At this time, the only use of address
   2572 			 * ranges is for IP multicast routing, for which the
   2573 			 * range is big enough to require all bits set.)
   2574 			 */
   2575 			goto allmulti;
   2576 		}
   2577 		hash = tlp_mchash(enm->enm_addrlo, hashsize);
   2578 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2579 		ETHER_NEXT_MULTI(step, enm);
   2580 	}
   2581 
   2582 	if (ifp->if_flags & IFF_BROADCAST) {
   2583 		/* ...and the broadcast address. */
   2584 		hash = tlp_mchash(etherbroadcastaddr, hashsize);
   2585 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2586 	}
   2587 
   2588 	if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
   2589 		/* ...and our station address. */
   2590 		hash = tlp_mchash(enaddr, hashsize);
   2591 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2592 	} else {
   2593 		/*
   2594 		 * Hash-Perfect mode; put our station address after
   2595 		 * the hash table.
   2596 		 */
   2597 		sp[39] = TULIP_SP_FIELD(enaddr, 0);
   2598 		sp[40] = TULIP_SP_FIELD(enaddr, 1);
   2599 		sp[41] = TULIP_SP_FIELD(enaddr, 2);
   2600 	}
   2601 	ifp->if_flags &= ~IFF_ALLMULTI;
   2602 	goto setit;
   2603 
   2604  allmulti:
   2605 	/*
   2606 	 * Use Perfect filter mode.  First address is the broadcast address,
   2607 	 * and pad the rest with our station address.  We'll set Pass-all-
   2608 	 * multicast in OPMODE below.
   2609 	 */
   2610 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   2611 	sp = TULIP_CDSP(sc);
   2612 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2613 	cnt = 0;
   2614 	if (ifp->if_flags & IFF_BROADCAST) {
   2615 		cnt++;
   2616 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2617 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2618 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2619 	}
   2620 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   2621 		*sp++ = TULIP_SP_FIELD(enaddr, 0);
   2622 		*sp++ = TULIP_SP_FIELD(enaddr, 1);
   2623 		*sp++ = TULIP_SP_FIELD(enaddr, 2);
   2624 	}
   2625 	ifp->if_flags |= IFF_ALLMULTI;
   2626 
   2627  setit:
   2628 	if (ifp->if_flags & IFF_ALLMULTI)
   2629 		sc->sc_opmode |= OPMODE_PM;
   2630 
   2631 	/* Sync the setup packet buffer. */
   2632 	TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE);
   2633 
   2634 	/*
   2635 	 * Fill in the setup packet descriptor.
   2636 	 */
   2637 	txs = SIMPLEQ_FIRST(&sc->sc_txfreeq);
   2638 
   2639 	txs->txs_firstdesc = sc->sc_txnext;
   2640 	txs->txs_lastdesc = sc->sc_txnext;
   2641 	txs->txs_ndescs = 1;
   2642 	txs->txs_mbuf = NULL;
   2643 
   2644 	sc->sc_txdescs[sc->sc_txnext].td_bufaddr1 =
   2645 	    htole32(TULIP_CDSPADDR(sc));
   2646 	sc->sc_txdescs[sc->sc_txnext].td_ctl =
   2647 	    htole32((TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) |
   2648 	    sc->sc_filtmode | TDCTL_Tx_SET | TDCTL_Tx_FS |
   2649 	    TDCTL_Tx_LS | TDCTL_Tx_IC | sc->sc_tdctl_ch |
   2650 	    (sc->sc_txnext == (TULIP_NTXDESC - 1) ? sc->sc_tdctl_er : 0));
   2651 	sc->sc_txdescs[sc->sc_txnext].td_status = htole32(TDSTAT_OWN);
   2652 	TULIP_CDTXSYNC(sc, sc->sc_txnext, txs->txs_ndescs,
   2653 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   2654 
   2655 	/* Advance the tx pointer. */
   2656 	sc->sc_txfree -= 1;
   2657 	sc->sc_txnext = TULIP_NEXTTX(sc->sc_txnext);
   2658 
   2659 	SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
   2660 	SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
   2661 
   2662 	/*
   2663 	 * Set the OPMODE register.  This will also resume the
   2664 	 * transmit transmit process we idled above.
   2665 	 */
   2666 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2667 
   2668 	sc->sc_flags |= TULIPF_DOING_SETUP;
   2669 
   2670 	/*
   2671 	 * Kick the transmitter; this will cause the Tulip to
   2672 	 * read the setup descriptor.
   2673 	 */
   2674 	/* XXX USE AUTOPOLLING? */
   2675 	TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
   2676 
   2677 	/* Set up a watchdog timer in case the chip flakes out. */
   2678 	ifp->if_timer = 5;
   2679 
   2680 	DPRINTF(sc, ("%s: tlp_filter_setup: returning\n", sc->sc_dev.dv_xname));
   2681 }
   2682 
   2683 /*
   2684  * tlp_winb_filter_setup:
   2685  *
   2686  *	Set the Winbond 89C840F's receive filter.
   2687  */
   2688 void
   2689 tlp_winb_filter_setup(sc)
   2690 	struct tulip_softc *sc;
   2691 {
   2692 	struct ethercom *ec = &sc->sc_ethercom;
   2693 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2694 	struct ether_multi *enm;
   2695 	struct ether_multistep step;
   2696 	u_int32_t hash, mchash[2];
   2697 
   2698 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n",
   2699 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2700 
   2701 	sc->sc_opmode &= ~(OPMODE_WINB_APP|OPMODE_WINB_AMP|OPMODE_WINB_ABP);
   2702 
   2703 	if (ifp->if_flags & IFF_MULTICAST)
   2704 		sc->sc_opmode |= OPMODE_WINB_AMP;
   2705 
   2706 	if (ifp->if_flags & IFF_BROADCAST)
   2707 		sc->sc_opmode |= OPMODE_WINB_ABP;
   2708 
   2709 	if (ifp->if_flags & IFF_PROMISC) {
   2710 		sc->sc_opmode |= OPMODE_WINB_APP;
   2711 		goto allmulti;
   2712 	}
   2713 
   2714 	mchash[0] = mchash[1] = 0;
   2715 
   2716 	ETHER_FIRST_MULTI(step, ec, enm);
   2717 	while (enm != NULL) {
   2718 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2719 			/*
   2720 			 * We must listen to a range of multicast addresses.
   2721 			 * For now, just accept all multicasts, rather than
   2722 			 * trying to set only those filter bits needed to match
   2723 			 * the range.  (At this time, the only use of address
   2724 			 * ranges is for IP multicast routing, for which the
   2725 			 * range is big enough to require all bits set.)
   2726 			 */
   2727 			goto allmulti;
   2728 		}
   2729 
   2730 		/*
   2731 		 * According to the FreeBSD `wb' driver, yes, you
   2732 		 * really do invert the hash.
   2733 		 */
   2734 		hash =
   2735 		    (~(ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26))
   2736 		    & 0x3f;
   2737 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
   2738 		ETHER_NEXT_MULTI(step, enm);
   2739 	}
   2740 	ifp->if_flags &= ~IFF_ALLMULTI;
   2741 	goto setit;
   2742 
   2743  allmulti:
   2744 	ifp->if_flags |= IFF_ALLMULTI;
   2745 	mchash[0] = mchash[1] = 0xffffffff;
   2746 
   2747  setit:
   2748 	TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]);
   2749 	TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]);
   2750 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2751 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n",
   2752 	    sc->sc_dev.dv_xname));
   2753 }
   2754 
   2755 /*
   2756  * tlp_al981_filter_setup:
   2757  *
   2758  *	Set the ADMtek AL981's receive filter.
   2759  */
   2760 void
   2761 tlp_al981_filter_setup(sc)
   2762 	struct tulip_softc *sc;
   2763 {
   2764 	struct ethercom *ec = &sc->sc_ethercom;
   2765 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2766 	struct ether_multi *enm;
   2767 	struct ether_multistep step;
   2768 	u_int32_t hash, mchash[2];
   2769 
   2770 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: sc_flags 0x%08x\n",
   2771 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2772 
   2773 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2774 
   2775 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
   2776 
   2777 	if (ifp->if_flags & IFF_PROMISC) {
   2778 		sc->sc_opmode |= OPMODE_PR;
   2779 		goto allmulti;
   2780 	}
   2781 
   2782 	mchash[0] = mchash[1] = 0;
   2783 
   2784 	ETHER_FIRST_MULTI(step, ec, enm);
   2785 	while (enm != NULL) {
   2786 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2787 			/*
   2788 			 * We must listen to a range of multicast addresses.
   2789 			 * For now, just accept all multicasts, rather than
   2790 			 * trying to set only those filter bits needed to match
   2791 			 * the range.  (At this time, the only use of address
   2792 			 * ranges is for IP multicast routing, for which the
   2793 			 * range is big enough to require all bits set.)
   2794 			 */
   2795 			goto allmulti;
   2796 		}
   2797 
   2798 		hash = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
   2799 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
   2800 		ETHER_NEXT_MULTI(step, enm);
   2801 	}
   2802 	ifp->if_flags &= ~IFF_ALLMULTI;
   2803 	goto setit;
   2804 
   2805  allmulti:
   2806 	ifp->if_flags |= IFF_ALLMULTI;
   2807 	mchash[0] = mchash[1] = 0xffffffff;
   2808 
   2809  setit:
   2810 	TULIP_WRITE(sc, CSR_ADM_MAR0, mchash[0]);
   2811 	TULIP_WRITE(sc, CSR_ADM_MAR1, mchash[1]);
   2812 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2813 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: returning\n",
   2814 	    sc->sc_dev.dv_xname));
   2815 }
   2816 
   2817 /*
   2818  * tlp_idle:
   2819  *
   2820  *	Cause the transmit and/or receive processes to go idle.
   2821  */
   2822 void
   2823 tlp_idle(sc, bits)
   2824 	struct tulip_softc *sc;
   2825 	u_int32_t bits;
   2826 {
   2827 	static const char *tx_state_names[] = {
   2828 		"STOPPED",
   2829 		"RUNNING - FETCH",
   2830 		"RUNNING - WAIT",
   2831 		"RUNNING - READING",
   2832 		"-- RESERVED --",
   2833 		"RUNNING - SETUP",
   2834 		"SUSPENDED",
   2835 		"RUNNING - CLOSE",
   2836 	};
   2837 	static const char *rx_state_names[] = {
   2838 		"STOPPED",
   2839 		"RUNNING - FETCH",
   2840 		"RUNNING - CHECK",
   2841 		"RUNNING - WAIT",
   2842 		"SUSPENDED",
   2843 		"RUNNING - CLOSE",
   2844 		"RUNNING - FLUSH",
   2845 		"RUNNING - QUEUE",
   2846 	};
   2847 	u_int32_t csr, ackmask = 0;
   2848 	int i;
   2849 
   2850 	if (bits & OPMODE_ST)
   2851 		ackmask |= STATUS_TPS;
   2852 
   2853 	if (bits & OPMODE_SR)
   2854 		ackmask |= STATUS_RPS;
   2855 
   2856 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits);
   2857 
   2858 	for (i = 0; i < 1000; i++) {
   2859 		if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
   2860 			break;
   2861 		delay(10);
   2862 	}
   2863 
   2864 	csr = TULIP_READ(sc, CSR_STATUS);
   2865 	if ((csr & ackmask) != ackmask) {
   2866 		if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
   2867 		    (csr & STATUS_TS) != STATUS_TS_STOPPED)
   2868 			printf("%s: transmit process failed to idle: "
   2869 			    "state %s\n", sc->sc_dev.dv_xname,
   2870 			    tx_state_names[(csr & STATUS_TS) >> 20]);
   2871 		if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
   2872 		    (csr & STATUS_RS) != STATUS_RS_STOPPED)
   2873 			printf("%s: receive process failed to idle: "
   2874 			    "state %s\n", sc->sc_dev.dv_xname,
   2875 			    rx_state_names[(csr & STATUS_RS) >> 17]);
   2876 	}
   2877 	TULIP_WRITE(sc, CSR_STATUS, ackmask);
   2878 }
   2879 
   2880 /*****************************************************************************
   2881  * Generic media support functions.
   2882  *****************************************************************************/
   2883 
   2884 /*
   2885  * tlp_mediastatus:	[ifmedia interface function]
   2886  *
   2887  *	Query the current media.
   2888  */
   2889 void
   2890 tlp_mediastatus(ifp, ifmr)
   2891 	struct ifnet *ifp;
   2892 	struct ifmediareq *ifmr;
   2893 {
   2894 	struct tulip_softc *sc = ifp->if_softc;
   2895 
   2896 	if (TULIP_IS_ENABLED(sc) == 0) {
   2897 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
   2898 		ifmr->ifm_status = 0;
   2899 		return;
   2900 	}
   2901 
   2902 	(*sc->sc_mediasw->tmsw_get)(sc, ifmr);
   2903 }
   2904 
   2905 /*
   2906  * tlp_mediachange:	[ifmedia interface function]
   2907  *
   2908  *	Update the current media.
   2909  */
   2910 int
   2911 tlp_mediachange(ifp)
   2912 	struct ifnet *ifp;
   2913 {
   2914 	struct tulip_softc *sc = ifp->if_softc;
   2915 
   2916 	return ((*sc->sc_mediasw->tmsw_set)(sc));
   2917 }
   2918 
   2919 /*****************************************************************************
   2920  * Support functions for MII-attached media.
   2921  *****************************************************************************/
   2922 
   2923 /*
   2924  * tlp_mii_tick:
   2925  *
   2926  *	One second timer, used to tick the MII.
   2927  */
   2928 void
   2929 tlp_mii_tick(arg)
   2930 	void *arg;
   2931 {
   2932 	struct tulip_softc *sc = arg;
   2933 	int s;
   2934 
   2935 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   2936 		return;
   2937 
   2938 	s = splnet();
   2939 	mii_tick(&sc->sc_mii);
   2940 	splx(s);
   2941 
   2942 	callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc);
   2943 }
   2944 
   2945 /*
   2946  * tlp_mii_statchg:	[mii interface function]
   2947  *
   2948  *	Callback from PHY when media changes.
   2949  */
   2950 void
   2951 tlp_mii_statchg(self)
   2952 	struct device *self;
   2953 {
   2954 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2955 
   2956 	/* Idle the transmit and receive processes. */
   2957 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2958 
   2959 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_HBD);
   2960 
   2961 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
   2962 		sc->sc_opmode |= OPMODE_TTM;
   2963 	else
   2964 		sc->sc_opmode |= OPMODE_HBD;
   2965 
   2966 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   2967 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
   2968 
   2969 	/*
   2970 	 * Write new OPMODE bits.  This also restarts the transmit
   2971 	 * and receive processes.
   2972 	 */
   2973 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2974 }
   2975 
   2976 /*
   2977  * tlp_winb_mii_statchg: [mii interface function]
   2978  *
   2979  *	Callback from PHY when media changes.  This version is
   2980  *	for the Winbond 89C840F, which has different OPMODE bits.
   2981  */
   2982 void
   2983 tlp_winb_mii_statchg(self)
   2984 	struct device *self;
   2985 {
   2986 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2987 
   2988 	/* Idle the transmit and receive processes. */
   2989 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2990 
   2991 	sc->sc_opmode &= ~(OPMODE_WINB_FES|OPMODE_FD);
   2992 
   2993 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX)
   2994 		sc->sc_opmode |= OPMODE_WINB_FES;
   2995 
   2996 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   2997 		sc->sc_opmode |= OPMODE_FD;
   2998 
   2999 	/*
   3000 	 * Write new OPMODE bits.  This also restarts the transmit
   3001 	 * and receive processes.
   3002 	 */
   3003 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3004 }
   3005 
   3006 /*
   3007  * tlp_mii_getmedia:
   3008  *
   3009  *	Callback from ifmedia to request current media status.
   3010  */
   3011 void
   3012 tlp_mii_getmedia(sc, ifmr)
   3013 	struct tulip_softc *sc;
   3014 	struct ifmediareq *ifmr;
   3015 {
   3016 
   3017 	mii_pollstat(&sc->sc_mii);
   3018 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   3019 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   3020 }
   3021 
   3022 /*
   3023  * tlp_mii_setmedia:
   3024  *
   3025  *	Callback from ifmedia to request new media setting.
   3026  */
   3027 int
   3028 tlp_mii_setmedia(sc)
   3029 	struct tulip_softc *sc;
   3030 {
   3031 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   3032 
   3033 	if (ifp->if_flags & IFF_UP) {
   3034 		switch (sc->sc_chip) {
   3035 		case TULIP_CHIP_21142:
   3036 		case TULIP_CHIP_21143:
   3037 			/* Disable the internal Nway engine. */
   3038 			TULIP_WRITE(sc, CSR_SIATXRX, 0);
   3039 			break;
   3040 
   3041 		default:
   3042 			/* Nothing. */
   3043 		}
   3044 		mii_mediachg(&sc->sc_mii);
   3045 	}
   3046 	return (0);
   3047 }
   3048 
   3049 /*
   3050  * tlp_bitbang_mii_readreg:
   3051  *
   3052  *	Read a PHY register via bit-bang'ing the MII.
   3053  */
   3054 int
   3055 tlp_bitbang_mii_readreg(self, phy, reg)
   3056 	struct device *self;
   3057 	int phy, reg;
   3058 {
   3059 	struct tulip_softc *sc = (void *) self;
   3060 
   3061 	return (mii_bitbang_readreg(self, sc->sc_bitbang_ops, phy, reg));
   3062 }
   3063 
   3064 /*
   3065  * tlp_bitbang_mii_writereg:
   3066  *
   3067  *	Write a PHY register via bit-bang'ing the MII.
   3068  */
   3069 void
   3070 tlp_bitbang_mii_writereg(self, phy, reg, val)
   3071 	struct device *self;
   3072 	int phy, reg, val;
   3073 {
   3074 	struct tulip_softc *sc = (void *) self;
   3075 
   3076 	mii_bitbang_writereg(self, sc->sc_bitbang_ops, phy, reg, val);
   3077 }
   3078 
   3079 /*
   3080  * tlp_sio_mii_bitbang_read:
   3081  *
   3082  *	Read the MII serial port for the MII bit-bang module.
   3083  */
   3084 u_int32_t
   3085 tlp_sio_mii_bitbang_read(self)
   3086 	struct device *self;
   3087 {
   3088 	struct tulip_softc *sc = (void *) self;
   3089 
   3090 	return (TULIP_READ(sc, CSR_MIIROM));
   3091 }
   3092 
   3093 /*
   3094  * tlp_sio_mii_bitbang_write:
   3095  *
   3096  *	Write the MII serial port for the MII bit-bang module.
   3097  */
   3098 void
   3099 tlp_sio_mii_bitbang_write(self, val)
   3100 	struct device *self;
   3101 	u_int32_t val;
   3102 {
   3103 	struct tulip_softc *sc = (void *) self;
   3104 
   3105 	TULIP_WRITE(sc, CSR_MIIROM, val);
   3106 }
   3107 
   3108 /*
   3109  * tlp_pnic_mii_readreg:
   3110  *
   3111  *	Read a PHY register on the Lite-On PNIC.
   3112  */
   3113 int
   3114 tlp_pnic_mii_readreg(self, phy, reg)
   3115 	struct device *self;
   3116 	int phy, reg;
   3117 {
   3118 	struct tulip_softc *sc = (void *) self;
   3119 	u_int32_t val;
   3120 	int i;
   3121 
   3122 	TULIP_WRITE(sc, CSR_PNIC_MII,
   3123 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   3124 	    PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
   3125 	    (reg << PNIC_MII_REGSHIFT));
   3126 
   3127 	for (i = 0; i < 1000; i++) {
   3128 		delay(10);
   3129 		val = TULIP_READ(sc, CSR_PNIC_MII);
   3130 		if ((val & PNIC_MII_BUSY) == 0) {
   3131 			if ((val & PNIC_MII_DATA) == PNIC_MII_DATA)
   3132 				return (0);
   3133 			else
   3134 				return (val & PNIC_MII_DATA);
   3135 		}
   3136 	}
   3137 	printf("%s: MII read timed out\n", sc->sc_dev.dv_xname);
   3138 	return (0);
   3139 }
   3140 
   3141 /*
   3142  * tlp_pnic_mii_writereg:
   3143  *
   3144  *	Write a PHY register on the Lite-On PNIC.
   3145  */
   3146 void
   3147 tlp_pnic_mii_writereg(self, phy, reg, val)
   3148 	struct device *self;
   3149 	int phy, reg, val;
   3150 {
   3151 	struct tulip_softc *sc = (void *) self;
   3152 	int i;
   3153 
   3154 	TULIP_WRITE(sc, CSR_PNIC_MII,
   3155 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   3156 	    PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
   3157 	    (reg << PNIC_MII_REGSHIFT) | val);
   3158 
   3159 	for (i = 0; i < 1000; i++) {
   3160 		delay(10);
   3161 		if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
   3162 			return;
   3163 	}
   3164 	printf("%s: MII write timed out\n", sc->sc_dev.dv_xname);
   3165 }
   3166 
   3167 const bus_addr_t tlp_al981_phy_regmap[] = {
   3168 	CSR_ADM_BMCR,
   3169 	CSR_ADM_BMSR,
   3170 	CSR_ADM_PHYIDR1,
   3171 	CSR_ADM_PHYIDR2,
   3172 	CSR_ADM_ANAR,
   3173 	CSR_ADM_ANLPAR,
   3174 	CSR_ADM_ANER,
   3175 
   3176 	CSR_ADM_XMC,
   3177 	CSR_ADM_XCIIS,
   3178 	CSR_ADM_XIE,
   3179 	CSR_ADM_100CTR,
   3180 };
   3181 const int tlp_al981_phy_regmap_size = sizeof(tlp_al981_phy_regmap) /
   3182     sizeof(tlp_al981_phy_regmap[0]);
   3183 
   3184 /*
   3185  * tlp_al981_mii_readreg:
   3186  *
   3187  *	Read a PHY register on the ADMtek AL981.
   3188  */
   3189 int
   3190 tlp_al981_mii_readreg(self, phy, reg)
   3191 	struct device *self;
   3192 	int phy, reg;
   3193 {
   3194 	struct tulip_softc *sc = (struct tulip_softc *)self;
   3195 
   3196 	/* AL981 only has an internal PHY. */
   3197 	if (phy != 0)
   3198 		return (0);
   3199 
   3200 	if (reg >= tlp_al981_phy_regmap_size)
   3201 		return (0);
   3202 
   3203 	return (bus_space_read_4(sc->sc_st, sc->sc_sh,
   3204 	    tlp_al981_phy_regmap[reg]) & 0xffff);
   3205 }
   3206 
   3207 /*
   3208  * tlp_al981_mii_writereg:
   3209  *
   3210  *	Write a PHY register on the ADMtek AL981.
   3211  */
   3212 void
   3213 tlp_al981_mii_writereg(self, phy, reg, val)
   3214 	struct device *self;
   3215 	int phy, reg, val;
   3216 {
   3217 	struct tulip_softc *sc = (struct tulip_softc *)self;
   3218 
   3219 	/* AL981 only has an internal PHY. */
   3220 	if (phy != 0)
   3221 		return;
   3222 
   3223 	if (reg >= tlp_al981_phy_regmap_size)
   3224 		return;
   3225 
   3226 	bus_space_write_4(sc->sc_st, sc->sc_sh,
   3227 	    tlp_al981_phy_regmap[reg], val);
   3228 }
   3229 
   3230 /*****************************************************************************
   3231  * Chip-specific pre-init and reset functions.
   3232  *****************************************************************************/
   3233 
   3234 /*
   3235  * tlp_2114x_preinit:
   3236  *
   3237  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
   3238  */
   3239 void
   3240 tlp_2114x_preinit(sc)
   3241 	struct tulip_softc *sc;
   3242 {
   3243 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3244 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3245 
   3246 	/*
   3247 	 * Whether or not we're in MII or SIA/SYM mode, the media info
   3248 	 * contains the appropriate OPMODE bits.
   3249 	 *
   3250 	 * Note that if we have no media info, we are are doing
   3251 	 * non-MII `auto'.
   3252 	 *
   3253 	 * Also, we always set the Must-Be-One bit.
   3254 	 */
   3255 	if (tm == NULL) {
   3256 #ifdef DIAGNOSTIC
   3257 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   3258 			panic("tlp_2114x_preinit: not IFM_AUTO");
   3259 		if (sc->sc_nway_active == NULL)
   3260 			panic("tlp_2114x_preinit: nway_active NULL");
   3261 #endif
   3262 		tm = sc->sc_nway_active->ifm_aux;
   3263 	}
   3264 	sc->sc_opmode |= OPMODE_MBO | tm->tm_opmode;
   3265 
   3266 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3267 }
   3268 
   3269 /*
   3270  * tlp_2114x_mii_preinit:
   3271  *
   3272  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
   3273  *	This version is used by boards which only have MII and don't have
   3274  *	an ISV SROM.
   3275  */
   3276 void
   3277 tlp_2114x_mii_preinit(sc)
   3278 	struct tulip_softc *sc;
   3279 {
   3280 
   3281 	/*
   3282 	 * Always set the Must-Be-One bit, and Port Select (to select MII).
   3283 	 * We'll never be called during a media change.
   3284 	 */
   3285 	sc->sc_opmode |= OPMODE_MBO|OPMODE_PS;
   3286 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3287 }
   3288 
   3289 /*
   3290  * tlp_pnic_preinit:
   3291  *
   3292  *	Pre-init function for the Lite-On 82c168 and 82c169.
   3293  */
   3294 void
   3295 tlp_pnic_preinit(sc)
   3296 	struct tulip_softc *sc;
   3297 {
   3298 
   3299 	if (sc->sc_flags & TULIPF_HAS_MII) {
   3300 		/*
   3301 		 * MII case: just set the port-select bit; we will never
   3302 		 * be called during a media change.
   3303 		 */
   3304 		sc->sc_opmode |= OPMODE_PS;
   3305 	} else {
   3306 		/*
   3307 		 * ENDEC/PCS/Nway mode; enable the Tx backoff counter.
   3308 		 */
   3309 		sc->sc_opmode |= OPMODE_PNIC_TBEN;
   3310 	}
   3311 }
   3312 
   3313 /*
   3314  * tlp_21140_reset:
   3315  *
   3316  *	Issue a reset sequence on the 21140 via the GPIO facility.
   3317  */
   3318 void
   3319 tlp_21140_reset(sc)
   3320 	struct tulip_softc *sc;
   3321 {
   3322 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3323 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3324 	int i;
   3325 
   3326 	/* First, set the direction on the GPIO pins. */
   3327 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   3328 
   3329 	/* Now, issue the reset sequence. */
   3330 	for (i = 0; i < tm->tm_reset_length; i++) {
   3331 		delay(10);
   3332 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_reset_offset + i]);
   3333 	}
   3334 
   3335 	/* Now, issue the selection sequence. */
   3336 	for (i = 0; i < tm->tm_gp_length; i++) {
   3337 		delay(10);
   3338 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_gp_offset + i]);
   3339 	}
   3340 
   3341 	/* If there were no sequences, just lower the pins. */
   3342 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0)
   3343 		TULIP_WRITE(sc, CSR_GPP, 0);
   3344 }
   3345 
   3346 /*
   3347  * tlp_21142_reset:
   3348  *
   3349  *	Issue a reset sequence on the 21142 via the GPIO facility.
   3350  */
   3351 void
   3352 tlp_21142_reset(sc)
   3353 	struct tulip_softc *sc;
   3354 {
   3355 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3356 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3357 	const u_int8_t *ncp;
   3358 	int i;
   3359 
   3360 	ncp = &sc->sc_srom[tm->tm_reset_offset];
   3361 	for (i = 0; i < tm->tm_reset_length; i++, ncp += 2) {
   3362 		delay(10);
   3363 		TULIP_WRITE(sc, CSR_SIAGEN,
   3364 		    TULIP_ROM_GETW(ncp, 0) << 16);
   3365 	}
   3366 
   3367 	ncp = &sc->sc_srom[tm->tm_gp_offset];
   3368 	for (i = 0; i < tm->tm_gp_length; i++, ncp += 2) {
   3369 		delay(10);
   3370 		TULIP_WRITE(sc, CSR_SIAGEN,
   3371 		    TULIP_ROM_GETW(ncp, 0) << 16);
   3372 	}
   3373 
   3374 	/* If there were no sequences, just lower the pins. */
   3375 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   3376 		delay(10);
   3377 		TULIP_WRITE(sc, CSR_SIAGEN, 0);
   3378 	}
   3379 }
   3380 
   3381 /*
   3382  * tlp_pmac_reset:
   3383  *
   3384  *	Reset routine for Macronix chips.
   3385  */
   3386 void
   3387 tlp_pmac_reset(sc)
   3388 	struct tulip_softc *sc;
   3389 {
   3390 
   3391 	switch (sc->sc_chip) {
   3392 	case TULIP_CHIP_82C115:
   3393 	case TULIP_CHIP_MX98715:
   3394 	case TULIP_CHIP_MX98715A:
   3395 	case TULIP_CHIP_MX98725:
   3396 		/*
   3397 		 * Set the LED operating mode.  This information is located
   3398 		 * in the EEPROM at byte offset 0x77, per the MX98715A and
   3399 		 * MX98725 application notes.
   3400 		 */
   3401 		TULIP_WRITE(sc, CSR_MIIROM, sc->sc_srom[0x77] << 24);
   3402 		break;
   3403 
   3404 	default:
   3405 		/* Nothing. */
   3406 	}
   3407 }
   3408 
   3409 /*****************************************************************************
   3410  * Chip/board-specific media switches.  The ones here are ones that
   3411  * are potentially common to multiple front-ends.
   3412  *****************************************************************************/
   3413 
   3414 /*
   3415  * This table is a common place for all sorts of media information,
   3416  * keyed off of the SROM media code for that media.
   3417  *
   3418  * Note that we explicitly configure the 21142/21143 to always advertise
   3419  * NWay capabilities when using the UTP port.
   3420  * XXX Actually, we don't yet.
   3421  */
   3422 const struct tulip_srom_to_ifmedia tulip_srom_to_ifmedia_table[] = {
   3423 	{ TULIP_ROM_MB_MEDIA_TP,	IFM_10_T,	0,
   3424 	  "10baseT",
   3425 	  0,
   3426 	  { SIACONN_21040_10BASET,
   3427 	    SIATXRX_21040_10BASET,
   3428 	    SIAGEN_21040_10BASET },
   3429 
   3430 	  { SIACONN_21041_10BASET,
   3431 	    SIATXRX_21041_10BASET,
   3432 	    SIAGEN_21041_10BASET },
   3433 
   3434 	  { SIACONN_21142_10BASET,
   3435 	    SIATXRX_21142_10BASET,
   3436 	    SIAGEN_21142_10BASET } },
   3437 
   3438 	{ TULIP_ROM_MB_MEDIA_BNC,	IFM_10_2,	0,
   3439 	  "10base2",
   3440 	  0,
   3441 	  { 0,
   3442 	    0,
   3443 	    0 },
   3444 
   3445 	  { SIACONN_21041_BNC,
   3446 	    SIATXRX_21041_BNC,
   3447 	    SIAGEN_21041_BNC },
   3448 
   3449 	  { SIACONN_21142_BNC,
   3450 	    SIATXRX_21142_BNC,
   3451 	    SIAGEN_21142_BNC } },
   3452 
   3453 	{ TULIP_ROM_MB_MEDIA_AUI,	IFM_10_5,	0,
   3454 	  "10base5",
   3455 	  0,
   3456 	  { SIACONN_21040_AUI,
   3457 	    SIATXRX_21040_AUI,
   3458 	    SIAGEN_21040_AUI },
   3459 
   3460 	  { SIACONN_21041_AUI,
   3461 	    SIATXRX_21041_AUI,
   3462 	    SIAGEN_21041_AUI },
   3463 
   3464 	  { SIACONN_21142_AUI,
   3465 	    SIATXRX_21142_AUI,
   3466 	    SIAGEN_21142_AUI } },
   3467 
   3468 	{ TULIP_ROM_MB_MEDIA_100TX,	IFM_100_TX,	0,
   3469 	  "100baseTX",
   3470 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
   3471 	  { 0,
   3472 	    0,
   3473 	    0 },
   3474 
   3475 	  { 0,
   3476 	    0,
   3477 	    0 },
   3478 
   3479 	  { 0,
   3480 	    0,
   3481 	    SIAGEN_ABM } },
   3482 
   3483 	{ TULIP_ROM_MB_MEDIA_TP_FDX,	IFM_10_T,	IFM_FDX,
   3484 	  "10baseT-FDX",
   3485 	  OPMODE_FD|OPMODE_HBD,
   3486 	  { SIACONN_21040_10BASET_FDX,
   3487 	    SIATXRX_21040_10BASET_FDX,
   3488 	    SIAGEN_21040_10BASET_FDX },
   3489 
   3490 	  { SIACONN_21041_10BASET_FDX,
   3491 	    SIATXRX_21041_10BASET_FDX,
   3492 	    SIAGEN_21041_10BASET_FDX },
   3493 
   3494 	  { SIACONN_21142_10BASET_FDX,
   3495 	    SIATXRX_21142_10BASET_FDX,
   3496 	    SIAGEN_21142_10BASET_FDX } },
   3497 
   3498 	{ TULIP_ROM_MB_MEDIA_100TX_FDX,	IFM_100_TX,	IFM_FDX,
   3499 	  "100baseTX-FDX",
   3500 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_FD|OPMODE_HBD,
   3501 	  { 0,
   3502 	    0,
   3503 	    0 },
   3504 
   3505 	  { 0,
   3506 	    0,
   3507 	    0 },
   3508 
   3509 	  { 0,
   3510 	    0,
   3511 	    SIAGEN_ABM } },
   3512 
   3513 	{ TULIP_ROM_MB_MEDIA_100T4,	IFM_100_T4,	0,
   3514 	  "100baseT4",
   3515 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
   3516 	  { 0,
   3517 	    0,
   3518 	    0 },
   3519 
   3520 	  { 0,
   3521 	    0,
   3522 	    0 },
   3523 
   3524 	  { 0,
   3525 	    0,
   3526 	    SIAGEN_ABM } },
   3527 
   3528 	{ TULIP_ROM_MB_MEDIA_100FX,	IFM_100_FX,	0,
   3529 	  "100baseFX",
   3530 	  OPMODE_PS|OPMODE_PCS|OPMODE_HBD,
   3531 	  { 0,
   3532 	    0,
   3533 	    0 },
   3534 
   3535 	  { 0,
   3536 	    0,
   3537 	    0 },
   3538 
   3539 	  { 0,
   3540 	    0,
   3541 	    SIAGEN_ABM } },
   3542 
   3543 	{ TULIP_ROM_MB_MEDIA_100FX_FDX,	IFM_100_FX,	IFM_FDX,
   3544 	  "100baseFX-FDX",
   3545 	  OPMODE_PS|OPMODE_PCS|OPMODE_FD|OPMODE_HBD,
   3546 	  { 0,
   3547 	    0,
   3548 	    0 },
   3549 
   3550 	  { 0,
   3551 	    0,
   3552 	    0 },
   3553 
   3554 	  { 0,
   3555 	    0,
   3556 	    SIAGEN_ABM } },
   3557 
   3558 	{ 0,				0,		0,
   3559 	  NULL,
   3560 	  0,
   3561 	  { 0,
   3562 	    0,
   3563 	    0 },
   3564 
   3565 	  { 0,
   3566 	    0,
   3567 	    0 },
   3568 
   3569 	  { 0,
   3570 	    0,
   3571 	    0 } },
   3572 };
   3573 
   3574 const struct tulip_srom_to_ifmedia *tlp_srom_to_ifmedia __P((u_int8_t));
   3575 void	tlp_srom_media_info __P((struct tulip_softc *,
   3576 	    const struct tulip_srom_to_ifmedia *, struct tulip_21x4x_media *));
   3577 void	tlp_add_srom_media __P((struct tulip_softc *, int,
   3578 	    void (*)(struct tulip_softc *, struct ifmediareq *),
   3579 	    int (*)(struct tulip_softc *), const u_int8_t *, int));
   3580 void	tlp_print_media __P((struct tulip_softc *));
   3581 void	tlp_nway_activate __P((struct tulip_softc *, int));
   3582 void	tlp_get_minst __P((struct tulip_softc *));
   3583 
   3584 const struct tulip_srom_to_ifmedia *
   3585 tlp_srom_to_ifmedia(sm)
   3586 	u_int8_t sm;
   3587 {
   3588 	const struct tulip_srom_to_ifmedia *tsti;
   3589 
   3590 	for (tsti = tulip_srom_to_ifmedia_table;
   3591 	     tsti->tsti_name != NULL; tsti++) {
   3592 		if (tsti->tsti_srom == sm)
   3593 			return (tsti);
   3594 	}
   3595 
   3596 	return (NULL);
   3597 }
   3598 
   3599 void
   3600 tlp_srom_media_info(sc, tsti, tm)
   3601 	struct tulip_softc *sc;
   3602 	const struct tulip_srom_to_ifmedia *tsti;
   3603 	struct tulip_21x4x_media *tm;
   3604 {
   3605 
   3606 	tm->tm_name = tsti->tsti_name;
   3607 	tm->tm_opmode = tsti->tsti_opmode;
   3608 
   3609 	switch (sc->sc_chip) {
   3610 	case TULIP_CHIP_DE425:
   3611 	case TULIP_CHIP_21040:
   3612 		tm->tm_sia = tsti->tsti_21040;	/* struct assignment */
   3613 		break;
   3614 
   3615 	case TULIP_CHIP_21041:
   3616 		tm->tm_sia = tsti->tsti_21041;	/* struct assignment */
   3617 		break;
   3618 
   3619 	case TULIP_CHIP_21142:
   3620 	case TULIP_CHIP_21143:
   3621 	case TULIP_CHIP_82C115:
   3622 	case TULIP_CHIP_MX98715:
   3623 	case TULIP_CHIP_MX98715A:
   3624 	case TULIP_CHIP_MX98725:
   3625 		tm->tm_sia = tsti->tsti_21142;	/* struct assignment */
   3626 		break;
   3627 
   3628 	default:
   3629 		/* Nothing. */
   3630 	}
   3631 }
   3632 
   3633 void
   3634 tlp_add_srom_media(sc, type, get, set, list, cnt)
   3635 	struct tulip_softc *sc;
   3636 	int type;
   3637 	void (*get) __P((struct tulip_softc *, struct ifmediareq *));
   3638 	int (*set) __P((struct tulip_softc *));
   3639 	const u_int8_t *list;
   3640 	int cnt;
   3641 {
   3642 	struct tulip_21x4x_media *tm;
   3643 	const struct tulip_srom_to_ifmedia *tsti;
   3644 	int i;
   3645 
   3646 	for (i = 0; i < cnt; i++) {
   3647 		tsti = tlp_srom_to_ifmedia(list[i]);
   3648 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   3649 		memset(tm, 0, sizeof(*tm));
   3650 		tlp_srom_media_info(sc, tsti, tm);
   3651 		tm->tm_type = type;
   3652 		tm->tm_get = get;
   3653 		tm->tm_set = set;
   3654 
   3655 		ifmedia_add(&sc->sc_mii.mii_media,
   3656 		    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   3657 		    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   3658 	}
   3659 }
   3660 
   3661 void
   3662 tlp_print_media(sc)
   3663 	struct tulip_softc *sc;
   3664 {
   3665 	struct ifmedia_entry *ife;
   3666 	struct tulip_21x4x_media *tm;
   3667 	const char *sep = "";
   3668 
   3669 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
   3670 
   3671 	printf("%s: ", sc->sc_dev.dv_xname);
   3672 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   3673 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
   3674 		tm = ife->ifm_aux;
   3675 		if (tm == NULL) {
   3676 #ifdef DIAGNOSTIC
   3677 			if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   3678 				panic("tlp_print_media");
   3679 #endif
   3680 			PRINT("auto");
   3681 		} else if (tm->tm_type != TULIP_ROM_MB_21140_MII &&
   3682 			   tm->tm_type != TULIP_ROM_MB_21142_MII) {
   3683 			PRINT(tm->tm_name);
   3684 		}
   3685 	}
   3686 	printf("\n");
   3687 
   3688 #undef PRINT
   3689 }
   3690 
   3691 void
   3692 tlp_nway_activate(sc, media)
   3693 	struct tulip_softc *sc;
   3694 	int media;
   3695 {
   3696 	struct ifmedia_entry *ife;
   3697 
   3698 	ife = ifmedia_match(&sc->sc_mii.mii_media, media, 0);
   3699 #ifdef DIAGNOSTIC
   3700 	if (ife == NULL)
   3701 		panic("tlp_nway_activate");
   3702 #endif
   3703 	sc->sc_nway_active = ife;
   3704 }
   3705 
   3706 void
   3707 tlp_get_minst(sc)
   3708 	struct tulip_softc *sc;
   3709 {
   3710 
   3711 	if ((sc->sc_media_seen &
   3712 	    ~((1 << TULIP_ROM_MB_21140_MII) |
   3713 	      (1 << TULIP_ROM_MB_21142_MII))) == 0) {
   3714 		/*
   3715 		 * We have not yet seen any SIA/SYM media (but are
   3716 		 * about to; that's why we're called!), so assign
   3717 		 * the current media instance to be the `internal media'
   3718 		 * instance, and advance it so any MII media gets a
   3719 		 * fresh one (used to selecting/isolating a PHY).
   3720 		 */
   3721 		sc->sc_tlp_minst = sc->sc_mii.mii_instance++;
   3722 	}
   3723 }
   3724 
   3725 /*
   3726  * SIA Utility functions.
   3727  */
   3728 void	tlp_sia_update_link __P((struct tulip_softc *));
   3729 void	tlp_sia_get __P((struct tulip_softc *, struct ifmediareq *));
   3730 int	tlp_sia_set __P((struct tulip_softc *));
   3731 void	tlp_sia_fixup __P((struct tulip_softc *));
   3732 
   3733 void
   3734 tlp_sia_update_link(sc)
   3735 	struct tulip_softc *sc;
   3736 {
   3737 	struct ifmedia_entry *ife;
   3738 	struct tulip_21x4x_media *tm;
   3739 	u_int32_t siastat;
   3740 
   3741 	ife = TULIP_CURRENT_MEDIA(sc);
   3742 	tm = ife->ifm_aux;
   3743 
   3744 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
   3745 
   3746 	siastat = TULIP_READ(sc, CSR_SIASTAT);
   3747 
   3748 	/*
   3749 	 * Note that when we do SIA link tests, we are assuming that
   3750 	 * the chip is really in the mode that the current media setting
   3751 	 * reflects.  If we're not, then the link tests will not be
   3752 	 * accurate!
   3753 	 */
   3754 	switch (IFM_SUBTYPE(ife->ifm_media)) {
   3755 	case IFM_10_T:
   3756 		sc->sc_flags |= TULIPF_LINK_VALID;
   3757 		if ((siastat & SIASTAT_LS10) == 0)
   3758 			sc->sc_flags |= TULIPF_LINK_UP;
   3759 		break;
   3760 
   3761 	case IFM_100_TX:
   3762 	case IFM_100_T4:
   3763 		sc->sc_flags |= TULIPF_LINK_VALID;
   3764 		if ((siastat & SIASTAT_LS100) == 0)
   3765 			sc->sc_flags |= TULIPF_LINK_UP;
   3766 		break;
   3767 	}
   3768 
   3769 	switch (sc->sc_chip) {
   3770 	case TULIP_CHIP_21142:
   3771 	case TULIP_CHIP_21143:
   3772 		/*
   3773 		 * On these chips, we can tell more information about
   3774 		 * AUI/BNC.  Note that the AUI/BNC selection is made
   3775 		 * in a different register; for our purpose, it's all
   3776 		 * AUI.
   3777 		 */
   3778 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   3779 		case IFM_10_2:
   3780 		case IFM_10_5:
   3781 			sc->sc_flags |= TULIPF_LINK_VALID;
   3782 			if (siastat & SIASTAT_ARA) {
   3783 				TULIP_WRITE(sc, CSR_SIASTAT, SIASTAT_ARA);
   3784 				sc->sc_flags |= TULIPF_LINK_UP;
   3785 			}
   3786 			break;
   3787 
   3788 		default:
   3789 			/*
   3790 			 * If we're SYM media and can detect the link
   3791 			 * via the GPIO facility, prefer that status
   3792 			 * over LS100.
   3793 			 */
   3794 			if (tm->tm_type == TULIP_ROM_MB_21143_SYM &&
   3795 			    tm->tm_actmask != 0) {
   3796 				sc->sc_flags = (sc->sc_flags &
   3797 				    ~TULIPF_LINK_UP) | TULIPF_LINK_VALID;
   3798 				if (TULIP_ISSET(sc, CSR_SIAGEN,
   3799 				    tm->tm_actmask) == tm->tm_actdata)
   3800 					sc->sc_flags |= TULIPF_LINK_UP;
   3801 			}
   3802 		}
   3803 		break;
   3804 
   3805 	default:
   3806 		/* Nothing. */
   3807 	}
   3808 }
   3809 
   3810 void
   3811 tlp_sia_get(sc, ifmr)
   3812 	struct tulip_softc *sc;
   3813 	struct ifmediareq *ifmr;
   3814 {
   3815 	struct ifmedia_entry *ife;
   3816 
   3817 	ifmr->ifm_status = 0;
   3818 
   3819 	tlp_sia_update_link(sc);
   3820 
   3821 	ife = TULIP_CURRENT_MEDIA(sc);
   3822 
   3823 	if (sc->sc_flags & TULIPF_LINK_VALID)
   3824 		ifmr->ifm_status |= IFM_AVALID;
   3825 	if (sc->sc_flags & TULIPF_LINK_UP)
   3826 		ifmr->ifm_status |= IFM_ACTIVE;
   3827 	ifmr->ifm_active = ife->ifm_media;
   3828 }
   3829 
   3830 void
   3831 tlp_sia_fixup(sc)
   3832 	struct tulip_softc *sc;
   3833 {
   3834 	struct ifmedia_entry *ife;
   3835 	struct tulip_21x4x_media *tm;
   3836 	u_int32_t siaconn, siatxrx, siagen;
   3837 
   3838 	switch (sc->sc_chip) {
   3839 	case TULIP_CHIP_82C115:
   3840 	case TULIP_CHIP_MX98713A:
   3841 	case TULIP_CHIP_MX98715:
   3842 	case TULIP_CHIP_MX98715A:
   3843 	case TULIP_CHIP_MX98725:
   3844 		siaconn = PMAC_SIACONN_MASK;
   3845 		siatxrx = PMAC_SIATXRX_MASK;
   3846 		siagen  = PMAC_SIAGEN_MASK;
   3847 		break;
   3848 
   3849 	default:
   3850 		/* No fixups required on any other chips. */
   3851 		return;
   3852 	}
   3853 
   3854 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   3855 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
   3856 		tm = ife->ifm_aux;
   3857 		if (tm == NULL)
   3858 			continue;
   3859 
   3860 		tm->tm_siaconn &= siaconn;
   3861 		tm->tm_siatxrx &= siatxrx;
   3862 		tm->tm_siagen  &= siagen;
   3863 	}
   3864 }
   3865 
   3866 int
   3867 tlp_sia_set(sc)
   3868 	struct tulip_softc *sc;
   3869 {
   3870 	struct ifmedia_entry *ife;
   3871 	struct tulip_21x4x_media *tm;
   3872 
   3873 	ife = TULIP_CURRENT_MEDIA(sc);
   3874 	tm = ife->ifm_aux;
   3875 
   3876 	/*
   3877 	 * XXX This appears to be necessary on a bunch of the clone chips.
   3878 	 */
   3879 	delay(20000);
   3880 
   3881 	/*
   3882 	 * Idle the chip.
   3883 	 */
   3884 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   3885 
   3886 	/*
   3887 	 * Program the SIA.  It's important to write in this order,
   3888 	 * resetting the SIA first.
   3889 	 */
   3890 	TULIP_WRITE(sc, CSR_SIACONN, 0);		/* SRL bit clear */
   3891 	delay(1000);
   3892 
   3893 	TULIP_WRITE(sc, CSR_SIATXRX, tm->tm_siatxrx);
   3894 
   3895 	switch (sc->sc_chip) {
   3896 	case TULIP_CHIP_21142:
   3897 	case TULIP_CHIP_21143:
   3898 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpctl);
   3899 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpdata);
   3900 		break;
   3901 	default:
   3902 		TULIP_WRITE(sc, CSR_SIAGEN,  tm->tm_siagen);
   3903 	}
   3904 
   3905 	TULIP_WRITE(sc, CSR_SIACONN, tm->tm_siaconn);
   3906 
   3907 	/*
   3908 	 * Set the OPMODE bits for this media and write OPMODE.
   3909 	 * This will resume the transmit and receive processes.
   3910 	 */
   3911 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
   3912 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3913 
   3914 	return (0);
   3915 }
   3916 
   3917 /*
   3918  * 21140 GPIO utility functions.
   3919  */
   3920 void	tlp_21140_gpio_update_link __P((struct tulip_softc *));
   3921 void	tlp_21140_gpio_get __P((struct tulip_softc *sc,
   3922 	    struct ifmediareq *ifmr));
   3923 int	tlp_21140_gpio_set __P((struct tulip_softc *sc));
   3924 
   3925 void
   3926 tlp_21140_gpio_update_link(sc)
   3927 	struct tulip_softc *sc;
   3928 {
   3929 	struct ifmedia_entry *ife;
   3930 	struct tulip_21x4x_media *tm;
   3931 
   3932 	ife = TULIP_CURRENT_MEDIA(sc);
   3933 	tm = ife->ifm_aux;
   3934 
   3935 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
   3936 
   3937 	if (tm->tm_actmask != 0) {
   3938 		sc->sc_flags |= TULIPF_LINK_VALID;
   3939 		if (TULIP_ISSET(sc, CSR_GPP, tm->tm_actmask) ==
   3940 		    tm->tm_actdata)
   3941 			sc->sc_flags |= TULIPF_LINK_UP;
   3942 	}
   3943 }
   3944 
   3945 void
   3946 tlp_21140_gpio_get(sc, ifmr)
   3947 	struct tulip_softc *sc;
   3948 	struct ifmediareq *ifmr;
   3949 {
   3950 	struct ifmedia_entry *ife;
   3951 
   3952 	ifmr->ifm_status = 0;
   3953 
   3954 	tlp_21140_gpio_update_link(sc);
   3955 
   3956 	ife = TULIP_CURRENT_MEDIA(sc);
   3957 
   3958 	if (sc->sc_flags & TULIPF_LINK_VALID)
   3959 		ifmr->ifm_status |= IFM_AVALID;
   3960 	if (sc->sc_flags & TULIPF_LINK_UP)
   3961 		ifmr->ifm_status |= IFM_ACTIVE;
   3962 	ifmr->ifm_active = ife->ifm_media;
   3963 }
   3964 
   3965 int
   3966 tlp_21140_gpio_set(sc)
   3967 	struct tulip_softc *sc;
   3968 {
   3969 	struct ifmedia_entry *ife;
   3970 	struct tulip_21x4x_media *tm;
   3971 
   3972 	ife = TULIP_CURRENT_MEDIA(sc);
   3973 	tm = ife->ifm_aux;
   3974 
   3975 	/*
   3976 	 * Idle the chip.
   3977 	 */
   3978 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   3979 
   3980 	/*
   3981 	 * Set the GPIO pins for this media, to flip any
   3982 	 * relays, etc.
   3983 	 */
   3984 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   3985 	delay(10);
   3986 	TULIP_WRITE(sc, CSR_GPP, tm->tm_gpdata);
   3987 
   3988 	/*
   3989 	 * Set the OPMODE bits for this media and write OPMODE.
   3990 	 * This will resume the transmit and receive processes.
   3991 	 */
   3992 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
   3993 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3994 
   3995 	return (0);
   3996 }
   3997 
   3998 /*
   3999  * 21040 and 21041 media switches.
   4000  */
   4001 void	tlp_21040_tmsw_init __P((struct tulip_softc *));
   4002 void	tlp_21040_tp_tmsw_init __P((struct tulip_softc *));
   4003 void	tlp_21040_auibnc_tmsw_init __P((struct tulip_softc *));
   4004 void	tlp_21041_tmsw_init __P((struct tulip_softc *));
   4005 
   4006 const struct tulip_mediasw tlp_21040_mediasw = {
   4007 	tlp_21040_tmsw_init, tlp_sia_get, tlp_sia_set
   4008 };
   4009 
   4010 const struct tulip_mediasw tlp_21040_tp_mediasw = {
   4011 	tlp_21040_tp_tmsw_init, tlp_sia_get, tlp_sia_set
   4012 };
   4013 
   4014 const struct tulip_mediasw tlp_21040_auibnc_mediasw = {
   4015 	tlp_21040_auibnc_tmsw_init, tlp_sia_get, tlp_sia_set
   4016 };
   4017 
   4018 const struct tulip_mediasw tlp_21041_mediasw = {
   4019 	tlp_21041_tmsw_init, tlp_sia_get, tlp_sia_set
   4020 };
   4021 
   4022 
   4023 void
   4024 tlp_21040_tmsw_init(sc)
   4025 	struct tulip_softc *sc;
   4026 {
   4027 	static const u_int8_t media[] = {
   4028 		TULIP_ROM_MB_MEDIA_TP,
   4029 		TULIP_ROM_MB_MEDIA_TP_FDX,
   4030 		TULIP_ROM_MB_MEDIA_AUI,
   4031 	};
   4032 	struct tulip_21x4x_media *tm;
   4033 
   4034 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4035 	    tlp_mediastatus);
   4036 
   4037 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 3);
   4038 
   4039 	/*
   4040 	 * No SROM type for External SIA.
   4041 	 */
   4042 	tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4043 	memset(tm, 0, sizeof(*tm));
   4044 	tm->tm_name = "manual";
   4045 	tm->tm_opmode = 0;
   4046 	tm->tm_siaconn = SIACONN_21040_EXTSIA;
   4047 	tm->tm_siatxrx = SIATXRX_21040_EXTSIA;
   4048 	tm->tm_siagen  = SIAGEN_21040_EXTSIA;
   4049 	ifmedia_add(&sc->sc_mii.mii_media,
   4050 	    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, sc->sc_tlp_minst), 0, tm);
   4051 
   4052 	/*
   4053 	 * XXX Autosense not yet supported.
   4054 	 */
   4055 
   4056 	/* XXX This should be auto-sense. */
   4057 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   4058 
   4059 	tlp_print_media(sc);
   4060 }
   4061 
   4062 void
   4063 tlp_21040_tp_tmsw_init(sc)
   4064 	struct tulip_softc *sc;
   4065 {
   4066 	static const u_int8_t media[] = {
   4067 		TULIP_ROM_MB_MEDIA_TP,
   4068 		TULIP_ROM_MB_MEDIA_TP_FDX,
   4069 	};
   4070 
   4071 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4072 	    tlp_mediastatus);
   4073 
   4074 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 2);
   4075 
   4076 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   4077 
   4078 	tlp_print_media(sc);
   4079 }
   4080 
   4081 void
   4082 tlp_21040_auibnc_tmsw_init(sc)
   4083 	struct tulip_softc *sc;
   4084 {
   4085 	static const u_int8_t media[] = {
   4086 		TULIP_ROM_MB_MEDIA_AUI,
   4087 	};
   4088 
   4089 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4090 	    tlp_mediastatus);
   4091 
   4092 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 1);
   4093 
   4094 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5);
   4095 
   4096 	tlp_print_media(sc);
   4097 }
   4098 
   4099 void
   4100 tlp_21041_tmsw_init(sc)
   4101 	struct tulip_softc *sc;
   4102 {
   4103 	static const u_int8_t media[] = {
   4104 		TULIP_ROM_MB_MEDIA_TP,
   4105 		TULIP_ROM_MB_MEDIA_TP_FDX,
   4106 		TULIP_ROM_MB_MEDIA_BNC,
   4107 		TULIP_ROM_MB_MEDIA_AUI,
   4108 	};
   4109 	int i, defmedia, devcnt, leaf_offset, mb_offset, m_cnt;
   4110 	const struct tulip_srom_to_ifmedia *tsti;
   4111 	struct tulip_21x4x_media *tm;
   4112 	u_int16_t romdef;
   4113 	u_int8_t mb;
   4114 
   4115 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4116 	    tlp_mediastatus);
   4117 
   4118 	if (tlp_isv_srom(sc->sc_srom) == 0) {
   4119  not_isv_srom:
   4120 		/*
   4121 		 * If we have a board without the standard 21041 SROM format,
   4122 		 * we just assume all media are present and try and pick a
   4123 		 * reasonable default.
   4124 		 */
   4125 		tlp_add_srom_media(sc, 0, NULL, NULL, media, 4);
   4126 
   4127 		/*
   4128 		 * XXX Autosense not yet supported.
   4129 		 */
   4130 
   4131 		/* XXX This should be auto-sense. */
   4132 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   4133 
   4134 		tlp_print_media(sc);
   4135 		return;
   4136 	}
   4137 
   4138 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   4139 	for (i = 0; i < devcnt; i++) {
   4140 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   4141 			break;
   4142 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   4143 		    sc->sc_devno)
   4144 			break;
   4145 	}
   4146 
   4147 	if (i == devcnt)
   4148 		goto not_isv_srom;
   4149 
   4150 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
   4151 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
   4152 	mb_offset = leaf_offset + TULIP_ROM_IL_MEDIAn_BLOCK_BASE;
   4153 	m_cnt = sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
   4154 
   4155 	for (; m_cnt != 0;
   4156 	     m_cnt--, mb_offset += TULIP_ROM_MB_SIZE(mb)) {
   4157 		mb = sc->sc_srom[mb_offset];
   4158 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4159 		memset(tm, 0, sizeof(*tm));
   4160 		switch (mb & TULIP_ROM_MB_MEDIA_CODE) {
   4161 		case TULIP_ROM_MB_MEDIA_TP_FDX:
   4162 		case TULIP_ROM_MB_MEDIA_TP:
   4163 		case TULIP_ROM_MB_MEDIA_BNC:
   4164 		case TULIP_ROM_MB_MEDIA_AUI:
   4165 			tsti = tlp_srom_to_ifmedia(mb &
   4166 			    TULIP_ROM_MB_MEDIA_CODE);
   4167 
   4168 			tlp_srom_media_info(sc, tsti, tm);
   4169 
   4170 			/*
   4171 			 * Override our default SIA settings if the
   4172 			 * SROM contains its own.
   4173 			 */
   4174 			if (mb & TULIP_ROM_MB_EXT) {
   4175 				tm->tm_siaconn = TULIP_ROM_GETW(sc->sc_srom,
   4176 				    mb_offset + TULIP_ROM_MB_CSR13);
   4177 				tm->tm_siatxrx = TULIP_ROM_GETW(sc->sc_srom,
   4178 				    mb_offset + TULIP_ROM_MB_CSR14);
   4179 				tm->tm_siagen = TULIP_ROM_GETW(sc->sc_srom,
   4180 				    mb_offset + TULIP_ROM_MB_CSR15);
   4181 			}
   4182 
   4183 			ifmedia_add(&sc->sc_mii.mii_media,
   4184 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4185 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4186 			break;
   4187 
   4188 		default:
   4189 			printf("%s: unknown media code 0x%02x\n",
   4190 			    sc->sc_dev.dv_xname,
   4191 			    mb & TULIP_ROM_MB_MEDIA_CODE);
   4192 			free(tm, M_DEVBUF);
   4193 		}
   4194 	}
   4195 
   4196 	/*
   4197 	 * XXX Autosense not yet supported.
   4198 	 */
   4199 
   4200 	romdef = TULIP_ROM_GETW(sc->sc_srom, leaf_offset +
   4201 	    TULIP_ROM_IL_SELECT_CONN_TYPE);
   4202 	switch (romdef) {
   4203 	case SELECT_CONN_TYPE_TP:
   4204 	case SELECT_CONN_TYPE_TP_AUTONEG:
   4205 	case SELECT_CONN_TYPE_TP_NOLINKPASS:
   4206 		defmedia = IFM_ETHER|IFM_10_T;
   4207 		break;
   4208 
   4209 	case SELECT_CONN_TYPE_TP_FDX:
   4210 		defmedia = IFM_ETHER|IFM_10_T|IFM_FDX;
   4211 		break;
   4212 
   4213 	case SELECT_CONN_TYPE_BNC:
   4214 		defmedia = IFM_ETHER|IFM_10_2;
   4215 		break;
   4216 
   4217 	case SELECT_CONN_TYPE_AUI:
   4218 		defmedia = IFM_ETHER|IFM_10_5;
   4219 		break;
   4220 #if 0 /* XXX */
   4221 	case SELECT_CONN_TYPE_ASENSE:
   4222 	case SELECT_CONN_TYPE_ASENSE_AUTONEG:
   4223 		defmedia = IFM_ETHER|IFM_AUTO;
   4224 		break;
   4225 #endif
   4226 	default:
   4227 		defmedia = 0;
   4228 	}
   4229 
   4230 	if (defmedia == 0) {
   4231 		/*
   4232 		 * XXX We should default to auto-sense.
   4233 		 */
   4234 		defmedia = IFM_ETHER|IFM_10_T;
   4235 	}
   4236 
   4237 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
   4238 
   4239 	tlp_print_media(sc);
   4240 }
   4241 
   4242 /*
   4243  * DECchip 2114x ISV media switch.
   4244  */
   4245 void	tlp_2114x_isv_tmsw_init __P((struct tulip_softc *));
   4246 void	tlp_2114x_isv_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
   4247 int	tlp_2114x_isv_tmsw_set __P((struct tulip_softc *));
   4248 
   4249 const struct tulip_mediasw tlp_2114x_isv_mediasw = {
   4250 	tlp_2114x_isv_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
   4251 };
   4252 
   4253 void
   4254 tlp_2114x_isv_tmsw_init(sc)
   4255 	struct tulip_softc *sc;
   4256 {
   4257 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4258 	struct ifmedia_entry *ife;
   4259 	struct mii_softc *phy;
   4260 	struct tulip_21x4x_media *tm;
   4261 	const struct tulip_srom_to_ifmedia *tsti;
   4262 	int i, devcnt, leaf_offset, m_cnt, type, length;
   4263 	int defmedia, miidef;
   4264 	u_int16_t word;
   4265 	u_int8_t *cp, *ncp;
   4266 
   4267 	defmedia = miidef = 0;
   4268 
   4269 	sc->sc_mii.mii_ifp = ifp;
   4270 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
   4271 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
   4272 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4273 
   4274 	/*
   4275 	 * Ignore `instance'; we may get a mixture of SIA and MII
   4276 	 * media, and `instance' is used to isolate or select the
   4277 	 * PHY on the MII as appropriate.  Note that duplicate media
   4278 	 * are disallowed, so ignoring `instance' is safe.
   4279 	 */
   4280 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, tlp_mediachange,
   4281 	    tlp_mediastatus);
   4282 
   4283 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   4284 	for (i = 0; i < devcnt; i++) {
   4285 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   4286 			break;
   4287 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   4288 		    sc->sc_devno)
   4289 			break;
   4290 	}
   4291 
   4292 	if (i == devcnt) {
   4293 		printf("%s: unable to locate info leaf in SROM\n",
   4294 		    sc->sc_dev.dv_xname);
   4295 		return;
   4296 	}
   4297 
   4298 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
   4299 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
   4300 
   4301 	/* XXX SELECT CONN TYPE */
   4302 
   4303 	cp = &sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
   4304 
   4305 	/*
   4306 	 * On some chips, the first thing in the Info Leaf is the
   4307 	 * GPIO pin direction data.
   4308 	 */
   4309 	switch (sc->sc_chip) {
   4310 	case TULIP_CHIP_21140:
   4311 	case TULIP_CHIP_21140A:
   4312 	case TULIP_CHIP_MX98713:
   4313 	case TULIP_CHIP_AX88140:
   4314 	case TULIP_CHIP_AX88141:
   4315 		sc->sc_gp_dir = *cp++;
   4316 		break;
   4317 
   4318 	default:
   4319 		/* Nothing. */
   4320 	}
   4321 
   4322 	/* Get the media count. */
   4323 	m_cnt = *cp++;
   4324 
   4325 	for (; m_cnt != 0; cp = ncp, m_cnt--) {
   4326 		/*
   4327 		 * Determine the type and length of this media block.
   4328 		 */
   4329 		if ((*cp & 0x80) == 0) {
   4330 			length = 4;
   4331 			type = TULIP_ROM_MB_21140_GPR;
   4332 		} else {
   4333 			length = (*cp++ & 0x7f) - 1;
   4334 			type = *cp++ & 0x3f;
   4335 		}
   4336 
   4337 		/* Compute the start of the next block. */
   4338 		ncp = cp + length;
   4339 
   4340 		/* Now, parse the block. */
   4341 		switch (type) {
   4342 		case TULIP_ROM_MB_21140_GPR:
   4343 			tlp_get_minst(sc);
   4344 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_GPR;
   4345 
   4346 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4347 			memset(tm, 0, sizeof(*tm));
   4348 
   4349 			tm->tm_type = TULIP_ROM_MB_21140_GPR;
   4350 			tm->tm_get = tlp_21140_gpio_get;
   4351 			tm->tm_set = tlp_21140_gpio_set;
   4352 
   4353 			/* First is the media type code. */
   4354 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4355 			    TULIP_ROM_MB_MEDIA_CODE);
   4356 			if (tsti == NULL) {
   4357 				/* Invalid media code. */
   4358 				free(tm, M_DEVBUF);
   4359 				break;
   4360 			}
   4361 
   4362 			/* Get defaults. */
   4363 			tlp_srom_media_info(sc, tsti, tm);
   4364 
   4365 			/* Next is any GPIO info for this media. */
   4366 			tm->tm_gpdata = cp[1];
   4367 
   4368 			/*
   4369 			 * Next is a word containing OPMODE information
   4370 			 * and info on how to detect if this media is
   4371 			 * active.
   4372 			 */
   4373 			word = TULIP_ROM_GETW(cp, 2);
   4374 			tm->tm_opmode = TULIP_ROM_MB_OPMODE(word);
   4375 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
   4376 				tm->tm_actmask =
   4377 				    TULIP_ROM_MB_BITPOS(word);
   4378 				tm->tm_actdata =
   4379 				    (word & TULIP_ROM_MB_POLARITY) ?
   4380 				    0 : tm->tm_actmask;
   4381 			}
   4382 
   4383 			ifmedia_add(&sc->sc_mii.mii_media,
   4384 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4385 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4386 			break;
   4387 
   4388 		case TULIP_ROM_MB_21140_MII:
   4389 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_MII;
   4390 
   4391 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4392 			memset(tm, 0, sizeof(*tm));
   4393 
   4394 			tm->tm_type = TULIP_ROM_MB_21140_MII;
   4395 			tm->tm_get = tlp_mii_getmedia;
   4396 			tm->tm_set = tlp_mii_setmedia;
   4397 			tm->tm_opmode = OPMODE_PS;
   4398 
   4399 			if (sc->sc_reset == NULL)
   4400 				sc->sc_reset = tlp_21140_reset;
   4401 
   4402 			/* First is the PHY number. */
   4403 			tm->tm_phyno = *cp++;
   4404 
   4405 			/* Next is the MII select sequence length and offset. */
   4406 			tm->tm_gp_length = *cp++;
   4407 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
   4408 			cp += tm->tm_gp_length;
   4409 
   4410 			/* Next is the MII reset sequence length and offset. */
   4411 			tm->tm_reset_length = *cp++;
   4412 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
   4413 			cp += tm->tm_reset_length;
   4414 
   4415 			/*
   4416 			 * The following items are left in the media block
   4417 			 * that we don't particularly care about:
   4418 			 *
   4419 			 *	capabilities		W
   4420 			 *	advertisement		W
   4421 			 *	full duplex		W
   4422 			 *	tx threshold		W
   4423 			 *
   4424 			 * These appear to be bits in the PHY registers,
   4425 			 * which our MII code handles on its own.
   4426 			 */
   4427 
   4428 			/*
   4429 			 * Before we probe the MII bus, we need to reset
   4430 			 * it and issue the selection sequence.
   4431 			 */
   4432 
   4433 			/* Set the direction of the pins... */
   4434 			TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   4435 
   4436 			for (i = 0; i < tm->tm_reset_length; i++) {
   4437 				delay(10);
   4438 				TULIP_WRITE(sc, CSR_GPP,
   4439 				    sc->sc_srom[tm->tm_reset_offset + i]);
   4440 			}
   4441 
   4442 			for (i = 0; i < tm->tm_gp_length; i++) {
   4443 				delay(10);
   4444 				TULIP_WRITE(sc, CSR_GPP,
   4445 				    sc->sc_srom[tm->tm_gp_offset + i]);
   4446 			}
   4447 
   4448 			/* If there were no sequences, just lower the pins. */
   4449 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   4450 				delay(10);
   4451 				TULIP_WRITE(sc, CSR_GPP, 0);
   4452 			}
   4453 
   4454 			/*
   4455 			 * Now, probe the MII for the PHY.  Note, we know
   4456 			 * the location of the PHY on the bus, but we don't
   4457 			 * particularly care; the MII code just likes to
   4458 			 * search the whole thing anyhow.
   4459 			 */
   4460 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
   4461 			    MII_PHY_ANY, tm->tm_phyno, 0);
   4462 
   4463 			/*
   4464 			 * Now, search for the PHY we hopefully just
   4465 			 * configured.  If it's not configured into the
   4466 			 * kernel, we lose.  The PHY's default media always
   4467 			 * takes priority.
   4468 			 */
   4469 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
   4470 			     phy != NULL;
   4471 			     phy = LIST_NEXT(phy, mii_list))
   4472 				if (phy->mii_offset == tm->tm_phyno)
   4473 					break;
   4474 			if (phy == NULL) {
   4475 				printf("%s: unable to configure MII\n",
   4476 				    sc->sc_dev.dv_xname);
   4477 				break;
   4478 			}
   4479 
   4480 			sc->sc_flags |= TULIPF_HAS_MII;
   4481 			sc->sc_tick = tlp_mii_tick;
   4482 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
   4483 			    phy->mii_inst);
   4484 
   4485 			/*
   4486 			 * Okay, now that we've found the PHY and the MII
   4487 			 * layer has added all of the media associated
   4488 			 * with that PHY, we need to traverse the media
   4489 			 * list, and add our `tm' to each entry's `aux'
   4490 			 * pointer.
   4491 			 *
   4492 			 * We do this by looking for media with our
   4493 			 * PHY's `instance'.
   4494 			 */
   4495 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   4496 			     ife != NULL;
   4497 			     ife = TAILQ_NEXT(ife, ifm_list)) {
   4498 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
   4499 					continue;
   4500 				ife->ifm_aux = tm;
   4501 			}
   4502 			break;
   4503 
   4504 		case TULIP_ROM_MB_21142_SIA:
   4505 			tlp_get_minst(sc);
   4506 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_SIA;
   4507 
   4508 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4509 			memset(tm, 0, sizeof(*tm));
   4510 
   4511 			tm->tm_type = TULIP_ROM_MB_21142_SIA;
   4512 			tm->tm_get = tlp_sia_get;
   4513 			tm->tm_set = tlp_sia_set;
   4514 
   4515 			/* First is the media type code. */
   4516 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4517 			    TULIP_ROM_MB_MEDIA_CODE);
   4518 			if (tsti == NULL) {
   4519 				/* Invalid media code. */
   4520 				free(tm, M_DEVBUF);
   4521 				break;
   4522 			}
   4523 
   4524 			/* Get defaults. */
   4525 			tlp_srom_media_info(sc, tsti, tm);
   4526 
   4527 			/*
   4528 			 * Override our default SIA settings if the
   4529 			 * SROM contains its own.
   4530 			 */
   4531 			if (cp[0] & 0x40) {
   4532 				tm->tm_siaconn = TULIP_ROM_GETW(cp, 1);
   4533 				tm->tm_siatxrx = TULIP_ROM_GETW(cp, 3);
   4534 				tm->tm_siagen  = TULIP_ROM_GETW(cp, 5);
   4535 				cp += 7;
   4536 			} else
   4537 				cp++;
   4538 
   4539 			/* Next is GPIO control/data. */
   4540 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 0);
   4541 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 2);
   4542 
   4543 			ifmedia_add(&sc->sc_mii.mii_media,
   4544 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4545 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4546 			break;
   4547 
   4548 		case TULIP_ROM_MB_21142_MII:
   4549 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_MII;
   4550 
   4551 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4552 			memset(tm, 0, sizeof(*tm));
   4553 
   4554 			tm->tm_type = TULIP_ROM_MB_21142_MII;
   4555 			tm->tm_get = tlp_mii_getmedia;
   4556 			tm->tm_set = tlp_mii_setmedia;
   4557 			tm->tm_opmode = OPMODE_PS;
   4558 
   4559 			if (sc->sc_reset == NULL)
   4560 				sc->sc_reset = tlp_21142_reset;
   4561 
   4562 			/* First is the PHY number. */
   4563 			tm->tm_phyno = *cp++;
   4564 
   4565 			/* Next is the MII select sequence length and offset. */
   4566 			tm->tm_gp_length = *cp++;
   4567 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
   4568 			cp += tm->tm_gp_length * 2;
   4569 
   4570 			/* Next is the MII reset sequence length and offset. */
   4571 			tm->tm_reset_length = *cp++;
   4572 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
   4573 			cp += tm->tm_reset_length * 2;
   4574 
   4575 			/*
   4576 			 * The following items are left in the media block
   4577 			 * that we don't particularly care about:
   4578 			 *
   4579 			 *	capabilities		W
   4580 			 *	advertisement		W
   4581 			 *	full duplex		W
   4582 			 *	tx threshold		W
   4583 			 *	MII interrupt		W
   4584 			 *
   4585 			 * These appear to be bits in the PHY registers,
   4586 			 * which our MII code handles on its own.
   4587 			 */
   4588 
   4589 			/*
   4590 			 * Before we probe the MII bus, we need to reset
   4591 			 * it and issue the selection sequence.
   4592 			 */
   4593 
   4594 			ncp = &sc->sc_srom[tm->tm_reset_offset];
   4595 			for (i = 0; i < tm->tm_reset_length; i++, ncp += 2) {
   4596 				delay(10);
   4597 				TULIP_WRITE(sc, CSR_SIAGEN,
   4598 				    TULIP_ROM_GETW(ncp, 0) << 16);
   4599 			}
   4600 
   4601 			ncp = &sc->sc_srom[tm->tm_gp_offset];
   4602 			for (i = 0; i < tm->tm_gp_length; i++, ncp += 2) {
   4603 				delay(10);
   4604 				TULIP_WRITE(sc, CSR_SIAGEN,
   4605 				    TULIP_ROM_GETW(ncp, 0) << 16);
   4606 			}
   4607 
   4608 			/* If there were no sequences, just lower the pins. */
   4609 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   4610 				delay(10);
   4611 				TULIP_WRITE(sc, CSR_SIAGEN, 0);
   4612 			}
   4613 
   4614 			/*
   4615 			 * Now, probe the MII for the PHY.  Note, we know
   4616 			 * the location of the PHY on the bus, but we don't
   4617 			 * particularly care; the MII code just likes to
   4618 			 * search the whole thing anyhow.
   4619 			 */
   4620 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
   4621 			    MII_PHY_ANY, tm->tm_phyno, 0);
   4622 
   4623 			/*
   4624 			 * Now, search for the PHY we hopefully just
   4625 			 * configured.  If it's not configured into the
   4626 			 * kernel, we lose.  The PHY's default media always
   4627 			 * takes priority.
   4628 			 */
   4629 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
   4630 			     phy != NULL;
   4631 			     phy = LIST_NEXT(phy, mii_list))
   4632 				if (phy->mii_offset == tm->tm_phyno)
   4633 					break;
   4634 			if (phy == NULL) {
   4635 				printf("%s: unable to configure MII\n",
   4636 				    sc->sc_dev.dv_xname);
   4637 				break;
   4638 			}
   4639 
   4640 			sc->sc_flags |= TULIPF_HAS_MII;
   4641 			sc->sc_tick = tlp_mii_tick;
   4642 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
   4643 			    phy->mii_inst);
   4644 
   4645 			/*
   4646 			 * Okay, now that we've found the PHY and the MII
   4647 			 * layer has added all of the media associated
   4648 			 * with that PHY, we need to traverse the media
   4649 			 * list, and add our `tm' to each entry's `aux'
   4650 			 * pointer.
   4651 			 *
   4652 			 * We do this by looking for media with our
   4653 			 * PHY's `instance'.
   4654 			 */
   4655 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   4656 			     ife != NULL;
   4657 			     ife = TAILQ_NEXT(ife, ifm_list)) {
   4658 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
   4659 					continue;
   4660 				ife->ifm_aux = tm;
   4661 			}
   4662 			break;
   4663 
   4664 		case TULIP_ROM_MB_21143_SYM:
   4665 			tlp_get_minst(sc);
   4666 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21143_SYM;
   4667 
   4668 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4669 			memset(tm, 0, sizeof(*tm));
   4670 
   4671 			tm->tm_type = TULIP_ROM_MB_21143_SYM;
   4672 			tm->tm_get = tlp_sia_get;
   4673 			tm->tm_set = tlp_sia_set;
   4674 
   4675 			/* First is the media type code. */
   4676 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4677 			    TULIP_ROM_MB_MEDIA_CODE);
   4678 			if (tsti == NULL) {
   4679 				/* Invalid media code. */
   4680 				free(tm, M_DEVBUF);
   4681 				break;
   4682 			}
   4683 
   4684 			/* Get defaults. */
   4685 			tlp_srom_media_info(sc, tsti, tm);
   4686 
   4687 			/* Next is GPIO control/data. */
   4688 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 1);
   4689 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 3);
   4690 
   4691 			/*
   4692 			 * Next is a word containing OPMODE information
   4693 			 * and info on how to detect if this media is
   4694 			 * active.
   4695 			 */
   4696 			word = TULIP_ROM_GETW(cp, 5);
   4697 			tm->tm_opmode = TULIP_ROM_MB_OPMODE(word);
   4698 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
   4699 				tm->tm_actmask =
   4700 				    TULIP_ROM_MB_BITPOS(word);
   4701 				tm->tm_actdata =
   4702 				    (word & TULIP_ROM_MB_POLARITY) ?
   4703 				    0 : tm->tm_actmask;
   4704 			}
   4705 
   4706 			ifmedia_add(&sc->sc_mii.mii_media,
   4707 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4708 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4709 			break;
   4710 
   4711 		case TULIP_ROM_MB_21143_RESET:
   4712 			printf("%s: 21143 reset block\n", sc->sc_dev.dv_xname);
   4713 			break;
   4714 
   4715 		default:
   4716 			printf("%s: unknown ISV media block type 0x%02x\n",
   4717 			    sc->sc_dev.dv_xname, type);
   4718 		}
   4719 	}
   4720 
   4721 	/*
   4722 	 * Deal with the case where no media is configured.
   4723 	 */
   4724 	if (TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list) == NULL) {
   4725 		printf("%s: no media found!\n", sc->sc_dev.dv_xname);
   4726 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   4727 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   4728 		return;
   4729 	}
   4730 
   4731 	/*
   4732 	 * Pick the default media.
   4733 	 */
   4734 	if (miidef != 0)
   4735 		defmedia = miidef;
   4736 	else {
   4737 		/*
   4738 		 * XXX Pick a better default.  Should come from SROM
   4739 		 * XXX on 21140[A], and should be "auto" on 21142,
   4740 		 * XXX 21143, and Macronix chips.
   4741 		 */
   4742 		defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
   4743 	}
   4744 
   4745 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
   4746 
   4747 	/*
   4748 	 * Display any non-MII media we've located.
   4749 	 */
   4750 	if (sc->sc_media_seen &
   4751 	    ~((1 << TULIP_ROM_MB_21140_MII) | (1 << TULIP_ROM_MB_21142_MII)))
   4752 		tlp_print_media(sc);
   4753 
   4754 	tlp_sia_fixup(sc);
   4755 }
   4756 
   4757 void
   4758 tlp_2114x_isv_tmsw_get(sc, ifmr)
   4759 	struct tulip_softc *sc;
   4760 	struct ifmediareq *ifmr;
   4761 {
   4762 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   4763 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   4764 
   4765 	/*
   4766 	 * We might be polling a non-MII autosense; check for that.
   4767 	 */
   4768 	if (tm == NULL) {
   4769 #ifdef DIAGNOSTIC
   4770 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   4771 			panic("tlp_2114x_isv_tmsw_get");
   4772 #endif
   4773 		tm = sc->sc_nway_active->ifm_aux;
   4774 	}
   4775 
   4776 	(*tm->tm_get)(sc, ifmr);
   4777 }
   4778 
   4779 int
   4780 tlp_2114x_isv_tmsw_set(sc)
   4781 	struct tulip_softc *sc;
   4782 {
   4783 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   4784 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   4785 
   4786 	/*
   4787 	 * We might be setting a non-MII autosense; check for that.
   4788 	 */
   4789 	if (tm == NULL) {
   4790 #ifdef DIAGNOSTIC
   4791 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   4792 			panic("tlp_2114x_isv_tmsw_set");
   4793 #endif
   4794 		/* XXX XXX XXX */
   4795 	}
   4796 
   4797 	/*
   4798 	 * Check to see if we need to reset the chip, and do it.  The
   4799 	 * reset path will get the OPMODE register right the next
   4800 	 * time through.
   4801 	 */
   4802 	if (TULIP_MEDIA_NEEDSRESET(sc, tm->tm_opmode))
   4803 		return (tlp_init(sc));
   4804 
   4805 	return ((*tm->tm_set)(sc));
   4806 }
   4807 
   4808 /*
   4809  * MII-on-SIO media switch.  Handles only MII attached to the SIO.
   4810  */
   4811 void	tlp_sio_mii_tmsw_init __P((struct tulip_softc *));
   4812 
   4813 const struct tulip_mediasw tlp_sio_mii_mediasw = {
   4814 	tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   4815 };
   4816 
   4817 void
   4818 tlp_sio_mii_tmsw_init(sc)
   4819 	struct tulip_softc *sc;
   4820 {
   4821 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4822 
   4823 	/*
   4824 	 * We don't attach any media info structures to the ifmedia
   4825 	 * entries, so if we're using a pre-init function that needs
   4826 	 * that info, override it to one that doesn't.
   4827 	 */
   4828 	if (sc->sc_preinit == tlp_2114x_preinit)
   4829 		sc->sc_preinit = tlp_2114x_mii_preinit;
   4830 
   4831 	sc->sc_mii.mii_ifp = ifp;
   4832 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
   4833 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
   4834 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4835 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4836 	    tlp_mediastatus);
   4837 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   4838 	    MII_OFFSET_ANY, 0);
   4839 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   4840 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   4841 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   4842 	} else {
   4843 		sc->sc_flags |= TULIPF_HAS_MII;
   4844 		sc->sc_tick = tlp_mii_tick;
   4845 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4846 	}
   4847 }
   4848 
   4849 /*
   4850  * Lite-On PNIC media switch.  Must handle MII or internal NWAY.
   4851  */
   4852 void	tlp_pnic_tmsw_init __P((struct tulip_softc *));
   4853 void	tlp_pnic_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
   4854 int	tlp_pnic_tmsw_set __P((struct tulip_softc *));
   4855 
   4856 const struct tulip_mediasw tlp_pnic_mediasw = {
   4857 	tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
   4858 };
   4859 
   4860 void	tlp_pnic_nway_statchg __P((struct device *));
   4861 void	tlp_pnic_nway_tick __P((void *));
   4862 int	tlp_pnic_nway_service __P((struct tulip_softc *, int));
   4863 void	tlp_pnic_nway_reset __P((struct tulip_softc *));
   4864 int	tlp_pnic_nway_auto __P((struct tulip_softc *, int));
   4865 void	tlp_pnic_nway_auto_timeout __P((void *));
   4866 void	tlp_pnic_nway_status __P((struct tulip_softc *));
   4867 void	tlp_pnic_nway_acomp __P((struct tulip_softc *));
   4868 
   4869 void
   4870 tlp_pnic_tmsw_init(sc)
   4871 	struct tulip_softc *sc;
   4872 {
   4873 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4874 	const char *sep = "";
   4875 
   4876 #define	ADD(m, c)	ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL)
   4877 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
   4878 
   4879 	sc->sc_mii.mii_ifp = ifp;
   4880 	sc->sc_mii.mii_readreg = tlp_pnic_mii_readreg;
   4881 	sc->sc_mii.mii_writereg = tlp_pnic_mii_writereg;
   4882 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4883 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4884 	    tlp_mediastatus);
   4885 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   4886 	    MII_OFFSET_ANY, 0);
   4887 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   4888 		/* XXX What about AUI/BNC support? */
   4889 		printf("%s: ", sc->sc_dev.dv_xname);
   4890 
   4891 		tlp_pnic_nway_reset(sc);
   4892 
   4893 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
   4894 		    PNIC_NWAY_TW|PNIC_NWAY_CAP10T);
   4895 		PRINT("10baseT");
   4896 
   4897 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
   4898 		    PNIC_NWAY_TW|PNIC_NWAY_FD|PNIC_NWAY_CAP10TFDX);
   4899 		PRINT("10baseT-FDX");
   4900 
   4901 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
   4902 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_CAP100TX);
   4903 		PRINT("100baseTX");
   4904 
   4905 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
   4906 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_FD|
   4907 		    PNIC_NWAY_CAP100TXFDX);
   4908 		PRINT("100baseTX-FDX");
   4909 
   4910 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
   4911 		    PNIC_NWAY_TW|PNIC_NWAY_RN|PNIC_NWAY_NW|
   4912 		    PNIC_NWAY_CAP10T|PNIC_NWAY_CAP10TFDX|
   4913 		    PNIC_NWAY_CAP100TXFDX|PNIC_NWAY_CAP100TX);
   4914 		PRINT("auto");
   4915 
   4916 		printf("\n");
   4917 
   4918 		sc->sc_statchg = tlp_pnic_nway_statchg;
   4919 		sc->sc_tick = tlp_pnic_nway_tick;
   4920 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4921 	} else {
   4922 		sc->sc_flags |= TULIPF_HAS_MII;
   4923 		sc->sc_tick = tlp_mii_tick;
   4924 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4925 	}
   4926 
   4927 #undef ADD
   4928 #undef PRINT
   4929 }
   4930 
   4931 void
   4932 tlp_pnic_tmsw_get(sc, ifmr)
   4933 	struct tulip_softc *sc;
   4934 	struct ifmediareq *ifmr;
   4935 {
   4936 	struct mii_data *mii = &sc->sc_mii;
   4937 
   4938 	if (sc->sc_flags & TULIPF_HAS_MII)
   4939 		tlp_mii_getmedia(sc, ifmr);
   4940 	else {
   4941 		mii->mii_media_status = 0;
   4942 		mii->mii_media_active = IFM_NONE;
   4943 		tlp_pnic_nway_service(sc, MII_POLLSTAT);
   4944 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
   4945 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
   4946 	}
   4947 }
   4948 
   4949 int
   4950 tlp_pnic_tmsw_set(sc)
   4951 	struct tulip_softc *sc;
   4952 {
   4953 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4954 	struct mii_data *mii = &sc->sc_mii;
   4955 
   4956 	if (sc->sc_flags & TULIPF_HAS_MII) {
   4957 		/*
   4958 		 * Make sure the built-in Tx jabber timer is disabled.
   4959 		 */
   4960 		TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);
   4961 
   4962 		return (tlp_mii_setmedia(sc));
   4963 	}
   4964 
   4965 	if (ifp->if_flags & IFF_UP) {
   4966 		mii->mii_media_status = 0;
   4967 		mii->mii_media_active = IFM_NONE;
   4968 		return (tlp_pnic_nway_service(sc, MII_MEDIACHG));
   4969 	}
   4970 
   4971 	return (0);
   4972 }
   4973 
   4974 void
   4975 tlp_pnic_nway_statchg(self)
   4976 	struct device *self;
   4977 {
   4978 	struct tulip_softc *sc = (struct tulip_softc *)self;
   4979 
   4980 	/* Idle the transmit and receive processes. */
   4981 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   4982 
   4983 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_PS|OPMODE_PCS|
   4984 	    OPMODE_SCR|OPMODE_HBD);
   4985 
   4986 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
   4987 		sc->sc_opmode |= OPMODE_TTM;
   4988 		TULIP_WRITE(sc, CSR_GPP,
   4989 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) |
   4990 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
   4991 	} else {
   4992 		sc->sc_opmode |= OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD;
   4993 		TULIP_WRITE(sc, CSR_GPP,
   4994 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) |
   4995 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
   4996 	}
   4997 
   4998 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   4999 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
   5000 
   5001 	/*
   5002 	 * Write new OPMODE bits.  This also restarts the transmit
   5003 	 * and receive processes.
   5004 	 */
   5005 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   5006 }
   5007 
   5008 void
   5009 tlp_pnic_nway_tick(arg)
   5010 	void *arg;
   5011 {
   5012 	struct tulip_softc *sc = arg;
   5013 	int s;
   5014 
   5015 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   5016 		return;
   5017 
   5018 	s = splnet();
   5019 	tlp_pnic_nway_service(sc, MII_TICK);
   5020 	splx(s);
   5021 
   5022 	callout_reset(&sc->sc_tick_callout, hz, tlp_pnic_nway_tick, sc);
   5023 }
   5024 
   5025 /*
   5026  * Support for the Lite-On PNIC internal NWay block.  This is constructed
   5027  * somewhat like a PHY driver for simplicity.
   5028  */
   5029 
   5030 int
   5031 tlp_pnic_nway_service(sc, cmd)
   5032 	struct tulip_softc *sc;
   5033 	int cmd;
   5034 {
   5035 	struct mii_data *mii = &sc->sc_mii;
   5036 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   5037 
   5038 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
   5039 		return (0);
   5040 
   5041 	switch (cmd) {
   5042 	case MII_POLLSTAT:
   5043 		/* Nothing special to do here. */
   5044 		break;
   5045 
   5046 	case MII_MEDIACHG:
   5047 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   5048 		case IFM_AUTO:
   5049 			(void) tlp_pnic_nway_auto(sc, 1);
   5050 			break;
   5051 		case IFM_100_T4:
   5052 			/*
   5053 			 * XXX Not supported as a manual setting right now.
   5054 			 */
   5055 			return (EINVAL);
   5056 		default:
   5057 			/*
   5058 			 * NWAY register data is stored in the ifmedia entry.
   5059 			 */
   5060 			TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   5061 		}
   5062 		break;
   5063 
   5064 	case MII_TICK:
   5065 		/*
   5066 		 * Only used for autonegotiation.
   5067 		 */
   5068 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   5069 			return (0);
   5070 
   5071 		/*
   5072 		 * Check to see if we have link.  If we do, we don't
   5073 		 * need to restart the autonegotiation process.
   5074 		 */
   5075 		if (sc->sc_flags & TULIPF_LINK_UP)
   5076 			return (0);
   5077 
   5078 		/*
   5079 		 * Only retry autonegotiation every 5 seconds.
   5080 		 */
   5081 		if (++sc->sc_nway_ticks != 5)
   5082 			return (0);
   5083 
   5084 		sc->sc_nway_ticks = 0;
   5085 		tlp_pnic_nway_reset(sc);
   5086 		if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
   5087 			return (0);
   5088 		break;
   5089 	}
   5090 
   5091 	/* Update the media status. */
   5092 	tlp_pnic_nway_status(sc);
   5093 
   5094 	/* Callback if something changed. */
   5095 	if ((sc->sc_nway_active == NULL ||
   5096 	     sc->sc_nway_active->ifm_media != mii->mii_media_active) ||
   5097 	    cmd == MII_MEDIACHG) {
   5098 		(*sc->sc_statchg)(&sc->sc_dev);
   5099 		tlp_nway_activate(sc, mii->mii_media_active);
   5100 	}
   5101 	return (0);
   5102 }
   5103 
   5104 void
   5105 tlp_pnic_nway_reset(sc)
   5106 	struct tulip_softc *sc;
   5107 {
   5108 
   5109 	TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
   5110 	delay(100);
   5111 	TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
   5112 }
   5113 
   5114 int
   5115 tlp_pnic_nway_auto(sc, waitfor)
   5116 	struct tulip_softc *sc;
   5117 	int waitfor;
   5118 {
   5119 	struct mii_data *mii = &sc->sc_mii;
   5120 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   5121 	u_int32_t reg;
   5122 	int i;
   5123 
   5124 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
   5125 		TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   5126 
   5127 	if (waitfor) {
   5128 		/* Wait 500ms for it to complete. */
   5129 		for (i = 0; i < 500; i++) {
   5130 			reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5131 			if (reg & PNIC_NWAY_LPAR_MASK) {
   5132 				tlp_pnic_nway_acomp(sc);
   5133 				return (0);
   5134 			}
   5135 			delay(1000);
   5136 		}
   5137 #if 0
   5138 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   5139 			printf("%s: autonegotiation failed to complete\n",
   5140 			    sc->sc_dev.dv_xname);
   5141 #endif
   5142 
   5143 		/*
   5144 		 * Don't need to worry about clearing DOINGAUTO.
   5145 		 * If that's set, a timeout is pending, and it will
   5146 		 * clear the flag.
   5147 		 */
   5148 		return (EIO);
   5149 	}
   5150 
   5151 	/*
   5152 	 * Just let it finish asynchronously.  This is for the benefit of
   5153 	 * the tick handler driving autonegotiation.  Don't want 500ms
   5154 	 * delays all the time while the system is running!
   5155 	 */
   5156 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
   5157 		sc->sc_flags |= TULIPF_DOINGAUTO;
   5158 		callout_reset(&sc->sc_nway_callout, hz >> 1,
   5159 		    tlp_pnic_nway_auto_timeout, sc);
   5160 	}
   5161 	return (EJUSTRETURN);
   5162 }
   5163 
   5164 void
   5165 tlp_pnic_nway_auto_timeout(arg)
   5166 	void *arg;
   5167 {
   5168 	struct tulip_softc *sc = arg;
   5169 	u_int32_t reg;
   5170 	int s;
   5171 
   5172 	s = splnet();
   5173 	sc->sc_flags &= ~TULIPF_DOINGAUTO;
   5174 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5175 #if 0
   5176 	if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   5177 		printf("%s: autonegotiation failed to complete\n",
   5178 		    sc->sc_dev.dv_xname);
   5179 #endif
   5180 
   5181 	tlp_pnic_nway_acomp(sc);
   5182 
   5183 	/* Update the media status. */
   5184 	(void) tlp_pnic_nway_service(sc, MII_POLLSTAT);
   5185 	splx(s);
   5186 }
   5187 
   5188 void
   5189 tlp_pnic_nway_status(sc)
   5190 	struct tulip_softc *sc;
   5191 {
   5192 	struct mii_data *mii = &sc->sc_mii;
   5193 	u_int32_t reg;
   5194 
   5195 	mii->mii_media_status = IFM_AVALID;
   5196 	mii->mii_media_active = IFM_ETHER;
   5197 
   5198 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5199 
   5200 	if (sc->sc_flags & TULIPF_LINK_UP)
   5201 		mii->mii_media_status |= IFM_ACTIVE;
   5202 
   5203 	if (reg & PNIC_NWAY_NW) {
   5204 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
   5205 			/* Erg, still trying, I guess... */
   5206 			mii->mii_media_active |= IFM_NONE;
   5207 			return;
   5208 		}
   5209 
   5210 #if 0
   5211 		if (reg & PNIC_NWAY_LPAR100T4)
   5212 			mii->mii_media_active |= IFM_100_T4;
   5213 		else
   5214 #endif
   5215 		if (reg & PNIC_NWAY_LPAR100TXFDX)
   5216 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
   5217 		else if (reg & PNIC_NWAY_LPAR100TX)
   5218 			mii->mii_media_active |= IFM_100_TX;
   5219 		else if (reg & PNIC_NWAY_LPAR10TFDX)
   5220 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
   5221 		else if (reg & PNIC_NWAY_LPAR10T)
   5222 			mii->mii_media_active |= IFM_10_T;
   5223 		else
   5224 			mii->mii_media_active |= IFM_NONE;
   5225 	} else {
   5226 		if (reg & PNIC_NWAY_100)
   5227 			mii->mii_media_active |= IFM_100_TX;
   5228 		else
   5229 			mii->mii_media_active |= IFM_10_T;
   5230 		if (reg & PNIC_NWAY_FD)
   5231 			mii->mii_media_active |= IFM_FDX;
   5232 	}
   5233 }
   5234 
   5235 void
   5236 tlp_pnic_nway_acomp(sc)
   5237 	struct tulip_softc *sc;
   5238 {
   5239 	u_int32_t reg;
   5240 
   5241 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5242 	reg &= ~(PNIC_NWAY_FD|PNIC_NWAY_100|PNIC_NWAY_RN);
   5243 
   5244 	if (reg & (PNIC_NWAY_LPAR100TXFDX|PNIC_NWAY_LPAR100TX))
   5245 		reg |= PNIC_NWAY_100;
   5246 	if (reg & (PNIC_NWAY_LPAR10TFDX|PNIC_NWAY_LPAR100TXFDX))
   5247 		reg |= PNIC_NWAY_FD;
   5248 
   5249 	TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
   5250 }
   5251 
   5252 /*
   5253  * Macronix PMAC and Lite-On PNIC-II media switch:
   5254  *
   5255  *	MX98713 and MX98713A		21140-like MII or GPIO media.
   5256  *
   5257  *	MX98713A			21143-like MII or SIA/SYM media.
   5258  *
   5259  *	MX98715, MX98715A, MX98725,	21143-like SIA/SYM media.
   5260  *	82C115
   5261  *
   5262  * So, what we do here is fake MII-on-SIO or ISV media info, and
   5263  * use the ISV media switch get/set functions to handle the rest.
   5264  */
   5265 
   5266 void	tlp_pmac_tmsw_init __P((struct tulip_softc *));
   5267 
   5268 const struct tulip_mediasw tlp_pmac_mediasw = {
   5269 	tlp_pmac_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
   5270 };
   5271 
   5272 const struct tulip_mediasw tlp_pmac_mii_mediasw = {
   5273 	tlp_pmac_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   5274 };
   5275 
   5276 void
   5277 tlp_pmac_tmsw_init(sc)
   5278 	struct tulip_softc *sc;
   5279 {
   5280 	static const u_int8_t media[] = {
   5281 		TULIP_ROM_MB_MEDIA_TP,
   5282 		TULIP_ROM_MB_MEDIA_TP_FDX,
   5283 		TULIP_ROM_MB_MEDIA_100TX,
   5284 		TULIP_ROM_MB_MEDIA_100TX_FDX,
   5285 	};
   5286 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   5287 
   5288 	sc->sc_mii.mii_ifp = ifp;
   5289 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
   5290 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
   5291 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   5292 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   5293 	    tlp_mediastatus);
   5294 	if (sc->sc_chip == TULIP_CHIP_MX98713 ||
   5295 	    sc->sc_chip == TULIP_CHIP_MX98713A) {
   5296 		mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
   5297 		    MII_PHY_ANY, MII_OFFSET_ANY, 0);
   5298 		if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL) {
   5299 			sc->sc_flags |= TULIPF_HAS_MII;
   5300 			sc->sc_tick = tlp_mii_tick;
   5301 			sc->sc_preinit = tlp_2114x_mii_preinit;
   5302 			sc->sc_mediasw = &tlp_pmac_mii_mediasw;
   5303 			ifmedia_set(&sc->sc_mii.mii_media,
   5304 			    IFM_ETHER|IFM_AUTO);
   5305 			return;
   5306 		}
   5307 	}
   5308 
   5309 	switch (sc->sc_chip) {
   5310 	case TULIP_CHIP_MX98713:
   5311 		tlp_add_srom_media(sc, TULIP_ROM_MB_21140_GPR,
   5312 		    tlp_21140_gpio_get, tlp_21140_gpio_set, media, 4);
   5313 
   5314 		/*
   5315 		 * XXX Should implement auto-sense for this someday,
   5316 		 * XXX when we do the same for the 21140.
   5317 		 */
   5318 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   5319 		break;
   5320 
   5321 	default:
   5322 		tlp_add_srom_media(sc, TULIP_ROM_MB_21142_SIA,
   5323 		    tlp_sia_get, tlp_sia_set, media, 2);
   5324 		tlp_add_srom_media(sc, TULIP_ROM_MB_21143_SYM,
   5325 		    tlp_sia_get, tlp_sia_set, media + 2, 2);
   5326 
   5327 		/*
   5328 		 * XXX Autonegotiation not yet supported.
   5329 		 */
   5330 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   5331 		break;
   5332 	}
   5333 
   5334 	tlp_print_media(sc);
   5335 	tlp_sia_fixup(sc);
   5336 
   5337 	/* Set the LED modes. */
   5338 	tlp_pmac_reset(sc);
   5339 
   5340 	sc->sc_reset = tlp_pmac_reset;
   5341 }
   5342 
   5343 /*
   5344  * ADMtek AL981 media switch.  Only has internal PHY.
   5345  */
   5346 void	tlp_al981_tmsw_init __P((struct tulip_softc *));
   5347 
   5348 const struct tulip_mediasw tlp_al981_mediasw = {
   5349 	tlp_al981_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   5350 };
   5351 
   5352 void
   5353 tlp_al981_tmsw_init(sc)
   5354 	struct tulip_softc *sc;
   5355 {
   5356 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   5357 
   5358 	sc->sc_mii.mii_ifp = ifp;
   5359 	sc->sc_mii.mii_readreg = tlp_al981_mii_readreg;
   5360 	sc->sc_mii.mii_writereg = tlp_al981_mii_writereg;
   5361 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   5362 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   5363 	    tlp_mediastatus);
   5364 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   5365 	    MII_OFFSET_ANY, 0);
   5366 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   5367 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   5368 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   5369 	} else {
   5370 		sc->sc_flags |= TULIPF_HAS_MII;
   5371 		sc->sc_tick = tlp_mii_tick;
   5372 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   5373 	}
   5374 }
   5375