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