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