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