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