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