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