Home | History | Annotate | Line # | Download | only in isa
if_eg.c revision 1.42
      1 /*	$NetBSD: if_eg.c,v 1.42 1998/07/05 00:51:20 jonathan 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  * - get rid of isa indirect stuff
     40  */
     41 #include "opt_inet.h"
     42 #include "bpfilter.h"
     43 #include "rnd.h"
     44 
     45 #include <sys/types.h>
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/socket.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/errno.h>
     52 #include <sys/syslog.h>
     53 #include <sys/select.h>
     54 #include <sys/device.h>
     55 #if NRND > 0
     56 #include <sys/rnd.h>
     57 #endif
     58 
     59 #include <net/if.h>
     60 #include <net/if_dl.h>
     61 #include <net/if_types.h>
     62 
     63 #include <net/if_ether.h>
     64 
     65 #ifdef INET
     66 #include <netinet/in.h>
     67 #include <netinet/in_systm.h>
     68 #include <netinet/in_var.h>
     69 #include <netinet/ip.h>
     70 #include <netinet/if_inarp.h>
     71 #endif
     72 
     73 #ifdef NS
     74 #include <netns/ns.h>
     75 #include <netns/ns_if.h>
     76 #endif
     77 
     78 #if NBPFILTER > 0
     79 #include <net/bpf.h>
     80 #include <net/bpfdesc.h>
     81 #endif
     82 
     83 #include <machine/cpu.h>
     84 #include <machine/intr.h>
     85 #include <machine/bus.h>
     86 
     87 #include <dev/isa/isavar.h>
     88 #include <dev/isa/if_egreg.h>
     89 #include <dev/isa/elink.h>
     90 
     91 /* for debugging convenience */
     92 #ifdef EGDEBUG
     93 #define DPRINTF(x) printf x
     94 #else
     95 #define DPRINTF(x)
     96 #endif
     97 
     98 #define ETHER_MIN_LEN	64
     99 #define ETHER_MAX_LEN	1518
    100 #define ETHER_ADDR_LEN	6
    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 	bzero(pcb, 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_iobase & ~0x07f0) != 0) {
    339 		DPRINTF(("Weird iobase %x\n", ia->ia_iobase));
    340 		return 0;
    341 	}
    342 
    343 	/* Disallow wildcarded i/o address. */
    344 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
    345 		return (0);
    346 
    347 	/* Map i/o space. */
    348 	if (bus_space_map(iot, ia->ia_iobase, 0x08, 0, &ioh)) {
    349 		DPRINTF(("egprobe: can't map i/o space in probe\n"));
    350 		return 0;
    351 	}
    352 
    353 	/* hard reset card */
    354 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_RESET);
    355 	bus_space_write_1(iot, ioh, EG_CONTROL, 0);
    356 	for (i = 0; i < 5000; i++) {
    357 		delay(1000);
    358 		if ((bus_space_read_1(iot, ioh, EG_STATUS) &
    359 		    EG_PCB_STAT) == EG_PCB_NULL)
    360 			break;
    361 	}
    362 	if ((bus_space_read_1(iot, ioh, EG_STATUS) & EG_PCB_STAT) != EG_PCB_NULL) {
    363 		DPRINTF(("egprobe: Reset failed\n"));
    364 		goto out;
    365 	}
    366 	pcb[0] = EG_CMD_GETINFO; /* Get Adapter Info */
    367 	pcb[1] = 0;
    368 	if (egwritePCB(iot, ioh, pcb) != 0)
    369 		goto out;
    370 
    371 	if ((egreadPCB(iot, ioh, pcb) != 0) ||
    372 	    pcb[0] != EG_RSP_GETINFO || /* Get Adapter Info Response */
    373 	    pcb[1] != 0x0a) {
    374 		egprintpcb(pcb);
    375 		goto out;
    376 	}
    377 
    378 	ia->ia_iosize = 0x08;
    379 	ia->ia_msize = 0;
    380 	rval = 1;
    381 
    382  out:
    383 	bus_space_unmap(iot, ioh, 0x08);
    384 	return rval;
    385 }
    386 
    387 void
    388 egattach(parent, self, aux)
    389 	struct device *parent, *self;
    390 	void *aux;
    391 {
    392 	struct eg_softc *sc = (void *)self;
    393 	struct isa_attach_args *ia = aux;
    394 	bus_space_tag_t iot = ia->ia_iot;
    395 	bus_space_handle_t ioh;
    396 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    397 	u_int8_t myaddr[ETHER_ADDR_LEN];
    398 
    399 	printf("\n");
    400 
    401 	/* Map i/o space. */
    402 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
    403 		printf("%s: can't map i/o space\n", self->dv_xname);
    404 		return;
    405 	}
    406 
    407 	sc->sc_iot = iot;
    408 	sc->sc_ioh = ioh;
    409 
    410 	sc->eg_pcb[0] = EG_CMD_GETINFO; /* Get Adapter Info */
    411 	sc->eg_pcb[1] = 0;
    412 	if (egwritePCB(iot, ioh, sc->eg_pcb) != 0) {
    413 		printf("%s: error requesting adapter info\n", self->dv_xname);
    414 		return;
    415 	}
    416 	if (egreadPCB(iot, ioh, sc->eg_pcb) != 0) {
    417 		egprintpcb(sc->eg_pcb);
    418 		printf("%s: error reading adapter info\n", self->dv_xname);
    419 		return;
    420 	}
    421 
    422 	if (sc->eg_pcb[0] != EG_RSP_GETINFO || /* Get Adapter Info Response */
    423 	    sc->eg_pcb[1] != 0x0a) {
    424 		egprintpcb(sc->eg_pcb);
    425 		printf("%s: bogus adapter info\n", self->dv_xname);
    426 		return;
    427 	}
    428 
    429 	sc->eg_rom_major = sc->eg_pcb[3];
    430 	sc->eg_rom_minor = sc->eg_pcb[2];
    431 	sc->eg_ram = sc->eg_pcb[6] | (sc->eg_pcb[7] << 8);
    432 
    433 	egstop(sc);
    434 
    435 	sc->eg_pcb[0] = EG_CMD_GETEADDR; /* Get Station address */
    436 	sc->eg_pcb[1] = 0;
    437 	if (egwritePCB(iot, ioh, sc->eg_pcb) != 0) {
    438 		printf("%s: can't send Get Station Address\n", self->dv_xname);
    439 		return;
    440 	}
    441 	if (egreadPCB(iot, ioh, sc->eg_pcb) != 0) {
    442 		printf("%s: can't read station address\n", self->dv_xname);
    443 		egprintpcb(sc->eg_pcb);
    444 		return;
    445 	}
    446 
    447 	/* check Get station address response */
    448 	if (sc->eg_pcb[0] != EG_RSP_GETEADDR || sc->eg_pcb[1] != 0x06) {
    449 		printf("%s: card responded with garbage (1)\n",
    450 		    self->dv_xname);
    451 		egprintpcb(sc->eg_pcb);
    452 		return;
    453 	}
    454 	bcopy(&sc->eg_pcb[2], myaddr, ETHER_ADDR_LEN);
    455 
    456 	printf("%s: ROM v%d.%02d %dk address %s\n", self->dv_xname,
    457 	    sc->eg_rom_major, sc->eg_rom_minor, sc->eg_ram,
    458 	    ether_sprintf(myaddr));
    459 
    460 	sc->eg_pcb[0] = EG_CMD_SETEADDR; /* Set station address */
    461 	if (egwritePCB(iot, ioh, sc->eg_pcb) != 0) {
    462 		printf("%s: can't send Set Station Address\n", self->dv_xname);
    463 		return;
    464 	}
    465 	if (egreadPCB(iot, ioh, sc->eg_pcb) != 0) {
    466 		printf("%s: can't read Set Station Address status\n",
    467 		    self->dv_xname);
    468 		egprintpcb(sc->eg_pcb);
    469 		return;
    470 	}
    471 	if (sc->eg_pcb[0] != EG_RSP_SETEADDR || sc->eg_pcb[1] != 0x02 ||
    472 	    sc->eg_pcb[2] != 0 || sc->eg_pcb[3] != 0) {
    473 		printf("%s: card responded with garbage (2)\n",
    474 		    self->dv_xname);
    475 		egprintpcb(sc->eg_pcb);
    476 		return;
    477 	}
    478 
    479 	/* Initialize ifnet structure. */
    480 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    481 	ifp->if_softc = sc;
    482 	ifp->if_start = egstart;
    483 	ifp->if_ioctl = egioctl;
    484 	ifp->if_watchdog = egwatchdog;
    485 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    486 
    487 	/* Now we can attach the interface. */
    488 	if_attach(ifp);
    489 	ether_ifattach(ifp, myaddr);
    490 
    491 #if NBPFILTER > 0
    492 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    493 #endif
    494 
    495 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    496 	    IPL_NET, egintr, sc);
    497 
    498 #if NRND > 0
    499 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname, RND_TYPE_NET);
    500 #endif
    501 }
    502 
    503 void
    504 eginit(sc)
    505 	register struct eg_softc *sc;
    506 {
    507 	register struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    508 	bus_space_tag_t iot = sc->sc_iot;
    509 	bus_space_handle_t ioh = sc->sc_ioh;
    510 
    511 	/* soft reset the board */
    512 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_FLSH);
    513 	delay(100);
    514 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_ATTN);
    515 	delay(100);
    516 	bus_space_write_1(iot, ioh, EG_CONTROL, 0);
    517 	delay(200);
    518 
    519 	sc->eg_pcb[0] = EG_CMD_CONFIG82586; /* Configure 82586 */
    520 	sc->eg_pcb[1] = 2;
    521 	sc->eg_pcb[2] = 3; /* receive broadcast & multicast */
    522 	sc->eg_pcb[3] = 0;
    523 	if (egwritePCB(iot, ioh, sc->eg_pcb) != 0)
    524 		printf("%s: can't send Configure 82586\n",
    525 		    sc->sc_dev.dv_xname);
    526 
    527 	if (egreadPCB(iot, ioh, sc->eg_pcb) != 0) {
    528 		printf("%s: can't read Configure 82586 status\n",
    529 		    sc->sc_dev.dv_xname);
    530 		egprintpcb(sc->eg_pcb);
    531 	} else if (sc->eg_pcb[2] != 0 || sc->eg_pcb[3] != 0)
    532 		printf("%s: configure card command failed\n",
    533 		    sc->sc_dev.dv_xname);
    534 
    535 	if (sc->eg_inbuf == NULL) {
    536 		sc->eg_inbuf = malloc(EG_BUFLEN, M_TEMP, M_NOWAIT);
    537 		if (sc->eg_inbuf == NULL) {
    538 			printf("%s: can't allocate inbuf\n",
    539 			    sc->sc_dev.dv_xname);
    540 			panic("eginit");
    541 		}
    542 	}
    543 	sc->eg_incount = 0;
    544 
    545 	if (sc->eg_outbuf == NULL) {
    546 		sc->eg_outbuf = malloc(EG_BUFLEN, M_TEMP, M_NOWAIT);
    547 		if (sc->eg_outbuf == NULL) {
    548 			printf("%s: can't allocate outbuf\n",
    549 			    sc->sc_dev.dv_xname);
    550 			panic("eginit");
    551 		}
    552 	}
    553 
    554 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_CMDE);
    555 
    556 	sc->eg_incount = 0;
    557 	egrecv(sc);
    558 
    559 	/* Interface is now `running', with no output active. */
    560 	ifp->if_flags |= IFF_RUNNING;
    561 	ifp->if_flags &= ~IFF_OACTIVE;
    562 
    563 	/* Attempt to start output, if any. */
    564 	egstart(ifp);
    565 }
    566 
    567 void
    568 egrecv(sc)
    569 	struct eg_softc *sc;
    570 {
    571 
    572 	while (sc->eg_incount < EG_INLEN) {
    573 		sc->eg_pcb[0] = EG_CMD_RECVPACKET;
    574 		sc->eg_pcb[1] = 0x08;
    575 		sc->eg_pcb[2] = 0; /* address not used.. we send zero */
    576 		sc->eg_pcb[3] = 0;
    577 		sc->eg_pcb[4] = 0;
    578 		sc->eg_pcb[5] = 0;
    579 		sc->eg_pcb[6] = EG_BUFLEN & 0xff; /* our buffer size */
    580 		sc->eg_pcb[7] = (EG_BUFLEN >> 8) & 0xff;
    581 		sc->eg_pcb[8] = 0; /* timeout, 0 == none */
    582 		sc->eg_pcb[9] = 0;
    583 		if (egwritePCB(sc->sc_iot, sc->sc_ioh, sc->eg_pcb) != 0)
    584 			break;
    585 		sc->eg_incount++;
    586 	}
    587 }
    588 
    589 void
    590 egstart(ifp)
    591 	struct ifnet *ifp;
    592 {
    593 	register struct eg_softc *sc = ifp->if_softc;
    594 	bus_space_tag_t iot = sc->sc_iot;
    595 	bus_space_handle_t ioh = sc->sc_ioh;
    596 	struct mbuf *m0, *m;
    597 	caddr_t buffer;
    598 	int len;
    599 	u_int16_t *ptr;
    600 
    601 	/* Don't transmit if interface is busy or not running */
    602 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    603 		return;
    604 
    605 loop:
    606 	/* Dequeue the next datagram. */
    607 	IF_DEQUEUE(&ifp->if_snd, m0);
    608 	if (m0 == 0)
    609 		return;
    610 
    611 	ifp->if_flags |= IFF_OACTIVE;
    612 
    613 	/* We need to use m->m_pkthdr.len, so require the header */
    614 	if ((m0->m_flags & M_PKTHDR) == 0) {
    615 		printf("%s: no header mbuf\n", sc->sc_dev.dv_xname);
    616 		panic("egstart");
    617 	}
    618 	len = max(m0->m_pkthdr.len, ETHER_MIN_LEN);
    619 
    620 #if NBPFILTER > 0
    621 	if (ifp->if_bpf)
    622 		bpf_mtap(ifp->if_bpf, m0);
    623 #endif
    624 
    625 	sc->eg_pcb[0] = EG_CMD_SENDPACKET;
    626 	sc->eg_pcb[1] = 0x06;
    627 	sc->eg_pcb[2] = 0; /* address not used, we send zero */
    628 	sc->eg_pcb[3] = 0;
    629 	sc->eg_pcb[4] = 0;
    630 	sc->eg_pcb[5] = 0;
    631 	sc->eg_pcb[6] = len; /* length of packet */
    632 	sc->eg_pcb[7] = len >> 8;
    633 	if (egwritePCB(iot, ioh, sc->eg_pcb) != 0) {
    634 		printf("%s: can't send Send Packet command\n",
    635 		    sc->sc_dev.dv_xname);
    636 		ifp->if_oerrors++;
    637 		ifp->if_flags &= ~IFF_OACTIVE;
    638 		m_freem(m0);
    639 		goto loop;
    640 	}
    641 
    642 	buffer = sc->eg_outbuf;
    643 	for (m = m0; m != 0; m = m->m_next) {
    644 		bcopy(mtod(m, caddr_t), buffer, m->m_len);
    645 		buffer += m->m_len;
    646 	}
    647 
    648 	/* set direction bit: host -> adapter */
    649 	bus_space_write_1(iot, ioh, EG_CONTROL,
    650 	    bus_space_read_1(iot, ioh, EG_CONTROL) & ~EG_CTL_DIR);
    651 
    652 	for (ptr = (u_int16_t *) sc->eg_outbuf; len > 0; len -= 2) {
    653 		bus_space_write_2(iot, ioh, EG_DATA, *ptr++);
    654 		while (!(bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_HRDY))
    655 			; /* XXX need timeout here */
    656 	}
    657 
    658 	m_freem(m0);
    659 }
    660 
    661 int
    662 egintr(arg)
    663 	void *arg;
    664 {
    665 	register struct eg_softc *sc = arg;
    666 	bus_space_tag_t iot = sc->sc_iot;
    667 	bus_space_handle_t ioh = sc->sc_ioh;
    668 	int i, len, serviced;
    669 	u_int16_t *ptr;
    670 
    671 	serviced = 0;
    672 
    673 	while (bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_ACRF) {
    674 		egreadPCB(iot, ioh, sc->eg_pcb);
    675 		switch (sc->eg_pcb[0]) {
    676 		case EG_RSP_RECVPACKET:
    677 			len = sc->eg_pcb[6] | (sc->eg_pcb[7] << 8);
    678 
    679 			/* Set direction bit : Adapter -> host */
    680 			bus_space_write_1(iot, ioh, EG_CONTROL,
    681 			    bus_space_read_1(iot, ioh, EG_CONTROL) | EG_CTL_DIR);
    682 
    683 			for (ptr = (u_int16_t *) sc->eg_inbuf;
    684 			    len > 0; len -= 2) {
    685 				while (!(bus_space_read_1(iot, ioh, EG_STATUS) &
    686 				    EG_STAT_HRDY))
    687 					;
    688 				*ptr++ = bus_space_read_2(iot, ioh, EG_DATA);
    689 			}
    690 
    691 			len = sc->eg_pcb[8] | (sc->eg_pcb[9] << 8);
    692 			egread(sc, sc->eg_inbuf, len);
    693 
    694 			sc->eg_incount--;
    695 			egrecv(sc);
    696 			serviced = 1;
    697 			break;
    698 
    699 		case EG_RSP_SENDPACKET:
    700 			if (sc->eg_pcb[6] || sc->eg_pcb[7]) {
    701 				DPRINTF(("%s: packet dropped\n",
    702 				    sc->sc_dev.dv_xname));
    703 				sc->sc_ethercom.ec_if.if_oerrors++;
    704 			} else
    705 				sc->sc_ethercom.ec_if.if_opackets++;
    706 			sc->sc_ethercom.ec_if.if_collisions +=
    707 			    sc->eg_pcb[8] & 0xf;
    708 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
    709 			egstart(&sc->sc_ethercom.ec_if);
    710 			serviced = 1;
    711 			break;
    712 
    713 		/* XXX byte-order and type-size bugs here... */
    714 		case EG_RSP_GETSTATS:
    715 			DPRINTF(("%s: Card Statistics\n",
    716 			    sc->sc_dev.dv_xname));
    717 			bcopy(&sc->eg_pcb[2], &i, sizeof(i));
    718 			DPRINTF(("Receive Packets %d\n", i));
    719 			bcopy(&sc->eg_pcb[6], &i, sizeof(i));
    720 			DPRINTF(("Transmit Packets %d\n", i));
    721 			DPRINTF(("CRC errors %d\n",
    722 			    *(short *) &sc->eg_pcb[10]));
    723 			DPRINTF(("alignment errors %d\n",
    724 			    *(short *) &sc->eg_pcb[12]));
    725 			DPRINTF(("no resources errors %d\n",
    726 			    *(short *) &sc->eg_pcb[14]));
    727 			DPRINTF(("overrun errors %d\n",
    728 			    *(short *) &sc->eg_pcb[16]));
    729 			serviced = 1;
    730 			break;
    731 
    732 		default:
    733 			printf("%s: egintr: Unknown response %x??\n",
    734 			    sc->sc_dev.dv_xname, sc->eg_pcb[0]);
    735 			egprintpcb(sc->eg_pcb);
    736 			break;
    737 		}
    738 
    739 #if NRND > 0
    740 		rnd_add_uint32(&sc->rnd_source, sc->eg_pcb[0]);
    741 #endif
    742 	}
    743 
    744 	return serviced;
    745 }
    746 
    747 /*
    748  * Pass a packet up to the higher levels.
    749  */
    750 void
    751 egread(sc, buf, len)
    752 	struct eg_softc *sc;
    753 	caddr_t buf;
    754 	int len;
    755 {
    756 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    757 	struct mbuf *m;
    758 	struct ether_header *eh;
    759 
    760 	if (len <= sizeof(struct ether_header) ||
    761 	    len > ETHER_MAX_LEN) {
    762 		printf("%s: invalid packet size %d; dropping\n",
    763 		    sc->sc_dev.dv_xname, len);
    764 		ifp->if_ierrors++;
    765 		return;
    766 	}
    767 
    768 	/* Pull packet off interface. */
    769 	m = egget(sc, buf, len);
    770 	if (m == 0) {
    771 		ifp->if_ierrors++;
    772 		return;
    773 	}
    774 
    775 	ifp->if_ipackets++;
    776 
    777 	/* We assume the header fit entirely in one mbuf. */
    778 	eh = mtod(m, struct ether_header *);
    779 
    780 #if NBPFILTER > 0
    781 	/*
    782 	 * Check if there's a BPF listener on this interface.
    783 	 * If so, hand off the raw packet to BPF.
    784 	 */
    785 	if (ifp->if_bpf) {
    786 		bpf_mtap(ifp->if_bpf, m);
    787 
    788 		/*
    789 		 * Note that the interface cannot be in promiscuous mode if
    790 		 * there are no BPF listeners.  And if we are in promiscuous
    791 		 * mode, we have to check if this packet is really ours.
    792 		 */
    793 		if ((ifp->if_flags & IFF_PROMISC) &&
    794 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    795 		    bcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
    796 			    sizeof(eh->ether_dhost)) != 0) {
    797 			m_freem(m);
    798 			return;
    799 		}
    800 	}
    801 #endif
    802 
    803 	/* We assume the header fit entirely in one mbuf. */
    804 	m_adj(m, sizeof(struct ether_header));
    805 	ether_input(ifp, eh, m);
    806 }
    807 
    808 /*
    809  * convert buf into mbufs
    810  */
    811 struct mbuf *
    812 egget(sc, buf, totlen)
    813 	struct eg_softc *sc;
    814 	caddr_t buf;
    815 	int totlen;
    816 {
    817 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    818 	struct mbuf *top, **mp, *m;
    819 	int len;
    820 
    821 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    822 	if (m == 0)
    823 		return 0;
    824 	m->m_pkthdr.rcvif = ifp;
    825 	m->m_pkthdr.len = totlen;
    826 	len = MHLEN;
    827 	top = 0;
    828 	mp = &top;
    829 
    830 	while (totlen > 0) {
    831 		if (top) {
    832 			MGET(m, M_DONTWAIT, MT_DATA);
    833 			if (m == 0) {
    834 				m_freem(top);
    835 				return 0;
    836 			}
    837 			len = MLEN;
    838 		}
    839 		if (totlen >= MINCLSIZE) {
    840 			MCLGET(m, M_DONTWAIT);
    841 			if ((m->m_flags & M_EXT) == 0) {
    842 				m_free(m);
    843 				m_freem(top);
    844 				return 0;
    845 			}
    846 			len = MCLBYTES;
    847 		}
    848 		m->m_len = len = min(totlen, len);
    849 		bcopy((caddr_t)buf, mtod(m, caddr_t), len);
    850 		buf += len;
    851 		totlen -= len;
    852 		*mp = m;
    853 		mp = &m->m_next;
    854 	}
    855 
    856 	return top;
    857 }
    858 
    859 int
    860 egioctl(ifp, cmd, data)
    861 	register struct ifnet *ifp;
    862 	u_long cmd;
    863 	caddr_t data;
    864 {
    865 	struct eg_softc *sc = ifp->if_softc;
    866 	struct ifaddr *ifa = (struct ifaddr *)data;
    867 	int s, error = 0;
    868 
    869 	s = splnet();
    870 
    871 	switch (cmd) {
    872 
    873 	case SIOCSIFADDR:
    874 		ifp->if_flags |= IFF_UP;
    875 
    876 		switch (ifa->ifa_addr->sa_family) {
    877 #ifdef INET
    878 		case AF_INET:
    879 			eginit(sc);
    880 			arp_ifinit(ifp, ifa);
    881 			break;
    882 #endif
    883 #ifdef NS
    884 		case AF_NS:
    885 		    {
    886 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    887 
    888 			if (ns_nullhost(*ina))
    889 				ina->x_host =
    890 				   *(union ns_host *)LLADDR(ifp->if_sadl);
    891 			else
    892 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
    893 				    ETHER_ADDR_LEN);
    894 			/* Set new address. */
    895 			eginit(sc);
    896 			break;
    897 		    }
    898 #endif
    899 		default:
    900 			eginit(sc);
    901 			break;
    902 		}
    903 		break;
    904 
    905 	case SIOCSIFFLAGS:
    906 		if ((ifp->if_flags & IFF_UP) == 0 &&
    907 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    908 			/*
    909 			 * If interface is marked down and it is running, then
    910 			 * stop it.
    911 			 */
    912 			egstop(sc);
    913 			ifp->if_flags &= ~IFF_RUNNING;
    914 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    915 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    916 			/*
    917 			 * If interface is marked up and it is stopped, then
    918 			 * start it.
    919 			 */
    920 			eginit(sc);
    921 		} else {
    922 			sc->eg_pcb[0] = EG_CMD_GETSTATS;
    923 			sc->eg_pcb[1] = 0;
    924 			if (egwritePCB(sc->sc_iot, sc->sc_ioh, sc->eg_pcb) != 0)
    925 				DPRINTF(("write error\n"));
    926 			/*
    927 			 * XXX deal with flags changes:
    928 			 * IFF_MULTICAST, IFF_PROMISC,
    929 			 * IFF_LINK0, IFF_LINK1,
    930 			 */
    931 		}
    932 		break;
    933 
    934 	default:
    935 		error = EINVAL;
    936 		break;
    937 	}
    938 
    939 	splx(s);
    940 	return error;
    941 }
    942 
    943 void
    944 egreset(sc)
    945 	struct eg_softc *sc;
    946 {
    947 	int s;
    948 
    949 	DPRINTF(("%s: egreset()\n", sc->sc_dev.dv_xname));
    950 	s = splnet();
    951 	egstop(sc);
    952 	eginit(sc);
    953 	splx(s);
    954 }
    955 
    956 void
    957 egwatchdog(ifp)
    958 	struct ifnet *ifp;
    959 {
    960 	struct eg_softc *sc = ifp->if_softc;
    961 
    962 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    963 	sc->sc_ethercom.ec_if.if_oerrors++;
    964 
    965 	egreset(sc);
    966 }
    967 
    968 void
    969 egstop(sc)
    970 	register struct eg_softc *sc;
    971 {
    972 
    973 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, EG_CONTROL, 0);
    974 }
    975