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