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