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