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