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