xref: /original-bsd/sys/hp300/dev/if_le.c (revision 70c898fa)
1 /*
2  * Copyright (c) 1982, 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)if_le.c	7.16 (Berkeley) 03/11/93
8  */
9 
10 #include "le.h"
11 #if NLE > 0
12 
13 #include "bpfilter.h"
14 
15 /*
16  * AMD 7990 LANCE
17  *
18  * This driver will accept tailer encapsulated packets even
19  * though it buys us nothing.  The motivation was to avoid incompatibilities
20  * with VAXen, SUNs, and others that handle and benefit from them.
21  * This reasoning is dubious.
22  */
23 #include <sys/param.h>
24 #include <sys/proc.h>
25 #include <sys/systm.h>
26 #include <sys/mbuf.h>
27 #include <sys/buf.h>
28 #include <sys/protosw.h>
29 #include <sys/socket.h>
30 #include <sys/syslog.h>
31 #include <sys/ioctl.h>
32 #include <sys/errno.h>
33 
34 #include <net/if.h>
35 #include <net/netisr.h>
36 #include <net/route.h>
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 #if defined (CCITT) && defined (LLC)
52 #include <sys/socketvar.h>
53 #include <netccitt/x25.h>
54 extern llc_ctlinput(), cons_rtrequest();
55 #endif
56 
57 #include <machine/cpu.h>
58 #include <hp300/hp300/isr.h>
59 #include <machine/mtpr.h>
60 #include <hp/dev/device.h>
61 #include <hp300/dev/if_lereg.h>
62 #ifdef USELEDS
63 #include <hp300/hp300/led.h>
64 #endif
65 
66 #if NBPFILTER > 0
67 #include <net/bpf.h>
68 #include <net/bpfdesc.h>
69 #endif
70 
71 /* offsets for:	   ID,   REGS,    MEM,  NVRAM */
72 int	lestd[] = { 0, 0x4000, 0x8000, 0xC008 };
73 
74 int	leattach();
75 struct	driver ledriver = {
76 	leattach, "le",
77 };
78 
79 struct	isr le_isr[NLE];
80 int	ledebug = 0;		/* console error messages */
81 
82 int	leintr(), leinit(), leioctl(), lestart(), ether_output(), lereset();
83 struct	mbuf *m_devget();
84 extern	struct ifnet loif;
85 
86 /*
87  * Ethernet software status per interface.
88  *
89  * Each interface is referenced by a network interface structure,
90  * le_if, which the routing code uses to locate the interface.
91  * This structure contains the output queue for the interface, its address, ...
92  */
93 struct	le_softc {
94 	struct	arpcom sc_ac;	/* common Ethernet structures */
95 #define	sc_if	sc_ac.ac_if	/* network-visible interface */
96 #define	sc_addr	sc_ac.ac_enaddr	/* hardware Ethernet address */
97 	struct	lereg0 *sc_r0;	/* DIO registers */
98 	struct	lereg1 *sc_r1;	/* LANCE registers */
99 	struct	lereg2 *sc_r2;	/* dual-port RAM */
100 	int	sc_rmd;		/* predicted next rmd to process */
101 	int	sc_tmd;		/* next available tmd */
102 	int	sc_txcnt;	/* # of transmit buffers in use */
103 	/* stats */
104 	int	sc_runt;
105 	int	sc_jab;
106 	int	sc_merr;
107 	int	sc_babl;
108 	int	sc_cerr;
109 	int	sc_miss;
110 	int	sc_rown;
111 	int	sc_xown;
112 	int	sc_xown2;
113 	int	sc_uflo;
114 	int	sc_rxlen;
115 	int	sc_rxoff;
116 	int	sc_txoff;
117 	int	sc_busy;
118 	short	sc_iflags;
119 } le_softc[NLE];
120 
121 /* access LANCE registers */
122 #define	LERDWR(cntl, src, dst) \
123 	do { \
124 		(dst) = (src); \
125 	} while (((cntl)->ler0_status & LE_ACK) == 0);
126 
127 /*
128  * Interface exists: make available by filling in network interface
129  * record.  System will initialize the interface when it is ready
130  * to accept packets.
131  */
132 leattach(hd)
133 	struct hp_device *hd;
134 {
135 	register struct lereg0 *ler0;
136 	register struct lereg2 *ler2;
137 	struct lereg2 *lemem = 0;
138 	struct le_softc *le = &le_softc[hd->hp_unit];
139 	struct ifnet *ifp = &le->sc_if;
140 	char *cp;
141 	int i;
142 
143 	ler0 = le->sc_r0 = (struct lereg0 *)(lestd[0] + (int)hd->hp_addr);
144 	le->sc_r1 = (struct lereg1 *)(lestd[1] + (int)hd->hp_addr);
145 	ler2 = le->sc_r2 = (struct lereg2 *)(lestd[2] + (int)hd->hp_addr);
146 	if (ler0->ler0_id != LEID)
147 		return(0);
148 	le_isr[hd->hp_unit].isr_intr = leintr;
149 	hd->hp_ipl = le_isr[hd->hp_unit].isr_ipl = LE_IPL(ler0->ler0_status);
150 	le_isr[hd->hp_unit].isr_arg = hd->hp_unit;
151 	ler0->ler0_id = 0xFF;
152 	DELAY(100);
153 
154 	/*
155 	 * Read the ethernet address off the board, one nibble at a time.
156 	 */
157 	cp = (char *)(lestd[3] + (int)hd->hp_addr);
158 	for (i = 0; i < sizeof(le->sc_addr); i++) {
159 		le->sc_addr[i] = (*++cp & 0xF) << 4;
160 		cp++;
161 		le->sc_addr[i] |= *++cp & 0xF;
162 		cp++;
163 	}
164 	printf("le%d: hardware address %s\n", hd->hp_unit,
165 		ether_sprintf(le->sc_addr));
166 
167 	/*
168 	 * Setup for transmit/receive
169 	 */
170 	ler2->ler2_mode = LE_MODE;
171 	ler2->ler2_ladrf[0] = 0;
172 	ler2->ler2_ladrf[1] = 0;
173 	ler2->ler2_rlen = LE_RLEN;
174 	ler2->ler2_rdra = (int)lemem->ler2_rmd;
175 	ler2->ler2_tlen = LE_TLEN;
176 	ler2->ler2_tdra = (int)lemem->ler2_tmd;
177 	isrlink(&le_isr[hd->hp_unit]);
178 	ler0->ler0_status = LE_IE;
179 
180 	ifp->if_unit = hd->hp_unit;
181 	ifp->if_name = "le";
182 	ifp->if_mtu = ETHERMTU;
183 	ifp->if_init = leinit;
184 	ifp->if_reset = lereset;
185 	ifp->if_ioctl = leioctl;
186 	ifp->if_output = ether_output;
187 	ifp->if_start = lestart;
188 #ifdef MULTICAST
189 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
190 #else
191 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
192 #endif
193 #if NBPFILTER > 0
194 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
195 #endif
196 	if_attach(ifp);
197 	return (1);
198 }
199 
200 #ifdef MULTICAST
201 /*
202  * Setup the logical address filter
203  */
204 void
205 lesetladrf(sc)
206 	register struct le_softc *sc;
207 {
208 	register volatile struct lereg2 *ler2 = sc->sc_r2;
209 	register struct ifnet *ifp = &sc->sc_if;
210 	register struct ether_multi *enm;
211 	register u_char *cp;
212 	register u_long crc;
213 	register u_long c;
214 	register int i, len;
215 	struct ether_multistep step;
216 
217 	/*
218 	 * Set up multicast address filter by passing all multicast
219 	 * addresses through a crc generator, and then using the high
220 	 * order 6 bits as a index into the 64 bit logical address
221 	 * filter. The high order two bits select the word, while the
222 	 * rest of the bits select the bit within the word.
223 	 */
224 
225 	ler2->ler2_ladrf[0] = 0;
226 	ler2->ler2_ladrf[1] = 0;
227 	ifp->if_flags &= ~IFF_ALLMULTI;
228 	ETHER_FIRST_MULTI(step, &sc->sc_ac, enm);
229 	while (enm != NULL) {
230 		if (bcmp((caddr_t)&enm->enm_addrlo,
231 		    (caddr_t)&enm->enm_addrhi, sizeof(enm->enm_addrlo)) == 0) {
232 			/*
233 			 * We must listen to a range of multicast
234 			 * addresses. For now, just accept all
235 			 * multicasts, rather than trying to set only
236 			 * those filter bits needed to match the range.
237 			 * (At this time, the only use of address
238 			 * ranges is for IP multicast routing, for
239 			 * which the range is big enough to require all
240 			 * bits set.)
241 			 */
242 			ler2->ler2_ladrf[0] = 0xffffffff;
243 			ler2->ler2_ladrf[1] = 0xffffffff;
244 			ifp->if_flags |= IFF_ALLMULTI;
245 			return;
246 		}
247 
248 		cp = (unsigned char *)&enm->enm_addrlo;
249 		c = *cp;
250 		crc = 0xffffffff;
251 		len = 6;
252 		while (len-- > 0) {
253 			c = *cp;
254 			for (i = 0; i < 8; i++) {
255 				if ((c & 0x01) ^ (crc & 0x01)) {
256 					crc >>= 1;
257 					crc = crc ^ 0xedb88320;
258 				}
259 				else
260 					crc >>= 1;
261 				c >>= 1;
262 			}
263 			cp++;
264 		}
265 		/* Just want the 6 most significant bits. */
266 		crc = crc >> 26;
267 
268 		/* Turn on the corresponding bit in the filter. */
269 		ler2->ler2_ladrf[crc >> 5] |= 1 << (crc & 0x1f);
270 
271 		ETHER_NEXT_MULTI(step, enm);
272 	}
273 }
274 #endif
275 
276 ledrinit(ler2, le)
277 	register struct lereg2 *ler2;
278 	register struct le_softc *le;
279 {
280 	register struct lereg2 *lemem = 0;
281 	register int i;
282 
283 	ler2->ler2_padr[0] = le->sc_addr[1];
284 	ler2->ler2_padr[1] = le->sc_addr[0];
285 	ler2->ler2_padr[2] = le->sc_addr[3];
286 	ler2->ler2_padr[3] = le->sc_addr[2];
287 	ler2->ler2_padr[4] = le->sc_addr[5];
288 	ler2->ler2_padr[5] = le->sc_addr[4];
289 	for (i = 0; i < LERBUF; i++) {
290 		ler2->ler2_rmd[i].rmd0 = (int)lemem->ler2_rbuf[i];
291 		ler2->ler2_rmd[i].rmd1 = LE_OWN;
292 		ler2->ler2_rmd[i].rmd2 = -LEMTU;
293 		ler2->ler2_rmd[i].rmd3 = 0;
294 	}
295 	for (i = 0; i < LETBUF; i++) {
296 		ler2->ler2_tmd[i].tmd0 = (int)lemem->ler2_tbuf[i];
297 		ler2->ler2_tmd[i].tmd1 = 0;
298 		ler2->ler2_tmd[i].tmd2 = 0;
299 		ler2->ler2_tmd[i].tmd3 = 0;
300 	}
301 	/* Setup the logical address filter */
302 #ifdef MULTICAST
303 	lesetladrf(le);
304 #else
305 	ler2->ler2_ladrf[0] = 0;
306 	ler2->ler2_ladrf[1] = 0;
307 #endif
308 }
309 
310 lereset(unit)
311 	register int unit;
312 {
313 	register struct le_softc *le = &le_softc[unit];
314 	register struct lereg0 *ler0 = le->sc_r0;
315 	register struct lereg1 *ler1 = le->sc_r1;
316 	register struct lereg2 *lemem = 0;
317 	register int timo = 100000;
318 	register int stat;
319 
320 #ifdef lint
321 	stat = unit;
322 #endif
323 #if NBPFILTER > 0
324 	if (le->sc_if.if_flags & IFF_PROMISC)
325 		/* set the promiscuous bit */
326 		le->sc_r2->ler2_mode = LE_MODE|0x8000;
327 	else
328 		le->sc_r2->ler2_mode = LE_MODE;
329 #endif
330 	LERDWR(ler0, LE_CSR0, ler1->ler1_rap);
331 	LERDWR(ler0, LE_STOP, ler1->ler1_rdp);
332 	ledrinit(le->sc_r2, le);
333 	le->sc_rmd = le->sc_tmd = 0;
334 	LERDWR(ler0, LE_CSR1, ler1->ler1_rap);
335 	LERDWR(ler0, (int)&lemem->ler2_mode, ler1->ler1_rdp);
336 	LERDWR(ler0, LE_CSR2, ler1->ler1_rap);
337 	LERDWR(ler0, 0, ler1->ler1_rdp);
338 	LERDWR(ler0, LE_CSR0, ler1->ler1_rap);
339 	LERDWR(ler0, LE_INIT, ler1->ler1_rdp);
340 	do {
341 		if (--timo == 0) {
342 			printf("le%d: init timeout, stat = 0x%x\n",
343 			       unit, stat);
344 			break;
345 		}
346 		LERDWR(ler0, ler1->ler1_rdp, stat);
347 	} while ((stat & LE_IDON) == 0);
348 	LERDWR(ler0, LE_STOP, ler1->ler1_rdp);
349 	LERDWR(ler0, LE_CSR3, ler1->ler1_rap);
350 	LERDWR(ler0, LE_BSWP, ler1->ler1_rdp);
351 	LERDWR(ler0, LE_CSR0, ler1->ler1_rap);
352 	LERDWR(ler0, LE_STRT | LE_INEA, ler1->ler1_rdp);
353 	le->sc_if.if_flags &= ~IFF_OACTIVE;
354 	le->sc_txcnt = 0;
355 }
356 
357 /*
358  * Initialization of interface
359  */
360 leinit(unit)
361 	int unit;
362 {
363 	register struct ifnet *ifp = &le_softc[unit].sc_if;
364 	register struct ifaddr *ifa;
365 	int s;
366 
367 	/* not yet, if address still unknown */
368 	for (ifa = ifp->if_addrlist;; ifa = ifa->ifa_next)
369 		if (ifa == 0)
370 			return;
371 		else if (ifa->ifa_addr && ifa->ifa_addr->sa_family != AF_LINK)
372 			break;
373 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
374 		s = splimp();
375 		ifp->if_flags |= IFF_RUNNING;
376 		lereset(unit);
377 	        (void) lestart(ifp);
378 		splx(s);
379 	}
380 }
381 
382 /*
383  * Start output on interface.  Get another datagram to send
384  * off of the interface queue, and copy it to the interface
385  * before starting the output.
386  */
387 lestart(ifp)
388 	struct ifnet *ifp;
389 {
390 	register struct le_softc *le = &le_softc[ifp->if_unit];
391 	register struct letmd *tmd;
392 	register struct mbuf *m;
393 	int len;
394 
395 	if ((le->sc_if.if_flags & IFF_RUNNING) == 0)
396 		return (0);
397 	tmd = &le->sc_r2->ler2_tmd[le->sc_tmd];
398 	do {
399 		if (tmd->tmd1 & LE_OWN) {
400 			le->sc_xown2++;
401 			return (0);
402 		}
403 		IF_DEQUEUE(&le->sc_if.if_snd, m);
404 		if (m == 0)
405 			return (0);
406 		len = leput(le->sc_r2->ler2_tbuf[le->sc_tmd], m);
407 #if NBPFILTER > 0
408 		/*
409 		 * If bpf is listening on this interface, let it
410 		 * see the packet before we commit it to the wire.
411 		 */
412 		if (ifp->if_bpf)
413 			bpf_tap(ifp->if_bpf, le->sc_r2->ler2_tbuf[le->sc_tmd],
414 				len);
415 #endif
416 
417 		tmd->tmd3 = 0;
418 		tmd->tmd2 = -len;
419 		tmd->tmd1 = LE_OWN | LE_STP | LE_ENP;
420 		if (++le->sc_tmd == LETBUF) {
421 			le->sc_tmd = 0;
422 			tmd = le->sc_r2->ler2_tmd;
423 		} else
424 			tmd++;
425 	} while (++le->sc_txcnt < LETBUF);
426 	le->sc_if.if_flags |= IFF_OACTIVE;
427 	return (0);
428 }
429 
430 leintr(unit)
431 	register int unit;
432 {
433 	register struct le_softc *le = &le_softc[unit];
434 	register struct lereg0 *ler0 = le->sc_r0;
435 	register struct lereg1 *ler1;
436 	register int stat;
437 
438 	if ((ler0->ler0_status & LE_IR) == 0)
439 		return(0);
440 	if (ler0->ler0_status & LE_JAB) {
441 		le->sc_jab++;
442 		lereset(unit);
443 		return(1);
444 	}
445 	ler1 = le->sc_r1;
446 	LERDWR(ler0, ler1->ler1_rdp, stat);
447 	if (stat & LE_SERR) {
448 		leerror(unit, stat);
449 		if (stat & LE_MERR) {
450 			le->sc_merr++;
451 			lereset(unit);
452 			return(1);
453 		}
454 		if (stat & LE_BABL)
455 			le->sc_babl++;
456 		if (stat & LE_CERR)
457 			le->sc_cerr++;
458 		if (stat & LE_MISS)
459 			le->sc_miss++;
460 		LERDWR(ler0, LE_BABL|LE_CERR|LE_MISS|LE_INEA, ler1->ler1_rdp);
461 	}
462 	if ((stat & LE_RXON) == 0) {
463 		le->sc_rxoff++;
464 		lereset(unit);
465 		return(1);
466 	}
467 	if ((stat & LE_TXON) == 0) {
468 		le->sc_txoff++;
469 		lereset(unit);
470 		return(1);
471 	}
472 	if (stat & LE_RINT)
473 		lerint(unit);
474 	if (stat & LE_TINT)
475 		lexint(unit);
476 	return(1);
477 }
478 
479 /*
480  * Ethernet interface transmitter interrupt.
481  * Start another output if more data to send.
482  */
483 lexint(unit)
484 	register int unit;
485 {
486 	register struct le_softc *le = &le_softc[unit];
487 	register struct letmd *tmd;
488 	int i, gotone = 0;
489 
490 #ifdef USELEDS
491 	if (inledcontrol == 0)
492 		ledcontrol(0, 0, LED_LANXMT);
493 #endif
494 	do {
495 		if ((i = le->sc_tmd - le->sc_txcnt) < 0)
496 			i += LETBUF;
497 		tmd = &le->sc_r2->ler2_tmd[i];
498 		if (tmd->tmd1 & LE_OWN) {
499 			if (gotone)
500 				break;
501 			le->sc_xown++;
502 			return;
503 		}
504 
505 		/* clear interrupt */
506 		LERDWR(le->sc_r0, LE_TINT|LE_INEA, le->sc_r1->ler1_rdp);
507 
508 		/* XXX documentation says BUFF not included in ERR */
509 		if ((tmd->tmd1 & LE_ERR) || (tmd->tmd3 & LE_TBUFF)) {
510 			lexerror(unit);
511 			le->sc_if.if_oerrors++;
512 			if (tmd->tmd3 & (LE_TBUFF|LE_UFLO)) {
513 				le->sc_uflo++;
514 				lereset(unit);
515 			} else if (tmd->tmd3 & LE_LCOL)
516 				le->sc_if.if_collisions++;
517 			else if (tmd->tmd3 & LE_RTRY)
518 				le->sc_if.if_collisions += 16;
519 		} else if (tmd->tmd1 & LE_ONE)
520 			le->sc_if.if_collisions++;
521 		else if (tmd->tmd1 & LE_MORE)
522 			/* what is the real number? */
523 			le->sc_if.if_collisions += 2;
524 		else
525 			le->sc_if.if_opackets++;
526 		gotone++;
527 	} while (--le->sc_txcnt > 0);
528 	le->sc_if.if_flags &= ~IFF_OACTIVE;
529 	(void) lestart(&le->sc_if);
530 }
531 
532 #define	LENEXTRMP \
533 	if (++bix == LERBUF) bix = 0, rmd = le->sc_r2->ler2_rmd; else ++rmd
534 
535 /*
536  * Ethernet interface receiver interrupt.
537  * If input error just drop packet.
538  * Decapsulate packet based on type and pass to type specific
539  * higher-level input routine.
540  */
541 lerint(unit)
542 	int unit;
543 {
544 	register struct le_softc *le = &le_softc[unit];
545 	register int bix = le->sc_rmd;
546 	register struct lermd *rmd = &le->sc_r2->ler2_rmd[bix];
547 
548 #ifdef USELEDS
549 	if (inledcontrol == 0)
550 		ledcontrol(0, 0, LED_LANRCV);
551 #endif
552 	/*
553 	 * Out of sync with hardware, should never happen?
554 	 */
555 	if (rmd->rmd1 & LE_OWN) {
556 		le->sc_rown++;
557 		LERDWR(le->sc_r0, LE_RINT|LE_INEA, le->sc_r1->ler1_rdp);
558 		return;
559 	}
560 
561 	/*
562 	 * Process all buffers with valid data
563 	 */
564 	while ((rmd->rmd1 & LE_OWN) == 0) {
565 		int len = rmd->rmd3;
566 
567 		/* Clear interrupt to avoid race condition */
568 		LERDWR(le->sc_r0, LE_RINT|LE_INEA, le->sc_r1->ler1_rdp);
569 
570 		if (rmd->rmd1 & LE_ERR) {
571 			le->sc_rmd = bix;
572 			lererror(unit, "bad packet");
573 			le->sc_if.if_ierrors++;
574 		} else if ((rmd->rmd1 & (LE_STP|LE_ENP)) != (LE_STP|LE_ENP)) {
575 			/*
576 			 * Find the end of the packet so we can see how long
577 			 * it was.  We still throw it away.
578 			 */
579 			do {
580 				LERDWR(le->sc_r0, LE_RINT|LE_INEA,
581 				       le->sc_r1->ler1_rdp);
582 				rmd->rmd3 = 0;
583 				rmd->rmd1 = LE_OWN;
584 				LENEXTRMP;
585 			} while (!(rmd->rmd1 & (LE_OWN|LE_ERR|LE_STP|LE_ENP)));
586 			le->sc_rmd = bix;
587 			lererror(unit, "chained buffer");
588 			le->sc_rxlen++;
589 			/*
590 			 * If search terminated without successful completion
591 			 * we reset the hardware (conservative).
592 			 */
593 			if ((rmd->rmd1 & (LE_OWN|LE_ERR|LE_STP|LE_ENP)) !=
594 			    LE_ENP) {
595 				lereset(unit);
596 				return;
597 			}
598 		} else
599 			leread(unit, le->sc_r2->ler2_rbuf[bix], len);
600 		rmd->rmd3 = 0;
601 		rmd->rmd1 = LE_OWN;
602 		LENEXTRMP;
603 	}
604 	le->sc_rmd = bix;
605 }
606 
607 leread(unit, buf, len)
608 	int unit;
609 	char *buf;
610 	int len;
611 {
612 	register struct le_softc *le = &le_softc[unit];
613 	register struct ether_header *et;
614     	struct mbuf *m;
615 	int off, resid, flags;
616 
617 	le->sc_if.if_ipackets++;
618 	et = (struct ether_header *)buf;
619 	et->ether_type = ntohs((u_short)et->ether_type);
620 	/* adjust input length to account for header and CRC */
621 	len = len - sizeof(struct ether_header) - 4;
622 
623 #define	ledataaddr(et, off, type)	((type)(((caddr_t)((et)+1)+(off))))
624 	if (et->ether_type >= ETHERTYPE_TRAIL &&
625 	    et->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
626 		off = (et->ether_type - ETHERTYPE_TRAIL) * 512;
627 		if (off >= ETHERMTU)
628 			return;		/* sanity */
629 		et->ether_type = ntohs(*ledataaddr(et, off, u_short *));
630 		resid = ntohs(*(ledataaddr(et, off+2, u_short *)));
631 		if (off + resid > len)
632 			return;		/* sanity */
633 		len = off + resid;
634 	} else
635 		off = 0;
636 
637 	if (len <= 0) {
638 		if (ledebug)
639 			log(LOG_WARNING,
640 			    "le%d: ierror(runt packet): from %s: len=%d\n",
641 			    unit, ether_sprintf(et->ether_shost), len);
642 		le->sc_runt++;
643 		le->sc_if.if_ierrors++;
644 		return;
645 	}
646 	flags = 0;
647 	if (bcmp((caddr_t)etherbroadcastaddr,
648 	    (caddr_t)et->ether_dhost, sizeof(etherbroadcastaddr)) == 0)
649 		flags |= M_BCAST;
650 	if (et->ether_dhost[0] & 1)
651 		flags |= M_MCAST;
652 
653 #if NBPFILTER > 0
654 	/*
655 	 * Check if there's a bpf filter listening on this interface.
656 	 * If so, hand off the raw packet to enet.
657 	 */
658 	if (le->sc_if.if_bpf) {
659 		bpf_tap(le->sc_if.if_bpf, buf, len + sizeof(struct ether_header));
660 
661 		/*
662 		 * Keep the packet if it's a broadcast or has our
663 		 * physical ethernet address (or if we support
664 		 * multicast and it's one).
665 		 */
666 		if (
667 #ifdef MULTICAST
668 		    (flags & (M_BCAST | M_MCAST)) == 0 &&
669 #else
670 		    (flags & M_BCAST) == 0 &&
671 #endif
672 		    bcmp(et->ether_dhost, le->sc_addr,
673 			sizeof(et->ether_dhost)) != 0)
674 			return;
675 	}
676 #endif
677 	/*
678 	 * Pull packet off interface.  Off is nonzero if packet
679 	 * has trailing header; m_devget will then force this header
680 	 * information to be at the front, but we still have to drop
681 	 * the type and length which are at the front of any trailer data.
682 	 */
683 	m = m_devget((char *)(et + 1), len, off, &le->sc_if, 0);
684 	if (m == 0)
685 		return;
686 	m->m_flags |= flags;
687 	ether_input(&le->sc_if, et, m);
688 }
689 
690 /*
691  * Routine to copy from mbuf chain to transmit
692  * buffer in board local memory.
693  */
694 leput(lebuf, m)
695 	register char *lebuf;
696 	register struct mbuf *m;
697 {
698 	register struct mbuf *mp;
699 	register int len, tlen = 0;
700 
701 	for (mp = m; mp; mp = mp->m_next) {
702 		len = mp->m_len;
703 		if (len == 0)
704 			continue;
705 		tlen += len;
706 		bcopy(mtod(mp, char *), lebuf, len);
707 		lebuf += len;
708 	}
709 	m_freem(m);
710 	if (tlen < LEMINSIZE) {
711 		bzero(lebuf, LEMINSIZE - tlen);
712 		tlen = LEMINSIZE;
713 	}
714 	return(tlen);
715 }
716 
717 /*
718  * Process an ioctl request.
719  */
720 leioctl(ifp, cmd, data)
721 	register struct ifnet *ifp;
722 	int cmd;
723 	caddr_t data;
724 {
725 	register struct ifaddr *ifa = (struct ifaddr *)data;
726 	struct le_softc *le = &le_softc[ifp->if_unit];
727 	struct lereg1 *ler1 = le->sc_r1;
728 	int s = splimp(), error = 0;
729 
730 	switch (cmd) {
731 
732 	case SIOCSIFADDR:
733 		ifp->if_flags |= IFF_UP;
734 		switch (ifa->ifa_addr->sa_family) {
735 #ifdef INET
736 		case AF_INET:
737 			leinit(ifp->if_unit);	/* before arpwhohas */
738 			((struct arpcom *)ifp)->ac_ipaddr =
739 				IA_SIN(ifa)->sin_addr;
740 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
741 			break;
742 #endif
743 #ifdef NS
744 		case AF_NS:
745 		    {
746 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
747 
748 			if (ns_nullhost(*ina))
749 				ina->x_host = *(union ns_host *)(le->sc_addr);
750 			else {
751 				/*
752 				 * The manual says we can't change the address
753 				 * while the receiver is armed,
754 				 * so reset everything
755 				 */
756 				ifp->if_flags &= ~IFF_RUNNING;
757 				LERDWR(le->sc_r0, LE_STOP, ler1->ler1_rdp);
758 				bcopy((caddr_t)ina->x_host.c_host,
759 				    (caddr_t)le->sc_addr, sizeof(le->sc_addr));
760 			}
761 			leinit(ifp->if_unit); /* does le_setaddr() */
762 			break;
763 		    }
764 #endif
765 		default:
766 			leinit(ifp->if_unit);
767 			break;
768 		}
769 		break;
770 
771 #if defined (CCITT) && defined (LLC)
772 	case SIOCSIFCONF_X25:
773 		ifp -> if_flags |= IFF_UP;
774 		ifa -> ifa_rtrequest = cons_rtrequest;
775 		error = x25_llcglue(PRC_IFUP, ifa -> ifa_addr);
776 		if (error == 0)
777 			leinit(ifp -> if_unit);
778 		break;
779 #endif /* CCITT && LLC */
780 
781 
782 	case SIOCSIFFLAGS:
783 		if ((ifp->if_flags & IFF_UP) == 0 &&
784 		    ifp->if_flags & IFF_RUNNING) {
785 			LERDWR(le->sc_r0, LE_STOP, ler1->ler1_rdp);
786 			ifp->if_flags &= ~IFF_RUNNING;
787 		} else if (ifp->if_flags & IFF_UP &&
788 		    (ifp->if_flags & IFF_RUNNING) == 0)
789 			leinit(ifp->if_unit);
790 		/*
791 		 * If the state of the promiscuous bit changes, the interface
792 		 * must be reset to effect the change.
793 		 */
794 		if (((ifp->if_flags ^ le->sc_iflags) & IFF_PROMISC) &&
795 		    (ifp->if_flags & IFF_RUNNING)) {
796 			le->sc_iflags = ifp->if_flags;
797 			lereset(ifp->if_unit);
798 			lestart(ifp);
799 		}
800 		break;
801 
802 #ifdef MULTICAST
803 	case SIOCADDMULTI:
804 	case SIOCDELMULTI:
805 		/* Update our multicast list  */
806 		error = (cmd == SIOCADDMULTI) ?
807 		    ether_addmulti((struct ifreq *)data, &le->sc_ac) :
808 		    ether_delmulti((struct ifreq *)data, &le->sc_ac);
809 
810 		if (error == ENETRESET) {
811 			/*
812 			 * Multicast list has changed; set the hardware
813 			 * filter accordingly.
814 			 */
815 			lereset(ifp->if_unit);
816 			error = 0;
817 		}
818 		break;
819 #endif
820 	default:
821 		error = EINVAL;
822 	}
823 	splx(s);
824 	return (error);
825 }
826 
827 leerror(unit, stat)
828 	int unit;
829 	int stat;
830 {
831 	if (!ledebug)
832 		return;
833 
834 	/*
835 	 * Not all transceivers implement heartbeat
836 	 * so we only log CERR once.
837 	 */
838 	if ((stat & LE_CERR) && le_softc[unit].sc_cerr)
839 		return;
840 	log(LOG_WARNING,
841 	    "le%d: error: stat=%b\n", unit,
842 	    stat,
843 	    "\20\20ERR\17BABL\16CERR\15MISS\14MERR\13RINT\12TINT\11IDON\10INTR\07INEA\06RXON\05TXON\04TDMD\03STOP\02STRT\01INIT");
844 }
845 
846 lererror(unit, msg)
847 	int unit;
848 	char *msg;
849 {
850 	register struct le_softc *le = &le_softc[unit];
851 	register struct lermd *rmd;
852 	int len;
853 
854 	if (!ledebug)
855 		return;
856 
857 	rmd = &le->sc_r2->ler2_rmd[le->sc_rmd];
858 	len = rmd->rmd3;
859 	log(LOG_WARNING,
860 	    "le%d: ierror(%s): from %s: buf=%d, len=%d, rmd1=%b\n",
861 	    unit, msg,
862 	    len > 11 ?
863 		ether_sprintf((u_char *)&le->sc_r2->ler2_rbuf[le->sc_rmd][6]) :
864 		"unknown",
865 	    le->sc_rmd, len,
866 	    rmd->rmd1,
867 	    "\20\20OWN\17ERR\16FRAM\15OFLO\14CRC\13RBUF\12STP\11ENP");
868 }
869 
870 lexerror(unit)
871 	int unit;
872 {
873 	register struct le_softc *le = &le_softc[unit];
874 	register struct letmd *tmd;
875 	int len;
876 
877 	if (!ledebug)
878 		return;
879 
880 	tmd = le->sc_r2->ler2_tmd;
881 	len = -tmd->tmd2;
882 	log(LOG_WARNING,
883 	    "le%d: oerror: to %s: buf=%d, len=%d, tmd1=%b, tmd3=%b\n",
884 	    unit,
885 	    len > 5 ?
886 		ether_sprintf((u_char *)&le->sc_r2->ler2_tbuf[0][0]) :
887 		"unknown",
888 	    0, len,
889 	    tmd->tmd1,
890 	    "\20\20OWN\17ERR\16RES\15MORE\14ONE\13DEF\12STP\11ENP",
891 	    tmd->tmd3,
892 	    "\20\20BUFF\17UFLO\16RES\15LCOL\14LCAR\13RTRY");
893 }
894 #endif
895