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