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