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