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