xref: /original-bsd/sys/sparc/sbus/if_le.c (revision 404544c8)
1 /*-
2  * Copyright (c) 1982, 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)if_le.c	7.1 (Berkeley) 07/13/92
8  *
9  * from: $Header: if_le.c,v 1.17 92/07/10 06:45:17 torek Exp $
10  */
11 
12 #include "bpfilter.h"
13 
14 /*
15  * AMD 7990 LANCE
16  *
17  * This driver will generate and accept tailer encapsulated packets even
18  * though it buys us nothing.  The motivation was to avoid incompatibilities
19  * with VAXen, SUNs, and others that handle and benefit from them.
20  * This reasoning is dubious.
21  */
22 #include "sys/param.h"
23 #include "sys/device.h"
24 #include "sys/systm.h"
25 #include "sys/kernel.h"
26 #include "sys/mbuf.h"
27 #include "sys/buf.h"
28 #include "sys/socket.h"
29 #include "sys/syslog.h"
30 #include "sys/ioctl.h"
31 #include "sys/malloc.h"
32 #include "sys/errno.h"
33 
34 #include "../net/if.h"
35 #include "../net/netisr.h"
36 #include "../net/route.h"
37 #if NBPFILTER > 0
38 #include "sys/select.h"
39 #include "../net/bpf.h"
40 #include "../net/bpfdesc.h"
41 #endif
42 
43 #ifdef INET
44 #include "../netinet/in.h"
45 #include "../netinet/in_systm.h"
46 #include "../netinet/in_var.h"
47 #include "../netinet/ip.h"
48 #include "../netinet/if_ether.h"
49 #endif
50 
51 #ifdef NS
52 #include "../netns/ns.h"
53 #include "../netns/ns_if.h"
54 #endif
55 
56 #ifdef APPLETALK
57 #include "../netddp/atalk.h"
58 #endif
59 
60 #include "machine/autoconf.h"
61 #include "machine/cpu.h"
62 #include "machine/pmap.h"
63 
64 #include "if_lereg.h"
65 #include "sbusvar.h"
66 
67 /* DVMA address to LANCE address -- the Sbus/MMU will resupply the 0xff */
68 #define	LANCE_ADDR(x)	((int)(x) & ~0xff000000)
69 
70 int	ledebug = 0;		/* console error messages */
71 
72 #ifdef PACKETSTATS
73 long	lexpacketsizes[LEMTU+1];
74 long	lerpacketsizes[LEMTU+1];
75 #endif
76 
77 /* Per interface statistics */
78 /* XXX this should go in something like if_levar.h */
79 struct	lestats {
80 	long	lexints;	/* transmitter interrupts */
81 	long	lerints;	/* receiver interrupts */
82 	long	lerbufs;	/* total buffers received during interrupts */
83 	long	lerhits;	/* times current rbuf was full */
84 	long	lerscans;	/* rbufs scanned before finding first full */
85 };
86 
87 /*
88  * Ethernet software status per interface.
89  *
90  * Each interface is referenced by a network interface structure,
91  * le_if, which the routing code uses to locate the interface.
92  * This structure contains the output queue for the interface, its address, ...
93  */
94 struct le_softc {
95 	struct	device sc_dev;		/* base device */
96 	struct	sbusdev sc_sd;		/* sbus device */
97 	struct	intrhand sc_ih;		/* interrupt vectoring */
98 	int	sc_interrupts;		/* number of interrupts taken */
99 
100 	struct	arpcom sc_ac;		/* common Ethernet structures */
101 #define	sc_if	sc_ac.ac_if		/* network-visible interface */
102 #define	sc_addr	sc_ac.ac_enaddr		/* hardware Ethernet address */
103 	volatile struct	lereg1 *sc_r1;	/* LANCE registers */
104 	volatile struct	lereg2 *sc_r2;	/* dual-port RAM */
105 	int	sc_rmd;			/* predicted next rmd to process */
106 	int	sc_runt;
107 	int	sc_jab;
108 	int	sc_merr;
109 	int	sc_babl;
110 	int	sc_cerr;
111 	int	sc_miss;
112 	int	sc_xint;
113 	int	sc_xown;
114 	int	sc_uflo;
115 	int	sc_rxlen;
116 	int	sc_rxoff;
117 	int	sc_txoff;
118 	int	sc_busy;
119 	short	sc_iflags;
120 	struct	lestats sc_lestats;	/* per interface statistics */
121 #if NBPFILTER > 0
122 	caddr_t	sc_bpf;
123 #endif
124 };
125 
126 
127 /* autoconfiguration driver */
128 void	leattach(struct device *, struct device *, void *);
129 struct	cfdriver lecd =
130     { NULL, "le", matchbyname, leattach, DV_IFNET, sizeof(struct le_softc) };
131 
132 /* Forwards */
133 void	leattach(struct device *, struct device *, void *);
134 #ifdef MULTICAST
135 void	lesetladrf(struct le_softc *);
136 #endif
137 void	lereset(struct device *);
138 int	leinit(int);
139 int	lestart(struct ifnet *);
140 int	leintr(void *);
141 void	lexint(struct le_softc *);
142 void	lerint(struct le_softc *);
143 void	leread(struct le_softc *, char *, int);
144 int	leput(char *, struct mbuf *);
145 struct mbuf *leget(char *, int, int, struct ifnet *);
146 int	leioctl(struct ifnet *, int, caddr_t);
147 void	leerror(struct le_softc *, int);
148 void	lererror(struct le_softc *, char *);
149 void	lexerror(struct le_softc *);
150 
151 /*
152  * Interface exists: make available by filling in network interface
153  * record.  System will initialize the interface when it is ready
154  * to accept packets.
155  */
156 void
157 leattach(parent, self, args)
158 	struct device *parent;
159 	struct device *self;
160 	void *args;
161 {
162 	register struct le_softc *sc = (struct le_softc *)self;
163 	register struct sbus_attach_args *sa = args;
164 	register volatile struct lereg2 *ler2;
165 	struct ifnet *ifp = &sc->sc_if;
166 	register int a, pri;
167 #define	ISQUADALIGN(a) ((((long) a) & 0x3) == 0)
168 
169 	/* XXX the following declarations should be elsewhere */
170 	extern void myetheraddr(u_char *);
171 	extern caddr_t dvma_malloc(size_t);
172 
173 	if (sa->sa_ra.ra_nintr != 1) {
174 		printf(": expected 1 interrupt, got %d\n", sa->sa_ra.ra_nintr);
175 		return;
176 	}
177 	pri = sa->sa_ra.ra_intr[0].int_pri;
178 	printf(" pri %d", pri);
179 	sc->sc_r1 = (volatile struct lereg1 *)
180 	    mapiodev(sa->sa_ra.ra_paddr, sizeof(struct lereg1));
181 	ler2 = sc->sc_r2 = (volatile struct lereg2 *)
182 	    dvma_malloc(sizeof(struct lereg2));
183 if (!ISQUADALIGN(ler2))
184 	printf("? not quad aligned (0x%x)\n", ler2);
185 
186 	myetheraddr(sc->sc_addr);
187 	printf(": hardware address %s\n", ether_sprintf(sc->sc_addr));
188 
189 	/*
190 	 * Setup for transmit/receive
191 	 *
192 	 * According to Van, some versions of the Lance only use this
193 	 * address to receive packets; it doesn't put them in
194 	 * output packets. We'll want to make sure that lestart()
195 	 * installs the address.
196 	 */
197 	ler2->ler2_padr[0] = sc->sc_addr[1];
198 	ler2->ler2_padr[1] = sc->sc_addr[0];
199 	ler2->ler2_padr[2] = sc->sc_addr[3];
200 	ler2->ler2_padr[3] = sc->sc_addr[2];
201 	ler2->ler2_padr[4] = sc->sc_addr[5];
202 	ler2->ler2_padr[5] = sc->sc_addr[4];
203 	a = LANCE_ADDR(&ler2->ler2_rmd);
204 if (!ISQUADALIGN(a))
205 	printf("rdra not quad aligned (0x%x)\n", a);
206 	ler2->ler2_rlen = LE_RLEN | (a >> 16);
207 	ler2->ler2_rdra = a;
208 	a = LANCE_ADDR(&ler2->ler2_tmd);
209 if (!ISQUADALIGN(a))
210 	printf("tdra not quad aligned (0x%x)\n", a);
211 	ler2->ler2_tlen = LE_TLEN | (a >> 16);
212 	ler2->ler2_tdra = a;
213 
214 	/*
215 	 * Link into sbus, and establish interrupt handler.
216 	 */
217 	sc->sc_sd.sd_reset = lereset;
218 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
219 	sc->sc_ih.ih_fun = leintr;
220 	sc->sc_ih.ih_arg = sc;
221 	intr_establish(pri, &sc->sc_ih);
222 
223 	ifp->if_unit = sc->sc_dev.dv_unit;
224 	ifp->if_name = "le";
225 	ifp->if_mtu = ETHERMTU;
226 	ifp->if_init = leinit;
227 	ifp->if_ioctl = leioctl;
228 	ifp->if_output = ether_output;
229 	ifp->if_start = lestart;
230 #ifdef MULTICAST
231 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
232 #else
233 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
234 #endif
235 #ifdef IFF_NOTRAILERS
236 	/* XXX still compile when the blasted things are gone... */
237 	ifp->if_flags |= IFF_NOTRAILERS;
238 #endif
239 #if NBPFILTER > 0
240 	bpfattach(&sc->sc_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
241 #endif
242 	if_attach(ifp);
243 }
244 
245 #ifdef MULTICAST
246 /*
247  * Setup the logical address filter
248  */
249 void
250 lesetladrf(sc)
251 	register struct le_softc *sc;
252 {
253 	register volatile struct lereg2 *ler2 = sc->sc_r2;
254 	register struct ifnet *ifp = &sc->sc_if;
255 	register struct ether_multi *enm;
256 	register u_char *cp;
257 	register u_long crc;
258 	register u_long c;
259 	register int i, len;
260 	struct ether_multistep step;
261 
262 	/*
263 	 * Set up multicast address filter by passing all multicast
264 	 * addresses through a crc generator, and then using the high
265 	 * order 6 bits as a index into the 64 bit logical address
266 	 * filter. The high order two bits select the word, while the
267 	 * rest of the bits select the bit within the word.
268 	 */
269 
270 	ler2->ler2_ladrf[0] = 0;
271 	ler2->ler2_ladrf[1] = 0;
272 	ifp->if_flags &= ~IFF_ALLMULTI;
273 	ETHER_FIRST_MULTI(step, &sc->sc_ac, enm);
274 	while (enm != NULL) {
275 		if (bcmp((caddr_t)&enm->enm_addrlo,
276 		    (caddr_t)&enm->enm_addrhi, sizeof(enm->enm_addrlo)) == 0) {
277 			/*
278 			 * We must listen to a range of multicast
279 			 * addresses. For now, just accept all
280 			 * multicasts, rather than trying to set only
281 			 * those filter bits needed to match the range.
282 			 * (At this time, the only use of address
283 			 * ranges is for IP multicast routing, for
284 			 * which the range is big enough to require all
285 			 * bits set.)
286 			 */
287 			ler2->ler2_ladrf[0] = 0xffffffff;
288 			ler2->ler2_ladrf[1] = 0xffffffff;
289 			ifp->if_flags |= IFF_ALLMULTI;
290 			return;
291 		}
292 
293 		cp = (unsigned char *)&enm->enm_addrlo;
294 		c = *cp;
295 		crc = 0xffffffff;
296 		len = 6;
297 		while (len-- > 0) {
298 			c = *cp;
299 			for (i = 0; i < 8; i++) {
300 				if ((c & 0x01) ^ (crc & 0x01)) {
301 					crc >>= 1;
302 					crc = crc ^ 0xedb88320;
303 				}
304 				else
305 					crc >>= 1;
306 				c >>= 1;
307 			}
308 			cp++;
309 		}
310 		/* Just want the 6 most significant bits. */
311 		crc = crc >> 26;
312 
313 		/* Turn on the corresponding bit in the filter. */
314 		ler2->ler2_ladrf[crc >> 5] |= 1 << (crc & 0x1f);
315 
316 		ETHER_NEXT_MULTI(step, enm);
317 	}
318 }
319 #endif
320 
321 void
322 lereset(dev)
323 	struct device *dev;
324 {
325 	register struct le_softc *sc = (struct le_softc *)dev;
326 	register volatile struct lereg1 *ler1 = sc->sc_r1;
327 	register volatile struct lereg2 *ler2 = sc->sc_r2;
328 	register int i, a, timo, stat;
329 
330 #if NBPFILTER > 0
331 	if (sc->sc_if.if_flags & IFF_PROMISC)
332 		ler2->ler2_mode = LE_MODE_NORMAL | LE_MODE_PROM;
333 	else
334 #endif
335 		ler2->ler2_mode = LE_MODE_NORMAL;
336 	ler1->ler1_rap = LE_CSR0;
337 	ler1->ler1_rdp = LE_C0_STOP;
338 
339 	/* Setup the logical address filter */
340 #ifdef MULTICAST
341 	lesetladrf(sc);
342 #else
343 	ler2->ler2_ladrf[0] = 0;
344 	ler2->ler2_ladrf[1] = 0;
345 #endif
346 
347 	/* init receive and transmit rings */
348 a = LANCE_ADDR(&ler2->ler2_rbuf[0][0]);
349 if (!ISQUADALIGN(a))
350 	printf("rbuf not quad aligned (0x%x)\n", a);
351 	for (i = 0; i < LERBUF; i++) {
352 		a = LANCE_ADDR(&ler2->ler2_rbuf[i][0]);
353 		ler2->ler2_rmd[i].rmd0 = a;
354 		ler2->ler2_rmd[i].rmd1_hadr = a >> 16;
355 		ler2->ler2_rmd[i].rmd1_bits = LE_R1_OWN;
356 		ler2->ler2_rmd[i].rmd2 = -LEMTU;
357 		ler2->ler2_rmd[i].rmd3 = 0;
358 	}
359 a = LANCE_ADDR(&ler2->ler2_tbuf[0][0]);
360 if (!ISQUADALIGN(a))
361 	printf("tbuf not quad aligned (0x%x)\n", a);
362 	for (i = 0; i < LETBUF; i++) {
363 		a = LANCE_ADDR(&ler2->ler2_tbuf[i][0]);
364 		ler2->ler2_tmd[i].tmd0 = a;
365 		ler2->ler2_tmd[i].tmd1_hadr = a >> 16;
366 		ler2->ler2_tmd[i].tmd1_bits = 0;
367 		ler2->ler2_tmd[i].tmd2 = 0;
368 		ler2->ler2_tmd[i].tmd3 = 0;
369 	}
370 
371 bzero(&ler2->ler2_rbuf[0][0], (LERBUF + LETBUF) * LEMTU);
372 	/* lance will stuff packet into receive buffer 0 next */
373 	sc->sc_rmd = 0;
374 
375 	/* tell the chip where to find the initialization block */
376 	a = LANCE_ADDR(&ler2->ler2_mode);
377 	ler1->ler1_rap = LE_CSR1;
378 	ler1->ler1_rdp = a;
379 	ler1->ler1_rap = LE_CSR2;
380 	ler1->ler1_rdp = a >> 16;
381 	ler1->ler1_rap = LE_CSR3;
382 	ler1->ler1_rdp = LE_C3_BSWP | LE_C3_ACON | LE_C3_BCON;
383 	ler1->ler1_rap = LE_CSR0;
384 	ler1->ler1_rdp = LE_C0_INIT;
385 	timo = 100000;
386 	while (((stat = ler1->ler1_rdp) & (LE_C0_ERR | LE_C0_IDON)) == 0) {
387 		if (--timo == 0) {
388 			printf("%s: init timeout, stat=%b\n",
389 			    sc->sc_dev.dv_xname, stat, LE_C0_BITS);
390 			break;
391 		}
392 	}
393 	if (stat & LE_C0_ERR)
394 		printf("%s: init failed, stat=%b\n",
395 		    sc->sc_dev.dv_xname, stat, LE_C0_BITS);
396 	else
397 		ler1->ler1_rdp = LE_C0_IDON;	/* clear IDON */
398 	ler1->ler1_rdp = LE_C0_STRT | LE_C0_INEA;
399 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
400 }
401 
402 /*
403  * Initialization of interface
404  */
405 int
406 leinit(unit)
407 	int unit;
408 {
409 	register struct le_softc *sc = lecd.cd_devs[unit];
410 	register struct ifnet *ifp = &sc->sc_if;
411 	register int s;
412 
413 	/* not yet, if address still unknown */
414 	if (ifp->if_addrlist == (struct ifaddr *)0)
415 		return (0);
416 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
417 		s = splimp();
418 		ifp->if_flags |= IFF_RUNNING;
419 		lereset((struct device *)sc);
420 	        lestart(ifp);
421 		splx(s);
422 	}
423 	return (0);
424 }
425 
426 /*
427  * Start output on interface.  Get another datagram to send
428  * off of the interface queue, and copy it to the interface
429  * before starting the output.
430  */
431 int
432 lestart(ifp)
433 	register struct ifnet *ifp;
434 {
435 	register struct le_softc *sc = lecd.cd_devs[ifp->if_unit];
436 	register volatile struct letmd *tmd;
437 	register struct mbuf *m;
438 	register int len;
439 
440 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0)
441 		return (0);
442 	IF_DEQUEUE(&sc->sc_if.if_snd, m);
443 	if (m == 0)
444 		return (0);
445 	len = leput(sc->sc_r2->ler2_tbuf[0], m);
446 #if NBPFILTER > 0
447 	/*
448 	 * If bpf is listening on this interface, let it
449 	 * see the packet before we commit it to the wire.
450 	 */
451 	if (sc->sc_bpf)
452 		bpf_tap(sc->sc_bpf, sc->sc_r2->ler2_tbuf[0], len);
453 #endif
454 
455 #ifdef PACKETSTATS
456 	if (len <= LEMTU)
457 		lexpacketsizes[len]++;
458 #endif
459 	tmd = sc->sc_r2->ler2_tmd;
460 	tmd->tmd3 = 0;
461 	tmd->tmd2 = -len;
462 	tmd->tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
463 	sc->sc_if.if_flags |= IFF_OACTIVE;
464 	return (0);
465 }
466 
467 int
468 leintr(dev)
469 	register void *dev;
470 {
471 	register struct le_softc *sc = dev;
472 	register volatile struct lereg1 *ler1 = sc->sc_r1;
473 	register int csr0;
474 
475 	csr0 = ler1->ler1_rdp;
476 	if ((csr0 & LE_C0_INTR) == 0)
477 		return (0);
478 	sc->sc_interrupts++;
479 
480 	if (csr0 & LE_C0_ERR) {
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.
687 	 */
688 	if (sc->sc_bpf) {
689 		bpf_tap(sc->sc_bpf, pkt, len + sizeof(struct ether_header));
690 
691 		/*
692 		 * Keep the packet if it's a broadcast or has our
693 		 * physical ethernet address (or if we support
694 		 * multicast and it's one).
695 		 */
696 		if (
697 #ifdef MULTICAST
698 		    (flags & (M_BCAST | M_MCAST)) == 0 &&
699 #else
700 		    (flags & M_BCAST) == 0 &&
701 #endif
702 		    bcmp(et->ether_dhost, sc->sc_addr,
703 			sizeof(et->ether_dhost)) != 0)
704 			return;
705 	}
706 #endif
707 	m = leget(pkt, len, 0, ifp);
708 	if (m == 0)
709 		return;
710 
711 	/* XXX this code comes from ether_input() */
712 	ifp->if_lastchange = time;
713 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*et);
714 	if (flags) {
715 		m->m_flags |= flags;
716 		ifp->if_imcasts++;
717 	}
718 	/* XXX end of code from ether_input() */
719 
720 	switch (et->ether_type) {
721 
722 #ifdef INET
723 	case ETHERTYPE_IP:
724 		schednetisr(NETISR_IP);
725 		inq = &ipintrq;
726 		break;
727 
728 	case ETHERTYPE_ARP:
729 		schednetisr(NETISR_ARP);
730 		inq = &arpintrq;
731 		break;
732 #endif
733 #ifdef NS
734 	case ETHERTYPE_NS:
735 		schednetisr(NETISR_NS);
736 		inq = &nsintrq;
737 		break;
738 #endif
739 
740 #ifdef UTAHONLY
741 #ifdef APPLETALK
742 	case ETHERTYPE_APPLETALK:
743 		schednetisr(NETISR_DDP);
744 		inq = &ddpintq;
745 		break;
746 
747 	case ETHERTYPE_AARP:
748 		aarpinput(&sc->sc_ac, m);
749 		return;
750 #endif
751 #endif
752 	default:
753 		m_freem(m);
754 		return;
755 	}
756 
757 	if (IF_QFULL(inq)) {
758 		IF_DROP(inq);
759 		m_freem(m);
760 		return;
761 	}
762 	IF_ENQUEUE(inq, m);
763 }
764 
765 /*
766  * Routine to copy from mbuf chain to transmit
767  * buffer in board local memory.
768  *
769  * ### this can be done by remapping in some cases
770  */
771 int
772 leput(lebuf, m)
773 	register char *lebuf;
774 	register struct mbuf *m;
775 {
776 	register struct mbuf *mp;
777 	register int len, tlen = 0;
778 
779 	for (mp = m; mp; mp = mp->m_next) {
780 		len = mp->m_len;
781 		if (len == 0)
782 			continue;
783 		tlen += len;
784 		bcopy(mtod(mp, char *), lebuf, len);
785 		lebuf += len;
786 	}
787 	m_freem(m);
788 	if (tlen < LEMINSIZE) {
789 		bzero(lebuf, LEMINSIZE - tlen);
790 		tlen = LEMINSIZE;
791 	}
792 	return (tlen);
793 }
794 
795 /*
796  * Routine to copy from board local memory into mbufs.
797  */
798 struct mbuf *
799 leget(lebuf, totlen, off0, ifp)
800 	char *lebuf;
801 	int totlen, off0;
802 	struct ifnet *ifp;
803 {
804 	register struct mbuf *m;
805 	struct mbuf *top = 0, **mp = &top;
806 	register int off = off0, len;
807 	register char *cp;
808 	char *epkt;
809 
810 	lebuf += sizeof(struct ether_header);
811 	cp = lebuf;
812 	epkt = cp + totlen;
813 	if (off) {
814 		cp += off + 2 * sizeof(u_short);
815 		totlen -= 2 * sizeof(u_short);
816 	}
817 
818 	MGETHDR(m, M_DONTWAIT, MT_DATA);
819 	if (m == 0)
820 		return (0);
821 	m->m_pkthdr.rcvif = ifp;
822 	m->m_pkthdr.len = totlen;
823 	m->m_len = MHLEN;
824 
825 	while (totlen > 0) {
826 		if (top) {
827 			MGET(m, M_DONTWAIT, MT_DATA);
828 			if (m == 0) {
829 				m_freem(top);
830 				return (0);
831 			}
832 			m->m_len = MLEN;
833 		}
834 		len = min(totlen, epkt - cp);
835 		if (len >= MINCLSIZE) {
836 			MCLGET(m, M_DONTWAIT);
837 			if (m->m_flags & M_EXT)
838 				m->m_len = len = min(len, MCLBYTES);
839 			else
840 				len = m->m_len;
841 		} else {
842 			/*
843 			 * Place initial small packet/header at end of mbuf.
844 			 */
845 			if (len < m->m_len) {
846 				if (top == 0 && len + max_linkhdr <= m->m_len)
847 					m->m_data += max_linkhdr;
848 				m->m_len = len;
849 			} else
850 				len = m->m_len;
851 		}
852 		bcopy(cp, mtod(m, caddr_t), (unsigned)len);
853 		cp += len;
854 		*mp = m;
855 		mp = &m->m_next;
856 		totlen -= len;
857 		if (cp == epkt)
858 			cp = lebuf;
859 	}
860 	return (top);
861 }
862 
863 /*
864  * Process an ioctl request.
865  */
866 int
867 leioctl(ifp, cmd, data)
868 	register struct ifnet *ifp;
869 	int cmd;
870 	caddr_t data;
871 {
872 	register struct ifaddr *ifa;
873 	register struct le_softc *sc = lecd.cd_devs[ifp->if_unit];
874 	register volatile struct lereg1 *ler1;
875 	int s = splimp(), error = 0;
876 
877 	switch (cmd) {
878 
879 	case SIOCSIFADDR:
880 		ifa = (struct ifaddr *)data;
881 		ifp->if_flags |= IFF_UP;
882 		switch (ifa->ifa_addr->sa_family) {
883 #ifdef INET
884 		case AF_INET:
885 			(void)leinit(ifp->if_unit);	/* before arpwhohas */
886 			((struct arpcom *)ifp)->ac_ipaddr =
887 				IA_SIN(ifa)->sin_addr;
888 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
889 			break;
890 #endif
891 #ifdef NS
892 		case AF_NS:
893 		    {
894 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
895 
896 			if (ns_nullhost(*ina))
897 				ina->x_host = *(union ns_host *)(sc->sc_addr);
898 			else {
899 				/*
900 				 * The manual says we can't change the address
901 				 * while the receiver is armed,
902 				 * so reset everything
903 				 */
904 				ifp->if_flags &= ~IFF_RUNNING;
905 				bcopy((caddr_t)ina->x_host.c_host,
906 				    (caddr_t)sc->sc_addr, sizeof(sc->sc_addr));
907 			}
908 			(void)leinit(ifp->if_unit);	/* does le_setaddr() */
909 			break;
910 		    }
911 #endif
912 		default:
913 			(void)leinit(ifp->if_unit);
914 			break;
915 		}
916 		break;
917 
918 	case SIOCSIFFLAGS:
919 		ler1 = sc->sc_r1;
920 		if ((ifp->if_flags & IFF_UP) == 0 &&
921 		    ifp->if_flags & IFF_RUNNING) {
922 			ler1->ler1_rdp = LE_C0_STOP;
923 			ifp->if_flags &= ~IFF_RUNNING;
924 		} else if (ifp->if_flags & IFF_UP &&
925 		    (ifp->if_flags & IFF_RUNNING) == 0)
926 			(void)leinit(ifp->if_unit);
927 		/*
928 		 * If the state of the promiscuous bit changes, the interface
929 		 * must be reset to effect the change.
930 		 */
931 		if (((ifp->if_flags ^ sc->sc_iflags) & IFF_PROMISC) &&
932 		    (ifp->if_flags & IFF_RUNNING)) {
933 			sc->sc_iflags = ifp->if_flags;
934 			lereset((struct device *)sc);
935 			lestart(ifp);
936 		}
937 		break;
938 
939 #ifdef MULTICAST
940 	case SIOCADDMULTI:
941 	case SIOCDELMULTI:
942 		/* Update our multicast list  */
943 		error = (cmd == SIOCADDMULTI) ?
944 		    ether_addmulti((struct ifreq *)data, &sc->sc_ac) :
945 		    ether_delmulti((struct ifreq *)data, &sc->sc_ac);
946 
947 		if (error == ENETRESET) {
948 			/*
949 			 * Multicast list has changed; set the hardware
950 			 * filter accordingly.
951 			 */
952 			lereset((struct device *)sc);
953 			error = 0;
954 		}
955 		break;
956 #endif
957 
958 	default:
959 		error = EINVAL;
960 	}
961 	splx(s);
962 	return (error);
963 }
964 
965 void
966 leerror(sc, stat)
967 	register struct le_softc *sc;
968 	int stat;
969 {
970 	if (!ledebug)
971 		return;
972 
973 	/*
974 	 * Not all transceivers implement heartbeat
975 	 * so we only log CERR once.
976 	 */
977 	if ((stat & LE_C0_CERR) && sc->sc_cerr)
978 		return;
979 	log(LOG_WARNING, "%s: error: stat=%b\n",
980 	    sc->sc_dev.dv_xname, stat, LE_C0_BITS);
981 }
982 
983 void
984 lererror(sc, msg)
985 	register struct le_softc *sc;
986 	char *msg;
987 {
988 	register volatile struct lermd *rmd;
989 	int len;
990 
991 	if (!ledebug)
992 		return;
993 
994 	rmd = &sc->sc_r2->ler2_rmd[sc->sc_rmd];
995 	len = rmd->rmd3;
996 	log(LOG_WARNING, "%s: ierror(%s): from %s: buf=%d, len=%d, rmd1=%b\n",
997 	    sc->sc_dev.dv_xname, msg, len > 11 ?
998 	    ether_sprintf((u_char *)&sc->sc_r2->ler2_rbuf[sc->sc_rmd][6]) :
999 	    "unknown",
1000 	    sc->sc_rmd, len, rmd->rmd1_bits, LE_R1_BITS);
1001 }
1002 
1003 void
1004 lexerror(sc)
1005 	register struct le_softc *sc;
1006 {
1007 	register volatile struct letmd *tmd;
1008 	register int len, tmd3, tdr;
1009 
1010 	if (!ledebug)
1011 		return;
1012 
1013 	tmd = sc->sc_r2->ler2_tmd;
1014 	tmd3 = tmd->tmd3;
1015 	tdr = tmd3 & LE_T3_TDR_MASK;
1016 	len = -tmd->tmd2;
1017 	log(LOG_WARNING,
1018     "%s: oerror: to %s: buf=%d, len=%d, tmd1=%b, tmd3=%b, tdr=%d (%d nsecs)\n",
1019 	    sc->sc_dev.dv_xname, len > 5 ?
1020 	    ether_sprintf((u_char *)&sc->sc_r2->ler2_tbuf[0][0]) : "unknown",
1021 	    0, len,
1022 	    tmd->tmd1_bits, LE_T1_BITS,
1023 	    tmd3, LE_T3_BITS, tdr, tdr * 100);
1024 }
1025