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