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