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