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