Home | History | Annotate | Line # | Download | only in ic
tulip.c revision 1.11
      1 /*	$NetBSD: tulip.c,v 1.11 1999/09/17 21:57:36 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 
    154 void	tlp_rxintr __P((struct tulip_softc *));
    155 void	tlp_txintr __P((struct tulip_softc *));
    156 
    157 void	tlp_mii_tick __P((void *));
    158 void	tlp_mii_statchg __P((struct device *));
    159 void	tlp_winb_mii_statchg __P((struct device *));
    160 
    161 void	tlp_mii_getmedia __P((struct tulip_softc *, struct ifmediareq *));
    162 int	tlp_mii_setmedia __P((struct tulip_softc *));
    163 
    164 void	tlp_sio_mii_sync __P((struct tulip_softc *));
    165 void	tlp_sio_mii_sendbits __P((struct tulip_softc *, u_int32_t, int));
    166 int	tlp_sio_mii_readreg __P((struct device *, int, int));
    167 void	tlp_sio_mii_writereg __P((struct device *, int, int, int));
    168 
    169 int	tlp_pnic_mii_readreg __P((struct device *, int, int));
    170 void	tlp_pnic_mii_writereg __P((struct device *, int, int, int));
    171 
    172 u_int32_t tlp_crc32 __P((const u_int8_t *, size_t));
    173 #define	tlp_mchash(addr)	(tlp_crc32((addr), ETHER_ADDR_LEN) &	\
    174 				 (TULIP_MCHASHSIZE - 1))
    175 
    176 #ifdef TLP_DEBUG
    177 #define	DPRINTF(sc, x)	if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
    178 				printf x
    179 #else
    180 #define	DPRINTF(sc, x)	/* nothing */
    181 #endif
    182 
    183 /*
    184  * tlp_attach:
    185  *
    186  *	Attach a Tulip interface to the system.
    187  */
    188 void
    189 tlp_attach(sc, enaddr)
    190 	struct tulip_softc *sc;
    191 	const u_int8_t *enaddr;
    192 {
    193 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    194 	int i, rseg, error;
    195 	bus_dma_segment_t seg;
    196 
    197 	/*
    198 	 * NOTE: WE EXPECT THE FRONT-END TO INITIALIZE sc_regshift!
    199 	 */
    200 
    201 	/*
    202 	 * Setup the transmit threshold table.
    203 	 */
    204 	switch (sc->sc_chip) {
    205 	case TULIP_CHIP_DE425:
    206 	case TULIP_CHIP_21040:
    207 	case TULIP_CHIP_21041:
    208 		sc->sc_txth = tlp_10_txthresh_tab;
    209 		break;
    210 
    211 	default:
    212 		sc->sc_txth = tlp_10_100_txthresh_tab;
    213 		break;
    214 	}
    215 
    216 	/*
    217 	 * Setup the filter setup function.
    218 	 */
    219 	switch (sc->sc_chip) {
    220 	case TULIP_CHIP_WB89C840F:
    221 		sc->sc_filter_setup = tlp_winb_filter_setup;
    222 		break;
    223 
    224 	default:
    225 		sc->sc_filter_setup = tlp_filter_setup;
    226 		break;
    227 	}
    228 
    229 	/*
    230 	 * Set up the media status change function.
    231 	 */
    232 	switch (sc->sc_chip) {
    233 	case TULIP_CHIP_WB89C840F:
    234 		sc->sc_statchg = tlp_winb_mii_statchg;
    235 		break;
    236 
    237 	default:
    238 		/*
    239 		 * We may override this if we have special media
    240 		 * handling requirements (e.g. flipping GPIO pins).
    241 		 *
    242 		 * The pure-MII statchg function covers the basics.
    243 		 */
    244 		sc->sc_statchg = tlp_mii_statchg;
    245 		break;
    246 	}
    247 
    248 	/*
    249 	 * Set up various chip-specific quirks.
    250 	 */
    251 	switch (sc->sc_chip) {
    252 	case TULIP_CHIP_82C168:
    253 	case TULIP_CHIP_82C169:
    254 		/*
    255 		 * These chips seem to have busted DMA engines; just put them
    256 		 * in Store-and-Forward mode from the get-go.
    257 		 */
    258 		sc->sc_txthresh = TXTH_SF;
    259 		break;
    260 
    261 	case TULIP_CHIP_WB89C840F:
    262 		sc->sc_flags |= TULIPF_IC_FS;
    263 		break;
    264 
    265 	default:
    266 		/* Nothing. */
    267 	}
    268 
    269 	SIMPLEQ_INIT(&sc->sc_txfreeq);
    270 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
    271 
    272 	/*
    273 	 * Allocate the control data structures, and create and load the
    274 	 * DMA map for it.
    275 	 */
    276 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    277 	    sizeof(struct tulip_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
    278 	    0)) != 0) {
    279 		printf("%s: unable to allocate control data, error = %d\n",
    280 		    sc->sc_dev.dv_xname, error);
    281 		goto fail_0;
    282 	}
    283 
    284 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    285 	    sizeof(struct tulip_control_data), (caddr_t *)&sc->sc_control_data,
    286 	    BUS_DMA_COHERENT)) != 0) {
    287 		printf("%s: unable to map control data, error = %d\n",
    288 		    sc->sc_dev.dv_xname, error);
    289 		goto fail_1;
    290 	}
    291 
    292 	if ((error = bus_dmamap_create(sc->sc_dmat,
    293 	    sizeof(struct tulip_control_data), 1,
    294 	    sizeof(struct tulip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    295 		printf("%s: unable to create control data DMA map, "
    296 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    297 		goto fail_2;
    298 	}
    299 
    300 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    301 	    sc->sc_control_data, sizeof(struct tulip_control_data), NULL,
    302 	    0)) != 0) {
    303 		printf("%s: unable to load control data DMA map, error = %d\n",
    304 		    sc->sc_dev.dv_xname, error);
    305 		goto fail_3;
    306 	}
    307 
    308 	/*
    309 	 * Create the transmit buffer DMA maps.
    310 	 */
    311 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
    312 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    313 		    TULIP_NTXSEGS, MCLBYTES, 0, 0,
    314 		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
    315 			printf("%s: unable to create tx DMA map %d, "
    316 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    317 			goto fail_4;
    318 		}
    319 	}
    320 
    321 	/*
    322 	 * Create the recieve buffer DMA maps.
    323 	 */
    324 	for (i = 0; i < TULIP_NRXDESC; i++) {
    325 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    326 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    327 			printf("%s: unable to create rx DMA map %d, "
    328 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    329 			goto fail_5;
    330 		}
    331 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    332 	}
    333 
    334 	/*
    335 	 * Reset the chip to a known state.
    336 	 */
    337 	tlp_reset(sc);
    338 
    339 	/* Announce ourselves. */
    340 	printf("%s: %s%sEthernet address %s\n", sc->sc_dev.dv_xname,
    341 	    sc->sc_name[0] != '\0' ? sc->sc_name : "",
    342 	    sc->sc_name[0] != '\0' ? ", " : "",
    343 	    ether_sprintf(enaddr));
    344 
    345 	/*
    346 	 * Initialize our media structures.  This may probe the MII, if
    347 	 * present.
    348 	 */
    349 	(*sc->sc_mediasw->tmsw_init)(sc);
    350 
    351 	ifp = &sc->sc_ethercom.ec_if;
    352 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    353 	ifp->if_softc = sc;
    354 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    355 	ifp->if_ioctl = tlp_ioctl;
    356 	ifp->if_start = tlp_start;
    357 	ifp->if_watchdog = tlp_watchdog;
    358 
    359 	/*
    360 	 * Attach the interface.
    361 	 */
    362 	if_attach(ifp);
    363 	ether_ifattach(ifp, enaddr);
    364 #if NBPFILTER > 0
    365 	bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
    366 	    sizeof(struct ether_header));
    367 #endif
    368 
    369 	/*
    370 	 * Make sure the interface is shutdown during reboot.
    371 	 */
    372 	sc->sc_sdhook = shutdownhook_establish(tlp_shutdown, sc);
    373 	if (sc->sc_sdhook == NULL)
    374 		printf("%s: WARNING: unable to establish shutdown hook\n",
    375 		    sc->sc_dev.dv_xname);
    376 	return;
    377 
    378 	/*
    379 	 * Free any resources we've allocated during the failed attach
    380 	 * attempt.  Do this in reverse order and fall through.
    381 	 */
    382  fail_5:
    383 	for (i = 0; i < TULIP_NRXDESC; i++) {
    384 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    385 			bus_dmamap_destroy(sc->sc_dmat,
    386 			    sc->sc_rxsoft[i].rxs_dmamap);
    387 	}
    388  fail_4:
    389 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
    390 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
    391 			bus_dmamap_destroy(sc->sc_dmat,
    392 			    sc->sc_txsoft[i].txs_dmamap);
    393 	}
    394 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    395  fail_3:
    396 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    397  fail_2:
    398 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
    399 	    sizeof(struct tulip_control_data));
    400  fail_1:
    401 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    402  fail_0:
    403 	return;
    404 }
    405 
    406 /*
    407  * tlp_shutdown:
    408  *
    409  *	Make sure the interface is stopped at reboot time.
    410  */
    411 void
    412 tlp_shutdown(arg)
    413 	void *arg;
    414 {
    415 	struct tulip_softc *sc = arg;
    416 
    417 	tlp_stop(sc, 1);
    418 }
    419 
    420 /*
    421  * tlp_start:		[ifnet interface function]
    422  *
    423  *	Start packet transmission on the interface.
    424  */
    425 void
    426 tlp_start(ifp)
    427 	struct ifnet *ifp;
    428 {
    429 	struct tulip_softc *sc = ifp->if_softc;
    430 	struct mbuf *m0, *m;
    431 	struct tulip_txsoft *txs, *last_txs;
    432 	bus_dmamap_t dmamap;
    433 	int error, firsttx, nexttx, lasttx, ofree, seg;
    434 
    435 	DPRINTF(sc, ("%s: tlp_start: sc_flags 0x%08x, if_flags 0x%08x\n",
    436 	    sc->sc_dev.dv_xname, sc->sc_flags, ifp->if_flags));
    437 
    438 	/*
    439 	 * If we want a filter setup, it means no more descriptors were
    440 	 * available for the setup routine.  Let it get a chance to wedge
    441 	 * itself into the ring.
    442 	 */
    443 	if (sc->sc_flags & TULIPF_WANT_SETUP)
    444 		ifp->if_flags |= IFF_OACTIVE;
    445 
    446 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    447 		return;
    448 
    449 	/*
    450 	 * Remember the previous number of free descriptors and
    451 	 * the first descriptor we'll use.
    452 	 */
    453 	ofree = sc->sc_txfree;
    454 	firsttx = sc->sc_txnext;
    455 
    456 	DPRINTF(sc, ("%s: tlp_start: txfree %d, txnext %d\n",
    457 	    sc->sc_dev.dv_xname, ofree, firsttx));
    458 
    459 	/*
    460 	 * Loop through the send queue, setting up transmit descriptors
    461 	 * until we drain the queue, or use up all available transmit
    462 	 * descriptors.
    463 	 */
    464 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
    465 	       sc->sc_txfree != 0) {
    466 		/*
    467 		 * Grab a packet off the queue.
    468 		 */
    469 		IF_DEQUEUE(&ifp->if_snd, m0);
    470 		if (m0 == NULL)
    471 			break;
    472 
    473 		dmamap = txs->txs_dmamap;
    474 
    475 		/*
    476 		 * Load the DMA map.  If this fails, the packet either
    477 		 * didn't fit in the alloted number of segments, or we were
    478 		 * short on resources.  In this case, we'll copy and try
    479 		 * again.
    480 		 */
    481 		if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    482 		    BUS_DMA_NOWAIT) != 0) {
    483 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    484 			if (m == NULL) {
    485 				printf("%s: unable to allocate Tx mbuf\n",
    486 				    sc->sc_dev.dv_xname);
    487 				IF_PREPEND(&ifp->if_snd, m0);
    488 				break;
    489 			}
    490 			if (m0->m_pkthdr.len > MHLEN) {
    491 				MCLGET(m, M_DONTWAIT);
    492 				if ((m->m_flags & M_EXT) == 0) {
    493 					printf("%s: unable to allocate Tx "
    494 					    "cluster\n", sc->sc_dev.dv_xname);
    495 					m_freem(m);
    496 					IF_PREPEND(&ifp->if_snd, m0);
    497 					break;
    498 				}
    499 			}
    500 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
    501 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    502 			m_freem(m0);
    503 			m0 = m;
    504 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    505 			    m0, BUS_DMA_NOWAIT);
    506 			if (error) {
    507 				printf("%s: unable to load Tx buffer, "
    508 				    "error = %d\n", sc->sc_dev.dv_xname, error);
    509 				IF_PREPEND(&ifp->if_snd, m0);
    510 				break;
    511 			}
    512 		}
    513 
    514 		/*
    515 		 * Ensure we have enough descriptors free to describe
    516 		 * the packet.
    517 		 */
    518 		if (dmamap->dm_nsegs > sc->sc_txfree) {
    519 			/*
    520 			 * Not enough free descriptors to transmit this
    521 			 * packet.  We haven't committed to anything yet,
    522 			 * so just unload the DMA map, put the packet
    523 			 * back on the queue, and punt.  Notify the upper
    524 			 * layer that there are no more slots left.
    525 			 *
    526 			 * XXX We could allocate an mbuf and copy, but
    527 			 * XXX it is worth it?
    528 			 */
    529 			ifp->if_flags |= IFF_OACTIVE;
    530 			bus_dmamap_unload(sc->sc_dmat, dmamap);
    531 			IF_PREPEND(&ifp->if_snd, m0);
    532 			break;
    533 		}
    534 
    535 		/*
    536 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    537 		 */
    538 
    539 		/* Sync the DMA map. */
    540 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    541 		    BUS_DMASYNC_PREWRITE);
    542 
    543 		/*
    544 		 * Initialize the transmit descriptors.
    545 		 */
    546 		for (nexttx = sc->sc_txnext, seg = 0;
    547 		     seg < dmamap->dm_nsegs;
    548 		     seg++, nexttx = TULIP_NEXTTX(nexttx)) {
    549 			/*
    550 			 * If this is the first descriptor we're
    551 			 * enqueueing, don't set the OWN bit just
    552 			 * yet.  That could cause a race condition.
    553 			 * We'll do it below.
    554 			 */
    555 			sc->sc_txdescs[nexttx].td_status =
    556 			    (nexttx == firsttx) ? 0 : TDSTAT_OWN;
    557 			sc->sc_txdescs[nexttx].td_bufaddr1 =
    558 			    dmamap->dm_segs[seg].ds_addr;
    559 			sc->sc_txdescs[nexttx].td_ctl =
    560 			    (dmamap->dm_segs[seg].ds_len << TDCTL_SIZE1_SHIFT) |
    561 			    TDCTL_CH;
    562 			lasttx = nexttx;
    563 		}
    564 
    565 		/* Set `first segment' and `last segment' appropriately. */
    566 		sc->sc_txdescs[sc->sc_txnext].td_ctl |= TDCTL_Tx_FS;
    567 		sc->sc_txdescs[lasttx].td_ctl |= TDCTL_Tx_LS;
    568 
    569 #ifdef TLP_DEBUG
    570 		if (ifp->if_flags & IFF_DEBUG) {
    571 			printf("     txsoft %p trainsmit chain:\n", txs);
    572 			for (seg = sc->sc_txnext;; seg = TULIP_NEXTTX(seg)) {
    573 				printf("     descriptor %d:\n", seg);
    574 				printf("       td_status:   0x%08x\n",
    575 				    sc->sc_txdescs[seg].td_status);
    576 				printf("       td_ctl:      0x%08x\n",
    577 				    sc->sc_txdescs[seg].td_ctl);
    578 				printf("       td_bufaddr1: 0x%08x\n",
    579 				    sc->sc_txdescs[seg].td_bufaddr1);
    580 				printf("       td_bufaddr2: 0x%08x\n",
    581 				    sc->sc_txdescs[seg].td_bufaddr2);
    582 				if (seg == lasttx)
    583 					break;
    584 			}
    585 		}
    586 #endif
    587 
    588 		/* Sync the descriptors we're using. */
    589 		TULIP_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
    590 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    591 
    592 		/*
    593 		 * Store a pointer to the packet so we can free it later,
    594 		 * and remember what txdirty will be once the packet is
    595 		 * done.
    596 		 */
    597 		txs->txs_mbuf = m0;
    598 		txs->txs_firstdesc = sc->sc_txnext;
    599 		txs->txs_lastdesc = lasttx;
    600 
    601 		/* Advance the tx pointer. */
    602 		sc->sc_txfree -= dmamap->dm_nsegs;
    603 		sc->sc_txnext = nexttx;
    604 
    605 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
    606 		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
    607 
    608 		last_txs = txs;
    609 
    610 #if NBPFILTER > 0
    611 		/*
    612 		 * Pass the packet to any BPF listeners.
    613 		 */
    614 		if (ifp->if_bpf)
    615 			bpf_mtap(ifp->if_bpf, m0);
    616 #endif /* NBPFILTER > 0 */
    617 	}
    618 
    619 	if (txs == NULL || sc->sc_txfree == 0) {
    620 		/* No more slots left; notify upper layer. */
    621 		ifp->if_flags |= IFF_OACTIVE;
    622 	}
    623 
    624 	if (sc->sc_txfree != ofree) {
    625 		DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
    626 		    sc->sc_dev.dv_xname, lasttx, firsttx));
    627 		/*
    628 		 * Cause a transmit interrupt to happen on the
    629 		 * last packet we enqueued.
    630 		 */
    631 		sc->sc_txdescs[lasttx].td_ctl |= TDCTL_Tx_IC;
    632 		TULIP_CDTXSYNC(sc, lasttx, 1,
    633 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    634 
    635 		/*
    636 		 * Some clone chips want IC on the *first* segment in
    637 		 * the packet.  Appease them.
    638 		 */
    639 		if ((sc->sc_flags & TULIPF_IC_FS) != 0 &&
    640 		    last_txs->txs_firstdesc != lasttx) {
    641 			sc->sc_txdescs[last_txs->txs_firstdesc].td_ctl |=
    642 			    TDCTL_Tx_IC;
    643 			TULIP_CDTXSYNC(sc, last_txs->txs_firstdesc, 1,
    644 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    645 		}
    646 
    647 		/*
    648 		 * The entire packet chain is set up.  Give the
    649 		 * first descriptor to the chip now.
    650 		 */
    651 		sc->sc_txdescs[firsttx].td_status |= TDSTAT_OWN;
    652 		TULIP_CDTXSYNC(sc, firsttx, 1,
    653 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    654 
    655 		/* Wake up the transmitter. */
    656 		/* XXX USE AUTOPOLLING? */
    657 		TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
    658 
    659 		/* Set a watchdog timer in case the chip flakes out. */
    660 		ifp->if_timer = 5;
    661 	}
    662 }
    663 
    664 /*
    665  * tlp_watchdog:	[ifnet interface function]
    666  *
    667  *	Watchdog timer handler.
    668  */
    669 void
    670 tlp_watchdog(ifp)
    671 	struct ifnet *ifp;
    672 {
    673 	struct tulip_softc *sc = ifp->if_softc;
    674 	int doing_setup, doing_transmit;
    675 
    676 	doing_setup = (sc->sc_flags & TULIPF_DOING_SETUP);
    677 	doing_transmit = (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL);
    678 
    679 	if (doing_setup && doing_transmit) {
    680 		printf("%s: filter setup and transmit timeout\n",
    681 		    sc->sc_dev.dv_xname);
    682 		ifp->if_oerrors++;
    683 	} else if (doing_transmit) {
    684 		printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
    685 		ifp->if_oerrors++;
    686 	} else if (doing_setup)
    687 		printf("%s: filter setup timeout\n", sc->sc_dev.dv_xname);
    688 	else
    689 		printf("%s: spurious watchdog timeout\n", sc->sc_dev.dv_xname);
    690 
    691 	(void) tlp_init(sc);
    692 
    693 	/* Try to get more packets going. */
    694 	tlp_start(ifp);
    695 }
    696 
    697 /*
    698  * tlp_ioctl:		[ifnet interface function]
    699  *
    700  *	Handle control requests from the operator.
    701  */
    702 int
    703 tlp_ioctl(ifp, cmd, data)
    704 	struct ifnet *ifp;
    705 	u_long cmd;
    706 	caddr_t data;
    707 {
    708 	struct tulip_softc *sc = ifp->if_softc;
    709 	struct ifreq *ifr = (struct ifreq *)data;
    710 	struct ifaddr *ifa = (struct ifaddr *)data;
    711 	int s, error = 0;
    712 
    713 	s = splnet();
    714 
    715 	switch (cmd) {
    716 	case SIOCSIFADDR:
    717 		ifp->if_flags |= IFF_UP;
    718 
    719 		switch (ifa->ifa_addr->sa_family) {
    720 #ifdef INET
    721 		case AF_INET:
    722 			if ((error = tlp_init(sc)) != 0)
    723 				break;
    724 			arp_ifinit(ifp, ifa);
    725 			break;
    726 #endif /* INET */
    727 #ifdef NS
    728 		case AF_NS:
    729 		    {
    730 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    731 
    732 			if (ns_nullhost(*ina))
    733 				ina->x_host = *(union ns_host *)
    734 				    LLADDR(ifp->if_sadl);
    735 			else
    736 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
    737 				    ifp->if_addrlen);
    738 			/* Set new address. */
    739 			error = tlp_init(sc);
    740 			break;
    741 		    }
    742 #endif /* NS */
    743 		default:
    744 			error = tlp_init(sc);
    745 			break;
    746 		}
    747 		break;
    748 
    749 	case SIOCSIFMTU:
    750 		if (ifr->ifr_mtu > ETHERMTU)
    751 			error = EINVAL;
    752 		else
    753 			ifp->if_mtu = ifr->ifr_mtu;
    754 		break;
    755 
    756 	case SIOCSIFFLAGS:
    757 		if ((ifp->if_flags & IFF_UP) == 0 &&
    758 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    759 			/*
    760 			 * If interface is marked down and it is running, then
    761 			 * stop it.
    762 			 */
    763 			tlp_stop(sc, 1);
    764 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    765 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    766 			/*
    767 			 * If interfase it marked up and it is stopped, then
    768 			 * start it.
    769 			 */
    770 			error = tlp_init(sc);
    771 		} else if ((ifp->if_flags & IFF_UP) != 0) {
    772 			/*
    773 			 * Reset the interface to pick up changes in any other
    774 			 * flags that affect the hardware state.
    775 			 */
    776 			error = tlp_init(sc);
    777 		}
    778 		break;
    779 
    780 	case SIOCADDMULTI:
    781 	case SIOCDELMULTI:
    782 		error = (cmd == SIOCADDMULTI) ?
    783 		    ether_addmulti(ifr, &sc->sc_ethercom) :
    784 		    ether_delmulti(ifr, &sc->sc_ethercom);
    785 
    786 		if (error == ENETRESET) {
    787 			/*
    788 			 * Multicast list has changed.  Set the filter
    789 			 * accordingly.
    790 			 */
    791 			(*sc->sc_filter_setup)(sc);
    792 			error = 0;
    793 		}
    794 		break;
    795 
    796 	case SIOCSIFMEDIA:
    797 	case SIOCGIFMEDIA:
    798 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
    799 		break;
    800 
    801 	default:
    802 		error = EINVAL;
    803 		break;
    804 	}
    805 
    806 	/* Try to get more packets going. */
    807 	tlp_start(ifp);
    808 
    809 	splx(s);
    810 	return (error);
    811 }
    812 
    813 /*
    814  * tlp_intr:
    815  *
    816  *	Interrupt service routine.
    817  */
    818 int
    819 tlp_intr(arg)
    820 	void *arg;
    821 {
    822 	struct tulip_softc *sc = arg;
    823 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    824 	u_int32_t status, rxstatus, txstatus;
    825 	int handled = 0, txthresh;
    826 
    827 	DPRINTF(sc, ("%s: tlp_intr\n", sc->sc_dev.dv_xname));
    828 
    829 	/*
    830 	 * If the interface isn't running, the interrupt couldn't
    831 	 * possibly have come from us.
    832 	 */
    833 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    834 		return (0);
    835 
    836 	for (;;) {
    837 		status = TULIP_READ(sc, CSR_STATUS);
    838 		if (status)
    839 			TULIP_WRITE(sc, CSR_STATUS, status);
    840 
    841 		if ((status & sc->sc_inten) == 0)
    842 			break;
    843 
    844 		handled = 1;
    845 
    846 		rxstatus = status & sc->sc_rxint_mask;
    847 		txstatus = status & sc->sc_txint_mask;
    848 
    849 		if (rxstatus) {
    850 			/* Grab new any new packets. */
    851 			tlp_rxintr(sc);
    852 
    853 			if (rxstatus & STATUS_RWT)
    854 				printf("%s: receive watchdog timeout\n",
    855 				    sc->sc_dev.dv_xname);
    856 
    857 			if (rxstatus & STATUS_RU) {
    858 				printf("%s: receive ring overrun\n",
    859 				    sc->sc_dev.dv_xname);
    860 				/* Get the receive process going again. */
    861 				tlp_idle(sc, OPMODE_SR);
    862 				TULIP_WRITE(sc, CSR_RXLIST,
    863 				    TULIP_CDRXADDR(sc, sc->sc_rxptr));
    864 				TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
    865 				TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
    866 				break;
    867 			}
    868 		}
    869 
    870 		if (txstatus) {
    871 			/* Sweep up transmit descriptors. */
    872 			tlp_txintr(sc);
    873 
    874 			if (txstatus & STATUS_TJT)
    875 				printf("%s: transmit jabber timeout\n",
    876 				    sc->sc_dev.dv_xname);
    877 
    878 			if (txstatus & STATUS_UNF) {
    879 				/*
    880 				 * Increase our transmit threshold if
    881 				 * another is available.
    882 				 */
    883 				txthresh = sc->sc_txthresh + 1;
    884 				if (sc->sc_txth[txthresh].txth_name != NULL) {
    885 					/* Idle the transmit process. */
    886 					tlp_idle(sc, OPMODE_ST);
    887 
    888 					sc->sc_txthresh = txthresh;
    889 					sc->sc_opmode &= ~(OPMODE_TR|OPMODE_SF);
    890 					sc->sc_opmode |=
    891 					    sc->sc_txth[txthresh].txth_opmode;
    892 					printf("%s: transmit underrun; new "
    893 					    "threshold: %s\n",
    894 					    sc->sc_dev.dv_xname,
    895 					    sc->sc_txth[txthresh].txth_name);
    896 
    897 					/*
    898 					 * Set the new threshold and restart
    899 					 * the transmit process.
    900 					 */
    901 					TULIP_WRITE(sc, CSR_OPMODE,
    902 					    sc->sc_opmode);
    903 				}
    904 					/*
    905 					 * XXX Log every Nth underrun from
    906 					 * XXX now on?
    907 					 */
    908 			}
    909 		}
    910 
    911 		if (status & (STATUS_TPS|STATUS_RPS)) {
    912 			if (status & STATUS_TPS)
    913 				printf("%s: transmit process stopped\n",
    914 				    sc->sc_dev.dv_xname);
    915 			if (status & STATUS_RPS)
    916 				printf("%s: receive process stopped\n",
    917 				    sc->sc_dev.dv_xname);
    918 			(void) tlp_init(sc);
    919 			break;
    920 		}
    921 
    922 		if (status & STATUS_SE) {
    923 			const char *str;
    924 			switch (status & STATUS_EB) {
    925 			case STATUS_EB_PARITY:
    926 				str = "parity error";
    927 				break;
    928 
    929 			case STATUS_EB_MABT:
    930 				str = "master abort";
    931 				break;
    932 
    933 			case STATUS_EB_TABT:
    934 				str = "target abort";
    935 				break;
    936 
    937 			default:
    938 				str = "unknown error";
    939 				break;
    940 			}
    941 			printf("%s: fatal system error: %s\n",
    942 			    sc->sc_dev.dv_xname, str);
    943 			(void) tlp_init(sc);
    944 			break;
    945 		}
    946 
    947 		/*
    948 		 * Not handled:
    949 		 *
    950 		 *	Transmit buffer unavailable -- normal
    951 		 *	condition, nothing to do, really.
    952 		 *
    953 		 *	General purpose timer experied -- we don't
    954 		 *	use the general purpose timer.
    955 		 *
    956 		 *	Early receive interrupt -- not available on
    957 		 *	all chips, we just use RI.  We also only
    958 		 *	use single-segment receive DMA, so this
    959 		 *	is mostly useless.
    960 		 */
    961 	}
    962 
    963 	/* Try to get more packets going. */
    964 	tlp_start(ifp);
    965 
    966 	return (handled);
    967 }
    968 
    969 /*
    970  * tlp_rxintr:
    971  *
    972  *	Helper; handle receive interrupts.
    973  */
    974 void
    975 tlp_rxintr(sc)
    976 	struct tulip_softc *sc;
    977 {
    978 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    979 	struct ether_header *eh;
    980 	struct tulip_rxsoft *rxs;
    981 	struct mbuf *m;
    982 	u_int32_t rxstat;
    983 	int i, len;
    984 
    985 	for (i = sc->sc_rxptr;; i = TULIP_NEXTRX(i)) {
    986 		rxs = &sc->sc_rxsoft[i];
    987 
    988 		TULIP_CDRXSYNC(sc, i,
    989 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    990 
    991 		rxstat = sc->sc_rxdescs[i].td_status;
    992 
    993 		if (rxstat & TDSTAT_OWN) {
    994 			/*
    995 			 * We have processed all of the receive buffers.
    996 			 */
    997 			break;
    998 		}
    999 
   1000 		/*
   1001 		 * Make sure the packet fit in one buffer.  This should
   1002 		 * always be the case.  But the Lite-On PNIC, rev 33
   1003 		 * has an awful receive engine bug, which may require
   1004 		 * a very icky work-around.
   1005 		 */
   1006 		if ((rxstat & (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) !=
   1007 		    (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) {
   1008 			printf("%s: incoming packet spilled, resetting\n",
   1009 			    sc->sc_dev.dv_xname);
   1010 			(void) tlp_init(sc);
   1011 			return;
   1012 		}
   1013 
   1014 		/*
   1015 		 * If any collisions were seen on the wire, count one.
   1016 		 */
   1017 		if (rxstat & TDSTAT_Rx_CS)
   1018 			ifp->if_collisions++;
   1019 
   1020 		/*
   1021 		 * If an error occured, update stats, clear the status
   1022 		 * word, and leave the packet buffer in place.  It will
   1023 		 * simply be reused the next time the ring comes around.
   1024 		 */
   1025 		if (rxstat & TDSTAT_ES) {
   1026 #define	PRINTERR(bit, str)						\
   1027 			if (rxstat & (bit))				\
   1028 				printf("%s: receive error: %s\n",	\
   1029 				    sc->sc_dev.dv_xname, str)
   1030 			ifp->if_ierrors++;
   1031 			PRINTERR(TDSTAT_Rx_DE, "descriptor error");
   1032 			PRINTERR(TDSTAT_Rx_RF, "runt frame");
   1033 			PRINTERR(TDSTAT_Rx_TL, "frame too long");
   1034 			PRINTERR(TDSTAT_Rx_RE, "MII error");
   1035 			PRINTERR(TDSTAT_Rx_DB, "dribbling bit");
   1036 			PRINTERR(TDSTAT_Rx_CE, "CRC error");
   1037 #undef PRINTERR
   1038 			TULIP_INIT_RXDESC(sc, i);
   1039 			continue;
   1040 		}
   1041 
   1042 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1043 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1044 
   1045 		/*
   1046 		 * No errors; receive the packet.  Note the Tulip
   1047 		 * includes the CRC with every packet; trim it.
   1048 		 */
   1049 		len = TDSTAT_Rx_LENGTH(rxstat) - ETHER_CRC_LEN;
   1050 
   1051 #ifdef __NO_STRICT_ALIGNMENT
   1052 		/*
   1053 		 * Allocate a new mbuf cluster.  If that fails, we are
   1054 		 * out of memory, and must drop the packet and recycle
   1055 		 * the buffer that's already attached to this descriptor.
   1056 		 */
   1057 		m = rxs->rxs_mbuf;
   1058 		if (tlp_add_rxbuf(sc, i) != 0) {
   1059 			ifp->if_ierrors++;
   1060 			TULIP_INIT_RXDESC(sc, i);
   1061 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1062 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1063 			continue;
   1064 		}
   1065 #else
   1066 		/*
   1067 		 * The Tulip's receive buffers must be 4-byte aligned.
   1068 		 * But this means that the data after the Ethernet header
   1069 		 * is misaligned.  We must allocate a new buffer and
   1070 		 * copy the data, shifted forward 2 bytes.
   1071 		 */
   1072 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1073 		if (m == NULL) {
   1074  dropit:
   1075 			ifp->if_ierrors++;
   1076 			TULIP_INIT_RXDESC(sc, i);
   1077 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1078 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1079 			continue;
   1080 		}
   1081 		if (len > (MHLEN - 2)) {
   1082 			MCLGET(m, M_DONTWAIT);
   1083 			if ((m->m_flags & M_EXT) == 0) {
   1084 				m_freem(m);
   1085 				goto dropit;
   1086 			}
   1087 		}
   1088 		m->m_data += 2;
   1089 
   1090 		/*
   1091 		 * Note that we use clusters for incoming frames, so the
   1092 		 * buffer is virtually contiguous.
   1093 		 */
   1094 		memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
   1095 
   1096 		/* Allow the receive descriptor to continue using its mbuf. */
   1097 		TULIP_INIT_RXDESC(sc, i);
   1098 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1099 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1100 #endif /* __NO_STRICT_ALIGNMENT */
   1101 
   1102 		ifp->if_ipackets++;
   1103 		eh = mtod(m, struct ether_header *);
   1104 		m->m_pkthdr.rcvif = ifp;
   1105 		m->m_pkthdr.len = m->m_len = len;
   1106 
   1107 #if NBPFILTER > 0
   1108 		/*
   1109 		 * Pass this up to any BPF listeners, but only
   1110 		 * pass it up the stack if its for us.
   1111 		 */
   1112 		if (ifp->if_bpf)
   1113 			bpf_mtap(ifp->if_bpf, m);
   1114 #endif /* NPBFILTER > 0 */
   1115 
   1116 		/*
   1117 		 * This test is outside the NBPFILTER block because
   1118 		 * on the 21140 we have to use Hash-Only mode due to
   1119 		 * a bug in the filter logic.
   1120 		 */
   1121 		if ((ifp->if_flags & IFF_PROMISC) != 0 ||
   1122 		    sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
   1123 			if (memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
   1124 				 ETHER_ADDR_LEN) != 0 &&
   1125 			    ETHER_IS_MULTICAST(eh->ether_dhost) == 0) {
   1126 				m_freem(m);
   1127 				continue;
   1128 			}
   1129 		}
   1130 
   1131 		/* Pass it on. */
   1132 		(*ifp->if_input)(ifp, m);
   1133 	}
   1134 
   1135 	/* Update the recieve pointer. */
   1136 	sc->sc_rxptr = i;
   1137 }
   1138 
   1139 /*
   1140  * tlp_txintr:
   1141  *
   1142  *	Helper; handle transmit interrupts.
   1143  */
   1144 void
   1145 tlp_txintr(sc)
   1146 	struct tulip_softc *sc;
   1147 {
   1148 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1149 	struct tulip_txsoft *txs;
   1150 	u_int32_t txstat;
   1151 
   1152 	DPRINTF(sc, ("%s: tlp_txintr: sc_flags 0x%08x\n",
   1153 	    sc->sc_dev.dv_xname, sc->sc_flags));
   1154 
   1155 	ifp->if_flags &= ~IFF_OACTIVE;
   1156 
   1157 	/*
   1158 	 * If we were doing a filter setup, check to see if it completed.
   1159 	 */
   1160 	if (sc->sc_flags & TULIPF_DOING_SETUP) {
   1161 		TULIP_CDSDSYNC(sc, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1162 		if ((sc->sc_setup_desc.td_status & TDSTAT_OWN) == 0)
   1163 			sc->sc_flags &= ~TULIPF_DOING_SETUP;
   1164 	}
   1165 
   1166 	/*
   1167 	 * Go through our Tx list and free mbufs for those
   1168 	 * frames that have been transmitted.
   1169 	 */
   1170 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1171 		TULIP_CDTXSYNC(sc, txs->txs_firstdesc,
   1172 		    txs->txs_dmamap->dm_nsegs,
   1173 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1174 
   1175 #ifdef TLP_DEBUG
   1176 		if (ifp->if_flags & IFF_DEBUG) {
   1177 			int i;
   1178 			printf("    txsoft %p trainsmit chain:\n", txs);
   1179 			for (i = txs->txs_firstdesc;; i = TULIP_NEXTTX(i)) {
   1180 				printf("     descriptor %d:\n", i);
   1181 				printf("       td_status:   0x%08x\n",
   1182 				    sc->sc_txdescs[i].td_status);
   1183 				printf("       td_ctl:      0x%08x\n",
   1184 				    sc->sc_txdescs[i].td_ctl);
   1185 				printf("       td_bufaddr1: 0x%08x\n",
   1186 				    sc->sc_txdescs[i].td_bufaddr1);
   1187 				printf("       td_bufaddr2: 0x%08x\n",
   1188 				    sc->sc_txdescs[i].td_bufaddr2);
   1189 				if (i == txs->txs_lastdesc)
   1190 					break;
   1191 			}
   1192 		}
   1193 #endif
   1194 
   1195 		txstat = sc->sc_txdescs[txs->txs_firstdesc].td_status;
   1196 		if (txstat & TDSTAT_OWN)
   1197 			break;
   1198 
   1199 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
   1200 
   1201 		sc->sc_txfree += txs->txs_dmamap->dm_nsegs;
   1202 
   1203 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   1204 		    0, txs->txs_dmamap->dm_mapsize,
   1205 		    BUS_DMASYNC_POSTWRITE);
   1206 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1207 		m_freem(txs->txs_mbuf);
   1208 		txs->txs_mbuf = NULL;
   1209 
   1210 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1211 
   1212 		/*
   1213 		 * Check for errors and collisions.
   1214 		 */
   1215 		if (txstat &
   1216 		    (TDSTAT_Tx_UF|TDSTAT_Tx_NC|TDSTAT_Tx_LO|TDSTAT_Tx_TO)) {
   1217 			ifp->if_oerrors++;
   1218 #if 0
   1219 			/*
   1220 			 * XXX Can't check for late or excessive collisions;
   1221 			 * XXX Some 21040s seem to register those even on
   1222 			 * XXX successful transmissions!
   1223 			 */
   1224 			if (txstat & TDSTAT_Tx_EC)
   1225 				ifp->if_collisions += 16;
   1226 			if (txstat & TDSTAT_Tx_LC)
   1227 				ifp->if_collisions++;
   1228 #endif
   1229 		} else {
   1230 			/* Packet was transmitted successfully. */
   1231 			ifp->if_opackets++;
   1232 			ifp->if_collisions += TDSTAT_Tx_COLLISIONS(txstat);
   1233 		}
   1234 	}
   1235 
   1236 	/*
   1237 	 * If there are no more pending transmissions, cancel the watchdog
   1238 	 * timer.
   1239 	 */
   1240 	if (txs == NULL && (sc->sc_flags & TULIPF_DOING_SETUP) == 0)
   1241 		ifp->if_timer = 0;
   1242 
   1243 	/*
   1244 	 * If we have a receive filter setup pending, do it now.
   1245 	 */
   1246 	if (sc->sc_flags & TULIPF_WANT_SETUP)
   1247 		(*sc->sc_filter_setup)(sc);
   1248 }
   1249 
   1250 /*
   1251  * tlp_reset:
   1252  *
   1253  *	Perform a soft reset on the Tulip.
   1254  */
   1255 void
   1256 tlp_reset(sc)
   1257 	struct tulip_softc *sc;
   1258 {
   1259 	int i;
   1260 
   1261 	TULIP_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR);
   1262 
   1263 	for (i = 0; i < 1000; i++) {
   1264 		if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0)
   1265 			break;
   1266 		delay(10);
   1267 	}
   1268 
   1269 	if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR))
   1270 		printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
   1271 
   1272 	delay(1000);
   1273 }
   1274 
   1275 /*
   1276  * tlp_init:
   1277  *
   1278  *	Initialize the interface.  Must be called at splnet().
   1279  */
   1280 int
   1281 tlp_init(sc)
   1282 	struct tulip_softc *sc;
   1283 {
   1284 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1285 	struct tulip_txsoft *txs;
   1286 	struct tulip_rxsoft *rxs;
   1287 	int i, error = 0;
   1288 
   1289 	/*
   1290 	 * Cancel any pending I/O.
   1291 	 */
   1292 	tlp_stop(sc, 0);
   1293 
   1294 	/*
   1295 	 * Reset the Tulip to a known state.
   1296 	 */
   1297 	tlp_reset(sc);
   1298 
   1299 	/*
   1300 	 * Initialize the BUSMODE register.
   1301 	 *
   1302 	 * XXX What about read-multiple/read-line/write-line on
   1303 	 * XXX the 21140 and up?
   1304 	 */
   1305 	sc->sc_busmode = BUSMODE_BAR | BUSMODE_PBL_DEFAULT;
   1306 	switch (sc->sc_cacheline) {
   1307 	default:
   1308 		/*
   1309 		 * Note: We must *always* set these bits; a cache
   1310 		 * alignment of 0 is RESERVED.
   1311 		 */
   1312 	case 8:
   1313 		sc->sc_busmode |= BUSMODE_CAL_8LW;
   1314 		break;
   1315 	case 16:
   1316 		sc->sc_busmode |= BUSMODE_CAL_16LW;
   1317 		break;
   1318 	case 32:
   1319 		sc->sc_busmode |= BUSMODE_CAL_32LW;
   1320 		break;
   1321 	}
   1322 	switch (sc->sc_chip) {
   1323 	case TULIP_CHIP_82C168:
   1324 	case TULIP_CHIP_82C169:
   1325 		sc->sc_busmode |= BUSMODE_PNIC_MBO;
   1326 		break;
   1327 	default:
   1328 		/* Nothing. */
   1329 		break;
   1330 	}
   1331 #if BYTE_ORDER == BIG_ENDIAN
   1332 	/*
   1333 	 * XXX There are reports that this doesn't work properly
   1334 	 * in the old Tulip driver, but BUSMODE_DBO does.  However,
   1335 	 * BUSMODE_DBO is not available on the 21040, and requires
   1336 	 * us to byte-swap the setup packet.  What to do?
   1337 	 */
   1338 	sc->sc_busmode |= BUSMODE_BLE;
   1339 #endif
   1340 	TULIP_WRITE(sc, CSR_BUSMODE, sc->sc_busmode);
   1341 
   1342 	/*
   1343 	 * Initialize the OPMODE register.  We don't write it until
   1344 	 * we're ready to begin the transmit and receive processes.
   1345 	 *
   1346 	 * Media-related OPMODE bits are set in the media callbacks
   1347 	 * for each specific chip/board.
   1348 	 */
   1349 	sc->sc_opmode = OPMODE_SR | OPMODE_ST |
   1350 	    sc->sc_txth[sc->sc_txthresh].txth_opmode;
   1351 	switch (sc->sc_chip) {
   1352 	case TULIP_CHIP_21140:
   1353 	case TULIP_CHIP_21140A:
   1354 	case TULIP_CHIP_21142:
   1355 	case TULIP_CHIP_21143:
   1356 		sc->sc_opmode |= OPMODE_MBO;
   1357 		break;
   1358 
   1359 	default:
   1360 		/* Nothing. */
   1361 	}
   1362 
   1363 	if (sc->sc_flags & TULIPF_HAS_MII) {
   1364 		switch (sc->sc_chip) {
   1365 		case TULIP_CHIP_82C168:
   1366 		case TULIP_CHIP_82C169:
   1367 			/* Enable the MII port. */
   1368 			sc->sc_opmode |= OPMODE_PS;
   1369 
   1370 			TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);
   1371 			break;
   1372 
   1373 		case TULIP_CHIP_WB89C840F:
   1374 			/* Nothing. */
   1375 			break;
   1376 
   1377 		default:
   1378 			/* Enable the MII port. */
   1379 			sc->sc_opmode |= OPMODE_PS;
   1380 			break;
   1381 		}
   1382 	} else {
   1383 		switch (sc->sc_chip) {
   1384 		case TULIP_CHIP_82C168:
   1385 		case TULIP_CHIP_82C169:
   1386 			sc->sc_opmode |= OPMODE_PNIC_TBEN;
   1387 			break;
   1388 
   1389 		default:
   1390 			/* Nothing. */
   1391 		}
   1392 	}
   1393 
   1394 	/*
   1395 	 * Magical mystery initialization on the Macronix chips.
   1396 	 * The MX98713 uses its own magic value, the rest share
   1397 	 * a common one.
   1398 	 */
   1399 	switch (sc->sc_chip) {
   1400 	case TULIP_CHIP_MX98713:
   1401 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98713);
   1402 		break;
   1403 
   1404 	case TULIP_CHIP_MX98713A:
   1405 	case TULIP_CHIP_MX98715:
   1406 	case TULIP_CHIP_MX98725:
   1407 		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98715);
   1408 		break;
   1409 
   1410 	default:
   1411 		/* Nothing. */
   1412 	}
   1413 
   1414 	/*
   1415 	 * Initialize the transmit descriptor ring.
   1416 	 */
   1417 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
   1418 	for (i = 0; i < TULIP_NTXDESC; i++) {
   1419 		sc->sc_txdescs[i].td_ctl = TDCTL_CH;
   1420 		sc->sc_txdescs[i].td_bufaddr2 =
   1421 		    TULIP_CDTXADDR(sc, TULIP_NEXTTX(i));
   1422 	}
   1423 	TULIP_CDTXSYNC(sc, 0, TULIP_NTXDESC,
   1424 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1425 	sc->sc_txfree = TULIP_NTXDESC;
   1426 	sc->sc_txnext = 0;
   1427 
   1428 	/*
   1429 	 * Initialize the transmit job descriptors.
   1430 	 */
   1431 	SIMPLEQ_INIT(&sc->sc_txfreeq);
   1432 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
   1433 	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
   1434 		txs = &sc->sc_txsoft[i];
   1435 		txs->txs_mbuf = NULL;
   1436 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1437 	}
   1438 
   1439 	/*
   1440 	 * Initialize the receive descriptor and receive job
   1441 	 * descriptor rings.
   1442 	 */
   1443 	for (i = 0; i < TULIP_NRXDESC; i++) {
   1444 		rxs = &sc->sc_rxsoft[i];
   1445 		if (rxs->rxs_mbuf == NULL) {
   1446 			if ((error = tlp_add_rxbuf(sc, i)) != 0) {
   1447 				printf("%s: unable to allocate or map rx "
   1448 				    "buffer %d, error = %d\n",
   1449 				    sc->sc_dev.dv_xname, i, error);
   1450 				/*
   1451 				 * XXX Should attempt to run with fewer receive
   1452 				 * XXX buffers instead of just failing.
   1453 				 */
   1454 				tlp_rxdrain(sc);
   1455 				goto out;
   1456 			}
   1457 		}
   1458 	}
   1459 	sc->sc_rxptr = 0;
   1460 
   1461 	/*
   1462 	 * Initialize the interrupt mask and enable interrupts.
   1463 	 */
   1464 	/* normal interrupts */
   1465 	sc->sc_inten =  STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
   1466 
   1467 	/* abnormal interrupts */
   1468 	sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
   1469 	    STATUS_RU | STATUS_RPS | STATUS_RWT | STATUS_SE | STATUS_AIS;
   1470 
   1471 	sc->sc_rxint_mask = STATUS_RI|STATUS_RU|STATUS_RWT;
   1472 	sc->sc_txint_mask = STATUS_TI|STATUS_UNF|STATUS_TJT;
   1473 
   1474 	switch (sc->sc_chip) {
   1475 	case TULIP_CHIP_WB89C840F:
   1476 		/*
   1477 		 * Clear bits that we don't want that happen to
   1478 		 * overlap or don't exist.
   1479 		 */
   1480 		sc->sc_inten &= ~(STATUS_WINB_REI|STATUS_RWT);
   1481 		break;
   1482 
   1483 	default:
   1484 		/* Nothing. */
   1485 	}
   1486 
   1487 	sc->sc_rxint_mask &= sc->sc_inten;
   1488 	sc->sc_txint_mask &= sc->sc_inten;
   1489 
   1490 	TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
   1491 	TULIP_WRITE(sc, CSR_STATUS, 0xffffffff);
   1492 
   1493 	/*
   1494 	 * Give the transmit and receive rings to the Tulip.
   1495 	 */
   1496 	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDTXADDR(sc, sc->sc_txnext));
   1497 	TULIP_WRITE(sc, CSR_RXLIST, TULIP_CDRXADDR(sc, sc->sc_rxptr));
   1498 
   1499 	/*
   1500 	 * On chips that do this differently, set the station address.
   1501 	 */
   1502 	switch (sc->sc_chip) {
   1503 	case TULIP_CHIP_WB89C840F:
   1504 	    {
   1505 		/* XXX Do this with stream writes? */
   1506 		bus_addr_t cpa = TULIP_CSR_OFFSET(sc, CSR_WINB_CPA0);
   1507 
   1508 		for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1509 			bus_space_write_1(sc->sc_st, sc->sc_sh,
   1510 			    cpa + i, LLADDR(ifp->if_sadl)[i]);
   1511 		}
   1512 		break;
   1513 	    }
   1514 
   1515 	default:
   1516 		/* Nothing. */
   1517 	}
   1518 
   1519 	/*
   1520 	 * Set the receive filter.  This will start the transmit and
   1521 	 * receive processes.
   1522 	 */
   1523 	(*sc->sc_filter_setup)(sc);
   1524 
   1525 	/*
   1526 	 * Start the receive process.
   1527 	 */
   1528 	TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
   1529 
   1530 	if (sc->sc_tick != NULL) {
   1531 		/* Start the one second clock. */
   1532 		timeout(sc->sc_tick, sc, hz);
   1533 	}
   1534 
   1535 	/*
   1536 	 * Note that the interface is now running.
   1537 	 */
   1538 	ifp->if_flags |= IFF_RUNNING;
   1539 	ifp->if_flags &= ~IFF_OACTIVE;
   1540 
   1541 	/*
   1542 	 * Set the media.  We must do this after the transmit process is
   1543 	 * running, since we may actually have to transmit packets on
   1544 	 * our board to test link integrity.
   1545 	 */
   1546 	(void) (*sc->sc_mediasw->tmsw_set)(sc);
   1547 
   1548  out:
   1549 	if (error)
   1550 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1551 	return (error);
   1552 }
   1553 
   1554 /*
   1555  * tlp_rxdrain:
   1556  *
   1557  *	Drain the receive queue.
   1558  */
   1559 void
   1560 tlp_rxdrain(sc)
   1561 	struct tulip_softc *sc;
   1562 {
   1563 	struct tulip_rxsoft *rxs;
   1564 	int i;
   1565 
   1566 	for (i = 0; i < TULIP_NRXDESC; i++) {
   1567 		rxs = &sc->sc_rxsoft[i];
   1568 		if (rxs->rxs_mbuf != NULL) {
   1569 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1570 			m_freem(rxs->rxs_mbuf);
   1571 			rxs->rxs_mbuf = NULL;
   1572 		}
   1573 	}
   1574 }
   1575 
   1576 /*
   1577  * tlp_stop:
   1578  *
   1579  *	Stop transmission on the interface.
   1580  */
   1581 void
   1582 tlp_stop(sc, drain)
   1583 	struct tulip_softc *sc;
   1584 	int drain;
   1585 {
   1586 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1587 	struct tulip_txsoft *txs;
   1588 
   1589 	if (sc->sc_tick != NULL) {
   1590 		/* Stop the one second clock. */
   1591 		untimeout(sc->sc_tick, sc);
   1592 	}
   1593 
   1594 	/* Disable interrupts. */
   1595 	TULIP_WRITE(sc, CSR_INTEN, 0);
   1596 
   1597 	/* Stop the transmit and receive processes. */
   1598 	TULIP_WRITE(sc, CSR_OPMODE, 0);
   1599 	TULIP_WRITE(sc, CSR_RXLIST, 0);
   1600 	TULIP_WRITE(sc, CSR_TXLIST, 0);
   1601 
   1602 	/*
   1603 	 * Release any queued transmit buffers.
   1604 	 */
   1605 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1606 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
   1607 		if (txs->txs_mbuf != NULL) {
   1608 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1609 			m_freem(txs->txs_mbuf);
   1610 			txs->txs_mbuf = NULL;
   1611 		}
   1612 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1613 	}
   1614 
   1615 	if (drain) {
   1616 		/*
   1617 		 * Release the receive buffers.
   1618 		 */
   1619 		tlp_rxdrain(sc);
   1620 	}
   1621 
   1622 	sc->sc_flags &= ~(TULIPF_WANT_SETUP|TULIPF_DOING_SETUP);
   1623 
   1624 	/*
   1625 	 * Mark the interface down and cancel the watchdog timer.
   1626 	 */
   1627 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1628 	ifp->if_timer = 0;
   1629 }
   1630 
   1631 #define	SROM_EMIT(sc, x)						\
   1632 do {									\
   1633 	TULIP_WRITE((sc), CSR_MIIROM, (x));				\
   1634 	delay(1);							\
   1635 } while (0)
   1636 
   1637 /*
   1638  * tlp_srom_idle:
   1639  *
   1640  *	Put the SROM in idle state.
   1641  */
   1642 void
   1643 tlp_srom_idle(sc)
   1644 	struct tulip_softc *sc;
   1645 {
   1646 	u_int32_t miirom;
   1647 	int i;
   1648 
   1649 	miirom = MIIROM_SR;
   1650 	SROM_EMIT(sc, miirom);
   1651 
   1652 	miirom |= MIIROM_RD;
   1653 	SROM_EMIT(sc, miirom);
   1654 
   1655 	miirom |= MIIROM_SROMCS;
   1656 	SROM_EMIT(sc, miirom);
   1657 
   1658 	SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1659 
   1660 	/* Strobe the clock 25 times. */
   1661 	for (i = 0; i < 25; i++) {
   1662 		SROM_EMIT(sc, miirom);
   1663 		SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1664 	}
   1665 
   1666 	SROM_EMIT(sc, miirom);
   1667 
   1668 	miirom &= ~MIIROM_SROMCS;
   1669 	SROM_EMIT(sc, miirom);
   1670 
   1671 	SROM_EMIT(sc, 0);
   1672 }
   1673 
   1674 /*
   1675  * tlp_read_srom:
   1676  *
   1677  *	Read the Tulip SROM.
   1678  */
   1679 void
   1680 tlp_read_srom(sc, word, wordcnt, data)
   1681 	struct tulip_softc *sc;
   1682 	int word, wordcnt;
   1683 	u_int16_t *data;
   1684 {
   1685 	u_int32_t miirom;
   1686 	int i, x;
   1687 
   1688 	tlp_srom_idle(sc);
   1689 
   1690 	/* Select the SROM. */
   1691 	miirom = MIIROM_SR;
   1692 	SROM_EMIT(sc, miirom);
   1693 
   1694 	miirom |= MIIROM_RD;
   1695 	SROM_EMIT(sc, miirom);
   1696 
   1697 	for (i = 0; i < wordcnt; i++) {
   1698 		/* Send CHIP SELECT for one clock tick. */
   1699 		miirom |= MIIROM_SROMCS;
   1700 		SROM_EMIT(sc, miirom);
   1701 
   1702 		/* Shift in the READ opcode. */
   1703 		for (x = 3; x > 0; x--) {
   1704 			if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
   1705 				miirom |= MIIROM_SROMDI;
   1706 			else
   1707 				miirom &= ~MIIROM_SROMDI;
   1708 			SROM_EMIT(sc, miirom);
   1709 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1710 			SROM_EMIT(sc, miirom);
   1711 		}
   1712 
   1713 		/* Shift in address. */
   1714 		for (x = 6; x > 0; x--) {
   1715 			if ((word + i) & (1 << (x - 1)))
   1716 				miirom |= MIIROM_SROMDI;
   1717 			else
   1718 				miirom &= ~MIIROM_SROMDI;
   1719 			SROM_EMIT(sc, miirom);
   1720 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1721 			SROM_EMIT(sc, miirom);
   1722 		}
   1723 
   1724 		/* Shift out data. */
   1725 		miirom &= ~MIIROM_SROMDI;
   1726 		data[i] = 0;
   1727 		for (x = 16; x > 0; x--) {
   1728 			SROM_EMIT(sc, miirom|MIIROM_SROMSK);
   1729 			if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
   1730 				data[i] |= (1 << (x - 1));
   1731 			SROM_EMIT(sc, miirom);
   1732 		}
   1733 
   1734 		/* Clear CHIP SELECT. */
   1735 		miirom &= ~MIIROM_SROMCS;
   1736 		SROM_EMIT(sc, miirom);
   1737 	}
   1738 
   1739 	/* Deselect the SROM. */
   1740 	SROM_EMIT(sc, 0);
   1741 
   1742 	/* ...and idle it. */
   1743 	tlp_srom_idle(sc);
   1744 }
   1745 
   1746 #undef SROM_EMIT
   1747 
   1748 /*
   1749  * tlp_add_rxbuf:
   1750  *
   1751  *	Add a receive buffer to the indicated descriptor.
   1752  */
   1753 int
   1754 tlp_add_rxbuf(sc, idx)
   1755 	struct tulip_softc *sc;
   1756 	int idx;
   1757 {
   1758 	struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx];
   1759 	struct mbuf *m;
   1760 	int error;
   1761 
   1762 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1763 	if (m == NULL)
   1764 		return (ENOBUFS);
   1765 
   1766 	MCLGET(m, M_DONTWAIT);
   1767 	if ((m->m_flags & M_EXT) == 0) {
   1768 		m_freem(m);
   1769 		return (ENOBUFS);
   1770 	}
   1771 
   1772 	if (rxs->rxs_mbuf != NULL)
   1773 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1774 
   1775 	rxs->rxs_mbuf = m;
   1776 
   1777 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
   1778 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
   1779 	if (error) {
   1780 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1781 		    sc->sc_dev.dv_xname, idx, error);
   1782 		panic("tlp_add_rxbuf");	/* XXX */
   1783 	}
   1784 
   1785 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1786 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1787 
   1788 	TULIP_INIT_RXDESC(sc, idx);
   1789 
   1790 	return (0);
   1791 }
   1792 
   1793 /*
   1794  * tlp_crc32:
   1795  *
   1796  *	Compute the 32-bit CRC of the provided buffer.
   1797  */
   1798 u_int32_t
   1799 tlp_crc32(buf, len)
   1800 	const u_int8_t *buf;
   1801 	size_t len;
   1802 {
   1803 	static const u_int32_t crctab[] = {
   1804 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
   1805 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
   1806 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
   1807 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
   1808 	};
   1809 	u_int32_t crc;
   1810 	int i;
   1811 
   1812 	crc = 0xffffffff;
   1813 	for (i = 0; i < len; i++) {
   1814 		crc ^= buf[i];
   1815 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1816 		crc = (crc >> 4) ^ crctab[crc & 0xf];
   1817 	}
   1818 	return (crc);
   1819 }
   1820 
   1821 /*
   1822  * tlp_srom_crcok:
   1823  *
   1824  *	Check the CRC of the Tulip SROM.
   1825  */
   1826 int
   1827 tlp_srom_crcok(romdata)
   1828 	u_int8_t *romdata;
   1829 {
   1830 	u_int32_t crc;
   1831 
   1832 	crc = tlp_crc32(romdata, TULIP_ROM_CRC32_CHECKSUM);
   1833 	crc = (crc & 0xffff) ^ 0xffff;
   1834 	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM))
   1835 		return (1);
   1836 	return (0);
   1837 }
   1838 
   1839 /*
   1840  * tlp_parse_old_srom:
   1841  *
   1842  *	Parse old-format SROMs.
   1843  *
   1844  *	This routine is largely lifted from Matt Thomas's `de' driver.
   1845  */
   1846 int
   1847 tlp_parse_old_srom(sc, enaddr)
   1848 	struct tulip_softc *sc;
   1849 	u_int8_t *enaddr;
   1850 {
   1851 	static const u_int8_t testpat[] =
   1852 	    { 0xff, 0, 0x55, 0xaa, 0xff, 0, 0x55, 0xaa };
   1853 	int i;
   1854 	u_int32_t cksum;
   1855 
   1856 	if (memcmp(&sc->sc_srom[0], &sc->sc_srom[16], 8) != 0) {
   1857 		/*
   1858 		 * Some vendors (e.g. ZNYX) don't use the standard
   1859 		 * DEC Address ROM format, but rather just have an
   1860 		 * Ethernet address in the first 6 bytes, maybe a
   1861 		 * 2 byte checksum, and then all 0xff's.
   1862 		 */
   1863 		for (i = 8; i < 32; i++) {
   1864 			if (sc->sc_srom[i] != 0xff)
   1865 				return (0);
   1866 		}
   1867 
   1868 		/*
   1869 		 * Sanity check the Ethernet address:
   1870 		 *
   1871 		 *	- Make sure it's not multicast or locally
   1872 		 *	  assigned
   1873 		 *	- Make sure it has a non-0 OUI
   1874 		 */
   1875 		if (sc->sc_srom[0] & 3)
   1876 			return (0);
   1877 		if (sc->sc_srom[0] == 0 && sc->sc_srom[1] == 0 &&
   1878 		    sc->sc_srom[2] == 0)
   1879 			return (0);
   1880 
   1881 		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   1882 		return (1);
   1883 	}
   1884 
   1885 	/*
   1886 	 * Standard DEC Address ROM test.
   1887 	 */
   1888 
   1889 	if (memcmp(&sc->sc_srom[24], testpat, 8) != 0)
   1890 		return (0);
   1891 
   1892 	for (i = 0; i < 8; i++) {
   1893 		if (sc->sc_srom[i] != sc->sc_srom[15 - i])
   1894 			return (0);
   1895 	}
   1896 
   1897 	memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
   1898 
   1899 	cksum = *(u_int16_t *) &enaddr[0];
   1900 
   1901 	cksum <<= 1;
   1902 	if (cksum > 0xffff)
   1903 		cksum -= 0xffff;
   1904 
   1905 	cksum += *(u_int16_t *) &enaddr[2];
   1906 	if (cksum > 0xffff)
   1907 		cksum -= 0xffff;
   1908 
   1909 	cksum <<= 1;
   1910 	if (cksum > 0xffff)
   1911 		cksum -= 0xffff;
   1912 
   1913 	cksum += *(u_int16_t *) &enaddr[4];
   1914 	if (cksum >= 0xffff)
   1915 		cksum -= 0xffff;
   1916 
   1917 	if (cksum != *(u_int16_t *) &sc->sc_srom[6])
   1918 		return (0);
   1919 
   1920 	return (1);
   1921 }
   1922 
   1923 /*
   1924  * tlp_filter_setup:
   1925  *
   1926  *	Set the Tulip's receive filter.
   1927  */
   1928 void
   1929 tlp_filter_setup(sc)
   1930 	struct tulip_softc *sc;
   1931 {
   1932 	struct ethercom *ec = &sc->sc_ethercom;
   1933 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1934 	struct ether_multi *enm;
   1935 	struct ether_multistep step;
   1936 	__volatile u_int32_t *sp;
   1937 	u_int8_t enaddr[ETHER_ADDR_LEN];
   1938 	u_int32_t hash;
   1939 	int cnt;
   1940 
   1941 	DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n",
   1942 	    sc->sc_dev.dv_xname, sc->sc_flags));
   1943 
   1944 	memcpy(enaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
   1945 
   1946 	/*
   1947 	 * If there are transmissions pending, wait until they have
   1948 	 * completed.
   1949 	 */
   1950 	if (SIMPLEQ_FIRST(&sc->sc_txdirtyq) != NULL ||
   1951 	    (sc->sc_flags & TULIPF_DOING_SETUP) != 0) {
   1952 		sc->sc_flags |= TULIPF_WANT_SETUP;
   1953 		DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n",
   1954 		    sc->sc_dev.dv_xname));
   1955 		return;
   1956 	}
   1957 	sc->sc_flags &= ~TULIPF_WANT_SETUP;
   1958 
   1959 	/*
   1960 	 * If we're running, idle the transmit and receive engines.  If
   1961 	 * we're NOT running, we're being called from tlp_init(), and our
   1962 	 * writing OPMODE will start the transmit and receive processes
   1963 	 * in motion.
   1964 	 */
   1965 	if (ifp->if_flags & IFF_RUNNING)
   1966 		tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   1967 
   1968 	sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
   1969 
   1970 	if (ifp->if_flags & IFF_PROMISC) {
   1971 		sc->sc_opmode |= OPMODE_PR;
   1972 		goto allmulti;
   1973 	}
   1974 
   1975 	/*
   1976 	 * Try Perfect filtering first.
   1977 	 */
   1978 
   1979 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   1980 	sp = TULIP_CDSP(sc);
   1981 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   1982 	cnt = 0;
   1983 	ETHER_FIRST_MULTI(step, ec, enm);
   1984 	while (enm != NULL) {
   1985 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1986 			/*
   1987 			 * We must listen to a range of multicast addresses.
   1988 			 * For now, just accept all multicasts, rather than
   1989 			 * trying to set only those filter bits needed to match
   1990 			 * the range.  (At this time, the only use of address
   1991 			 * ranges is for IP multicast routing, for which the
   1992 			 * range is big enough to require all bits set.)
   1993 			 */
   1994 			goto allmulti;
   1995 		}
   1996 		if (cnt == (TULIP_MAXADDRS - 2)) {
   1997 			/*
   1998 			 * We already have our multicast limit (still need
   1999 			 * our station address and broadcast).  Go to
   2000 			 * Hash-Perfect mode.
   2001 			 */
   2002 			goto hashperfect;
   2003 		}
   2004 		*sp++ = ((u_int16_t *) enm->enm_addrlo)[0];
   2005 		*sp++ = ((u_int16_t *) enm->enm_addrlo)[1];
   2006 		*sp++ = ((u_int16_t *) enm->enm_addrlo)[2];
   2007 		ETHER_NEXT_MULTI(step, enm);
   2008 	}
   2009 
   2010 	if (ifp->if_flags & IFF_BROADCAST) {
   2011 		/* ...and the broadcast address. */
   2012 		cnt++;
   2013 		*sp++ = 0xffff;
   2014 		*sp++ = 0xffff;
   2015 		*sp++ = 0xffff;
   2016 	}
   2017 
   2018 	/* Pad the rest with our station address. */
   2019 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   2020 		*sp++ = ((u_int16_t *) enaddr)[0];
   2021 		*sp++ = ((u_int16_t *) enaddr)[1];
   2022 		*sp++ = ((u_int16_t *) enaddr)[2];
   2023 	}
   2024 	ifp->if_flags &= ~IFF_ALLMULTI;
   2025 	goto setit;
   2026 
   2027  hashperfect:
   2028 	/*
   2029 	 * Try Hash-Perfect mode.
   2030 	 */
   2031 
   2032 	/*
   2033 	 * Some 21140 chips have broken Hash-Perfect modes.  On these
   2034 	 * chips, we simply use Hash-Only mode, and put our station
   2035 	 * address into the filter.
   2036 	 */
   2037 	if (sc->sc_chip == TULIP_CHIP_21140)
   2038 		sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY;
   2039 	else
   2040 		sc->sc_filtmode = TDCTL_Tx_FT_HASH;
   2041 	sp = TULIP_CDSP(sc);
   2042 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2043 	ETHER_FIRST_MULTI(step, ec, enm);
   2044 	while (enm != NULL) {
   2045 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2046 			/*
   2047 			 * We must listen to a range of multicast addresses.
   2048 			 * For now, just accept all multicasts, rather than
   2049 			 * trying to set only those filter bits needed to match
   2050 			 * the range.  (At this time, the only use of address
   2051 			 * ranges is for IP multicast routing, for which the
   2052 			 * range is big enough to require all bits set.)
   2053 			 */
   2054 			goto allmulti;
   2055 		}
   2056 		hash = tlp_mchash(enm->enm_addrlo);
   2057 		sp[hash >> 4] |= 1 << (hash & 0xf);
   2058 		ETHER_NEXT_MULTI(step, enm);
   2059 	}
   2060 
   2061 	if (ifp->if_flags & IFF_BROADCAST) {
   2062 		/* ...and the broadcast address. */
   2063 		hash = tlp_mchash(etherbroadcastaddr);
   2064 		sp[hash >> 4] |= 1 << (hash & 0xf);
   2065 	}
   2066 
   2067 	if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
   2068 		/* ...and our station address. */
   2069 		hash = tlp_mchash(enaddr);
   2070 		sp[hash >> 4] |= 1 << (hash & 0xf);
   2071 	} else {
   2072 		/*
   2073 		 * Hash-Perfect mode; put our station address after
   2074 		 * the hash table.
   2075 		 */
   2076 		sp[39] = ((u_int16_t *) enaddr)[0];
   2077 		sp[40] = ((u_int16_t *) enaddr)[1];
   2078 		sp[41] = ((u_int16_t *) enaddr)[2];
   2079 	}
   2080 	ifp->if_flags &= ~IFF_ALLMULTI;
   2081 	goto setit;
   2082 
   2083  allmulti:
   2084 	/*
   2085 	 * Use Perfect filter mode.  First address is the broadcast address,
   2086 	 * and pad the rest with our station address.  We'll set Pass-all-
   2087 	 * multicast in OPMODE below.
   2088 	 */
   2089 	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
   2090 	sp = TULIP_CDSP(sc);
   2091 	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
   2092 	cnt = 0;
   2093 	if (ifp->if_flags & IFF_BROADCAST) {
   2094 		cnt++;
   2095 		*sp++ = 0xffff;
   2096 		*sp++ = 0xffff;
   2097 		*sp++ = 0xffff;
   2098 	}
   2099 	for (; cnt < TULIP_MAXADDRS; cnt++) {
   2100 		*sp++ = ((u_int16_t *) enaddr)[0];
   2101 		*sp++ = ((u_int16_t *) enaddr)[1];
   2102 		*sp++ = ((u_int16_t *) enaddr)[2];
   2103 	}
   2104 	ifp->if_flags |= IFF_ALLMULTI;
   2105 
   2106  setit:
   2107 	if (ifp->if_flags & IFF_ALLMULTI)
   2108 		sc->sc_opmode |= OPMODE_PM;
   2109 
   2110 	/* Sync the setup packet buffer. */
   2111 	TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE);
   2112 
   2113 	/*
   2114 	 * Fill in the setup packet descriptor.
   2115 	 */
   2116 	sc->sc_setup_desc.td_bufaddr1 = TULIP_CDSPADDR(sc);
   2117 	sc->sc_setup_desc.td_bufaddr2 = TULIP_CDTXADDR(sc, sc->sc_txnext);
   2118 	sc->sc_setup_desc.td_ctl =
   2119 	    (TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) |
   2120 	    sc->sc_filtmode | TDCTL_Tx_SET | TDCTL_Tx_FS | TDCTL_Tx_LS |
   2121 	    TDCTL_Tx_IC | TDCTL_CH;
   2122 	sc->sc_setup_desc.td_status = TDSTAT_OWN;
   2123 	TULIP_CDSDSYNC(sc, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   2124 
   2125 	/*
   2126 	 * Write the address of the setup descriptor.  This also has
   2127 	 * the side effect of giving the transmit ring to the chip,
   2128 	 * since the setup descriptor points to the next available
   2129 	 * descriptor in the ring.
   2130 	 */
   2131 	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDSDADDR(sc));
   2132 
   2133 	/*
   2134 	 * Set the OPMODE register.  This will also resume the
   2135 	 * transmit transmit process we idled above.
   2136 	 */
   2137 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2138 
   2139 	sc->sc_flags |= TULIPF_DOING_SETUP;
   2140 
   2141 	/*
   2142 	 * Kick the transmitter; this will cause the Tulip to
   2143 	 * read the setup descriptor.
   2144 	 */
   2145 	/* XXX USE AUTOPOLLING? */
   2146 	TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
   2147 
   2148 	/* Set up a watchdog timer in case the chip flakes out. */
   2149 	ifp->if_timer = 5;
   2150 
   2151 	DPRINTF(sc, ("%s: tlp_filter_setup: returning\n", sc->sc_dev.dv_xname));
   2152 }
   2153 
   2154 /*
   2155  * tlp_winb_filter_setup:
   2156  *
   2157  *	Set the Winbond 89C840F's receive filter.
   2158  */
   2159 void
   2160 tlp_winb_filter_setup(sc)
   2161 	struct tulip_softc *sc;
   2162 {
   2163 	struct ethercom *ec = &sc->sc_ethercom;
   2164 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2165 	struct ether_multi *enm;
   2166 	struct ether_multistep step;
   2167 	u_int32_t hash, mchash[2];
   2168 
   2169 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n",
   2170 	    sc->sc_dev.dv_xname, sc->sc_flags));
   2171 
   2172 	sc->sc_opmode &= ~(OPMODE_WINB_APP|OPMODE_WINB_AMP|OPMODE_WINB_ABP);
   2173 
   2174 	if (ifp->if_flags & IFF_MULTICAST)
   2175 		sc->sc_opmode |= OPMODE_WINB_AMP;
   2176 
   2177 	if (ifp->if_flags & IFF_BROADCAST)
   2178 		sc->sc_opmode |= OPMODE_WINB_ABP;
   2179 
   2180 	if (ifp->if_flags & IFF_PROMISC) {
   2181 		sc->sc_opmode |= OPMODE_WINB_APP;
   2182 		goto allmulti;
   2183 	}
   2184 
   2185 	mchash[0] = mchash[1] = 0;
   2186 
   2187 	ETHER_FIRST_MULTI(step, ec, enm);
   2188 	while (enm != NULL) {
   2189 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2190 			/*
   2191 			 * We must listen to a range of multicast addresses.
   2192 			 * For now, just accept all multicasts, rather than
   2193 			 * trying to set only those filter bits needed to match
   2194 			 * the range.  (At this time, the only use of address
   2195 			 * ranges is for IP multicast routing, for which the
   2196 			 * range is big enough to require all bits set.)
   2197 			 */
   2198 			goto allmulti;
   2199 		}
   2200 
   2201 		/*
   2202 		 * According to the FreeBSD `wb' driver, yes, you
   2203 		 * really do invert the hash.
   2204 		 */
   2205 		hash = (~(tlp_crc32(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26))
   2206 		    & 0x3f;
   2207 		mchash[hash >> 5] |= 1 << (hash & 0x1f);
   2208 		ETHER_NEXT_MULTI(step, enm);
   2209 	}
   2210 	ifp->if_flags &= ~IFF_ALLMULTI;
   2211 	goto setit;
   2212 
   2213  allmulti:
   2214 	ifp->if_flags |= IFF_ALLMULTI;
   2215 	mchash[0] = mchash[1] = 0xffffffff;
   2216 
   2217  setit:
   2218 	TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]);
   2219 	TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]);
   2220 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2221 	DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n",
   2222 	    sc->sc_dev.dv_xname));
   2223 }
   2224 
   2225 /*
   2226  * tlp_idle:
   2227  *
   2228  *	Cause the transmit and/or receive processes to go idle.
   2229  */
   2230 void
   2231 tlp_idle(sc, bits)
   2232 	struct tulip_softc *sc;
   2233 	u_int32_t bits;
   2234 {
   2235 	static const char *tx_state_names[] = {
   2236 		"STOPPED",
   2237 		"RUNNING - FETCH",
   2238 		"RUNNING - WAIT",
   2239 		"RUNNING - READING",
   2240 		"-- RESERVED --",
   2241 		"RUNNING - SETUP",
   2242 		"SUSPENDED",
   2243 		"RUNNING - CLOSE",
   2244 	};
   2245 	static const char *rx_state_names[] = {
   2246 		"STOPPED",
   2247 		"RUNNING - FETCH",
   2248 		"RUNNING - CHECK",
   2249 		"RUNNING - WAIT",
   2250 		"SUSPENDED",
   2251 		"RUNNING - CLOSE",
   2252 		"RUNNING - FLUSH",
   2253 		"RUNNING - QUEUE",
   2254 	};
   2255 	u_int32_t csr, ackmask = 0;
   2256 	int i;
   2257 
   2258 	if (bits & OPMODE_ST)
   2259 		ackmask |= STATUS_TPS;
   2260 
   2261 	if (bits & OPMODE_SR)
   2262 		ackmask |= STATUS_RPS;
   2263 
   2264 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits);
   2265 
   2266 	for (i = 0; i < 1000; i++) {
   2267 		if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
   2268 			break;
   2269 		delay(10);
   2270 	}
   2271 
   2272 	csr = TULIP_READ(sc, CSR_STATUS);
   2273 	if ((csr & ackmask) != ackmask) {
   2274 		if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
   2275 		    (csr & STATUS_TS) != STATUS_TS_STOPPED)
   2276 			printf("%s: transmit process failed to idle: "
   2277 			    "state %s\n", sc->sc_dev.dv_xname,
   2278 			    tx_state_names[(csr & STATUS_TS) >> 20]);
   2279 		if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
   2280 		    (csr & STATUS_RS) != STATUS_RS_STOPPED)
   2281 			printf("%s: receive process failed to idle: "
   2282 			    "state %s\n", sc->sc_dev.dv_xname,
   2283 			    rx_state_names[(csr & STATUS_RS) >> 17]);
   2284 	}
   2285 	TULIP_WRITE(sc, CSR_STATUS, ackmask);
   2286 }
   2287 
   2288 /*****************************************************************************
   2289  * Generic media support functions.
   2290  *****************************************************************************/
   2291 
   2292 /*
   2293  * tlp_mediastatus:	[ifmedia interface function]
   2294  *
   2295  *	Query the current media.
   2296  */
   2297 void
   2298 tlp_mediastatus(ifp, ifmr)
   2299 	struct ifnet *ifp;
   2300 	struct ifmediareq *ifmr;
   2301 {
   2302 	struct tulip_softc *sc = ifp->if_softc;
   2303 
   2304 	(*sc->sc_mediasw->tmsw_get)(sc, ifmr);
   2305 }
   2306 
   2307 /*
   2308  * tlp_mediachange:	[ifmedia interface function]
   2309  *
   2310  *	Update the current media.
   2311  */
   2312 int
   2313 tlp_mediachange(ifp)
   2314 	struct ifnet *ifp;
   2315 {
   2316 	struct tulip_softc *sc = ifp->if_softc;
   2317 
   2318 	return ((*sc->sc_mediasw->tmsw_set)(sc));
   2319 }
   2320 
   2321 /*****************************************************************************
   2322  * Support functions for MII-attached media.
   2323  *****************************************************************************/
   2324 
   2325 /*
   2326  * tlp_mii_tick:
   2327  *
   2328  *	One second timer, used to tick the MII.
   2329  */
   2330 void
   2331 tlp_mii_tick(arg)
   2332 	void *arg;
   2333 {
   2334 	struct tulip_softc *sc = arg;
   2335 	int s;
   2336 
   2337 	s = splnet();
   2338 	mii_tick(&sc->sc_mii);
   2339 	splx(s);
   2340 
   2341 	timeout(sc->sc_tick, sc, hz);
   2342 }
   2343 
   2344 /*
   2345  * tlp_mii_statchg:	[mii interface function]
   2346  *
   2347  *	Callback from PHY when media changes.
   2348  */
   2349 void
   2350 tlp_mii_statchg(self)
   2351 	struct device *self;
   2352 {
   2353 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2354 
   2355 	/* Idle the transmit and receive processes. */
   2356 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2357 
   2358 	/*
   2359 	 * XXX What about Heartbeat Disable?  Is it magically frobbed
   2360 	 * XXX by the PHY?  I hope so...
   2361 	 */
   2362 
   2363 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD);
   2364 
   2365 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
   2366 		sc->sc_opmode |= OPMODE_TTM;
   2367 
   2368 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   2369 		sc->sc_opmode |= OPMODE_FD;
   2370 
   2371 	/*
   2372 	 * Write new OPMODE bits.  This also restarts the transmit
   2373 	 * and receive processes.
   2374 	 */
   2375 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2376 
   2377 	/* XXX Update ifp->if_baudrate */
   2378 }
   2379 
   2380 /*
   2381  * tlp_winb_mii_statchg: [mii interface function]
   2382  *
   2383  *	Callback from PHY when media changes.  This version is
   2384  *	for the Winbond 89C840F, which has different OPMODE bits.
   2385  */
   2386 void
   2387 tlp_winb_mii_statchg(self)
   2388 	struct device *self;
   2389 {
   2390 	struct tulip_softc *sc = (struct tulip_softc *)self;
   2391 
   2392 	/* Idle the transmit and receive processes. */
   2393 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2394 
   2395 	/*
   2396 	 * XXX What about Heartbeat Disable?  Is it magically frobbed
   2397 	 * XXX by the PHY?  I hope so...
   2398 	 */
   2399 
   2400 	sc->sc_opmode &= ~(OPMODE_WINB_FES|OPMODE_FD);
   2401 
   2402 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX)
   2403 		sc->sc_opmode |= OPMODE_WINB_FES;
   2404 
   2405 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   2406 		sc->sc_opmode |= OPMODE_FD;
   2407 
   2408 	/*
   2409 	 * Write new OPMODE bits.  This also restarts the transmit
   2410 	 * and receive processes.
   2411 	 */
   2412 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2413 
   2414 	/* XXX Update ifp->if_baudrate */
   2415 }
   2416 
   2417 /*
   2418  * tlp_mii_getmedia:
   2419  *
   2420  *	Callback from ifmedia to request current media status.
   2421  */
   2422 void
   2423 tlp_mii_getmedia(sc, ifmr)
   2424 	struct tulip_softc *sc;
   2425 	struct ifmediareq *ifmr;
   2426 {
   2427 
   2428 	mii_pollstat(&sc->sc_mii);
   2429 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   2430 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   2431 }
   2432 
   2433 /*
   2434  * tlp_mii_setmedia:
   2435  *
   2436  *	Callback from ifmedia to request new media setting.
   2437  */
   2438 int
   2439 tlp_mii_setmedia(sc)
   2440 	struct tulip_softc *sc;
   2441 {
   2442 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2443 
   2444 	if (ifp->if_flags & IFF_UP)
   2445 		mii_mediachg(&sc->sc_mii);
   2446 	return (0);
   2447 }
   2448 
   2449 #define	MII_EMIT(sc, x)							\
   2450 do {									\
   2451 	TULIP_WRITE((sc), CSR_MIIROM, (x));				\
   2452 	delay(1);							\
   2453 } while (0)
   2454 
   2455 /*
   2456  * tlp_sio_mii_sync:
   2457  *
   2458  *	Synchronize the SIO-attached MII.
   2459  */
   2460 void
   2461 tlp_sio_mii_sync(sc)
   2462 	struct tulip_softc *sc;
   2463 {
   2464 	u_int32_t miirom;
   2465 	int i;
   2466 
   2467 	miirom = MIIROM_MIIDIR|MIIROM_MDO;
   2468 
   2469 	MII_EMIT(sc, miirom);
   2470 	for (i = 0; i < 32; i++) {
   2471 		MII_EMIT(sc, miirom | MIIROM_MDC);
   2472 		MII_EMIT(sc, miirom);
   2473 	}
   2474 }
   2475 
   2476 /*
   2477  * tlp_sio_mii_sendbits:
   2478  *
   2479  *	Send a series of bits out the SIO to the MII.
   2480  */
   2481 void
   2482 tlp_sio_mii_sendbits(sc, data, nbits)
   2483 	struct tulip_softc *sc;
   2484 	u_int32_t data;
   2485 	int nbits;
   2486 {
   2487 	u_int32_t miirom, i;
   2488 
   2489 	miirom = MIIROM_MIIDIR;
   2490 	MII_EMIT(sc, miirom);
   2491 
   2492 	for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
   2493 		if (data & i)
   2494 			miirom |= MIIROM_MDO;
   2495 		else
   2496 			miirom &= ~MIIROM_MDO;
   2497 		MII_EMIT(sc, miirom);
   2498 		MII_EMIT(sc, miirom|MIIROM_MDC);
   2499 		MII_EMIT(sc, miirom);
   2500 	}
   2501 }
   2502 
   2503 /*
   2504  * tlp_sio_mii_readreg:
   2505  *
   2506  *	Read a PHY register via SIO-attached MII.
   2507  */
   2508 int
   2509 tlp_sio_mii_readreg(self, phy, reg)
   2510 	struct device *self;
   2511 	int phy, reg;
   2512 {
   2513 	struct tulip_softc *sc = (void *) self;
   2514 	int val = 0, err = 0, i;
   2515 
   2516 	tlp_sio_mii_sync(sc);
   2517 
   2518 	tlp_sio_mii_sendbits(sc, MII_COMMAND_START, 2);
   2519 	tlp_sio_mii_sendbits(sc, MII_COMMAND_READ, 2);
   2520 	tlp_sio_mii_sendbits(sc, phy, 5);
   2521 	tlp_sio_mii_sendbits(sc, reg, 5);
   2522 
   2523 	MII_EMIT(sc, MIIROM_MIIDIR);
   2524 	MII_EMIT(sc, MIIROM_MIIDIR|MIIROM_MDC);
   2525 
   2526 	MII_EMIT(sc, 0);
   2527 	MII_EMIT(sc, MIIROM_MDC);
   2528 
   2529 	err = TULIP_ISSET(sc, CSR_MIIROM, MIIROM_MDI);
   2530 
   2531 	MII_EMIT(sc, 0);
   2532 	MII_EMIT(sc, MIIROM_MDC);
   2533 
   2534 	for (i = 0; i < 16; i++) {
   2535 		val <<= 1;
   2536 		MII_EMIT(sc, 0);
   2537 		if (err == 0 && TULIP_ISSET(sc, CSR_MIIROM, MIIROM_MDI))
   2538 			val |= 1;
   2539 		MII_EMIT(sc, MIIROM_MDC);
   2540 	}
   2541 
   2542 	MII_EMIT(sc, 0);
   2543 
   2544 	return (err ? 0 : val);
   2545 }
   2546 
   2547 /*
   2548  * tlp_sio_mii_writereg:
   2549  *
   2550  *	Write a PHY register via SIO-attached MII.
   2551  */
   2552 void
   2553 tlp_sio_mii_writereg(self, phy, reg, val)
   2554 	struct device *self;
   2555 	int phy, reg, val;
   2556 {
   2557 	struct tulip_softc *sc = (void *) self;
   2558 
   2559 	tlp_sio_mii_sync(sc);
   2560 
   2561 	tlp_sio_mii_sendbits(sc, MII_COMMAND_START, 2);
   2562 	tlp_sio_mii_sendbits(sc, MII_COMMAND_WRITE, 2);
   2563 	tlp_sio_mii_sendbits(sc, phy, 5);
   2564 	tlp_sio_mii_sendbits(sc, reg, 5);
   2565 	tlp_sio_mii_sendbits(sc, MII_COMMAND_ACK, 2);
   2566 	tlp_sio_mii_sendbits(sc, val, 16);
   2567 
   2568 	MII_EMIT(sc, 0);
   2569 }
   2570 
   2571 #undef MII_EMIT
   2572 
   2573 /*
   2574  * tlp_pnic_mii_readreg:
   2575  *
   2576  *	Read a PHY register on the Lite-On PNIC.
   2577  */
   2578 int
   2579 tlp_pnic_mii_readreg(self, phy, reg)
   2580 	struct device *self;
   2581 	int phy, reg;
   2582 {
   2583 	struct tulip_softc *sc = (void *) self;
   2584 	u_int32_t val;
   2585 	int i;
   2586 
   2587 	TULIP_WRITE(sc, CSR_PNIC_MII,
   2588 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   2589 	    PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
   2590 	    (reg << PNIC_MII_REGSHIFT));
   2591 
   2592 	for (i = 0; i < 1000; i++) {
   2593 		delay(10);
   2594 		val = TULIP_READ(sc, CSR_PNIC_MII);
   2595 		if ((val & PNIC_MII_BUSY) == 0) {
   2596 			if ((val & PNIC_MII_DATA) == PNIC_MII_DATA)
   2597 				return (0);
   2598 			else
   2599 				return (val & PNIC_MII_DATA);
   2600 		}
   2601 	}
   2602 	printf("%s: MII read timed out\n", sc->sc_dev.dv_xname);
   2603 	return (0);
   2604 }
   2605 
   2606 /*
   2607  * tlp_pnic_mii_writereg:
   2608  *
   2609  *	Write a PHY register on the Lite-On PNIC.
   2610  */
   2611 void
   2612 tlp_pnic_mii_writereg(self, phy, reg, val)
   2613 	struct device *self;
   2614 	int phy, reg, val;
   2615 {
   2616 	struct tulip_softc *sc = (void *) self;
   2617 	int i;
   2618 
   2619 	TULIP_WRITE(sc, CSR_PNIC_MII,
   2620 	    PNIC_MII_MBO | PNIC_MII_RESERVED |
   2621 	    PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
   2622 	    (reg << PNIC_MII_REGSHIFT) | val);
   2623 
   2624 	for (i = 0; i < 1000; i++) {
   2625 		delay(10);
   2626 		if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
   2627 			return;
   2628 	}
   2629 	printf("%s: MII write timed out\n", sc->sc_dev.dv_xname);
   2630 }
   2631 
   2632 /*****************************************************************************
   2633  * Chip/board-specific media switches.  The ones here are ones that
   2634  * are potentially common to multiple front-ends.
   2635  *****************************************************************************/
   2636 
   2637 /*
   2638  * 21040 and 21041 media switches.
   2639  */
   2640 void	tlp_21040_tmsw_init __P((struct tulip_softc *));
   2641 void	tlp_21040_tp_tmsw_init __P((struct tulip_softc *));
   2642 void	tlp_21040_auibnc_tmsw_init __P((struct tulip_softc *));
   2643 void	tlp_21040_21041_tmsw_get __P((struct tulip_softc *,
   2644 	    struct ifmediareq *));
   2645 int	tlp_21040_21041_tmsw_set __P((struct tulip_softc *));
   2646 
   2647 const struct tulip_mediasw tlp_21040_mediasw = {
   2648 	tlp_21040_tmsw_init, tlp_21040_21041_tmsw_get, tlp_21040_21041_tmsw_set
   2649 };
   2650 
   2651 const struct tulip_mediasw tlp_21040_tp_mediasw = {
   2652 	tlp_21040_tp_tmsw_init, tlp_21040_21041_tmsw_get,
   2653 	    tlp_21040_21041_tmsw_set
   2654 };
   2655 
   2656 const struct tulip_mediasw tlp_21040_auibnc_mediasw = {
   2657 	tlp_21040_auibnc_tmsw_init, tlp_21040_21041_tmsw_get,
   2658 	    tlp_21040_21041_tmsw_set
   2659 };
   2660 
   2661 #define	ADD(m, t)	ifmedia_add(&sc->sc_mii.mii_media, (m), 0, (t))
   2662 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
   2663 
   2664 void
   2665 tlp_21040_tmsw_init(sc)
   2666 	struct tulip_softc *sc;
   2667 {
   2668 	struct tulip_21040_21041_sia_media *tsm;
   2669 	const char *sep = "";
   2670 
   2671 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   2672 	    tlp_mediastatus);
   2673 
   2674 	printf("%s: ", sc->sc_dev.dv_xname);
   2675 
   2676 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
   2677 	    M_WAITOK);
   2678 	tsm->tsm_siaconn = SIACONN_21040_10BASET;
   2679 	tsm->tsm_siatxrx = SIATXRX_21040_10BASET;
   2680 	tsm->tsm_siagen  = SIAGEN_21040_10BASET;
   2681 	ADD(IFM_ETHER|IFM_10_T, tsm);
   2682 	PRINT("10baseT");
   2683 
   2684 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
   2685 	    M_WAITOK);
   2686 	tsm->tsm_siaconn = SIACONN_21040_10BASET_FDX;
   2687 	tsm->tsm_siatxrx = SIATXRX_21040_10BASET_FDX;
   2688 	tsm->tsm_siagen  = SIAGEN_21040_10BASET_FDX;
   2689 	ADD(IFM_ETHER|IFM_10_T|IFM_FDX, tsm);
   2690 	PRINT("10baseT-FDX");
   2691 
   2692 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
   2693 	    M_WAITOK);
   2694 	tsm->tsm_siaconn = SIACONN_21040_AUI;
   2695 	tsm->tsm_siatxrx = SIATXRX_21040_AUI;
   2696 	tsm->tsm_siagen  = SIAGEN_21040_AUI;
   2697 	ADD(IFM_ETHER|IFM_10_5, tsm);
   2698 	PRINT("10base5");
   2699 
   2700 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
   2701 	    M_WAITOK);
   2702 	tsm->tsm_siaconn = SIACONN_21040_EXTSIA;
   2703 	tsm->tsm_siatxrx = SIATXRX_21040_EXTSIA;
   2704 	tsm->tsm_siagen  = SIAGEN_21040_EXTSIA;
   2705 	ADD(IFM_ETHER|IFM_MANUAL, tsm);
   2706 	PRINT("manual");
   2707 
   2708 	/*
   2709 	 * XXX Autosense not yet supported.
   2710 	 */
   2711 
   2712 	/* XXX This should be auto-sense. */
   2713 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   2714 	printf(", default 10baseT");
   2715 
   2716 	printf("\n");
   2717 }
   2718 
   2719 void
   2720 tlp_21040_tp_tmsw_init(sc)
   2721 	struct tulip_softc *sc;
   2722 {
   2723 	struct tulip_21040_21041_sia_media *tsm;
   2724 	const char *sep = "";
   2725 
   2726 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   2727 	    tlp_mediastatus);
   2728 
   2729 	printf("%s: ", sc->sc_dev.dv_xname);
   2730 
   2731 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
   2732 	    M_WAITOK);
   2733 	tsm->tsm_siaconn = SIACONN_21040_10BASET;
   2734 	tsm->tsm_siatxrx = SIATXRX_21040_10BASET;
   2735 	tsm->tsm_siagen  = SIAGEN_21040_10BASET;
   2736 	ADD(IFM_ETHER|IFM_10_T, tsm);
   2737 	PRINT("10baseT");
   2738 
   2739 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
   2740 	    M_WAITOK);
   2741 	tsm->tsm_siaconn = SIACONN_21040_10BASET_FDX;
   2742 	tsm->tsm_siatxrx = SIATXRX_21040_10BASET_FDX;
   2743 	tsm->tsm_siagen  = SIAGEN_21040_10BASET_FDX;
   2744 	ADD(IFM_ETHER|IFM_10_T|IFM_FDX, tsm);
   2745 	PRINT("10baseT-FDX");
   2746 
   2747 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
   2748 	printf(", default 10baseT");
   2749 
   2750 	printf("\n");
   2751 }
   2752 
   2753 void
   2754 tlp_21040_auibnc_tmsw_init(sc)
   2755 	struct tulip_softc *sc;
   2756 {
   2757 	struct tulip_21040_21041_sia_media *tsm;
   2758 	const char *sep = "";
   2759 
   2760 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   2761 	    tlp_mediastatus);
   2762 
   2763 	printf("%s: ", sc->sc_dev.dv_xname);
   2764 
   2765 	tsm = malloc(sizeof(struct tulip_21040_21041_sia_media), M_DEVBUF,
   2766 	    M_WAITOK);
   2767 	tsm->tsm_siaconn = SIACONN_21040_AUI;
   2768 	tsm->tsm_siatxrx = SIATXRX_21040_AUI;
   2769 	tsm->tsm_siagen  = SIAGEN_21040_AUI;
   2770 	ADD(IFM_ETHER|IFM_10_5, tsm);
   2771 	PRINT("10base5");
   2772 
   2773 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5);
   2774 
   2775 	printf("\n");
   2776 }
   2777 
   2778 #undef ADD
   2779 #undef PRINT
   2780 
   2781 void
   2782 tlp_21040_21041_tmsw_get(sc, ifmr)
   2783 	struct tulip_softc *sc;
   2784 	struct ifmediareq *ifmr;
   2785 {
   2786 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   2787 
   2788 	ifmr->ifm_status = 0;
   2789 
   2790 	switch (IFM_SUBTYPE(ife->ifm_media)) {
   2791 	case IFM_AUTO:
   2792 		/*
   2793 		 * XXX Implement autosensing case.
   2794 		 */
   2795 		break;
   2796 
   2797 	case IFM_10_T:
   2798 		/*
   2799 		 * We're able to detect link directly on twisted pair.
   2800 		 */
   2801 		ifmr->ifm_status = IFM_AVALID;
   2802 		if (TULIP_ISSET(sc, CSR_SIASTAT, SIASTAT_LKF) == 0)
   2803 			ifmr->ifm_status |= IFM_ACTIVE;
   2804 		/* FALLTHROUGH */
   2805 	default:
   2806 		/*
   2807 		 * If not autosensing, active media is the currently
   2808 		 * selected media.
   2809 		 */
   2810 		ifmr->ifm_active = ife->ifm_media;
   2811 	}
   2812 }
   2813 
   2814 int
   2815 tlp_21040_21041_tmsw_set(sc)
   2816 	struct tulip_softc *sc;
   2817 {
   2818 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   2819 	struct tulip_21040_21041_sia_media *tsm;
   2820 
   2821 	if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
   2822 		/*
   2823 		 * If not autosensing, just pull the SIA settings out
   2824 		 * of the media entry.
   2825 		 */
   2826 		tsm = ife->ifm_aux;
   2827 		TULIP_WRITE(sc, CSR_SIACONN, SIACONN_SRL);
   2828 		TULIP_WRITE(sc, CSR_SIATXRX, tsm->tsm_siatxrx);
   2829 		TULIP_WRITE(sc, CSR_SIAGEN,  tsm->tsm_siagen);
   2830 		TULIP_WRITE(sc, CSR_SIACONN, tsm->tsm_siaconn);
   2831 
   2832 		tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   2833 		sc->sc_opmode &= ~OPMODE_FD;
   2834 		if (ife->ifm_media & IFM_FDX)
   2835 			sc->sc_opmode |= OPMODE_FD;
   2836 		TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   2837 	} else {
   2838 		/*
   2839 		 * XXX Implement autosensing case.
   2840 		 */
   2841 	}
   2842 
   2843 	return (0);
   2844 }
   2845 
   2846 /*
   2847  * MII-on-SIO media switch.  Handles only MII attached to the SIO.
   2848  */
   2849 void	tlp_sio_mii_tmsw_init __P((struct tulip_softc *));
   2850 
   2851 const struct tulip_mediasw tlp_sio_mii_mediasw = {
   2852 	tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
   2853 };
   2854 
   2855 void
   2856 tlp_sio_mii_tmsw_init(sc)
   2857 	struct tulip_softc *sc;
   2858 {
   2859 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2860 
   2861 	sc->sc_mii.mii_ifp = ifp;
   2862 	sc->sc_mii.mii_readreg = tlp_sio_mii_readreg;
   2863 	sc->sc_mii.mii_writereg = tlp_sio_mii_writereg;
   2864 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   2865 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   2866 	    tlp_mediastatus);
   2867 	mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff);
   2868 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   2869 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
   2870 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
   2871 	} else {
   2872 		sc->sc_flags |= TULIPF_HAS_MII;
   2873 		sc->sc_tick = tlp_mii_tick;
   2874 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   2875 	}
   2876 }
   2877 
   2878 /*
   2879  * Lite-On PNIC media switch.  Must handle MII or internal NWAY.
   2880  */
   2881 void	tlp_pnic_tmsw_init __P((struct tulip_softc *));
   2882 void	tlp_pnic_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
   2883 int	tlp_pnic_tmsw_set __P((struct tulip_softc *));
   2884 
   2885 const struct tulip_mediasw tlp_pnic_mediasw = {
   2886 	tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
   2887 };
   2888 
   2889 void	tlp_pnic_nway_statchg __P((struct device *));
   2890 void	tlp_pnic_nway_tick __P((void *));
   2891 int	tlp_pnic_nway_service __P((struct tulip_softc *, int));
   2892 void	tlp_pnic_nway_reset __P((struct tulip_softc *));
   2893 int	tlp_pnic_nway_auto __P((struct tulip_softc *, int));
   2894 void	tlp_pnic_nway_auto_timeout __P((void *));
   2895 void	tlp_pnic_nway_status __P((struct tulip_softc *));
   2896 void	tlp_pnic_nway_acomp __P((struct tulip_softc *));
   2897 
   2898 void
   2899 tlp_pnic_tmsw_init(sc)
   2900 	struct tulip_softc *sc;
   2901 {
   2902 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2903 	const char *sep = "";
   2904 
   2905 #define	ADD(m, c)	ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL)
   2906 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
   2907 
   2908 	sc->sc_mii.mii_ifp = ifp;
   2909 	sc->sc_mii.mii_readreg = tlp_pnic_mii_readreg;
   2910 	sc->sc_mii.mii_writereg = tlp_pnic_mii_writereg;
   2911 	sc->sc_mii.mii_statchg = sc->sc_statchg;
   2912 	ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
   2913 	    tlp_mediastatus);
   2914 	mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff);
   2915 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
   2916 		/* XXX What about AUI/BNC support? */
   2917 		printf("%s: ", sc->sc_dev.dv_xname);
   2918 
   2919 		tlp_pnic_nway_reset(sc);
   2920 
   2921 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
   2922 		    PNIC_NWAY_TW|PNIC_NWAY_CAP10T);
   2923 		PRINT("10baseT");
   2924 
   2925 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
   2926 		    PNIC_NWAY_TW|PNIC_NWAY_FD|PNIC_NWAY_CAP10TFDX);
   2927 		PRINT("10baseT-FDX");
   2928 
   2929 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
   2930 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_CAP100TX);
   2931 		PRINT("100baseTX");
   2932 
   2933 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
   2934 		    PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_FD|
   2935 		    PNIC_NWAY_CAP100TXFDX);
   2936 		PRINT("100baseTX-FDX");
   2937 
   2938 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
   2939 		    PNIC_NWAY_TW|PNIC_NWAY_RN|PNIC_NWAY_NW|
   2940 		    PNIC_NWAY_CAP10T|PNIC_NWAY_CAP10TFDX|
   2941 		    PNIC_NWAY_CAP100TXFDX|PNIC_NWAY_CAP100TX);
   2942 		PRINT("auto");
   2943 
   2944 		printf("\n");
   2945 
   2946 		sc->sc_statchg = tlp_pnic_nway_statchg;
   2947 		sc->sc_tick = tlp_pnic_nway_tick;
   2948 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   2949 	} else {
   2950 		sc->sc_flags |= TULIPF_HAS_MII;
   2951 		sc->sc_tick = tlp_mii_tick;
   2952 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
   2953 	}
   2954 
   2955 #undef ADD
   2956 #undef PRINT
   2957 }
   2958 
   2959 void
   2960 tlp_pnic_tmsw_get(sc, ifmr)
   2961 	struct tulip_softc *sc;
   2962 	struct ifmediareq *ifmr;
   2963 {
   2964 	struct mii_data *mii = &sc->sc_mii;
   2965 
   2966 	if (sc->sc_flags & TULIPF_HAS_MII)
   2967 		tlp_mii_getmedia(sc, ifmr);
   2968 	else {
   2969 		mii->mii_media_status = 0;
   2970 		mii->mii_media_active = IFM_NONE;
   2971 		tlp_pnic_nway_service(sc, MII_POLLSTAT);
   2972 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
   2973 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
   2974 	}
   2975 }
   2976 
   2977 int
   2978 tlp_pnic_tmsw_set(sc)
   2979 	struct tulip_softc *sc;
   2980 {
   2981 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2982 	struct mii_data *mii = &sc->sc_mii;
   2983 
   2984 	if (sc->sc_flags & TULIPF_HAS_MII)
   2985 		return (tlp_mii_setmedia(sc));
   2986 
   2987 	if (ifp->if_flags & IFF_UP) {
   2988 		mii->mii_media_status = 0;
   2989 		mii->mii_media_active = IFM_NONE;
   2990 		return (tlp_pnic_nway_service(sc, MII_MEDIACHG));
   2991 	}
   2992 
   2993 	return (0);
   2994 }
   2995 
   2996 void
   2997 tlp_pnic_nway_statchg(self)
   2998 	struct device *self;
   2999 {
   3000 	struct tulip_softc *sc = (struct tulip_softc *)self;
   3001 
   3002 	/* Idle the transmit and receive processes. */
   3003 	tlp_idle(sc, OPMODE_ST|OPMODE_SR);
   3004 
   3005 	/*
   3006 	 * XXX What about Heartbeat Disable?  Is it magically frobbed
   3007 	 * XXX by the PHY?  I hope so...
   3008 	 */
   3009 
   3010 	sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_PS|OPMODE_PCS|
   3011 	    OPMODE_SCR);
   3012 
   3013 	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
   3014 		sc->sc_opmode |= OPMODE_TTM;
   3015 		TULIP_WRITE(sc, CSR_GPP,
   3016 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) |
   3017 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
   3018 	} else {
   3019 		sc->sc_opmode |= OPMODE_PS|OPMODE_PCS|OPMODE_SCR;
   3020 		TULIP_WRITE(sc, CSR_GPP,
   3021 		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) |
   3022 		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
   3023 	}
   3024 
   3025 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   3026 		sc->sc_opmode |= OPMODE_FD;
   3027 
   3028 	/*
   3029 	 * Write new OPMODE bits.  This also restarts the transmit
   3030 	 * and receive processes.
   3031 	 */
   3032 	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
   3033 
   3034 	/* XXX Update ifp->if_baudrate */
   3035 }
   3036 
   3037 void
   3038 tlp_pnic_nway_tick(arg)
   3039 	void *arg;
   3040 {
   3041 	struct tulip_softc *sc = arg;
   3042 	int s;
   3043 
   3044 	s = splnet();
   3045 	tlp_pnic_nway_service(sc, MII_TICK);
   3046 	splx(s);
   3047 
   3048 	timeout(tlp_pnic_nway_tick, sc, hz);
   3049 }
   3050 
   3051 /*
   3052  * Support for the Lite-On PNIC internal NWay block.  This is constructed
   3053  * somewhat like a PHY driver for simplicity.
   3054  */
   3055 
   3056 int
   3057 tlp_pnic_nway_service(sc, cmd)
   3058 	struct tulip_softc *sc;
   3059 	int cmd;
   3060 {
   3061 	struct mii_data *mii = &sc->sc_mii;
   3062 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   3063 
   3064 	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
   3065 		return (0);
   3066 
   3067 	switch (cmd) {
   3068 	case MII_POLLSTAT:
   3069 		/* Nothing special to do here. */
   3070 		break;
   3071 
   3072 	case MII_MEDIACHG:
   3073 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   3074 		case IFM_AUTO:
   3075 			(void) tlp_pnic_nway_auto(sc, 1);
   3076 			break;
   3077 		case IFM_100_T4:
   3078 			/*
   3079 			 * XXX Not supported as a manual setting right now.
   3080 			 */
   3081 			return (EINVAL);
   3082 		default:
   3083 			/*
   3084 			 * NWAY register data is stored in the ifmedia entry.
   3085 			 */
   3086 			TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   3087 		}
   3088 		break;
   3089 
   3090 	case MII_TICK:
   3091 		/*
   3092 		 * Only used for autonegotiation.
   3093 		 */
   3094 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   3095 			return (0);
   3096 
   3097 		/*
   3098 		 * Check to see if we have link.  If we do, we don't
   3099 		 * need to restart the autonegotiation process.
   3100 		 */
   3101 		if (sc->sc_flags & TULIPF_LINK_UP)
   3102 			return (0);
   3103 
   3104 		/*
   3105 		 * Only retry autonegotiation every 5 seconds.
   3106 		 */
   3107 		if (++sc->sc_nway_ticks != 5)
   3108 			return (0);
   3109 
   3110 		sc->sc_nway_ticks = 0;
   3111 		tlp_pnic_nway_reset(sc);
   3112 		if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
   3113 			return (0);
   3114 		break;
   3115 	}
   3116 
   3117 	/* Update the media status. */
   3118 	tlp_pnic_nway_status(sc);
   3119 
   3120 	/* Callback if something changed. */
   3121 	if (sc->sc_nway_active != mii->mii_media_active ||
   3122 	    cmd == MII_MEDIACHG) {
   3123 		(*sc->sc_statchg)(&sc->sc_dev);
   3124 		sc->sc_nway_active = mii->mii_media_active;
   3125 	}
   3126 	return (0);
   3127 }
   3128 
   3129 void
   3130 tlp_pnic_nway_reset(sc)
   3131 	struct tulip_softc *sc;
   3132 {
   3133 
   3134 	TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
   3135 	delay(100);
   3136 	TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
   3137 }
   3138 
   3139 int
   3140 tlp_pnic_nway_auto(sc, waitfor)
   3141 	struct tulip_softc *sc;
   3142 	int waitfor;
   3143 {
   3144 	struct mii_data *mii = &sc->sc_mii;
   3145 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   3146 	u_int32_t reg;
   3147 	int i;
   3148 
   3149 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
   3150 		TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
   3151 
   3152 	if (waitfor) {
   3153 		/* Wait 500ms for it to complete. */
   3154 		for (i = 0; i < 500; i++) {
   3155 			reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   3156 			if (reg & PNIC_NWAY_LPAR_MASK) {
   3157 				tlp_pnic_nway_acomp(sc);
   3158 				return (0);
   3159 			}
   3160 			delay(1000);
   3161 		}
   3162 #if 0
   3163 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   3164 			printf("%s: autonegotiation failed to complete\n",
   3165 			    sc->sc_dev.dv_xname);
   3166 #endif
   3167 
   3168 		/*
   3169 		 * Don't need to worry about clearing DOINGAUTO.
   3170 		 * If that's set, a timeout is pending, and it will
   3171 		 * clear the flag.
   3172 		 */
   3173 		return (EIO);
   3174 	}
   3175 
   3176 	/*
   3177 	 * Just let it finish asynchronously.  This is for the benefit of
   3178 	 * the tick handler driving autonegotiation.  Don't want 500ms
   3179 	 * delays all the time while the system is running!
   3180 	 */
   3181 	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
   3182 		sc->sc_flags |= TULIPF_DOINGAUTO;
   3183 		timeout(tlp_pnic_nway_auto_timeout, sc, hz >> 1);
   3184 	}
   3185 	return (EJUSTRETURN);
   3186 }
   3187 
   3188 void
   3189 tlp_pnic_nway_auto_timeout(arg)
   3190 	void *arg;
   3191 {
   3192 	struct tulip_softc *sc = arg;
   3193 	u_int32_t reg;
   3194 	int s;
   3195 
   3196 	s = splnet();
   3197 	sc->sc_flags &= ~TULIPF_DOINGAUTO;
   3198 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   3199 #if 0
   3200 	if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
   3201 		printf("%s: autonegotiation failed to complete\n",
   3202 		    sc->sc_dev.dv_xname);
   3203 #endif
   3204 
   3205 	tlp_pnic_nway_acomp(sc);
   3206 
   3207 	/* Update the media status. */
   3208 	(void) tlp_pnic_nway_service(sc, MII_POLLSTAT);
   3209 	splx(s);
   3210 }
   3211 
   3212 void
   3213 tlp_pnic_nway_status(sc)
   3214 	struct tulip_softc *sc;
   3215 {
   3216 	struct mii_data *mii = &sc->sc_mii;
   3217 	u_int32_t reg;
   3218 
   3219 	mii->mii_media_status = IFM_AVALID;
   3220 	mii->mii_media_active = IFM_ETHER;
   3221 
   3222 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   3223 
   3224 	if (sc->sc_flags & TULIPF_LINK_UP)
   3225 		mii->mii_media_status |= IFM_ACTIVE;
   3226 
   3227 	if (reg & PNIC_NWAY_NW) {
   3228 		if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
   3229 			/* Erg, still trying, I guess... */
   3230 			mii->mii_media_active |= IFM_NONE;
   3231 			return;
   3232 		}
   3233 
   3234 #if 0
   3235 		if (reg & PNIC_NWAY_LPAR100T4)
   3236 			mii->mii_media_active |= IFM_100_T4;
   3237 		else
   3238 #endif
   3239 		if (reg & PNIC_NWAY_LPAR100TXFDX)
   3240 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
   3241 		else if (reg & PNIC_NWAY_LPAR100TX)
   3242 			mii->mii_media_active |= IFM_100_TX;
   3243 		else if (reg & PNIC_NWAY_LPAR10TFDX)
   3244 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
   3245 		else if (reg & PNIC_NWAY_LPAR10T)
   3246 			mii->mii_media_active |= IFM_10_T;
   3247 		else
   3248 			mii->mii_media_active |= IFM_NONE;
   3249 	} else {
   3250 		if (reg & PNIC_NWAY_100)
   3251 			mii->mii_media_active |= IFM_100_TX;
   3252 		else
   3253 			mii->mii_media_active |= IFM_10_T;
   3254 		if (reg & PNIC_NWAY_FD)
   3255 			mii->mii_media_active |= IFM_FDX;
   3256 	}
   3257 }
   3258 
   3259 void
   3260 tlp_pnic_nway_acomp(sc)
   3261 	struct tulip_softc *sc;
   3262 {
   3263 	u_int32_t reg;
   3264 
   3265 	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
   3266 	reg &= ~(PNIC_NWAY_FD|PNIC_NWAY_100|PNIC_NWAY_RN);
   3267 
   3268 	if (reg & (PNIC_NWAY_LPAR100TXFDX|PNIC_NWAY_LPAR100TX))
   3269 		reg |= PNIC_NWAY_100;
   3270 	if (reg & (PNIC_NWAY_LPAR10TFDX|PNIC_NWAY_LPAR100TXFDX))
   3271 		reg |= PNIC_NWAY_FD;
   3272 
   3273 	TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
   3274 }
   3275