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