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