Home | History | Annotate | Line # | Download | only in ic
elink3.c revision 1.26
      1 /*	$NetBSD: elink3.c,v 1.26 1997/04/24 02:24:06 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1997 Jonathan Stone <jonathan (at) NetBSD.org>
      5  * Copyright (c) 1994 Herb Peyerl <hpeyerl (at) beer.org>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Herb Peyerl.
     19  * 4. The name of Herb Peyerl may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include "bpfilter.h"
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/mbuf.h>
     39 #include <sys/socket.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/errno.h>
     42 #include <sys/syslog.h>
     43 #include <sys/select.h>
     44 #include <sys/device.h>
     45 
     46 #include <net/if.h>
     47 #include <net/if_dl.h>
     48 #include <net/if_ether.h>
     49 #include <net/if_media.h>
     50 
     51 #ifdef INET
     52 #include <netinet/in.h>
     53 #include <netinet/in_systm.h>
     54 #include <netinet/in_var.h>
     55 #include <netinet/ip.h>
     56 #include <netinet/if_inarp.h>
     57 #endif
     58 
     59 #ifdef NS
     60 #include <netns/ns.h>
     61 #include <netns/ns_if.h>
     62 #endif
     63 
     64 #if NBPFILTER > 0
     65 #include <net/bpf.h>
     66 #include <net/bpfdesc.h>
     67 #endif
     68 
     69 #include <machine/cpu.h>
     70 #include <machine/bus.h>
     71 #include <machine/intr.h>
     72 
     73 #include <dev/ic/elink3var.h>
     74 #include <dev/ic/elink3reg.h>
     75 
     76 #define ETHER_MIN_LEN	64
     77 #define ETHER_MAX_LEN   1518
     78 #define ETHER_ADDR_LEN  6
     79 
     80 /*
     81  * Structure to map  media-present bits in boards to
     82  * ifmedia codes and printable media names. Used for table-driven
     83  * ifmedia initialization.
     84  */
     85 struct ep_media {
     86 	int	epm_eeprom_data;	/* bitmask for eeprom config */
     87 	int	epm_conn;		/* sc->ep_connectors code for medium */
     88 	char*	epm_name;		/* name of medium */
     89 	int	epm_ifmedia;		/* ifmedia word for medium */
     90 	int	epm_ifdata;
     91 };
     92 
     93 /*
     94  * ep_media table for Vortex/Demon/Boomerang:
     95  * map from media-present bits in register RESET_OPTIONS+2
     96  * to  ifmedia "media words" and printable names.
     97  *
     98  * XXX indexed directly by INTERNAL_CONFIG default_media field,
     99  * (i.e., EPMEDIA_ constants)  forcing order of entries.
    100  *  Note that 3 is reserved.
    101  */
    102 struct ep_media ep_vortex_media[8] = {
    103   { EP_PCI_UTP,        EPC_UTP, "utp",	    IFM_ETHER|IFM_10_T,
    104        EPMEDIA_10BASE_T },
    105   { EP_PCI_AUI,        EPC_AUI, "aui",	    IFM_ETHER|IFM_10_5,
    106        EPMEDIA_AUI },
    107   { 0,                 0,  	"reserved", IFM_NONE,  EPMEDIA_RESV1 },
    108   { EP_PCI_BNC,        EPC_BNC, "bnc",	    IFM_ETHER|IFM_10_2,
    109        EPMEDIA_10BASE_2 },
    110   { EP_PCI_100BASE_TX, EPC_100TX, "100-TX", IFM_ETHER|IFM_100_TX,
    111        EPMEDIA_100BASE_TX },
    112   { EP_PCI_100BASE_FX, EPC_100FX, "100-FX", IFM_ETHER|IFM_100_FX,
    113        EPMEDIA_100BASE_FX },
    114   { EP_PCI_100BASE_MII,EPC_MII,   "mii",    IFM_ETHER|IFM_100_TX,
    115        EPMEDIA_MII },
    116   { EP_PCI_100BASE_T4, EPC_100T4, "100-T4", IFM_ETHER|IFM_100_T4,
    117        EPMEDIA_100BASE_T4 }
    118 };
    119 
    120 /*
    121  * ep_media table for 3c509/3c509b/3c579/3c589:
    122  * map from media-present bits in register CNFG_CNTRL
    123  * (window 0, offset ?) to  ifmedia "media words" and printable names.
    124  */
    125 struct ep_media ep_isa_media[3] = {
    126   { EP_W0_CC_UTP,  EPC_UTP, "utp",   IFM_ETHER|IFM_10_T, EPMEDIA_10BASE_T },
    127   { EP_W0_CC_AUI,  EPC_AUI, "aui",   IFM_ETHER|IFM_10_5, EPMEDIA_AUI },
    128   { EP_W0_CC_BNC,  EPC_BNC, "bnc",   IFM_ETHER|IFM_10_2, EPMEDIA_10BASE_2 },
    129 };
    130 
    131 /* Map vortex reset_options bits to if_media codes. */
    132 const u_int ep_default_to_media[8] = {
    133 	IFM_ETHER | IFM_10_T,
    134 	IFM_ETHER | IFM_10_5,
    135 	0, 			/* reserved by 3Com */
    136 	IFM_ETHER | IFM_10_2,
    137 	IFM_ETHER | IFM_100_TX,
    138 	IFM_ETHER | IFM_100_FX,
    139 	IFM_ETHER | IFM_100_TX,	/* XXX really MII: need to talk to PHY */
    140 	IFM_ETHER | IFM_100_T4,
    141 };
    142 
    143 /* Autoconfig defintion of driver back-end */
    144 struct cfdriver ep_cd = {
    145 	NULL, "ep", DV_IFNET
    146 };
    147 
    148 
    149 void	ep_internalconfig __P((struct ep_softc *sc));
    150 void	ep_vortex_probemedia __P((struct ep_softc *sc));
    151 void	ep_isa_probemedia __P((struct ep_softc *sc));
    152 
    153 static void eptxstat __P((struct ep_softc *));
    154 static int epstatus __P((struct ep_softc *));
    155 void epinit __P((struct ep_softc *));
    156 int epioctl __P((struct ifnet *, u_long, caddr_t));
    157 void epstart __P((struct ifnet *));
    158 void epwatchdog __P((struct ifnet *));
    159 void epreset __P((struct ep_softc *));
    160 static void epshutdown __P((void *));
    161 void	epread __P((struct ep_softc *));
    162 struct mbuf *epget __P((struct ep_softc *, int));
    163 void	epmbuffill __P((void *));
    164 void	epmbufempty __P((struct ep_softc *));
    165 void	epsetfilter __P((struct ep_softc *));
    166 int	epsetmedia __P((struct ep_softc *, int epmedium));
    167 
    168 /* ifmedia callbacks */
    169 int	ep_media_change __P((struct ifnet *ifp));
    170 void	ep_media_status __P((struct ifnet *ifp, struct ifmediareq *req));
    171 
    172 static int epbusyeeprom __P((struct ep_softc *));
    173 static inline void ep_complete_cmd __P((struct ep_softc *sc,
    174 					u_int cmd, u_int arg));
    175 
    176 
    177 /*
    178  * Issue a (reset) command, and be sure it has completed.
    179  * Used for commands that reset part or all of the  board.
    180  * On newer hardware we could poll SC_COMMAND_IN_PROGRESS,
    181  * but older hardware doesn't implement it and we must delay.
    182  * It's easiest to just delay always.
    183  */
    184 static inline void
    185 ep_complete_cmd(sc, cmd, arg)
    186 	struct ep_softc *sc;
    187 	u_int cmd, arg;
    188 {
    189 	register bus_space_tag_t iot = sc->sc_iot;
    190 	register bus_space_handle_t ioh = sc->sc_ioh;
    191 
    192 	bus_space_write_2(iot, ioh, cmd, arg);
    193 
    194 #ifdef notyet
    195 	/* if this adapter family has S_COMMAND_IN_PROGRESS, use it */
    196 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
    197 		;
    198 	else
    199 #else
    200 	DELAY(100000);	/* need at least 1 ms, but be generous. */
    201 #endif
    202 }
    203 
    204 
    205 
    206 /*
    207  * Back-end attach and configure.
    208  */
    209 void
    210 epconfig(sc, chipset)
    211 	struct ep_softc *sc;
    212 	u_short chipset;
    213 {
    214 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    215 	bus_space_tag_t iot = sc->sc_iot;
    216 	bus_space_handle_t ioh = sc->sc_ioh;
    217 	u_int16_t i;
    218 	u_int8_t myla[6];
    219 
    220 	sc->ep_chipset = chipset;
    221 
    222 	/*
    223 	 * Read the station address from the eeprom
    224 	 */
    225 	for (i = 0; i < 3; i++) {
    226 		u_int16_t x;
    227 		if (epbusyeeprom(sc))
    228 			return;		/* XXX why is eeprom busy? */
    229 		bus_space_write_2(iot, ioh, EP_W0_EEPROM_COMMAND,
    230 		    READ_EEPROM | i);
    231 		if (epbusyeeprom(sc))
    232 			return;		/* XXX why is eeprom busy? */
    233 		x = bus_space_read_2(iot, ioh, EP_W0_EEPROM_DATA);
    234 		myla[(i << 1)] = x >> 8;
    235 		myla[(i << 1) + 1] = x;
    236 	}
    237 
    238 	printf("%s: MAC address %s\n", sc->sc_dev.dv_xname,
    239 	    ether_sprintf(myla));
    240 
    241 	/*
    242 	 * Vortex-based (3c59x pci,eisa) and Boomerang (3c900,3c515?) cards
    243 	 * allow FDDI-sized (4500) byte packets.  Commands only take an
    244 	 * 11-bit parameter, and  11 bits isn't enough to hold a full-size
    245 	 * packet length.
    246 	 * Commands to these cards implicitly upshift a packet size
    247 	 * or threshold by 2 bits.
    248 	 * To detect  cards with large-packet support, we probe by setting
    249 	 * the transmit threshold register, then change windows and
    250 	 * read back the threshold register directly, and see if the
    251 	 * threshold value was shifted or not.
    252 	 */
    253 	bus_space_write_2(iot, ioh, EP_COMMAND,
    254 			  SET_TX_AVAIL_THRESH | EP_LARGEWIN_PROBE );
    255 	GO_WINDOW(5);
    256 	i = bus_space_read_2(iot, ioh, EP_W5_TX_AVAIL_THRESH);
    257 	GO_WINDOW(1);
    258 	switch (i)  {
    259 	case EP_LARGEWIN_PROBE:
    260 	case (EP_LARGEWIN_PROBE & EP_LARGEWIN_MASK):
    261 		sc->ep_pktlenshift = 0;
    262 		break;
    263 
    264 	case (EP_LARGEWIN_PROBE << 2):
    265 		sc->ep_pktlenshift = 2;
    266 		/* XXX does the 3c515 support Vortex-style RESET_OPTIONS? */
    267 		break;
    268 
    269 	default:
    270 		printf("%s: wrote %d to TX_AVAIL_THRESH, read back %d. "
    271 		    "Interface disabled\n",
    272 		    sc->sc_dev.dv_xname, EP_THRESH_DISABLE, (int) i);
    273 		return;
    274 	}
    275 
    276 	/*
    277 	 * Ensure Tx-available interrupts are enabled for
    278 	 * start the interface.
    279 	 * XXX should be in epinit()?
    280 	 */
    281 	bus_space_write_2(iot, ioh, EP_COMMAND,
    282 	    SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
    283 
    284 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    285 	ifp->if_softc = sc;
    286 	ifp->if_start = epstart;
    287 	ifp->if_ioctl = epioctl;
    288 	ifp->if_watchdog = epwatchdog;
    289 	ifp->if_flags =
    290 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    291 
    292 	if_attach(ifp);
    293 	ether_ifattach(ifp, myla);
    294 
    295 	/*
    296 	 * Finish configuration:
    297 	 * determine chipset if the front-end couldn't do so,
    298 	 * show board details, set media.
    299 	 */
    300 
    301 	/* print RAM size */
    302 	ep_internalconfig(sc);
    303 	GO_WINDOW(0);
    304 
    305 	ifmedia_init(&sc->sc_media, 0, ep_media_change, ep_media_status);
    306 
    307 	/*
    308 	 * If we've got an indirect (ISA, PCMCIA?) board, the chipset
    309 	 * is unknown.  If the board has large-packet support, it's a
    310 	 * Vortex/Boomerang, otherwise it's a 3c509.
    311 	 * XXX use eeprom capability word instead?
    312 	 */
    313 	if (sc->ep_chipset == EP_CHIPSET_UNKNOWN && sc->ep_pktlenshift)  {
    314 		printf("warning: unknown chipset, possibly 3c515?\n");
    315 #ifdef notyet
    316 		sc->sc_chipset = EP_CHIPSET_VORTEX;
    317 #endif	/* notyet */
    318 	}
    319 
    320 	/*
    321 	 * Ascertain which media types are present and inform ifmedia.
    322 	 */
    323 	switch (sc->ep_chipset) {
    324 	/* on a direct bus, the attach routine can tell, but check anyway. */
    325 	case EP_CHIPSET_VORTEX:
    326 	case EP_CHIPSET_BOOMERANG2:
    327 		ep_vortex_probemedia(sc);
    328 		break;
    329 
    330 	/* on ISA we can't yet tell 3c509 from 3c515. Assume the former. */
    331 	case EP_CHIPSET_3C509:
    332 	default:
    333 		ep_isa_probemedia(sc);
    334 		break;
    335 	}
    336 
    337 	GO_WINDOW(1);		/* Window 1 is operating window */
    338 
    339 #if NBPFILTER > 0
    340 	bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
    341 		  sizeof(struct ether_header));
    342 #endif
    343 
    344 	sc->tx_start_thresh = 20;	/* probably a good starting point. */
    345 
    346 	/*  Establish callback to reset card when we reboot. */
    347 	shutdownhook_establish(epshutdown, sc);
    348 
    349 	ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
    350 	ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
    351 }
    352 
    353 
    354 /*
    355  * Show interface-model-independent info from window 3
    356  * internal-configuration register.
    357  */
    358 void
    359 ep_internalconfig(sc)
    360 	struct ep_softc *sc;
    361 {
    362 	bus_space_tag_t iot = sc->sc_iot;
    363 	bus_space_handle_t ioh = sc->sc_ioh;
    364 
    365 	u_int config0;
    366 	u_int config1;
    367 
    368 	int  ram_size, ram_width, ram_speed, rom_size, ram_split;
    369 	/*
    370 	 * NVRAM buffer Rx:Tx config names for busmastering cards
    371 	 * (Demon, Vortex, and later).
    372 	 */
    373 	const char *onboard_ram_config[] = {
    374 		"5:3", "3:1", "1:1", "(undefined)" };
    375 
    376 	GO_WINDOW(3);
    377 	config0 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG);
    378 	config1 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG+2);
    379 	GO_WINDOW(0);
    380 
    381 	ram_size  = (config0 & CONFIG_RAMSIZE) >> CONFIG_RAMSIZE_SHIFT;
    382 	ram_width = (config0 & CONFIG_RAMWIDTH) >> CONFIG_RAMWIDTH_SHIFT;
    383 	ram_speed = (config0 & CONFIG_RAMSPEED) >> CONFIG_RAMSPEED_SHIFT;
    384 	rom_size  = (config0 & CONFIG_ROMSIZE) >> CONFIG_ROMSIZE_SHIFT;
    385 
    386 	ram_split  = (config1 & CONFIG_RAMSPLIT) >> CONFIG_RAMSPLIT_SHIFT;
    387 
    388 	printf("%s: %dKB %s-wide FIFO, %s Rx:Tx split, ",
    389 	       sc->sc_dev.dv_xname,
    390 	       8 << ram_size,
    391 	       (ram_width) ? "word" : "byte",
    392 	       onboard_ram_config[ram_split]);
    393 }
    394 
    395 
    396 /*
    397  * Find supported media on 3c509-generation hardware that doesn't have
    398  * a "reset_options" register in window 3.
    399  * Use the config_cntrl register  in window 0 instead.
    400  * Used on original, 10Mbit ISA (3c509), 3c509B, and pre-Demon EISA cards
    401  * that implement  CONFIG_CTRL.  We don't have a good way to set the
    402  * default active mediuim; punt to ifconfig  instead.
    403  *
    404  * XXX what about 3c515, pcmcia 10/100?
    405  */
    406 void
    407 ep_isa_probemedia(sc)
    408 	struct ep_softc *sc;
    409 {
    410 	bus_space_tag_t iot = sc->sc_iot;
    411 	bus_space_handle_t ioh = sc->sc_ioh;
    412 	struct ifmedia *ifm = &sc->sc_media;
    413 
    414 	int	conn, i;
    415 	u_int16_t ep_w0_config, port;
    416 
    417 	conn = 0;
    418 	GO_WINDOW(0);
    419 	ep_w0_config = bus_space_read_2(iot, ioh, EP_W0_CONFIG_CTRL);
    420 	for (i = 0; i < 3; i++) {
    421 		struct ep_media * epm = ep_isa_media + i;
    422 
    423 		if ((ep_w0_config & epm->epm_eeprom_data) != 0) {
    424 
    425 			ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_ifdata, 0);
    426 			if (conn)
    427 				printf("/");
    428 			printf(epm->epm_name);
    429 			conn |= epm->epm_conn;
    430 		}
    431 	}
    432 	sc->ep_connectors = conn;
    433 
    434 	/* get default medium from EEPROM */
    435 	if (epbusyeeprom(sc))
    436 		return;		/* XXX why is eeprom busy? */
    437 	bus_space_write_2(iot, ioh, EP_W0_EEPROM_COMMAND,
    438 	    READ_EEPROM | 8);
    439 	if (epbusyeeprom(sc))
    440 		return;		/* XXX why is  eeprom busy? */
    441 	port = bus_space_read_2(iot, ioh, EP_W0_EEPROM_DATA);
    442 	port = port >> 14;
    443 
    444 	printf(" (default %s)\n", ep_vortex_media[port].epm_name);
    445 	/* tell ifconfig what currently-active media is. */
    446 	ifmedia_set(ifm, ep_default_to_media[port]);
    447 
    448 	/* XXX autoselect not yet implemented */
    449 }
    450 
    451 
    452 /*
    453  * Find media present on large-packet-capable elink3 devices.
    454  * Show onboard configuration of large-packet-capable elink3 devices
    455  * (Demon, Vortex, Boomerang), which do not implement CONFIG_CTRL in window 0.
    456  * Use media and card-version info in window 3 instead.
    457  *
    458  * XXX how much of this works with 3c515, pcmcia 10/100?  With 3c509B, 3c589?
    459  */
    460 void
    461 ep_vortex_probemedia(sc)
    462 	struct ep_softc *sc;
    463 {
    464 	bus_space_tag_t iot = sc->sc_iot;
    465 	bus_space_handle_t ioh = sc->sc_ioh;
    466 	struct ifmedia *ifm = &sc->sc_media;
    467 
    468 	u_int config1;
    469 	int reset_options;
    470 	u_int conn = 0;
    471 
    472 	int  default_media;	/* 3-bit encoding of default (EEPROM) media */
    473 	int  autoselect;	/* boolean: should default to autoselect */
    474 
    475 
    476 	const char *medium_name;
    477 	register int i;
    478 
    479 	GO_WINDOW(3);
    480 	config1 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG+2);
    481 	reset_options  = (int)bus_space_read_1(iot, ioh, EP_W3_RESET_OPTIONS);
    482 	GO_WINDOW(0);
    483 
    484 	default_media = (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
    485         autoselect = (config1 & CONFIG_AUTOSELECT) >> CONFIG_AUTOSELECT_SHIFT;
    486 
    487 	/* set available media options */
    488 	for (i = 0; i < 8; i++) {
    489 		struct ep_media * epm = ep_vortex_media + i;
    490 
    491 		if ((reset_options & epm->epm_eeprom_data) != 0) {
    492 			if (conn) printf("/");
    493 			printf(epm->epm_name);
    494 			conn |= epm->epm_conn;
    495 			ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_ifdata, 0);
    496 		}
    497 	}
    498 
    499 	sc->ep_connectors = conn;
    500 
    501 	/* Show  eeprom's idea of default media.  */
    502 	medium_name = (default_media > 8)
    503 		? "(unknown/impossible media)"
    504 		: ep_vortex_media[default_media].epm_name;
    505 	printf(" default %s%s\n",
    506 	       medium_name,  (autoselect)? ", autoselect" : "" );
    507 #ifdef notyet
    508 	/*
    509 	 * Set default: either the active interface the card
    510 	 * reads  from the EEPROM, or if autoselect is true,
    511 	 * whatever we find is actually connected.
    512 	 *
    513 	 * XXX autoselect not yet implemented.
    514 	 */
    515 #endif	/* notyet */
    516 
    517 	/* tell ifconfig what currently-active media is. */
    518 	ifmedia_set(ifm, ep_default_to_media[default_media]);
    519 }
    520 
    521 
    522 /*
    523  * Bring device up.
    524  *
    525  * The order in here seems important. Otherwise we may not receive
    526  * interrupts. ?!
    527  */
    528 void
    529 epinit(sc)
    530 	register struct ep_softc *sc;
    531 {
    532 	register struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    533 	bus_space_tag_t iot = sc->sc_iot;
    534 	bus_space_handle_t ioh = sc->sc_ioh;
    535 	int i;
    536 
    537 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
    538 		;
    539 
    540 	if (sc->bustype != EP_BUS_PCI) {
    541 		GO_WINDOW(0);
    542 		bus_space_write_2(iot, ioh, EP_W0_CONFIG_CTRL, 0);
    543 		bus_space_write_2(iot, ioh, EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
    544 	}
    545 
    546 	if (sc->bustype == EP_BUS_PCMCIA) {
    547 #ifdef EP_COAX_DEFAULT
    548 		bus_space_write_2(iot, ioh, EP_W0_ADDRESS_CFG,3<<14);
    549 #else
    550 		bus_space_write_2(iot, ioh, EP_W0_ADDRESS_CFG,0<<14);
    551 #endif
    552 		bus_space_write_2(iot, ioh, EP_W0_RESOURCE_CFG, 0x3f00);
    553 	}
    554 
    555 	GO_WINDOW(2);
    556 	for (i = 0; i < 6; i++)	/* Reload the ether_addr. */
    557 		bus_space_write_1(iot, ioh, EP_W2_ADDR_0 + i,
    558 		    LLADDR(ifp->if_sadl)[i]);
    559 
    560 	/*
    561 	 * Reset the station-address receive filter.
    562 	 * A bug workaround for busmastering  (Vortex, Demon) cards.
    563 	 */
    564 	for (i = 0; i < 6; i++)
    565 		bus_space_write_1(iot, ioh, EP_W2_RECVMASK_0 + i, 0);
    566 
    567 	ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
    568 	ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
    569 
    570 	GO_WINDOW(1);		/* Window 1 is operating window */
    571 	for (i = 0; i < 31; i++)
    572 		bus_space_read_1(iot, ioh, EP_W1_TX_STATUS);
    573 
    574 	/* Enable interrupts. */
    575 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RD_0_MASK | S_CARD_FAILURE |
    576 				S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
    577 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_INTR_MASK | S_CARD_FAILURE |
    578 				S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
    579 
    580 	/*
    581 	 * Attempt to get rid of any stray interrupts that occured during
    582 	 * configuration.  On the i386 this isn't possible because one may
    583 	 * already be queued.  However, a single stray interrupt is
    584 	 * unimportant.
    585 	 */
    586 	bus_space_write_2(iot, ioh, EP_COMMAND, ACK_INTR | 0xff);
    587 
    588 	epsetfilter(sc);
    589 	epsetmedia(sc, sc->sc_media.ifm_cur->ifm_data);
    590 
    591 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_ENABLE);
    592 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_ENABLE);
    593 
    594 	epmbuffill(sc);
    595 
    596 	/* Interface is now `running', with no output active. */
    597 	ifp->if_flags |= IFF_RUNNING;
    598 	ifp->if_flags &= ~IFF_OACTIVE;
    599 
    600 	/* Attempt to start output, if any. */
    601 	epstart(ifp);
    602 }
    603 
    604 
    605 /*
    606  * Set multicast receive filter.
    607  * elink3 hardware has no selective multicast filter in hardware.
    608  * Enable reception of all multicasts and filter in software.
    609  */
    610 void
    611 epsetfilter(sc)
    612 	register struct ep_softc *sc;
    613 {
    614 	register struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    615 
    616 	GO_WINDOW(1);		/* Window 1 is operating window */
    617 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, EP_COMMAND, SET_RX_FILTER |
    618 	    FIL_INDIVIDUAL | FIL_BRDCST |
    619 	    ((ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0 ) |
    620 	    ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 ));
    621 }
    622 
    623 
    624 int
    625 ep_media_change(ifp)
    626 	struct ifnet *ifp;
    627 {
    628 	register struct ep_softc *sc = ifp->if_softc;
    629 
    630 	return	epsetmedia(sc, sc->sc_media.ifm_cur->ifm_data);
    631 }
    632 
    633 /*
    634  * Set active media to a specific given EPMEDIA_<> value.
    635  * For dumb 3c509 cards, just power on the selected transceiver.
    636  * For vortex/demon/boomerang cards, update media field in w3_internal_config,
    637  *       and power on selected transciever.
    638  * for pcmcia cards, update media field in w0_address_config.
    639  *      and power on selected transciever.
    640  */
    641 int
    642 epsetmedia(sc, medium)
    643 	register struct ep_softc *sc;
    644 	int medium;
    645 {
    646 	bus_space_tag_t iot = sc->sc_iot;
    647 	bus_space_handle_t ioh = sc->sc_ioh;
    648 	int config0, config1;
    649 	int w4_media;
    650 	int port;
    651 
    652 	port = sc->sc_media.ifm_cur->ifm_data;
    653 
    654 
    655 	/*
    656 	 * First, change the media-control bits in EP_W4_MEDIA_TYPE.
    657 	 */
    658 
    659 	 /* Turn everything off.  First turn off linkbeat and UTP. */
    660 	GO_WINDOW(4);
    661 	w4_media = bus_space_read_2(iot, ioh, EP_W4_MEDIA_TYPE);
    662 	w4_media =  w4_media & ~(ENABLE_UTP|SQE_ENABLE);
    663 	bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE, w4_media);
    664 
    665 	/* Turn off coax */
    666 	bus_space_write_2(iot, ioh, EP_COMMAND, STOP_TRANSCEIVER);
    667 	delay(1000);
    668 
    669 	/* If pcmcia, do the pcmcia media (power?) dance. */
    670 	if (sc->bustype == EP_BUS_PCMCIA) {
    671 		GO_WINDOW(0);
    672 		switch (medium) {
    673 		case EPMEDIA_10BASE_T:
    674 			bus_space_write_2(iot, ioh,
    675 				EP_W0_ADDRESS_CFG,0<<14);
    676 			DELAY(1000);
    677 			break;
    678 
    679 		case EPMEDIA_10BASE_2:
    680 			bus_space_write_2(iot, ioh,
    681 				EP_W0_ADDRESS_CFG,3<<14);
    682 			DELAY(1000);
    683 			break;
    684 		}
    685 	}
    686 
    687 	/* Now turn on the selected media/transciever. */
    688 	GO_WINDOW(4);
    689 	switch  (medium) {
    690 	case EPMEDIA_10BASE_T:
    691 		bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE,
    692 				  w4_media | ENABLE_UTP);
    693 		break;
    694 
    695 	case EPMEDIA_10BASE_2:
    696 		bus_space_write_2(iot, ioh, EP_COMMAND, START_TRANSCEIVER);
    697 		DELAY(1000);	/* 50ms not enmough? */
    698 		break;
    699 
    700 	/* XXX following only for new-generation cards */
    701 	case EPMEDIA_100BASE_TX:
    702 	case EPMEDIA_100BASE_FX:
    703 	case EPMEDIA_100BASE_T4:	/* XXX check documentation */
    704 		bus_space_write_2(iot, ioh,
    705 			  EP_W4_MEDIA_TYPE, w4_media | LINKBEAT_ENABLE);
    706 		DELAY(1000);	/* not strictly necessary? */
    707 		break;
    708 
    709 	case EPMEDIA_AUI:
    710 		/* we already did everything necessary. */
    711 		bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE,
    712 				  w4_media | SQE_ENABLE);
    713 		DELAY(1000);	/*  not strictly necessary? */
    714 		break;
    715 	case EPMEDIA_MII:
    716 		break;
    717 	default:
    718 #if defined(DEBUG)
    719 		printf("%s uknown medium 0x%x\n",
    720 		    sc->sc_dev.dv_xname, medium);
    721 #endif
    722 		break;
    723 
    724 	}
    725 
    726 	/*
    727 	 * For Vortex/Demon/Boomerang, tell the chip which PHY [sic] to use.
    728 	 */
    729 	if  (sc->ep_chipset==EP_CHIPSET_VORTEX	||
    730 	     sc->ep_chipset==EP_CHIPSET_BOOMERANG2) {
    731 
    732 		GO_WINDOW(3);
    733 		config0 = (u_int)bus_space_read_2(iot, ioh,
    734 						  EP_W3_INTERNAL_CONFIG);
    735 		config1 = (u_int)bus_space_read_2(iot, ioh,
    736 						  EP_W3_INTERNAL_CONFIG+2);
    737 
    738 #if defined(DEBUG)
    739 		printf("%s:  read 0x%x, 0x%x from EP_W3_CONFIG register\n",
    740 		       sc->sc_dev.dv_xname, config0, config1);
    741 #endif
    742 		config1 = config1 & ~CONFIG_MEDIAMASK;
    743 		config1 |=  (medium << CONFIG_MEDIAMASK_SHIFT);
    744 
    745 #if defined(DEBUG)
    746 		printf("epsetmedia: %s: medium 0x%x, 0x%x to EP_W3_CONFIG\n",
    747 		       sc->sc_dev.dv_xname, medium, config1);
    748 #endif
    749 		bus_space_write_2(iot, ioh, EP_W3_INTERNAL_CONFIG,
    750 				  config0);
    751 		bus_space_write_2(iot, ioh, EP_W3_INTERNAL_CONFIG+2,
    752 				  config1);
    753 	}
    754 
    755 	GO_WINDOW(1);		/* Window 1 is operating window */
    756 
    757 	return (0);
    758 }
    759 
    760 /*
    761  * Get currently-selected media from card.
    762  * (if_media callback, may be called before interface is brought up).
    763  */
    764 void
    765 ep_media_status(ifp, req)
    766 	struct ifnet *ifp;
    767 	struct ifmediareq *req;
    768 {
    769 	register struct ep_softc *sc = ifp->if_softc;
    770 	bus_space_tag_t iot = sc->sc_iot;
    771 	bus_space_handle_t ioh = sc->sc_ioh;
    772 	u_int config1 = 0;
    773 	u_int ep_mediastatus;
    774 
    775 	/* XXX read from softc when we start autosensing media */
    776 	req->ifm_active = sc->sc_media.ifm_cur->ifm_media;
    777 
    778 	switch (sc->ep_chipset) {
    779 	case EP_CHIPSET_VORTEX:
    780 	case EP_CHIPSET_BOOMERANG:
    781 		GO_WINDOW(3);
    782 		delay(5000);
    783 
    784 		config1 = bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG+2);
    785 		GO_WINDOW(1);
    786 
    787 
    788 		config1 =
    789 		    (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
    790 		req->ifm_active = ep_default_to_media[config1];
    791 
    792 		/* XXX check full-duplex bits? */
    793 
    794 		GO_WINDOW(4);
    795 		req->ifm_status = IFM_AVALID;	/* XXX */
    796 		ep_mediastatus = bus_space_read_2(iot, ioh, EP_W4_MEDIA_TYPE);
    797 		if (ep_mediastatus & LINKBEAT_DETECT)
    798 			req->ifm_status |= IFM_ACTIVE; 	/* XXX  automedia */
    799 
    800 		break;
    801 
    802 	case EP_CHIPSET_UNKNOWN:
    803 	case EP_CHIPSET_3C509:
    804 		req->ifm_status = 0;	/* XXX */
    805 		break;
    806 
    807 	default:
    808 		printf("%s: media_status on unknown chipset 0x%x\n",
    809 		       ifp->if_xname, sc->ep_chipset);
    810 		break;
    811 	}
    812 
    813 	/* XXX look for softc heartbeat for other chips or media */
    814 
    815 	GO_WINDOW(1);
    816 	return;
    817 }
    818 
    819 
    820 
    821 /*
    822  * Start outputting on the interface.
    823  * Always called as splnet().
    824  */
    825 void
    826 epstart(ifp)
    827 	struct ifnet *ifp;
    828 {
    829 	register struct ep_softc *sc = ifp->if_softc;
    830 	bus_space_tag_t iot = sc->sc_iot;
    831 	bus_space_handle_t ioh = sc->sc_ioh;
    832 	struct mbuf *m, *m0;
    833 	int sh, len, pad;
    834 
    835 	/* Don't transmit if interface is busy or not running */
    836 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    837 		return;
    838 
    839 startagain:
    840 	/* Sneak a peek at the next packet */
    841 	m0 = ifp->if_snd.ifq_head;
    842 	if (m0 == 0)
    843 		return;
    844 
    845 	/* We need to use m->m_pkthdr.len, so require the header */
    846 	if ((m0->m_flags & M_PKTHDR) == 0)
    847 		panic("epstart: no header mbuf");
    848 	len = m0->m_pkthdr.len;
    849 
    850 	pad = (4 - len) & 3;
    851 
    852 	/*
    853 	 * The 3c509 automatically pads short packets to minimum ethernet
    854 	 * length, but we drop packets that are too large. Perhaps we should
    855 	 * truncate them instead?
    856 	 */
    857 	if (len + pad > ETHER_MAX_LEN) {
    858 		/* packet is obviously too large: toss it */
    859 		++ifp->if_oerrors;
    860 		IF_DEQUEUE(&ifp->if_snd, m0);
    861 		m_freem(m0);
    862 		goto readcheck;
    863 	}
    864 
    865 	if (bus_space_read_2(iot, ioh, EP_W1_FREE_TX) < len + pad + 4) {
    866 		bus_space_write_2(iot, ioh, EP_COMMAND,
    867 		    SET_TX_AVAIL_THRESH |
    868 		    ((len + pad + 4) >> sc->ep_pktlenshift));
    869 		/* not enough room in FIFO */
    870 		ifp->if_flags |= IFF_OACTIVE;
    871 		return;
    872 	} else {
    873 		bus_space_write_2(iot, ioh, EP_COMMAND,
    874 		    SET_TX_AVAIL_THRESH | EP_THRESH_DISABLE );
    875 	}
    876 
    877 	IF_DEQUEUE(&ifp->if_snd, m0);
    878 	if (m0 == 0)		/* not really needed */
    879 		return;
    880 
    881 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_TX_START_THRESH |
    882 	    ((len / 4 + sc->tx_start_thresh) /* >> sc->ep_pktlenshift*/) );
    883 
    884 #if NBPFILTER > 0
    885 	if (ifp->if_bpf)
    886 		bpf_mtap(ifp->if_bpf, m0);
    887 #endif
    888 
    889 	/*
    890 	 * Do the output at splhigh() so that an interrupt from another device
    891 	 * won't cause a FIFO underrun.
    892 	 */
    893 	sh = splhigh();
    894 
    895 	bus_space_write_2(iot, ioh, EP_W1_TX_PIO_WR_1, len);
    896 	bus_space_write_2(iot, ioh, EP_W1_TX_PIO_WR_1,
    897 	    0xffff);	/* Second dword meaningless */
    898 	if (EP_IS_BUS_32(sc->bustype)) {
    899 		for (m = m0; m; ) {
    900 			if (m->m_len > 3)  {
    901 				/* align our reads from core */
    902 				if (mtod(m, u_long) & 3)  {
    903 					u_long count =
    904 					    4 - (mtod(m, u_long) & 3);
    905 					bus_space_write_multi_1(iot, ioh,
    906 					    EP_W1_TX_PIO_WR_1,
    907 					    mtod(m, u_int8_t *), count);
    908 					m->m_data =
    909 					    (void *)(mtod(m, u_long) + count);
    910 					m->m_len -= count;
    911 				}
    912 				bus_space_write_multi_4(iot, ioh,
    913 				    EP_W1_TX_PIO_WR_1,
    914 				    mtod(m, u_int32_t *), m->m_len >> 2);
    915 				m->m_data = (void *)(mtod(m, u_long) +
    916 					(u_long)(m->m_len & ~3));
    917 				m->m_len -= m->m_len & ~3;
    918 			}
    919 			if (m->m_len)  {
    920 				bus_space_write_multi_1(iot, ioh,
    921 				    EP_W1_TX_PIO_WR_1,
    922 				    mtod(m, u_int8_t *), m->m_len);
    923 			}
    924 			MFREE(m, m0);
    925 			m = m0;
    926 		}
    927 	} else {
    928 		for (m = m0; m; ) {
    929 			if (m->m_len > 1)  {
    930 				if (mtod(m, u_long) & 1)  {
    931 					bus_space_write_1(iot, ioh,
    932 					    EP_W1_TX_PIO_WR_1,
    933 					    *(mtod(m, u_int8_t *)));
    934 					m->m_data =
    935 					    (void *)(mtod(m, u_long) + 1);
    936 					m->m_len -= 1;
    937 				}
    938 				bus_space_write_multi_2(iot, ioh,
    939 				    EP_W1_TX_PIO_WR_1, mtod(m, u_int16_t *),
    940 				    m->m_len >> 1);
    941 			}
    942 			if (m->m_len & 1)  {
    943 				bus_space_write_1(iot, ioh, EP_W1_TX_PIO_WR_1,
    944 				     *(mtod(m, u_int8_t *) + m->m_len - 1));
    945 			}
    946 			MFREE(m, m0);
    947 			m = m0;
    948 		}
    949 	}
    950 	while (pad--)
    951 		bus_space_write_1(iot, ioh, EP_W1_TX_PIO_WR_1, 0);
    952 
    953 	splx(sh);
    954 
    955 	++ifp->if_opackets;
    956 
    957 readcheck:
    958 	if ((bus_space_read_2(iot, ioh, EP_W1_RX_STATUS) & ERR_INCOMPLETE) == 0) {
    959 		/* We received a complete packet. */
    960 		u_int16_t status = bus_space_read_2(iot, ioh, EP_STATUS);
    961 
    962 		if ((status & S_INTR_LATCH) == 0) {
    963 			/*
    964 			 * No interrupt, read the packet and continue
    965 			 * Is  this supposed to happen? Is my motherboard
    966 			 * completely busted?
    967 			 */
    968 			epread(sc);
    969 		}
    970 		else
    971 			/* Got an interrupt, return so that it gets serviced. */
    972 			return;
    973 	}
    974 	else {
    975 		/* Check if we are stuck and reset [see XXX comment] */
    976 		if (epstatus(sc)) {
    977 			if (ifp->if_flags & IFF_DEBUG)
    978 				printf("%s: adapter reset\n",
    979 				    sc->sc_dev.dv_xname);
    980 			epreset(sc);
    981 		}
    982 	}
    983 
    984 	goto startagain;
    985 }
    986 
    987 
    988 /*
    989  * XXX: The 3c509 card can get in a mode where both the fifo status bit
    990  *	FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
    991  *	We detect this situation and we reset the adapter.
    992  *	It happens at times when there is a lot of broadcast traffic
    993  *	on the cable (once in a blue moon).
    994  */
    995 static int
    996 epstatus(sc)
    997 	register struct ep_softc *sc;
    998 {
    999 	bus_space_tag_t iot = sc->sc_iot;
   1000 	bus_space_handle_t ioh = sc->sc_ioh;
   1001 	u_int16_t fifost;
   1002 
   1003 	/*
   1004 	 * Check the FIFO status and act accordingly
   1005 	 */
   1006 	GO_WINDOW(4);
   1007 	fifost = bus_space_read_2(iot, ioh, EP_W4_FIFO_DIAG);
   1008 	GO_WINDOW(1);
   1009 
   1010 	if (fifost & FIFOS_RX_UNDERRUN) {
   1011 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1012 			printf("%s: RX underrun\n", sc->sc_dev.dv_xname);
   1013 		epreset(sc);
   1014 		return 0;
   1015 	}
   1016 
   1017 	if (fifost & FIFOS_RX_STATUS_OVERRUN) {
   1018 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1019 			printf("%s: RX Status overrun\n", sc->sc_dev.dv_xname);
   1020 		return 1;
   1021 	}
   1022 
   1023 	if (fifost & FIFOS_RX_OVERRUN) {
   1024 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1025 			printf("%s: RX overrun\n", sc->sc_dev.dv_xname);
   1026 		return 1;
   1027 	}
   1028 
   1029 	if (fifost & FIFOS_TX_OVERRUN) {
   1030 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1031 			printf("%s: TX overrun\n", sc->sc_dev.dv_xname);
   1032 		epreset(sc);
   1033 		return 0;
   1034 	}
   1035 
   1036 	return 0;
   1037 }
   1038 
   1039 
   1040 static void
   1041 eptxstat(sc)
   1042 	register struct ep_softc *sc;
   1043 {
   1044 	bus_space_tag_t iot = sc->sc_iot;
   1045 	bus_space_handle_t ioh = sc->sc_ioh;
   1046 	int i;
   1047 
   1048 	/*
   1049 	 * We need to read+write TX_STATUS until we get a 0 status
   1050 	 * in order to turn off the interrupt flag.
   1051 	 */
   1052 	while ((i = bus_space_read_1(iot, ioh, EP_W1_TX_STATUS)) & TXS_COMPLETE) {
   1053 		bus_space_write_1(iot, ioh, EP_W1_TX_STATUS, 0x0);
   1054 
   1055 		if (i & TXS_JABBER) {
   1056 			++sc->sc_ethercom.ec_if.if_oerrors;
   1057 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1058 				printf("%s: jabber (%x)\n",
   1059 				       sc->sc_dev.dv_xname, i);
   1060 			epreset(sc);
   1061 		} else if (i & TXS_UNDERRUN) {
   1062 			++sc->sc_ethercom.ec_if.if_oerrors;
   1063 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1064 				printf("%s: fifo underrun (%x) @%d\n",
   1065 				       sc->sc_dev.dv_xname, i,
   1066 				       sc->tx_start_thresh);
   1067 			if (sc->tx_succ_ok < 100)
   1068 				    sc->tx_start_thresh = min(ETHER_MAX_LEN,
   1069 					    sc->tx_start_thresh + 20);
   1070 			sc->tx_succ_ok = 0;
   1071 			epreset(sc);
   1072 		} else if (i & TXS_MAX_COLLISION) {
   1073 			++sc->sc_ethercom.ec_if.if_collisions;
   1074 			bus_space_write_2(iot, ioh, EP_COMMAND, TX_ENABLE);
   1075 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1076 		} else
   1077 			sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
   1078 	}
   1079 }
   1080 
   1081 int
   1082 epintr(arg)
   1083 	void *arg;
   1084 {
   1085 	register struct ep_softc *sc = arg;
   1086 	bus_space_tag_t iot = sc->sc_iot;
   1087 	bus_space_handle_t ioh = sc->sc_ioh;
   1088 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1089 	u_int16_t status;
   1090 	int ret = 0;
   1091 
   1092 	for (;;) {
   1093 		bus_space_write_2(iot, ioh, EP_COMMAND, C_INTR_LATCH);
   1094 
   1095 		status = bus_space_read_2(iot, ioh, EP_STATUS);
   1096 
   1097 		if ((status & (S_TX_COMPLETE | S_TX_AVAIL |
   1098 			       S_RX_COMPLETE | S_CARD_FAILURE)) == 0)
   1099 			break;
   1100 
   1101 		ret = 1;
   1102 
   1103 		/*
   1104 		 * Acknowledge any interrupts.  It's important that we do this
   1105 		 * first, since there would otherwise be a race condition.
   1106 		 * Due to the i386 interrupt queueing, we may get spurious
   1107 		 * interrupts occasionally.
   1108 		 */
   1109 		bus_space_write_2(iot, ioh, EP_COMMAND, ACK_INTR | status);
   1110 
   1111 		if (status & S_RX_COMPLETE)
   1112 			epread(sc);
   1113 		if (status & S_TX_AVAIL) {
   1114 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1115 			epstart(&sc->sc_ethercom.ec_if);
   1116 		}
   1117 		if (status & S_CARD_FAILURE) {
   1118 			printf("%s: adapter failure (%x)\n",
   1119 			    sc->sc_dev.dv_xname, status);
   1120 			epreset(sc);
   1121 			return (1);
   1122 		}
   1123 		if (status & S_TX_COMPLETE) {
   1124 			eptxstat(sc);
   1125 			epstart(ifp);
   1126 		}
   1127 	}
   1128 
   1129 	/* no more interrupts */
   1130 	return (ret);
   1131 }
   1132 
   1133 void
   1134 epread(sc)
   1135 	register struct ep_softc *sc;
   1136 {
   1137 	bus_space_tag_t iot = sc->sc_iot;
   1138 	bus_space_handle_t ioh = sc->sc_ioh;
   1139 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1140 	struct mbuf *m;
   1141 	struct ether_header *eh;
   1142 	int len;
   1143 
   1144 	len = bus_space_read_2(iot, ioh, EP_W1_RX_STATUS);
   1145 
   1146 again:
   1147 	if (ifp->if_flags & IFF_DEBUG) {
   1148 		int err = len & ERR_MASK;
   1149 		char *s = NULL;
   1150 
   1151 		if (len & ERR_INCOMPLETE)
   1152 			s = "incomplete packet";
   1153 		else if (err == ERR_OVERRUN)
   1154 			s = "packet overrun";
   1155 		else if (err == ERR_RUNT)
   1156 			s = "runt packet";
   1157 		else if (err == ERR_ALIGNMENT)
   1158 			s = "bad alignment";
   1159 		else if (err == ERR_CRC)
   1160 			s = "bad crc";
   1161 		else if (err == ERR_OVERSIZE)
   1162 			s = "oversized packet";
   1163 		else if (err == ERR_DRIBBLE)
   1164 			s = "dribble bits";
   1165 
   1166 		if (s)
   1167 			printf("%s: %s\n", sc->sc_dev.dv_xname, s);
   1168 	}
   1169 
   1170 	if (len & ERR_INCOMPLETE)
   1171 		return;
   1172 
   1173 	if (len & ERR_RX) {
   1174 		++ifp->if_ierrors;
   1175 		goto abort;
   1176 	}
   1177 
   1178 	len &= RX_BYTES_MASK;	/* Lower 11 bits = RX bytes. */
   1179 
   1180 	/* Pull packet off interface. */
   1181 	m = epget(sc, len);
   1182 	if (m == 0) {
   1183 		ifp->if_ierrors++;
   1184 		goto abort;
   1185 	}
   1186 
   1187 	++ifp->if_ipackets;
   1188 
   1189 	/* We assume the header fit entirely in one mbuf. */
   1190 	eh = mtod(m, struct ether_header *);
   1191 
   1192 #if NBPFILTER > 0
   1193 	/*
   1194 	 * Check if there's a BPF listener on this interface.
   1195 	 * If so, hand off the raw packet to BPF.
   1196 	 */
   1197 	if (ifp->if_bpf) {
   1198 		bpf_mtap(ifp->if_bpf, m);
   1199 
   1200 		/*
   1201 		 * Note that the interface cannot be in promiscuous mode if
   1202 		 * there are no BPF listeners.  And if we are in promiscuous
   1203 		 * mode, we have to check if this packet is really ours.
   1204 		 */
   1205 		if ((ifp->if_flags & IFF_PROMISC) &&
   1206 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
   1207 		    bcmp(eh->ether_dhost, LLADDR(sc->sc_ethercom.ec_if.if_sadl),
   1208 			    sizeof(eh->ether_dhost)) != 0) {
   1209 			m_freem(m);
   1210 			return;
   1211 		}
   1212 	}
   1213 #endif
   1214 
   1215 	/* We assume the header fit entirely in one mbuf. */
   1216 	m_adj(m, sizeof(struct ether_header));
   1217 	ether_input(ifp, eh, m);
   1218 
   1219 	/*
   1220 	 * In periods of high traffic we can actually receive enough
   1221 	 * packets so that the fifo overrun bit will be set at this point,
   1222 	 * even though we just read a packet. In this case we
   1223 	 * are not going to receive any more interrupts. We check for
   1224 	 * this condition and read again until the fifo is not full.
   1225 	 * We could simplify this test by not using epstatus(), but
   1226 	 * rechecking the RX_STATUS register directly. This test could
   1227 	 * result in unnecessary looping in cases where there is a new
   1228 	 * packet but the fifo is not full, but it will not fix the
   1229 	 * stuck behavior.
   1230 	 *
   1231 	 * Even with this improvement, we still get packet overrun errors
   1232 	 * which are hurting performance. Maybe when I get some more time
   1233 	 * I'll modify epread() so that it can handle RX_EARLY interrupts.
   1234 	 */
   1235 	if (epstatus(sc)) {
   1236 		len = bus_space_read_2(iot, ioh, EP_W1_RX_STATUS);
   1237 		/* Check if we are stuck and reset [see XXX comment] */
   1238 		if (len & ERR_INCOMPLETE) {
   1239 			if (ifp->if_flags & IFF_DEBUG)
   1240 				printf("%s: adapter reset\n",
   1241 				    sc->sc_dev.dv_xname);
   1242 			epreset(sc);
   1243 			return;
   1244 		}
   1245 		goto again;
   1246 	}
   1247 
   1248 	return;
   1249 
   1250 abort:
   1251 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
   1252 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
   1253 		;
   1254 }
   1255 
   1256 struct mbuf *
   1257 epget(sc, totlen)
   1258 	struct ep_softc *sc;
   1259 	int totlen;
   1260 {
   1261 	bus_space_tag_t iot = sc->sc_iot;
   1262 	bus_space_handle_t ioh = sc->sc_ioh;
   1263 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1264 	struct mbuf *top, **mp, *m;
   1265 	int len, remaining;
   1266 	int sh;
   1267 
   1268 	m = sc->mb[sc->next_mb];
   1269 	sc->mb[sc->next_mb] = 0;
   1270 	if (m == 0) {
   1271 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1272 		if (m == 0)
   1273 			return 0;
   1274 	} else {
   1275 		/* If the queue is no longer full, refill. */
   1276 		if (sc->last_mb == sc->next_mb)
   1277 			timeout(epmbuffill, sc, 1);
   1278 		/* Convert one of our saved mbuf's. */
   1279 		sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
   1280 		m->m_data = m->m_pktdat;
   1281 		m->m_flags = M_PKTHDR;
   1282 	}
   1283 	m->m_pkthdr.rcvif = ifp;
   1284 	m->m_pkthdr.len = totlen;
   1285 	len = MHLEN;
   1286 	top = 0;
   1287 	mp = &top;
   1288 
   1289 	/*
   1290 	 * We read the packet at splhigh() so that an interrupt from another
   1291 	 * device doesn't cause the card's buffer to overflow while we're
   1292 	 * reading it.  We may still lose packets at other times.
   1293 	 */
   1294 	sh = splhigh();
   1295 
   1296 	while (totlen > 0) {
   1297 		if (top) {
   1298 			m = sc->mb[sc->next_mb];
   1299 			sc->mb[sc->next_mb] = 0;
   1300 			if (m == 0) {
   1301 				MGET(m, M_DONTWAIT, MT_DATA);
   1302 				if (m == 0) {
   1303 					splx(sh);
   1304 					m_freem(top);
   1305 					return 0;
   1306 				}
   1307 			} else {
   1308 				sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
   1309 			}
   1310 			len = MLEN;
   1311 		}
   1312 		if (totlen >= MINCLSIZE) {
   1313 			MCLGET(m, M_DONTWAIT);
   1314 			if ((m->m_flags & M_EXT) == 0)
   1315 				splx(sh);
   1316 				m_freem(top);
   1317 				return 0;
   1318 			}
   1319 			len = MCLBYTES;
   1320 		}
   1321 		if (top == 0)  {
   1322 			/* align the struct ip header */
   1323 			caddr_t newdata = (caddr_t)
   1324 			    ALIGN(m->m_data + sizeof(struct ether_header))
   1325 			    - sizeof(struct ether_header);
   1326 			len -= newdata - m->m_data;
   1327 			m->m_data = newdata;
   1328 		}
   1329 		remaining = len = min(totlen, len);
   1330 		if (EP_IS_BUS_32(sc->bustype)) {
   1331 			u_long offset = mtod(m, u_long);
   1332 			/*
   1333 			 * Read bytes up to the point where we are aligned.
   1334 			 * (We can align to 4 bytes, rather than ALIGNBYTES,
   1335 			 * here because we're later reading 4-byte chunks.)
   1336 			 */
   1337 			if ((remaining > 3) && (offset & 3))  {
   1338 				int count = (4 - (offset & 3));
   1339 				bus_space_read_multi_1(iot, ioh,
   1340 				    EP_W1_RX_PIO_RD_1,
   1341 				    (u_int8_t *) offset, count);
   1342 				offset += count;
   1343 				remaining -= count;
   1344 			}
   1345 			if (remaining > 3) {
   1346 				bus_space_read_multi_4(iot, ioh,
   1347 				    EP_W1_RX_PIO_RD_1,
   1348 				    (u_int32_t *) offset, remaining >> 2);
   1349 				offset += remaining & ~3;
   1350 				remaining &= 3;
   1351 			}
   1352 			if (remaining)  {
   1353 				bus_space_read_multi_1(iot, ioh,
   1354 				    EP_W1_RX_PIO_RD_1,
   1355 				    (u_int8_t *) offset, remaining);
   1356 			}
   1357 		} else {
   1358 			u_long offset = mtod(m, u_long);
   1359 			if ((remaining > 1) && (offset & 1))  {
   1360 				bus_space_read_multi_1(iot, ioh,
   1361 				    EP_W1_RX_PIO_RD_1,
   1362 				    (u_int8_t *) offset, 1);
   1363 				remaining -= 1;
   1364 				offset += 1;
   1365 			}
   1366 			if (remaining > 1) {
   1367 				bus_space_read_multi_2(iot, ioh,
   1368 				    EP_W1_RX_PIO_RD_1,
   1369 				    (u_int16_t *) offset, remaining >> 1);
   1370 				offset += remaining & ~1;
   1371 			}
   1372 			if (remaining & 1)  {
   1373 				bus_space_read_multi_1(iot, ioh,
   1374 				    EP_W1_RX_PIO_RD_1,
   1375 				    (u_int8_t *) offset, remaining & 1);
   1376 			}
   1377 		}
   1378 		m->m_len = len;
   1379 		totlen -= len;
   1380 		*mp = m;
   1381 		mp = &m->m_next;
   1382 	}
   1383 
   1384 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
   1385 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
   1386 		;
   1387 
   1388 	splx(sh);
   1389 
   1390 	return top;
   1391 }
   1392 
   1393 int
   1394 epioctl(ifp, cmd, data)
   1395 	register struct ifnet *ifp;
   1396 	u_long cmd;
   1397 	caddr_t data;
   1398 {
   1399 	struct ep_softc *sc = ifp->if_softc;
   1400 	struct ifaddr *ifa = (struct ifaddr *)data;
   1401 	struct ifreq *ifr = (struct ifreq *)data;
   1402 	int s, error = 0;
   1403 
   1404 	s = splnet();
   1405 
   1406 	switch (cmd) {
   1407 
   1408 	case SIOCSIFADDR:
   1409 		ifp->if_flags |= IFF_UP;
   1410 
   1411 		switch (ifa->ifa_addr->sa_family) {
   1412 #ifdef INET
   1413 		case AF_INET:
   1414 			epinit(sc);
   1415 			arp_ifinit(&sc->sc_ethercom.ec_if, ifa);
   1416 			break;
   1417 #endif
   1418 #ifdef NS
   1419 		case AF_NS:
   1420 		    {
   1421 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1422 
   1423 			if (ns_nullhost(*ina))
   1424 				ina->x_host = *(union ns_host *)
   1425 				    LLADDR(ifp->if_sadl);
   1426 			else
   1427 				bcopy(ina->x_host.c_host,
   1428 				    LLADDR(ifp->if_sadl),
   1429 				    ifp->if_addrlen);
   1430 			/* Set new address. */
   1431 			epinit(sc);
   1432 			break;
   1433 		    }
   1434 #endif
   1435 		default:
   1436 			epinit(sc);
   1437 			break;
   1438 		}
   1439 		break;
   1440 
   1441 	case SIOCSIFMEDIA:
   1442 	case SIOCGIFMEDIA:
   1443 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1444 		break;
   1445 
   1446 	case SIOCSIFFLAGS:
   1447 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1448 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1449 			/*
   1450 			 * If interface is marked down and it is running, then
   1451 			 * stop it.
   1452 			 */
   1453 			epstop(sc);
   1454 			ifp->if_flags &= ~IFF_RUNNING;
   1455 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1456 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1457 			/*
   1458 			 * If interface is marked up and it is stopped, then
   1459 			 * start it.
   1460 			 */
   1461 			epinit(sc);
   1462 		} else {
   1463 			/*
   1464 			 * deal with flags changes:
   1465 			 * IFF_MULTICAST, IFF_PROMISC.
   1466 			 */
   1467 			epsetfilter(sc);
   1468 		}
   1469 		break;
   1470 
   1471 	case SIOCADDMULTI:
   1472 	case SIOCDELMULTI:
   1473 		error = (cmd == SIOCADDMULTI) ?
   1474 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1475 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1476 
   1477 		if (error == ENETRESET) {
   1478 			/*
   1479 			 * Multicast list has changed; set the hardware filter
   1480 			 * accordingly.
   1481 			 */
   1482 			epreset(sc);
   1483 			error = 0;
   1484 		}
   1485 		break;
   1486 
   1487 	default:
   1488 		error = EINVAL;
   1489 		break;
   1490 	}
   1491 
   1492 	splx(s);
   1493 	return (error);
   1494 }
   1495 
   1496 void
   1497 epreset(sc)
   1498 	struct ep_softc *sc;
   1499 {
   1500 	int s;
   1501 
   1502 	s = splnet();
   1503 	epstop(sc);
   1504 	epinit(sc);
   1505 	splx(s);
   1506 }
   1507 
   1508 void
   1509 epwatchdog(ifp)
   1510 	struct ifnet *ifp;
   1511 {
   1512 	struct ep_softc *sc = ifp->if_softc;
   1513 
   1514 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1515 	++sc->sc_ethercom.ec_if.if_oerrors;
   1516 
   1517 	epreset(sc);
   1518 }
   1519 
   1520 void
   1521 epstop(sc)
   1522 	register struct ep_softc *sc;
   1523 {
   1524 	bus_space_tag_t iot = sc->sc_iot;
   1525 	bus_space_handle_t ioh = sc->sc_ioh;
   1526 
   1527 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISABLE);
   1528 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
   1529 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
   1530 		;
   1531 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_DISABLE);
   1532 	bus_space_write_2(iot, ioh, EP_COMMAND, STOP_TRANSCEIVER);
   1533 
   1534 	ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
   1535 	ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
   1536 
   1537 	bus_space_write_2(iot, ioh, EP_COMMAND, C_INTR_LATCH);
   1538 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RD_0_MASK);
   1539 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_INTR_MASK);
   1540 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RX_FILTER);
   1541 
   1542 	epmbufempty(sc);
   1543 }
   1544 
   1545 
   1546 /*
   1547  * Before reboots, reset card completely.
   1548  */
   1549 static void
   1550 epshutdown(arg)
   1551 	void *arg;
   1552 {
   1553 	register struct ep_softc *sc = arg;
   1554 
   1555 	epstop(sc);
   1556 	ep_complete_cmd(sc, EP_COMMAND, GLOBAL_RESET);
   1557 }
   1558 
   1559 /*
   1560  * We get eeprom data from the id_port given an offset into the
   1561  * eeprom.  Basically; after the ID_sequence is sent to all of
   1562  * the cards; they enter the ID_CMD state where they will accept
   1563  * command requests. 0x80-0xbf loads the eeprom data.  We then
   1564  * read the port 16 times and with every read; the cards check
   1565  * for contention (ie: if one card writes a 0 bit and another
   1566  * writes a 1 bit then the host sees a 0. At the end of the cycle;
   1567  * each card compares the data on the bus; if there is a difference
   1568  * then that card goes into ID_WAIT state again). In the meantime;
   1569  * one bit of data is returned in the AX register which is conveniently
   1570  * returned to us by bus_space_read_1().  Hence; we read 16 times getting one
   1571  * bit of data with each read.
   1572  *
   1573  * NOTE: the caller must provide an i/o handle for ELINK_ID_PORT!
   1574  */
   1575 u_int16_t
   1576 epreadeeprom(iot, ioh, offset)
   1577 	bus_space_tag_t iot;
   1578 	bus_space_handle_t ioh;
   1579 	int offset;
   1580 {
   1581 	u_int16_t data = 0;
   1582 	int i;
   1583 
   1584 	bus_space_write_1(iot, ioh, 0, 0x80 + offset);
   1585 	delay(1000);
   1586 	for (i = 0; i < 16; i++)
   1587 		data = (data << 1) | (bus_space_read_2(iot, ioh, 0) & 1);
   1588 	return (data);
   1589 }
   1590 
   1591 static int
   1592 epbusyeeprom(sc)
   1593 	struct ep_softc *sc;
   1594 {
   1595 	bus_space_tag_t iot = sc->sc_iot;
   1596 	bus_space_handle_t ioh = sc->sc_ioh;
   1597 	int i = 100, j;
   1598 
   1599 	if (sc->bustype == EP_BUS_PCMCIA) {
   1600 		delay(1000);
   1601 		return 0;
   1602 	}
   1603 
   1604 	while (i--) {
   1605 		j = bus_space_read_2(iot, ioh, EP_W0_EEPROM_COMMAND);
   1606 		if (j & EEPROM_BUSY)
   1607 			delay(100);
   1608 		else
   1609 			break;
   1610 	}
   1611 	if (!i) {
   1612 		printf("\n%s: eeprom failed to come ready\n",
   1613 		    sc->sc_dev.dv_xname);
   1614 		return (1);
   1615 	}
   1616 	if (j & EEPROM_TST_MODE) {
   1617 		printf("\n%s: erase pencil mark, or disable plug-n-play mode!\n",
   1618 		    sc->sc_dev.dv_xname);
   1619 		return (1);
   1620 	}
   1621 	return (0);
   1622 }
   1623 
   1624 void
   1625 epmbuffill(v)
   1626 	void *v;
   1627 {
   1628 	struct ep_softc *sc = v;
   1629 	int s, i;
   1630 
   1631 	s = splnet();
   1632 	i = sc->last_mb;
   1633 	do {
   1634 		if (sc->mb[i] == NULL)
   1635 			MGET(sc->mb[i], M_DONTWAIT, MT_DATA);
   1636 		if (sc->mb[i] == NULL)
   1637 			break;
   1638 		i = (i + 1) % MAX_MBS;
   1639 	} while (i != sc->next_mb);
   1640 	sc->last_mb = i;
   1641 	/* If the queue was not filled, try again. */
   1642 	if (sc->last_mb != sc->next_mb)
   1643 		timeout(epmbuffill, sc, 1);
   1644 	splx(s);
   1645 }
   1646 
   1647 void
   1648 epmbufempty(sc)
   1649 	struct ep_softc *sc;
   1650 {
   1651 	int s, i;
   1652 
   1653 	s = splnet();
   1654 	for (i = 0; i<MAX_MBS; i++) {
   1655 		if (sc->mb[i]) {
   1656 			m_freem(sc->mb[i]);
   1657 			sc->mb[i] = NULL;
   1658 		}
   1659 	}
   1660 	sc->last_mb = sc->next_mb = 0;
   1661 	untimeout(epmbuffill, sc);
   1662 	splx(s);
   1663 }
   1664