Home | History | Annotate | Line # | Download | only in ic
tulip.c revision 1.37
      1 /*	$NetBSD: tulip.c,v 1.37 2000/01/25 03:14:12 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999 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.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Device driver for the Digital Semiconductor ``Tulip'' (21x4x)
     42  * Ethernet controller family, and a variety of clone chips.
     43  */
     44 
     45 #include "opt_inet.h"
     46 #include "opt_ns.h"
     47 #include "bpfilter.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/mbuf.h>
     52 #include <sys/malloc.h>
     53 #include <sys/kernel.h>
     54 #include <sys/socket.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/errno.h>
     57 #include <sys/device.h>
     58 
     59 #include <machine/endian.h>
     60 
     61 #include <vm/vm.h>		/* for PAGE_SIZE */
     62 
     63 #include <net/if.h>
     64 #include <net/if_dl.h>
     65 #include <net/if_media.h>
     66 #include <net/if_ether.h>
     67 
     68 #if NBPFILTER > 0
     69 #include <net/bpf.h>
     70 #endif
     71 
     72 #ifdef INET
     73 #include <netinet/in.h>
     74 #include <netinet/if_inarp.h>
     75 #endif
     76 
     77 #ifdef NS
     78 #include <netns/ns.h>
     79 #include <netns/ns_if.h>
     80 #endif
     81 
     82 #include <machine/bus.h>
     83 #include <machine/intr.h>
     84 
     85 #include <dev/mii/mii.h>
     86 #include <dev/mii/miivar.h>
     87 #include <dev/mii/mii_bitbang.h>
     88 
     89 #include <dev/ic/tulipreg.h>
     90 #include <dev/ic/tulipvar.h>
     91 
     92 /*
     93  * The following tables compute the transmit threshold mode.  We start
     94  * at index 0.  When ever we get a transmit underrun, we increment our
     95  * index, falling back if we encounter the NULL terminator.
     96  *
     97  * Note: Store and forward mode is only available on the 100mbps chips
     98  * (21140 and higher).
     99  */
    100 const struct tulip_txthresh_tab tlp_10_txthresh_tab[] = {
    101 	{ OPMODE_TR_72,		"72 bytes" },
    102 	{ OPMODE_TR_96,		"96 bytes" },
    103 	{ OPMODE_TR_128,	"128 bytes" },
    104 	{ OPMODE_TR_160,	"160 bytes" },
    105 	{ 0,			NULL },
    106 };
    107 
    108 const struct tulip_txthresh_tab tlp_10_100_txthresh_tab[] = {
    109 	{ OPMODE_TR_72,		"72/128 bytes" },
    110 	{ OPMODE_TR_96,		"96/256 bytes" },
    111 	{ OPMODE_TR_128,	"128/512 bytes" },
    112 	{ OPMODE_TR_160,	"160/1024 bytes" },
    113 	{ OPMODE_SF,		"store and forward mode" },
    114 	{ 0,			NULL },
    115 };
    116 
    117 #define	TXTH_72		0
    118 #define	TXTH_96		1
    119 #define	TXTH_128	2
    120 #define	TXTH_160	3
    121 #define	TXTH_SF		4
    122 
    123 /*
    124  * The Winbond 89C840F does transmit threshold control totally
    125  * differently.  It simply has a 7-bit field which indicates
    126  * the threshold:
    127  *
    128  *	txth = ((OPMODE & OPMODE_WINB_TTH) >> OPMODE_WINB_TTH_SHIFT) * 16;
    129  *
    130  * However, we just do Store-and-Forward mode on these chips, since
    131  * the DMA engines seem to be flaky.
    132  */
    133 const struct tulip_txthresh_tab tlp_winb_txthresh_tab[] = {
    134 	{ 0,			"store and forward mode" },
    135 	{ 0,			NULL },
    136 };
    137 
    138 #define	TXTH_WINB_SF	0
    139 
    140 void	tlp_start __P((struct ifnet *));
    141 void	tlp_watchdog __P((struct ifnet *));
    142 int	tlp_ioctl __P((struct ifnet *, u_long, caddr_t));
    143 
    144 void	tlp_shutdown __P((void *));
    145 
    146 void	tlp_reset __P((struct tulip_softc *));
    147 int	tlp_init __P((struct tulip_softc *));
    148 void	tlp_rxdrain __P((struct tulip_softc *));
    149 void	tlp_stop __P((struct tulip_softc *, int));
    150 int	tlp_add_rxbuf __P((struct tulip_softc *, int));
    151 void	tlp_idle __P((struct tulip_softc *, u_int32_t));
    152 void	tlp_srom_idle __P((struct tulip_softc *));
    153 
    154 void	tlp_filter_setup __P((struct tulip_softc *));
    155 void	tlp_winb_filter_setup __P((struct tulip_softc *));
    156 void	tlp_al981_filter_setup __P((struct tulip_softc *));
    157 
    158 void	tlp_rxintr __P((struct tulip_softc *));
    159 void	tlp_txintr __P((struct tulip_softc *));
    160 
    161 void	tlp_mii_tick __P((void *));
    162 void	tlp_mii_statchg __P((struct device *));
    163 void	tlp_winb_mii_statchg __P((struct device *));
    164 
    165 void	tlp_mii_getmedia __P((struct tulip_softc *, struct ifmediareq *));
    166 int	tlp_mii_setmedia __P((struct tulip_softc *));
    167 
    168 int	tlp_bitbang_mii_readreg __P((struct device *, int, int));
    169 void	tlp_bitbang_mii_writereg __P((struct device *, int, int, int));
    170 
    171 int	tlp_pnic_mii_readreg __P((struct device *, int, int));
    172 void	tlp_pnic_mii_writereg __P((struct device *, int, int, int));
    173 
    174 int	tlp_al981_mii_readreg __P((struct device *, int, int));
    175 void	tlp_al981_mii_writereg __P((struct device *, int, int, int));
    176 
    177 void	tlp_2114x_preinit __P((struct tulip_softc *));
    178 void	tlp_2114x_mii_preinit __P((struct tulip_softc *));
    179 void	tlp_pnic_preinit __P((struct tulip_softc *));
    180 
    181 void	tlp_21140_reset __P((struct tulip_softc *));
    182 void	tlp_21142_reset __P((struct tulip_softc *));
    183 void	tlp_pmac_reset __P((struct tulip_softc *));
    184 
    185 u_int32_t tlp_crc32 __P((const u_int8_t *, size_t));
    186 #define	tlp_mchash(addr, sz) (tlp_crc32((addr), ETHER_ADDR_LEN) & ((sz) - 1))
    187 
    188 /*
    189  * MII bit-bang glue.
    190  */
    191 u_int32_t tlp_sio_mii_bitbang_read __P((struct device *));
    192 void	tlp_sio_mii_bitbang_write __P((struct device *, u_int32_t));
    193 
    194 const struct mii_bitbang_ops tlp_sio_mii_bitbang_ops = {
    195 	tlp_sio_mii_bitbang_read,
    196 	tlp_sio_mii_bitbang_write,
    197 	{
    198 		MIIROM_MDO,		/* MII_BIT_MDO */
    199 		MIIROM_MDI,		/* MII_BIT_MDI */
    200 		MIIROM_MDC,		/* MII_BIT_MDC */
    201 		0,			/* MII_BIT_DIR_HOST_PHY */
    202 		MIIROM_MIIDIR,		/* MII_BIT_DIR_PHY_HOST */
    203 	}
    204 };
    205 
    206 #ifdef TLP_DEBUG
    207 #define	DPRINTF(sc, x)	if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
    208 				printf x
    209 #else
    210 #define	DPRINTF(sc, x)	/* nothing */
    211 #endif
    212 
    213 #ifdef TLP_STATS
    214 void	tlp_print_stats __P((struct tulip_softc *));
    215 #endif
    216 
    217 /*
    218  * tlp_attach:
    219  *
    220  *	Attach a Tulip interface to the system.
    221  */
    222 void
    223 tlp_attach(sc, enaddr)
    224 	struct tulip_softc *sc;
    225 	const u_int8_t *enaddr;
    226 {
    227 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    228 	int i, rseg, error;
    229 	bus_dma_segment_t seg;
    230 
    231 	/*
    232 	 * NOTE: WE EXPECT THE FRONT-END TO INITIALIZE sc_regshift!
    233 	 */
    234 
    235 	/*
    236 	 * Setup the transmit threshold table.
    237 	 */
    238 	switch (sc->sc_chip) {
    239 	case TULIP_CHIP_DE425:
    240 	case TULIP_CHIP_21040:
    241 	case TULIP_CHIP_21041:
    242 		sc->sc_txth = tlp_10_txthresh_tab;
    243 		break;
    244 
    245 	default:
    246 		sc->sc_txth = tlp_10_100_txthresh_tab;
    247 		break;
    248 	}
    249 
    250 	/*
    251 	 * Setup the filter setup function.
    252 	 */
    253 	switch (sc->sc_chip) {
    254 	case TULIP_CHIP_WB89C840F:
    255 		sc->sc_filter_setup = tlp_winb_filter_setup;
    256 		break;
    257 
    258 	case TULIP_CHIP_AL981:
    259 		sc->sc_filter_setup = tlp_al981_filter_setup;
    260 		break;
    261 
    262 	default:
    263 		sc->sc_filter_setup = tlp_filter_setup;
    264 		break;
    265 	}
    266 
    267 	/*
    268 	 * Set up the media status change function.
    269 	 */
    270 	switch (sc->sc_chip) {
    271 	case TULIP_CHIP_WB89C840F:
    272 		sc->sc_statchg = tlp_winb_mii_statchg;
    273 		break;
    274 
    275 	default:
    276 		/*
    277 		 * We may override this if we have special media
    278 		 * handling requirements (e.g. flipping GPIO pins).
    279 		 *
    280 		 * The pure-MII statchg function covers the basics.
    281 		 */
    282 		sc->sc_statchg = tlp_mii_statchg;
    283 		break;
    284 	}
    285 
    286 	/*
    287 	 * Set up various chip-specific quirks.
    288 	 */
    289 	switch (sc->sc_chip) {
    290 	case TULIP_CHIP_21140:
    291 	case TULIP_CHIP_21140A:
    292 	case TULIP_CHIP_21142:
    293 	case TULIP_CHIP_21143:
    294 	case TULIP_CHIP_82C115:		/* 21143-like */
    295 	case TULIP_CHIP_MX98713:	/* 21140-like */
    296 	case TULIP_CHIP_MX98713A:	/* 21143-like */
    297 	case TULIP_CHIP_MX98715:	/* 21143-like */
    298 	case TULIP_CHIP_MX98715A:	/* 21143-like */
    299 	case TULIP_CHIP_MX98725:	/* 21143-like */
    300 		sc->sc_preinit = tlp_2114x_preinit;
    301 		break;
    302 
    303 	case TULIP_CHIP_82C168:
    304 	case TULIP_CHIP_82C169:
    305 		sc->sc_preinit = tlp_pnic_preinit;
    306 
    307 		/*
    308 		 * These chips seem to have busted DMA engines; just put them
    309 		 * in Store-and-Forward mode from the get-go.
    310 		 */
    311 		sc->sc_txthresh = TXTH_SF;
    312 		break;
    313 
    314 	case TULIP_CHIP_WB89C840F:
    315 		sc->sc_flags |= TULIPF_IC_FS;
    316 		break;
    317 
    318 	default:
    319 		/* Nothing. */
    320 	}
    321 
    322 	/*
    323 	 * Set up the MII bit-bang operations.
    324 	 */
    325 	switch (sc->sc_chip) {
    326 	case TULIP_CHIP_WB89C840F:	/* XXX direction bit different? */
    327 		sc->sc_bitbang_ops = &tlp_sio_mii_bitbang_ops;
    328 		break;
    329 
    330 	default:
    331 		sc->sc_bitbang_ops = &tlp_sio_mii_bitbang_ops;
    332 	}
    333 
    334 	SIMPLEQ_INIT(&sc->sc_txfreeq);
    335 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
    336 
    337 	/*
    338 	 * Allocate the control data structures, and create and load the
    339 	 * DMA map for it.
    340 	 */
    341 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    342 	    sizeof(struct tulip_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
    343 	    0)) != 0) {
    344 		printf("%s: unable to allocate control data, error = %d\n",
    345 		    sc->sc_dev.dv_xname, error);
    346 		goto fail_0;
    347 	}
    348 
    349 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    350 	    sizeof(struct tulip_control_data), (caddr_t *)&sc->sc_control_data,
    351 	    BUS_DMA_COHERENT)) != 0) {
    352 		printf("%s: unable to map control data, error = %d\n",
    353 		    sc->sc_dev.dv_xname, error);
    354 		goto fail_1;
    355 	}
    356 
    357 	if ((error = bus_dmamap_create(sc->sc_dmat,
    358 	    sizeof(struct tulip_control_data), 1,
    359 	    sizeof(struct tulip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    360 		printf("%s: unable to create control data DMA map, "
    361 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    362 		goto fail_2;
    363 	}
    364 
    365 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    366 	    sc->sc_control_data, sizeof(struct tulip_control_data), NULL,
    367 	    0)) != 0) {
    368 		printf("%s: unable to load control data DMA map, error = %d\n",
    369 		    sc->sc_dev.dv_xname, error);
    370 		goto fail_3;
    371 	}
    372 
    373 	/*
    374 	 * Create the transmit buffer DMA maps.
    375 	 */
    376 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
    377 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    378 		    TULIP_NTXSEGS, MCLBYTES, 0, 0,
    379 		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
    380 			printf("%s: unable to create tx DMA map %d, "
    381 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    382 			goto fail_4;
    383 		}
    384 	}
    385 
    386 	/*
    387 	 * Create the recieve buffer DMA maps.
    388 	 */
    389 	for (i = 0; i < TULIP_NRXDESC; i++) {
    390 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    391 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    392 			printf("%s: unable to create rx DMA map %d, "
    393 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    394 			goto fail_5;
    395 		}
    396 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    397 	}
    398 
    399 	/*
    400 	 * Reset the chip to a known state.
    401 	 */
    402 	tlp_reset(sc);
    403 
    404 	/* Announce ourselves. */
    405 	printf("%s: %s%sEthernet address %s\n", sc->sc_dev.dv_xname,
    406 	    sc->sc_name[0] != '\0' ? sc->sc_name : "",
    407 	    sc->sc_name[0] != '\0' ? ", " : "",
    408 	    ether_sprintf(enaddr));
    409 
    410 	/*
    411 	 * Initialize our media structures.  This may probe the MII, if
    412 	 * present.
    413 	 */
    414 	(*sc->sc_mediasw->tmsw_init)(sc);
    415 
    416 	ifp = &sc->sc_ethercom.ec_if;
    417 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    418 	ifp->if_softc = sc;
    419 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    420 	ifp->if_ioctl = tlp_ioctl;
    421 	ifp->if_start = tlp_start;
    422 	ifp->if_watchdog = tlp_watchdog;
    423 
    424 	/*
    425 	 * Attach the interface.
    426 	 */
    427 	if_attach(ifp);
    428 	ether_ifattach(ifp, enaddr);
    429 #if NBPFILTER > 0
    430 	bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
    431 	    sizeof(struct ether_header));
    432 #endif
    433 
    434 	/*
    435 	 * Make sure the interface is shutdown during reboot.
    436 	 */
    437 	sc->sc_sdhook = shutdownhook_establish(tlp_shutdown, sc);
    438 	if (sc->sc_sdhook == NULL)
    439 		printf("%s: WARNING: unable to establish shutdown hook\n",
    440 		    sc->sc_dev.dv_xname);
    441 	return;
    442 
    443 	/*
    444 	 * Free any resources we've allocated during the failed attach
    445 	 * attempt.  Do this in reverse order and fall through.
    446 	 */
    447  fail_5:
    448 	for (i = 0; i < TULIP_NRXDESC; i++) {
    449 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    450 			bus_dmamap_destroy(sc->sc_dmat,
    451 			    sc->sc_rxsoft[i].rxs_dmamap);
    452 	}
    453  fail_4:
    454 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
    455 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
    456 			bus_dmamap_destroy(sc->sc_dmat,
    457 			    sc->sc_txsoft[i].txs_dmamap);
    458 	}
    459 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    460  fail_3:
    461 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    462  fail_2:
    463 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
    464 	    sizeof(struct tulip_control_data));
    465  fail_1:
    466 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    467  fail_0:
    468 	return;
    469 }
    470 
    471 /*
    472  * tlp_shutdown:
    473  *
    474  *	Make sure the interface is stopped at reboot time.
    475  */
    476 void
    477 tlp_shutdown(arg)
    478 	void *arg;
    479 {
    480 	struct tulip_softc *sc = arg;
    481 
    482 	tlp_stop(sc, 1);
    483 }
    484 
    485 /*
    486  * tlp_start:		[ifnet interface function]
    487  *
    488  *	Start packet transmission on the interface.
    489  */
    490 void
    491 tlp_start(ifp)
    492 	struct ifnet *ifp;
    493 {
    494 	struct tulip_softc *sc = ifp->if_softc;
    495 	struct mbuf *m0, *m;
    496 	struct tulip_txsoft *txs, *last_txs;
    497 	bus_dmamap_t dmamap;
    498 	int error, firsttx, nexttx, lasttx, ofree, seg;
    499 
    500 	DPRINTF(sc, ("%s: tlp_start: sc_flags 0x%08x, if_flags 0x%08x\n",
    501 	    sc->sc_dev.dv_xname, sc->sc_flags, ifp->if_flags));
    502 
    503 	/*
    504 	 * If we want a filter setup, it means no more descriptors were
    505 	 * available for the setup routine.  Let it get a chance to wedge
    506 	 * itself into the ring.
    507 	 */
    508 	if (sc->sc_flags & TULIPF_WANT_SETUP)
    509 		ifp->if_flags |= IFF_OACTIVE;
    510 
    511 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    512 		return;
    513 
    514 	/*
    515 	 * Remember the previous number of free descriptors and
    516 	 * the first descriptor we'll use.
    517 	 */
    518 	ofree = sc->sc_txfree;
    519 	firsttx = sc->sc_txnext;
    520 
    521 	DPRINTF(sc, ("%s: tlp_start: txfree %d, txnext %d\n",
    522 	    sc->sc_dev.dv_xname, ofree, firsttx));
    523 
    524 	/*
    525 	 * Loop through the send queue, setting up transmit descriptors
    526 	 * until we drain the queue, or use up all available transmit
    527 	 * descriptors.
    528 	 */
    529 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
    530 	       sc->sc_txfree != 0) {
    531 		/*
    532 		 * Grab a packet off the queue.
    533 		 */
    534 		IF_DEQUEUE(&ifp->if_snd, m0);
    535 		if (m0 == NULL)
    536 			break;
    537 
    538 		dmamap = txs->txs_dmamap;
    539 
    540 		/*
    541 		 * Load the DMA map.  If this fails, the packet either
    542 		 * didn't fit in the alloted number of segments, or we were
    543 		 * short on resources.  In this case, we'll copy and try
    544 		 * again.
    545 		 */
    546 		if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    547 		    BUS_DMA_NOWAIT) != 0) {
    548 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    549 			if (m == NULL) {
    550 				printf("%s: unable to allocate Tx mbuf\n",
    551 				    sc->sc_dev.dv_xname);
    552 				IF_PREPEND(&ifp->if_snd, m0);
    553 				break;
    554 			}
    555 			if (m0->m_pkthdr.len > MHLEN) {
    556 				MCLGET(m, M_DONTWAIT);
    557 				if ((m->m_flags & M_EXT) == 0) {
    558 					printf("%s: unable to allocate Tx "
    559 					    "cluster\n", sc->sc_dev.dv_xname);
    560 					m_freem(m);
    561 					IF_PREPEND(&ifp->if_snd, m0);
    562 					break;
    563 				}
    564 			}
    565 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
    566 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    567 			m_freem(m0);
    568 			m0 = m;
    569 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    570 			    m0, BUS_DMA_NOWAIT);
    571 			if (error) {
    572 				printf("%s: unable to load Tx buffer, "
    573 				    "error = %d\n", sc->sc_dev.dv_xname, error);
    574 				IF_PREPEND(&ifp->if_snd, m0);
    575 				break;
    576 			}
    577 		}
    578 
    579 		/*
    580 		 * Ensure we have enough descriptors free to describe
    581 		 * the packet.
    582 		 */
    583 		if (dmamap->dm_nsegs > sc->sc_txfree) {
    584 			/*
    585 			 * Not enough free descriptors to transmit this
    586 			 * packet.  We haven't committed to anything yet,
    587 			 * so just unload the DMA map, put the packet
    588 			 * back on the queue, and punt.  Notify the upper
    589 			 * layer that there are no more slots left.
    590 			 *
    591 			 * XXX We could allocate an mbuf and copy, but
    592 			 * XXX it is worth it?
    593 			 */
    594 			ifp->if_flags |= IFF_OACTIVE;
    595 			bus_dmamap_unload(sc->sc_dmat, dmamap);
    596 			IF_PREPEND(&ifp->if_snd, m0);
    597 			break;
    598 		}
    599 
    600 		/*
    601 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    602 		 */
    603 
    604 		/* Sync the DMA map. */
    605 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    606 		    BUS_DMASYNC_PREWRITE);
    607 
    608 		/*
    609 		 * Initialize the transmit descriptors.
    610 		 */
    611 		for (nexttx = sc->sc_txnext, seg = 0;
    612 		     seg < dmamap->dm_nsegs;
    613 		     seg++, nexttx = TULIP_NEXTTX(nexttx)) {
    614 			/*
    615 			 * If this is the first descriptor we're
    616 			 * enqueueing, don't set the OWN bit just
    617 			 * yet.  That could cause a race condition.
    618 			 * We'll do it below.
    619 			 */
    620 			sc->sc_txdescs[nexttx].td_status =
    621 			    (nexttx == firsttx) ? 0 : htole32(TDSTAT_OWN);
    622 			sc->sc_txdescs[nexttx].td_bufaddr1 =
    623 			    htole32(dmamap->dm_segs[seg].ds_addr);
    624 			sc->sc_txdescs[nexttx].td_ctl =
    625 			    htole32((dmamap->dm_segs[seg].ds_len <<
    626 			        TDCTL_SIZE1_SHIFT) | TDCTL_CH);
    627 			lasttx = nexttx;
    628 		}
    629 
    630 		/* Set `first segment' and `last segment' appropriately. */
    631 		sc->sc_txdescs[sc->sc_txnext].td_ctl |= htole32(TDCTL_Tx_FS);
    632 		sc->sc_txdescs[lasttx].td_ctl |= htole32(TDCTL_Tx_LS);
    633 
    634 #ifdef TLP_DEBUG
    635 		if (ifp->if_flags & IFF_DEBUG) {
    636 			printf("     txsoft %p trainsmit chain:\n", txs);
    637 			for (seg = sc->sc_txnext;; seg = TULIP_NEXTTX(seg)) {
    638 				printf("     descriptor %d:\n", seg);
    639 				printf("       td_status:   0x%08x\n",
    640 				    le32toh(sc->sc_txdescs[seg].td_status));
    641 				printf("       td_ctl:      0x%08x\n",
    642 				    le32toh(sc->sc_txdescs[seg].td_ctl));
    643 				printf("       td_bufaddr1: 0x%08x\n",
    644 				    le32toh(sc->sc_txdescs[seg].td_bufaddr1));
    645 				printf("       td_bufaddr2: 0x%08x\n",
    646 				    le32toh(sc->sc_txdescs[seg].td_bufaddr2));
    647 				if (seg == lasttx)
    648 					break;
    649 			}
    650 		}
    651 #endif
    652 
    653 		/* Sync the descriptors we're using. */
    654 		TULIP_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
    655 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    656 
    657 		/*
    658 		 * Store a pointer to the packet so we can free it later,
    659 		 * and remember what txdirty will be once the packet is
    660 		 * done.
    661 		 */
    662 		txs->txs_mbuf = m0;
    663 		txs->txs_firstdesc = sc->sc_txnext;
    664 		txs->txs_lastdesc = lasttx;
    665 
    666 		/* Advance the tx pointer. */
    667 		sc->sc_txfree -= dmamap->dm_nsegs;
    668 		sc->sc_txnext = nexttx;
    669 
    670 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
    671 		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
    672 
    673 		last_txs = txs;
    674 
    675 #if NBPFILTER > 0
    676 		/*
    677 		 * Pass the packet to any BPF listeners.
    678 		 */
    679 		if (ifp->if_bpf)
    680 			bpf_mtap(ifp->if_bpf, m0);
    681 #endif /* NBPFILTER > 0 */
    682 	}
    683 
    684 	if (txs == NULL || sc->sc_txfree == 0) {
    685 		/* No more slots left; notify upper layer. */
    686 		ifp->if_flags |= IFF_OACTIVE;
    687 	}
    688 
    689 	if (sc->sc_txfree != ofree) {
    690 		DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
    691 		    sc->sc_dev.dv_xname, lasttx, firsttx));
    692 		/*
    693 		 * Cause a transmit interrupt to happen on the
    694 		 * last packet we enqueued.
    695 		 */
    696 		sc->sc_txdescs[lasttx].td_ctl |= htole32(TDCTL_Tx_IC);
    697 		TULIP_CDTXSYNC(sc, lasttx, 1,
    698 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    699 
    700 		/*
    701 		 * Some clone chips want IC on the *first* segment in
    702 		 * the packet.  Appease them.
    703 		 */
    704 		if ((sc->sc_flags & TULIPF_IC_FS) != 0 &&
    705 		    last_txs->txs_firstdesc != lasttx) {
    706 			sc->sc_txdescs[last_txs->txs_firstdesc].td_ctl |=
    707 			    htole32(TDCTL_Tx_IC);
    708 			TULIP_CDTXSYNC(sc, last_txs->txs_firstdesc, 1,
    709 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    710 		}
    711 
    712 		/*
    713 		 * The entire packet chain is set up.  Give the
    714 		 * first descriptor to the chip now.
    715 		 */
    716 		sc->sc_txdescs[firsttx].td_status |= htole32(TDSTAT_OWN);
    717 		TULIP_CDTXSYNC(sc, firsttx, 1,
    718 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    719 
    720 		/* Wake up the transmitter. */
    721 		/* XXX USE AUTOPOLLING? */
    722 		TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
    723 
    724 		/* Set a watchdog timer in case the chip flakes out. */
    725 		ifp->if_timer = 5;
    726 	}
    727 }
    728 
    729 /*
    730  * tlp_watchdog:	[ifnet interface function]
    731  *
    732  *	Watchdog timer handler.
    733  */
    734 void
    735 tlp_watchdog(ifp)
    736 	struct ifnet *ifp;
    737 {
    738 	struct tulip_softc *sc = ifp->if_softc;
    739 	int doing_setup, doing_transmit;
    740 
    741 	doing_setup = (sc->sc_flags & TULIPF_DOING_SETUP);
    742 	doing_transmit = (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL);
    743 
    744 	if (doing_setup && doing_transmit) {
    745 		printf("%s: filter setup and transmit timeout\n",
    746 		    sc->sc_dev.dv_xname);
    747 		ifp->if_oerrors++;
    748 	} else if (doing_transmit) {
    749 		printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
    750 		ifp->if_oerrors++;
    751 	} else if (doing_setup)
    752 		printf("%s: filter setup timeout\n", sc->sc_dev.dv_xname);
    753 	else
    754 		printf("%s: spurious watchdog timeout\n", sc->sc_dev.dv_xname);
    755 
    756 	(void) tlp_init(sc);
    757 
    758 	/* Try to get more packets going. */
    759 	tlp_start(ifp);
    760 }
    761 
    762 /*
    763  * tlp_ioctl:		[ifnet interface function]
    764  *
    765  *	Handle control requests from the operator.
    766  */
    767 int
    768 tlp_ioctl(ifp, cmd, data)
    769 	struct ifnet *ifp;
    770 	u_long cmd;
    771 	caddr_t data;
    772 {
    773 	struct tulip_softc *sc = ifp->if_softc;
    774 	struct ifreq *ifr = (struct ifreq *)data;
    775 	struct ifaddr *ifa = (struct ifaddr *)data;
    776 	int s, error = 0;
    777 
    778 	s = splnet();
    779 
    780 	switch (cmd) {
    781 	case SIOCSIFADDR:
    782 		ifp->if_flags |= IFF_UP;
    783 
    784 		switch (ifa->ifa_addr->sa_family) {
    785 #ifdef INET
    786 		case AF_INET:
    787 			if ((error = tlp_init(sc)) != 0)
    788 				break;
    789 			arp_ifinit(ifp, ifa);
    790 			break;
    791 #endif /* INET */
    792 #ifdef NS
    793 		case AF_NS:
    794 		    {
    795 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    796 
    797 			if (ns_nullhost(*ina))
    798 				ina->x_host = *(union ns_host *)
    799 				    LLADDR(ifp->if_sadl);
    800 			else
    801 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
    802 				    ifp->if_addrlen);
    803 			/* Set new address. */
    804 			error = tlp_init(sc);
    805 			break;
    806 		    }
    807 #endif /* NS */
    808 		default:
    809 			error = tlp_init(sc);
    810 			break;
    811 		}
    812 		break;
    813 
    814 	case SIOCSIFMTU:
    815 		if (ifr->ifr_mtu > ETHERMTU)
    816 			error = EINVAL;
    817 		else
    818 			ifp->if_mtu = ifr->ifr_mtu;
    819 		break;
    820 
    821 	case SIOCSIFFLAGS:
    822 #ifdef TLP_STATS
    823 		if (ifp->if_flags & IFF_DEBUG)
    824 			tlp_print_stats(sc);
    825 #endif
    826 		if ((ifp->if_flags & IFF_UP) == 0 &&
    827 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    828 			/*
    829 			 * If interface is marked down and it is running, then
    830 			 * stop it.
    831 			 */
    832 			tlp_stop(sc, 1);
    833 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    834 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    835 			/*
    836 			 * If interfase it marked up and it is stopped, then
    837 			 * start it.
    838 			 */
    839 			error = tlp_init(sc);
    840 		} else if ((ifp->if_flags & IFF_UP) != 0) {
    841 			/*
    842 			 * Reset the interface to pick up changes in any other
    843 			 * flags that affect the hardware state.
    844 			 */
    845 			error = tlp_init(sc);
    846 		}
    847 		break;
    848 
    849 	case SIOCADDMULTI:
    850 	case SIOCDELMULTI:
    851 		error = (cmd == SIOCADDMULTI) ?
    852 		    ether_addmulti(ifr, &sc->sc_ethercom) :
    853 		    ether_delmulti(ifr, &sc->sc_ethercom);
    854 
    855 		if (error == ENETRESET) {
    856 			/*
    857 			 * Multicast list has changed.  Set the filter
    858 			 * accordingly.
    859 			 */
    860 			(*sc->sc_filter_setup)(sc);
    861 			error = 0;
    862 		}
    863 		break;
    864 
    865 	case SIOCSIFMEDIA:
    866 	case SIOCGIFMEDIA:
    867 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
    868 		break;
    869 
    870 	default:
    871 		error = EINVAL;
    872 		break;
    873 	}
    874 
    875 	/* Try to get more packets going. */
    876 	tlp_start(ifp);
    877 
    878 	splx(s);
    879 	return (error);
    880 }
    881 
    882 /*
    883  * tlp_intr:
    884  *
    885  *	Interrupt service routine.
    886  */
    887 int
    888 tlp_intr(arg)
    889 	void *arg;
    890 {
    891 	struct tulip_softc *sc = arg;
    892 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    893 	u_int32_t status, rxstatus, txstatus;
    894 	int handled = 0, txthresh;
    895 
    896 	DPRINTF(sc, ("%s: tlp_intr\n", sc->sc_dev.dv_xname));
    897 
    898 	/*
    899 	 * If the interface isn't running, the interrupt couldn't
    900 	 * possibly have come from us.
    901 	 */
    902 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    903 		return (0);
    904 
    905 	for (;;) {
    906 		status = TULIP_READ(sc, CSR_STATUS);
    907 		if (status)
    908 			TULIP_WRITE(sc, CSR_STATUS, status);
    909 
    910 		if ((status & sc->sc_inten) == 0)
    911 			break;
    912 
    913 		handled = 1;
    914 
    915 		rxstatus = status & sc->sc_rxint_mask;
    916 		txstatus = status & sc->sc_txint_mask;
    917 
    918 		if (rxstatus) {
    919 			/* Grab new any new packets. */
    920 			tlp_rxintr(sc);
    921 
    922 			if (rxstatus & STATUS_RWT)
    923 				printf("%s: receive watchdog timeout\n",
    924 				    sc->sc_dev.dv_xname);
    925 
    926 			if (rxstatus & STATUS_RU) {
    927 				printf("%s: receive ring overrun\n",
    928 				    sc->sc_dev.dv_xname);
    929 				/* Get the receive process going again. */
    930 				tlp_idle(sc, OPMODE_SR);
    931 				TULIP_WRITE(sc, CSR_RXLIST,
    932 				    TULIP_CDRXADDR(sc, sc->sc_rxptr));
    933 				TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
    934 				TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
    935 				break;
    936 			}
    937 		}
    938 
    939 		if (txstatus) {
    940 			/* Sweep up transmit descriptors. */
    941 			tlp_txintr(sc);
    942 
    943 			if (txstatus & STATUS_TJT)
    944 				printf("%s: transmit jabber timeout\n",
    945 				    sc->sc_dev.dv_xname);
    946 
    947 			if (txstatus & STATUS_UNF) {
    948 				/*
    949 				 * Increase our transmit threshold if
    950 				 * another is available.
    951 				 */
    952 				txthresh = sc->sc_txthresh + 1;
    953 				if (sc->sc_txth[txthresh].txth_name != NULL) {
    954 					/* Idle the transmit process. */
    955 					tlp_idle(sc, OPMODE_ST);
    956 
    957 					sc->sc_txthresh = txthresh;
    958 					sc->sc_opmode &= ~(OPMODE_TR|OPMODE_SF);
    959 					sc->sc_opmode |=
    960 					    sc->sc_txth[txthresh].txth_opmode;
    961 					printf("%s: transmit underrun; new "
    962 					    "threshold: %s\n",
    963 					    sc->sc_dev.dv_xname,
    964 					    sc->sc_txth[txthresh].txth_name);
    965 
    966 					/*
    967 					 * Set the new threshold and restart
    968 					 * the transmit process.
    969 					 */
    970 					TULIP_WRITE(sc, CSR_OPMODE,
    971 					    sc->sc_opmode);
    972 				}
    973 					/*
    974 					 * XXX Log every Nth underrun from
    975 					 * XXX now on?
    976 					 */
    977 			}
    978 		}
    979 
    980 		if (status & (STATUS_TPS|STATUS_RPS)) {
    981 			if (status & STATUS_TPS)
    982 				printf("%s: transmit process stopped\n",
    983 				    sc->sc_dev.dv_xname);
    984 			if (status & STATUS_RPS)
    985 				printf("%s: receive process stopped\n",
    986 				    sc->sc_dev.dv_xname);
    987 			(void) tlp_init(sc);
    988 			break;
    989 		}
    990 
    991 		if (status & STATUS_SE) {
    992 			const char *str;
    993 			switch (status & STATUS_EB) {
    994 			case STATUS_EB_PARITY:
    995 				str = "parity error";
    996 				break;
    997 
    998 			case STATUS_EB_MABT:
    999 				str = "master abort";
   1000 				break;
   1001 
   1002 			case STATUS_EB_TABT:
   1003 				str = "target abort";
   1004 				break;
   1005 
   1006 			default:
   1007 				str = "unknown error";
   1008 				break;
   1009 			}
   1010 			printf("%s: fatal system error: %s\n",
   1011 			    sc->sc_dev.dv_xname, str);
   1012 			(void) tlp_init(sc);
   1013 			break;
   1014 		}
   1015 
   1016 		/*
   1017 		 * Not handled:
   1018 		 *
   1019 		 *	Transmit buffer unavailable -- normal
   1020 		 *	condition, nothing to do, really.
   1021 		 *
   1022 		 *	General purpose timer experied -- we don't
   1023 		 *	use the general purpose timer.
   1024 		 *
   1025 		 *	Early receive interrupt -- not available on
   1026 		 *	all chips, we just use RI.  We also only
   1027 		 *	use single-segment receive DMA, so this
   1028 		 *	is mostly useless.
   1029 		 */
   1030 	}
   1031 
   1032 	/* Try to get more packets going. */
   1033 	tlp_start(ifp);
   1034 
   1035 	return (handled);
   1036 }
   1037 
   1038 /*
   1039  * tlp_rxintr:
   1040  *
   1041  *	Helper; handle receive interrupts.
   1042  */
   1043 void
   1044 tlp_rxintr(sc)
   1045 	struct tulip_softc *sc;
   1046 {
   1047 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1048 	struct ether_header *eh;
   1049 	struct tulip_rxsoft *rxs;
   1050 	struct mbuf *m;
   1051 	u_int32_t rxstat;
   1052 	int i, len;
   1053 
   1054 	for (i = sc->sc_rxptr;; i = TULIP_NEXTRX(i)) {
   1055 		rxs = &sc->sc_rxsoft[i];
   1056 
   1057 		TULIP_CDRXSYNC(sc, i,
   1058 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1059 
   1060 		rxstat = le32toh(sc->sc_rxdescs[i].td_status);
   1061 
   1062 		if (rxstat & TDSTAT_OWN) {
   1063 			/*
   1064 			 * We have processed all of the receive buffers.
   1065 			 */
   1066 			break;
   1067 		}
   1068 
   1069 		/*
   1070 		 * Make sure the packet fit in one buffer.  This should
   1071 		 * always be the case.  But the Lite-On PNIC, rev 33
   1072 		 * has an awful receive engine bug, which may require
   1073 		 * a very icky work-around.
   1074 		 */
   1075 		if ((rxstat & (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) !=
   1076 		    (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) {
   1077 			printf("%s: incoming packet spilled, resetting\n",
   1078 			    sc->sc_dev.dv_xname);
   1079 			(void) tlp_init(sc);
   1080 			return;
   1081 		}
   1082 
   1083 		/*
   1084 		 * If any collisions were seen on the wire, count one.
   1085 		 */
   1086 		if (rxstat & TDSTAT_Rx_CS)
   1087 			ifp->if_collisions++;
   1088 
   1089 		/*
   1090 		 * If an error occured, update stats, clear the status
   1091 		 * word, and leave the packet buffer in place.  It will
   1092 		 * simply be reused the next time the ring comes around.
   1093 		 */
   1094 		if (rxstat & TDSTAT_ES) {
   1095 #define	PRINTERR(bit, str)						\
   1096 			if (rxstat & (bit))				\
   1097 				printf("%s: receive error: %s\n",	\
   1098 				    sc->sc_dev.dv_xname, str)
   1099 			ifp->if_ierrors++;
   1100 			PRINTERR(TDSTAT_Rx_DE, "descriptor error");
   1101 			PRINTERR(TDSTAT_Rx_RF, "runt frame");
   1102 			PRINTERR(TDSTAT_Rx_TL, "frame too long");
   1103 			PRINTERR(TDSTAT_Rx_RE, "MII error");
   1104 			PRINTERR(TDSTAT_Rx_DB, "dribbling bit");
   1105 			PRINTERR(TDSTAT_Rx_CE, "CRC error");
   1106 #undef PRINTERR
   1107 			TULIP_INIT_RXDESC(sc, i);
   1108 			continue;
   1109 		}
   1110 
   1111 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1112 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1113 
   1114 		/*
   1115 		 * No errors; receive the packet.  Note the Tulip
   1116 		 * includes the CRC with every packet; trim it.
   1117 		 */
   1118 		len = TDSTAT_Rx_LENGTH(rxstat) - ETHER_CRC_LEN;
   1119 
   1120 #ifdef __NO_STRICT_ALIGNMENT
   1121 		/*
   1122 		 * Allocate a new mbuf cluster.  If that fails, we are
   1123 		 * out of memory, and must drop the packet and recycle
   1124 		 * the buffer that's already attached to this descriptor.
   1125 		 */
   1126 		m = rxs->rxs_mbuf;
   1127 		if (tlp_add_rxbuf(sc, i) != 0) {
   1128 			ifp->if_ierrors++;
   1129 			TULIP_INIT_RXDESC(sc, i);
   1130 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1131 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1132 			continue;
   1133 		}
   1134 #else
   1135 		/*
   1136 		 * The Tulip's receive buffers must be 4-byte aligned.
   1137 		 * But this means that the data after the Ethernet header
   1138 		 * is misaligned.  We must allocate a new buffer and
   1139 		 * copy the data, shifted forward 2 bytes.
   1140 		 */
   1141 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1142 		if (m == NULL) {
   1143  dropit:
   1144 			ifp->if_ierrors++;
   1145 			TULIP_INIT_RXDESC(sc, i);
   1146 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1147 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1148 			continue;
   1149 		}
   1150 		if (len > (MHLEN - 2)) {
   1151 			MCLGET(m, M_DONTWAIT);
   1152 			if ((m->m_flags & M_EXT) == 0) {
   1153 				m_freem(m);
   1154 				goto dropit;
   1155 			}
   1156 		}
   1157 		m->m_data += 2;
   1158 
   1159 		/*
   1160 		 * Note that we use clusters for incoming frames, so the
   1161 		 * buffer is virtually contiguous.
   1162 		 */
   1163 		memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
   1164 
   1165 		/* Allow the receive descriptor to continue using its mbuf. */
   1166 		TULIP_INIT_RXDESC(sc, i);
   1167 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1168 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1169 #endif /* __NO_STRICT_ALIGNMENT */
   1170 
   1171 		ifp->if_ipackets++;
   1172 		eh = mtod(m, struct ether_header *);
   1173 		m->m_pkthdr.rcvif = ifp;
   1174 		m->m_pkthdr.len = m->m_len = len;
   1175 
   1176 #if NBPFILTER > 0
   1177 		/*
   1178 		 * Pass this up to any BPF listeners, but only
   1179 		 * pass it up the stack if its for us.
   1180 		 */
   1181 		if (ifp->if_bpf)
   1182 			bpf_mtap(ifp->if_bpf, m);
   1183 #endif /* NPBFILTER > 0 */
   1184 
   1185 		/*
   1186 		 * This test is outside the NBPFILTER block because
   1187 		 * on the 21140 we have to use Hash-Only mode due to
   1188 		 * a bug in the filter logic.
   1189 		 */
   1190 		if ((ifp->if_flags & IFF_PROMISC) != 0 ||
   1191 		    sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
   1192 			if (memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
   1193 				 ETHER_ADDR_LEN) != 0 &&
   1194 			    ETHER_IS_MULTICAST(eh->ether_dhost) == 0) {
   1195 				m_freem(m);
   1196 				continue;
   1197 			}
   1198 		}
   1199 
   1200 		/* Pass it on. */
   1201 		(*ifp->if_input)(ifp, m);
   1202 	}
   1203 
   1204 	/* Update the recieve pointer. */
   1205 	sc->sc_rxptr = i;
   1206 }
   1207 
   1208 /*
   1209  * tlp_txintr:
   1210  *
   1211  *	Helper; handle transmit interrupts.
   1212  */
   1213 void
   1214 tlp_txintr(sc)
   1215 	struct tulip_softc *sc;
   1216 {
   1217 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1218 	struct tulip_txsoft *txs;
   1219 	u_int32_t txstat;
   1220 
   1221 	DPRINTF(sc, ("%s: tlp_txintr: sc_flags 0x%08x\n",
   1222 	    sc->sc_dev.dv_xname, sc->sc_flags));
   1223 
   1224 	ifp->if_flags &= ~IFF_OACTIVE;
   1225 
   1226 	/*
   1227 	 * If we were doing a filter setup, check to see if it completed.
   1228 	 */
   1229 	if (sc->sc_flags & TULIPF_DOING_SETUP) {
   1230 		TULIP_CDSDSYNC(sc, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1231 		if ((sc->sc_setup_desc.td_status & htole32(TDSTAT_OWN)) == 0)
   1232 			sc->sc_flags &= ~TULIPF_DOING_SETUP;
   1233 	}
   1234 
   1235 	/*
   1236 	 * Go through our Tx list and free mbufs for those
   1237 	 * frames that have been transmitted.
   1238 	 */
   1239 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1240 		TULIP_CDTXSYNC(sc, txs->txs_lastdesc,
   1241 		    txs->txs_dmamap->dm_nsegs,
   1242 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1243 
   1244 #ifdef TLP_DEBUG
   1245 		if (ifp->if_flags & IFF_DEBUG) {
   1246 			int i;
   1247 			printf("    txsoft %p trainsmit chain:\n", txs);
   1248 			for (i = txs->txs_firstdesc;; i = TULIP_NEXTTX(i)) {
   1249 				printf("     descriptor %d:\n", i);
   1250 				printf("       td_status:   0x%08x\n",
   1251 				    le32toh(sc->sc_txdescs[i].td_status));
   1252 				printf("       td_ctl:      0x%08x\n",
   1253 				    le32toh(sc->sc_txdescs[i].td_ctl));
   1254 				printf("       td_bufaddr1: 0x%08x\n",
   1255 				    le32toh(sc->sc_txdescs[i].td_bufaddr1));
   1256 				printf("       td_bufaddr2: 0x%08x\n",
   1257 				    le32toh(sc->sc_txdescs[i].td_bufaddr2));
   1258 				if (i == txs->txs_lastdesc)
   1259 					break;
   1260 			}
   1261 		}
   1262 #endif
   1263 
   1264 		txstat = le32toh(sc->sc_txdescs[txs->txs_lastdesc].td_status);
   1265 		if (txstat & TDSTAT_OWN)
   1266 			break;
   1267 
   1268 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
   1269 
   1270 		sc->sc_txfree += txs->txs_dmamap->dm_nsegs;
   1271 
   1272 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   1273 		    0, txs->txs_dmamap->dm_mapsize,
   1274 		    BUS_DMASYNC_POSTWRITE);
   1275 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1276 		m_freem(txs->txs_mbuf);
   1277 		txs->txs_mbuf = NULL;
   1278 
   1279 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1280 
   1281 		/*
   1282 		 * Check for errors and collisions.
   1283 		 */
   1284 #ifdef TLP_STATS
   1285 		if (txstat & TDSTAT_Tx_UF)
   1286 			sc->sc_stats.ts_tx_uf++;
   1287 		if (txstat & TDSTAT_Tx_TO)
   1288 			sc->sc_stats.ts_tx_to++;
   1289 		if (txstat & TDSTAT_Tx_EC)
   1290 			sc->sc_stats.ts_tx_ec++;
   1291 		if (txstat & TDSTAT_Tx_LC)
   1292 			sc->sc_stats.ts_tx_lc++;
   1293 #endif
   1294 
   1295 		if (txstat & (TDSTAT_Tx_UF|TDSTAT_Tx_TO))
   1296 			ifp->if_oerrors++;
   1297 
   1298 		if (txstat & TDSTAT_Tx_EC)
   1299 			ifp->if_collisions += 16;
   1300 		else
   1301 			ifp->if_collisions += TDSTAT_Tx_COLLISIONS(txstat);
   1302 		if (txstat & TDSTAT_Tx_LC)
   1303 			ifp->if_collisions++;
   1304 
   1305 		ifp->if_opackets++;
   1306 	}
   1307 
   1308 	/*
   1309 	 * If there are no more pending transmissions, cancel the watchdog
   1310 	 * timer.
   1311 	 */
   1312 	if (txs == NULL && (sc->sc_flags & TULIPF_DOING_SETUP) == 0)
   1313 		ifp->if_timer = 0;
   1314 
   1315 	/*
   1316 	 * If we have a receive filter setup pending, do it now.
   1317 	 */
   1318 	if (sc->sc_flags & TULIPF_WANT_SETUP)
   1319 		(*sc->sc_filter_setup)(sc);
   1320 }
   1321 
   1322 #ifdef TLP_STATS
   1323 void
   1324 tlp_print_stats(sc)
   1325 	struct tulip_softc *sc;
   1326 {
   1327 
   1328 	printf("%s: tx_uf %lu, tx_to %lu, tx_ec %lu, tx_lc %lu\n",
   1329 	    sc->sc_dev.dv_xname,
   1330 	    sc->sc_stats.ts_tx_uf, sc->sc_stats.ts_tx_to,
   1331 	    sc->sc_stats.ts_tx_ec, sc->sc_stats.ts_tx_lc);
   1332 }
   1333 #endif
   1334 
   1335 /*
   1336  * tlp_reset:
   1337  *
   1338  *	Perform a soft reset on the Tulip.
   1339  */
   1340 void
   1341 tlp_reset(sc)
   1342 	struct tulip_softc *sc;
   1343 {
   1344 	int i;
   1345 
   1346 	TULIP_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR);
   1347 
   1348 	for (i = 0; i < 1000; i++) {
   1349 		/*
   1350 		 * Wait at least 50 PCI cycles for the reset to
   1351 		 * complete before peeking at the Tulip again.
   1352 		 * 10 uSec is a bit longer than 50 PCI cycles
   1353 		 * (at 33MHz), but it doesn't hurt have the extra
   1354 		 * wait.
   1355 		 */
   1356 		delay(10);
   1357 		if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0)
   1358 			break;
   1359 	}
   1360 
   1361 	if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR))
   1362 		printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
   1363 
   1364 	delay(1000);
   1365 
   1366 	/*
   1367 	 * If the board has any GPIO reset sequences to issue, do them now.
   1368 	 */
   1369 	if (sc->sc_reset != NULL)
   1370 		(*sc->sc_reset)(sc);
   1371 }
   1372 
   1373 /*
   1374  * tlp_init:
   1375  *
   1376  *	Initialize the interface.  Must be called at splnet().
   1377  */
   1378 int
   1379 tlp_init(sc)
   1380 	struct tulip_softc *sc;
   1381 {
   1382 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1383 	struct tulip_txsoft *txs;
   1384 	struct tulip_rxsoft *rxs;
   1385 	int i, error = 0;
   1386 
   1387 	/*
   1388 	 * Cancel any pending I/O.
   1389 	 */
   1390 	tlp_stop(sc, 0);
   1391 
   1392 	/*
   1393 	 * Initialize `opmode' to 0, and call the pre-init routine, if
   1394 	 * any.  This is required because the 2114x and some of the
   1395 	 * clones require that the media-related bits in `opmode' be
   1396 	 * set before performing a soft-reset in order to get internal
   1397 	 * chip pathways are correct.  Yay!
   1398 	 */
   1399 	sc->sc_opmode = 0;
   1400 	if (sc->sc_preinit != NULL)
   1401 		(*sc->sc_preinit)(sc);
   1402 
   1403 	/*
   1404 	 * Reset the Tulip to a known state.
   1405 	 */
   1406 	tlp_reset(sc);
   1407 
   1408 	/*
   1409 	 * Initialize the BUSMODE register.
   1410 	 */
   1411 	sc->sc_busmode = BUSMODE_BAR;
   1412 	switch (sc->sc_chip) {
   1413 	case TULIP_CHIP_21140:
   1414 	case TULIP_CHIP_21140A:
   1415 	case TULIP_CHIP_21142:
   1416 	case TULIP_CHIP_21143:
   1417 	case TULIP_CHIP_82C115:
   1418 	case TULIP_CHIP_MX98725:
   1419 		/*
   1420 		 * If we're allowed to do so, use Memory Read Line
   1421 		 * and Memory Read Multiple.
   1422 		 *
   1423 		 * XXX Should we use Memory Write and Invalidate?
   1424 		 */
   1425 		if (sc->sc_flags & TULIPF_MRL)
   1426 			sc->sc_busmode |= BUSMODE_RLE;
   1427 		if (sc->sc_flags & TULIPF_MRM)
   1428 			sc->sc_busmode |= BUSMODE_RME;
   1429 #if 0
   1430 		if (sc->sc_flags & TULIPF_MWI)
   1431 			sc->sc_busmode |= BUSMODE_WLE;
   1432 #endif
   1433 
   1434 	default:
   1435 		/* Nothing. */
   1436 	}
   1437 	switch (sc->sc_cacheline) {
   1438 	default:
   1439 		/*
   1440 		 * Note: We must *always* set these bits; a cache
   1441 		 * alignment of 0 is RESERVED.
   1442 		 */
   1443 	case 8:
   1444 		sc->sc_busmode |= BUSMODE_CAL_8LW;
   1445 		break;
   1446 	case 16:
   1447 		sc->sc_busmode |= BUSMODE_CAL_16LW;
   1448 		break;
   1449 	case 32:
   1450 		sc->sc_busmode |= BUSMODE_CAL_32LW;
   1451 		break;
   1452 	}
   1453 	switch (sc->sc_chip) {
   1454 	case TULIP_CHIP_82C168:
   1455 	case TULIP_CHIP_82C169:
   1456 		sc->sc_busmode |= BUSMODE_PBL_16LW | BUSMODE_PNIC_MBO;
   1457 		break;
   1458 	default:
   1459 		sc->sc_busmode |= BUSMODE_PBL_DEFAULT;
   1460 		break;
   1461 	}
   1462 #if BYTE_ORDER == BIG_ENDIAN
   1463 	/*
   1464 	 * Can't use BUSMODE_BLE or BUSMODE_DBO; not all chips
   1465 	 * support them, and even on ones that do, it doesn't
   1466 	 * always work.
   1467 	 */
   1468 #endif
   1469 	TULIP_WRITE(sc, CSR_BUSMODE, sc->sc_busmode);
   1470 
   1471 	/*
   1472 	 * Initialize the OPMODE register.  We don't write it until
   1473 	 * we're ready to begin the transmit and receive processes.
   1474 	 *
   1475 	 * Media-related OPMODE bits are set in the media callbacks
   1476 	 * for each specific chip/board.
   1477 	 */
   1478 	sc->sc_opmode |= OPMODE_SR | OPMODE_ST |
   1479 	    sc->sc_txth[sc->sc_txthresh].txth_opmode;
   1480 
   1481 	/*
   1482 	 * Magical mystery initialization on the Macronix chips.
   1483 	 * The MX98713 uses its own magic value, the rest share
   1484 	 * a common one.
   1485 	 */
   1486 	switch (sc->sc_chip) {
   1487 	case TULIP_CHIP_MX98713:
   1488 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98713);
   1489 		break;
   1490 
   1491 	case TULIP_CHIP_MX98713A:
   1492 	case TULIP_CHIP_MX98715:
   1493 	case TULIP_CHIP_MX98715A:
   1494 	case TULIP_CHIP_MX98725:
   1495 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98715);
   1496 		break;
   1497 
   1498 	default:
   1499 		/* Nothing. */
   1500 	}
   1501 
   1502 	/*
   1503 	 * Initialize the transmit descriptor ring.
   1504 	 */
   1505 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
   1506 	for (i = 0; i < TULIP_NTXDESC; i++) {
   1507 		sc->sc_txdescs[i].td_ctl = htole32(TDCTL_CH);
   1508 		sc->sc_txdescs[i].td_bufaddr2 =
   1509 		    htole32(TULIP_CDTXADDR(sc, TULIP_NEXTTX(i)));
   1510 	}
   1511 	TULIP_CDTXSYNC(sc, 0, TULIP_NTXDESC,
   1512 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1513 	sc->sc_txfree = TULIP_NTXDESC;
   1514 	sc->sc_txnext = 0;
   1515 
   1516 	/*
   1517 	 * Initialize the transmit job descriptors.
   1518 	 */
   1519 	SIMPLEQ_INIT(&sc->sc_txfreeq);
   1520 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
   1521 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
   1522 		txs = &sc->sc_txsoft[i];
   1523 		txs->txs_mbuf = NULL;
   1524 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1525 	}
   1526 
   1527 	/*
   1528 	 * Initialize the receive descriptor and receive job
   1529 	 * descriptor rings.
   1530 	 */
   1531 	for (i = 0; i < TULIP_NRXDESC; i++) {
   1532 		rxs = &sc->sc_rxsoft[i];
   1533 		if (rxs->rxs_mbuf == NULL) {
   1534 			if ((error = tlp_add_rxbuf(sc, i)) != 0) {
   1535 				printf("%s: unable to allocate or map rx "
   1536 				    "buffer %d, error = %d\n",
   1537 				    sc->sc_dev.dv_xname, i, error);
   1538 				/*
   1539 				 * XXX Should attempt to run with fewer receive
   1540 				 * XXX buffers instead of just failing.
   1541 				 */
   1542 				tlp_rxdrain(sc);
   1543 				goto out;
   1544 			}
   1545 		}
   1546 	}
   1547 	sc->sc_rxptr = 0;
   1548 
   1549 	/*
   1550 	 * Initialize the interrupt mask and enable interrupts.
   1551 	 */
   1552 	/* normal interrupts */
   1553 	sc->sc_inten =  STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
   1554 
   1555 	/* abnormal interrupts */
   1556 	sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
   1557 	    STATUS_RU | STATUS_RPS | STATUS_RWT | STATUS_SE | STATUS_AIS;
   1558 
   1559 	sc->sc_rxint_mask = STATUS_RI|STATUS_RU|STATUS_RWT;
   1560 	sc->sc_txint_mask = STATUS_TI|STATUS_UNF|STATUS_TJT;
   1561 
   1562 	switch (sc->sc_chip) {
   1563 	case TULIP_CHIP_WB89C840F:
   1564 		/*
   1565 		 * Clear bits that we don't want that happen to
   1566 		 * overlap or don't exist.
   1567 		 */
   1568 		sc->sc_inten &= ~(STATUS_WINB_REI|STATUS_RWT);
   1569 		break;
   1570 
   1571 	default:
   1572 		/* Nothing. */
   1573 	}
   1574 
   1575 	sc->sc_rxint_mask &= sc->sc_inten;
   1576 	sc->sc_txint_mask &= sc->sc_inten;
   1577 
   1578 	TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
   1579 	TULIP_WRITE(sc, CSR_STATUS, 0xffffffff);
   1580 
   1581 	/*
   1582 	 * Give the transmit and receive rings to the Tulip.
   1583 	 */
   1584 	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDTXADDR(sc, sc->sc_txnext));
   1585 	TULIP_WRITE(sc, CSR_RXLIST, TULIP_CDRXADDR(sc, sc->sc_rxptr));
   1586 
   1587 	/*
   1588 	 * On chips that do this differently, set the station address.
   1589 	 */
   1590 	switch (sc->sc_chip) {
   1591 	case TULIP_CHIP_WB89C840F:
   1592 	    {
   1593 		/* XXX Do this with stream writes? */
   1594 		bus_addr_t cpa = TULIP_CSR_OFFSET(sc, CSR_WINB_CPA0);
   1595 
   1596 		for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1597 			bus_space_write_1(sc->sc_st, sc->sc_sh,
   1598 			    cpa + i, LLADDR(ifp->if_sadl)[i]);
   1599 		}
   1600 		break;
   1601 	    }
   1602 
   1603 	case TULIP_CHIP_AL981:
   1604 	    {
   1605 		u_int32_t reg;
   1606 		u_int8_t *enaddr = LLADDR(ifp->if_sadl);
   1607 
   1608 		reg = enaddr[0] |
   1609 		      (enaddr[1] << 8) |
   1610 		      (enaddr[2] << 16) |
   1611 		      (enaddr[3] << 24);
   1612 		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR0, reg);
   1613 
   1614 		reg = enaddr[4] |
   1615 		      (enaddr[5] << 8);
   1616 		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR1, reg);
   1617 	    }
   1618 
   1619 	default:
   1620 		/* Nothing. */
   1621 	}
   1622 
   1623 	/*
   1624 	 * Set the receive filter.  This will start the transmit and
   1625 	 * receive processes.
   1626 	 */
   1627 	(*sc->sc_filter_setup)(sc);
   1628 
   1629 	/*
   1630 	 * Set the current media.
   1631 	 */
   1632 	(void) (*sc->sc_mediasw->tmsw_set)(sc);
   1633 
   1634 	/*
   1635 	 * Start the receive process.
   1636 	 */
   1637 	TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
   1638 
   1639 	if (sc->sc_tick != NULL) {
   1640 		/* Start the one second clock. */
   1641 		timeout(sc->sc_tick, sc, hz);
   1642 	}
   1643 
   1644 	/*
   1645 	 * Note that the interface is now running.
   1646 	 */
   1647 	ifp->if_flags |= IFF_RUNNING;
   1648 	ifp->if_flags &= ~IFF_OACTIVE;
   1649 
   1650  out:
   1651 	if (error)
   1652 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1653 	return (error);
   1654 }
   1655 
   1656 /*
   1657  * tlp_rxdrain:
   1658  *
   1659  *	Drain the receive queue.
   1660  */
   1661 void
   1662 tlp_rxdrain(sc)
   1663 	struct tulip_softc *sc;
   1664 {
   1665 	struct tulip_rxsoft *rxs;
   1666 	int i;
   1667 
   1668 	for (i = 0; i < TULIP_NRXDESC; i++) {
   1669 		rxs = &sc->sc_rxsoft[i];
   1670 		if (rxs->rxs_mbuf != NULL) {
   1671 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1672 			m_freem(rxs->rxs_mbuf);
   1673 			rxs->rxs_mbuf = NULL;
   1674 		}
   1675 	}
   1676 }
   1677 
   1678 /*
   1679  * tlp_stop:
   1680  *
   1681  *	Stop transmission on the interface.
   1682  */
   1683 void
   1684 tlp_stop(sc, drain)
   1685 	struct tulip_softc *sc;
   1686 	int drain;
   1687 {
   1688 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1689 	struct tulip_txsoft *txs;
   1690 
   1691 	if (sc->sc_tick != NULL) {
   1692 		/* Stop the one second clock. */
   1693 		untimeout(sc->sc_tick, sc);
   1694 	}
   1695 
   1696 	if (sc->sc_flags & TULIPF_HAS_MII) {
   1697 		/* Down the MII. */
   1698 		mii_down(&sc->sc_mii);
   1699 	}
   1700 
   1701 	/* Disable interrupts. */
   1702 	TULIP_WRITE(sc, CSR_INTEN, 0);
   1703 
   1704 	/* Stop the transmit and receive processes. */
   1705 	TULIP_WRITE(sc, CSR_OPMODE, 0);
   1706 	TULIP_WRITE(sc, CSR_RXLIST, 0);
   1707 	TULIP_WRITE(sc, CSR_TXLIST, 0);
   1708 
   1709 	/*
   1710 	 * Release any queued transmit buffers.
   1711 	 */
   1712 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1713 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
   1714 		if (txs->txs_mbuf != NULL) {
   1715 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1716 			m_freem(txs->txs_mbuf);
   1717 			txs->txs_mbuf = NULL;
   1718 		}
   1719 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1720 	}
   1721 
   1722 	if (drain) {
   1723 		/*
   1724 		 * Release the receive buffers.
   1725 		 */
   1726 		tlp_rxdrain(sc);
   1727 	}
   1728 
   1729 	sc->sc_flags &= ~(TULIPF_WANT_SETUP|TULIPF_DOING_SETUP);
   1730 
   1731 	/*
   1732 	 * Mark the interface down and cancel the watchdog timer.
   1733 	 */
   1734 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1735 	ifp->if_timer = 0;
   1736 }
   1737 
   1738 #define	SROM_EMIT(sc, x)						\
   1739 do {									\
   1740 	TULIP_WRITE((sc), CSR_MIIROM, (x));				\
   1741 	delay(1);							\
   1742 } while (0)
   1743 
   1744 /*
   1745  * tlp_srom_idle:
   1746  *
   1747  *	Put the SROM in idle state.
   1748  */
   1749 void
   1750 tlp_srom_idle(sc)
   1751 	struct tulip_softc *sc;
   1752 {
   1753 	u_int32_t miirom;
   1754 	int i;
   1755 
   1756 	miirom = MIIROM_SR;
   1757 	SROM_EMIT(sc, miirom);
   1758 
   1759 	miirom |= MIIROM_RD;
   1760 	SROM_EMIT(sc, miirom);
   1761 
   1762 	miirom |= MIIROM_SROMCS;
   1763 	SROM_EMIT(sc, miirom);
   1764 
   1765 	SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1766 
   1767 	/* Strobe the clock 25 times. */
   1768 	for (i = 0; i < 25; i++) {
   1769 		SROM_EMIT(sc, miirom);
   1770 		SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1771 	}
   1772 
   1773 	SROM_EMIT(sc, miirom);
   1774 
   1775 	miirom &= ~MIIROM_SROMCS;
   1776 	SROM_EMIT(sc, miirom);
   1777 
   1778 	SROM_EMIT(sc, 0);
   1779 }
   1780 
   1781 /*
   1782  * tlp_read_srom:
   1783  *
   1784  *	Read the Tulip SROM.
   1785  */
   1786 void
   1787 tlp_read_srom(sc, word, wordcnt, data)
   1788 	struct tulip_softc *sc;
   1789 	int word, wordcnt;
   1790 	u_int8_t *data;
   1791 {
   1792 	u_int32_t miirom;
   1793 	u_int16_t datain;
   1794 	int i, x;
   1795 
   1796 	tlp_srom_idle(sc);
   1797 
   1798 	/* Select the SROM. */
   1799 	miirom = MIIROM_SR;
   1800 	SROM_EMIT(sc, miirom);
   1801 
   1802 	miirom |= MIIROM_RD;
   1803 	SROM_EMIT(sc, miirom);
   1804 
   1805 	for (i = 0; i < wordcnt; i++) {
   1806 		/* Send CHIP SELECT for one clock tick. */
   1807 		miirom |= MIIROM_SROMCS;
   1808 		SROM_EMIT(sc, miirom);
   1809 
   1810 		/* Shift in the READ opcode. */
   1811 		for (x = 3; x > 0; x--) {
   1812 			if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
   1813 				miirom |= MIIROM_SROMDI;
   1814 			else
   1815 				miirom &= ~MIIROM_SROMDI;
   1816 			SROM_EMIT(sc, miirom);
   1817 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1818 			SROM_EMIT(sc, miirom);
   1819 		}
   1820 
   1821 		/* Shift in address. */
   1822 		for (x = sc->sc_srom_addrbits; x > 0; x--) {
   1823 			if ((word + i) & (1 << (x - 1)))
   1824 				miirom |= MIIROM_SROMDI;
   1825 			else
   1826 				miirom &= ~MIIROM_SROMDI;
   1827 			SROM_EMIT(sc, miirom);
   1828 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1829 			SROM_EMIT(sc, miirom);
   1830 		}
   1831 
   1832 		/* Shift out data. */
   1833 		miirom &= ~MIIROM_SROMDI;
   1834 		datain = 0;
   1835 		for (x = 16; x > 0; x--) {
   1836 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1837 			if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
   1838 				datain |= (1 << (x - 1));
   1839 			SROM_EMIT(sc, miirom);
   1840 		}
   1841 		data[2 * i] = datain & 0xff;
   1842 		data[(2 * i) + 1] = datain >> 8;
   1843 
   1844 		/* Clear CHIP SELECT. */
   1845 		miirom &= ~MIIROM_SROMCS;
   1846 		SROM_EMIT(sc, miirom);
   1847 	}
   1848 
   1849 	/* Deselect the SROM. */
   1850 	SROM_EMIT(sc, 0);
   1851 
   1852 	/* ...and idle it. */
   1853 	tlp_srom_idle(sc);
   1854 }
   1855 
   1856 #undef SROM_EMIT
   1857 
   1858 /*
   1859  * tlp_add_rxbuf:
   1860  *
   1861  *	Add a receive buffer to the indicated descriptor.
   1862  */
   1863 int
   1864 tlp_add_rxbuf(sc, idx)
   1865 	struct tulip_softc *sc;
   1866 	int idx;
   1867 {
   1868 	struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx];
   1869 	struct mbuf *m;
   1870 	int error;
   1871 
   1872 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1873 	if (m == NULL)
   1874 		return (ENOBUFS);
   1875 
   1876 	MCLGET(m, M_DONTWAIT);
   1877 	if ((m->m_flags & M_EXT) == 0) {
   1878 		m_freem(m);
   1879 		return (ENOBUFS);
   1880 	}
   1881 
   1882 	if (rxs->rxs_mbuf != NULL)
   1883 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1884 
   1885 	rxs->rxs_mbuf = m;
   1886 
   1887 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
   1888 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
   1889 	if (error) {
   1890 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1891 		    sc->sc_dev.dv_xname, idx, error);
   1892 		panic("tlp_add_rxbuf");	/* XXX */
   1893 	}
   1894 
   1895 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1896 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1897 
   1898 	TULIP_INIT_RXDESC(sc, idx);
   1899 
   1900 	return (0);
   1901 }
   1902 
   1903 /*
   1904  * tlp_crc32:
   1905  *
   1906  *	Compute the 32-bit CRC of the provided buffer.
   1907  */
   1908 u_int32_t
   1909 tlp_crc32(buf, len)
   1910 	const u_int8_t *buf;
   1911 	size_t len;
   1912 {
   1913 	static const u_int32_t crctab[] = {
   1914 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   1915 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   1916 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   1917 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   1918 	};
   1919 	u_int32_t crc;
   1920 	int i;
   1921 
   1922 	crc = 0xffffffff;
   1923 	for (i = 0; i < len; i++) {
   1924 		crc ^= buf[i];
   1925 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1926 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1927 	}
   1928 	return (crc);
   1929 }
   1930 
   1931 /*
   1932  * tlp_srom_crcok:
   1933  *
   1934  *	Check the CRC of the Tulip SROM.
   1935  */
   1936 int
   1937 tlp_srom_crcok(romdata)
   1938 	const u_int8_t *romdata;
   1939 {
   1940 	u_int32_t crc;
   1941 
   1942 	crc = tlp_crc32(romdata, TULIP_ROM_CRC32_CHECKSUM);
   1943 	crc = (crc & 0xffff) ^ 0xffff;
   1944 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM))
   1945 		return (1);
   1946 
   1947 	/*
   1948 	 * Try an alternate checksum.
   1949 	 */
   1950 	crc = tlp_crc32(romdata, TULIP_ROM_CRC32_CHECKSUM1);
   1951 	crc = (crc & 0xffff) ^ 0xffff;
   1952 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM1))
   1953 		return (1);
   1954 
   1955 	return (0);
   1956 }
   1957 
   1958 /*
   1959  * tlp_isv_srom:
   1960  *
   1961  *	Check to see if the SROM is in the new standardized format.
   1962  */
   1963 int
   1964 tlp_isv_srom(romdata)
   1965 	const u_int8_t *romdata;
   1966 {
   1967 	int i;
   1968 	u_int16_t cksum;
   1969 
   1970 	if (tlp_srom_crcok(romdata)) {
   1971 		/*
   1972 		 * SROM CRC checks out; must be in the new format.
   1973 		 */
   1974 		return (1);
   1975 	}
   1976 
   1977 	cksum = TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM);
   1978 	if (cksum == 0xffff || cksum == 0) {
   1979 		/*
   1980 		 * No checksum present.  Check the SROM ID; 18 bytes of 0
   1981 		 * followed by 1 (version) followed by the number of
   1982 		 * adapters which use this SROM (should be non-zero).
   1983 		 */
   1984 		for (i = 0; i < TULIP_ROM_SROM_FORMAT_VERION; i++) {
   1985 			if (romdata[i] != 0)
   1986 				return (0);
   1987 		}
   1988 		if (romdata[TULIP_ROM_SROM_FORMAT_VERION] != 1)
   1989 			return (0);
   1990 		if (romdata[TULIP_ROM_CHIP_COUNT] == 0)
   1991 			return (0);
   1992 		return (1);
   1993 	}
   1994 
   1995 	return (0);
   1996 }
   1997 
   1998 /*
   1999  * tlp_isv_srom_enaddr:
   2000  *
   2001  *	Get the Ethernet address from an ISV SROM.
   2002  */
   2003 int
   2004 tlp_isv_srom_enaddr(sc, enaddr)
   2005 	struct tulip_softc *sc;
   2006 	u_int8_t *enaddr;
   2007 {
   2008 	int i, devcnt;
   2009 
   2010 	if (tlp_isv_srom(sc->sc_srom) == 0)
   2011 		return (0);
   2012 
   2013 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   2014 	for (i = 0; i < devcnt; i++) {
   2015 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   2016 			break;
   2017 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   2018 		    sc->sc_devno)
   2019 			break;
   2020 	}
   2021 
   2022 	if (i == devcnt)
   2023 		return (0);
   2024 
   2025 	memcpy(enaddr, &sc->sc_srom[TULIP_ROM_IEEE_NETWORK_ADDRESS],
   2026 	    ETHER_ADDR_LEN);
   2027 	enaddr[5] += i;
   2028 
   2029 	return (1);
   2030 }
   2031 
   2032 /*
   2033  * tlp_parse_old_srom:
   2034  *
   2035  *	Parse old-format SROMs.
   2036  *
   2037  *	This routine is largely lifted from Matt Thomas's `de' driver.
   2038  */
   2039 int
   2040 tlp_parse_old_srom(sc, enaddr)
   2041 	struct tulip_softc *sc;
   2042 	u_int8_t *enaddr;
   2043 {
   2044 	static const u_int8_t testpat[] =
   2045 	    { 0xff, 0, 0x55, 0xaa, 0xff, 0, 0x55, 0xaa };
   2046 	int i;
   2047 	u_int32_t cksum;
   2048 
   2049 	if (memcmp(&sc->sc_srom[0], &sc->sc_srom[16], 8) != 0) {
   2050 		/*
   2051 		 * Some vendors (e.g. ZNYX) don't use the standard
   2052 		 * DEC Address ROM format, but rather just have an
   2053 		 * Ethernet address in the first 6 bytes, maybe a
   2054 		 * 2 byte checksum, and then all 0xff's.
   2055 		 *
   2056 		 * On the other hand, Cobalt Networks interfaces
   2057 		 * simply have the address in the first six bytes
   2058 		 * with the rest zeroed out.
   2059 		 */
   2060 		for (i = 8; i < 32; i++) {
   2061 			if (sc->sc_srom[i] != 0xff &&
   2062 			    sc->sc_srom[i] != 0)
   2063 				return (0);
   2064 		}
   2065 
   2066 		/*
   2067 		 * Sanity check the Ethernet address:
   2068 		 *
   2069 		 *	- Make sure it's not multicast or locally
   2070 		 *	  assigned
   2071 		 *	- Make sure it has a non-0 OUI
   2072 		 */
   2073 		if (sc->sc_srom[0] & 3)
   2074 			return (0);
   2075 		if (sc->sc_srom[0] == 0 && sc->sc_srom[1] == 0 &&
   2076 		    sc->sc_srom[2] == 0)
   2077 			return (0);
   2078 
   2079 		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   2080 		return (1);
   2081 	}
   2082 
   2083 	/*
   2084 	 * Standard DEC Address ROM test.
   2085 	 */
   2086 
   2087 	if (memcmp(&sc->sc_srom[24], testpat, 8) != 0)
   2088 		return (0);
   2089 
   2090 	for (i = 0; i < 8; i++) {
   2091 		if (sc->sc_srom[i] != sc->sc_srom[15 - i])
   2092 			return (0);
   2093 	}
   2094 
   2095 	memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   2096 
   2097 	cksum = *(u_int16_t *) &enaddr[0];
   2098 
   2099 	cksum <<= 1;
   2100 	if (cksum > 0xffff)
   2101 		cksum -= 0xffff;
   2102 
   2103 	cksum += *(u_int16_t *) &enaddr[2];
   2104 	if (cksum > 0xffff)
   2105 		cksum -= 0xffff;
   2106 
   2107 	cksum <<= 1;
   2108 	if (cksum > 0xffff)
   2109 		cksum -= 0xffff;
   2110 
   2111 	cksum += *(u_int16_t *) &enaddr[4];
   2112 	if (cksum >= 0xffff)
   2113 		cksum -= 0xffff;
   2114 
   2115 	if (cksum != *(u_int16_t *) &sc->sc_srom[6])
   2116 		return (0);
   2117 
   2118 	return (1);
   2119 }
   2120 
   2121 /*
   2122  * tlp_filter_setup:
   2123  *
   2124  *	Set the Tulip's receive filter.
   2125  */
   2126 void
   2127 tlp_filter_setup(sc)
   2128 	struct tulip_softc *sc;
   2129 {
   2130 	struct ethercom *ec = &sc->sc_ethercom;
   2131 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2132 	struct ether_multi *enm;
   2133 	struct ether_multistep step;
   2134 	__volatile u_int32_t *sp;
   2135 	u_int8_t enaddr[ETHER_ADDR_LEN];
   2136 	u_int32_t hash, hashsize;
   2137 	int cnt;
   2138 
   2139 	DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n",
   2140 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2141 
   2142 	memcpy(enaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   2143 
   2144 	/*
   2145 	 * If there are transmissions pending, wait until they have
   2146 	 * completed.
   2147 	 */
   2148 	if (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL ||
   2149 	    (sc->sc_flags & TULIPF_DOING_SETUP) != 0) {
   2150 		sc->sc_flags |= TULIPF_WANT_SETUP;
   2151 		DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n",
   2152 		    sc->sc_dev.dv_xname));
   2153 		return;
   2154 	}
   2155 	sc->sc_flags &= ~TULIPF_WANT_SETUP;
   2156 
   2157 	switch (sc->sc_chip) {
   2158 	case TULIP_CHIP_82C115:
   2159 		hashsize = TULIP_PNICII_HASHSIZE;
   2160 		break;
   2161 
   2162 	default:
   2163 		hashsize = TULIP_MCHASHSIZE;
   2164 	}
   2165 
   2166 	/*
   2167 	 * If we're running, idle the transmit and receive engines.  If
   2168 	 * we're NOT running, we're being called from tlp_init(), and our
   2169 	 * writing OPMODE will start the transmit and receive processes
   2170 	 * in motion.
   2171 	 */
   2172 	if (ifp->if_flags & IFF_RUNNING) {
   2173 		/*
   2174 		 * Actually, some chips seem to need a really hard
   2175 		 * kick in the head for this to work.  The genuine
   2176 		 * DEC chips can just be idled, but some of the
   2177 		 * clones seem to REALLY want a reset here.  Doing
   2178 		 * the reset will end up here again, but with
   2179 		 * IFF_RUNNING cleared.
   2180 		 */
   2181 		switch (sc->sc_chip) {
   2182 		case TULIP_CHIP_82C168:
   2183 		case TULIP_CHIP_82C169:
   2184 			tlp_init(sc);
   2185 			return;
   2186 
   2187 		default:
   2188 			tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2189 		}
   2190 	}
   2191 
   2192 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
   2193 
   2194 	if (ifp->if_flags & IFF_PROMISC) {
   2195 		sc->sc_opmode |= OPMODE_PR;
   2196 		goto allmulti;
   2197 	}
   2198 
   2199 	/*
   2200 	 * Try Perfect filtering first.
   2201 	 */
   2202 
   2203 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   2204 	sp = TULIP_CDSP(sc);
   2205 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2206 	cnt = 0;
   2207 	ETHER_FIRST_MULTI(step, ec, enm);
   2208 	while (enm != NULL) {
   2209 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2210 			/*
   2211 			 * We must listen to a range of multicast addresses.
   2212 			 * For now, just accept all multicasts, rather than
   2213 			 * trying to set only those filter bits needed to match
   2214 			 * the range.  (At this time, the only use of address
   2215 			 * ranges is for IP multicast routing, for which the
   2216 			 * range is big enough to require all bits set.)
   2217 			 */
   2218 			goto allmulti;
   2219 		}
   2220 		if (cnt == (TULIP_MAXADDRS - 2)) {
   2221 			/*
   2222 			 * We already have our multicast limit (still need
   2223 			 * our station address and broadcast).  Go to
   2224 			 * Hash-Perfect mode.
   2225 			 */
   2226 			goto hashperfect;
   2227 		}
   2228 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 0);
   2229 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 1);
   2230 		*sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 2);
   2231 		ETHER_NEXT_MULTI(step, enm);
   2232 	}
   2233 
   2234 	if (ifp->if_flags & IFF_BROADCAST) {
   2235 		/* ...and the broadcast address. */
   2236 		cnt++;
   2237 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2238 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2239 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2240 	}
   2241 
   2242 	/* Pad the rest with our station address. */
   2243 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   2244 		*sp++ = TULIP_SP_FIELD(enaddr, 0);
   2245 		*sp++ = TULIP_SP_FIELD(enaddr, 1);
   2246 		*sp++ = TULIP_SP_FIELD(enaddr, 2);
   2247 	}
   2248 	ifp->if_flags &= ~IFF_ALLMULTI;
   2249 	goto setit;
   2250 
   2251  hashperfect:
   2252 	/*
   2253 	 * Try Hash-Perfect mode.
   2254 	 */
   2255 
   2256 	/*
   2257 	 * Some 21140 chips have broken Hash-Perfect modes.  On these
   2258 	 * chips, we simply use Hash-Only mode, and put our station
   2259 	 * address into the filter.
   2260 	 */
   2261 	if (sc->sc_chip == TULIP_CHIP_21140)
   2262 		sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY;
   2263 	else
   2264 		sc->sc_filtmode = TDCTL_Tx_FT_HASH;
   2265 	sp = TULIP_CDSP(sc);
   2266 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2267 	ETHER_FIRST_MULTI(step, ec, enm);
   2268 	while (enm != NULL) {
   2269 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2270 			/*
   2271 			 * We must listen to a range of multicast addresses.
   2272 			 * For now, just accept all multicasts, rather than
   2273 			 * trying to set only those filter bits needed to match
   2274 			 * the range.  (At this time, the only use of address
   2275 			 * ranges is for IP multicast routing, for which the
   2276 			 * range is big enough to require all bits set.)
   2277 			 */
   2278 			goto allmulti;
   2279 		}
   2280 		hash = tlp_mchash(enm->enm_addrlo, hashsize);
   2281 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2282 		ETHER_NEXT_MULTI(step, enm);
   2283 	}
   2284 
   2285 	if (ifp->if_flags & IFF_BROADCAST) {
   2286 		/* ...and the broadcast address. */
   2287 		hash = tlp_mchash(etherbroadcastaddr, hashsize);
   2288 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2289 	}
   2290 
   2291 	if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
   2292 		/* ...and our station address. */
   2293 		hash = tlp_mchash(enaddr, hashsize);
   2294 		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
   2295 	} else {
   2296 		/*
   2297 		 * Hash-Perfect mode; put our station address after
   2298 		 * the hash table.
   2299 		 */
   2300 		sp[39] = TULIP_SP_FIELD(enaddr, 0);
   2301 		sp[40] = TULIP_SP_FIELD(enaddr, 1);
   2302 		sp[41] = TULIP_SP_FIELD(enaddr, 2);
   2303 	}
   2304 	ifp->if_flags &= ~IFF_ALLMULTI;
   2305 	goto setit;
   2306 
   2307  allmulti:
   2308 	/*
   2309 	 * Use Perfect filter mode.  First address is the broadcast address,
   2310 	 * and pad the rest with our station address.  We'll set Pass-all-
   2311 	 * multicast in OPMODE below.
   2312 	 */
   2313 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   2314 	sp = TULIP_CDSP(sc);
   2315 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2316 	cnt = 0;
   2317 	if (ifp->if_flags & IFF_BROADCAST) {
   2318 		cnt++;
   2319 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2320 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2321 		*sp++ = TULIP_SP_FIELD_C(0xffff);
   2322 	}
   2323 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   2324 		*sp++ = TULIP_SP_FIELD(enaddr, 0);
   2325 		*sp++ = TULIP_SP_FIELD(enaddr, 1);
   2326 		*sp++ = TULIP_SP_FIELD(enaddr, 2);
   2327 	}
   2328 	ifp->if_flags |= IFF_ALLMULTI;
   2329 
   2330  setit:
   2331 	if (ifp->if_flags & IFF_ALLMULTI)
   2332 		sc->sc_opmode |= OPMODE_PM;
   2333 
   2334 	/* Sync the setup packet buffer. */
   2335 	TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE);
   2336 
   2337 	/*
   2338 	 * Fill in the setup packet descriptor.
   2339 	 */
   2340 	sc->sc_setup_desc.td_bufaddr1 = htole32(TULIP_CDSPADDR(sc));
   2341 	sc->sc_setup_desc.td_bufaddr2 =
   2342 	    htole32(TULIP_CDTXADDR(sc, sc->sc_txnext));
   2343 	sc->sc_setup_desc.td_ctl =
   2344 	    htole32((TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) |
   2345 	    sc->sc_filtmode | TDCTL_Tx_SET | TDCTL_Tx_FS | TDCTL_Tx_LS |
   2346 	    TDCTL_Tx_IC | TDCTL_CH);
   2347 	sc->sc_setup_desc.td_status = htole32(TDSTAT_OWN);
   2348 	TULIP_CDSDSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   2349 
   2350 	/*
   2351 	 * Write the address of the setup descriptor.  This also has
   2352 	 * the side effect of giving the transmit ring to the chip,
   2353 	 * since the setup descriptor points to the next available
   2354 	 * descriptor in the ring.
   2355 	 */
   2356 	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDSDADDR(sc));
   2357 
   2358 	/*
   2359 	 * Set the OPMODE register.  This will also resume the
   2360 	 * transmit transmit process we idled above.
   2361 	 */
   2362 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2363 
   2364 	sc->sc_flags |= TULIPF_DOING_SETUP;
   2365 
   2366 	/*
   2367 	 * Kick the transmitter; this will cause the Tulip to
   2368 	 * read the setup descriptor.
   2369 	 */
   2370 	/* XXX USE AUTOPOLLING? */
   2371 	TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
   2372 
   2373 	/* Set up a watchdog timer in case the chip flakes out. */
   2374 	ifp->if_timer = 5;
   2375 
   2376 	DPRINTF(sc, ("%s: tlp_filter_setup: returning\n", sc->sc_dev.dv_xname));
   2377 }
   2378 
   2379 /*
   2380  * tlp_winb_filter_setup:
   2381  *
   2382  *	Set the Winbond 89C840F's receive filter.
   2383  */
   2384 void
   2385 tlp_winb_filter_setup(sc)
   2386 	struct tulip_softc *sc;
   2387 {
   2388 	struct ethercom *ec = &sc->sc_ethercom;
   2389 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2390 	struct ether_multi *enm;
   2391 	struct ether_multistep step;
   2392 	u_int32_t hash, mchash[2];
   2393 
   2394 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n",
   2395 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2396 
   2397 	sc->sc_opmode &= ~(OPMODE_WINB_APP|OPMODE_WINB_AMP|OPMODE_WINB_ABP);
   2398 
   2399 	if (ifp->if_flags & IFF_MULTICAST)
   2400 		sc->sc_opmode |= OPMODE_WINB_AMP;
   2401 
   2402 	if (ifp->if_flags & IFF_BROADCAST)
   2403 		sc->sc_opmode |= OPMODE_WINB_ABP;
   2404 
   2405 	if (ifp->if_flags & IFF_PROMISC) {
   2406 		sc->sc_opmode |= OPMODE_WINB_APP;
   2407 		goto allmulti;
   2408 	}
   2409 
   2410 	mchash[0] = mchash[1] = 0;
   2411 
   2412 	ETHER_FIRST_MULTI(step, ec, enm);
   2413 	while (enm != NULL) {
   2414 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2415 			/*
   2416 			 * We must listen to a range of multicast addresses.
   2417 			 * For now, just accept all multicasts, rather than
   2418 			 * trying to set only those filter bits needed to match
   2419 			 * the range.  (At this time, the only use of address
   2420 			 * ranges is for IP multicast routing, for which the
   2421 			 * range is big enough to require all bits set.)
   2422 			 */
   2423 			goto allmulti;
   2424 		}
   2425 
   2426 		/*
   2427 		 * According to the FreeBSD `wb' driver, yes, you
   2428 		 * really do invert the hash.
   2429 		 */
   2430 		hash = (~(tlp_crc32(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26))
   2431 		    & 0x3f;
   2432 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
   2433 		ETHER_NEXT_MULTI(step, enm);
   2434 	}
   2435 	ifp->if_flags &= ~IFF_ALLMULTI;
   2436 	goto setit;
   2437 
   2438  allmulti:
   2439 	ifp->if_flags |= IFF_ALLMULTI;
   2440 	mchash[0] = mchash[1] = 0xffffffff;
   2441 
   2442  setit:
   2443 	TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]);
   2444 	TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]);
   2445 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2446 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n",
   2447 	    sc->sc_dev.dv_xname));
   2448 }
   2449 
   2450 /*
   2451  * tlp_al981_filter_setup:
   2452  *
   2453  *	Set the ADMtek AL981's receive filter.
   2454  */
   2455 void
   2456 tlp_al981_filter_setup(sc)
   2457 	struct tulip_softc *sc;
   2458 {
   2459 	struct ethercom *ec = &sc->sc_ethercom;
   2460 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2461 	struct ether_multi *enm;
   2462 	struct ether_multistep step;
   2463 	u_int32_t hash, mchash[2];
   2464 
   2465 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: sc_flags 0x%08x\n",
   2466 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2467 
   2468 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2469 
   2470 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
   2471 
   2472 	if (ifp->if_flags & IFF_PROMISC) {
   2473 		sc->sc_opmode |= OPMODE_PR;
   2474 		goto allmulti;
   2475 	}
   2476 
   2477 	mchash[0] = mchash[1] = 0;
   2478 
   2479 	ETHER_FIRST_MULTI(step, ec, enm);
   2480 	while (enm != NULL) {
   2481 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2482 			/*
   2483 			 * We must listen to a range of multicast addresses.
   2484 			 * For now, just accept all multicasts, rather than
   2485 			 * trying to set only those filter bits needed to match
   2486 			 * the range.  (At this time, the only use of address
   2487 			 * ranges is for IP multicast routing, for which the
   2488 			 * range is big enough to require all bits set.)
   2489 			 */
   2490 			goto allmulti;
   2491 		}
   2492 
   2493 		hash = (tlp_crc32(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26)
   2494 		    & 0x3f;
   2495 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
   2496 		ETHER_NEXT_MULTI(step, enm);
   2497 	}
   2498 	ifp->if_flags &= ~IFF_ALLMULTI;
   2499 	goto setit;
   2500 
   2501  allmulti:
   2502 	ifp->if_flags |= IFF_ALLMULTI;
   2503 	mchash[0] = mchash[1] = 0xffffffff;
   2504 
   2505  setit:
   2506 	TULIP_WRITE(sc, CSR_ADM_MAR0, mchash[0]);
   2507 	TULIP_WRITE(sc, CSR_ADM_MAR1, mchash[1]);
   2508 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2509 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: returning\n",
   2510 	    sc->sc_dev.dv_xname));
   2511 }
   2512 
   2513 /*
   2514  * tlp_idle:
   2515  *
   2516  *	Cause the transmit and/or receive processes to go idle.
   2517  */
   2518 void
   2519 tlp_idle(sc, bits)
   2520 	struct tulip_softc *sc;
   2521 	u_int32_t bits;
   2522 {
   2523 	static const char *tx_state_names[] = {
   2524 		"STOPPED",
   2525 		"RUNNING - FETCH",
   2526 		"RUNNING - WAIT",
   2527 		"RUNNING - READING",
   2528 		"-- RESERVED --",
   2529 		"RUNNING - SETUP",
   2530 		"SUSPENDED",
   2531 		"RUNNING - CLOSE",
   2532 	};
   2533 	static const char *rx_state_names[] = {
   2534 		"STOPPED",
   2535 		"RUNNING - FETCH",
   2536 		"RUNNING - CHECK",
   2537 		"RUNNING - WAIT",
   2538 		"SUSPENDED",
   2539 		"RUNNING - CLOSE",
   2540 		"RUNNING - FLUSH",
   2541 		"RUNNING - QUEUE",
   2542 	};
   2543 	u_int32_t csr, ackmask = 0;
   2544 	int i;
   2545 
   2546 	if (bits & OPMODE_ST)
   2547 		ackmask |= STATUS_TPS;
   2548 
   2549 	if (bits & OPMODE_SR)
   2550 		ackmask |= STATUS_RPS;
   2551 
   2552 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits);
   2553 
   2554 	for (i = 0; i < 1000; i++) {
   2555 		if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
   2556 			break;
   2557 		delay(10);
   2558 	}
   2559 
   2560 	csr = TULIP_READ(sc, CSR_STATUS);
   2561 	if ((csr & ackmask) != ackmask) {
   2562 		if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
   2563 		    (csr & STATUS_TS) != STATUS_TS_STOPPED)
   2564 			printf("%s: transmit process failed to idle: "
   2565 			    "state %s\n", sc->sc_dev.dv_xname,
   2566 			    tx_state_names[(csr & STATUS_TS) >> 20]);
   2567 		if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
   2568 		    (csr & STATUS_RS) != STATUS_RS_STOPPED)
   2569 			printf("%s: receive process failed to idle: "
   2570 			    "state %s\n", sc->sc_dev.dv_xname,
   2571 			    rx_state_names[(csr & STATUS_RS) >> 17]);
   2572 	}
   2573 	TULIP_WRITE(sc, CSR_STATUS, ackmask);
   2574 }
   2575 
   2576 /*****************************************************************************
   2577  * Generic media support functions.
   2578  *****************************************************************************/
   2579 
   2580 /*
   2581  * tlp_mediastatus:	[ifmedia interface function]
   2582  *
   2583  *	Query the current media.
   2584  */
   2585 void
   2586 tlp_mediastatus(ifp, ifmr)
   2587 	struct ifnet *ifp;
   2588 	struct ifmediareq *ifmr;
   2589 {
   2590 	struct tulip_softc *sc = ifp->if_softc;
   2591 
   2592 	(*sc->sc_mediasw->tmsw_get)(sc, ifmr);
   2593 }
   2594 
   2595 /*
   2596  * tlp_mediachange:	[ifmedia interface function]
   2597  *
   2598  *	Update the current media.
   2599  */
   2600 int
   2601 tlp_mediachange(ifp)
   2602 	struct ifnet *ifp;
   2603 {
   2604 	struct tulip_softc *sc = ifp->if_softc;
   2605 
   2606 	return ((*sc->sc_mediasw->tmsw_set)(sc));
   2607 }
   2608 
   2609 /*****************************************************************************
   2610  * Support functions for MII-attached media.
   2611  *****************************************************************************/
   2612 
   2613 /*
   2614  * tlp_mii_tick:
   2615  *
   2616  *	One second timer, used to tick the MII.
   2617  */
   2618 void
   2619 tlp_mii_tick(arg)
   2620 	void *arg;
   2621 {
   2622 	struct tulip_softc *sc = arg;
   2623 	int s;
   2624 
   2625 	s = splnet();
   2626 	mii_tick(&sc->sc_mii);
   2627 	splx(s);
   2628 
   2629 	timeout(sc->sc_tick, sc, hz);
   2630 }
   2631 
   2632 /*
   2633  * tlp_mii_statchg:	[mii interface function]
   2634  *
   2635  *	Callback from PHY when media changes.
   2636  */
   2637 void
   2638 tlp_mii_statchg(self)
   2639 	struct device *self;
   2640 {
   2641 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2642 
   2643 	/* Idle the transmit and receive processes. */
   2644 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2645 
   2646 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_HBD);
   2647 
   2648 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
   2649 		sc->sc_opmode |= OPMODE_TTM;
   2650 	else
   2651 		sc->sc_opmode |= OPMODE_HBD;
   2652 
   2653 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   2654 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
   2655 
   2656 	/*
   2657 	 * Write new OPMODE bits.  This also restarts the transmit
   2658 	 * and receive processes.
   2659 	 */
   2660 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2661 
   2662 	/* XXX Update ifp->if_baudrate */
   2663 }
   2664 
   2665 /*
   2666  * tlp_winb_mii_statchg: [mii interface function]
   2667  *
   2668  *	Callback from PHY when media changes.  This version is
   2669  *	for the Winbond 89C840F, which has different OPMODE bits.
   2670  */
   2671 void
   2672 tlp_winb_mii_statchg(self)
   2673 	struct device *self;
   2674 {
   2675 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2676 
   2677 	/* Idle the transmit and receive processes. */
   2678 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2679 
   2680 	sc->sc_opmode &= ~(OPMODE_WINB_FES|OPMODE_FD);
   2681 
   2682 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX)
   2683 		sc->sc_opmode |= OPMODE_WINB_FES;
   2684 
   2685 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   2686 		sc->sc_opmode |= OPMODE_FD;
   2687 
   2688 	/*
   2689 	 * Write new OPMODE bits.  This also restarts the transmit
   2690 	 * and receive processes.
   2691 	 */
   2692 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2693 
   2694 	/* XXX Update ifp->if_baudrate */
   2695 }
   2696 
   2697 /*
   2698  * tlp_mii_getmedia:
   2699  *
   2700  *	Callback from ifmedia to request current media status.
   2701  */
   2702 void
   2703 tlp_mii_getmedia(sc, ifmr)
   2704 	struct tulip_softc *sc;
   2705 	struct ifmediareq *ifmr;
   2706 {
   2707 
   2708 	mii_pollstat(&sc->sc_mii);
   2709 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   2710 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   2711 }
   2712 
   2713 /*
   2714  * tlp_mii_setmedia:
   2715  *
   2716  *	Callback from ifmedia to request new media setting.
   2717  */
   2718 int
   2719 tlp_mii_setmedia(sc)
   2720 	struct tulip_softc *sc;
   2721 {
   2722 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2723 
   2724 	if (ifp->if_flags & IFF_UP)
   2725 		mii_mediachg(&sc->sc_mii);
   2726 	return (0);
   2727 }
   2728 
   2729 /*
   2730  * tlp_bitbang_mii_readreg:
   2731  *
   2732  *	Read a PHY register via bit-bang'ing the MII.
   2733  */
   2734 int
   2735 tlp_bitbang_mii_readreg(self, phy, reg)
   2736 	struct device *self;
   2737 	int phy, reg;
   2738 {
   2739 	struct tulip_softc *sc = (void *) self;
   2740 
   2741 	return (mii_bitbang_readreg(self, sc->sc_bitbang_ops, phy, reg));
   2742 }
   2743 
   2744 /*
   2745  * tlp_bitbang_mii_writereg:
   2746  *
   2747  *	Write a PHY register via bit-bang'ing the MII.
   2748  */
   2749 void
   2750 tlp_bitbang_mii_writereg(self, phy, reg, val)
   2751 	struct device *self;
   2752 	int phy, reg, val;
   2753 {
   2754 	struct tulip_softc *sc = (void *) self;
   2755 
   2756 	mii_bitbang_writereg(self, sc->sc_bitbang_ops, phy, reg, val);
   2757 }
   2758 
   2759 /*
   2760  * tlp_sio_mii_bitbang_read:
   2761  *
   2762  *	Read the MII serial port for the MII bit-bang module.
   2763  */
   2764 u_int32_t
   2765 tlp_sio_mii_bitbang_read(self)
   2766 	struct device *self;
   2767 {
   2768 	struct tulip_softc *sc = (void *) self;
   2769 
   2770 	return (TULIP_READ(sc, CSR_MIIROM));
   2771 }
   2772 
   2773 /*
   2774  * tlp_sio_mii_bitbang_write:
   2775  *
   2776  *	Write the MII serial port for the MII bit-bang module.
   2777  */
   2778 void
   2779 tlp_sio_mii_bitbang_write(self, val)
   2780 	struct device *self;
   2781 	u_int32_t val;
   2782 {
   2783 	struct tulip_softc *sc = (void *) self;
   2784 
   2785 	TULIP_WRITE(sc, CSR_MIIROM, val);
   2786 }
   2787 
   2788 /*
   2789  * tlp_pnic_mii_readreg:
   2790  *
   2791  *	Read a PHY register on the Lite-On PNIC.
   2792  */
   2793 int
   2794 tlp_pnic_mii_readreg(self, phy, reg)
   2795 	struct device *self;
   2796 	int phy, reg;
   2797 {
   2798 	struct tulip_softc *sc = (void *) self;
   2799 	u_int32_t val;
   2800 	int i;
   2801 
   2802 	TULIP_WRITE(sc, CSR_PNIC_MII,
   2803 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   2804 	    PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
   2805 	    (reg << PNIC_MII_REGSHIFT));
   2806 
   2807 	for (i = 0; i < 1000; i++) {
   2808 		delay(10);
   2809 		val = TULIP_READ(sc, CSR_PNIC_MII);
   2810 		if ((val & PNIC_MII_BUSY) == 0) {
   2811 			if ((val & PNIC_MII_DATA) == PNIC_MII_DATA)
   2812 				return (0);
   2813 			else
   2814 				return (val & PNIC_MII_DATA);
   2815 		}
   2816 	}
   2817 	printf("%s: MII read timed out\n", sc->sc_dev.dv_xname);
   2818 	return (0);
   2819 }
   2820 
   2821 /*
   2822  * tlp_pnic_mii_writereg:
   2823  *
   2824  *	Write a PHY register on the Lite-On PNIC.
   2825  */
   2826 void
   2827 tlp_pnic_mii_writereg(self, phy, reg, val)
   2828 	struct device *self;
   2829 	int phy, reg, val;
   2830 {
   2831 	struct tulip_softc *sc = (void *) self;
   2832 	int i;
   2833 
   2834 	TULIP_WRITE(sc, CSR_PNIC_MII,
   2835 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   2836 	    PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
   2837 	    (reg << PNIC_MII_REGSHIFT) | val);
   2838 
   2839 	for (i = 0; i < 1000; i++) {
   2840 		delay(10);
   2841 		if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
   2842 			return;
   2843 	}
   2844 	printf("%s: MII write timed out\n", sc->sc_dev.dv_xname);
   2845 }
   2846 
   2847 const bus_addr_t tlp_al981_phy_regmap[] = {
   2848 	CSR_ADM_BMCR,
   2849 	CSR_ADM_BMSR,
   2850 	CSR_ADM_PHYIDR1,
   2851 	CSR_ADM_PHYIDR2,
   2852 	CSR_ADM_ANAR,
   2853 	CSR_ADM_ANLPAR,
   2854 	CSR_ADM_ANER,
   2855 
   2856 	CSR_ADM_XMC,
   2857 	CSR_ADM_XCIIS,
   2858 	CSR_ADM_XIE,
   2859 	CSR_ADM_100CTR,
   2860 };
   2861 const int tlp_al981_phy_regmap_size = sizeof(tlp_al981_phy_regmap) /
   2862     sizeof(tlp_al981_phy_regmap[0]);
   2863 
   2864 /*
   2865  * tlp_al981_mii_readreg:
   2866  *
   2867  *	Read a PHY register on the ADMtek AL981.
   2868  */
   2869 int
   2870 tlp_al981_mii_readreg(self, phy, reg)
   2871 	struct device *self;
   2872 	int phy, reg;
   2873 {
   2874 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2875 
   2876 	/* AL981 only has an internal PHY. */
   2877 	if (phy != 0)
   2878 		return (0);
   2879 
   2880 	if (reg >= tlp_al981_phy_regmap_size)
   2881 		return (0);
   2882 
   2883 	return (bus_space_read_4(sc->sc_st, sc->sc_sh,
   2884 	    tlp_al981_phy_regmap[reg]) & 0xffff);
   2885 }
   2886 
   2887 /*
   2888  * tlp_al981_mii_writereg:
   2889  *
   2890  *	Write a PHY register on the ADMtek AL981.
   2891  */
   2892 void
   2893 tlp_al981_mii_writereg(self, phy, reg, val)
   2894 	struct device *self;
   2895 	int phy, reg, val;
   2896 {
   2897 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2898 
   2899 	/* AL981 only has an internal PHY. */
   2900 	if (phy != 0)
   2901 		return;
   2902 
   2903 	if (reg >= tlp_al981_phy_regmap_size)
   2904 		return;
   2905 
   2906 	bus_space_write_4(sc->sc_st, sc->sc_sh,
   2907 	    tlp_al981_phy_regmap[reg], val);
   2908 }
   2909 
   2910 /*****************************************************************************
   2911  * Chip-specific pre-init and reset functions.
   2912  *****************************************************************************/
   2913 
   2914 /*
   2915  * tlp_2114x_preinit:
   2916  *
   2917  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
   2918  */
   2919 void
   2920 tlp_2114x_preinit(sc)
   2921 	struct tulip_softc *sc;
   2922 {
   2923 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   2924 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   2925 
   2926 	/*
   2927 	 * Whether or not we're in MII or SIA/SYM mode, the media info
   2928 	 * contains the appropriate OPMODE bits.
   2929 	 *
   2930 	 * Note that if we have no media info, we are are doing
   2931 	 * non-MII `auto'.
   2932 	 *
   2933 	 * Also, we always set the Must-Be-One bit.
   2934 	 */
   2935 	if (tm == NULL) {
   2936 #ifdef DIAGNOSTIC
   2937 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   2938 			panic("tlp_2114x_preinit: not IFM_AUTO");
   2939 		if (sc->sc_nway_active == NULL)
   2940 			panic("tlp_2114x_preinit: nway_active NULL");
   2941 #endif
   2942 		tm = sc->sc_nway_active->ifm_aux;
   2943 	}
   2944 	sc->sc_opmode |= OPMODE_MBO | tm->tm_opmode;
   2945 
   2946 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2947 }
   2948 
   2949 /*
   2950  * tlp_2114x_mii_preinit:
   2951  *
   2952  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
   2953  *	This version is used by boards which only have MII and don't have
   2954  *	an ISV SROM.
   2955  */
   2956 void
   2957 tlp_2114x_mii_preinit(sc)
   2958 	struct tulip_softc *sc;
   2959 {
   2960 
   2961 	/*
   2962 	 * Always set the Must-Be-One bit, and Port Select (to select MII).
   2963 	 * We'll never be called during a media change.
   2964 	 */
   2965 	sc->sc_opmode |= OPMODE_MBO|OPMODE_PS;
   2966 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2967 }
   2968 
   2969 /*
   2970  * tlp_pnic_preinit:
   2971  *
   2972  *	Pre-init function for the Lite-On 82c168 and 82c169.
   2973  */
   2974 void
   2975 tlp_pnic_preinit(sc)
   2976 	struct tulip_softc *sc;
   2977 {
   2978 
   2979 	if (sc->sc_flags & TULIPF_HAS_MII) {
   2980 		/*
   2981 		 * MII case: just set the port-select bit; we will never
   2982 		 * be called during a media change.
   2983 		 */
   2984 		sc->sc_opmode |= OPMODE_PS;
   2985 	} else {
   2986 		/*
   2987 		 * ENDEC/PCS/Nway mode; enable the Tx backoff counter.
   2988 		 */
   2989 		sc->sc_opmode |= OPMODE_PNIC_TBEN;
   2990 	}
   2991 }
   2992 
   2993 /*
   2994  * tlp_21140_reset:
   2995  *
   2996  *	Issue a reset sequence on the 21140 via the GPIO facility.
   2997  */
   2998 void
   2999 tlp_21140_reset(sc)
   3000 	struct tulip_softc *sc;
   3001 {
   3002 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3003 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3004 	int i;
   3005 
   3006 	/* First, set the direction on the GPIO pins. */
   3007 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   3008 
   3009 	/* Now, issue the reset sequence. */
   3010 	for (i = 0; i < tm->tm_reset_length; i++) {
   3011 		delay(10);
   3012 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_reset_offset + i]);
   3013 	}
   3014 
   3015 	/* Now, issue the selection sequence. */
   3016 	for (i = 0; i < tm->tm_gp_length; i++) {
   3017 		delay(10);
   3018 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_gp_offset + i]);
   3019 	}
   3020 
   3021 	/* If there were no sequences, just lower the pins. */
   3022 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0)
   3023 		TULIP_WRITE(sc, CSR_GPP, 0);
   3024 }
   3025 
   3026 /*
   3027  * tlp_21142_reset:
   3028  *
   3029  *	Issue a reset sequence on the 21142 via the GPIO facility.
   3030  */
   3031 void
   3032 tlp_21142_reset(sc)
   3033 	struct tulip_softc *sc;
   3034 {
   3035 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3036 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3037 	const u_int8_t *ncp;
   3038 	int i;
   3039 
   3040 	ncp = &sc->sc_srom[tm->tm_reset_offset];
   3041 	for (i = 0; i < tm->tm_reset_length; i++, ncp += 2) {
   3042 		delay(10);
   3043 		TULIP_WRITE(sc, CSR_SIAGEN,
   3044 		    TULIP_ROM_GETW(ncp, 0) << 16);
   3045 	}
   3046 
   3047 	ncp = &sc->sc_srom[tm->tm_gp_offset];
   3048 	for (i = 0; i < tm->tm_gp_length; i++, ncp += 2) {
   3049 		delay(10);
   3050 		TULIP_WRITE(sc, CSR_SIAGEN,
   3051 		    TULIP_ROM_GETW(ncp, 0) << 16);
   3052 	}
   3053 
   3054 	/* If there were no sequences, just lower the pins. */
   3055 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   3056 		delay(10);
   3057 		TULIP_WRITE(sc, CSR_SIAGEN, 0);
   3058 	}
   3059 }
   3060 
   3061 /*
   3062  * tlp_pmac_reset:
   3063  *
   3064  *	Reset routine for Macronix chips.
   3065  */
   3066 void
   3067 tlp_pmac_reset(sc)
   3068 	struct tulip_softc *sc;
   3069 {
   3070 
   3071 	switch (sc->sc_chip) {
   3072 	case TULIP_CHIP_82C115:
   3073 	case TULIP_CHIP_MX98715:
   3074 	case TULIP_CHIP_MX98715A:
   3075 	case TULIP_CHIP_MX98725:
   3076 		/*
   3077 		 * Set the LED operating mode.  This information is located
   3078 		 * in the EEPROM at byte offset 0x77, per the MX98715A and
   3079 		 * MX98725 application notes.
   3080 		 */
   3081 		TULIP_WRITE(sc, CSR_MIIROM, sc->sc_srom[0x77] << 24);
   3082 		break;
   3083 
   3084 	default:
   3085 		/* Nothing. */
   3086 	}
   3087 }
   3088 
   3089 /*****************************************************************************
   3090  * Chip/board-specific media switches.  The ones here are ones that
   3091  * are potentially common to multiple front-ends.
   3092  *****************************************************************************/
   3093 
   3094 /*
   3095  * This table is a common place for all sorts of media information,
   3096  * keyed off of the SROM media code for that media.
   3097  *
   3098  * Note that we explicitly configure the 21142/21143 to always advertise
   3099  * NWay capabilities when using the UTP port.
   3100  * XXX Actually, we don't yet.
   3101  */
   3102 const struct tulip_srom_to_ifmedia tulip_srom_to_ifmedia_table[] = {
   3103 	{ TULIP_ROM_MB_MEDIA_TP,	IFM_10_T,	0,
   3104 	  "10baseT",
   3105 	  0,
   3106 	  { SIACONN_21040_10BASET,
   3107 	    SIATXRX_21040_10BASET,
   3108 	    SIAGEN_21040_10BASET },
   3109 
   3110 	  { SIACONN_21041_10BASET,
   3111 	    SIATXRX_21041_10BASET,
   3112 	    SIAGEN_21041_10BASET },
   3113 
   3114 	  { SIACONN_21142_10BASET,
   3115 	    SIATXRX_21142_10BASET,
   3116 	    SIAGEN_21142_10BASET } },
   3117 
   3118 	{ TULIP_ROM_MB_MEDIA_BNC,	IFM_10_2,	0,
   3119 	  "10base2",
   3120 	  0,
   3121 	  { 0,
   3122 	    0,
   3123 	    0 },
   3124 
   3125 	  { SIACONN_21041_BNC,
   3126 	    SIATXRX_21041_BNC,
   3127 	    SIAGEN_21041_BNC },
   3128 
   3129 	  { SIACONN_21142_BNC,
   3130 	    SIATXRX_21142_BNC,
   3131 	    SIAGEN_21142_BNC } },
   3132 
   3133 	{ TULIP_ROM_MB_MEDIA_AUI,	IFM_10_5,	0,
   3134 	  "10base5",
   3135 	  0,
   3136 	  { SIACONN_21040_AUI,
   3137 	    SIATXRX_21040_AUI,
   3138 	    SIAGEN_21040_AUI },
   3139 
   3140 	  { SIACONN_21041_AUI,
   3141 	    SIATXRX_21041_AUI,
   3142 	    SIAGEN_21041_AUI },
   3143 
   3144 	  { SIACONN_21142_AUI,
   3145 	    SIATXRX_21142_AUI,
   3146 	    SIAGEN_21142_AUI } },
   3147 
   3148 	{ TULIP_ROM_MB_MEDIA_100TX,	IFM_100_TX,	0,
   3149 	  "100baseTX",
   3150 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
   3151 	  { 0,
   3152 	    0,
   3153 	    0 },
   3154 
   3155 	  { 0,
   3156 	    0,
   3157 	    0 },
   3158 
   3159 	  { 0,
   3160 	    0,
   3161 	    SIAGEN_ABM } },
   3162 
   3163 	{ TULIP_ROM_MB_MEDIA_TP_FDX,	IFM_10_T,	IFM_FDX,
   3164 	  "10baseT-FDX",
   3165 	  OPMODE_FD|OPMODE_HBD,
   3166 	  { SIACONN_21040_10BASET_FDX,
   3167 	    SIATXRX_21040_10BASET_FDX,
   3168 	    SIAGEN_21040_10BASET_FDX },
   3169 
   3170 	  { SIACONN_21041_10BASET_FDX,
   3171 	    SIATXRX_21041_10BASET_FDX,
   3172 	    SIAGEN_21041_10BASET_FDX },
   3173 
   3174 	  { SIACONN_21142_10BASET_FDX,
   3175 	    SIATXRX_21142_10BASET_FDX,
   3176 	    SIAGEN_21142_10BASET_FDX } },
   3177 
   3178 	{ TULIP_ROM_MB_MEDIA_100TX_FDX,	IFM_100_TX,	IFM_FDX,
   3179 	  "100baseTX-FDX",
   3180 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_FD|OPMODE_HBD,
   3181 	  { 0,
   3182 	    0,
   3183 	    0 },
   3184 
   3185 	  { 0,
   3186 	    0,
   3187 	    0 },
   3188 
   3189 	  { 0,
   3190 	    0,
   3191 	    SIAGEN_ABM } },
   3192 
   3193 	{ TULIP_ROM_MB_MEDIA_100T4,	IFM_100_T4,	0,
   3194 	  "100baseT4",
   3195 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
   3196 	  { 0,
   3197 	    0,
   3198 	    0 },
   3199 
   3200 	  { 0,
   3201 	    0,
   3202 	    0 },
   3203 
   3204 	  { 0,
   3205 	    0,
   3206 	    SIAGEN_ABM } },
   3207 
   3208 	{ TULIP_ROM_MB_MEDIA_100FX,	IFM_100_FX,	0,
   3209 	  "100baseFX",
   3210 	  OPMODE_PS|OPMODE_PCS|OPMODE_HBD,
   3211 	  { 0,
   3212 	    0,
   3213 	    0 },
   3214 
   3215 	  { 0,
   3216 	    0,
   3217 	    0 },
   3218 
   3219 	  { 0,
   3220 	    0,
   3221 	    SIAGEN_ABM } },
   3222 
   3223 	{ TULIP_ROM_MB_MEDIA_100FX_FDX,	IFM_100_FX,	IFM_FDX,
   3224 	  "100baseFX-FDX",
   3225 	  OPMODE_PS|OPMODE_PCS|OPMODE_FD|OPMODE_HBD,
   3226 	  { 0,
   3227 	    0,
   3228 	    0 },
   3229 
   3230 	  { 0,
   3231 	    0,
   3232 	    0 },
   3233 
   3234 	  { 0,
   3235 	    0,
   3236 	    SIAGEN_ABM } },
   3237 
   3238 	{ 0,				0,		0,
   3239 	  NULL,
   3240 	  0,
   3241 	  { 0,
   3242 	    0,
   3243 	    0 },
   3244 
   3245 	  { 0,
   3246 	    0,
   3247 	    0 },
   3248 
   3249 	  { 0,
   3250 	    0,
   3251 	    0 } },
   3252 };
   3253 
   3254 const struct tulip_srom_to_ifmedia *tlp_srom_to_ifmedia __P((u_int8_t));
   3255 void	tlp_srom_media_info __P((struct tulip_softc *,
   3256 	    const struct tulip_srom_to_ifmedia *, struct tulip_21x4x_media *));
   3257 void	tlp_add_srom_media __P((struct tulip_softc *, int,
   3258 	    void (*)(struct tulip_softc *, struct ifmediareq *),
   3259 	    int (*)(struct tulip_softc *), const u_int8_t *, int));
   3260 void	tlp_print_media __P((struct tulip_softc *));
   3261 void	tlp_nway_activate __P((struct tulip_softc *, int));
   3262 void	tlp_get_minst __P((struct tulip_softc *));
   3263 
   3264 const struct tulip_srom_to_ifmedia *
   3265 tlp_srom_to_ifmedia(sm)
   3266 	u_int8_t sm;
   3267 {
   3268 	const struct tulip_srom_to_ifmedia *tsti;
   3269 
   3270 	for (tsti = tulip_srom_to_ifmedia_table;
   3271 	     tsti->tsti_name != NULL; tsti++) {
   3272 		if (tsti->tsti_srom == sm)
   3273 			return (tsti);
   3274 	}
   3275 
   3276 	return (NULL);
   3277 }
   3278 
   3279 void
   3280 tlp_srom_media_info(sc, tsti, tm)
   3281 	struct tulip_softc *sc;
   3282 	const struct tulip_srom_to_ifmedia *tsti;
   3283 	struct tulip_21x4x_media *tm;
   3284 {
   3285 
   3286 	tm->tm_name = tsti->tsti_name;
   3287 	tm->tm_opmode = tsti->tsti_opmode;
   3288 
   3289 	switch (sc->sc_chip) {
   3290 	case TULIP_CHIP_DE425:
   3291 	case TULIP_CHIP_21040:
   3292 		tm->tm_sia = tsti->tsti_21040;	/* struct assignment */
   3293 		break;
   3294 
   3295 	case TULIP_CHIP_21041:
   3296 		tm->tm_sia = tsti->tsti_21041;	/* struct assignment */
   3297 		break;
   3298 
   3299 	case TULIP_CHIP_21142:
   3300 	case TULIP_CHIP_21143:
   3301 	case TULIP_CHIP_82C115:
   3302 	case TULIP_CHIP_MX98715:
   3303 	case TULIP_CHIP_MX98715A:
   3304 	case TULIP_CHIP_MX98725:
   3305 		tm->tm_sia = tsti->tsti_21142;	/* struct assignment */
   3306 		break;
   3307 
   3308 	default:
   3309 		/* Nothing. */
   3310 	}
   3311 }
   3312 
   3313 void
   3314 tlp_add_srom_media(sc, type, get, set, list, cnt)
   3315 	struct tulip_softc *sc;
   3316 	int type;
   3317 	void (*get) __P((struct tulip_softc *, struct ifmediareq *));
   3318 	int (*set) __P((struct tulip_softc *));
   3319 	const u_int8_t *list;
   3320 	int cnt;
   3321 {
   3322 	struct tulip_21x4x_media *tm;
   3323 	const struct tulip_srom_to_ifmedia *tsti;
   3324 	int i;
   3325 
   3326 	for (i = 0; i < cnt; i++) {
   3327 		tsti = tlp_srom_to_ifmedia(list[i]);
   3328 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   3329 		memset(tm, 0, sizeof(*tm));
   3330 		tlp_srom_media_info(sc, tsti, tm);
   3331 		tm->tm_type = type;
   3332 		tm->tm_get = get;
   3333 		tm->tm_set = set;
   3334 
   3335 		ifmedia_add(&sc->sc_mii.mii_media,
   3336 		    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   3337 		    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   3338 	}
   3339 }
   3340 
   3341 void
   3342 tlp_print_media(sc)
   3343 	struct tulip_softc *sc;
   3344 {
   3345 	struct ifmedia_entry *ife;
   3346 	struct tulip_21x4x_media *tm;
   3347 	const char *sep = "";
   3348 
   3349 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
   3350 
   3351 	printf("%s: ", sc->sc_dev.dv_xname);
   3352 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   3353 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
   3354 		tm = ife->ifm_aux;
   3355 		if (tm == NULL) {
   3356 #ifdef DIAGNOSTIC
   3357 			if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   3358 				panic("tlp_print_media");
   3359 #endif
   3360 			PRINT("auto");
   3361 		} else if (tm->tm_type != TULIP_ROM_MB_21140_MII &&
   3362 			   tm->tm_type != TULIP_ROM_MB_21142_MII) {
   3363 			PRINT(tm->tm_name);
   3364 		}
   3365 	}
   3366 	printf("\n");
   3367 
   3368 #undef PRINT
   3369 }
   3370 
   3371 void
   3372 tlp_nway_activate(sc, media)
   3373 	struct tulip_softc *sc;
   3374 	int media;
   3375 {
   3376 	struct ifmedia_entry *ife;
   3377 
   3378 	ife = ifmedia_match(&sc->sc_mii.mii_media, media, 0);
   3379 #ifdef DIAGNOSTIC
   3380 	if (ife == NULL)
   3381 		panic("tlp_nway_activate");
   3382 #endif
   3383 	sc->sc_nway_active = ife;
   3384 }
   3385 
   3386 void
   3387 tlp_get_minst(sc)
   3388 	struct tulip_softc *sc;
   3389 {
   3390 
   3391 	if ((sc->sc_media_seen &
   3392 	    ~((1 << TULIP_ROM_MB_21140_MII) |
   3393 	      (1 << TULIP_ROM_MB_21142_MII))) == 0) {
   3394 		/*
   3395 		 * We have not yet seen any SIA/SYM media (but are
   3396 		 * about to; that's why we're called!), so assign
   3397 		 * the current media instance to be the `internal media'
   3398 		 * instance, and advance it so any MII media gets a
   3399 		 * fresh one (used to selecting/isolating a PHY).
   3400 		 */
   3401 		sc->sc_tlp_minst = sc->sc_mii.mii_instance++;
   3402 	}
   3403 }
   3404 
   3405 /*
   3406  * SIA Utility functions.
   3407  */
   3408 void	tlp_sia_update_link __P((struct tulip_softc *));
   3409 void	tlp_sia_get __P((struct tulip_softc *, struct ifmediareq *));
   3410 int	tlp_sia_set __P((struct tulip_softc *));
   3411 void	tlp_sia_fixup __P((struct tulip_softc *));
   3412 
   3413 void
   3414 tlp_sia_update_link(sc)
   3415 	struct tulip_softc *sc;
   3416 {
   3417 	struct ifmedia_entry *ife;
   3418 	struct tulip_21x4x_media *tm;
   3419 	u_int32_t siastat;
   3420 
   3421 	ife = TULIP_CURRENT_MEDIA(sc);
   3422 	tm = ife->ifm_aux;
   3423 
   3424 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
   3425 
   3426 	siastat = TULIP_READ(sc, CSR_SIASTAT);
   3427 
   3428 	/*
   3429 	 * Note that when we do SIA link tests, we are assuming that
   3430 	 * the chip is really in the mode that the current media setting
   3431 	 * reflects.  If we're not, then the link tests will not be
   3432 	 * accurate!
   3433 	 */
   3434 	switch (IFM_SUBTYPE(ife->ifm_media)) {
   3435 	case IFM_10_T:
   3436 		sc->sc_flags |= TULIPF_LINK_VALID;
   3437 		if ((siastat & SIASTAT_LS10) == 0)
   3438 			sc->sc_flags |= TULIPF_LINK_UP;
   3439 		break;
   3440 
   3441 	case IFM_100_TX:
   3442 	case IFM_100_T4:
   3443 		sc->sc_flags |= TULIPF_LINK_VALID;
   3444 		if ((siastat & SIASTAT_LS100) == 0)
   3445 			sc->sc_flags |= TULIPF_LINK_UP;
   3446 		break;
   3447 	}
   3448 
   3449 	switch (sc->sc_chip) {
   3450 	case TULIP_CHIP_21142:
   3451 	case TULIP_CHIP_21143:
   3452 		/*
   3453 		 * On these chips, we can tell more information about
   3454 		 * AUI/BNC.  Note that the AUI/BNC selection is made
   3455 		 * in a different register; for our purpose, it's all
   3456 		 * AUI.
   3457 		 */
   3458 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   3459 		case IFM_10_2:
   3460 		case IFM_10_5:
   3461 			sc->sc_flags |= TULIPF_LINK_VALID;
   3462 			if (siastat & SIASTAT_ARA) {
   3463 				TULIP_WRITE(sc, CSR_SIASTAT, SIASTAT_ARA);
   3464 				sc->sc_flags |= TULIPF_LINK_UP;
   3465 			}
   3466 			break;
   3467 
   3468 		default:
   3469 			/*
   3470 			 * If we're SYM media and can detect the link
   3471 			 * via the GPIO facility, prefer that status
   3472 			 * over LS100.
   3473 			 */
   3474 			if (tm->tm_type == TULIP_ROM_MB_21143_SYM &&
   3475 			    tm->tm_actmask != 0) {
   3476 				sc->sc_flags = (sc->sc_flags &
   3477 				    ~TULIPF_LINK_UP) | TULIPF_LINK_VALID;
   3478 				if (TULIP_ISSET(sc, CSR_SIAGEN,
   3479 				    tm->tm_actmask) == tm->tm_actdata)
   3480 					sc->sc_flags |= TULIPF_LINK_UP;
   3481 			}
   3482 		}
   3483 		break;
   3484 
   3485 	default:
   3486 		/* Nothing. */
   3487 	}
   3488 }
   3489 
   3490 void
   3491 tlp_sia_get(sc, ifmr)
   3492 	struct tulip_softc *sc;
   3493 	struct ifmediareq *ifmr;
   3494 {
   3495 	struct ifmedia_entry *ife;
   3496 
   3497 	ifmr->ifm_status = 0;
   3498 
   3499 	tlp_sia_update_link(sc);
   3500 
   3501 	ife = TULIP_CURRENT_MEDIA(sc);
   3502 
   3503 	if (sc->sc_flags & TULIPF_LINK_VALID)
   3504 		ifmr->ifm_status |= IFM_AVALID;
   3505 	if (sc->sc_flags & TULIPF_LINK_UP)
   3506 		ifmr->ifm_status |= IFM_ACTIVE;
   3507 	ifmr->ifm_active = ife->ifm_media;
   3508 }
   3509 
   3510 void
   3511 tlp_sia_fixup(sc)
   3512 	struct tulip_softc *sc;
   3513 {
   3514 	struct ifmedia_entry *ife;
   3515 	struct tulip_21x4x_media *tm;
   3516 	u_int32_t siaconn, siatxrx, siagen;
   3517 
   3518 	switch (sc->sc_chip) {
   3519 	case TULIP_CHIP_82C115:
   3520 	case TULIP_CHIP_MX98713A:
   3521 	case TULIP_CHIP_MX98715:
   3522 	case TULIP_CHIP_MX98715A:
   3523 	case TULIP_CHIP_MX98725:
   3524 		siaconn = PMAC_SIACONN_MASK;
   3525 		siatxrx = PMAC_SIATXRX_MASK;
   3526 		siagen  = PMAC_SIAGEN_MASK;
   3527 		break;
   3528 
   3529 	default:
   3530 		/* No fixups required on any other chips. */
   3531 		return;
   3532 	}
   3533 
   3534 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   3535 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
   3536 		tm = ife->ifm_aux;
   3537 		if (tm == NULL)
   3538 			continue;
   3539 
   3540 		tm->tm_siaconn &= siaconn;
   3541 		tm->tm_siatxrx &= siatxrx;
   3542 		tm->tm_siagen  &= siagen;
   3543 	}
   3544 }
   3545 
   3546 int
   3547 tlp_sia_set(sc)
   3548 	struct tulip_softc *sc;
   3549 {
   3550 	struct ifmedia_entry *ife;
   3551 	struct tulip_21x4x_media *tm;
   3552 
   3553 	ife = TULIP_CURRENT_MEDIA(sc);
   3554 	tm = ife->ifm_aux;
   3555 
   3556 	/*
   3557 	 * XXX This appears to be necessary on a bunch of the clone chips.
   3558 	 */
   3559 	delay(20000);
   3560 
   3561 	/*
   3562 	 * Idle the chip.
   3563 	 */
   3564 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   3565 
   3566 	/*
   3567 	 * Program the SIA.  It's important to write in this order,
   3568 	 * resetting the SIA first.
   3569 	 */
   3570 	TULIP_WRITE(sc, CSR_SIACONN, 0);		/* SRL bit clear */
   3571 	delay(1000);
   3572 
   3573 	TULIP_WRITE(sc, CSR_SIATXRX, tm->tm_siatxrx);
   3574 
   3575 	switch (sc->sc_chip) {
   3576 	case TULIP_CHIP_21142:
   3577 	case TULIP_CHIP_21143:
   3578 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpctl);
   3579 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpdata);
   3580 		break;
   3581 	default:
   3582 		TULIP_WRITE(sc, CSR_SIAGEN,  tm->tm_siagen);
   3583 	}
   3584 
   3585 	TULIP_WRITE(sc, CSR_SIACONN, tm->tm_siaconn);
   3586 
   3587 	/*
   3588 	 * Set the OPMODE bits for this media and write OPMODE.
   3589 	 * This will resume the transmit and receive processes.
   3590 	 */
   3591 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
   3592 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3593 
   3594 	return (0);
   3595 }
   3596 
   3597 /*
   3598  * 21140 GPIO utility functions.
   3599  */
   3600 void	tlp_21140_gpio_update_link __P((struct tulip_softc *));
   3601 void	tlp_21140_gpio_get __P((struct tulip_softc *sc,
   3602 	    struct ifmediareq *ifmr));
   3603 int	tlp_21140_gpio_set __P((struct tulip_softc *sc));
   3604 
   3605 void
   3606 tlp_21140_gpio_update_link(sc)
   3607 	struct tulip_softc *sc;
   3608 {
   3609 	struct ifmedia_entry *ife;
   3610 	struct tulip_21x4x_media *tm;
   3611 
   3612 	ife = TULIP_CURRENT_MEDIA(sc);
   3613 	tm = ife->ifm_aux;
   3614 
   3615 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
   3616 
   3617 	if (tm->tm_actmask != 0) {
   3618 		sc->sc_flags |= TULIPF_LINK_VALID;
   3619 		if (TULIP_ISSET(sc, CSR_GPP, tm->tm_actmask) ==
   3620 		    tm->tm_actdata)
   3621 			sc->sc_flags |= TULIPF_LINK_UP;
   3622 	}
   3623 }
   3624 
   3625 void
   3626 tlp_21140_gpio_get(sc, ifmr)
   3627 	struct tulip_softc *sc;
   3628 	struct ifmediareq *ifmr;
   3629 {
   3630 	struct ifmedia_entry *ife;
   3631 
   3632 	ifmr->ifm_status = 0;
   3633 
   3634 	tlp_21140_gpio_update_link(sc);
   3635 
   3636 	ife = TULIP_CURRENT_MEDIA(sc);
   3637 
   3638 	if (sc->sc_flags & TULIPF_LINK_VALID)
   3639 		ifmr->ifm_status |= IFM_AVALID;
   3640 	if (sc->sc_flags & TULIPF_LINK_UP)
   3641 		ifmr->ifm_status |= IFM_ACTIVE;
   3642 	ifmr->ifm_active = ife->ifm_media;
   3643 }
   3644 
   3645 int
   3646 tlp_21140_gpio_set(sc)
   3647 	struct tulip_softc *sc;
   3648 {
   3649 	struct ifmedia_entry *ife;
   3650 	struct tulip_21x4x_media *tm;
   3651 
   3652 	ife = TULIP_CURRENT_MEDIA(sc);
   3653 	tm = ife->ifm_aux;
   3654 
   3655 	/*
   3656 	 * Idle the chip.
   3657 	 */
   3658 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   3659 
   3660 	/*
   3661 	 * Set the GPIO pins for this media, to flip any
   3662 	 * relays, etc.
   3663 	 */
   3664 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   3665 	delay(10);
   3666 	TULIP_WRITE(sc, CSR_GPP, tm->tm_gpdata);
   3667 
   3668 	/*
   3669 	 * Set the OPMODE bits for this media and write OPMODE.
   3670 	 * This will resume the transmit and receive processes.
   3671 	 */
   3672 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
   3673 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3674 
   3675 	return (0);
   3676 }
   3677 
   3678 /*
   3679  * 21040 and 21041 media switches.
   3680  */
   3681 void	tlp_21040_tmsw_init __P((struct tulip_softc *));
   3682 void	tlp_21040_tp_tmsw_init __P((struct tulip_softc *));
   3683 void	tlp_21040_auibnc_tmsw_init __P((struct tulip_softc *));
   3684 void	tlp_21041_tmsw_init __P((struct tulip_softc *));
   3685 
   3686 const struct tulip_mediasw tlp_21040_mediasw = {
   3687 	tlp_21040_tmsw_init, tlp_sia_get, tlp_sia_set
   3688 };
   3689 
   3690 const struct tulip_mediasw tlp_21040_tp_mediasw = {
   3691 	tlp_21040_tp_tmsw_init, tlp_sia_get, tlp_sia_set
   3692 };
   3693 
   3694 const struct tulip_mediasw tlp_21040_auibnc_mediasw = {
   3695 	tlp_21040_auibnc_tmsw_init, tlp_sia_get, tlp_sia_set
   3696 };
   3697 
   3698 const struct tulip_mediasw tlp_21041_mediasw = {
   3699 	tlp_21041_tmsw_init, tlp_sia_get, tlp_sia_set
   3700 };
   3701 
   3702 
   3703 void
   3704 tlp_21040_tmsw_init(sc)
   3705 	struct tulip_softc *sc;
   3706 {
   3707 	static const u_int8_t media[] = {
   3708 		TULIP_ROM_MB_MEDIA_TP,
   3709 		TULIP_ROM_MB_MEDIA_TP_FDX,
   3710 		TULIP_ROM_MB_MEDIA_AUI,
   3711 	};
   3712 	struct tulip_21x4x_media *tm;
   3713 
   3714 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   3715 	    tlp_mediastatus);
   3716 
   3717 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 3);
   3718 
   3719 	/*
   3720 	 * No SROM type for External SIA.
   3721 	 */
   3722 	tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   3723 	memset(tm, 0, sizeof(*tm));
   3724 	tm->tm_name = "manual";
   3725 	tm->tm_opmode = 0;
   3726 	tm->tm_siaconn = SIACONN_21040_EXTSIA;
   3727 	tm->tm_siatxrx = SIATXRX_21040_EXTSIA;
   3728 	tm->tm_siagen  = SIAGEN_21040_EXTSIA;
   3729 	ifmedia_add(&sc->sc_mii.mii_media,
   3730 	    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, sc->sc_tlp_minst), 0, tm);
   3731 
   3732 	/*
   3733 	 * XXX Autosense not yet supported.
   3734 	 */
   3735 
   3736 	/* XXX This should be auto-sense. */
   3737 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   3738 
   3739 	tlp_print_media(sc);
   3740 }
   3741 
   3742 void
   3743 tlp_21040_tp_tmsw_init(sc)
   3744 	struct tulip_softc *sc;
   3745 {
   3746 	static const u_int8_t media[] = {
   3747 		TULIP_ROM_MB_MEDIA_TP,
   3748 		TULIP_ROM_MB_MEDIA_TP_FDX,
   3749 	};
   3750 
   3751 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   3752 	    tlp_mediastatus);
   3753 
   3754 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 2);
   3755 
   3756 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   3757 
   3758 	tlp_print_media(sc);
   3759 }
   3760 
   3761 void
   3762 tlp_21040_auibnc_tmsw_init(sc)
   3763 	struct tulip_softc *sc;
   3764 {
   3765 	static const u_int8_t media[] = {
   3766 		TULIP_ROM_MB_MEDIA_AUI,
   3767 	};
   3768 
   3769 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   3770 	    tlp_mediastatus);
   3771 
   3772 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 1);
   3773 
   3774 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5);
   3775 
   3776 	tlp_print_media(sc);
   3777 }
   3778 
   3779 void
   3780 tlp_21041_tmsw_init(sc)
   3781 	struct tulip_softc *sc;
   3782 {
   3783 	static const u_int8_t media[] = {
   3784 		TULIP_ROM_MB_MEDIA_TP,
   3785 		TULIP_ROM_MB_MEDIA_TP_FDX,
   3786 		TULIP_ROM_MB_MEDIA_BNC,
   3787 		TULIP_ROM_MB_MEDIA_AUI,
   3788 	};
   3789 	int i, defmedia, devcnt, leaf_offset, mb_offset, m_cnt;
   3790 	const struct tulip_srom_to_ifmedia *tsti;
   3791 	struct tulip_21x4x_media *tm;
   3792 	u_int16_t romdef;
   3793 	u_int8_t mb;
   3794 
   3795 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   3796 	    tlp_mediastatus);
   3797 
   3798 	if (tlp_isv_srom(sc->sc_srom) == 0) {
   3799  not_isv_srom:
   3800 		/*
   3801 		 * If we have a board without the standard 21041 SROM format,
   3802 		 * we just assume all media are present and try and pick a
   3803 		 * reasonable default.
   3804 		 */
   3805 		tlp_add_srom_media(sc, 0, NULL, NULL, media, 4);
   3806 
   3807 		/*
   3808 		 * XXX Autosense not yet supported.
   3809 		 */
   3810 
   3811 		/* XXX This should be auto-sense. */
   3812 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   3813 
   3814 		tlp_print_media(sc);
   3815 		return;
   3816 	}
   3817 
   3818 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   3819 	for (i = 0; i < devcnt; i++) {
   3820 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   3821 			break;
   3822 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   3823 		    sc->sc_devno)
   3824 			break;
   3825 	}
   3826 
   3827 	if (i == devcnt)
   3828 		goto not_isv_srom;
   3829 
   3830 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
   3831 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
   3832 	mb_offset = leaf_offset + TULIP_ROM_IL_MEDIAn_BLOCK_BASE;
   3833 	m_cnt = sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
   3834 
   3835 	for (; m_cnt != 0;
   3836 	     m_cnt--, mb_offset += TULIP_ROM_MB_SIZE(mb)) {
   3837 		mb = sc->sc_srom[mb_offset];
   3838 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   3839 		memset(tm, 0, sizeof(*tm));
   3840 		switch (mb & TULIP_ROM_MB_MEDIA_CODE) {
   3841 		case TULIP_ROM_MB_MEDIA_TP_FDX:
   3842 		case TULIP_ROM_MB_MEDIA_TP:
   3843 		case TULIP_ROM_MB_MEDIA_BNC:
   3844 		case TULIP_ROM_MB_MEDIA_AUI:
   3845 			tsti = tlp_srom_to_ifmedia(mb &
   3846 			    TULIP_ROM_MB_MEDIA_CODE);
   3847 
   3848 			tlp_srom_media_info(sc, tsti, tm);
   3849 
   3850 			/*
   3851 			 * Override our default SIA settings if the
   3852 			 * SROM contains its own.
   3853 			 */
   3854 			if (mb & TULIP_ROM_MB_EXT) {
   3855 				tm->tm_siaconn = TULIP_ROM_GETW(sc->sc_srom,
   3856 				    mb_offset + TULIP_ROM_MB_CSR13);
   3857 				tm->tm_siatxrx = TULIP_ROM_GETW(sc->sc_srom,
   3858 				    mb_offset + TULIP_ROM_MB_CSR14);
   3859 				tm->tm_siagen = TULIP_ROM_GETW(sc->sc_srom,
   3860 				    mb_offset + TULIP_ROM_MB_CSR15);
   3861 			}
   3862 
   3863 			ifmedia_add(&sc->sc_mii.mii_media,
   3864 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   3865 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   3866 			break;
   3867 
   3868 		default:
   3869 			printf("%s: unknown media code 0x%02x\n",
   3870 			    sc->sc_dev.dv_xname,
   3871 			    mb & TULIP_ROM_MB_MEDIA_CODE);
   3872 			free(tm, M_DEVBUF);
   3873 		}
   3874 	}
   3875 
   3876 	/*
   3877 	 * XXX Autosense not yet supported.
   3878 	 */
   3879 
   3880 	romdef = TULIP_ROM_GETW(sc->sc_srom, leaf_offset +
   3881 	    TULIP_ROM_IL_SELECT_CONN_TYPE);
   3882 	switch (romdef) {
   3883 	case SELECT_CONN_TYPE_TP:
   3884 	case SELECT_CONN_TYPE_TP_AUTONEG:
   3885 	case SELECT_CONN_TYPE_TP_NOLINKPASS:
   3886 		defmedia = IFM_ETHER|IFM_10_T;
   3887 		break;
   3888 
   3889 	case SELECT_CONN_TYPE_TP_FDX:
   3890 		defmedia = IFM_ETHER|IFM_10_T|IFM_FDX;
   3891 		break;
   3892 
   3893 	case SELECT_CONN_TYPE_BNC:
   3894 		defmedia = IFM_ETHER|IFM_10_2;
   3895 		break;
   3896 
   3897 	case SELECT_CONN_TYPE_AUI:
   3898 		defmedia = IFM_ETHER|IFM_10_5;
   3899 		break;
   3900 #if 0 /* XXX */
   3901 	case SELECT_CONN_TYPE_ASENSE:
   3902 	case SELECT_CONN_TYPE_ASENSE_AUTONEG:
   3903 		defmedia = IFM_ETHER|IFM_AUTO;
   3904 		break;
   3905 #endif
   3906 	default:
   3907 		defmedia = 0;
   3908 	}
   3909 
   3910 	if (defmedia == 0) {
   3911 		/*
   3912 		 * XXX We should default to auto-sense.
   3913 		 */
   3914 		defmedia = IFM_ETHER|IFM_10_T;
   3915 	}
   3916 
   3917 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
   3918 
   3919 	tlp_print_media(sc);
   3920 }
   3921 
   3922 /*
   3923  * DECchip 2114x ISV media switch.
   3924  */
   3925 void	tlp_2114x_isv_tmsw_init __P((struct tulip_softc *));
   3926 void	tlp_2114x_isv_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
   3927 int	tlp_2114x_isv_tmsw_set __P((struct tulip_softc *));
   3928 
   3929 const struct tulip_mediasw tlp_2114x_isv_mediasw = {
   3930 	tlp_2114x_isv_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
   3931 };
   3932 
   3933 void
   3934 tlp_2114x_isv_tmsw_init(sc)
   3935 	struct tulip_softc *sc;
   3936 {
   3937 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   3938 	struct ifmedia_entry *ife;
   3939 	struct mii_softc *phy;
   3940 	struct tulip_21x4x_media *tm;
   3941 	const struct tulip_srom_to_ifmedia *tsti;
   3942 	int i, devcnt, leaf_offset, m_cnt, type, length;
   3943 	int defmedia, miidef;
   3944 	u_int16_t word;
   3945 	u_int8_t *cp, *ncp;
   3946 
   3947 	defmedia = miidef = 0;
   3948 
   3949 	sc->sc_mii.mii_ifp = ifp;
   3950 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
   3951 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
   3952 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   3953 
   3954 	/*
   3955 	 * Ignore `instance'; we may get a mixture of SIA and MII
   3956 	 * media, and `instance' is used to isolate or select the
   3957 	 * PHY on the MII as appropriate.  Note that duplicate media
   3958 	 * are disallowed, so ignoring `instance' is safe.
   3959 	 */
   3960 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, tlp_mediachange,
   3961 	    tlp_mediastatus);
   3962 
   3963 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   3964 	for (i = 0; i < devcnt; i++) {
   3965 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   3966 			break;
   3967 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   3968 		    sc->sc_devno)
   3969 			break;
   3970 	}
   3971 
   3972 	if (i == devcnt) {
   3973 		printf("%s: unable to locate info leaf in SROM\n",
   3974 		    sc->sc_dev.dv_xname);
   3975 		return;
   3976 	}
   3977 
   3978 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
   3979 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
   3980 
   3981 	/* XXX SELECT CONN TYPE */
   3982 
   3983 	cp = &sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
   3984 
   3985 	/*
   3986 	 * On some chips, the first thing in the Info Leaf is the
   3987 	 * GPIO pin direction data.
   3988 	 */
   3989 	switch (sc->sc_chip) {
   3990 	case TULIP_CHIP_21140:
   3991 	case TULIP_CHIP_21140A:
   3992 	case TULIP_CHIP_MX98713:
   3993 	case TULIP_CHIP_AX88140:
   3994 	case TULIP_CHIP_AX88141:
   3995 		sc->sc_gp_dir = *cp++;
   3996 		break;
   3997 
   3998 	default:
   3999 		/* Nothing. */
   4000 	}
   4001 
   4002 	/* Get the media count. */
   4003 	m_cnt = *cp++;
   4004 
   4005 	for (; m_cnt != 0; cp = ncp, m_cnt--) {
   4006 		/*
   4007 		 * Determine the type and length of this media block.
   4008 		 */
   4009 		if ((*cp & 0x80) == 0) {
   4010 			length = 4;
   4011 			type = TULIP_ROM_MB_21140_GPR;
   4012 		} else {
   4013 			length = (*cp++ & 0x7f) - 1;
   4014 			type = *cp++ & 0x3f;
   4015 		}
   4016 
   4017 		/* Compute the start of the next block. */
   4018 		ncp = cp + length;
   4019 
   4020 		/* Now, parse the block. */
   4021 		switch (type) {
   4022 		case TULIP_ROM_MB_21140_GPR:
   4023 			tlp_get_minst(sc);
   4024 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_GPR;
   4025 
   4026 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4027 			memset(tm, 0, sizeof(*tm));
   4028 
   4029 			tm->tm_type = TULIP_ROM_MB_21140_GPR;
   4030 			tm->tm_get = tlp_21140_gpio_get;
   4031 			tm->tm_set = tlp_21140_gpio_set;
   4032 
   4033 			/* First is the media type code. */
   4034 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4035 			    TULIP_ROM_MB_MEDIA_CODE);
   4036 			if (tsti == NULL) {
   4037 				/* Invalid media code. */
   4038 				free(tm, M_DEVBUF);
   4039 				break;
   4040 			}
   4041 
   4042 			/* Get defaults. */
   4043 			tlp_srom_media_info(sc, tsti, tm);
   4044 
   4045 			/* Next is any GPIO info for this media. */
   4046 			tm->tm_gpdata = cp[1];
   4047 
   4048 			/*
   4049 			 * Next is a word containing OPMODE information
   4050 			 * and info on how to detect if this media is
   4051 			 * active.
   4052 			 */
   4053 			word = TULIP_ROM_GETW(cp, 2);
   4054 			tm->tm_opmode = TULIP_ROM_MB_OPMODE(word);
   4055 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
   4056 				tm->tm_actmask =
   4057 				    TULIP_ROM_MB_BITPOS(word);
   4058 				tm->tm_actdata =
   4059 				    (word & TULIP_ROM_MB_POLARITY) ?
   4060 				    0 : tm->tm_actmask;
   4061 			}
   4062 
   4063 			ifmedia_add(&sc->sc_mii.mii_media,
   4064 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4065 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4066 			break;
   4067 
   4068 		case TULIP_ROM_MB_21140_MII:
   4069 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_MII;
   4070 
   4071 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4072 			memset(tm, 0, sizeof(*tm));
   4073 
   4074 			tm->tm_type = TULIP_ROM_MB_21140_MII;
   4075 			tm->tm_get = tlp_mii_getmedia;
   4076 			tm->tm_set = tlp_mii_setmedia;
   4077 			tm->tm_opmode = OPMODE_PS;
   4078 
   4079 			if (sc->sc_reset == NULL)
   4080 				sc->sc_reset = tlp_21140_reset;
   4081 
   4082 			/* First is the PHY number. */
   4083 			tm->tm_phyno = *cp++;
   4084 
   4085 			/* Next is the MII select sequence length and offset. */
   4086 			tm->tm_gp_length = *cp++;
   4087 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
   4088 			cp += tm->tm_gp_length;
   4089 
   4090 			/* Next is the MII reset sequence length and offset. */
   4091 			tm->tm_reset_length = *cp++;
   4092 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
   4093 			cp += tm->tm_reset_length;
   4094 
   4095 			/*
   4096 			 * The following items are left in the media block
   4097 			 * that we don't particularly care about:
   4098 			 *
   4099 			 *	capabilities		W
   4100 			 *	advertisement		W
   4101 			 *	full duplex		W
   4102 			 *	tx threshold		W
   4103 			 *
   4104 			 * These appear to be bits in the PHY registers,
   4105 			 * which our MII code handles on its own.
   4106 			 */
   4107 
   4108 			/*
   4109 			 * Before we probe the MII bus, we need to reset
   4110 			 * it and issue the selection sequence.
   4111 			 */
   4112 
   4113 			/* Set the direction of the pins... */
   4114 			TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   4115 
   4116 			for (i = 0; i < tm->tm_reset_length; i++) {
   4117 				delay(10);
   4118 				TULIP_WRITE(sc, CSR_GPP,
   4119 				    sc->sc_srom[tm->tm_reset_offset + i]);
   4120 			}
   4121 
   4122 			for (i = 0; i < tm->tm_gp_length; i++) {
   4123 				delay(10);
   4124 				TULIP_WRITE(sc, CSR_GPP,
   4125 				    sc->sc_srom[tm->tm_gp_offset + i]);
   4126 			}
   4127 
   4128 			/* If there were no sequences, just lower the pins. */
   4129 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   4130 				delay(10);
   4131 				TULIP_WRITE(sc, CSR_GPP, 0);
   4132 			}
   4133 
   4134 			/*
   4135 			 * Now, probe the MII for the PHY.  Note, we know
   4136 			 * the location of the PHY on the bus, but we don't
   4137 			 * particularly care; the MII code just likes to
   4138 			 * search the whole thing anyhow.
   4139 			 */
   4140 			mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
   4141 			    MII_PHY_ANY, tm->tm_phyno);
   4142 
   4143 			/*
   4144 			 * Now, search for the PHY we hopefully just
   4145 			 * configured.  If it's not configured into the
   4146 			 * kernel, we lose.  The PHY's default media always
   4147 			 * takes priority.
   4148 			 */
   4149 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
   4150 			     phy != NULL;
   4151 			     phy = LIST_NEXT(phy, mii_list))
   4152 				if (phy->mii_offset == tm->tm_phyno)
   4153 					break;
   4154 			if (phy == NULL) {
   4155 				printf("%s: unable to configure MII\n",
   4156 				    sc->sc_dev.dv_xname);
   4157 				break;
   4158 			}
   4159 
   4160 			sc->sc_flags |= TULIPF_HAS_MII;
   4161 			sc->sc_tick = tlp_mii_tick;
   4162 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
   4163 			    phy->mii_inst);
   4164 
   4165 			/*
   4166 			 * Okay, now that we've found the PHY and the MII
   4167 			 * layer has added all of the media associated
   4168 			 * with that PHY, we need to traverse the media
   4169 			 * list, and add our `tm' to each entry's `aux'
   4170 			 * pointer.
   4171 			 *
   4172 			 * We do this by looking for media with our
   4173 			 * PHY's `instance'.
   4174 			 */
   4175 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   4176 			     ife != NULL;
   4177 			     ife = TAILQ_NEXT(ife, ifm_list)) {
   4178 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
   4179 					continue;
   4180 				ife->ifm_aux = tm;
   4181 			}
   4182 			break;
   4183 
   4184 		case TULIP_ROM_MB_21142_SIA:
   4185 			tlp_get_minst(sc);
   4186 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_SIA;
   4187 
   4188 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4189 			memset(tm, 0, sizeof(*tm));
   4190 
   4191 			tm->tm_type = TULIP_ROM_MB_21142_SIA;
   4192 			tm->tm_get = tlp_sia_get;
   4193 			tm->tm_set = tlp_sia_set;
   4194 
   4195 			/* First is the media type code. */
   4196 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4197 			    TULIP_ROM_MB_MEDIA_CODE);
   4198 			if (tsti == NULL) {
   4199 				/* Invalid media code. */
   4200 				free(tm, M_DEVBUF);
   4201 				break;
   4202 			}
   4203 
   4204 			/* Get defaults. */
   4205 			tlp_srom_media_info(sc, tsti, tm);
   4206 
   4207 			/*
   4208 			 * Override our default SIA settings if the
   4209 			 * SROM contains its own.
   4210 			 */
   4211 			if (cp[0] & 0x40) {
   4212 				tm->tm_siaconn = TULIP_ROM_GETW(cp, 1);
   4213 				tm->tm_siatxrx = TULIP_ROM_GETW(cp, 3);
   4214 				tm->tm_siagen  = TULIP_ROM_GETW(cp, 5);
   4215 				cp += 7;
   4216 			} else
   4217 				cp++;
   4218 
   4219 			/* Next is GPIO control/data. */
   4220 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 0);
   4221 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 2);
   4222 
   4223 			ifmedia_add(&sc->sc_mii.mii_media,
   4224 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4225 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4226 			break;
   4227 
   4228 		case TULIP_ROM_MB_21142_MII:
   4229 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_MII;
   4230 
   4231 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4232 			memset(tm, 0, sizeof(*tm));
   4233 
   4234 			tm->tm_type = TULIP_ROM_MB_21142_MII;
   4235 			tm->tm_get = tlp_mii_getmedia;
   4236 			tm->tm_set = tlp_mii_setmedia;
   4237 			tm->tm_opmode = OPMODE_PS;
   4238 
   4239 			if (sc->sc_reset == NULL)
   4240 				sc->sc_reset = tlp_21142_reset;
   4241 
   4242 			/* First is the PHY number. */
   4243 			tm->tm_phyno = *cp++;
   4244 
   4245 			/* Next is the MII select sequence length and offset. */
   4246 			tm->tm_gp_length = *cp++;
   4247 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
   4248 			cp += tm->tm_gp_length * 2;
   4249 
   4250 			/* Next is the MII reset sequence length and offset. */
   4251 			tm->tm_reset_length = *cp++;
   4252 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
   4253 			cp += tm->tm_reset_length * 2;
   4254 
   4255 			/*
   4256 			 * The following items are left in the media block
   4257 			 * that we don't particularly care about:
   4258 			 *
   4259 			 *	capabilities		W
   4260 			 *	advertisement		W
   4261 			 *	full duplex		W
   4262 			 *	tx threshold		W
   4263 			 *	MII interrupt		W
   4264 			 *
   4265 			 * These appear to be bits in the PHY registers,
   4266 			 * which our MII code handles on its own.
   4267 			 */
   4268 
   4269 			/*
   4270 			 * Before we probe the MII bus, we need to reset
   4271 			 * it and issue the selection sequence.
   4272 			 */
   4273 
   4274 			ncp = &sc->sc_srom[tm->tm_reset_offset];
   4275 			for (i = 0; i < tm->tm_reset_length; i++, ncp += 2) {
   4276 				delay(10);
   4277 				TULIP_WRITE(sc, CSR_SIAGEN,
   4278 				    TULIP_ROM_GETW(ncp, 0) << 16);
   4279 			}
   4280 
   4281 			ncp = &sc->sc_srom[tm->tm_gp_offset];
   4282 			for (i = 0; i < tm->tm_gp_length; i++, ncp += 2) {
   4283 				delay(10);
   4284 				TULIP_WRITE(sc, CSR_SIAGEN,
   4285 				    TULIP_ROM_GETW(ncp, 0) << 16);
   4286 			}
   4287 
   4288 			/* If there were no sequences, just lower the pins. */
   4289 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   4290 				delay(10);
   4291 				TULIP_WRITE(sc, CSR_SIAGEN, 0);
   4292 			}
   4293 
   4294 			/*
   4295 			 * Now, probe the MII for the PHY.  Note, we know
   4296 			 * the location of the PHY on the bus, but we don't
   4297 			 * particularly care; the MII code just likes to
   4298 			 * search the whole thing anyhow.
   4299 			 */
   4300 			mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
   4301 			    MII_PHY_ANY, tm->tm_phyno);
   4302 
   4303 			/*
   4304 			 * Now, search for the PHY we hopefully just
   4305 			 * configured.  If it's not configured into the
   4306 			 * kernel, we lose.  The PHY's default media always
   4307 			 * takes priority.
   4308 			 */
   4309 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
   4310 			     phy != NULL;
   4311 			     phy = LIST_NEXT(phy, mii_list))
   4312 				if (phy->mii_offset == tm->tm_phyno)
   4313 					break;
   4314 			if (phy == NULL) {
   4315 				printf("%s: unable to configure MII\n",
   4316 				    sc->sc_dev.dv_xname);
   4317 				break;
   4318 			}
   4319 
   4320 			sc->sc_flags |= TULIPF_HAS_MII;
   4321 			sc->sc_tick = tlp_mii_tick;
   4322 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
   4323 			    phy->mii_inst);
   4324 
   4325 			/*
   4326 			 * Okay, now that we've found the PHY and the MII
   4327 			 * layer has added all of the media associated
   4328 			 * with that PHY, we need to traverse the media
   4329 			 * list, and add our `tm' to each entry's `aux'
   4330 			 * pointer.
   4331 			 *
   4332 			 * We do this by looking for media with our
   4333 			 * PHY's `instance'.
   4334 			 */
   4335 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   4336 			     ife != NULL;
   4337 			     ife = TAILQ_NEXT(ife, ifm_list)) {
   4338 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
   4339 					continue;
   4340 				ife->ifm_aux = tm;
   4341 			}
   4342 			break;
   4343 
   4344 		case TULIP_ROM_MB_21143_SYM:
   4345 			tlp_get_minst(sc);
   4346 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21143_SYM;
   4347 
   4348 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4349 			memset(tm, 0, sizeof(*tm));
   4350 
   4351 			tm->tm_type = TULIP_ROM_MB_21143_SYM;
   4352 			tm->tm_get = tlp_sia_get;
   4353 			tm->tm_set = tlp_sia_set;
   4354 
   4355 			/* First is the media type code. */
   4356 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4357 			    TULIP_ROM_MB_MEDIA_CODE);
   4358 			if (tsti == NULL) {
   4359 				/* Invalid media code. */
   4360 				free(tm, M_DEVBUF);
   4361 				break;
   4362 			}
   4363 
   4364 			/* Get defaults. */
   4365 			tlp_srom_media_info(sc, tsti, tm);
   4366 
   4367 			/* Next is GPIO control/data. */
   4368 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 1);
   4369 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 3);
   4370 
   4371 			/*
   4372 			 * Next is a word containing OPMODE information
   4373 			 * and info on how to detect if this media is
   4374 			 * active.
   4375 			 */
   4376 			word = TULIP_ROM_GETW(cp, 5);
   4377 			tm->tm_opmode = TULIP_ROM_MB_OPMODE(word);
   4378 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
   4379 				tm->tm_actmask =
   4380 				    TULIP_ROM_MB_BITPOS(word);
   4381 				tm->tm_actdata =
   4382 				    (word & TULIP_ROM_MB_POLARITY) ?
   4383 				    0 : tm->tm_actmask;
   4384 			}
   4385 
   4386 			ifmedia_add(&sc->sc_mii.mii_media,
   4387 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4388 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4389 			break;
   4390 
   4391 		case TULIP_ROM_MB_21143_RESET:
   4392 			printf("%s: 21143 reset block\n", sc->sc_dev.dv_xname);
   4393 			break;
   4394 
   4395 		default:
   4396 			printf("%s: unknown ISV media block type 0x%02x\n",
   4397 			    sc->sc_dev.dv_xname, type);
   4398 		}
   4399 	}
   4400 
   4401 	/*
   4402 	 * Deal with the case where no media is configured.
   4403 	 */
   4404 	if (TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list) == NULL) {
   4405 		printf("%s: no media found!\n", sc->sc_dev.dv_xname);
   4406 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   4407 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   4408 		return;
   4409 	}
   4410 
   4411 	/*
   4412 	 * Pick the default media.
   4413 	 */
   4414 	if (miidef != 0)
   4415 		defmedia = miidef;
   4416 	else {
   4417 		/*
   4418 		 * XXX Pick a better default.  Should come from SROM
   4419 		 * XXX on 21140[A], and should be "auto" on 21142,
   4420 		 * XXX 21143, and Macronix chips.
   4421 		 */
   4422 		defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
   4423 	}
   4424 
   4425 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
   4426 
   4427 	/*
   4428 	 * Display any non-MII media we've located.
   4429 	 */
   4430 	if (sc->sc_media_seen &
   4431 	    ~((1 << TULIP_ROM_MB_21140_MII) | (1 << TULIP_ROM_MB_21142_MII)))
   4432 		tlp_print_media(sc);
   4433 
   4434 	tlp_sia_fixup(sc);
   4435 }
   4436 
   4437 void
   4438 tlp_2114x_isv_tmsw_get(sc, ifmr)
   4439 	struct tulip_softc *sc;
   4440 	struct ifmediareq *ifmr;
   4441 {
   4442 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   4443 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   4444 
   4445 	/*
   4446 	 * We might be polling a non-MII autosense; check for that.
   4447 	 */
   4448 	if (tm == NULL) {
   4449 #ifdef DIAGNOSTIC
   4450 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   4451 			panic("tlp_2114x_isv_tmsw_get");
   4452 #endif
   4453 		tm = sc->sc_nway_active->ifm_aux;
   4454 	}
   4455 
   4456 	(*tm->tm_get)(sc, ifmr);
   4457 }
   4458 
   4459 int
   4460 tlp_2114x_isv_tmsw_set(sc)
   4461 	struct tulip_softc *sc;
   4462 {
   4463 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   4464 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   4465 
   4466 	/*
   4467 	 * We might be setting a non-MII autosense; check for that.
   4468 	 */
   4469 	if (tm == NULL) {
   4470 #ifdef DIAGNOSTIC
   4471 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   4472 			panic("tlp_2114x_isv_tmsw_set");
   4473 #endif
   4474 		/* XXX XXX XXX */
   4475 	}
   4476 
   4477 	/*
   4478 	 * Check to see if we need to reset the chip, and do it.  The
   4479 	 * reset path will get the OPMODE register right the next
   4480 	 * time through.
   4481 	 */
   4482 	if (TULIP_MEDIA_NEEDSRESET(sc, tm->tm_opmode))
   4483 		return (tlp_init(sc));
   4484 
   4485 	return ((*tm->tm_set)(sc));
   4486 }
   4487 
   4488 /*
   4489  * MII-on-SIO media switch.  Handles only MII attached to the SIO.
   4490  */
   4491 void	tlp_sio_mii_tmsw_init __P((struct tulip_softc *));
   4492 
   4493 const struct tulip_mediasw tlp_sio_mii_mediasw = {
   4494 	tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   4495 };
   4496 
   4497 void
   4498 tlp_sio_mii_tmsw_init(sc)
   4499 	struct tulip_softc *sc;
   4500 {
   4501 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4502 
   4503 	/*
   4504 	 * We don't attach any media info structures to the ifmedia
   4505 	 * entries, so if we're using a pre-init function that needs
   4506 	 * that info, override it to one that doesn't.
   4507 	 */
   4508 	if (sc->sc_preinit == tlp_2114x_preinit)
   4509 		sc->sc_preinit = tlp_2114x_mii_preinit;
   4510 
   4511 	sc->sc_mii.mii_ifp = ifp;
   4512 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
   4513 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
   4514 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4515 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4516 	    tlp_mediastatus);
   4517 	mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   4518 	    MII_OFFSET_ANY);
   4519 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   4520 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   4521 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   4522 	} else {
   4523 		sc->sc_flags |= TULIPF_HAS_MII;
   4524 		sc->sc_tick = tlp_mii_tick;
   4525 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4526 	}
   4527 }
   4528 
   4529 /*
   4530  * Lite-On PNIC media switch.  Must handle MII or internal NWAY.
   4531  */
   4532 void	tlp_pnic_tmsw_init __P((struct tulip_softc *));
   4533 void	tlp_pnic_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
   4534 int	tlp_pnic_tmsw_set __P((struct tulip_softc *));
   4535 
   4536 const struct tulip_mediasw tlp_pnic_mediasw = {
   4537 	tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
   4538 };
   4539 
   4540 void	tlp_pnic_nway_statchg __P((struct device *));
   4541 void	tlp_pnic_nway_tick __P((void *));
   4542 int	tlp_pnic_nway_service __P((struct tulip_softc *, int));
   4543 void	tlp_pnic_nway_reset __P((struct tulip_softc *));
   4544 int	tlp_pnic_nway_auto __P((struct tulip_softc *, int));
   4545 void	tlp_pnic_nway_auto_timeout __P((void *));
   4546 void	tlp_pnic_nway_status __P((struct tulip_softc *));
   4547 void	tlp_pnic_nway_acomp __P((struct tulip_softc *));
   4548 
   4549 void
   4550 tlp_pnic_tmsw_init(sc)
   4551 	struct tulip_softc *sc;
   4552 {
   4553 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4554 	const char *sep = "";
   4555 
   4556 #define	ADD(m, c)	ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL)
   4557 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
   4558 
   4559 	sc->sc_mii.mii_ifp = ifp;
   4560 	sc->sc_mii.mii_readreg = tlp_pnic_mii_readreg;
   4561 	sc->sc_mii.mii_writereg = tlp_pnic_mii_writereg;
   4562 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4563 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4564 	    tlp_mediastatus);
   4565 	mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   4566 	    MII_OFFSET_ANY);
   4567 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   4568 		/* XXX What about AUI/BNC support? */
   4569 		printf("%s: ", sc->sc_dev.dv_xname);
   4570 
   4571 		tlp_pnic_nway_reset(sc);
   4572 
   4573 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
   4574 		    PNIC_NWAY_TW|PNIC_NWAY_CAP10T);
   4575 		PRINT("10baseT");
   4576 
   4577 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
   4578 		    PNIC_NWAY_TW|PNIC_NWAY_FD|PNIC_NWAY_CAP10TFDX);
   4579 		PRINT("10baseT-FDX");
   4580 
   4581 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
   4582 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_CAP100TX);
   4583 		PRINT("100baseTX");
   4584 
   4585 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
   4586 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_FD|
   4587 		    PNIC_NWAY_CAP100TXFDX);
   4588 		PRINT("100baseTX-FDX");
   4589 
   4590 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
   4591 		    PNIC_NWAY_TW|PNIC_NWAY_RN|PNIC_NWAY_NW|
   4592 		    PNIC_NWAY_CAP10T|PNIC_NWAY_CAP10TFDX|
   4593 		    PNIC_NWAY_CAP100TXFDX|PNIC_NWAY_CAP100TX);
   4594 		PRINT("auto");
   4595 
   4596 		printf("\n");
   4597 
   4598 		sc->sc_statchg = tlp_pnic_nway_statchg;
   4599 		sc->sc_tick = tlp_pnic_nway_tick;
   4600 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4601 	} else {
   4602 		sc->sc_flags |= TULIPF_HAS_MII;
   4603 		sc->sc_tick = tlp_mii_tick;
   4604 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4605 	}
   4606 
   4607 #undef ADD
   4608 #undef PRINT
   4609 }
   4610 
   4611 void
   4612 tlp_pnic_tmsw_get(sc, ifmr)
   4613 	struct tulip_softc *sc;
   4614 	struct ifmediareq *ifmr;
   4615 {
   4616 	struct mii_data *mii = &sc->sc_mii;
   4617 
   4618 	if (sc->sc_flags & TULIPF_HAS_MII)
   4619 		tlp_mii_getmedia(sc, ifmr);
   4620 	else {
   4621 		mii->mii_media_status = 0;
   4622 		mii->mii_media_active = IFM_NONE;
   4623 		tlp_pnic_nway_service(sc, MII_POLLSTAT);
   4624 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
   4625 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
   4626 	}
   4627 }
   4628 
   4629 int
   4630 tlp_pnic_tmsw_set(sc)
   4631 	struct tulip_softc *sc;
   4632 {
   4633 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4634 	struct mii_data *mii = &sc->sc_mii;
   4635 
   4636 	if (sc->sc_flags & TULIPF_HAS_MII) {
   4637 		/*
   4638 		 * Make sure the built-in Tx jabber timer is disabled.
   4639 		 */
   4640 		TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);
   4641 
   4642 		return (tlp_mii_setmedia(sc));
   4643 	}
   4644 
   4645 	if (ifp->if_flags & IFF_UP) {
   4646 		mii->mii_media_status = 0;
   4647 		mii->mii_media_active = IFM_NONE;
   4648 		return (tlp_pnic_nway_service(sc, MII_MEDIACHG));
   4649 	}
   4650 
   4651 	return (0);
   4652 }
   4653 
   4654 void
   4655 tlp_pnic_nway_statchg(self)
   4656 	struct device *self;
   4657 {
   4658 	struct tulip_softc *sc = (struct tulip_softc *)self;
   4659 
   4660 	/* Idle the transmit and receive processes. */
   4661 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   4662 
   4663 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_PS|OPMODE_PCS|
   4664 	    OPMODE_SCR|OPMODE_HBD);
   4665 
   4666 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
   4667 		sc->sc_opmode |= OPMODE_TTM;
   4668 		TULIP_WRITE(sc, CSR_GPP,
   4669 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) |
   4670 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
   4671 	} else {
   4672 		sc->sc_opmode |= OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD;
   4673 		TULIP_WRITE(sc, CSR_GPP,
   4674 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) |
   4675 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
   4676 	}
   4677 
   4678 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   4679 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
   4680 
   4681 	/*
   4682 	 * Write new OPMODE bits.  This also restarts the transmit
   4683 	 * and receive processes.
   4684 	 */
   4685 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   4686 
   4687 	/* XXX Update ifp->if_baudrate */
   4688 }
   4689 
   4690 void
   4691 tlp_pnic_nway_tick(arg)
   4692 	void *arg;
   4693 {
   4694 	struct tulip_softc *sc = arg;
   4695 	int s;
   4696 
   4697 	s = splnet();
   4698 	tlp_pnic_nway_service(sc, MII_TICK);
   4699 	splx(s);
   4700 
   4701 	timeout(tlp_pnic_nway_tick, sc, hz);
   4702 }
   4703 
   4704 /*
   4705  * Support for the Lite-On PNIC internal NWay block.  This is constructed
   4706  * somewhat like a PHY driver for simplicity.
   4707  */
   4708 
   4709 int
   4710 tlp_pnic_nway_service(sc, cmd)
   4711 	struct tulip_softc *sc;
   4712 	int cmd;
   4713 {
   4714 	struct mii_data *mii = &sc->sc_mii;
   4715 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   4716 
   4717 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
   4718 		return (0);
   4719 
   4720 	switch (cmd) {
   4721 	case MII_POLLSTAT:
   4722 		/* Nothing special to do here. */
   4723 		break;
   4724 
   4725 	case MII_MEDIACHG:
   4726 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   4727 		case IFM_AUTO:
   4728 			(void) tlp_pnic_nway_auto(sc, 1);
   4729 			break;
   4730 		case IFM_100_T4:
   4731 			/*
   4732 			 * XXX Not supported as a manual setting right now.
   4733 			 */
   4734 			return (EINVAL);
   4735 		default:
   4736 			/*
   4737 			 * NWAY register data is stored in the ifmedia entry.
   4738 			 */
   4739 			TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   4740 		}
   4741 		break;
   4742 
   4743 	case MII_TICK:
   4744 		/*
   4745 		 * Only used for autonegotiation.
   4746 		 */
   4747 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   4748 			return (0);
   4749 
   4750 		/*
   4751 		 * Check to see if we have link.  If we do, we don't
   4752 		 * need to restart the autonegotiation process.
   4753 		 */
   4754 		if (sc->sc_flags & TULIPF_LINK_UP)
   4755 			return (0);
   4756 
   4757 		/*
   4758 		 * Only retry autonegotiation every 5 seconds.
   4759 		 */
   4760 		if (++sc->sc_nway_ticks != 5)
   4761 			return (0);
   4762 
   4763 		sc->sc_nway_ticks = 0;
   4764 		tlp_pnic_nway_reset(sc);
   4765 		if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
   4766 			return (0);
   4767 		break;
   4768 	}
   4769 
   4770 	/* Update the media status. */
   4771 	tlp_pnic_nway_status(sc);
   4772 
   4773 	/* Callback if something changed. */
   4774 	if ((sc->sc_nway_active == NULL ||
   4775 	     sc->sc_nway_active->ifm_media != mii->mii_media_active) ||
   4776 	    cmd == MII_MEDIACHG) {
   4777 		(*sc->sc_statchg)(&sc->sc_dev);
   4778 		tlp_nway_activate(sc, mii->mii_media_active);
   4779 	}
   4780 	return (0);
   4781 }
   4782 
   4783 void
   4784 tlp_pnic_nway_reset(sc)
   4785 	struct tulip_softc *sc;
   4786 {
   4787 
   4788 	TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
   4789 	delay(100);
   4790 	TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
   4791 }
   4792 
   4793 int
   4794 tlp_pnic_nway_auto(sc, waitfor)
   4795 	struct tulip_softc *sc;
   4796 	int waitfor;
   4797 {
   4798 	struct mii_data *mii = &sc->sc_mii;
   4799 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   4800 	u_int32_t reg;
   4801 	int i;
   4802 
   4803 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
   4804 		TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   4805 
   4806 	if (waitfor) {
   4807 		/* Wait 500ms for it to complete. */
   4808 		for (i = 0; i < 500; i++) {
   4809 			reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   4810 			if (reg & PNIC_NWAY_LPAR_MASK) {
   4811 				tlp_pnic_nway_acomp(sc);
   4812 				return (0);
   4813 			}
   4814 			delay(1000);
   4815 		}
   4816 #if 0
   4817 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   4818 			printf("%s: autonegotiation failed to complete\n",
   4819 			    sc->sc_dev.dv_xname);
   4820 #endif
   4821 
   4822 		/*
   4823 		 * Don't need to worry about clearing DOINGAUTO.
   4824 		 * If that's set, a timeout is pending, and it will
   4825 		 * clear the flag.
   4826 		 */
   4827 		return (EIO);
   4828 	}
   4829 
   4830 	/*
   4831 	 * Just let it finish asynchronously.  This is for the benefit of
   4832 	 * the tick handler driving autonegotiation.  Don't want 500ms
   4833 	 * delays all the time while the system is running!
   4834 	 */
   4835 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
   4836 		sc->sc_flags |= TULIPF_DOINGAUTO;
   4837 		timeout(tlp_pnic_nway_auto_timeout, sc, hz >> 1);
   4838 	}
   4839 	return (EJUSTRETURN);
   4840 }
   4841 
   4842 void
   4843 tlp_pnic_nway_auto_timeout(arg)
   4844 	void *arg;
   4845 {
   4846 	struct tulip_softc *sc = arg;
   4847 	u_int32_t reg;
   4848 	int s;
   4849 
   4850 	s = splnet();
   4851 	sc->sc_flags &= ~TULIPF_DOINGAUTO;
   4852 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   4853 #if 0
   4854 	if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   4855 		printf("%s: autonegotiation failed to complete\n",
   4856 		    sc->sc_dev.dv_xname);
   4857 #endif
   4858 
   4859 	tlp_pnic_nway_acomp(sc);
   4860 
   4861 	/* Update the media status. */
   4862 	(void) tlp_pnic_nway_service(sc, MII_POLLSTAT);
   4863 	splx(s);
   4864 }
   4865 
   4866 void
   4867 tlp_pnic_nway_status(sc)
   4868 	struct tulip_softc *sc;
   4869 {
   4870 	struct mii_data *mii = &sc->sc_mii;
   4871 	u_int32_t reg;
   4872 
   4873 	mii->mii_media_status = IFM_AVALID;
   4874 	mii->mii_media_active = IFM_ETHER;
   4875 
   4876 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   4877 
   4878 	if (sc->sc_flags & TULIPF_LINK_UP)
   4879 		mii->mii_media_status |= IFM_ACTIVE;
   4880 
   4881 	if (reg & PNIC_NWAY_NW) {
   4882 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
   4883 			/* Erg, still trying, I guess... */
   4884 			mii->mii_media_active |= IFM_NONE;
   4885 			return;
   4886 		}
   4887 
   4888 #if 0
   4889 		if (reg & PNIC_NWAY_LPAR100T4)
   4890 			mii->mii_media_active |= IFM_100_T4;
   4891 		else
   4892 #endif
   4893 		if (reg & PNIC_NWAY_LPAR100TXFDX)
   4894 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
   4895 		else if (reg & PNIC_NWAY_LPAR100TX)
   4896 			mii->mii_media_active |= IFM_100_TX;
   4897 		else if (reg & PNIC_NWAY_LPAR10TFDX)
   4898 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
   4899 		else if (reg & PNIC_NWAY_LPAR10T)
   4900 			mii->mii_media_active |= IFM_10_T;
   4901 		else
   4902 			mii->mii_media_active |= IFM_NONE;
   4903 	} else {
   4904 		if (reg & PNIC_NWAY_100)
   4905 			mii->mii_media_active |= IFM_100_TX;
   4906 		else
   4907 			mii->mii_media_active |= IFM_10_T;
   4908 		if (reg & PNIC_NWAY_FD)
   4909 			mii->mii_media_active |= IFM_FDX;
   4910 	}
   4911 }
   4912 
   4913 void
   4914 tlp_pnic_nway_acomp(sc)
   4915 	struct tulip_softc *sc;
   4916 {
   4917 	u_int32_t reg;
   4918 
   4919 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   4920 	reg &= ~(PNIC_NWAY_FD|PNIC_NWAY_100|PNIC_NWAY_RN);
   4921 
   4922 	if (reg & (PNIC_NWAY_LPAR100TXFDX|PNIC_NWAY_LPAR100TX))
   4923 		reg |= PNIC_NWAY_100;
   4924 	if (reg & (PNIC_NWAY_LPAR10TFDX|PNIC_NWAY_LPAR100TXFDX))
   4925 		reg |= PNIC_NWAY_FD;
   4926 
   4927 	TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
   4928 }
   4929 
   4930 /*
   4931  * Macronix PMAC and Lite-On PNIC-II media switch:
   4932  *
   4933  *	MX98713 and MX98713A		21140-like MII or GPIO media.
   4934  *
   4935  *	MX98713A			21143-like MII or SIA/SYM media.
   4936  *
   4937  *	MX98715, MX98715A, MX98725,	21143-like SIA/SYM media.
   4938  *	82C115
   4939  *
   4940  * So, what we do here is fake MII-on-SIO or ISV media info, and
   4941  * use the ISV media switch get/set functions to handle the rest.
   4942  */
   4943 
   4944 void	tlp_pmac_tmsw_init __P((struct tulip_softc *));
   4945 
   4946 const struct tulip_mediasw tlp_pmac_mediasw = {
   4947 	tlp_pmac_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
   4948 };
   4949 
   4950 const struct tulip_mediasw tlp_pmac_mii_mediasw = {
   4951 	tlp_pmac_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   4952 };
   4953 
   4954 void
   4955 tlp_pmac_tmsw_init(sc)
   4956 	struct tulip_softc *sc;
   4957 {
   4958 	static const u_int8_t media[] = {
   4959 		TULIP_ROM_MB_MEDIA_TP,
   4960 		TULIP_ROM_MB_MEDIA_TP_FDX,
   4961 		TULIP_ROM_MB_MEDIA_100TX,
   4962 		TULIP_ROM_MB_MEDIA_100TX_FDX,
   4963 	};
   4964 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4965 
   4966 	sc->sc_mii.mii_ifp = ifp;
   4967 	sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
   4968 	sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
   4969 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4970 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4971 	    tlp_mediastatus);
   4972 	if (sc->sc_chip == TULIP_CHIP_MX98713 ||
   4973 	    sc->sc_chip == TULIP_CHIP_MX98713A) {
   4974 		mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
   4975 		    MII_PHY_ANY, MII_OFFSET_ANY);
   4976 		if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL) {
   4977 			sc->sc_flags |= TULIPF_HAS_MII;
   4978 			sc->sc_tick = tlp_mii_tick;
   4979 			sc->sc_preinit = tlp_2114x_mii_preinit;
   4980 			sc->sc_mediasw = &tlp_pmac_mii_mediasw;
   4981 			ifmedia_set(&sc->sc_mii.mii_media,
   4982 			    IFM_ETHER|IFM_AUTO);
   4983 			return;
   4984 		}
   4985 	}
   4986 
   4987 	switch (sc->sc_chip) {
   4988 	case TULIP_CHIP_MX98713:
   4989 		tlp_add_srom_media(sc, TULIP_ROM_MB_21140_GPR,
   4990 		    tlp_21140_gpio_get, tlp_21140_gpio_set, media, 4);
   4991 
   4992 		/*
   4993 		 * XXX Should implement auto-sense for this someday,
   4994 		 * XXX when we do the same for the 21140.
   4995 		 */
   4996 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   4997 		break;
   4998 
   4999 	default:
   5000 		tlp_add_srom_media(sc, TULIP_ROM_MB_21142_SIA,
   5001 		    tlp_sia_get, tlp_sia_set, media, 2);
   5002 		tlp_add_srom_media(sc, TULIP_ROM_MB_21143_SYM,
   5003 		    tlp_sia_get, tlp_sia_set, media + 2, 2);
   5004 
   5005 		/*
   5006 		 * XXX Autonegotiation not yet supported.
   5007 		 */
   5008 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   5009 		break;
   5010 	}
   5011 
   5012 	tlp_print_media(sc);
   5013 	tlp_sia_fixup(sc);
   5014 
   5015 	/* Set the LED modes. */
   5016 	tlp_pmac_reset(sc);
   5017 
   5018 	sc->sc_reset = tlp_pmac_reset;
   5019 }
   5020 
   5021 /*
   5022  * ADMtek AL981 media switch.  Only has internal PHY.
   5023  */
   5024 void	tlp_al981_tmsw_init __P((struct tulip_softc *));
   5025 
   5026 const struct tulip_mediasw tlp_al981_mediasw = {
   5027 	tlp_al981_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   5028 };
   5029 
   5030 void
   5031 tlp_al981_tmsw_init(sc)
   5032 	struct tulip_softc *sc;
   5033 {
   5034 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   5035 
   5036 	sc->sc_mii.mii_ifp = ifp;
   5037 	sc->sc_mii.mii_readreg = tlp_al981_mii_readreg;
   5038 	sc->sc_mii.mii_writereg = tlp_al981_mii_writereg;
   5039 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   5040 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   5041 	    tlp_mediastatus);
   5042 	mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   5043 	    MII_OFFSET_ANY);
   5044 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   5045 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   5046 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   5047 	} else {
   5048 		sc->sc_flags |= TULIPF_HAS_MII;
   5049 		sc->sc_tick = tlp_mii_tick;
   5050 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   5051 	}
   5052 }
   5053