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