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