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