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