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