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