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