xref: /netbsd-src/sys/dev/isa/if_eg.c (revision 8a8f936f250a330d54f8a24ed0e92aadf9743a7b)
1 /*	$NetBSD: if_eg.c,v 1.54 2001/07/18 20:52:48 thorpej 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 	memset(pcb, 0, 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 	memcpy(myaddr, &sc->eg_pcb[2], 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 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
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 	IFQ_SET_READY(&ifp->if_snd);
484 
485 	/* Now we can attach the interface. */
486 	if_attach(ifp);
487 	ether_ifattach(ifp, myaddr);
488 
489 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
490 	    IPL_NET, egintr, sc);
491 
492 #if NRND > 0
493 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
494 			  RND_TYPE_NET, 0);
495 #endif
496 }
497 
498 void
499 eginit(sc)
500 	struct eg_softc *sc;
501 {
502 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
503 	bus_space_tag_t iot = sc->sc_iot;
504 	bus_space_handle_t ioh = sc->sc_ioh;
505 
506 	/* soft reset the board */
507 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_FLSH);
508 	delay(100);
509 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_ATTN);
510 	delay(100);
511 	bus_space_write_1(iot, ioh, EG_CONTROL, 0);
512 	delay(200);
513 
514 	sc->eg_pcb[0] = EG_CMD_CONFIG82586; /* Configure 82586 */
515 	sc->eg_pcb[1] = 2;
516 	sc->eg_pcb[2] = 3; /* receive broadcast & multicast */
517 	sc->eg_pcb[3] = 0;
518 	if (egwritePCB(iot, ioh, sc->eg_pcb) != 0)
519 		printf("%s: can't send Configure 82586\n",
520 		    sc->sc_dev.dv_xname);
521 
522 	if (egreadPCB(iot, ioh, sc->eg_pcb) != 0) {
523 		printf("%s: can't read Configure 82586 status\n",
524 		    sc->sc_dev.dv_xname);
525 		egprintpcb(sc->eg_pcb);
526 	} else if (sc->eg_pcb[2] != 0 || sc->eg_pcb[3] != 0)
527 		printf("%s: configure card command failed\n",
528 		    sc->sc_dev.dv_xname);
529 
530 	if (sc->eg_inbuf == NULL) {
531 		sc->eg_inbuf = malloc(EG_BUFLEN, M_TEMP, M_NOWAIT);
532 		if (sc->eg_inbuf == NULL) {
533 			printf("%s: can't allocate inbuf\n",
534 			    sc->sc_dev.dv_xname);
535 			panic("eginit");
536 		}
537 	}
538 	sc->eg_incount = 0;
539 
540 	if (sc->eg_outbuf == NULL) {
541 		sc->eg_outbuf = malloc(EG_BUFLEN, M_TEMP, M_NOWAIT);
542 		if (sc->eg_outbuf == NULL) {
543 			printf("%s: can't allocate outbuf\n",
544 			    sc->sc_dev.dv_xname);
545 			panic("eginit");
546 		}
547 	}
548 
549 	bus_space_write_1(iot, ioh, EG_CONTROL, EG_CTL_CMDE);
550 
551 	sc->eg_incount = 0;
552 	egrecv(sc);
553 
554 	/* Interface is now `running', with no output active. */
555 	ifp->if_flags |= IFF_RUNNING;
556 	ifp->if_flags &= ~IFF_OACTIVE;
557 
558 	/* Attempt to start output, if any. */
559 	egstart(ifp);
560 }
561 
562 void
563 egrecv(sc)
564 	struct eg_softc *sc;
565 {
566 
567 	while (sc->eg_incount < EG_INLEN) {
568 		sc->eg_pcb[0] = EG_CMD_RECVPACKET;
569 		sc->eg_pcb[1] = 0x08;
570 		sc->eg_pcb[2] = 0; /* address not used.. we send zero */
571 		sc->eg_pcb[3] = 0;
572 		sc->eg_pcb[4] = 0;
573 		sc->eg_pcb[5] = 0;
574 		sc->eg_pcb[6] = EG_BUFLEN & 0xff; /* our buffer size */
575 		sc->eg_pcb[7] = (EG_BUFLEN >> 8) & 0xff;
576 		sc->eg_pcb[8] = 0; /* timeout, 0 == none */
577 		sc->eg_pcb[9] = 0;
578 		if (egwritePCB(sc->sc_iot, sc->sc_ioh, sc->eg_pcb) != 0)
579 			break;
580 		sc->eg_incount++;
581 	}
582 }
583 
584 void
585 egstart(ifp)
586 	struct ifnet *ifp;
587 {
588 	struct eg_softc *sc = ifp->if_softc;
589 	bus_space_tag_t iot = sc->sc_iot;
590 	bus_space_handle_t ioh = sc->sc_ioh;
591 	struct mbuf *m0, *m;
592 	caddr_t buffer;
593 	int len;
594 	u_int16_t *ptr;
595 
596 	/* Don't transmit if interface is busy or not running */
597 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
598 		return;
599 
600 loop:
601 	/* Dequeue the next datagram. */
602 	IFQ_DEQUEUE(&ifp->if_snd, m0);
603 	if (m0 == 0)
604 		return;
605 
606 	ifp->if_flags |= IFF_OACTIVE;
607 
608 	/* We need to use m->m_pkthdr.len, so require the header */
609 	if ((m0->m_flags & M_PKTHDR) == 0) {
610 		printf("%s: no header mbuf\n", sc->sc_dev.dv_xname);
611 		panic("egstart");
612 	}
613 	len = max(m0->m_pkthdr.len, ETHER_MIN_LEN - ETHER_CRC_LEN);
614 
615 #if NBPFILTER > 0
616 	if (ifp->if_bpf)
617 		bpf_mtap(ifp->if_bpf, m0);
618 #endif
619 
620 	sc->eg_pcb[0] = EG_CMD_SENDPACKET;
621 	sc->eg_pcb[1] = 0x06;
622 	sc->eg_pcb[2] = 0; /* address not used, we send zero */
623 	sc->eg_pcb[3] = 0;
624 	sc->eg_pcb[4] = 0;
625 	sc->eg_pcb[5] = 0;
626 	sc->eg_pcb[6] = len; /* length of packet */
627 	sc->eg_pcb[7] = len >> 8;
628 	if (egwritePCB(iot, ioh, sc->eg_pcb) != 0) {
629 		printf("%s: can't send Send Packet command\n",
630 		    sc->sc_dev.dv_xname);
631 		ifp->if_oerrors++;
632 		ifp->if_flags &= ~IFF_OACTIVE;
633 		m_freem(m0);
634 		goto loop;
635 	}
636 
637 	buffer = sc->eg_outbuf;
638 	for (m = m0; m != 0; m = m->m_next) {
639 		memcpy(buffer, mtod(m, caddr_t), m->m_len);
640 		buffer += m->m_len;
641 	}
642 
643 	/* set direction bit: host -> adapter */
644 	bus_space_write_1(iot, ioh, EG_CONTROL,
645 	    bus_space_read_1(iot, ioh, EG_CONTROL) & ~EG_CTL_DIR);
646 
647 	for (ptr = (u_int16_t *) sc->eg_outbuf; len > 0; len -= 2) {
648 		bus_space_write_2(iot, ioh, EG_DATA, *ptr++);
649 		while (!(bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_HRDY))
650 			; /* XXX need timeout here */
651 	}
652 
653 	m_freem(m0);
654 }
655 
656 int
657 egintr(arg)
658 	void *arg;
659 {
660 	struct eg_softc *sc = arg;
661 	bus_space_tag_t iot = sc->sc_iot;
662 	bus_space_handle_t ioh = sc->sc_ioh;
663 	int i, len, serviced;
664 	u_int16_t *ptr;
665 
666 	serviced = 0;
667 
668 	while (bus_space_read_1(iot, ioh, EG_STATUS) & EG_STAT_ACRF) {
669 		egreadPCB(iot, ioh, sc->eg_pcb);
670 		switch (sc->eg_pcb[0]) {
671 		case EG_RSP_RECVPACKET:
672 			len = sc->eg_pcb[6] | (sc->eg_pcb[7] << 8);
673 
674 			/* Set direction bit : Adapter -> host */
675 			bus_space_write_1(iot, ioh, EG_CONTROL,
676 			    bus_space_read_1(iot, ioh, EG_CONTROL) | EG_CTL_DIR);
677 
678 			for (ptr = (u_int16_t *) sc->eg_inbuf;
679 			    len > 0; len -= 2) {
680 				while (!(bus_space_read_1(iot, ioh, EG_STATUS) &
681 				    EG_STAT_HRDY))
682 					;
683 				*ptr++ = bus_space_read_2(iot, ioh, EG_DATA);
684 			}
685 
686 			len = sc->eg_pcb[8] | (sc->eg_pcb[9] << 8);
687 			egread(sc, sc->eg_inbuf, len);
688 
689 			sc->eg_incount--;
690 			egrecv(sc);
691 			serviced = 1;
692 			break;
693 
694 		case EG_RSP_SENDPACKET:
695 			if (sc->eg_pcb[6] || sc->eg_pcb[7]) {
696 				DPRINTF(("%s: packet dropped\n",
697 				    sc->sc_dev.dv_xname));
698 				sc->sc_ethercom.ec_if.if_oerrors++;
699 			} else
700 				sc->sc_ethercom.ec_if.if_opackets++;
701 			sc->sc_ethercom.ec_if.if_collisions +=
702 			    sc->eg_pcb[8] & 0xf;
703 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
704 			egstart(&sc->sc_ethercom.ec_if);
705 			serviced = 1;
706 			break;
707 
708 		/* XXX byte-order and type-size bugs here... */
709 		case EG_RSP_GETSTATS:
710 			DPRINTF(("%s: Card Statistics\n",
711 			    sc->sc_dev.dv_xname));
712 			memcpy(&i, &sc->eg_pcb[2], sizeof(i));
713 			DPRINTF(("Receive Packets %d\n", i));
714 			memcpy(&i, &sc->eg_pcb[6], sizeof(i));
715 			DPRINTF(("Transmit Packets %d\n", i));
716 			DPRINTF(("CRC errors %d\n",
717 			    *(short *) &sc->eg_pcb[10]));
718 			DPRINTF(("alignment errors %d\n",
719 			    *(short *) &sc->eg_pcb[12]));
720 			DPRINTF(("no resources errors %d\n",
721 			    *(short *) &sc->eg_pcb[14]));
722 			DPRINTF(("overrun errors %d\n",
723 			    *(short *) &sc->eg_pcb[16]));
724 			serviced = 1;
725 			break;
726 
727 		default:
728 			printf("%s: egintr: Unknown response %x??\n",
729 			    sc->sc_dev.dv_xname, sc->eg_pcb[0]);
730 			egprintpcb(sc->eg_pcb);
731 			break;
732 		}
733 
734 #if NRND > 0
735 		rnd_add_uint32(&sc->rnd_source, sc->eg_pcb[0]);
736 #endif
737 	}
738 
739 	return serviced;
740 }
741 
742 /*
743  * Pass a packet up to the higher levels.
744  */
745 void
746 egread(sc, buf, len)
747 	struct eg_softc *sc;
748 	caddr_t buf;
749 	int len;
750 {
751 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
752 	struct mbuf *m;
753 
754 	if (len <= sizeof(struct ether_header) ||
755 	    len > ETHER_MAX_LEN) {
756 		printf("%s: invalid packet size %d; dropping\n",
757 		    sc->sc_dev.dv_xname, len);
758 		ifp->if_ierrors++;
759 		return;
760 	}
761 
762 	/* Pull packet off interface. */
763 	m = egget(sc, buf, len);
764 	if (m == 0) {
765 		ifp->if_ierrors++;
766 		return;
767 	}
768 
769 	ifp->if_ipackets++;
770 
771 #if NBPFILTER > 0
772 	/*
773 	 * Check if there's a BPF listener on this interface.
774 	 * If so, hand off the raw packet to BPF.
775 	 */
776 	if (ifp->if_bpf)
777 		bpf_mtap(ifp->if_bpf, m);
778 #endif
779 
780 	(*ifp->if_input)(ifp, m);
781 }
782 
783 /*
784  * convert buf into mbufs
785  */
786 struct mbuf *
787 egget(sc, buf, totlen)
788 	struct eg_softc *sc;
789 	caddr_t buf;
790 	int totlen;
791 {
792 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
793 	struct mbuf *m, *m0, *newm;
794 	int len;
795 
796 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
797 	if (m0 == 0)
798 		return (0);
799 	m0->m_pkthdr.rcvif = ifp;
800 	m0->m_pkthdr.len = totlen;
801 	len = MHLEN;
802 	m = m0;
803 
804 	while (totlen > 0) {
805 		if (totlen >= MINCLSIZE) {
806 			MCLGET(m, M_DONTWAIT);
807 			if ((m->m_flags & M_EXT) == 0)
808 				goto bad;
809 			len = MCLBYTES;
810 		}
811 
812 		m->m_len = len = min(totlen, len);
813 		memcpy(mtod(m, caddr_t), (caddr_t)buf, len);
814 		buf += len;
815 
816 		totlen -= len;
817 		if (totlen > 0) {
818 			MGET(newm, M_DONTWAIT, MT_DATA);
819 			if (newm == 0)
820 				goto bad;
821 			len = MLEN;
822 			m = m->m_next = newm;
823 		}
824 	}
825 
826 	return (m0);
827 
828 bad:
829 	m_freem(m0);
830 	return (0);
831 }
832 
833 int
834 egioctl(ifp, cmd, data)
835 	struct ifnet *ifp;
836 	u_long cmd;
837 	caddr_t data;
838 {
839 	struct eg_softc *sc = ifp->if_softc;
840 	struct ifaddr *ifa = (struct ifaddr *)data;
841 	int s, error = 0;
842 
843 	s = splnet();
844 
845 	switch (cmd) {
846 
847 	case SIOCSIFADDR:
848 		ifp->if_flags |= IFF_UP;
849 
850 		switch (ifa->ifa_addr->sa_family) {
851 #ifdef INET
852 		case AF_INET:
853 			eginit(sc);
854 			arp_ifinit(ifp, ifa);
855 			break;
856 #endif
857 #ifdef NS
858 		case AF_NS:
859 		    {
860 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
861 
862 			if (ns_nullhost(*ina))
863 				ina->x_host =
864 				   *(union ns_host *)LLADDR(ifp->if_sadl);
865 			else
866 				memcpy(LLADDR(ifp->if_sadl), ina->x_host.c_host,
867 				    ETHER_ADDR_LEN);
868 			/* Set new address. */
869 			eginit(sc);
870 			break;
871 		    }
872 #endif
873 		default:
874 			eginit(sc);
875 			break;
876 		}
877 		break;
878 
879 	case SIOCSIFFLAGS:
880 		if ((ifp->if_flags & IFF_UP) == 0 &&
881 		    (ifp->if_flags & IFF_RUNNING) != 0) {
882 			/*
883 			 * If interface is marked down and it is running, then
884 			 * stop it.
885 			 */
886 			egstop(sc);
887 			ifp->if_flags &= ~IFF_RUNNING;
888 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
889 			   (ifp->if_flags & IFF_RUNNING) == 0) {
890 			/*
891 			 * If interface is marked up and it is stopped, then
892 			 * start it.
893 			 */
894 			eginit(sc);
895 		} else {
896 			sc->eg_pcb[0] = EG_CMD_GETSTATS;
897 			sc->eg_pcb[1] = 0;
898 			if (egwritePCB(sc->sc_iot, sc->sc_ioh, sc->eg_pcb) != 0)
899 				DPRINTF(("write error\n"));
900 			/*
901 			 * XXX deal with flags changes:
902 			 * IFF_MULTICAST, IFF_PROMISC,
903 			 * IFF_LINK0, IFF_LINK1,
904 			 */
905 		}
906 		break;
907 
908 	default:
909 		error = EINVAL;
910 		break;
911 	}
912 
913 	splx(s);
914 	return error;
915 }
916 
917 void
918 egreset(sc)
919 	struct eg_softc *sc;
920 {
921 	int s;
922 
923 	DPRINTF(("%s: egreset()\n", sc->sc_dev.dv_xname));
924 	s = splnet();
925 	egstop(sc);
926 	eginit(sc);
927 	splx(s);
928 }
929 
930 void
931 egwatchdog(ifp)
932 	struct ifnet *ifp;
933 {
934 	struct eg_softc *sc = ifp->if_softc;
935 
936 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
937 	sc->sc_ethercom.ec_if.if_oerrors++;
938 
939 	egreset(sc);
940 }
941 
942 void
943 egstop(sc)
944 	struct eg_softc *sc;
945 {
946 
947 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, EG_CONTROL, 0);
948 }
949