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