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