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