xref: /netbsd-src/sys/arch/sun3/dev/if_le.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*	$NetBSD: if_le.c,v 1.15 1994/12/12 18:59:12 gwr Exp $	*/
2 
3 /*-
4  * Copyright (c) 1982, 1992, 1993
5  *	The Regents of the University of California.  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	8.2 (Berkeley) 10/30/93
36  */
37 
38 #include "bpfilter.h"
39 
40 /*
41  * AMD 7990 LANCE
42  */
43 #include <sys/param.h>
44 #include <sys/device.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/mbuf.h>
48 #include <sys/buf.h>
49 #include <sys/socket.h>
50 #include <sys/syslog.h>
51 #include <sys/ioctl.h>
52 #include <sys/malloc.h>
53 #include <sys/errno.h>
54 
55 #include <net/if.h>
56 #include <net/netisr.h>
57 #include <net/route.h>
58 
59 #if NBPFILTER > 0
60 #include <sys/select.h>
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 #ifdef APPLETALK
79 #include <netddp/atalk.h>
80 #endif
81 
82 #include <machine/autoconf.h>
83 #include <machine/cpu.h>
84 
85 #include "if_lereg.h"
86 #include "if_le.h"
87 #include "if_le_subr.h"
88 
89 /*
90  * The lance has only 24 address lines.  When it accesses memory,
91  * the high address lines are hard-wired to 0xFF, so we must:
92  * (1) put what we want the LANCE to see above 0xFF000000, and
93  * (2) mask our CPU addresses down to 24 bits for the LANCE.
94  */
95 #define	LANCE_ADDR(x)	((u_int)(x) & 0xFFffff)
96 
97 /* console error messages */
98 int	ledebug = 0;
99 
100 #ifdef PACKETSTATS
101 long	lexpacketsizes[LEMTU+1];
102 long	lerpacketsizes[LEMTU+1];
103 #endif
104 
105 /* autoconfiguration driver */
106 void	le_attach(struct device *, struct device *, void *);
107 
108 struct	cfdriver lecd = {
109 	NULL, "le", le_md_match, le_attach,
110 	DV_IFNET, sizeof(struct le_softc),
111 };
112 
113 /* Forwards */
114 void	lesetladrf(struct le_softc *);
115 void	lereset(struct device *);
116 int 	leinit(int);
117 int 	lestart(struct ifnet *);
118 void	lexint(struct le_softc *);
119 void	lerint(struct le_softc *);
120 void	leread(struct le_softc *, char *, int);
121 int 	leput(char *, struct mbuf *);
122 struct mbuf *leget(char *, int, int, struct ifnet *);
123 int 	leioctl(struct ifnet *, u_long, caddr_t);
124 void	leerror(struct le_softc *, int);
125 void	lererror(struct le_softc *, char *);
126 void	lexerror(struct le_softc *);
127 int 	lewatchdog(int);	/* XXX */
128 
129 /*
130  * Interface exists: make available by filling in network interface
131  * record.  System will initialize the interface when it is ready
132  * to accept packets.
133  */
134 void
135 le_attach(parent, self, aux)
136 	struct device *parent;
137 	struct device *self;
138 	void *aux;
139 {
140 	struct le_softc *sc = (void *) self;
141 	volatile struct lereg2 *ler2;
142 	struct ifnet *ifp = &sc->sc_if;
143 	int pri;
144 	u_int a;
145 	caddr_t dvma_malloc();
146 
147 	le_md_attach(parent, self, aux);
148 	printf(" hwaddr %s\n", ether_sprintf(sc->sc_addr));
149 
150 	/*
151 	 * Setup for transmit/receive
152 	 *
153 	 * According to Van, some versions of the Lance only use this
154 	 * address to receive packets; it doesn't put them in
155 	 * output packets. We'll want to make sure that lestart()
156 	 * installs the address.
157 	 */
158 	ler2 = sc->sc_r2;
159 	ler2->ler2_padr[0] = sc->sc_addr[1];
160 	ler2->ler2_padr[1] = sc->sc_addr[0];
161 	ler2->ler2_padr[2] = sc->sc_addr[3];
162 	ler2->ler2_padr[3] = sc->sc_addr[2];
163 	ler2->ler2_padr[4] = sc->sc_addr[5];
164 	ler2->ler2_padr[5] = sc->sc_addr[4];
165 	a = LANCE_ADDR(ler2->ler2_rmd);
166 	ler2->ler2_rlen = LE_RLEN | (a >> 16);
167 	ler2->ler2_rdra = a;
168 	a = LANCE_ADDR(ler2->ler2_tmd);
169 	ler2->ler2_tlen = LE_TLEN | (a >> 16);
170 	ler2->ler2_tdra = a;
171 
172 	/*
173 	 * Set up event counters.
174 	 */
175 	evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
176 	evcnt_attach(&sc->sc_dev, "errs", &sc->sc_errcnt);
177 
178 	/*
179 	 * Initialize and attach S/W interface
180 	 */
181 	ifp->if_unit = sc->sc_dev.dv_unit;
182 	ifp->if_name = lecd.cd_name;
183 	ifp->if_ioctl = leioctl;
184 	ifp->if_output = ether_output;
185 	ifp->if_start = lestart;
186 	ifp->if_watchdog = lewatchdog;	/* XXX */
187 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
188 #ifdef IFF_NOTRAILERS
189 	/* XXX still compile when the blasted things are gone... */
190 	ifp->if_flags |= IFF_NOTRAILERS;
191 #endif
192 	if_attach(ifp);
193 	ether_ifattach(ifp);
194 #if NBPFILTER > 0
195 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
196 			  sizeof(struct ether_header));
197 #endif
198 }
199 
200 /*
201  * Setup the logical address filter
202  */
203 void
204 lesetladrf(sc)
205 	register struct le_softc *sc;
206 {
207 	register volatile struct lereg2 *ler2 = sc->sc_r2;
208 	register struct ifnet *ifp = &sc->sc_if;
209 	register struct ether_multi *enm;
210 	register u_char *cp, c;
211 	register u_long crc;
212 	register int i, len;
213 	struct ether_multistep step;
214 
215 	/*
216 	 * Set up multicast address filter by passing all multicast
217 	 * addresses through a crc generator, and then using the high
218 	 * order 6 bits as a index into the 64 bit logical address
219 	 * filter. The high order two bits select the word, while the
220 	 * rest of the bits select the bit within the word.
221 	 */
222 
223 	ler2->ler2_ladrf[0] = 0;
224 	ler2->ler2_ladrf[1] = 0;
225 	ler2->ler2_ladrf[2] = 0;
226 	ler2->ler2_ladrf[3] = 0;
227 	ifp->if_flags &= ~IFF_ALLMULTI;
228 	ETHER_FIRST_MULTI(step, &sc->sc_ac, enm);
229 	while (enm != NULL) {
230 		if (bcmp((caddr_t)&enm->enm_addrlo,
231 		    (caddr_t)&enm->enm_addrhi, sizeof(enm->enm_addrlo)) != 0) {
232 			/*
233 			 * We must listen to a range of multicast
234 			 * addresses. For now, just accept all
235 			 * multicasts, rather than trying to set only
236 			 * those filter bits needed to match the range.
237 			 * (At this time, the only use of address
238 			 * ranges is for IP multicast routing, for
239 			 * which the range is big enough to require all
240 			 * bits set.)
241 			 */
242 			ler2->ler2_ladrf[0] = 0xffff;
243 			ler2->ler2_ladrf[1] = 0xffff;
244 			ler2->ler2_ladrf[2] = 0xffff;
245 			ler2->ler2_ladrf[3] = 0xffff;
246 			ifp->if_flags |= IFF_ALLMULTI;
247 			return;
248 		}
249 
250 		/*
251 		 * One would think, given the AM7990 document's polynomial
252 		 * of 0x04c11db6, that this should be 0x6db88320 (the bit
253 		 * reversal of the AMD value), but that is not right.  See
254 		 * the BASIC listing: bit 0 (our bit 31) must then be set.
255 		 */
256 		cp = (unsigned char *)&enm->enm_addrlo;
257 		crc = 0xffffffff;
258 		for (len = 6; --len >= 0;) {
259 			c = *cp++;
260 			for (i = 0; i < 8; i++) {
261 				if ((c & 0x01) ^ (crc & 0x01)) {
262 					crc >>= 1;
263 					crc = crc ^ 0xedb88320;
264 				} else
265 					crc >>= 1;
266 				c >>= 1;
267 			}
268 		}
269 		/* Just want the 6 most significant bits. */
270 		crc = crc >> 26;
271 
272 		/* Turn on the corresponding bit in the filter. */
273 		ler2->ler2_ladrf[crc >> 4] |= 1 << (crc & 0xf);
274 
275 		ETHER_NEXT_MULTI(step, enm);
276 	}
277 }
278 
279 void
280 lereset(dev)
281 	struct device *dev;
282 {
283 	struct le_softc *sc = (struct le_softc *)dev;
284 	volatile struct lereg1 *ler1 = sc->sc_r1;
285 	volatile struct lereg2 *ler2 = sc->sc_r2;
286 	int i, timo, stat;
287 	u_int a;
288 
289 	if (ledebug)
290 	    printf("%s: resetting, reg %x, ram %x\n",
291 			   sc->sc_dev.dv_xname, sc->sc_r1, sc->sc_r2);
292 
293 #ifdef	DIAGNOSTIC
294 	i = getsr();
295 	if ((i & PSL_IPL) < PSL_IPL3)
296 		panic("lereset at low ipl, sr=%x", i);
297 #endif
298 
299 #if NBPFILTER > 0
300 	if (sc->sc_if.if_flags & IFF_PROMISC)
301 		ler2->ler2_mode = LE_MODE_NORMAL | LE_MODE_PROM;
302 	else
303 #endif
304 		ler2->ler2_mode = LE_MODE_NORMAL;
305 	ler1->ler1_rap = LE_CSR0;
306 	ler1->ler1_rdp = LE_C0_STOP;
307 
308 	/* Setup the logical address filter */
309 	lesetladrf(sc);
310 
311 	/* init receive and transmit rings */
312 	for (i = 0; i < LERBUF; i++) {
313 		a = LANCE_ADDR(&ler2->ler2_rbuf[i][0]);
314 		ler2->ler2_rmd[i].rmd0 = a;
315 		ler2->ler2_rmd[i].rmd1_hadr = a >> 16;
316 		ler2->ler2_rmd[i].rmd1_bits = LE_R1_OWN;
317 		ler2->ler2_rmd[i].rmd2 = -LEMTU | LE_XMD2_ONES;
318 		ler2->ler2_rmd[i].rmd3 = 0;
319 	}
320 	for (i = 0; i < LETBUF; i++) {
321 		a = LANCE_ADDR(&ler2->ler2_tbuf[i][0]);
322 		ler2->ler2_tmd[i].tmd0 = a;
323 		ler2->ler2_tmd[i].tmd1_hadr = a >> 16;
324 		ler2->ler2_tmd[i].tmd1_bits = 0;
325 		ler2->ler2_tmd[i].tmd2 = LE_XMD2_ONES;
326 		ler2->ler2_tmd[i].tmd3 = 0;
327 	}
328 
329 	bzero(&ler2->ler2_rbuf[0][0], (LERBUF + LETBUF) * LEMTU);
330 
331 	/* lance will stuff packet into receive buffer 0 next */
332 	sc->sc_rmd = 0;
333 
334 	/*
335 	 * Tell the chip where to find the initialization block.
336 	 * Note that CSR1, CSR2, and CSR3 may only be accessed
337 	 * while the STOP bit is set in CSR0.
338 	 */
339 	a = LANCE_ADDR(&ler2->ler2_mode);
340 	ler1->ler1_rap = LE_CSR1;
341 	ler1->ler1_rdp = a;
342 	ler1->ler1_rap = LE_CSR2;
343 	ler1->ler1_rdp = a >> 16;
344 	ler1->ler1_rap = LE_CSR3;
345 	ler1->ler1_rdp = LE_C3_CONFIG;
346 	ler1->ler1_rap = LE_CSR0;
347 	ler1->ler1_rdp = LE_C0_INIT;
348 	timo = 10000;
349 	while (((stat = ler1->ler1_rdp) & (LE_C0_ERR | LE_C0_IDON)) == 0) {
350 		delay(100); 	/* XXX */
351 		if (--timo == 0) {
352 			printf("%s: init timeout, stat=%b\n",
353 			    sc->sc_dev.dv_xname, stat, LE_C0_BITS);
354 			break;
355 		}
356 	}
357 	if (stat & LE_C0_ERR) {
358 		printf("%s: init failed, stat=%b\n",
359 		    sc->sc_dev.dv_xname, stat, LE_C0_BITS);
360 		sc->sc_if.if_flags &= ~IFF_RUNNING; 	/* XXX */
361 		return;
362 	}
363 	ler1->ler1_rdp = LE_C0_IDON;	/* clear IDON */
364 	ler1->ler1_rdp = LE_C0_STRT | LE_C0_INEA;
365 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
366 	delay(100);		/* XXX */
367 }
368 
369 /*
370  * Device timeout/watchdog routine.  Entered if the device neglects to
371  * generate an interrupt after a transmit has been started on it.
372  */
373 int
374 lewatchdog(unit)
375 	int unit;
376 {
377 	struct le_softc *sc = lecd.cd_devs[unit];
378 	int s;
379 
380 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
381 	sc->sc_if.if_oerrors++;
382 
383 #ifdef	DIAGNOSTIC
384 	s = getsr();
385 	if ((s & PSL_IPL) > PSL_IPL3)
386 		panic("lewatchdog would lower spl, sr=%x", s);
387 #endif
388 
389 	s = splimp();	/* XXX - Can this lower the IPL? */
390 	lereset(&sc->sc_dev);
391 	lestart(&sc->sc_if);
392 	splx(s);
393 }
394 
395 /*
396  * Initialization of interface
397  */
398 int
399 leinit(unit)
400 	int unit;
401 {
402 	struct le_softc *sc = lecd.cd_devs[unit];
403 	struct ifnet *ifp = &sc->sc_if;
404 	int s;
405 
406 	/* not yet, if address still unknown */
407 	if (ifp->if_addrlist == (struct ifaddr *)0) {
408 		if (ledebug)
409 			printf("leinit: no address yet\n");
410 		return (0);
411 	}
412 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
413 		s = splimp();
414 		if (ledebug)
415 		    printf("le: initializing unit %d, reg %x, ram %x\n",
416 				   unit, sc->sc_r1, sc->sc_r2);
417 		ifp->if_flags |= IFF_RUNNING;
418 		lereset(&sc->sc_dev);
419 		lestart(ifp);
420 		splx(s);
421 	}
422 	return (0);
423 }
424 
425 /*
426  * Start output on interface.  Get another datagram to send
427  * off of the interface queue, and copy it to the interface
428  * before starting the output.
429  */
430 int
431 lestart(ifp)
432 	register struct ifnet *ifp;
433 {
434 	register struct le_softc *sc = lecd.cd_devs[ifp->if_unit];
435 	register volatile struct letmd *tmd;
436 	register struct mbuf *m;
437 	register int len;
438 
439 #ifdef	DIAGNOSTIC
440 	int s = getsr();
441 	if ((s & PSL_IPL) < PSL_IPL3)
442 		panic("lestart at low ipl, sr=%x", s);
443 #endif
444 
445 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
446 		if (ledebug)
447 			printf("lestart: not running\n");
448 		return (0);
449 	}
450 	IF_DEQUEUE(&sc->sc_if.if_snd, m);
451 	if (m == 0) {
452 		if (ledebug & 2)
453 			printf("lestart: send queue empty\n");
454 		return (0);
455 	}
456 	len = leput(sc->sc_r2->ler2_tbuf[0], m);
457 #if NBPFILTER > 0
458 	/*
459 	 * If bpf is listening on this interface, let it
460 	 * see the packet before we commit it to the wire.
461 	 */
462 	if (sc->sc_if.if_bpf)
463 		bpf_tap(sc->sc_if.if_bpf, sc->sc_r2->ler2_tbuf[0], len);
464 #endif
465 
466 #ifdef PACKETSTATS
467 	if (len <= LEMTU)
468 		lexpacketsizes[len]++;
469 #endif
470 	tmd = sc->sc_r2->ler2_tmd;
471 	tmd->tmd3 = 0;
472 	tmd->tmd2 = -len | LE_XMD2_ONES;
473 	tmd->tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
474 	sc->sc_if.if_flags |= IFF_OACTIVE;
475 
476 	/* Set a timer just in case we never hear from the board again. */
477 	ifp->if_timer = 2;
478 
479 	return (0);
480 }
481 
482 int
483 le_intr(arg)
484 	register void *arg;
485 {
486 	register struct le_softc *sc = arg;
487 	register volatile struct lereg1 *ler1 = sc->sc_r1;
488 	register int csr0;
489 
490 	csr0 = ler1->ler1_rdp;
491 
492 	if ((csr0 & LE_C0_INTR) == 0)
493 		return (0);
494 
495 	if (ledebug & 2)
496 	    printf("[%s: intr, stat %b]\n",
497 			   sc->sc_dev.dv_xname, csr0, LE_C0_BITS);
498 
499 	sc->sc_intrcnt.ev_count++;
500 
501 	if (csr0 & LE_C0_ERR) {
502 		sc->sc_errcnt.ev_count++;
503 		leerror(sc, csr0);
504 		if (csr0 & LE_C0_MERR) {
505 			sc->sc_merr++;
506 			lereset(&sc->sc_dev);
507 			return (1);
508 		}
509 		if (csr0 & LE_C0_BABL)
510 			sc->sc_babl++;
511 		if (csr0 & LE_C0_CERR)
512 			sc->sc_cerr++;
513 		if (csr0 & LE_C0_MISS)
514 			sc->sc_miss++;
515 		ler1->ler1_rdp = LE_C0_BABL|LE_C0_CERR|LE_C0_MISS|LE_C0_INEA;
516 	}
517 	if ((csr0 & LE_C0_RXON) == 0) {
518 		sc->sc_rxoff++;
519 		lereset(&sc->sc_dev);
520 		return (1);
521 	}
522 	if ((csr0 & LE_C0_TXON) == 0) {
523 		sc->sc_txoff++;
524 		lereset(&sc->sc_dev);
525 		return (1);
526 	}
527 	if (csr0 & LE_C0_RINT) {
528 		/* interrupt is cleared in lerint */
529 		lerint(sc);
530 	}
531 	if (csr0 & LE_C0_TINT) {
532 		ler1->ler1_rdp = LE_C0_TINT|LE_C0_INEA;
533 		lexint(sc);
534 	}
535 	return (1);
536 }
537 
538 /*
539  * Ethernet interface transmitter interrupt.
540  * Start another output if more data to send.
541  */
542 void
543 lexint(sc)
544 	register struct le_softc *sc;
545 {
546 	register volatile struct letmd *tmd = sc->sc_r2->ler2_tmd;
547 
548 	sc->sc_lestats.lexints++;
549 	if ((sc->sc_if.if_flags & IFF_OACTIVE) == 0) {
550 		sc->sc_xint++;
551 		return;
552 	}
553 	if (tmd->tmd1_bits & LE_T1_OWN) {
554 		sc->sc_xown++;
555 		return;
556 	}
557 	if (tmd->tmd1_bits & LE_T1_ERR) {
558 err:
559 		lexerror(sc);
560 		sc->sc_if.if_oerrors++;
561 		if (tmd->tmd3 & (LE_T3_BUFF|LE_T3_UFLO)) {
562 			sc->sc_uflo++;
563 			lereset(&sc->sc_dev);
564 		} else if (tmd->tmd3 & LE_T3_LCOL)
565 			sc->sc_if.if_collisions++;
566 		else if (tmd->tmd3 & LE_T3_RTRY)
567 			sc->sc_if.if_collisions += 16;
568 	}
569 	else if (tmd->tmd3 & LE_T3_BUFF)
570 		/* XXX documentation says BUFF not included in ERR */
571 		goto err;
572 	else if (tmd->tmd1_bits & LE_T1_ONE)
573 		sc->sc_if.if_collisions++;
574 	else if (tmd->tmd1_bits & LE_T1_MORE)
575 		/* what is the real number? */
576 		sc->sc_if.if_collisions += 2;
577 	else
578 		sc->sc_if.if_opackets++;
579 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
580 	sc->sc_if.if_timer = 0;		/* XXX */
581 	lestart(&sc->sc_if);
582 }
583 
584 #define	LENEXTRMP \
585 	if (++bix == LERBUF) bix = 0, rmd = sc->sc_r2->ler2_rmd; else ++rmd
586 
587 /*
588  * Ethernet interface receiver interrupt.
589  * If input error just drop packet.
590  * Decapsulate packet based on type and pass to type specific
591  * higher-level input routine.
592  */
593 void
594 lerint(sc)
595 	register struct le_softc *sc;
596 {
597 	register int bix = sc->sc_rmd;
598 	register volatile struct lermd *rmd = &sc->sc_r2->ler2_rmd[bix];
599 
600 	sc->sc_lestats.lerints++;
601 	/*
602 	 * Out of sync with hardware, should never happen?
603 	 */
604 	if (rmd->rmd1_bits & LE_R1_OWN) {
605 		do {
606 			sc->sc_lestats.lerscans++;
607 			LENEXTRMP;
608 		} while ((rmd->rmd1_bits & LE_R1_OWN) && bix != sc->sc_rmd);
609 		if (bix == sc->sc_rmd)
610 			printf("%s: RINT with no buffer\n",
611 			    sc->sc_dev.dv_xname);
612 	} else
613 		sc->sc_lestats.lerhits++;
614 
615 	/*
616 	 * Process all buffers with valid data
617 	 */
618 	while ((rmd->rmd1_bits & LE_R1_OWN) == 0) {
619 		int len = rmd->rmd3;
620 
621 		/* Clear interrupt to avoid race condition */
622 		sc->sc_r1->ler1_rdp = LE_C0_RINT|LE_C0_INEA;
623 
624 		if (rmd->rmd1_bits & LE_R1_ERR) {
625 			sc->sc_rmd = bix;
626 			lererror(sc, "bad packet");
627 			sc->sc_if.if_ierrors++;
628 		} else if ((rmd->rmd1_bits & (LE_R1_STP|LE_R1_ENP)) !=
629 		    (LE_R1_STP|LE_R1_ENP)) {
630 			/* XXX make a define for LE_R1_STP|LE_R1_ENP? */
631 			/*
632 			 * Find the end of the packet so we can see how long
633 			 * it was.  We still throw it away.
634 			 */
635 			do {
636 				sc->sc_r1->ler1_rdp = LE_C0_RINT|LE_C0_INEA;
637 				rmd->rmd3 = 0;
638 				rmd->rmd1_bits = LE_R1_OWN;
639 				LENEXTRMP;
640 			} while (!(rmd->rmd1_bits &
641 			    (LE_R1_OWN|LE_R1_ERR|LE_R1_STP|LE_R1_ENP)));
642 			sc->sc_rmd = bix;
643 			lererror(sc, "chained buffer");
644 			sc->sc_rxlen++;
645 			/*
646 			 * If search terminated without successful completion
647 			 * we reset the hardware (conservative).
648 			 */
649 			if ((rmd->rmd1_bits &
650 			    (LE_R1_OWN|LE_R1_ERR|LE_R1_STP|LE_R1_ENP)) !=
651 			    LE_R1_ENP) {
652 				lereset(&sc->sc_dev);
653 				return;
654 			}
655 		} else {
656 			leread(sc, sc->sc_r2->ler2_rbuf[bix], len);
657 #ifdef PACKETSTATS
658 			lerpacketsizes[len]++;
659 #endif
660 			sc->sc_lestats.lerbufs++;
661 		}
662 		rmd->rmd3 = 0;
663 		rmd->rmd1_bits = LE_R1_OWN;
664 		LENEXTRMP;
665 	}
666 	sc->sc_rmd = bix;
667 }
668 
669 void
670 leread(sc, pkt, len)
671 	register struct le_softc *sc;
672 	char *pkt;
673 	int len;
674 {
675 	register struct ether_header *et;
676 	register struct ifnet *ifp = &sc->sc_if;
677 	struct mbuf *m;
678 	struct ifqueue *inq;
679 	int flags;
680 
681 	ifp->if_ipackets++;
682 	et = (struct ether_header *)pkt;
683 	et->ether_type = ntohs((u_short)et->ether_type);
684 	/* adjust input length to account for header and CRC */
685 	len -= sizeof(struct ether_header) + 4;
686 
687 	if (len <= 0) {
688 		if (ledebug)
689 			log(LOG_WARNING,
690 			    "%s: ierror(runt packet): from %s: len=%d\n",
691 			    sc->sc_dev.dv_xname,
692 			    ether_sprintf(et->ether_shost), len);
693 		sc->sc_runt++;
694 		ifp->if_ierrors++;
695 		return;
696 	}
697 
698 	/* Setup mbuf flags we'll need later */
699 	flags = 0;
700 	if (bcmp((caddr_t)etherbroadcastaddr,
701 	    (caddr_t)et->ether_dhost, sizeof(etherbroadcastaddr)) == 0)
702 		flags |= M_BCAST;
703 	if (et->ether_dhost[0] & 1)
704 		flags |= M_MCAST;
705 
706 #if NBPFILTER > 0
707 	/*
708 	 * Check if there's a bpf filter listening on this interface.
709 	 * If so, hand off the raw packet to enet, then discard things
710 	 * not destined for us (but be sure to keep broadcast/multicast).
711 	 */
712 	if (ifp->if_bpf) {
713 		bpf_tap(ifp->if_bpf, pkt,
714 		    len + sizeof(struct ether_header));
715 		if ((flags & (M_BCAST | M_MCAST)) == 0 &&
716 		    bcmp(et->ether_dhost, sc->sc_addr,
717 			    sizeof(et->ether_dhost)) != 0)
718 			return;
719 	}
720 #endif
721 	m = leget(pkt, len, 0, ifp);
722 	if (m == 0)
723 		return;
724 
725 	ether_input(ifp, et, m);
726 }
727 
728 /*
729  * Routine to copy from mbuf chain to transmit
730  * buffer in board local memory.
731  *
732  * ### this can be done by remapping in some cases
733  */
734 int
735 leput(lebuf, m)
736 	register char *lebuf;
737 	register struct mbuf *m;
738 {
739 	register struct mbuf *mp;
740 	register int len, tlen = 0;
741 
742 	for (mp = m; mp; mp = mp->m_next) {
743 		len = mp->m_len;
744 		if (len == 0)
745 			continue;
746 		tlen += len;
747 		bcopy(mtod(mp, char *), lebuf, len);
748 		lebuf += len;
749 	}
750 	m_freem(m);
751 	if (tlen < LEMINSIZE) {
752 		bzero(lebuf, LEMINSIZE - tlen);
753 		tlen = LEMINSIZE;
754 	}
755 	return (tlen);
756 }
757 
758 /*
759  * Routine to copy from board local memory into mbufs.
760  */
761 struct mbuf *
762 leget(lebuf, totlen, off0, ifp)
763 	char *lebuf;
764 	int totlen, off0;
765 	struct ifnet *ifp;
766 {
767 	register struct mbuf *m;
768 	struct mbuf *top = 0, **mp = &top;
769 	register int off = off0, len;
770 	register char *cp;
771 	char *epkt;
772 
773 	lebuf += sizeof(struct ether_header);
774 	cp = lebuf;
775 	epkt = cp + totlen;
776 	if (off) {
777 		cp += off + 2 * sizeof(u_short);
778 		totlen -= 2 * sizeof(u_short);
779 	}
780 
781 	MGETHDR(m, M_DONTWAIT, MT_DATA);
782 	if (m == 0)
783 		return (0);
784 	m->m_pkthdr.rcvif = ifp;
785 	m->m_pkthdr.len = totlen;
786 	m->m_len = MHLEN;
787 
788 	while (totlen > 0) {
789 		if (top) {
790 			MGET(m, M_DONTWAIT, MT_DATA);
791 			if (m == 0) {
792 				m_freem(top);
793 				return (0);
794 			}
795 			m->m_len = MLEN;
796 		}
797 		len = min(totlen, epkt - cp);
798 		if (len >= MINCLSIZE) {
799 			MCLGET(m, M_DONTWAIT);
800 			if (m->m_flags & M_EXT)
801 				m->m_len = len = min(len, MCLBYTES);
802 			else
803 				len = m->m_len;
804 		} else {
805 			/*
806 			 * Place initial small packet/header at end of mbuf.
807 			 */
808 			if (len < m->m_len) {
809 				if (top == 0 && len + max_linkhdr <= m->m_len)
810 					m->m_data += max_linkhdr;
811 				m->m_len = len;
812 			} else
813 				len = m->m_len;
814 		}
815 		bcopy(cp, mtod(m, caddr_t), (unsigned)len);
816 		cp += len;
817 		*mp = m;
818 		mp = &m->m_next;
819 		totlen -= len;
820 		if (cp == epkt)
821 			cp = lebuf;
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 	register struct ifaddr *ifa;
836 	register struct le_softc *sc = lecd.cd_devs[ifp->if_unit];
837 	register volatile struct lereg1 *ler1;
838 	int s, error;
839 
840 	/* Make sure attach was called. */
841 	if (sc->sc_r1 == NULL)
842 		return (ENXIO);
843 
844 	error = 0;
845 	s = splimp();
846 	switch (cmd) {
847 
848 	case SIOCSIFADDR:
849 		ifa = (struct ifaddr *)data;
850 		ifp->if_flags |= IFF_UP;
851 		switch (ifa->ifa_addr->sa_family) {
852 #ifdef INET
853 		case AF_INET:
854 			/* before arpwhohas */
855 		    if ((ifp->if_flags & IFF_RUNNING) == 0) 	/* XXX */
856 				(void)leinit(ifp->if_unit);
857 			((struct arpcom *)ifp)->ac_ipaddr =
858 				IA_SIN(ifa)->sin_addr;
859 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
860 			break;
861 #endif
862 #ifdef NS
863 		case AF_NS:
864 		    {
865 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
866 
867 			if (ns_nullhost(*ina))
868 				ina->x_host = *(union ns_host *)(sc->sc_addr);
869 			else {
870 				/*
871 				 * The manual says we can't change the address
872 				 * while the receiver is armed,
873 				 * so reset everything
874 				 */
875 				ifp->if_flags &= ~IFF_RUNNING;
876 				bcopy((caddr_t)ina->x_host.c_host,
877 				    (caddr_t)sc->sc_addr, sizeof(sc->sc_addr));
878 			}
879 			(void)leinit(ifp->if_unit);	/* does le_setaddr() */
880 			break;
881 		    }
882 #endif
883 		default:
884 			(void)leinit(ifp->if_unit);
885 			break;
886 		}
887 		break;
888 
889 	case SIOCSIFFLAGS:
890 		ler1 = sc->sc_r1;
891 		if ((ifp->if_flags & IFF_UP) == 0 &&
892 		    ifp->if_flags & IFF_RUNNING) {
893 			ler1->ler1_rdp = LE_C0_STOP;
894 			ifp->if_flags &= ~IFF_RUNNING;
895 		} else if (ifp->if_flags & IFF_UP &&
896 		    (ifp->if_flags & IFF_RUNNING) == 0)
897 			(void)leinit(ifp->if_unit);
898 		/*
899 		 * If the state of the promiscuous bit changes, the interface
900 		 * must be reset to effect the change.
901 		 */
902 		if (((ifp->if_flags ^ sc->sc_iflags) & IFF_PROMISC) &&
903 		    (ifp->if_flags & IFF_RUNNING)) {
904 			sc->sc_iflags = ifp->if_flags;
905 			lereset(&sc->sc_dev);
906 			lestart(ifp);
907 		}
908 		break;
909 
910 	case SIOCADDMULTI:
911 		error = ether_addmulti((struct ifreq *)data, &sc->sc_ac);
912 		goto update_multicast;
913 
914 	case SIOCDELMULTI:
915 		error = ether_delmulti((struct ifreq *)data, &sc->sc_ac);
916 	update_multicast:
917 		if (error == ENETRESET) {
918 			/*
919 			 * Multicast list has changed; set the hardware
920 			 * filter accordingly.
921 			 */
922 			lereset(&sc->sc_dev);
923 			lestart(ifp);			/* XXX */
924 			error = 0;
925 		}
926 		break;
927 
928 	default:
929 		error = EINVAL;
930 	}
931 	splx(s);
932 	return (error);
933 }
934 
935 void
936 leerror(sc, stat)
937 	register struct le_softc *sc;
938 	int stat;
939 {
940 	if (!ledebug)
941 		return;
942 
943 	/*
944 	 * Not all transceivers implement heartbeat
945 	 * so we only log CERR once.
946 	 */
947 	if ((stat & LE_C0_CERR) && sc->sc_cerr)
948 		return;
949 	log(LOG_WARNING, "%s: error: stat=%b\n",
950 	    sc->sc_dev.dv_xname, stat, LE_C0_BITS);
951 }
952 
953 void
954 lererror(sc, msg)
955 	register struct le_softc *sc;
956 	char *msg;
957 {
958 	register volatile struct lermd *rmd;
959 	int len;
960 
961 	if (!ledebug)
962 		return;
963 
964 	rmd = &sc->sc_r2->ler2_rmd[sc->sc_rmd];
965 	len = rmd->rmd3;
966 	log(LOG_WARNING, "%s: ierror(%s): from %s: buf=%d, len=%d, rmd1=%b\n",
967 	    sc->sc_dev.dv_xname, msg, len > 11 ?
968 	    ether_sprintf((u_char *)&sc->sc_r2->ler2_rbuf[sc->sc_rmd][6]) :
969 	    "unknown",
970 	    sc->sc_rmd, len, rmd->rmd1_bits, LE_R1_BITS);
971 }
972 
973 void
974 lexerror(sc)
975 	register struct le_softc *sc;
976 {
977 	register volatile struct letmd *tmd;
978 	register int len, tmd3, tdr;
979 
980 	if (!ledebug)
981 		return;
982 
983 	tmd = sc->sc_r2->ler2_tmd;
984 	tmd3 = tmd->tmd3;
985 	tdr = tmd3 & LE_T3_TDR_MASK;
986 	len = -(tmd->tmd2 & ~LE_XMD2_ONES);
987 	log(LOG_WARNING,
988     "%s: oerror: to %s: buf=%d, len=%d, tmd1=%b, tmd3=%b, tdr=%d (%d nsecs)\n",
989 	    sc->sc_dev.dv_xname, len > 5 ?
990 	    ether_sprintf((u_char *)&sc->sc_r2->ler2_tbuf[0][0]) : "unknown",
991 	    0, len,
992 	    tmd->tmd1_bits, LE_T1_BITS,
993 	    tmd3, LE_T3_BITS, tdr, tdr * 100);
994 }
995