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