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