xref: /openbsd/sys/dev/ic/am7990.c (revision db3296cf)
1 /*	$OpenBSD: am7990.c,v 1.31 2003/06/02 23:28:01 millert Exp $	*/
2 /*	$NetBSD: am7990.c,v 1.22 1996/10/13 01:37:19 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
6  * Copyright (c) 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Ralph Campbell and Rick Macklem.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
37  */
38 
39 #include "bpfilter.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/mbuf.h>
44 #include <sys/syslog.h>
45 #include <sys/socket.h>
46 #include <sys/device.h>
47 #include <sys/malloc.h>
48 #include <sys/ioctl.h>
49 #include <sys/errno.h>
50 
51 #include <net/if.h>
52 #include <net/if_media.h>
53 
54 #ifdef INET
55 #include <netinet/in.h>
56 #include <netinet/if_ether.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #endif
61 
62 #if NBPFILTER > 0
63 #include <net/bpf.h>
64 #include <net/bpfdesc.h>
65 #endif
66 
67 #include <dev/ic/am7990reg.h>
68 #include <dev/ic/am7990var.h>
69 
70 #ifdef LEDEBUG
71 void am7990_recv_print(struct am7990_softc *, int);
72 void am7990_xmit_print(struct am7990_softc *, int);
73 #endif
74 
75 integrate void am7990_rint(struct am7990_softc *);
76 integrate void am7990_tint(struct am7990_softc *);
77 
78 integrate int am7990_put(struct am7990_softc *, int, struct mbuf *);
79 integrate struct mbuf *am7990_get(struct am7990_softc *, int, int);
80 integrate void am7990_read(struct am7990_softc *, int, int);
81 
82 hide void am7990_shutdown(void *);
83 
84 #define	ifp	(&sc->sc_arpcom.ac_if)
85 
86 #if 0	/* XXX what do we do about this?!  --thorpej */
87 static inline u_int16_t ether_cmp(void *, void *);
88 
89 /*
90  * Compare two Ether/802 addresses for equality, inlined and
91  * unrolled for speed.  I'd love to have an inline assembler
92  * version of this...   XXX: Who wanted that? mycroft?
93  * I wrote one, but the following is just as efficient.
94  * This expands to 10 short m68k instructions! -gwr
95  * Note: use this like bcmp()
96  */
97 static inline u_short
98 ether_cmp(one, two)
99 	void *one, *two;
100 {
101 	register u_int16_t *a = (u_short *) one;
102 	register u_int16_t *b = (u_short *) two;
103 	register u_int16_t diff;
104 
105 	diff  = *a++ - *b++;
106 	diff |= *a++ - *b++;
107 	diff |= *a++ - *b++;
108 
109 	return (diff);
110 }
111 
112 #define ETHER_CMP	ether_cmp
113 #endif /* XXX */
114 
115 #ifndef	ETHER_CMP
116 #define	ETHER_CMP(a, b) bcmp((a), (b), ETHER_ADDR_LEN)
117 #endif
118 
119 /*
120  * am7990 configuration driver.  Attachments are provided by
121  * machine-dependent driver front-ends.
122  */
123 struct cfdriver le_cd = {
124 	NULL, "le", DV_IFNET
125 };
126 
127 void
128 am7990_config(sc)
129 	struct am7990_softc *sc;
130 {
131 	int mem;
132 
133 	/* Make sure the chip is stopped. */
134 	am7990_stop(sc);
135 
136 	/* Initialize ifnet structure. */
137 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
138 	ifp->if_softc = sc;
139 	ifp->if_start = am7990_start;
140 	ifp->if_ioctl = am7990_ioctl;
141 	ifp->if_watchdog = am7990_watchdog;
142 	ifp->if_flags =
143 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
144 #ifdef LANCE_REVC_BUG
145 	ifp->if_flags &= ~IFF_MULTICAST;
146 #endif
147 	ifp->if_baudrate = IF_Mbps(10);
148 	IFQ_SET_READY(&ifp->if_snd);
149 
150 	/* Attach the interface. */
151 	if_attach(ifp);
152 	ether_ifattach(ifp);
153 
154 	if (sc->sc_memsize > 262144)
155 		sc->sc_memsize = 262144;
156 
157 	switch (sc->sc_memsize) {
158 	case 8192:
159 		sc->sc_nrbuf = 4;
160 		sc->sc_ntbuf = 1;
161 		break;
162 	case 16384:
163 		sc->sc_nrbuf = 8;
164 		sc->sc_ntbuf = 2;
165 		break;
166 	case 32768:
167 		sc->sc_nrbuf = 16;
168 		sc->sc_ntbuf = 4;
169 		break;
170 	case 65536:
171 		sc->sc_nrbuf = 32;
172 		sc->sc_ntbuf = 8;
173 		break;
174 	case 131072:
175 		sc->sc_nrbuf = 64;
176 		sc->sc_ntbuf = 16;
177 		break;
178 	case 262144:
179 		sc->sc_nrbuf = 128;
180 		sc->sc_ntbuf = 32;
181 		break;
182 	default:
183 		panic("am7990_config: weird memory size %d", sc->sc_memsize);
184 	}
185 
186 	printf(": address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
187 	printf("%s: %d receive buffers, %d transmit buffers\n",
188 	    sc->sc_dev.dv_xname, sc->sc_nrbuf, sc->sc_ntbuf);
189 
190 	sc->sc_sh = shutdownhook_establish(am7990_shutdown, sc);
191 	if (sc->sc_sh == NULL)
192 		panic("am7990_config: can't establish shutdownhook");
193 
194 	mem = 0;
195 	sc->sc_initaddr = mem;
196 	mem += sizeof(struct leinit);
197 	sc->sc_rmdaddr = mem;
198 	mem += sizeof(struct lermd) * sc->sc_nrbuf;
199 	sc->sc_tmdaddr = mem;
200 	mem += sizeof(struct letmd) * sc->sc_ntbuf;
201 	sc->sc_rbufaddr = mem;
202 	mem += LEBLEN * sc->sc_nrbuf;
203 	sc->sc_tbufaddr = mem;
204 	mem += LEBLEN * sc->sc_ntbuf;
205 #ifdef notyet
206 	if (mem > ...)
207 		panic(...);
208 #endif
209 }
210 
211 void
212 am7990_reset(sc)
213 	struct am7990_softc *sc;
214 {
215 	int s;
216 
217 	s = splimp();
218 	am7990_init(sc);
219 	splx(s);
220 }
221 
222 /*
223  * Set up the initialization block and the descriptor rings.
224  */
225 void
226 am7990_meminit(sc)
227 	register struct am7990_softc *sc;
228 {
229 	u_long a;
230 	int bix;
231 	struct leinit init;
232 	struct lermd rmd;
233 	struct letmd tmd;
234 
235 #if NBPFILTER > 0
236 	if (ifp->if_flags & IFF_PROMISC)
237 		init.init_mode = LE_MODE_NORMAL | LE_MODE_PROM;
238 	else
239 #endif
240 		init.init_mode = LE_MODE_NORMAL;
241 	init.init_padr[0] =
242 	    (sc->sc_arpcom.ac_enaddr[1] << 8) | sc->sc_arpcom.ac_enaddr[0];
243 	init.init_padr[1] =
244 	    (sc->sc_arpcom.ac_enaddr[3] << 8) | sc->sc_arpcom.ac_enaddr[2];
245 	init.init_padr[2] =
246 	    (sc->sc_arpcom.ac_enaddr[5] << 8) | sc->sc_arpcom.ac_enaddr[4];
247 	am7990_setladrf(&sc->sc_arpcom, init.init_ladrf);
248 
249 	sc->sc_last_rd = 0;
250 	sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
251 
252 	a = sc->sc_addr + LE_RMDADDR(sc, 0);
253 	init.init_rdra = a;
254 	init.init_rlen = (a >> 16) | ((ffs(sc->sc_nrbuf) - 1) << 13);
255 
256 	a = sc->sc_addr + LE_TMDADDR(sc, 0);
257 	init.init_tdra = a;
258 	init.init_tlen = (a >> 16) | ((ffs(sc->sc_ntbuf) - 1) << 13);
259 
260 	(*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
261 
262 	/*
263 	 * Set up receive ring descriptors.
264 	 */
265 	for (bix = 0; bix < sc->sc_nrbuf; bix++) {
266 		a = sc->sc_addr + LE_RBUFADDR(sc, bix);
267 		rmd.rmd0 = a;
268 		rmd.rmd1_hadr = a >> 16;
269 		rmd.rmd1_bits = LE_R1_OWN;
270 		rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
271 		rmd.rmd3 = 0;
272 		(*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
273 		    sizeof(rmd));
274 	}
275 
276 	/*
277 	 * Set up transmit ring descriptors.
278 	 */
279 	for (bix = 0; bix < sc->sc_ntbuf; bix++) {
280 		a = sc->sc_addr + LE_TBUFADDR(sc, bix);
281 		tmd.tmd0 = a;
282 		tmd.tmd1_hadr = a >> 16;
283 		tmd.tmd1_bits = 0;
284 		tmd.tmd2 = 0 | LE_XMD2_ONES;
285 		tmd.tmd3 = 0;
286 		(*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
287 		    sizeof(tmd));
288 	}
289 }
290 
291 void
292 am7990_stop(sc)
293 	struct am7990_softc *sc;
294 {
295 
296 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
297 }
298 
299 /*
300  * Initialization of interface; set up initialization block
301  * and transmit/receive descriptor rings.
302  */
303 void
304 am7990_init(sc)
305 	register struct am7990_softc *sc;
306 {
307 	register int timo;
308 	u_long a;
309 
310 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
311 	DELAY(100);
312 
313 	/* Newer LANCE chips have a reset register */
314 	if (sc->sc_hwreset)
315 		(*sc->sc_hwreset)(sc);
316 
317 	/* Set the correct byte swapping mode, etc. */
318 	(*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
319 
320 	/* Set up LANCE init block. */
321 	am7990_meminit(sc);
322 
323 	/* Give LANCE the physical address of its init block. */
324 	a = sc->sc_addr + LE_INITADDR(sc);
325 	(*sc->sc_wrcsr)(sc, LE_CSR1, a);
326 	(*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
327 
328 	/* Try to initialize the LANCE. */
329 	DELAY(100);
330 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
331 
332 	/* Wait for initialization to finish. */
333 	for (timo = 100000; timo; timo--)
334 		if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
335 			break;
336 
337 	if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
338 		/* Start the LANCE. */
339 		(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT |
340 		    LE_C0_IDON);
341 		ifp->if_flags |= IFF_RUNNING;
342 		ifp->if_flags &= ~IFF_OACTIVE;
343 		ifp->if_timer = 0;
344 		am7990_start(ifp);
345 	} else
346 		printf("%s: controller failed to initialize\n", sc->sc_dev.dv_xname);
347 	if (sc->sc_hwinit)
348 		(*sc->sc_hwinit)(sc);
349 }
350 
351 /*
352  * Routine to copy from mbuf chain to transmit buffer in
353  * network buffer memory.
354  */
355 integrate int
356 am7990_put(sc, boff, m)
357 	struct am7990_softc *sc;
358 	int boff;
359 	register struct mbuf *m;
360 {
361 	register struct mbuf *n;
362 	register int len, tlen = 0;
363 
364 	for (; m; m = n) {
365 		len = m->m_len;
366 		if (len == 0) {
367 			MFREE(m, n);
368 			continue;
369 		}
370 		(*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
371 		boff += len;
372 		tlen += len;
373 		MFREE(m, n);
374 	}
375 	if (tlen < LEMINSIZE) {
376 		(*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
377 		tlen = LEMINSIZE;
378 	}
379 	return (tlen);
380 }
381 
382 /*
383  * Pull data off an interface.
384  * Len is length of data, with local net header stripped.
385  * We copy the data into mbufs.  When full cluster sized units are present
386  * we copy into clusters.
387  */
388 integrate struct mbuf *
389 am7990_get(sc, boff, totlen)
390 	struct am7990_softc *sc;
391 	int boff, totlen;
392 {
393 	register struct mbuf *m;
394 	struct mbuf *top, **mp;
395 	int len, pad;
396 
397 	MGETHDR(m, M_DONTWAIT, MT_DATA);
398 	if (m == 0)
399 		return (0);
400 	m->m_pkthdr.rcvif = ifp;
401 	m->m_pkthdr.len = totlen;
402 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
403 	m->m_data += pad;
404 	len = MHLEN - pad;
405 	top = 0;
406 	mp = &top;
407 
408 	while (totlen > 0) {
409 		if (top) {
410 			MGET(m, M_DONTWAIT, MT_DATA);
411 			if (m == 0) {
412 				m_freem(top);
413 				return 0;
414 			}
415 			len = MLEN;
416 		}
417 		if (totlen >= MINCLSIZE) {
418 			MCLGET(m, M_DONTWAIT);
419 			if (m->m_flags & M_EXT) {
420 				len = MCLBYTES;
421 				if (!top) {
422 					m->m_data += pad;
423 					len -= pad;
424 				}
425 			}
426 		}
427 		m->m_len = len = min(totlen, len);
428 		(*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
429 		boff += len;
430 		totlen -= len;
431 		*mp = m;
432 		mp = &m->m_next;
433 	}
434 
435 	return (top);
436 }
437 
438 /*
439  * Pass a packet to the higher levels.
440  */
441 integrate void
442 am7990_read(sc, boff, len)
443 	register struct am7990_softc *sc;
444 	int boff, len;
445 {
446 	struct mbuf *m;
447 #ifdef LANCE_REVC_BUG
448 	struct ether_header *eh;
449 #endif
450 
451 	if (len <= sizeof(struct ether_header) ||
452 	    len > ETHERMTU + sizeof(struct ether_header)) {
453 #ifdef LEDEBUG
454 		printf("%s: invalid packet size %d; dropping\n",
455 		    sc->sc_dev.dv_xname, len);
456 #endif
457 		ifp->if_ierrors++;
458 		return;
459 	}
460 
461 	/* Pull packet off interface. */
462 	m = am7990_get(sc, boff, len);
463 	if (m == 0) {
464 		ifp->if_ierrors++;
465 		return;
466 	}
467 
468 	ifp->if_ipackets++;
469 
470 #if NBPFILTER > 0
471 	/*
472 	 * Check if there's a BPF listener on this interface.
473 	 * If so, hand off the raw packet to BPF.
474 	 */
475 	if (ifp->if_bpf)
476 		bpf_mtap(ifp->if_bpf, m);
477 #endif
478 
479 #ifdef LANCE_REVC_BUG
480 	/*
481 	 * The old LANCE (Rev. C) chips have a bug which causes
482 	 * garbage to be inserted in front of the received packet.
483 	 * The work-around is to ignore packets with an invalid
484 	 * destination address (garbage will usually not match).
485 	 * Of course, this precludes multicast support...
486 	 */
487 	eh = mtod(m, struct ether_header *);
488 	if (ETHER_CMP(eh->ether_dhost, sc->sc_arpcom.ac_enaddr) &&
489 	    ETHER_CMP(eh->ether_dhost, etherbroadcastaddr)) {
490 		m_freem(m);
491 		return;
492 	}
493 #endif
494 
495 	/* Pass the packet up. */
496 	ether_input_mbuf(ifp, m);
497 }
498 
499 integrate void
500 am7990_rint(sc)
501 	struct am7990_softc *sc;
502 {
503 	register int bix;
504 	int rp;
505 	struct lermd rmd;
506 
507 	bix = sc->sc_last_rd;
508 
509 	/* Process all buffers with valid data. */
510 	for (;;) {
511 		rp = LE_RMDADDR(sc, bix);
512 		(*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
513 
514 		if (rmd.rmd1_bits & LE_R1_OWN)
515 			break;
516 
517 		if (rmd.rmd1_bits & LE_R1_ERR) {
518 			if (rmd.rmd1_bits & LE_R1_ENP) {
519 #ifdef LEDEBUG
520 				if ((rmd.rmd1_bits & LE_R1_OFLO) == 0) {
521 					if (rmd.rmd1_bits & LE_R1_FRAM)
522 						printf("%s: framing error\n",
523 						    sc->sc_dev.dv_xname);
524 					if (rmd.rmd1_bits & LE_R1_CRC)
525 						printf("%s: crc mismatch\n",
526 						    sc->sc_dev.dv_xname);
527 				}
528 #endif
529 			} else {
530 				if (rmd.rmd1_bits & LE_R1_OFLO)
531 					printf("%s: overflow\n",
532 					    sc->sc_dev.dv_xname);
533 			}
534 			if (rmd.rmd1_bits & LE_R1_BUFF)
535 				printf("%s: receive buffer error\n",
536 				    sc->sc_dev.dv_xname);
537 			ifp->if_ierrors++;
538 		} else if ((rmd.rmd1_bits & (LE_R1_STP | LE_R1_ENP)) !=
539 		    (LE_R1_STP | LE_R1_ENP)) {
540 			printf("%s: dropping chained buffer\n",
541 			    sc->sc_dev.dv_xname);
542 			ifp->if_ierrors++;
543 		} else {
544 #ifdef LEDEBUG1
545 			if (sc->sc_debug)
546 				am7990_recv_print(sc, sc->sc_last_rd);
547 #endif
548 			am7990_read(sc, LE_RBUFADDR(sc, bix),
549 			    (int)rmd.rmd3 - 4);
550 		}
551 
552 		rmd.rmd1_bits = LE_R1_OWN;
553 		rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
554 		rmd.rmd3 = 0;
555 		(*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
556 
557 #ifdef LEDEBUG1
558 		if (sc->sc_debug)
559 			printf("sc->sc_last_rd = %x, rmd: "
560 			       "ladr %04x, hadr %02x, flags %02x, "
561 			       "bcnt %04x, mcnt %04x\n",
562 				sc->sc_last_rd,
563 				rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits,
564 				rmd.rmd2, rmd.rmd3);
565 #endif
566 
567 		if (++bix == sc->sc_nrbuf)
568 			bix = 0;
569 	}
570 
571 	sc->sc_last_rd = bix;
572 }
573 
574 integrate void
575 am7990_tint(sc)
576 	register struct am7990_softc *sc;
577 {
578 	register int bix;
579 	struct letmd tmd;
580 
581 	bix = sc->sc_first_td;
582 
583 	for (;;) {
584 		if (sc->sc_no_td <= 0)
585 			break;
586 
587 		(*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
588 		    sizeof(tmd));
589 
590 #ifdef LEDEBUG
591 		if (sc->sc_debug)
592 			printf("trans tmd: "
593 			    "ladr %04x, hadr %02x, flags %02x, "
594 			    "bcnt %04x, mcnt %04x\n",
595 			    tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits,
596 			    tmd.tmd2, tmd.tmd3);
597 #endif
598 
599 		if (tmd.tmd1_bits & LE_T1_OWN)
600 			break;
601 
602 		ifp->if_flags &= ~IFF_OACTIVE;
603 
604 		if (tmd.tmd1_bits & LE_T1_ERR) {
605 			if (tmd.tmd3 & LE_T3_BUFF)
606 				printf("%s: transmit buffer error\n",
607 				    sc->sc_dev.dv_xname);
608 			else if (tmd.tmd3 & LE_T3_UFLO)
609 				printf("%s: underflow\n", sc->sc_dev.dv_xname);
610 			if (tmd.tmd3 & (LE_T3_BUFF | LE_T3_UFLO)) {
611 				am7990_reset(sc);
612 				return;
613 			}
614 			if (tmd.tmd3 & LE_T3_LCAR) {
615 				if (sc->sc_nocarrier)
616 					(*sc->sc_nocarrier)(sc);
617 			}
618 			if (tmd.tmd3 & LE_T3_LCOL)
619 				ifp->if_collisions++;
620 			if (tmd.tmd3 & LE_T3_RTRY) {
621 				printf("%s: excessive collisions, tdr %d\n",
622 				    sc->sc_dev.dv_xname,
623 				    tmd.tmd3 & LE_T3_TDR_MASK);
624 				ifp->if_collisions += 16;
625 			}
626 			ifp->if_oerrors++;
627 		} else {
628 			if (tmd.tmd1_bits & LE_T1_ONE)
629 				ifp->if_collisions++;
630 			else if (tmd.tmd1_bits & LE_T1_MORE)
631 				/* Real number is unknown. */
632 				ifp->if_collisions += 2;
633 			ifp->if_opackets++;
634 		}
635 
636 		if (++bix == sc->sc_ntbuf)
637 			bix = 0;
638 
639 		--sc->sc_no_td;
640 	}
641 
642 	sc->sc_first_td = bix;
643 
644 	am7990_start(ifp);
645 
646 	if (sc->sc_no_td == 0)
647 		ifp->if_timer = 0;
648 }
649 
650 /*
651  * Controller interrupt.
652  */
653 int
654 am7990_intr(arg)
655 	register void *arg;
656 {
657 	register struct am7990_softc *sc = arg;
658 	register u_int16_t isr;
659 
660 	isr = (*sc->sc_rdcsr)(sc, LE_CSR0);
661 #ifdef LEDEBUG
662 	if (sc->sc_debug){
663 		printf("%s: am7990_intr entering with isr=%04x\n",
664 		    sc->sc_dev.dv_xname, isr);
665 		printf(" isr: 0x%b\n", isr, LE_C0_BITS);
666 	}
667 #endif
668 	if ((isr & LE_C0_INTR) == 0)
669 		return (0);
670 
671 	/*
672 	 * After receiving an interrupt, we need to toggle the interrupt
673 	 * enable bit in order to keep receiving them (some chips works
674 	 * without this, some do not)
675 	 */
676 	(*sc->sc_wrcsr)(sc, LE_CSR0, isr & ~LE_C0_INEA);
677 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA);
678 
679 	if (isr & LE_C0_ERR) {
680 		if (isr & LE_C0_BABL) {
681 #ifdef LEDEBUG
682 			printf("%s: babble\n", sc->sc_dev.dv_xname);
683 #endif
684 			ifp->if_oerrors++;
685 		}
686 #if 0
687 		if (isr & LE_C0_CERR) {
688 			printf("%s: collision error\n", sc->sc_dev.dv_xname);
689 			ifp->if_collisions++;
690 		}
691 #endif
692 		if (isr & LE_C0_MISS) {
693 #ifdef LEDEBUG
694 			printf("%s: missed packet\n", sc->sc_dev.dv_xname);
695 #endif
696 			ifp->if_ierrors++;
697 		}
698 		if (isr & LE_C0_MERR) {
699 			printf("%s: memory error\n", sc->sc_dev.dv_xname);
700 			am7990_reset(sc);
701 			return (1);
702 		}
703 	}
704 
705 	if ((isr & LE_C0_RXON) == 0) {
706 		printf("%s: receiver disabled\n", sc->sc_dev.dv_xname);
707 		ifp->if_ierrors++;
708 		am7990_reset(sc);
709 		return (1);
710 	}
711 	if ((isr & LE_C0_TXON) == 0) {
712 		printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname);
713 		ifp->if_oerrors++;
714 		am7990_reset(sc);
715 		return (1);
716 	}
717 
718 	if (isr & LE_C0_RINT)
719 		am7990_rint(sc);
720 	if (isr & LE_C0_TINT)
721 		am7990_tint(sc);
722 
723 	return (1);
724 }
725 
726 #undef	ifp
727 
728 void
729 am7990_watchdog(ifp)
730 	struct ifnet *ifp;
731 {
732 	struct am7990_softc *sc = ifp->if_softc;
733 
734 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
735 	++ifp->if_oerrors;
736 
737 	am7990_reset(sc);
738 }
739 
740 /*
741  * Setup output on interface.
742  * Get another datagram to send off of the interface queue, and map it to the
743  * interface before starting the output.
744  * Called only at splimp or interrupt level.
745  */
746 void
747 am7990_start(ifp)
748 	register struct ifnet *ifp;
749 {
750 	register struct am7990_softc *sc = ifp->if_softc;
751 	register int bix;
752 	register struct mbuf *m;
753 	struct letmd tmd;
754 	int rp;
755 	int len;
756 
757 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
758 		return;
759 
760 	bix = sc->sc_last_td;
761 
762 	for (;;) {
763 		rp = LE_TMDADDR(sc, bix);
764 		(*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
765 
766 		if (tmd.tmd1_bits & LE_T1_OWN) {
767 			ifp->if_flags |= IFF_OACTIVE;
768 			printf("missing buffer, no_td = %d, last_td = %d\n",
769 			    sc->sc_no_td, sc->sc_last_td);
770 		}
771 
772 		IFQ_DEQUEUE(&ifp->if_snd, m);
773 		if (m == 0)
774 			break;
775 
776 #if NBPFILTER > 0
777 		/*
778 		 * If BPF is listening on this interface, let it see the packet
779 		 * before we commit it to the wire.
780 		 */
781 		if (ifp->if_bpf)
782 			bpf_mtap(ifp->if_bpf, m);
783 #endif
784 
785 		/*
786 		 * Copy the mbuf chain into the transmit buffer.
787 		 */
788 		len = am7990_put(sc, LE_TBUFADDR(sc, bix), m);
789 
790 #ifdef LEDEBUG
791 		if (len > ETHERMTU + sizeof(struct ether_header))
792 			printf("packet length %d\n", len);
793 #endif
794 
795 		ifp->if_timer = 5;
796 
797 		/*
798 		 * Init transmit registers, and set transmit start flag.
799 		 */
800 		tmd.tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
801 		tmd.tmd2 = -len | LE_XMD2_ONES;
802 		tmd.tmd3 = 0;
803 
804 		(*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
805 
806 #ifdef LEDEBUG
807 		if (sc->sc_debug)
808 			am7990_xmit_print(sc, sc->sc_last_td);
809 #endif
810 
811 		(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
812 
813 		if (++bix == sc->sc_ntbuf)
814 			bix = 0;
815 
816 		if (++sc->sc_no_td == sc->sc_ntbuf) {
817 #ifdef LEDEBUG
818 			printf("\nequal!\n");
819 #endif
820 			ifp->if_flags |= IFF_OACTIVE;
821 			break;
822 		}
823 
824 	}
825 
826 	sc->sc_last_td = bix;
827 }
828 
829 /*
830  * Process an ioctl request.
831  */
832 int
833 am7990_ioctl(ifp, cmd, data)
834 	register struct ifnet *ifp;
835 	u_long cmd;
836 	caddr_t data;
837 {
838 	register struct am7990_softc *sc = ifp->if_softc;
839 	struct ifaddr *ifa = (struct ifaddr *)data;
840 	struct ifreq *ifr = (struct ifreq *)data;
841 	int s, error = 0;
842 
843 	s = splimp();
844 
845 	if ((error = ether_ioctl(ifp, &sc->sc_arpcom, cmd, data)) > 0) {
846 		splx(s);
847 		return error;
848 	}
849 
850 	switch (cmd) {
851 
852 	case SIOCSIFADDR:
853 		ifp->if_flags |= IFF_UP;
854 
855 		switch (ifa->ifa_addr->sa_family) {
856 #ifdef INET
857 		case AF_INET:
858 			am7990_init(sc);
859 			arp_ifinit(&sc->sc_arpcom, ifa);
860 			break;
861 #endif
862 		default:
863 			am7990_init(sc);
864 			break;
865 		}
866 		break;
867 
868 	case SIOCSIFFLAGS:
869 		if ((ifp->if_flags & IFF_UP) == 0 &&
870 		    (ifp->if_flags & IFF_RUNNING) != 0) {
871 			/*
872 			 * If interface is marked down and it is running, then
873 			 * stop it.
874 			 */
875 			am7990_stop(sc);
876 			ifp->if_flags &= ~IFF_RUNNING;
877 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
878 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
879 			/*
880 			 * If interface is marked up and it is stopped, then
881 			 * start it.
882 			 */
883 			am7990_init(sc);
884 		} else {
885 			/*
886 			 * Reset the interface to pick up changes in any other
887 			 * flags that affect hardware registers.
888 			 */
889 			/*am7990_stop(sc);*/
890 			am7990_init(sc);
891 		}
892 #ifdef LEDEBUG
893 		if (ifp->if_flags & IFF_DEBUG)
894 			sc->sc_debug = 1;
895 		else
896 			sc->sc_debug = 0;
897 #endif
898 		break;
899 
900 	case SIOCADDMULTI:
901 	case SIOCDELMULTI:
902 		error = (cmd == SIOCADDMULTI) ?
903 		    ether_addmulti(ifr, &sc->sc_arpcom) :
904 		    ether_delmulti(ifr, &sc->sc_arpcom);
905 
906 		if (error == ENETRESET) {
907 			/*
908 			 * Multicast list has changed; set the hardware filter
909 			 * accordingly.
910 			 */
911 			am7990_reset(sc);
912 			error = 0;
913 		}
914 		break;
915 
916 	case SIOCGIFMEDIA:
917 	case SIOCSIFMEDIA:
918 		if (sc->sc_hasifmedia)
919 			error = ifmedia_ioctl(ifp, ifr, &sc->sc_ifmedia, cmd);
920 		else
921 			error = EINVAL;
922 		break;
923 
924 	default:
925 		error = EINVAL;
926 		break;
927 	}
928 
929 	splx(s);
930 	return (error);
931 }
932 
933 hide void
934 am7990_shutdown(arg)
935 	void *arg;
936 {
937 
938 	am7990_stop((struct am7990_softc *)arg);
939 }
940 
941 #ifdef LEDEBUG
942 void
943 am7990_recv_print(sc, no)
944 	struct am7990_softc *sc;
945 	int no;
946 {
947 	struct lermd rmd;
948 	u_int16_t len;
949 	struct ether_header eh;
950 
951 	(*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
952 	len = rmd.rmd3;
953 	printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
954 	    len);
955 	printf("%s: status %04x\n", sc->sc_dev.dv_xname,
956 	    (*sc->sc_rdcsr)(sc, LE_CSR0));
957 	printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
958 	    sc->sc_dev.dv_xname,
959 	    rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits, rmd.rmd2, rmd.rmd3);
960 	if (len >= sizeof(eh)) {
961 		(*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
962 		printf("%s: dst %s", sc->sc_dev.dv_xname,
963 			ether_sprintf(eh.ether_dhost));
964 		printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
965 			ntohs(eh.ether_type));
966 	}
967 }
968 
969 void
970 am7990_xmit_print(sc, no)
971 	struct am7990_softc *sc;
972 	int no;
973 {
974 	struct letmd tmd;
975 	u_int16_t len;
976 	struct ether_header eh;
977 
978 	(*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
979 	len = -tmd.tmd2;
980 	printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
981 	    len);
982 	printf("%s: status %04x\n", sc->sc_dev.dv_xname,
983 	    (*sc->sc_rdcsr)(sc, LE_CSR0));
984 	printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
985 	    sc->sc_dev.dv_xname,
986 	    tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits, tmd.tmd2, tmd.tmd3);
987 	if (len >= sizeof(eh)) {
988 		(*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
989 		printf("%s: dst %s", sc->sc_dev.dv_xname,
990 			ether_sprintf(eh.ether_dhost));
991 		printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
992 		    ntohs(eh.ether_type));
993 	}
994 }
995 #endif /* LEDEBUG */
996 
997 /*
998  * Set up the logical address filter.
999  */
1000 void
1001 am7990_setladrf(ac, af)
1002 	struct arpcom *ac;
1003 	u_int16_t *af;
1004 {
1005 	struct ifnet *ifp = &ac->ac_if;
1006 	struct ether_multi *enm;
1007 	register u_char *cp, c;
1008 	register u_int32_t crc;
1009 	register int i, len;
1010 	struct ether_multistep step;
1011 
1012 	/*
1013 	 * Set up multicast address filter by passing all multicast addresses
1014 	 * through a crc generator, and then using the high order 6 bits as an
1015 	 * index into the 64 bit logical address filter.  The high order bit
1016 	 * selects the word, while the rest of the bits select the bit within
1017 	 * the word.
1018 	 */
1019 
1020 	if (ifp->if_flags & IFF_PROMISC)
1021 		goto allmulti;
1022 
1023 	af[0] = af[1] = af[2] = af[3] = 0x0000;
1024 	ETHER_FIRST_MULTI(step, ac, enm);
1025 	while (enm != NULL) {
1026 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
1027 			/*
1028 			 * We must listen to a range of multicast addresses.
1029 			 * For now, just accept all multicasts, rather than
1030 			 * trying to set only those filter bits needed to match
1031 			 * the range.  (At this time, the only use of address
1032 			 * ranges is for IP multicast routing, for which the
1033 			 * range is big enough to require all bits set.)
1034 			 */
1035 			goto allmulti;
1036 		}
1037 
1038 		cp = enm->enm_addrlo;
1039 		crc = 0xffffffff;
1040 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1041 			c = *cp++;
1042 			for (i = 8; --i >= 0;) {
1043 				if ((crc & 0x01) ^ (c & 0x01)) {
1044 					crc >>= 1;
1045 					crc ^= 0xedb88320;
1046 				} else
1047 					crc >>= 1;
1048 				c >>= 1;
1049 			}
1050 		}
1051 		/* Just want the 6 most significant bits. */
1052 		crc >>= 26;
1053 
1054 		/* Set the corresponding bit in the filter. */
1055 		af[crc >> 4] |= 1 << (crc & 0xf);
1056 
1057 		ETHER_NEXT_MULTI(step, enm);
1058 	}
1059 	ifp->if_flags &= ~IFF_ALLMULTI;
1060 	return;
1061 
1062 allmulti:
1063 	ifp->if_flags |= IFF_ALLMULTI;
1064 	af[0] = af[1] = af[2] = af[3] = 0xffff;
1065 }
1066 
1067 
1068 /*
1069  * Routines for accessing the transmit and receive buffers.
1070  * The various CPU and adapter configurations supported by this
1071  * driver require three different access methods for buffers
1072  * and descriptors:
1073  *	(1) contig (contiguous data; no padding),
1074  *	(2) gap2 (two bytes of data followed by two bytes of padding),
1075  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
1076  */
1077 
1078 /*
1079  * contig: contiguous data with no padding.
1080  *
1081  * Buffers may have any alignment.
1082  */
1083 
1084 void
1085 am7990_copytobuf_contig(sc, from, boff, len)
1086 	struct am7990_softc *sc;
1087 	void *from;
1088 	int boff, len;
1089 {
1090 	volatile caddr_t buf = sc->sc_mem;
1091 
1092 	/*
1093 	 * Just call bcopy() to do the work.
1094 	 */
1095 	bcopy(from, buf + boff, len);
1096 }
1097 
1098 void
1099 am7990_copyfrombuf_contig(sc, to, boff, len)
1100 	struct am7990_softc *sc;
1101 	void *to;
1102 	int boff, len;
1103 {
1104 	volatile caddr_t buf = sc->sc_mem;
1105 
1106 	/*
1107 	 * Just call bcopy() to do the work.
1108 	 */
1109 	bcopy(buf + boff, to, len);
1110 }
1111 
1112 void
1113 am7990_zerobuf_contig(sc, boff, len)
1114 	struct am7990_softc *sc;
1115 	int boff, len;
1116 {
1117 	volatile caddr_t buf = sc->sc_mem;
1118 
1119 	/*
1120 	 * Just let bzero() do the work
1121 	 */
1122 	bzero(buf + boff, len);
1123 }
1124 
1125 #if 0
1126 /*
1127  * Examples only; duplicate these and tweak (if necessary) in
1128  * machine-specific front-ends.
1129  */
1130 
1131 /*
1132  * gap2: two bytes of data followed by two bytes of pad.
1133  *
1134  * Buffers must be 4-byte aligned.  The code doesn't worry about
1135  * doing an extra byte.
1136  */
1137 
1138 void
1139 am7990_copytobuf_gap2(sc, fromv, boff, len)
1140 	struct am7990_softc *sc;
1141 	void *fromv;
1142 	int boff;
1143 	register int len;
1144 {
1145 	volatile caddr_t buf = sc->sc_mem;
1146 	register caddr_t from = fromv;
1147 	register volatile u_int16_t *bptr;
1148 
1149 	if (boff & 0x1) {
1150 		/* handle unaligned first byte */
1151 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
1152 		*bptr = (*from++ << 8) | (*bptr & 0xff);
1153 		bptr += 2;
1154 		len--;
1155 	} else
1156 		bptr = ((volatile u_int16_t *)buf) + boff;
1157 	while (len > 1) {
1158 		*bptr = (from[1] << 8) | (from[0] & 0xff);
1159 		bptr += 2;
1160 		from += 2;
1161 		len -= 2;
1162 	}
1163 	if (len == 1)
1164 		*bptr = (u_int16_t)*from;
1165 }
1166 
1167 void
1168 am7990_copyfrombuf_gap2(sc, tov, boff, len)
1169 	struct am7990_softc *sc;
1170 	void *tov;
1171 	int boff, len;
1172 {
1173 	volatile caddr_t buf = sc->sc_mem;
1174 	register caddr_t to = tov;
1175 	register volatile u_int16_t *bptr;
1176 	register u_int16_t tmp;
1177 
1178 	if (boff & 0x1) {
1179 		/* handle unaligned first byte */
1180 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
1181 		*to++ = (*bptr >> 8) & 0xff;
1182 		bptr += 2;
1183 		len--;
1184 	} else
1185 		bptr = ((volatile u_int16_t *)buf) + boff;
1186 	while (len > 1) {
1187 		tmp = *bptr;
1188 		*to++ = tmp & 0xff;
1189 		*to++ = (tmp >> 8) & 0xff;
1190 		bptr += 2;
1191 		len -= 2;
1192 	}
1193 	if (len == 1)
1194 		*to = *bptr & 0xff;
1195 }
1196 
1197 void
1198 am7990_zerobuf_gap2(sc, boff, len)
1199 	struct am7990_softc *sc;
1200 	int boff, len;
1201 {
1202 	volatile caddr_t buf = sc->sc_mem;
1203 	register volatile u_int16_t *bptr;
1204 
1205 	if ((unsigned)boff & 0x1) {
1206 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
1207 		*bptr &= 0xff;
1208 		bptr += 2;
1209 		len--;
1210 	} else
1211 		bptr = ((volatile u_int16_t *)buf) + boff;
1212 	while (len > 0) {
1213 		*bptr = 0;
1214 		bptr += 2;
1215 		len -= 2;
1216 	}
1217 }
1218 
1219 /*
1220  * gap16: 16 bytes of data followed by 16 bytes of pad.
1221  *
1222  * Buffers must be 32-byte aligned.
1223  */
1224 
1225 void
1226 am7990_copytobuf_gap16(sc, fromv, boff, len)
1227 	struct am7990_softc *sc;
1228 	void *fromv;
1229 	int boff;
1230 	register int len;
1231 {
1232 	volatile caddr_t buf = sc->sc_mem;
1233 	register caddr_t from = fromv;
1234 	register caddr_t bptr;
1235 	register int xfer;
1236 
1237 	bptr = buf + ((boff << 1) & ~0x1f);
1238 	boff &= 0xf;
1239 	xfer = min(len, 16 - boff);
1240 	while (len > 0) {
1241 		bcopy(from, bptr + boff, xfer);
1242 		from += xfer;
1243 		bptr += 32;
1244 		boff = 0;
1245 		len -= xfer;
1246 		xfer = min(len, 16);
1247 	}
1248 }
1249 
1250 void
1251 am7990_copyfrombuf_gap16(sc, tov, boff, len)
1252 	struct am7990_softc *sc;
1253 	void *tov;
1254 	int boff, len;
1255 {
1256 	volatile caddr_t buf = sc->sc_mem;
1257 	register caddr_t to = tov;
1258 	register caddr_t bptr;
1259 	register int xfer;
1260 
1261 	bptr = buf + ((boff << 1) & ~0x1f);
1262 	boff &= 0xf;
1263 	xfer = min(len, 16 - boff);
1264 	while (len > 0) {
1265 		bcopy(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 am7990_zerobuf_gap16(sc, boff, len)
1276 	struct am7990_softc *sc;
1277 	int boff, len;
1278 {
1279 	volatile caddr_t buf = sc->sc_mem;
1280 	register caddr_t bptr;
1281 	register int xfer;
1282 
1283 	bptr = buf + ((boff << 1) & ~0x1f);
1284 	boff &= 0xf;
1285 	xfer = min(len, 16 - boff);
1286 	while (len > 0) {
1287 		bzero(bptr + boff, xfer);
1288 		bptr += 32;
1289 		boff = 0;
1290 		len -= xfer;
1291 		xfer = min(len, 16);
1292 	}
1293 }
1294 #endif /* Example only */
1295