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