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