Home | History | Annotate | Line # | Download | only in ic
tulip.c revision 1.59
      1 /*	$NetBSD: tulip.c,v 1.59 2000/04/02 23:38:05 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 < 4 || x > 12) {
   2106 		printf("%s: broken MicroWire interface detected; setting SROM size to 1Kb\n",
   2107 		    sc->sc_dev.dv_xname);
   2108 		return (6);
   2109 	} else {
   2110 #if 0
   2111 		printf("%s: SROM size is 2^%d*16 bits (%d bytes)\n",
   2112 			sc->sc_dev.dv_xname, x, (1 << (x + 4)) >> 3);
   2113 #endif
   2114 		return (x);
   2115 	}
   2116 }
   2117 
   2118 /*
   2119  * tlp_read_srom:
   2120  *
   2121  *	Read the Tulip SROM.
   2122  */
   2123 int
   2124 tlp_read_srom(sc)
   2125 	struct tulip_softc *sc;
   2126 {
   2127 	int size;
   2128 	u_int32_t miirom;
   2129 	u_int16_t datain;
   2130 	int i, x;
   2131 
   2132 	tlp_srom_idle(sc);
   2133 
   2134 	sc->sc_srom_addrbits = tlp_srom_size(sc);
   2135 	if (sc->sc_srom_addrbits == 0)
   2136 		return (0);
   2137 	size = TULIP_ROM_SIZE(sc->sc_srom_addrbits);
   2138 	sc->sc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
   2139 
   2140 	/* Select the SROM. */
   2141 	miirom = MIIROM_SR;
   2142 	SROM_EMIT(sc, miirom);
   2143 
   2144 	miirom |= MIIROM_RD;
   2145 	SROM_EMIT(sc, miirom);
   2146 
   2147 	for (i = 0; i < size; i += 2) {
   2148 		/* Send CHIP SELECT for one clock tick. */
   2149 		miirom |= MIIROM_SROMCS;
   2150 		SROM_EMIT(sc, miirom);
   2151 
   2152 		/* Shift in the READ opcode. */
   2153 		for (x = 3; x > 0; x--) {
   2154 			if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
   2155 				miirom |= MIIROM_SROMDI;
   2156 			else
   2157 				miirom &= ~MIIROM_SROMDI;
   2158 			SROM_EMIT(sc, miirom);
   2159 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2160 			SROM_EMIT(sc, miirom);
   2161 		}
   2162 
   2163 		/* Shift in address. */
   2164 		for (x = sc->sc_srom_addrbits; x > 0; x--) {
   2165 			if (i & (1 << x))
   2166 				miirom |= MIIROM_SROMDI;
   2167 			else
   2168 				miirom &= ~MIIROM_SROMDI;
   2169 			SROM_EMIT(sc, miirom);
   2170 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2171 			SROM_EMIT(sc, miirom);
   2172 		}
   2173 
   2174 		/* Shift out data. */
   2175 		miirom &= ~MIIROM_SROMDI;
   2176 		datain = 0;
   2177 		for (x = 16; x > 0; x--) {
   2178 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   2179 			if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
   2180 				datain |= (1 << (x - 1));
   2181 			SROM_EMIT(sc, miirom);
   2182 		}
   2183 		sc->sc_srom[i] = datain & 0xff;
   2184 		sc->sc_srom[i + 1] = datain >> 8;
   2185 
   2186 		/* Clear CHIP SELECT. */
   2187 		miirom &= ~MIIROM_SROMCS;
   2188 		SROM_EMIT(sc, miirom);
   2189 	}
   2190 
   2191 	/* Deselect the SROM. */
   2192 	SROM_EMIT(sc, 0);
   2193 
   2194 	/* ...and idle it. */
   2195 	tlp_srom_idle(sc);
   2196 
   2197 #if 0
   2198 	printf("SROM CONTENTS:");
   2199 	for (i = 0; i < size; i++) {
   2200 		if ((i % 8) == 0)
   2201 			printf("\n\t");
   2202 		printf("0x%02x ", sc->sc_srom[i]);
   2203 	}
   2204 	printf("\n");
   2205 #endif
   2206 
   2207 	return (1);
   2208 }
   2209 
   2210 #undef SROM_EMIT
   2211 
   2212 /*
   2213  * tlp_add_rxbuf:
   2214  *
   2215  *	Add a receive buffer to the indicated descriptor.
   2216  */
   2217 int
   2218 tlp_add_rxbuf(sc, idx)
   2219 	struct tulip_softc *sc;
   2220 	int idx;
   2221 {
   2222 	struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx];
   2223 	struct mbuf *m;
   2224 	int error;
   2225 
   2226 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2227 	if (m == NULL)
   2228 		return (ENOBUFS);
   2229 
   2230 	MCLGET(m, M_DONTWAIT);
   2231 	if ((m->m_flags & M_EXT) == 0) {
   2232 		m_freem(m);
   2233 		return (ENOBUFS);
   2234 	}
   2235 
   2236 	if (rxs->rxs_mbuf != NULL)
   2237 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   2238 
   2239 	rxs->rxs_mbuf = m;
   2240 
   2241 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
   2242 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
   2243 	if (error) {
   2244 		printf("%s: can't load rx DMA map %d, error = %d\n",
   2245 		    sc->sc_dev.dv_xname, idx, error);
   2246 		panic("tlp_add_rxbuf");	/* XXX */
   2247 	}
   2248 
   2249 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   2250 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   2251 
   2252 	TULIP_INIT_RXDESC(sc, idx);
   2253 
   2254 	return (0);
   2255 }
   2256 
   2257 /*
   2258  * tlp_crc32:
   2259  *
   2260  *	Compute the 32-bit CRC of the provided buffer.
   2261  */
   2262 u_int32_t
   2263 tlp_crc32(buf, len)
   2264 	const u_int8_t *buf;
   2265 	size_t len;
   2266 {
   2267 	static const u_int32_t crctab[] = {
   2268 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   2269 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   2270 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   2271 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   2272 	};
   2273 	u_int32_t crc;
   2274 	int i;
   2275 
   2276 	crc = 0xffffffff;
   2277 	for (i = 0; i < len; i++) {
   2278 		crc ^= buf[i];
   2279 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   2280 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   2281 	}
   2282 	return (crc);
   2283 }
   2284 
   2285 /*
   2286  * tlp_srom_crcok:
   2287  *
   2288  *	Check the CRC of the Tulip SROM.
   2289  */
   2290 int
   2291 tlp_srom_crcok(romdata)
   2292 	const u_int8_t *romdata;
   2293 {
   2294 	u_int32_t crc;
   2295 
   2296 	crc = tlp_crc32(romdata, TULIP_ROM_CRC32_CHECKSUM);
   2297 	crc = (crc & 0xffff) ^ 0xffff;
   2298 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM))
   2299 		return (1);
   2300 
   2301 	/*
   2302 	 * Try an alternate checksum.
   2303 	 */
   2304 	crc = tlp_crc32(romdata, TULIP_ROM_CRC32_CHECKSUM1);
   2305 	crc = (crc & 0xffff) ^ 0xffff;
   2306 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM1))
   2307 		return (1);
   2308 
   2309 	return (0);
   2310 }
   2311 
   2312 /*
   2313  * tlp_isv_srom:
   2314  *
   2315  *	Check to see if the SROM is in the new standardized format.
   2316  */
   2317 int
   2318 tlp_isv_srom(romdata)
   2319 	const u_int8_t *romdata;
   2320 {
   2321 	int i;
   2322 	u_int16_t cksum;
   2323 
   2324 	if (tlp_srom_crcok(romdata)) {
   2325 		/*
   2326 		 * SROM CRC checks out; must be in the new format.
   2327 		 */
   2328 		return (1);
   2329 	}
   2330 
   2331 	cksum = TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM);
   2332 	if (cksum == 0xffff || cksum == 0) {
   2333 		/*
   2334 		 * No checksum present.  Check the SROM ID; 18 bytes of 0
   2335 		 * followed by 1 (version) followed by the number of
   2336 		 * adapters which use this SROM (should be non-zero).
   2337 		 */
   2338 		for (i = 0; i < TULIP_ROM_SROM_FORMAT_VERION; i++) {
   2339 			if (romdata[i] != 0)
   2340 				return (0);
   2341 		}
   2342 		if (romdata[TULIP_ROM_SROM_FORMAT_VERION] != 1)
   2343 			return (0);
   2344 		if (romdata[TULIP_ROM_CHIP_COUNT] == 0)
   2345 			return (0);
   2346 		return (1);
   2347 	}
   2348 
   2349 	return (0);
   2350 }
   2351 
   2352 /*
   2353  * tlp_isv_srom_enaddr:
   2354  *
   2355  *	Get the Ethernet address from an ISV SROM.
   2356  */
   2357 int
   2358 tlp_isv_srom_enaddr(sc, enaddr)
   2359 	struct tulip_softc *sc;
   2360 	u_int8_t *enaddr;
   2361 {
   2362 	int i, devcnt;
   2363 
   2364 	if (tlp_isv_srom(sc->sc_srom) == 0)
   2365 		return (0);
   2366 
   2367 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   2368 	for (i = 0; i < devcnt; i++) {
   2369 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   2370 			break;
   2371 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   2372 		    sc->sc_devno)
   2373 			break;
   2374 	}
   2375 
   2376 	if (i == devcnt)
   2377 		return (0);
   2378 
   2379 	memcpy(enaddr, &sc->sc_srom[TULIP_ROM_IEEE_NETWORK_ADDRESS],
   2380 	    ETHER_ADDR_LEN);
   2381 	enaddr[5] += i;
   2382 
   2383 	return (1);
   2384 }
   2385 
   2386 /*
   2387  * tlp_parse_old_srom:
   2388  *
   2389  *	Parse old-format SROMs.
   2390  *
   2391  *	This routine is largely lifted from Matt Thomas's `de' driver.
   2392  */
   2393 int
   2394 tlp_parse_old_srom(sc, enaddr)
   2395 	struct tulip_softc *sc;
   2396 	u_int8_t *enaddr;
   2397 {
   2398 	static const u_int8_t testpat[] =
   2399 	    { 0xff, 0, 0x55, 0xaa, 0xff, 0, 0x55, 0xaa };
   2400 	int i;
   2401 	u_int32_t cksum;
   2402 
   2403 	if (memcmp(&sc->sc_srom[0], &sc->sc_srom[16], 8) != 0) {
   2404 		/*
   2405 		 * Some vendors (e.g. ZNYX) don't use the standard
   2406 		 * DEC Address ROM format, but rather just have an
   2407 		 * Ethernet address in the first 6 bytes, maybe a
   2408 		 * 2 byte checksum, and then all 0xff's.
   2409 		 *
   2410 		 * On the other hand, Cobalt Networks interfaces
   2411 		 * simply have the address in the first six bytes
   2412 		 * with the rest zeroed out.
   2413 		 */
   2414 		for (i = 8; i < 32; i++) {
   2415 			if (sc->sc_srom[i] != 0xff &&
   2416 			    sc->sc_srom[i] != 0)
   2417 				return (0);
   2418 		}
   2419 
   2420 		/*
   2421 		 * Sanity check the Ethernet address:
   2422 		 *
   2423 		 *	- Make sure it's not multicast or locally
   2424 		 *	  assigned
   2425 		 *	- Make sure it has a non-0 OUI
   2426 		 */
   2427 		if (sc->sc_srom[0] & 3)
   2428 			return (0);
   2429 		if (sc->sc_srom[0] == 0 && sc->sc_srom[1] == 0 &&
   2430 		    sc->sc_srom[2] == 0)
   2431 			return (0);
   2432 
   2433 		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   2434 		return (1);
   2435 	}
   2436 
   2437 	/*
   2438 	 * Standard DEC Address ROM test.
   2439 	 */
   2440 
   2441 	if (memcmp(&sc->sc_srom[24], testpat, 8) != 0)
   2442 		return (0);
   2443 
   2444 	for (i = 0; i < 8; i++) {
   2445 		if (sc->sc_srom[i] != sc->sc_srom[15 - i])
   2446 			return (0);
   2447 	}
   2448 
   2449 	memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   2450 
   2451 	cksum = *(u_int16_t *) &enaddr[0];
   2452 
   2453 	cksum <<= 1;
   2454 	if (cksum > 0xffff)
   2455 		cksum -= 0xffff;
   2456 
   2457 	cksum += *(u_int16_t *) &enaddr[2];
   2458 	if (cksum > 0xffff)
   2459 		cksum -= 0xffff;
   2460 
   2461 	cksum <<= 1;
   2462 	if (cksum > 0xffff)
   2463 		cksum -= 0xffff;
   2464 
   2465 	cksum += *(u_int16_t *) &enaddr[4];
   2466 	if (cksum >= 0xffff)
   2467 		cksum -= 0xffff;
   2468 
   2469 	if (cksum != *(u_int16_t *) &sc->sc_srom[6])
   2470 		return (0);
   2471 
   2472 	return (1);
   2473 }
   2474 
   2475 /*
   2476  * tlp_filter_setup:
   2477  *
   2478  *	Set the Tulip's receive filter.
   2479  */
   2480 void
   2481 tlp_filter_setup(sc)
   2482 	struct tulip_softc *sc;
   2483 {
   2484 	struct ethercom *ec = &sc->sc_ethercom;
   2485 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2486 	struct ether_multi *enm;
   2487 	struct ether_multistep step;
   2488 	__volatile u_int32_t *sp;
   2489 	struct tulip_txsoft *txs;
   2490 	u_int8_t enaddr[ETHER_ADDR_LEN];
   2491 	u_int32_t hash, hashsize;
   2492 	int cnt;
   2493 
   2494 	DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n",
   2495 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2496 
   2497 	memcpy(enaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   2498 
   2499 	/*
   2500 	 * If there are transmissions pending, wait until they have
   2501 	 * completed.
   2502 	 */
   2503 	if (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL ||
   2504 	    (sc->sc_flags & TULIPF_DOING_SETUP) != 0) {
   2505 		sc->sc_flags |= TULIPF_WANT_SETUP;
   2506 		DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n",
   2507 		    sc->sc_dev.dv_xname));
   2508 		return;
   2509 	}
   2510 	sc->sc_flags &= ~TULIPF_WANT_SETUP;
   2511 
   2512 	switch (sc->sc_chip) {
   2513 	case TULIP_CHIP_82C115:
   2514 		hashsize = TULIP_PNICII_HASHSIZE;
   2515 		break;
   2516 
   2517 	default:
   2518 		hashsize = TULIP_MCHASHSIZE;
   2519 	}
   2520 
   2521 	/*
   2522 	 * If we're running, idle the transmit and receive engines.  If
   2523 	 * we're NOT running, we're being called from tlp_init(), and our
   2524 	 * writing OPMODE will start the transmit and receive processes
   2525 	 * in motion.
   2526 	 */
   2527 	if (ifp->if_flags & IFF_RUNNING) {
   2528 		/*
   2529 		 * Actually, some chips seem to need a really hard
   2530 		 * kick in the head for this to work.  The genuine
   2531 		 * DEC chips can just be idled, but some of the
   2532 		 * clones seem to REALLY want a reset here.  Doing
   2533 		 * the reset will end up here again, but with
   2534 		 * IFF_RUNNING cleared.
   2535 		 */
   2536 		switch (sc->sc_chip) {
   2537 		case TULIP_CHIP_82C168:
   2538 		case TULIP_CHIP_82C169:
   2539 			tlp_init(sc);
   2540 			return;
   2541 
   2542 		default:
   2543 			tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2544 		}
   2545 	}
   2546 
   2547 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
   2548 
   2549 	if (ifp->if_flags & IFF_PROMISC) {
   2550 		sc->sc_opmode |= OPMODE_PR;
   2551 		goto allmulti;
   2552 	}
   2553 
   2554 	/*
   2555 	 * Try Perfect filtering first.
   2556 	 */
   2557 
   2558 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   2559 	sp = TULIP_CDSP(sc);
   2560 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2561 	cnt = 0;
   2562 	ETHER_FIRST_MULTI(step, ec, enm);
   2563 	while (enm != NULL) {
   2564 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2565 			/*
   2566 			 * We must listen to a range of multicast addresses.
   2567 			 * For now, just accept all multicasts, rather than
   2568 			 * trying to set only those filter bits needed to match
   2569 			 * the range.  (At this time, the only use of address
   2570 			 * ranges is for IP multicast routing, for which the
   2571 			 * range is big enough to require all bits set.)
   2572 			 */
   2573 			goto allmulti;
   2574 		}
   2575 		if (cnt == (TULIP_MAXADDRS - 2)) {
   2576 			/*
   2577 			 * We already have our multicast limit (still need
   2578 			 * our station address and broadcast).  Go to
   2579 			 * Hash-Perfect mode.
   2580 			 */
   2581 			goto hashperfect;
   2582 		}
   2583 		cnt++;
   2584 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 0);
   2585 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 1);
   2586 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 2);
   2587 		ETHER_NEXT_MULTI(step, enm);
   2588 	}
   2589 
   2590 	if (ifp->if_flags & IFF_BROADCAST) {
   2591 		/* ...and the broadcast address. */
   2592 		cnt++;
   2593 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2594 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2595 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2596 	}
   2597 
   2598 	/* Pad the rest with our station address. */
   2599 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   2600 		*sp++ = TULIP_SP_FIELD(enaddr, 0);
   2601 		*sp++ = TULIP_SP_FIELD(enaddr, 1);
   2602 		*sp++ = TULIP_SP_FIELD(enaddr, 2);
   2603 	}
   2604 	ifp->if_flags &= ~IFF_ALLMULTI;
   2605 	goto setit;
   2606 
   2607  hashperfect:
   2608 	/*
   2609 	 * Try Hash-Perfect mode.
   2610 	 */
   2611 
   2612 	/*
   2613 	 * Some 21140 chips have broken Hash-Perfect modes.  On these
   2614 	 * chips, we simply use Hash-Only mode, and put our station
   2615 	 * address into the filter.
   2616 	 */
   2617 	if (sc->sc_chip == TULIP_CHIP_21140)
   2618 		sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY;
   2619 	else
   2620 		sc->sc_filtmode = TDCTL_Tx_FT_HASH;
   2621 	sp = TULIP_CDSP(sc);
   2622 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2623 	ETHER_FIRST_MULTI(step, ec, enm);
   2624 	while (enm != NULL) {
   2625 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2626 			/*
   2627 			 * We must listen to a range of multicast addresses.
   2628 			 * For now, just accept all multicasts, rather than
   2629 			 * trying to set only those filter bits needed to match
   2630 			 * the range.  (At this time, the only use of address
   2631 			 * ranges is for IP multicast routing, for which the
   2632 			 * range is big enough to require all bits set.)
   2633 			 */
   2634 			goto allmulti;
   2635 		}
   2636 		hash = tlp_mchash(enm->enm_addrlo, hashsize);
   2637 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2638 		ETHER_NEXT_MULTI(step, enm);
   2639 	}
   2640 
   2641 	if (ifp->if_flags & IFF_BROADCAST) {
   2642 		/* ...and the broadcast address. */
   2643 		hash = tlp_mchash(etherbroadcastaddr, hashsize);
   2644 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2645 	}
   2646 
   2647 	if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
   2648 		/* ...and our station address. */
   2649 		hash = tlp_mchash(enaddr, hashsize);
   2650 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2651 	} else {
   2652 		/*
   2653 		 * Hash-Perfect mode; put our station address after
   2654 		 * the hash table.
   2655 		 */
   2656 		sp[39] = TULIP_SP_FIELD(enaddr, 0);
   2657 		sp[40] = TULIP_SP_FIELD(enaddr, 1);
   2658 		sp[41] = TULIP_SP_FIELD(enaddr, 2);
   2659 	}
   2660 	ifp->if_flags &= ~IFF_ALLMULTI;
   2661 	goto setit;
   2662 
   2663  allmulti:
   2664 	/*
   2665 	 * Use Perfect filter mode.  First address is the broadcast address,
   2666 	 * and pad the rest with our station address.  We'll set Pass-all-
   2667 	 * multicast in OPMODE below.
   2668 	 */
   2669 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   2670 	sp = TULIP_CDSP(sc);
   2671 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2672 	cnt = 0;
   2673 	if (ifp->if_flags & IFF_BROADCAST) {
   2674 		cnt++;
   2675 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2676 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2677 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2678 	}
   2679 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   2680 		*sp++ = TULIP_SP_FIELD(enaddr, 0);
   2681 		*sp++ = TULIP_SP_FIELD(enaddr, 1);
   2682 		*sp++ = TULIP_SP_FIELD(enaddr, 2);
   2683 	}
   2684 	ifp->if_flags |= IFF_ALLMULTI;
   2685 
   2686  setit:
   2687 	if (ifp->if_flags & IFF_ALLMULTI)
   2688 		sc->sc_opmode |= OPMODE_PM;
   2689 
   2690 	/* Sync the setup packet buffer. */
   2691 	TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE);
   2692 
   2693 	/*
   2694 	 * Fill in the setup packet descriptor.
   2695 	 */
   2696 	txs = SIMPLEQ_FIRST(&sc->sc_txfreeq);
   2697 
   2698 	txs->txs_firstdesc = sc->sc_txnext;
   2699 	txs->txs_lastdesc = sc->sc_txnext;
   2700 	txs->txs_ndescs = 1;
   2701 	txs->txs_mbuf = NULL;
   2702 
   2703 	sc->sc_txdescs[sc->sc_txnext].td_bufaddr1 =
   2704 	    htole32(TULIP_CDSPADDR(sc));
   2705 	sc->sc_txdescs[sc->sc_txnext].td_ctl =
   2706 	    htole32((TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) |
   2707 	    sc->sc_filtmode | TDCTL_Tx_SET | TDCTL_Tx_FS |
   2708 	    TDCTL_Tx_LS | TDCTL_Tx_IC | sc->sc_tdctl_ch |
   2709 	    (sc->sc_txnext == (TULIP_NTXDESC - 1) ? sc->sc_tdctl_er : 0));
   2710 	sc->sc_txdescs[sc->sc_txnext].td_status = htole32(TDSTAT_OWN);
   2711 	TULIP_CDTXSYNC(sc, sc->sc_txnext, txs->txs_ndescs,
   2712 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   2713 
   2714 	/* Advance the tx pointer. */
   2715 	sc->sc_txfree -= 1;
   2716 	sc->sc_txnext = TULIP_NEXTTX(sc->sc_txnext);
   2717 
   2718 	SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
   2719 	SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
   2720 
   2721 	/*
   2722 	 * Set the OPMODE register.  This will also resume the
   2723 	 * transmit transmit process we idled above.
   2724 	 */
   2725 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2726 
   2727 	sc->sc_flags |= TULIPF_DOING_SETUP;
   2728 
   2729 	/*
   2730 	 * Kick the transmitter; this will cause the Tulip to
   2731 	 * read the setup descriptor.
   2732 	 */
   2733 	/* XXX USE AUTOPOLLING? */
   2734 	TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
   2735 
   2736 	/* Set up a watchdog timer in case the chip flakes out. */
   2737 	ifp->if_timer = 5;
   2738 
   2739 	DPRINTF(sc, ("%s: tlp_filter_setup: returning\n", sc->sc_dev.dv_xname));
   2740 }
   2741 
   2742 /*
   2743  * tlp_winb_filter_setup:
   2744  *
   2745  *	Set the Winbond 89C840F's receive filter.
   2746  */
   2747 void
   2748 tlp_winb_filter_setup(sc)
   2749 	struct tulip_softc *sc;
   2750 {
   2751 	struct ethercom *ec = &sc->sc_ethercom;
   2752 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2753 	struct ether_multi *enm;
   2754 	struct ether_multistep step;
   2755 	u_int32_t hash, mchash[2];
   2756 
   2757 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n",
   2758 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2759 
   2760 	sc->sc_opmode &= ~(OPMODE_WINB_APP|OPMODE_WINB_AMP|OPMODE_WINB_ABP);
   2761 
   2762 	if (ifp->if_flags & IFF_MULTICAST)
   2763 		sc->sc_opmode |= OPMODE_WINB_AMP;
   2764 
   2765 	if (ifp->if_flags & IFF_BROADCAST)
   2766 		sc->sc_opmode |= OPMODE_WINB_ABP;
   2767 
   2768 	if (ifp->if_flags & IFF_PROMISC) {
   2769 		sc->sc_opmode |= OPMODE_WINB_APP;
   2770 		goto allmulti;
   2771 	}
   2772 
   2773 	mchash[0] = mchash[1] = 0;
   2774 
   2775 	ETHER_FIRST_MULTI(step, ec, enm);
   2776 	while (enm != NULL) {
   2777 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2778 			/*
   2779 			 * We must listen to a range of multicast addresses.
   2780 			 * For now, just accept all multicasts, rather than
   2781 			 * trying to set only those filter bits needed to match
   2782 			 * the range.  (At this time, the only use of address
   2783 			 * ranges is for IP multicast routing, for which the
   2784 			 * range is big enough to require all bits set.)
   2785 			 */
   2786 			goto allmulti;
   2787 		}
   2788 
   2789 		/*
   2790 		 * According to the FreeBSD `wb' driver, yes, you
   2791 		 * really do invert the hash.
   2792 		 */
   2793 		hash = (~(tlp_crc32(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26))
   2794 		    & 0x3f;
   2795 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
   2796 		ETHER_NEXT_MULTI(step, enm);
   2797 	}
   2798 	ifp->if_flags &= ~IFF_ALLMULTI;
   2799 	goto setit;
   2800 
   2801  allmulti:
   2802 	ifp->if_flags |= IFF_ALLMULTI;
   2803 	mchash[0] = mchash[1] = 0xffffffff;
   2804 
   2805  setit:
   2806 	TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]);
   2807 	TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]);
   2808 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2809 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n",
   2810 	    sc->sc_dev.dv_xname));
   2811 }
   2812 
   2813 /*
   2814  * tlp_al981_filter_setup:
   2815  *
   2816  *	Set the ADMtek AL981's receive filter.
   2817  */
   2818 void
   2819 tlp_al981_filter_setup(sc)
   2820 	struct tulip_softc *sc;
   2821 {
   2822 	struct ethercom *ec = &sc->sc_ethercom;
   2823 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2824 	struct ether_multi *enm;
   2825 	struct ether_multistep step;
   2826 	u_int32_t hash, mchash[2];
   2827 
   2828 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: sc_flags 0x%08x\n",
   2829 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2830 
   2831 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2832 
   2833 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
   2834 
   2835 	if (ifp->if_flags & IFF_PROMISC) {
   2836 		sc->sc_opmode |= OPMODE_PR;
   2837 		goto allmulti;
   2838 	}
   2839 
   2840 	mchash[0] = mchash[1] = 0;
   2841 
   2842 	ETHER_FIRST_MULTI(step, ec, enm);
   2843 	while (enm != NULL) {
   2844 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2845 			/*
   2846 			 * We must listen to a range of multicast addresses.
   2847 			 * For now, just accept all multicasts, rather than
   2848 			 * trying to set only those filter bits needed to match
   2849 			 * the range.  (At this time, the only use of address
   2850 			 * ranges is for IP multicast routing, for which the
   2851 			 * range is big enough to require all bits set.)
   2852 			 */
   2853 			goto allmulti;
   2854 		}
   2855 
   2856 		hash = (tlp_crc32(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26)
   2857 		    & 0x3f;
   2858 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
   2859 		ETHER_NEXT_MULTI(step, enm);
   2860 	}
   2861 	ifp->if_flags &= ~IFF_ALLMULTI;
   2862 	goto setit;
   2863 
   2864  allmulti:
   2865 	ifp->if_flags |= IFF_ALLMULTI;
   2866 	mchash[0] = mchash[1] = 0xffffffff;
   2867 
   2868  setit:
   2869 	TULIP_WRITE(sc, CSR_ADM_MAR0, mchash[0]);
   2870 	TULIP_WRITE(sc, CSR_ADM_MAR1, mchash[1]);
   2871 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2872 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: returning\n",
   2873 	    sc->sc_dev.dv_xname));
   2874 }
   2875 
   2876 /*
   2877  * tlp_idle:
   2878  *
   2879  *	Cause the transmit and/or receive processes to go idle.
   2880  */
   2881 void
   2882 tlp_idle(sc, bits)
   2883 	struct tulip_softc *sc;
   2884 	u_int32_t bits;
   2885 {
   2886 	static const char *tx_state_names[] = {
   2887 		"STOPPED",
   2888 		"RUNNING - FETCH",
   2889 		"RUNNING - WAIT",
   2890 		"RUNNING - READING",
   2891 		"-- RESERVED --",
   2892 		"RUNNING - SETUP",
   2893 		"SUSPENDED",
   2894 		"RUNNING - CLOSE",
   2895 	};
   2896 	static const char *rx_state_names[] = {
   2897 		"STOPPED",
   2898 		"RUNNING - FETCH",
   2899 		"RUNNING - CHECK",
   2900 		"RUNNING - WAIT",
   2901 		"SUSPENDED",
   2902 		"RUNNING - CLOSE",
   2903 		"RUNNING - FLUSH",
   2904 		"RUNNING - QUEUE",
   2905 	};
   2906 	u_int32_t csr, ackmask = 0;
   2907 	int i;
   2908 
   2909 	if (bits & OPMODE_ST)
   2910 		ackmask |= STATUS_TPS;
   2911 
   2912 	if (bits & OPMODE_SR)
   2913 		ackmask |= STATUS_RPS;
   2914 
   2915 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits);
   2916 
   2917 	for (i = 0; i < 1000; i++) {
   2918 		if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
   2919 			break;
   2920 		delay(10);
   2921 	}
   2922 
   2923 	csr = TULIP_READ(sc, CSR_STATUS);
   2924 	if ((csr & ackmask) != ackmask) {
   2925 		if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
   2926 		    (csr & STATUS_TS) != STATUS_TS_STOPPED)
   2927 			printf("%s: transmit process failed to idle: "
   2928 			    "state %s\n", sc->sc_dev.dv_xname,
   2929 			    tx_state_names[(csr & STATUS_TS) >> 20]);
   2930 		if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
   2931 		    (csr & STATUS_RS) != STATUS_RS_STOPPED)
   2932 			printf("%s: receive process failed to idle: "
   2933 			    "state %s\n", sc->sc_dev.dv_xname,
   2934 			    rx_state_names[(csr & STATUS_RS) >> 17]);
   2935 	}
   2936 	TULIP_WRITE(sc, CSR_STATUS, ackmask);
   2937 }
   2938 
   2939 /*****************************************************************************
   2940  * Generic media support functions.
   2941  *****************************************************************************/
   2942 
   2943 /*
   2944  * tlp_mediastatus:	[ifmedia interface function]
   2945  *
   2946  *	Query the current media.
   2947  */
   2948 void
   2949 tlp_mediastatus(ifp, ifmr)
   2950 	struct ifnet *ifp;
   2951 	struct ifmediareq *ifmr;
   2952 {
   2953 	struct tulip_softc *sc = ifp->if_softc;
   2954 
   2955 	if (TULIP_IS_ENABLED(sc) == 0) {
   2956 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
   2957 		ifmr->ifm_status = 0;
   2958 		return;
   2959 	}
   2960 
   2961 	(*sc->sc_mediasw->tmsw_get)(sc, ifmr);
   2962 }
   2963 
   2964 /*
   2965  * tlp_mediachange:	[ifmedia interface function]
   2966  *
   2967  *	Update the current media.
   2968  */
   2969 int
   2970 tlp_mediachange(ifp)
   2971 	struct ifnet *ifp;
   2972 {
   2973 	struct tulip_softc *sc = ifp->if_softc;
   2974 
   2975 	return ((*sc->sc_mediasw->tmsw_set)(sc));
   2976 }
   2977 
   2978 /*****************************************************************************
   2979  * Support functions for MII-attached media.
   2980  *****************************************************************************/
   2981 
   2982 /*
   2983  * tlp_mii_tick:
   2984  *
   2985  *	One second timer, used to tick the MII.
   2986  */
   2987 void
   2988 tlp_mii_tick(arg)
   2989 	void *arg;
   2990 {
   2991 	struct tulip_softc *sc = arg;
   2992 	int s;
   2993 
   2994 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   2995 		return;
   2996 
   2997 	s = splnet();
   2998 	mii_tick(&sc->sc_mii);
   2999 	splx(s);
   3000 
   3001 	callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc);
   3002 }
   3003 
   3004 /*
   3005  * tlp_mii_statchg:	[mii interface function]
   3006  *
   3007  *	Callback from PHY when media changes.
   3008  */
   3009 void
   3010 tlp_mii_statchg(self)
   3011 	struct device *self;
   3012 {
   3013 	struct tulip_softc *sc = (struct tulip_softc *)self;
   3014 
   3015 	/* Idle the transmit and receive processes. */
   3016 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   3017 
   3018 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_HBD);
   3019 
   3020 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
   3021 		sc->sc_opmode |= OPMODE_TTM;
   3022 	else
   3023 		sc->sc_opmode |= OPMODE_HBD;
   3024 
   3025 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   3026 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
   3027 
   3028 	/*
   3029 	 * Write new OPMODE bits.  This also restarts the transmit
   3030 	 * and receive processes.
   3031 	 */
   3032 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3033 }
   3034 
   3035 /*
   3036  * tlp_winb_mii_statchg: [mii interface function]
   3037  *
   3038  *	Callback from PHY when media changes.  This version is
   3039  *	for the Winbond 89C840F, which has different OPMODE bits.
   3040  */
   3041 void
   3042 tlp_winb_mii_statchg(self)
   3043 	struct device *self;
   3044 {
   3045 	struct tulip_softc *sc = (struct tulip_softc *)self;
   3046 
   3047 	/* Idle the transmit and receive processes. */
   3048 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   3049 
   3050 	sc->sc_opmode &= ~(OPMODE_WINB_FES|OPMODE_FD);
   3051 
   3052 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX)
   3053 		sc->sc_opmode |= OPMODE_WINB_FES;
   3054 
   3055 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   3056 		sc->sc_opmode |= OPMODE_FD;
   3057 
   3058 	/*
   3059 	 * Write new OPMODE bits.  This also restarts the transmit
   3060 	 * and receive processes.
   3061 	 */
   3062 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3063 }
   3064 
   3065 /*
   3066  * tlp_mii_getmedia:
   3067  *
   3068  *	Callback from ifmedia to request current media status.
   3069  */
   3070 void
   3071 tlp_mii_getmedia(sc, ifmr)
   3072 	struct tulip_softc *sc;
   3073 	struct ifmediareq *ifmr;
   3074 {
   3075 
   3076 	mii_pollstat(&sc->sc_mii);
   3077 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   3078 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   3079 }
   3080 
   3081 /*
   3082  * tlp_mii_setmedia:
   3083  *
   3084  *	Callback from ifmedia to request new media setting.
   3085  */
   3086 int
   3087 tlp_mii_setmedia(sc)
   3088 	struct tulip_softc *sc;
   3089 {
   3090 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   3091 
   3092 	if (ifp->if_flags & IFF_UP) {
   3093 		switch (sc->sc_chip) {
   3094 		case TULIP_CHIP_21142:
   3095 		case TULIP_CHIP_21143:
   3096 			/* Disable the internal Nway engine. */
   3097 			TULIP_WRITE(sc, CSR_SIATXRX, 0);
   3098 			break;
   3099 
   3100 		default:
   3101 			/* Nothing. */
   3102 		}
   3103 		mii_mediachg(&sc->sc_mii);
   3104 	}
   3105 	return (0);
   3106 }
   3107 
   3108 /*
   3109  * tlp_bitbang_mii_readreg:
   3110  *
   3111  *	Read a PHY register via bit-bang'ing the MII.
   3112  */
   3113 int
   3114 tlp_bitbang_mii_readreg(self, phy, reg)
   3115 	struct device *self;
   3116 	int phy, reg;
   3117 {
   3118 	struct tulip_softc *sc = (void *) self;
   3119 
   3120 	return (mii_bitbang_readreg(self, sc->sc_bitbang_ops, phy, reg));
   3121 }
   3122 
   3123 /*
   3124  * tlp_bitbang_mii_writereg:
   3125  *
   3126  *	Write a PHY register via bit-bang'ing the MII.
   3127  */
   3128 void
   3129 tlp_bitbang_mii_writereg(self, phy, reg, val)
   3130 	struct device *self;
   3131 	int phy, reg, val;
   3132 {
   3133 	struct tulip_softc *sc = (void *) self;
   3134 
   3135 	mii_bitbang_writereg(self, sc->sc_bitbang_ops, phy, reg, val);
   3136 }
   3137 
   3138 /*
   3139  * tlp_sio_mii_bitbang_read:
   3140  *
   3141  *	Read the MII serial port for the MII bit-bang module.
   3142  */
   3143 u_int32_t
   3144 tlp_sio_mii_bitbang_read(self)
   3145 	struct device *self;
   3146 {
   3147 	struct tulip_softc *sc = (void *) self;
   3148 
   3149 	return (TULIP_READ(sc, CSR_MIIROM));
   3150 }
   3151 
   3152 /*
   3153  * tlp_sio_mii_bitbang_write:
   3154  *
   3155  *	Write the MII serial port for the MII bit-bang module.
   3156  */
   3157 void
   3158 tlp_sio_mii_bitbang_write(self, val)
   3159 	struct device *self;
   3160 	u_int32_t val;
   3161 {
   3162 	struct tulip_softc *sc = (void *) self;
   3163 
   3164 	TULIP_WRITE(sc, CSR_MIIROM, val);
   3165 }
   3166 
   3167 /*
   3168  * tlp_pnic_mii_readreg:
   3169  *
   3170  *	Read a PHY register on the Lite-On PNIC.
   3171  */
   3172 int
   3173 tlp_pnic_mii_readreg(self, phy, reg)
   3174 	struct device *self;
   3175 	int phy, reg;
   3176 {
   3177 	struct tulip_softc *sc = (void *) self;
   3178 	u_int32_t val;
   3179 	int i;
   3180 
   3181 	TULIP_WRITE(sc, CSR_PNIC_MII,
   3182 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   3183 	    PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
   3184 	    (reg << PNIC_MII_REGSHIFT));
   3185 
   3186 	for (i = 0; i < 1000; i++) {
   3187 		delay(10);
   3188 		val = TULIP_READ(sc, CSR_PNIC_MII);
   3189 		if ((val & PNIC_MII_BUSY) == 0) {
   3190 			if ((val & PNIC_MII_DATA) == PNIC_MII_DATA)
   3191 				return (0);
   3192 			else
   3193 				return (val & PNIC_MII_DATA);
   3194 		}
   3195 	}
   3196 	printf("%s: MII read timed out\n", sc->sc_dev.dv_xname);
   3197 	return (0);
   3198 }
   3199 
   3200 /*
   3201  * tlp_pnic_mii_writereg:
   3202  *
   3203  *	Write a PHY register on the Lite-On PNIC.
   3204  */
   3205 void
   3206 tlp_pnic_mii_writereg(self, phy, reg, val)
   3207 	struct device *self;
   3208 	int phy, reg, val;
   3209 {
   3210 	struct tulip_softc *sc = (void *) self;
   3211 	int i;
   3212 
   3213 	TULIP_WRITE(sc, CSR_PNIC_MII,
   3214 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   3215 	    PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
   3216 	    (reg << PNIC_MII_REGSHIFT) | val);
   3217 
   3218 	for (i = 0; i < 1000; i++) {
   3219 		delay(10);
   3220 		if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
   3221 			return;
   3222 	}
   3223 	printf("%s: MII write timed out\n", sc->sc_dev.dv_xname);
   3224 }
   3225 
   3226 const bus_addr_t tlp_al981_phy_regmap[] = {
   3227 	CSR_ADM_BMCR,
   3228 	CSR_ADM_BMSR,
   3229 	CSR_ADM_PHYIDR1,
   3230 	CSR_ADM_PHYIDR2,
   3231 	CSR_ADM_ANAR,
   3232 	CSR_ADM_ANLPAR,
   3233 	CSR_ADM_ANER,
   3234 
   3235 	CSR_ADM_XMC,
   3236 	CSR_ADM_XCIIS,
   3237 	CSR_ADM_XIE,
   3238 	CSR_ADM_100CTR,
   3239 };
   3240 const int tlp_al981_phy_regmap_size = sizeof(tlp_al981_phy_regmap) /
   3241     sizeof(tlp_al981_phy_regmap[0]);
   3242 
   3243 /*
   3244  * tlp_al981_mii_readreg:
   3245  *
   3246  *	Read a PHY register on the ADMtek AL981.
   3247  */
   3248 int
   3249 tlp_al981_mii_readreg(self, phy, reg)
   3250 	struct device *self;
   3251 	int phy, reg;
   3252 {
   3253 	struct tulip_softc *sc = (struct tulip_softc *)self;
   3254 
   3255 	/* AL981 only has an internal PHY. */
   3256 	if (phy != 0)
   3257 		return (0);
   3258 
   3259 	if (reg >= tlp_al981_phy_regmap_size)
   3260 		return (0);
   3261 
   3262 	return (bus_space_read_4(sc->sc_st, sc->sc_sh,
   3263 	    tlp_al981_phy_regmap[reg]) & 0xffff);
   3264 }
   3265 
   3266 /*
   3267  * tlp_al981_mii_writereg:
   3268  *
   3269  *	Write a PHY register on the ADMtek AL981.
   3270  */
   3271 void
   3272 tlp_al981_mii_writereg(self, phy, reg, val)
   3273 	struct device *self;
   3274 	int phy, reg, val;
   3275 {
   3276 	struct tulip_softc *sc = (struct tulip_softc *)self;
   3277 
   3278 	/* AL981 only has an internal PHY. */
   3279 	if (phy != 0)
   3280 		return;
   3281 
   3282 	if (reg >= tlp_al981_phy_regmap_size)
   3283 		return;
   3284 
   3285 	bus_space_write_4(sc->sc_st, sc->sc_sh,
   3286 	    tlp_al981_phy_regmap[reg], val);
   3287 }
   3288 
   3289 /*****************************************************************************
   3290  * Chip-specific pre-init and reset functions.
   3291  *****************************************************************************/
   3292 
   3293 /*
   3294  * tlp_2114x_preinit:
   3295  *
   3296  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
   3297  */
   3298 void
   3299 tlp_2114x_preinit(sc)
   3300 	struct tulip_softc *sc;
   3301 {
   3302 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3303 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3304 
   3305 	/*
   3306 	 * Whether or not we're in MII or SIA/SYM mode, the media info
   3307 	 * contains the appropriate OPMODE bits.
   3308 	 *
   3309 	 * Note that if we have no media info, we are are doing
   3310 	 * non-MII `auto'.
   3311 	 *
   3312 	 * Also, we always set the Must-Be-One bit.
   3313 	 */
   3314 	if (tm == NULL) {
   3315 #ifdef DIAGNOSTIC
   3316 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   3317 			panic("tlp_2114x_preinit: not IFM_AUTO");
   3318 		if (sc->sc_nway_active == NULL)
   3319 			panic("tlp_2114x_preinit: nway_active NULL");
   3320 #endif
   3321 		tm = sc->sc_nway_active->ifm_aux;
   3322 	}
   3323 	sc->sc_opmode |= OPMODE_MBO | tm->tm_opmode;
   3324 
   3325 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3326 }
   3327 
   3328 /*
   3329  * tlp_2114x_mii_preinit:
   3330  *
   3331  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
   3332  *	This version is used by boards which only have MII and don't have
   3333  *	an ISV SROM.
   3334  */
   3335 void
   3336 tlp_2114x_mii_preinit(sc)
   3337 	struct tulip_softc *sc;
   3338 {
   3339 
   3340 	/*
   3341 	 * Always set the Must-Be-One bit, and Port Select (to select MII).
   3342 	 * We'll never be called during a media change.
   3343 	 */
   3344 	sc->sc_opmode |= OPMODE_MBO|OPMODE_PS;
   3345 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3346 }
   3347 
   3348 /*
   3349  * tlp_pnic_preinit:
   3350  *
   3351  *	Pre-init function for the Lite-On 82c168 and 82c169.
   3352  */
   3353 void
   3354 tlp_pnic_preinit(sc)
   3355 	struct tulip_softc *sc;
   3356 {
   3357 
   3358 	if (sc->sc_flags & TULIPF_HAS_MII) {
   3359 		/*
   3360 		 * MII case: just set the port-select bit; we will never
   3361 		 * be called during a media change.
   3362 		 */
   3363 		sc->sc_opmode |= OPMODE_PS;
   3364 	} else {
   3365 		/*
   3366 		 * ENDEC/PCS/Nway mode; enable the Tx backoff counter.
   3367 		 */
   3368 		sc->sc_opmode |= OPMODE_PNIC_TBEN;
   3369 	}
   3370 }
   3371 
   3372 /*
   3373  * tlp_21140_reset:
   3374  *
   3375  *	Issue a reset sequence on the 21140 via the GPIO facility.
   3376  */
   3377 void
   3378 tlp_21140_reset(sc)
   3379 	struct tulip_softc *sc;
   3380 {
   3381 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3382 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3383 	int i;
   3384 
   3385 	/* First, set the direction on the GPIO pins. */
   3386 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   3387 
   3388 	/* Now, issue the reset sequence. */
   3389 	for (i = 0; i < tm->tm_reset_length; i++) {
   3390 		delay(10);
   3391 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_reset_offset + i]);
   3392 	}
   3393 
   3394 	/* Now, issue the selection sequence. */
   3395 	for (i = 0; i < tm->tm_gp_length; i++) {
   3396 		delay(10);
   3397 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_gp_offset + i]);
   3398 	}
   3399 
   3400 	/* If there were no sequences, just lower the pins. */
   3401 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0)
   3402 		TULIP_WRITE(sc, CSR_GPP, 0);
   3403 }
   3404 
   3405 /*
   3406  * tlp_21142_reset:
   3407  *
   3408  *	Issue a reset sequence on the 21142 via the GPIO facility.
   3409  */
   3410 void
   3411 tlp_21142_reset(sc)
   3412 	struct tulip_softc *sc;
   3413 {
   3414 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3415 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3416 	const u_int8_t *ncp;
   3417 	int i;
   3418 
   3419 	ncp = &sc->sc_srom[tm->tm_reset_offset];
   3420 	for (i = 0; i < tm->tm_reset_length; i++, ncp += 2) {
   3421 		delay(10);
   3422 		TULIP_WRITE(sc, CSR_SIAGEN,
   3423 		    TULIP_ROM_GETW(ncp, 0) << 16);
   3424 	}
   3425 
   3426 	ncp = &sc->sc_srom[tm->tm_gp_offset];
   3427 	for (i = 0; i < tm->tm_gp_length; i++, ncp += 2) {
   3428 		delay(10);
   3429 		TULIP_WRITE(sc, CSR_SIAGEN,
   3430 		    TULIP_ROM_GETW(ncp, 0) << 16);
   3431 	}
   3432 
   3433 	/* If there were no sequences, just lower the pins. */
   3434 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   3435 		delay(10);
   3436 		TULIP_WRITE(sc, CSR_SIAGEN, 0);
   3437 	}
   3438 }
   3439 
   3440 /*
   3441  * tlp_pmac_reset:
   3442  *
   3443  *	Reset routine for Macronix chips.
   3444  */
   3445 void
   3446 tlp_pmac_reset(sc)
   3447 	struct tulip_softc *sc;
   3448 {
   3449 
   3450 	switch (sc->sc_chip) {
   3451 	case TULIP_CHIP_82C115:
   3452 	case TULIP_CHIP_MX98715:
   3453 	case TULIP_CHIP_MX98715A:
   3454 	case TULIP_CHIP_MX98725:
   3455 		/*
   3456 		 * Set the LED operating mode.  This information is located
   3457 		 * in the EEPROM at byte offset 0x77, per the MX98715A and
   3458 		 * MX98725 application notes.
   3459 		 */
   3460 		TULIP_WRITE(sc, CSR_MIIROM, sc->sc_srom[0x77] << 24);
   3461 		break;
   3462 
   3463 	default:
   3464 		/* Nothing. */
   3465 	}
   3466 }
   3467 
   3468 /*****************************************************************************
   3469  * Chip/board-specific media switches.  The ones here are ones that
   3470  * are potentially common to multiple front-ends.
   3471  *****************************************************************************/
   3472 
   3473 /*
   3474  * This table is a common place for all sorts of media information,
   3475  * keyed off of the SROM media code for that media.
   3476  *
   3477  * Note that we explicitly configure the 21142/21143 to always advertise
   3478  * NWay capabilities when using the UTP port.
   3479  * XXX Actually, we don't yet.
   3480  */
   3481 const struct tulip_srom_to_ifmedia tulip_srom_to_ifmedia_table[] = {
   3482 	{ TULIP_ROM_MB_MEDIA_TP,	IFM_10_T,	0,
   3483 	  "10baseT",
   3484 	  0,
   3485 	  { SIACONN_21040_10BASET,
   3486 	    SIATXRX_21040_10BASET,
   3487 	    SIAGEN_21040_10BASET },
   3488 
   3489 	  { SIACONN_21041_10BASET,
   3490 	    SIATXRX_21041_10BASET,
   3491 	    SIAGEN_21041_10BASET },
   3492 
   3493 	  { SIACONN_21142_10BASET,
   3494 	    SIATXRX_21142_10BASET,
   3495 	    SIAGEN_21142_10BASET } },
   3496 
   3497 	{ TULIP_ROM_MB_MEDIA_BNC,	IFM_10_2,	0,
   3498 	  "10base2",
   3499 	  0,
   3500 	  { 0,
   3501 	    0,
   3502 	    0 },
   3503 
   3504 	  { SIACONN_21041_BNC,
   3505 	    SIATXRX_21041_BNC,
   3506 	    SIAGEN_21041_BNC },
   3507 
   3508 	  { SIACONN_21142_BNC,
   3509 	    SIATXRX_21142_BNC,
   3510 	    SIAGEN_21142_BNC } },
   3511 
   3512 	{ TULIP_ROM_MB_MEDIA_AUI,	IFM_10_5,	0,
   3513 	  "10base5",
   3514 	  0,
   3515 	  { SIACONN_21040_AUI,
   3516 	    SIATXRX_21040_AUI,
   3517 	    SIAGEN_21040_AUI },
   3518 
   3519 	  { SIACONN_21041_AUI,
   3520 	    SIATXRX_21041_AUI,
   3521 	    SIAGEN_21041_AUI },
   3522 
   3523 	  { SIACONN_21142_AUI,
   3524 	    SIATXRX_21142_AUI,
   3525 	    SIAGEN_21142_AUI } },
   3526 
   3527 	{ TULIP_ROM_MB_MEDIA_100TX,	IFM_100_TX,	0,
   3528 	  "100baseTX",
   3529 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
   3530 	  { 0,
   3531 	    0,
   3532 	    0 },
   3533 
   3534 	  { 0,
   3535 	    0,
   3536 	    0 },
   3537 
   3538 	  { 0,
   3539 	    0,
   3540 	    SIAGEN_ABM } },
   3541 
   3542 	{ TULIP_ROM_MB_MEDIA_TP_FDX,	IFM_10_T,	IFM_FDX,
   3543 	  "10baseT-FDX",
   3544 	  OPMODE_FD|OPMODE_HBD,
   3545 	  { SIACONN_21040_10BASET_FDX,
   3546 	    SIATXRX_21040_10BASET_FDX,
   3547 	    SIAGEN_21040_10BASET_FDX },
   3548 
   3549 	  { SIACONN_21041_10BASET_FDX,
   3550 	    SIATXRX_21041_10BASET_FDX,
   3551 	    SIAGEN_21041_10BASET_FDX },
   3552 
   3553 	  { SIACONN_21142_10BASET_FDX,
   3554 	    SIATXRX_21142_10BASET_FDX,
   3555 	    SIAGEN_21142_10BASET_FDX } },
   3556 
   3557 	{ TULIP_ROM_MB_MEDIA_100TX_FDX,	IFM_100_TX,	IFM_FDX,
   3558 	  "100baseTX-FDX",
   3559 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_FD|OPMODE_HBD,
   3560 	  { 0,
   3561 	    0,
   3562 	    0 },
   3563 
   3564 	  { 0,
   3565 	    0,
   3566 	    0 },
   3567 
   3568 	  { 0,
   3569 	    0,
   3570 	    SIAGEN_ABM } },
   3571 
   3572 	{ TULIP_ROM_MB_MEDIA_100T4,	IFM_100_T4,	0,
   3573 	  "100baseT4",
   3574 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
   3575 	  { 0,
   3576 	    0,
   3577 	    0 },
   3578 
   3579 	  { 0,
   3580 	    0,
   3581 	    0 },
   3582 
   3583 	  { 0,
   3584 	    0,
   3585 	    SIAGEN_ABM } },
   3586 
   3587 	{ TULIP_ROM_MB_MEDIA_100FX,	IFM_100_FX,	0,
   3588 	  "100baseFX",
   3589 	  OPMODE_PS|OPMODE_PCS|OPMODE_HBD,
   3590 	  { 0,
   3591 	    0,
   3592 	    0 },
   3593 
   3594 	  { 0,
   3595 	    0,
   3596 	    0 },
   3597 
   3598 	  { 0,
   3599 	    0,
   3600 	    SIAGEN_ABM } },
   3601 
   3602 	{ TULIP_ROM_MB_MEDIA_100FX_FDX,	IFM_100_FX,	IFM_FDX,
   3603 	  "100baseFX-FDX",
   3604 	  OPMODE_PS|OPMODE_PCS|OPMODE_FD|OPMODE_HBD,
   3605 	  { 0,
   3606 	    0,
   3607 	    0 },
   3608 
   3609 	  { 0,
   3610 	    0,
   3611 	    0 },
   3612 
   3613 	  { 0,
   3614 	    0,
   3615 	    SIAGEN_ABM } },
   3616 
   3617 	{ 0,				0,		0,
   3618 	  NULL,
   3619 	  0,
   3620 	  { 0,
   3621 	    0,
   3622 	    0 },
   3623 
   3624 	  { 0,
   3625 	    0,
   3626 	    0 },
   3627 
   3628 	  { 0,
   3629 	    0,
   3630 	    0 } },
   3631 };
   3632 
   3633 const struct tulip_srom_to_ifmedia *tlp_srom_to_ifmedia __P((u_int8_t));
   3634 void	tlp_srom_media_info __P((struct tulip_softc *,
   3635 	    const struct tulip_srom_to_ifmedia *, struct tulip_21x4x_media *));
   3636 void	tlp_add_srom_media __P((struct tulip_softc *, int,
   3637 	    void (*)(struct tulip_softc *, struct ifmediareq *),
   3638 	    int (*)(struct tulip_softc *), const u_int8_t *, int));
   3639 void	tlp_print_media __P((struct tulip_softc *));
   3640 void	tlp_nway_activate __P((struct tulip_softc *, int));
   3641 void	tlp_get_minst __P((struct tulip_softc *));
   3642 
   3643 const struct tulip_srom_to_ifmedia *
   3644 tlp_srom_to_ifmedia(sm)
   3645 	u_int8_t sm;
   3646 {
   3647 	const struct tulip_srom_to_ifmedia *tsti;
   3648 
   3649 	for (tsti = tulip_srom_to_ifmedia_table;
   3650 	     tsti->tsti_name != NULL; tsti++) {
   3651 		if (tsti->tsti_srom == sm)
   3652 			return (tsti);
   3653 	}
   3654 
   3655 	return (NULL);
   3656 }
   3657 
   3658 void
   3659 tlp_srom_media_info(sc, tsti, tm)
   3660 	struct tulip_softc *sc;
   3661 	const struct tulip_srom_to_ifmedia *tsti;
   3662 	struct tulip_21x4x_media *tm;
   3663 {
   3664 
   3665 	tm->tm_name = tsti->tsti_name;
   3666 	tm->tm_opmode = tsti->tsti_opmode;
   3667 
   3668 	switch (sc->sc_chip) {
   3669 	case TULIP_CHIP_DE425:
   3670 	case TULIP_CHIP_21040:
   3671 		tm->tm_sia = tsti->tsti_21040;	/* struct assignment */
   3672 		break;
   3673 
   3674 	case TULIP_CHIP_21041:
   3675 		tm->tm_sia = tsti->tsti_21041;	/* struct assignment */
   3676 		break;
   3677 
   3678 	case TULIP_CHIP_21142:
   3679 	case TULIP_CHIP_21143:
   3680 	case TULIP_CHIP_82C115:
   3681 	case TULIP_CHIP_MX98715:
   3682 	case TULIP_CHIP_MX98715A:
   3683 	case TULIP_CHIP_MX98725:
   3684 		tm->tm_sia = tsti->tsti_21142;	/* struct assignment */
   3685 		break;
   3686 
   3687 	default:
   3688 		/* Nothing. */
   3689 	}
   3690 }
   3691 
   3692 void
   3693 tlp_add_srom_media(sc, type, get, set, list, cnt)
   3694 	struct tulip_softc *sc;
   3695 	int type;
   3696 	void (*get) __P((struct tulip_softc *, struct ifmediareq *));
   3697 	int (*set) __P((struct tulip_softc *));
   3698 	const u_int8_t *list;
   3699 	int cnt;
   3700 {
   3701 	struct tulip_21x4x_media *tm;
   3702 	const struct tulip_srom_to_ifmedia *tsti;
   3703 	int i;
   3704 
   3705 	for (i = 0; i < cnt; i++) {
   3706 		tsti = tlp_srom_to_ifmedia(list[i]);
   3707 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   3708 		memset(tm, 0, sizeof(*tm));
   3709 		tlp_srom_media_info(sc, tsti, tm);
   3710 		tm->tm_type = type;
   3711 		tm->tm_get = get;
   3712 		tm->tm_set = set;
   3713 
   3714 		ifmedia_add(&sc->sc_mii.mii_media,
   3715 		    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   3716 		    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   3717 	}
   3718 }
   3719 
   3720 void
   3721 tlp_print_media(sc)
   3722 	struct tulip_softc *sc;
   3723 {
   3724 	struct ifmedia_entry *ife;
   3725 	struct tulip_21x4x_media *tm;
   3726 	const char *sep = "";
   3727 
   3728 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
   3729 
   3730 	printf("%s: ", sc->sc_dev.dv_xname);
   3731 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   3732 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
   3733 		tm = ife->ifm_aux;
   3734 		if (tm == NULL) {
   3735 #ifdef DIAGNOSTIC
   3736 			if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   3737 				panic("tlp_print_media");
   3738 #endif
   3739 			PRINT("auto");
   3740 		} else if (tm->tm_type != TULIP_ROM_MB_21140_MII &&
   3741 			   tm->tm_type != TULIP_ROM_MB_21142_MII) {
   3742 			PRINT(tm->tm_name);
   3743 		}
   3744 	}
   3745 	printf("\n");
   3746 
   3747 #undef PRINT
   3748 }
   3749 
   3750 void
   3751 tlp_nway_activate(sc, media)
   3752 	struct tulip_softc *sc;
   3753 	int media;
   3754 {
   3755 	struct ifmedia_entry *ife;
   3756 
   3757 	ife = ifmedia_match(&sc->sc_mii.mii_media, media, 0);
   3758 #ifdef DIAGNOSTIC
   3759 	if (ife == NULL)
   3760 		panic("tlp_nway_activate");
   3761 #endif
   3762 	sc->sc_nway_active = ife;
   3763 }
   3764 
   3765 void
   3766 tlp_get_minst(sc)
   3767 	struct tulip_softc *sc;
   3768 {
   3769 
   3770 	if ((sc->sc_media_seen &
   3771 	    ~((1 << TULIP_ROM_MB_21140_MII) |
   3772 	      (1 << TULIP_ROM_MB_21142_MII))) == 0) {
   3773 		/*
   3774 		 * We have not yet seen any SIA/SYM media (but are
   3775 		 * about to; that's why we're called!), so assign
   3776 		 * the current media instance to be the `internal media'
   3777 		 * instance, and advance it so any MII media gets a
   3778 		 * fresh one (used to selecting/isolating a PHY).
   3779 		 */
   3780 		sc->sc_tlp_minst = sc->sc_mii.mii_instance++;
   3781 	}
   3782 }
   3783 
   3784 /*
   3785  * SIA Utility functions.
   3786  */
   3787 void	tlp_sia_update_link __P((struct tulip_softc *));
   3788 void	tlp_sia_get __P((struct tulip_softc *, struct ifmediareq *));
   3789 int	tlp_sia_set __P((struct tulip_softc *));
   3790 void	tlp_sia_fixup __P((struct tulip_softc *));
   3791 
   3792 void
   3793 tlp_sia_update_link(sc)
   3794 	struct tulip_softc *sc;
   3795 {
   3796 	struct ifmedia_entry *ife;
   3797 	struct tulip_21x4x_media *tm;
   3798 	u_int32_t siastat;
   3799 
   3800 	ife = TULIP_CURRENT_MEDIA(sc);
   3801 	tm = ife->ifm_aux;
   3802 
   3803 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
   3804 
   3805 	siastat = TULIP_READ(sc, CSR_SIASTAT);
   3806 
   3807 	/*
   3808 	 * Note that when we do SIA link tests, we are assuming that
   3809 	 * the chip is really in the mode that the current media setting
   3810 	 * reflects.  If we're not, then the link tests will not be
   3811 	 * accurate!
   3812 	 */
   3813 	switch (IFM_SUBTYPE(ife->ifm_media)) {
   3814 	case IFM_10_T:
   3815 		sc->sc_flags |= TULIPF_LINK_VALID;
   3816 		if ((siastat & SIASTAT_LS10) == 0)
   3817 			sc->sc_flags |= TULIPF_LINK_UP;
   3818 		break;
   3819 
   3820 	case IFM_100_TX:
   3821 	case IFM_100_T4:
   3822 		sc->sc_flags |= TULIPF_LINK_VALID;
   3823 		if ((siastat & SIASTAT_LS100) == 0)
   3824 			sc->sc_flags |= TULIPF_LINK_UP;
   3825 		break;
   3826 	}
   3827 
   3828 	switch (sc->sc_chip) {
   3829 	case TULIP_CHIP_21142:
   3830 	case TULIP_CHIP_21143:
   3831 		/*
   3832 		 * On these chips, we can tell more information about
   3833 		 * AUI/BNC.  Note that the AUI/BNC selection is made
   3834 		 * in a different register; for our purpose, it's all
   3835 		 * AUI.
   3836 		 */
   3837 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   3838 		case IFM_10_2:
   3839 		case IFM_10_5:
   3840 			sc->sc_flags |= TULIPF_LINK_VALID;
   3841 			if (siastat & SIASTAT_ARA) {
   3842 				TULIP_WRITE(sc, CSR_SIASTAT, SIASTAT_ARA);
   3843 				sc->sc_flags |= TULIPF_LINK_UP;
   3844 			}
   3845 			break;
   3846 
   3847 		default:
   3848 			/*
   3849 			 * If we're SYM media and can detect the link
   3850 			 * via the GPIO facility, prefer that status
   3851 			 * over LS100.
   3852 			 */
   3853 			if (tm->tm_type == TULIP_ROM_MB_21143_SYM &&
   3854 			    tm->tm_actmask != 0) {
   3855 				sc->sc_flags = (sc->sc_flags &
   3856 				    ~TULIPF_LINK_UP) | TULIPF_LINK_VALID;
   3857 				if (TULIP_ISSET(sc, CSR_SIAGEN,
   3858 				    tm->tm_actmask) == tm->tm_actdata)
   3859 					sc->sc_flags |= TULIPF_LINK_UP;
   3860 			}
   3861 		}
   3862 		break;
   3863 
   3864 	default:
   3865 		/* Nothing. */
   3866 	}
   3867 }
   3868 
   3869 void
   3870 tlp_sia_get(sc, ifmr)
   3871 	struct tulip_softc *sc;
   3872 	struct ifmediareq *ifmr;
   3873 {
   3874 	struct ifmedia_entry *ife;
   3875 
   3876 	ifmr->ifm_status = 0;
   3877 
   3878 	tlp_sia_update_link(sc);
   3879 
   3880 	ife = TULIP_CURRENT_MEDIA(sc);
   3881 
   3882 	if (sc->sc_flags & TULIPF_LINK_VALID)
   3883 		ifmr->ifm_status |= IFM_AVALID;
   3884 	if (sc->sc_flags & TULIPF_LINK_UP)
   3885 		ifmr->ifm_status |= IFM_ACTIVE;
   3886 	ifmr->ifm_active = ife->ifm_media;
   3887 }
   3888 
   3889 void
   3890 tlp_sia_fixup(sc)
   3891 	struct tulip_softc *sc;
   3892 {
   3893 	struct ifmedia_entry *ife;
   3894 	struct tulip_21x4x_media *tm;
   3895 	u_int32_t siaconn, siatxrx, siagen;
   3896 
   3897 	switch (sc->sc_chip) {
   3898 	case TULIP_CHIP_82C115:
   3899 	case TULIP_CHIP_MX98713A:
   3900 	case TULIP_CHIP_MX98715:
   3901 	case TULIP_CHIP_MX98715A:
   3902 	case TULIP_CHIP_MX98725:
   3903 		siaconn = PMAC_SIACONN_MASK;
   3904 		siatxrx = PMAC_SIATXRX_MASK;
   3905 		siagen  = PMAC_SIAGEN_MASK;
   3906 		break;
   3907 
   3908 	default:
   3909 		/* No fixups required on any other chips. */
   3910 		return;
   3911 	}
   3912 
   3913 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   3914 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
   3915 		tm = ife->ifm_aux;
   3916 		if (tm == NULL)
   3917 			continue;
   3918 
   3919 		tm->tm_siaconn &= siaconn;
   3920 		tm->tm_siatxrx &= siatxrx;
   3921 		tm->tm_siagen  &= siagen;
   3922 	}
   3923 }
   3924 
   3925 int
   3926 tlp_sia_set(sc)
   3927 	struct tulip_softc *sc;
   3928 {
   3929 	struct ifmedia_entry *ife;
   3930 	struct tulip_21x4x_media *tm;
   3931 
   3932 	ife = TULIP_CURRENT_MEDIA(sc);
   3933 	tm = ife->ifm_aux;
   3934 
   3935 	/*
   3936 	 * XXX This appears to be necessary on a bunch of the clone chips.
   3937 	 */
   3938 	delay(20000);
   3939 
   3940 	/*
   3941 	 * Idle the chip.
   3942 	 */
   3943 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   3944 
   3945 	/*
   3946 	 * Program the SIA.  It's important to write in this order,
   3947 	 * resetting the SIA first.
   3948 	 */
   3949 	TULIP_WRITE(sc, CSR_SIACONN, 0);		/* SRL bit clear */
   3950 	delay(1000);
   3951 
   3952 	TULIP_WRITE(sc, CSR_SIATXRX, tm->tm_siatxrx);
   3953 
   3954 	switch (sc->sc_chip) {
   3955 	case TULIP_CHIP_21142:
   3956 	case TULIP_CHIP_21143:
   3957 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpctl);
   3958 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpdata);
   3959 		break;
   3960 	default:
   3961 		TULIP_WRITE(sc, CSR_SIAGEN,  tm->tm_siagen);
   3962 	}
   3963 
   3964 	TULIP_WRITE(sc, CSR_SIACONN, tm->tm_siaconn);
   3965 
   3966 	/*
   3967 	 * Set the OPMODE bits for this media and write OPMODE.
   3968 	 * This will resume the transmit and receive processes.
   3969 	 */
   3970 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
   3971 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3972 
   3973 	return (0);
   3974 }
   3975 
   3976 /*
   3977  * 21140 GPIO utility functions.
   3978  */
   3979 void	tlp_21140_gpio_update_link __P((struct tulip_softc *));
   3980 void	tlp_21140_gpio_get __P((struct tulip_softc *sc,
   3981 	    struct ifmediareq *ifmr));
   3982 int	tlp_21140_gpio_set __P((struct tulip_softc *sc));
   3983 
   3984 void
   3985 tlp_21140_gpio_update_link(sc)
   3986 	struct tulip_softc *sc;
   3987 {
   3988 	struct ifmedia_entry *ife;
   3989 	struct tulip_21x4x_media *tm;
   3990 
   3991 	ife = TULIP_CURRENT_MEDIA(sc);
   3992 	tm = ife->ifm_aux;
   3993 
   3994 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
   3995 
   3996 	if (tm->tm_actmask != 0) {
   3997 		sc->sc_flags |= TULIPF_LINK_VALID;
   3998 		if (TULIP_ISSET(sc, CSR_GPP, tm->tm_actmask) ==
   3999 		    tm->tm_actdata)
   4000 			sc->sc_flags |= TULIPF_LINK_UP;
   4001 	}
   4002 }
   4003 
   4004 void
   4005 tlp_21140_gpio_get(sc, ifmr)
   4006 	struct tulip_softc *sc;
   4007 	struct ifmediareq *ifmr;
   4008 {
   4009 	struct ifmedia_entry *ife;
   4010 
   4011 	ifmr->ifm_status = 0;
   4012 
   4013 	tlp_21140_gpio_update_link(sc);
   4014 
   4015 	ife = TULIP_CURRENT_MEDIA(sc);
   4016 
   4017 	if (sc->sc_flags & TULIPF_LINK_VALID)
   4018 		ifmr->ifm_status |= IFM_AVALID;
   4019 	if (sc->sc_flags & TULIPF_LINK_UP)
   4020 		ifmr->ifm_status |= IFM_ACTIVE;
   4021 	ifmr->ifm_active = ife->ifm_media;
   4022 }
   4023 
   4024 int
   4025 tlp_21140_gpio_set(sc)
   4026 	struct tulip_softc *sc;
   4027 {
   4028 	struct ifmedia_entry *ife;
   4029 	struct tulip_21x4x_media *tm;
   4030 
   4031 	ife = TULIP_CURRENT_MEDIA(sc);
   4032 	tm = ife->ifm_aux;
   4033 
   4034 	/*
   4035 	 * Idle the chip.
   4036 	 */
   4037 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   4038 
   4039 	/*
   4040 	 * Set the GPIO pins for this media, to flip any
   4041 	 * relays, etc.
   4042 	 */
   4043 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   4044 	delay(10);
   4045 	TULIP_WRITE(sc, CSR_GPP, tm->tm_gpdata);
   4046 
   4047 	/*
   4048 	 * Set the OPMODE bits for this media and write OPMODE.
   4049 	 * This will resume the transmit and receive processes.
   4050 	 */
   4051 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
   4052 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   4053 
   4054 	return (0);
   4055 }
   4056 
   4057 /*
   4058  * 21040 and 21041 media switches.
   4059  */
   4060 void	tlp_21040_tmsw_init __P((struct tulip_softc *));
   4061 void	tlp_21040_tp_tmsw_init __P((struct tulip_softc *));
   4062 void	tlp_21040_auibnc_tmsw_init __P((struct tulip_softc *));
   4063 void	tlp_21041_tmsw_init __P((struct tulip_softc *));
   4064 
   4065 const struct tulip_mediasw tlp_21040_mediasw = {
   4066 	tlp_21040_tmsw_init, tlp_sia_get, tlp_sia_set
   4067 };
   4068 
   4069 const struct tulip_mediasw tlp_21040_tp_mediasw = {
   4070 	tlp_21040_tp_tmsw_init, tlp_sia_get, tlp_sia_set
   4071 };
   4072 
   4073 const struct tulip_mediasw tlp_21040_auibnc_mediasw = {
   4074 	tlp_21040_auibnc_tmsw_init, tlp_sia_get, tlp_sia_set
   4075 };
   4076 
   4077 const struct tulip_mediasw tlp_21041_mediasw = {
   4078 	tlp_21041_tmsw_init, tlp_sia_get, tlp_sia_set
   4079 };
   4080 
   4081 
   4082 void
   4083 tlp_21040_tmsw_init(sc)
   4084 	struct tulip_softc *sc;
   4085 {
   4086 	static const u_int8_t media[] = {
   4087 		TULIP_ROM_MB_MEDIA_TP,
   4088 		TULIP_ROM_MB_MEDIA_TP_FDX,
   4089 		TULIP_ROM_MB_MEDIA_AUI,
   4090 	};
   4091 	struct tulip_21x4x_media *tm;
   4092 
   4093 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4094 	    tlp_mediastatus);
   4095 
   4096 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 3);
   4097 
   4098 	/*
   4099 	 * No SROM type for External SIA.
   4100 	 */
   4101 	tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4102 	memset(tm, 0, sizeof(*tm));
   4103 	tm->tm_name = "manual";
   4104 	tm->tm_opmode = 0;
   4105 	tm->tm_siaconn = SIACONN_21040_EXTSIA;
   4106 	tm->tm_siatxrx = SIATXRX_21040_EXTSIA;
   4107 	tm->tm_siagen  = SIAGEN_21040_EXTSIA;
   4108 	ifmedia_add(&sc->sc_mii.mii_media,
   4109 	    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, sc->sc_tlp_minst), 0, tm);
   4110 
   4111 	/*
   4112 	 * XXX Autosense not yet supported.
   4113 	 */
   4114 
   4115 	/* XXX This should be auto-sense. */
   4116 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   4117 
   4118 	tlp_print_media(sc);
   4119 }
   4120 
   4121 void
   4122 tlp_21040_tp_tmsw_init(sc)
   4123 	struct tulip_softc *sc;
   4124 {
   4125 	static const u_int8_t media[] = {
   4126 		TULIP_ROM_MB_MEDIA_TP,
   4127 		TULIP_ROM_MB_MEDIA_TP_FDX,
   4128 	};
   4129 
   4130 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4131 	    tlp_mediastatus);
   4132 
   4133 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 2);
   4134 
   4135 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   4136 
   4137 	tlp_print_media(sc);
   4138 }
   4139 
   4140 void
   4141 tlp_21040_auibnc_tmsw_init(sc)
   4142 	struct tulip_softc *sc;
   4143 {
   4144 	static const u_int8_t media[] = {
   4145 		TULIP_ROM_MB_MEDIA_AUI,
   4146 	};
   4147 
   4148 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4149 	    tlp_mediastatus);
   4150 
   4151 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 1);
   4152 
   4153 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5);
   4154 
   4155 	tlp_print_media(sc);
   4156 }
   4157 
   4158 void
   4159 tlp_21041_tmsw_init(sc)
   4160 	struct tulip_softc *sc;
   4161 {
   4162 	static const u_int8_t media[] = {
   4163 		TULIP_ROM_MB_MEDIA_TP,
   4164 		TULIP_ROM_MB_MEDIA_TP_FDX,
   4165 		TULIP_ROM_MB_MEDIA_BNC,
   4166 		TULIP_ROM_MB_MEDIA_AUI,
   4167 	};
   4168 	int i, defmedia, devcnt, leaf_offset, mb_offset, m_cnt;
   4169 	const struct tulip_srom_to_ifmedia *tsti;
   4170 	struct tulip_21x4x_media *tm;
   4171 	u_int16_t romdef;
   4172 	u_int8_t mb;
   4173 
   4174 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4175 	    tlp_mediastatus);
   4176 
   4177 	if (tlp_isv_srom(sc->sc_srom) == 0) {
   4178  not_isv_srom:
   4179 		/*
   4180 		 * If we have a board without the standard 21041 SROM format,
   4181 		 * we just assume all media are present and try and pick a
   4182 		 * reasonable default.
   4183 		 */
   4184 		tlp_add_srom_media(sc, 0, NULL, NULL, media, 4);
   4185 
   4186 		/*
   4187 		 * XXX Autosense not yet supported.
   4188 		 */
   4189 
   4190 		/* XXX This should be auto-sense. */
   4191 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   4192 
   4193 		tlp_print_media(sc);
   4194 		return;
   4195 	}
   4196 
   4197 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   4198 	for (i = 0; i < devcnt; i++) {
   4199 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   4200 			break;
   4201 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   4202 		    sc->sc_devno)
   4203 			break;
   4204 	}
   4205 
   4206 	if (i == devcnt)
   4207 		goto not_isv_srom;
   4208 
   4209 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
   4210 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
   4211 	mb_offset = leaf_offset + TULIP_ROM_IL_MEDIAn_BLOCK_BASE;
   4212 	m_cnt = sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
   4213 
   4214 	for (; m_cnt != 0;
   4215 	     m_cnt--, mb_offset += TULIP_ROM_MB_SIZE(mb)) {
   4216 		mb = sc->sc_srom[mb_offset];
   4217 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4218 		memset(tm, 0, sizeof(*tm));
   4219 		switch (mb & TULIP_ROM_MB_MEDIA_CODE) {
   4220 		case TULIP_ROM_MB_MEDIA_TP_FDX:
   4221 		case TULIP_ROM_MB_MEDIA_TP:
   4222 		case TULIP_ROM_MB_MEDIA_BNC:
   4223 		case TULIP_ROM_MB_MEDIA_AUI:
   4224 			tsti = tlp_srom_to_ifmedia(mb &
   4225 			    TULIP_ROM_MB_MEDIA_CODE);
   4226 
   4227 			tlp_srom_media_info(sc, tsti, tm);
   4228 
   4229 			/*
   4230 			 * Override our default SIA settings if the
   4231 			 * SROM contains its own.
   4232 			 */
   4233 			if (mb & TULIP_ROM_MB_EXT) {
   4234 				tm->tm_siaconn = TULIP_ROM_GETW(sc->sc_srom,
   4235 				    mb_offset + TULIP_ROM_MB_CSR13);
   4236 				tm->tm_siatxrx = TULIP_ROM_GETW(sc->sc_srom,
   4237 				    mb_offset + TULIP_ROM_MB_CSR14);
   4238 				tm->tm_siagen = TULIP_ROM_GETW(sc->sc_srom,
   4239 				    mb_offset + TULIP_ROM_MB_CSR15);
   4240 			}
   4241 
   4242 			ifmedia_add(&sc->sc_mii.mii_media,
   4243 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4244 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4245 			break;
   4246 
   4247 		default:
   4248 			printf("%s: unknown media code 0x%02x\n",
   4249 			    sc->sc_dev.dv_xname,
   4250 			    mb & TULIP_ROM_MB_MEDIA_CODE);
   4251 			free(tm, M_DEVBUF);
   4252 		}
   4253 	}
   4254 
   4255 	/*
   4256 	 * XXX Autosense not yet supported.
   4257 	 */
   4258 
   4259 	romdef = TULIP_ROM_GETW(sc->sc_srom, leaf_offset +
   4260 	    TULIP_ROM_IL_SELECT_CONN_TYPE);
   4261 	switch (romdef) {
   4262 	case SELECT_CONN_TYPE_TP:
   4263 	case SELECT_CONN_TYPE_TP_AUTONEG:
   4264 	case SELECT_CONN_TYPE_TP_NOLINKPASS:
   4265 		defmedia = IFM_ETHER|IFM_10_T;
   4266 		break;
   4267 
   4268 	case SELECT_CONN_TYPE_TP_FDX:
   4269 		defmedia = IFM_ETHER|IFM_10_T|IFM_FDX;
   4270 		break;
   4271 
   4272 	case SELECT_CONN_TYPE_BNC:
   4273 		defmedia = IFM_ETHER|IFM_10_2;
   4274 		break;
   4275 
   4276 	case SELECT_CONN_TYPE_AUI:
   4277 		defmedia = IFM_ETHER|IFM_10_5;
   4278 		break;
   4279 #if 0 /* XXX */
   4280 	case SELECT_CONN_TYPE_ASENSE:
   4281 	case SELECT_CONN_TYPE_ASENSE_AUTONEG:
   4282 		defmedia = IFM_ETHER|IFM_AUTO;
   4283 		break;
   4284 #endif
   4285 	default:
   4286 		defmedia = 0;
   4287 	}
   4288 
   4289 	if (defmedia == 0) {
   4290 		/*
   4291 		 * XXX We should default to auto-sense.
   4292 		 */
   4293 		defmedia = IFM_ETHER|IFM_10_T;
   4294 	}
   4295 
   4296 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
   4297 
   4298 	tlp_print_media(sc);
   4299 }
   4300 
   4301 /*
   4302  * DECchip 2114x ISV media switch.
   4303  */
   4304 void	tlp_2114x_isv_tmsw_init __P((struct tulip_softc *));
   4305 void	tlp_2114x_isv_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
   4306 int	tlp_2114x_isv_tmsw_set __P((struct tulip_softc *));
   4307 
   4308 const struct tulip_mediasw tlp_2114x_isv_mediasw = {
   4309 	tlp_2114x_isv_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
   4310 };
   4311 
   4312 void
   4313 tlp_2114x_isv_tmsw_init(sc)
   4314 	struct tulip_softc *sc;
   4315 {
   4316 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4317 	struct ifmedia_entry *ife;
   4318 	struct mii_softc *phy;
   4319 	struct tulip_21x4x_media *tm;
   4320 	const struct tulip_srom_to_ifmedia *tsti;
   4321 	int i, devcnt, leaf_offset, m_cnt, type, length;
   4322 	int defmedia, miidef;
   4323 	u_int16_t word;
   4324 	u_int8_t *cp, *ncp;
   4325 
   4326 	defmedia = miidef = 0;
   4327 
   4328 	sc->sc_mii.mii_ifp = ifp;
   4329 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
   4330 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
   4331 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4332 
   4333 	/*
   4334 	 * Ignore `instance'; we may get a mixture of SIA and MII
   4335 	 * media, and `instance' is used to isolate or select the
   4336 	 * PHY on the MII as appropriate.  Note that duplicate media
   4337 	 * are disallowed, so ignoring `instance' is safe.
   4338 	 */
   4339 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, tlp_mediachange,
   4340 	    tlp_mediastatus);
   4341 
   4342 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   4343 	for (i = 0; i < devcnt; i++) {
   4344 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   4345 			break;
   4346 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   4347 		    sc->sc_devno)
   4348 			break;
   4349 	}
   4350 
   4351 	if (i == devcnt) {
   4352 		printf("%s: unable to locate info leaf in SROM\n",
   4353 		    sc->sc_dev.dv_xname);
   4354 		return;
   4355 	}
   4356 
   4357 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
   4358 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
   4359 
   4360 	/* XXX SELECT CONN TYPE */
   4361 
   4362 	cp = &sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
   4363 
   4364 	/*
   4365 	 * On some chips, the first thing in the Info Leaf is the
   4366 	 * GPIO pin direction data.
   4367 	 */
   4368 	switch (sc->sc_chip) {
   4369 	case TULIP_CHIP_21140:
   4370 	case TULIP_CHIP_21140A:
   4371 	case TULIP_CHIP_MX98713:
   4372 	case TULIP_CHIP_AX88140:
   4373 	case TULIP_CHIP_AX88141:
   4374 		sc->sc_gp_dir = *cp++;
   4375 		break;
   4376 
   4377 	default:
   4378 		/* Nothing. */
   4379 	}
   4380 
   4381 	/* Get the media count. */
   4382 	m_cnt = *cp++;
   4383 
   4384 	for (; m_cnt != 0; cp = ncp, m_cnt--) {
   4385 		/*
   4386 		 * Determine the type and length of this media block.
   4387 		 */
   4388 		if ((*cp & 0x80) == 0) {
   4389 			length = 4;
   4390 			type = TULIP_ROM_MB_21140_GPR;
   4391 		} else {
   4392 			length = (*cp++ & 0x7f) - 1;
   4393 			type = *cp++ & 0x3f;
   4394 		}
   4395 
   4396 		/* Compute the start of the next block. */
   4397 		ncp = cp + length;
   4398 
   4399 		/* Now, parse the block. */
   4400 		switch (type) {
   4401 		case TULIP_ROM_MB_21140_GPR:
   4402 			tlp_get_minst(sc);
   4403 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_GPR;
   4404 
   4405 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4406 			memset(tm, 0, sizeof(*tm));
   4407 
   4408 			tm->tm_type = TULIP_ROM_MB_21140_GPR;
   4409 			tm->tm_get = tlp_21140_gpio_get;
   4410 			tm->tm_set = tlp_21140_gpio_set;
   4411 
   4412 			/* First is the media type code. */
   4413 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4414 			    TULIP_ROM_MB_MEDIA_CODE);
   4415 			if (tsti == NULL) {
   4416 				/* Invalid media code. */
   4417 				free(tm, M_DEVBUF);
   4418 				break;
   4419 			}
   4420 
   4421 			/* Get defaults. */
   4422 			tlp_srom_media_info(sc, tsti, tm);
   4423 
   4424 			/* Next is any GPIO info for this media. */
   4425 			tm->tm_gpdata = cp[1];
   4426 
   4427 			/*
   4428 			 * Next is a word containing OPMODE information
   4429 			 * and info on how to detect if this media is
   4430 			 * active.
   4431 			 */
   4432 			word = TULIP_ROM_GETW(cp, 2);
   4433 			tm->tm_opmode = TULIP_ROM_MB_OPMODE(word);
   4434 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
   4435 				tm->tm_actmask =
   4436 				    TULIP_ROM_MB_BITPOS(word);
   4437 				tm->tm_actdata =
   4438 				    (word & TULIP_ROM_MB_POLARITY) ?
   4439 				    0 : tm->tm_actmask;
   4440 			}
   4441 
   4442 			ifmedia_add(&sc->sc_mii.mii_media,
   4443 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4444 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4445 			break;
   4446 
   4447 		case TULIP_ROM_MB_21140_MII:
   4448 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_MII;
   4449 
   4450 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4451 			memset(tm, 0, sizeof(*tm));
   4452 
   4453 			tm->tm_type = TULIP_ROM_MB_21140_MII;
   4454 			tm->tm_get = tlp_mii_getmedia;
   4455 			tm->tm_set = tlp_mii_setmedia;
   4456 			tm->tm_opmode = OPMODE_PS;
   4457 
   4458 			if (sc->sc_reset == NULL)
   4459 				sc->sc_reset = tlp_21140_reset;
   4460 
   4461 			/* First is the PHY number. */
   4462 			tm->tm_phyno = *cp++;
   4463 
   4464 			/* Next is the MII select sequence length and offset. */
   4465 			tm->tm_gp_length = *cp++;
   4466 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
   4467 			cp += tm->tm_gp_length;
   4468 
   4469 			/* Next is the MII reset sequence length and offset. */
   4470 			tm->tm_reset_length = *cp++;
   4471 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
   4472 			cp += tm->tm_reset_length;
   4473 
   4474 			/*
   4475 			 * The following items are left in the media block
   4476 			 * that we don't particularly care about:
   4477 			 *
   4478 			 *	capabilities		W
   4479 			 *	advertisement		W
   4480 			 *	full duplex		W
   4481 			 *	tx threshold		W
   4482 			 *
   4483 			 * These appear to be bits in the PHY registers,
   4484 			 * which our MII code handles on its own.
   4485 			 */
   4486 
   4487 			/*
   4488 			 * Before we probe the MII bus, we need to reset
   4489 			 * it and issue the selection sequence.
   4490 			 */
   4491 
   4492 			/* Set the direction of the pins... */
   4493 			TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   4494 
   4495 			for (i = 0; i < tm->tm_reset_length; i++) {
   4496 				delay(10);
   4497 				TULIP_WRITE(sc, CSR_GPP,
   4498 				    sc->sc_srom[tm->tm_reset_offset + i]);
   4499 			}
   4500 
   4501 			for (i = 0; i < tm->tm_gp_length; i++) {
   4502 				delay(10);
   4503 				TULIP_WRITE(sc, CSR_GPP,
   4504 				    sc->sc_srom[tm->tm_gp_offset + i]);
   4505 			}
   4506 
   4507 			/* If there were no sequences, just lower the pins. */
   4508 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   4509 				delay(10);
   4510 				TULIP_WRITE(sc, CSR_GPP, 0);
   4511 			}
   4512 
   4513 			/*
   4514 			 * Now, probe the MII for the PHY.  Note, we know
   4515 			 * the location of the PHY on the bus, but we don't
   4516 			 * particularly care; the MII code just likes to
   4517 			 * search the whole thing anyhow.
   4518 			 */
   4519 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
   4520 			    MII_PHY_ANY, tm->tm_phyno, 0);
   4521 
   4522 			/*
   4523 			 * Now, search for the PHY we hopefully just
   4524 			 * configured.  If it's not configured into the
   4525 			 * kernel, we lose.  The PHY's default media always
   4526 			 * takes priority.
   4527 			 */
   4528 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
   4529 			     phy != NULL;
   4530 			     phy = LIST_NEXT(phy, mii_list))
   4531 				if (phy->mii_offset == tm->tm_phyno)
   4532 					break;
   4533 			if (phy == NULL) {
   4534 				printf("%s: unable to configure MII\n",
   4535 				    sc->sc_dev.dv_xname);
   4536 				break;
   4537 			}
   4538 
   4539 			sc->sc_flags |= TULIPF_HAS_MII;
   4540 			sc->sc_tick = tlp_mii_tick;
   4541 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
   4542 			    phy->mii_inst);
   4543 
   4544 			/*
   4545 			 * Okay, now that we've found the PHY and the MII
   4546 			 * layer has added all of the media associated
   4547 			 * with that PHY, we need to traverse the media
   4548 			 * list, and add our `tm' to each entry's `aux'
   4549 			 * pointer.
   4550 			 *
   4551 			 * We do this by looking for media with our
   4552 			 * PHY's `instance'.
   4553 			 */
   4554 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   4555 			     ife != NULL;
   4556 			     ife = TAILQ_NEXT(ife, ifm_list)) {
   4557 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
   4558 					continue;
   4559 				ife->ifm_aux = tm;
   4560 			}
   4561 			break;
   4562 
   4563 		case TULIP_ROM_MB_21142_SIA:
   4564 			tlp_get_minst(sc);
   4565 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_SIA;
   4566 
   4567 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4568 			memset(tm, 0, sizeof(*tm));
   4569 
   4570 			tm->tm_type = TULIP_ROM_MB_21142_SIA;
   4571 			tm->tm_get = tlp_sia_get;
   4572 			tm->tm_set = tlp_sia_set;
   4573 
   4574 			/* First is the media type code. */
   4575 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4576 			    TULIP_ROM_MB_MEDIA_CODE);
   4577 			if (tsti == NULL) {
   4578 				/* Invalid media code. */
   4579 				free(tm, M_DEVBUF);
   4580 				break;
   4581 			}
   4582 
   4583 			/* Get defaults. */
   4584 			tlp_srom_media_info(sc, tsti, tm);
   4585 
   4586 			/*
   4587 			 * Override our default SIA settings if the
   4588 			 * SROM contains its own.
   4589 			 */
   4590 			if (cp[0] & 0x40) {
   4591 				tm->tm_siaconn = TULIP_ROM_GETW(cp, 1);
   4592 				tm->tm_siatxrx = TULIP_ROM_GETW(cp, 3);
   4593 				tm->tm_siagen  = TULIP_ROM_GETW(cp, 5);
   4594 				cp += 7;
   4595 			} else
   4596 				cp++;
   4597 
   4598 			/* Next is GPIO control/data. */
   4599 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 0);
   4600 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 2);
   4601 
   4602 			ifmedia_add(&sc->sc_mii.mii_media,
   4603 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4604 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4605 			break;
   4606 
   4607 		case TULIP_ROM_MB_21142_MII:
   4608 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_MII;
   4609 
   4610 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4611 			memset(tm, 0, sizeof(*tm));
   4612 
   4613 			tm->tm_type = TULIP_ROM_MB_21142_MII;
   4614 			tm->tm_get = tlp_mii_getmedia;
   4615 			tm->tm_set = tlp_mii_setmedia;
   4616 			tm->tm_opmode = OPMODE_PS;
   4617 
   4618 			if (sc->sc_reset == NULL)
   4619 				sc->sc_reset = tlp_21142_reset;
   4620 
   4621 			/* First is the PHY number. */
   4622 			tm->tm_phyno = *cp++;
   4623 
   4624 			/* Next is the MII select sequence length and offset. */
   4625 			tm->tm_gp_length = *cp++;
   4626 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
   4627 			cp += tm->tm_gp_length * 2;
   4628 
   4629 			/* Next is the MII reset sequence length and offset. */
   4630 			tm->tm_reset_length = *cp++;
   4631 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
   4632 			cp += tm->tm_reset_length * 2;
   4633 
   4634 			/*
   4635 			 * The following items are left in the media block
   4636 			 * that we don't particularly care about:
   4637 			 *
   4638 			 *	capabilities		W
   4639 			 *	advertisement		W
   4640 			 *	full duplex		W
   4641 			 *	tx threshold		W
   4642 			 *	MII interrupt		W
   4643 			 *
   4644 			 * These appear to be bits in the PHY registers,
   4645 			 * which our MII code handles on its own.
   4646 			 */
   4647 
   4648 			/*
   4649 			 * Before we probe the MII bus, we need to reset
   4650 			 * it and issue the selection sequence.
   4651 			 */
   4652 
   4653 			ncp = &sc->sc_srom[tm->tm_reset_offset];
   4654 			for (i = 0; i < tm->tm_reset_length; i++, ncp += 2) {
   4655 				delay(10);
   4656 				TULIP_WRITE(sc, CSR_SIAGEN,
   4657 				    TULIP_ROM_GETW(ncp, 0) << 16);
   4658 			}
   4659 
   4660 			ncp = &sc->sc_srom[tm->tm_gp_offset];
   4661 			for (i = 0; i < tm->tm_gp_length; i++, ncp += 2) {
   4662 				delay(10);
   4663 				TULIP_WRITE(sc, CSR_SIAGEN,
   4664 				    TULIP_ROM_GETW(ncp, 0) << 16);
   4665 			}
   4666 
   4667 			/* If there were no sequences, just lower the pins. */
   4668 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   4669 				delay(10);
   4670 				TULIP_WRITE(sc, CSR_SIAGEN, 0);
   4671 			}
   4672 
   4673 			/*
   4674 			 * Now, probe the MII for the PHY.  Note, we know
   4675 			 * the location of the PHY on the bus, but we don't
   4676 			 * particularly care; the MII code just likes to
   4677 			 * search the whole thing anyhow.
   4678 			 */
   4679 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
   4680 			    MII_PHY_ANY, tm->tm_phyno, 0);
   4681 
   4682 			/*
   4683 			 * Now, search for the PHY we hopefully just
   4684 			 * configured.  If it's not configured into the
   4685 			 * kernel, we lose.  The PHY's default media always
   4686 			 * takes priority.
   4687 			 */
   4688 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
   4689 			     phy != NULL;
   4690 			     phy = LIST_NEXT(phy, mii_list))
   4691 				if (phy->mii_offset == tm->tm_phyno)
   4692 					break;
   4693 			if (phy == NULL) {
   4694 				printf("%s: unable to configure MII\n",
   4695 				    sc->sc_dev.dv_xname);
   4696 				break;
   4697 			}
   4698 
   4699 			sc->sc_flags |= TULIPF_HAS_MII;
   4700 			sc->sc_tick = tlp_mii_tick;
   4701 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
   4702 			    phy->mii_inst);
   4703 
   4704 			/*
   4705 			 * Okay, now that we've found the PHY and the MII
   4706 			 * layer has added all of the media associated
   4707 			 * with that PHY, we need to traverse the media
   4708 			 * list, and add our `tm' to each entry's `aux'
   4709 			 * pointer.
   4710 			 *
   4711 			 * We do this by looking for media with our
   4712 			 * PHY's `instance'.
   4713 			 */
   4714 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   4715 			     ife != NULL;
   4716 			     ife = TAILQ_NEXT(ife, ifm_list)) {
   4717 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
   4718 					continue;
   4719 				ife->ifm_aux = tm;
   4720 			}
   4721 			break;
   4722 
   4723 		case TULIP_ROM_MB_21143_SYM:
   4724 			tlp_get_minst(sc);
   4725 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21143_SYM;
   4726 
   4727 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4728 			memset(tm, 0, sizeof(*tm));
   4729 
   4730 			tm->tm_type = TULIP_ROM_MB_21143_SYM;
   4731 			tm->tm_get = tlp_sia_get;
   4732 			tm->tm_set = tlp_sia_set;
   4733 
   4734 			/* First is the media type code. */
   4735 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4736 			    TULIP_ROM_MB_MEDIA_CODE);
   4737 			if (tsti == NULL) {
   4738 				/* Invalid media code. */
   4739 				free(tm, M_DEVBUF);
   4740 				break;
   4741 			}
   4742 
   4743 			/* Get defaults. */
   4744 			tlp_srom_media_info(sc, tsti, tm);
   4745 
   4746 			/* Next is GPIO control/data. */
   4747 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 1);
   4748 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 3);
   4749 
   4750 			/*
   4751 			 * Next is a word containing OPMODE information
   4752 			 * and info on how to detect if this media is
   4753 			 * active.
   4754 			 */
   4755 			word = TULIP_ROM_GETW(cp, 5);
   4756 			tm->tm_opmode = TULIP_ROM_MB_OPMODE(word);
   4757 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
   4758 				tm->tm_actmask =
   4759 				    TULIP_ROM_MB_BITPOS(word);
   4760 				tm->tm_actdata =
   4761 				    (word & TULIP_ROM_MB_POLARITY) ?
   4762 				    0 : tm->tm_actmask;
   4763 			}
   4764 
   4765 			ifmedia_add(&sc->sc_mii.mii_media,
   4766 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4767 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4768 			break;
   4769 
   4770 		case TULIP_ROM_MB_21143_RESET:
   4771 			printf("%s: 21143 reset block\n", sc->sc_dev.dv_xname);
   4772 			break;
   4773 
   4774 		default:
   4775 			printf("%s: unknown ISV media block type 0x%02x\n",
   4776 			    sc->sc_dev.dv_xname, type);
   4777 		}
   4778 	}
   4779 
   4780 	/*
   4781 	 * Deal with the case where no media is configured.
   4782 	 */
   4783 	if (TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list) == NULL) {
   4784 		printf("%s: no media found!\n", sc->sc_dev.dv_xname);
   4785 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   4786 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   4787 		return;
   4788 	}
   4789 
   4790 	/*
   4791 	 * Pick the default media.
   4792 	 */
   4793 	if (miidef != 0)
   4794 		defmedia = miidef;
   4795 	else {
   4796 		/*
   4797 		 * XXX Pick a better default.  Should come from SROM
   4798 		 * XXX on 21140[A], and should be "auto" on 21142,
   4799 		 * XXX 21143, and Macronix chips.
   4800 		 */
   4801 		defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
   4802 	}
   4803 
   4804 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
   4805 
   4806 	/*
   4807 	 * Display any non-MII media we've located.
   4808 	 */
   4809 	if (sc->sc_media_seen &
   4810 	    ~((1 << TULIP_ROM_MB_21140_MII) | (1 << TULIP_ROM_MB_21142_MII)))
   4811 		tlp_print_media(sc);
   4812 
   4813 	tlp_sia_fixup(sc);
   4814 }
   4815 
   4816 void
   4817 tlp_2114x_isv_tmsw_get(sc, ifmr)
   4818 	struct tulip_softc *sc;
   4819 	struct ifmediareq *ifmr;
   4820 {
   4821 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   4822 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   4823 
   4824 	/*
   4825 	 * We might be polling a non-MII autosense; check for that.
   4826 	 */
   4827 	if (tm == NULL) {
   4828 #ifdef DIAGNOSTIC
   4829 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   4830 			panic("tlp_2114x_isv_tmsw_get");
   4831 #endif
   4832 		tm = sc->sc_nway_active->ifm_aux;
   4833 	}
   4834 
   4835 	(*tm->tm_get)(sc, ifmr);
   4836 }
   4837 
   4838 int
   4839 tlp_2114x_isv_tmsw_set(sc)
   4840 	struct tulip_softc *sc;
   4841 {
   4842 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   4843 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   4844 
   4845 	/*
   4846 	 * We might be setting a non-MII autosense; check for that.
   4847 	 */
   4848 	if (tm == NULL) {
   4849 #ifdef DIAGNOSTIC
   4850 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   4851 			panic("tlp_2114x_isv_tmsw_set");
   4852 #endif
   4853 		/* XXX XXX XXX */
   4854 	}
   4855 
   4856 	/*
   4857 	 * Check to see if we need to reset the chip, and do it.  The
   4858 	 * reset path will get the OPMODE register right the next
   4859 	 * time through.
   4860 	 */
   4861 	if (TULIP_MEDIA_NEEDSRESET(sc, tm->tm_opmode))
   4862 		return (tlp_init(sc));
   4863 
   4864 	return ((*tm->tm_set)(sc));
   4865 }
   4866 
   4867 /*
   4868  * MII-on-SIO media switch.  Handles only MII attached to the SIO.
   4869  */
   4870 void	tlp_sio_mii_tmsw_init __P((struct tulip_softc *));
   4871 
   4872 const struct tulip_mediasw tlp_sio_mii_mediasw = {
   4873 	tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   4874 };
   4875 
   4876 void
   4877 tlp_sio_mii_tmsw_init(sc)
   4878 	struct tulip_softc *sc;
   4879 {
   4880 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4881 
   4882 	/*
   4883 	 * We don't attach any media info structures to the ifmedia
   4884 	 * entries, so if we're using a pre-init function that needs
   4885 	 * that info, override it to one that doesn't.
   4886 	 */
   4887 	if (sc->sc_preinit == tlp_2114x_preinit)
   4888 		sc->sc_preinit = tlp_2114x_mii_preinit;
   4889 
   4890 	sc->sc_mii.mii_ifp = ifp;
   4891 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
   4892 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
   4893 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4894 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4895 	    tlp_mediastatus);
   4896 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   4897 	    MII_OFFSET_ANY, 0);
   4898 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   4899 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   4900 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   4901 	} else {
   4902 		sc->sc_flags |= TULIPF_HAS_MII;
   4903 		sc->sc_tick = tlp_mii_tick;
   4904 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4905 	}
   4906 }
   4907 
   4908 /*
   4909  * Lite-On PNIC media switch.  Must handle MII or internal NWAY.
   4910  */
   4911 void	tlp_pnic_tmsw_init __P((struct tulip_softc *));
   4912 void	tlp_pnic_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
   4913 int	tlp_pnic_tmsw_set __P((struct tulip_softc *));
   4914 
   4915 const struct tulip_mediasw tlp_pnic_mediasw = {
   4916 	tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
   4917 };
   4918 
   4919 void	tlp_pnic_nway_statchg __P((struct device *));
   4920 void	tlp_pnic_nway_tick __P((void *));
   4921 int	tlp_pnic_nway_service __P((struct tulip_softc *, int));
   4922 void	tlp_pnic_nway_reset __P((struct tulip_softc *));
   4923 int	tlp_pnic_nway_auto __P((struct tulip_softc *, int));
   4924 void	tlp_pnic_nway_auto_timeout __P((void *));
   4925 void	tlp_pnic_nway_status __P((struct tulip_softc *));
   4926 void	tlp_pnic_nway_acomp __P((struct tulip_softc *));
   4927 
   4928 void
   4929 tlp_pnic_tmsw_init(sc)
   4930 	struct tulip_softc *sc;
   4931 {
   4932 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4933 	const char *sep = "";
   4934 
   4935 #define	ADD(m, c)	ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL)
   4936 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
   4937 
   4938 	sc->sc_mii.mii_ifp = ifp;
   4939 	sc->sc_mii.mii_readreg = tlp_pnic_mii_readreg;
   4940 	sc->sc_mii.mii_writereg = tlp_pnic_mii_writereg;
   4941 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4942 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4943 	    tlp_mediastatus);
   4944 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   4945 	    MII_OFFSET_ANY, 0);
   4946 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   4947 		/* XXX What about AUI/BNC support? */
   4948 		printf("%s: ", sc->sc_dev.dv_xname);
   4949 
   4950 		tlp_pnic_nway_reset(sc);
   4951 
   4952 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
   4953 		    PNIC_NWAY_TW|PNIC_NWAY_CAP10T);
   4954 		PRINT("10baseT");
   4955 
   4956 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
   4957 		    PNIC_NWAY_TW|PNIC_NWAY_FD|PNIC_NWAY_CAP10TFDX);
   4958 		PRINT("10baseT-FDX");
   4959 
   4960 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
   4961 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_CAP100TX);
   4962 		PRINT("100baseTX");
   4963 
   4964 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
   4965 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_FD|
   4966 		    PNIC_NWAY_CAP100TXFDX);
   4967 		PRINT("100baseTX-FDX");
   4968 
   4969 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
   4970 		    PNIC_NWAY_TW|PNIC_NWAY_RN|PNIC_NWAY_NW|
   4971 		    PNIC_NWAY_CAP10T|PNIC_NWAY_CAP10TFDX|
   4972 		    PNIC_NWAY_CAP100TXFDX|PNIC_NWAY_CAP100TX);
   4973 		PRINT("auto");
   4974 
   4975 		printf("\n");
   4976 
   4977 		sc->sc_statchg = tlp_pnic_nway_statchg;
   4978 		sc->sc_tick = tlp_pnic_nway_tick;
   4979 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4980 	} else {
   4981 		sc->sc_flags |= TULIPF_HAS_MII;
   4982 		sc->sc_tick = tlp_mii_tick;
   4983 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4984 	}
   4985 
   4986 #undef ADD
   4987 #undef PRINT
   4988 }
   4989 
   4990 void
   4991 tlp_pnic_tmsw_get(sc, ifmr)
   4992 	struct tulip_softc *sc;
   4993 	struct ifmediareq *ifmr;
   4994 {
   4995 	struct mii_data *mii = &sc->sc_mii;
   4996 
   4997 	if (sc->sc_flags & TULIPF_HAS_MII)
   4998 		tlp_mii_getmedia(sc, ifmr);
   4999 	else {
   5000 		mii->mii_media_status = 0;
   5001 		mii->mii_media_active = IFM_NONE;
   5002 		tlp_pnic_nway_service(sc, MII_POLLSTAT);
   5003 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
   5004 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
   5005 	}
   5006 }
   5007 
   5008 int
   5009 tlp_pnic_tmsw_set(sc)
   5010 	struct tulip_softc *sc;
   5011 {
   5012 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   5013 	struct mii_data *mii = &sc->sc_mii;
   5014 
   5015 	if (sc->sc_flags & TULIPF_HAS_MII) {
   5016 		/*
   5017 		 * Make sure the built-in Tx jabber timer is disabled.
   5018 		 */
   5019 		TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);
   5020 
   5021 		return (tlp_mii_setmedia(sc));
   5022 	}
   5023 
   5024 	if (ifp->if_flags & IFF_UP) {
   5025 		mii->mii_media_status = 0;
   5026 		mii->mii_media_active = IFM_NONE;
   5027 		return (tlp_pnic_nway_service(sc, MII_MEDIACHG));
   5028 	}
   5029 
   5030 	return (0);
   5031 }
   5032 
   5033 void
   5034 tlp_pnic_nway_statchg(self)
   5035 	struct device *self;
   5036 {
   5037 	struct tulip_softc *sc = (struct tulip_softc *)self;
   5038 
   5039 	/* Idle the transmit and receive processes. */
   5040 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   5041 
   5042 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_PS|OPMODE_PCS|
   5043 	    OPMODE_SCR|OPMODE_HBD);
   5044 
   5045 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
   5046 		sc->sc_opmode |= OPMODE_TTM;
   5047 		TULIP_WRITE(sc, CSR_GPP,
   5048 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) |
   5049 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
   5050 	} else {
   5051 		sc->sc_opmode |= OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD;
   5052 		TULIP_WRITE(sc, CSR_GPP,
   5053 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) |
   5054 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
   5055 	}
   5056 
   5057 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   5058 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
   5059 
   5060 	/*
   5061 	 * Write new OPMODE bits.  This also restarts the transmit
   5062 	 * and receive processes.
   5063 	 */
   5064 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   5065 }
   5066 
   5067 void
   5068 tlp_pnic_nway_tick(arg)
   5069 	void *arg;
   5070 {
   5071 	struct tulip_softc *sc = arg;
   5072 	int s;
   5073 
   5074 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   5075 		return;
   5076 
   5077 	s = splnet();
   5078 	tlp_pnic_nway_service(sc, MII_TICK);
   5079 	splx(s);
   5080 
   5081 	callout_reset(&sc->sc_tick_callout, hz, tlp_pnic_nway_tick, sc);
   5082 }
   5083 
   5084 /*
   5085  * Support for the Lite-On PNIC internal NWay block.  This is constructed
   5086  * somewhat like a PHY driver for simplicity.
   5087  */
   5088 
   5089 int
   5090 tlp_pnic_nway_service(sc, cmd)
   5091 	struct tulip_softc *sc;
   5092 	int cmd;
   5093 {
   5094 	struct mii_data *mii = &sc->sc_mii;
   5095 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   5096 
   5097 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
   5098 		return (0);
   5099 
   5100 	switch (cmd) {
   5101 	case MII_POLLSTAT:
   5102 		/* Nothing special to do here. */
   5103 		break;
   5104 
   5105 	case MII_MEDIACHG:
   5106 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   5107 		case IFM_AUTO:
   5108 			(void) tlp_pnic_nway_auto(sc, 1);
   5109 			break;
   5110 		case IFM_100_T4:
   5111 			/*
   5112 			 * XXX Not supported as a manual setting right now.
   5113 			 */
   5114 			return (EINVAL);
   5115 		default:
   5116 			/*
   5117 			 * NWAY register data is stored in the ifmedia entry.
   5118 			 */
   5119 			TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   5120 		}
   5121 		break;
   5122 
   5123 	case MII_TICK:
   5124 		/*
   5125 		 * Only used for autonegotiation.
   5126 		 */
   5127 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   5128 			return (0);
   5129 
   5130 		/*
   5131 		 * Check to see if we have link.  If we do, we don't
   5132 		 * need to restart the autonegotiation process.
   5133 		 */
   5134 		if (sc->sc_flags & TULIPF_LINK_UP)
   5135 			return (0);
   5136 
   5137 		/*
   5138 		 * Only retry autonegotiation every 5 seconds.
   5139 		 */
   5140 		if (++sc->sc_nway_ticks != 5)
   5141 			return (0);
   5142 
   5143 		sc->sc_nway_ticks = 0;
   5144 		tlp_pnic_nway_reset(sc);
   5145 		if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
   5146 			return (0);
   5147 		break;
   5148 	}
   5149 
   5150 	/* Update the media status. */
   5151 	tlp_pnic_nway_status(sc);
   5152 
   5153 	/* Callback if something changed. */
   5154 	if ((sc->sc_nway_active == NULL ||
   5155 	     sc->sc_nway_active->ifm_media != mii->mii_media_active) ||
   5156 	    cmd == MII_MEDIACHG) {
   5157 		(*sc->sc_statchg)(&sc->sc_dev);
   5158 		tlp_nway_activate(sc, mii->mii_media_active);
   5159 	}
   5160 	return (0);
   5161 }
   5162 
   5163 void
   5164 tlp_pnic_nway_reset(sc)
   5165 	struct tulip_softc *sc;
   5166 {
   5167 
   5168 	TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
   5169 	delay(100);
   5170 	TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
   5171 }
   5172 
   5173 int
   5174 tlp_pnic_nway_auto(sc, waitfor)
   5175 	struct tulip_softc *sc;
   5176 	int waitfor;
   5177 {
   5178 	struct mii_data *mii = &sc->sc_mii;
   5179 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   5180 	u_int32_t reg;
   5181 	int i;
   5182 
   5183 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
   5184 		TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   5185 
   5186 	if (waitfor) {
   5187 		/* Wait 500ms for it to complete. */
   5188 		for (i = 0; i < 500; i++) {
   5189 			reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5190 			if (reg & PNIC_NWAY_LPAR_MASK) {
   5191 				tlp_pnic_nway_acomp(sc);
   5192 				return (0);
   5193 			}
   5194 			delay(1000);
   5195 		}
   5196 #if 0
   5197 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   5198 			printf("%s: autonegotiation failed to complete\n",
   5199 			    sc->sc_dev.dv_xname);
   5200 #endif
   5201 
   5202 		/*
   5203 		 * Don't need to worry about clearing DOINGAUTO.
   5204 		 * If that's set, a timeout is pending, and it will
   5205 		 * clear the flag.
   5206 		 */
   5207 		return (EIO);
   5208 	}
   5209 
   5210 	/*
   5211 	 * Just let it finish asynchronously.  This is for the benefit of
   5212 	 * the tick handler driving autonegotiation.  Don't want 500ms
   5213 	 * delays all the time while the system is running!
   5214 	 */
   5215 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
   5216 		sc->sc_flags |= TULIPF_DOINGAUTO;
   5217 		callout_reset(&sc->sc_nway_callout, hz >> 1,
   5218 		    tlp_pnic_nway_auto_timeout, sc);
   5219 	}
   5220 	return (EJUSTRETURN);
   5221 }
   5222 
   5223 void
   5224 tlp_pnic_nway_auto_timeout(arg)
   5225 	void *arg;
   5226 {
   5227 	struct tulip_softc *sc = arg;
   5228 	u_int32_t reg;
   5229 	int s;
   5230 
   5231 	s = splnet();
   5232 	sc->sc_flags &= ~TULIPF_DOINGAUTO;
   5233 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5234 #if 0
   5235 	if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   5236 		printf("%s: autonegotiation failed to complete\n",
   5237 		    sc->sc_dev.dv_xname);
   5238 #endif
   5239 
   5240 	tlp_pnic_nway_acomp(sc);
   5241 
   5242 	/* Update the media status. */
   5243 	(void) tlp_pnic_nway_service(sc, MII_POLLSTAT);
   5244 	splx(s);
   5245 }
   5246 
   5247 void
   5248 tlp_pnic_nway_status(sc)
   5249 	struct tulip_softc *sc;
   5250 {
   5251 	struct mii_data *mii = &sc->sc_mii;
   5252 	u_int32_t reg;
   5253 
   5254 	mii->mii_media_status = IFM_AVALID;
   5255 	mii->mii_media_active = IFM_ETHER;
   5256 
   5257 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5258 
   5259 	if (sc->sc_flags & TULIPF_LINK_UP)
   5260 		mii->mii_media_status |= IFM_ACTIVE;
   5261 
   5262 	if (reg & PNIC_NWAY_NW) {
   5263 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
   5264 			/* Erg, still trying, I guess... */
   5265 			mii->mii_media_active |= IFM_NONE;
   5266 			return;
   5267 		}
   5268 
   5269 #if 0
   5270 		if (reg & PNIC_NWAY_LPAR100T4)
   5271 			mii->mii_media_active |= IFM_100_T4;
   5272 		else
   5273 #endif
   5274 		if (reg & PNIC_NWAY_LPAR100TXFDX)
   5275 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
   5276 		else if (reg & PNIC_NWAY_LPAR100TX)
   5277 			mii->mii_media_active |= IFM_100_TX;
   5278 		else if (reg & PNIC_NWAY_LPAR10TFDX)
   5279 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
   5280 		else if (reg & PNIC_NWAY_LPAR10T)
   5281 			mii->mii_media_active |= IFM_10_T;
   5282 		else
   5283 			mii->mii_media_active |= IFM_NONE;
   5284 	} else {
   5285 		if (reg & PNIC_NWAY_100)
   5286 			mii->mii_media_active |= IFM_100_TX;
   5287 		else
   5288 			mii->mii_media_active |= IFM_10_T;
   5289 		if (reg & PNIC_NWAY_FD)
   5290 			mii->mii_media_active |= IFM_FDX;
   5291 	}
   5292 }
   5293 
   5294 void
   5295 tlp_pnic_nway_acomp(sc)
   5296 	struct tulip_softc *sc;
   5297 {
   5298 	u_int32_t reg;
   5299 
   5300 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5301 	reg &= ~(PNIC_NWAY_FD|PNIC_NWAY_100|PNIC_NWAY_RN);
   5302 
   5303 	if (reg & (PNIC_NWAY_LPAR100TXFDX|PNIC_NWAY_LPAR100TX))
   5304 		reg |= PNIC_NWAY_100;
   5305 	if (reg & (PNIC_NWAY_LPAR10TFDX|PNIC_NWAY_LPAR100TXFDX))
   5306 		reg |= PNIC_NWAY_FD;
   5307 
   5308 	TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
   5309 }
   5310 
   5311 /*
   5312  * Macronix PMAC and Lite-On PNIC-II media switch:
   5313  *
   5314  *	MX98713 and MX98713A		21140-like MII or GPIO media.
   5315  *
   5316  *	MX98713A			21143-like MII or SIA/SYM media.
   5317  *
   5318  *	MX98715, MX98715A, MX98725,	21143-like SIA/SYM media.
   5319  *	82C115
   5320  *
   5321  * So, what we do here is fake MII-on-SIO or ISV media info, and
   5322  * use the ISV media switch get/set functions to handle the rest.
   5323  */
   5324 
   5325 void	tlp_pmac_tmsw_init __P((struct tulip_softc *));
   5326 
   5327 const struct tulip_mediasw tlp_pmac_mediasw = {
   5328 	tlp_pmac_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
   5329 };
   5330 
   5331 const struct tulip_mediasw tlp_pmac_mii_mediasw = {
   5332 	tlp_pmac_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   5333 };
   5334 
   5335 void
   5336 tlp_pmac_tmsw_init(sc)
   5337 	struct tulip_softc *sc;
   5338 {
   5339 	static const u_int8_t media[] = {
   5340 		TULIP_ROM_MB_MEDIA_TP,
   5341 		TULIP_ROM_MB_MEDIA_TP_FDX,
   5342 		TULIP_ROM_MB_MEDIA_100TX,
   5343 		TULIP_ROM_MB_MEDIA_100TX_FDX,
   5344 	};
   5345 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   5346 
   5347 	sc->sc_mii.mii_ifp = ifp;
   5348 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
   5349 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
   5350 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   5351 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   5352 	    tlp_mediastatus);
   5353 	if (sc->sc_chip == TULIP_CHIP_MX98713 ||
   5354 	    sc->sc_chip == TULIP_CHIP_MX98713A) {
   5355 		mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
   5356 		    MII_PHY_ANY, MII_OFFSET_ANY, 0);
   5357 		if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL) {
   5358 			sc->sc_flags |= TULIPF_HAS_MII;
   5359 			sc->sc_tick = tlp_mii_tick;
   5360 			sc->sc_preinit = tlp_2114x_mii_preinit;
   5361 			sc->sc_mediasw = &tlp_pmac_mii_mediasw;
   5362 			ifmedia_set(&sc->sc_mii.mii_media,
   5363 			    IFM_ETHER|IFM_AUTO);
   5364 			return;
   5365 		}
   5366 	}
   5367 
   5368 	switch (sc->sc_chip) {
   5369 	case TULIP_CHIP_MX98713:
   5370 		tlp_add_srom_media(sc, TULIP_ROM_MB_21140_GPR,
   5371 		    tlp_21140_gpio_get, tlp_21140_gpio_set, media, 4);
   5372 
   5373 		/*
   5374 		 * XXX Should implement auto-sense for this someday,
   5375 		 * XXX when we do the same for the 21140.
   5376 		 */
   5377 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   5378 		break;
   5379 
   5380 	default:
   5381 		tlp_add_srom_media(sc, TULIP_ROM_MB_21142_SIA,
   5382 		    tlp_sia_get, tlp_sia_set, media, 2);
   5383 		tlp_add_srom_media(sc, TULIP_ROM_MB_21143_SYM,
   5384 		    tlp_sia_get, tlp_sia_set, media + 2, 2);
   5385 
   5386 		/*
   5387 		 * XXX Autonegotiation not yet supported.
   5388 		 */
   5389 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   5390 		break;
   5391 	}
   5392 
   5393 	tlp_print_media(sc);
   5394 	tlp_sia_fixup(sc);
   5395 
   5396 	/* Set the LED modes. */
   5397 	tlp_pmac_reset(sc);
   5398 
   5399 	sc->sc_reset = tlp_pmac_reset;
   5400 }
   5401 
   5402 /*
   5403  * ADMtek AL981 media switch.  Only has internal PHY.
   5404  */
   5405 void	tlp_al981_tmsw_init __P((struct tulip_softc *));
   5406 
   5407 const struct tulip_mediasw tlp_al981_mediasw = {
   5408 	tlp_al981_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   5409 };
   5410 
   5411 void
   5412 tlp_al981_tmsw_init(sc)
   5413 	struct tulip_softc *sc;
   5414 {
   5415 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   5416 
   5417 	sc->sc_mii.mii_ifp = ifp;
   5418 	sc->sc_mii.mii_readreg = tlp_al981_mii_readreg;
   5419 	sc->sc_mii.mii_writereg = tlp_al981_mii_writereg;
   5420 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   5421 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   5422 	    tlp_mediastatus);
   5423 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   5424 	    MII_OFFSET_ANY, 0);
   5425 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   5426 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   5427 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   5428 	} else {
   5429 		sc->sc_flags |= TULIPF_HAS_MII;
   5430 		sc->sc_tick = tlp_mii_tick;
   5431 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   5432 	}
   5433 }
   5434