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