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