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