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