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