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