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