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