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