Home | History | Annotate | Line # | Download | only in ic
elink3.c revision 1.86
      1 /*	$NetBSD: elink3.c,v 1.86 2000/12/14 06:27:24 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 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  * Copyright (c) 1996, 1997 Jonathan Stone <jonathan (at) NetBSD.org>
     42  * Copyright (c) 1994 Herb Peyerl <hpeyerl (at) beer.org>
     43  * All rights reserved.
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *    notice, this list of conditions and the following disclaimer in the
     52  *    documentation and/or other materials provided with the distribution.
     53  * 3. All advertising materials mentioning features or use of this software
     54  *    must display the following acknowledgement:
     55  *      This product includes software developed by Herb Peyerl.
     56  * 4. The name of Herb Peyerl may not be used to endorse or promote products
     57  *    derived from this software without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     60  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     61  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     62  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     63  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     64  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     65  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     66  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     67  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     68  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     69  */
     70 
     71 #include "opt_inet.h"
     72 #include "opt_ns.h"
     73 #include "bpfilter.h"
     74 #include "rnd.h"
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/callout.h>
     79 #include <sys/kernel.h>
     80 #include <sys/mbuf.h>
     81 #include <sys/socket.h>
     82 #include <sys/ioctl.h>
     83 #include <sys/errno.h>
     84 #include <sys/syslog.h>
     85 #include <sys/select.h>
     86 #include <sys/device.h>
     87 #if NRND > 0
     88 #include <sys/rnd.h>
     89 #endif
     90 
     91 #include <net/if.h>
     92 #include <net/if_dl.h>
     93 #include <net/if_ether.h>
     94 #include <net/if_media.h>
     95 
     96 #ifdef INET
     97 #include <netinet/in.h>
     98 #include <netinet/in_systm.h>
     99 #include <netinet/in_var.h>
    100 #include <netinet/ip.h>
    101 #include <netinet/if_inarp.h>
    102 #endif
    103 
    104 #ifdef NS
    105 #include <netns/ns.h>
    106 #include <netns/ns_if.h>
    107 #endif
    108 
    109 #if NBPFILTER > 0
    110 #include <net/bpf.h>
    111 #include <net/bpfdesc.h>
    112 #endif
    113 
    114 #include <machine/cpu.h>
    115 #include <machine/bus.h>
    116 #include <machine/intr.h>
    117 
    118 #include <dev/mii/mii.h>
    119 #include <dev/mii/miivar.h>
    120 #include <dev/mii/mii_bitbang.h>
    121 
    122 #include <dev/ic/elink3var.h>
    123 #include <dev/ic/elink3reg.h>
    124 
    125 #ifdef DEBUG
    126 int epdebug = 0;
    127 #endif
    128 
    129 /*
    130  * XXX endian workaround for big-endian CPUs  with pcmcia:
    131  * if stream methods for bus_space_multi are not provided, define them
    132  * using non-stream bus_space_{read,write}_multi_.
    133  * Assumes host CPU is same endian-ness as bus.
    134  */
    135 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
    136 #define bus_space_read_multi_stream_2	bus_space_read_multi_2
    137 #define bus_space_read_multi_stream_4	bus_space_read_multi_4
    138 #define bus_space_write_multi_stream_2	bus_space_write_multi_2
    139 #define bus_space_write_multi_stream_4	bus_space_write_multi_4
    140 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
    141 
    142 /*
    143  * Structure to map media-present bits in boards to ifmedia codes and
    144  * printable media names. Used for table-driven ifmedia initialization.
    145  */
    146 struct ep_media {
    147 	int	epm_mpbit;		/* media present bit */
    148 	const char *epm_name;		/* name of medium */
    149 	int	epm_ifmedia;		/* ifmedia word for medium */
    150 	int	epm_epmedia;		/* ELINKMEDIA_* constant */
    151 };
    152 
    153 /*
    154  * Media table for the Demon/Vortex/Boomerang chipsets.
    155  *
    156  * Note that MII on the Demon and Vortex (3c59x) indicates an external
    157  * MII connector (for connecting an external PHY) ... I think.  Treat
    158  * it as `manual' on these chips.
    159  *
    160  * Any Boomerang (3c90x) chips with MII really do have an internal
    161  * MII and real PHYs attached; no `native' media.
    162  */
    163 struct ep_media ep_vortex_media[] = {
    164 	{ ELINK_PCI_10BASE_T,	"10baseT",	IFM_ETHER|IFM_10_T,
    165 	  ELINKMEDIA_10BASE_T },
    166 	{ ELINK_PCI_10BASE_T,	"10baseT-FDX",	IFM_ETHER|IFM_10_T|IFM_FDX,
    167 	  ELINKMEDIA_10BASE_T },
    168 	{ ELINK_PCI_AUI,	"10base5",	IFM_ETHER|IFM_10_5,
    169 	  ELINKMEDIA_AUI },
    170 	{ ELINK_PCI_BNC,	"10base2",	IFM_ETHER|IFM_10_2,
    171 	  ELINKMEDIA_10BASE_2 },
    172 	{ ELINK_PCI_100BASE_TX,	"100baseTX",	IFM_ETHER|IFM_100_TX,
    173 	  ELINKMEDIA_100BASE_TX },
    174 	{ ELINK_PCI_100BASE_TX,	"100baseTX-FDX",IFM_ETHER|IFM_100_TX|IFM_FDX,
    175 	  ELINKMEDIA_100BASE_TX },
    176 	{ ELINK_PCI_100BASE_FX,	"100baseFX",	IFM_ETHER|IFM_100_FX,
    177 	  ELINKMEDIA_100BASE_FX },
    178 	{ ELINK_PCI_100BASE_MII,"manual",	IFM_ETHER|IFM_MANUAL,
    179 	  ELINKMEDIA_MII },
    180 	{ ELINK_PCI_100BASE_T4,	"100baseT4",	IFM_ETHER|IFM_100_T4,
    181 	  ELINKMEDIA_100BASE_T4 },
    182 	{ 0,			NULL,		0,
    183 	  0 },
    184 };
    185 
    186 /*
    187  * Media table for the older 3Com Etherlink III chipset, used
    188  * in the 3c509, 3c579, and 3c589.
    189  */
    190 struct ep_media ep_509_media[] = {
    191 	{ ELINK_W0_CC_UTP,	"10baseT",	IFM_ETHER|IFM_10_T,
    192 	  ELINKMEDIA_10BASE_T },
    193 	{ ELINK_W0_CC_AUI,	"10base5",	IFM_ETHER|IFM_10_5,
    194 	  ELINKMEDIA_AUI },
    195 	{ ELINK_W0_CC_BNC,	"10base2",	IFM_ETHER|IFM_10_2,
    196 	  ELINKMEDIA_10BASE_2 },
    197 	{ 0,			NULL,		0,
    198 	  0 },
    199 };
    200 
    201 void	ep_internalconfig __P((struct ep_softc *sc));
    202 void	ep_vortex_probemedia __P((struct ep_softc *sc));
    203 void	ep_509_probemedia __P((struct ep_softc *sc));
    204 
    205 static void eptxstat __P((struct ep_softc *));
    206 static int epstatus __P((struct ep_softc *));
    207 void	epinit __P((struct ep_softc *));
    208 int	epioctl __P((struct ifnet *, u_long, caddr_t));
    209 void	epstart __P((struct ifnet *));
    210 void	epwatchdog __P((struct ifnet *));
    211 void	epreset __P((struct ep_softc *));
    212 static void epshutdown __P((void *));
    213 void	epread __P((struct ep_softc *));
    214 struct mbuf *epget __P((struct ep_softc *, int));
    215 void	epmbuffill __P((void *));
    216 void	epmbufempty __P((struct ep_softc *));
    217 void	epsetfilter __P((struct ep_softc *));
    218 void	ep_roadrunner_mii_enable __P((struct ep_softc *));
    219 void	epsetmedia __P((struct ep_softc *));
    220 
    221 /* ifmedia callbacks */
    222 int	ep_media_change __P((struct ifnet *ifp));
    223 void	ep_media_status __P((struct ifnet *ifp, struct ifmediareq *req));
    224 
    225 /* MII callbacks */
    226 int	ep_mii_readreg __P((struct device *, int, int));
    227 void	ep_mii_writereg __P((struct device *, int, int, int));
    228 void	ep_statchg __P((struct device *));
    229 
    230 void	ep_tick __P((void *));
    231 
    232 static int epbusyeeprom __P((struct ep_softc *));
    233 u_int16_t ep_read_eeprom __P((struct ep_softc *, u_int16_t));
    234 static inline void ep_reset_cmd __P((struct ep_softc *sc,
    235 					u_int cmd, u_int arg));
    236 static inline void ep_finish_reset __P((bus_space_tag_t, bus_space_handle_t));
    237 static inline void ep_discard_rxtop __P((bus_space_tag_t, bus_space_handle_t));
    238 static __inline int ep_w1_reg __P((struct ep_softc *, int));
    239 
    240 /*
    241  * MII bit-bang glue.
    242  */
    243 u_int32_t ep_mii_bitbang_read __P((struct device *));
    244 void ep_mii_bitbang_write __P((struct device *, u_int32_t));
    245 
    246 const struct mii_bitbang_ops ep_mii_bitbang_ops = {
    247 	ep_mii_bitbang_read,
    248 	ep_mii_bitbang_write,
    249 	{
    250 		PHYSMGMT_DATA,		/* MII_BIT_MDO */
    251 		PHYSMGMT_DATA,		/* MII_BIT_MDI */
    252 		PHYSMGMT_CLK,		/* MII_BIT_MDC */
    253 		PHYSMGMT_DIR,		/* MII_BIT_DIR_HOST_PHY */
    254 		0,			/* MII_BIT_DIR_PHY_HOST */
    255 	}
    256 };
    257 
    258 /*
    259  * Some chips (3c515 [Corkscrew] and 3c574 [RoadRunner]) have
    260  * Window 1 registers offset!
    261  */
    262 static __inline int
    263 ep_w1_reg(sc, reg)
    264 	struct ep_softc *sc;
    265 	int reg;
    266 {
    267 
    268 	switch (sc->ep_chipset) {
    269 	case ELINK_CHIPSET_CORKSCREW:
    270 		return (reg + 0x10);
    271 
    272 	case ELINK_CHIPSET_ROADRUNNER:
    273 		switch (reg) {
    274 		case ELINK_W1_FREE_TX:
    275 		case ELINK_W1_RUNNER_RDCTL:
    276 		case ELINK_W1_RUNNER_WRCTL:
    277 			return (reg);
    278 		}
    279 		return (reg + 0x10);
    280 	}
    281 
    282 	return (reg);
    283 }
    284 
    285 /*
    286  * Wait for any pending reset to complete.
    287  * On newer hardware we could poll SC_COMMAND_IN_PROGRESS,
    288  * but older hardware doesn't implement it and we must delay.
    289  */
    290 static inline void
    291 ep_finish_reset(iot, ioh)
    292 	bus_space_tag_t iot;
    293 	bus_space_handle_t ioh;
    294 {
    295 	int i;
    296 
    297 	for (i = 0; i < 10000; i++) {
    298 		if ((bus_space_read_2(iot, ioh, ELINK_STATUS) &
    299 		    S_COMMAND_IN_PROGRESS) == 0)
    300 			break;
    301 		DELAY(10);
    302 	}
    303 }
    304 
    305 /*
    306  * Issue a (reset) command, and be sure it has completed.
    307  * Used for global reset, TX_RESET, RX_RESET.
    308  */
    309 static inline void
    310 ep_reset_cmd(sc, cmd, arg)
    311 	struct ep_softc *sc;
    312 	u_int cmd, arg;
    313 {
    314 	bus_space_tag_t iot = sc->sc_iot;
    315 	bus_space_handle_t ioh = sc->sc_ioh;
    316 
    317 	bus_space_write_2(iot, ioh, cmd, arg);
    318 	ep_finish_reset(iot, ioh);
    319 }
    320 
    321 
    322 static inline void
    323 ep_discard_rxtop(iot, ioh)
    324 	bus_space_tag_t iot;
    325 	bus_space_handle_t ioh;
    326 {
    327 	int i;
    328 
    329 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_DISCARD_TOP_PACK);
    330 
    331         /*
    332 	 * Spin for about 1 msec, to avoid forcing a DELAY() between
    333 	 * every received packet (adding latency and  limiting pkt-recv rate).
    334 	 * On PCI, at 4 30-nsec PCI bus cycles for a read, 8000 iterations
    335 	 * is about right.
    336 	 */
    337 	for (i = 0; i < 8000; i++) {
    338 		if ((bus_space_read_2(iot, ioh, ELINK_STATUS) &
    339 		    S_COMMAND_IN_PROGRESS) == 0)
    340 		    return;
    341 	}
    342 
    343 	/*  Didn't complete in a hurry. Do DELAY()s. */
    344 	ep_finish_reset(iot, ioh);
    345 }
    346 
    347 /*
    348  * Back-end attach and configure.
    349  */
    350 int
    351 epconfig(sc, chipset, enaddr)
    352 	struct ep_softc *sc;
    353 	u_short chipset;
    354 	u_int8_t *enaddr;
    355 {
    356 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    357 	bus_space_tag_t iot = sc->sc_iot;
    358 	bus_space_handle_t ioh = sc->sc_ioh;
    359 	u_int16_t i;
    360 	u_int8_t myla[ETHER_ADDR_LEN];
    361 
    362 	callout_init(&sc->sc_mii_callout);
    363 	callout_init(&sc->sc_mbuf_callout);
    364 
    365 	sc->ep_chipset = chipset;
    366 
    367 	/*
    368 	 * We could have been groveling around in other register
    369 	 * windows in the front-end; make sure we're in window 0
    370 	 * to read the EEPROM.
    371 	 */
    372 	GO_WINDOW(0);
    373 
    374 	if (enaddr == NULL) {
    375 		/*
    376 		 * Read the station address from the eeprom.
    377 		 */
    378 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++) {
    379 			u_int16_t x = ep_read_eeprom(sc, i);
    380 			myla[(i << 1)] = x >> 8;
    381 			myla[(i << 1) + 1] = x;
    382 		}
    383 		enaddr = myla;
    384 	}
    385 
    386 	/*
    387 	 * Vortex-based (3c59x pci,eisa) and Boomerang (3c900) cards
    388 	 * allow FDDI-sized (4500) byte packets.  Commands only take an
    389 	 * 11-bit parameter, and  11 bits isn't enough to hold a full-size
    390 	 * packet length.
    391 	 * Commands to these cards implicitly upshift a packet size
    392 	 * or threshold by 2 bits.
    393 	 * To detect  cards with large-packet support, we probe by setting
    394 	 * the transmit threshold register, then change windows and
    395 	 * read back the threshold register directly, and see if the
    396 	 * threshold value was shifted or not.
    397 	 */
    398 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    399 	    SET_TX_AVAIL_THRESH | ELINK_LARGEWIN_PROBE);
    400 	GO_WINDOW(5);
    401 	i = bus_space_read_2(iot, ioh, ELINK_W5_TX_AVAIL_THRESH);
    402 	GO_WINDOW(1);
    403 	switch (i)  {
    404 	case ELINK_LARGEWIN_PROBE:
    405 	case (ELINK_LARGEWIN_PROBE & ELINK_LARGEWIN_MASK):
    406 		sc->ep_pktlenshift = 0;
    407 		break;
    408 
    409 	case (ELINK_LARGEWIN_PROBE << 2):
    410 		sc->ep_pktlenshift = 2;
    411 		break;
    412 
    413 	default:
    414 		printf("%s: wrote 0x%x to TX_AVAIL_THRESH, read back 0x%x. "
    415 		    "Interface disabled\n",
    416 		    sc->sc_dev.dv_xname, ELINK_LARGEWIN_PROBE, (int) i);
    417 		return (1);
    418 	}
    419 
    420 	/*
    421 	 * Ensure Tx-available interrupts are enabled for
    422 	 * start the interface.
    423 	 * XXX should be in epinit()?
    424 	 */
    425 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    426 	    SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
    427 
    428 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    429 	ifp->if_softc = sc;
    430 	ifp->if_start = epstart;
    431 	ifp->if_ioctl = epioctl;
    432 	ifp->if_watchdog = epwatchdog;
    433 	ifp->if_flags =
    434 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    435 	IFQ_SET_READY(&ifp->if_snd);
    436 
    437 	if_attach(ifp);
    438 	ether_ifattach(ifp, enaddr);
    439 
    440 	/*
    441 	 * Finish configuration:
    442 	 * determine chipset if the front-end couldn't do so,
    443 	 * show board details, set media.
    444 	 */
    445 
    446 	/*
    447 	 * Print RAM size.  We also print the Ethernet address in here.
    448 	 * It's extracted from the ifp, so we have to make sure it's
    449 	 * been attached first.
    450 	 */
    451 	ep_internalconfig(sc);
    452 	GO_WINDOW(0);
    453 
    454 	/*
    455 	 * Display some additional information, if pertinent.
    456 	 */
    457 	if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER)
    458 		printf("%s: RoadRunner FIFO buffer enabled\n",
    459 		    sc->sc_dev.dv_xname);
    460 
    461 	/*
    462 	 * Initialize our media structures and MII info.  We'll
    463 	 * probe the MII if we discover that we have one.
    464 	 */
    465 	sc->sc_mii.mii_ifp = ifp;
    466 	sc->sc_mii.mii_readreg = ep_mii_readreg;
    467 	sc->sc_mii.mii_writereg = ep_mii_writereg;
    468 	sc->sc_mii.mii_statchg = ep_statchg;
    469 	ifmedia_init(&sc->sc_mii.mii_media, 0, ep_media_change,
    470 	    ep_media_status);
    471 
    472 	/*
    473 	 * Now, determine which media we have.
    474 	 */
    475 	switch (sc->ep_chipset) {
    476 	case ELINK_CHIPSET_ROADRUNNER:
    477 		if (sc->ep_flags & ELINK_FLAGS_MII) {
    478 			ep_roadrunner_mii_enable(sc);
    479 			GO_WINDOW(0);
    480 		}
    481 		/* FALLTHROUGH */
    482 
    483 	case ELINK_CHIPSET_BOOMERANG:
    484 		/*
    485 		 * If the device has MII, probe it.  We won't be using
    486 		 * any `native' media in this case, only PHYs.  If
    487 		 * we don't, just treat the Boomerang like the Vortex.
    488 		 */
    489 		if (sc->ep_flags & ELINK_FLAGS_MII) {
    490 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
    491 			    MII_PHY_ANY, MII_OFFSET_ANY, 0);
    492 			if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    493 				ifmedia_add(&sc->sc_mii.mii_media,
    494 				    IFM_ETHER|IFM_NONE, 0, NULL);
    495 				ifmedia_set(&sc->sc_mii.mii_media,
    496 				    IFM_ETHER|IFM_NONE);
    497 			} else {
    498 				ifmedia_set(&sc->sc_mii.mii_media,
    499 				    IFM_ETHER|IFM_AUTO);
    500 			}
    501 			break;
    502 		}
    503 		/* FALLTHROUGH */
    504 
    505 	case ELINK_CHIPSET_VORTEX:
    506 		ep_vortex_probemedia(sc);
    507 		break;
    508 
    509 	default:
    510 		ep_509_probemedia(sc);
    511 		break;
    512 	}
    513 
    514 	GO_WINDOW(1);		/* Window 1 is operating window */
    515 
    516 #if NRND > 0
    517 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    518 	    RND_TYPE_NET, 0);
    519 #endif
    520 
    521 	sc->tx_start_thresh = 20;	/* probably a good starting point. */
    522 
    523 	/*  Establish callback to reset card when we reboot. */
    524 	sc->sd_hook = shutdownhook_establish(epshutdown, sc);
    525 
    526 	ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
    527 	ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
    528 
    529 	/* The attach is successful. */
    530 	sc->sc_flags |= ELINK_FLAGS_ATTACHED;
    531 	return (0);
    532 }
    533 
    534 
    535 /*
    536  * Show interface-model-independent info from window 3
    537  * internal-configuration register.
    538  */
    539 void
    540 ep_internalconfig(sc)
    541 	struct ep_softc *sc;
    542 {
    543 	bus_space_tag_t iot = sc->sc_iot;
    544 	bus_space_handle_t ioh = sc->sc_ioh;
    545 
    546 	u_int config0;
    547 	u_int config1;
    548 
    549 	int  ram_size, ram_width, ram_speed, rom_size, ram_split;
    550 	/*
    551 	 * NVRAM buffer Rx:Tx config names for busmastering cards
    552 	 * (Demon, Vortex, and later).
    553 	 */
    554 	const char *onboard_ram_config[] = {
    555 		"5:3", "3:1", "1:1", "3:5" };
    556 
    557 	GO_WINDOW(3);
    558 	config0 = (u_int)bus_space_read_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG);
    559 	config1 = (u_int)bus_space_read_2(iot, ioh,
    560 	    ELINK_W3_INTERNAL_CONFIG + 2);
    561 	GO_WINDOW(0);
    562 
    563 	ram_size  = (config0 & CONFIG_RAMSIZE) >> CONFIG_RAMSIZE_SHIFT;
    564 	ram_width = (config0 & CONFIG_RAMWIDTH) >> CONFIG_RAMWIDTH_SHIFT;
    565 	ram_speed = (config0 & CONFIG_RAMSPEED) >> CONFIG_RAMSPEED_SHIFT;
    566 	rom_size  = (config0 & CONFIG_ROMSIZE) >> CONFIG_ROMSIZE_SHIFT;
    567 
    568 	ram_split  = (config1 & CONFIG_RAMSPLIT) >> CONFIG_RAMSPLIT_SHIFT;
    569 
    570 	printf("%s: address %s, %dKB %s-wide FIFO, %s Rx:Tx split\n",
    571 	       sc->sc_dev.dv_xname,
    572 	       ether_sprintf(LLADDR(sc->sc_ethercom.ec_if.if_sadl)),
    573 	       8 << ram_size,
    574 	       (ram_width) ? "word" : "byte",
    575 	       onboard_ram_config[ram_split]);
    576 }
    577 
    578 
    579 /*
    580  * Find supported media on 3c509-generation hardware that doesn't have
    581  * a "reset_options" register in window 3.
    582  * Use the config_cntrl register  in window 0 instead.
    583  * Used on original, 10Mbit ISA (3c509), 3c509B, and pre-Demon EISA cards
    584  * that implement  CONFIG_CTRL.  We don't have a good way to set the
    585  * default active mediuim; punt to ifconfig  instead.
    586  */
    587 void
    588 ep_509_probemedia(sc)
    589 	struct ep_softc *sc;
    590 {
    591 	bus_space_tag_t iot = sc->sc_iot;
    592 	bus_space_handle_t ioh = sc->sc_ioh;
    593 	struct ifmedia *ifm = &sc->sc_mii.mii_media;
    594 	u_int16_t ep_w0_config, port;
    595 	struct ep_media *epm;
    596 	const char *sep = "", *defmedianame = NULL;
    597 	int defmedia = 0;
    598 
    599 	GO_WINDOW(0);
    600 	ep_w0_config = bus_space_read_2(iot, ioh, ELINK_W0_CONFIG_CTRL);
    601 
    602 	printf("%s: ", sc->sc_dev.dv_xname);
    603 
    604 	/* Sanity check that there are any media! */
    605 	if ((ep_w0_config & ELINK_W0_CC_MEDIAMASK) == 0) {
    606 		printf("no media present!\n");
    607 		ifmedia_add(ifm, IFM_ETHER|IFM_NONE, 0, NULL);
    608 		ifmedia_set(ifm, IFM_ETHER|IFM_NONE);
    609 		return;
    610 	}
    611 
    612 	/*
    613 	 * Get the default media from the EEPROM.
    614 	 */
    615 	port = ep_read_eeprom(sc, EEPROM_ADDR_CFG) >> 14;
    616 
    617 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
    618 
    619 	for (epm = ep_509_media; epm->epm_name != NULL; epm++) {
    620 		if (ep_w0_config & epm->epm_mpbit) {
    621 			/*
    622 			 * This simple test works because 509 chipsets
    623 			 * don't do full-duplex.
    624 			 */
    625 			if (epm->epm_epmedia == port || defmedia == 0) {
    626 				defmedia = epm->epm_ifmedia;
    627 				defmedianame = epm->epm_name;
    628 			}
    629 			ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_epmedia,
    630 			    NULL);
    631 			PRINT(epm->epm_name);
    632 		}
    633 	}
    634 
    635 #undef PRINT
    636 
    637 #ifdef DIAGNOSTIC
    638 	if (defmedia == 0)
    639 		panic("ep_509_probemedia: impossible");
    640 #endif
    641 
    642 	printf(" (default %s)\n", defmedianame);
    643 	ifmedia_set(ifm, defmedia);
    644 }
    645 
    646 /*
    647  * Find media present on large-packet-capable elink3 devices.
    648  * Show onboard configuration of large-packet-capable elink3 devices
    649  * (Demon, Vortex, Boomerang), which do not implement CONFIG_CTRL in window 0.
    650  * Use media and card-version info in window 3 instead.
    651  */
    652 void
    653 ep_vortex_probemedia(sc)
    654 	struct ep_softc *sc;
    655 {
    656 	bus_space_tag_t iot = sc->sc_iot;
    657 	bus_space_handle_t ioh = sc->sc_ioh;
    658 	struct ifmedia *ifm = &sc->sc_mii.mii_media;
    659 	struct ep_media *epm;
    660 	u_int config1;
    661 	int reset_options;
    662 	int default_media;	/* 3-bit encoding of default (EEPROM) media */
    663 	int defmedia = 0;
    664 	const char *sep = "", *defmedianame = NULL;
    665 
    666 	GO_WINDOW(3);
    667 	config1 = (u_int)bus_space_read_2(iot, ioh,
    668 	    ELINK_W3_INTERNAL_CONFIG + 2);
    669 	reset_options = (int)bus_space_read_1(iot, ioh, ELINK_W3_RESET_OPTIONS);
    670 	GO_WINDOW(0);
    671 
    672 	default_media = (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
    673 
    674 	printf("%s: ", sc->sc_dev.dv_xname);
    675 
    676 	/* Sanity check that there are any media! */
    677 	if ((reset_options & ELINK_PCI_MEDIAMASK) == 0) {
    678 		printf("no media present!\n");
    679 		ifmedia_add(ifm, IFM_ETHER|IFM_NONE, 0, NULL);
    680 		ifmedia_set(ifm, IFM_ETHER|IFM_NONE);
    681 		return;
    682 	}
    683 
    684 #define	PRINT(s)	printf("%s%s", sep, s); sep = ", "
    685 
    686 	for (epm = ep_vortex_media; epm->epm_name != NULL; epm++) {
    687 		if (reset_options & epm->epm_mpbit) {
    688 			/*
    689 			 * Default media is a little more complicated
    690 			 * on the Vortex.  We support full-duplex which
    691 			 * uses the same reset options bit.
    692 			 *
    693 			 * XXX Check EEPROM for default to FDX?
    694 			 */
    695 			if (epm->epm_epmedia == default_media) {
    696 				if ((epm->epm_ifmedia & IFM_FDX) == 0) {
    697 					defmedia = epm->epm_ifmedia;
    698 					defmedianame = epm->epm_name;
    699 				}
    700 			} else if (defmedia == 0) {
    701 				defmedia = epm->epm_ifmedia;
    702 				defmedianame = epm->epm_name;
    703 			}
    704 			ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_epmedia,
    705 			    NULL);
    706 			PRINT(epm->epm_name);
    707 		}
    708 	}
    709 
    710 #undef PRINT
    711 
    712 #ifdef DIAGNOSTIC
    713 	if (defmedia == 0)
    714 		panic("ep_vortex_probemedia: impossible");
    715 #endif
    716 
    717 	printf(" (default %s)\n", defmedianame);
    718 	ifmedia_set(ifm, defmedia);
    719 }
    720 
    721 /*
    722  * One second timer, used to tick the MII.
    723  */
    724 void
    725 ep_tick(arg)
    726 	void *arg;
    727 {
    728 	struct ep_softc *sc = arg;
    729 	int s;
    730 
    731 #ifdef DIAGNOSTIC
    732 	if ((sc->ep_flags & ELINK_FLAGS_MII) == 0)
    733 		panic("ep_tick");
    734 #endif
    735 
    736 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    737 		return;
    738 
    739 	s = splnet();
    740 	mii_tick(&sc->sc_mii);
    741 	splx(s);
    742 
    743 	callout_reset(&sc->sc_mii_callout, hz, ep_tick, sc);
    744 }
    745 
    746 /*
    747  * Bring device up.
    748  *
    749  * The order in here seems important. Otherwise we may not receive
    750  * interrupts. ?!
    751  */
    752 void
    753 epinit(sc)
    754 	struct ep_softc *sc;
    755 {
    756 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    757 	bus_space_tag_t iot = sc->sc_iot;
    758 	bus_space_handle_t ioh = sc->sc_ioh;
    759 	int i;
    760 
    761 	/* Make sure any pending reset has completed before touching board. */
    762 	ep_finish_reset(iot, ioh);
    763 
    764 	/*
    765 	 * Cance any pending I/O.
    766 	 */
    767 	epstop(sc);
    768 
    769 	if (sc->bustype != ELINK_BUS_PCI && sc->bustype != ELINK_BUS_EISA) {
    770 		GO_WINDOW(0);
    771 		bus_space_write_2(iot, ioh, ELINK_W0_CONFIG_CTRL, 0);
    772 		bus_space_write_2(iot, ioh, ELINK_W0_CONFIG_CTRL,
    773 		    ENABLE_DRQ_IRQ);
    774 	}
    775 
    776 	if (sc->bustype == ELINK_BUS_PCMCIA) {
    777 		bus_space_write_2(iot, ioh, ELINK_W0_RESOURCE_CFG, 0x3f00);
    778 	}
    779 
    780 	GO_WINDOW(2);
    781 	for (i = 0; i < 6; i++)	/* Reload the ether_addr. */
    782 		bus_space_write_1(iot, ioh, ELINK_W2_ADDR_0 + i,
    783 		    LLADDR(ifp->if_sadl)[i]);
    784 
    785 	/*
    786 	 * Reset the station-address receive filter.
    787 	 * A bug workaround for busmastering (Vortex, Demon) cards.
    788 	 */
    789 	for (i = 0; i < 6; i++)
    790 		bus_space_write_1(iot, ioh, ELINK_W2_RECVMASK_0 + i, 0);
    791 
    792 	ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
    793 	ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
    794 
    795 	GO_WINDOW(1);		/* Window 1 is operating window */
    796 	for (i = 0; i < 31; i++)
    797 		bus_space_read_1(iot, ioh, ep_w1_reg(sc, ELINK_W1_TX_STATUS));
    798 
    799 	/* Set threshhold for for Tx-space avaiable interrupt. */
    800 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    801 	    SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
    802 
    803 	if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER) {
    804 		/*
    805 		 * Enable options in the PCMCIA LAN COR register, via
    806 		 * RoadRunner Window 1.
    807 		 *
    808 		 * XXX MAGIC CONSTANTS!
    809 		 */
    810 		u_int16_t cor;
    811 
    812 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, (1 << 11));
    813 
    814 		cor = bus_space_read_2(iot, ioh, 0) & ~0x30;
    815 		if (sc->ep_flags & ELINK_FLAGS_USESHAREDMEM)
    816 			cor |= 0x10;
    817 		if (sc->ep_flags & ELINK_FLAGS_FORCENOWAIT)
    818 			cor |= 0x20;
    819 		bus_space_write_2(iot, ioh, 0, cor);
    820 
    821 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_WRCTL, 0);
    822 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, 0);
    823 
    824 		if (sc->ep_flags & ELINK_FLAGS_MII) {
    825 			ep_roadrunner_mii_enable(sc);
    826 			GO_WINDOW(1);
    827 		}
    828 	}
    829 
    830 	/* Enable interrupts. */
    831 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    832 	    SET_RD_0_MASK | S_CARD_FAILURE | S_RX_COMPLETE | S_TX_COMPLETE |
    833 	    S_TX_AVAIL);
    834 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    835 	    SET_INTR_MASK | S_CARD_FAILURE | S_RX_COMPLETE | S_TX_COMPLETE |
    836 	    S_TX_AVAIL);
    837 
    838 	/*
    839 	 * Attempt to get rid of any stray interrupts that occured during
    840 	 * configuration.  On the i386 this isn't possible because one may
    841 	 * already be queued.  However, a single stray interrupt is
    842 	 * unimportant.
    843 	 */
    844 	bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR | 0xff);
    845 
    846 	epsetfilter(sc);
    847 	epsetmedia(sc);
    848 
    849 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_ENABLE);
    850 	bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_ENABLE);
    851 
    852 	epmbuffill(sc);
    853 
    854 	/* Interface is now `running', with no output active. */
    855 	ifp->if_flags |= IFF_RUNNING;
    856 	ifp->if_flags &= ~IFF_OACTIVE;
    857 
    858 	if (sc->ep_flags & ELINK_FLAGS_MII) {
    859 		/* Start the one second clock. */
    860 		callout_reset(&sc->sc_mii_callout, hz, ep_tick, sc);
    861 	}
    862 
    863 	/* Attempt to start output, if any. */
    864 	epstart(ifp);
    865 }
    866 
    867 
    868 /*
    869  * Set multicast receive filter.
    870  * elink3 hardware has no selective multicast filter in hardware.
    871  * Enable reception of all multicasts and filter in software.
    872  */
    873 void
    874 epsetfilter(sc)
    875 	struct ep_softc *sc;
    876 {
    877 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    878 
    879 	GO_WINDOW(1);		/* Window 1 is operating window */
    880 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND,
    881 	    SET_RX_FILTER | FIL_INDIVIDUAL | FIL_BRDCST |
    882 	    ((ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0) |
    883 	    ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0));
    884 }
    885 
    886 int
    887 ep_media_change(ifp)
    888 	struct ifnet *ifp;
    889 {
    890 	struct ep_softc *sc = ifp->if_softc;
    891 
    892 	if (sc->enabled && (ifp->if_flags & IFF_UP) != 0)
    893 		epreset(sc);
    894 
    895 	return (0);
    896 }
    897 
    898 /*
    899  * Reset and enable the MII on the RoadRunner.
    900  */
    901 void
    902 ep_roadrunner_mii_enable(sc)
    903 	struct ep_softc *sc;
    904 {
    905 	bus_space_tag_t iot = sc->sc_iot;
    906 	bus_space_handle_t ioh = sc->sc_ioh;
    907 
    908 	GO_WINDOW(3);
    909 	bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
    910 	    ELINK_PCI_100BASE_MII|ELINK_RUNNER_ENABLE_MII);
    911 	delay(1000);
    912 	bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
    913 	    ELINK_PCI_100BASE_MII|ELINK_RUNNER_MII_RESET|
    914 	    ELINK_RUNNER_ENABLE_MII);
    915 	ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
    916 	ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
    917 	delay(1000);
    918 	bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
    919 	    ELINK_PCI_100BASE_MII|ELINK_RUNNER_ENABLE_MII);
    920 }
    921 
    922 /*
    923  * Set the card to use the specified media.
    924  */
    925 void
    926 epsetmedia(sc)
    927 	struct ep_softc *sc;
    928 {
    929 	bus_space_tag_t iot = sc->sc_iot;
    930 	bus_space_handle_t ioh = sc->sc_ioh;
    931 
    932 	/* Turn everything off.  First turn off linkbeat and UTP. */
    933 	GO_WINDOW(4);
    934 	bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE, 0x0);
    935 
    936 	/* Turn off coax */
    937 	bus_space_write_2(iot, ioh, ELINK_COMMAND, STOP_TRANSCEIVER);
    938 	delay(1000);
    939 
    940 	/*
    941 	 * If the device has MII, select it, and then tell the
    942 	 * PHY which media to use.
    943 	 */
    944 	if (sc->ep_flags & ELINK_FLAGS_MII) {
    945 		int config0, config1;
    946 
    947 		GO_WINDOW(3);
    948 
    949 		if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER) {
    950 			int resopt;
    951 
    952 			resopt = bus_space_read_2(iot, ioh,
    953 			    ELINK_W3_RESET_OPTIONS);
    954 			bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
    955 			    resopt | ELINK_RUNNER_ENABLE_MII);
    956 		}
    957 
    958 		config0 = (u_int)bus_space_read_2(iot, ioh,
    959 		    ELINK_W3_INTERNAL_CONFIG);
    960 		config1 = (u_int)bus_space_read_2(iot, ioh,
    961 		    ELINK_W3_INTERNAL_CONFIG + 2);
    962 
    963 		config1 = config1 & ~CONFIG_MEDIAMASK;
    964 		config1 |= (ELINKMEDIA_MII << CONFIG_MEDIAMASK_SHIFT);
    965 
    966 		bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG, config0);
    967 		bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG + 2,
    968 		    config1);
    969 		GO_WINDOW(1);	/* back to operating window */
    970 
    971 		mii_mediachg(&sc->sc_mii);
    972 		return;
    973 	}
    974 
    975 	/*
    976 	 * Now turn on the selected media/transceiver.
    977 	 */
    978 	GO_WINDOW(4);
    979 	switch (IFM_SUBTYPE(sc->sc_mii.mii_media.ifm_cur->ifm_media)) {
    980 	case IFM_10_T:
    981 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
    982 		    JABBER_GUARD_ENABLE|LINKBEAT_ENABLE);
    983 		break;
    984 
    985 	case IFM_10_2:
    986 		bus_space_write_2(iot, ioh, ELINK_COMMAND, START_TRANSCEIVER);
    987 		DELAY(1000);	/* 50ms not enmough? */
    988 		break;
    989 
    990 	case IFM_100_TX:
    991 	case IFM_100_FX:
    992 	case IFM_100_T4:		/* XXX check documentation */
    993 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
    994 		    LINKBEAT_ENABLE);
    995 		DELAY(1000);	/* not strictly necessary? */
    996 		break;
    997 
    998 	case IFM_10_5:
    999 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
   1000 		    SQE_ENABLE);
   1001 		DELAY(1000);	/* not strictly necessary? */
   1002 		break;
   1003 
   1004 	case IFM_MANUAL:
   1005 		/*
   1006 		 * Nothing to do here; we are actually enabling the
   1007 		 * external PHY on the MII port.
   1008 		 */
   1009 		break;
   1010 
   1011 	case IFM_NONE:
   1012 		printf("%s: interface disabled\n", sc->sc_dev.dv_xname);
   1013 		return;
   1014 
   1015 	default:
   1016 		panic("epsetmedia: impossible");
   1017 	}
   1018 
   1019 	/*
   1020 	 * Tell the chip which port to use.
   1021 	 */
   1022 	switch (sc->ep_chipset) {
   1023 	case ELINK_CHIPSET_VORTEX:
   1024 	case ELINK_CHIPSET_BOOMERANG:
   1025 	    {
   1026 		int mctl, config0, config1;
   1027 
   1028 		GO_WINDOW(3);
   1029 		config0 = (u_int)bus_space_read_2(iot, ioh,
   1030 		    ELINK_W3_INTERNAL_CONFIG);
   1031 		config1 = (u_int)bus_space_read_2(iot, ioh,
   1032 		    ELINK_W3_INTERNAL_CONFIG + 2);
   1033 
   1034 		config1 = config1 & ~CONFIG_MEDIAMASK;
   1035 		config1 |= (sc->sc_mii.mii_media.ifm_cur->ifm_data <<
   1036 		    CONFIG_MEDIAMASK_SHIFT);
   1037 
   1038 		bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG, config0);
   1039 		bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG + 2,
   1040 		    config1);
   1041 
   1042 		mctl = bus_space_read_2(iot, ioh, ELINK_W3_MAC_CONTROL);
   1043 		if (sc->sc_mii.mii_media.ifm_cur->ifm_media & IFM_FDX)
   1044 			mctl |= MAC_CONTROL_FDX;
   1045 		else
   1046 			mctl &= ~MAC_CONTROL_FDX;
   1047 		bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL, mctl);
   1048 		break;
   1049 	    }
   1050 	default:
   1051 	    {
   1052 		int w0_addr_cfg;
   1053 
   1054 		GO_WINDOW(0);
   1055 		w0_addr_cfg = bus_space_read_2(iot, ioh, ELINK_W0_ADDRESS_CFG);
   1056 		w0_addr_cfg &= 0x3fff;
   1057 		bus_space_write_2(iot, ioh, ELINK_W0_ADDRESS_CFG, w0_addr_cfg |
   1058 		    (sc->sc_mii.mii_media.ifm_cur->ifm_data << 14));
   1059 		DELAY(1000);
   1060 		break;
   1061 	    }
   1062 	}
   1063 
   1064 	GO_WINDOW(1);		/* Window 1 is operating window */
   1065 }
   1066 
   1067 /*
   1068  * Get currently-selected media from card.
   1069  * (if_media callback, may be called before interface is brought up).
   1070  */
   1071 void
   1072 ep_media_status(ifp, req)
   1073 	struct ifnet *ifp;
   1074 	struct ifmediareq *req;
   1075 {
   1076 	struct ep_softc *sc = ifp->if_softc;
   1077 	bus_space_tag_t iot = sc->sc_iot;
   1078 	bus_space_handle_t ioh = sc->sc_ioh;
   1079 
   1080 	if (sc->enabled == 0) {
   1081 		req->ifm_active = IFM_ETHER|IFM_NONE;
   1082 		req->ifm_status = 0;
   1083 		return;
   1084 	}
   1085 
   1086 	/*
   1087 	 * If we have MII, go ask the PHY what's going on.
   1088 	 */
   1089 	if (sc->ep_flags & ELINK_FLAGS_MII) {
   1090 		mii_pollstat(&sc->sc_mii);
   1091 		req->ifm_active = sc->sc_mii.mii_media_active;
   1092 		req->ifm_status = sc->sc_mii.mii_media_status;
   1093 		return;
   1094 	}
   1095 
   1096 	/*
   1097 	 * Ok, at this point we claim that our active media is
   1098 	 * the currently selected media.  We'll update our status
   1099 	 * if our chipset allows us to detect link.
   1100 	 */
   1101 	req->ifm_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
   1102 	req->ifm_status = 0;
   1103 
   1104 	switch (sc->ep_chipset) {
   1105 	case ELINK_CHIPSET_VORTEX:
   1106 	case ELINK_CHIPSET_BOOMERANG:
   1107 		GO_WINDOW(4);
   1108 		req->ifm_status = IFM_AVALID;
   1109 		if (bus_space_read_2(iot, ioh, ELINK_W4_MEDIA_TYPE) &
   1110 		    LINKBEAT_DETECT)
   1111 			req->ifm_status |= IFM_ACTIVE;
   1112 		GO_WINDOW(1);	/* back to operating window */
   1113 		break;
   1114 	}
   1115 }
   1116 
   1117 
   1118 
   1119 /*
   1120  * Start outputting on the interface.
   1121  * Always called as splnet().
   1122  */
   1123 void
   1124 epstart(ifp)
   1125 	struct ifnet *ifp;
   1126 {
   1127 	struct ep_softc *sc = ifp->if_softc;
   1128 	bus_space_tag_t iot = sc->sc_iot;
   1129 	bus_space_handle_t ioh = sc->sc_ioh;
   1130 	struct mbuf *m, *m0;
   1131 	int sh, len, pad;
   1132 	bus_addr_t txreg;
   1133 
   1134 	/* Don't transmit if interface is busy or not running */
   1135 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1136 		return;
   1137 
   1138 startagain:
   1139 	/* Sneak a peek at the next packet */
   1140 	IFQ_POLL(&ifp->if_snd, m0);
   1141 	if (m0 == 0)
   1142 		return;
   1143 
   1144 	/* We need to use m->m_pkthdr.len, so require the header */
   1145 	if ((m0->m_flags & M_PKTHDR) == 0)
   1146 		panic("epstart: no header mbuf");
   1147 	len = m0->m_pkthdr.len;
   1148 
   1149 	pad = (4 - len) & 3;
   1150 
   1151 	/*
   1152 	 * The 3c509 automatically pads short packets to minimum ethernet
   1153 	 * length, but we drop packets that are too large. Perhaps we should
   1154 	 * truncate them instead?
   1155 	 */
   1156 	if (len + pad > ETHER_MAX_LEN) {
   1157 		/* packet is obviously too large: toss it */
   1158 		++ifp->if_oerrors;
   1159 		IFQ_DEQUEUE(&ifp->if_snd, m0);
   1160 		m_freem(m0);
   1161 		goto readcheck;
   1162 	}
   1163 
   1164 	if (bus_space_read_2(iot, ioh, ep_w1_reg(sc, ELINK_W1_FREE_TX)) <
   1165 	    len + pad + 4) {
   1166 		bus_space_write_2(iot, ioh, ELINK_COMMAND,
   1167 		    SET_TX_AVAIL_THRESH |
   1168 		    ((len + pad + 4) >> sc->ep_pktlenshift));
   1169 		/* not enough room in FIFO */
   1170 		ifp->if_flags |= IFF_OACTIVE;
   1171 		return;
   1172 	} else {
   1173 		bus_space_write_2(iot, ioh, ELINK_COMMAND,
   1174 		    SET_TX_AVAIL_THRESH | ELINK_THRESH_DISABLE);
   1175 	}
   1176 
   1177 	IFQ_DEQUEUE(&ifp->if_snd, m0);
   1178 	if (m0 == 0)		/* not really needed */
   1179 		return;
   1180 
   1181 	bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_TX_START_THRESH |
   1182 	    ((len / 4 + sc->tx_start_thresh) /* >> sc->ep_pktlenshift*/));
   1183 
   1184 #if NBPFILTER > 0
   1185 	if (ifp->if_bpf)
   1186 		bpf_mtap(ifp->if_bpf, m0);
   1187 #endif
   1188 
   1189 	/*
   1190 	 * Do the output at a high interrupt priority level so that an
   1191 	 * interrupt from another device won't cause a FIFO underrun.
   1192 	 * We choose splsched() since that blocks essentially everything
   1193 	 * except for interrupts from serial devices (which typically
   1194 	 * lose data of their interrupt isn't serviced fast enough).
   1195 	 *
   1196 	 * XXX THIS CAN CAUSE CLOCK DRIFT!
   1197 	 */
   1198 	sh = splsched();
   1199 
   1200 	txreg = ep_w1_reg(sc, ELINK_W1_TX_PIO_WR_1);
   1201 
   1202 	if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER) {
   1203 		/*
   1204 		 * Prime the FIFO buffer counter (number of 16-bit
   1205 		 * words about to be written to the FIFO).
   1206 		 *
   1207 		 * NOTE: NO OTHER ACCESS CAN BE PERFORMED WHILE THIS
   1208 		 * COUNTER IS NON-ZERO!
   1209 		 */
   1210 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_WRCTL,
   1211 		    (len + pad) >> 1);
   1212 	}
   1213 
   1214 	bus_space_write_2(iot, ioh, txreg, len);
   1215 	bus_space_write_2(iot, ioh, txreg, 0xffff); /* Second is meaningless */
   1216 	if (ELINK_IS_BUS_32(sc->bustype)) {
   1217 		for (m = m0; m;) {
   1218 			if (m->m_len > 3)  {
   1219 				/* align our reads from core */
   1220 				if (mtod(m, u_long) & 3)  {
   1221 					u_long count =
   1222 					    4 - (mtod(m, u_long) & 3);
   1223 					bus_space_write_multi_1(iot, ioh,
   1224 					    txreg, mtod(m, u_int8_t *), count);
   1225 					m->m_data =
   1226 					    (void *)(mtod(m, u_long) + count);
   1227 					m->m_len -= count;
   1228 				}
   1229 				bus_space_write_multi_stream_4(iot, ioh,
   1230 				    txreg, mtod(m, u_int32_t *), m->m_len >> 2);
   1231 				m->m_data = (void *)(mtod(m, u_long) +
   1232 					(u_long)(m->m_len & ~3));
   1233 				m->m_len -= m->m_len & ~3;
   1234 			}
   1235 			if (m->m_len)  {
   1236 				bus_space_write_multi_1(iot, ioh,
   1237 				    txreg, mtod(m, u_int8_t *), m->m_len);
   1238 			}
   1239 			MFREE(m, m0);
   1240 			m = m0;
   1241 		}
   1242 	} else {
   1243 		for (m = m0; m;) {
   1244 			if (m->m_len > 1)  {
   1245 				if (mtod(m, u_long) & 1)  {
   1246 					bus_space_write_1(iot, ioh,
   1247 					    txreg, *(mtod(m, u_int8_t *)));
   1248 					m->m_data =
   1249 					    (void *)(mtod(m, u_long) + 1);
   1250 					m->m_len -= 1;
   1251 				}
   1252 				bus_space_write_multi_stream_2(iot, ioh,
   1253 				    txreg, mtod(m, u_int16_t *),
   1254 				    m->m_len >> 1);
   1255 			}
   1256 			if (m->m_len & 1)  {
   1257 				bus_space_write_1(iot, ioh, txreg,
   1258 				     *(mtod(m, u_int8_t *) + m->m_len - 1));
   1259 			}
   1260 			MFREE(m, m0);
   1261 			m = m0;
   1262 		}
   1263 	}
   1264 	while (pad--)
   1265 		bus_space_write_1(iot, ioh, txreg, 0);
   1266 
   1267 	splx(sh);
   1268 
   1269 	++ifp->if_opackets;
   1270 
   1271 readcheck:
   1272 	if ((bus_space_read_2(iot, ioh, ep_w1_reg(sc, ELINK_W1_RX_STATUS)) &
   1273 	    ERR_INCOMPLETE) == 0) {
   1274 		/* We received a complete packet. */
   1275 		u_int16_t status = bus_space_read_2(iot, ioh, ELINK_STATUS);
   1276 
   1277 		if ((status & S_INTR_LATCH) == 0) {
   1278 			/*
   1279 			 * No interrupt, read the packet and continue
   1280 			 * Is  this supposed to happen? Is my motherboard
   1281 			 * completely busted?
   1282 			 */
   1283 			epread(sc);
   1284 		} else {
   1285 			/* Got an interrupt, return so that it gets serviced. */
   1286 			return;
   1287 		}
   1288 	} else {
   1289 		/* Check if we are stuck and reset [see XXX comment] */
   1290 		if (epstatus(sc)) {
   1291 			if (ifp->if_flags & IFF_DEBUG)
   1292 				printf("%s: adapter reset\n",
   1293 				    sc->sc_dev.dv_xname);
   1294 			epreset(sc);
   1295 		}
   1296 	}
   1297 
   1298 	goto startagain;
   1299 }
   1300 
   1301 
   1302 /*
   1303  * XXX: The 3c509 card can get in a mode where both the fifo status bit
   1304  *	FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
   1305  *	We detect this situation and we reset the adapter.
   1306  *	It happens at times when there is a lot of broadcast traffic
   1307  *	on the cable (once in a blue moon).
   1308  */
   1309 static int
   1310 epstatus(sc)
   1311 	struct ep_softc *sc;
   1312 {
   1313 	bus_space_tag_t iot = sc->sc_iot;
   1314 	bus_space_handle_t ioh = sc->sc_ioh;
   1315 	u_int16_t fifost;
   1316 
   1317 	/*
   1318 	 * Check the FIFO status and act accordingly
   1319 	 */
   1320 	GO_WINDOW(4);
   1321 	fifost = bus_space_read_2(iot, ioh, ELINK_W4_FIFO_DIAG);
   1322 	GO_WINDOW(1);
   1323 
   1324 	if (fifost & FIFOS_RX_UNDERRUN) {
   1325 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1326 			printf("%s: RX underrun\n", sc->sc_dev.dv_xname);
   1327 		epreset(sc);
   1328 		return 0;
   1329 	}
   1330 
   1331 	if (fifost & FIFOS_RX_STATUS_OVERRUN) {
   1332 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1333 			printf("%s: RX Status overrun\n", sc->sc_dev.dv_xname);
   1334 		return 1;
   1335 	}
   1336 
   1337 	if (fifost & FIFOS_RX_OVERRUN) {
   1338 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1339 			printf("%s: RX overrun\n", sc->sc_dev.dv_xname);
   1340 		return 1;
   1341 	}
   1342 
   1343 	if (fifost & FIFOS_TX_OVERRUN) {
   1344 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1345 			printf("%s: TX overrun\n", sc->sc_dev.dv_xname);
   1346 		epreset(sc);
   1347 		return 0;
   1348 	}
   1349 
   1350 	return 0;
   1351 }
   1352 
   1353 
   1354 static void
   1355 eptxstat(sc)
   1356 	struct ep_softc *sc;
   1357 {
   1358 	bus_space_tag_t iot = sc->sc_iot;
   1359 	bus_space_handle_t ioh = sc->sc_ioh;
   1360 	int i;
   1361 
   1362 	/*
   1363 	 * We need to read+write TX_STATUS until we get a 0 status
   1364 	 * in order to turn off the interrupt flag.
   1365 	 */
   1366 	while ((i = bus_space_read_1(iot, ioh,
   1367 	     ep_w1_reg(sc, ELINK_W1_TX_STATUS))) & TXS_COMPLETE) {
   1368 		bus_space_write_1(iot, ioh, ep_w1_reg(sc, ELINK_W1_TX_STATUS),
   1369 		    0x0);
   1370 
   1371 		if (i & TXS_JABBER) {
   1372 			++sc->sc_ethercom.ec_if.if_oerrors;
   1373 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1374 				printf("%s: jabber (%x)\n",
   1375 				       sc->sc_dev.dv_xname, i);
   1376 			epreset(sc);
   1377 		} else if (i & TXS_UNDERRUN) {
   1378 			++sc->sc_ethercom.ec_if.if_oerrors;
   1379 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1380 				printf("%s: fifo underrun (%x) @%d\n",
   1381 				       sc->sc_dev.dv_xname, i,
   1382 				       sc->tx_start_thresh);
   1383 			if (sc->tx_succ_ok < 100)
   1384 				    sc->tx_start_thresh = min(ETHER_MAX_LEN,
   1385 					    sc->tx_start_thresh + 20);
   1386 			sc->tx_succ_ok = 0;
   1387 			epreset(sc);
   1388 		} else if (i & TXS_MAX_COLLISION) {
   1389 			++sc->sc_ethercom.ec_if.if_collisions;
   1390 			bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_ENABLE);
   1391 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1392 		} else
   1393 			sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
   1394 	}
   1395 }
   1396 
   1397 int
   1398 epintr(arg)
   1399 	void *arg;
   1400 {
   1401 	struct ep_softc *sc = arg;
   1402 	bus_space_tag_t iot = sc->sc_iot;
   1403 	bus_space_handle_t ioh = sc->sc_ioh;
   1404 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1405 	u_int16_t status;
   1406 	int ret = 0;
   1407 	int addrandom = 0;
   1408 
   1409 	if (sc->enabled == 0 ||
   1410 	    (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
   1411 		return (0);
   1412 
   1413 	for (;;) {
   1414 		bus_space_write_2(iot, ioh, ELINK_COMMAND, C_INTR_LATCH);
   1415 
   1416 		status = bus_space_read_2(iot, ioh, ELINK_STATUS);
   1417 
   1418 		if ((status & (S_TX_COMPLETE | S_TX_AVAIL |
   1419 			       S_RX_COMPLETE | S_CARD_FAILURE)) == 0) {
   1420 			if ((status & S_INTR_LATCH) == 0) {
   1421 #if 0
   1422 				printf("%s: intr latch cleared\n",
   1423 				       sc->sc_dev.dv_xname);
   1424 #endif
   1425 				break;
   1426 			}
   1427 		}
   1428 
   1429 		ret = 1;
   1430 
   1431 		/*
   1432 		 * Acknowledge any interrupts.  It's important that we do this
   1433 		 * first, since there would otherwise be a race condition.
   1434 		 * Due to the i386 interrupt queueing, we may get spurious
   1435 		 * interrupts occasionally.
   1436 		 */
   1437 		bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR |
   1438 				  (status & (C_INTR_LATCH |
   1439 					     C_CARD_FAILURE |
   1440 					     C_TX_COMPLETE |
   1441 					     C_TX_AVAIL |
   1442 					     C_RX_COMPLETE |
   1443 					     C_RX_EARLY |
   1444 					     C_INT_RQD |
   1445 					     C_UPD_STATS)));
   1446 
   1447 #if 0
   1448 		status = bus_space_read_2(iot, ioh, ELINK_STATUS);
   1449 
   1450 		printf("%s: intr%s%s%s%s\n", sc->sc_dev.dv_xname,
   1451 		       (status & S_RX_COMPLETE)?" RX_COMPLETE":"",
   1452 		       (status & S_TX_COMPLETE)?" TX_COMPLETE":"",
   1453 		       (status & S_TX_AVAIL)?" TX_AVAIL":"",
   1454 		       (status & S_CARD_FAILURE)?" CARD_FAILURE":"");
   1455 #endif
   1456 
   1457 		if (status & S_RX_COMPLETE) {
   1458 			epread(sc);
   1459 			addrandom = 1;
   1460 		}
   1461 		if (status & S_TX_AVAIL) {
   1462 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1463 			epstart(&sc->sc_ethercom.ec_if);
   1464 			addrandom = 1;
   1465 		}
   1466 		if (status & S_CARD_FAILURE) {
   1467 			printf("%s: adapter failure (%x)\n",
   1468 			    sc->sc_dev.dv_xname, status);
   1469 #if 1
   1470 			epinit(sc);
   1471 #else
   1472 			epreset(sc);
   1473 #endif
   1474 			return (1);
   1475 		}
   1476 		if (status & S_TX_COMPLETE) {
   1477 			eptxstat(sc);
   1478 			epstart(ifp);
   1479 			addrandom = 1;
   1480 		}
   1481 
   1482 #if NRND > 0
   1483 		if (status)
   1484 			rnd_add_uint32(&sc->rnd_source, status);
   1485 #endif
   1486 	}
   1487 
   1488 	/* no more interrupts */
   1489 	return (ret);
   1490 }
   1491 
   1492 void
   1493 epread(sc)
   1494 	struct ep_softc *sc;
   1495 {
   1496 	bus_space_tag_t iot = sc->sc_iot;
   1497 	bus_space_handle_t ioh = sc->sc_ioh;
   1498 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1499 	struct mbuf *m;
   1500 	int len;
   1501 
   1502 	len = bus_space_read_2(iot, ioh, ep_w1_reg(sc, ELINK_W1_RX_STATUS));
   1503 
   1504 again:
   1505 	if (ifp->if_flags & IFF_DEBUG) {
   1506 		int err = len & ERR_MASK;
   1507 		char *s = NULL;
   1508 
   1509 		if (len & ERR_INCOMPLETE)
   1510 			s = "incomplete packet";
   1511 		else if (err == ERR_OVERRUN)
   1512 			s = "packet overrun";
   1513 		else if (err == ERR_RUNT)
   1514 			s = "runt packet";
   1515 		else if (err == ERR_ALIGNMENT)
   1516 			s = "bad alignment";
   1517 		else if (err == ERR_CRC)
   1518 			s = "bad crc";
   1519 		else if (err == ERR_OVERSIZE)
   1520 			s = "oversized packet";
   1521 		else if (err == ERR_DRIBBLE)
   1522 			s = "dribble bits";
   1523 
   1524 		if (s)
   1525 			printf("%s: %s\n", sc->sc_dev.dv_xname, s);
   1526 	}
   1527 
   1528 	if (len & ERR_INCOMPLETE)
   1529 		return;
   1530 
   1531 	if (len & ERR_RX) {
   1532 		++ifp->if_ierrors;
   1533 		goto abort;
   1534 	}
   1535 
   1536 	len &= RX_BYTES_MASK;	/* Lower 11 bits = RX bytes. */
   1537 
   1538 	/* Pull packet off interface. */
   1539 	m = epget(sc, len);
   1540 	if (m == 0) {
   1541 		ifp->if_ierrors++;
   1542 		goto abort;
   1543 	}
   1544 
   1545 	++ifp->if_ipackets;
   1546 
   1547 #if NBPFILTER > 0
   1548 	/*
   1549 	 * Check if there's a BPF listener on this interface.
   1550 	 * If so, hand off the raw packet to BPF.
   1551 	 */
   1552 	if (ifp->if_bpf)
   1553 		bpf_mtap(ifp->if_bpf, m);
   1554 #endif
   1555 
   1556 	(*ifp->if_input)(ifp, m);
   1557 
   1558 	/*
   1559 	 * In periods of high traffic we can actually receive enough
   1560 	 * packets so that the fifo overrun bit will be set at this point,
   1561 	 * even though we just read a packet. In this case we
   1562 	 * are not going to receive any more interrupts. We check for
   1563 	 * this condition and read again until the fifo is not full.
   1564 	 * We could simplify this test by not using epstatus(), but
   1565 	 * rechecking the RX_STATUS register directly. This test could
   1566 	 * result in unnecessary looping in cases where there is a new
   1567 	 * packet but the fifo is not full, but it will not fix the
   1568 	 * stuck behavior.
   1569 	 *
   1570 	 * Even with this improvement, we still get packet overrun errors
   1571 	 * which are hurting performance. Maybe when I get some more time
   1572 	 * I'll modify epread() so that it can handle RX_EARLY interrupts.
   1573 	 */
   1574 	if (epstatus(sc)) {
   1575 		len = bus_space_read_2(iot, ioh,
   1576 		    ep_w1_reg(sc, ELINK_W1_RX_STATUS));
   1577 		/* Check if we are stuck and reset [see XXX comment] */
   1578 		if (len & ERR_INCOMPLETE) {
   1579 			if (ifp->if_flags & IFF_DEBUG)
   1580 				printf("%s: adapter reset\n",
   1581 				    sc->sc_dev.dv_xname);
   1582 			epreset(sc);
   1583 			return;
   1584 		}
   1585 		goto again;
   1586 	}
   1587 
   1588 	return;
   1589 
   1590 abort:
   1591 	ep_discard_rxtop(iot, ioh);
   1592 
   1593 }
   1594 
   1595 struct mbuf *
   1596 epget(sc, totlen)
   1597 	struct ep_softc *sc;
   1598 	int totlen;
   1599 {
   1600 	bus_space_tag_t iot = sc->sc_iot;
   1601 	bus_space_handle_t ioh = sc->sc_ioh;
   1602 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1603 	struct mbuf *top, **mp, *m, *rv = NULL;
   1604 	bus_addr_t rxreg;
   1605 	int len, remaining;
   1606 	int sh;
   1607 
   1608 	m = sc->mb[sc->next_mb];
   1609 	sc->mb[sc->next_mb] = 0;
   1610 	if (m == 0) {
   1611 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1612 		if (m == 0)
   1613 			return 0;
   1614 	} else {
   1615 		/* If the queue is no longer full, refill. */
   1616 		if (sc->last_mb == sc->next_mb)
   1617 			callout_reset(&sc->sc_mbuf_callout, 1, epmbuffill, sc);
   1618 		/* Convert one of our saved mbuf's. */
   1619 		sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
   1620 		m->m_data = m->m_pktdat;
   1621 		m->m_flags = M_PKTHDR;
   1622 		bzero(&m->m_pkthdr, sizeof(m->m_pkthdr));
   1623 	}
   1624 	m->m_pkthdr.rcvif = ifp;
   1625 	m->m_pkthdr.len = totlen;
   1626 	len = MHLEN;
   1627 	top = 0;
   1628 	mp = &top;
   1629 
   1630 	/*
   1631 	 * We read the packet at a high interrupt priority level so that
   1632 	 * an interrupt from another device won't cause the card's packet
   1633 	 * buffer to overflow.  We choose splsched() since that blocks
   1634 	 * essentially everything except for interrupts from serial
   1635 	 * devices (which typically lose data of their interrupt isn't
   1636 	 * serviced fast enough).
   1637 	 *
   1638 	 * XXX THIS CAN CAUSE CLOCK DRIFT!
   1639 	 */
   1640 	sh = splsched();
   1641 
   1642 	rxreg = ep_w1_reg(sc, ELINK_W1_RX_PIO_RD_1);
   1643 
   1644 	if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER) {
   1645 		/*
   1646 		 * Prime the FIFO buffer counter (number of 16-bit
   1647 		 * words about to be read from the FIFO).
   1648 		 *
   1649 		 * NOTE: NO OTHER ACCESS CAN BE PERFORMED WHILE THIS
   1650 		 * COUNTER IS NON-ZERO!
   1651 		 */
   1652 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, totlen >> 1);
   1653 	}
   1654 
   1655 	while (totlen > 0) {
   1656 		if (top) {
   1657 			m = sc->mb[sc->next_mb];
   1658 			sc->mb[sc->next_mb] = 0;
   1659 			if (m == 0) {
   1660 				MGET(m, M_DONTWAIT, MT_DATA);
   1661 				if (m == 0) {
   1662 					m_freem(top);
   1663 					goto out;
   1664 				}
   1665 			} else {
   1666 				sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
   1667 			}
   1668 			len = MLEN;
   1669 		}
   1670 		if (totlen >= MINCLSIZE) {
   1671 			MCLGET(m, M_DONTWAIT);
   1672 			if ((m->m_flags & M_EXT) == 0) {
   1673 				m_free(m);
   1674 				m_freem(top);
   1675 				goto out;
   1676 			}
   1677 			len = MCLBYTES;
   1678 		}
   1679 		if (top == 0)  {
   1680 			/* align the struct ip header */
   1681 			caddr_t newdata = (caddr_t)
   1682 			    ALIGN(m->m_data + sizeof(struct ether_header))
   1683 			    - sizeof(struct ether_header);
   1684 			len -= newdata - m->m_data;
   1685 			m->m_data = newdata;
   1686 		}
   1687 		remaining = len = min(totlen, len);
   1688 		if (ELINK_IS_BUS_32(sc->bustype)) {
   1689 			u_long offset = mtod(m, u_long);
   1690 			/*
   1691 			 * Read bytes up to the point where we are aligned.
   1692 			 * (We can align to 4 bytes, rather than ALIGNBYTES,
   1693 			 * here because we're later reading 4-byte chunks.)
   1694 			 */
   1695 			if ((remaining > 3) && (offset & 3))  {
   1696 				int count = (4 - (offset & 3));
   1697 				bus_space_read_multi_1(iot, ioh,
   1698 				    rxreg, (u_int8_t *) offset, count);
   1699 				offset += count;
   1700 				remaining -= count;
   1701 			}
   1702 			if (remaining > 3) {
   1703 				bus_space_read_multi_stream_4(iot, ioh,
   1704 				    rxreg, (u_int32_t *) offset,
   1705 				    remaining >> 2);
   1706 				offset += remaining & ~3;
   1707 				remaining &= 3;
   1708 			}
   1709 			if (remaining)  {
   1710 				bus_space_read_multi_1(iot, ioh,
   1711 				    rxreg, (u_int8_t *) offset, remaining);
   1712 			}
   1713 		} else {
   1714 			u_long offset = mtod(m, u_long);
   1715 			if ((remaining > 1) && (offset & 1))  {
   1716 				bus_space_read_multi_1(iot, ioh,
   1717 				    rxreg, (u_int8_t *) offset, 1);
   1718 				remaining -= 1;
   1719 				offset += 1;
   1720 			}
   1721 			if (remaining > 1) {
   1722 				bus_space_read_multi_stream_2(iot, ioh,
   1723 				    rxreg, (u_int16_t *) offset,
   1724 				    remaining >> 1);
   1725 				offset += remaining & ~1;
   1726 			}
   1727 			if (remaining & 1)  {
   1728 				bus_space_read_multi_1(iot, ioh,
   1729 				    rxreg, (u_int8_t *) offset, remaining & 1);
   1730 			}
   1731 		}
   1732 		m->m_len = len;
   1733 		totlen -= len;
   1734 		*mp = m;
   1735 		mp = &m->m_next;
   1736 	}
   1737 
   1738 	rv = top;
   1739 
   1740 	ep_discard_rxtop(iot, ioh);
   1741 
   1742  out:
   1743 	if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER)
   1744 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, 0);
   1745 	splx(sh);
   1746 
   1747 	return rv;
   1748 }
   1749 
   1750 int
   1751 epioctl(ifp, cmd, data)
   1752 	struct ifnet *ifp;
   1753 	u_long cmd;
   1754 	caddr_t data;
   1755 {
   1756 	struct ep_softc *sc = ifp->if_softc;
   1757 	struct ifaddr *ifa = (struct ifaddr *)data;
   1758 	struct ifreq *ifr = (struct ifreq *)data;
   1759 	int s, error = 0;
   1760 
   1761 	s = splnet();
   1762 
   1763 	switch (cmd) {
   1764 
   1765 	case SIOCSIFADDR:
   1766 		if ((error = epenable(sc)) != 0)
   1767 			break;
   1768 		/* epinit is called just below */
   1769 		ifp->if_flags |= IFF_UP;
   1770 		switch (ifa->ifa_addr->sa_family) {
   1771 #ifdef INET
   1772 		case AF_INET:
   1773 			epinit(sc);
   1774 			arp_ifinit(&sc->sc_ethercom.ec_if, ifa);
   1775 			break;
   1776 #endif
   1777 #ifdef NS
   1778 		case AF_NS:
   1779 		    {
   1780 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1781 
   1782 			if (ns_nullhost(*ina))
   1783 				ina->x_host = *(union ns_host *)
   1784 				    LLADDR(ifp->if_sadl);
   1785 			else
   1786 				bcopy(ina->x_host.c_host,
   1787 				    LLADDR(ifp->if_sadl),
   1788 				    ifp->if_addrlen);
   1789 			/* Set new address. */
   1790 			epinit(sc);
   1791 			break;
   1792 		    }
   1793 #endif
   1794 		default:
   1795 			epinit(sc);
   1796 			break;
   1797 		}
   1798 		break;
   1799 
   1800 	case SIOCSIFMEDIA:
   1801 	case SIOCGIFMEDIA:
   1802 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   1803 		break;
   1804 
   1805 	case SIOCSIFFLAGS:
   1806 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1807 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1808 			/*
   1809 			 * If interface is marked down and it is running, then
   1810 			 * stop it.
   1811 			 */
   1812 			epstop(sc);
   1813 			ifp->if_flags &= ~IFF_RUNNING;
   1814 			epdisable(sc);
   1815 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1816 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1817 			/*
   1818 			 * If interface is marked up and it is stopped, then
   1819 			 * start it.
   1820 			 */
   1821 			if ((error = epenable(sc)) != 0)
   1822 				break;
   1823 			epinit(sc);
   1824 		} else if ((ifp->if_flags & IFF_UP) != 0) {
   1825 			/*
   1826 			 * deal with flags changes:
   1827 			 * IFF_MULTICAST, IFF_PROMISC.
   1828 			 */
   1829 			epsetfilter(sc);
   1830 		}
   1831 		break;
   1832 
   1833 	case SIOCADDMULTI:
   1834 	case SIOCDELMULTI:
   1835 		if (sc->enabled == 0) {
   1836 			error = EIO;
   1837 			break;
   1838 		}
   1839 
   1840 		error = (cmd == SIOCADDMULTI) ?
   1841 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1842 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1843 
   1844 		if (error == ENETRESET) {
   1845 			/*
   1846 			 * Multicast list has changed; set the hardware filter
   1847 			 * accordingly.
   1848 			 */
   1849 			epreset(sc);
   1850 			error = 0;
   1851 		}
   1852 		break;
   1853 
   1854 	default:
   1855 		error = EINVAL;
   1856 		break;
   1857 	}
   1858 
   1859 	splx(s);
   1860 	return (error);
   1861 }
   1862 
   1863 void
   1864 epreset(sc)
   1865 	struct ep_softc *sc;
   1866 {
   1867 	int s;
   1868 
   1869 	s = splnet();
   1870 	epinit(sc);
   1871 	splx(s);
   1872 }
   1873 
   1874 void
   1875 epwatchdog(ifp)
   1876 	struct ifnet *ifp;
   1877 {
   1878 	struct ep_softc *sc = ifp->if_softc;
   1879 
   1880 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1881 	++sc->sc_ethercom.ec_if.if_oerrors;
   1882 
   1883 	epreset(sc);
   1884 }
   1885 
   1886 void
   1887 epstop(sc)
   1888 	struct ep_softc *sc;
   1889 {
   1890 	bus_space_tag_t iot = sc->sc_iot;
   1891 	bus_space_handle_t ioh = sc->sc_ioh;
   1892 
   1893 	if (sc->ep_flags & ELINK_FLAGS_MII) {
   1894 		/* Stop the one second clock. */
   1895 		callout_stop(&sc->sc_mbuf_callout);
   1896 
   1897 		/* Down the MII. */
   1898 		mii_down(&sc->sc_mii);
   1899 	}
   1900 
   1901 	if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER) {
   1902 		/*
   1903 		 * Clear the FIFO buffer count, thus halting
   1904 		 * any currently-running transactions.
   1905 		 */
   1906 		GO_WINDOW(1);		/* sanity */
   1907 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_WRCTL, 0);
   1908 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, 0);
   1909 	}
   1910 
   1911 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_DISABLE);
   1912 	ep_discard_rxtop(iot, ioh);
   1913 
   1914 	bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_DISABLE);
   1915 	bus_space_write_2(iot, ioh, ELINK_COMMAND, STOP_TRANSCEIVER);
   1916 
   1917 	ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
   1918 	ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
   1919 
   1920 	bus_space_write_2(iot, ioh, ELINK_COMMAND, C_INTR_LATCH);
   1921 	bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_RD_0_MASK);
   1922 	bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_INTR_MASK);
   1923 	bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_RX_FILTER);
   1924 
   1925 	epmbufempty(sc);
   1926 }
   1927 
   1928 
   1929 /*
   1930  * Before reboots, reset card completely.
   1931  */
   1932 static void
   1933 epshutdown(arg)
   1934 	void *arg;
   1935 {
   1936 	struct ep_softc *sc = arg;
   1937 	int s = splnet();
   1938 
   1939 	if (sc->enabled) {
   1940 		epstop(sc);
   1941 		ep_reset_cmd(sc, ELINK_COMMAND, GLOBAL_RESET);
   1942 		sc->enabled = 0;
   1943 	}
   1944 	splx(s);
   1945 }
   1946 
   1947 /*
   1948  * We get eeprom data from the id_port given an offset into the
   1949  * eeprom.  Basically; after the ID_sequence is sent to all of
   1950  * the cards; they enter the ID_CMD state where they will accept
   1951  * command requests. 0x80-0xbf loads the eeprom data.  We then
   1952  * read the port 16 times and with every read; the cards check
   1953  * for contention (ie: if one card writes a 0 bit and another
   1954  * writes a 1 bit then the host sees a 0. At the end of the cycle;
   1955  * each card compares the data on the bus; if there is a difference
   1956  * then that card goes into ID_WAIT state again). In the meantime;
   1957  * one bit of data is returned in the AX register which is conveniently
   1958  * returned to us by bus_space_read_1().  Hence; we read 16 times getting one
   1959  * bit of data with each read.
   1960  *
   1961  * NOTE: the caller must provide an i/o handle for ELINK_ID_PORT!
   1962  */
   1963 u_int16_t
   1964 epreadeeprom(iot, ioh, offset)
   1965 	bus_space_tag_t iot;
   1966 	bus_space_handle_t ioh;
   1967 	int offset;
   1968 {
   1969 	u_int16_t data = 0;
   1970 	int i;
   1971 
   1972 	bus_space_write_1(iot, ioh, 0, 0x80 + offset);
   1973 	delay(1000);
   1974 	for (i = 0; i < 16; i++)
   1975 		data = (data << 1) | (bus_space_read_2(iot, ioh, 0) & 1);
   1976 	return (data);
   1977 }
   1978 
   1979 static int
   1980 epbusyeeprom(sc)
   1981 	struct ep_softc *sc;
   1982 {
   1983 	bus_space_tag_t iot = sc->sc_iot;
   1984 	bus_space_handle_t ioh = sc->sc_ioh;
   1985 	int i = 100, j;
   1986 
   1987 	if (sc->bustype == ELINK_BUS_PCMCIA) {
   1988 		delay(1000);
   1989 		return 0;
   1990 	}
   1991 
   1992 	j = 0;		/* bad GCC flow analysis */
   1993 	while (i--) {
   1994 		j = bus_space_read_2(iot, ioh, ELINK_W0_EEPROM_COMMAND);
   1995 		if (j & EEPROM_BUSY)
   1996 			delay(100);
   1997 		else
   1998 			break;
   1999 	}
   2000 	if (!i) {
   2001 		printf("\n%s: eeprom failed to come ready\n",
   2002 		    sc->sc_dev.dv_xname);
   2003 		return (1);
   2004 	}
   2005 	if (j & EEPROM_TST_MODE) {
   2006 		/* XXX PnP mode? */
   2007 		printf("\n%s: erase pencil mark!\n", sc->sc_dev.dv_xname);
   2008 		return (1);
   2009 	}
   2010 	return (0);
   2011 }
   2012 
   2013 u_int16_t
   2014 ep_read_eeprom(sc, offset)
   2015 	struct ep_softc *sc;
   2016 	u_int16_t offset;
   2017 {
   2018 	u_int16_t readcmd;
   2019 
   2020 	/*
   2021 	 * RoadRunner has a larger EEPROM, so a different read command
   2022 	 * is required.
   2023 	 */
   2024 	if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER)
   2025 		readcmd = READ_EEPROM_RR;
   2026 	else
   2027 		readcmd = READ_EEPROM;
   2028 
   2029 	if (epbusyeeprom(sc))
   2030 		return (0);		/* XXX why is eeprom busy? */
   2031 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_W0_EEPROM_COMMAND,
   2032 	    readcmd | offset);
   2033 	if (epbusyeeprom(sc))
   2034 		return (0);		/* XXX why is eeprom busy? */
   2035 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, ELINK_W0_EEPROM_DATA));
   2036 }
   2037 
   2038 void
   2039 epmbuffill(v)
   2040 	void *v;
   2041 {
   2042 	struct ep_softc *sc = v;
   2043 	struct mbuf *m;
   2044 	int s, i;
   2045 
   2046 	s = splnet();
   2047 	i = sc->last_mb;
   2048 	do {
   2049 		if (sc->mb[i] == 0) {
   2050 			MGET(m, M_DONTWAIT, MT_DATA);
   2051 			if (m == 0)
   2052 				break;
   2053 			sc->mb[i] = m;
   2054 		}
   2055 		i = (i + 1) % MAX_MBS;
   2056 	} while (i != sc->next_mb);
   2057 	sc->last_mb = i;
   2058 	/* If the queue was not filled, try again. */
   2059 	if (sc->last_mb != sc->next_mb)
   2060 		callout_reset(&sc->sc_mbuf_callout, 1, epmbuffill, sc);
   2061 	splx(s);
   2062 }
   2063 
   2064 void
   2065 epmbufempty(sc)
   2066 	struct ep_softc *sc;
   2067 {
   2068 	int s, i;
   2069 
   2070 	s = splnet();
   2071 	for (i = 0; i<MAX_MBS; i++) {
   2072 		if (sc->mb[i]) {
   2073 			m_freem(sc->mb[i]);
   2074 			sc->mb[i] = NULL;
   2075 		}
   2076 	}
   2077 	sc->last_mb = sc->next_mb = 0;
   2078 	callout_stop(&sc->sc_mbuf_callout);
   2079 	splx(s);
   2080 }
   2081 
   2082 int
   2083 epenable(sc)
   2084 	struct ep_softc *sc;
   2085 {
   2086 
   2087 	if (sc->enabled == 0 && sc->enable != NULL) {
   2088 		if ((*sc->enable)(sc) != 0) {
   2089 			printf("%s: device enable failed\n",
   2090 			    sc->sc_dev.dv_xname);
   2091 			return (EIO);
   2092 		}
   2093 	}
   2094 
   2095 	sc->enabled = 1;
   2096 	return (0);
   2097 }
   2098 
   2099 void
   2100 epdisable(sc)
   2101 	struct ep_softc *sc;
   2102 {
   2103 
   2104 	if (sc->enabled != 0 && sc->disable != NULL) {
   2105 		(*sc->disable)(sc);
   2106 		sc->enabled = 0;
   2107 	}
   2108 }
   2109 
   2110 /*
   2111  * ep_activate:
   2112  *
   2113  *	Handle device activation/deactivation requests.
   2114  */
   2115 int
   2116 ep_activate(self, act)
   2117 	struct device *self;
   2118 	enum devact act;
   2119 {
   2120 	struct ep_softc *sc = (struct ep_softc *)self;
   2121 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2122 	int error = 0, s;
   2123 
   2124 	s = splnet();
   2125 	switch (act) {
   2126 	case DVACT_ACTIVATE:
   2127 		error = EOPNOTSUPP;
   2128 		break;
   2129 
   2130 	case DVACT_DEACTIVATE:
   2131 		if (sc->ep_flags & ELINK_FLAGS_MII)
   2132 			mii_activate(&sc->sc_mii, act, MII_PHY_ANY,
   2133 			    MII_OFFSET_ANY);
   2134 		if_deactivate(ifp);
   2135 		break;
   2136 	}
   2137 	splx(s);
   2138 	return (error);
   2139 }
   2140 
   2141 /*
   2142  * ep_detach:
   2143  *
   2144  *	Detach a elink3 interface.
   2145  */
   2146 int
   2147 ep_detach(self, flags)
   2148 	struct device *self;
   2149 	int flags;
   2150 {
   2151 	struct ep_softc *sc = (struct ep_softc *)self;
   2152 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2153 
   2154 	/* Succeed now if there's no work to do. */
   2155 	if ((sc->sc_flags & ELINK_FLAGS_ATTACHED) == 0)
   2156 		return (0);
   2157 
   2158 	epdisable(sc);
   2159 
   2160 	callout_stop(&sc->sc_mii_callout);
   2161 	callout_stop(&sc->sc_mbuf_callout);
   2162 
   2163 	if (sc->ep_flags & ELINK_FLAGS_MII) {
   2164 		/* Detach all PHYs */
   2165 		mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   2166 	}
   2167 
   2168 	/* Delete all remaining media. */
   2169 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
   2170 
   2171 #if NRND > 0
   2172 	rnd_detach_source(&sc->rnd_source);
   2173 #endif
   2174 	ether_ifdetach(ifp);
   2175 	if_detach(ifp);
   2176 
   2177 	shutdownhook_disestablish(sc->sd_hook);
   2178 
   2179 	return (0);
   2180 }
   2181 
   2182 u_int32_t
   2183 ep_mii_bitbang_read(self)
   2184 	struct device *self;
   2185 {
   2186 	struct ep_softc *sc = (void *) self;
   2187 
   2188 	/* We're already in Window 4. */
   2189 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh,
   2190 	    ELINK_W4_BOOM_PHYSMGMT));
   2191 }
   2192 
   2193 void
   2194 ep_mii_bitbang_write(self, val)
   2195 	struct device *self;
   2196 	u_int32_t val;
   2197 {
   2198 	struct ep_softc *sc = (void *) self;
   2199 
   2200 	/* We're already in Window 4. */
   2201 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
   2202 	    ELINK_W4_BOOM_PHYSMGMT, val);
   2203 }
   2204 
   2205 int
   2206 ep_mii_readreg(self, phy, reg)
   2207 	struct device *self;
   2208 	int phy, reg;
   2209 {
   2210 	struct ep_softc *sc = (void *) self;
   2211 	int val;
   2212 
   2213 	GO_WINDOW(4);
   2214 
   2215 	val = mii_bitbang_readreg(self, &ep_mii_bitbang_ops, phy, reg);
   2216 
   2217 	GO_WINDOW(1);
   2218 
   2219 	return (val);
   2220 }
   2221 
   2222 void
   2223 ep_mii_writereg(self, phy, reg, val)
   2224 	struct device *self;
   2225 	int phy, reg, val;
   2226 {
   2227 	struct ep_softc *sc = (void *) self;
   2228 
   2229 	GO_WINDOW(4);
   2230 
   2231 	mii_bitbang_writereg(self, &ep_mii_bitbang_ops, phy, reg, val);
   2232 
   2233 	GO_WINDOW(1);
   2234 }
   2235 
   2236 void
   2237 ep_statchg(self)
   2238 	struct device *self;
   2239 {
   2240 	struct ep_softc *sc = (struct ep_softc *)self;
   2241 	bus_space_tag_t iot = sc->sc_iot;
   2242 	bus_space_handle_t ioh = sc->sc_ioh;
   2243 	int mctl;
   2244 
   2245 	GO_WINDOW(3);
   2246 	mctl = bus_space_read_2(iot, ioh, ELINK_W3_MAC_CONTROL);
   2247 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   2248 		mctl |= MAC_CONTROL_FDX;
   2249 	else
   2250 		mctl &= ~MAC_CONTROL_FDX;
   2251 	bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL, mctl);
   2252 	GO_WINDOW(1);	/* back to operating window */
   2253 }
   2254