if_le.c revision 1.22 1 /* $NetBSD: if_le.c,v 1.22 1995/06/27 14:34:32 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 s = splimp();
302
303 /* Don't want to get in a weird state. */
304 lewrcsr(sc, 0, LE_STOP);
305 delay(100);
306
307 sc->sc_last_rd = sc->sc_last_td = sc->sc_no_td = 0;
308
309 /* Set up LANCE init block. */
310 lememinit(sc);
311
312 /* Set byte swapping etc. */
313 lewrcsr(sc, 3, LE_CONF3);
314
315 /* Give LANCE the physical address of its init block. */
316 a = LANCE_ADDR(sc, sc->sc_init);
317 lewrcsr(sc, 1, a);
318 lewrcsr(sc, 2, (a >> 16) & 0xff);
319
320 /* Try to initialize the LANCE. */
321 delay(100);
322 lewrcsr(sc, 0, LE_INIT);
323
324 /* Wait for initialization to finish. */
325 for (timo = 1000; timo; timo--)
326 if (lerdcsr(sc, 0) & LE_IDON)
327 break;
328
329 if (lerdcsr(sc, 0) & LE_IDON) {
330 /* Start the LANCE. */
331 lewrcsr(sc, 0, LE_INEA | LE_STRT | LE_IDON);
332 ifp->if_flags |= IFF_RUNNING;
333 ifp->if_flags &= ~IFF_OACTIVE;
334 lestart(ifp);
335 } else
336 printf("%s: card failed to initialize\n", sc->sc_dev.dv_xname);
337
338 (void) splx(s);
339 }
340
341 /*
342 * Controller interrupt.
343 */
344 int
345 leintr(vsc)
346 void *vsc;
347 {
348 register struct le_softc *sc = vsc;
349 register u_short isr;
350
351 isr = lerdcsr(sc, 0);
352 #ifdef LEDEBUG
353 if (sc->sc_debug)
354 printf("%s: leintr entering with isr=%04x\n",
355 sc->sc_dev.dv_xname, isr);
356 #endif
357 if ((isr & LE_INTR) == 0)
358 return 0;
359
360 do {
361 lewrcsr(sc, 0,
362 isr & (LE_INEA | LE_BABL | LE_MISS | LE_MERR |
363 LE_RINT | LE_TINT | LE_IDON));
364 if (isr & (LE_BABL | LE_CERR | LE_MISS | LE_MERR)) {
365 if (isr & LE_BABL) {
366 printf("%s: babble\n", sc->sc_dev.dv_xname);
367 sc->sc_if.if_oerrors++;
368 }
369 #if 0
370 if (isr & LE_CERR) {
371 printf("%s: collision error\n", sc->sc_dev.dv_xname);
372 sc->sc_if.if_collisions++;
373 }
374 #endif
375 if (isr & LE_MISS) {
376 #if 0
377 printf("%s: missed packet\n", sc->sc_dev.dv_xname);
378 #endif
379 sc->sc_if.if_ierrors++;
380 }
381 if (isr & LE_MERR) {
382 printf("%s: memory error\n", sc->sc_dev.dv_xname);
383 lereset(sc);
384 goto out;
385 }
386 }
387
388 if ((isr & LE_RXON) == 0) {
389 printf("%s: receiver disabled\n", sc->sc_dev.dv_xname);
390 sc->sc_if.if_ierrors++;
391 lereset(sc);
392 goto out;
393 }
394 if ((isr & LE_TXON) == 0) {
395 printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname);
396 sc->sc_if.if_oerrors++;
397 lereset(sc);
398 goto out;
399 }
400
401 if (isr & LE_RINT) {
402 /* Reset watchdog timer. */
403 sc->sc_if.if_timer = 0;
404 lerint(sc);
405 }
406 if (isr & LE_TINT) {
407 /* Reset watchdog timer. */
408 sc->sc_if.if_timer = 0;
409 letint(sc);
410 }
411
412 isr = lerdcsr(sc, 0);
413 } while ((isr & LE_INTR) != 0);
414
415 #ifdef LEDEBUG
416 if (sc->sc_debug)
417 printf("%s: leintr returning with isr=%04x\n",
418 sc->sc_dev.dv_xname, isr);
419 #endif
420
421 out:
422 return 1;
423 }
424
425 #define NEXTTDS \
426 if (++tmd == NTBUF) tmd=0, cdm=sc->sc_td; else ++cdm
427
428 /*
429 * Setup output on interface.
430 * Get another datagram to send off of the interface queue, and map it to the
431 * interface before starting the output.
432 * Called only at splimp or interrupt level.
433 */
434 void
435 lestart(ifp)
436 struct ifnet *ifp;
437 {
438 register struct le_softc *sc = lecd.cd_devs[ifp->if_unit];
439 register int tmd;
440 volatile struct mds *cdm;
441 struct mbuf *m0, *m;
442 u_char *buffer;
443 int len;
444
445 if ((sc->sc_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
446 IFF_RUNNING)
447 return;
448
449 tmd = sc->sc_last_td;
450 cdm = &sc->sc_td[tmd];
451
452 for (;;) {
453 if (sc->sc_no_td >= NTBUF) {
454 sc->sc_if.if_flags |= IFF_OACTIVE;
455 #ifdef LEDEBUG
456 if (sc->sc_debug)
457 printf("no_td = %d, last_td = %d\n", sc->sc_no_td,
458 sc->sc_last_td);
459 #endif
460 break;
461 }
462
463 #ifdef LEDEBUG
464 if (cdm->flags & LE_OWN) {
465 sc->sc_if.if_flags |= IFF_OACTIVE;
466 printf("missing buffer, no_td = %d, last_td = %d\n",
467 sc->sc_no_td, sc->sc_last_td);
468 }
469 #endif
470
471 IF_DEQUEUE(&sc->sc_if.if_snd, m);
472 if (!m)
473 break;
474
475 ++sc->sc_no_td;
476
477 /*
478 * Copy the mbuf chain into the transmit buffer.
479 */
480 buffer = sc->sc_tbuf + (BUFSIZE * sc->sc_last_td);
481 len = 0;
482 for (m0 = m; m; m = m->m_next) {
483 bcopy(mtod(m, caddr_t), buffer, m->m_len);
484 buffer += m->m_len;
485 len += m->m_len;
486 }
487
488 #ifdef LEDEBUG
489 if (len > ETHER_MAX_LEN)
490 printf("packet length %d\n", len);
491 #endif
492
493 #if NBPFILTER > 0
494 if (sc->sc_if.if_bpf)
495 bpf_mtap(sc->sc_if.if_bpf, m0);
496 #endif
497
498 m_freem(m0);
499 len = max(len, ETHER_MIN_LEN);
500
501 /*
502 * Init transmit registers, and set transmit start flag.
503 */
504 cdm->bcnt = -len;
505 cdm->mcnt = 0;
506 cdm->flags |= LE_OWN | LE_STP | LE_ENP;
507
508 #ifdef LEDEBUG
509 if (sc->sc_debug)
510 xmit_print(sc, sc->sc_last_td);
511 #endif
512
513 lewrcsr(sc, 0, LE_INEA | LE_TDMD);
514
515 NEXTTDS;
516 }
517
518 sc->sc_last_td = tmd;
519 }
520
521 void
522 letint(sc)
523 struct le_softc *sc;
524 {
525 register int tmd = (sc->sc_last_td - sc->sc_no_td + NTBUF) % NTBUF;
526 volatile struct mds *cdm;
527
528 cdm = &sc->sc_td[tmd];
529 if (cdm->flags & LE_OWN) {
530 /* Race condition with loop below. */
531 #ifdef LEDEBUG
532 if (sc->sc_debug)
533 printf("%s: extra tint\n", sc->sc_dev.dv_xname);
534 #endif
535 return;
536 }
537
538 sc->sc_if.if_flags &= ~IFF_OACTIVE;
539
540 do {
541 if (sc->sc_no_td <= 0)
542 break;
543 #ifdef LEDEBUG
544 if (sc->sc_debug)
545 printf("trans cdm = %x\n", cdm);
546 #endif
547 sc->sc_if.if_opackets++;
548 --sc->sc_no_td;
549 if (cdm->mcnt & (LE_TBUFF | LE_UFLO | LE_LCOL | LE_LCAR | LE_RTRY)) {
550 if (cdm->mcnt & LE_TBUFF)
551 printf("%s: transmit buffer error\n", sc->sc_dev.dv_xname);
552 if ((cdm->mcnt & (LE_TBUFF | LE_UFLO)) == LE_UFLO)
553 printf("%s: underflow\n", sc->sc_dev.dv_xname);
554 if (cdm->mcnt & LE_UFLO) {
555 lereset(sc);
556 return;
557 }
558 #if 0
559 if (cdm->mcnt & LE_LCOL) {
560 printf("%s: late collision\n", sc->sc_dev.dv_xname);
561 sc->sc_if.if_collisions++;
562 }
563 if (cdm->mcnt & LE_LCAR)
564 printf("%s: lost carrier\n", sc->sc_dev.dv_xname);
565 if (cdm->mcnt & LE_RTRY) {
566 printf("%s: excessive collisions, tdr %d\n",
567 sc->sc_dev.dv_xname, cdm->flags & 0x1ff);
568 sc->sc_if.if_collisions += 16;
569 }
570 #endif
571 } else if (cdm->flags & LE_ONE)
572 sc->sc_if.if_collisions++;
573 else if (cdm->flags & LE_MORE)
574 /* Real number is unknown. */
575 sc->sc_if.if_collisions += 2;
576 NEXTTDS;
577 } while ((cdm->flags & LE_OWN) == 0);
578
579 lestart(&sc->sc_if);
580 }
581
582 #define NEXTRDS \
583 if (++rmd == NRBUF) rmd=0, cdm=sc->sc_rd; else ++cdm
584
585 /* only called from one place, so may as well integrate */
586 void
587 lerint(sc)
588 struct le_softc *sc;
589 {
590 register int rmd = sc->sc_last_rd;
591 volatile struct mds *cdm;
592
593 cdm = &sc->sc_rd[rmd];
594 if (cdm->flags & LE_OWN) {
595 /* Race condition with loop below. */
596 #ifdef LEDEBUG
597 if (sc->sc_debug)
598 printf("%s: extra rint\n", sc->sc_dev.dv_xname);
599 #endif
600 return;
601 }
602
603 /* Process all buffers with valid data. */
604 do {
605 if (cdm->flags & LE_ERR) {
606 #ifdef LEDEBUG
607 /*
608 * XXX - These happen a LOT on the Sun3/50 so
609 * it is really NOT appropriate to print them.
610 */
611 printf("%s: error, cdm->flags=%b\n",
612 sc->sc_dev.dv_xname, cdm->flags, RMD_BITS);
613 #endif
614 sc->sc_if.if_ierrors++;
615 } else if (cdm->flags & (LE_STP | LE_ENP) != (LE_STP | LE_ENP)) {
616 do {
617 cdm->mcnt = 0;
618 cdm->flags |= LE_OWN;
619 NEXTRDS;
620 } while ((cdm->flags & (LE_OWN | LE_ERR | LE_STP | LE_ENP)) == 0);
621 sc->sc_last_rd = rmd;
622 printf("%s: chained buffer\n", sc->sc_dev.dv_xname);
623 if ((cdm->flags & (LE_OWN | LE_ERR | LE_STP | LE_ENP)) != LE_ENP) {
624 lereset(sc);
625 return;
626 }
627 } else {
628 #ifdef LEDEBUG
629 if (sc->sc_debug)
630 recv_print(sc, sc->sc_last_rd);
631 #endif
632 leread(sc, sc->sc_rbuf + (BUFSIZE * rmd),
633 (int)cdm->mcnt);
634 sc->sc_if.if_ipackets++;
635 }
636
637 cdm->bcnt = -BUFSIZE;
638 cdm->mcnt = 0;
639 cdm->flags |= LE_OWN;
640 NEXTRDS;
641 #ifdef LEDEBUG
642 if (sc->sc_debug)
643 printf("sc->sc_last_rd = %x, cdm = %x\n",
644 sc->sc_last_rd, cdm);
645 #endif
646 } while ((cdm->flags & LE_OWN) == 0);
647
648 sc->sc_last_rd = rmd;
649 }
650
651 /*
652 * Pass a packet to the higher levels.
653 */
654 void
655 leread(sc, buf, len)
656 register struct le_softc *sc;
657 u_char *buf;
658 int len;
659 {
660 struct ifnet *ifp;
661 struct mbuf *m;
662 struct ether_header *eh;
663
664 len -= 4;
665 if (len <= 0)
666 return;
667
668 #ifdef LANCE_REVC_BUG /* XXX - Must be a better way... */
669 /*
670 * Check for unreported packet errors. Rev C of the LANCE chip
671 * has a bug which can cause "random" bytes to be prepended to
672 * the start of the packet. The work-around is to make sure that
673 * the Ethernet destination address in the packet matches our
674 * address (or the broadcast address).
675 */
676 {
677 register short *pp, *ea;
678
679 pp = (short *) buf;
680 ea = (short *) &sc->sc_enaddr;
681 if ((pp[0] == ea[0]) && (pp[1] == ea[1]) && (pp[2] == ea[2]))
682 goto ok;
683 if ((pp[0] == -1) && (pp[1] == -1) && (pp[2] == -1))
684 goto ok;
685 /* XXX - Multicast packets? */
686
687 sc->sc_if.if_ierrors++;
688 log(LOG_ERR, "%s: LANCE Rev C Extra Byte(s) bug; Packet punted\n",
689 sc->sc_dev.dv_xname);
690 return;
691 ok:
692 }
693 #endif /* LANCE_REVC_BUG */
694
695 /* Pull packet off interface. */
696 ifp = &sc->sc_if;
697 m = leget(buf, len, ifp);
698 if (m == 0)
699 return;
700
701 /* We assume that the header fit entirely in one mbuf. */
702 eh = mtod(m, struct ether_header *);
703
704 #if NBPFILTER > 0
705 /*
706 * Check if there's a BPF listener on this interface.
707 * If so, hand off the raw packet to BPF.
708 */
709 if (ifp->if_bpf) {
710 bpf_mtap(ifp->if_bpf, m);
711
712 /*
713 * Note that the interface cannot be in promiscuous mode if
714 * there are no BPF listeners. And if we are in promiscuous
715 * mode, we have to check if this packet is really ours.
716 */
717 if ((ifp->if_flags & IFF_PROMISC) &&
718 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
719 bcmp(eh->ether_dhost, sc->sc_enaddr,
720 sizeof(eh->ether_dhost)) != 0) {
721 m_freem(m);
722 return;
723 }
724 }
725 #endif
726
727 /* We assume that the header fit entirely in one mbuf. */
728 m->m_pkthdr.len -= sizeof(*eh);
729 m->m_len -= sizeof(*eh);
730 m->m_data += sizeof(*eh);
731
732 ether_input(ifp, eh, m);
733 }
734
735 /*
736 * Supporting routines
737 */
738
739 /*
740 * Pull data off an interface.
741 * Len is length of data, with local net header stripped.
742 * We copy the data into mbufs. When full cluster sized units are present
743 * we copy into clusters.
744 */
745 struct mbuf *
746 leget(buf, totlen, ifp)
747 u_char *buf;
748 int totlen;
749 struct ifnet *ifp;
750 {
751 struct mbuf *top, **mp, *m;
752 int len;
753
754 MGETHDR(m, M_DONTWAIT, MT_DATA);
755 if (m == 0)
756 return 0;
757 m->m_pkthdr.rcvif = ifp;
758 m->m_pkthdr.len = totlen;
759 len = MHLEN;
760 top = 0;
761 mp = ⊤
762
763 while (totlen > 0) {
764 if (top) {
765 MGET(m, M_DONTWAIT, MT_DATA);
766 if (m == 0) {
767 m_freem(top);
768 return 0;
769 }
770 len = MLEN;
771 }
772 if (totlen >= MINCLSIZE) {
773 MCLGET(m, M_DONTWAIT);
774 if (m->m_flags & M_EXT)
775 len = MCLBYTES;
776 }
777 m->m_len = len = min(totlen, len);
778 bcopy((caddr_t)buf, mtod(m, caddr_t), len);
779 buf += len;
780 totlen -= len;
781 *mp = m;
782 mp = &m->m_next;
783 }
784
785 return top;
786 }
787
788 /*
789 * Process an ioctl request.
790 */
791 int
792 leioctl(ifp, cmd, data)
793 register struct ifnet *ifp;
794 u_long cmd;
795 caddr_t data;
796 {
797 struct le_softc *sc = lecd.cd_devs[ifp->if_unit];
798 struct ifaddr *ifa = (struct ifaddr *)data;
799 struct ifreq *ifr = (struct ifreq *)data;
800 int s, error = 0;
801
802 s = splimp();
803
804 switch (cmd) {
805
806 case SIOCSIFADDR:
807 ifp->if_flags |= IFF_UP;
808
809 switch (ifa->ifa_addr->sa_family) {
810 #ifdef INET
811 case AF_INET:
812 leinit(sc);
813 arp_ifinit(&sc->sc_ac, ifa);
814 break;
815 #endif
816 #ifdef NS
817 /* XXX - This code is probably wrong. */
818 case AF_NS:
819 {
820 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
821
822 if (ns_nullhost(*ina))
823 ina->x_host =
824 *(union ns_host *)(sc->sc_enaddr);
825 else
826 bcopy(ina->x_host.c_host,
827 sc->sc_enaddr,
828 sizeof(sc->sc_enaddr));
829 /* Set new address. */
830 leinit(sc);
831 break;
832 }
833 #endif
834 default:
835 leinit(sc);
836 break;
837 }
838 break;
839
840 case SIOCSIFFLAGS:
841 /*
842 * If interface is marked down and it is running, then stop it
843 */
844 if ((ifp->if_flags & IFF_UP) == 0 &&
845 (ifp->if_flags & IFF_RUNNING) != 0) {
846 /*
847 * If interface is marked down and it is running, then
848 * stop it.
849 */
850 lestop(sc);
851 ifp->if_flags &= ~IFF_RUNNING;
852 } else if ((ifp->if_flags & IFF_UP) != 0 &&
853 (ifp->if_flags & IFF_RUNNING) == 0) {
854 /*
855 * If interface is marked up and it is stopped, then
856 * start it.
857 */
858 leinit(sc);
859 } else {
860 /*
861 * Reset the interface to pick up changes in any other
862 * flags that affect hardware registers.
863 */
864 /*lestop(sc);*/
865 leinit(sc);
866 }
867 #ifdef LEDEBUG
868 if (ifp->if_flags & IFF_DEBUG)
869 sc->sc_debug = 1;
870 else
871 sc->sc_debug = 0;
872 #endif
873 break;
874
875 case SIOCADDMULTI:
876 case SIOCDELMULTI:
877 error = (cmd == SIOCADDMULTI) ?
878 ether_addmulti(ifr, &sc->sc_ac):
879 ether_delmulti(ifr, &sc->sc_ac);
880
881 if (error == ENETRESET) {
882 /*
883 * Multicast list has changed; set the hardware filter
884 * accordingly.
885 */
886 leinit(sc);
887 error = 0;
888 }
889 break;
890
891 default:
892 error = EINVAL;
893 }
894 (void) splx(s);
895 return error;
896 }
897
898 #ifdef LEDEBUG
899 void
900 recv_print(sc, no)
901 struct le_softc *sc;
902 int no;
903 {
904 struct mds *rmd;
905 int i, printed = 0;
906 u_short len;
907
908 rmd = &sc->sc_rd[no];
909 len = rmd->mcnt;
910 printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
911 len);
912 printf("%s: status %x\n", sc->sc_dev.dv_xname, lerdcsr(sc, 0));
913 for (i = 0; i < len; i++) {
914 if (!printed) {
915 printed = 1;
916 printf("%s: data: ", sc->sc_dev.dv_xname);
917 }
918 printf("%x ", *(sc->sc_rbuf + (BUFSIZE*no) + i));
919 }
920 if (printed)
921 printf("\n");
922 }
923
924 void
925 xmit_print(sc, no)
926 struct le_softc *sc;
927 int no;
928 {
929 struct mds *rmd;
930 int i, printed=0;
931 u_short len;
932
933 rmd = &sc->sc_td[no];
934 len = -rmd->bcnt;
935 printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
936 len);
937 printf("%s: status %x\n", sc->sc_dev.dv_xname, lerdcsr(sc, 0));
938 printf("%s: addr %x, flags %x, bcnt %x, mcnt %x\n",
939 sc->sc_dev.dv_xname, rmd->addr, rmd->flags, rmd->bcnt, rmd->mcnt);
940 for (i = 0; i < len; i++) {
941 if (!printed) {
942 printed = 1;
943 printf("%s: data: ", sc->sc_dev.dv_xname);
944 }
945 printf("%x ", *(sc->sc_tbuf + (BUFSIZE*no) + i));
946 }
947 if (printed)
948 printf("\n");
949 }
950 #endif /* LEDEBUG */
951
952 /*
953 * Set up the logical address filter.
954 */
955 void
956 lesetladrf(ac, af)
957 struct arpcom *ac;
958 u_long *af;
959 {
960 struct ifnet *ifp = &ac->ac_if;
961 struct ether_multi *enm;
962 register u_char *cp, c;
963 register u_long crc;
964 register int i, len;
965 struct ether_multistep step;
966
967 /*
968 * Set up multicast address filter by passing all multicast addresses
969 * through a crc generator, and then using the high order 6 bits as an
970 * index into the 64 bit logical address filter. The high order bit
971 * selects the word, while the rest of the bits select the bit within
972 * the word.
973 */
974
975 if (ifp->if_flags & IFF_PROMISC) {
976 ifp->if_flags |= IFF_ALLMULTI;
977 af[0] = af[1] = 0xffffffff;
978 return;
979 }
980
981 af[0] = af[1] = 0;
982 ETHER_FIRST_MULTI(step, ac, enm);
983 while (enm != NULL) {
984 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
985 sizeof(enm->enm_addrlo)) != 0) {
986 /*
987 * We must listen to a range of multicast addresses.
988 * For now, just accept all multicasts, rather than
989 * trying to set only those filter bits needed to match
990 * the range. (At this time, the only use of address
991 * ranges is for IP multicast routing, for which the
992 * range is big enough to require all bits set.)
993 */
994 ifp->if_flags |= IFF_ALLMULTI;
995 af[0] = af[1] = 0xffffffff;
996 return;
997 }
998
999 cp = enm->enm_addrlo;
1000 crc = 0xffffffff;
1001 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1002 c = *cp++;
1003 for (i = 8; --i >= 0;) {
1004 if ((crc & 0x01) ^ (c & 0x01)) {
1005 crc >>= 1;
1006 crc ^= 0x6db88320 | 0x80000000;
1007 } else
1008 crc >>= 1;
1009 c >>= 1;
1010 }
1011 }
1012 /* Just want the 6 most significant bits. */
1013 crc >>= 26;
1014
1015 /* Turn on the corresponding bit in the filter. */
1016 af[crc >> 5] |= 1 << ((crc & 0x1f) ^ 0);
1017
1018 ETHER_NEXT_MULTI(step, enm);
1019 }
1020 ifp->if_flags &= ~IFF_ALLMULTI;
1021 }
1022