xref: /original-bsd/sys/pmax/dev/if_le.c (revision 4ba124f7)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Ralph Campbell and Rick Macklem.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)if_le.c	8.3 (Berkeley) 07/03/94
11  */
12 
13 #include <le.h>
14 #if NLE > 0
15 
16 #include <bpfilter.h>
17 
18 /*
19  * AMD 7990 LANCE
20  *
21  * This driver will generate and accept trailer encapsulated packets even
22  * though it buys us nothing.  The motivation was to avoid incompatibilities
23  * with VAXen, SUNs, and others that handle and benefit from them.
24  * This reasoning is dubious.
25  */
26 #include <sys/param.h>
27 #include <sys/proc.h>
28 #include <sys/systm.h>
29 #include <sys/mbuf.h>
30 #include <sys/buf.h>
31 #include <sys/protosw.h>
32 #include <sys/socket.h>
33 #include <sys/syslog.h>
34 #include <sys/ioctl.h>
35 #include <sys/errno.h>
36 
37 #include <net/if.h>
38 #include <net/netisr.h>
39 #include <net/route.h>
40 
41 #ifdef INET
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/in_var.h>
45 #include <netinet/ip.h>
46 #include <netinet/if_ether.h>
47 #endif
48 
49 #ifdef NS
50 #include <netns/ns.h>
51 #include <netns/ns_if.h>
52 #endif
53 
54 #if defined (CCITT) && defined (LLC)
55 #include <sys/socketvar.h>
56 #include <netccitt/x25.h>
57 extern llc_ctlinput(), cons_rtrequest();
58 #endif
59 
60 #include <machine/machConst.h>
61 
62 #include <pmax/pmax/pmaxtype.h>
63 #include <pmax/pmax/kn01.h>
64 #include <pmax/pmax/kmin.h>
65 #include <pmax/pmax/asic.h>
66 
67 #include <pmax/dev/device.h>
68 #include <pmax/dev/if_lereg.h>
69 
70 #if NBPFILTER > 0
71 #include <net/bpf.h>
72 #include <net/bpfdesc.h>
73 #endif
74 
75 int	leprobe();
76 void	leintr();
77 struct	driver ledriver = {
78 	"le", leprobe, 0, 0, leintr,
79 };
80 
81 int	ledebug = 0;		/* console error messages */
82 
83 /*
84  * Ethernet software status per interface.
85  *
86  * Each interface is referenced by a network interface structure,
87  * le_if, which the routing code uses to locate the interface.
88  * This structure contains the output queue for the interface, its address, ...
89  */
90 struct	le_softc {
91 	struct	arpcom sc_ac;	/* common Ethernet structures */
92 #define	sc_if	sc_ac.ac_if	/* network-visible interface */
93 #define	sc_addr	sc_ac.ac_enaddr	/* hardware Ethernet address */
94 	volatile struct	lereg1 *sc_r1;	/* LANCE registers */
95 	volatile void *sc_r2;	/* dual-port RAM */
96 	int	sc_ler2pad;	/* Do ring descriptors require short pads? */
97 	void	(*sc_copytobuf)(); /* Copy to buffer */
98 	void	(*sc_copyfrombuf)(); /* Copy from buffer */
99 	void	(*sc_zerobuf)(); /* and Zero bytes in buffer */
100 	int	sc_rmd;		/* predicted next rmd to process */
101 	int	sc_tmd;		/* last tmd processed */
102 	int	sc_tmdnext;	/* next tmd to transmit with */
103 	/* stats */
104 	int	sc_runt;
105 	int	sc_merr;
106 	int	sc_babl;
107 	int	sc_cerr;
108 	int	sc_miss;
109 	int	sc_rown;
110 	int	sc_xint;
111 	int	sc_uflo;
112 	int	sc_rxlen;
113 	int	sc_rxoff;
114 	int	sc_txoff;
115 	int	sc_busy;
116 	short	sc_iflags;
117 } le_softc[NLE];
118 
119 /* access LANCE registers */
120 static void lewritereg();
121 #define	LERDWR(cntl, src, dst)	{ (dst) = (src); DELAY(10); }
122 #define	LEWREG(src, dst)	lewritereg(&(dst), (src))
123 
124 #define CPU_TO_CHIP_ADDR(cpu) \
125 	((unsigned)(&(((struct lereg2 *)0)->cpu)))
126 
127 #define LE_OFFSET_RAM		0x0
128 #define LE_OFFSET_LANCE		0x100000
129 #define LE_OFFSET_ROM		0x1c0000
130 
131 void copytobuf_contig(), copyfrombuf_contig(), bzerobuf_contig();
132 void copytobuf_gap2(), copyfrombuf_gap2(), bzerobuf_gap2();
133 void copytobuf_gap16(), copyfrombuf_gap16(), bzerobuf_gap16();
134 
135 extern int pmax_boardtype;
136 extern u_long le_iomem;
137 extern u_long asic_base;
138 
139 /*
140  * Test to see if device is present.
141  * Return true if found and initialized ok.
142  * If interface exists, make available by filling in network interface
143  * record.  System will initialize the interface when it is ready
144  * to accept packets.
145  */
146 leprobe(dp)
147 	struct pmax_ctlr *dp;
148 {
149 	volatile struct lereg1 *ler1;
150 	struct le_softc *le = &le_softc[dp->pmax_unit];
151 	struct ifnet *ifp = &le->sc_if;
152 	u_char *cp;
153 	int i;
154 	extern int leinit(), lereset(), leioctl(), lestart(), ether_output();
155 
156 	switch (pmax_boardtype) {
157 	case DS_PMAX:
158 		le->sc_r1 = ler1 = (volatile struct lereg1 *)dp->pmax_addr;
159 		le->sc_r2 = (volatile void *)MACH_PHYS_TO_UNCACHED(0x19000000);
160 		cp = (u_char *)(MACH_PHYS_TO_UNCACHED(KN01_SYS_CLOCK) + 1);
161 		le->sc_ler2pad = 1;
162 		le->sc_copytobuf = copytobuf_gap2;
163 		le->sc_copyfrombuf = copyfrombuf_gap2;
164 		le->sc_zerobuf = bzerobuf_gap2;
165 		break;
166 	case DS_3MIN:
167 	case DS_MAXINE:
168 	case DS_3MAXPLUS:
169 		if (dp->pmax_unit == 0) {
170 			volatile u_int *ssr, *ldp;
171 
172 			le->sc_r1 = ler1 = (volatile struct lereg1 *)
173 				ASIC_SYS_LANCE(asic_base);
174 			cp = (u_char *)ASIC_SYS_ETHER_ADDRESS(asic_base);
175 			le->sc_r2 = (volatile void *)
176 				MACH_PHYS_TO_UNCACHED(le_iomem);
177 			le->sc_ler2pad = 1;
178 			le->sc_copytobuf = copytobuf_gap16;
179 			le->sc_copyfrombuf = copyfrombuf_gap16;
180 			le->sc_zerobuf = bzerobuf_gap16;
181 
182 			/*
183 			 * And enable Lance dma through the asic.
184 			 */
185 			ssr = (volatile u_int *)ASIC_REG_CSR(asic_base);
186 			ldp = (volatile u_int *)
187 				ASIC_REG_LANCE_DMAPTR(asic_base);
188 			*ldp = (le_iomem << 3);	/* phys addr << 3 */
189 			*ssr |= ASIC_CSR_DMAEN_LANCE;
190 			break;
191 		}
192 		/*
193 		 * Units other than 0 are turbochannel option boards and fall
194 		 * through to DS_3MAX.
195 		 */
196 	case DS_3MAX:
197 		le->sc_r1 = ler1 = (volatile struct lereg1 *)
198 			(dp->pmax_addr + LE_OFFSET_LANCE);
199 		le->sc_r2 = (volatile void *)(dp->pmax_addr + LE_OFFSET_RAM);
200 		cp = (u_char *)(dp->pmax_addr + LE_OFFSET_ROM + 2);
201 		le->sc_ler2pad = 0;
202 		le->sc_copytobuf = copytobuf_contig;
203 		le->sc_copyfrombuf = copyfrombuf_contig;
204 		le->sc_zerobuf = bzerobuf_contig;
205 		break;
206 	default:
207 		printf("Unknown CPU board type %d\n", pmax_boardtype);
208 		return (0);
209 	};
210 
211 	/*
212 	 * Get the ethernet address out of rom
213 	 */
214 	for (i = 0; i < sizeof(le->sc_addr); i++) {
215 		le->sc_addr[i] = *cp;
216 		cp += 4;
217 	}
218 
219 	/* make sure the chip is stopped */
220 	LEWREG(LE_CSR0, ler1->ler1_rap);
221 	LEWREG(LE_STOP, ler1->ler1_rdp);
222 
223 	ifp->if_unit = dp->pmax_unit;
224 	ifp->if_name = "le";
225 	ifp->if_mtu = ETHERMTU;
226 	ifp->if_init = leinit;
227 	ifp->if_reset = lereset;
228 	ifp->if_ioctl = leioctl;
229 	ifp->if_output = ether_output;
230 	ifp->if_start = lestart;
231 #ifdef MULTICAST
232 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
233 #else
234 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
235 #endif
236 #if NBPFILTER > 0
237 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
238 #endif
239 	if_attach(ifp);
240 
241 	printf("le%d at nexus0 csr 0x%x priority %d ethernet address %s\n",
242 		dp->pmax_unit, dp->pmax_addr, dp->pmax_pri,
243 		ether_sprintf(le->sc_addr));
244 	return (1);
245 }
246 
247 #ifdef MULTICAST
248 /*
249  * Setup the logical address filter
250  */
251 void
252 lesetladrf(le)
253 	register struct le_softc *le;
254 {
255 	register volatile struct lereg2 *ler2 = le->sc_r2;
256 	register struct ifnet *ifp = &le->sc_if;
257 	register struct ether_multi *enm;
258 	register u_char *cp;
259 	register u_long crc;
260 	register u_long c;
261 	register int i, len;
262 	struct ether_multistep step;
263 
264 	/*
265 	 * Set up multicast address filter by passing all multicast
266 	 * addresses through a crc generator, and then using the high
267 	 * order 6 bits as an index into the 64 bit logical address
268 	 * filter. The high order two bits select the word, while the
269 	 * rest of the bits select the bit within the word.
270 	 */
271 
272 	LER2_ladrf0(ler2, 0);
273 	LER2_ladrf1(ler2, 0);
274 	ifp->if_flags &= ~IFF_ALLMULTI;
275 	ETHER_FIRST_MULTI(step, &le->sc_ac, enm);
276 	while (enm != NULL) {
277 		if (bcmp((caddr_t)&enm->enm_addrlo,
278 		    (caddr_t)&enm->enm_addrhi, sizeof(enm->enm_addrlo)) == 0) {
279 			/*
280 			 * We must listen to a range of multicast
281 			 * addresses. For now, just accept all
282 			 * multicasts, rather than trying to set only
283 			 * those filter bits needed to match the range.
284 			 * (At this time, the only use of address
285 			 * ranges is for IP multicast routing, for
286 			 * which the range is big enough to require all
287 			 * bits set.)
288 			 */
289 			LER2_ladrf0(ler2, 0xff);
290 			LER2_ladrf1(ler2, 0xff);
291 			LER2_ladrf2(ler2, 0xff);
292 			LER2_ladrf3(ler2, 0xff);
293 			ifp->if_flags |= IFF_ALLMULTI;
294 			return;
295 		}
296 
297 		cp = (unsigned char *)&enm->enm_addrlo;
298 		c = *cp;
299 		crc = 0xffffffff;
300 		len = 6;
301 		while (len-- > 0) {
302 			c = *cp;
303 			for (i = 0; i < 8; i++) {
304 				if ((c & 0x01) ^ (crc & 0x01)) {
305 					crc >>= 1;
306 					crc = crc ^ 0xedb88320;
307 				}
308 				else
309 					crc >>= 1;
310 				c >>= 1;
311 			}
312 			cp++;
313 		}
314 		/* Just want the 6 most significant bits. */
315 		crc = crc >> 26;
316 
317 		/* Turn on the corresponding bit in the filter. */
318 		switch (crc >> 5) {
319 		case 0:
320 			LER2_ladrf0(ler2, 1 << (crc & 0x1f));
321 			break;
322 		case 1:
323 			LER2_ladrf1(ler2, 1 << (crc & 0x1f));
324 			break;
325 		case 2:
326 			LER2_ladrf2(ler2, 1 << (crc & 0x1f));
327 			break;
328 		case 3:
329 			LER2_ladrf3(ler2, 1 << (crc & 0x1f));
330 		}
331 
332 		ETHER_NEXT_MULTI(step, enm);
333 	}
334 }
335 #endif
336 
337 ledrinit(le)
338 	struct le_softc *le;
339 {
340 	register volatile void *rp;
341 	register int i;
342 
343 	for (i = 0; i < LERBUF; i++) {
344 		rp = LER2_RMDADDR(le->sc_r2, i);
345 		LER2_rmd0(rp, CPU_TO_CHIP_ADDR(ler2_rbuf[i][0]));
346 		LER2_rmd1(rp, LE_OWN);
347 		LER2_rmd2(rp, -LEMTU);
348 		LER2_rmd3(rp, 0);
349 	}
350 	for (i = 0; i < LETBUF; i++) {
351 		rp = LER2_TMDADDR(le->sc_r2, i);
352 		LER2_tmd0(rp, CPU_TO_CHIP_ADDR(ler2_tbuf[i][0]));
353 		LER2_tmd1(rp, 0);
354 		LER2_tmd2(rp, 0);
355 		LER2_tmd3(rp, 0);
356 	}
357 }
358 
359 lereset(unit)
360 	register int unit;
361 {
362 	register struct le_softc *le = &le_softc[unit];
363 	register volatile struct lereg1 *ler1 = le->sc_r1;
364 	register volatile void *ler2 = le->sc_r2;
365 	register int timo = 100000;
366 	register int stat;
367 
368 #ifdef lint
369 	stat = unit;
370 #endif
371 	LEWREG(LE_CSR0, ler1->ler1_rap);
372 	LEWREG(LE_STOP, ler1->ler1_rdp);
373 
374 	/*
375 	 * Setup for transmit/receive
376 	 */
377 #if NBPFILTER > 0
378 	if (le->sc_if.if_flags & IFF_PROMISC)
379 		/* set the promiscuous bit */
380 		LER2_mode(ler2, LE_MODE | 0x8000);
381 	else
382 #endif
383 		LER2_mode(ler2, LE_MODE);
384 	LER2_padr0(ler2, (le->sc_addr[1] << 8) | le->sc_addr[0]);
385 	LER2_padr1(ler2, (le->sc_addr[3] << 8) | le->sc_addr[2]);
386 	LER2_padr2(ler2, (le->sc_addr[5] << 8) | le->sc_addr[4]);
387 	/* Setup the logical address filter */
388 #ifdef MULTICAST
389 	lesetladrf(le);
390 #else
391 	LER2_ladrf0(ler2, 0);
392 	LER2_ladrf1(ler2, 0);
393 	LER2_ladrf2(ler2, 0);
394 	LER2_ladrf3(ler2, 0);
395 #endif
396 	LER2_rlen(ler2, LE_RLEN);
397 	LER2_rdra(ler2, CPU_TO_CHIP_ADDR(ler2_rmd[0]));
398 	LER2_tlen(ler2, LE_TLEN);
399 	LER2_tdra(ler2, CPU_TO_CHIP_ADDR(ler2_tmd[0]));
400 	ledrinit(le);
401 	le->sc_rmd = 0;
402 	le->sc_tmd = LETBUF - 1;
403 	le->sc_tmdnext = 0;
404 
405 	LEWREG(LE_CSR1, ler1->ler1_rap);
406 	LEWREG(CPU_TO_CHIP_ADDR(ler2_mode), ler1->ler1_rdp);
407 	LEWREG(LE_CSR2, ler1->ler1_rap);
408 	LEWREG(0, ler1->ler1_rdp);
409 	LEWREG(LE_CSR3, ler1->ler1_rap);
410 	LEWREG(0, ler1->ler1_rdp);
411 	LEWREG(LE_CSR0, ler1->ler1_rap);
412 	LERDWR(ler0, LE_INIT, ler1->ler1_rdp);
413 	do {
414 		if (--timo == 0) {
415 			printf("le%d: init timeout, stat = 0x%x\n",
416 			       unit, stat);
417 			break;
418 		}
419 		stat = ler1->ler1_rdp;
420 	} while ((stat & LE_IDON) == 0);
421 	LERDWR(ler0, LE_IDON, ler1->ler1_rdp);
422 	LERDWR(ler0, LE_STRT | LE_INEA, ler1->ler1_rdp);
423 	le->sc_if.if_flags &= ~IFF_OACTIVE;
424 }
425 
426 /*
427  * Initialization of interface
428  */
429 leinit(unit)
430 	int unit;
431 {
432 	register struct ifnet *ifp = &le_softc[unit].sc_if;
433 	register struct ifaddr *ifa;
434 	int s;
435 
436 	/* not yet, if address still unknown */
437 	for (ifa = ifp->if_addrlist;; ifa = ifa->ifa_next)
438 		if (ifa == 0)
439 			return;
440 		else if (ifa->ifa_addr && ifa->ifa_addr->sa_family != AF_LINK)
441 			break;
442 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
443 		s = splnet();
444 		ifp->if_flags |= IFF_RUNNING;
445 		lereset(unit);
446 	        (void) lestart(ifp);
447 		splx(s);
448 	}
449 }
450 
451 #define	LENEXTTMP \
452 	if (++bix == LETBUF) \
453 		bix = 0; \
454 	tmd = LER2_TMDADDR(le->sc_r2, bix)
455 
456 /*
457  * Start output on interface.  Get another datagram to send
458  * off of the interface queue, and copy it to the interface
459  * before starting the output.
460  */
461 lestart(ifp)
462 	struct ifnet *ifp;
463 {
464 	register struct le_softc *le = &le_softc[ifp->if_unit];
465 	register int bix = le->sc_tmdnext;
466 	register volatile void *tmd = LER2_TMDADDR(le->sc_r2, bix);
467 	register struct mbuf *m;
468 	int len = 0;
469 
470 	if ((le->sc_if.if_flags & IFF_RUNNING) == 0)
471 		return (0);
472 	while (bix != le->sc_tmd) {
473 		if (LER2V_tmd1(tmd) & LE_OWN)
474 			panic("lestart");
475 		IF_DEQUEUE(&le->sc_if.if_snd, m);
476 		if (m == 0)
477 			break;
478 #if NBPFILTER > 0
479 		/*
480 		 * If bpf is listening on this interface, let it
481 		 * see the packet before we commit it to the wire.
482 		 */
483 		if (ifp->if_bpf)
484 			bpf_mtap(ifp->if_bpf, m);
485 #endif
486 		len = leput(le, LER2_TBUFADDR(le->sc_r2, bix), m);
487 		LER2_tmd3(tmd, 0);
488 		LER2_tmd2(tmd, -len);
489 		LER2_tmd1(tmd, LE_OWN | LE_STP | LE_ENP);
490 		LENEXTTMP;
491 	}
492 	if (len != 0) {
493 		le->sc_if.if_flags |= IFF_OACTIVE;
494 		LERDWR(ler0, LE_TDMD | LE_INEA, le->sc_r1->ler1_rdp);
495 	}
496 	le->sc_tmdnext = bix;
497 	return (0);
498 }
499 
500 /*
501  * Process interrupts from the 7990 chip.
502  */
503 void
504 leintr(unit)
505 	int unit;
506 {
507 	register struct le_softc *le;
508 	register volatile struct lereg1 *ler1;
509 	register int stat;
510 
511 	le = &le_softc[unit];
512 	ler1 = le->sc_r1;
513 	stat = ler1->ler1_rdp;
514 	if (!(stat & LE_INTR)) {
515 		printf("le%d: spurrious interrupt\n", unit);
516 		return;
517 	}
518 	if (stat & LE_SERR) {
519 		leerror(unit, stat);
520 		if (stat & LE_MERR) {
521 			le->sc_merr++;
522 			lereset(unit);
523 			return;
524 		}
525 		if (stat & LE_BABL)
526 			le->sc_babl++;
527 		if (stat & LE_CERR)
528 			le->sc_cerr++;
529 		if (stat & LE_MISS)
530 			le->sc_miss++;
531 		LERDWR(ler0, LE_BABL|LE_CERR|LE_MISS|LE_INEA, ler1->ler1_rdp);
532 	}
533 	if ((stat & LE_RXON) == 0) {
534 		le->sc_rxoff++;
535 		lereset(unit);
536 		return;
537 	}
538 	if ((stat & LE_TXON) == 0) {
539 		le->sc_txoff++;
540 		lereset(unit);
541 		return;
542 	}
543 	if (stat & LE_RINT) {
544 		/* interrupt is cleared in lerint */
545 		lerint(unit);
546 	}
547 	if (stat & LE_TINT) {
548 		LERDWR(ler0, LE_TINT|LE_INEA, ler1->ler1_rdp);
549 		lexint(unit);
550 	}
551 }
552 
553 /*
554  * Ethernet interface transmitter interrupt.
555  * Start another output if more data to send.
556  */
557 lexint(unit)
558 	register int unit;
559 {
560 	register struct le_softc *le = &le_softc[unit];
561 	register int bix = le->sc_tmd;
562 	register volatile void *tmd;
563 
564 	if ((le->sc_if.if_flags & IFF_OACTIVE) == 0) {
565 		le->sc_xint++;
566 		return;
567 	}
568 	LENEXTTMP;
569 	while (bix != le->sc_tmdnext && (LER2V_tmd1(tmd) & LE_OWN) == 0) {
570 		le->sc_tmd = bix;
571 		if ((LER2V_tmd1(tmd) & LE_ERR) || (LER2V_tmd3(tmd) & LE_TBUFF)) {
572 			lexerror(unit);
573 			le->sc_if.if_oerrors++;
574 			if (LER2V_tmd3(tmd) & (LE_TBUFF|LE_UFLO)) {
575 				le->sc_uflo++;
576 				lereset(unit);
577 				break;
578 			}
579 			else if (LER2V_tmd3(tmd) & LE_LCOL)
580 				le->sc_if.if_collisions++;
581 			else if (LER2V_tmd3(tmd) & LE_RTRY)
582 				le->sc_if.if_collisions += 16;
583 		}
584 		else if (LER2V_tmd1(tmd) & LE_ONE)
585 			le->sc_if.if_collisions++;
586 		else if (LER2V_tmd1(tmd) & LE_MORE)
587 			/* what is the real number? */
588 			le->sc_if.if_collisions += 2;
589 		else
590 			le->sc_if.if_opackets++;
591 		LENEXTTMP;
592 	}
593 	if (bix == le->sc_tmdnext)
594 		le->sc_if.if_flags &= ~IFF_OACTIVE;
595 	(void) lestart(&le->sc_if);
596 }
597 
598 #define	LENEXTRMP \
599 	if (++bix == LERBUF) \
600 		bix = 0; \
601 	rmd = LER2_RMDADDR(le->sc_r2, bix)
602 
603 /*
604  * Ethernet interface receiver interrupt.
605  * If input error just drop packet.
606  * Decapsulate packet based on type and pass to type specific
607  * higher-level input routine.
608  */
609 lerint(unit)
610 	int unit;
611 {
612 	register struct le_softc *le = &le_softc[unit];
613 	register int bix = le->sc_rmd;
614 	register volatile void *rmd = LER2_RMDADDR(le->sc_r2, bix);
615 
616 	/*
617 	 * Out of sync with hardware, should never happen?
618 	 */
619 	if (LER2V_rmd1(rmd) & LE_OWN) {
620 		le->sc_rown++;
621 		LERDWR(le->sc_r0, LE_RINT|LE_INEA, le->sc_r1->ler1_rdp);
622 		return;
623 	}
624 
625 	/*
626 	 * Process all buffers with valid data
627 	 */
628 	while ((LER2V_rmd1(rmd) & LE_OWN) == 0) {
629 		int len = LER2V_rmd3(rmd);
630 
631 		/* Clear interrupt to avoid race condition */
632 		LERDWR(le->sc_r0, LE_RINT|LE_INEA, le->sc_r1->ler1_rdp);
633 
634 		if (LER2V_rmd1(rmd) & LE_ERR) {
635 			le->sc_rmd = bix;
636 			lererror(unit, "bad packet");
637 			le->sc_if.if_ierrors++;
638 		} else if ((LER2V_rmd1(rmd) & (LE_STP|LE_ENP)) != (LE_STP|LE_ENP)) {
639 			/*
640 			 * Find the end of the packet so we can see how long
641 			 * it was.  We still throw it away.
642 			 */
643 			do {
644 				LERDWR(le->sc_r0, LE_RINT|LE_INEA,
645 				       le->sc_r1->ler1_rdp);
646 				LER2_rmd3(rmd, 0);
647 				LER2_rmd1(rmd, LE_OWN);
648 				LENEXTRMP;
649 			} while (!(LER2V_rmd1(rmd) & (LE_OWN|LE_ERR|LE_STP|LE_ENP)));
650 			le->sc_rmd = bix;
651 			lererror(unit, "chained buffer");
652 			le->sc_rxlen++;
653 			/*
654 			 * If search terminated without successful completion
655 			 * we reset the hardware (conservative).
656 			 */
657 			if ((LER2V_rmd1(rmd) & (LE_OWN|LE_ERR|LE_STP|LE_ENP)) !=
658 			    LE_ENP) {
659 				lereset(unit);
660 				return;
661 			}
662 		} else
663 			leread(unit, LER2_RBUFADDR(le->sc_r2, bix), len);
664 		LER2_rmd3(rmd, 0);
665 		LER2_rmd1(rmd, LE_OWN);
666 		LENEXTRMP;
667 	}
668 	MachEmptyWriteBuffer();		/* Paranoia */
669 	le->sc_rmd = bix;
670 }
671 
672 /*
673  * Look at the packet in network buffer memory so we can be smart about how
674  * we copy the data into mbufs.
675  * This needs work since we can't just read network buffer memory like
676  * regular memory.
677  */
678 leread(unit, buf, len)
679 	int unit;
680 	volatile void *buf;
681 	int len;
682 {
683 	register struct le_softc *le = &le_softc[unit];
684 	struct ether_header et;
685     	struct mbuf *m;
686 	int off, resid, flags;
687 	u_short sbuf[2], eth_type;
688 	extern struct mbuf *leget();
689 
690 	le->sc_if.if_ipackets++;
691 	(*le->sc_copyfrombuf)(buf, 0, (char *)&et, sizeof (et));
692 	eth_type = ntohs(et.ether_type);
693 	/* adjust input length to account for header and CRC */
694 	len = len - sizeof(struct ether_header) - 4;
695 
696 	if (eth_type >= ETHERTYPE_TRAIL &&
697 	    eth_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
698 		off = (eth_type - ETHERTYPE_TRAIL) * 512;
699 		if (off >= ETHERMTU)
700 			return;		/* sanity */
701 		(*le->sc_copyfrombuf)(buf, sizeof (et) + off, (char *)sbuf,
702 			sizeof (sbuf));
703 		eth_type = ntohs(sbuf[0]);
704 		resid = ntohs(sbuf[1]);
705 		if (off + resid > len)
706 			return;		/* sanity */
707 		len = off + resid;
708 	} else
709 		off = 0;
710 
711 	if (len <= 0) {
712 		if (ledebug)
713 			log(LOG_WARNING,
714 			    "le%d: ierror(runt packet): from %s: len=%d\n",
715 			    unit, ether_sprintf(et.ether_shost), len);
716 		le->sc_runt++;
717 		le->sc_if.if_ierrors++;
718 		return;
719 	}
720 	flags = 0;
721 	if (bcmp((caddr_t)etherbroadcastaddr,
722 	    (caddr_t)et.ether_dhost, sizeof(etherbroadcastaddr)) == 0)
723 		flags |= M_BCAST;
724 	if (et.ether_dhost[0] & 1)
725 		flags |= M_MCAST;
726 
727 	/*
728 	 * Pull packet off interface.  Off is nonzero if packet
729 	 * has trailing header; leget will then force this header
730 	 * information to be at the front, but we still have to drop
731 	 * the type and length which are at the front of any trailer data.
732 	 */
733 	m = leget(le, buf, len, off, &le->sc_if);
734 	if (m == 0)
735 		return;
736 #if NBPFILTER > 0
737 	/*
738 	 * Check if there's a bpf filter listening on this interface.
739 	 * If so, hand off the raw packet to enet.
740 	 */
741 	if (le->sc_if.if_bpf) {
742 		bpf_mtap(le->sc_if.if_bpf, m);
743 
744 		/*
745 		 * Keep the packet if it's a broadcast or has our
746 		 * physical ethernet address (or if we support
747 		 * multicast and it's one).
748 		 */
749 		if (
750 #ifdef MULTICAST
751 		    (flags & (M_BCAST | M_MCAST)) == 0 &&
752 #else
753 		    (flags & M_BCAST) == 0 &&
754 #endif
755 		    bcmp(et.ether_dhost, le->sc_addr,
756 			sizeof(et.ether_dhost)) != 0) {
757 			m_freem(m);
758 			return;
759 		}
760 	}
761 #endif
762 	m->m_flags |= flags;
763 	et.ether_type = eth_type;
764 	ether_input(&le->sc_if, &et, m);
765 }
766 
767 /*
768  * Routine to copy from mbuf chain to transmit buffer in
769  * network buffer memory.
770  */
771 leput(le, lebuf, m)
772 	struct le_softc *le;
773 	register volatile void *lebuf;
774 	register struct mbuf *m;
775 {
776 	register struct mbuf *mp;
777 	register int len, tlen = 0;
778 	register int boff = 0;
779 
780 	for (mp = m; mp; mp = mp->m_next) {
781 		len = mp->m_len;
782 		if (len == 0)
783 			continue;
784 		(*le->sc_copytobuf)(mtod(mp, char *), lebuf, boff, len);
785 		tlen += len;
786 		boff += len;
787 	}
788 	m_freem(m);
789 	if (tlen < LEMINSIZE) {
790 		(*le->sc_zerobuf)(lebuf, boff, LEMINSIZE - tlen);
791 		tlen = LEMINSIZE;
792 	}
793 	return(tlen);
794 }
795 
796 /*
797  * Routine to copy from network buffer memory into mbufs.
798  */
799 struct mbuf *
800 leget(le, lebuf, totlen, off, ifp)
801 	struct le_softc *le;
802 	volatile void *lebuf;
803 	int totlen, off;
804 	struct ifnet *ifp;
805 {
806 	register struct mbuf *m;
807 	struct mbuf *top = 0, **mp = &top;
808 	register int len, resid, boff;
809 
810 	/* NOTE: sizeof(struct ether_header) should be even */
811 	boff = sizeof(struct ether_header);
812 	if (off) {
813 		/* NOTE: off should be even */
814 		boff += off + 2 * sizeof(u_short);
815 		totlen -= 2 * sizeof(u_short);
816 		resid = totlen - off;
817 	} else
818 		resid = totlen;
819 
820 	MGETHDR(m, M_DONTWAIT, MT_DATA);
821 	if (m == 0)
822 		return (0);
823 	m->m_pkthdr.rcvif = ifp;
824 	m->m_pkthdr.len = totlen;
825 	m->m_len = MHLEN;
826 
827 	while (totlen > 0) {
828 		if (top) {
829 			MGET(m, M_DONTWAIT, MT_DATA);
830 			if (m == 0) {
831 				m_freem(top);
832 				return (0);
833 			}
834 			m->m_len = MLEN;
835 		}
836 
837 		if (resid >= MINCLSIZE)
838 			MCLGET(m, M_DONTWAIT);
839 		if (m->m_flags & M_EXT)
840 			m->m_len = min(resid, MCLBYTES);
841 		else if (resid < m->m_len) {
842 			/*
843 			 * Place initial small packet/header at end of mbuf.
844 			 */
845 			if (top == 0 && resid + max_linkhdr <= m->m_len)
846 				m->m_data += max_linkhdr;
847 			m->m_len = resid;
848 		}
849 		len = m->m_len;
850 		(*le->sc_copyfrombuf)(lebuf, boff, mtod(m, char *), len);
851 		boff += len;
852 		*mp = m;
853 		mp = &m->m_next;
854 		totlen -= len;
855 		resid -= len;
856 		if (resid == 0) {
857 			boff = sizeof (struct ether_header);
858 			resid = totlen;
859 		}
860 	}
861 	return (top);
862 }
863 
864 /*
865  * Process an ioctl request.
866  */
867 leioctl(ifp, cmd, data)
868 	register struct ifnet *ifp;
869 	int cmd;
870 	caddr_t data;
871 {
872 	register struct ifaddr *ifa = (struct ifaddr *)data;
873 	struct le_softc *le = &le_softc[ifp->if_unit];
874 	volatile struct lereg1 *ler1 = le->sc_r1;
875 	int s, error = 0;
876 
877 	s = splnet();
878 	switch (cmd) {
879 
880 	case SIOCSIFADDR:
881 		ifp->if_flags |= IFF_UP;
882 		switch (ifa->ifa_addr->sa_family) {
883 #ifdef INET
884 		case AF_INET:
885 			leinit(ifp->if_unit);	/* before arpwhohas */
886 			((struct arpcom *)ifp)->ac_ipaddr =
887 				IA_SIN(ifa)->sin_addr;
888 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
889 			break;
890 #endif
891 #ifdef NS
892 		case AF_NS:
893 		    {
894 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
895 
896 			if (ns_nullhost(*ina))
897 				ina->x_host = *(union ns_host *)(le->sc_addr);
898 			else {
899 				/*
900 				 * The manual says we can't change the address
901 				 * while the receiver is armed,
902 				 * so reset everything
903 				 */
904 				ifp->if_flags &= ~IFF_RUNNING;
905 				LEWREG(LE_STOP, ler1->ler1_rdp);
906 				bcopy((caddr_t)ina->x_host.c_host,
907 				    (caddr_t)le->sc_addr, sizeof(le->sc_addr));
908 			}
909 			leinit(ifp->if_unit); /* does le_setaddr() */
910 			break;
911 		    }
912 #endif
913 		default:
914 			leinit(ifp->if_unit);
915 			break;
916 		}
917 		break;
918 
919 #if defined (CCITT) && defined (LLC)
920 	case SIOCSIFCONF_X25:
921 		ifp->if_flags |= IFF_UP;
922 		ifa->ifa_rtrequest = cons_rtrequest;
923 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
924 		if (error == 0)
925 			leinit(ifp->if_unit);
926 		break;
927 #endif /* CCITT && LLC */
928 
929 	case SIOCSIFFLAGS:
930 		if ((ifp->if_flags & IFF_UP) == 0 &&
931 		    ifp->if_flags & IFF_RUNNING) {
932 			LEWREG(LE_STOP, ler1->ler1_rdp);
933 			ifp->if_flags &= ~IFF_RUNNING;
934 		} else if (ifp->if_flags & IFF_UP &&
935 		    (ifp->if_flags & IFF_RUNNING) == 0)
936 			leinit(ifp->if_unit);
937 		/*
938 		 * If the state of the promiscuous bit changes, the interface
939 		 * must be reset to effect the change.
940 		 */
941 		if (((ifp->if_flags ^ le->sc_iflags) & IFF_PROMISC) &&
942 		    (ifp->if_flags & IFF_RUNNING)) {
943 			le->sc_iflags = ifp->if_flags;
944 			lereset(ifp->if_unit);
945 			lestart(ifp);
946 		}
947 		break;
948 
949 #ifdef MULTICAST
950 	case SIOCADDMULTI:
951 	case SIOCDELMULTI:
952 		/* Update our multicast list  */
953 		error = (cmd == SIOCADDMULTI) ?
954 		    ether_addmulti((struct ifreq *)data, &le->sc_ac) :
955 		    ether_delmulti((struct ifreq *)data, &le->sc_ac);
956 
957 		if (error == ENETRESET) {
958 			/*
959 			 * Multicast list has changed; set the hardware
960 			 * filter accordingly.
961 			 */
962 			lereset(ifp->if_unit);
963 			error = 0;
964 		}
965 		break;
966 #endif
967 
968 	default:
969 		error = EINVAL;
970 	}
971 	splx(s);
972 	return (error);
973 }
974 
975 leerror(unit, stat)
976 	int unit;
977 	int stat;
978 {
979 	if (!ledebug)
980 		return;
981 
982 	/*
983 	 * Not all transceivers implement heartbeat
984 	 * so we only log CERR once.
985 	 */
986 	if ((stat & LE_CERR) && le_softc[unit].sc_cerr)
987 		return;
988 	log(LOG_WARNING,
989 	    "le%d: error: stat=%b\n", unit,
990 	    stat,
991 	    "\20\20ERR\17BABL\16CERR\15MISS\14MERR\13RINT\12TINT\11IDON\10INTR\07INEA\06RXON\05TXON\04TDMD\03STOP\02STRT\01INIT");
992 }
993 
994 lererror(unit, msg)
995 	int unit;
996 	char *msg;
997 {
998 	register struct le_softc *le = &le_softc[unit];
999 	register volatile void *rmd;
1000 	u_char eaddr[6];
1001 	int len;
1002 
1003 	if (!ledebug)
1004 		return;
1005 
1006 	rmd = LER2_RMDADDR(le->sc_r2, le->sc_rmd);
1007 	len = LER2V_rmd3(rmd);
1008 	if (len > 11)
1009 		(*le->sc_copyfrombuf)(LER2_RBUFADDR(le->sc_r2, le->sc_rmd),
1010 			6, eaddr, 6);
1011 	log(LOG_WARNING,
1012 	    "le%d: ierror(%s): from %s: buf=%d, len=%d, rmd1=%b\n",
1013 	    unit, msg,
1014 	    len > 11 ? ether_sprintf(eaddr) : "unknown",
1015 	    le->sc_rmd, len,
1016 	    LER2V_rmd1(rmd),
1017 	    "\20\20OWN\17ERR\16FRAM\15OFLO\14CRC\13RBUF\12STP\11ENP");
1018 }
1019 
1020 lexerror(unit)
1021 	int unit;
1022 {
1023 	register struct le_softc *le = &le_softc[unit];
1024 	register volatile void *tmd;
1025 	u_char eaddr[6];
1026 	int len;
1027 
1028 	if (!ledebug)
1029 		return;
1030 
1031 	tmd = LER2_TMDADDR(le->sc_r2, 0);
1032 	len = -LER2V_tmd2(tmd);
1033 	if (len > 5)
1034 		(*le->sc_copyfrombuf)(LER2_TBUFADDR(le->sc_r2, 0), 0, eaddr, 6);
1035 	log(LOG_WARNING,
1036 	    "le%d: oerror: to %s: buf=%d, len=%d, tmd1=%b, tmd3=%b\n",
1037 	    unit,
1038 	    len > 5 ? ether_sprintf(eaddr) : "unknown",
1039 	    0, len,
1040 	    LER2V_tmd1(tmd),
1041 	    "\20\20OWN\17ERR\16RES\15MORE\14ONE\13DEF\12STP\11ENP",
1042 	    LER2V_tmd3(tmd),
1043 	    "\20\20BUFF\17UFLO\16RES\15LCOL\14LCAR\13RTRY");
1044 }
1045 
1046 /*
1047  * Write a lance register port, reading it back to ensure success. This seems
1048  * to be necessary during initialization, since the chip appears to be a bit
1049  * pokey sometimes.
1050  */
1051 static void
1052 lewritereg(regptr, val)
1053 	register volatile u_short *regptr;
1054 	register u_short val;
1055 {
1056 	register int i = 0;
1057 
1058 	while (*regptr != val) {
1059 		*regptr = val;
1060 		MachEmptyWriteBuffer();
1061 		if (++i > 10000) {
1062 			printf("le: Reg did not settle (to x%x): x%x\n",
1063 			       val, *regptr);
1064 			return;
1065 		}
1066 		DELAY(100);
1067 	}
1068 }
1069 
1070 /*
1071  * Routines for accessing the transmit and receive buffers. Unfortunately,
1072  * CPU addressing of these buffers is done in one of 3 ways:
1073  * - contiguous (for the 3max and turbochannel option card)
1074  * - gap2, which means shorts (2 bytes) interspersed with short (2 byte)
1075  *   spaces (for the pmax)
1076  * - gap16, which means 16bytes interspersed with 16byte spaces
1077  *   for buffers which must begin on a 32byte boundary (for 3min and maxine)
1078  * The buffer offset is the logical byte offset, assuming contiguous storage.
1079  */
1080 void
1081 copytobuf_contig(from, lebuf, boff, len)
1082 	char *from;
1083 	volatile void *lebuf;
1084 	int boff;
1085 	int len;
1086 {
1087 
1088 	/*
1089 	 * Just call bcopy() to do the work.
1090 	 */
1091 	bcopy(from, ((char *)lebuf) + boff, len);
1092 }
1093 
1094 void
1095 copyfrombuf_contig(lebuf, boff, to, len)
1096 	volatile void *lebuf;
1097 	int boff;
1098 	char *to;
1099 	int len;
1100 {
1101 
1102 	/*
1103 	 * Just call bcopy() to do the work.
1104 	 */
1105 	bcopy(((char *)lebuf) + boff, to, len);
1106 }
1107 
1108 void
1109 bzerobuf_contig(lebuf, boff, len)
1110 	volatile void *lebuf;
1111 	int boff;
1112 	int len;
1113 {
1114 
1115 	/*
1116 	 * Just let bzero() do the work
1117 	 */
1118 	bzero(((char *)lebuf) + boff, len);
1119 }
1120 
1121 /*
1122  * For the pmax the buffer consists of shorts (2 bytes) interspersed with
1123  * short (2 byte) spaces and must be accessed with halfword load/stores.
1124  * (don't worry about doing an extra byte)
1125  */
1126 void
1127 copytobuf_gap2(from, lebuf, boff, len)
1128 	register char *from;
1129 	volatile void *lebuf;
1130 	int boff;
1131 	register int len;
1132 {
1133 	register volatile u_short *bptr;
1134 	register int xfer;
1135 
1136 	if (boff & 0x1) {
1137 		/* handle unaligned first byte */
1138 		bptr = ((volatile u_short *)lebuf) + (boff - 1);
1139 		*bptr = (*from++ << 8) | (*bptr & 0xff);
1140 		bptr += 2;
1141 		len--;
1142 	} else
1143 		bptr = ((volatile u_short *)lebuf) + boff;
1144 	if ((unsigned)from & 0x1) {
1145 		while (len > 1) {
1146 			*bptr = (from[1] << 8) | (from[0] & 0xff);
1147 			bptr += 2;
1148 			from += 2;
1149 			len -= 2;
1150 		}
1151 	} else {
1152 		/* optimize for aligned transfers */
1153 		xfer = (int)((unsigned)len & ~0x1);
1154 		CopyToBuffer((u_short *)from, bptr, xfer);
1155 		bptr += xfer;
1156 		from += xfer;
1157 		len -= xfer;
1158 	}
1159 	if (len == 1)
1160 		*bptr = (u_short)*from;
1161 }
1162 
1163 void
1164 copyfrombuf_gap2(lebuf, boff, to, len)
1165 	volatile void *lebuf;
1166 	int boff;
1167 	register char *to;
1168 	register int len;
1169 {
1170 	register volatile u_short *bptr;
1171 	register u_short tmp;
1172 	register int xfer;
1173 
1174 	if (boff & 0x1) {
1175 		/* handle unaligned first byte */
1176 		bptr = ((volatile u_short *)lebuf) + (boff - 1);
1177 		*to++ = (*bptr >> 8) & 0xff;
1178 		bptr += 2;
1179 		len--;
1180 	} else
1181 		bptr = ((volatile u_short *)lebuf) + boff;
1182 	if ((unsigned)to & 0x1) {
1183 		while (len > 1) {
1184 			tmp = *bptr;
1185 			*to++ = tmp & 0xff;
1186 			*to++ = (tmp >> 8) & 0xff;
1187 			bptr += 2;
1188 			len -= 2;
1189 		}
1190 	} else {
1191 		/* optimize for aligned transfers */
1192 		xfer = (int)((unsigned)len & ~0x1);
1193 		CopyFromBuffer(bptr, to, xfer);
1194 		bptr += xfer;
1195 		to += xfer;
1196 		len -= xfer;
1197 	}
1198 	if (len == 1)
1199 		*to = *bptr & 0xff;
1200 }
1201 
1202 void
1203 bzerobuf_gap2(lebuf, boff, len)
1204 	volatile void *lebuf;
1205 	int boff;
1206 	int len;
1207 {
1208 	register volatile u_short *bptr;
1209 
1210 	if ((unsigned)boff & 0x1) {
1211 		bptr = ((volatile u_short *)lebuf) + (boff - 1);
1212 		*bptr &= 0xff;
1213 		bptr += 2;
1214 		len--;
1215 	} else
1216 		bptr = ((volatile u_short *)lebuf) + boff;
1217 	while (len > 0) {
1218 		*bptr = 0;
1219 		bptr += 2;
1220 		len -= 2;
1221 	}
1222 }
1223 
1224 /*
1225  * For the 3min and maxine, the buffers are in main memory filled in with
1226  * 16byte blocks interspersed with 16byte spaces.
1227  */
1228 void
1229 copytobuf_gap16(from, lebuf, boff, len)
1230 	register char *from;
1231 	volatile void *lebuf;
1232 	int boff;
1233 	register int len;
1234 {
1235 	register char *bptr;
1236 	register int xfer;
1237 
1238 	bptr = ((char *)lebuf) + ((boff << 1) & ~0x1f);
1239 	boff &= 0xf;
1240 	xfer = min(len, 16 - boff);
1241 	while (len > 0) {
1242 		bcopy(from, ((char *)bptr) + boff, xfer);
1243 		from += xfer;
1244 		bptr += 32;
1245 		boff = 0;
1246 		len -= xfer;
1247 		xfer = min(len, 16);
1248 	}
1249 }
1250 
1251 void
1252 copyfrombuf_gap16(lebuf, boff, to, len)
1253 	volatile void *lebuf;
1254 	int boff;
1255 	register char *to;
1256 	register int len;
1257 {
1258 	register char *bptr;
1259 	register int xfer;
1260 
1261 	bptr = ((char *)lebuf) + ((boff << 1) & ~0x1f);
1262 	boff &= 0xf;
1263 	xfer = min(len, 16 - boff);
1264 	while (len > 0) {
1265 		bcopy(((char *)bptr) + boff, to, xfer);
1266 		to += xfer;
1267 		bptr += 32;
1268 		boff = 0;
1269 		len -= xfer;
1270 		xfer = min(len, 16);
1271 	}
1272 }
1273 
1274 void
1275 bzerobuf_gap16(lebuf, boff, len)
1276 	volatile void *lebuf;
1277 	int boff;
1278 	register int len;
1279 {
1280 	register char *bptr;
1281 	register int xfer;
1282 
1283 	bptr = ((char *)lebuf) + ((boff << 1) & ~0x1f);
1284 	boff &= 0xf;
1285 	xfer = min(len, 16 - boff);
1286 	while (len > 0) {
1287 		bzero(((char *)bptr) + boff, xfer);
1288 		bptr += 32;
1289 		boff = 0;
1290 		len -= xfer;
1291 		xfer = min(len, 16);
1292 	}
1293 }
1294 #endif /* NLE */
1295