Home | History | Annotate | Line # | Download | only in ic
elink3.c revision 1.19
      1 /*	$NetBSD: elink3.c,v 1.19 1997/02/16 04:09: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 "bpfilter.h"
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/mbuf.h>
     39 #include <sys/socket.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/errno.h>
     42 #include <sys/syslog.h>
     43 #include <sys/select.h>
     44 #include <sys/device.h>
     45 
     46 #include <net/if.h>
     47 #include <net/netisr.h>
     48 #include <net/if_dl.h>
     49 #include <net/if_types.h>
     50 #include <net/netisr.h>
     51 
     52 #ifdef INET
     53 #include <netinet/in.h>
     54 #include <netinet/in_systm.h>
     55 #include <netinet/in_var.h>
     56 #include <netinet/ip.h>
     57 #include <netinet/if_ether.h>
     58 #endif
     59 
     60 #ifdef NS
     61 #include <netns/ns.h>
     62 #include <netns/ns_if.h>
     63 #endif
     64 
     65 #if NBPFILTER > 0
     66 #include <net/bpf.h>
     67 #include <net/bpfdesc.h>
     68 #endif
     69 
     70 #include <machine/cpu.h>
     71 #include <machine/bus.h>
     72 #include <machine/intr.h>
     73 
     74 #include <dev/ic/elink3var.h>
     75 #include <dev/ic/elink3reg.h>
     76 
     77 #define ETHER_MIN_LEN	64
     78 #define ETHER_MAX_LEN   1518
     79 #define ETHER_ADDR_LEN  6
     80 
     81 struct cfdriver ep_cd = {
     82 	NULL, "ep", DV_IFNET
     83 };
     84 
     85 void	ep_internalconfig __P((struct ep_softc *sc));
     86 void	ep_vortex_internalconfig __P((struct ep_softc *sc));
     87 static void eptxstat __P((struct ep_softc *));
     88 static int epstatus __P((struct ep_softc *));
     89 void epinit __P((struct ep_softc *));
     90 int epioctl __P((struct ifnet *, u_long, caddr_t));
     91 void epstart __P((struct ifnet *));
     92 void epwatchdog __P((struct ifnet *));
     93 void epreset __P((struct ep_softc *));
     94 static void epshutdown __P((void *));
     95 void epread __P((struct ep_softc *));
     96 struct mbuf *epget __P((struct ep_softc *, int));
     97 void epmbuffill __P((void *));
     98 void epmbufempty __P((struct ep_softc *));
     99 void epsetfilter __P((struct ep_softc *));
    100 void epsetlink __P((struct ep_softc *));
    101 
    102 static int epbusyeeprom __P((struct ep_softc *));
    103 static inline void ep_complete_cmd __P((struct ep_softc *sc,
    104 					u_int cmd, u_int arg));
    105 
    106 
    107 /*
    108  * Issue a (reset) command, and be sure it has completed.
    109  * Used for commands that reset part or all of the  board.
    110  * On newer hardware we could poll SC_COMMAND_IN_PROGRESS,
    111  * but older hardware doesn't implement it and we must delay.
    112  * It's easiest to just delay always.
    113  */
    114 static inline void
    115 ep_complete_cmd(sc, cmd, arg)
    116 	struct ep_softc *sc;
    117 	u_int cmd, arg;
    118 {
    119 	register bus_space_tag_t iot = sc->sc_iot;
    120 	register bus_space_handle_t ioh = sc->sc_ioh;
    121 
    122 	bus_space_write_2(iot, ioh, cmd, arg);
    123 
    124 #ifdef notyet
    125 	/* if this adapter family has S_COMMAND_IN_PROGRESS, use it */
    126 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
    127 		;
    128 	else
    129 #else
    130 	DELAY(100000);	/* need at least 1 ms, but be generous. */
    131 #endif
    132 }
    133 
    134 
    135 
    136 void
    137 epconfig(sc, conn)
    138 	struct ep_softc *sc;
    139 	u_int conn;
    140 {
    141 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    142 	bus_space_tag_t iot = sc->sc_iot;
    143 	bus_space_handle_t ioh = sc->sc_ioh;
    144 	u_int16_t i;
    145 
    146 	printf("%s: ", sc->sc_dev.dv_xname);
    147 
    148 
    149 	/* print RAM size */
    150 	ep_internalconfig(sc);
    151 	GO_WINDOW(0);
    152 
    153 	/* determine connectors available */
    154 	sc->ep_connectors = 0;
    155 	if (conn & IS_AUI) {
    156 		printf("aui");
    157 		sc->ep_connectors |= AUI;
    158 	}
    159 	if (conn & IS_BNC) {
    160 		if (sc->ep_connectors)
    161 			printf("/");
    162 		printf("bnc");
    163 		sc->ep_connectors |= BNC;
    164 	}
    165 	if (conn & IS_UTP) {
    166 		if (sc->ep_connectors)
    167 			printf("/");
    168 		printf("10baseT");
    169 		sc->ep_connectors |= UTP;
    170 	}
    171 	if (conn & IS_100BASE_TX) {
    172 		if (sc->ep_connectors)
    173 			printf("/");
    174 		printf("100base-TX");
    175 		sc->ep_connectors |= TX;
    176 	}
    177 	if (conn & IS_100BASE_T4) {
    178 		if (sc->ep_connectors)
    179 			printf("/");
    180 		printf("100base-T4");
    181 		sc->ep_connectors |= T4;
    182 	}
    183 	if (conn & IS_100BASE_FX) {
    184 		if (sc->ep_connectors)
    185 			printf("/");
    186 		printf("100base-FX");
    187 		sc->ep_connectors |= FX;
    188 	}
    189 	if (conn & IS_100BASE_MII) {
    190 		if (sc->ep_connectors)
    191 			printf("/");
    192 		printf("MII");
    193 		sc->ep_connectors |= MII;
    194 	}
    195 
    196 	if (!sc->ep_connectors)
    197 		printf("no connectors!");
    198 	printf("\n");
    199 
    200 	/*
    201 	 * Read the station address from the eeprom
    202 	 */
    203 	for (i = 0; i < 3; i++) {
    204 		u_int16_t x;
    205 		if (epbusyeeprom(sc))
    206 			return;
    207 		bus_space_write_2(iot, ioh, EP_W0_EEPROM_COMMAND,
    208 		    READ_EEPROM | i);
    209 		if (epbusyeeprom(sc))
    210 			return;
    211 		x = bus_space_read_2(iot, ioh, EP_W0_EEPROM_DATA);
    212 		sc->sc_arpcom.ac_enaddr[(i << 1)] = x >> 8;
    213 		sc->sc_arpcom.ac_enaddr[(i << 1) + 1] = x;
    214 	}
    215 
    216 	printf("%s: MAC address %s\n", sc->sc_dev.dv_xname,
    217 	    ether_sprintf(sc->sc_arpcom.ac_enaddr));
    218 
    219 	/*
    220 	 * Vortex-based (3c59x, eisa)? and Boomerang (3c900)cards allow
    221 	 * FDDI-sized (4500) byte packets.  Commands only take an 11-bit
    222 	 * parameter, and  11 bits isn't enough to hold a full-size pkt length.
    223 	 * Commands to these cards implicitly upshift a packet size
    224 	 * or threshold by 2 bits.
    225 	 * To detect  cards with large-packet support, we probe by setting
    226 	 * the transmit threshold register, then change windows and
    227 	 * read back the threshold register directly, and see if the
    228 	 * threshold value was shifted or not.
    229 	 */
    230 	bus_space_write_2(iot, ioh, EP_COMMAND,
    231 			  SET_TX_AVAIL_THRESH | EP_LARGEWIN_PROBE );
    232 	GO_WINDOW(5);
    233 	i = bus_space_read_2(iot, ioh, EP_W5_TX_AVAIL_THRESH);
    234 	GO_WINDOW(1);
    235 	switch (i)  {
    236 	case EP_LARGEWIN_PROBE:
    237 	case (EP_LARGEWIN_PROBE & EP_LARGEWIN_MASK):
    238 		sc->ep_pktlenshift = 0;
    239 		break;
    240 
    241 	case (EP_LARGEWIN_PROBE << 2):
    242 		sc->ep_pktlenshift = 2;
    243 		/* XXX do 3c579, 3c515 support Vortex-style RESET_OPTIONS? */
    244 		if (sc->bustype == EP_BUS_PCI)
    245 			ep_vortex_internalconfig(sc);
    246 		break;
    247 
    248 	default:
    249 		printf("%s: wrote %d to TX_AVAIL_THRESH, read back %d. "
    250 		    "Interface disabled\n",
    251 		    sc->sc_dev.dv_xname, EP_THRESH_DISABLE, (int) i);
    252 		return;
    253 	}
    254 	/*
    255 	 * Ensure Tx-available interrupts are enabled for
    256 	 * start the interface.
    257 	 * XXX should be in epinit().
    258 	 */
    259 	bus_space_write_2(iot, ioh, EP_COMMAND,
    260 	    SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
    261 
    262 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    263 	ifp->if_softc = sc;
    264 	ifp->if_start = epstart;
    265 	ifp->if_ioctl = epioctl;
    266 	ifp->if_watchdog = epwatchdog;
    267 	ifp->if_flags =
    268 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    269 
    270 	if_attach(ifp);
    271 	ether_ifattach(ifp);
    272 
    273 #if NBPFILTER > 0
    274 	bpfattach(&sc->sc_arpcom.ac_if.if_bpf, ifp, DLT_EN10MB,
    275 		  sizeof(struct ether_header));
    276 #endif
    277 
    278 	sc->tx_start_thresh = 20;	/* probably a good starting point. */
    279 
    280 	/*  Establish callback to reset card when we reboot. */
    281 	shutdownhook_establish(epshutdown, sc);
    282 
    283 #if 1
    284 	/* XXX */
    285 	ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
    286 	ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
    287 #else
    288 	epinit(sc);		/*XXX fix up after probe */
    289 	DELAY(5000);
    290 	epstop(sc);		/*XXX reset after probe, stop interface. */
    291 	DELAY(5000);
    292 #endif
    293 }
    294 
    295 /*
    296  * Show interface-model-independent info from window 3
    297  * internal-configuration register.
    298  */
    299 void
    300 ep_internalconfig(sc)
    301 	struct ep_softc *sc;
    302 {
    303 	bus_space_tag_t iot = sc->sc_iot;
    304 	bus_space_handle_t ioh = sc->sc_ioh;
    305 
    306 	u_int config0;
    307 	u_int config1;
    308 
    309 	int  ram_size, ram_width, ram_speed, rom_size, ram_split;
    310 	/*
    311 	 * NVRAM buffer Rx:Tx config names for busmastering cards
    312 	 * (Demon, Vortex, and later).
    313 	 */
    314 	const char *onboard_ram_config[] = {
    315 		"5:3", "3:1", "1:1", "(undefined)" };
    316 
    317 	GO_WINDOW(3);
    318 	config0 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG);
    319 	config1 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG+2);
    320 	GO_WINDOW(0);
    321 
    322 	ram_size  = (config0 & CONFIG_RAMSIZE) >> CONFIG_RAMSIZE_SHIFT;
    323 	ram_width = (config0 & CONFIG_RAMWIDTH) >> CONFIG_RAMWIDTH_SHIFT;
    324 	ram_speed = (config0 & CONFIG_RAMSPEED) >> CONFIG_RAMSPEED_SHIFT;
    325 	rom_size  = (config0 & CONFIG_ROMSIZE) >> CONFIG_ROMSIZE_SHIFT;
    326 
    327 	ram_split  = (config1 & CONFIG_RAMSPLIT) >> CONFIG_RAMSPLIT_SHIFT;
    328 
    329 	printf("%dKB %s-wide FIFO, %s Rx:Tx split, ",
    330 	    8 << ram_size,
    331 	    (ram_width) ? "word" : "byte",
    332 	    onboard_ram_config[ram_split]);
    333 }
    334 
    335 
    336 /*
    337  * Show onboard configuration of large-packet-capable elink3 devices (Demon,
    338  * Vortex, Boomerang),  using media and card-version info in window 3.
    339  *
    340  * XXX how much of this works with 3c515, pcmcia 10/100?  With 3c509, 3c589?
    341  */
    342 void
    343 ep_vortex_internalconfig(sc)
    344 	struct ep_softc *sc;
    345 {
    346 	bus_space_tag_t iot = sc->sc_iot;
    347 	bus_space_handle_t ioh = sc->sc_ioh;
    348 	u_int config0;
    349 	u_int config1;
    350 	int reset_options;
    351 
    352 	int  media_mask, autoselect;
    353 	/*  Names for  media in the media bitmask field. */
    354 	const char *medium_name;
    355 	const char *media_names[8] ={
    356 		"10baseT",
    357 		"10base AUI",
    358 		"undefined",
    359 		"10base2",
    360 		"100baseTX",
    361 		"100baseFX",
    362 		"MII",
    363 		"100baseT4"};
    364 
    365 	GO_WINDOW(3);
    366 	config0 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG);
    367 	config1 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG+2);
    368 	reset_options  = (int)bus_space_read_1(iot, ioh, EP_W3_RESET_OPTIONS);
    369 	GO_WINDOW(0);
    370 
    371 	media_mask = (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
    372         autoselect = (config1 & CONFIG_AUTOSELECT) >> CONFIG_AUTOSELECT_SHIFT;
    373 
    374 	medium_name = (media_mask > 8) ? "(unknown/impossible media)"
    375 		                       : media_names[media_mask];
    376 
    377 	media_mask = (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
    378         autoselect = (config1 & CONFIG_AUTOSELECT) >> CONFIG_AUTOSELECT_SHIFT;
    379 
    380 
    381 	printf("%s: default medium %s, autoselect %s\n",
    382 	       sc->sc_dev.dv_xname,
    383 	       medium_name,  (autoselect)? "on" : "off" );
    384 }
    385 
    386 /*
    387  * The order in here seems important. Otherwise we may not receive
    388  * interrupts. ?!
    389  */
    390 void
    391 epinit(sc)
    392 	register struct ep_softc *sc;
    393 {
    394 	register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    395 	bus_space_tag_t iot = sc->sc_iot;
    396 	bus_space_handle_t ioh = sc->sc_ioh;
    397 	int i;
    398 
    399 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
    400 		;
    401 
    402 	if (sc->bustype != EP_BUS_PCI) {
    403 		GO_WINDOW(0);
    404 		bus_space_write_2(iot, ioh, EP_W0_CONFIG_CTRL, 0);
    405 		bus_space_write_2(iot, ioh, EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
    406 	}
    407 
    408 	if (sc->bustype == EP_BUS_PCMCIA) {
    409 #ifdef EP_COAX_DEFAULT
    410 		bus_space_write_2(iot, ioh, EP_W0_ADDRESS_CFG,3<<14);
    411 #else
    412 		bus_space_write_2(iot, ioh, EP_W0_ADDRESS_CFG,0<<14);
    413 #endif
    414 		bus_space_write_2(iot, ioh, EP_W0_RESOURCE_CFG, 0x3f00);
    415 	}
    416 
    417 	GO_WINDOW(2);
    418 	for (i = 0; i < 6; i++)	/* Reload the ether_addr. */
    419 		bus_space_write_1(iot, ioh, EP_W2_ADDR_0 + i,
    420 		    sc->sc_arpcom.ac_enaddr[i]);
    421 
    422 	/*
    423 	 * Reset the station-address receive filter.
    424 	 * A bug workaround for busmastering  (Vortex, Demon) cards.
    425 	 */
    426 	for (i = 0; i < 6; i++)
    427 		bus_space_write_1(iot, ioh, EP_W2_RECVMASK_0 + i, 0);
    428 
    429 	ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
    430 	ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
    431 
    432 	GO_WINDOW(1);		/* Window 1 is operating window */
    433 	for (i = 0; i < 31; i++)
    434 		bus_space_read_1(iot, ioh, EP_W1_TX_STATUS);
    435 
    436 	/* Enable interrupts. */
    437 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RD_0_MASK | S_CARD_FAILURE |
    438 				S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
    439 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_INTR_MASK | S_CARD_FAILURE |
    440 				S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
    441 
    442 	/*
    443 	 * Attempt to get rid of any stray interrupts that occured during
    444 	 * configuration.  On the i386 this isn't possible because one may
    445 	 * already be queued.  However, a single stray interrupt is
    446 	 * unimportant.
    447 	 */
    448 	bus_space_write_2(iot, ioh, EP_COMMAND, ACK_INTR | 0xff);
    449 
    450 	epsetfilter(sc);
    451 	epsetlink(sc);
    452 
    453 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_ENABLE);
    454 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_ENABLE);
    455 
    456 	epmbuffill(sc);
    457 
    458 	/* Interface is now `running', with no output active. */
    459 	ifp->if_flags |= IFF_RUNNING;
    460 	ifp->if_flags &= ~IFF_OACTIVE;
    461 
    462 	/* Attempt to start output, if any. */
    463 	epstart(ifp);
    464 }
    465 
    466 void
    467 epsetfilter(sc)
    468 	register struct ep_softc *sc;
    469 {
    470 	register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    471 
    472 	GO_WINDOW(1);		/* Window 1 is operating window */
    473 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, EP_COMMAND, SET_RX_FILTER |
    474 	    FIL_INDIVIDUAL | FIL_BRDCST |
    475 	    ((ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0 ) |
    476 	    ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 ));
    477 }
    478 
    479 /*
    480  * select media based on link{0,1,2} switches.
    481  * Assumes 10Mbit interface, totatlly broken for 10/100 adaptors.
    482  */
    483 void
    484 epsetlink(sc)
    485 	register struct ep_softc *sc;
    486 {
    487 	register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    488 	bus_space_tag_t iot = sc->sc_iot;
    489 	bus_space_handle_t ioh = sc->sc_ioh;
    490 
    491 	/*
    492 	 * you can `ifconfig (link0|-link0) ep0' to get the following
    493 	 * behaviour:
    494 	 *	-link0	disable AUI/UTP. enable BNC.
    495 	 *	link0	disable BNC. enable AUI.
    496 	 *	link1	if the card has a UTP connector, and link0 is
    497 	 *		set too, then you get the UTP port.
    498 	 */
    499 	GO_WINDOW(4);
    500 	bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE, DISABLE_UTP);
    501 	if (!(ifp->if_flags & IFF_LINK0) && (sc->ep_connectors & BNC)) {
    502 		if (sc->bustype == EP_BUS_PCMCIA) {
    503 			GO_WINDOW(0);
    504 			bus_space_write_2(iot, ioh, EP_W0_ADDRESS_CFG,3<<14);
    505 			GO_WINDOW(1);
    506 		}
    507 		bus_space_write_2(iot, ioh, EP_COMMAND, START_TRANSCEIVER);
    508 		delay(1000);
    509 	}
    510 	if (ifp->if_flags & IFF_LINK0) {
    511 		bus_space_write_2(iot, ioh, EP_COMMAND, STOP_TRANSCEIVER);
    512 		delay(1000);
    513 		if ((ifp->if_flags & IFF_LINK1) && (sc->ep_connectors & UTP)) {
    514 			if (sc->bustype == EP_BUS_PCMCIA) {
    515 				GO_WINDOW(0);
    516 				bus_space_write_2(iot, ioh,
    517 				    EP_W0_ADDRESS_CFG,0<<14);
    518 				GO_WINDOW(4);
    519 			}
    520 			bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE, ENABLE_UTP);
    521 		}
    522 	}
    523 	GO_WINDOW(1);
    524 }
    525 
    526 /*
    527  * Start outputting on the interface.
    528  * Always called as splnet().
    529  */
    530 void
    531 epstart(ifp)
    532 	struct ifnet *ifp;
    533 {
    534 	register struct ep_softc *sc = ifp->if_softc;
    535 	bus_space_tag_t iot = sc->sc_iot;
    536 	bus_space_handle_t ioh = sc->sc_ioh;
    537 	struct mbuf *m, *m0;
    538 	int sh, len, pad;
    539 
    540 	/* Don't transmit if interface is busy or not running */
    541 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    542 		return;
    543 
    544 startagain:
    545 	/* Sneak a peek at the next packet */
    546 	m0 = ifp->if_snd.ifq_head;
    547 	if (m0 == 0)
    548 		return;
    549 
    550 	/* We need to use m->m_pkthdr.len, so require the header */
    551 	if ((m0->m_flags & M_PKTHDR) == 0)
    552 		panic("epstart: no header mbuf");
    553 	len = m0->m_pkthdr.len;
    554 
    555 	pad = (4 - len) & 3;
    556 
    557 	/*
    558 	 * The 3c509 automatically pads short packets to minimum ethernet
    559 	 * length, but we drop packets that are too large. Perhaps we should
    560 	 * truncate them instead?
    561 	 */
    562 	if (len + pad > ETHER_MAX_LEN) {
    563 		/* packet is obviously too large: toss it */
    564 		++ifp->if_oerrors;
    565 		IF_DEQUEUE(&ifp->if_snd, m0);
    566 		m_freem(m0);
    567 		goto readcheck;
    568 	}
    569 
    570 	if (bus_space_read_2(iot, ioh, EP_W1_FREE_TX) < len + pad + 4) {
    571 		bus_space_write_2(iot, ioh, EP_COMMAND,
    572 		    SET_TX_AVAIL_THRESH |
    573 		    ((len + pad + 4) >> sc->ep_pktlenshift));
    574 		/* not enough room in FIFO */
    575 		ifp->if_flags |= IFF_OACTIVE;
    576 		return;
    577 	} else {
    578 		bus_space_write_2(iot, ioh, EP_COMMAND,
    579 		    SET_TX_AVAIL_THRESH | EP_THRESH_DISABLE );
    580 	}
    581 
    582 	IF_DEQUEUE(&ifp->if_snd, m0);
    583 	if (m0 == 0)		/* not really needed */
    584 		return;
    585 
    586 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_TX_START_THRESH |
    587 	    ((len / 4 + sc->tx_start_thresh) /* >> sc->ep_pktlenshift*/) );
    588 
    589 #if NBPFILTER > 0
    590 	if (ifp->if_bpf)
    591 		bpf_mtap(ifp->if_bpf, m0);
    592 #endif
    593 
    594 	/*
    595 	 * Do the output at splhigh() so that an interrupt from another device
    596 	 * won't cause a FIFO underrun.
    597 	 */
    598 	sh = splhigh();
    599 
    600 	bus_space_write_2(iot, ioh, EP_W1_TX_PIO_WR_1, len);
    601 	bus_space_write_2(iot, ioh, EP_W1_TX_PIO_WR_1,
    602 	    0xffff);	/* Second dword meaningless */
    603 	if (EP_IS_BUS_32(sc->bustype)) {
    604 		for (m = m0; m; ) {
    605 			if (m->m_len > 3)  {
    606 				/* align our reads from core */
    607 				if (mtod(m, u_long) & 3)  {
    608 					u_long count =
    609 					    4 - (mtod(m, u_long) & 3);
    610 					bus_space_write_multi_1(iot, ioh,
    611 					    EP_W1_TX_PIO_WR_1,
    612 					    mtod(m, u_int8_t *), count);
    613 					m->m_data =
    614 					    (void *)(mtod(m, u_long) + count);
    615 					m->m_len -= count;
    616 				}
    617 				bus_space_write_multi_4(iot, ioh,
    618 				    EP_W1_TX_PIO_WR_1,
    619 				    mtod(m, u_int32_t *), m->m_len >> 2);
    620 				m->m_data = (void *)(mtod(m, u_long) +
    621 					(u_long)(m->m_len & ~3));
    622 				m->m_len -= m->m_len & ~3;
    623 			}
    624 			if (m->m_len)  {
    625 				bus_space_write_multi_1(iot, ioh,
    626 				    EP_W1_TX_PIO_WR_1,
    627 				    mtod(m, u_int8_t *), m->m_len);
    628 			}
    629 			MFREE(m, m0);
    630 			m = m0;
    631 		}
    632 	} else {
    633 		for (m = m0; m; ) {
    634 			if (m->m_len > 1)  {
    635 				if (mtod(m, u_long) & 1)  {
    636 					bus_space_write_1(iot, ioh,
    637 					    EP_W1_TX_PIO_WR_1,
    638 					    *(mtod(m, u_int8_t *)));
    639 					m->m_data =
    640 					    (void *)(mtod(m, u_long) + 1);
    641 					m->m_len -= 1;
    642 				}
    643 				bus_space_write_multi_2(iot, ioh,
    644 				    EP_W1_TX_PIO_WR_1, mtod(m, u_int16_t *),
    645 				    m->m_len >> 1);
    646 			}
    647 			if (m->m_len & 1)  {
    648 				bus_space_write_1(iot, ioh, EP_W1_TX_PIO_WR_1,
    649 				     *(mtod(m, u_int8_t *) + m->m_len - 1));
    650 			}
    651 			MFREE(m, m0);
    652 			m = m0;
    653 		}
    654 	}
    655 	while (pad--)
    656 		bus_space_write_1(iot, ioh, EP_W1_TX_PIO_WR_1, 0);
    657 
    658 	splx(sh);
    659 
    660 	++ifp->if_opackets;
    661 
    662 readcheck:
    663 	if ((bus_space_read_2(iot, ioh, EP_W1_RX_STATUS) & ERR_INCOMPLETE) == 0) {
    664 		/* We received a complete packet. */
    665 		u_int16_t status = bus_space_read_2(iot, ioh, EP_STATUS);
    666 
    667 		if ((status & S_INTR_LATCH) == 0) {
    668 			/*
    669 			 * No interrupt, read the packet and continue
    670 			 * Is  this supposed to happen? Is my motherboard
    671 			 * completely busted?
    672 			 */
    673 			epread(sc);
    674 		}
    675 		else
    676 			/* Got an interrupt, return so that it gets serviced. */
    677 			return;
    678 	}
    679 	else {
    680 		/* Check if we are stuck and reset [see XXX comment] */
    681 		if (epstatus(sc)) {
    682 			if (ifp->if_flags & IFF_DEBUG)
    683 				printf("%s: adapter reset\n",
    684 				    sc->sc_dev.dv_xname);
    685 			epreset(sc);
    686 		}
    687 	}
    688 
    689 	goto startagain;
    690 }
    691 
    692 
    693 /*
    694  * XXX: The 3c509 card can get in a mode where both the fifo status bit
    695  *	FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
    696  *	We detect this situation and we reset the adapter.
    697  *	It happens at times when there is a lot of broadcast traffic
    698  *	on the cable (once in a blue moon).
    699  */
    700 static int
    701 epstatus(sc)
    702 	register struct ep_softc *sc;
    703 {
    704 	bus_space_tag_t iot = sc->sc_iot;
    705 	bus_space_handle_t ioh = sc->sc_ioh;
    706 	u_int16_t fifost;
    707 
    708 	/*
    709 	 * Check the FIFO status and act accordingly
    710 	 */
    711 	GO_WINDOW(4);
    712 	fifost = bus_space_read_2(iot, ioh, EP_W4_FIFO_DIAG);
    713 	GO_WINDOW(1);
    714 
    715 	if (fifost & FIFOS_RX_UNDERRUN) {
    716 		if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    717 			printf("%s: RX underrun\n", sc->sc_dev.dv_xname);
    718 		epreset(sc);
    719 		return 0;
    720 	}
    721 
    722 	if (fifost & FIFOS_RX_STATUS_OVERRUN) {
    723 		if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    724 			printf("%s: RX Status overrun\n", sc->sc_dev.dv_xname);
    725 		return 1;
    726 	}
    727 
    728 	if (fifost & FIFOS_RX_OVERRUN) {
    729 		if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    730 			printf("%s: RX overrun\n", sc->sc_dev.dv_xname);
    731 		return 1;
    732 	}
    733 
    734 	if (fifost & FIFOS_TX_OVERRUN) {
    735 		if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    736 			printf("%s: TX overrun\n", sc->sc_dev.dv_xname);
    737 		epreset(sc);
    738 		return 0;
    739 	}
    740 
    741 	return 0;
    742 }
    743 
    744 
    745 static void
    746 eptxstat(sc)
    747 	register struct ep_softc *sc;
    748 {
    749 	bus_space_tag_t iot = sc->sc_iot;
    750 	bus_space_handle_t ioh = sc->sc_ioh;
    751 	int i;
    752 
    753 	/*
    754 	 * We need to read+write TX_STATUS until we get a 0 status
    755 	 * in order to turn off the interrupt flag.
    756 	 */
    757 	while ((i = bus_space_read_1(iot, ioh, EP_W1_TX_STATUS)) & TXS_COMPLETE) {
    758 		bus_space_write_1(iot, ioh, EP_W1_TX_STATUS, 0x0);
    759 
    760 		if (i & TXS_JABBER) {
    761 			++sc->sc_arpcom.ac_if.if_oerrors;
    762 			if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    763 				printf("%s: jabber (%x)\n",
    764 				       sc->sc_dev.dv_xname, i);
    765 			epreset(sc);
    766 		} else if (i & TXS_UNDERRUN) {
    767 			++sc->sc_arpcom.ac_if.if_oerrors;
    768 			if (sc->sc_arpcom.ac_if.if_flags & IFF_DEBUG)
    769 				printf("%s: fifo underrun (%x) @%d\n",
    770 				       sc->sc_dev.dv_xname, i,
    771 				       sc->tx_start_thresh);
    772 			if (sc->tx_succ_ok < 100)
    773 				    sc->tx_start_thresh = min(ETHER_MAX_LEN,
    774 					    sc->tx_start_thresh + 20);
    775 			sc->tx_succ_ok = 0;
    776 			epreset(sc);
    777 		} else if (i & TXS_MAX_COLLISION) {
    778 			++sc->sc_arpcom.ac_if.if_collisions;
    779 			bus_space_write_2(iot, ioh, EP_COMMAND, TX_ENABLE);
    780 			sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
    781 		} else
    782 			sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
    783 	}
    784 }
    785 
    786 int
    787 epintr(arg)
    788 	void *arg;
    789 {
    790 	register struct ep_softc *sc = arg;
    791 	bus_space_tag_t iot = sc->sc_iot;
    792 	bus_space_handle_t ioh = sc->sc_ioh;
    793 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    794 	u_int16_t status;
    795 	int ret = 0;
    796 
    797 	for (;;) {
    798 		bus_space_write_2(iot, ioh, EP_COMMAND, C_INTR_LATCH);
    799 
    800 		status = bus_space_read_2(iot, ioh, EP_STATUS);
    801 
    802 		if ((status & (S_TX_COMPLETE | S_TX_AVAIL |
    803 			       S_RX_COMPLETE | S_CARD_FAILURE)) == 0)
    804 			break;
    805 
    806 		ret = 1;
    807 
    808 		/*
    809 		 * Acknowledge any interrupts.  It's important that we do this
    810 		 * first, since there would otherwise be a race condition.
    811 		 * Due to the i386 interrupt queueing, we may get spurious
    812 		 * interrupts occasionally.
    813 		 */
    814 		bus_space_write_2(iot, ioh, EP_COMMAND, ACK_INTR | status);
    815 
    816 		if (status & S_RX_COMPLETE)
    817 			epread(sc);
    818 		if (status & S_TX_AVAIL) {
    819 			sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
    820 			epstart(&sc->sc_arpcom.ac_if);
    821 		}
    822 		if (status & S_CARD_FAILURE) {
    823 			printf("%s: adapter failure (%x)\n",
    824 			    sc->sc_dev.dv_xname, status);
    825 			epreset(sc);
    826 			return (1);
    827 		}
    828 		if (status & S_TX_COMPLETE) {
    829 			eptxstat(sc);
    830 			epstart(ifp);
    831 		}
    832 	}
    833 
    834 	/* no more interrupts */
    835 	return (ret);
    836 }
    837 
    838 void
    839 epread(sc)
    840 	register struct ep_softc *sc;
    841 {
    842 	bus_space_tag_t iot = sc->sc_iot;
    843 	bus_space_handle_t ioh = sc->sc_ioh;
    844 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    845 	struct mbuf *m;
    846 	struct ether_header *eh;
    847 	int len;
    848 
    849 	len = bus_space_read_2(iot, ioh, EP_W1_RX_STATUS);
    850 
    851 again:
    852 	if (ifp->if_flags & IFF_DEBUG) {
    853 		int err = len & ERR_MASK;
    854 		char *s = NULL;
    855 
    856 		if (len & ERR_INCOMPLETE)
    857 			s = "incomplete packet";
    858 		else if (err == ERR_OVERRUN)
    859 			s = "packet overrun";
    860 		else if (err == ERR_RUNT)
    861 			s = "runt packet";
    862 		else if (err == ERR_ALIGNMENT)
    863 			s = "bad alignment";
    864 		else if (err == ERR_CRC)
    865 			s = "bad crc";
    866 		else if (err == ERR_OVERSIZE)
    867 			s = "oversized packet";
    868 		else if (err == ERR_DRIBBLE)
    869 			s = "dribble bits";
    870 
    871 		if (s)
    872 			printf("%s: %s\n", sc->sc_dev.dv_xname, s);
    873 	}
    874 
    875 	if (len & ERR_INCOMPLETE)
    876 		return;
    877 
    878 	if (len & ERR_RX) {
    879 		++ifp->if_ierrors;
    880 		goto abort;
    881 	}
    882 
    883 	len &= RX_BYTES_MASK;	/* Lower 11 bits = RX bytes. */
    884 
    885 	/* Pull packet off interface. */
    886 	m = epget(sc, len);
    887 	if (m == 0) {
    888 		ifp->if_ierrors++;
    889 		goto abort;
    890 	}
    891 
    892 	++ifp->if_ipackets;
    893 
    894 	/* We assume the header fit entirely in one mbuf. */
    895 	eh = mtod(m, struct ether_header *);
    896 
    897 #if NBPFILTER > 0
    898 	/*
    899 	 * Check if there's a BPF listener on this interface.
    900 	 * If so, hand off the raw packet to BPF.
    901 	 */
    902 	if (ifp->if_bpf) {
    903 		bpf_mtap(ifp->if_bpf, m);
    904 
    905 		/*
    906 		 * Note that the interface cannot be in promiscuous mode if
    907 		 * there are no BPF listeners.  And if we are in promiscuous
    908 		 * mode, we have to check if this packet is really ours.
    909 		 */
    910 		if ((ifp->if_flags & IFF_PROMISC) &&
    911 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    912 		    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
    913 			    sizeof(eh->ether_dhost)) != 0) {
    914 			m_freem(m);
    915 			return;
    916 		}
    917 	}
    918 #endif
    919 
    920 	/* We assume the header fit entirely in one mbuf. */
    921 	m_adj(m, sizeof(struct ether_header));
    922 	ether_input(ifp, eh, m);
    923 
    924 	/*
    925 	 * In periods of high traffic we can actually receive enough
    926 	 * packets so that the fifo overrun bit will be set at this point,
    927 	 * even though we just read a packet. In this case we
    928 	 * are not going to receive any more interrupts. We check for
    929 	 * this condition and read again until the fifo is not full.
    930 	 * We could simplify this test by not using epstatus(), but
    931 	 * rechecking the RX_STATUS register directly. This test could
    932 	 * result in unnecessary looping in cases where there is a new
    933 	 * packet but the fifo is not full, but it will not fix the
    934 	 * stuck behavior.
    935 	 *
    936 	 * Even with this improvement, we still get packet overrun errors
    937 	 * which are hurting performance. Maybe when I get some more time
    938 	 * I'll modify epread() so that it can handle RX_EARLY interrupts.
    939 	 */
    940 	if (epstatus(sc)) {
    941 		len = bus_space_read_2(iot, ioh, EP_W1_RX_STATUS);
    942 		/* Check if we are stuck and reset [see XXX comment] */
    943 		if (len & ERR_INCOMPLETE) {
    944 			if (ifp->if_flags & IFF_DEBUG)
    945 				printf("%s: adapter reset\n",
    946 				    sc->sc_dev.dv_xname);
    947 			epreset(sc);
    948 			return;
    949 		}
    950 		goto again;
    951 	}
    952 
    953 	return;
    954 
    955 abort:
    956 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
    957 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
    958 		;
    959 }
    960 
    961 struct mbuf *
    962 epget(sc, totlen)
    963 	struct ep_softc *sc;
    964 	int totlen;
    965 {
    966 	bus_space_tag_t iot = sc->sc_iot;
    967 	bus_space_handle_t ioh = sc->sc_ioh;
    968 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    969 	struct mbuf *top, **mp, *m;
    970 	int len, remaining;
    971 	int sh;
    972 
    973 	m = sc->mb[sc->next_mb];
    974 	sc->mb[sc->next_mb] = 0;
    975 	if (m == 0) {
    976 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    977 		if (m == 0)
    978 			return 0;
    979 	} else {
    980 		/* If the queue is no longer full, refill. */
    981 		if (sc->last_mb == sc->next_mb)
    982 			timeout(epmbuffill, sc, 1);
    983 		/* Convert one of our saved mbuf's. */
    984 		sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
    985 		m->m_data = m->m_pktdat;
    986 		m->m_flags = M_PKTHDR;
    987 	}
    988 	m->m_pkthdr.rcvif = ifp;
    989 	m->m_pkthdr.len = totlen;
    990 	len = MHLEN;
    991 	top = 0;
    992 	mp = &top;
    993 
    994 	/*
    995 	 * We read the packet at splhigh() so that an interrupt from another
    996 	 * device doesn't cause the card's buffer to overflow while we're
    997 	 * reading it.  We may still lose packets at other times.
    998 	 */
    999 	sh = splhigh();
   1000 
   1001 	while (totlen > 0) {
   1002 		if (top) {
   1003 			m = sc->mb[sc->next_mb];
   1004 			sc->mb[sc->next_mb] = 0;
   1005 			if (m == 0) {
   1006 				MGET(m, M_DONTWAIT, MT_DATA);
   1007 				if (m == 0) {
   1008 					splx(sh);
   1009 					m_freem(top);
   1010 					return 0;
   1011 				}
   1012 			} else {
   1013 				sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
   1014 			}
   1015 			len = MLEN;
   1016 		}
   1017 		if (totlen >= MINCLSIZE) {
   1018 			MCLGET(m, M_DONTWAIT);
   1019 			if (m->m_flags & M_EXT)
   1020 				len = MCLBYTES;
   1021 		}
   1022 		if (EP_IS_BUS_32(sc->bustype) )  {
   1023 			u_long pad;
   1024 			if (top == 0)  {
   1025 			    /* align the struct ip header */
   1026 			    pad = ALIGN(sizeof(struct ether_header))
   1027 				 - sizeof(struct ether_header);
   1028 			} else {
   1029 			    /* XXX do we really need this? */
   1030 			    pad = ALIGN(m->m_data) - (u_long) m->m_data;
   1031 			}
   1032 			m->m_data += pad;
   1033 			len -= pad;
   1034 		}
   1035 		remaining = len = min(totlen, len);
   1036 		if (EP_IS_BUS_32(sc->bustype)) {
   1037 			u_long offset = mtod(m, u_long);
   1038 			/*
   1039 			 * Read bytes up to the point where we are aligned.
   1040 			 * (We can align to 4 bytes, rather than ALIGNBYTES,
   1041 			 * here because we're later reading 4-byte chunks.)
   1042 			 */
   1043 			if ((remaining > 3) && (offset & 3))  {
   1044 				int count = (4 - (offset & 3));
   1045 				bus_space_read_multi_1(iot, ioh,
   1046 				    EP_W1_RX_PIO_RD_1,
   1047 				    (u_int8_t *) offset, count);
   1048 				offset += count;
   1049 				remaining -= count;
   1050 			}
   1051 			if (remaining > 3) {
   1052 				bus_space_read_multi_4(iot, ioh,
   1053 				    EP_W1_RX_PIO_RD_1,
   1054 				    (u_int32_t *) offset, remaining >> 2);
   1055 				offset += remaining & ~3;
   1056 				remaining &= 3;
   1057 			}
   1058 			if (remaining)  {
   1059 				bus_space_read_multi_1(iot, ioh,
   1060 				    EP_W1_RX_PIO_RD_1,
   1061 				    (u_int8_t *) offset, remaining);
   1062 			}
   1063 		} else {
   1064 			u_long offset = mtod(m, u_long);
   1065 			if ((remaining > 1) && (offset & 1))  {
   1066 				bus_space_read_multi_1(iot, ioh,
   1067 				    EP_W1_RX_PIO_RD_1,
   1068 				    (u_int8_t *) offset, 1);
   1069 				remaining -= 1;
   1070 				offset += 1;
   1071 			}
   1072 			if (remaining > 1) {
   1073 				bus_space_read_multi_2(iot, ioh,
   1074 				    EP_W1_RX_PIO_RD_1,
   1075 				    (u_int16_t *) offset, remaining >> 1);
   1076 				offset += remaining & ~1;
   1077 			}
   1078 			if (remaining & 1)  {
   1079 				bus_space_read_multi_1(iot, ioh,
   1080 				    EP_W1_RX_PIO_RD_1,
   1081 				    (u_int8_t *) offset, remaining & 1);
   1082 			}
   1083 		}
   1084 		m->m_len = len;
   1085 		totlen -= len;
   1086 		*mp = m;
   1087 		mp = &m->m_next;
   1088 	}
   1089 
   1090 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
   1091 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
   1092 		;
   1093 
   1094 	splx(sh);
   1095 
   1096 	return top;
   1097 }
   1098 
   1099 int
   1100 epioctl(ifp, cmd, data)
   1101 	register struct ifnet *ifp;
   1102 	u_long cmd;
   1103 	caddr_t data;
   1104 {
   1105 	struct ep_softc *sc = ifp->if_softc;
   1106 	struct ifaddr *ifa = (struct ifaddr *)data;
   1107 	struct ifreq *ifr = (struct ifreq *)data;
   1108 	int s, error = 0;
   1109 
   1110 	s = splnet();
   1111 
   1112 	switch (cmd) {
   1113 
   1114 	case SIOCSIFADDR:
   1115 		ifp->if_flags |= IFF_UP;
   1116 
   1117 		switch (ifa->ifa_addr->sa_family) {
   1118 #ifdef INET
   1119 		case AF_INET:
   1120 			epinit(sc);
   1121 			arp_ifinit(&sc->sc_arpcom, ifa);
   1122 			break;
   1123 #endif
   1124 #ifdef NS
   1125 		case AF_NS:
   1126 		    {
   1127 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1128 
   1129 			if (ns_nullhost(*ina))
   1130 				ina->x_host =
   1131 				    *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
   1132 			else
   1133 				bcopy(ina->x_host.c_host,
   1134 				    sc->sc_arpcom.ac_enaddr,
   1135 				    sizeof(sc->sc_arpcom.ac_enaddr));
   1136 			/* Set new address. */
   1137 			epinit(sc);
   1138 			break;
   1139 		    }
   1140 #endif
   1141 		default:
   1142 			epinit(sc);
   1143 			break;
   1144 		}
   1145 		break;
   1146 
   1147 	case SIOCSIFFLAGS:
   1148 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1149 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1150 			/*
   1151 			 * If interface is marked down and it is running, then
   1152 			 * stop it.
   1153 			 */
   1154 			epstop(sc);
   1155 			ifp->if_flags &= ~IFF_RUNNING;
   1156 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1157 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1158 			/*
   1159 			 * If interface is marked up and it is stopped, then
   1160 			 * start it.
   1161 			 */
   1162 			epinit(sc);
   1163 		} else {
   1164 			/*
   1165 			 * deal with flags changes:
   1166 			 * IFF_MULTICAST, IFF_PROMISC,
   1167 			 * IFF_LINK0, IFF_LINK1,
   1168 			 */
   1169 			epsetfilter(sc);
   1170 			epsetlink(sc);
   1171 		}
   1172 		break;
   1173 
   1174 	case SIOCADDMULTI:
   1175 	case SIOCDELMULTI:
   1176 		error = (cmd == SIOCADDMULTI) ?
   1177 		    ether_addmulti(ifr, &sc->sc_arpcom) :
   1178 		    ether_delmulti(ifr, &sc->sc_arpcom);
   1179 
   1180 		if (error == ENETRESET) {
   1181 			/*
   1182 			 * Multicast list has changed; set the hardware filter
   1183 			 * accordingly.
   1184 			 */
   1185 			epreset(sc);
   1186 			error = 0;
   1187 		}
   1188 		break;
   1189 
   1190 	default:
   1191 		error = EINVAL;
   1192 		break;
   1193 	}
   1194 
   1195 	splx(s);
   1196 	return (error);
   1197 }
   1198 
   1199 void
   1200 epreset(sc)
   1201 	struct ep_softc *sc;
   1202 {
   1203 	int s;
   1204 
   1205 	s = splnet();
   1206 	epstop(sc);
   1207 	epinit(sc);
   1208 	splx(s);
   1209 }
   1210 
   1211 void
   1212 epwatchdog(ifp)
   1213 	struct ifnet *ifp;
   1214 {
   1215 	struct ep_softc *sc = ifp->if_softc;
   1216 
   1217 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1218 	++sc->sc_arpcom.ac_if.if_oerrors;
   1219 
   1220 	epreset(sc);
   1221 }
   1222 
   1223 void
   1224 epstop(sc)
   1225 	register struct ep_softc *sc;
   1226 {
   1227 	bus_space_tag_t iot = sc->sc_iot;
   1228 	bus_space_handle_t ioh = sc->sc_ioh;
   1229 
   1230 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISABLE);
   1231 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
   1232 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
   1233 		;
   1234 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_DISABLE);
   1235 	bus_space_write_2(iot, ioh, EP_COMMAND, STOP_TRANSCEIVER);
   1236 
   1237 	ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
   1238 	ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
   1239 
   1240 	bus_space_write_2(iot, ioh, EP_COMMAND, C_INTR_LATCH);
   1241 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RD_0_MASK);
   1242 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_INTR_MASK);
   1243 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RX_FILTER);
   1244 
   1245 	epmbufempty(sc);
   1246 }
   1247 
   1248 
   1249 /*
   1250  * Before reboots, reset card completely.
   1251  */
   1252 static void
   1253 epshutdown(arg)
   1254 	void *arg;
   1255 {
   1256 	register struct ep_softc *sc = arg;
   1257 
   1258 	epstop(sc);
   1259 	ep_complete_cmd(sc, EP_COMMAND, GLOBAL_RESET);
   1260 }
   1261 
   1262 
   1263 /*
   1264  * We get eeprom data from the id_port given an offset into the
   1265  * eeprom.  Basically; after the ID_sequence is sent to all of
   1266  * the cards; they enter the ID_CMD state where they will accept
   1267  * command requests. 0x80-0xbf loads the eeprom data.  We then
   1268  * read the port 16 times and with every read; the cards check
   1269  * for contention (ie: if one card writes a 0 bit and another
   1270  * writes a 1 bit then the host sees a 0. At the end of the cycle;
   1271  * each card compares the data on the bus; if there is a difference
   1272  * then that card goes into ID_WAIT state again). In the meantime;
   1273  * one bit of data is returned in the AX register which is conveniently
   1274  * returned to us by bus_space_read_1().  Hence; we read 16 times getting one
   1275  * bit of data with each read.
   1276  *
   1277  * NOTE: the caller must provide an i/o handle for ELINK_ID_PORT!
   1278  */
   1279 u_int16_t
   1280 epreadeeprom(iot, ioh, offset)
   1281 	bus_space_tag_t iot;
   1282 	bus_space_handle_t ioh;
   1283 	int offset;
   1284 {
   1285 	u_int16_t data = 0;
   1286 	int i;
   1287 
   1288 	bus_space_write_1(iot, ioh, 0, 0x80 + offset);
   1289 	delay(1000);
   1290 	for (i = 0; i < 16; i++)
   1291 		data = (data << 1) | (bus_space_read_2(iot, ioh, 0) & 1);
   1292 	return (data);
   1293 }
   1294 
   1295 static int
   1296 epbusyeeprom(sc)
   1297 	struct ep_softc *sc;
   1298 {
   1299 	bus_space_tag_t iot = sc->sc_iot;
   1300 	bus_space_handle_t ioh = sc->sc_ioh;
   1301 	int i = 100, j;
   1302 
   1303 	if (sc->bustype == EP_BUS_PCMCIA) {
   1304 		delay(1000);
   1305 		return 0;
   1306 	}
   1307 
   1308 	while (i--) {
   1309 		j = bus_space_read_2(iot, ioh, EP_W0_EEPROM_COMMAND);
   1310 		if (j & EEPROM_BUSY)
   1311 			delay(100);
   1312 		else
   1313 			break;
   1314 	}
   1315 	if (!i) {
   1316 		printf("\n%s: eeprom failed to come ready\n",
   1317 		    sc->sc_dev.dv_xname);
   1318 		return (1);
   1319 	}
   1320 	if (j & EEPROM_TST_MODE) {
   1321 		printf("\n%s: erase pencil mark, or disable plug-n-play mode!\n",
   1322 		    sc->sc_dev.dv_xname);
   1323 		return (1);
   1324 	}
   1325 	return (0);
   1326 }
   1327 
   1328 void
   1329 epmbuffill(v)
   1330 	void *v;
   1331 {
   1332 	struct ep_softc *sc = v;
   1333 	int s, i;
   1334 
   1335 	s = splnet();
   1336 	i = sc->last_mb;
   1337 	do {
   1338 		if (sc->mb[i] == NULL)
   1339 			MGET(sc->mb[i], M_DONTWAIT, MT_DATA);
   1340 		if (sc->mb[i] == NULL)
   1341 			break;
   1342 		i = (i + 1) % MAX_MBS;
   1343 	} while (i != sc->next_mb);
   1344 	sc->last_mb = i;
   1345 	/* If the queue was not filled, try again. */
   1346 	if (sc->last_mb != sc->next_mb)
   1347 		timeout(epmbuffill, sc, 1);
   1348 	splx(s);
   1349 }
   1350 
   1351 void
   1352 epmbufempty(sc)
   1353 	struct ep_softc *sc;
   1354 {
   1355 	int s, i;
   1356 
   1357 	s = splnet();
   1358 	for (i = 0; i<MAX_MBS; i++) {
   1359 		if (sc->mb[i]) {
   1360 			m_freem(sc->mb[i]);
   1361 			sc->mb[i] = NULL;
   1362 		}
   1363 	}
   1364 	sc->last_mb = sc->next_mb = 0;
   1365 	untimeout(epmbuffill, sc);
   1366 	splx(s);
   1367 }
   1368