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