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