Home | History | Annotate | Line # | Download | only in ic
tulip.c revision 1.30
      1 /*	$NetBSD: tulip.c,v 1.30 1999/11/04 01:20:57 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 	sc->sc_busmode = BUSMODE_BAR;
   1380 	switch (sc->sc_chip) {
   1381 	case TULIP_CHIP_21140:
   1382 	case TULIP_CHIP_21140A:
   1383 	case TULIP_CHIP_21142:
   1384 	case TULIP_CHIP_21143:
   1385 		/*
   1386 		 * If we're allowed to do so, use Memory Read Line
   1387 		 * and Memory Read Multiple.
   1388 		 *
   1389 		 * XXX Should we use Memory Write and Invalidate?
   1390 		 */
   1391 		if (sc->sc_flags & TULIPF_MRL)
   1392 			sc->sc_busmode |= BUSMODE_RLE;
   1393 		if (sc->sc_flags & TULIPF_MRM)
   1394 			sc->sc_busmode |= BUSMODE_RME;
   1395 #if 0
   1396 		if (sc->sc_flags & TULIPF_MWI)
   1397 			sc->sc_busmode |= BUSMODE_WLE;
   1398 #endif
   1399 
   1400 	default:
   1401 		/* Nothing. */
   1402 	}
   1403 	switch (sc->sc_cacheline) {
   1404 	default:
   1405 		/*
   1406 		 * Note: We must *always* set these bits; a cache
   1407 		 * alignment of 0 is RESERVED.
   1408 		 */
   1409 	case 8:
   1410 		sc->sc_busmode |= BUSMODE_CAL_8LW;
   1411 		break;
   1412 	case 16:
   1413 		sc->sc_busmode |= BUSMODE_CAL_16LW;
   1414 		break;
   1415 	case 32:
   1416 		sc->sc_busmode |= BUSMODE_CAL_32LW;
   1417 		break;
   1418 	}
   1419 	switch (sc->sc_chip) {
   1420 	case TULIP_CHIP_82C168:
   1421 	case TULIP_CHIP_82C169:
   1422 		sc->sc_busmode |= BUSMODE_PBL_16LW | BUSMODE_PNIC_MBO;
   1423 		break;
   1424 	default:
   1425 		sc->sc_busmode |= BUSMODE_PBL_DEFAULT;
   1426 		break;
   1427 	}
   1428 #if BYTE_ORDER == BIG_ENDIAN
   1429 	/*
   1430 	 * XXX There are reports that this doesn't work properly
   1431 	 * in the old Tulip driver, but BUSMODE_DBO does.  However,
   1432 	 * BUSMODE_DBO is not available on the 21040, and requires
   1433 	 * us to byte-swap the setup packet.  What to do?
   1434 	 */
   1435 	sc->sc_busmode |= BUSMODE_BLE;
   1436 #endif
   1437 	TULIP_WRITE(sc, CSR_BUSMODE, sc->sc_busmode);
   1438 
   1439 	/*
   1440 	 * Initialize the OPMODE register.  We don't write it until
   1441 	 * we're ready to begin the transmit and receive processes.
   1442 	 *
   1443 	 * Media-related OPMODE bits are set in the media callbacks
   1444 	 * for each specific chip/board.
   1445 	 */
   1446 	sc->sc_opmode |= OPMODE_SR | OPMODE_ST |
   1447 	    sc->sc_txth[sc->sc_txthresh].txth_opmode;
   1448 
   1449 	/*
   1450 	 * Magical mystery initialization on the Macronix chips.
   1451 	 * The MX98713 uses its own magic value, the rest share
   1452 	 * a common one.
   1453 	 */
   1454 	switch (sc->sc_chip) {
   1455 	case TULIP_CHIP_MX98713:
   1456 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98713);
   1457 		break;
   1458 
   1459 	case TULIP_CHIP_MX98713A:
   1460 	case TULIP_CHIP_MX98715:
   1461 	case TULIP_CHIP_MX98715A:
   1462 	case TULIP_CHIP_MX98725:
   1463 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98715);
   1464 		break;
   1465 
   1466 	default:
   1467 		/* Nothing. */
   1468 	}
   1469 
   1470 	/*
   1471 	 * Initialize the transmit descriptor ring.
   1472 	 */
   1473 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
   1474 	for (i = 0; i < TULIP_NTXDESC; i++) {
   1475 		sc->sc_txdescs[i].td_ctl = TDCTL_CH;
   1476 		sc->sc_txdescs[i].td_bufaddr2 =
   1477 		    TULIP_CDTXADDR(sc, TULIP_NEXTTX(i));
   1478 	}
   1479 	TULIP_CDTXSYNC(sc, 0, TULIP_NTXDESC,
   1480 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1481 	sc->sc_txfree = TULIP_NTXDESC;
   1482 	sc->sc_txnext = 0;
   1483 
   1484 	/*
   1485 	 * Initialize the transmit job descriptors.
   1486 	 */
   1487 	SIMPLEQ_INIT(&sc->sc_txfreeq);
   1488 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
   1489 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
   1490 		txs = &sc->sc_txsoft[i];
   1491 		txs->txs_mbuf = NULL;
   1492 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1493 	}
   1494 
   1495 	/*
   1496 	 * Initialize the receive descriptor and receive job
   1497 	 * descriptor rings.
   1498 	 */
   1499 	for (i = 0; i < TULIP_NRXDESC; i++) {
   1500 		rxs = &sc->sc_rxsoft[i];
   1501 		if (rxs->rxs_mbuf == NULL) {
   1502 			if ((error = tlp_add_rxbuf(sc, i)) != 0) {
   1503 				printf("%s: unable to allocate or map rx "
   1504 				    "buffer %d, error = %d\n",
   1505 				    sc->sc_dev.dv_xname, i, error);
   1506 				/*
   1507 				 * XXX Should attempt to run with fewer receive
   1508 				 * XXX buffers instead of just failing.
   1509 				 */
   1510 				tlp_rxdrain(sc);
   1511 				goto out;
   1512 			}
   1513 		}
   1514 	}
   1515 	sc->sc_rxptr = 0;
   1516 
   1517 	/*
   1518 	 * Initialize the interrupt mask and enable interrupts.
   1519 	 */
   1520 	/* normal interrupts */
   1521 	sc->sc_inten =  STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
   1522 
   1523 	/* abnormal interrupts */
   1524 	sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
   1525 	    STATUS_RU | STATUS_RPS | STATUS_RWT | STATUS_SE | STATUS_AIS;
   1526 
   1527 	sc->sc_rxint_mask = STATUS_RI|STATUS_RU|STATUS_RWT;
   1528 	sc->sc_txint_mask = STATUS_TI|STATUS_UNF|STATUS_TJT;
   1529 
   1530 	switch (sc->sc_chip) {
   1531 	case TULIP_CHIP_WB89C840F:
   1532 		/*
   1533 		 * Clear bits that we don't want that happen to
   1534 		 * overlap or don't exist.
   1535 		 */
   1536 		sc->sc_inten &= ~(STATUS_WINB_REI|STATUS_RWT);
   1537 		break;
   1538 
   1539 	default:
   1540 		/* Nothing. */
   1541 	}
   1542 
   1543 	sc->sc_rxint_mask &= sc->sc_inten;
   1544 	sc->sc_txint_mask &= sc->sc_inten;
   1545 
   1546 	TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
   1547 	TULIP_WRITE(sc, CSR_STATUS, 0xffffffff);
   1548 
   1549 	/*
   1550 	 * Give the transmit and receive rings to the Tulip.
   1551 	 */
   1552 	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDTXADDR(sc, sc->sc_txnext));
   1553 	TULIP_WRITE(sc, CSR_RXLIST, TULIP_CDRXADDR(sc, sc->sc_rxptr));
   1554 
   1555 	/*
   1556 	 * On chips that do this differently, set the station address.
   1557 	 */
   1558 	switch (sc->sc_chip) {
   1559 	case TULIP_CHIP_WB89C840F:
   1560 	    {
   1561 		/* XXX Do this with stream writes? */
   1562 		bus_addr_t cpa = TULIP_CSR_OFFSET(sc, CSR_WINB_CPA0);
   1563 
   1564 		for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1565 			bus_space_write_1(sc->sc_st, sc->sc_sh,
   1566 			    cpa + i, LLADDR(ifp->if_sadl)[i]);
   1567 		}
   1568 		break;
   1569 	    }
   1570 
   1571 	case TULIP_CHIP_AL981:
   1572 	    {
   1573 		u_int32_t reg;
   1574 		u_int8_t *enaddr = LLADDR(ifp->if_sadl);
   1575 
   1576 		reg = enaddr[0] |
   1577 		      (enaddr[1] << 8) |
   1578 		      (enaddr[2] << 16) |
   1579 		      (enaddr[3] << 24);
   1580 		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR0, reg);
   1581 
   1582 		reg = enaddr[4] |
   1583 		      (enaddr[5] << 8);
   1584 		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR1, reg);
   1585 	    }
   1586 
   1587 	default:
   1588 		/* Nothing. */
   1589 	}
   1590 
   1591 	/*
   1592 	 * Set the receive filter.  This will start the transmit and
   1593 	 * receive processes.
   1594 	 */
   1595 	(*sc->sc_filter_setup)(sc);
   1596 
   1597 	/*
   1598 	 * Set the current media.
   1599 	 */
   1600 	(void) (*sc->sc_mediasw->tmsw_set)(sc);
   1601 
   1602 	/*
   1603 	 * Start the receive process.
   1604 	 */
   1605 	TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
   1606 
   1607 	if (sc->sc_tick != NULL) {
   1608 		/* Start the one second clock. */
   1609 		timeout(sc->sc_tick, sc, hz);
   1610 	}
   1611 
   1612 	/*
   1613 	 * Note that the interface is now running.
   1614 	 */
   1615 	ifp->if_flags |= IFF_RUNNING;
   1616 	ifp->if_flags &= ~IFF_OACTIVE;
   1617 
   1618  out:
   1619 	if (error)
   1620 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1621 	return (error);
   1622 }
   1623 
   1624 /*
   1625  * tlp_rxdrain:
   1626  *
   1627  *	Drain the receive queue.
   1628  */
   1629 void
   1630 tlp_rxdrain(sc)
   1631 	struct tulip_softc *sc;
   1632 {
   1633 	struct tulip_rxsoft *rxs;
   1634 	int i;
   1635 
   1636 	for (i = 0; i < TULIP_NRXDESC; i++) {
   1637 		rxs = &sc->sc_rxsoft[i];
   1638 		if (rxs->rxs_mbuf != NULL) {
   1639 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1640 			m_freem(rxs->rxs_mbuf);
   1641 			rxs->rxs_mbuf = NULL;
   1642 		}
   1643 	}
   1644 }
   1645 
   1646 /*
   1647  * tlp_stop:
   1648  *
   1649  *	Stop transmission on the interface.
   1650  */
   1651 void
   1652 tlp_stop(sc, drain)
   1653 	struct tulip_softc *sc;
   1654 	int drain;
   1655 {
   1656 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1657 	struct tulip_txsoft *txs;
   1658 
   1659 	if (sc->sc_tick != NULL) {
   1660 		/* Stop the one second clock. */
   1661 		untimeout(sc->sc_tick, sc);
   1662 	}
   1663 
   1664 	/* Disable interrupts. */
   1665 	TULIP_WRITE(sc, CSR_INTEN, 0);
   1666 
   1667 	/* Stop the transmit and receive processes. */
   1668 	TULIP_WRITE(sc, CSR_OPMODE, 0);
   1669 	TULIP_WRITE(sc, CSR_RXLIST, 0);
   1670 	TULIP_WRITE(sc, CSR_TXLIST, 0);
   1671 
   1672 	/*
   1673 	 * Release any queued transmit buffers.
   1674 	 */
   1675 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1676 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
   1677 		if (txs->txs_mbuf != NULL) {
   1678 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1679 			m_freem(txs->txs_mbuf);
   1680 			txs->txs_mbuf = NULL;
   1681 		}
   1682 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1683 	}
   1684 
   1685 	if (drain) {
   1686 		/*
   1687 		 * Release the receive buffers.
   1688 		 */
   1689 		tlp_rxdrain(sc);
   1690 	}
   1691 
   1692 	sc->sc_flags &= ~(TULIPF_WANT_SETUP|TULIPF_DOING_SETUP);
   1693 
   1694 	/*
   1695 	 * Mark the interface down and cancel the watchdog timer.
   1696 	 */
   1697 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1698 	ifp->if_timer = 0;
   1699 }
   1700 
   1701 #define	SROM_EMIT(sc, x)						\
   1702 do {									\
   1703 	TULIP_WRITE((sc), CSR_MIIROM, (x));				\
   1704 	delay(1);							\
   1705 } while (0)
   1706 
   1707 /*
   1708  * tlp_srom_idle:
   1709  *
   1710  *	Put the SROM in idle state.
   1711  */
   1712 void
   1713 tlp_srom_idle(sc)
   1714 	struct tulip_softc *sc;
   1715 {
   1716 	u_int32_t miirom;
   1717 	int i;
   1718 
   1719 	miirom = MIIROM_SR;
   1720 	SROM_EMIT(sc, miirom);
   1721 
   1722 	miirom |= MIIROM_RD;
   1723 	SROM_EMIT(sc, miirom);
   1724 
   1725 	miirom |= MIIROM_SROMCS;
   1726 	SROM_EMIT(sc, miirom);
   1727 
   1728 	SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1729 
   1730 	/* Strobe the clock 25 times. */
   1731 	for (i = 0; i < 25; i++) {
   1732 		SROM_EMIT(sc, miirom);
   1733 		SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1734 	}
   1735 
   1736 	SROM_EMIT(sc, miirom);
   1737 
   1738 	miirom &= ~MIIROM_SROMCS;
   1739 	SROM_EMIT(sc, miirom);
   1740 
   1741 	SROM_EMIT(sc, 0);
   1742 }
   1743 
   1744 /*
   1745  * tlp_read_srom:
   1746  *
   1747  *	Read the Tulip SROM.
   1748  */
   1749 void
   1750 tlp_read_srom(sc, word, wordcnt, data)
   1751 	struct tulip_softc *sc;
   1752 	int word, wordcnt;
   1753 	u_int8_t *data;
   1754 {
   1755 	u_int32_t miirom;
   1756 	u_int16_t datain;
   1757 	int i, x;
   1758 
   1759 	tlp_srom_idle(sc);
   1760 
   1761 	/* Select the SROM. */
   1762 	miirom = MIIROM_SR;
   1763 	SROM_EMIT(sc, miirom);
   1764 
   1765 	miirom |= MIIROM_RD;
   1766 	SROM_EMIT(sc, miirom);
   1767 
   1768 	for (i = 0; i < wordcnt; i++) {
   1769 		/* Send CHIP SELECT for one clock tick. */
   1770 		miirom |= MIIROM_SROMCS;
   1771 		SROM_EMIT(sc, miirom);
   1772 
   1773 		/* Shift in the READ opcode. */
   1774 		for (x = 3; x > 0; x--) {
   1775 			if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
   1776 				miirom |= MIIROM_SROMDI;
   1777 			else
   1778 				miirom &= ~MIIROM_SROMDI;
   1779 			SROM_EMIT(sc, miirom);
   1780 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1781 			SROM_EMIT(sc, miirom);
   1782 		}
   1783 
   1784 		/* Shift in address. */
   1785 		for (x = 6; x > 0; x--) {
   1786 			if ((word + i) & (1 << (x - 1)))
   1787 				miirom |= MIIROM_SROMDI;
   1788 			else
   1789 				miirom &= ~MIIROM_SROMDI;
   1790 			SROM_EMIT(sc, miirom);
   1791 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1792 			SROM_EMIT(sc, miirom);
   1793 		}
   1794 
   1795 		/* Shift out data. */
   1796 		miirom &= ~MIIROM_SROMDI;
   1797 		datain = 0;
   1798 		for (x = 16; x > 0; x--) {
   1799 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1800 			if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
   1801 				datain |= (1 << (x - 1));
   1802 			SROM_EMIT(sc, miirom);
   1803 		}
   1804 		data[2 * i] = datain & 0xff;
   1805 		data[(2 * i) + 1] = datain >> 8;
   1806 
   1807 		/* Clear CHIP SELECT. */
   1808 		miirom &= ~MIIROM_SROMCS;
   1809 		SROM_EMIT(sc, miirom);
   1810 	}
   1811 
   1812 	/* Deselect the SROM. */
   1813 	SROM_EMIT(sc, 0);
   1814 
   1815 	/* ...and idle it. */
   1816 	tlp_srom_idle(sc);
   1817 }
   1818 
   1819 #undef SROM_EMIT
   1820 
   1821 /*
   1822  * tlp_add_rxbuf:
   1823  *
   1824  *	Add a receive buffer to the indicated descriptor.
   1825  */
   1826 int
   1827 tlp_add_rxbuf(sc, idx)
   1828 	struct tulip_softc *sc;
   1829 	int idx;
   1830 {
   1831 	struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx];
   1832 	struct mbuf *m;
   1833 	int error;
   1834 
   1835 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1836 	if (m == NULL)
   1837 		return (ENOBUFS);
   1838 
   1839 	MCLGET(m, M_DONTWAIT);
   1840 	if ((m->m_flags & M_EXT) == 0) {
   1841 		m_freem(m);
   1842 		return (ENOBUFS);
   1843 	}
   1844 
   1845 	if (rxs->rxs_mbuf != NULL)
   1846 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1847 
   1848 	rxs->rxs_mbuf = m;
   1849 
   1850 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
   1851 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
   1852 	if (error) {
   1853 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1854 		    sc->sc_dev.dv_xname, idx, error);
   1855 		panic("tlp_add_rxbuf");	/* XXX */
   1856 	}
   1857 
   1858 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1859 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1860 
   1861 	TULIP_INIT_RXDESC(sc, idx);
   1862 
   1863 	return (0);
   1864 }
   1865 
   1866 /*
   1867  * tlp_crc32:
   1868  *
   1869  *	Compute the 32-bit CRC of the provided buffer.
   1870  */
   1871 u_int32_t
   1872 tlp_crc32(buf, len)
   1873 	const u_int8_t *buf;
   1874 	size_t len;
   1875 {
   1876 	static const u_int32_t crctab[] = {
   1877 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   1878 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   1879 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   1880 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   1881 	};
   1882 	u_int32_t crc;
   1883 	int i;
   1884 
   1885 	crc = 0xffffffff;
   1886 	for (i = 0; i < len; i++) {
   1887 		crc ^= buf[i];
   1888 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1889 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1890 	}
   1891 	return (crc);
   1892 }
   1893 
   1894 /*
   1895  * tlp_srom_crcok:
   1896  *
   1897  *	Check the CRC of the Tulip SROM.
   1898  */
   1899 int
   1900 tlp_srom_crcok(romdata)
   1901 	const u_int8_t *romdata;
   1902 {
   1903 	u_int32_t crc;
   1904 
   1905 	crc = tlp_crc32(romdata, TULIP_ROM_CRC32_CHECKSUM);
   1906 	crc = (crc & 0xffff) ^ 0xffff;
   1907 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM))
   1908 		return (1);
   1909 	return (0);
   1910 }
   1911 
   1912 /*
   1913  * tlp_isv_srom:
   1914  *
   1915  *	Check to see if the SROM is in the new standardized format.
   1916  */
   1917 int
   1918 tlp_isv_srom(romdata)
   1919 	const u_int8_t *romdata;
   1920 {
   1921 	int i;
   1922 	u_int16_t cksum;
   1923 
   1924 	if (tlp_srom_crcok(romdata)) {
   1925 		/*
   1926 		 * SROM CRC checks out; must be in the new format.
   1927 		 */
   1928 		return (1);
   1929 	}
   1930 
   1931 	cksum = TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM);
   1932 	if (cksum == 0xffff || cksum == 0) {
   1933 		/*
   1934 		 * No checksum present.  Check the SROM ID; 18 bytes of 0
   1935 		 * followed by 1 (version) followed by the number of
   1936 		 * adapters which use this SROM (should be non-zero).
   1937 		 */
   1938 		for (i = 0; i < TULIP_ROM_SROM_FORMAT_VERION; i++) {
   1939 			if (romdata[i] != 0)
   1940 				return (0);
   1941 		}
   1942 		if (romdata[TULIP_ROM_SROM_FORMAT_VERION] != 1)
   1943 			return (0);
   1944 		if (romdata[TULIP_ROM_CHIP_COUNT] == 0)
   1945 			return (0);
   1946 		return (1);
   1947 	}
   1948 
   1949 	return (0);
   1950 }
   1951 
   1952 /*
   1953  * tlp_isv_srom_enaddr:
   1954  *
   1955  *	Get the Ethernet address from an ISV SROM.
   1956  */
   1957 int
   1958 tlp_isv_srom_enaddr(sc, enaddr)
   1959 	struct tulip_softc *sc;
   1960 	u_int8_t *enaddr;
   1961 {
   1962 	int i, devcnt;
   1963 
   1964 	if (tlp_isv_srom(sc->sc_srom) == 0)
   1965 		return (0);
   1966 
   1967 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   1968 	for (i = 0; i < devcnt; i++) {
   1969 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   1970 			break;
   1971 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   1972 		    sc->sc_devno)
   1973 			break;
   1974 	}
   1975 
   1976 	if (i == devcnt)
   1977 		return (0);
   1978 
   1979 	memcpy(enaddr, &sc->sc_srom[TULIP_ROM_IEEE_NETWORK_ADDRESS],
   1980 	    ETHER_ADDR_LEN);
   1981 	enaddr[5] += i;
   1982 
   1983 	return (1);
   1984 }
   1985 
   1986 /*
   1987  * tlp_parse_old_srom:
   1988  *
   1989  *	Parse old-format SROMs.
   1990  *
   1991  *	This routine is largely lifted from Matt Thomas's `de' driver.
   1992  */
   1993 int
   1994 tlp_parse_old_srom(sc, enaddr)
   1995 	struct tulip_softc *sc;
   1996 	u_int8_t *enaddr;
   1997 {
   1998 	static const u_int8_t testpat[] =
   1999 	    { 0xff, 0, 0x55, 0xaa, 0xff, 0, 0x55, 0xaa };
   2000 	int i;
   2001 	u_int32_t cksum;
   2002 
   2003 	if (memcmp(&sc->sc_srom[0], &sc->sc_srom[16], 8) != 0) {
   2004 		/*
   2005 		 * Some vendors (e.g. ZNYX) don't use the standard
   2006 		 * DEC Address ROM format, but rather just have an
   2007 		 * Ethernet address in the first 6 bytes, maybe a
   2008 		 * 2 byte checksum, and then all 0xff's.
   2009 		 */
   2010 		for (i = 8; i < 32; i++) {
   2011 			if (sc->sc_srom[i] != 0xff)
   2012 				return (0);
   2013 		}
   2014 
   2015 		/*
   2016 		 * Sanity check the Ethernet address:
   2017 		 *
   2018 		 *	- Make sure it's not multicast or locally
   2019 		 *	  assigned
   2020 		 *	- Make sure it has a non-0 OUI
   2021 		 */
   2022 		if (sc->sc_srom[0] & 3)
   2023 			return (0);
   2024 		if (sc->sc_srom[0] == 0 && sc->sc_srom[1] == 0 &&
   2025 		    sc->sc_srom[2] == 0)
   2026 			return (0);
   2027 
   2028 		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   2029 		return (1);
   2030 	}
   2031 
   2032 	/*
   2033 	 * Standard DEC Address ROM test.
   2034 	 */
   2035 
   2036 	if (memcmp(&sc->sc_srom[24], testpat, 8) != 0)
   2037 		return (0);
   2038 
   2039 	for (i = 0; i < 8; i++) {
   2040 		if (sc->sc_srom[i] != sc->sc_srom[15 - i])
   2041 			return (0);
   2042 	}
   2043 
   2044 	memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   2045 
   2046 	cksum = *(u_int16_t *) &enaddr[0];
   2047 
   2048 	cksum <<= 1;
   2049 	if (cksum > 0xffff)
   2050 		cksum -= 0xffff;
   2051 
   2052 	cksum += *(u_int16_t *) &enaddr[2];
   2053 	if (cksum > 0xffff)
   2054 		cksum -= 0xffff;
   2055 
   2056 	cksum <<= 1;
   2057 	if (cksum > 0xffff)
   2058 		cksum -= 0xffff;
   2059 
   2060 	cksum += *(u_int16_t *) &enaddr[4];
   2061 	if (cksum >= 0xffff)
   2062 		cksum -= 0xffff;
   2063 
   2064 	if (cksum != *(u_int16_t *) &sc->sc_srom[6])
   2065 		return (0);
   2066 
   2067 	return (1);
   2068 }
   2069 
   2070 /*
   2071  * tlp_filter_setup:
   2072  *
   2073  *	Set the Tulip's receive filter.
   2074  */
   2075 void
   2076 tlp_filter_setup(sc)
   2077 	struct tulip_softc *sc;
   2078 {
   2079 	struct ethercom *ec = &sc->sc_ethercom;
   2080 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2081 	struct ether_multi *enm;
   2082 	struct ether_multistep step;
   2083 	__volatile u_int32_t *sp;
   2084 	u_int8_t enaddr[ETHER_ADDR_LEN];
   2085 	u_int32_t hash, hashsize;
   2086 	int cnt;
   2087 
   2088 	DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n",
   2089 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2090 
   2091 	memcpy(enaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   2092 
   2093 	/*
   2094 	 * If there are transmissions pending, wait until they have
   2095 	 * completed.
   2096 	 */
   2097 	if (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL ||
   2098 	    (sc->sc_flags & TULIPF_DOING_SETUP) != 0) {
   2099 		sc->sc_flags |= TULIPF_WANT_SETUP;
   2100 		DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n",
   2101 		    sc->sc_dev.dv_xname));
   2102 		return;
   2103 	}
   2104 	sc->sc_flags &= ~TULIPF_WANT_SETUP;
   2105 
   2106 	switch (sc->sc_chip) {
   2107 	case TULIP_CHIP_82C115:
   2108 		hashsize = TULIP_PNICII_HASHSIZE;
   2109 		break;
   2110 
   2111 	default:
   2112 		hashsize = TULIP_MCHASHSIZE;
   2113 	}
   2114 
   2115 	/*
   2116 	 * If we're running, idle the transmit and receive engines.  If
   2117 	 * we're NOT running, we're being called from tlp_init(), and our
   2118 	 * writing OPMODE will start the transmit and receive processes
   2119 	 * in motion.
   2120 	 */
   2121 	if (ifp->if_flags & IFF_RUNNING) {
   2122 		/*
   2123 		 * Actually, some chips seem to need a really hard
   2124 		 * kick in the head for this to work.  The genuine
   2125 		 * DEC chips can just be idled, but some of the
   2126 		 * clones seem to REALLY want a reset here.  Doing
   2127 		 * the reset will end up here again, but with
   2128 		 * IFF_RUNNING cleared.
   2129 		 */
   2130 		switch (sc->sc_chip) {
   2131 		case TULIP_CHIP_82C168:
   2132 		case TULIP_CHIP_82C169:
   2133 			tlp_init(sc);
   2134 			return;
   2135 
   2136 		default:
   2137 			tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2138 		}
   2139 	}
   2140 
   2141 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
   2142 
   2143 	if (ifp->if_flags & IFF_PROMISC) {
   2144 		sc->sc_opmode |= OPMODE_PR;
   2145 		goto allmulti;
   2146 	}
   2147 
   2148 	/*
   2149 	 * Try Perfect filtering first.
   2150 	 */
   2151 
   2152 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   2153 	sp = TULIP_CDSP(sc);
   2154 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2155 	cnt = 0;
   2156 	ETHER_FIRST_MULTI(step, ec, enm);
   2157 	while (enm != NULL) {
   2158 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2159 			/*
   2160 			 * We must listen to a range of multicast addresses.
   2161 			 * For now, just accept all multicasts, rather than
   2162 			 * trying to set only those filter bits needed to match
   2163 			 * the range.  (At this time, the only use of address
   2164 			 * ranges is for IP multicast routing, for which the
   2165 			 * range is big enough to require all bits set.)
   2166 			 */
   2167 			goto allmulti;
   2168 		}
   2169 		if (cnt == (TULIP_MAXADDRS - 2)) {
   2170 			/*
   2171 			 * We already have our multicast limit (still need
   2172 			 * our station address and broadcast).  Go to
   2173 			 * Hash-Perfect mode.
   2174 			 */
   2175 			goto hashperfect;
   2176 		}
   2177 		*sp++ = ((u_int16_t *) enm->enm_addrlo)[0];
   2178 		*sp++ = ((u_int16_t *) enm->enm_addrlo)[1];
   2179 		*sp++ = ((u_int16_t *) enm->enm_addrlo)[2];
   2180 		ETHER_NEXT_MULTI(step, enm);
   2181 	}
   2182 
   2183 	if (ifp->if_flags & IFF_BROADCAST) {
   2184 		/* ...and the broadcast address. */
   2185 		cnt++;
   2186 		*sp++ = 0xffff;
   2187 		*sp++ = 0xffff;
   2188 		*sp++ = 0xffff;
   2189 	}
   2190 
   2191 	/* Pad the rest with our station address. */
   2192 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   2193 		*sp++ = ((u_int16_t *) enaddr)[0];
   2194 		*sp++ = ((u_int16_t *) enaddr)[1];
   2195 		*sp++ = ((u_int16_t *) enaddr)[2];
   2196 	}
   2197 	ifp->if_flags &= ~IFF_ALLMULTI;
   2198 	goto setit;
   2199 
   2200  hashperfect:
   2201 	/*
   2202 	 * Try Hash-Perfect mode.
   2203 	 */
   2204 
   2205 	/*
   2206 	 * Some 21140 chips have broken Hash-Perfect modes.  On these
   2207 	 * chips, we simply use Hash-Only mode, and put our station
   2208 	 * address into the filter.
   2209 	 */
   2210 	if (sc->sc_chip == TULIP_CHIP_21140)
   2211 		sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY;
   2212 	else
   2213 		sc->sc_filtmode = TDCTL_Tx_FT_HASH;
   2214 	sp = TULIP_CDSP(sc);
   2215 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2216 	ETHER_FIRST_MULTI(step, ec, enm);
   2217 	while (enm != NULL) {
   2218 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2219 			/*
   2220 			 * We must listen to a range of multicast addresses.
   2221 			 * For now, just accept all multicasts, rather than
   2222 			 * trying to set only those filter bits needed to match
   2223 			 * the range.  (At this time, the only use of address
   2224 			 * ranges is for IP multicast routing, for which the
   2225 			 * range is big enough to require all bits set.)
   2226 			 */
   2227 			goto allmulti;
   2228 		}
   2229 		hash = tlp_mchash(enm->enm_addrlo, hashsize);
   2230 		sp[hash >> 4] |= 1 << (hash & 0xf);
   2231 		ETHER_NEXT_MULTI(step, enm);
   2232 	}
   2233 
   2234 	if (ifp->if_flags & IFF_BROADCAST) {
   2235 		/* ...and the broadcast address. */
   2236 		hash = tlp_mchash(etherbroadcastaddr, hashsize);
   2237 		sp[hash >> 4] |= 1 << (hash & 0xf);
   2238 	}
   2239 
   2240 	if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
   2241 		/* ...and our station address. */
   2242 		hash = tlp_mchash(enaddr, hashsize);
   2243 		sp[hash >> 4] |= 1 << (hash & 0xf);
   2244 	} else {
   2245 		/*
   2246 		 * Hash-Perfect mode; put our station address after
   2247 		 * the hash table.
   2248 		 */
   2249 		sp[39] = ((u_int16_t *) enaddr)[0];
   2250 		sp[40] = ((u_int16_t *) enaddr)[1];
   2251 		sp[41] = ((u_int16_t *) enaddr)[2];
   2252 	}
   2253 	ifp->if_flags &= ~IFF_ALLMULTI;
   2254 	goto setit;
   2255 
   2256  allmulti:
   2257 	/*
   2258 	 * Use Perfect filter mode.  First address is the broadcast address,
   2259 	 * and pad the rest with our station address.  We'll set Pass-all-
   2260 	 * multicast in OPMODE below.
   2261 	 */
   2262 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   2263 	sp = TULIP_CDSP(sc);
   2264 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2265 	cnt = 0;
   2266 	if (ifp->if_flags & IFF_BROADCAST) {
   2267 		cnt++;
   2268 		*sp++ = 0xffff;
   2269 		*sp++ = 0xffff;
   2270 		*sp++ = 0xffff;
   2271 	}
   2272 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   2273 		*sp++ = ((u_int16_t *) enaddr)[0];
   2274 		*sp++ = ((u_int16_t *) enaddr)[1];
   2275 		*sp++ = ((u_int16_t *) enaddr)[2];
   2276 	}
   2277 	ifp->if_flags |= IFF_ALLMULTI;
   2278 
   2279  setit:
   2280 	if (ifp->if_flags & IFF_ALLMULTI)
   2281 		sc->sc_opmode |= OPMODE_PM;
   2282 
   2283 	/* Sync the setup packet buffer. */
   2284 	TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE);
   2285 
   2286 	/*
   2287 	 * Fill in the setup packet descriptor.
   2288 	 */
   2289 	sc->sc_setup_desc.td_bufaddr1 = TULIP_CDSPADDR(sc);
   2290 	sc->sc_setup_desc.td_bufaddr2 = TULIP_CDTXADDR(sc, sc->sc_txnext);
   2291 	sc->sc_setup_desc.td_ctl =
   2292 	    (TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) |
   2293 	    sc->sc_filtmode | TDCTL_Tx_SET | TDCTL_Tx_FS | TDCTL_Tx_LS |
   2294 	    TDCTL_Tx_IC | TDCTL_CH;
   2295 	sc->sc_setup_desc.td_status = TDSTAT_OWN;
   2296 	TULIP_CDSDSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   2297 
   2298 	/*
   2299 	 * Write the address of the setup descriptor.  This also has
   2300 	 * the side effect of giving the transmit ring to the chip,
   2301 	 * since the setup descriptor points to the next available
   2302 	 * descriptor in the ring.
   2303 	 */
   2304 	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDSDADDR(sc));
   2305 
   2306 	/*
   2307 	 * Set the OPMODE register.  This will also resume the
   2308 	 * transmit transmit process we idled above.
   2309 	 */
   2310 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2311 
   2312 	sc->sc_flags |= TULIPF_DOING_SETUP;
   2313 
   2314 	/*
   2315 	 * Kick the transmitter; this will cause the Tulip to
   2316 	 * read the setup descriptor.
   2317 	 */
   2318 	/* XXX USE AUTOPOLLING? */
   2319 	TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
   2320 
   2321 	/* Set up a watchdog timer in case the chip flakes out. */
   2322 	ifp->if_timer = 5;
   2323 
   2324 	DPRINTF(sc, ("%s: tlp_filter_setup: returning\n", sc->sc_dev.dv_xname));
   2325 }
   2326 
   2327 /*
   2328  * tlp_winb_filter_setup:
   2329  *
   2330  *	Set the Winbond 89C840F's receive filter.
   2331  */
   2332 void
   2333 tlp_winb_filter_setup(sc)
   2334 	struct tulip_softc *sc;
   2335 {
   2336 	struct ethercom *ec = &sc->sc_ethercom;
   2337 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2338 	struct ether_multi *enm;
   2339 	struct ether_multistep step;
   2340 	u_int32_t hash, mchash[2];
   2341 
   2342 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n",
   2343 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2344 
   2345 	sc->sc_opmode &= ~(OPMODE_WINB_APP|OPMODE_WINB_AMP|OPMODE_WINB_ABP);
   2346 
   2347 	if (ifp->if_flags & IFF_MULTICAST)
   2348 		sc->sc_opmode |= OPMODE_WINB_AMP;
   2349 
   2350 	if (ifp->if_flags & IFF_BROADCAST)
   2351 		sc->sc_opmode |= OPMODE_WINB_ABP;
   2352 
   2353 	if (ifp->if_flags & IFF_PROMISC) {
   2354 		sc->sc_opmode |= OPMODE_WINB_APP;
   2355 		goto allmulti;
   2356 	}
   2357 
   2358 	mchash[0] = mchash[1] = 0;
   2359 
   2360 	ETHER_FIRST_MULTI(step, ec, enm);
   2361 	while (enm != NULL) {
   2362 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2363 			/*
   2364 			 * We must listen to a range of multicast addresses.
   2365 			 * For now, just accept all multicasts, rather than
   2366 			 * trying to set only those filter bits needed to match
   2367 			 * the range.  (At this time, the only use of address
   2368 			 * ranges is for IP multicast routing, for which the
   2369 			 * range is big enough to require all bits set.)
   2370 			 */
   2371 			goto allmulti;
   2372 		}
   2373 
   2374 		/*
   2375 		 * According to the FreeBSD `wb' driver, yes, you
   2376 		 * really do invert the hash.
   2377 		 */
   2378 		hash = (~(tlp_crc32(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26))
   2379 		    & 0x3f;
   2380 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
   2381 		ETHER_NEXT_MULTI(step, enm);
   2382 	}
   2383 	ifp->if_flags &= ~IFF_ALLMULTI;
   2384 	goto setit;
   2385 
   2386  allmulti:
   2387 	ifp->if_flags |= IFF_ALLMULTI;
   2388 	mchash[0] = mchash[1] = 0xffffffff;
   2389 
   2390  setit:
   2391 	TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]);
   2392 	TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]);
   2393 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2394 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n",
   2395 	    sc->sc_dev.dv_xname));
   2396 }
   2397 
   2398 /*
   2399  * tlp_al981_filter_setup:
   2400  *
   2401  *	Set the ADMtek AL981's receive filter.
   2402  */
   2403 void
   2404 tlp_al981_filter_setup(sc)
   2405 	struct tulip_softc *sc;
   2406 {
   2407 	struct ethercom *ec = &sc->sc_ethercom;
   2408 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2409 	struct ether_multi *enm;
   2410 	struct ether_multistep step;
   2411 	u_int32_t hash, mchash[2];
   2412 
   2413 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: sc_flags 0x%08x\n",
   2414 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2415 
   2416 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2417 
   2418 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
   2419 
   2420 	if (ifp->if_flags & IFF_PROMISC) {
   2421 		sc->sc_opmode |= OPMODE_PR;
   2422 		goto allmulti;
   2423 	}
   2424 
   2425 	mchash[0] = mchash[1] = 0;
   2426 
   2427 	ETHER_FIRST_MULTI(step, ec, enm);
   2428 	while (enm != NULL) {
   2429 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2430 			/*
   2431 			 * We must listen to a range of multicast addresses.
   2432 			 * For now, just accept all multicasts, rather than
   2433 			 * trying to set only those filter bits needed to match
   2434 			 * the range.  (At this time, the only use of address
   2435 			 * ranges is for IP multicast routing, for which the
   2436 			 * range is big enough to require all bits set.)
   2437 			 */
   2438 			goto allmulti;
   2439 		}
   2440 
   2441 		hash = (tlp_crc32(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26)
   2442 		    & 0x3f;
   2443 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
   2444 		ETHER_NEXT_MULTI(step, enm);
   2445 	}
   2446 	ifp->if_flags &= ~IFF_ALLMULTI;
   2447 	goto setit;
   2448 
   2449  allmulti:
   2450 	ifp->if_flags |= IFF_ALLMULTI;
   2451 	mchash[0] = mchash[1] = 0xffffffff;
   2452 
   2453  setit:
   2454 	TULIP_WRITE(sc, CSR_ADM_MAR0, mchash[0]);
   2455 	TULIP_WRITE(sc, CSR_ADM_MAR1, mchash[1]);
   2456 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2457 	DPRINTF(sc, ("%s: tlp_al981_filter_setup: returning\n",
   2458 	    sc->sc_dev.dv_xname));
   2459 }
   2460 
   2461 /*
   2462  * tlp_idle:
   2463  *
   2464  *	Cause the transmit and/or receive processes to go idle.
   2465  */
   2466 void
   2467 tlp_idle(sc, bits)
   2468 	struct tulip_softc *sc;
   2469 	u_int32_t bits;
   2470 {
   2471 	static const char *tx_state_names[] = {
   2472 		"STOPPED",
   2473 		"RUNNING - FETCH",
   2474 		"RUNNING - WAIT",
   2475 		"RUNNING - READING",
   2476 		"-- RESERVED --",
   2477 		"RUNNING - SETUP",
   2478 		"SUSPENDED",
   2479 		"RUNNING - CLOSE",
   2480 	};
   2481 	static const char *rx_state_names[] = {
   2482 		"STOPPED",
   2483 		"RUNNING - FETCH",
   2484 		"RUNNING - CHECK",
   2485 		"RUNNING - WAIT",
   2486 		"SUSPENDED",
   2487 		"RUNNING - CLOSE",
   2488 		"RUNNING - FLUSH",
   2489 		"RUNNING - QUEUE",
   2490 	};
   2491 	u_int32_t csr, ackmask = 0;
   2492 	int i;
   2493 
   2494 	if (bits & OPMODE_ST)
   2495 		ackmask |= STATUS_TPS;
   2496 
   2497 	if (bits & OPMODE_SR)
   2498 		ackmask |= STATUS_RPS;
   2499 
   2500 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits);
   2501 
   2502 	for (i = 0; i < 1000; i++) {
   2503 		if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
   2504 			break;
   2505 		delay(10);
   2506 	}
   2507 
   2508 	csr = TULIP_READ(sc, CSR_STATUS);
   2509 	if ((csr & ackmask) != ackmask) {
   2510 		if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
   2511 		    (csr & STATUS_TS) != STATUS_TS_STOPPED)
   2512 			printf("%s: transmit process failed to idle: "
   2513 			    "state %s\n", sc->sc_dev.dv_xname,
   2514 			    tx_state_names[(csr & STATUS_TS) >> 20]);
   2515 		if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
   2516 		    (csr & STATUS_RS) != STATUS_RS_STOPPED)
   2517 			printf("%s: receive process failed to idle: "
   2518 			    "state %s\n", sc->sc_dev.dv_xname,
   2519 			    rx_state_names[(csr & STATUS_RS) >> 17]);
   2520 	}
   2521 	TULIP_WRITE(sc, CSR_STATUS, ackmask);
   2522 }
   2523 
   2524 /*****************************************************************************
   2525  * Generic media support functions.
   2526  *****************************************************************************/
   2527 
   2528 /*
   2529  * tlp_mediastatus:	[ifmedia interface function]
   2530  *
   2531  *	Query the current media.
   2532  */
   2533 void
   2534 tlp_mediastatus(ifp, ifmr)
   2535 	struct ifnet *ifp;
   2536 	struct ifmediareq *ifmr;
   2537 {
   2538 	struct tulip_softc *sc = ifp->if_softc;
   2539 
   2540 	(*sc->sc_mediasw->tmsw_get)(sc, ifmr);
   2541 }
   2542 
   2543 /*
   2544  * tlp_mediachange:	[ifmedia interface function]
   2545  *
   2546  *	Update the current media.
   2547  */
   2548 int
   2549 tlp_mediachange(ifp)
   2550 	struct ifnet *ifp;
   2551 {
   2552 	struct tulip_softc *sc = ifp->if_softc;
   2553 
   2554 	return ((*sc->sc_mediasw->tmsw_set)(sc));
   2555 }
   2556 
   2557 /*****************************************************************************
   2558  * Support functions for MII-attached media.
   2559  *****************************************************************************/
   2560 
   2561 /*
   2562  * tlp_mii_tick:
   2563  *
   2564  *	One second timer, used to tick the MII.
   2565  */
   2566 void
   2567 tlp_mii_tick(arg)
   2568 	void *arg;
   2569 {
   2570 	struct tulip_softc *sc = arg;
   2571 	int s;
   2572 
   2573 	s = splnet();
   2574 	mii_tick(&sc->sc_mii);
   2575 	splx(s);
   2576 
   2577 	timeout(sc->sc_tick, sc, hz);
   2578 }
   2579 
   2580 /*
   2581  * tlp_mii_statchg:	[mii interface function]
   2582  *
   2583  *	Callback from PHY when media changes.
   2584  */
   2585 void
   2586 tlp_mii_statchg(self)
   2587 	struct device *self;
   2588 {
   2589 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2590 
   2591 	/* Idle the transmit and receive processes. */
   2592 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2593 
   2594 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_HBD);
   2595 
   2596 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
   2597 		sc->sc_opmode |= OPMODE_TTM;
   2598 	else
   2599 		sc->sc_opmode |= OPMODE_HBD;
   2600 
   2601 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   2602 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
   2603 
   2604 	/*
   2605 	 * Write new OPMODE bits.  This also restarts the transmit
   2606 	 * and receive processes.
   2607 	 */
   2608 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2609 
   2610 	/* XXX Update ifp->if_baudrate */
   2611 }
   2612 
   2613 /*
   2614  * tlp_winb_mii_statchg: [mii interface function]
   2615  *
   2616  *	Callback from PHY when media changes.  This version is
   2617  *	for the Winbond 89C840F, which has different OPMODE bits.
   2618  */
   2619 void
   2620 tlp_winb_mii_statchg(self)
   2621 	struct device *self;
   2622 {
   2623 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2624 
   2625 	/* Idle the transmit and receive processes. */
   2626 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2627 
   2628 	sc->sc_opmode &= ~(OPMODE_WINB_FES|OPMODE_FD);
   2629 
   2630 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX)
   2631 		sc->sc_opmode |= OPMODE_WINB_FES;
   2632 
   2633 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   2634 		sc->sc_opmode |= OPMODE_FD;
   2635 
   2636 	/*
   2637 	 * Write new OPMODE bits.  This also restarts the transmit
   2638 	 * and receive processes.
   2639 	 */
   2640 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2641 
   2642 	/* XXX Update ifp->if_baudrate */
   2643 }
   2644 
   2645 /*
   2646  * tlp_mii_getmedia:
   2647  *
   2648  *	Callback from ifmedia to request current media status.
   2649  */
   2650 void
   2651 tlp_mii_getmedia(sc, ifmr)
   2652 	struct tulip_softc *sc;
   2653 	struct ifmediareq *ifmr;
   2654 {
   2655 
   2656 	mii_pollstat(&sc->sc_mii);
   2657 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   2658 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   2659 }
   2660 
   2661 /*
   2662  * tlp_mii_setmedia:
   2663  *
   2664  *	Callback from ifmedia to request new media setting.
   2665  */
   2666 int
   2667 tlp_mii_setmedia(sc)
   2668 	struct tulip_softc *sc;
   2669 {
   2670 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2671 
   2672 	if (ifp->if_flags & IFF_UP)
   2673 		mii_mediachg(&sc->sc_mii);
   2674 	return (0);
   2675 }
   2676 
   2677 #define	MII_EMIT(sc, x)							\
   2678 do {									\
   2679 	TULIP_WRITE((sc), CSR_MIIROM, (x));				\
   2680 	delay(1);							\
   2681 } while (0)
   2682 
   2683 /*
   2684  * tlp_sio_mii_sync:
   2685  *
   2686  *	Synchronize the SIO-attached MII.
   2687  */
   2688 void
   2689 tlp_sio_mii_sync(sc)
   2690 	struct tulip_softc *sc;
   2691 {
   2692 	u_int32_t miirom;
   2693 	int i;
   2694 
   2695 	miirom = MIIROM_MDO;
   2696 
   2697 	MII_EMIT(sc, miirom);
   2698 	for (i = 0; i < 32; i++) {
   2699 		MII_EMIT(sc, miirom | MIIROM_MDC);
   2700 		MII_EMIT(sc, miirom);
   2701 	}
   2702 }
   2703 
   2704 /*
   2705  * tlp_sio_mii_sendbits:
   2706  *
   2707  *	Send a series of bits out the SIO to the MII.
   2708  */
   2709 void
   2710 tlp_sio_mii_sendbits(sc, data, nbits)
   2711 	struct tulip_softc *sc;
   2712 	u_int32_t data;
   2713 	int nbits;
   2714 {
   2715 	u_int32_t miirom, i;
   2716 
   2717 	miirom = 0;
   2718 	MII_EMIT(sc, miirom);
   2719 
   2720 	for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
   2721 		if (data & i)
   2722 			miirom |= MIIROM_MDO;
   2723 		else
   2724 			miirom &= ~MIIROM_MDO;
   2725 		MII_EMIT(sc, miirom);
   2726 		MII_EMIT(sc, miirom|MIIROM_MDC);
   2727 		MII_EMIT(sc, miirom);
   2728 	}
   2729 }
   2730 
   2731 /*
   2732  * tlp_sio_mii_readreg:
   2733  *
   2734  *	Read a PHY register via SIO-attached MII.
   2735  */
   2736 int
   2737 tlp_sio_mii_readreg(self, phy, reg)
   2738 	struct device *self;
   2739 	int phy, reg;
   2740 {
   2741 	struct tulip_softc *sc = (void *) self;
   2742 	int val = 0, err = 0, i;
   2743 
   2744 	tlp_sio_mii_sync(sc);
   2745 
   2746 	tlp_sio_mii_sendbits(sc, MII_COMMAND_START, 2);
   2747 	tlp_sio_mii_sendbits(sc, MII_COMMAND_READ, 2);
   2748 	tlp_sio_mii_sendbits(sc, phy, 5);
   2749 	tlp_sio_mii_sendbits(sc, reg, 5);
   2750 
   2751 	/* Switch direction to PHY->host, without a clock transition. */
   2752 	MII_EMIT(sc, MIIROM_MIIDIR);
   2753 
   2754 	MII_EMIT(sc, MIIROM_MIIDIR|MIIROM_MDC);
   2755 	MII_EMIT(sc, MIIROM_MIIDIR);
   2756 
   2757 	err = TULIP_ISSET(sc, CSR_MIIROM, MIIROM_MDI);
   2758 
   2759 	MII_EMIT(sc, MIIROM_MIIDIR|MIIROM_MDC);
   2760 	MII_EMIT(sc, MIIROM_MIIDIR);
   2761 
   2762 	for (i = 0; i < 16; i++) {
   2763 		val <<= 1;
   2764 		/* Read data prior to clock low-high transition. */
   2765 		if (err == 0 && TULIP_ISSET(sc, CSR_MIIROM, MIIROM_MDI))
   2766 			val |= 1;
   2767 
   2768 		MII_EMIT(sc, MIIROM_MIIDIR|MIIROM_MDC);
   2769 		MII_EMIT(sc, MIIROM_MIIDIR);
   2770 	}
   2771 
   2772 	/* Set direction to host->PHY, without a clock transition. */
   2773 	MII_EMIT(sc, 0);
   2774 
   2775 	return (err ? 0 : val);
   2776 }
   2777 
   2778 /*
   2779  * tlp_sio_mii_writereg:
   2780  *
   2781  *	Write a PHY register via SIO-attached MII.
   2782  */
   2783 void
   2784 tlp_sio_mii_writereg(self, phy, reg, val)
   2785 	struct device *self;
   2786 	int phy, reg, val;
   2787 {
   2788 	struct tulip_softc *sc = (void *) self;
   2789 
   2790 	tlp_sio_mii_sync(sc);
   2791 
   2792 	tlp_sio_mii_sendbits(sc, MII_COMMAND_START, 2);
   2793 	tlp_sio_mii_sendbits(sc, MII_COMMAND_WRITE, 2);
   2794 	tlp_sio_mii_sendbits(sc, phy, 5);
   2795 	tlp_sio_mii_sendbits(sc, reg, 5);
   2796 	tlp_sio_mii_sendbits(sc, MII_COMMAND_ACK, 2);
   2797 	tlp_sio_mii_sendbits(sc, val, 16);
   2798 
   2799 	MII_EMIT(sc, 0);
   2800 }
   2801 
   2802 #undef MII_EMIT
   2803 
   2804 /*
   2805  * tlp_pnic_mii_readreg:
   2806  *
   2807  *	Read a PHY register on the Lite-On PNIC.
   2808  */
   2809 int
   2810 tlp_pnic_mii_readreg(self, phy, reg)
   2811 	struct device *self;
   2812 	int phy, reg;
   2813 {
   2814 	struct tulip_softc *sc = (void *) self;
   2815 	u_int32_t val;
   2816 	int i;
   2817 
   2818 	TULIP_WRITE(sc, CSR_PNIC_MII,
   2819 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   2820 	    PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
   2821 	    (reg << PNIC_MII_REGSHIFT));
   2822 
   2823 	for (i = 0; i < 1000; i++) {
   2824 		delay(10);
   2825 		val = TULIP_READ(sc, CSR_PNIC_MII);
   2826 		if ((val & PNIC_MII_BUSY) == 0) {
   2827 			if ((val & PNIC_MII_DATA) == PNIC_MII_DATA)
   2828 				return (0);
   2829 			else
   2830 				return (val & PNIC_MII_DATA);
   2831 		}
   2832 	}
   2833 	printf("%s: MII read timed out\n", sc->sc_dev.dv_xname);
   2834 	return (0);
   2835 }
   2836 
   2837 /*
   2838  * tlp_pnic_mii_writereg:
   2839  *
   2840  *	Write a PHY register on the Lite-On PNIC.
   2841  */
   2842 void
   2843 tlp_pnic_mii_writereg(self, phy, reg, val)
   2844 	struct device *self;
   2845 	int phy, reg, val;
   2846 {
   2847 	struct tulip_softc *sc = (void *) self;
   2848 	int i;
   2849 
   2850 	TULIP_WRITE(sc, CSR_PNIC_MII,
   2851 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   2852 	    PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
   2853 	    (reg << PNIC_MII_REGSHIFT) | val);
   2854 
   2855 	for (i = 0; i < 1000; i++) {
   2856 		delay(10);
   2857 		if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
   2858 			return;
   2859 	}
   2860 	printf("%s: MII write timed out\n", sc->sc_dev.dv_xname);
   2861 }
   2862 
   2863 const bus_addr_t tlp_al981_phy_regmap[] = {
   2864 	CSR_ADM_BMCR,
   2865 	CSR_ADM_BMSR,
   2866 	CSR_ADM_PHYIDR1,
   2867 	CSR_ADM_PHYIDR2,
   2868 	CSR_ADM_ANAR,
   2869 	CSR_ADM_ANLPAR,
   2870 	CSR_ADM_ANER,
   2871 
   2872 	CSR_ADM_XMC,
   2873 	CSR_ADM_XCIIS,
   2874 	CSR_ADM_XIE,
   2875 	CSR_ADM_100CTR,
   2876 };
   2877 const int tlp_al981_phy_regmap_size = sizeof(tlp_al981_phy_regmap) /
   2878     sizeof(tlp_al981_phy_regmap[0]);
   2879 
   2880 /*
   2881  * tlp_al981_mii_readreg:
   2882  *
   2883  *	Read a PHY register on the ADMtek AL981.
   2884  */
   2885 int
   2886 tlp_al981_mii_readreg(self, phy, reg)
   2887 	struct device *self;
   2888 	int phy, reg;
   2889 {
   2890 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2891 
   2892 	/* AL981 only has an internal PHY. */
   2893 	if (phy != 0)
   2894 		return (0);
   2895 
   2896 	if (reg >= tlp_al981_phy_regmap_size)
   2897 		return (0);
   2898 
   2899 	return (bus_space_read_4(sc->sc_st, sc->sc_sh,
   2900 	    tlp_al981_phy_regmap[reg]) & 0xffff);
   2901 }
   2902 
   2903 /*
   2904  * tlp_al981_mii_writereg:
   2905  *
   2906  *	Write a PHY register on the ADMtek AL981.
   2907  */
   2908 void
   2909 tlp_al981_mii_writereg(self, phy, reg, val)
   2910 	struct device *self;
   2911 	int phy, reg, val;
   2912 {
   2913 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2914 
   2915 	/* AL981 only has an internal PHY. */
   2916 	if (phy != 0)
   2917 		return;
   2918 
   2919 	if (reg >= tlp_al981_phy_regmap_size)
   2920 		return;
   2921 
   2922 	bus_space_write_4(sc->sc_st, sc->sc_sh,
   2923 	    tlp_al981_phy_regmap[reg], val);
   2924 }
   2925 
   2926 /*****************************************************************************
   2927  * Chip-specific pre-init and reset functions.
   2928  *****************************************************************************/
   2929 
   2930 /*
   2931  * tlp_2114x_preinit:
   2932  *
   2933  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
   2934  */
   2935 void
   2936 tlp_2114x_preinit(sc)
   2937 	struct tulip_softc *sc;
   2938 {
   2939 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   2940 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   2941 
   2942 	/*
   2943 	 * Whether or not we're in MII or SIA/SYM mode, the media info
   2944 	 * contains the appropriate OPMODE bits.
   2945 	 *
   2946 	 * Note that if we have no media info, we are are doing
   2947 	 * non-MII `auto'.
   2948 	 *
   2949 	 * Also, we always set the Must-Be-One bit.
   2950 	 */
   2951 	if (tm == NULL) {
   2952 #ifdef DIAGNOSTIC
   2953 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   2954 			panic("tlp_2114x_preinit: not IFM_AUTO");
   2955 		if (sc->sc_nway_active == NULL)
   2956 			panic("tlp_2114x_preinit: nway_active NULL");
   2957 #endif
   2958 		tm = sc->sc_nway_active->ifm_aux;
   2959 	}
   2960 	sc->sc_opmode |= OPMODE_MBO | tm->tm_opmode;
   2961 
   2962 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2963 }
   2964 
   2965 /*
   2966  * tlp_2114x_mii_preinit:
   2967  *
   2968  *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
   2969  *	This version is used by boards which only have MII and don't have
   2970  *	an ISV SROM.
   2971  */
   2972 void
   2973 tlp_2114x_mii_preinit(sc)
   2974 	struct tulip_softc *sc;
   2975 {
   2976 
   2977 	/*
   2978 	 * Always set the Must-Be-One bit, and Port Select (to select MII).
   2979 	 * We'll never be called during a media change.
   2980 	 */
   2981 	sc->sc_opmode |= OPMODE_MBO|OPMODE_PS;
   2982 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2983 }
   2984 
   2985 /*
   2986  * tlp_pnic_preinit:
   2987  *
   2988  *	Pre-init function for the Lite-On 82c168 and 82c169.
   2989  */
   2990 void
   2991 tlp_pnic_preinit(sc)
   2992 	struct tulip_softc *sc;
   2993 {
   2994 
   2995 	if (sc->sc_flags & TULIPF_HAS_MII) {
   2996 		/*
   2997 		 * MII case: just set the port-select bit; we will never
   2998 		 * be called during a media change.
   2999 		 */
   3000 		sc->sc_opmode |= OPMODE_PS;
   3001 	} else {
   3002 		/*
   3003 		 * ENDEC/PCS/Nway mode; enable the Tx backoff counter.
   3004 		 */
   3005 		sc->sc_opmode |= OPMODE_PNIC_TBEN;
   3006 	}
   3007 }
   3008 
   3009 /*
   3010  * tlp_21140_reset:
   3011  *
   3012  *	Issue a reset sequence on the 21140 via the GPIO facility.
   3013  */
   3014 void
   3015 tlp_21140_reset(sc)
   3016 	struct tulip_softc *sc;
   3017 {
   3018 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   3019 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   3020 	int i;
   3021 
   3022 	/* First, set the direction on the GPIO pins. */
   3023 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   3024 
   3025 	/* Now, issue the reset sequence. */
   3026 	for (i = 0; i < tm->tm_reset_length; i++) {
   3027 		delay(10);
   3028 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_reset_offset + i]);
   3029 	}
   3030 
   3031 	/* Now, issue the selection sequence. */
   3032 	for (i = 0; i < tm->tm_gp_length; i++) {
   3033 		delay(10);
   3034 		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_gp_offset + i]);
   3035 	}
   3036 
   3037 	/* If there were no sequences, just lower the pins. */
   3038 	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0)
   3039 		TULIP_WRITE(sc, CSR_GPP, 0);
   3040 }
   3041 
   3042 /*
   3043  * tlp_pmac_reset:
   3044  *
   3045  *	Reset routine for Macronix chips.
   3046  */
   3047 void
   3048 tlp_pmac_reset(sc)
   3049 	struct tulip_softc *sc;
   3050 {
   3051 
   3052 	switch (sc->sc_chip) {
   3053 	case TULIP_CHIP_82C115:
   3054 	case TULIP_CHIP_MX98715:
   3055 	case TULIP_CHIP_MX98715A:
   3056 	case TULIP_CHIP_MX98725:
   3057 		/*
   3058 		 * Set the LED operating mode.  This information is located
   3059 		 * in the EEPROM at byte offset 0x77, per the MX98715A and
   3060 		 * MX98725 application notes.
   3061 		 */
   3062 		TULIP_WRITE(sc, CSR_MIIROM, sc->sc_srom[0x77] << 24);
   3063 		break;
   3064 
   3065 	default:
   3066 		/* Nothing. */
   3067 	}
   3068 }
   3069 
   3070 /*****************************************************************************
   3071  * Chip/board-specific media switches.  The ones here are ones that
   3072  * are potentially common to multiple front-ends.
   3073  *****************************************************************************/
   3074 
   3075 /*
   3076  * This table is a common place for all sorts of media information,
   3077  * keyed off of the SROM media code for that media.
   3078  *
   3079  * Note that we explicitly configure the 21142/21143 to always advertise
   3080  * NWay capabilities when using the UTP port.
   3081  * XXX Actually, we don't yet.
   3082  */
   3083 const struct tulip_srom_to_ifmedia tulip_srom_to_ifmedia_table[] = {
   3084 	{ TULIP_ROM_MB_MEDIA_TP,	IFM_10_T,	0,
   3085 	  "10baseT",
   3086 	  0,
   3087 	  { SIACONN_21040_10BASET,
   3088 	    SIATXRX_21040_10BASET,
   3089 	    SIAGEN_21040_10BASET },
   3090 
   3091 	  { SIACONN_21041_10BASET,
   3092 	    SIATXRX_21041_10BASET,
   3093 	    SIAGEN_21041_10BASET },
   3094 
   3095 	  { SIACONN_21142_10BASET,
   3096 	    SIATXRX_21142_10BASET,
   3097 	    SIAGEN_21142_10BASET } },
   3098 
   3099 	{ TULIP_ROM_MB_MEDIA_BNC,	IFM_10_2,	0,
   3100 	  "10base2",
   3101 	  0,
   3102 	  { 0,
   3103 	    0,
   3104 	    0 },
   3105 
   3106 	  { SIACONN_21041_BNC,
   3107 	    SIATXRX_21041_BNC,
   3108 	    SIAGEN_21041_BNC },
   3109 
   3110 	  { SIACONN_21142_BNC,
   3111 	    SIATXRX_21142_BNC,
   3112 	    SIAGEN_21142_BNC } },
   3113 
   3114 	{ TULIP_ROM_MB_MEDIA_AUI,	IFM_10_5,	0,
   3115 	  "10base5",
   3116 	  0,
   3117 	  { SIACONN_21040_AUI,
   3118 	    SIATXRX_21040_AUI,
   3119 	    SIAGEN_21040_AUI },
   3120 
   3121 	  { SIACONN_21041_AUI,
   3122 	    SIATXRX_21041_AUI,
   3123 	    SIAGEN_21041_AUI },
   3124 
   3125 	  { SIACONN_21142_AUI,
   3126 	    SIATXRX_21142_AUI,
   3127 	    SIAGEN_21142_AUI } },
   3128 
   3129 	{ TULIP_ROM_MB_MEDIA_100TX,	IFM_100_TX,	0,
   3130 	  "100baseTX",
   3131 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
   3132 	  { 0,
   3133 	    0,
   3134 	    0 },
   3135 
   3136 	  { 0,
   3137 	    0,
   3138 	    0 },
   3139 
   3140 	  { 0,
   3141 	    0,
   3142 	    SIAGEN_ABM } },
   3143 
   3144 	{ TULIP_ROM_MB_MEDIA_TP_FDX,	IFM_10_T,	IFM_FDX,
   3145 	  "10baseT-FDX",
   3146 	  OPMODE_FD|OPMODE_HBD,
   3147 	  { SIACONN_21040_10BASET_FDX,
   3148 	    SIATXRX_21040_10BASET_FDX,
   3149 	    SIAGEN_21040_10BASET_FDX },
   3150 
   3151 	  { SIACONN_21041_10BASET_FDX,
   3152 	    SIATXRX_21041_10BASET_FDX,
   3153 	    SIAGEN_21041_10BASET_FDX },
   3154 
   3155 	  { SIACONN_21142_10BASET_FDX,
   3156 	    SIATXRX_21142_10BASET_FDX,
   3157 	    SIAGEN_21142_10BASET_FDX } },
   3158 
   3159 	{ TULIP_ROM_MB_MEDIA_100TX_FDX,	IFM_100_TX,	IFM_FDX,
   3160 	  "100baseTX-FDX",
   3161 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_FD|OPMODE_HBD,
   3162 	  { 0,
   3163 	    0,
   3164 	    0 },
   3165 
   3166 	  { 0,
   3167 	    0,
   3168 	    0 },
   3169 
   3170 	  { 0,
   3171 	    0,
   3172 	    SIAGEN_ABM } },
   3173 
   3174 	{ TULIP_ROM_MB_MEDIA_100T4,	IFM_100_T4,	0,
   3175 	  "100baseT4",
   3176 	  OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
   3177 	  { 0,
   3178 	    0,
   3179 	    0 },
   3180 
   3181 	  { 0,
   3182 	    0,
   3183 	    0 },
   3184 
   3185 	  { 0,
   3186 	    0,
   3187 	    SIAGEN_ABM } },
   3188 
   3189 	{ TULIP_ROM_MB_MEDIA_100FX,	IFM_100_FX,	0,
   3190 	  "100baseFX",
   3191 	  OPMODE_PS|OPMODE_PCS|OPMODE_HBD,
   3192 	  { 0,
   3193 	    0,
   3194 	    0 },
   3195 
   3196 	  { 0,
   3197 	    0,
   3198 	    0 },
   3199 
   3200 	  { 0,
   3201 	    0,
   3202 	    SIAGEN_ABM } },
   3203 
   3204 	{ TULIP_ROM_MB_MEDIA_100FX_FDX,	IFM_100_FX,	IFM_FDX,
   3205 	  "100baseFX-FDX",
   3206 	  OPMODE_PS|OPMODE_PCS|OPMODE_FD|OPMODE_HBD,
   3207 	  { 0,
   3208 	    0,
   3209 	    0 },
   3210 
   3211 	  { 0,
   3212 	    0,
   3213 	    0 },
   3214 
   3215 	  { 0,
   3216 	    0,
   3217 	    SIAGEN_ABM } },
   3218 
   3219 	{ 0,				0,		0,
   3220 	  NULL,
   3221 	  0,
   3222 	  { 0,
   3223 	    0,
   3224 	    0 },
   3225 
   3226 	  { 0,
   3227 	    0,
   3228 	    0 },
   3229 
   3230 	  { 0,
   3231 	    0,
   3232 	    0 } },
   3233 };
   3234 
   3235 const struct tulip_srom_to_ifmedia *tlp_srom_to_ifmedia __P((u_int8_t));
   3236 void	tlp_srom_media_info __P((struct tulip_softc *,
   3237 	    const struct tulip_srom_to_ifmedia *, struct tulip_21x4x_media *));
   3238 void	tlp_add_srom_media __P((struct tulip_softc *, int,
   3239 	    void (*)(struct tulip_softc *, struct ifmediareq *),
   3240 	    int (*)(struct tulip_softc *), const u_int8_t *, int));
   3241 void	tlp_print_media __P((struct tulip_softc *));
   3242 void	tlp_nway_activate __P((struct tulip_softc *, int));
   3243 void	tlp_get_minst __P((struct tulip_softc *));
   3244 
   3245 const struct tulip_srom_to_ifmedia *
   3246 tlp_srom_to_ifmedia(sm)
   3247 	u_int8_t sm;
   3248 {
   3249 	const struct tulip_srom_to_ifmedia *tsti;
   3250 
   3251 	for (tsti = tulip_srom_to_ifmedia_table;
   3252 	     tsti->tsti_name != NULL; tsti++) {
   3253 		if (tsti->tsti_srom == sm)
   3254 			return (tsti);
   3255 	}
   3256 
   3257 	return (NULL);
   3258 }
   3259 
   3260 void
   3261 tlp_srom_media_info(sc, tsti, tm)
   3262 	struct tulip_softc *sc;
   3263 	const struct tulip_srom_to_ifmedia *tsti;
   3264 	struct tulip_21x4x_media *tm;
   3265 {
   3266 
   3267 	tm->tm_name = tsti->tsti_name;
   3268 	tm->tm_opmode = tsti->tsti_opmode;
   3269 
   3270 	switch (sc->sc_chip) {
   3271 	case TULIP_CHIP_DE425:
   3272 	case TULIP_CHIP_21040:
   3273 		tm->tm_sia = tsti->tsti_21040;	/* struct assignment */
   3274 		break;
   3275 
   3276 	case TULIP_CHIP_21041:
   3277 		tm->tm_sia = tsti->tsti_21041;	/* struct assignment */
   3278 		break;
   3279 
   3280 	case TULIP_CHIP_21142:
   3281 	case TULIP_CHIP_21143:
   3282 	case TULIP_CHIP_82C115:
   3283 	case TULIP_CHIP_MX98715:
   3284 	case TULIP_CHIP_MX98715A:
   3285 	case TULIP_CHIP_MX98725:
   3286 		tm->tm_sia = tsti->tsti_21142;	/* struct assignment */
   3287 		break;
   3288 
   3289 	default:
   3290 		/* Nothing. */
   3291 	}
   3292 }
   3293 
   3294 void
   3295 tlp_add_srom_media(sc, type, get, set, list, cnt)
   3296 	struct tulip_softc *sc;
   3297 	int type;
   3298 	void (*get) __P((struct tulip_softc *, struct ifmediareq *));
   3299 	int (*set) __P((struct tulip_softc *));
   3300 	const u_int8_t *list;
   3301 	int cnt;
   3302 {
   3303 	struct tulip_21x4x_media *tm;
   3304 	const struct tulip_srom_to_ifmedia *tsti;
   3305 	int i;
   3306 
   3307 	for (i = 0; i < cnt; i++) {
   3308 		tsti = tlp_srom_to_ifmedia(list[i]);
   3309 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   3310 		memset(tm, 0, sizeof(*tm));
   3311 		tlp_srom_media_info(sc, tsti, tm);
   3312 		tm->tm_type = type;
   3313 		tm->tm_get = get;
   3314 		tm->tm_set = set;
   3315 
   3316 		ifmedia_add(&sc->sc_mii.mii_media,
   3317 		    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   3318 		    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   3319 	}
   3320 }
   3321 
   3322 void
   3323 tlp_print_media(sc)
   3324 	struct tulip_softc *sc;
   3325 {
   3326 	struct ifmedia_entry *ife;
   3327 	struct tulip_21x4x_media *tm;
   3328 	const char *sep = "";
   3329 
   3330 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
   3331 
   3332 	printf("%s: ", sc->sc_dev.dv_xname);
   3333 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   3334 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
   3335 		tm = ife->ifm_aux;
   3336 		if (tm == NULL) {
   3337 #ifdef DIAGNOSTIC
   3338 			if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   3339 				panic("tlp_print_media");
   3340 #endif
   3341 			PRINT("auto");
   3342 		} else if (tm->tm_type != TULIP_ROM_MB_21140_MII &&
   3343 			   tm->tm_type != TULIP_ROM_MB_21142_MII) {
   3344 			PRINT(tm->tm_name);
   3345 		}
   3346 	}
   3347 	printf("\n");
   3348 
   3349 #undef PRINT
   3350 }
   3351 
   3352 void
   3353 tlp_nway_activate(sc, media)
   3354 	struct tulip_softc *sc;
   3355 	int media;
   3356 {
   3357 	struct ifmedia_entry *ife;
   3358 
   3359 	ife = ifmedia_match(&sc->sc_mii.mii_media, media, 0);
   3360 #ifdef DIAGNOSTIC
   3361 	if (ife == NULL)
   3362 		panic("tlp_nway_activate");
   3363 #endif
   3364 	sc->sc_nway_active = ife;
   3365 }
   3366 
   3367 void
   3368 tlp_get_minst(sc)
   3369 	struct tulip_softc *sc;
   3370 {
   3371 
   3372 	if ((sc->sc_media_seen &
   3373 	    ~((1 << TULIP_ROM_MB_21140_MII) |
   3374 	      (1 << TULIP_ROM_MB_21142_MII))) == 0) {
   3375 		/*
   3376 		 * We have not yet seen any SIA/SYM media (but are
   3377 		 * about to; that's why we're called!), so assign
   3378 		 * the current media instance to be the `internal media'
   3379 		 * instance, and advance it so any MII media gets a
   3380 		 * fresh one (used to selecting/isolating a PHY).
   3381 		 */
   3382 		sc->sc_tlp_minst = sc->sc_mii.mii_instance++;
   3383 	}
   3384 }
   3385 
   3386 /*
   3387  * SIA Utility functions.
   3388  */
   3389 void	tlp_sia_update_link __P((struct tulip_softc *));
   3390 void	tlp_sia_get __P((struct tulip_softc *, struct ifmediareq *));
   3391 int	tlp_sia_set __P((struct tulip_softc *));
   3392 void	tlp_sia_fixup __P((struct tulip_softc *));
   3393 
   3394 void
   3395 tlp_sia_update_link(sc)
   3396 	struct tulip_softc *sc;
   3397 {
   3398 	struct ifmedia_entry *ife;
   3399 	struct tulip_21x4x_media *tm;
   3400 	u_int32_t siastat;
   3401 
   3402 	ife = TULIP_CURRENT_MEDIA(sc);
   3403 	tm = ife->ifm_aux;
   3404 
   3405 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
   3406 
   3407 	siastat = TULIP_READ(sc, CSR_SIASTAT);
   3408 
   3409 	/*
   3410 	 * Note that when we do SIA link tests, we are assuming that
   3411 	 * the chip is really in the mode that the current media setting
   3412 	 * reflects.  If we're not, then the link tests will not be
   3413 	 * accurate!
   3414 	 */
   3415 	switch (IFM_SUBTYPE(ife->ifm_media)) {
   3416 	case IFM_10_T:
   3417 		sc->sc_flags |= TULIPF_LINK_VALID;
   3418 		if ((siastat & SIASTAT_LS10) == 0)
   3419 			sc->sc_flags |= TULIPF_LINK_UP;
   3420 		break;
   3421 
   3422 	case IFM_100_TX:
   3423 	case IFM_100_T4:
   3424 		sc->sc_flags |= TULIPF_LINK_VALID;
   3425 		if ((siastat & SIASTAT_LS100) == 0)
   3426 			sc->sc_flags |= TULIPF_LINK_UP;
   3427 		break;
   3428 	}
   3429 
   3430 	switch (sc->sc_chip) {
   3431 	case TULIP_CHIP_21142:
   3432 	case TULIP_CHIP_21143:
   3433 		/*
   3434 		 * On these chips, we can tell more information about
   3435 		 * AUI/BNC.  Note that the AUI/BNC selection is made
   3436 		 * in a different register; for our purpose, it's all
   3437 		 * AUI.
   3438 		 */
   3439 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   3440 		case IFM_10_2:
   3441 		case IFM_10_5:
   3442 			sc->sc_flags |= TULIPF_LINK_VALID;
   3443 			if (siastat & SIASTAT_ARA) {
   3444 				TULIP_WRITE(sc, CSR_SIASTAT, SIASTAT_ARA);
   3445 				sc->sc_flags |= TULIPF_LINK_UP;
   3446 			}
   3447 			break;
   3448 
   3449 		default:
   3450 			/*
   3451 			 * If we're SYM media and can detect the link
   3452 			 * via the GPIO facility, prefer that status
   3453 			 * over LS100.
   3454 			 */
   3455 			if (tm->tm_type == TULIP_ROM_MB_21143_SYM &&
   3456 			    tm->tm_actmask != 0) {
   3457 				sc->sc_flags = (sc->sc_flags &
   3458 				    ~TULIPF_LINK_UP) | TULIPF_LINK_VALID;
   3459 				if (TULIP_ISSET(sc, CSR_SIAGEN,
   3460 				    tm->tm_actmask) == tm->tm_actdata)
   3461 					sc->sc_flags |= TULIPF_LINK_UP;
   3462 			}
   3463 		}
   3464 		break;
   3465 
   3466 	default:
   3467 		/* Nothing. */
   3468 	}
   3469 }
   3470 
   3471 void
   3472 tlp_sia_get(sc, ifmr)
   3473 	struct tulip_softc *sc;
   3474 	struct ifmediareq *ifmr;
   3475 {
   3476 	struct ifmedia_entry *ife;
   3477 
   3478 	ifmr->ifm_status = 0;
   3479 
   3480 	tlp_sia_update_link(sc);
   3481 
   3482 	ife = TULIP_CURRENT_MEDIA(sc);
   3483 
   3484 	if (sc->sc_flags & TULIPF_LINK_VALID)
   3485 		ifmr->ifm_status |= IFM_AVALID;
   3486 	if (sc->sc_flags & TULIPF_LINK_UP)
   3487 		ifmr->ifm_status |= IFM_ACTIVE;
   3488 	ifmr->ifm_active = ife->ifm_media;
   3489 }
   3490 
   3491 void
   3492 tlp_sia_fixup(sc)
   3493 	struct tulip_softc *sc;
   3494 {
   3495 	struct ifmedia_entry *ife;
   3496 	struct tulip_21x4x_media *tm;
   3497 	u_int32_t siaconn, siatxrx, siagen;
   3498 
   3499 	switch (sc->sc_chip) {
   3500 	case TULIP_CHIP_82C115:
   3501 	case TULIP_CHIP_MX98713A:
   3502 	case TULIP_CHIP_MX98715:
   3503 	case TULIP_CHIP_MX98715A:
   3504 	case TULIP_CHIP_MX98725:
   3505 		siaconn = PMAC_SIACONN_MASK;
   3506 		siatxrx = PMAC_SIATXRX_MASK;
   3507 		siagen  = PMAC_SIAGEN_MASK;
   3508 		break;
   3509 
   3510 	default:
   3511 		/* No fixups required on any other chips. */
   3512 		return;
   3513 	}
   3514 
   3515 	for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   3516 	     ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
   3517 		tm = ife->ifm_aux;
   3518 		if (tm == NULL)
   3519 			continue;
   3520 
   3521 		tm->tm_siaconn &= siaconn;
   3522 		tm->tm_siatxrx &= siatxrx;
   3523 		tm->tm_siagen  &= siagen;
   3524 	}
   3525 }
   3526 
   3527 int
   3528 tlp_sia_set(sc)
   3529 	struct tulip_softc *sc;
   3530 {
   3531 	struct ifmedia_entry *ife;
   3532 	struct tulip_21x4x_media *tm;
   3533 
   3534 	ife = TULIP_CURRENT_MEDIA(sc);
   3535 	tm = ife->ifm_aux;
   3536 
   3537 	/*
   3538 	 * XXX This appears to be necessary on a bunch of the clone chips.
   3539 	 */
   3540 	delay(20000);
   3541 
   3542 	/*
   3543 	 * Idle the chip.
   3544 	 */
   3545 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   3546 
   3547 	/*
   3548 	 * Program the SIA.  It's important to write in this order,
   3549 	 * resetting the SIA first.
   3550 	 */
   3551 	TULIP_WRITE(sc, CSR_SIACONN, 0);		/* SRL bit clear */
   3552 	delay(1000);
   3553 
   3554 	TULIP_WRITE(sc, CSR_SIATXRX, tm->tm_siatxrx);
   3555 
   3556 	switch (sc->sc_chip) {
   3557 	case TULIP_CHIP_21142:
   3558 	case TULIP_CHIP_21143:
   3559 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpctl);
   3560 		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpdata);
   3561 		break;
   3562 	default:
   3563 		TULIP_WRITE(sc, CSR_SIAGEN,  tm->tm_siagen);
   3564 	}
   3565 
   3566 	TULIP_WRITE(sc, CSR_SIACONN, tm->tm_siaconn);
   3567 
   3568 	/*
   3569 	 * Set the OPMODE bits for this media and write OPMODE.
   3570 	 * This will resume the transmit and receive processes.
   3571 	 */
   3572 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
   3573 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3574 
   3575 	return (0);
   3576 }
   3577 
   3578 /*
   3579  * 21140 GPIO utility functions.
   3580  */
   3581 void	tlp_21140_gpio_update_link __P((struct tulip_softc *));
   3582 void	tlp_21140_gpio_get __P((struct tulip_softc *sc,
   3583 	    struct ifmediareq *ifmr));
   3584 int	tlp_21140_gpio_set __P((struct tulip_softc *sc));
   3585 
   3586 void
   3587 tlp_21140_gpio_update_link(sc)
   3588 	struct tulip_softc *sc;
   3589 {
   3590 	struct ifmedia_entry *ife;
   3591 	struct tulip_21x4x_media *tm;
   3592 
   3593 	ife = TULIP_CURRENT_MEDIA(sc);
   3594 	tm = ife->ifm_aux;
   3595 
   3596 	sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
   3597 
   3598 	if (tm->tm_actmask != 0) {
   3599 		sc->sc_flags |= TULIPF_LINK_VALID;
   3600 		if (TULIP_ISSET(sc, CSR_GPP, tm->tm_actmask) ==
   3601 		    tm->tm_actdata)
   3602 			sc->sc_flags |= TULIPF_LINK_UP;
   3603 	}
   3604 }
   3605 
   3606 void
   3607 tlp_21140_gpio_get(sc, ifmr)
   3608 	struct tulip_softc *sc;
   3609 	struct ifmediareq *ifmr;
   3610 {
   3611 	struct ifmedia_entry *ife;
   3612 
   3613 	ifmr->ifm_status = 0;
   3614 
   3615 	tlp_21140_gpio_update_link(sc);
   3616 
   3617 	ife = TULIP_CURRENT_MEDIA(sc);
   3618 
   3619 	if (sc->sc_flags & TULIPF_LINK_VALID)
   3620 		ifmr->ifm_status |= IFM_AVALID;
   3621 	if (sc->sc_flags & TULIPF_LINK_UP)
   3622 		ifmr->ifm_status |= IFM_ACTIVE;
   3623 	ifmr->ifm_active = ife->ifm_media;
   3624 }
   3625 
   3626 int
   3627 tlp_21140_gpio_set(sc)
   3628 	struct tulip_softc *sc;
   3629 {
   3630 	struct ifmedia_entry *ife;
   3631 	struct tulip_21x4x_media *tm;
   3632 
   3633 	ife = TULIP_CURRENT_MEDIA(sc);
   3634 	tm = ife->ifm_aux;
   3635 
   3636 	/*
   3637 	 * Idle the chip.
   3638 	 */
   3639 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   3640 
   3641 	/*
   3642 	 * Set the GPIO pins for this media, to flip any
   3643 	 * relays, etc.
   3644 	 */
   3645 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   3646 	delay(10);
   3647 	TULIP_WRITE(sc, CSR_GPP, tm->tm_gpdata);
   3648 
   3649 	/*
   3650 	 * Set the OPMODE bits for this media and write OPMODE.
   3651 	 * This will resume the transmit and receive processes.
   3652 	 */
   3653 	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
   3654 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3655 
   3656 	return (0);
   3657 }
   3658 
   3659 /*
   3660  * 21040 and 21041 media switches.
   3661  */
   3662 void	tlp_21040_tmsw_init __P((struct tulip_softc *));
   3663 void	tlp_21040_tp_tmsw_init __P((struct tulip_softc *));
   3664 void	tlp_21040_auibnc_tmsw_init __P((struct tulip_softc *));
   3665 void	tlp_21041_tmsw_init __P((struct tulip_softc *));
   3666 
   3667 const struct tulip_mediasw tlp_21040_mediasw = {
   3668 	tlp_21040_tmsw_init, tlp_sia_get, tlp_sia_set
   3669 };
   3670 
   3671 const struct tulip_mediasw tlp_21040_tp_mediasw = {
   3672 	tlp_21040_tp_tmsw_init, tlp_sia_get, tlp_sia_set
   3673 };
   3674 
   3675 const struct tulip_mediasw tlp_21040_auibnc_mediasw = {
   3676 	tlp_21040_auibnc_tmsw_init, tlp_sia_get, tlp_sia_set
   3677 };
   3678 
   3679 const struct tulip_mediasw tlp_21041_mediasw = {
   3680 	tlp_21041_tmsw_init, tlp_sia_get, tlp_sia_set
   3681 };
   3682 
   3683 
   3684 void
   3685 tlp_21040_tmsw_init(sc)
   3686 	struct tulip_softc *sc;
   3687 {
   3688 	static const u_int8_t media[] = {
   3689 		TULIP_ROM_MB_MEDIA_TP,
   3690 		TULIP_ROM_MB_MEDIA_TP_FDX,
   3691 		TULIP_ROM_MB_MEDIA_AUI,
   3692 	};
   3693 	struct tulip_21x4x_media *tm;
   3694 
   3695 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   3696 	    tlp_mediastatus);
   3697 
   3698 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 3);
   3699 
   3700 	/*
   3701 	 * No SROM type for External SIA.
   3702 	 */
   3703 	tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   3704 	memset(tm, 0, sizeof(*tm));
   3705 	tm->tm_name = "manual";
   3706 	tm->tm_opmode = 0;
   3707 	tm->tm_siaconn = SIACONN_21040_EXTSIA;
   3708 	tm->tm_siatxrx = SIATXRX_21040_EXTSIA;
   3709 	tm->tm_siagen  = SIAGEN_21040_EXTSIA;
   3710 	ifmedia_add(&sc->sc_mii.mii_media,
   3711 	    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, sc->sc_tlp_minst), 0, tm);
   3712 
   3713 	/*
   3714 	 * XXX Autosense not yet supported.
   3715 	 */
   3716 
   3717 	/* XXX This should be auto-sense. */
   3718 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   3719 
   3720 	tlp_print_media(sc);
   3721 }
   3722 
   3723 void
   3724 tlp_21040_tp_tmsw_init(sc)
   3725 	struct tulip_softc *sc;
   3726 {
   3727 	static const u_int8_t media[] = {
   3728 		TULIP_ROM_MB_MEDIA_TP,
   3729 		TULIP_ROM_MB_MEDIA_TP_FDX,
   3730 	};
   3731 
   3732 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   3733 	    tlp_mediastatus);
   3734 
   3735 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 2);
   3736 
   3737 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   3738 
   3739 	tlp_print_media(sc);
   3740 }
   3741 
   3742 void
   3743 tlp_21040_auibnc_tmsw_init(sc)
   3744 	struct tulip_softc *sc;
   3745 {
   3746 	static const u_int8_t media[] = {
   3747 		TULIP_ROM_MB_MEDIA_AUI,
   3748 	};
   3749 
   3750 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   3751 	    tlp_mediastatus);
   3752 
   3753 	tlp_add_srom_media(sc, 0, NULL, NULL, media, 1);
   3754 
   3755 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5);
   3756 
   3757 	tlp_print_media(sc);
   3758 }
   3759 
   3760 void
   3761 tlp_21041_tmsw_init(sc)
   3762 	struct tulip_softc *sc;
   3763 {
   3764 	static const u_int8_t media[] = {
   3765 		TULIP_ROM_MB_MEDIA_TP,
   3766 		TULIP_ROM_MB_MEDIA_TP_FDX,
   3767 		TULIP_ROM_MB_MEDIA_BNC,
   3768 		TULIP_ROM_MB_MEDIA_AUI,
   3769 	};
   3770 	int i, defmedia, devcnt, leaf_offset, mb_offset, m_cnt;
   3771 	const struct tulip_srom_to_ifmedia *tsti;
   3772 	struct tulip_21x4x_media *tm;
   3773 	u_int16_t romdef;
   3774 	u_int8_t mb;
   3775 
   3776 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   3777 	    tlp_mediastatus);
   3778 
   3779 	if (tlp_isv_srom(sc->sc_srom) == 0) {
   3780  not_isv_srom:
   3781 		/*
   3782 		 * If we have a board without the standard 21041 SROM format,
   3783 		 * we just assume all media are present and try and pick a
   3784 		 * reasonable default.
   3785 		 */
   3786 		tlp_add_srom_media(sc, 0, NULL, NULL, media, 4);
   3787 
   3788 		/*
   3789 		 * XXX Autosense not yet supported.
   3790 		 */
   3791 
   3792 		/* XXX This should be auto-sense. */
   3793 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   3794 
   3795 		tlp_print_media(sc);
   3796 		return;
   3797 	}
   3798 
   3799 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   3800 	for (i = 0; i < devcnt; i++) {
   3801 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   3802 			break;
   3803 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   3804 		    sc->sc_devno)
   3805 			break;
   3806 	}
   3807 
   3808 	if (i == devcnt)
   3809 		goto not_isv_srom;
   3810 
   3811 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
   3812 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
   3813 	mb_offset = leaf_offset + TULIP_ROM_IL_MEDIAn_BLOCK_BASE;
   3814 	m_cnt = sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
   3815 
   3816 	for (; m_cnt != 0;
   3817 	     m_cnt--, mb_offset += TULIP_ROM_MB_SIZE(mb)) {
   3818 		mb = sc->sc_srom[mb_offset];
   3819 		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   3820 		memset(tm, 0, sizeof(*tm));
   3821 		switch (mb & TULIP_ROM_MB_MEDIA_CODE) {
   3822 		case TULIP_ROM_MB_MEDIA_TP_FDX:
   3823 		case TULIP_ROM_MB_MEDIA_TP:
   3824 		case TULIP_ROM_MB_MEDIA_BNC:
   3825 		case TULIP_ROM_MB_MEDIA_AUI:
   3826 			tsti = tlp_srom_to_ifmedia(mb &
   3827 			    TULIP_ROM_MB_MEDIA_CODE);
   3828 
   3829 			tlp_srom_media_info(sc, tsti, tm);
   3830 
   3831 			/*
   3832 			 * Override our default SIA settings if the
   3833 			 * SROM contains its own.
   3834 			 */
   3835 			if (mb & TULIP_ROM_MB_EXT) {
   3836 				tm->tm_siaconn = TULIP_ROM_GETW(sc->sc_srom,
   3837 				    mb_offset + TULIP_ROM_MB_CSR13);
   3838 				tm->tm_siatxrx = TULIP_ROM_GETW(sc->sc_srom,
   3839 				    mb_offset + TULIP_ROM_MB_CSR14);
   3840 				tm->tm_siagen = TULIP_ROM_GETW(sc->sc_srom,
   3841 				    mb_offset + TULIP_ROM_MB_CSR15);
   3842 			}
   3843 
   3844 			ifmedia_add(&sc->sc_mii.mii_media,
   3845 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   3846 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   3847 			break;
   3848 
   3849 		default:
   3850 			printf("%s: unknown media code 0x%02x\n",
   3851 			    sc->sc_dev.dv_xname,
   3852 			    mb & TULIP_ROM_MB_MEDIA_CODE);
   3853 			free(tm, M_DEVBUF);
   3854 		}
   3855 	}
   3856 
   3857 	/*
   3858 	 * XXX Autosense not yet supported.
   3859 	 */
   3860 
   3861 	romdef = TULIP_ROM_GETW(sc->sc_srom, leaf_offset +
   3862 	    TULIP_ROM_IL_SELECT_CONN_TYPE);
   3863 	switch (romdef) {
   3864 	case SELECT_CONN_TYPE_TP:
   3865 	case SELECT_CONN_TYPE_TP_AUTONEG:
   3866 	case SELECT_CONN_TYPE_TP_NOLINKPASS:
   3867 		defmedia = IFM_ETHER|IFM_10_T;
   3868 		break;
   3869 
   3870 	case SELECT_CONN_TYPE_TP_FDX:
   3871 		defmedia = IFM_ETHER|IFM_10_T|IFM_FDX;
   3872 		break;
   3873 
   3874 	case SELECT_CONN_TYPE_BNC:
   3875 		defmedia = IFM_ETHER|IFM_10_2;
   3876 		break;
   3877 
   3878 	case SELECT_CONN_TYPE_AUI:
   3879 		defmedia = IFM_ETHER|IFM_10_5;
   3880 		break;
   3881 #if 0 /* XXX */
   3882 	case SELECT_CONN_TYPE_ASENSE:
   3883 	case SELECT_CONN_TYPE_ASENSE_AUTONEG:
   3884 		defmedia = IFM_ETHER|IFM_AUTO;
   3885 		break;
   3886 #endif
   3887 	default:
   3888 		defmedia = 0;
   3889 	}
   3890 
   3891 	if (defmedia == 0) {
   3892 		/*
   3893 		 * XXX We should default to auto-sense.
   3894 		 */
   3895 		defmedia = IFM_ETHER|IFM_10_T;
   3896 	}
   3897 
   3898 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
   3899 
   3900 	tlp_print_media(sc);
   3901 }
   3902 
   3903 /*
   3904  * DECchip 2114x ISV media switch.
   3905  */
   3906 void	tlp_2114x_isv_tmsw_init __P((struct tulip_softc *));
   3907 void	tlp_2114x_isv_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
   3908 int	tlp_2114x_isv_tmsw_set __P((struct tulip_softc *));
   3909 
   3910 const struct tulip_mediasw tlp_2114x_isv_mediasw = {
   3911 	tlp_2114x_isv_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
   3912 };
   3913 
   3914 void
   3915 tlp_2114x_isv_tmsw_init(sc)
   3916 	struct tulip_softc *sc;
   3917 {
   3918 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   3919 	struct ifmedia_entry *ife;
   3920 	struct mii_softc *phy;
   3921 	struct tulip_21x4x_media *tm;
   3922 	const struct tulip_srom_to_ifmedia *tsti;
   3923 	int i, devcnt, leaf_offset, m_cnt, type, length;
   3924 	int defmedia, miidef;
   3925 	u_int16_t word;
   3926 	u_int8_t *cp, *ncp;
   3927 
   3928 	defmedia = miidef = 0;
   3929 
   3930 	sc->sc_mii.mii_ifp = ifp;
   3931 	sc->sc_mii.mii_readreg = tlp_sio_mii_readreg;
   3932 	sc->sc_mii.mii_writereg = tlp_sio_mii_writereg;
   3933 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   3934 
   3935 	/*
   3936 	 * Ignore `instance'; we may get a mixture of SIA and MII
   3937 	 * media, and `instance' is used to isolate or select the
   3938 	 * PHY on the MII as appropriate.  Note that duplicate media
   3939 	 * are disallowed, so ignoring `instance' is safe.
   3940 	 */
   3941 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, tlp_mediachange,
   3942 	    tlp_mediastatus);
   3943 
   3944 	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
   3945 	for (i = 0; i < devcnt; i++) {
   3946 		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
   3947 			break;
   3948 		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
   3949 		    sc->sc_devno)
   3950 			break;
   3951 	}
   3952 
   3953 	if (i == devcnt) {
   3954 		printf("%s: unable to locate info leaf in SROM\n",
   3955 		    sc->sc_dev.dv_xname);
   3956 		return;
   3957 	}
   3958 
   3959 	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
   3960 	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
   3961 
   3962 	/* XXX SELECT CONN TYPE */
   3963 
   3964 	cp = &sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
   3965 
   3966 	/*
   3967 	 * On some chips, the first thing in the Info Leaf is the
   3968 	 * GPIO pin direction data.
   3969 	 */
   3970 	switch (sc->sc_chip) {
   3971 	case TULIP_CHIP_21140:
   3972 	case TULIP_CHIP_21140A:
   3973 	case TULIP_CHIP_MX98713:
   3974 	case TULIP_CHIP_AX88140:
   3975 	case TULIP_CHIP_AX88141:
   3976 		sc->sc_gp_dir = *cp++;
   3977 		break;
   3978 
   3979 	default:
   3980 		/* Nothing. */
   3981 	}
   3982 
   3983 	/* Get the media count. */
   3984 	m_cnt = *cp++;
   3985 
   3986 	for (; m_cnt != 0; cp = ncp, m_cnt--) {
   3987 		/*
   3988 		 * Determine the type and length of this media block.
   3989 		 */
   3990 		if ((*cp & 0x80) == 0) {
   3991 			length = 4;
   3992 			type = TULIP_ROM_MB_21140_GPR;
   3993 		} else {
   3994 			length = (*cp++ & 0x7f) - 1;
   3995 			type = *cp++ & 0x3f;
   3996 		}
   3997 
   3998 		/* Compute the start of the next block. */
   3999 		ncp = cp + length;
   4000 
   4001 		/* Now, parse the block. */
   4002 		switch (type) {
   4003 		case TULIP_ROM_MB_21140_GPR:
   4004 			tlp_get_minst(sc);
   4005 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_GPR;
   4006 
   4007 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4008 			memset(tm, 0, sizeof(*tm));
   4009 
   4010 			tm->tm_type = TULIP_ROM_MB_21140_GPR;
   4011 			tm->tm_get = tlp_21140_gpio_get;
   4012 			tm->tm_set = tlp_21140_gpio_set;
   4013 
   4014 			/* First is the media type code. */
   4015 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4016 			    TULIP_ROM_MB_MEDIA_CODE);
   4017 			if (tsti == NULL) {
   4018 				/* Invalid media code. */
   4019 				free(tm, M_DEVBUF);
   4020 				break;
   4021 			}
   4022 
   4023 			/* Get defaults. */
   4024 			tlp_srom_media_info(sc, tsti, tm);
   4025 
   4026 			/* Next is any GPIO info for this media. */
   4027 			tm->tm_gpdata = cp[1];
   4028 
   4029 			/*
   4030 			 * Next is a word containing OPMODE information
   4031 			 * and info on how to detect if this media is
   4032 			 * active.
   4033 			 */
   4034 			word = TULIP_ROM_GETW(cp, 2);
   4035 			tm->tm_opmode = TULIP_ROM_MB_OPMODE(word);
   4036 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
   4037 				tm->tm_actmask =
   4038 				    TULIP_ROM_MB_BITPOS(word);
   4039 				tm->tm_actdata =
   4040 				    (word & TULIP_ROM_MB_POLARITY) ?
   4041 				    0 : tm->tm_actmask;
   4042 			}
   4043 
   4044 			ifmedia_add(&sc->sc_mii.mii_media,
   4045 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4046 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4047 			break;
   4048 
   4049 		case TULIP_ROM_MB_21140_MII:
   4050 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_MII;
   4051 
   4052 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4053 			memset(tm, 0, sizeof(*tm));
   4054 
   4055 			tm->tm_type = TULIP_ROM_MB_21140_MII;
   4056 			tm->tm_get = tlp_mii_getmedia;
   4057 			tm->tm_set = tlp_mii_setmedia;
   4058 			tm->tm_opmode = OPMODE_PS;
   4059 
   4060 			if (sc->sc_reset == NULL)
   4061 				sc->sc_reset = tlp_21140_reset;
   4062 
   4063 			/* First is the PHY number. */
   4064 			tm->tm_phyno = *cp++;
   4065 
   4066 			/* Next is the MII select sequence length and offset. */
   4067 			tm->tm_gp_length = *cp++;
   4068 			tm->tm_gp_offset = cp - &sc->sc_srom[0];
   4069 			cp += tm->tm_gp_length;
   4070 
   4071 			/* Next is the MII reset sequence length and offset. */
   4072 			tm->tm_reset_length = *cp++;
   4073 			tm->tm_reset_offset = cp - &sc->sc_srom[0];
   4074 			cp += tm->tm_reset_length;
   4075 
   4076 			/*
   4077 			 * The following items are left in the media block
   4078 			 * that we don't particularly care about:
   4079 			 *
   4080 			 *	capabilities		W
   4081 			 *	advertisement		W
   4082 			 *	full duplex		W
   4083 			 *	tx threshold		W
   4084 			 *
   4085 			 * These appear to be bits in the PHY registers,
   4086 			 * which our MII code handles on its own.
   4087 			 */
   4088 
   4089 			/*
   4090 			 * Before we probe the MII bus, we need to reset
   4091 			 * it and issue the selection sequence.
   4092 			 */
   4093 
   4094 			/* Set the direction of the pins... */
   4095 			TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
   4096 
   4097 			for (i = 0; i < tm->tm_reset_length; i++) {
   4098 				delay(10);
   4099 				TULIP_WRITE(sc, CSR_GPP,
   4100 				    sc->sc_srom[tm->tm_reset_offset + i]);
   4101 			}
   4102 
   4103 			for (i = 0; i < tm->tm_gp_length; i++) {
   4104 				delay(10);
   4105 				TULIP_WRITE(sc, CSR_GPP,
   4106 				    sc->sc_srom[tm->tm_gp_offset + i]);
   4107 			}
   4108 
   4109 			/* If there were no sequences, just lower the pins. */
   4110 			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
   4111 				delay(10);
   4112 				TULIP_WRITE(sc, CSR_GPP, 0);
   4113 			}
   4114 
   4115 			/*
   4116 			 * Now, probe the MII for the PHY.  Note, we know
   4117 			 * the location of the PHY on the bus, but we don't
   4118 			 * particularly care; the MII code just likes to
   4119 			 * search the whole thing anyhow.
   4120 			 */
   4121 			mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
   4122 			    MII_PHY_ANY, tm->tm_phyno);
   4123 
   4124 			/*
   4125 			 * Now, search for the PHY we hopefully just
   4126 			 * configured.  If it's not configured into the
   4127 			 * kernel, we lose.  The PHY's default media always
   4128 			 * takes priority.
   4129 			 */
   4130 			for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
   4131 			     phy != NULL;
   4132 			     phy = LIST_NEXT(phy, mii_list))
   4133 				if (phy->mii_offset == tm->tm_phyno)
   4134 					break;
   4135 			if (phy == NULL) {
   4136 				printf("%s: unable to configure MII\n",
   4137 				    sc->sc_dev.dv_xname);
   4138 				break;
   4139 			}
   4140 
   4141 			sc->sc_flags |= TULIPF_HAS_MII;
   4142 			sc->sc_tick = tlp_mii_tick;
   4143 			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
   4144 			    phy->mii_inst);
   4145 
   4146 			/*
   4147 			 * Okay, now that we've found the PHY and the MII
   4148 			 * layer has added all of the media associated
   4149 			 * with that PHY, we need to traverse the media
   4150 			 * list, and add our `tm' to each entry's `aux'
   4151 			 * pointer.
   4152 			 *
   4153 			 * We do this by looking for media with our
   4154 			 * PHY's `instance'.
   4155 			 */
   4156 			for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
   4157 			     ife != NULL;
   4158 			     ife = TAILQ_NEXT(ife, ifm_list)) {
   4159 				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
   4160 					continue;
   4161 				ife->ifm_aux = tm;
   4162 			}
   4163 			break;
   4164 
   4165 		case TULIP_ROM_MB_21142_SIA:
   4166 			tlp_get_minst(sc);
   4167 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_SIA;
   4168 
   4169 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4170 			memset(tm, 0, sizeof(*tm));
   4171 
   4172 			tm->tm_type = TULIP_ROM_MB_21142_SIA;
   4173 			tm->tm_get = tlp_sia_get;
   4174 			tm->tm_set = tlp_sia_set;
   4175 
   4176 			/* First is the media type code. */
   4177 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4178 			    TULIP_ROM_MB_MEDIA_CODE);
   4179 			if (tsti == NULL) {
   4180 				/* Invalid media code. */
   4181 				free(tm, M_DEVBUF);
   4182 				break;
   4183 			}
   4184 
   4185 			/* Get defaults. */
   4186 			tlp_srom_media_info(sc, tsti, tm);
   4187 
   4188 			/*
   4189 			 * Override our default SIA settings if the
   4190 			 * SROM contains its own.
   4191 			 */
   4192 			if (cp[0] & 0x40) {
   4193 				tm->tm_siaconn = TULIP_ROM_GETW(cp, 1);
   4194 				tm->tm_siatxrx = TULIP_ROM_GETW(cp, 3);
   4195 				tm->tm_siagen  = TULIP_ROM_GETW(cp, 5);
   4196 				cp += 7;
   4197 			} else
   4198 				cp++;
   4199 
   4200 			/* Next is GPIO control/data. */
   4201 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 0);
   4202 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 2);
   4203 
   4204 			ifmedia_add(&sc->sc_mii.mii_media,
   4205 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4206 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4207 			break;
   4208 
   4209 		case TULIP_ROM_MB_21142_MII:
   4210 			printf("%s: 21142 MII block\n", sc->sc_dev.dv_xname);
   4211 			break;
   4212 
   4213 		case TULIP_ROM_MB_21143_SYM:
   4214 			tlp_get_minst(sc);
   4215 			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21143_SYM;
   4216 
   4217 			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK);
   4218 			memset(tm, 0, sizeof(*tm));
   4219 
   4220 			tm->tm_type = TULIP_ROM_MB_21143_SYM;
   4221 			tm->tm_get = tlp_sia_get;
   4222 			tm->tm_set = tlp_sia_set;
   4223 
   4224 			/* First is the media type code. */
   4225 			tsti = tlp_srom_to_ifmedia(cp[0] &
   4226 			    TULIP_ROM_MB_MEDIA_CODE);
   4227 			if (tsti == NULL) {
   4228 				/* Invalid media code. */
   4229 				free(tm, M_DEVBUF);
   4230 				break;
   4231 			}
   4232 
   4233 			/* Get defaults. */
   4234 			tlp_srom_media_info(sc, tsti, tm);
   4235 
   4236 			/* Next is GPIO control/data. */
   4237 			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 1);
   4238 			tm->tm_gpdata = TULIP_ROM_GETW(cp, 3);
   4239 
   4240 			/*
   4241 			 * Next is a word containing OPMODE information
   4242 			 * and info on how to detect if this media is
   4243 			 * active.
   4244 			 */
   4245 			word = TULIP_ROM_GETW(cp, 5);
   4246 			tm->tm_opmode = TULIP_ROM_MB_OPMODE(word);
   4247 			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
   4248 				tm->tm_actmask =
   4249 				    TULIP_ROM_MB_BITPOS(word);
   4250 				tm->tm_actdata =
   4251 				    (word & TULIP_ROM_MB_POLARITY) ?
   4252 				    0 : tm->tm_actmask;
   4253 			}
   4254 
   4255 			ifmedia_add(&sc->sc_mii.mii_media,
   4256 			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
   4257 			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
   4258 			break;
   4259 
   4260 		case TULIP_ROM_MB_21143_RESET:
   4261 			printf("%s: 21143 reset block\n", sc->sc_dev.dv_xname);
   4262 			break;
   4263 
   4264 		default:
   4265 			printf("%s: unknown ISV media block type 0x%02x\n",
   4266 			    sc->sc_dev.dv_xname, type);
   4267 		}
   4268 	}
   4269 
   4270 	/*
   4271 	 * Deal with the case where no media is configured.
   4272 	 */
   4273 	if (TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list) == NULL) {
   4274 		printf("%s: no media found!\n", sc->sc_dev.dv_xname);
   4275 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   4276 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   4277 		return;
   4278 	}
   4279 
   4280 	/*
   4281 	 * Pick the default media.
   4282 	 */
   4283 	if (miidef != 0)
   4284 		defmedia = miidef;
   4285 	else {
   4286 		/*
   4287 		 * XXX Pick a better default.  Should come from SROM
   4288 		 * XXX on 21140[A], and should be "auto" on 21142,
   4289 		 * XXX 21143, and Macronix chips.
   4290 		 */
   4291 		defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
   4292 	}
   4293 
   4294 	ifmedia_set(&sc->sc_mii.mii_media, defmedia);
   4295 
   4296 	/*
   4297 	 * Display any non-MII media we've located.
   4298 	 */
   4299 	if (sc->sc_media_seen &
   4300 	    ~((1 << TULIP_ROM_MB_21140_MII) | (1 << TULIP_ROM_MB_21142_MII)))
   4301 		tlp_print_media(sc);
   4302 
   4303 	tlp_sia_fixup(sc);
   4304 }
   4305 
   4306 void
   4307 tlp_2114x_isv_tmsw_get(sc, ifmr)
   4308 	struct tulip_softc *sc;
   4309 	struct ifmediareq *ifmr;
   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 polling 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_get");
   4321 #endif
   4322 		tm = sc->sc_nway_active->ifm_aux;
   4323 	}
   4324 
   4325 	(*tm->tm_get)(sc, ifmr);
   4326 }
   4327 
   4328 int
   4329 tlp_2114x_isv_tmsw_set(sc)
   4330 	struct tulip_softc *sc;
   4331 {
   4332 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   4333 	struct tulip_21x4x_media *tm = ife->ifm_aux;
   4334 
   4335 	/*
   4336 	 * We might be setting a non-MII autosense; check for that.
   4337 	 */
   4338 	if (tm == NULL) {
   4339 #ifdef DIAGNOSTIC
   4340 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   4341 			panic("tlp_2114x_isv_tmsw_set");
   4342 #endif
   4343 		/* XXX XXX XXX */
   4344 	}
   4345 
   4346 	/*
   4347 	 * Check to see if we need to reset the chip, and do it.  The
   4348 	 * reset path will get the OPMODE register right the next
   4349 	 * time through.
   4350 	 */
   4351 	if (TULIP_MEDIA_NEEDSRESET(sc, tm->tm_opmode))
   4352 		return (tlp_init(sc));
   4353 
   4354 	return ((*tm->tm_set)(sc));
   4355 }
   4356 
   4357 /*
   4358  * MII-on-SIO media switch.  Handles only MII attached to the SIO.
   4359  */
   4360 void	tlp_sio_mii_tmsw_init __P((struct tulip_softc *));
   4361 
   4362 const struct tulip_mediasw tlp_sio_mii_mediasw = {
   4363 	tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   4364 };
   4365 
   4366 void
   4367 tlp_sio_mii_tmsw_init(sc)
   4368 	struct tulip_softc *sc;
   4369 {
   4370 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4371 
   4372 	/*
   4373 	 * We don't attach any media info structures to the ifmedia
   4374 	 * entries, so if we're using a pre-init function that needs
   4375 	 * that info, override it to one that doesn't.
   4376 	 */
   4377 	if (sc->sc_preinit == tlp_2114x_preinit)
   4378 		sc->sc_preinit = tlp_2114x_mii_preinit;
   4379 
   4380 	sc->sc_mii.mii_ifp = ifp;
   4381 	sc->sc_mii.mii_readreg = tlp_sio_mii_readreg;
   4382 	sc->sc_mii.mii_writereg = tlp_sio_mii_writereg;
   4383 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4384 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4385 	    tlp_mediastatus);
   4386 	mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   4387 	    MII_OFFSET_ANY);
   4388 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   4389 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   4390 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   4391 	} else {
   4392 		sc->sc_flags |= TULIPF_HAS_MII;
   4393 		sc->sc_tick = tlp_mii_tick;
   4394 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4395 	}
   4396 }
   4397 
   4398 /*
   4399  * Lite-On PNIC media switch.  Must handle MII or internal NWAY.
   4400  */
   4401 void	tlp_pnic_tmsw_init __P((struct tulip_softc *));
   4402 void	tlp_pnic_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
   4403 int	tlp_pnic_tmsw_set __P((struct tulip_softc *));
   4404 
   4405 const struct tulip_mediasw tlp_pnic_mediasw = {
   4406 	tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
   4407 };
   4408 
   4409 void	tlp_pnic_nway_statchg __P((struct device *));
   4410 void	tlp_pnic_nway_tick __P((void *));
   4411 int	tlp_pnic_nway_service __P((struct tulip_softc *, int));
   4412 void	tlp_pnic_nway_reset __P((struct tulip_softc *));
   4413 int	tlp_pnic_nway_auto __P((struct tulip_softc *, int));
   4414 void	tlp_pnic_nway_auto_timeout __P((void *));
   4415 void	tlp_pnic_nway_status __P((struct tulip_softc *));
   4416 void	tlp_pnic_nway_acomp __P((struct tulip_softc *));
   4417 
   4418 void
   4419 tlp_pnic_tmsw_init(sc)
   4420 	struct tulip_softc *sc;
   4421 {
   4422 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4423 	const char *sep = "";
   4424 
   4425 #define	ADD(m, c)	ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL)
   4426 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
   4427 
   4428 	sc->sc_mii.mii_ifp = ifp;
   4429 	sc->sc_mii.mii_readreg = tlp_pnic_mii_readreg;
   4430 	sc->sc_mii.mii_writereg = tlp_pnic_mii_writereg;
   4431 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4432 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4433 	    tlp_mediastatus);
   4434 	mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   4435 	    MII_OFFSET_ANY);
   4436 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   4437 		/* XXX What about AUI/BNC support? */
   4438 		printf("%s: ", sc->sc_dev.dv_xname);
   4439 
   4440 		tlp_pnic_nway_reset(sc);
   4441 
   4442 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
   4443 		    PNIC_NWAY_TW|PNIC_NWAY_CAP10T);
   4444 		PRINT("10baseT");
   4445 
   4446 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
   4447 		    PNIC_NWAY_TW|PNIC_NWAY_FD|PNIC_NWAY_CAP10TFDX);
   4448 		PRINT("10baseT-FDX");
   4449 
   4450 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
   4451 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_CAP100TX);
   4452 		PRINT("100baseTX");
   4453 
   4454 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
   4455 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_FD|
   4456 		    PNIC_NWAY_CAP100TXFDX);
   4457 		PRINT("100baseTX-FDX");
   4458 
   4459 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
   4460 		    PNIC_NWAY_TW|PNIC_NWAY_RN|PNIC_NWAY_NW|
   4461 		    PNIC_NWAY_CAP10T|PNIC_NWAY_CAP10TFDX|
   4462 		    PNIC_NWAY_CAP100TXFDX|PNIC_NWAY_CAP100TX);
   4463 		PRINT("auto");
   4464 
   4465 		printf("\n");
   4466 
   4467 		sc->sc_statchg = tlp_pnic_nway_statchg;
   4468 		sc->sc_tick = tlp_pnic_nway_tick;
   4469 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4470 	} else {
   4471 		sc->sc_flags |= TULIPF_HAS_MII;
   4472 		sc->sc_tick = tlp_mii_tick;
   4473 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4474 	}
   4475 
   4476 #undef ADD
   4477 #undef PRINT
   4478 }
   4479 
   4480 void
   4481 tlp_pnic_tmsw_get(sc, ifmr)
   4482 	struct tulip_softc *sc;
   4483 	struct ifmediareq *ifmr;
   4484 {
   4485 	struct mii_data *mii = &sc->sc_mii;
   4486 
   4487 	if (sc->sc_flags & TULIPF_HAS_MII)
   4488 		tlp_mii_getmedia(sc, ifmr);
   4489 	else {
   4490 		mii->mii_media_status = 0;
   4491 		mii->mii_media_active = IFM_NONE;
   4492 		tlp_pnic_nway_service(sc, MII_POLLSTAT);
   4493 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
   4494 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
   4495 	}
   4496 }
   4497 
   4498 int
   4499 tlp_pnic_tmsw_set(sc)
   4500 	struct tulip_softc *sc;
   4501 {
   4502 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4503 	struct mii_data *mii = &sc->sc_mii;
   4504 
   4505 	if (sc->sc_flags & TULIPF_HAS_MII) {
   4506 		/*
   4507 		 * Make sure the built-in Tx jabber timer is disabled.
   4508 		 */
   4509 		TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);
   4510 
   4511 		return (tlp_mii_setmedia(sc));
   4512 	}
   4513 
   4514 	if (ifp->if_flags & IFF_UP) {
   4515 		mii->mii_media_status = 0;
   4516 		mii->mii_media_active = IFM_NONE;
   4517 		return (tlp_pnic_nway_service(sc, MII_MEDIACHG));
   4518 	}
   4519 
   4520 	return (0);
   4521 }
   4522 
   4523 void
   4524 tlp_pnic_nway_statchg(self)
   4525 	struct device *self;
   4526 {
   4527 	struct tulip_softc *sc = (struct tulip_softc *)self;
   4528 
   4529 	/* Idle the transmit and receive processes. */
   4530 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   4531 
   4532 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_PS|OPMODE_PCS|
   4533 	    OPMODE_SCR|OPMODE_HBD);
   4534 
   4535 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
   4536 		sc->sc_opmode |= OPMODE_TTM;
   4537 		TULIP_WRITE(sc, CSR_GPP,
   4538 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) |
   4539 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
   4540 	} else {
   4541 		sc->sc_opmode |= OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD;
   4542 		TULIP_WRITE(sc, CSR_GPP,
   4543 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) |
   4544 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
   4545 	}
   4546 
   4547 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   4548 		sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
   4549 
   4550 	/*
   4551 	 * Write new OPMODE bits.  This also restarts the transmit
   4552 	 * and receive processes.
   4553 	 */
   4554 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   4555 
   4556 	/* XXX Update ifp->if_baudrate */
   4557 }
   4558 
   4559 void
   4560 tlp_pnic_nway_tick(arg)
   4561 	void *arg;
   4562 {
   4563 	struct tulip_softc *sc = arg;
   4564 	int s;
   4565 
   4566 	s = splnet();
   4567 	tlp_pnic_nway_service(sc, MII_TICK);
   4568 	splx(s);
   4569 
   4570 	timeout(tlp_pnic_nway_tick, sc, hz);
   4571 }
   4572 
   4573 /*
   4574  * Support for the Lite-On PNIC internal NWay block.  This is constructed
   4575  * somewhat like a PHY driver for simplicity.
   4576  */
   4577 
   4578 int
   4579 tlp_pnic_nway_service(sc, cmd)
   4580 	struct tulip_softc *sc;
   4581 	int cmd;
   4582 {
   4583 	struct mii_data *mii = &sc->sc_mii;
   4584 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   4585 
   4586 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
   4587 		return (0);
   4588 
   4589 	switch (cmd) {
   4590 	case MII_POLLSTAT:
   4591 		/* Nothing special to do here. */
   4592 		break;
   4593 
   4594 	case MII_MEDIACHG:
   4595 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   4596 		case IFM_AUTO:
   4597 			(void) tlp_pnic_nway_auto(sc, 1);
   4598 			break;
   4599 		case IFM_100_T4:
   4600 			/*
   4601 			 * XXX Not supported as a manual setting right now.
   4602 			 */
   4603 			return (EINVAL);
   4604 		default:
   4605 			/*
   4606 			 * NWAY register data is stored in the ifmedia entry.
   4607 			 */
   4608 			TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   4609 		}
   4610 		break;
   4611 
   4612 	case MII_TICK:
   4613 		/*
   4614 		 * Only used for autonegotiation.
   4615 		 */
   4616 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   4617 			return (0);
   4618 
   4619 		/*
   4620 		 * Check to see if we have link.  If we do, we don't
   4621 		 * need to restart the autonegotiation process.
   4622 		 */
   4623 		if (sc->sc_flags & TULIPF_LINK_UP)
   4624 			return (0);
   4625 
   4626 		/*
   4627 		 * Only retry autonegotiation every 5 seconds.
   4628 		 */
   4629 		if (++sc->sc_nway_ticks != 5)
   4630 			return (0);
   4631 
   4632 		sc->sc_nway_ticks = 0;
   4633 		tlp_pnic_nway_reset(sc);
   4634 		if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
   4635 			return (0);
   4636 		break;
   4637 	}
   4638 
   4639 	/* Update the media status. */
   4640 	tlp_pnic_nway_status(sc);
   4641 
   4642 	/* Callback if something changed. */
   4643 	if ((sc->sc_nway_active == NULL ||
   4644 	     sc->sc_nway_active->ifm_media != mii->mii_media_active) ||
   4645 	    cmd == MII_MEDIACHG) {
   4646 		(*sc->sc_statchg)(&sc->sc_dev);
   4647 		tlp_nway_activate(sc, mii->mii_media_active);
   4648 	}
   4649 	return (0);
   4650 }
   4651 
   4652 void
   4653 tlp_pnic_nway_reset(sc)
   4654 	struct tulip_softc *sc;
   4655 {
   4656 
   4657 	TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
   4658 	delay(100);
   4659 	TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
   4660 }
   4661 
   4662 int
   4663 tlp_pnic_nway_auto(sc, waitfor)
   4664 	struct tulip_softc *sc;
   4665 	int waitfor;
   4666 {
   4667 	struct mii_data *mii = &sc->sc_mii;
   4668 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   4669 	u_int32_t reg;
   4670 	int i;
   4671 
   4672 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
   4673 		TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   4674 
   4675 	if (waitfor) {
   4676 		/* Wait 500ms for it to complete. */
   4677 		for (i = 0; i < 500; i++) {
   4678 			reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   4679 			if (reg & PNIC_NWAY_LPAR_MASK) {
   4680 				tlp_pnic_nway_acomp(sc);
   4681 				return (0);
   4682 			}
   4683 			delay(1000);
   4684 		}
   4685 #if 0
   4686 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   4687 			printf("%s: autonegotiation failed to complete\n",
   4688 			    sc->sc_dev.dv_xname);
   4689 #endif
   4690 
   4691 		/*
   4692 		 * Don't need to worry about clearing DOINGAUTO.
   4693 		 * If that's set, a timeout is pending, and it will
   4694 		 * clear the flag.
   4695 		 */
   4696 		return (EIO);
   4697 	}
   4698 
   4699 	/*
   4700 	 * Just let it finish asynchronously.  This is for the benefit of
   4701 	 * the tick handler driving autonegotiation.  Don't want 500ms
   4702 	 * delays all the time while the system is running!
   4703 	 */
   4704 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
   4705 		sc->sc_flags |= TULIPF_DOINGAUTO;
   4706 		timeout(tlp_pnic_nway_auto_timeout, sc, hz >> 1);
   4707 	}
   4708 	return (EJUSTRETURN);
   4709 }
   4710 
   4711 void
   4712 tlp_pnic_nway_auto_timeout(arg)
   4713 	void *arg;
   4714 {
   4715 	struct tulip_softc *sc = arg;
   4716 	u_int32_t reg;
   4717 	int s;
   4718 
   4719 	s = splnet();
   4720 	sc->sc_flags &= ~TULIPF_DOINGAUTO;
   4721 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   4722 #if 0
   4723 	if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   4724 		printf("%s: autonegotiation failed to complete\n",
   4725 		    sc->sc_dev.dv_xname);
   4726 #endif
   4727 
   4728 	tlp_pnic_nway_acomp(sc);
   4729 
   4730 	/* Update the media status. */
   4731 	(void) tlp_pnic_nway_service(sc, MII_POLLSTAT);
   4732 	splx(s);
   4733 }
   4734 
   4735 void
   4736 tlp_pnic_nway_status(sc)
   4737 	struct tulip_softc *sc;
   4738 {
   4739 	struct mii_data *mii = &sc->sc_mii;
   4740 	u_int32_t reg;
   4741 
   4742 	mii->mii_media_status = IFM_AVALID;
   4743 	mii->mii_media_active = IFM_ETHER;
   4744 
   4745 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   4746 
   4747 	if (sc->sc_flags & TULIPF_LINK_UP)
   4748 		mii->mii_media_status |= IFM_ACTIVE;
   4749 
   4750 	if (reg & PNIC_NWAY_NW) {
   4751 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
   4752 			/* Erg, still trying, I guess... */
   4753 			mii->mii_media_active |= IFM_NONE;
   4754 			return;
   4755 		}
   4756 
   4757 #if 0
   4758 		if (reg & PNIC_NWAY_LPAR100T4)
   4759 			mii->mii_media_active |= IFM_100_T4;
   4760 		else
   4761 #endif
   4762 		if (reg & PNIC_NWAY_LPAR100TXFDX)
   4763 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
   4764 		else if (reg & PNIC_NWAY_LPAR100TX)
   4765 			mii->mii_media_active |= IFM_100_TX;
   4766 		else if (reg & PNIC_NWAY_LPAR10TFDX)
   4767 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
   4768 		else if (reg & PNIC_NWAY_LPAR10T)
   4769 			mii->mii_media_active |= IFM_10_T;
   4770 		else
   4771 			mii->mii_media_active |= IFM_NONE;
   4772 	} else {
   4773 		if (reg & PNIC_NWAY_100)
   4774 			mii->mii_media_active |= IFM_100_TX;
   4775 		else
   4776 			mii->mii_media_active |= IFM_10_T;
   4777 		if (reg & PNIC_NWAY_FD)
   4778 			mii->mii_media_active |= IFM_FDX;
   4779 	}
   4780 }
   4781 
   4782 void
   4783 tlp_pnic_nway_acomp(sc)
   4784 	struct tulip_softc *sc;
   4785 {
   4786 	u_int32_t reg;
   4787 
   4788 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   4789 	reg &= ~(PNIC_NWAY_FD|PNIC_NWAY_100|PNIC_NWAY_RN);
   4790 
   4791 	if (reg & (PNIC_NWAY_LPAR100TXFDX|PNIC_NWAY_LPAR100TX))
   4792 		reg |= PNIC_NWAY_100;
   4793 	if (reg & (PNIC_NWAY_LPAR10TFDX|PNIC_NWAY_LPAR100TXFDX))
   4794 		reg |= PNIC_NWAY_FD;
   4795 
   4796 	TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
   4797 }
   4798 
   4799 /*
   4800  * Macronix PMAC and Lite-On PNIC-II media switch:
   4801  *
   4802  *	MX98713 and MX98713A		21140-like MII or GPIO media.
   4803  *
   4804  *	MX98713A			21143-like MII or SIA/SYM media.
   4805  *
   4806  *	MX98715, MX98715A, MX98725,	21143-like SIA/SYM media.
   4807  *	82C115
   4808  *
   4809  * So, what we do here is fake MII-on-SIO or ISV media info, and
   4810  * use the ISV media switch get/set functions to handle the rest.
   4811  */
   4812 
   4813 void	tlp_pmac_tmsw_init __P((struct tulip_softc *));
   4814 
   4815 const struct tulip_mediasw tlp_pmac_mediasw = {
   4816 	tlp_pmac_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
   4817 };
   4818 
   4819 const struct tulip_mediasw tlp_pmac_mii_mediasw = {
   4820 	tlp_pmac_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   4821 };
   4822 
   4823 void
   4824 tlp_pmac_tmsw_init(sc)
   4825 	struct tulip_softc *sc;
   4826 {
   4827 	static const u_int8_t media[] = {
   4828 		TULIP_ROM_MB_MEDIA_TP,
   4829 		TULIP_ROM_MB_MEDIA_TP_FDX,
   4830 		TULIP_ROM_MB_MEDIA_100TX,
   4831 		TULIP_ROM_MB_MEDIA_100TX_FDX,
   4832 	};
   4833 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4834 
   4835 	sc->sc_mii.mii_ifp = ifp;
   4836 	sc->sc_mii.mii_readreg = tlp_sio_mii_readreg;
   4837 	sc->sc_mii.mii_writereg = tlp_sio_mii_writereg;
   4838 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4839 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4840 	    tlp_mediastatus);
   4841 	if (sc->sc_chip == TULIP_CHIP_MX98713 ||
   4842 	    sc->sc_chip == TULIP_CHIP_MX98713A) {
   4843 		mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
   4844 		    MII_PHY_ANY, MII_OFFSET_ANY);
   4845 		if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL) {
   4846 			sc->sc_flags |= TULIPF_HAS_MII;
   4847 			sc->sc_tick = tlp_mii_tick;
   4848 			sc->sc_preinit = tlp_2114x_mii_preinit;
   4849 			sc->sc_mediasw = &tlp_pmac_mii_mediasw;
   4850 			ifmedia_set(&sc->sc_mii.mii_media,
   4851 			    IFM_ETHER|IFM_AUTO);
   4852 			return;
   4853 		}
   4854 	}
   4855 
   4856 	switch (sc->sc_chip) {
   4857 	case TULIP_CHIP_MX98713:
   4858 		tlp_add_srom_media(sc, TULIP_ROM_MB_21140_GPR,
   4859 		    tlp_21140_gpio_get, tlp_21140_gpio_set, media, 4);
   4860 
   4861 		/*
   4862 		 * XXX Should implement auto-sense for this someday,
   4863 		 * XXX when we do the same for the 21140.
   4864 		 */
   4865 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   4866 		break;
   4867 
   4868 	default:
   4869 		tlp_add_srom_media(sc, TULIP_ROM_MB_21142_SIA,
   4870 		    tlp_sia_get, tlp_sia_set, media, 2);
   4871 		tlp_add_srom_media(sc, TULIP_ROM_MB_21143_SYM,
   4872 		    tlp_sia_get, tlp_sia_set, media + 2, 2);
   4873 
   4874 		/*
   4875 		 * XXX Autonegotiation not yet supported.
   4876 		 */
   4877 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   4878 		break;
   4879 	}
   4880 
   4881 	tlp_print_media(sc);
   4882 	tlp_sia_fixup(sc);
   4883 
   4884 	/* Set the LED modes. */
   4885 	tlp_pmac_reset(sc);
   4886 
   4887 	sc->sc_reset = tlp_pmac_reset;
   4888 }
   4889 
   4890 /*
   4891  * ADMtek AL981 media switch.  Only has internal PHY.
   4892  */
   4893 void	tlp_al981_tmsw_init __P((struct tulip_softc *));
   4894 
   4895 const struct tulip_mediasw tlp_al981_mediasw = {
   4896 	tlp_al981_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   4897 };
   4898 
   4899 void
   4900 tlp_al981_tmsw_init(sc)
   4901 	struct tulip_softc *sc;
   4902 {
   4903 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4904 
   4905 	sc->sc_mii.mii_ifp = ifp;
   4906 	sc->sc_mii.mii_readreg = tlp_al981_mii_readreg;
   4907 	sc->sc_mii.mii_writereg = tlp_al981_mii_writereg;
   4908 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   4909 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   4910 	    tlp_mediastatus);
   4911 	mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   4912 	    MII_OFFSET_ANY);
   4913 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   4914 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   4915 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   4916 	} else {
   4917 		sc->sc_flags |= TULIPF_HAS_MII;
   4918 		sc->sc_tick = tlp_mii_tick;
   4919 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   4920 	}
   4921 }
   4922