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