if_le.c revision 1.21 1 /* $NetBSD: if_le.c,v 1.21 1995/05/24 20:49:38 gwr Exp $ */
2
3 /*
4 * LANCE Ethernet driver
5 *
6 * Copyright (c) 1995 Gordon W. Ross
7 * Copyright (c) 1994 Charles Hannum.
8 *
9 * Copyright (C) 1993, Paul Richards. This software may be used, modified,
10 * copied, distributed, and sold, in both source and binary form provided
11 * that the above copyright and these terms are retained. Under no
12 * circumstances is the author responsible for the proper functioning
13 * of this software, nor does the author assume any responsibility
14 * for damages incurred with its use.
15 */
16
17 #include "bpfilter.h"
18
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/errno.h>
22 #include <sys/ioctl.h>
23 #include <sys/mbuf.h>
24 #include <sys/socket.h>
25 #include <sys/syslog.h>
26 #include <sys/device.h>
27
28 #include <net/if.h>
29 #include <net/if_dl.h>
30 #include <net/if_types.h>
31 #include <net/netisr.h>
32
33 #ifdef INET
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/in_var.h>
37 #include <netinet/ip.h>
38 #include <netinet/if_ether.h>
39 #endif
40
41 #ifdef NS
42 #include <netns/ns.h>
43 #include <netns/ns_if.h>
44 #endif
45
46 #if NBPFILTER > 0
47 #include <net/bpf.h>
48 #include <net/bpfdesc.h>
49 #endif
50
51 #include <machine/autoconf.h>
52 #include <machine/cpu.h>
53
54 /* XXX - Yes, we DO have to deal with this bug. */
55 #define LANCE_REVC_BUG 1
56
57 /* #define LEDEBUG 1 */
58
59 #include "if_lereg.h"
60 #include "if_le.h"
61 #include "if_le_subr.h"
62
63 #define RMD_BITS "\20\20own\17err\16fram\15oflo\14crc\13rbuf\12stp\11enp"
64
65 #define ETHER_MIN_LEN 64
66 #define ETHER_MAX_LEN 1518
67
68 /*
69 * The lance has only 24 address lines. When it accesses memory,
70 * the high address lines are hard-wired to 0xFF, so we must:
71 * (1) put what we want the LANCE to see above 0xFF000000, and
72 * (2) mask our CPU addresses down to 24 bits for the LANCE.
73 */
74 #define LANCE_ADDR(sc,x) ((u_int)(x) & 0xFFffff)
75
76 #ifdef PACKETSTATS
77 long lexpacketsizes[LEMTU+1];
78 long lerpacketsizes[LEMTU+1];
79 #endif
80
81 /* autoconfiguration driver */
82 void le_attach(struct device *, struct device *, void *);
83
84 struct cfdriver lecd = {
85 NULL, "le", le_md_match, le_attach,
86 DV_IFNET, sizeof(struct le_softc),
87 };
88
89 int leioctl __P((struct ifnet *, u_long, caddr_t));
90 void lestart __P((struct ifnet *));
91 void lewatchdog __P((/* short */));
92 static inline void lewrcsr __P((/* struct le_softc *, u_short, u_short */));
93 static inline u_short lerdcsr __P((/* struct le_softc *, u_short */));
94 void leinit __P((struct le_softc *));
95 void lememinit __P((struct le_softc *));
96 void lereset __P((struct le_softc *));
97 void lestop __P((struct le_softc *));
98 void letint __P((struct le_softc *));
99 void lerint __P((struct le_softc *));
100 void leread __P((struct le_softc *, u_char *, int));
101 struct mbuf *leget __P((u_char *, int, struct ifnet *));
102 void lesetladrf __P((struct arpcom *, u_long *));
103 #ifdef LEDEBUG
104 void recv_print __P((struct le_softc *, int));
105 void xmit_print __P((struct le_softc *, int));
106 #endif
107
108 /*
109 * Inline routines to read and write the LANCE registers.
110 */
111
112 static inline void
113 lewrcsr(sc, regnum, value)
114 struct le_softc *sc;
115 u_short regnum;
116 u_short value;
117 {
118 volatile struct le_regs *regs = sc->sc_regs;
119
120 regs->lereg_addr = regnum;
121 regs->lereg_data = value;
122 }
123
124 static inline u_short
125 lerdcsr(sc, regnum)
126 struct le_softc *sc;
127 u_short regnum;
128 {
129 volatile struct le_regs *regs = sc->sc_regs;
130 u_short value;
131
132 regs->lereg_addr = regnum;
133 value = regs->lereg_data;
134
135 return (value);
136 }
137
138 /*
139 * The probe is done in if_le_subr.c:if_md_match()
140 */
141
142 /*
143 * Interface exists: make available by filling in network interface
144 * record. System will initialize the interface when it is ready
145 * to accept packets. We get the ethernet address here.
146 */
147 void
148 le_attach(parent, self, aux)
149 struct device *parent, *self;
150 void *aux;
151 {
152 struct le_softc *sc = (void *)self;
153 struct confargs *ca = aux;
154 struct ifnet *ifp = &sc->sc_if;
155 int pri;
156 u_int a;
157
158 le_md_attach(parent, self, aux);
159 printf(" hwaddr %s\n", ether_sprintf(sc->sc_enaddr));
160
161 /*
162 * Initialize and attach S/W interface
163 */
164 ifp->if_unit = sc->sc_dev.dv_unit;
165 ifp->if_name = lecd.cd_name;
166 ifp->if_start = lestart;
167 ifp->if_ioctl = leioctl;
168 ifp->if_watchdog = lewatchdog;
169 ifp->if_flags =
170 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
171 #ifndef LANCE_REVC_BUG
172 /* XXX - Must be a better way... */
173 ifp->if_flags |= IFF_MULTICAST;
174 #endif
175
176 /* Attach the interface. */
177 if_attach(ifp);
178 ether_ifattach(ifp);
179 #if NBPFILTER > 0
180 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
181 #endif
182 }
183
184 void
185 lereset(sc)
186 struct le_softc *sc;
187 {
188
189 leinit(sc);
190 }
191
192 void
193 lewatchdog(unit)
194 short unit;
195 {
196 struct le_softc *sc = lecd.cd_devs[unit];
197
198 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
199 ++sc->sc_if.if_oerrors;
200 lereset(sc);
201 }
202
203 /* LANCE initialization block set up. */
204 void
205 lememinit(sc)
206 register struct le_softc *sc;
207 {
208 struct ifnet *ifp = &sc->sc_if;
209 int i;
210 void *mem;
211 u_long a;
212
213 /*
214 * At this point we assume that the memory allocated to the Lance is
215 * quadword aligned. If it isn't then the initialisation is going
216 * fail later on.
217 */
218 mem = sc->sc_mem;
219
220 sc->sc_init = mem;
221 #if NBPFILTER > 0
222 if (ifp->if_flags & IFF_PROMISC)
223 sc->sc_init->mode = LE_NORMAL | LE_PROM;
224 else
225 #endif
226 sc->sc_init->mode = LE_NORMAL;
227
228 /* Set the Ethernet address (have to byte-swap) */
229 for (i = 0; i < 6; i += 2) {
230 sc->sc_init->padr[i] = sc->sc_enaddr[i+1];
231 sc->sc_init->padr[i+1] = sc->sc_enaddr[i];
232 }
233 lesetladrf(&sc->sc_ac, sc->sc_init->ladrf);
234 mem += sizeof(struct init_block);
235
236 sc->sc_rd = mem;
237 a = LANCE_ADDR(sc, mem);
238 sc->sc_init->rdra = a;
239 sc->sc_init->rlen = ((a >> 16) & 0xff) | (RLEN << 13);
240 mem += NRBUF * sizeof(struct mds);
241
242 sc->sc_td = mem;
243 a = LANCE_ADDR(sc, mem);
244 sc->sc_init->tdra = a;
245 sc->sc_init->tlen = ((a >> 16) & 0xff) | (TLEN << 13);
246 mem += NTBUF * sizeof(struct mds);
247
248 /*
249 * Set up receive ring descriptors.
250 */
251 sc->sc_rbuf = mem;
252 for (i = 0; i < NRBUF; i++) {
253 a = LANCE_ADDR(sc, mem);
254 sc->sc_rd[i].addr = a;
255 sc->sc_rd[i].flags = ((a >> 16) & 0xff) | LE_OWN;
256 sc->sc_rd[i].bcnt = -BUFSIZE;
257 sc->sc_rd[i].mcnt = 0;
258 mem += BUFSIZE;
259 }
260
261 /*
262 * Set up transmit ring descriptors.
263 */
264 sc->sc_tbuf = mem;
265 for (i = 0; i < NTBUF; i++) {
266 a = LANCE_ADDR(sc, mem);
267 sc->sc_td[i].addr = a;
268 sc->sc_td[i].flags= ((a >> 16) & 0xff);
269 sc->sc_td[i].bcnt = 0xf000;
270 sc->sc_td[i].mcnt = 0;
271 mem += BUFSIZE;
272 }
273
274 #ifdef DIAGNOSTIC
275 if (mem > (sc->sc_mem + MEMSIZE))
276 panic("lememinit: used 0x%x\n", mem - sc->sc_mem);
277 #endif
278 }
279
280 void
281 lestop(sc)
282 struct le_softc *sc;
283 {
284
285 lewrcsr(sc, 0, LE_STOP);
286 }
287
288 /*
289 * Initialization of interface; set up initialization block
290 * and transmit/receive descriptor rings.
291 */
292 void
293 leinit(sc)
294 register struct le_softc *sc;
295 {
296 struct ifnet *ifp = &sc->sc_if;
297 int s;
298 register int timo;
299 u_long a;
300
301 /* Address not known. */
302 if (!ifp->if_addrlist)
303 return;
304
305 s = splimp();
306
307 /* Don't want to get in a weird state. */
308 lewrcsr(sc, 0, LE_STOP);
309 delay(100);
310
311 sc->sc_last_rd = sc->sc_last_td = sc->sc_no_td = 0;
312
313 /* Set up LANCE init block. */
314 lememinit(sc);
315
316 /* Set byte swapping etc. */
317 lewrcsr(sc, 3, LE_CONF3);
318
319 /* Give LANCE the physical address of its init block. */
320 a = LANCE_ADDR(sc, sc->sc_init);
321 lewrcsr(sc, 1, a);
322 lewrcsr(sc, 2, (a >> 16) & 0xff);
323
324 /* Try to initialize the LANCE. */
325 delay(100);
326 lewrcsr(sc, 0, LE_INIT);
327
328 /* Wait for initialization to finish. */
329 for (timo = 1000; timo; timo--)
330 if (lerdcsr(sc, 0) & LE_IDON)
331 break;
332
333 if (lerdcsr(sc, 0) & LE_IDON) {
334 /* Start the LANCE. */
335 lewrcsr(sc, 0, LE_INEA | LE_STRT | LE_IDON);
336 ifp->if_flags |= IFF_RUNNING;
337 ifp->if_flags &= ~IFF_OACTIVE;
338 lestart(ifp);
339 } else
340 printf("%s: card failed to initialize\n", sc->sc_dev.dv_xname);
341
342 (void) splx(s);
343 }
344
345 /*
346 * Controller interrupt.
347 */
348 int
349 leintr(vsc)
350 void *vsc;
351 {
352 register struct le_softc *sc = vsc;
353 register u_short isr;
354
355 isr = lerdcsr(sc, 0);
356 #ifdef LEDEBUG
357 if (sc->sc_debug)
358 printf("%s: leintr entering with isr=%04x\n",
359 sc->sc_dev.dv_xname, isr);
360 #endif
361 if ((isr & LE_INTR) == 0)
362 return 0;
363
364 do {
365 lewrcsr(sc, 0,
366 isr & (LE_INEA | LE_BABL | LE_MISS | LE_MERR |
367 LE_RINT | LE_TINT | LE_IDON));
368 if (isr & (LE_BABL | LE_CERR | LE_MISS | LE_MERR)) {
369 if (isr & LE_BABL) {
370 printf("%s: babble\n", sc->sc_dev.dv_xname);
371 sc->sc_if.if_oerrors++;
372 }
373 #if 0
374 if (isr & LE_CERR) {
375 printf("%s: collision error\n", sc->sc_dev.dv_xname);
376 sc->sc_if.if_collisions++;
377 }
378 #endif
379 if (isr & LE_MISS) {
380 #if 0
381 printf("%s: missed packet\n", sc->sc_dev.dv_xname);
382 #endif
383 sc->sc_if.if_ierrors++;
384 }
385 if (isr & LE_MERR) {
386 printf("%s: memory error\n", sc->sc_dev.dv_xname);
387 lereset(sc);
388 goto out;
389 }
390 }
391
392 if ((isr & LE_RXON) == 0) {
393 printf("%s: receiver disabled\n", sc->sc_dev.dv_xname);
394 sc->sc_if.if_ierrors++;
395 lereset(sc);
396 goto out;
397 }
398 if ((isr & LE_TXON) == 0) {
399 printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname);
400 sc->sc_if.if_oerrors++;
401 lereset(sc);
402 goto out;
403 }
404
405 if (isr & LE_RINT) {
406 /* Reset watchdog timer. */
407 sc->sc_if.if_timer = 0;
408 lerint(sc);
409 }
410 if (isr & LE_TINT) {
411 /* Reset watchdog timer. */
412 sc->sc_if.if_timer = 0;
413 letint(sc);
414 }
415
416 isr = lerdcsr(sc, 0);
417 } while ((isr & LE_INTR) != 0);
418
419 #ifdef LEDEBUG
420 if (sc->sc_debug)
421 printf("%s: leintr returning with isr=%04x\n",
422 sc->sc_dev.dv_xname, isr);
423 #endif
424
425 out:
426 return 1;
427 }
428
429 #define NEXTTDS \
430 if (++tmd == NTBUF) tmd=0, cdm=sc->sc_td; else ++cdm
431
432 /*
433 * Setup output on interface.
434 * Get another datagram to send off of the interface queue, and map it to the
435 * interface before starting the output.
436 * Called only at splimp or interrupt level.
437 */
438 void
439 lestart(ifp)
440 struct ifnet *ifp;
441 {
442 register struct le_softc *sc = lecd.cd_devs[ifp->if_unit];
443 register int tmd;
444 volatile struct mds *cdm;
445 struct mbuf *m0, *m;
446 u_char *buffer;
447 int len;
448
449 if ((sc->sc_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
450 IFF_RUNNING)
451 return;
452
453 tmd = sc->sc_last_td;
454 cdm = &sc->sc_td[tmd];
455
456 for (;;) {
457 if (sc->sc_no_td >= NTBUF) {
458 sc->sc_if.if_flags |= IFF_OACTIVE;
459 #ifdef LEDEBUG
460 if (sc->sc_debug)
461 printf("no_td = %d, last_td = %d\n", sc->sc_no_td,
462 sc->sc_last_td);
463 #endif
464 break;
465 }
466
467 #ifdef LEDEBUG
468 if (cdm->flags & LE_OWN) {
469 sc->sc_if.if_flags |= IFF_OACTIVE;
470 printf("missing buffer, no_td = %d, last_td = %d\n",
471 sc->sc_no_td, sc->sc_last_td);
472 }
473 #endif
474
475 IF_DEQUEUE(&sc->sc_if.if_snd, m);
476 if (!m)
477 break;
478
479 ++sc->sc_no_td;
480
481 /*
482 * Copy the mbuf chain into the transmit buffer.
483 */
484 buffer = sc->sc_tbuf + (BUFSIZE * sc->sc_last_td);
485 len = 0;
486 for (m0 = m; m; m = m->m_next) {
487 bcopy(mtod(m, caddr_t), buffer, m->m_len);
488 buffer += m->m_len;
489 len += m->m_len;
490 }
491
492 #ifdef LEDEBUG
493 if (len > ETHER_MAX_LEN)
494 printf("packet length %d\n", len);
495 #endif
496
497 #if NBPFILTER > 0
498 if (sc->sc_if.if_bpf)
499 bpf_mtap(sc->sc_if.if_bpf, m0);
500 #endif
501
502 m_freem(m0);
503 len = max(len, ETHER_MIN_LEN);
504
505 /*
506 * Init transmit registers, and set transmit start flag.
507 */
508 cdm->bcnt = -len;
509 cdm->mcnt = 0;
510 cdm->flags |= LE_OWN | LE_STP | LE_ENP;
511
512 #ifdef LEDEBUG
513 if (sc->sc_debug)
514 xmit_print(sc, sc->sc_last_td);
515 #endif
516
517 lewrcsr(sc, 0, LE_INEA | LE_TDMD);
518
519 NEXTTDS;
520 }
521
522 sc->sc_last_td = tmd;
523 }
524
525 void
526 letint(sc)
527 struct le_softc *sc;
528 {
529 register int tmd = (sc->sc_last_td - sc->sc_no_td + NTBUF) % NTBUF;
530 volatile struct mds *cdm;
531
532 cdm = &sc->sc_td[tmd];
533 if (cdm->flags & LE_OWN) {
534 /* Race condition with loop below. */
535 #ifdef LEDEBUG
536 if (sc->sc_debug)
537 printf("%s: extra tint\n", sc->sc_dev.dv_xname);
538 #endif
539 return;
540 }
541
542 sc->sc_if.if_flags &= ~IFF_OACTIVE;
543
544 do {
545 if (sc->sc_no_td <= 0)
546 break;
547 #ifdef LEDEBUG
548 if (sc->sc_debug)
549 printf("trans cdm = %x\n", cdm);
550 #endif
551 sc->sc_if.if_opackets++;
552 --sc->sc_no_td;
553 if (cdm->mcnt & (LE_TBUFF | LE_UFLO | LE_LCOL | LE_LCAR | LE_RTRY)) {
554 if (cdm->mcnt & LE_TBUFF)
555 printf("%s: transmit buffer error\n", sc->sc_dev.dv_xname);
556 if ((cdm->mcnt & (LE_TBUFF | LE_UFLO)) == LE_UFLO)
557 printf("%s: underflow\n", sc->sc_dev.dv_xname);
558 if (cdm->mcnt & LE_UFLO) {
559 lereset(sc);
560 return;
561 }
562 #if 0
563 if (cdm->mcnt & LE_LCOL) {
564 printf("%s: late collision\n", sc->sc_dev.dv_xname);
565 sc->sc_if.if_collisions++;
566 }
567 if (cdm->mcnt & LE_LCAR)
568 printf("%s: lost carrier\n", sc->sc_dev.dv_xname);
569 if (cdm->mcnt & LE_RTRY) {
570 printf("%s: excessive collisions, tdr %d\n",
571 sc->sc_dev.dv_xname, cdm->flags & 0x1ff);
572 sc->sc_if.if_collisions += 16;
573 }
574 #endif
575 } else if (cdm->flags & LE_ONE)
576 sc->sc_if.if_collisions++;
577 else if (cdm->flags & LE_MORE)
578 /* Real number is unknown. */
579 sc->sc_if.if_collisions += 2;
580 NEXTTDS;
581 } while ((cdm->flags & LE_OWN) == 0);
582
583 lestart(&sc->sc_if);
584 }
585
586 #define NEXTRDS \
587 if (++rmd == NRBUF) rmd=0, cdm=sc->sc_rd; else ++cdm
588
589 /* only called from one place, so may as well integrate */
590 void
591 lerint(sc)
592 struct le_softc *sc;
593 {
594 register int rmd = sc->sc_last_rd;
595 volatile struct mds *cdm;
596
597 cdm = &sc->sc_rd[rmd];
598 if (cdm->flags & LE_OWN) {
599 /* Race condition with loop below. */
600 #ifdef LEDEBUG
601 if (sc->sc_debug)
602 printf("%s: extra rint\n", sc->sc_dev.dv_xname);
603 #endif
604 return;
605 }
606
607 /* Process all buffers with valid data. */
608 do {
609 if (cdm->flags & LE_ERR) {
610 #ifdef LEDEBUG
611 /*
612 * XXX - These happen a LOT on the Sun3/50 so
613 * it is really NOT appropriate to print them.
614 */
615 printf("%s: error, cdm->flags=%b\n",
616 sc->sc_dev.dv_xname, cdm->flags, RMD_BITS);
617 #endif
618 sc->sc_if.if_ierrors++;
619 } else if (cdm->flags & (LE_STP | LE_ENP) != (LE_STP | LE_ENP)) {
620 do {
621 cdm->mcnt = 0;
622 cdm->flags |= LE_OWN;
623 NEXTRDS;
624 } while ((cdm->flags & (LE_OWN | LE_ERR | LE_STP | LE_ENP)) == 0);
625 sc->sc_last_rd = rmd;
626 printf("%s: chained buffer\n", sc->sc_dev.dv_xname);
627 if ((cdm->flags & (LE_OWN | LE_ERR | LE_STP | LE_ENP)) != LE_ENP) {
628 lereset(sc);
629 return;
630 }
631 } else {
632 #ifdef LEDEBUG
633 if (sc->sc_debug)
634 recv_print(sc, sc->sc_last_rd);
635 #endif
636 leread(sc, sc->sc_rbuf + (BUFSIZE * rmd),
637 (int)cdm->mcnt);
638 sc->sc_if.if_ipackets++;
639 }
640
641 cdm->bcnt = -BUFSIZE;
642 cdm->mcnt = 0;
643 cdm->flags |= LE_OWN;
644 NEXTRDS;
645 #ifdef LEDEBUG
646 if (sc->sc_debug)
647 printf("sc->sc_last_rd = %x, cdm = %x\n",
648 sc->sc_last_rd, cdm);
649 #endif
650 } while ((cdm->flags & LE_OWN) == 0);
651
652 sc->sc_last_rd = rmd;
653 }
654
655 /*
656 * Pass a packet to the higher levels.
657 */
658 void
659 leread(sc, buf, len)
660 register struct le_softc *sc;
661 u_char *buf;
662 int len;
663 {
664 struct ifnet *ifp;
665 struct mbuf *m;
666 struct ether_header *eh;
667
668 len -= 4;
669 if (len <= 0)
670 return;
671
672 #ifdef LANCE_REVC_BUG /* XXX - Must be a better way... */
673 /*
674 * Check for unreported packet errors. Rev C of the LANCE chip
675 * has a bug which can cause "random" bytes to be prepended to
676 * the start of the packet. The work-around is to make sure that
677 * the Ethernet destination address in the packet matches our
678 * address (or the broadcast address).
679 */
680 {
681 register short *pp, *ea;
682
683 pp = (short *) buf;
684 ea = (short *) &sc->sc_enaddr;
685 if ((pp[0] == ea[0]) && (pp[1] == ea[1]) && (pp[2] == ea[2]))
686 goto ok;
687 if ((pp[0] == -1) && (pp[1] == -1) && (pp[2] == -1))
688 goto ok;
689 /* XXX - Multicast packets? */
690
691 sc->sc_if.if_ierrors++;
692 log(LOG_ERR, "%s: LANCE Rev C Extra Byte(s) bug; Packet punted\n",
693 sc->sc_dev.dv_xname);
694 return;
695 ok:
696 }
697 #endif /* LANCE_REVC_BUG */
698
699 /* Pull packet off interface. */
700 ifp = &sc->sc_if;
701 m = leget(buf, len, ifp);
702 if (m == 0)
703 return;
704
705 /* We assume that the header fit entirely in one mbuf. */
706 eh = mtod(m, struct ether_header *);
707
708 #if NBPFILTER > 0
709 /*
710 * Check if there's a BPF listener on this interface.
711 * If so, hand off the raw packet to BPF.
712 */
713 if (ifp->if_bpf) {
714 bpf_mtap(ifp->if_bpf, m);
715
716 /*
717 * Note that the interface cannot be in promiscuous mode if
718 * there are no BPF listeners. And if we are in promiscuous
719 * mode, we have to check if this packet is really ours.
720 */
721 if ((ifp->if_flags & IFF_PROMISC) &&
722 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
723 bcmp(eh->ether_dhost, sc->sc_enaddr,
724 sizeof(eh->ether_dhost)) != 0) {
725 m_freem(m);
726 return;
727 }
728 }
729 #endif
730
731 /* We assume that the header fit entirely in one mbuf. */
732 m->m_pkthdr.len -= sizeof(*eh);
733 m->m_len -= sizeof(*eh);
734 m->m_data += sizeof(*eh);
735
736 ether_input(ifp, eh, m);
737 }
738
739 /*
740 * Supporting routines
741 */
742
743 /*
744 * Pull data off an interface.
745 * Len is length of data, with local net header stripped.
746 * We copy the data into mbufs. When full cluster sized units are present
747 * we copy into clusters.
748 */
749 struct mbuf *
750 leget(buf, totlen, ifp)
751 u_char *buf;
752 int totlen;
753 struct ifnet *ifp;
754 {
755 struct mbuf *top, **mp, *m;
756 int len;
757
758 MGETHDR(m, M_DONTWAIT, MT_DATA);
759 if (m == 0)
760 return 0;
761 m->m_pkthdr.rcvif = ifp;
762 m->m_pkthdr.len = totlen;
763 len = MHLEN;
764 top = 0;
765 mp = ⊤
766
767 while (totlen > 0) {
768 if (top) {
769 MGET(m, M_DONTWAIT, MT_DATA);
770 if (m == 0) {
771 m_freem(top);
772 return 0;
773 }
774 len = MLEN;
775 }
776 if (totlen >= MINCLSIZE) {
777 MCLGET(m, M_DONTWAIT);
778 if (m->m_flags & M_EXT)
779 len = MCLBYTES;
780 }
781 m->m_len = len = min(totlen, len);
782 bcopy((caddr_t)buf, mtod(m, caddr_t), len);
783 buf += len;
784 totlen -= len;
785 *mp = m;
786 mp = &m->m_next;
787 }
788
789 return top;
790 }
791
792 /*
793 * Process an ioctl request.
794 */
795 int
796 leioctl(ifp, cmd, data)
797 register struct ifnet *ifp;
798 u_long cmd;
799 caddr_t data;
800 {
801 struct le_softc *sc = lecd.cd_devs[ifp->if_unit];
802 struct ifaddr *ifa = (struct ifaddr *)data;
803 struct ifreq *ifr = (struct ifreq *)data;
804 int s, error = 0;
805
806 s = splimp();
807
808 switch (cmd) {
809
810 case SIOCSIFADDR:
811 ifp->if_flags |= IFF_UP;
812
813 switch (ifa->ifa_addr->sa_family) {
814 #ifdef INET
815 case AF_INET:
816 leinit(sc);
817 arp_ifinit(&sc->sc_ac, ifa);
818 break;
819 #endif
820 #ifdef NS
821 /* XXX - This code is probably wrong. */
822 case AF_NS:
823 {
824 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
825
826 if (ns_nullhost(*ina))
827 ina->x_host =
828 *(union ns_host *)(sc->sc_enaddr);
829 else
830 bcopy(ina->x_host.c_host,
831 sc->sc_enaddr,
832 sizeof(sc->sc_enaddr));
833 /* Set new address. */
834 leinit(sc);
835 break;
836 }
837 #endif
838 default:
839 leinit(sc);
840 break;
841 }
842 break;
843
844 case SIOCSIFFLAGS:
845 /*
846 * If interface is marked down and it is running, then stop it
847 */
848 if ((ifp->if_flags & IFF_UP) == 0 &&
849 (ifp->if_flags & IFF_RUNNING) != 0) {
850 /*
851 * If interface is marked down and it is running, then
852 * stop it.
853 */
854 lestop(sc);
855 ifp->if_flags &= ~IFF_RUNNING;
856 } else if ((ifp->if_flags & IFF_UP) != 0 &&
857 (ifp->if_flags & IFF_RUNNING) == 0) {
858 /*
859 * If interface is marked up and it is stopped, then
860 * start it.
861 */
862 leinit(sc);
863 } else {
864 /*
865 * Reset the interface to pick up changes in any other
866 * flags that affect hardware registers.
867 */
868 /*lestop(sc);*/
869 leinit(sc);
870 }
871 #ifdef LEDEBUG
872 if (ifp->if_flags & IFF_DEBUG)
873 sc->sc_debug = 1;
874 else
875 sc->sc_debug = 0;
876 #endif
877 break;
878
879 case SIOCADDMULTI:
880 case SIOCDELMULTI:
881 error = (cmd == SIOCADDMULTI) ?
882 ether_addmulti(ifr, &sc->sc_ac):
883 ether_delmulti(ifr, &sc->sc_ac);
884
885 if (error == ENETRESET) {
886 /*
887 * Multicast list has changed; set the hardware filter
888 * accordingly.
889 */
890 leinit(sc);
891 error = 0;
892 }
893 break;
894
895 default:
896 error = EINVAL;
897 }
898 (void) splx(s);
899 return error;
900 }
901
902 #ifdef LEDEBUG
903 void
904 recv_print(sc, no)
905 struct le_softc *sc;
906 int no;
907 {
908 struct mds *rmd;
909 int i, printed = 0;
910 u_short len;
911
912 rmd = &sc->sc_rd[no];
913 len = rmd->mcnt;
914 printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
915 len);
916 printf("%s: status %x\n", sc->sc_dev.dv_xname, lerdcsr(sc, 0));
917 for (i = 0; i < len; i++) {
918 if (!printed) {
919 printed = 1;
920 printf("%s: data: ", sc->sc_dev.dv_xname);
921 }
922 printf("%x ", *(sc->sc_rbuf + (BUFSIZE*no) + i));
923 }
924 if (printed)
925 printf("\n");
926 }
927
928 void
929 xmit_print(sc, no)
930 struct le_softc *sc;
931 int no;
932 {
933 struct mds *rmd;
934 int i, printed=0;
935 u_short len;
936
937 rmd = &sc->sc_td[no];
938 len = -rmd->bcnt;
939 printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
940 len);
941 printf("%s: status %x\n", sc->sc_dev.dv_xname, lerdcsr(sc, 0));
942 printf("%s: addr %x, flags %x, bcnt %x, mcnt %x\n",
943 sc->sc_dev.dv_xname, rmd->addr, rmd->flags, rmd->bcnt, rmd->mcnt);
944 for (i = 0; i < len; i++) {
945 if (!printed) {
946 printed = 1;
947 printf("%s: data: ", sc->sc_dev.dv_xname);
948 }
949 printf("%x ", *(sc->sc_tbuf + (BUFSIZE*no) + i));
950 }
951 if (printed)
952 printf("\n");
953 }
954 #endif /* LEDEBUG */
955
956 /*
957 * Set up the logical address filter.
958 */
959 void
960 lesetladrf(ac, af)
961 struct arpcom *ac;
962 u_long *af;
963 {
964 struct ifnet *ifp = &ac->ac_if;
965 struct ether_multi *enm;
966 register u_char *cp, c;
967 register u_long crc;
968 register int i, len;
969 struct ether_multistep step;
970
971 /*
972 * Set up multicast address filter by passing all multicast addresses
973 * through a crc generator, and then using the high order 6 bits as an
974 * index into the 64 bit logical address filter. The high order bit
975 * selects the word, while the rest of the bits select the bit within
976 * the word.
977 */
978
979 if (ifp->if_flags & IFF_PROMISC) {
980 ifp->if_flags |= IFF_ALLMULTI;
981 af[0] = af[1] = 0xffffffff;
982 return;
983 }
984
985 af[0] = af[1] = 0;
986 ETHER_FIRST_MULTI(step, ac, enm);
987 while (enm != NULL) {
988 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
989 sizeof(enm->enm_addrlo)) != 0) {
990 /*
991 * We must listen to a range of multicast addresses.
992 * For now, just accept all multicasts, rather than
993 * trying to set only those filter bits needed to match
994 * the range. (At this time, the only use of address
995 * ranges is for IP multicast routing, for which the
996 * range is big enough to require all bits set.)
997 */
998 ifp->if_flags |= IFF_ALLMULTI;
999 af[0] = af[1] = 0xffffffff;
1000 return;
1001 }
1002
1003 cp = enm->enm_addrlo;
1004 crc = 0xffffffff;
1005 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1006 c = *cp++;
1007 for (i = 8; --i >= 0;) {
1008 if ((crc & 0x01) ^ (c & 0x01)) {
1009 crc >>= 1;
1010 crc ^= 0x6db88320 | 0x80000000;
1011 } else
1012 crc >>= 1;
1013 c >>= 1;
1014 }
1015 }
1016 /* Just want the 6 most significant bits. */
1017 crc >>= 26;
1018
1019 /* Turn on the corresponding bit in the filter. */
1020 af[crc >> 5] |= 1 << ((crc & 0x1f) ^ 0);
1021
1022 ETHER_NEXT_MULTI(step, enm);
1023 }
1024 ifp->if_flags &= ~IFF_ALLMULTI;
1025 }
1026