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