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