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