Home | History | Annotate | Line # | Download | only in isa
if_eg.c revision 1.32
      1 /*	$NetBSD: if_eg.c,v 1.32 1996/10/21 22:40:50 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993 Dean Huxley <dean (at) fsa.ca>
      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 Dean Huxley.
     18  * 4. The name of Dean Huxley 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  * Support for 3Com 3c505 Etherlink+ card.
     34  */
     35 
     36 /* To do:
     37  * - multicast
     38  * - promiscuous
     39  * - get rid of isa indirect stuff
     40  */
     41 #include "bpfilter.h"
     42 
     43 #include <sys/types.h>
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/socket.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/errno.h>
     50 #include <sys/syslog.h>
     51 #include <sys/select.h>
     52 #include <sys/device.h>
     53 
     54 #include <net/if.h>
     55 #include <net/netisr.h>
     56 #include <net/if_dl.h>
     57 #include <net/if_types.h>
     58 #include <net/netisr.h>
     59 
     60 #ifdef INET
     61 #include <netinet/in.h>
     62 #include <netinet/in_systm.h>
     63 #include <netinet/in_var.h>
     64 #include <netinet/ip.h>
     65 #include <netinet/if_ether.h>
     66 #endif
     67 
     68 #ifdef NS
     69 #include <netns/ns.h>
     70 #include <netns/ns_if.h>
     71 #endif
     72 
     73 #if NBPFILTER > 0
     74 #include <net/bpf.h>
     75 #include <net/bpfdesc.h>
     76 #endif
     77 
     78 #include <machine/cpu.h>
     79 #include <machine/intr.h>
     80 #include <machine/bus.h>
     81 
     82 #include <dev/isa/isavar.h>
     83 #include <dev/isa/if_egreg.h>
     84 #include <dev/isa/elink.h>
     85 
     86 /* for debugging convenience */
     87 #ifdef EGDEBUG
     88 #define DPRINTF(x) printf x
     89 #else
     90 #define DPRINTF(x)
     91 #endif
     92 
     93 #define ETHER_MIN_LEN	64
     94 #define ETHER_MAX_LEN	1518
     95 #define ETHER_ADDR_LEN	6
     96 
     97 #define EG_INLEN  	10
     98 #define EG_BUFLEN	0x0670
     99 
    100 /*
    101  * Ethernet software status per interface.
    102  */
    103 struct eg_softc {
    104 	struct device sc_dev;
    105 	void *sc_ih;
    106 	struct arpcom sc_arpcom;	/* Ethernet common part */
    107 	bus_space_tag_t sc_iot;		/* bus space identifier */
    108 	bus_space_handle_t sc_ioh;	/* i/o handle */
    109 	u_int8_t eg_rom_major;		/* Cards ROM version (major number) */
    110 	u_int8_t eg_rom_minor;		/* Cards ROM version (minor number) */
    111 	short	 eg_ram;		/* Amount of RAM on the card */
    112 	u_int8_t eg_pcb[64];		/* Primary Command Block buffer */
    113 	u_int8_t eg_incount;		/* Number of buffers currently used */
    114 	caddr_t	eg_inbuf;		/* Incoming packet buffer */
    115 	caddr_t	eg_outbuf;		/* Outgoing packet buffer */
    116 };
    117 
    118 int egprobe __P((struct device *, void *, void *));
    119 void egattach __P((struct device *, struct device *, void *));
    120 
    121 struct cfattach eg_ca = {
    122 	sizeof(struct eg_softc), egprobe, egattach
    123 };
    124 
    125 struct cfdriver eg_cd = {
    126 	NULL, "eg", DV_IFNET
    127 };
    128 
    129 int egintr __P((void *));
    130 void eginit __P((struct eg_softc *));
    131 int egioctl __P((struct ifnet *, u_long, caddr_t));
    132 void egrecv __P((struct eg_softc *));
    133 void egstart __P((struct ifnet *));
    134 void egwatchdog __P((struct ifnet *));
    135 void egreset __P((struct eg_softc *));
    136 void egread __P((struct eg_softc *, caddr_t, int));
    137 struct mbuf *egget __P((struct eg_softc *, caddr_t, int));
    138 void egstop __P((struct eg_softc *));
    139 
    140 static inline void egprintpcb __P((struct eg_softc *));
    141 static inline void egprintstat __P((u_char));
    142 static int egoutPCB __P((struct eg_softc *, u_int8_t));
    143 static int egreadPCBstat __P((struct eg_softc *, u_int8_t));
    144 static int egreadPCBready __P((struct eg_softc *));
    145 static int egwritePCB __P((struct eg_softc *));
    146 static int egreadPCB __P((struct eg_softc *));
    147 
    148 /*
    149  * Support stuff
    150  */
    151 
    152 static inline void
    153 egprintpcb(sc)
    154 	struct eg_softc *sc;
    155 {
    156 	int i;
    157 
    158 	for (i = 0; i < sc->eg_pcb[1] + 2; i++)
    159 		DPRINTF(("pcb[%2d] = %x\n", i, sc->eg_pcb[i]));
    160 }
    161 
    162 
    163 static inline void
    164 egprintstat(b)
    165 	u_char b;
    166 {
    167 	DPRINTF(("%s %s %s %s %s %s %s\n",
    168 		 (b & EG_STAT_HCRE)?"HCRE":"",
    169 		 (b & EG_STAT_ACRF)?"ACRF":"",
    170 		 (b & EG_STAT_DIR )?"DIR ":"",
    171 		 (b & EG_STAT_DONE)?"DONE":"",
    172 		 (b & EG_STAT_ASF3)?"ASF3":"",
    173 		 (b & EG_STAT_ASF2)?"ASF2":"",
    174 		 (b & EG_STAT_ASF1)?"ASF1":""));
    175 }
    176 
    177 static int
    178 egoutPCB(sc, b)
    179 	struct eg_softc *sc;
    180 	u_int8_t b;
    181 {
    182 	bus_space_tag_t iot = sc->sc_iot;
    183 	bus_space_handle_t ioh = sc->sc_ioh;
    184 	int i;
    185 
    186 	for (i=0; i < 4000; i++) {
    187 		if (bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_HCRE) {
    188 			bus_space_write_1(iot, ioh, EG_COMMAND, b);
    189 			return 0;
    190 		}
    191 		delay(10);
    192 	}
    193 	DPRINTF(("egoutPCB failed\n"));
    194 	return 1;
    195 }
    196 
    197 static int
    198 egreadPCBstat(sc, statb)
    199 	struct eg_softc *sc;
    200 	u_int8_t statb;
    201 {
    202 	bus_space_tag_t iot = sc->sc_iot;
    203 	bus_space_handle_t ioh = sc->sc_ioh;
    204 	int i;
    205 
    206 	for (i=0; i < 5000; i++) {
    207 		if ((bus_space_read_1(iot, ioh, EG_STATUS) &
    208 		    EG_PCB_STAT) != EG_PCB_NULL)
    209 			break;
    210 		delay(10);
    211 	}
    212 	if ((bus_space_read_1(iot, ioh, EG_STATUS) & EG_PCB_STAT) == statb)
    213 		return 0;
    214 	return 1;
    215 }
    216 
    217 static int
    218 egreadPCBready(sc)
    219 	struct eg_softc *sc;
    220 {
    221 	bus_space_tag_t iot = sc->sc_iot;
    222 	bus_space_handle_t ioh = sc->sc_ioh;
    223 	int i;
    224 
    225 	for (i=0; i < 10000; i++) {
    226 		if (bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_ACRF)
    227 			return 0;
    228 		delay(5);
    229 	}
    230 	DPRINTF(("PCB read not ready\n"));
    231 	return 1;
    232 }
    233 
    234 static int
    235 egwritePCB(sc)
    236 	struct eg_softc *sc;
    237 {
    238 	bus_space_tag_t iot = sc->sc_iot;
    239 	bus_space_handle_t ioh = sc->sc_ioh;
    240 	int i;
    241 	u_int8_t len;
    242 
    243 	bus_space_write_1(iot, ioh, EG_CONTROL,
    244 	    (bus_space_read_1(iot, ioh, EG_CONTROL) & ~EG_PCB_STAT) | EG_PCB_NULL);
    245 
    246 	len = sc->eg_pcb[1] + 2;
    247 	for (i = 0; i < len; i++)
    248 		egoutPCB(sc, sc->eg_pcb[i]);
    249 
    250 	for (i=0; i < 4000; i++) {
    251 		if (bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_HCRE)
    252 			break;
    253 		delay(10);
    254 	}
    255 
    256 	bus_space_write_1(iot, ioh, EG_CONTROL,
    257 	    (bus_space_read_1(iot, ioh, EG_CONTROL) & ~EG_PCB_STAT) | EG_PCB_DONE);
    258 
    259 	egoutPCB(sc, len);
    260 
    261 	if (egreadPCBstat(sc, EG_PCB_ACCEPT))
    262 		return 1;
    263 	return 0;
    264 }
    265 
    266 static int
    267 egreadPCB(sc)
    268 	struct eg_softc *sc;
    269 {
    270 	bus_space_tag_t iot = sc->sc_iot;
    271 	bus_space_handle_t ioh = sc->sc_ioh;
    272 	int i;
    273 	u_int8_t b;
    274 
    275 	bus_space_write_1(iot, ioh, EG_CONTROL,
    276 	    (bus_space_read_1(iot, ioh, EG_CONTROL) & ~EG_PCB_STAT) | EG_PCB_NULL);
    277 
    278 	bzero(sc->eg_pcb, sizeof(sc->eg_pcb));
    279 
    280 	if (egreadPCBready(sc))
    281 		return 1;
    282 
    283 	sc->eg_pcb[0] = bus_space_read_1(iot, ioh, EG_COMMAND);
    284 
    285 	if (egreadPCBready(sc))
    286 		return 1;
    287 
    288 	sc->eg_pcb[1] = bus_space_read_1(iot, ioh, EG_COMMAND);
    289 
    290 	if (sc->eg_pcb[1] > 62) {
    291 		DPRINTF(("len %d too large\n", sc->eg_pcb[1]));
    292 		return 1;
    293 	}
    294 
    295 	for (i = 0; i < sc->eg_pcb[1]; i++) {
    296 		if (egreadPCBready(sc))
    297 			return 1;
    298 		sc->eg_pcb[2+i] = bus_space_read_1(iot, ioh, EG_COMMAND);
    299 	}
    300 	if (egreadPCBready(sc))
    301 		return 1;
    302 	if (egreadPCBstat(sc, EG_PCB_DONE))
    303 		return 1;
    304 	if ((b = bus_space_read_1(iot, ioh, EG_COMMAND)) != sc->eg_pcb[1] + 2) {
    305 		DPRINTF(("%d != %d\n", b, sc->eg_pcb[1] + 2));
    306 		return 1;
    307 	}
    308 
    309 	bus_space_write_1(iot, ioh, EG_CONTROL,
    310 	    (bus_space_read_1(iot, ioh, EG_CONTROL) &
    311 	    ~EG_PCB_STAT) | EG_PCB_ACCEPT);
    312 
    313 	return 0;
    314 }
    315 
    316 /*
    317  * Real stuff
    318  */
    319 
    320 int
    321 egprobe(parent, match, aux)
    322 	struct device *parent;
    323 	void *match, *aux;
    324 {
    325 	struct eg_softc *sc = match;
    326 	struct isa_attach_args *ia = aux;
    327 	bus_space_tag_t iot = ia->ia_iot;
    328 	bus_space_handle_t ioh;
    329 	int i, rval;
    330 
    331 	rval = 0;
    332 
    333 	if ((ia->ia_iobase & ~0x07f0) != 0) {
    334 		DPRINTF(("Weird iobase %x\n", ia->ia_iobase));
    335 		return 0;
    336 	}
    337 
    338 	/* Map i/o space. */
    339 	if (bus_space_map(iot, ia->ia_iobase, 0x08, 0, &ioh)) {
    340 		DPRINTF(("egprobe: can't map i/o space in probe\n"));
    341 		return 0;
    342 	}
    343 
    344 	/*
    345 	 * XXX Indirect brokenness.
    346 	 */
    347 	sc->sc_iot = iot;			/* XXX */
    348 	sc->sc_ioh = ioh;		/* XXX */
    349 
    350 	/* hard reset card */
    351 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_RESET);
    352 	bus_space_write_1(iot, ioh, EG_CONTROL, 0);
    353 	for (i = 0; i < 5000; i++) {
    354 		delay(1000);
    355 		if ((bus_space_read_1(iot, ioh, EG_STATUS) &
    356 		    EG_PCB_STAT) == EG_PCB_NULL)
    357 			break;
    358 	}
    359 	if ((bus_space_read_1(iot, ioh, EG_STATUS) & EG_PCB_STAT) != EG_PCB_NULL) {
    360 		DPRINTF(("egprobe: Reset failed\n"));
    361 		goto out;
    362 	}
    363 	sc->eg_pcb[0] = EG_CMD_GETINFO; /* Get Adapter Info */
    364 	sc->eg_pcb[1] = 0;
    365 	if (egwritePCB(sc) != 0)
    366 		goto out;
    367 
    368 	if (egreadPCB(sc) != 0) {
    369 		egprintpcb(sc);
    370 		goto out;
    371 	}
    372 
    373 	if (sc->eg_pcb[0] != EG_RSP_GETINFO || /* Get Adapter Info Response */
    374 	    sc->eg_pcb[1] != 0x0a) {
    375 		egprintpcb(sc);
    376 		goto out;
    377 	}
    378 	sc->eg_rom_major = sc->eg_pcb[3];
    379 	sc->eg_rom_minor = sc->eg_pcb[2];
    380 	sc->eg_ram = sc->eg_pcb[6] | (sc->eg_pcb[7] << 8);
    381 
    382 	ia->ia_iosize = 0x08;
    383 	ia->ia_msize = 0;
    384 	rval = 1;
    385 
    386  out:
    387 	bus_space_unmap(iot, ioh, 0x08);
    388 	return rval;
    389 }
    390 
    391 void
    392 egattach(parent, self, aux)
    393 	struct device *parent, *self;
    394 	void *aux;
    395 {
    396 	struct eg_softc *sc = (void *)self;
    397 	struct isa_attach_args *ia = aux;
    398 	bus_space_tag_t iot = ia->ia_iot;
    399 	bus_space_handle_t ioh;
    400 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    401 
    402 	printf("\n");
    403 
    404 	/* Map i/o space. */
    405 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
    406 		printf("%s: can't map i/o space\n", self->dv_xname);
    407 		return;
    408 	}
    409 
    410 	sc->sc_iot = iot;
    411 	sc->sc_ioh = ioh;
    412 
    413 	egstop(sc);
    414 
    415 	sc->eg_pcb[0] = EG_CMD_GETEADDR; /* Get Station address */
    416 	sc->eg_pcb[1] = 0;
    417 	if (egwritePCB(sc) != 0) {
    418 		printf("%s: can't send Get Station Address\n", self->dv_xname);
    419 		return;
    420 	}
    421 	if (egreadPCB(sc) != 0) {
    422 		printf("%s: can't read station address\n", self->dv_xname);
    423 		egprintpcb(sc);
    424 		return;
    425 	}
    426 
    427 	/* check Get station address response */
    428 	if (sc->eg_pcb[0] != EG_RSP_GETEADDR || sc->eg_pcb[1] != 0x06) {
    429 		printf("%s: card responded with garbage (1)\n",
    430 		    self->dv_xname);
    431 		egprintpcb(sc);
    432 		return;
    433 	}
    434 	bcopy(&sc->eg_pcb[2], sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN);
    435 
    436 	printf("%s: ROM v%d.%02d %dk address %s\n", self->dv_xname,
    437 	    sc->eg_rom_major, sc->eg_rom_minor, sc->eg_ram,
    438 	    ether_sprintf(sc->sc_arpcom.ac_enaddr));
    439 
    440 	sc->eg_pcb[0] = EG_CMD_SETEADDR; /* Set station address */
    441 	if (egwritePCB(sc) != 0) {
    442 		printf("%s: can't send Set Station Address\n", self->dv_xname);
    443 		return;
    444 	}
    445 	if (egreadPCB(sc) != 0) {
    446 		printf("%s: can't read Set Station Address status\n",
    447 		    self->dv_xname);
    448 		egprintpcb(sc);
    449 		return;
    450 	}
    451 	if (sc->eg_pcb[0] != EG_RSP_SETEADDR || sc->eg_pcb[1] != 0x02 ||
    452 	    sc->eg_pcb[2] != 0 || sc->eg_pcb[3] != 0) {
    453 		printf("%s: card responded with garbage (2)\n",
    454 		    self->dv_xname);
    455 		egprintpcb(sc);
    456 		return;
    457 	}
    458 
    459 	/* Initialize ifnet structure. */
    460 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    461 	ifp->if_softc = sc;
    462 	ifp->if_start = egstart;
    463 	ifp->if_ioctl = egioctl;
    464 	ifp->if_watchdog = egwatchdog;
    465 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    466 
    467 	/* Now we can attach the interface. */
    468 	if_attach(ifp);
    469 	ether_ifattach(ifp);
    470 
    471 #if NBPFILTER > 0
    472 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    473 #endif
    474 
    475 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    476 	    IPL_NET, egintr, sc);
    477 }
    478 
    479 void
    480 eginit(sc)
    481 	register struct eg_softc *sc;
    482 {
    483 	register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    484 	bus_space_tag_t iot = sc->sc_iot;
    485 	bus_space_handle_t ioh = sc->sc_ioh;
    486 
    487 	/* soft reset the board */
    488 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_FLSH);
    489 	delay(100);
    490 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_ATTN);
    491 	delay(100);
    492 	bus_space_write_1(iot, ioh, EG_CONTROL, 0);
    493 	delay(200);
    494 
    495 	sc->eg_pcb[0] = EG_CMD_CONFIG82586; /* Configure 82586 */
    496 	sc->eg_pcb[1] = 2;
    497 	sc->eg_pcb[2] = 3; /* receive broadcast & multicast */
    498 	sc->eg_pcb[3] = 0;
    499 	if (egwritePCB(sc) != 0)
    500 		printf("%s: can't send Configure 82586\n",
    501 		    sc->sc_dev.dv_xname);
    502 
    503 	if (egreadPCB(sc) != 0) {
    504 		printf("%s: can't read Configure 82586 status\n",
    505 		    sc->sc_dev.dv_xname);
    506 		egprintpcb(sc);
    507 	} else if (sc->eg_pcb[2] != 0 || sc->eg_pcb[3] != 0)
    508 		printf("%s: configure card command failed\n",
    509 		    sc->sc_dev.dv_xname);
    510 
    511 	if (sc->eg_inbuf == NULL) {
    512 		sc->eg_inbuf = malloc(EG_BUFLEN, M_TEMP, M_NOWAIT);
    513 		if (sc->eg_inbuf == NULL) {
    514 			printf("%s: can't allocate inbuf\n",
    515 			    sc->sc_dev.dv_xname);
    516 			panic("eginit");
    517 		}
    518 	}
    519 	sc->eg_incount = 0;
    520 
    521 	if (sc->eg_outbuf == NULL) {
    522 		sc->eg_outbuf = malloc(EG_BUFLEN, M_TEMP, M_NOWAIT);
    523 		if (sc->eg_outbuf == NULL) {
    524 			printf("%s: can't allocate outbuf\n",
    525 			    sc->sc_dev.dv_xname);
    526 			panic("eginit");
    527 		}
    528 	}
    529 
    530 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_CMDE);
    531 
    532 	sc->eg_incount = 0;
    533 	egrecv(sc);
    534 
    535 	/* Interface is now `running', with no output active. */
    536 	ifp->if_flags |= IFF_RUNNING;
    537 	ifp->if_flags &= ~IFF_OACTIVE;
    538 
    539 	/* Attempt to start output, if any. */
    540 	egstart(ifp);
    541 }
    542 
    543 void
    544 egrecv(sc)
    545 	struct eg_softc *sc;
    546 {
    547 
    548 	while (sc->eg_incount < EG_INLEN) {
    549 		sc->eg_pcb[0] = EG_CMD_RECVPACKET;
    550 		sc->eg_pcb[1] = 0x08;
    551 		sc->eg_pcb[2] = 0; /* address not used.. we send zero */
    552 		sc->eg_pcb[3] = 0;
    553 		sc->eg_pcb[4] = 0;
    554 		sc->eg_pcb[5] = 0;
    555 		sc->eg_pcb[6] = EG_BUFLEN & 0xff; /* our buffer size */
    556 		sc->eg_pcb[7] = (EG_BUFLEN >> 8) & 0xff;
    557 		sc->eg_pcb[8] = 0; /* timeout, 0 == none */
    558 		sc->eg_pcb[9] = 0;
    559 		if (egwritePCB(sc) != 0)
    560 			break;
    561 		sc->eg_incount++;
    562 	}
    563 }
    564 
    565 void
    566 egstart(ifp)
    567 	struct ifnet *ifp;
    568 {
    569 	register struct eg_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 *m0, *m;
    573 	caddr_t buffer;
    574 	int len;
    575 	u_int16_t *ptr;
    576 
    577 	/* Don't transmit if interface is busy or not running */
    578 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    579 		return;
    580 
    581 loop:
    582 	/* Dequeue the next datagram. */
    583 	IF_DEQUEUE(&ifp->if_snd, m0);
    584 	if (m0 == 0)
    585 		return;
    586 
    587 	ifp->if_flags |= IFF_OACTIVE;
    588 
    589 	/* We need to use m->m_pkthdr.len, so require the header */
    590 	if ((m0->m_flags & M_PKTHDR) == 0) {
    591 		printf("%s: no header mbuf\n", sc->sc_dev.dv_xname);
    592 		panic("egstart");
    593 	}
    594 	len = max(m0->m_pkthdr.len, ETHER_MIN_LEN);
    595 
    596 #if NBPFILTER > 0
    597 	if (ifp->if_bpf)
    598 		bpf_mtap(ifp->if_bpf, m0);
    599 #endif
    600 
    601 	sc->eg_pcb[0] = EG_CMD_SENDPACKET;
    602 	sc->eg_pcb[1] = 0x06;
    603 	sc->eg_pcb[2] = 0; /* address not used, we send zero */
    604 	sc->eg_pcb[3] = 0;
    605 	sc->eg_pcb[4] = 0;
    606 	sc->eg_pcb[5] = 0;
    607 	sc->eg_pcb[6] = len; /* length of packet */
    608 	sc->eg_pcb[7] = len >> 8;
    609 	if (egwritePCB(sc) != 0) {
    610 		printf("%s: can't send Send Packet command\n",
    611 		    sc->sc_dev.dv_xname);
    612 		ifp->if_oerrors++;
    613 		ifp->if_flags &= ~IFF_OACTIVE;
    614 		m_freem(m0);
    615 		goto loop;
    616 	}
    617 
    618 	buffer = sc->eg_outbuf;
    619 	for (m = m0; m != 0; m = m->m_next) {
    620 		bcopy(mtod(m, caddr_t), buffer, m->m_len);
    621 		buffer += m->m_len;
    622 	}
    623 
    624 	/* set direction bit: host -> adapter */
    625 	bus_space_write_1(iot, ioh, EG_CONTROL,
    626 	    bus_space_read_1(iot, ioh, EG_CONTROL) & ~EG_CTL_DIR);
    627 
    628 	for (ptr = (u_int16_t *) sc->eg_outbuf; len > 0; len -= 2) {
    629 		bus_space_write_2(iot, ioh, EG_DATA, *ptr++);
    630 		while (!(bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_HRDY))
    631 			; /* XXX need timeout here */
    632 	}
    633 
    634 	m_freem(m0);
    635 }
    636 
    637 int
    638 egintr(arg)
    639 	void *arg;
    640 {
    641 	register struct eg_softc *sc = arg;
    642 	bus_space_tag_t iot = sc->sc_iot;
    643 	bus_space_handle_t ioh = sc->sc_ioh;
    644 	int i, len, serviced;
    645 	u_int16_t *ptr;
    646 
    647 	serviced = 0;
    648 
    649 	while (bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_ACRF) {
    650 		egreadPCB(sc);
    651 		switch (sc->eg_pcb[0]) {
    652 		case EG_RSP_RECVPACKET:
    653 			len = sc->eg_pcb[6] | (sc->eg_pcb[7] << 8);
    654 
    655 			/* Set direction bit : Adapter -> host */
    656 			bus_space_write_1(iot, ioh, EG_CONTROL,
    657 			    bus_space_read_1(iot, ioh, EG_CONTROL) | EG_CTL_DIR);
    658 
    659 			for (ptr = (u_int16_t *) sc->eg_inbuf;
    660 			    len > 0; len -= 2) {
    661 				while (!(bus_space_read_1(iot, ioh, EG_STATUS) &
    662 				    EG_STAT_HRDY))
    663 					;
    664 				*ptr++ = bus_space_read_2(iot, ioh, EG_DATA);
    665 			}
    666 
    667 			len = sc->eg_pcb[8] | (sc->eg_pcb[9] << 8);
    668 			egread(sc, sc->eg_inbuf, len);
    669 
    670 			sc->eg_incount--;
    671 			egrecv(sc);
    672 			serviced = 1;
    673 			break;
    674 
    675 		case EG_RSP_SENDPACKET:
    676 			if (sc->eg_pcb[6] || sc->eg_pcb[7]) {
    677 				DPRINTF(("%s: packet dropped\n",
    678 				    sc->sc_dev.dv_xname));
    679 				sc->sc_arpcom.ac_if.if_oerrors++;
    680 			} else
    681 				sc->sc_arpcom.ac_if.if_opackets++;
    682 			sc->sc_arpcom.ac_if.if_collisions +=
    683 			    sc->eg_pcb[8] & 0xf;
    684 			sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
    685 			egstart(&sc->sc_arpcom.ac_if);
    686 			serviced = 1;
    687 			break;
    688 
    689 		/* XXX byte-order and type-size bugs here... */
    690 		case EG_RSP_GETSTATS:
    691 			DPRINTF(("%s: Card Statistics\n",
    692 			    sc->sc_dev.dv_xname));
    693 			bcopy(&sc->eg_pcb[2], &i, sizeof(i));
    694 			DPRINTF(("Receive Packets %d\n", i));
    695 			bcopy(&sc->eg_pcb[6], &i, sizeof(i));
    696 			DPRINTF(("Transmit Packets %d\n", i));
    697 			DPRINTF(("CRC errors %d\n",
    698 			    *(short *) &sc->eg_pcb[10]));
    699 			DPRINTF(("alignment errors %d\n",
    700 			    *(short *) &sc->eg_pcb[12]));
    701 			DPRINTF(("no resources errors %d\n",
    702 			    *(short *) &sc->eg_pcb[14]));
    703 			DPRINTF(("overrun errors %d\n",
    704 			    *(short *) &sc->eg_pcb[16]));
    705 			serviced = 1;
    706 			break;
    707 
    708 		default:
    709 			printf("%s: egintr: Unknown response %x??\n",
    710 			    sc->sc_dev.dv_xname, sc->eg_pcb[0]);
    711 			egprintpcb(sc);
    712 			break;
    713 		}
    714 	}
    715 
    716 	return serviced;
    717 }
    718 
    719 /*
    720  * Pass a packet up to the higher levels.
    721  */
    722 void
    723 egread(sc, buf, len)
    724 	struct eg_softc *sc;
    725 	caddr_t buf;
    726 	int len;
    727 {
    728 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    729 	struct mbuf *m;
    730 	struct ether_header *eh;
    731 
    732 	if (len <= sizeof(struct ether_header) ||
    733 	    len > ETHER_MAX_LEN) {
    734 		printf("%s: invalid packet size %d; dropping\n",
    735 		    sc->sc_dev.dv_xname, len);
    736 		ifp->if_ierrors++;
    737 		return;
    738 	}
    739 
    740 	/* Pull packet off interface. */
    741 	m = egget(sc, buf, len);
    742 	if (m == 0) {
    743 		ifp->if_ierrors++;
    744 		return;
    745 	}
    746 
    747 	ifp->if_ipackets++;
    748 
    749 	/* We assume the header fit entirely in one mbuf. */
    750 	eh = mtod(m, struct ether_header *);
    751 
    752 #if NBPFILTER > 0
    753 	/*
    754 	 * Check if there's a BPF listener on this interface.
    755 	 * If so, hand off the raw packet to BPF.
    756 	 */
    757 	if (ifp->if_bpf) {
    758 		bpf_mtap(ifp->if_bpf, m);
    759 
    760 		/*
    761 		 * Note that the interface cannot be in promiscuous mode if
    762 		 * there are no BPF listeners.  And if we are in promiscuous
    763 		 * mode, we have to check if this packet is really ours.
    764 		 */
    765 		if ((ifp->if_flags & IFF_PROMISC) &&
    766 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    767 		    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
    768 			    sizeof(eh->ether_dhost)) != 0) {
    769 			m_freem(m);
    770 			return;
    771 		}
    772 	}
    773 #endif
    774 
    775 	/* We assume the header fit entirely in one mbuf. */
    776 	m_adj(m, sizeof(struct ether_header));
    777 	ether_input(ifp, eh, m);
    778 }
    779 
    780 /*
    781  * convert buf into mbufs
    782  */
    783 struct mbuf *
    784 egget(sc, buf, totlen)
    785 	struct eg_softc *sc;
    786 	caddr_t buf;
    787 	int totlen;
    788 {
    789 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    790 	struct mbuf *top, **mp, *m;
    791 	int len;
    792 
    793 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    794 	if (m == 0)
    795 		return 0;
    796 	m->m_pkthdr.rcvif = ifp;
    797 	m->m_pkthdr.len = totlen;
    798 	len = MHLEN;
    799 	top = 0;
    800 	mp = &top;
    801 
    802 	while (totlen > 0) {
    803 		if (top) {
    804 			MGET(m, M_DONTWAIT, MT_DATA);
    805 			if (m == 0) {
    806 				m_freem(top);
    807 				return 0;
    808 			}
    809 			len = MLEN;
    810 		}
    811 		if (totlen >= MINCLSIZE) {
    812 			MCLGET(m, M_DONTWAIT);
    813 			if (m->m_flags & M_EXT)
    814 				len = MCLBYTES;
    815 		}
    816 		m->m_len = len = min(totlen, len);
    817 		bcopy((caddr_t)buf, mtod(m, caddr_t), len);
    818 		buf += len;
    819 		totlen -= len;
    820 		*mp = m;
    821 		mp = &m->m_next;
    822 	}
    823 
    824 	return top;
    825 }
    826 
    827 int
    828 egioctl(ifp, cmd, data)
    829 	register struct ifnet *ifp;
    830 	u_long cmd;
    831 	caddr_t data;
    832 {
    833 	struct eg_softc *sc = ifp->if_softc;
    834 	struct ifaddr *ifa = (struct ifaddr *)data;
    835 	int s, error = 0;
    836 
    837 	s = splnet();
    838 
    839 	switch (cmd) {
    840 
    841 	case SIOCSIFADDR:
    842 		ifp->if_flags |= IFF_UP;
    843 
    844 		switch (ifa->ifa_addr->sa_family) {
    845 #ifdef INET
    846 		case AF_INET:
    847 			eginit(sc);
    848 			arp_ifinit(&sc->sc_arpcom, ifa);
    849 			break;
    850 #endif
    851 #ifdef NS
    852 		case AF_NS:
    853 		    {
    854 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    855 
    856 			if (ns_nullhost(*ina))
    857 				ina->x_host =
    858 				   *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
    859 			else
    860 				bcopy(ina->x_host.c_host,
    861 				    sc->sc_arpcom.ac_enaddr,
    862 				    sizeof(sc->sc_arpcom.ac_enaddr));
    863 			/* Set new address. */
    864 			eginit(sc);
    865 			break;
    866 		    }
    867 #endif
    868 		default:
    869 			eginit(sc);
    870 			break;
    871 		}
    872 		break;
    873 
    874 	case SIOCSIFFLAGS:
    875 		if ((ifp->if_flags & IFF_UP) == 0 &&
    876 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    877 			/*
    878 			 * If interface is marked down and it is running, then
    879 			 * stop it.
    880 			 */
    881 			egstop(sc);
    882 			ifp->if_flags &= ~IFF_RUNNING;
    883 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    884 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    885 			/*
    886 			 * If interface is marked up and it is stopped, then
    887 			 * start it.
    888 			 */
    889 			eginit(sc);
    890 		} else {
    891 			sc->eg_pcb[0] = EG_CMD_GETSTATS;
    892 			sc->eg_pcb[1] = 0;
    893 			if (egwritePCB(sc) != 0)
    894 				DPRINTF(("write error\n"));
    895 			/*
    896 			 * XXX deal with flags changes:
    897 			 * IFF_MULTICAST, IFF_PROMISC,
    898 			 * IFF_LINK0, IFF_LINK1,
    899 			 */
    900 		}
    901 		break;
    902 
    903 	default:
    904 		error = EINVAL;
    905 		break;
    906 	}
    907 
    908 	splx(s);
    909 	return error;
    910 }
    911 
    912 void
    913 egreset(sc)
    914 	struct eg_softc *sc;
    915 {
    916 	int s;
    917 
    918 	DPRINTF(("%s: egreset()\n", sc->sc_dev.dv_xname));
    919 	s = splnet();
    920 	egstop(sc);
    921 	eginit(sc);
    922 	splx(s);
    923 }
    924 
    925 void
    926 egwatchdog(ifp)
    927 	struct ifnet *ifp;
    928 {
    929 	struct eg_softc *sc = ifp->if_softc;
    930 
    931 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    932 	sc->sc_arpcom.ac_if.if_oerrors++;
    933 
    934 	egreset(sc);
    935 }
    936 
    937 void
    938 egstop(sc)
    939 	register struct eg_softc *sc;
    940 {
    941 
    942 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, EG_CONTROL, 0);
    943 }
    944