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