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