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