xref: /original-bsd/sys/pmax/dev/if_le.c (revision f1d75c93)
1 /*-
2  * Copyright (c) 1992 The Regents of the University of California.
3  * 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	7.11 (Berkeley) 05/29/93
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 = 1;		/* 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 a 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 		len = leput(le, LER2_TBUFADDR(le->sc_r2, bix), m);
479 #if NBPFILTER > 0
480 		/*
481 		 * If bpf is listening on this interface, let it
482 		 * see the packet before we commit it to the wire.
483 		 */
484 		if (ifp->if_bpf)
485 			bpf_tap(ifp->if_bpf,
486 				LER2_TBUFADDR(le->sc_r2, le->sc_tmd), len);
487 #endif
488 		LER2_tmd3(tmd, 0);
489 		LER2_tmd2(tmd, -len);
490 		LER2_tmd1(tmd, LE_OWN | LE_STP | LE_ENP);
491 		LENEXTTMP;
492 	}
493 	if (len != 0) {
494 		le->sc_if.if_flags |= IFF_OACTIVE;
495 		LERDWR(ler0, LE_TDMD | LE_INEA, le->sc_r1->ler1_rdp);
496 	}
497 	le->sc_tmdnext = bix;
498 	return (0);
499 }
500 
501 /*
502  * Process interrupts from the 7990 chip.
503  */
504 void
505 leintr(unit)
506 	int unit;
507 {
508 	register struct le_softc *le;
509 	register volatile struct lereg1 *ler1;
510 	register int stat;
511 
512 	le = &le_softc[unit];
513 	ler1 = le->sc_r1;
514 	stat = ler1->ler1_rdp;
515 	if (!(stat & LE_INTR)) {
516 		printf("le%d: spurrious interrupt\n", unit);
517 		return;
518 	}
519 	if (stat & LE_SERR) {
520 		leerror(unit, stat);
521 		if (stat & LE_MERR) {
522 			le->sc_merr++;
523 			lereset(unit);
524 			return;
525 		}
526 		if (stat & LE_BABL)
527 			le->sc_babl++;
528 		if (stat & LE_CERR)
529 			le->sc_cerr++;
530 		if (stat & LE_MISS)
531 			le->sc_miss++;
532 		LERDWR(ler0, LE_BABL|LE_CERR|LE_MISS|LE_INEA, ler1->ler1_rdp);
533 	}
534 	if ((stat & LE_RXON) == 0) {
535 		le->sc_rxoff++;
536 		lereset(unit);
537 		return;
538 	}
539 	if ((stat & LE_TXON) == 0) {
540 		le->sc_txoff++;
541 		lereset(unit);
542 		return;
543 	}
544 	if (stat & LE_RINT) {
545 		/* interrupt is cleared in lerint */
546 		lerint(unit);
547 	}
548 	if (stat & LE_TINT) {
549 		LERDWR(ler0, LE_TINT|LE_INEA, ler1->ler1_rdp);
550 		lexint(unit);
551 	}
552 }
553 
554 /*
555  * Ethernet interface transmitter interrupt.
556  * Start another output if more data to send.
557  */
558 lexint(unit)
559 	register int unit;
560 {
561 	register struct le_softc *le = &le_softc[unit];
562 	register int bix = le->sc_tmd;
563 	register volatile void *tmd;
564 
565 	if ((le->sc_if.if_flags & IFF_OACTIVE) == 0) {
566 		le->sc_xint++;
567 		return;
568 	}
569 	LENEXTTMP;
570 	while (bix != le->sc_tmdnext && (LER2V_tmd1(tmd) & LE_OWN) == 0) {
571 		le->sc_tmd = bix;
572 		if ((LER2V_tmd1(tmd) & LE_ERR) || (LER2V_tmd3(tmd) & LE_TBUFF)) {
573 			lexerror(unit);
574 			le->sc_if.if_oerrors++;
575 			if (LER2V_tmd3(tmd) & (LE_TBUFF|LE_UFLO)) {
576 				le->sc_uflo++;
577 				lereset(unit);
578 				break;
579 			}
580 			else if (LER2V_tmd3(tmd) & LE_LCOL)
581 				le->sc_if.if_collisions++;
582 			else if (LER2V_tmd3(tmd) & LE_RTRY)
583 				le->sc_if.if_collisions += 16;
584 		}
585 		else if (LER2V_tmd1(tmd) & LE_ONE)
586 			le->sc_if.if_collisions++;
587 		else if (LER2V_tmd1(tmd) & LE_MORE)
588 			/* what is the real number? */
589 			le->sc_if.if_collisions += 2;
590 		else
591 			le->sc_if.if_opackets++;
592 		LENEXTTMP;
593 	}
594 	if (bix == le->sc_tmdnext)
595 		le->sc_if.if_flags &= ~IFF_OACTIVE;
596 	(void) lestart(&le->sc_if);
597 }
598 
599 #define	LENEXTRMP \
600 	if (++bix == LERBUF) \
601 		bix = 0; \
602 	rmd = LER2_RMDADDR(le->sc_r2, bix)
603 
604 /*
605  * Ethernet interface receiver interrupt.
606  * If input error just drop packet.
607  * Decapsulate packet based on type and pass to type specific
608  * higher-level input routine.
609  */
610 lerint(unit)
611 	int unit;
612 {
613 	register struct le_softc *le = &le_softc[unit];
614 	register int bix = le->sc_rmd;
615 	register volatile void *rmd = LER2_RMDADDR(le->sc_r2, bix);
616 
617 	/*
618 	 * Out of sync with hardware, should never happen?
619 	 */
620 	if (LER2V_rmd1(rmd) & LE_OWN) {
621 		le->sc_rown++;
622 		LERDWR(le->sc_r0, LE_RINT|LE_INEA, le->sc_r1->ler1_rdp);
623 		return;
624 	}
625 
626 	/*
627 	 * Process all buffers with valid data
628 	 */
629 	while ((LER2V_rmd1(rmd) & LE_OWN) == 0) {
630 		int len = LER2V_rmd3(rmd);
631 
632 		/* Clear interrupt to avoid race condition */
633 		LERDWR(le->sc_r0, LE_RINT|LE_INEA, le->sc_r1->ler1_rdp);
634 
635 		if (LER2V_rmd1(rmd) & LE_ERR) {
636 			le->sc_rmd = bix;
637 			lererror(unit, "bad packet");
638 			le->sc_if.if_ierrors++;
639 		} else if ((LER2V_rmd1(rmd) & (LE_STP|LE_ENP)) != (LE_STP|LE_ENP)) {
640 			/*
641 			 * Find the end of the packet so we can see how long
642 			 * it was.  We still throw it away.
643 			 */
644 			do {
645 				LERDWR(le->sc_r0, LE_RINT|LE_INEA,
646 				       le->sc_r1->ler1_rdp);
647 				LER2_rmd3(rmd, 0);
648 				LER2_rmd1(rmd, LE_OWN);
649 				LENEXTRMP;
650 			} while (!(LER2V_rmd1(rmd) & (LE_OWN|LE_ERR|LE_STP|LE_ENP)));
651 			le->sc_rmd = bix;
652 			lererror(unit, "chained buffer");
653 			le->sc_rxlen++;
654 			/*
655 			 * If search terminated without successful completion
656 			 * we reset the hardware (conservative).
657 			 */
658 			if ((LER2V_rmd1(rmd) & (LE_OWN|LE_ERR|LE_STP|LE_ENP)) !=
659 			    LE_ENP) {
660 				lereset(unit);
661 				return;
662 			}
663 		} else
664 			leread(unit, LER2_RBUFADDR(le->sc_r2, bix), len);
665 		LER2_rmd3(rmd, 0);
666 		LER2_rmd1(rmd, LE_OWN);
667 		LENEXTRMP;
668 	}
669 	MachEmptyWriteBuffer();		/* Paranoia */
670 	le->sc_rmd = bix;
671 }
672 
673 /*
674  * Look at the packet in network buffer memory so we can be smart about how
675  * we copy the data into mbufs.
676  * This needs work since we can't just read network buffer memory like
677  * regular memory.
678  */
679 leread(unit, buf, len)
680 	int unit;
681 	volatile void *buf;
682 	int len;
683 {
684 	register struct le_softc *le = &le_softc[unit];
685 	struct ether_header et;
686     	struct mbuf *m, **hdrmp, **tailmp;
687 	int off, resid, flags;
688 	u_short sbuf[2], eth_type;
689 	extern struct mbuf *leget();
690 
691 	le->sc_if.if_ipackets++;
692 	(*le->sc_copyfrombuf)(buf, 0, (char *)&et, sizeof (et));
693 	eth_type = ntohs(et.ether_type);
694 	/* adjust input length to account for header and CRC */
695 	len = len - sizeof(struct ether_header) - 4;
696 
697 	if (eth_type >= ETHERTYPE_TRAIL &&
698 	    eth_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
699 		off = (eth_type - ETHERTYPE_TRAIL) * 512;
700 		if (off >= ETHERMTU)
701 			return;		/* sanity */
702 		(*le->sc_copyfrombuf)(buf, sizeof (et) + off, (char *)sbuf,
703 			sizeof (sbuf));
704 		eth_type = ntohs(sbuf[0]);
705 		resid = ntohs(sbuf[1]);
706 		if (off + resid > len)
707 			return;		/* sanity */
708 		len = off + resid;
709 	} else
710 		off = 0;
711 
712 	if (len <= 0) {
713 		if (ledebug)
714 			log(LOG_WARNING,
715 			    "le%d: ierror(runt packet): from %s: len=%d\n",
716 			    unit, ether_sprintf(et.ether_shost), len);
717 		le->sc_runt++;
718 		le->sc_if.if_ierrors++;
719 		return;
720 	}
721 	flags = 0;
722 	if (bcmp((caddr_t)etherbroadcastaddr,
723 	    (caddr_t)et.ether_dhost, sizeof(etherbroadcastaddr)) == 0)
724 		flags |= M_BCAST;
725 	if (et.ether_dhost[0] & 1)
726 		flags |= M_MCAST;
727 
728 #if NBPFILTER > 0
729 	/*
730 	 * Check if there's a bpf filter listening on this interface.
731 	 * If so, hand off the raw packet to enet.
732 	 */
733 	if (le->sc_if.if_bpf) {
734 		bpf_tap(le->sc_if.if_bpf, buf, len + sizeof(struct ether_header));
735 
736 		/*
737 		 * Keep the packet if it's a broadcast or has our
738 		 * physical ethernet address (or if we support
739 		 * multicast and it's one).
740 		 */
741 		if (
742 #ifdef MULTICAST
743 		    (flags & (M_BCAST | M_MCAST)) == 0 &&
744 #else
745 		    (flags & M_BCAST) == 0 &&
746 #endif
747 		    bcmp(et.ether_dhost, le->sc_addr,
748 			sizeof(et.ether_dhost)) != 0)
749 			return;
750 	}
751 #endif
752 
753 	/*
754 	 * Pull packet off interface.  Off is nonzero if packet
755 	 * has trailing header; leget will then force this header
756 	 * information to be at the front, but we still have to drop
757 	 * the type and length which are at the front of any trailer data.
758 	 * The hdrmp and tailmp pointers are used by lebpf_tap() to
759 	 * temporarily reorder the mbuf list. See the comment at the beginning
760 	 * of lebpf_tap() for all the ugly details.
761 	 */
762 	m = leget(le, buf, len, off, &le->sc_if, &hdrmp, &tailmp);
763 	if (m == 0)
764 		return;
765 	m->m_flags |= flags;
766 	et.ether_type = eth_type;
767 	ether_input(&le->sc_if, &et, m);
768 }
769 
770 /*
771  * Routine to copy from mbuf chain to transmit buffer in
772  * network buffer memory.
773  */
774 leput(le, lebuf, m)
775 	struct le_softc *le;
776 	register volatile void *lebuf;
777 	register struct mbuf *m;
778 {
779 	register struct mbuf *mp;
780 	register int len, tlen = 0;
781 	register int boff = 0;
782 
783 	for (mp = m; mp; mp = mp->m_next) {
784 		len = mp->m_len;
785 		if (len == 0)
786 			continue;
787 		(*le->sc_copytobuf)(mtod(mp, char *), lebuf, boff, len);
788 		tlen += len;
789 		boff += len;
790 	}
791 	m_freem(m);
792 	if (tlen < LEMINSIZE) {
793 		(*le->sc_zerobuf)(lebuf, boff, LEMINSIZE - tlen);
794 		tlen = LEMINSIZE;
795 	}
796 	return(tlen);
797 }
798 
799 /*
800  * Routine to copy from network buffer memory into mbufs.
801  */
802 struct mbuf *
803 leget(le, lebuf, totlen, off, ifp, hdrmp, tailmp)
804 	struct le_softc *le;
805 	volatile void *lebuf;
806 	int totlen, off;
807 	struct ifnet *ifp;
808 	struct mbuf ***hdrmp, ***tailmp;
809 {
810 	register struct mbuf *m;
811 	struct mbuf *top = 0, **mp = &top;
812 	register int len, resid, boff;
813 
814 	/* NOTE: sizeof(struct ether_header) should be even */
815 	boff = sizeof(struct ether_header);
816 	if (off) {
817 		/* NOTE: off should be even */
818 		boff += off + 2 * sizeof(u_short);
819 		totlen -= 2 * sizeof(u_short);
820 		resid = totlen - off;
821 	} else
822 		resid = totlen;
823 
824 	MGETHDR(m, M_DONTWAIT, MT_DATA);
825 	if (m == 0)
826 		return (0);
827 	m->m_pkthdr.rcvif = ifp;
828 	m->m_pkthdr.len = totlen;
829 	m->m_len = MHLEN;
830 
831 	while (totlen > 0) {
832 		if (top) {
833 			MGET(m, M_DONTWAIT, MT_DATA);
834 			if (m == 0) {
835 				m_freem(top);
836 				return (0);
837 			}
838 			m->m_len = MLEN;
839 		}
840 
841 		if (resid >= MINCLSIZE)
842 			MCLGET(m, M_DONTWAIT);
843 		if (m->m_flags & M_EXT)
844 			m->m_len = min(resid, MCLBYTES);
845 		else if (resid < m->m_len) {
846 			/*
847 			 * Place initial small packet/header at end of mbuf.
848 			 */
849 			if (top == 0 && resid + max_linkhdr <= m->m_len)
850 				m->m_data += max_linkhdr;
851 			m->m_len = resid;
852 		}
853 		len = m->m_len;
854 		(*le->sc_copyfrombuf)(lebuf, boff, mtod(m, char *), len);
855 		boff += len;
856 		*mp = m;
857 		mp = &m->m_next;
858 		totlen -= len;
859 		resid -= len;
860 		if (resid == 0) {
861 			boff = sizeof (struct ether_header);
862 			resid = totlen;
863 			*hdrmp = mp;
864 		}
865 	}
866 	*tailmp = mp;
867 	return (top);
868 }
869 
870 /*
871  * Process an ioctl request.
872  */
873 leioctl(ifp, cmd, data)
874 	register struct ifnet *ifp;
875 	int cmd;
876 	caddr_t data;
877 {
878 	register struct ifaddr *ifa = (struct ifaddr *)data;
879 	struct le_softc *le = &le_softc[ifp->if_unit];
880 	volatile struct lereg1 *ler1 = le->sc_r1;
881 	int s, error = 0;
882 
883 	s = splnet();
884 	switch (cmd) {
885 
886 	case SIOCSIFADDR:
887 		ifp->if_flags |= IFF_UP;
888 		switch (ifa->ifa_addr->sa_family) {
889 #ifdef INET
890 		case AF_INET:
891 			leinit(ifp->if_unit);	/* before arpwhohas */
892 			((struct arpcom *)ifp)->ac_ipaddr =
893 				IA_SIN(ifa)->sin_addr;
894 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
895 			break;
896 #endif
897 #ifdef NS
898 		case AF_NS:
899 		    {
900 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
901 
902 			if (ns_nullhost(*ina))
903 				ina->x_host = *(union ns_host *)(le->sc_addr);
904 			else {
905 				/*
906 				 * The manual says we can't change the address
907 				 * while the receiver is armed,
908 				 * so reset everything
909 				 */
910 				ifp->if_flags &= ~IFF_RUNNING;
911 				LEWREG(LE_STOP, ler1->ler1_rdp);
912 				bcopy((caddr_t)ina->x_host.c_host,
913 				    (caddr_t)le->sc_addr, sizeof(le->sc_addr));
914 			}
915 			leinit(ifp->if_unit); /* does le_setaddr() */
916 			break;
917 		    }
918 #endif
919 		default:
920 			leinit(ifp->if_unit);
921 			break;
922 		}
923 		break;
924 
925 #if defined (CCITT) && defined (LLC)
926 	case SIOCSIFCONF_X25:
927 		ifp->if_flags |= IFF_UP;
928 		ifa->ifa_rtrequest = cons_rtrequest;
929 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
930 		if (error == 0)
931 			leinit(ifp->if_unit);
932 		break;
933 #endif /* CCITT && LLC */
934 
935 	case SIOCSIFFLAGS:
936 		if ((ifp->if_flags & IFF_UP) == 0 &&
937 		    ifp->if_flags & IFF_RUNNING) {
938 			LEWREG(LE_STOP, ler1->ler1_rdp);
939 			ifp->if_flags &= ~IFF_RUNNING;
940 		} else if (ifp->if_flags & IFF_UP &&
941 		    (ifp->if_flags & IFF_RUNNING) == 0)
942 			leinit(ifp->if_unit);
943 		/*
944 		 * If the state of the promiscuous bit changes, the interface
945 		 * must be reset to effect the change.
946 		 */
947 		if (((ifp->if_flags ^ le->sc_iflags) & IFF_PROMISC) &&
948 		    (ifp->if_flags & IFF_RUNNING)) {
949 			le->sc_iflags = ifp->if_flags;
950 			lereset(ifp->if_unit);
951 			lestart(ifp);
952 		}
953 		break;
954 
955 #ifdef MULTICAST
956 	case SIOCADDMULTI:
957 	case SIOCDELMULTI:
958 		/* Update our multicast list  */
959 		error = (cmd == SIOCADDMULTI) ?
960 		    ether_addmulti((struct ifreq *)data, &le->sc_ac) :
961 		    ether_delmulti((struct ifreq *)data, &le->sc_ac);
962 
963 		if (error == ENETRESET) {
964 			/*
965 			 * Multicast list has changed; set the hardware
966 			 * filter accordingly.
967 			 */
968 			lereset(ifp->if_unit);
969 			error = 0;
970 		}
971 		break;
972 #endif
973 
974 	default:
975 		error = EINVAL;
976 	}
977 	splx(s);
978 	return (error);
979 }
980 
981 leerror(unit, stat)
982 	int unit;
983 	int stat;
984 {
985 	if (!ledebug)
986 		return;
987 
988 	/*
989 	 * Not all transceivers implement heartbeat
990 	 * so we only log CERR once.
991 	 */
992 	if ((stat & LE_CERR) && le_softc[unit].sc_cerr)
993 		return;
994 	log(LOG_WARNING,
995 	    "le%d: error: stat=%b\n", unit,
996 	    stat,
997 	    "\20\20ERR\17BABL\16CERR\15MISS\14MERR\13RINT\12TINT\11IDON\10INTR\07INEA\06RXON\05TXON\04TDMD\03STOP\02STRT\01INIT");
998 }
999 
1000 lererror(unit, msg)
1001 	int unit;
1002 	char *msg;
1003 {
1004 	register struct le_softc *le = &le_softc[unit];
1005 	register volatile void *rmd;
1006 	u_char eaddr[6];
1007 	int len;
1008 
1009 	if (!ledebug)
1010 		return;
1011 
1012 	rmd = LER2_RMDADDR(le->sc_r2, le->sc_rmd);
1013 	len = LER2V_rmd3(rmd);
1014 	if (len > 11)
1015 		(*le->sc_copyfrombuf)(LER2_RBUFADDR(le->sc_r2, le->sc_rmd),
1016 			6, eaddr, 6);
1017 	log(LOG_WARNING,
1018 	    "le%d: ierror(%s): from %s: buf=%d, len=%d, rmd1=%b\n",
1019 	    unit, msg,
1020 	    len > 11 ? ether_sprintf(eaddr) : "unknown",
1021 	    le->sc_rmd, len,
1022 	    LER2V_rmd1(rmd),
1023 	    "\20\20OWN\17ERR\16FRAM\15OFLO\14CRC\13RBUF\12STP\11ENP");
1024 }
1025 
1026 lexerror(unit)
1027 	int unit;
1028 {
1029 	register struct le_softc *le = &le_softc[unit];
1030 	register volatile void *tmd;
1031 	u_char eaddr[6];
1032 	int len;
1033 
1034 	if (!ledebug)
1035 		return;
1036 
1037 	tmd = LER2_TMDADDR(le->sc_r2, 0);
1038 	len = -LER2V_tmd2(tmd);
1039 	if (len > 5)
1040 		(*le->sc_copyfrombuf)(LER2_TBUFADDR(le->sc_r2, 0), 0, eaddr, 6);
1041 	log(LOG_WARNING,
1042 	    "le%d: oerror: to %s: buf=%d, len=%d, tmd1=%b, tmd3=%b\n",
1043 	    unit,
1044 	    len > 5 ? ether_sprintf(eaddr) : "unknown",
1045 	    0, len,
1046 	    LER2V_tmd1(tmd),
1047 	    "\20\20OWN\17ERR\16RES\15MORE\14ONE\13DEF\12STP\11ENP",
1048 	    LER2V_tmd3(tmd),
1049 	    "\20\20BUFF\17UFLO\16RES\15LCOL\14LCAR\13RTRY");
1050 }
1051 
1052 /*
1053  * Write a lance register port, reading it back to ensure success. This seems
1054  * to be necessary during initialization, since the chip appears to be a bit
1055  * pokey sometimes.
1056  */
1057 static void
1058 lewritereg(regptr, val)
1059 	register volatile u_short *regptr;
1060 	register u_short val;
1061 {
1062 	register int i = 0;
1063 
1064 	while (*regptr != val) {
1065 		*regptr = val;
1066 		MachEmptyWriteBuffer();
1067 		if (++i > 10000) {
1068 			printf("le: Reg did not settle (to x%x): x%x\n",
1069 			       val, *regptr);
1070 			return;
1071 		}
1072 		DELAY(100);
1073 	}
1074 }
1075 
1076 /*
1077  * Routines for accessing the transmit and receive buffers. Unfortunately,
1078  * CPU addressing of these buffers is done in one of 3 ways:
1079  * - contiguous (for the 3max and turbochannel option card)
1080  * - gap2, which means shorts (2 bytes) interspersed with short (2 byte)
1081  *   spaces (for the pmax)
1082  * - gap16, which means 16bytes interspersed with 16byte spaces
1083  *   for buffers which must begin on a 32byte boundary (for 3min and maxine)
1084  * The buffer offset is the logical byte offset, assuming contiguous storage.
1085  */
1086 void
1087 copytobuf_contig(from, lebuf, boff, len)
1088 	char *from;
1089 	volatile void *lebuf;
1090 	int boff;
1091 	int len;
1092 {
1093 
1094 	/*
1095 	 * Just call bcopy() to do the work.
1096 	 */
1097 	bcopy(from, ((char *)lebuf) + boff, len);
1098 }
1099 
1100 void
1101 copyfrombuf_contig(lebuf, boff, to, len)
1102 	volatile void *lebuf;
1103 	int boff;
1104 	char *to;
1105 	int len;
1106 {
1107 
1108 	/*
1109 	 * Just call bcopy() to do the work.
1110 	 */
1111 	bcopy(((char *)lebuf) + boff, to, len);
1112 }
1113 
1114 void
1115 bzerobuf_contig(lebuf, boff, len)
1116 	volatile void *lebuf;
1117 	int boff;
1118 	int len;
1119 {
1120 
1121 	/*
1122 	 * Just let bzero() do the work
1123 	 */
1124 	bzero(((char *)lebuf) + boff, len);
1125 }
1126 
1127 /*
1128  * For the pmax the buffer consists of shorts (2 bytes) interspersed with
1129  * short (2 byte) spaces and must be accessed with halfword load/stores.
1130  * (don't worry about doing an extra byte)
1131  */
1132 void
1133 copytobuf_gap2(from, lebuf, boff, len)
1134 	register char *from;
1135 	volatile void *lebuf;
1136 	int boff;
1137 	register int len;
1138 {
1139 	register volatile u_short *bptr;
1140 	register int xfer;
1141 
1142 	if (boff & 0x1) {
1143 		/* handle unaligned first byte */
1144 		bptr = ((volatile u_short *)lebuf) + (boff - 1);
1145 		*bptr = (*from++ << 8) | (*bptr & 0xff);
1146 		bptr += 2;
1147 		len--;
1148 	} else
1149 		bptr = ((volatile u_short *)lebuf) + boff;
1150 	if ((unsigned)from & 0x1) {
1151 		while (len > 1) {
1152 			*bptr = (from[1] << 8) | from[0];
1153 			bptr += 2;
1154 			from += 2;
1155 			len -= 2;
1156 		}
1157 	} else {
1158 		/* optimize for aligned transfers */
1159 		xfer = (int)((unsigned)len & ~0x1);
1160 		CopyToBuffer((u_short *)from, bptr, xfer);
1161 		bptr += xfer;
1162 		from += xfer;
1163 		len -= xfer;
1164 	}
1165 	if (len == 1)
1166 		*bptr = (u_short)*from;
1167 }
1168 
1169 void
1170 copyfrombuf_gap2(lebuf, boff, to, len)
1171 	volatile void *lebuf;
1172 	int boff;
1173 	register char *to;
1174 	register int len;
1175 {
1176 	register volatile u_short *bptr;
1177 	register u_short tmp;
1178 	register int xfer;
1179 
1180 	if (boff & 0x1) {
1181 		/* handle unaligned first byte */
1182 		bptr = ((volatile u_short *)lebuf) + (boff - 1);
1183 		*to++ = (*bptr >> 8) & 0xff;
1184 		bptr += 2;
1185 		len--;
1186 	} else
1187 		bptr = ((volatile u_short *)lebuf) + boff;
1188 	if ((unsigned)to & 0x1) {
1189 		while (len > 1) {
1190 			tmp = *bptr;
1191 			*to++ = tmp & 0xff;
1192 			*to++ = (tmp >> 8) & 0xff;
1193 			bptr += 2;
1194 			len -= 2;
1195 		}
1196 	} else {
1197 		/* optimize for aligned transfers */
1198 		xfer = (int)((unsigned)len & ~0x1);
1199 		CopyFromBuffer(bptr, to, xfer);
1200 		bptr += xfer;
1201 		to += xfer;
1202 		len -= xfer;
1203 	}
1204 	if (len == 1)
1205 		*to = *bptr & 0xff;
1206 }
1207 
1208 void
1209 bzerobuf_gap2(lebuf, boff, len)
1210 	volatile void *lebuf;
1211 	int boff;
1212 	int len;
1213 {
1214 	register volatile u_short *bptr;
1215 
1216 	if ((unsigned)boff & 0x1) {
1217 		bptr = ((volatile u_short *)lebuf) + (boff - 1);
1218 		*bptr &= 0xff;
1219 		bptr += 2;
1220 		len--;
1221 	} else
1222 		bptr = ((volatile u_short *)lebuf) + boff;
1223 	while (len > 0) {
1224 		*bptr = 0;
1225 		bptr += 2;
1226 		len -= 2;
1227 	}
1228 }
1229 
1230 /*
1231  * For the 3min and maxine, the buffers are in main memory filled in with
1232  * 16byte blocks interspersed with 16byte spaces.
1233  */
1234 void
1235 copytobuf_gap16(from, lebuf, boff, len)
1236 	register char *from;
1237 	volatile void *lebuf;
1238 	int boff;
1239 	register int len;
1240 {
1241 	register char *bptr;
1242 	register int xfer;
1243 
1244 	bptr = ((char *)lebuf) + ((boff << 1) & ~0x1f);
1245 	boff &= 0xf;
1246 	xfer = min(len, 16 - boff);
1247 	while (len > 0) {
1248 		bcopy(from, ((char *)bptr) + boff, xfer);
1249 		from += xfer;
1250 		bptr += 32;
1251 		boff = 0;
1252 		len -= xfer;
1253 		xfer = min(len, 16);
1254 	}
1255 }
1256 
1257 void
1258 copyfrombuf_gap16(lebuf, boff, to, len)
1259 	volatile void *lebuf;
1260 	int boff;
1261 	register char *to;
1262 	register int len;
1263 {
1264 	register char *bptr;
1265 	register int xfer;
1266 
1267 	bptr = ((char *)lebuf) + ((boff << 1) & ~0x1f);
1268 	boff &= 0xf;
1269 	xfer = min(len, 16 - boff);
1270 	while (len > 0) {
1271 		bcopy(((char *)bptr) + boff, to, xfer);
1272 		to += xfer;
1273 		bptr += 32;
1274 		boff = 0;
1275 		len -= xfer;
1276 		xfer = min(len, 16);
1277 	}
1278 }
1279 
1280 void
1281 bzerobuf_gap16(lebuf, boff, len)
1282 	volatile void *lebuf;
1283 	int boff;
1284 	register int len;
1285 {
1286 	register char *bptr;
1287 	register int xfer;
1288 
1289 	bptr = ((char *)lebuf) + ((boff << 1) & ~0x1f);
1290 	boff &= 0xf;
1291 	xfer = min(len, 16 - boff);
1292 	while (len > 0) {
1293 		bzero(((char *)bptr) + boff, xfer);
1294 		bptr += 32;
1295 		boff = 0;
1296 		len -= xfer;
1297 		xfer = min(len, 16);
1298 	}
1299 }
1300 #endif /* NLE */
1301