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