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