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