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