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