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