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