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