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