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