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