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