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