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