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