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