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