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