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