Home | History | Annotate | Line # | Download | only in ic
tulip.c revision 1.200
      1 /*	$NetBSD: tulip.c,v 1.200 2019/11/10 21:16:35 chs 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.200 2019/11/10 21:16:35 chs 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 	/* Delete all remaining media. */
    623 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
    624 
    625 	rnd_detach_source(&sc->sc_rnd_source);
    626 
    627 	ether_ifdetach(ifp);
    628 	if_detach(ifp);
    629 
    630 	for (i = 0; i < TULIP_NRXDESC; i++) {
    631 		rxs = &sc->sc_rxsoft[i];
    632 		if (rxs->rxs_mbuf != NULL) {
    633 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
    634 			m_freem(rxs->rxs_mbuf);
    635 			rxs->rxs_mbuf = NULL;
    636 		}
    637 		bus_dmamap_destroy(sc->sc_dmat, rxs->rxs_dmamap);
    638 	}
    639 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
    640 		txs = &sc->sc_txsoft[i];
    641 		if (txs->txs_mbuf != NULL) {
    642 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
    643 			m_freem(txs->txs_mbuf);
    644 			txs->txs_mbuf = NULL;
    645 		}
    646 		bus_dmamap_destroy(sc->sc_dmat, txs->txs_dmamap);
    647 	}
    648 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    649 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    650 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
    651 	    sizeof(struct tulip_control_data));
    652 	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
    653 
    654 	pmf_device_deregister(self);
    655 
    656 	if (sc->sc_srom)
    657 		free(sc->sc_srom, M_DEVBUF);
    658 
    659 	return 0;
    660 }
    661 
    662 /*
    663  * tlp_start:		[ifnet interface function]
    664  *
    665  *	Start packet transmission on the interface.
    666  */
    667 static void
    668 tlp_start(struct ifnet *ifp)
    669 {
    670 	struct tulip_softc *sc = ifp->if_softc;
    671 	struct mbuf *m0, *m;
    672 	struct tulip_txsoft *txs, *last_txs = NULL;
    673 	bus_dmamap_t dmamap;
    674 	int error, firsttx, nexttx, lasttx = 1, ofree, seg;
    675 	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 		ifp->if_oerrors++;
    944 	} else if (doing_transmit) {
    945 		printf("%s: transmit timeout\n", device_xname(sc->sc_dev));
    946 		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 			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 			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 			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 			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 
   1495 		if (txstat & (TDSTAT_Tx_UF | TDSTAT_Tx_TO))
   1496 			ifp->if_oerrors++;
   1497 
   1498 		if (txstat & TDSTAT_Tx_EC)
   1499 			ifp->if_collisions += 16;
   1500 		else
   1501 			ifp->if_collisions += TDSTAT_Tx_COLLISIONS(txstat);
   1502 		if (txstat & TDSTAT_Tx_LC)
   1503 			ifp->if_collisions++;
   1504 
   1505 		ifp->if_opackets++;
   1506 	}
   1507 
   1508 	/*
   1509 	 * If there are no more pending transmissions, cancel the watchdog
   1510 	 * timer.
   1511 	 */
   1512 	if (txs == NULL && (sc->sc_flags & TULIPF_DOING_SETUP) == 0)
   1513 		ifp->if_timer = 0;
   1514 
   1515 	/*
   1516 	 * If we have a receive filter setup pending, do it now.
   1517 	 */
   1518 	if (sc->sc_flags & TULIPF_WANT_SETUP)
   1519 		(*sc->sc_filter_setup)(sc);
   1520 }
   1521 
   1522 #ifdef TLP_STATS
   1523 void
   1524 tlp_print_stats(struct tulip_softc *sc)
   1525 {
   1526 
   1527 	printf("%s: tx_uf %lu, tx_to %lu, tx_ec %lu, tx_lc %lu\n",
   1528 	    device_xname(sc->sc_dev),
   1529 	    sc->sc_stats.ts_tx_uf, sc->sc_stats.ts_tx_to,
   1530 	    sc->sc_stats.ts_tx_ec, sc->sc_stats.ts_tx_lc);
   1531 }
   1532 #endif
   1533 
   1534 /*
   1535  * tlp_reset:
   1536  *
   1537  *	Perform a soft reset on the Tulip.
   1538  */
   1539 void
   1540 tlp_reset(struct tulip_softc *sc)
   1541 {
   1542 	int i;
   1543 
   1544 	TULIP_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR);
   1545 
   1546 	/*
   1547 	 * Xircom, ASIX and Conexant clones don't bring themselves
   1548 	 * out of reset automatically.
   1549 	 * Instead, we have to wait at least 50 PCI cycles, and then
   1550 	 * clear SWR.
   1551 	 */
   1552 	switch (sc->sc_chip) {
   1553 		case TULIP_CHIP_X3201_3:
   1554 		case TULIP_CHIP_AX88140:
   1555 		case TULIP_CHIP_AX88141:
   1556 		case TULIP_CHIP_RS7112:
   1557 			delay(10);
   1558 			TULIP_WRITE(sc, CSR_BUSMODE, 0);
   1559 			break;
   1560 		default:
   1561 			break;
   1562 	}
   1563 
   1564 	for (i = 0; i < 1000; i++) {
   1565 		/*
   1566 		 * Wait at least 50 PCI cycles for the reset to
   1567 		 * complete before peeking at the Tulip again.
   1568 		 * 10 uSec is a bit longer than 50 PCI cycles
   1569 		 * (at 33MHz), but it doesn't hurt have the extra
   1570 		 * wait.
   1571 		 */
   1572 		delay(10);
   1573 		if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0)
   1574 			break;
   1575 	}
   1576 
   1577 	if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR))
   1578 		aprint_error_dev(sc->sc_dev, "reset failed to complete\n");
   1579 
   1580 	delay(1000);
   1581 
   1582 	/*
   1583 	 * If the board has any GPIO reset sequences to issue, do them now.
   1584 	 */
   1585 	if (sc->sc_reset != NULL)
   1586 		(*sc->sc_reset)(sc);
   1587 }
   1588 
   1589 /*
   1590  * tlp_init:		[ ifnet interface function ]
   1591  *
   1592  *	Initialize the interface.  Must be called at splnet().
   1593  */
   1594 static int
   1595 tlp_init(struct ifnet *ifp)
   1596 {
   1597 	struct tulip_softc *sc = ifp->if_softc;
   1598 	struct tulip_txsoft *txs;
   1599 	struct tulip_rxsoft *rxs;
   1600 	int i, error = 0;
   1601 
   1602 	if ((error = tlp_enable(sc)) != 0)
   1603 		goto out;
   1604 
   1605 	/*
   1606 	 * Cancel any pending I/O.
   1607 	 */
   1608 	tlp_stop(ifp, 0);
   1609 
   1610 	/*
   1611 	 * Initialize `opmode' to 0, and call the pre-init routine, if
   1612 	 * any.  This is required because the 2114x and some of the
   1613 	 * clones require that the media-related bits in `opmode' be
   1614 	 * set before performing a soft-reset in order to get internal
   1615 	 * chip pathways are correct.  Yay!
   1616 	 */
   1617 	sc->sc_opmode = 0;
   1618 	if (sc->sc_preinit != NULL)
   1619 		(*sc->sc_preinit)(sc);
   1620 
   1621 	/*
   1622 	 * Reset the Tulip to a known state.
   1623 	 */
   1624 	tlp_reset(sc);
   1625 
   1626 	/*
   1627 	 * Initialize the BUSMODE register.
   1628 	 */
   1629 	sc->sc_busmode = BUSMODE_BAR;
   1630 	switch (sc->sc_chip) {
   1631 	case TULIP_CHIP_21140:
   1632 	case TULIP_CHIP_21140A:
   1633 	case TULIP_CHIP_21142:
   1634 	case TULIP_CHIP_21143:
   1635 	case TULIP_CHIP_82C115:
   1636 	case TULIP_CHIP_MX98725:
   1637 		/*
   1638 		 * If we're allowed to do so, use Memory Read Line
   1639 		 * and Memory Read Multiple.
   1640 		 *
   1641 		 * XXX Should we use Memory Write and Invalidate?
   1642 		 */
   1643 		if (sc->sc_flags & TULIPF_MRL)
   1644 			sc->sc_busmode |= BUSMODE_RLE;
   1645 		if (sc->sc_flags & TULIPF_MRM)
   1646 			sc->sc_busmode |= BUSMODE_RME;
   1647 #if 0
   1648 		if (sc->sc_flags & TULIPF_MWI)
   1649 			sc->sc_busmode |= BUSMODE_WLE;
   1650 #endif
   1651 		break;
   1652 
   1653 	case TULIP_CHIP_82C168:
   1654 	case TULIP_CHIP_82C169:
   1655 		sc->sc_busmode |= BUSMODE_PNIC_MBO;
   1656 		if (sc->sc_maxburst == 0)
   1657 			sc->sc_maxburst = 16;
   1658 		break;
   1659 
   1660 	case TULIP_CHIP_AX88140:
   1661 	case TULIP_CHIP_AX88141:
   1662 		if (sc->sc_maxburst == 0)
   1663 			sc->sc_maxburst = 16;
   1664 		break;
   1665 
   1666 	default:
   1667 		/* Nothing. */
   1668 		break;
   1669 	}
   1670 	switch (sc->sc_cacheline) {
   1671 	default:
   1672 		/*
   1673 		 * Note: We must *always* set these bits; a cache
   1674 		 * alignment of 0 is RESERVED.
   1675 		 */
   1676 	case 8:
   1677 		sc->sc_busmode |= BUSMODE_CAL_8LW;
   1678 		break;
   1679 	case 16:
   1680 		sc->sc_busmode |= BUSMODE_CAL_16LW;
   1681 		break;
   1682 	case 32:
   1683 		sc->sc_busmode |= BUSMODE_CAL_32LW;
   1684 		break;
   1685 	}
   1686 	switch (sc->sc_maxburst) {
   1687 	case 1:
   1688 		sc->sc_busmode |= BUSMODE_PBL_1LW;
   1689 		break;
   1690 	case 2:
   1691 		sc->sc_busmode |= BUSMODE_PBL_2LW;
   1692 		break;
   1693 	case 4:
   1694 		sc->sc_busmode |= BUSMODE_PBL_4LW;
   1695 		break;
   1696 	case 8:
   1697 		sc->sc_busmode |= BUSMODE_PBL_8LW;
   1698 		break;
   1699 	case 16:
   1700 		sc->sc_busmode |= BUSMODE_PBL_16LW;
   1701 		break;
   1702 	case 32:
   1703 		sc->sc_busmode |= BUSMODE_PBL_32LW;
   1704 		break;
   1705 	default:
   1706 		sc->sc_busmode |= BUSMODE_PBL_DEFAULT;
   1707 		break;
   1708 	}
   1709 #if BYTE_ORDER == BIG_ENDIAN
   1710 	/*
   1711 	 * Can't use BUSMODE_BLE or BUSMODE_DBO; not all chips
   1712 	 * support them, and even on ones that do, it doesn't
   1713 	 * always work.  So we always access descriptors with
   1714 	 * little endian via htole32/le32toh.
   1715 	 */
   1716 #endif
   1717 	/*
   1718 	 * Big-endian bus requires BUSMODE_BLE anyway.
   1719 	 * Also, BUSMODE_DBO is needed because we assume
   1720 	 * descriptors are little endian.
   1721 	 */
   1722 	if (sc->sc_flags & TULIPF_BLE)
   1723 		sc->sc_busmode |= BUSMODE_BLE;
   1724 	if (sc->sc_flags & TULIPF_DBO)
   1725 		sc->sc_busmode |= BUSMODE_DBO;
   1726 
   1727 	/*
   1728 	 * Some chips have a broken bus interface.
   1729 	 */
   1730 	switch (sc->sc_chip) {
   1731 	case TULIP_CHIP_DM9102:
   1732 	case TULIP_CHIP_DM9102A:
   1733 		sc->sc_busmode = 0;
   1734 		break;
   1735 
   1736 	default:
   1737 		/* Nothing. */
   1738 		break;
   1739 	}
   1740 
   1741 	TULIP_WRITE(sc, CSR_BUSMODE, sc->sc_busmode);
   1742 
   1743 	/*
   1744 	 * Initialize the OPMODE register.  We don't write it until
   1745 	 * we're ready to begin the transmit and receive processes.
   1746 	 *
   1747 	 * Media-related OPMODE bits are set in the media callbacks
   1748 	 * for each specific chip/board.
   1749 	 */
   1750 	sc->sc_opmode |= OPMODE_SR | OPMODE_ST |
   1751 	    sc->sc_txth[sc->sc_txthresh].txth_opmode;
   1752 
   1753 	/*
   1754 	 * Magical mystery initialization on the Macronix chips.
   1755 	 * The MX98713 uses its own magic value, the rest share
   1756 	 * a common one.
   1757 	 */
   1758 	switch (sc->sc_chip) {
   1759 	case TULIP_CHIP_MX98713:
   1760 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98713);
   1761 		break;
   1762 
   1763 	case TULIP_CHIP_MX98713A:
   1764 	case TULIP_CHIP_MX98715:
   1765 	case TULIP_CHIP_MX98715A:
   1766 	case TULIP_CHIP_MX98715AEC_X:
   1767 	case TULIP_CHIP_MX98725:
   1768 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98715);
   1769 		break;
   1770 
   1771 	default:
   1772 		/* Nothing. */
   1773 		break;
   1774 	}
   1775 
   1776 	/*
   1777 	 * Initialize the transmit descriptor ring.
   1778 	 */
   1779 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
   1780 	for (i = 0; i < TULIP_NTXDESC; i++) {
   1781 		struct tulip_desc *txd = &sc->sc_txdescs[i];
   1782 		txd->td_ctl = htole32(sc->sc_tdctl_ch);
   1783 		txd->td_bufaddr2 = htole32(TULIP_CDTXADDR(sc, TULIP_NEXTTX(i)));
   1784 	}
   1785 	sc->sc_txdescs[TULIP_NTXDESC - 1].td_ctl |= htole32(sc->sc_tdctl_er);
   1786 	TULIP_CDTXSYNC(sc, 0, TULIP_NTXDESC,
   1787 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1788 	sc->sc_txfree = TULIP_NTXDESC;
   1789 	sc->sc_txnext = 0;
   1790 
   1791 	/*
   1792 	 * Initialize the transmit job descriptors.
   1793 	 */
   1794 	SIMPLEQ_INIT(&sc->sc_txfreeq);
   1795 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
   1796 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
   1797 		txs = &sc->sc_txsoft[i];
   1798 		txs->txs_mbuf = NULL;
   1799 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1800 	}
   1801 
   1802 	/*
   1803 	 * Initialize the receive descriptor and receive job
   1804 	 * descriptor rings.
   1805 	 */
   1806 	for (i = 0; i < TULIP_NRXDESC; i++) {
   1807 		rxs = &sc->sc_rxsoft[i];
   1808 		if (rxs->rxs_mbuf == NULL) {
   1809 			if ((error = tlp_add_rxbuf(sc, i)) != 0) {
   1810 				aprint_error_dev(sc->sc_dev,
   1811 				    "unable to allocate or map rx "
   1812 				    "buffer %d, error = %d\n", i, error);
   1813 				/*
   1814 				 * XXX Should attempt to run with fewer receive
   1815 				 * XXX buffers instead of just failing.
   1816 				 */
   1817 				tlp_rxdrain(sc);
   1818 				goto out;
   1819 			}
   1820 		} else
   1821 			TULIP_INIT_RXDESC(sc, i);
   1822 	}
   1823 	sc->sc_rxptr = 0;
   1824 
   1825 	/*
   1826 	 * Initialize the interrupt mask and enable interrupts.
   1827 	 */
   1828 	/* normal interrupts */
   1829 	sc->sc_inten = STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
   1830 
   1831 	/* abnormal interrupts */
   1832 	sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
   1833 	    STATUS_RU | STATUS_RPS | STATUS_RWT | STATUS_SE | STATUS_AIS;
   1834 
   1835 	sc->sc_rxint_mask = STATUS_RI | STATUS_RU | STATUS_RWT;
   1836 	sc->sc_txint_mask = STATUS_TI | STATUS_UNF | STATUS_TJT;
   1837 
   1838 	switch (sc->sc_chip) {
   1839 	case TULIP_CHIP_WB89C840F:
   1840 		/*
   1841 		 * Clear bits that we don't want that happen to
   1842 		 * overlap or don't exist.
   1843 		 */
   1844 		sc->sc_inten &= ~(STATUS_WINB_REI | STATUS_RWT);
   1845 		break;
   1846 
   1847 	default:
   1848 		/* Nothing. */
   1849 		break;
   1850 	}
   1851 
   1852 	sc->sc_rxint_mask &= sc->sc_inten;
   1853 	sc->sc_txint_mask &= sc->sc_inten;
   1854 
   1855 	TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
   1856 	TULIP_WRITE(sc, CSR_STATUS, 0xffffffff);
   1857 
   1858 	/*
   1859 	 * Give the transmit and receive rings to the Tulip.
   1860 	 */
   1861 	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDTXADDR(sc, sc->sc_txnext));
   1862 	TULIP_WRITE(sc, CSR_RXLIST, TULIP_CDRXADDR(sc, sc->sc_rxptr));
   1863 
   1864 	/*
   1865 	 * On chips that do this differently, set the station address.
   1866 	 */
   1867 	switch (sc->sc_chip) {
   1868 	case TULIP_CHIP_WB89C840F:
   1869 	    {
   1870 		/* XXX Do this with stream writes? */
   1871 		bus_addr_t cpa = TULIP_CSR_OFFSET(sc, CSR_WINB_CPA0);
   1872 
   1873 		for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1874 			bus_space_write_1(sc->sc_st, sc->sc_sh,
   1875 			    cpa + i, CLLADDR(ifp->if_sadl)[i]);
   1876 		}
   1877 		break;
   1878 	    }
   1879 
   1880 	case TULIP_CHIP_AL981:
   1881 	case TULIP_CHIP_AN983:
   1882 	case TULIP_CHIP_AN985:
   1883 	    {
   1884 		uint32_t reg;
   1885 		const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
   1886 
   1887 		reg = enaddr[0] |
   1888 		      (enaddr[1] << 8) |
   1889 		      (enaddr[2] << 16) |
   1890 		      (enaddr[3] << 24);
   1891 		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR0, reg);
   1892 
   1893 		reg = enaddr[4] |
   1894 		      (enaddr[5] << 8);
   1895 		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR1, reg);
   1896 		break;
   1897 	    }
   1898 
   1899 	case TULIP_CHIP_AX88140:
   1900 	case TULIP_CHIP_AX88141:
   1901 	    {
   1902 		uint32_t reg;
   1903 		const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
   1904 
   1905 		reg = enaddr[0] |
   1906 		      (enaddr[1] << 8) |
   1907 		      (enaddr[2] << 16) |
   1908 		      (enaddr[3] << 24);
   1909 		TULIP_WRITE(sc, CSR_AX_FILTIDX, AX_FILTIDX_PAR0);
   1910 		TULIP_WRITE(sc, CSR_AX_FILTDATA, reg);
   1911 
   1912 		reg = enaddr[4] | (enaddr[5] << 8);
   1913 		TULIP_WRITE(sc, CSR_AX_FILTIDX, AX_FILTIDX_PAR1);
   1914 		TULIP_WRITE(sc, CSR_AX_FILTDATA, reg);
   1915 		break;
   1916 	    }
   1917 
   1918 	default:
   1919 		/* Nothing. */
   1920 		break;
   1921 	}
   1922 
   1923 	/*
   1924 	 * Set the receive filter.  This will start the transmit and
   1925 	 * receive processes.
   1926 	 */
   1927 	(*sc->sc_filter_setup)(sc);
   1928 
   1929 	/*
   1930 	 * Set the current media.
   1931 	 */
   1932 	(void)(*sc->sc_mediasw->tmsw_set)(sc);
   1933 
   1934 	/*
   1935 	 * Start the receive process.
   1936 	 */
   1937 	TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
   1938 
   1939 	if (sc->sc_tick != NULL) {
   1940 		/* Start the one second clock. */
   1941 		callout_reset(&sc->sc_tick_callout, hz >> 3, sc->sc_tick, sc);
   1942 	}
   1943 
   1944 	/*
   1945 	 * Note that the interface is now running.
   1946 	 */
   1947 	ifp->if_flags |= IFF_RUNNING;
   1948 	ifp->if_flags &= ~IFF_OACTIVE;
   1949 	sc->sc_if_flags = ifp->if_flags;
   1950 
   1951  out:
   1952 	if (error) {
   1953 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1954 		ifp->if_timer = 0;
   1955 		printf("%s: interface not running\n", device_xname(sc->sc_dev));
   1956 	}
   1957 	return error;
   1958 }
   1959 
   1960 /*
   1961  * tlp_enable:
   1962  *
   1963  *	Enable the Tulip chip.
   1964  */
   1965 static int
   1966 tlp_enable(struct tulip_softc *sc)
   1967 {
   1968 
   1969 	if (TULIP_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
   1970 		if ((*sc->sc_enable)(sc) != 0) {
   1971 			aprint_error_dev(sc->sc_dev, "device enable failed\n");
   1972 			return EIO;
   1973 		}
   1974 		sc->sc_flags |= TULIPF_ENABLED;
   1975 	}
   1976 	return 0;
   1977 }
   1978 
   1979 /*
   1980  * tlp_disable:
   1981  *
   1982  *	Disable the Tulip chip.
   1983  */
   1984 static void
   1985 tlp_disable(struct tulip_softc *sc)
   1986 {
   1987 
   1988 	if (TULIP_IS_ENABLED(sc) && sc->sc_disable != NULL) {
   1989 		(*sc->sc_disable)(sc);
   1990 		sc->sc_flags &= ~TULIPF_ENABLED;
   1991 	}
   1992 }
   1993 
   1994 /*
   1995  * tlp_rxdrain:
   1996  *
   1997  *	Drain the receive queue.
   1998  */
   1999 static void
   2000 tlp_rxdrain(struct tulip_softc *sc)
   2001 {
   2002 	struct tulip_rxsoft *rxs;
   2003 	int i;
   2004 
   2005 	for (i = 0; i < TULIP_NRXDESC; i++) {
   2006 		rxs = &sc->sc_rxsoft[i];
   2007 		if (rxs->rxs_mbuf != NULL) {
   2008 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   2009 			m_freem(rxs->rxs_mbuf);
   2010 			rxs->rxs_mbuf = NULL;
   2011 		}
   2012 	}
   2013 }
   2014 
   2015 /*
   2016  * tlp_stop:		[ ifnet interface function ]
   2017  *
   2018  *	Stop transmission on the interface.
   2019  */
   2020 static void
   2021 tlp_stop(struct ifnet *ifp, int disable)
   2022 {
   2023 	struct tulip_softc *sc = ifp->if_softc;
   2024 	struct tulip_txsoft *txs;
   2025 
   2026 	if (sc->sc_tick != NULL) {
   2027 		/* Stop the one second clock. */
   2028 		callout_stop(&sc->sc_tick_callout);
   2029 	}
   2030 
   2031 	if (sc->sc_flags & TULIPF_HAS_MII) {
   2032 		/* Down the MII. */
   2033 		mii_down(&sc->sc_mii);
   2034 	}
   2035 
   2036 	/* Disable interrupts. */
   2037 	TULIP_WRITE(sc, CSR_INTEN, 0);
   2038 
   2039 	/* Stop the transmit and receive processes. */
   2040 	sc->sc_opmode = 0;
   2041 	TULIP_WRITE(sc, CSR_OPMODE, 0);
   2042 	TULIP_WRITE(sc, CSR_RXLIST, 0);
   2043 	TULIP_WRITE(sc, CSR_TXLIST, 0);
   2044 
   2045 	/*
   2046 	 * Release any queued transmit buffers.
   2047 	 */
   2048 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   2049 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
   2050 		if (txs->txs_mbuf != NULL) {
   2051 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   2052 			m_freem(txs->txs_mbuf);
   2053 			txs->txs_mbuf = NULL;
   2054 		}
   2055 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   2056 	}
   2057 
   2058 	sc->sc_flags &= ~(TULIPF_WANT_SETUP | TULIPF_DOING_SETUP);
   2059 
   2060 	/*
   2061 	 * Mark the interface down and cancel the watchdog timer.
   2062 	 */
   2063 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2064 	sc->sc_if_flags = ifp->if_flags;
   2065 	ifp->if_timer = 0;
   2066 
   2067 	/*
   2068 	 * Reset the chip (needed on some flavors to actually disable it).
   2069 	 */
   2070 	tlp_reset(sc);
   2071 
   2072 	if (disable) {
   2073 		tlp_rxdrain(sc);
   2074 		tlp_disable(sc);
   2075 	}
   2076 }
   2077 
   2078 #define	SROM_EMIT(sc, x)						\
   2079 do {									\
   2080 	TULIP_WRITE((sc), CSR_MIIROM, (x));				\
   2081 	delay(2);							\
   2082 } while (0)
   2083 
   2084 /*
   2085  * tlp_srom_idle:
   2086  *
   2087  *	Put the SROM in idle state.
   2088  */
   2089 static void
   2090 tlp_srom_idle(struct tulip_softc *sc)
   2091 {
   2092 	uint32_t miirom;
   2093 	int i;
   2094 
   2095 	miirom = MIIROM_SR;
   2096 	SROM_EMIT(sc, miirom);
   2097 
   2098 	miirom |= MIIROM_RD;
   2099 	SROM_EMIT(sc, miirom);
   2100 
   2101 	miirom |= MIIROM_SROMCS;
   2102 	SROM_EMIT(sc, miirom);
   2103 
   2104 	SROM_EMIT(sc, miirom | MIIROM_SROMSK);
   2105 
   2106 	/* Strobe the clock 32 times. */
   2107 	for (i = 0; i < 32; i++) {
   2108 		SROM_EMIT(sc, miirom);
   2109 		SROM_EMIT(sc, miirom | MIIROM_SROMSK);
   2110 	}
   2111 
   2112 	SROM_EMIT(sc, miirom);
   2113 
   2114 	miirom &= ~MIIROM_SROMCS;
   2115 	SROM_EMIT(sc, miirom);
   2116 
   2117 	SROM_EMIT(sc, 0);
   2118 }
   2119 
   2120 /*
   2121  * tlp_srom_size:
   2122  *
   2123  *	Determine the number of address bits in the SROM.
   2124  */
   2125 static int
   2126 tlp_srom_size(struct tulip_softc *sc)
   2127 {
   2128 	uint32_t miirom;
   2129 	int x;
   2130 
   2131 	/* Select the SROM. */
   2132 	miirom = MIIROM_SR;
   2133 	SROM_EMIT(sc, miirom);
   2134 
   2135 	miirom |= MIIROM_RD;
   2136 	SROM_EMIT(sc, miirom);
   2137 
   2138 	/* Send CHIP SELECT for one clock tick. */
   2139 	miirom |= MIIROM_SROMCS;
   2140 	SROM_EMIT(sc, miirom);
   2141 
   2142 	/* Shift in the READ opcode. */
   2143 	for (x = 3; x > 0; x--) {
   2144 		if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
   2145 			miirom |= MIIROM_SROMDI;
   2146 		else
   2147 			miirom &= ~MIIROM_SROMDI;
   2148 		SROM_EMIT(sc, miirom);
   2149 		SROM_EMIT(sc, miirom | MIIROM_SROMSK);
   2150 		SROM_EMIT(sc, miirom);
   2151 	}
   2152 
   2153 	/* Shift in address and look for dummy 0 bit. */
   2154 	for (x = 1; x <= 12; x++) {
   2155 		miirom &= ~MIIROM_SROMDI;
   2156 		SROM_EMIT(sc, miirom);
   2157 		SROM_EMIT(sc, miirom | MIIROM_SROMSK);
   2158 		if (!TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
   2159 			break;
   2160 		SROM_EMIT(sc, miirom);
   2161 	}
   2162 
   2163 	/* Clear CHIP SELECT. */
   2164 	miirom &= ~MIIROM_SROMCS;
   2165 	SROM_EMIT(sc, miirom);
   2166 
   2167 	/* Deselect the SROM. */
   2168 	SROM_EMIT(sc, 0);
   2169 
   2170 	if (x < 4 || x > 12) {
   2171 		aprint_debug_dev(sc->sc_dev, "broken MicroWire interface "
   2172 		    "detected; setting SROM size to 1Kb\n");
   2173 		return 6;
   2174 	} else {
   2175 		if (tlp_srom_debug)
   2176 			printf("%s: SROM size is 2^%d*16 bits (%d bytes)\n",
   2177 			    device_xname(sc->sc_dev), x, (1 << (x + 4)) >> 3);
   2178 		return x;
   2179 	}
   2180 }
   2181 
   2182 /*
   2183  * tlp_read_srom:
   2184  *
   2185  *	Read the Tulip SROM.
   2186  */
   2187 int
   2188 tlp_read_srom(struct tulip_softc *sc)
   2189 {
   2190 	int size;
   2191 	uint32_t miirom;
   2192 	uint16_t datain;
   2193 	int i, x;
   2194 
   2195 	tlp_srom_idle(sc);
   2196 
   2197 	sc->sc_srom_addrbits = tlp_srom_size(sc);
   2198 	if (sc->sc_srom_addrbits == 0)
   2199 		return 0;
   2200 	size = TULIP_ROM_SIZE(sc->sc_srom_addrbits);
   2201 	sc->sc_srom = malloc(size, M_DEVBUF, M_WAITOK);
   2202 
   2203 	/* Select the SROM. */
   2204 	miirom = MIIROM_SR;
   2205 	SROM_EMIT(sc, miirom);
   2206 
   2207 	miirom |= MIIROM_RD;
   2208 	SROM_EMIT(sc, miirom);
   2209 
   2210 	for (i = 0; i < size; i += 2) {
   2211 		/* Send CHIP SELECT for one clock tick. */
   2212 		miirom |= MIIROM_SROMCS;
   2213 		SROM_EMIT(sc, miirom);
   2214 
   2215 		/* Shift in the READ opcode. */
   2216 		for (x = 3; x > 0; x--) {
   2217 			if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
   2218 				miirom |= MIIROM_SROMDI;
   2219 			else
   2220 				miirom &= ~MIIROM_SROMDI;
   2221 			SROM_EMIT(sc, miirom);
   2222 			SROM_EMIT(sc, miirom | MIIROM_SROMSK);
   2223 			SROM_EMIT(sc, miirom);
   2224 		}
   2225 
   2226 		/* Shift in address. */
   2227 		for (x = sc->sc_srom_addrbits; x > 0; x--) {
   2228 			if (i & (1 << x))
   2229 				miirom |= MIIROM_SROMDI;
   2230 			else
   2231 				miirom &= ~MIIROM_SROMDI;
   2232 			SROM_EMIT(sc, miirom);
   2233 			SROM_EMIT(sc, miirom | MIIROM_SROMSK);
   2234 			SROM_EMIT(sc, miirom);
   2235 		}
   2236 
   2237 		/* Shift out data. */
   2238 		miirom &= ~MIIROM_SROMDI;
   2239 		datain = 0;
   2240 		for (x = 16; x > 0; x--) {
   2241 			SROM_EMIT(sc, miirom | MIIROM_SROMSK);
   2242 			if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
   2243 				datain |= (1 << (x - 1));
   2244 			SROM_EMIT(sc, miirom);
   2245 		}
   2246 		sc->sc_srom[i] = datain & 0xff;
   2247 		sc->sc_srom[i + 1] = datain >> 8;
   2248 
   2249 		/* Clear CHIP SELECT. */
   2250 		miirom &= ~MIIROM_SROMCS;
   2251 		SROM_EMIT(sc, miirom);
   2252 	}
   2253 
   2254 	/* Deselect the SROM. */
   2255 	SROM_EMIT(sc, 0);
   2256 
   2257 	/* ...and idle it. */
   2258 	tlp_srom_idle(sc);
   2259 
   2260 	if (tlp_srom_debug) {
   2261 		printf("SROM CONTENTS:");
   2262 		for (i = 0; i < size; i++) {
   2263 			if ((i % 8) == 0)
   2264 				printf("\n\t");
   2265 			printf("0x%02x ", sc->sc_srom[i]);
   2266 		}
   2267 		printf("\n");
   2268 	}
   2269 
   2270 	return 1;
   2271 }
   2272 
   2273 #undef SROM_EMIT
   2274 
   2275 /*
   2276  * tlp_add_rxbuf:
   2277  *
   2278  *	Add a receive buffer to the indicated descriptor.
   2279  */
   2280 static int
   2281 tlp_add_rxbuf(struct tulip_softc *sc, int idx)
   2282 {
   2283 	struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx];
   2284 	struct mbuf *m;
   2285 	int error;
   2286 
   2287 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2288 	if (m == NULL)
   2289 		return ENOBUFS;
   2290 
   2291 	MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
   2292 	MCLGET(m, M_DONTWAIT);
   2293 	if ((m->m_flags & M_EXT) == 0) {
   2294 		m_freem(m);
   2295 		return ENOBUFS;
   2296 	}
   2297 
   2298 	if (rxs->rxs_mbuf != NULL)
   2299 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   2300 
   2301 	rxs->rxs_mbuf = m;
   2302 
   2303 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
   2304 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   2305 	    BUS_DMA_READ | BUS_DMA_NOWAIT);
   2306 	if (error) {
   2307 		aprint_error_dev(sc->sc_dev,
   2308 		    "can't load rx DMA map %d, error = %d\n", idx, error);
   2309 		panic("tlp_add_rxbuf");	/* XXX */
   2310 	}
   2311 
   2312 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   2313 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   2314 
   2315 	TULIP_INIT_RXDESC(sc, idx);
   2316 
   2317 	return 0;
   2318 }
   2319 
   2320 /*
   2321  * tlp_srom_crcok:
   2322  *
   2323  *	Check the CRC of the Tulip SROM.
   2324  */
   2325 int
   2326 tlp_srom_crcok(const uint8_t *romdata)
   2327 {
   2328 	uint32_t crc;
   2329 
   2330 	crc = ether_crc32_le(romdata, TULIP_ROM_CRC32_CHECKSUM);
   2331 	crc = (crc & 0xffff) ^ 0xffff;
   2332 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM))
   2333 		return 1;
   2334 
   2335 	/*
   2336 	 * Try an alternate checksum.
   2337 	 */
   2338 	crc = ether_crc32_le(romdata, TULIP_ROM_CRC32_CHECKSUM1);
   2339 	crc = (crc & 0xffff) ^ 0xffff;
   2340 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM1))
   2341 		return 1;
   2342 
   2343 	return 0;
   2344 }
   2345 
   2346 /*
   2347  * tlp_isv_srom:
   2348  *
   2349  *	Check to see if the SROM is in the new standardized format.
   2350  */
   2351 int
   2352 tlp_isv_srom(const uint8_t *romdata)
   2353 {
   2354 	int i;
   2355 	uint16_t cksum;
   2356 
   2357 	if (tlp_srom_crcok(romdata)) {
   2358 		/*
   2359 		 * SROM CRC checks out; must be in the new format.
   2360 		 */
   2361 		return 1;
   2362 	}
   2363 
   2364 	cksum = TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM);
   2365 	if (cksum == 0xffff || cksum == 0) {
   2366 		/*
   2367 		 * No checksum present.  Check the SROM ID; 18 bytes of 0
   2368 		 * followed by 1 (version) followed by the number of
   2369 		 * adapters which use this SROM (should be non-zero).
   2370 		 */
   2371 		for (i = 0; i < TULIP_ROM_SROM_FORMAT_VERION; i++) {
   2372 			if (romdata[i] != 0)
   2373 				return 0;
   2374 		}
   2375 		if (romdata[TULIP_ROM_SROM_FORMAT_VERION] != 1)
   2376 			return 0;
   2377 		if (romdata[TULIP_ROM_CHIP_COUNT] == 0)
   2378 			return 0;
   2379 		return 1;
   2380 	}
   2381 
   2382 	return 0;
   2383 }
   2384 
   2385 /*
   2386  * tlp_isv_srom_enaddr:
   2387  *
   2388  *	Get the Ethernet address from an ISV SROM.
   2389  */
   2390 int
   2391 tlp_isv_srom_enaddr(struct tulip_softc *sc, uint8_t *enaddr)
   2392 {
   2393 	int i, devcnt;
   2394 
   2395 	if (tlp_isv_srom(sc->sc_srom) == 0)
   2396 		return 0;
   2397 
   2398 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   2399 	for (i = 0; i < devcnt; i++) {
   2400 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   2401 			break;
   2402 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   2403 		    sc->sc_devno)
   2404 			break;
   2405 	}
   2406 
   2407 	if (i == devcnt)
   2408 		return 0;
   2409 
   2410 	memcpy(enaddr, &sc->sc_srom[TULIP_ROM_IEEE_NETWORK_ADDRESS],
   2411 	    ETHER_ADDR_LEN);
   2412 	enaddr[5] += i;
   2413 
   2414 	return 1;
   2415 }
   2416 
   2417 /*
   2418  * tlp_parse_old_srom:
   2419  *
   2420  *	Parse old-format SROMs.
   2421  *
   2422  *	This routine is largely lifted from Matt Thomas's `de' driver.
   2423  */
   2424 int
   2425 tlp_parse_old_srom(struct tulip_softc *sc, uint8_t *enaddr)
   2426 {
   2427 	static const uint8_t testpat[] =
   2428 	    { 0xff, 0, 0x55, 0xaa, 0xff, 0, 0x55, 0xaa };
   2429 	int i;
   2430 	uint32_t cksum;
   2431 
   2432 	if (memcmp(&sc->sc_srom[0], &sc->sc_srom[16], 8) != 0) {
   2433 		/*
   2434 		 * Phobos G100 interfaces have the address at
   2435 		 * offsets 0 and 20, but each pair of bytes is
   2436 		 * swapped.
   2437 		 */
   2438 		if (sc->sc_srom_addrbits == 6 &&
   2439 		    sc->sc_srom[1] == 0x00 &&
   2440 		    sc->sc_srom[0] == 0x60 &&
   2441 		    sc->sc_srom[3] == 0xf5 &&
   2442 		    memcmp(&sc->sc_srom[0], &sc->sc_srom[20], 6) == 0) {
   2443 			for (i = 0; i < 6; i += 2) {
   2444 				enaddr[i] = sc->sc_srom[i + 1];
   2445 				enaddr[i + 1] = sc->sc_srom[i];
   2446 			}
   2447 			return 1;
   2448 		}
   2449 
   2450 		/*
   2451 		 * Phobos G130/G160 interfaces have the address at
   2452 		 * offsets 20 and 84, but each pair of bytes is
   2453 		 * swapped.
   2454 		 */
   2455 		if (sc->sc_srom_addrbits == 6 &&
   2456 		    sc->sc_srom[21] == 0x00 &&
   2457 		    sc->sc_srom[20] == 0x60 &&
   2458 		    sc->sc_srom[23] == 0xf5 &&
   2459 		    memcmp(&sc->sc_srom[20], &sc->sc_srom[84], 6) == 0) {
   2460 			for (i = 0; i < 6; i += 2) {
   2461 				enaddr[i] = sc->sc_srom[20 + i + 1];
   2462 				enaddr[i + 1] = sc->sc_srom[20 + i];
   2463 			}
   2464 			return 1;
   2465 		}
   2466 
   2467 		/*
   2468 		 * Cobalt Networks interfaces simply have the address
   2469 		 * in the first six bytes. The rest is zeroed out
   2470 		 * on some models, but others contain unknown data.
   2471 		 */
   2472 		if (sc->sc_srom[0] == 0x00 &&
   2473 		    sc->sc_srom[1] == 0x10 &&
   2474 		    sc->sc_srom[2] == 0xe0) {
   2475 			memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   2476 			return 1;
   2477 		}
   2478 
   2479 		/*
   2480 		 * Some vendors (e.g. ZNYX) don't use the standard
   2481 		 * DEC Address ROM format, but rather just have an
   2482 		 * Ethernet address in the first 6 bytes, maybe a
   2483 		 * 2 byte checksum, and then all 0xff's.
   2484 		 */
   2485 		for (i = 8; i < 32; i++) {
   2486 			if (sc->sc_srom[i] != 0xff &&
   2487 			    sc->sc_srom[i] != 0)
   2488 				return 0;
   2489 		}
   2490 
   2491 		/*
   2492 		 * Sanity check the Ethernet address:
   2493 		 *
   2494 		 *	- Make sure it's not multicast or locally
   2495 		 *	  assigned
   2496 		 *	- Make sure it has a non-0 OUI
   2497 		 */
   2498 		if (sc->sc_srom[0] & 3)
   2499 			return 0;
   2500 		if (sc->sc_srom[0] == 0 && sc->sc_srom[1] == 0 &&
   2501 		    sc->sc_srom[2] == 0)
   2502 			return 0;
   2503 
   2504 		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   2505 		return 1;
   2506 	}
   2507 
   2508 	/*
   2509 	 * Standard DEC Address ROM test.
   2510 	 */
   2511 
   2512 	if (memcmp(&sc->sc_srom[24], testpat, 8) != 0)
   2513 		return 0;
   2514 
   2515 	for (i = 0; i < 8; i++) {
   2516 		if (sc->sc_srom[i] != sc->sc_srom[15 - i])
   2517 			return 0;
   2518 	}
   2519 
   2520 	memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   2521 
   2522 	cksum = *(uint16_t *) &enaddr[0];
   2523 
   2524 	cksum <<= 1;
   2525 	if (cksum > 0xffff)
   2526 		cksum -= 0xffff;
   2527 
   2528 	cksum += *(uint16_t *) &enaddr[2];
   2529 	if (cksum > 0xffff)
   2530 		cksum -= 0xffff;
   2531 
   2532 	cksum <<= 1;
   2533 	if (cksum > 0xffff)
   2534 		cksum -= 0xffff;
   2535 
   2536 	cksum += *(uint16_t *) &enaddr[4];
   2537 	if (cksum >= 0xffff)
   2538 		cksum -= 0xffff;
   2539 
   2540 	if (cksum != *(uint16_t *) &sc->sc_srom[6])
   2541 		return 0;
   2542 
   2543 	return 1;
   2544 }
   2545 
   2546 /*
   2547  * tlp_filter_setup:
   2548  *
   2549  *	Set the Tulip's receive filter.
   2550  */
   2551 static void
   2552 tlp_filter_setup(struct tulip_softc *sc)
   2553 {
   2554 	struct ethercom *ec = &sc->sc_ethercom;
   2555 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2556 	struct ether_multi *enm;
   2557 	struct ether_multistep step;
   2558 	volatile uint32_t *sp;
   2559 	struct tulip_txsoft *txs;
   2560 	struct tulip_desc *txd;
   2561 	uint8_t enaddr[ETHER_ADDR_LEN];
   2562 	uint32_t hash, hashsize;
   2563 	int cnt, nexttx;
   2564 
   2565 	DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n",
   2566 	    device_xname(sc->sc_dev), sc->sc_flags));
   2567 
   2568 	memcpy(enaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   2569 
   2570 	/*
   2571 	 * If there are transmissions pending, wait until they have
   2572 	 * completed.
   2573 	 */
   2574 	if (! SIMPLEQ_EMPTY(&sc->sc_txdirtyq) ||
   2575 	    (sc->sc_flags & TULIPF_DOING_SETUP) != 0) {
   2576 		sc->sc_flags |= TULIPF_WANT_SETUP;
   2577 		DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n",
   2578 		    device_xname(sc->sc_dev)));
   2579 		return;
   2580 	}
   2581 	sc->sc_flags &= ~TULIPF_WANT_SETUP;
   2582 
   2583 	switch (sc->sc_chip) {
   2584 	case TULIP_CHIP_82C115:
   2585 		hashsize = TULIP_PNICII_HASHSIZE;
   2586 		break;
   2587 
   2588 	default:
   2589 		hashsize = TULIP_MCHASHSIZE;
   2590 	}
   2591 
   2592 	/*
   2593 	 * If we're running, idle the transmit and receive engines.  If
   2594 	 * we're NOT running, we're being called from tlp_init(), and our
   2595 	 * writing OPMODE will start the transmit and receive processes
   2596 	 * in motion.
   2597 	 */
   2598 	if (ifp->if_flags & IFF_RUNNING)
   2599 		tlp_idle(sc, OPMODE_ST | OPMODE_SR);
   2600 
   2601 	sc->sc_opmode &= ~(OPMODE_PR | OPMODE_PM);
   2602 
   2603 	if (ifp->if_flags & IFF_PROMISC) {
   2604 		sc->sc_opmode |= OPMODE_PR;
   2605 		goto allmulti;
   2606 	}
   2607 
   2608 	/*
   2609 	 * Try Perfect filtering first.
   2610 	 */
   2611 
   2612 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   2613 	sp = TULIP_CDSP(sc);
   2614 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2615 	cnt = 0;
   2616 	ETHER_LOCK(ec);
   2617 	ETHER_FIRST_MULTI(step, ec, enm);
   2618 	while (enm != NULL) {
   2619 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2620 			/*
   2621 			 * We must listen to a range of multicast addresses.
   2622 			 * For now, just accept all multicasts, rather than
   2623 			 * trying to set only those filter bits needed to match
   2624 			 * the range.  (At this time, the only use of address
   2625 			 * ranges is for IP multicast routing, for which the
   2626 			 * range is big enough to require all bits set.)
   2627 			 */
   2628 			ETHER_UNLOCK(ec);
   2629 			goto allmulti;
   2630 		}
   2631 		if (cnt == (TULIP_MAXADDRS - 2)) {
   2632 			/*
   2633 			 * We already have our multicast limit (still need
   2634 			 * our station address and broadcast).  Go to
   2635 			 * Hash-Perfect mode.
   2636 			 */
   2637 			ETHER_UNLOCK(ec);
   2638 			goto hashperfect;
   2639 		}
   2640 		cnt++;
   2641 		*sp++ = htole32(TULIP_SP_FIELD(enm->enm_addrlo, 0));
   2642 		*sp++ = htole32(TULIP_SP_FIELD(enm->enm_addrlo, 1));
   2643 		*sp++ = htole32(TULIP_SP_FIELD(enm->enm_addrlo, 2));
   2644 		ETHER_NEXT_MULTI(step, enm);
   2645 	}
   2646 	ETHER_UNLOCK(ec);
   2647 
   2648 	if (ifp->if_flags & IFF_BROADCAST) {
   2649 		/* ...and the broadcast address. */
   2650 		cnt++;
   2651 		*sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff));
   2652 		*sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff));
   2653 		*sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff));
   2654 	}
   2655 
   2656 	/* Pad the rest with our station address. */
   2657 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   2658 		*sp++ = htole32(TULIP_SP_FIELD(enaddr, 0));
   2659 		*sp++ = htole32(TULIP_SP_FIELD(enaddr, 1));
   2660 		*sp++ = htole32(TULIP_SP_FIELD(enaddr, 2));
   2661 	}
   2662 	ifp->if_flags &= ~IFF_ALLMULTI;
   2663 	goto setit;
   2664 
   2665  hashperfect:
   2666 	/*
   2667 	 * Try Hash-Perfect mode.
   2668 	 */
   2669 
   2670 	/*
   2671 	 * Some 21140 chips have broken Hash-Perfect modes.  On these
   2672 	 * chips, we simply use Hash-Only mode, and put our station
   2673 	 * address into the filter.
   2674 	 */
   2675 	if (sc->sc_chip == TULIP_CHIP_21140)
   2676 		sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY;
   2677 	else
   2678 		sc->sc_filtmode = TDCTL_Tx_FT_HASH;
   2679 	sp = TULIP_CDSP(sc);
   2680 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2681 	ETHER_LOCK(ec);
   2682 	ETHER_FIRST_MULTI(step, ec, enm);
   2683 	while (enm != NULL) {
   2684 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2685 			/*
   2686 			 * We must listen to a range of multicast addresses.
   2687 			 * For now, just accept all multicasts, rather than
   2688 			 * trying to set only those filter bits needed to match
   2689 			 * the range.  (At this time, the only use of address
   2690 			 * ranges is for IP multicast routing, for which the
   2691 			 * range is big enough to require all bits set.)
   2692 			 */
   2693 			ETHER_UNLOCK(ec);
   2694 			goto allmulti;
   2695 		}
   2696 		hash = tlp_mchash(enm->enm_addrlo, hashsize);
   2697 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2698 		ETHER_NEXT_MULTI(step, enm);
   2699 	}
   2700 	ETHER_UNLOCK(ec);
   2701 
   2702 	if (ifp->if_flags & IFF_BROADCAST) {
   2703 		/* ...and the broadcast address. */
   2704 		hash = tlp_mchash(etherbroadcastaddr, hashsize);
   2705 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2706 	}
   2707 
   2708 	if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
   2709 		/* ...and our station address. */
   2710 		hash = tlp_mchash(enaddr, hashsize);
   2711 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2712 	} else {
   2713 		/*
   2714 		 * Hash-Perfect mode; put our station address after
   2715 		 * the hash table.
   2716 		 */
   2717 		sp[39] = htole32(TULIP_SP_FIELD(enaddr, 0));
   2718 		sp[40] = htole32(TULIP_SP_FIELD(enaddr, 1));
   2719 		sp[41] = htole32(TULIP_SP_FIELD(enaddr, 2));
   2720 	}
   2721 	ifp->if_flags &= ~IFF_ALLMULTI;
   2722 	goto setit;
   2723 
   2724  allmulti:
   2725 	/*
   2726 	 * Use Perfect filter mode.  First address is the broadcast address,
   2727 	 * and pad the rest with our station address.  We'll set Pass-all-
   2728 	 * multicast in OPMODE below.
   2729 	 */
   2730 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   2731 	sp = TULIP_CDSP(sc);
   2732 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2733 	cnt = 0;
   2734 	if (ifp->if_flags & IFF_BROADCAST) {
   2735 		cnt++;
   2736 		*sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff));
   2737 		*sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff));
   2738 		*sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff));
   2739 	}
   2740 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   2741 		*sp++ = htole32(TULIP_SP_FIELD(enaddr, 0));
   2742 		*sp++ = htole32(TULIP_SP_FIELD(enaddr, 1));
   2743 		*sp++ = htole32(TULIP_SP_FIELD(enaddr, 2));
   2744 	}
   2745 	ifp->if_flags |= IFF_ALLMULTI;
   2746 
   2747  setit:
   2748 	if (ifp->if_flags & IFF_ALLMULTI)
   2749 		sc->sc_opmode |= OPMODE_PM;
   2750 
   2751 	/* Sync the setup packet buffer. */
   2752 	TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE);
   2753 
   2754 	/*
   2755 	 * Fill in the setup packet descriptor.
   2756 	 */
   2757 	txs = SIMPLEQ_FIRST(&sc->sc_txfreeq);
   2758 
   2759 	txs->txs_firstdesc = sc->sc_txnext;
   2760 	txs->txs_lastdesc = sc->sc_txnext;
   2761 	txs->txs_ndescs = 1;
   2762 	txs->txs_mbuf = NULL;
   2763 
   2764 	nexttx = sc->sc_txnext;
   2765 	txd = &sc->sc_txdescs[nexttx];
   2766 	txd->td_status = 0;
   2767 	txd->td_bufaddr1 = htole32(TULIP_CDSPADDR(sc));
   2768 	txd->td_ctl = htole32((TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) |
   2769 	    sc->sc_filtmode | TDCTL_Tx_SET | sc->sc_setup_fsls |
   2770 	    TDCTL_Tx_IC | sc->sc_tdctl_ch |
   2771 	    (nexttx == (TULIP_NTXDESC - 1) ? sc->sc_tdctl_er : 0));
   2772 	TULIP_CDTXSYNC(sc, nexttx, 1,
   2773 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2774 
   2775 #ifdef TLP_DEBUG
   2776 	if (ifp->if_flags & IFF_DEBUG) {
   2777 		printf("     filter_setup %p transmit chain:\n", txs);
   2778 		printf("     descriptor %d:\n", nexttx);
   2779 		printf("       td_status:   0x%08x\n", le32toh(txd->td_status));
   2780 		printf("       td_ctl:      0x%08x\n", le32toh(txd->td_ctl));
   2781 		printf("       td_bufaddr1: 0x%08x\n",
   2782 		    le32toh(txd->td_bufaddr1));
   2783 		printf("       td_bufaddr2: 0x%08x\n",
   2784 		    le32toh(txd->td_bufaddr2));
   2785 	}
   2786 #endif
   2787 
   2788 	txd->td_status = htole32(TDSTAT_OWN);
   2789 	TULIP_CDTXSYNC(sc, nexttx, 1,
   2790 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2791 
   2792 	/* Advance the tx pointer. */
   2793 	sc->sc_txfree -= 1;
   2794 	sc->sc_txnext = TULIP_NEXTTX(nexttx);
   2795 
   2796 	SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
   2797 	SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
   2798 
   2799 	/*
   2800 	 * Set the OPMODE register.  This will also resume the
   2801 	 * transmit process we idled above.
   2802 	 */
   2803 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2804 
   2805 	sc->sc_flags |= TULIPF_DOING_SETUP;
   2806 
   2807 	/*
   2808 	 * Kick the transmitter; this will cause the Tulip to
   2809 	 * read the setup descriptor.
   2810 	 */
   2811 	/* XXX USE AUTOPOLLING? */
   2812 	TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
   2813 
   2814 	/* Set up a watchdog timer in case the chip flakes out. */
   2815 	ifp->if_timer = 5;
   2816 
   2817 	DPRINTF(sc, ("%s: tlp_filter_setup: returning\n",
   2818 		device_xname(sc->sc_dev)));
   2819 }
   2820 
   2821 /*
   2822  * tlp_winb_filter_setup:
   2823  *
   2824  *	Set the Winbond 89C840F's receive filter.
   2825  */
   2826 static void
   2827 tlp_winb_filter_setup(struct tulip_softc *sc)
   2828 {
   2829 	struct ethercom *ec = &sc->sc_ethercom;
   2830 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2831 	struct ether_multi *enm;
   2832 	struct ether_multistep step;
   2833 	uint32_t hash, mchash[2];
   2834 
   2835 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n",
   2836 	    device_xname(sc->sc_dev), sc->sc_flags));
   2837 
   2838 	sc->sc_opmode &= ~(OPMODE_WINB_APP | OPMODE_WINB_AMP |OPMODE_WINB_ABP);
   2839 
   2840 	if (ifp->if_flags & IFF_MULTICAST)
   2841 		sc->sc_opmode |= OPMODE_WINB_AMP;
   2842 
   2843 	if (ifp->if_flags & IFF_BROADCAST)
   2844 		sc->sc_opmode |= OPMODE_WINB_ABP;
   2845 
   2846 	if (ifp->if_flags & IFF_PROMISC) {
   2847 		sc->sc_opmode |= OPMODE_WINB_APP;
   2848 		goto allmulti;
   2849 	}
   2850 
   2851 	mchash[0] = mchash[1] = 0;
   2852 
   2853 	ETHER_FIRST_MULTI(step, ec, enm);
   2854 	while (enm != NULL) {
   2855 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2856 			/*
   2857 			 * We must listen to a range of multicast addresses.
   2858 			 * For now, just accept all multicasts, rather than
   2859 			 * trying to set only those filter bits needed to match
   2860 			 * the range.  (At this time, the only use of address
   2861 			 * ranges is for IP multicast routing, for which the
   2862 			 * range is big enough to require all bits set.)
   2863 			 */
   2864 			goto allmulti;
   2865 		}
   2866 
   2867 		/*
   2868 		 * According to the FreeBSD `wb' driver, yes, you
   2869 		 * really do invert the hash.
   2870 		 */
   2871 		hash =
   2872 		    (~(ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26))
   2873 		    & 0x3f;
   2874 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
   2875 		ETHER_NEXT_MULTI(step, enm);
   2876 	}
   2877 	ifp->if_flags &= ~IFF_ALLMULTI;
   2878 	goto setit;
   2879 
   2880  allmulti:
   2881 	ifp->if_flags |= IFF_ALLMULTI;
   2882 	mchash[0] = mchash[1] = 0xffffffff;
   2883 
   2884  setit:
   2885 	TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]);
   2886 	TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]);
   2887 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2888 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n",
   2889 	    device_xname(sc->sc_dev)));
   2890 }
   2891 
   2892 /*
   2893  * tlp_al981_filter_setup:
   2894  *
   2895  *	Set the ADMtek AL981's receive filter.
   2896  */
   2897 static void
   2898 tlp_al981_filter_setup(struct tulip_softc *sc)
   2899 {
   2900 	struct ethercom *ec = &sc->sc_ethercom;
   2901 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2902 	struct ether_multi *enm;
   2903 	struct ether_multistep step;
   2904 	uint32_t hash, mchash[2];
   2905 
   2906 	/*
   2907 	 * If the chip is running, we need to reset the interface,
   2908 	 * and will revisit here (with IFF_RUNNING) clear.  The
   2909 	 * chip seems to really not like to have its multicast
   2910 	 * filter programmed without a reset.
   2911 	 */
   2912 	if (ifp->if_flags & IFF_RUNNING) {
   2913 		(void) tlp_init(ifp);
   2914 		return;
   2915 	}
   2916 
   2917 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: sc_flags 0x%08x\n",
   2918 	    device_xname(sc->sc_dev), sc->sc_flags));
   2919 
   2920 	sc->sc_opmode &= ~(OPMODE_PR | OPMODE_PM);
   2921 
   2922 	if (ifp->if_flags & IFF_PROMISC) {
   2923 		sc->sc_opmode |= OPMODE_PR;
   2924 		goto allmulti;
   2925 	}
   2926 
   2927 	mchash[0] = mchash[1] = 0;
   2928 
   2929 	ETHER_LOCK(ec);
   2930 	ETHER_FIRST_MULTI(step, ec, enm);
   2931 	while (enm != NULL) {
   2932 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2933 			/*
   2934 			 * We must listen to a range of multicast addresses.
   2935 			 * For now, just accept all multicasts, rather than
   2936 			 * trying to set only those filter bits needed to match
   2937 			 * the range.  (At this time, the only use of address
   2938 			 * ranges is for IP multicast routing, for which the
   2939 			 * range is big enough to require all bits set.)
   2940 			 */
   2941 			ETHER_UNLOCK(ec);
   2942 			goto allmulti;
   2943 		}
   2944 
   2945 		hash = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN) & 0x3f;
   2946 		mchash[hash >> 5] |= __BIT(hash & 0x1f);
   2947 		ETHER_NEXT_MULTI(step, enm);
   2948 	}
   2949 	ETHER_UNLOCK(ec);
   2950 	ifp->if_flags &= ~IFF_ALLMULTI;
   2951 	goto setit;
   2952 
   2953  allmulti:
   2954 	ifp->if_flags |= IFF_ALLMULTI;
   2955 	mchash[0] = mchash[1] = 0xffffffff;
   2956 
   2957  setit:
   2958 	bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_MAR0, mchash[0]);
   2959 	bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_MAR1, mchash[1]);
   2960 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2961 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: returning\n",
   2962 	    device_xname(sc->sc_dev)));
   2963 }
   2964 
   2965 /*
   2966  * tlp_asix_filter_setup:
   2967  *
   2968  * 	Set the ASIX AX8814x receive filter.
   2969  */
   2970 static void
   2971 tlp_asix_filter_setup(struct tulip_softc *sc)
   2972 {
   2973 	struct ethercom *ec = &sc->sc_ethercom;
   2974 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2975 	struct ether_multi *enm;
   2976 	struct ether_multistep step;
   2977 	uint32_t hash, mchash[2];
   2978 
   2979 	DPRINTF(sc, ("%s: tlp_asix_filter_setup: sc_flags 0x%08x\n",
   2980 		device_xname(sc->sc_dev), sc->sc_flags));
   2981 
   2982 	sc->sc_opmode &= ~(OPMODE_PM | OPMODE_AX_RB | OPMODE_PR);
   2983 
   2984 	if (ifp->if_flags & IFF_MULTICAST)
   2985 		sc->sc_opmode |= OPMODE_PM;
   2986 
   2987 	if (ifp->if_flags & IFF_BROADCAST)
   2988 		sc->sc_opmode |= OPMODE_AX_RB;
   2989 
   2990 	if (ifp->if_flags & IFF_PROMISC) {
   2991 		sc->sc_opmode |= OPMODE_PR;
   2992 		goto allmulti;
   2993 	}
   2994 
   2995 	mchash[0] = mchash[1] = 0;
   2996 
   2997 	ETHER_LOCK(ec);
   2998 	ETHER_FIRST_MULTI(step, ec, enm);
   2999 	while (enm != NULL) {
   3000 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   3001 			/*
   3002 			 * We must listen to a range of multicast addresses.
   3003 			 * For now, just accept all multicasts, rather than
   3004 			 * trying to set only those filter bits needed to match
   3005 			 * the range.  (At this time, the only use of address
   3006 			 * ranges is for IP multicast routing, for which the
   3007 			 * range is big enough to require all bits set.)
   3008 			 */
   3009 			ETHER_UNLOCK(ec);
   3010 			goto allmulti;
   3011 		}
   3012 		hash = (ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26)
   3013 		       & 0x3f;
   3014 		if (hash < 32)
   3015 			mchash[0] |= (1 << hash);
   3016 		else
   3017 			mchash[1] |= (1 << (hash - 32));
   3018 		ETHER_NEXT_MULTI(step, enm);
   3019 	}
   3020 	ETHER_UNLOCK(ec);
   3021 	ifp->if_flags &= ~IFF_ALLMULTI;
   3022 	goto setit;
   3023 
   3024 allmulti:
   3025 	ifp->if_flags |= IFF_ALLMULTI;
   3026 	mchash[0] = mchash[1] = 0xffffffff;
   3027 
   3028 setit:
   3029 	TULIP_WRITE(sc, CSR_AX_FILTIDX, AX_FILTIDX_MAR0);
   3030 	TULIP_WRITE(sc, CSR_AX_FILTDATA, mchash[0]);
   3031 	TULIP_WRITE(sc, CSR_AX_FILTIDX, AX_FILTIDX_MAR1);
   3032 	TULIP_WRITE(sc, CSR_AX_FILTDATA, mchash[1]);
   3033 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3034 	DPRINTF(sc, ("%s: tlp_asix_filter_setup: returning\n",
   3035 		device_xname(sc->sc_dev)));
   3036 }
   3037 
   3038 
   3039 /*
   3040  * tlp_idle:
   3041  *
   3042  *	Cause the transmit and/or receive processes to go idle.
   3043  */
   3044 void
   3045 tlp_idle(struct tulip_softc *sc, uint32_t bits)
   3046 {
   3047 	static const char * const tlp_tx_state_names[] = {
   3048 		"STOPPED",
   3049 		"RUNNING - FETCH",
   3050 		"RUNNING - WAIT",
   3051 		"RUNNING - READING",
   3052 		"-- RESERVED --",
   3053 		"RUNNING - SETUP",
   3054 		"SUSPENDED",
   3055 		"RUNNING - CLOSE",
   3056 	};
   3057 	static const char * const tlp_rx_state_names[] = {
   3058 		"STOPPED",
   3059 		"RUNNING - FETCH",
   3060 		"RUNNING - CHECK",
   3061 		"RUNNING - WAIT",
   3062 		"SUSPENDED",
   3063 		"RUNNING - CLOSE",
   3064 		"RUNNING - FLUSH",
   3065 		"RUNNING - QUEUE",
   3066 	};
   3067 	static const char * const dm9102_tx_state_names[] = {
   3068 		"STOPPED",
   3069 		"RUNNING - FETCH",
   3070 		"RUNNING - SETUP",
   3071 		"RUNNING - READING",
   3072 		"RUNNING - CLOSE - CLEAR OWNER",
   3073 		"RUNNING - WAIT",
   3074 		"RUNNING - CLOSE - WRITE STATUS",
   3075 		"SUSPENDED",
   3076 	};
   3077 	static const char * const dm9102_rx_state_names[] = {
   3078 		"STOPPED",
   3079 		"RUNNING - FETCH",
   3080 		"RUNNING - WAIT",
   3081 		"RUNNING - QUEUE",
   3082 		"RUNNING - CLOSE - CLEAR OWNER",
   3083 		"RUNNING - CLOSE - WRITE STATUS",
   3084 		"SUSPENDED",
   3085 		"RUNNING - FLUSH",
   3086 	};
   3087 
   3088 	const char * const *tx_state_names, * const *rx_state_names;
   3089 	uint32_t csr, ackmask = 0;
   3090 	int i;
   3091 
   3092 	switch (sc->sc_chip) {
   3093 	case TULIP_CHIP_DM9102:
   3094 	case TULIP_CHIP_DM9102A:
   3095 		tx_state_names = dm9102_tx_state_names;
   3096 		rx_state_names = dm9102_rx_state_names;
   3097 		break;
   3098 
   3099 	default:
   3100 		tx_state_names = tlp_tx_state_names;
   3101 		rx_state_names = tlp_rx_state_names;
   3102 		break;
   3103 	}
   3104 
   3105 	if (bits & OPMODE_ST)
   3106 		ackmask |= STATUS_TPS;
   3107 
   3108 	if (bits & OPMODE_SR)
   3109 		ackmask |= STATUS_RPS;
   3110 
   3111 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits);
   3112 
   3113 	for (i = 0; i < 1000; i++) {
   3114 		if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
   3115 			break;
   3116 		delay(10);
   3117 	}
   3118 
   3119 	csr = TULIP_READ(sc, CSR_STATUS);
   3120 	if ((csr & ackmask) != ackmask) {
   3121 		if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
   3122 		    (csr & STATUS_TS) != STATUS_TS_STOPPED) {
   3123 			switch (sc->sc_chip) {
   3124 			case TULIP_CHIP_AX88140:
   3125 			case TULIP_CHIP_AX88141:
   3126 				/*
   3127 				 * Filter the message out on noisy chips.
   3128 				 */
   3129 				break;
   3130 			default:
   3131 				printf("%s: transmit process failed to idle: "
   3132 				    "state %s\n", device_xname(sc->sc_dev),
   3133 				    tx_state_names[(csr & STATUS_TS) >> 20]);
   3134 			}
   3135 		}
   3136 		if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
   3137 		    (csr & STATUS_RS) != STATUS_RS_STOPPED) {
   3138 			switch (sc->sc_chip) {
   3139 			case TULIP_CHIP_AN983:
   3140 			case TULIP_CHIP_AN985:
   3141 			case TULIP_CHIP_DM9102A:
   3142 			case TULIP_CHIP_RS7112:
   3143 				/*
   3144 				 * Filter the message out on noisy chips.
   3145 				 */
   3146 				break;
   3147 			default:
   3148 				printf("%s: receive process failed to idle: "
   3149 				    "state %s\n", device_xname(sc->sc_dev),
   3150 				    rx_state_names[(csr & STATUS_RS) >> 17]);
   3151 			}
   3152 		}
   3153 	}
   3154 	TULIP_WRITE(sc, CSR_STATUS, ackmask);
   3155 }
   3156 
   3157 /*****************************************************************************
   3158  * Generic media support functions.
   3159  *****************************************************************************/
   3160 
   3161 /*
   3162  * tlp_mediastatus:	[ifmedia interface function]
   3163  *
   3164  *	Query the current media.
   3165  */
   3166 void
   3167 tlp_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   3168 {
   3169 	struct tulip_softc *sc = ifp->if_softc;
   3170 
   3171 	if (TULIP_IS_ENABLED(sc) == 0) {
   3172 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
   3173 		ifmr->ifm_status = 0;
   3174 		return;
   3175 	}
   3176 
   3177 	(*sc->sc_mediasw->tmsw_get)(sc, ifmr);
   3178 }
   3179 
   3180 /*
   3181  * tlp_mediachange:	[ifmedia interface function]
   3182  *
   3183  *	Update the current media.
   3184  */
   3185 int
   3186 tlp_mediachange(struct ifnet *ifp)
   3187 {
   3188 	struct tulip_softc *sc = ifp->if_softc;
   3189 
   3190 	if ((ifp->if_flags & IFF_UP) == 0)
   3191 		return 0;
   3192 	return (*sc->sc_mediasw->tmsw_set)(sc);
   3193 }
   3194 
   3195 /*****************************************************************************
   3196  * Support functions for MII-attached media.
   3197  *****************************************************************************/
   3198 
   3199 /*
   3200  * tlp_mii_tick:
   3201  *
   3202  *	One second timer, used to tick the MII.
   3203  */
   3204 static void
   3205 tlp_mii_tick(void *arg)
   3206 {
   3207 	struct tulip_softc *sc = arg;
   3208 	int s;
   3209 
   3210 	if (!device_is_active(sc->sc_dev))
   3211 		return;
   3212 
   3213 	s = splnet();
   3214 	mii_tick(&sc->sc_mii);
   3215 	splx(s);
   3216 
   3217 	callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc);
   3218 }
   3219 
   3220 /*
   3221  * tlp_mii_statchg:	[mii interface function]
   3222  *
   3223  *	Callback from PHY when media changes.
   3224  */
   3225 static void
   3226 tlp_mii_statchg(struct ifnet *ifp)
   3227 {
   3228 	struct tulip_softc *sc = ifp->if_softc;
   3229 
   3230 	/* Idle the transmit and receive processes. */
   3231 	tlp_idle(sc, OPMODE_ST | OPMODE_SR);
   3232 
   3233 	sc->sc_opmode &= ~(OPMODE_TTM | OPMODE_FD | OPMODE_HBD);
   3234 
   3235 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
   3236 		sc->sc_opmode |= OPMODE_TTM;
   3237 	else
   3238 		sc->sc_opmode |= OPMODE_HBD;
   3239 
   3240 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   3241 		sc->sc_opmode |= OPMODE_FD | OPMODE_HBD;
   3242 
   3243 	/*
   3244 	 * Write new OPMODE bits.  This also restarts the transmit
   3245 	 * and receive processes.
   3246 	 */
   3247 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3248 }
   3249 
   3250 /*
   3251  * tlp_winb_mii_statchg: [mii interface function]
   3252  *
   3253  *	Callback from PHY when media changes.  This version is
   3254  *	for the Winbond 89C840F, which has different OPMODE bits.
   3255  */
   3256 static void
   3257 tlp_winb_mii_statchg(struct ifnet *ifp)
   3258 {
   3259 	struct tulip_softc *sc = ifp->if_softc;
   3260 
   3261 	/* Idle the transmit and receive processes. */
   3262 	tlp_idle(sc, OPMODE_ST | OPMODE_SR);
   3263 
   3264 	sc->sc_opmode &= ~(OPMODE_WINB_FES | OPMODE_FD);
   3265 
   3266 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX)
   3267 		sc->sc_opmode |= OPMODE_WINB_FES;
   3268 
   3269 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   3270 		sc->sc_opmode |= OPMODE_FD;
   3271 
   3272 	/*
   3273 	 * Write new OPMODE bits.  This also restarts the transmit
   3274 	 * and receive processes.
   3275 	 */
   3276 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3277 }
   3278 
   3279 /*
   3280  * tlp_dm9102_mii_statchg: [mii interface function]
   3281  *
   3282  *	Callback from PHY when media changes.  This version is
   3283  *	for the DM9102.
   3284  */
   3285 static void
   3286 tlp_dm9102_mii_statchg(struct ifnet *ifp)
   3287 {
   3288 	struct tulip_softc *sc = ifp->if_softc;
   3289 
   3290 	/*
   3291 	 * Don't idle the transmit and receive processes, here.  It
   3292 	 * seems to fail, and just causes excess noise.
   3293 	 */
   3294 	sc->sc_opmode &= ~(OPMODE_TTM | OPMODE_FD);
   3295 
   3296 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) != IFM_100_TX)
   3297 		sc->sc_opmode |= OPMODE_TTM;
   3298 
   3299 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   3300 		sc->sc_opmode |= OPMODE_FD;
   3301 
   3302 	/*
   3303 	 * Write new OPMODE bits.
   3304 	 */
   3305 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3306 }
   3307 
   3308 /*
   3309  * tlp_mii_getmedia:
   3310  *
   3311  *	Callback from ifmedia to request current media status.
   3312  */
   3313 static void
   3314 tlp_mii_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr)
   3315 {
   3316 	struct mii_data * const mii = &sc->sc_mii;
   3317 
   3318 	mii_pollstat(mii);
   3319 	ifmr->ifm_status = mii->mii_media_status;
   3320 	ifmr->ifm_active = mii->mii_media_active;
   3321 }
   3322 
   3323 /*
   3324  * tlp_mii_setmedia:
   3325  *
   3326  *	Callback from ifmedia to request new media setting.
   3327  */
   3328 static int
   3329 tlp_mii_setmedia(struct tulip_softc *sc)
   3330 {
   3331 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   3332 	int rc;
   3333 
   3334 	if ((ifp->if_flags & IFF_UP) == 0)
   3335 		return 0;
   3336 	switch (sc->sc_chip) {
   3337 	case TULIP_CHIP_21142:
   3338 	case TULIP_CHIP_21143:
   3339 		/* Disable the internal Nway engine. */
   3340 		TULIP_WRITE(sc, CSR_SIATXRX, 0);
   3341 		break;
   3342 
   3343 	default:
   3344 		/* Nothing. */
   3345 		break;
   3346 	}
   3347 	if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
   3348 		return 0;
   3349 	return rc;
   3350 }
   3351 
   3352 /*
   3353  * tlp_bitbang_mii_readreg:
   3354  *
   3355  *	Read a PHY register via bit-bang'ing the MII.
   3356  */
   3357 static int
   3358 tlp_bitbang_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
   3359 {
   3360 	struct tulip_softc *sc = device_private(self);
   3361 
   3362 	return mii_bitbang_readreg(self, sc->sc_bitbang_ops, phy, reg, val);
   3363 }
   3364 
   3365 /*
   3366  * tlp_bitbang_mii_writereg:
   3367  *
   3368  *	Write a PHY register via bit-bang'ing the MII.
   3369  */
   3370 static int
   3371 tlp_bitbang_mii_writereg(device_t self, int phy, int reg, uint16_t val)
   3372 {
   3373 	struct tulip_softc *sc = device_private(self);
   3374 
   3375 	return mii_bitbang_writereg(self, sc->sc_bitbang_ops, phy, reg, val);
   3376 }
   3377 
   3378 /*
   3379  * tlp_sio_mii_bitbang_read:
   3380  *
   3381  *	Read the MII serial port for the MII bit-bang module.
   3382  */
   3383 static uint32_t
   3384 tlp_sio_mii_bitbang_read(device_t self)
   3385 {
   3386 	struct tulip_softc *sc = device_private(self);
   3387 
   3388 	return TULIP_READ(sc, CSR_MIIROM);
   3389 }
   3390 
   3391 /*
   3392  * tlp_sio_mii_bitbang_write:
   3393  *
   3394  *	Write the MII serial port for the MII bit-bang module.
   3395  */
   3396 static void
   3397 tlp_sio_mii_bitbang_write(device_t self, uint32_t val)
   3398 {
   3399 	struct tulip_softc *sc = device_private(self);
   3400 
   3401 	TULIP_WRITE(sc, CSR_MIIROM, val);
   3402 }
   3403 
   3404 /*
   3405  * tlp_pnic_mii_readreg:
   3406  *
   3407  *	Read a PHY register on the Lite-On PNIC.
   3408  */
   3409 static int
   3410 tlp_pnic_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
   3411 {
   3412 	struct tulip_softc *sc = device_private(self);
   3413 	uint32_t data;
   3414 	int i;
   3415 
   3416 	TULIP_WRITE(sc, CSR_PNIC_MII,
   3417 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   3418 	    PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
   3419 	    (reg << PNIC_MII_REGSHIFT));
   3420 
   3421 	for (i = 0; i < 1000; i++) {
   3422 		delay(10);
   3423 		data = TULIP_READ(sc, CSR_PNIC_MII);
   3424 		if ((data & PNIC_MII_BUSY) == 0) {
   3425 			if ((data & PNIC_MII_DATA) == PNIC_MII_DATA)
   3426 				return -1;
   3427 			else {
   3428 				*val = data & PNIC_MII_DATA;
   3429 				return 0;
   3430 			}
   3431 		}
   3432 	}
   3433 	printf("%s: MII read timed out\n", device_xname(sc->sc_dev));
   3434 	return ETIMEDOUT;
   3435 }
   3436 
   3437 /*
   3438  * tlp_pnic_mii_writereg:
   3439  *
   3440  *	Write a PHY register on the Lite-On PNIC.
   3441  */
   3442 static int
   3443 tlp_pnic_mii_writereg(device_t self, int phy, int reg, uint16_t val)
   3444 {
   3445 	struct tulip_softc *sc = device_private(self);
   3446 	int i;
   3447 
   3448 	TULIP_WRITE(sc, CSR_PNIC_MII,
   3449 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   3450 	    PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
   3451 	    (reg << PNIC_MII_REGSHIFT) | val);
   3452 
   3453 	for (i = 0; i < 1000; i++) {
   3454 		delay(10);
   3455 		if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
   3456 			return 0;
   3457 	}
   3458 	printf("%s: MII write timed out\n", device_xname(sc->sc_dev));
   3459 	return ETIMEDOUT;
   3460 }
   3461 
   3462 static const bus_addr_t tlp_al981_phy_regmap[] = {
   3463 	CSR_ADM_BMCR,
   3464 	CSR_ADM_BMSR,
   3465 	CSR_ADM_PHYIDR1,
   3466 	CSR_ADM_PHYIDR2,
   3467 	CSR_ADM_ANAR,
   3468 	CSR_ADM_ANLPAR,
   3469 	CSR_ADM_ANER,
   3470 
   3471 	CSR_ADM_XMC,
   3472 	CSR_ADM_XCIIS,
   3473 	CSR_ADM_XIE,
   3474 	CSR_ADM_100CTR,
   3475 };
   3476 static const int tlp_al981_phy_regmap_size = sizeof(tlp_al981_phy_regmap) /
   3477     sizeof(tlp_al981_phy_regmap[0]);
   3478 
   3479 /*
   3480  * tlp_al981_mii_readreg:
   3481  *
   3482  *	Read a PHY register on the ADMtek AL981.
   3483  */
   3484 static int
   3485 tlp_al981_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
   3486 {
   3487 	struct tulip_softc *sc = device_private(self);
   3488 
   3489 	/* AL981 only has an internal PHY. */
   3490 	if (phy != 0)
   3491 		return -1;
   3492 
   3493 	if (reg >= tlp_al981_phy_regmap_size)
   3494 		return -1;
   3495 
   3496 	*val = bus_space_read_4(sc->sc_st, sc->sc_sh,
   3497 	    tlp_al981_phy_regmap[reg]) & 0xffff;
   3498 	return 0;
   3499 }
   3500 
   3501 /*
   3502  * tlp_al981_mii_writereg:
   3503  *
   3504  *	Write a PHY register on the ADMtek AL981.
   3505  */
   3506 static int
   3507 tlp_al981_mii_writereg(device_t self, int phy, int reg, uint16_t val)
   3508 {
   3509 	struct tulip_softc *sc = device_private(self);
   3510 
   3511 	/* AL981 only has an internal PHY. */
   3512 	if (phy != 0)
   3513 		return -1;
   3514 
   3515 	if (reg >= tlp_al981_phy_regmap_size)
   3516 		return -1;
   3517 
   3518 	bus_space_write_4(sc->sc_st, sc->sc_sh,
   3519 	    tlp_al981_phy_regmap[reg], val);
   3520 
   3521 	return 0;
   3522 }
   3523 
   3524 /*****************************************************************************
   3525  * Chip-specific pre-init and reset functions.
   3526  *****************************************************************************/
   3527 
   3528 /*
   3529  * tlp_2114x_preinit:
   3530  *
   3531  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
   3532  */
   3533 static void
   3534 tlp_2114x_preinit(struct tulip_softc *sc)
   3535 {
   3536 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3537 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3538 
   3539 	/*
   3540 	 * Whether or not we're in MII or SIA/SYM mode, the media info
   3541 	 * contains the appropriate OPMODE bits.
   3542 	 *
   3543 	 * Also, we always set the Must-Be-One bit.
   3544 	 */
   3545 	sc->sc_opmode |= OPMODE_MBO | tm->tm_opmode;
   3546 
   3547 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3548 }
   3549 
   3550 /*
   3551  * tlp_2114x_mii_preinit:
   3552  *
   3553  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
   3554  *	This version is used by boards which only have MII and don't have
   3555  *	an ISV SROM.
   3556  */
   3557 static void
   3558 tlp_2114x_mii_preinit(struct tulip_softc *sc)
   3559 {
   3560 
   3561 	/*
   3562 	 * Always set the Must-Be-One bit, and Port Select (to select MII).
   3563 	 * We'll never be called during a media change.
   3564 	 */
   3565 	sc->sc_opmode |= OPMODE_MBO | OPMODE_PS;
   3566 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3567 }
   3568 
   3569 /*
   3570  * tlp_pnic_preinit:
   3571  *
   3572  *	Pre-init function for the Lite-On 82c168 and 82c169.
   3573  */
   3574 static void
   3575 tlp_pnic_preinit(struct tulip_softc *sc)
   3576 {
   3577 
   3578 	if (sc->sc_flags & TULIPF_HAS_MII) {
   3579 		/*
   3580 		 * MII case: just set the port-select bit; we will never
   3581 		 * be called during a media change.
   3582 		 */
   3583 		sc->sc_opmode |= OPMODE_PS;
   3584 	} else {
   3585 		/*
   3586 		 * ENDEC/PCS/Nway mode; enable the Tx backoff counter.
   3587 		 */
   3588 		sc->sc_opmode |= OPMODE_PNIC_TBEN;
   3589 	}
   3590 }
   3591 
   3592 /*
   3593  * tlp_asix_preinit:
   3594  *
   3595  * 	Pre-init function for the ASIX chipsets.
   3596  */
   3597 static void
   3598 tlp_asix_preinit(struct tulip_softc *sc)
   3599 {
   3600 
   3601 	switch (sc->sc_chip) {
   3602 		case TULIP_CHIP_AX88140:
   3603 		case TULIP_CHIP_AX88141:
   3604 			/* XXX Handle PHY. */
   3605 			sc->sc_opmode |= OPMODE_HBD | OPMODE_PS;
   3606 			break;
   3607 		default:
   3608 			/* Nothing */
   3609 			break;
   3610 	}
   3611 
   3612 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3613 }
   3614 
   3615 /*
   3616  * tlp_dm9102_preinit:
   3617  *
   3618  *	Pre-init function for the Davicom DM9102.
   3619  */
   3620 static void
   3621 tlp_dm9102_preinit(struct tulip_softc *sc)
   3622 {
   3623 
   3624 	switch (sc->sc_chip) {
   3625 	case TULIP_CHIP_DM9102:
   3626 		sc->sc_opmode |= OPMODE_MBO | OPMODE_HBD | OPMODE_PS;
   3627 		break;
   3628 
   3629 	case TULIP_CHIP_DM9102A:
   3630 		/*
   3631 		 * XXX Figure out how to actually deal with the HomePNA
   3632 		 * XXX portion of the DM9102A.
   3633 		 */
   3634 		sc->sc_opmode |= OPMODE_MBO | OPMODE_HBD;
   3635 		break;
   3636 
   3637 	default:
   3638 		/* Nothing. */
   3639 		break;
   3640 	}
   3641 
   3642 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3643 }
   3644 
   3645 /*
   3646  * tlp_21140_reset:
   3647  *
   3648  *	Issue a reset sequence on the 21140 via the GPIO facility.
   3649  */
   3650 static void
   3651 tlp_21140_reset(struct tulip_softc *sc)
   3652 {
   3653 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3654 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3655 	int i;
   3656 
   3657 	/* First, set the direction on the GPIO pins. */
   3658 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | sc->sc_gp_dir);
   3659 
   3660 	/* Now, issue the reset sequence. */
   3661 	for (i = 0; i < tm->tm_reset_length; i++) {
   3662 		delay(10);
   3663 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_reset_offset + i]);
   3664 	}
   3665 
   3666 	/* Now, issue the selection sequence. */
   3667 	for (i = 0; i < tm->tm_gp_length; i++) {
   3668 		delay(10);
   3669 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_gp_offset + i]);
   3670 	}
   3671 
   3672 	/* If there were no sequences, just lower the pins. */
   3673 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   3674 		delay(10);
   3675 		TULIP_WRITE(sc, CSR_GPP, 0);
   3676 	}
   3677 }
   3678 
   3679 /*
   3680  * tlp_21142_reset:
   3681  *
   3682  *	Issue a reset sequence on the 21142 via the GPIO facility.
   3683  */
   3684 static void
   3685 tlp_21142_reset(struct tulip_softc *sc)
   3686 {
   3687 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3688 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3689 	const uint8_t *cp;
   3690 	int i;
   3691 
   3692 	cp = &sc->sc_srom[tm->tm_reset_offset];
   3693 	for (i = 0; i < tm->tm_reset_length; i++, cp += 2) {
   3694 		delay(10);
   3695 		TULIP_WRITE(sc, CSR_SIAGEN, TULIP_ROM_GETW(cp, 0) << 16);
   3696 	}
   3697 
   3698 	cp = &sc->sc_srom[tm->tm_gp_offset];
   3699 	for (i = 0; i < tm->tm_gp_length; i++, cp += 2) {
   3700 		delay(10);
   3701 		TULIP_WRITE(sc, CSR_SIAGEN, TULIP_ROM_GETW(cp, 0) << 16);
   3702 	}
   3703 
   3704 	/* If there were no sequences, just lower the pins. */
   3705 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   3706 		delay(10);
   3707 		TULIP_WRITE(sc, CSR_SIAGEN, 0);
   3708 	}
   3709 }
   3710 
   3711 /*
   3712  * tlp_pmac_reset:
   3713  *
   3714  *	Reset routine for Macronix chips.
   3715  */
   3716 static void
   3717 tlp_pmac_reset(struct tulip_softc *sc)
   3718 {
   3719 
   3720 	switch (sc->sc_chip) {
   3721 	case TULIP_CHIP_82C115:
   3722 	case TULIP_CHIP_MX98715:
   3723 	case TULIP_CHIP_MX98715A:
   3724 	case TULIP_CHIP_MX98725:
   3725 		/*
   3726 		 * Set the LED operating mode.  This information is located
   3727 		 * in the EEPROM at byte offset 0x77, per the MX98715A and
   3728 		 * MX98725 application notes.
   3729 		 */
   3730 		TULIP_WRITE(sc, CSR_MIIROM, sc->sc_srom[0x77] << 24);
   3731 		break;
   3732 	case TULIP_CHIP_MX98715AEC_X:
   3733 		/*
   3734 		 * Set the LED operating mode.  This information is located
   3735 		 * in the EEPROM at byte offset 0x76, per the MX98715AEC
   3736 		 * application note.
   3737 		 */
   3738 		TULIP_WRITE(sc, CSR_MIIROM, ((0xf & sc->sc_srom[0x76]) << 28)
   3739 		    | ((0xf0 & sc->sc_srom[0x76]) << 20));
   3740 		break;
   3741 
   3742 	default:
   3743 		/* Nothing. */
   3744 		break;
   3745 	}
   3746 }
   3747 
   3748 #if 0
   3749 /*
   3750  * tlp_dm9102_reset:
   3751  *
   3752  *	Reset routine for the Davicom DM9102.
   3753  */
   3754 static void
   3755 tlp_dm9102_reset(struct tulip_softc *sc)
   3756 {
   3757 
   3758 	TULIP_WRITE(sc, CSR_DM_PHYSTAT, DM_PHYSTAT_GEPC | DM_PHYSTAT_GPED);
   3759 	delay(100);
   3760 	TULIP_WRITE(sc, CSR_DM_PHYSTAT, 0);
   3761 }
   3762 #endif
   3763 
   3764 /*****************************************************************************
   3765  * Chip/board-specific media switches.  The ones here are ones that
   3766  * are potentially common to multiple front-ends.
   3767  *****************************************************************************/
   3768 
   3769 /*
   3770  * This table is a common place for all sorts of media information,
   3771  * keyed off of the SROM media code for that media.
   3772  *
   3773  * Note that we explicitly configure the 21142/21143 to always advertise
   3774  * NWay capabilities when using the UTP port.
   3775  * XXX Actually, we don't yet.
   3776  */
   3777 static const struct tulip_srom_to_ifmedia tulip_srom_to_ifmedia_table[] = {
   3778 	{ TULIP_ROM_MB_MEDIA_TP,	IFM_10_T,	0,
   3779 	  "10baseT",
   3780 	  OPMODE_TTM,
   3781 	  BMSR_10THDX,
   3782 	  { SIACONN_21040_10BASET,
   3783 	    SIATXRX_21040_10BASET,
   3784 	    SIAGEN_21040_10BASET },
   3785 
   3786 	  { SIACONN_21041_10BASET,
   3787 	    SIATXRX_21041_10BASET,
   3788 	    SIAGEN_21041_10BASET },
   3789 
   3790 	  { SIACONN_21142_10BASET,
   3791 	    SIATXRX_21142_10BASET,
   3792 	    SIAGEN_21142_10BASET } },
   3793 
   3794 	{ TULIP_ROM_MB_MEDIA_BNC,	IFM_10_2,	0,
   3795 	  "10base2",
   3796 	  0,
   3797 	  0,
   3798 	  { 0,
   3799 	    0,
   3800 	    0 },
   3801 
   3802 	  { SIACONN_21041_BNC,
   3803 	    SIATXRX_21041_BNC,
   3804 	    SIAGEN_21041_BNC },
   3805 
   3806 	  { SIACONN_21142_BNC,
   3807 	    SIATXRX_21142_BNC,
   3808 	    SIAGEN_21142_BNC } },
   3809 
   3810 	{ TULIP_ROM_MB_MEDIA_AUI,	IFM_10_5,	0,
   3811 	  "10base5",
   3812 	  0,
   3813 	  0,
   3814 	  { SIACONN_21040_AUI,
   3815 	    SIATXRX_21040_AUI,
   3816 	    SIAGEN_21040_AUI },
   3817 
   3818 	  { SIACONN_21041_AUI,
   3819 	    SIATXRX_21041_AUI,
   3820 	    SIAGEN_21041_AUI },
   3821 
   3822 	  { SIACONN_21142_AUI,
   3823 	    SIATXRX_21142_AUI,
   3824 	    SIAGEN_21142_AUI } },
   3825 
   3826 	{ TULIP_ROM_MB_MEDIA_100TX,	IFM_100_TX,	0,
   3827 	  "100baseTX",
   3828 	  OPMODE_PS | OPMODE_PCS | OPMODE_SCR | OPMODE_HBD,
   3829 	  BMSR_100TXHDX,
   3830 	  { 0,
   3831 	    0,
   3832 	    0 },
   3833 
   3834 	  { 0,
   3835 	    0,
   3836 	    0 },
   3837 
   3838 	  { 0,
   3839 	    0,
   3840 	    SIAGEN_ABM } },
   3841 
   3842 	{ TULIP_ROM_MB_MEDIA_TP_FDX,	IFM_10_T,	IFM_FDX,
   3843 	  "10baseT-FDX",
   3844 	  OPMODE_TTM | OPMODE_FD | OPMODE_HBD,
   3845 	  BMSR_10TFDX,
   3846 	  { SIACONN_21040_10BASET_FDX,
   3847 	    SIATXRX_21040_10BASET_FDX,
   3848 	    SIAGEN_21040_10BASET_FDX },
   3849 
   3850 	  { SIACONN_21041_10BASET_FDX,
   3851 	    SIATXRX_21041_10BASET_FDX,
   3852 	    SIAGEN_21041_10BASET_FDX },
   3853 
   3854 	  { SIACONN_21142_10BASET_FDX,
   3855 	    SIATXRX_21142_10BASET_FDX,
   3856 	    SIAGEN_21142_10BASET_FDX } },
   3857 
   3858 	{ TULIP_ROM_MB_MEDIA_100TX_FDX,	IFM_100_TX,	IFM_FDX,
   3859 	  "100baseTX-FDX",
   3860 	  OPMODE_PS | OPMODE_PCS | OPMODE_SCR | OPMODE_FD | OPMODE_HBD,
   3861 	  BMSR_100TXFDX,
   3862 	  { 0,
   3863 	    0,
   3864 	    0 },
   3865 
   3866 	  { 0,
   3867 	    0,
   3868 	    0 },
   3869 
   3870 	  { 0,
   3871 	    0,
   3872 	    SIAGEN_ABM } },
   3873 
   3874 	{ TULIP_ROM_MB_MEDIA_100T4,	IFM_100_T4,	0,
   3875 	  "100baseT4",
   3876 	  OPMODE_PS | OPMODE_PCS | OPMODE_SCR | OPMODE_HBD,
   3877 	  BMSR_100T4,
   3878 	  { 0,
   3879 	    0,
   3880 	    0 },
   3881 
   3882 	  { 0,
   3883 	    0,
   3884 	    0 },
   3885 
   3886 	  { 0,
   3887 	    0,
   3888 	    SIAGEN_ABM } },
   3889 
   3890 	{ TULIP_ROM_MB_MEDIA_100FX,	IFM_100_FX,	0,
   3891 	  "100baseFX",
   3892 	  OPMODE_PS | OPMODE_PCS | OPMODE_HBD,
   3893 	  0,
   3894 	  { 0,
   3895 	    0,
   3896 	    0 },
   3897 
   3898 	  { 0,
   3899 	    0,
   3900 	    0 },
   3901 
   3902 	  { 0,
   3903 	    0,
   3904 	    SIAGEN_ABM } },
   3905 
   3906 	{ TULIP_ROM_MB_MEDIA_100FX_FDX,	IFM_100_FX,	IFM_FDX,
   3907 	  "100baseFX-FDX",
   3908 	  OPMODE_PS | OPMODE_PCS | OPMODE_FD | OPMODE_HBD,
   3909 	  0,
   3910 	  { 0,
   3911 	    0,
   3912 	    0 },
   3913 
   3914 	  { 0,
   3915 	    0,
   3916 	    0 },
   3917 
   3918 	  { 0,
   3919 	    0,
   3920 	    SIAGEN_ABM } },
   3921 
   3922 	{ 0,				0,		0,
   3923 	  NULL,
   3924 	  0,
   3925 	  0,
   3926 	  { 0,
   3927 	    0,
   3928 	    0 },
   3929 
   3930 	  { 0,
   3931 	    0,
   3932 	    0 },
   3933 
   3934 	  { 0,
   3935 	    0,
   3936 	    0 } },
   3937 };
   3938 
   3939 static const struct tulip_srom_to_ifmedia *tlp_srom_to_ifmedia(uint8_t);
   3940 static void	tlp_srom_media_info(struct tulip_softc *,
   3941 		    const struct tulip_srom_to_ifmedia *,
   3942 		    struct tulip_21x4x_media *);
   3943 static void	tlp_add_srom_media(struct tulip_softc *, int,
   3944 		    void (*)(struct tulip_softc *, struct ifmediareq *),
   3945 		    int (*)(struct tulip_softc *), const uint8_t *, int);
   3946 static void	tlp_print_media(struct tulip_softc *);
   3947 static void	tlp_nway_activate(struct tulip_softc *, int);
   3948 static void	tlp_get_minst(struct tulip_softc *);
   3949 
   3950 static const struct tulip_srom_to_ifmedia *
   3951 tlp_srom_to_ifmedia(uint8_t sm)
   3952 {
   3953 	const struct tulip_srom_to_ifmedia *tsti;
   3954 
   3955 	for (tsti = tulip_srom_to_ifmedia_table;
   3956 	     tsti->tsti_name != NULL; tsti++) {
   3957 		if (tsti->tsti_srom == sm)
   3958 			return tsti;
   3959 	}
   3960 
   3961 	return NULL;
   3962 }
   3963 
   3964 static void
   3965 tlp_srom_media_info(struct tulip_softc *sc,
   3966     const struct tulip_srom_to_ifmedia *tsti, struct tulip_21x4x_media *tm)
   3967 {
   3968 
   3969 	tm->tm_name = tsti->tsti_name;
   3970 	tm->tm_opmode = tsti->tsti_opmode;
   3971 
   3972 	sc->sc_sia_cap |= tsti->tsti_sia_cap;
   3973 
   3974 	switch (sc->sc_chip) {
   3975 	case TULIP_CHIP_DE425:
   3976 	case TULIP_CHIP_21040:
   3977 		tm->tm_sia = tsti->tsti_21040;	/* struct assignment */
   3978 		break;
   3979 
   3980 	case TULIP_CHIP_21041:
   3981 		tm->tm_sia = tsti->tsti_21041;	/* struct assignment */
   3982 		break;
   3983 
   3984 	case TULIP_CHIP_21142:
   3985 	case TULIP_CHIP_21143:
   3986 	case TULIP_CHIP_82C115:
   3987 	case TULIP_CHIP_MX98715:
   3988 	case TULIP_CHIP_MX98715A:
   3989 	case TULIP_CHIP_MX98715AEC_X:
   3990 	case TULIP_CHIP_MX98725:
   3991 		tm->tm_sia = tsti->tsti_21142;	/* struct assignment */
   3992 		break;
   3993 
   3994 	default:
   3995 		/* Nothing. */
   3996 		break;
   3997 	}
   3998 }
   3999 
   4000 static void
   4001 tlp_add_srom_media(struct tulip_softc *sc, int type,
   4002     void (*get)(struct tulip_softc *, struct ifmediareq *),
   4003     int (*set)(struct tulip_softc *), const uint8_t *list,
   4004     int cnt)
   4005 {
   4006 	struct tulip_21x4x_media *tm;
   4007 	const struct tulip_srom_to_ifmedia *tsti;
   4008 	int i;
   4009 
   4010 	for (i = 0; i < cnt; i++) {
   4011 		tsti = tlp_srom_to_ifmedia(list[i]);
   4012 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
   4013 		tlp_srom_media_info(sc, tsti, tm);
   4014 		tm->tm_type = type;
   4015 		tm->tm_get = get;
   4016 		tm->tm_set = set;
   4017 
   4018 		ifmedia_add(&sc->sc_mii.mii_media,
   4019 		    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4020 		    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4021 	}
   4022 }
   4023 
   4024 static void
   4025 tlp_print_media(struct tulip_softc *sc)
   4026 {
   4027 	struct ifmedia_entry *ife;
   4028 	struct tulip_21x4x_media *tm;
   4029 	const char *sep = "";
   4030 
   4031 #define	PRINT(str)	aprint_normal("%s%s", sep, str); sep = ", "
   4032 
   4033 	aprint_normal_dev(sc->sc_dev, "");
   4034 	TAILQ_FOREACH(ife, &sc->sc_mii.mii_media.ifm_list, ifm_list) {
   4035 		tm = ife->ifm_aux;
   4036 		if (tm == NULL) {
   4037 #ifdef DIAGNOSTIC
   4038 			if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   4039 				panic("tlp_print_media");
   4040 #endif
   4041 			PRINT("auto");
   4042 		} else if (tm->tm_type != TULIP_ROM_MB_21140_MII &&
   4043 			   tm->tm_type != TULIP_ROM_MB_21142_MII) {
   4044 			PRINT(tm->tm_name);
   4045 		}
   4046 	}
   4047 	aprint_normal("\n");
   4048 
   4049 #undef PRINT
   4050 }
   4051 
   4052 static void
   4053 tlp_nway_activate(struct tulip_softc *sc, int media)
   4054 {
   4055 	struct ifmedia_entry *ife;
   4056 
   4057 	ife = ifmedia_match(&sc->sc_mii.mii_media, media, 0);
   4058 #ifdef DIAGNOSTIC
   4059 	if (ife == NULL)
   4060 		panic("tlp_nway_activate");
   4061 #endif
   4062 	sc->sc_nway_active = ife;
   4063 }
   4064 
   4065 static void
   4066 tlp_get_minst(struct tulip_softc *sc)
   4067 {
   4068 
   4069 	if ((sc->sc_media_seen &
   4070 	    ~((1 << TULIP_ROM_MB_21140_MII) |
   4071 	      (1 << TULIP_ROM_MB_21142_MII))) == 0) {
   4072 		/*
   4073 		 * We have not yet seen any SIA/SYM media (but are
   4074 		 * about to; that's why we're called!), so assign
   4075 		 * the current media instance to be the `internal media'
   4076 		 * instance, and advance it so any MII media gets a
   4077 		 * fresh one (used to selecting/isolating a PHY).
   4078 		 */
   4079 		sc->sc_tlp_minst = sc->sc_mii.mii_instance++;
   4080 	}
   4081 }
   4082 
   4083 /*
   4084  * SIA Utility functions.
   4085  */
   4086 static void	tlp_sia_update_link(struct tulip_softc *);
   4087 static void	tlp_sia_get(struct tulip_softc *, struct ifmediareq *);
   4088 static int	tlp_sia_set(struct tulip_softc *);
   4089 static int	tlp_sia_media(struct tulip_softc *, struct ifmedia_entry *);
   4090 static void	tlp_sia_fixup(struct tulip_softc *);
   4091 
   4092 static void
   4093 tlp_sia_update_link(struct tulip_softc *sc)
   4094 {
   4095 	struct ifmedia_entry *ife;
   4096 	struct tulip_21x4x_media *tm;
   4097 	uint32_t siastat;
   4098 
   4099 	ife = TULIP_CURRENT_MEDIA(sc);
   4100 	tm = ife->ifm_aux;
   4101 
   4102 	sc->sc_flags &= ~(TULIPF_LINK_UP | TULIPF_LINK_VALID);
   4103 
   4104 	siastat = TULIP_READ(sc, CSR_SIASTAT);
   4105 
   4106 	/*
   4107 	 * Note that when we do SIA link tests, we are assuming that
   4108 	 * the chip is really in the mode that the current media setting
   4109 	 * reflects.  If we're not, then the link tests will not be
   4110 	 * accurate!
   4111 	 */
   4112 	switch (IFM_SUBTYPE(ife->ifm_media)) {
   4113 	case IFM_10_T:
   4114 		sc->sc_flags |= TULIPF_LINK_VALID;
   4115 		if ((siastat & SIASTAT_LS10) == 0)
   4116 			sc->sc_flags |= TULIPF_LINK_UP;
   4117 		break;
   4118 
   4119 	case IFM_100_TX:
   4120 	case IFM_100_T4:
   4121 		sc->sc_flags |= TULIPF_LINK_VALID;
   4122 		if ((siastat & SIASTAT_LS100) == 0)
   4123 			sc->sc_flags |= TULIPF_LINK_UP;
   4124 		break;
   4125 	}
   4126 
   4127 	switch (sc->sc_chip) {
   4128 	case TULIP_CHIP_21142:
   4129 	case TULIP_CHIP_21143:
   4130 		/*
   4131 		 * On these chips, we can tell more information about
   4132 		 * AUI/BNC.  Note that the AUI/BNC selection is made
   4133 		 * in a different register; for our purpose, it's all
   4134 		 * AUI.
   4135 		 */
   4136 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   4137 		case IFM_10_2:
   4138 		case IFM_10_5:
   4139 			sc->sc_flags |= TULIPF_LINK_VALID;
   4140 			if (siastat & SIASTAT_ARA) {
   4141 				TULIP_WRITE(sc, CSR_SIASTAT, SIASTAT_ARA);
   4142 				sc->sc_flags |= TULIPF_LINK_UP;
   4143 			}
   4144 			break;
   4145 
   4146 		default:
   4147 			/*
   4148 			 * If we're SYM media and can detect the link
   4149 			 * via the GPIO facility, prefer that status
   4150 			 * over LS100.
   4151 			 */
   4152 			if (tm->tm_type == TULIP_ROM_MB_21143_SYM &&
   4153 			    tm->tm_actmask != 0) {
   4154 				sc->sc_flags = (sc->sc_flags &
   4155 				    ~TULIPF_LINK_UP) | TULIPF_LINK_VALID;
   4156 				if (TULIP_ISSET(sc, CSR_SIAGEN,
   4157 				    tm->tm_actmask) == tm->tm_actdata)
   4158 					sc->sc_flags |= TULIPF_LINK_UP;
   4159 			}
   4160 		}
   4161 		break;
   4162 
   4163 	default:
   4164 		/* Nothing. */
   4165 		break;
   4166 	}
   4167 }
   4168 
   4169 static void
   4170 tlp_sia_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
   4171 {
   4172 	struct ifmedia_entry *ife;
   4173 
   4174 	ifmr->ifm_status = 0;
   4175 
   4176 	tlp_sia_update_link(sc);
   4177 
   4178 	ife = TULIP_CURRENT_MEDIA(sc);
   4179 
   4180 	if (sc->sc_flags & TULIPF_LINK_VALID)
   4181 		ifmr->ifm_status |= IFM_AVALID;
   4182 	if (sc->sc_flags & TULIPF_LINK_UP)
   4183 		ifmr->ifm_status |= IFM_ACTIVE;
   4184 	ifmr->ifm_active = ife->ifm_media;
   4185 }
   4186 
   4187 static void
   4188 tlp_sia_fixup(struct tulip_softc *sc)
   4189 {
   4190 	struct ifmedia_entry *ife;
   4191 	struct tulip_21x4x_media *tm;
   4192 	uint32_t siaconn, siatxrx, siagen;
   4193 
   4194 	switch (sc->sc_chip) {
   4195 	case TULIP_CHIP_82C115:
   4196 	case TULIP_CHIP_MX98713A:
   4197 	case TULIP_CHIP_MX98715:
   4198 	case TULIP_CHIP_MX98715A:
   4199 	case TULIP_CHIP_MX98715AEC_X:
   4200 	case TULIP_CHIP_MX98725:
   4201 		siaconn = PMAC_SIACONN_MASK;
   4202 		siatxrx = PMAC_SIATXRX_MASK;
   4203 		siagen  = PMAC_SIAGEN_MASK;
   4204 		break;
   4205 
   4206 	default:
   4207 		/* No fixups required on any other chips. */
   4208 		return;
   4209 	}
   4210 
   4211 	TAILQ_FOREACH(ife, &sc->sc_mii.mii_media.ifm_list, ifm_list) {
   4212 		tm = ife->ifm_aux;
   4213 		if (tm == NULL)
   4214 			continue;
   4215 
   4216 		tm->tm_siaconn &= siaconn;
   4217 		tm->tm_siatxrx &= siatxrx;
   4218 		tm->tm_siagen  &= siagen;
   4219 	}
   4220 }
   4221 
   4222 static int
   4223 tlp_sia_set(struct tulip_softc *sc)
   4224 {
   4225 
   4226 	return tlp_sia_media(sc, TULIP_CURRENT_MEDIA(sc));
   4227 }
   4228 
   4229 static int
   4230 tlp_sia_media(struct tulip_softc *sc, struct ifmedia_entry *ife)
   4231 {
   4232 	struct tulip_21x4x_media *tm;
   4233 
   4234 	tm = ife->ifm_aux;
   4235 
   4236 	/*
   4237 	 * XXX This appears to be necessary on a bunch of the clone chips.
   4238 	 */
   4239 	delay(20000);
   4240 
   4241 	/*
   4242 	 * Idle the chip.
   4243 	 */
   4244 	tlp_idle(sc, OPMODE_ST | OPMODE_SR);
   4245 
   4246 	/*
   4247 	 * Program the SIA.  It's important to write in this order,
   4248 	 * resetting the SIA first.
   4249 	 */
   4250 	TULIP_WRITE(sc, CSR_SIACONN, 0);		/* SRL bit clear */
   4251 	delay(1000);
   4252 
   4253 	TULIP_WRITE(sc, CSR_SIATXRX, tm->tm_siatxrx);
   4254 
   4255 	switch (sc->sc_chip) {
   4256 	case TULIP_CHIP_21142:
   4257 	case TULIP_CHIP_21143:
   4258 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpctl);
   4259 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpdata);
   4260 		break;
   4261 	default:
   4262 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen);
   4263 	}
   4264 
   4265 	TULIP_WRITE(sc, CSR_SIACONN, tm->tm_siaconn);
   4266 
   4267 	/*
   4268 	 * Set the OPMODE bits for this media and write OPMODE.
   4269 	 * This will resume the transmit and receive processes.
   4270 	 */
   4271 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
   4272 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   4273 
   4274 	return 0;
   4275 }
   4276 
   4277 /*
   4278  * 21140 GPIO utility functions.
   4279  */
   4280 static void	tlp_21140_gpio_update_link(struct tulip_softc *);
   4281 
   4282 static void
   4283 tlp_21140_gpio_update_link(struct tulip_softc *sc)
   4284 {
   4285 	struct ifmedia_entry *ife;
   4286 	struct tulip_21x4x_media *tm;
   4287 
   4288 	ife = TULIP_CURRENT_MEDIA(sc);
   4289 	tm = ife->ifm_aux;
   4290 
   4291 	sc->sc_flags &= ~(TULIPF_LINK_UP | TULIPF_LINK_VALID);
   4292 
   4293 	if (tm->tm_actmask != 0) {
   4294 		sc->sc_flags |= TULIPF_LINK_VALID;
   4295 		if (TULIP_ISSET(sc, CSR_GPP, tm->tm_actmask) ==
   4296 		    tm->tm_actdata)
   4297 			sc->sc_flags |= TULIPF_LINK_UP;
   4298 	}
   4299 }
   4300 
   4301 void
   4302 tlp_21140_gpio_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
   4303 {
   4304 	struct ifmedia_entry *ife;
   4305 
   4306 	ifmr->ifm_status = 0;
   4307 
   4308 	tlp_21140_gpio_update_link(sc);
   4309 
   4310 	ife = TULIP_CURRENT_MEDIA(sc);
   4311 
   4312 	if (sc->sc_flags & TULIPF_LINK_VALID)
   4313 		ifmr->ifm_status |= IFM_AVALID;
   4314 	if (sc->sc_flags & TULIPF_LINK_UP)
   4315 		ifmr->ifm_status |= IFM_ACTIVE;
   4316 	ifmr->ifm_active = ife->ifm_media;
   4317 }
   4318 
   4319 int
   4320 tlp_21140_gpio_set(struct tulip_softc *sc)
   4321 {
   4322 	struct ifmedia_entry *ife;
   4323 	struct tulip_21x4x_media *tm;
   4324 
   4325 	ife = TULIP_CURRENT_MEDIA(sc);
   4326 	tm = ife->ifm_aux;
   4327 
   4328 	/*
   4329 	 * Idle the chip.
   4330 	 */
   4331 	tlp_idle(sc, OPMODE_ST | OPMODE_SR);
   4332 
   4333 	/*
   4334 	 * Set the GPIO pins for this media, to flip any
   4335 	 * relays, etc.
   4336 	 */
   4337 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | sc->sc_gp_dir);
   4338 	delay(10);
   4339 	TULIP_WRITE(sc, CSR_GPP, tm->tm_gpdata);
   4340 
   4341 	/*
   4342 	 * Set the OPMODE bits for this media and write OPMODE.
   4343 	 * This will resume the transmit and receive processes.
   4344 	 */
   4345 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
   4346 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   4347 
   4348 	return 0;
   4349 }
   4350 
   4351 /*
   4352  * 21040 and 21041 media switches.
   4353  */
   4354 static void	tlp_21040_tmsw_init(struct tulip_softc *);
   4355 static void	tlp_21040_tp_tmsw_init(struct tulip_softc *);
   4356 static void	tlp_21040_auibnc_tmsw_init(struct tulip_softc *);
   4357 static void	tlp_21041_tmsw_init(struct tulip_softc *);
   4358 
   4359 const struct tulip_mediasw tlp_21040_mediasw = {
   4360 	tlp_21040_tmsw_init, tlp_sia_get, tlp_sia_set
   4361 };
   4362 
   4363 const struct tulip_mediasw tlp_21040_tp_mediasw = {
   4364 	tlp_21040_tp_tmsw_init, tlp_sia_get, tlp_sia_set
   4365 };
   4366 
   4367 const struct tulip_mediasw tlp_21040_auibnc_mediasw = {
   4368 	tlp_21040_auibnc_tmsw_init, tlp_sia_get, tlp_sia_set
   4369 };
   4370 
   4371 const struct tulip_mediasw tlp_21041_mediasw = {
   4372 	tlp_21041_tmsw_init, tlp_sia_get, tlp_sia_set
   4373 };
   4374 
   4375 static void
   4376 tlp_21040_tmsw_init(struct tulip_softc *sc)
   4377 {
   4378 	struct mii_data * const mii = &sc->sc_mii;
   4379 	static const uint8_t media[] = {
   4380 		TULIP_ROM_MB_MEDIA_TP,
   4381 		TULIP_ROM_MB_MEDIA_TP_FDX,
   4382 		TULIP_ROM_MB_MEDIA_AUI,
   4383 	};
   4384 	struct tulip_21x4x_media *tm;
   4385 
   4386 	sc->sc_ethercom.ec_mii = mii;
   4387 	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
   4388 
   4389 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 3);
   4390 
   4391 	/*
   4392 	 * No SROM type for External SIA.
   4393 	 */
   4394 	tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
   4395 	tm->tm_name = "manual";
   4396 	tm->tm_opmode = 0;
   4397 	tm->tm_siaconn = SIACONN_21040_EXTSIA;
   4398 	tm->tm_siatxrx = SIATXRX_21040_EXTSIA;
   4399 	tm->tm_siagen  = SIAGEN_21040_EXTSIA;
   4400 	ifmedia_add(&mii->mii_media,
   4401 	    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, sc->sc_tlp_minst), 0, tm);
   4402 
   4403 	/*
   4404 	 * XXX Autosense not yet supported.
   4405 	 */
   4406 
   4407 	/* XXX This should be auto-sense. */
   4408 	ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_10_T);
   4409 
   4410 	tlp_print_media(sc);
   4411 }
   4412 
   4413 static void
   4414 tlp_21040_tp_tmsw_init(struct tulip_softc *sc)
   4415 {
   4416 	struct mii_data * const mii = &sc->sc_mii;
   4417 	static const uint8_t media[] = {
   4418 		TULIP_ROM_MB_MEDIA_TP,
   4419 		TULIP_ROM_MB_MEDIA_TP_FDX,
   4420 	};
   4421 
   4422 	sc->sc_ethercom.ec_mii = mii;
   4423 	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
   4424 
   4425 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 2);
   4426 
   4427 	ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_10_T);
   4428 
   4429 	tlp_print_media(sc);
   4430 }
   4431 
   4432 static void
   4433 tlp_21040_auibnc_tmsw_init(struct tulip_softc *sc)
   4434 {
   4435 	struct mii_data * const mii = &sc->sc_mii;
   4436 	static const uint8_t media[] = {
   4437 		TULIP_ROM_MB_MEDIA_AUI,
   4438 	};
   4439 
   4440 	sc->sc_ethercom.ec_mii = mii;
   4441 	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
   4442 
   4443 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 1);
   4444 
   4445 	ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_10_5);
   4446 
   4447 	tlp_print_media(sc);
   4448 }
   4449 
   4450 static void
   4451 tlp_21041_tmsw_init(struct tulip_softc *sc)
   4452 {
   4453 	struct mii_data * const mii = &sc->sc_mii;
   4454 	static const uint8_t media[] = {
   4455 		TULIP_ROM_MB_MEDIA_TP,
   4456 		TULIP_ROM_MB_MEDIA_TP_FDX,
   4457 		TULIP_ROM_MB_MEDIA_BNC,
   4458 		TULIP_ROM_MB_MEDIA_AUI,
   4459 	};
   4460 	int i, defmedia, devcnt, leaf_offset, mb_offset, m_cnt;
   4461 	const struct tulip_srom_to_ifmedia *tsti;
   4462 	struct tulip_21x4x_media *tm;
   4463 	uint16_t romdef;
   4464 	uint8_t mb;
   4465 
   4466 	sc->sc_ethercom.ec_mii = mii;
   4467 	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
   4468 
   4469 	if (tlp_isv_srom(sc->sc_srom) == 0) {
   4470  not_isv_srom:
   4471 		/*
   4472 		 * If we have a board without the standard 21041 SROM format,
   4473 		 * we just assume all media are present and try and pick a
   4474 		 * reasonable default.
   4475 		 */
   4476 		tlp_add_srom_media(sc, 0, NULL, NULL, media, 4);
   4477 
   4478 		/*
   4479 		 * XXX Autosense not yet supported.
   4480 		 */
   4481 
   4482 		/* XXX This should be auto-sense. */
   4483 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_10_T);
   4484 
   4485 		tlp_print_media(sc);
   4486 		return;
   4487 	}
   4488 
   4489 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   4490 	for (i = 0; i < devcnt; i++) {
   4491 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   4492 			break;
   4493 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   4494 		    sc->sc_devno)
   4495 			break;
   4496 	}
   4497 
   4498 	if (i == devcnt)
   4499 		goto not_isv_srom;
   4500 
   4501 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
   4502 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
   4503 	mb_offset = leaf_offset + TULIP_ROM_IL_MEDIAn_BLOCK_BASE;
   4504 	m_cnt = sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
   4505 
   4506 	for (; m_cnt != 0;
   4507 	     m_cnt--, mb_offset += TULIP_ROM_MB_SIZE(mb)) {
   4508 		mb = sc->sc_srom[mb_offset];
   4509 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
   4510 		switch (mb & TULIP_ROM_MB_MEDIA_CODE) {
   4511 		case TULIP_ROM_MB_MEDIA_TP_FDX:
   4512 		case TULIP_ROM_MB_MEDIA_TP:
   4513 		case TULIP_ROM_MB_MEDIA_BNC:
   4514 		case TULIP_ROM_MB_MEDIA_AUI:
   4515 			tsti = tlp_srom_to_ifmedia(mb &
   4516 			    TULIP_ROM_MB_MEDIA_CODE);
   4517 
   4518 			tlp_srom_media_info(sc, tsti, tm);
   4519 
   4520 			/*
   4521 			 * Override our default SIA settings if the
   4522 			 * SROM contains its own.
   4523 			 */
   4524 			if (mb & TULIP_ROM_MB_EXT) {
   4525 				tm->tm_siaconn = TULIP_ROM_GETW(sc->sc_srom,
   4526 				    mb_offset + TULIP_ROM_MB_CSR13);
   4527 				tm->tm_siatxrx = TULIP_ROM_GETW(sc->sc_srom,
   4528 				    mb_offset + TULIP_ROM_MB_CSR14);
   4529 				tm->tm_siagen = TULIP_ROM_GETW(sc->sc_srom,
   4530 				    mb_offset + TULIP_ROM_MB_CSR15);
   4531 			}
   4532 
   4533 			ifmedia_add(&mii->mii_media,
   4534 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4535 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4536 			break;
   4537 
   4538 		default:
   4539 			aprint_error_dev(sc->sc_dev,
   4540 			    "unknown media code 0x%02x\n",
   4541 			    mb & TULIP_ROM_MB_MEDIA_CODE);
   4542 			free(tm, M_DEVBUF);
   4543 		}
   4544 	}
   4545 
   4546 	/*
   4547 	 * XXX Autosense not yet supported.
   4548 	 */
   4549 
   4550 	romdef = TULIP_ROM_GETW(sc->sc_srom, leaf_offset +
   4551 	    TULIP_ROM_IL_SELECT_CONN_TYPE);
   4552 	switch (romdef) {
   4553 	case SELECT_CONN_TYPE_TP:
   4554 	case SELECT_CONN_TYPE_TP_AUTONEG:
   4555 	case SELECT_CONN_TYPE_TP_NOLINKPASS:
   4556 		defmedia = IFM_ETHER | IFM_10_T;
   4557 		break;
   4558 
   4559 	case SELECT_CONN_TYPE_TP_FDX:
   4560 		defmedia = IFM_ETHER | IFM_10_T | IFM_FDX;
   4561 		break;
   4562 
   4563 	case SELECT_CONN_TYPE_BNC:
   4564 		defmedia = IFM_ETHER | IFM_10_2;
   4565 		break;
   4566 
   4567 	case SELECT_CONN_TYPE_AUI:
   4568 		defmedia = IFM_ETHER | IFM_10_5;
   4569 		break;
   4570 #if 0 /* XXX */
   4571 	case SELECT_CONN_TYPE_ASENSE:
   4572 	case SELECT_CONN_TYPE_ASENSE_AUTONEG:
   4573 		defmedia = IFM_ETHER | IFM_AUTO;
   4574 		break;
   4575 #endif
   4576 	default:
   4577 		defmedia = 0;
   4578 	}
   4579 
   4580 	if (defmedia == 0) {
   4581 		/*
   4582 		 * XXX We should default to auto-sense.
   4583 		 */
   4584 		defmedia = IFM_ETHER | IFM_10_T;
   4585 	}
   4586 
   4587 	ifmedia_set(&mii->mii_media, defmedia);
   4588 
   4589 	tlp_print_media(sc);
   4590 }
   4591 
   4592 /*
   4593  * DECchip 2114x ISV media switch.
   4594  */
   4595 static void	tlp_2114x_isv_tmsw_init(struct tulip_softc *);
   4596 static void	tlp_2114x_isv_tmsw_get(struct tulip_softc *,
   4597 		    struct ifmediareq *);
   4598 static int	tlp_2114x_isv_tmsw_set(struct tulip_softc *);
   4599 
   4600 const struct tulip_mediasw tlp_2114x_isv_mediasw = {
   4601 	tlp_2114x_isv_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
   4602 };
   4603 
   4604 static void	tlp_2114x_nway_get(struct tulip_softc *, struct ifmediareq *);
   4605 static int	tlp_2114x_nway_set(struct tulip_softc *);
   4606 
   4607 static void	tlp_2114x_nway_statchg(struct ifnet *);
   4608 static int	tlp_2114x_nway_service(struct tulip_softc *, int);
   4609 static void	tlp_2114x_nway_auto(struct tulip_softc *);
   4610 static void	tlp_2114x_nway_status(struct tulip_softc *);
   4611 
   4612 static void
   4613 tlp_2114x_isv_tmsw_init(struct tulip_softc *sc)
   4614 {
   4615 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4616 	struct mii_data * const mii = &sc->sc_mii;
   4617 	struct ifmedia_entry *ife;
   4618 	struct mii_softc *phy;
   4619 	struct tulip_21x4x_media *tm;
   4620 	const struct tulip_srom_to_ifmedia *tsti;
   4621 	int i, devcnt, leaf_offset, m_cnt, type, length;
   4622 	int defmedia, miidef;
   4623 	uint16_t word;
   4624 	uint8_t *cp, *ncp;
   4625 
   4626 	defmedia = miidef = 0;
   4627 
   4628 	mii->mii_ifp = ifp;
   4629 	mii->mii_readreg = tlp_bitbang_mii_readreg;
   4630 	mii->mii_writereg = tlp_bitbang_mii_writereg;
   4631 	mii->mii_statchg = sc->sc_statchg;
   4632 	sc->sc_ethercom.ec_mii = mii;
   4633 
   4634 	/*
   4635 	 * Ignore `instance'; we may get a mixture of SIA and MII
   4636 	 * media, and `instance' is used to isolate or select the
   4637 	 * PHY on the MII as appropriate.  Note that duplicate media
   4638 	 * are disallowed, so ignoring `instance' is safe.
   4639 	 */
   4640 	ifmedia_init(&mii->mii_media, IFM_IMASK, tlp_mediachange,
   4641 	    tlp_mediastatus);
   4642 
   4643 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   4644 	for (i = 0; i < devcnt; i++) {
   4645 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   4646 			break;
   4647 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   4648 		    sc->sc_devno)
   4649 			break;
   4650 	}
   4651 
   4652 	if (i == devcnt) {
   4653 		aprint_error_dev(sc->sc_dev,
   4654 		    "unable to locate info leaf in SROM\n");
   4655 		return;
   4656 	}
   4657 
   4658 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
   4659 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
   4660 
   4661 	/* XXX SELECT CONN TYPE */
   4662 
   4663 	cp = &sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
   4664 
   4665 	/*
   4666 	 * On some chips, the first thing in the Info Leaf is the
   4667 	 * GPIO pin direction data.
   4668 	 */
   4669 	switch (sc->sc_chip) {
   4670 	case TULIP_CHIP_21140:
   4671 	case TULIP_CHIP_21140A:
   4672 	case TULIP_CHIP_MX98713:
   4673 	case TULIP_CHIP_AX88140:
   4674 	case TULIP_CHIP_AX88141:
   4675 		sc->sc_gp_dir = *cp++;
   4676 		break;
   4677 
   4678 	default:
   4679 		/* Nothing. */
   4680 		break;
   4681 	}
   4682 
   4683 	/* Get the media count. */
   4684 	m_cnt = *cp++;
   4685 
   4686 	if (m_cnt == 0) {
   4687 		sc->sc_mediasw = &tlp_sio_mii_mediasw;
   4688 		(*sc->sc_mediasw->tmsw_init)(sc);
   4689 		return;
   4690 	}
   4691 
   4692 	for (; m_cnt != 0; cp = ncp, m_cnt--) {
   4693 		/*
   4694 		 * Determine the type and length of this media block.
   4695 		 * The 21143 is spec'd to always use extended format blocks,
   4696 		 * but some cards don't set the bit to indicate this.
   4697 		 * Hopefully there are no cards which really don't use
   4698 		 * extended format blocks.
   4699 		 */
   4700 		if ((*cp & 0x80) == 0 && sc->sc_chip != TULIP_CHIP_21143) {
   4701 			length = 4;
   4702 			type = TULIP_ROM_MB_21140_GPR;
   4703 		} else {
   4704 			length = (*cp++ & 0x7f) - 1;
   4705 			type = *cp++ & 0x3f;
   4706 		}
   4707 
   4708 		/* Compute the start of the next block. */
   4709 		ncp = cp + length;
   4710 
   4711 		/* Now, parse the block. */
   4712 		switch (type) {
   4713 		case TULIP_ROM_MB_21140_GPR:
   4714 			tlp_get_minst(sc);
   4715 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_GPR;
   4716 
   4717 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
   4718 
   4719 			tm->tm_type = TULIP_ROM_MB_21140_GPR;
   4720 			tm->tm_get = tlp_21140_gpio_get;
   4721 			tm->tm_set = tlp_21140_gpio_set;
   4722 
   4723 			/* First is the media type code. */
   4724 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4725 			    TULIP_ROM_MB_MEDIA_CODE);
   4726 			if (tsti == NULL) {
   4727 				/* Invalid media code. */
   4728 				free(tm, M_DEVBUF);
   4729 				break;
   4730 			}
   4731 
   4732 			/* Get defaults. */
   4733 			tlp_srom_media_info(sc, tsti, tm);
   4734 
   4735 			/* Next is any GPIO info for this media. */
   4736 			tm->tm_gpdata = cp[1];
   4737 
   4738 			/*
   4739 			 * Next is a word containing OPMODE information
   4740 			 * and info on how to detect if this media is
   4741 			 * active.
   4742 			 */
   4743 			word = TULIP_ROM_GETW(cp, 2);
   4744 			tm->tm_opmode &= OPMODE_FD;
   4745 			tm->tm_opmode |= TULIP_ROM_MB_OPMODE(word);
   4746 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
   4747 				tm->tm_actmask =
   4748 				    TULIP_ROM_MB_BITPOS(word);
   4749 				tm->tm_actdata =
   4750 				    (word & TULIP_ROM_MB_POLARITY) ?
   4751 				    0 : tm->tm_actmask;
   4752 			}
   4753 
   4754 			ifmedia_add(&mii->mii_media,
   4755 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4756 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4757 			break;
   4758 
   4759 		case TULIP_ROM_MB_21140_MII:
   4760 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_MII;
   4761 
   4762 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
   4763 
   4764 			tm->tm_type = TULIP_ROM_MB_21140_MII;
   4765 			tm->tm_get = tlp_mii_getmedia;
   4766 			tm->tm_set = tlp_mii_setmedia;
   4767 			tm->tm_opmode = OPMODE_PS;
   4768 
   4769 			if (sc->sc_reset == NULL)
   4770 				sc->sc_reset = tlp_21140_reset;
   4771 
   4772 			/* First is the PHY number. */
   4773 			tm->tm_phyno = *cp++;
   4774 
   4775 			/* Next is the MII select sequence length and offset. */
   4776 			tm->tm_gp_length = *cp++;
   4777 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
   4778 			cp += tm->tm_gp_length;
   4779 
   4780 			/* Next is the MII reset sequence length and offset. */
   4781 			tm->tm_reset_length = *cp++;
   4782 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
   4783 			cp += tm->tm_reset_length;
   4784 
   4785 			/*
   4786 			 * The following items are left in the media block
   4787 			 * that we don't particularly care about:
   4788 			 *
   4789 			 *	capabilities		W
   4790 			 *	advertisement		W
   4791 			 *	full duplex		W
   4792 			 *	tx threshold		W
   4793 			 *
   4794 			 * These appear to be bits in the PHY registers,
   4795 			 * which our MII code handles on its own.
   4796 			 */
   4797 
   4798 			/*
   4799 			 * Before we probe the MII bus, we need to reset
   4800 			 * it and issue the selection sequence.
   4801 			 */
   4802 
   4803 			/* Set the direction of the pins... */
   4804 			TULIP_WRITE(sc, CSR_GPP, GPP_GPC | sc->sc_gp_dir);
   4805 
   4806 			for (i = 0; i < tm->tm_reset_length; i++) {
   4807 				delay(10);
   4808 				TULIP_WRITE(sc, CSR_GPP,
   4809 				    sc->sc_srom[tm->tm_reset_offset + i]);
   4810 			}
   4811 
   4812 			for (i = 0; i < tm->tm_gp_length; i++) {
   4813 				delay(10);
   4814 				TULIP_WRITE(sc, CSR_GPP,
   4815 				    sc->sc_srom[tm->tm_gp_offset + i]);
   4816 			}
   4817 
   4818 			/* If there were no sequences, just lower the pins. */
   4819 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   4820 				delay(10);
   4821 				TULIP_WRITE(sc, CSR_GPP, 0);
   4822 			}
   4823 
   4824 			/*
   4825 			 * Now, probe the MII for the PHY.  Note, we know
   4826 			 * the location of the PHY on the bus, but we don't
   4827 			 * particularly care; the MII code just likes to
   4828 			 * search the whole thing anyhow.
   4829 			 */
   4830 			mii_attach(sc->sc_dev, mii, 0xffffffff,
   4831 			    MII_PHY_ANY, tm->tm_phyno, 0);
   4832 
   4833 			/*
   4834 			 * Now, search for the PHY we hopefully just
   4835 			 * configured.  If it's not configured into the
   4836 			 * kernel, we lose.  The PHY's default media always
   4837 			 * takes priority.
   4838 			 */
   4839 			LIST_FOREACH(phy, &mii->mii_phys, mii_list) {
   4840 				if (phy->mii_offset == tm->tm_phyno)
   4841 					break;
   4842 			}
   4843 			if (phy == NULL) {
   4844 				aprint_error_dev(sc->sc_dev,
   4845 				    "unable to configure MII\n");
   4846 				break;
   4847 			}
   4848 
   4849 			sc->sc_flags |= TULIPF_HAS_MII;
   4850 			sc->sc_tick = tlp_mii_tick;
   4851 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
   4852 			    phy->mii_inst);
   4853 
   4854 			/*
   4855 			 * Okay, now that we've found the PHY and the MII
   4856 			 * layer has added all of the media associated
   4857 			 * with that PHY, we need to traverse the media
   4858 			 * list, and add our `tm' to each entry's `aux'
   4859 			 * pointer.
   4860 			 *
   4861 			 * We do this by looking for media with our
   4862 			 * PHY's `instance'.
   4863 			 */
   4864 			TAILQ_FOREACH(ife, &mii->mii_media.ifm_list,
   4865 			      ifm_list) {
   4866 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
   4867 					continue;
   4868 				ife->ifm_aux = tm;
   4869 			}
   4870 			break;
   4871 
   4872 		case TULIP_ROM_MB_21142_SIA:
   4873 			tlp_get_minst(sc);
   4874 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_SIA;
   4875 
   4876 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
   4877 
   4878 			tm->tm_type = TULIP_ROM_MB_21142_SIA;
   4879 			tm->tm_get = tlp_sia_get;
   4880 			tm->tm_set = tlp_sia_set;
   4881 
   4882 			/* First is the media type code. */
   4883 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4884 			    TULIP_ROM_MB_MEDIA_CODE);
   4885 			if (tsti == NULL) {
   4886 				/* Invalid media code. */
   4887 				free(tm, M_DEVBUF);
   4888 				break;
   4889 			}
   4890 
   4891 			/* Get defaults. */
   4892 			tlp_srom_media_info(sc, tsti, tm);
   4893 
   4894 			/*
   4895 			 * Override our default SIA settings if the
   4896 			 * SROM contains its own.
   4897 			 */
   4898 			if (cp[0] & 0x40) {
   4899 				tm->tm_siaconn = TULIP_ROM_GETW(cp, 1);
   4900 				tm->tm_siatxrx = TULIP_ROM_GETW(cp, 3);
   4901 				tm->tm_siagen  = TULIP_ROM_GETW(cp, 5);
   4902 				cp += 7;
   4903 			} else
   4904 				cp++;
   4905 
   4906 			/* Next is GPIO control/data. */
   4907 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 0) << 16;
   4908 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 2) << 16;
   4909 
   4910 			ifmedia_add(&mii->mii_media,
   4911 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4912 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4913 			break;
   4914 
   4915 		case TULIP_ROM_MB_21142_MII:
   4916 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_MII;
   4917 
   4918 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
   4919 
   4920 			tm->tm_type = TULIP_ROM_MB_21142_MII;
   4921 			tm->tm_get = tlp_mii_getmedia;
   4922 			tm->tm_set = tlp_mii_setmedia;
   4923 			tm->tm_opmode = OPMODE_PS;
   4924 
   4925 			if (sc->sc_reset == NULL)
   4926 				sc->sc_reset = tlp_21142_reset;
   4927 
   4928 			/* First is the PHY number. */
   4929 			tm->tm_phyno = *cp++;
   4930 
   4931 			/* Next is the MII select sequence length and offset. */
   4932 			tm->tm_gp_length = *cp++;
   4933 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
   4934 			cp += tm->tm_gp_length * 2;
   4935 
   4936 			/* Next is the MII reset sequence length and offset. */
   4937 			tm->tm_reset_length = *cp++;
   4938 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
   4939 			cp += tm->tm_reset_length * 2;
   4940 
   4941 			/*
   4942 			 * The following items are left in the media block
   4943 			 * that we don't particularly care about:
   4944 			 *
   4945 			 *	capabilities		W
   4946 			 *	advertisement		W
   4947 			 *	full duplex		W
   4948 			 *	tx threshold		W
   4949 			 *	MII interrupt		W
   4950 			 *
   4951 			 * These appear to be bits in the PHY registers,
   4952 			 * which our MII code handles on its own.
   4953 			 */
   4954 
   4955 			/*
   4956 			 * Before we probe the MII bus, we need to reset
   4957 			 * it and issue the selection sequence.
   4958 			 */
   4959 
   4960 			cp = &sc->sc_srom[tm->tm_reset_offset];
   4961 			for (i = 0; i < tm->tm_reset_length; i++, cp += 2) {
   4962 				delay(10);
   4963 				TULIP_WRITE(sc, CSR_SIAGEN,
   4964 				    TULIP_ROM_GETW(cp, 0) << 16);
   4965 			}
   4966 
   4967 			cp = &sc->sc_srom[tm->tm_gp_offset];
   4968 			for (i = 0; i < tm->tm_gp_length; i++, cp += 2) {
   4969 				delay(10);
   4970 				TULIP_WRITE(sc, CSR_SIAGEN,
   4971 				    TULIP_ROM_GETW(cp, 0) << 16);
   4972 			}
   4973 
   4974 			/* If there were no sequences, just lower the pins. */
   4975 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   4976 				delay(10);
   4977 				TULIP_WRITE(sc, CSR_SIAGEN, 0);
   4978 			}
   4979 
   4980 			/*
   4981 			 * Now, probe the MII for the PHY.  Note, we know
   4982 			 * the location of the PHY on the bus, but we don't
   4983 			 * particularly care; the MII code just likes to
   4984 			 * search the whole thing anyhow.
   4985 			 */
   4986 			mii_attach(sc->sc_dev, mii, 0xffffffff,
   4987 			    MII_PHY_ANY, tm->tm_phyno, 0);
   4988 
   4989 			/*
   4990 			 * Now, search for the PHY we hopefully just
   4991 			 * configured.  If it's not configured into the
   4992 			 * kernel, we lose.  The PHY's default media always
   4993 			 * takes priority.
   4994 			 */
   4995 			LIST_FOREACH(phy, &mii->mii_phys, mii_list) {
   4996 				if (phy->mii_offset == tm->tm_phyno)
   4997 					break;
   4998 			}
   4999 			if (phy == NULL) {
   5000 				aprint_error_dev(sc->sc_dev,
   5001 				    "unable to configure MII\n");
   5002 				break;
   5003 			}
   5004 
   5005 			sc->sc_flags |= TULIPF_HAS_MII;
   5006 			sc->sc_tick = tlp_mii_tick;
   5007 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
   5008 			    phy->mii_inst);
   5009 
   5010 			/*
   5011 			 * Okay, now that we've found the PHY and the MII
   5012 			 * layer has added all of the media associated
   5013 			 * with that PHY, we need to traverse the media
   5014 			 * list, and add our `tm' to each entry's `aux'
   5015 			 * pointer.
   5016 			 *
   5017 			 * We do this by looking for media with our
   5018 			 * PHY's `instance'.
   5019 			 */
   5020 			TAILQ_FOREACH(ife, &mii->mii_media.ifm_list,
   5021 			      ifm_list) {
   5022 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
   5023 					continue;
   5024 				ife->ifm_aux = tm;
   5025 			}
   5026 			break;
   5027 
   5028 		case TULIP_ROM_MB_21143_SYM:
   5029 			tlp_get_minst(sc);
   5030 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21143_SYM;
   5031 
   5032 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
   5033 
   5034 			tm->tm_type = TULIP_ROM_MB_21143_SYM;
   5035 			tm->tm_get = tlp_sia_get;
   5036 			tm->tm_set = tlp_sia_set;
   5037 
   5038 			/* First is the media type code. */
   5039 			tsti = tlp_srom_to_ifmedia(cp[0] &
   5040 			    TULIP_ROM_MB_MEDIA_CODE);
   5041 			if (tsti == NULL) {
   5042 				/* Invalid media code. */
   5043 				free(tm, M_DEVBUF);
   5044 				break;
   5045 			}
   5046 
   5047 			/* Get defaults. */
   5048 			tlp_srom_media_info(sc, tsti, tm);
   5049 
   5050 			/* Next is GPIO control/data. */
   5051 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 1) << 16;
   5052 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 3) << 16;
   5053 
   5054 			/*
   5055 			 * Next is a word containing OPMODE information
   5056 			 * and info on how to detect if this media is
   5057 			 * active.
   5058 			 */
   5059 			word = TULIP_ROM_GETW(cp, 5);
   5060 			tm->tm_opmode &= OPMODE_FD;
   5061 			tm->tm_opmode |= TULIP_ROM_MB_OPMODE(word);
   5062 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
   5063 				tm->tm_actmask =
   5064 				    TULIP_ROM_MB_BITPOS(word);
   5065 				tm->tm_actdata =
   5066 				    (word & TULIP_ROM_MB_POLARITY) ?
   5067 				    0 : tm->tm_actmask;
   5068 			}
   5069 
   5070 			ifmedia_add(&mii->mii_media,
   5071 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   5072 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   5073 			break;
   5074 
   5075 		case TULIP_ROM_MB_21143_RESET:
   5076 			aprint_normal_dev(sc->sc_dev, "21143 reset block\n");
   5077 			break;
   5078 
   5079 		default:
   5080 			aprint_error_dev(sc->sc_dev,
   5081 			    "unknown ISV media block type 0x%02x\n", type);
   5082 		}
   5083 	}
   5084 
   5085 	/*
   5086 	 * Deal with the case where no media is configured.
   5087 	 */
   5088 	if (TAILQ_FIRST(&mii->mii_media.ifm_list) == NULL) {
   5089 		aprint_error_dev(sc->sc_dev, "no media found!\n");
   5090 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   5091 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   5092 		return;
   5093 	}
   5094 
   5095 	/*
   5096 	 * Pick the default media.
   5097 	 */
   5098 	if (miidef != 0)
   5099 		defmedia = miidef;
   5100 	else {
   5101 		switch (sc->sc_chip) {
   5102 		case TULIP_CHIP_21140:
   5103 		case TULIP_CHIP_21140A:
   5104 			/* XXX should come from SROM */
   5105 			defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
   5106 			if (ifmedia_match(&mii->mii_media, defmedia,
   5107 				mii->mii_media.ifm_mask) == NULL) {
   5108 				/*
   5109 				 * There is not a 10baseT media.
   5110 				 * Fall back to the first found one.
   5111 				 */
   5112 				ife = TAILQ_FIRST(&mii->mii_media.ifm_list);
   5113 				defmedia = ife->ifm_media;
   5114 			}
   5115 			break;
   5116 
   5117 		case TULIP_CHIP_21142:
   5118 		case TULIP_CHIP_21143:
   5119 		case TULIP_CHIP_MX98713A:
   5120 		case TULIP_CHIP_MX98715:
   5121 		case TULIP_CHIP_MX98715A:
   5122 		case TULIP_CHIP_MX98715AEC_X:
   5123 		case TULIP_CHIP_MX98725:
   5124 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
   5125 			tm->tm_name = "auto";
   5126 			tm->tm_get = tlp_2114x_nway_get;
   5127 			tm->tm_set = tlp_2114x_nway_set;
   5128 
   5129 			defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0);
   5130 			ifmedia_add(&mii->mii_media, defmedia, 0, tm);
   5131 
   5132 			sc->sc_statchg = tlp_2114x_nway_statchg;
   5133 			sc->sc_tick = tlp_2114x_nway_tick;
   5134 			break;
   5135 
   5136 		default:
   5137 			defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
   5138 			break;
   5139 		}
   5140 	}
   5141 
   5142 	ifmedia_set(&mii->mii_media, defmedia);
   5143 
   5144 	/*
   5145 	 * Display any non-MII media we've located.
   5146 	 */
   5147 	if (sc->sc_media_seen &
   5148 	    ~((1 << TULIP_ROM_MB_21140_MII) | (1 << TULIP_ROM_MB_21142_MII)))
   5149 		tlp_print_media(sc);
   5150 
   5151 	tlp_sia_fixup(sc);
   5152 }
   5153 
   5154 static void
   5155 tlp_2114x_nway_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
   5156 {
   5157 
   5158 	(void) tlp_2114x_nway_service(sc, MII_POLLSTAT);
   5159 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   5160 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   5161 }
   5162 
   5163 static int
   5164 tlp_2114x_nway_set(struct tulip_softc *sc)
   5165 {
   5166 
   5167 	return tlp_2114x_nway_service(sc, MII_MEDIACHG);
   5168 }
   5169 
   5170 static void
   5171 tlp_2114x_nway_statchg(struct ifnet *ifp)
   5172 {
   5173 	struct tulip_softc *sc = ifp->if_softc;
   5174 	struct mii_data *mii = &sc->sc_mii;
   5175 	struct ifmedia_entry *ife;
   5176 
   5177 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)
   5178 		return;
   5179 
   5180 	if ((ife = ifmedia_match(&mii->mii_media, mii->mii_media_active,
   5181 	    mii->mii_media.ifm_mask)) == NULL) {
   5182 		printf("tlp_2114x_nway_statchg: no match for media 0x%x/0x%x\n",
   5183 		    mii->mii_media_active, ~mii->mii_media.ifm_mask);
   5184 		panic("tlp_2114x_nway_statchg");
   5185 	}
   5186 
   5187 	tlp_sia_media(sc, ife);
   5188 }
   5189 
   5190 static void
   5191 tlp_2114x_nway_tick(void *arg)
   5192 {
   5193 	struct tulip_softc *sc = arg;
   5194 	struct mii_data *mii = &sc->sc_mii;
   5195 	int s, ticks;
   5196 
   5197 	if (!device_is_active(sc->sc_dev))
   5198 		return;
   5199 
   5200 	s = splnet();
   5201 	tlp_2114x_nway_service(sc, MII_TICK);
   5202 	if ((sc->sc_flags & TULIPF_LINK_UP) == 0 &&
   5203 	    (mii->mii_media_status & IFM_ACTIVE) != 0 &&
   5204 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
   5205 		sc->sc_flags |= TULIPF_LINK_UP;
   5206 		tlp_start(&sc->sc_ethercom.ec_if);
   5207 	} else if ((sc->sc_flags & TULIPF_LINK_UP) != 0 &&
   5208 	    (mii->mii_media_status & IFM_ACTIVE) == 0) {
   5209 		sc->sc_flags &= ~TULIPF_LINK_UP;
   5210 	}
   5211 	splx(s);
   5212 
   5213 	if ((sc->sc_flags & TULIPF_LINK_UP) == 0)
   5214 		ticks = hz >> 3;
   5215 	else
   5216 		ticks = hz;
   5217 	callout_reset(&sc->sc_tick_callout, ticks, tlp_2114x_nway_tick, sc);
   5218 }
   5219 
   5220 /*
   5221  * Support for the 2114X internal NWay block.  This is constructed
   5222  * somewhat like a PHY driver for simplicity.
   5223  */
   5224 
   5225 static int
   5226 tlp_2114x_nway_service(struct tulip_softc *sc, int cmd)
   5227 {
   5228 	struct mii_data *mii = &sc->sc_mii;
   5229 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   5230 
   5231 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
   5232 		return 0;
   5233 
   5234 	switch (cmd) {
   5235 	case MII_POLLSTAT:
   5236 		/* Nothing special to do here. */
   5237 		break;
   5238 
   5239 	case MII_MEDIACHG:
   5240 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   5241 		case IFM_AUTO:
   5242 			goto restart;
   5243 		default:
   5244 			/* Manual setting doesn't go through here. */
   5245 			printf("tlp_2114x_nway_service: oops!\n");
   5246 			return EINVAL;
   5247 		}
   5248 		break;
   5249 
   5250 	case MII_TICK:
   5251 		/*
   5252 		 * Only used for autonegotiation.
   5253 		 */
   5254 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   5255 			break;
   5256 
   5257 		/*
   5258 		 * Check to see if we have link.  If we do, we don't
   5259 		 * need to restart the autonegotiation process.
   5260 		 */
   5261 #if 0
   5262 		if (mii->mii_media_status & IFM_ACTIVE)
   5263 #else
   5264 		if (sc->sc_flags & TULIPF_LINK_UP)
   5265 #endif
   5266 			break;
   5267 
   5268 		/*
   5269 		 * Only retry autonegotiation every 5 seconds.
   5270 		 */
   5271 		if (++sc->sc_nway_ticks != (5 << 3))
   5272 			break;
   5273 
   5274 	restart:
   5275 		sc->sc_nway_ticks = 0;
   5276 		ife->ifm_data = IFM_NONE;
   5277 		tlp_2114x_nway_auto(sc);
   5278 		break;
   5279 	}
   5280 
   5281 	/* Update the media status. */
   5282 	tlp_2114x_nway_status(sc);
   5283 
   5284 	/*
   5285 	 * Callback if something changed.  Manually configuration goes through
   5286 	 * tlp_sia_set() anyway, so ignore that here.
   5287 	 */
   5288 	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO &&
   5289 	    ife->ifm_data != mii->mii_media_active) {
   5290 		(*sc->sc_statchg)(mii->mii_ifp);
   5291 		ife->ifm_data = mii->mii_media_active;
   5292 	}
   5293 	return 0;
   5294 }
   5295 
   5296 static void
   5297 tlp_2114x_nway_auto(struct tulip_softc *sc)
   5298 {
   5299 	uint32_t siastat, siatxrx;
   5300 
   5301 	tlp_idle(sc, OPMODE_ST | OPMODE_SR);
   5302 
   5303 	sc->sc_opmode &= ~(OPMODE_PS | OPMODE_PCS | OPMODE_SCR | OPMODE_FD);
   5304 	sc->sc_opmode |= OPMODE_TTM | OPMODE_HBD;
   5305 	siatxrx = 0xffbf;		/* XXX magic number */
   5306 
   5307 	/* Compute the link code word to advertise. */
   5308 	if (sc->sc_sia_cap & BMSR_100T4)
   5309 		siatxrx |= SIATXRX_T4;
   5310 	if (sc->sc_sia_cap & BMSR_100TXFDX)
   5311 		siatxrx |= SIATXRX_TXF;
   5312 	if (sc->sc_sia_cap & BMSR_100TXHDX)
   5313 		siatxrx |= SIATXRX_THX;
   5314 	if (sc->sc_sia_cap & BMSR_10TFDX)
   5315 		sc->sc_opmode |= OPMODE_FD;
   5316 	if (sc->sc_sia_cap & BMSR_10THDX)
   5317 		siatxrx |= SIATXRX_TH;
   5318 
   5319 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   5320 
   5321 	TULIP_WRITE(sc, CSR_SIACONN, 0);
   5322 	delay(1000);
   5323 	TULIP_WRITE(sc, CSR_SIATXRX, siatxrx);
   5324 	TULIP_WRITE(sc, CSR_SIACONN, SIACONN_SRL);
   5325 
   5326 	siastat = TULIP_READ(sc, CSR_SIASTAT);
   5327 	siastat &= ~(SIASTAT_ANS | SIASTAT_LPC | SIASTAT_TRA | SIASTAT_ARA |
   5328 		     SIASTAT_LS100 | SIASTAT_LS10 | SIASTAT_MRA);
   5329 	siastat |= SIASTAT_ANS_TXDIS;
   5330 	TULIP_WRITE(sc, CSR_SIASTAT, siastat);
   5331 }
   5332 
   5333 static void
   5334 tlp_2114x_nway_status(struct tulip_softc *sc)
   5335 {
   5336 	struct mii_data *mii = &sc->sc_mii;
   5337 	uint32_t siatxrx, siastat, anlpar;
   5338 
   5339 	mii->mii_media_status = IFM_AVALID;
   5340 	mii->mii_media_active = IFM_ETHER;
   5341 
   5342 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
   5343 		return;
   5344 
   5345 	siastat = TULIP_READ(sc, CSR_SIASTAT);
   5346 	siatxrx = TULIP_READ(sc, CSR_SIATXRX);
   5347 
   5348 	if (siatxrx & SIATXRX_ANE) {
   5349 		if ((siastat & SIASTAT_ANS) != SIASTAT_ANS_FLPGOOD) {
   5350 			/* Erg, still trying, I guess... */
   5351 			mii->mii_media_active |= IFM_NONE;
   5352 			return;
   5353 		}
   5354 
   5355 		if (~siastat & (SIASTAT_LS10 | SIASTAT_LS100))
   5356 			mii->mii_media_status |= IFM_ACTIVE;
   5357 
   5358 		if (siastat & SIASTAT_LPN) {
   5359 			anlpar = SIASTAT_GETLPC(siastat);
   5360 			if (anlpar & ANLPAR_T4 &&
   5361 			    sc->sc_sia_cap & BMSR_100T4)
   5362 				mii->mii_media_active |= IFM_100_T4;
   5363 			else if (anlpar & ANLPAR_TX_FD &&
   5364 				 sc->sc_sia_cap & BMSR_100TXFDX)
   5365 				mii->mii_media_active |= IFM_100_TX | IFM_FDX;
   5366 			else if (anlpar & ANLPAR_TX &&
   5367 				 sc->sc_sia_cap & BMSR_100TXHDX)
   5368 				mii->mii_media_active |= IFM_100_TX;
   5369 			else if (anlpar & ANLPAR_10_FD &&
   5370 				 sc->sc_sia_cap & BMSR_10TFDX)
   5371 				mii->mii_media_active |= IFM_10_T | IFM_FDX;
   5372 			else if (anlpar & ANLPAR_10 &&
   5373 				 sc->sc_sia_cap & BMSR_10THDX)
   5374 				mii->mii_media_active |= IFM_10_T;
   5375 			else
   5376 				mii->mii_media_active |= IFM_NONE;
   5377 		} else {
   5378 			/*
   5379 			 * If the other side doesn't support NWAY, then the
   5380 			 * best we can do is determine if we have a 10Mbps or
   5381 			 * 100Mbps link. There's no way to know if the link
   5382 			 * is full or half duplex, so we default to half duplex
   5383 			 * and hope that the user is clever enough to manually
   5384 			 * change the media settings if we're wrong.
   5385 			 */
   5386 			if ((siastat & SIASTAT_LS100) == 0)
   5387 				mii->mii_media_active |= IFM_100_TX;
   5388 			else if ((siastat & SIASTAT_LS10) == 0)
   5389 				mii->mii_media_active |= IFM_10_T;
   5390 			else
   5391 				mii->mii_media_active |= IFM_NONE;
   5392 		}
   5393 	} else {
   5394 		if (~siastat & (SIASTAT_LS10 | SIASTAT_LS100))
   5395 			mii->mii_media_status |= IFM_ACTIVE;
   5396 
   5397 		if (sc->sc_opmode & OPMODE_TTM)
   5398 			mii->mii_media_active |= IFM_10_T;
   5399 		else
   5400 			mii->mii_media_active |= IFM_100_TX;
   5401 		if (sc->sc_opmode & OPMODE_FD)
   5402 			mii->mii_media_active |= IFM_FDX;
   5403 	}
   5404 }
   5405 
   5406 static void
   5407 tlp_2114x_isv_tmsw_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
   5408 {
   5409 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   5410 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   5411 
   5412 	(*tm->tm_get)(sc, ifmr);
   5413 }
   5414 
   5415 static int
   5416 tlp_2114x_isv_tmsw_set(struct tulip_softc *sc)
   5417 {
   5418 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   5419 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   5420 
   5421 	/*
   5422 	 * Check to see if we need to reset the chip, and do it.  The
   5423 	 * reset path will get the OPMODE register right the next
   5424 	 * time through.
   5425 	 */
   5426 	if (TULIP_MEDIA_NEEDSRESET(sc, tm->tm_opmode))
   5427 		return tlp_init(&sc->sc_ethercom.ec_if);
   5428 
   5429 	return (*tm->tm_set)(sc);
   5430 }
   5431 
   5432 /*
   5433  * MII-on-SIO media switch.  Handles only MII attached to the SIO.
   5434  */
   5435 static void	tlp_sio_mii_tmsw_init(struct tulip_softc *);
   5436 
   5437 const struct tulip_mediasw tlp_sio_mii_mediasw = {
   5438 	tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   5439 };
   5440 
   5441 static void
   5442 tlp_sio_mii_tmsw_init(struct tulip_softc *sc)
   5443 {
   5444 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   5445 	struct mii_data * const mii = &sc->sc_mii;
   5446 
   5447 	/*
   5448 	 * We don't attach any media info structures to the ifmedia
   5449 	 * entries, so if we're using a pre-init function that needs
   5450 	 * that info, override it to one that doesn't.
   5451 	 */
   5452 	if (sc->sc_preinit == tlp_2114x_preinit)
   5453 		sc->sc_preinit = tlp_2114x_mii_preinit;
   5454 
   5455 	mii->mii_ifp = ifp;
   5456 	mii->mii_readreg = tlp_bitbang_mii_readreg;
   5457 	mii->mii_writereg = tlp_bitbang_mii_writereg;
   5458 	mii->mii_statchg = sc->sc_statchg;
   5459 	sc->sc_ethercom.ec_mii = mii;
   5460 	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
   5461 	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
   5462 	    MII_OFFSET_ANY, 0);
   5463 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   5464 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   5465 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   5466 	} else {
   5467 		sc->sc_flags |= TULIPF_HAS_MII;
   5468 		sc->sc_tick = tlp_mii_tick;
   5469 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   5470 	}
   5471 }
   5472 
   5473 /*
   5474  * Lite-On PNIC media switch.  Must handle MII or internal NWAY.
   5475  */
   5476 static void	tlp_pnic_tmsw_init(struct tulip_softc *);
   5477 static void	tlp_pnic_tmsw_get(struct tulip_softc *, struct ifmediareq *);
   5478 static int	tlp_pnic_tmsw_set(struct tulip_softc *);
   5479 
   5480 const struct tulip_mediasw tlp_pnic_mediasw = {
   5481 	tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
   5482 };
   5483 
   5484 static void	tlp_pnic_nway_statchg(struct ifnet *);
   5485 static void	tlp_pnic_nway_tick(void *);
   5486 static int	tlp_pnic_nway_service(struct tulip_softc *, int);
   5487 static void	tlp_pnic_nway_reset(struct tulip_softc *);
   5488 static int	tlp_pnic_nway_auto(struct tulip_softc *, int);
   5489 static void	tlp_pnic_nway_auto_timeout(void *);
   5490 static void	tlp_pnic_nway_status(struct tulip_softc *);
   5491 static void	tlp_pnic_nway_acomp(struct tulip_softc *);
   5492 
   5493 static void
   5494 tlp_pnic_tmsw_init(struct tulip_softc *sc)
   5495 {
   5496 	struct mii_data * const mii = &sc->sc_mii;
   5497 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   5498 	const char *sep = "";
   5499 
   5500 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
   5501 #define	PRINT(str)	aprint_normal("%s%s", sep, str); sep = ", "
   5502 
   5503 	mii->mii_ifp = ifp;
   5504 	mii->mii_readreg = tlp_pnic_mii_readreg;
   5505 	mii->mii_writereg = tlp_pnic_mii_writereg;
   5506 	mii->mii_statchg = sc->sc_statchg;
   5507 	sc->sc_ethercom.ec_mii = mii;
   5508 	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
   5509 	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
   5510 	    MII_OFFSET_ANY, 0);
   5511 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   5512 		/* XXX What about AUI/BNC support? */
   5513 		aprint_normal_dev(sc->sc_dev, "");
   5514 
   5515 		tlp_pnic_nway_reset(sc);
   5516 
   5517 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
   5518 		    PNIC_NWAY_TW | PNIC_NWAY_CAP10T);
   5519 		PRINT("10baseT");
   5520 
   5521 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
   5522 		    PNIC_NWAY_TW | PNIC_NWAY_FD | PNIC_NWAY_CAP10TFDX);
   5523 		PRINT("10baseT-FDX");
   5524 
   5525 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
   5526 		    PNIC_NWAY_TW | PNIC_NWAY_100 | PNIC_NWAY_CAP100TX);
   5527 		PRINT("100baseTX");
   5528 
   5529 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
   5530 		    PNIC_NWAY_TW | PNIC_NWAY_100 | PNIC_NWAY_FD |
   5531 		    PNIC_NWAY_CAP100TXFDX);
   5532 		PRINT("100baseTX-FDX");
   5533 
   5534 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
   5535 		    PNIC_NWAY_TW | PNIC_NWAY_RN | PNIC_NWAY_NW |
   5536 		    PNIC_NWAY_CAP10T | PNIC_NWAY_CAP10TFDX |
   5537 		    PNIC_NWAY_CAP100TXFDX | PNIC_NWAY_CAP100TX);
   5538 		PRINT("auto");
   5539 
   5540 		aprint_normal("\n");
   5541 
   5542 		sc->sc_statchg = tlp_pnic_nway_statchg;
   5543 		sc->sc_tick = tlp_pnic_nway_tick;
   5544 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   5545 	} else {
   5546 		sc->sc_flags |= TULIPF_HAS_MII;
   5547 		sc->sc_tick = tlp_mii_tick;
   5548 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   5549 	}
   5550 
   5551 #undef ADD
   5552 #undef PRINT
   5553 }
   5554 
   5555 static void
   5556 tlp_pnic_tmsw_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
   5557 {
   5558 	struct mii_data *mii = &sc->sc_mii;
   5559 
   5560 	if (sc->sc_flags & TULIPF_HAS_MII)
   5561 		tlp_mii_getmedia(sc, ifmr);
   5562 	else {
   5563 		mii->mii_media_status = 0;
   5564 		mii->mii_media_active = IFM_NONE;
   5565 		tlp_pnic_nway_service(sc, MII_POLLSTAT);
   5566 		ifmr->ifm_status = mii->mii_media_status;
   5567 		ifmr->ifm_active = mii->mii_media_active;
   5568 	}
   5569 }
   5570 
   5571 static int
   5572 tlp_pnic_tmsw_set(struct tulip_softc *sc)
   5573 {
   5574 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   5575 	struct mii_data *mii = &sc->sc_mii;
   5576 
   5577 	if (sc->sc_flags & TULIPF_HAS_MII) {
   5578 		/*
   5579 		 * Make sure the built-in Tx jabber timer is disabled.
   5580 		 */
   5581 		TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);
   5582 
   5583 		return tlp_mii_setmedia(sc);
   5584 	}
   5585 
   5586 	if (ifp->if_flags & IFF_UP) {
   5587 		mii->mii_media_status = 0;
   5588 		mii->mii_media_active = IFM_NONE;
   5589 		return tlp_pnic_nway_service(sc, MII_MEDIACHG);
   5590 	}
   5591 
   5592 	return 0;
   5593 }
   5594 
   5595 static void
   5596 tlp_pnic_nway_statchg(struct ifnet *ifp)
   5597 {
   5598 	struct tulip_softc *sc = ifp->if_softc;
   5599 
   5600 	/* Idle the transmit and receive processes. */
   5601 	tlp_idle(sc, OPMODE_ST | OPMODE_SR);
   5602 
   5603 	sc->sc_opmode &= ~(OPMODE_TTM | OPMODE_FD | OPMODE_PS | OPMODE_PCS |
   5604 	    OPMODE_SCR | OPMODE_HBD);
   5605 
   5606 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
   5607 		sc->sc_opmode |= OPMODE_TTM;
   5608 		TULIP_WRITE(sc, CSR_GPP,
   5609 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) |
   5610 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
   5611 	} else {
   5612 		sc->sc_opmode |= OPMODE_PS |OPMODE_PCS |OPMODE_SCR |OPMODE_HBD;
   5613 		TULIP_WRITE(sc, CSR_GPP,
   5614 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) |
   5615 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
   5616 	}
   5617 
   5618 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   5619 		sc->sc_opmode |= OPMODE_FD | OPMODE_HBD;
   5620 
   5621 	/*
   5622 	 * Write new OPMODE bits.  This also restarts the transmit
   5623 	 * and receive processes.
   5624 	 */
   5625 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   5626 }
   5627 
   5628 static void
   5629 tlp_pnic_nway_tick(void *arg)
   5630 {
   5631 	struct tulip_softc *sc = arg;
   5632 	int s;
   5633 
   5634 	if (!device_is_active(sc->sc_dev))
   5635 		return;
   5636 
   5637 	s = splnet();
   5638 	tlp_pnic_nway_service(sc, MII_TICK);
   5639 	splx(s);
   5640 
   5641 	callout_reset(&sc->sc_tick_callout, hz, tlp_pnic_nway_tick, sc);
   5642 }
   5643 
   5644 /*
   5645  * Support for the Lite-On PNIC internal NWay block.  This is constructed
   5646  * somewhat like a PHY driver for simplicity.
   5647  */
   5648 
   5649 static int
   5650 tlp_pnic_nway_service(struct tulip_softc *sc, int cmd)
   5651 {
   5652 	struct mii_data *mii = &sc->sc_mii;
   5653 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   5654 
   5655 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
   5656 		return 0;
   5657 
   5658 	switch (cmd) {
   5659 	case MII_POLLSTAT:
   5660 		/* Nothing special to do here. */
   5661 		break;
   5662 
   5663 	case MII_MEDIACHG:
   5664 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   5665 		case IFM_AUTO:
   5666 			(void) tlp_pnic_nway_auto(sc, 1);
   5667 			break;
   5668 		case IFM_100_T4:
   5669 			/*
   5670 			 * XXX Not supported as a manual setting right now.
   5671 			 */
   5672 			return EINVAL;
   5673 		default:
   5674 			/*
   5675 			 * NWAY register data is stored in the ifmedia entry.
   5676 			 */
   5677 			TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   5678 		}
   5679 		break;
   5680 
   5681 	case MII_TICK:
   5682 		/*
   5683 		 * Only used for autonegotiation.
   5684 		 */
   5685 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   5686 			return 0;
   5687 
   5688 		/*
   5689 		 * Check to see if we have link.  If we do, we don't
   5690 		 * need to restart the autonegotiation process.
   5691 		 */
   5692 		if (sc->sc_flags & TULIPF_LINK_UP)
   5693 			return 0;
   5694 
   5695 		/*
   5696 		 * Only retry autonegotiation every 5 seconds.
   5697 		 */
   5698 		if (++sc->sc_nway_ticks != 5)
   5699 			return 0;
   5700 
   5701 		sc->sc_nway_ticks = 0;
   5702 		tlp_pnic_nway_reset(sc);
   5703 		if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
   5704 			return 0;
   5705 		break;
   5706 	}
   5707 
   5708 	/* Update the media status. */
   5709 	tlp_pnic_nway_status(sc);
   5710 
   5711 	/* Callback if something changed. */
   5712 	if ((sc->sc_nway_active == NULL ||
   5713 	     sc->sc_nway_active->ifm_media != mii->mii_media_active) ||
   5714 	    cmd == MII_MEDIACHG) {
   5715 		(*sc->sc_statchg)(mii->mii_ifp);
   5716 		tlp_nway_activate(sc, mii->mii_media_active);
   5717 	}
   5718 	return 0;
   5719 }
   5720 
   5721 static void
   5722 tlp_pnic_nway_reset(struct tulip_softc *sc)
   5723 {
   5724 
   5725 	TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
   5726 	delay(100);
   5727 	TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
   5728 }
   5729 
   5730 static int
   5731 tlp_pnic_nway_auto(struct tulip_softc *sc, int waitfor)
   5732 {
   5733 	struct mii_data *mii = &sc->sc_mii;
   5734 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   5735 	uint32_t reg;
   5736 	int i;
   5737 
   5738 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
   5739 		TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   5740 
   5741 	if (waitfor) {
   5742 		/* Wait 500ms for it to complete. */
   5743 		for (i = 0; i < 500; i++) {
   5744 			reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5745 			if (reg & PNIC_NWAY_LPAR_MASK) {
   5746 				tlp_pnic_nway_acomp(sc);
   5747 				return 0;
   5748 			}
   5749 			delay(1000);
   5750 		}
   5751 #if 0
   5752 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   5753 			aprint_error_dev(sc->sc_dev,
   5754 			    "autonegotiation failed to complete\n");
   5755 #endif
   5756 
   5757 		/*
   5758 		 * Don't need to worry about clearing DOINGAUTO.
   5759 		 * If that's set, a timeout is pending, and it will
   5760 		 * clear the flag.
   5761 		 */
   5762 		return EIO;
   5763 	}
   5764 
   5765 	/*
   5766 	 * Just let it finish asynchronously.  This is for the benefit of
   5767 	 * the tick handler driving autonegotiation.  Don't want 500ms
   5768 	 * delays all the time while the system is running!
   5769 	 */
   5770 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
   5771 		sc->sc_flags |= TULIPF_DOINGAUTO;
   5772 		callout_reset(&sc->sc_nway_callout, hz >> 1,
   5773 		    tlp_pnic_nway_auto_timeout, sc);
   5774 	}
   5775 	return EJUSTRETURN;
   5776 }
   5777 
   5778 static void
   5779 tlp_pnic_nway_auto_timeout(void *arg)
   5780 {
   5781 	struct tulip_softc *sc = arg;
   5782 	/* uint32_t reg; */
   5783 	int s;
   5784 
   5785 	s = splnet();
   5786 	sc->sc_flags &= ~TULIPF_DOINGAUTO;
   5787 	/* reg = */
   5788 	TULIP_READ(sc, CSR_PNIC_NWAY);
   5789 #if 0
   5790 	if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   5791 		aprint_error_dev(sc->sc_dev,
   5792 		    "autonegotiation failed to complete\n");
   5793 #endif
   5794 
   5795 	tlp_pnic_nway_acomp(sc);
   5796 
   5797 	/* Update the media status. */
   5798 	(void)tlp_pnic_nway_service(sc, MII_POLLSTAT);
   5799 	splx(s);
   5800 }
   5801 
   5802 static void
   5803 tlp_pnic_nway_status(struct tulip_softc *sc)
   5804 {
   5805 	struct mii_data *mii = &sc->sc_mii;
   5806 	uint32_t reg;
   5807 
   5808 	mii->mii_media_status = IFM_AVALID;
   5809 	mii->mii_media_active = IFM_ETHER;
   5810 
   5811 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5812 
   5813 	if (sc->sc_flags & TULIPF_LINK_UP)
   5814 		mii->mii_media_status |= IFM_ACTIVE;
   5815 
   5816 	if (reg & PNIC_NWAY_NW) {
   5817 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
   5818 			/* Erg, still trying, I guess... */
   5819 			mii->mii_media_active |= IFM_NONE;
   5820 			return;
   5821 		}
   5822 
   5823 #if 0
   5824 		if (reg & PNIC_NWAY_LPAR100T4)
   5825 			mii->mii_media_active |= IFM_100_T4;
   5826 		else
   5827 #endif
   5828 		if (reg & PNIC_NWAY_LPAR100TXFDX)
   5829 			mii->mii_media_active |= IFM_100_TX | IFM_FDX;
   5830 		else if (reg & PNIC_NWAY_LPAR100TX)
   5831 			mii->mii_media_active |= IFM_100_TX;
   5832 		else if (reg & PNIC_NWAY_LPAR10TFDX)
   5833 			mii->mii_media_active |= IFM_10_T | IFM_FDX;
   5834 		else if (reg & PNIC_NWAY_LPAR10T)
   5835 			mii->mii_media_active |= IFM_10_T;
   5836 		else
   5837 			mii->mii_media_active |= IFM_NONE;
   5838 	} else {
   5839 		if (reg & PNIC_NWAY_100)
   5840 			mii->mii_media_active |= IFM_100_TX;
   5841 		else
   5842 			mii->mii_media_active |= IFM_10_T;
   5843 		if (reg & PNIC_NWAY_FD)
   5844 			mii->mii_media_active |= IFM_FDX;
   5845 	}
   5846 }
   5847 
   5848 static void
   5849 tlp_pnic_nway_acomp(struct tulip_softc *sc)
   5850 {
   5851 	uint32_t reg;
   5852 
   5853 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   5854 	reg &= ~(PNIC_NWAY_FD | PNIC_NWAY_100 | PNIC_NWAY_RN);
   5855 
   5856 	if (reg & (PNIC_NWAY_LPAR100TXFDX | PNIC_NWAY_LPAR100TX))
   5857 		reg |= PNIC_NWAY_100;
   5858 	if (reg & (PNIC_NWAY_LPAR10TFDX | PNIC_NWAY_LPAR100TXFDX))
   5859 		reg |= PNIC_NWAY_FD;
   5860 
   5861 	TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
   5862 }
   5863 
   5864 /*
   5865  * Macronix PMAC and Lite-On PNIC-II media switch:
   5866  *
   5867  *	MX98713 and MX98713A		21140-like MII or GPIO media.
   5868  *
   5869  *	MX98713A			21143-like MII or SIA/SYM media.
   5870  *
   5871  *	MX98715, MX98715A, MX98725,	21143-like SIA/SYM media.
   5872  *	82C115, MX98715AEC-C, -E
   5873  *
   5874  * So, what we do here is fake MII-on-SIO or ISV media info, and
   5875  * use the ISV media switch get/set functions to handle the rest.
   5876  */
   5877 
   5878 static void	tlp_pmac_tmsw_init(struct tulip_softc *);
   5879 
   5880 const struct tulip_mediasw tlp_pmac_mediasw = {
   5881 	tlp_pmac_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
   5882 };
   5883 
   5884 const struct tulip_mediasw tlp_pmac_mii_mediasw = {
   5885 	tlp_pmac_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   5886 };
   5887 
   5888 static void
   5889 tlp_pmac_tmsw_init(struct tulip_softc *sc)
   5890 {
   5891 	struct mii_data * const mii = &sc->sc_mii;
   5892 	static const uint8_t media[] = {
   5893 		TULIP_ROM_MB_MEDIA_TP,
   5894 		TULIP_ROM_MB_MEDIA_TP_FDX,
   5895 		TULIP_ROM_MB_MEDIA_100TX,
   5896 		TULIP_ROM_MB_MEDIA_100TX_FDX,
   5897 	};
   5898 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   5899 	struct tulip_21x4x_media *tm;
   5900 
   5901 	mii->mii_ifp = ifp;
   5902 	mii->mii_readreg = tlp_bitbang_mii_readreg;
   5903 	mii->mii_writereg = tlp_bitbang_mii_writereg;
   5904 	mii->mii_statchg = sc->sc_statchg;
   5905 	sc->sc_ethercom.ec_mii = mii;
   5906 	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
   5907 	if (sc->sc_chip == TULIP_CHIP_MX98713 ||
   5908 	    sc->sc_chip == TULIP_CHIP_MX98713A) {
   5909 		mii_attach(sc->sc_dev, mii, 0xffffffff,
   5910 		    MII_PHY_ANY, MII_OFFSET_ANY, 0);
   5911 		if (LIST_FIRST(&mii->mii_phys) != NULL) {
   5912 			sc->sc_flags |= TULIPF_HAS_MII;
   5913 			sc->sc_tick = tlp_mii_tick;
   5914 			sc->sc_preinit = tlp_2114x_mii_preinit;
   5915 			sc->sc_mediasw = &tlp_pmac_mii_mediasw;
   5916 			ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   5917 			return;
   5918 		}
   5919 	}
   5920 
   5921 	switch (sc->sc_chip) {
   5922 	case TULIP_CHIP_MX98713:
   5923 		tlp_add_srom_media(sc, TULIP_ROM_MB_21140_GPR,
   5924 		    tlp_21140_gpio_get, tlp_21140_gpio_set, media, 4);
   5925 
   5926 		/*
   5927 		 * XXX Should implement auto-sense for this someday,
   5928 		 * XXX when we do the same for the 21140.
   5929 		 */
   5930 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_10_T);
   5931 		break;
   5932 
   5933 	default:
   5934 		tlp_add_srom_media(sc, TULIP_ROM_MB_21142_SIA,
   5935 		    tlp_sia_get, tlp_sia_set, media, 2);
   5936 		tlp_add_srom_media(sc, TULIP_ROM_MB_21143_SYM,
   5937 		    tlp_sia_get, tlp_sia_set, media + 2, 2);
   5938 
   5939 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
   5940 		tm->tm_name = "auto";
   5941 		tm->tm_get = tlp_2114x_nway_get;
   5942 		tm->tm_set = tlp_2114x_nway_set;
   5943 		ifmedia_add(&mii->mii_media,
   5944 		    IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0), 0, tm);
   5945 
   5946 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   5947 		sc->sc_statchg = tlp_2114x_nway_statchg;
   5948 		sc->sc_tick = tlp_2114x_nway_tick;
   5949 		break;
   5950 	}
   5951 
   5952 	tlp_print_media(sc);
   5953 	tlp_sia_fixup(sc);
   5954 
   5955 	/* Set the LED modes. */
   5956 	tlp_pmac_reset(sc);
   5957 
   5958 	sc->sc_reset = tlp_pmac_reset;
   5959 }
   5960 
   5961 /*
   5962  * ADMtek AL981 media switch.  Only has internal PHY.
   5963  */
   5964 static void	tlp_al981_tmsw_init(struct tulip_softc *);
   5965 
   5966 const struct tulip_mediasw tlp_al981_mediasw = {
   5967 	tlp_al981_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   5968 };
   5969 
   5970 static void
   5971 tlp_al981_tmsw_init(struct tulip_softc *sc)
   5972 {
   5973 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   5974 	struct mii_data * const mii = &sc->sc_mii;
   5975 
   5976 	mii->mii_ifp = ifp;
   5977 	mii->mii_readreg = tlp_al981_mii_readreg;
   5978 	mii->mii_writereg = tlp_al981_mii_writereg;
   5979 	mii->mii_statchg = sc->sc_statchg;
   5980 	sc->sc_ethercom.ec_mii = mii;
   5981 	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
   5982 	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
   5983 	    MII_OFFSET_ANY, 0);
   5984 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   5985 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   5986 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   5987 	} else {
   5988 		sc->sc_flags |= TULIPF_HAS_MII;
   5989 		sc->sc_tick = tlp_mii_tick;
   5990 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   5991 	}
   5992 }
   5993 
   5994 /*
   5995  * ADMtek AN983/985 media switch.  Only has internal PHY, but
   5996  * on an SIO-like interface.  Unfortunately, we can't use the
   5997  * standard SIO media switch, because the AN985 "ghosts" the
   5998  * singly PHY at every address.
   5999  */
   6000 static void	tlp_an985_tmsw_init(struct tulip_softc *);
   6001 
   6002 const struct tulip_mediasw tlp_an985_mediasw = {
   6003 	tlp_an985_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   6004 };
   6005 
   6006 static void
   6007 tlp_an985_tmsw_init(struct tulip_softc *sc)
   6008 {
   6009 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   6010 	struct mii_data * const mii = &sc->sc_mii;
   6011 
   6012 	mii->mii_ifp = ifp;
   6013 	mii->mii_readreg = tlp_bitbang_mii_readreg;
   6014 	mii->mii_writereg = tlp_bitbang_mii_writereg;
   6015 	mii->mii_statchg = sc->sc_statchg;
   6016 	sc->sc_ethercom.ec_mii = mii;
   6017 	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
   6018 	mii_attach(sc->sc_dev, mii, 0xffffffff, 1, MII_OFFSET_ANY, 0);
   6019 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   6020 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   6021 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   6022 	} else {
   6023 		sc->sc_flags |= TULIPF_HAS_MII;
   6024 		sc->sc_tick = tlp_mii_tick;
   6025 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   6026 	}
   6027 }
   6028 
   6029 /*
   6030  * Davicom DM9102 media switch.  Internal PHY and possibly HomePNA.
   6031  */
   6032 static void	tlp_dm9102_tmsw_init(struct tulip_softc *);
   6033 static void	tlp_dm9102_tmsw_getmedia(struct tulip_softc *,
   6034 		    struct ifmediareq *);
   6035 static int	tlp_dm9102_tmsw_setmedia(struct tulip_softc *);
   6036 
   6037 const struct tulip_mediasw tlp_dm9102_mediasw = {
   6038 	tlp_dm9102_tmsw_init, tlp_dm9102_tmsw_getmedia,
   6039 	    tlp_dm9102_tmsw_setmedia
   6040 };
   6041 
   6042 static void
   6043 tlp_dm9102_tmsw_init(struct tulip_softc *sc)
   6044 {
   6045 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   6046 	struct mii_data * const mii = &sc->sc_mii;
   6047 	uint32_t opmode;
   6048 
   6049 	mii->mii_ifp = ifp;
   6050 	mii->mii_readreg = tlp_bitbang_mii_readreg;
   6051 	mii->mii_writereg = tlp_bitbang_mii_writereg;
   6052 	mii->mii_statchg = sc->sc_statchg;
   6053 	sc->sc_ethercom.ec_mii = mii;
   6054 	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
   6055 
   6056 	/* PHY block already reset via tlp_reset(). */
   6057 
   6058 	/*
   6059 	 * Configure OPMODE properly for the internal MII interface.
   6060 	 */
   6061 	switch (sc->sc_chip) {
   6062 	case TULIP_CHIP_DM9102:
   6063 		opmode = OPMODE_MBO | OPMODE_HBD | OPMODE_PS;
   6064 		break;
   6065 
   6066 	case TULIP_CHIP_DM9102A:
   6067 		opmode = OPMODE_MBO | OPMODE_HBD;
   6068 		break;
   6069 
   6070 	default:
   6071 		opmode = 0;
   6072 		break;
   6073 	}
   6074 
   6075 	TULIP_WRITE(sc, CSR_OPMODE, opmode);
   6076 
   6077 	/* Now, probe the internal MII for the internal PHY. */
   6078 	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
   6079 	    MII_OFFSET_ANY, 0);
   6080 
   6081 	/*
   6082 	 * XXX Figure out what to do about the HomePNA portion
   6083 	 * XXX of the DM9102A.
   6084 	 */
   6085 
   6086 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   6087 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   6088 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   6089 	} else {
   6090 		sc->sc_flags |= TULIPF_HAS_MII;
   6091 		sc->sc_tick = tlp_mii_tick;
   6092 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   6093 	}
   6094 }
   6095 
   6096 static void
   6097 tlp_dm9102_tmsw_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr)
   6098 {
   6099 
   6100 	/* XXX HomePNA on DM9102A. */
   6101 	tlp_mii_getmedia(sc, ifmr);
   6102 }
   6103 
   6104 static int
   6105 tlp_dm9102_tmsw_setmedia(struct tulip_softc *sc)
   6106 {
   6107 
   6108 	/* XXX HomePNA on DM9102A. */
   6109 	return tlp_mii_setmedia(sc);
   6110 }
   6111 
   6112 /*
   6113  * ASIX AX88140A/AX88141 media switch. Internal PHY or MII.
   6114  */
   6115 
   6116 static void	tlp_asix_tmsw_init(struct tulip_softc *);
   6117 static void	tlp_asix_tmsw_getmedia(struct tulip_softc *,
   6118 		    struct ifmediareq *);
   6119 static int	tlp_asix_tmsw_setmedia(struct tulip_softc *);
   6120 
   6121 const struct tulip_mediasw tlp_asix_mediasw = {
   6122 	tlp_asix_tmsw_init, tlp_asix_tmsw_getmedia,
   6123 	tlp_asix_tmsw_setmedia
   6124 };
   6125 
   6126 static void
   6127 tlp_asix_tmsw_init(struct tulip_softc *sc)
   6128 {
   6129 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   6130 	struct mii_data * const mii = &sc->sc_mii;
   6131 	uint32_t opmode;
   6132 
   6133 	mii->mii_ifp = ifp;
   6134 	mii->mii_readreg = tlp_bitbang_mii_readreg;
   6135 	mii->mii_writereg = tlp_bitbang_mii_writereg;
   6136 	mii->mii_statchg = sc->sc_statchg;
   6137 	sc->sc_ethercom.ec_mii = mii;
   6138 	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
   6139 
   6140 	/*
   6141 	 * Configure OPMODE properly for the internal MII interface.
   6142 	 */
   6143 	switch (sc->sc_chip) {
   6144 	case TULIP_CHIP_AX88140:
   6145 	case TULIP_CHIP_AX88141:
   6146 		opmode = OPMODE_HBD | OPMODE_PS;
   6147 		break;
   6148 	default:
   6149 		opmode = 0;
   6150 		break;
   6151 	}
   6152 
   6153 	TULIP_WRITE(sc, CSR_OPMODE, opmode);
   6154 
   6155 	/* Now, probe the internal MII for the internal PHY. */
   6156 	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
   6157 	    MII_OFFSET_ANY, 0);
   6158 
   6159 	/* XXX Figure how to handle the PHY. */
   6160 
   6161 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   6162 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   6163 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   6164 	} else {
   6165 		sc->sc_flags |= TULIPF_HAS_MII;
   6166 		sc->sc_tick = tlp_mii_tick;
   6167 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   6168 	}
   6169 
   6170 
   6171 }
   6172 
   6173 static void
   6174 tlp_asix_tmsw_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr)
   6175 {
   6176 
   6177 	/* XXX PHY handling. */
   6178 	tlp_mii_getmedia(sc, ifmr);
   6179 }
   6180 
   6181 static int
   6182 tlp_asix_tmsw_setmedia(struct tulip_softc *sc)
   6183 {
   6184 
   6185 	/* XXX PHY handling. */
   6186 	return tlp_mii_setmedia(sc);
   6187 }
   6188 
   6189 /*
   6190  * RS7112 media switch.  Handles only MII attached to the SIO.
   6191  * We only have a PHY at 1.
   6192  */
   6193 void   tlp_rs7112_tmsw_init(struct tulip_softc *);
   6194 
   6195 const struct tulip_mediasw tlp_rs7112_mediasw = {
   6196 	tlp_rs7112_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   6197 };
   6198 
   6199 void
   6200 tlp_rs7112_tmsw_init(struct tulip_softc *sc)
   6201 {
   6202 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   6203 	struct mii_data * const mii = &sc->sc_mii;
   6204 
   6205 	/*
   6206 	 * We don't attach any media info structures to the ifmedia
   6207 	 * entries, so if we're using a pre-init function that needs
   6208 	 * that info, override it to one that doesn't.
   6209 	 */
   6210 	if (sc->sc_preinit == tlp_2114x_preinit)
   6211 		sc->sc_preinit = tlp_2114x_mii_preinit;
   6212 
   6213 	mii->mii_ifp = ifp;
   6214 	mii->mii_readreg = tlp_bitbang_mii_readreg;
   6215 	mii->mii_writereg = tlp_bitbang_mii_writereg;
   6216 	mii->mii_statchg = sc->sc_statchg;
   6217 	sc->sc_ethercom.ec_mii = mii;
   6218 	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
   6219 
   6220 	/*
   6221 	 * The RS7112 reports a PHY at 0 (possibly HomePNA?)
   6222 	 * and 1 (ethernet). We attach ethernet only.
   6223 	 */
   6224 	mii_attach(sc->sc_dev, mii, 0xffffffff, 1, MII_OFFSET_ANY, 0);
   6225 
   6226 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   6227 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   6228 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   6229 	} else {
   6230 		sc->sc_flags |= TULIPF_HAS_MII;
   6231 		sc->sc_tick = tlp_mii_tick;
   6232 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   6233 	}
   6234 }
   6235 
   6236 const char *
   6237 tlp_chip_name(tulip_chip_t t) {
   6238 	if ((int)t < 0 || (int)t >= __arraycount(tlp_chip_names)) {
   6239 		static char buf[256];
   6240 		(void)snprintf(buf, sizeof(buf), "[unknown 0x%x]", t);
   6241 		return buf;
   6242 	}
   6243 	return tlp_chip_names[t];
   6244 }
   6245