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