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