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