1/*	$NetBSD: am79900.c,v 1.17 2005/12/24 20:27:29 perry Exp $	*/
2
3/*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the NetBSD
21 *	Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39/*-
40 * Copyright (c) 1992, 1993
41 *	The Regents of the University of California.  All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * Ralph Campbell and Rick Macklem.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 *    notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 *    notice, this list of conditions and the following disclaimer in the
53 *    documentation and/or other materials provided with the distribution.
54 * 3. Neither the name of the University nor the names of its contributors
55 *    may be used to endorse or promote products derived from this software
56 *    without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
71 */
72
73/*-
74 * Copyright (c) 1998
75 *	Matthias Drochner.  All rights reserved.
76 * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
77 *
78 * This code is derived from software contributed to Berkeley by
79 * Ralph Campbell and Rick Macklem.
80 *
81 * Redistribution and use in source and binary forms, with or without
82 * modification, are permitted provided that the following conditions
83 * are met:
84 * 1. Redistributions of source code must retain the above copyright
85 *    notice, this list of conditions and the following disclaimer.
86 * 2. Redistributions in binary form must reproduce the above copyright
87 *    notice, this list of conditions and the following disclaimer in the
88 *    documentation and/or other materials provided with the distribution.
89 * 3. All advertising materials mentioning features or use of this software
90 *    must display the following acknowledgement:
91 *	This product includes software developed by the University of
92 *	California, Berkeley and its contributors.
93 * 4. Neither the name of the University nor the names of its contributors
94 *    may be used to endorse or promote products derived from this software
95 *    without specific prior written permission.
96 *
97 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
98 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
99 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
100 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
101 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
102 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
103 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
104 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
105 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
106 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
107 * SUCH DAMAGE.
108 *
109 *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
110 */
111
112#include <sys/cdefs.h>
113__FBSDID("$FreeBSD$");
114
115#include <sys/param.h>
116#include <sys/bus.h>
117#include <sys/endian.h>
118#include <sys/lock.h>
119#include <sys/mbuf.h>
120#include <sys/mutex.h>
121#include <sys/socket.h>
122
123#include <net/bpf.h>
124#include <net/ethernet.h>
125#include <net/if.h>
126#include <net/if_arp.h>
127#include <net/if_dl.h>
128#include <net/if_media.h>
129#include <net/if_var.h>
130
131#include <machine/bus.h>
132
133#include <dev/le/lancereg.h>
134#include <dev/le/lancevar.h>
135#include <dev/le/am79900reg.h>
136#include <dev/le/am79900var.h>
137
138static void	am79900_meminit(struct lance_softc *);
139static void	am79900_rint(struct lance_softc *);
140static void	am79900_tint(struct lance_softc *);
141static void	am79900_start_locked(struct lance_softc *sc);
142
143#ifdef LEDEBUG
144static void	am79900_recv_print(struct lance_softc *, int);
145static void	am79900_xmit_print(struct lance_softc *, int);
146#endif
147
148int
149am79900_config(struct am79900_softc *sc, const char* name, int unit)
150{
151	int error, mem;
152
153	sc->lsc.sc_meminit = am79900_meminit;
154	sc->lsc.sc_start_locked = am79900_start_locked;
155
156	error = lance_config(&sc->lsc, name, unit);
157	if (error != 0)
158		return (error);
159
160	mem = 0;
161	sc->lsc.sc_initaddr = mem;
162	mem += sizeof(struct leinit);
163	sc->lsc.sc_rmdaddr = mem;
164	mem += sizeof(struct lermd) * sc->lsc.sc_nrbuf;
165	sc->lsc.sc_tmdaddr = mem;
166	mem += sizeof(struct letmd) * sc->lsc.sc_ntbuf;
167	sc->lsc.sc_rbufaddr = mem;
168	mem += LEBLEN * sc->lsc.sc_nrbuf;
169	sc->lsc.sc_tbufaddr = mem;
170	mem += LEBLEN * sc->lsc.sc_ntbuf;
171
172	if (mem > sc->lsc.sc_memsize)
173		panic("%s: memsize", __func__);
174
175	lance_attach(&sc->lsc);
176
177	return (0);
178}
179
180void
181am79900_detach(struct am79900_softc *sc)
182{
183
184	lance_detach(&sc->lsc);
185}
186
187/*
188 * Set up the initialization block and the descriptor rings.
189 */
190static void
191am79900_meminit(struct lance_softc *sc)
192{
193	struct ifnet *ifp = sc->sc_ifp;
194	struct leinit init;
195	struct lermd rmd;
196	struct letmd tmd;
197	u_long a;
198	int bix;
199
200	LE_LOCK_ASSERT(sc, MA_OWNED);
201
202	if (ifp->if_flags & IFF_PROMISC)
203		init.init_mode = LE_HTOLE32(LE_MODE_NORMAL | LE_MODE_PROM);
204	else
205		init.init_mode = LE_HTOLE32(LE_MODE_NORMAL);
206
207	init.init_mode |= LE_HTOLE32(((ffs(sc->sc_ntbuf) - 1) << 28) |
208	    ((ffs(sc->sc_nrbuf) - 1) << 20));
209
210	init.init_padr[0] = LE_HTOLE32(sc->sc_enaddr[0] |
211	    (sc->sc_enaddr[1] << 8) | (sc->sc_enaddr[2] << 16) |
212	    (sc->sc_enaddr[3] << 24));
213	init.init_padr[1] = LE_HTOLE32(sc->sc_enaddr[4] |
214	    (sc->sc_enaddr[5] << 8));
215	lance_setladrf(sc, init.init_ladrf);
216
217	sc->sc_last_rd = 0;
218	sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
219
220	a = sc->sc_addr + LE_RMDADDR(sc, 0);
221	init.init_rdra = LE_HTOLE32(a);
222
223	a = sc->sc_addr + LE_TMDADDR(sc, 0);
224	init.init_tdra = LE_HTOLE32(a);
225
226	(*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
227
228	/*
229	 * Set up receive ring descriptors.
230	 */
231	for (bix = 0; bix < sc->sc_nrbuf; bix++) {
232		a = sc->sc_addr + LE_RBUFADDR(sc, bix);
233		rmd.rmd0 = LE_HTOLE32(a);
234		rmd.rmd1 = LE_HTOLE32(LE_R1_OWN | LE_R1_ONES |
235		    (-LEBLEN & 0xfff));
236		rmd.rmd2 = 0;
237		rmd.rmd3 = 0;
238		(*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
239		    sizeof(rmd));
240	}
241
242	/*
243	 * Set up transmit ring descriptors.
244	 */
245	for (bix = 0; bix < sc->sc_ntbuf; bix++) {
246		a = sc->sc_addr + LE_TBUFADDR(sc, bix);
247		tmd.tmd0 = LE_HTOLE32(a);
248		tmd.tmd1 = LE_HTOLE32(LE_T1_ONES);
249		tmd.tmd2 = 0;
250		tmd.tmd3 = 0;
251		(*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
252		    sizeof(tmd));
253	}
254}
255
256static inline void
257am79900_rint(struct lance_softc *sc)
258{
259	struct ifnet *ifp = sc->sc_ifp;
260	struct mbuf *m;
261	struct lermd rmd;
262	uint32_t rmd1;
263	int bix, rp;
264#if defined(__i386__) && !defined(PC98)
265	struct ether_header *eh;
266#endif
267
268	bix = sc->sc_last_rd;
269
270	/* Process all buffers with valid data. */
271	for (;;) {
272		rp = LE_RMDADDR(sc, bix);
273		(*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
274
275		rmd1 = LE_LE32TOH(rmd.rmd1);
276		if (rmd1 & LE_R1_OWN)
277			break;
278
279		m = NULL;
280		if ((rmd1 & (LE_R1_ERR | LE_R1_STP | LE_R1_ENP)) !=
281		    (LE_R1_STP | LE_R1_ENP)){
282			if (rmd1 & LE_R1_ERR) {
283#ifdef LEDEBUG
284				if (rmd1 & LE_R1_ENP) {
285					if ((rmd1 & LE_R1_OFLO) == 0) {
286						if (rmd1 & LE_R1_FRAM)
287							if_printf(ifp,
288							    "framing error\n");
289						if (rmd1 & LE_R1_CRC)
290							if_printf(ifp,
291							    "crc mismatch\n");
292					}
293				} else
294					if (rmd1 & LE_R1_OFLO)
295						if_printf(ifp, "overflow\n");
296#endif
297				if (rmd1 & LE_R1_BUFF)
298					if_printf(ifp,
299					    "receive buffer error\n");
300			} else if ((rmd1 & (LE_R1_STP | LE_R1_ENP)) !=
301			    (LE_R1_STP | LE_R1_ENP))
302				if_printf(ifp, "dropping chained buffer\n");
303		} else {
304#ifdef LEDEBUG
305			if (sc->sc_flags & LE_DEBUG)
306				am79900_recv_print(sc, bix);
307#endif
308			/* Pull the packet off the interface. */
309			m = lance_get(sc, LE_RBUFADDR(sc, bix),
310			    (LE_LE32TOH(rmd.rmd2) & 0xfff) - ETHER_CRC_LEN);
311		}
312
313		rmd.rmd1 = LE_HTOLE32(LE_R1_OWN | LE_R1_ONES |
314		    (-LEBLEN & 0xfff));
315		rmd.rmd2 = 0;
316		rmd.rmd3 = 0;
317		(*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
318
319		if (++bix == sc->sc_nrbuf)
320			bix = 0;
321
322		if (m != NULL) {
323			ifp->if_ipackets++;
324
325#if defined(__i386__) && !defined(PC98)
326			/*
327			 * The VMware LANCE does not present IFF_SIMPLEX
328			 * behavior on multicast packets. Thus drop the
329			 * packet if it is from ourselves.
330			 */
331			eh = mtod(m, struct ether_header *);
332			if (!ether_cmp(eh->ether_shost, sc->sc_enaddr)) {
333				m_freem(m);
334				continue;
335			}
336#endif
337
338			/* Pass the packet up. */
339			LE_UNLOCK(sc);
340			(*ifp->if_input)(ifp, m);
341			LE_LOCK(sc);
342		} else
343			ifp->if_ierrors++;
344	}
345
346	sc->sc_last_rd = bix;
347}
348
349static inline void
350am79900_tint(struct lance_softc *sc)
351{
352	struct ifnet *ifp = sc->sc_ifp;
353	struct letmd tmd;
354	uint32_t tmd1, tmd2;
355	int bix;
356
357	bix = sc->sc_first_td;
358
359	for (;;) {
360		if (sc->sc_no_td <= 0)
361			break;
362
363		(*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
364		    sizeof(tmd));
365
366		tmd1 = LE_LE32TOH(tmd.tmd1);
367
368#ifdef LEDEBUG
369		if (sc->sc_flags & LE_DEBUG)
370			if_printf(ifp, "trans tmd: "
371			    "adr %08x, flags/blen %08x\n",
372			    LE_LE32TOH(tmd.tmd0), tmd1);
373#endif
374
375		if (tmd1 & LE_T1_OWN)
376			break;
377
378		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
379
380		if (tmd1 & LE_T1_ERR) {
381			tmd2 = LE_LE32TOH(tmd.tmd2);
382			if (tmd2 & LE_T2_BUFF)
383				if_printf(ifp, "transmit buffer error\n");
384			else if (tmd2 & LE_T2_UFLO)
385				if_printf(ifp, "underflow\n");
386			if (tmd2 & (LE_T2_BUFF | LE_T2_UFLO)) {
387				lance_init_locked(sc);
388				return;
389			}
390			if (tmd2 & LE_T2_LCAR) {
391				if (sc->sc_flags & LE_CARRIER)
392					if_link_state_change(ifp,
393					    LINK_STATE_DOWN);
394				sc->sc_flags &= ~LE_CARRIER;
395				if (sc->sc_nocarrier)
396					(*sc->sc_nocarrier)(sc);
397				else
398					if_printf(ifp, "lost carrier\n");
399			}
400			if (tmd2 & LE_T2_LCOL)
401				ifp->if_collisions++;
402			if (tmd2 & LE_T2_RTRY) {
403#ifdef LEDEBUG
404				if_printf(ifp, "excessive collisions\n");
405#endif
406				ifp->if_collisions += 16;
407			}
408			ifp->if_oerrors++;
409		} else {
410			if (tmd1 & LE_T1_ONE)
411				ifp->if_collisions++;
412			else if (tmd1 & LE_T1_MORE)
413				/* Real number is unknown. */
414				ifp->if_collisions += 2;
415			ifp->if_opackets++;
416		}
417
418		if (++bix == sc->sc_ntbuf)
419			bix = 0;
420
421		--sc->sc_no_td;
422	}
423
424	sc->sc_first_td = bix;
425
426	sc->sc_wdog_timer = sc->sc_no_td > 0 ? 5 : 0;
427}
428
429/*
430 * Controller interrupt
431 */
432void
433am79900_intr(void *arg)
434{
435	struct lance_softc *sc = arg;
436	struct ifnet *ifp = sc->sc_ifp;
437	uint16_t isr;
438
439	LE_LOCK(sc);
440
441	if (sc->sc_hwintr && (*sc->sc_hwintr)(sc) == -1) {
442		ifp->if_ierrors++;
443		lance_init_locked(sc);
444		LE_UNLOCK(sc);
445		return;
446	}
447
448#ifndef __HAIKU__
449	isr = (*sc->sc_rdcsr)(sc, LE_CSR0);
450#else
451	{
452		HAIKU_INTR_REGISTER_STATE;
453		HAIKU_INTR_REGISTER_ENTER();
454		isr = sc->sc_lastisr;
455		sc->sc_lastisr = 0;
456		HAIKU_INTR_REGISTER_LEAVE();
457	}
458#endif
459#if defined(LEDEBUG) && LEDEBUG > 1
460	if (sc->sc_flags & LE_DEBUG)
461		if_printf(ifp, "%s: entering with isr=%04x\n", __func__, isr);
462#endif
463	if ((isr & LE_C0_INTR) == 0) {
464		LE_UNLOCK(sc);
465		return;
466	}
467
468	/*
469	 * Clear interrupt source flags and turn off interrupts. If we
470	 * don't clear these flags before processing their sources we
471	 * could completely miss some interrupt events as the NIC can
472	 * change these flags while we're in this handler. We toggle
473	 * the interrupt enable bit in order to keep receiving them
474	 * (some chips work without this, some don't).
475	 */
476	(*sc->sc_wrcsr)(sc, LE_CSR0, isr & ~(LE_C0_INEA | LE_C0_TDMD |
477	    LE_C0_STOP | LE_C0_STRT | LE_C0_INIT));
478
479	if (isr & LE_C0_ERR) {
480		if (isr & LE_C0_BABL) {
481#ifdef LEDEBUG
482			if_printf(ifp, "babble\n");
483#endif
484			ifp->if_oerrors++;
485		}
486#if 0
487		if (isr & LE_C0_CERR) {
488			if_printf(ifp, "collision error\n");
489			ifp->if_collisions++;
490		}
491#endif
492		if (isr & LE_C0_MISS) {
493#ifdef LEDEBUG
494			if_printf(ifp, "missed packet\n");
495#endif
496			ifp->if_ierrors++;
497		}
498		if (isr & LE_C0_MERR) {
499			if_printf(ifp, "memory error\n");
500			lance_init_locked(sc);
501			LE_UNLOCK(sc);
502			return;
503		}
504	}
505
506	if ((isr & LE_C0_RXON) == 0) {
507		if_printf(ifp, "receiver disabled\n");
508		ifp->if_ierrors++;
509		lance_init_locked(sc);
510		LE_UNLOCK(sc);
511		return;
512	}
513	if ((isr & LE_C0_TXON) == 0) {
514		if_printf(ifp, "transmitter disabled\n");
515		ifp->if_oerrors++;
516		lance_init_locked(sc);
517		LE_UNLOCK(sc);
518		return;
519	}
520
521	/*
522	 * Pretend we have carrier; if we don't this will be cleared shortly.
523	 */
524	if (!(sc->sc_flags & LE_CARRIER))
525		if_link_state_change(ifp, LINK_STATE_UP);
526	sc->sc_flags |= LE_CARRIER;
527
528	if (isr & LE_C0_RINT)
529		am79900_rint(sc);
530	if (isr & LE_C0_TINT)
531		am79900_tint(sc);
532
533	/* Enable interrupts again. */
534	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA);
535
536	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
537		am79900_start_locked(sc);
538
539	LE_UNLOCK(sc);
540}
541
542/*
543 * Set up output on interface.
544 * Get another datagram to send off of the interface queue, and map it to the
545 * interface before starting the output.
546 */
547static void
548am79900_start_locked(struct lance_softc *sc)
549{
550	struct ifnet *ifp = sc->sc_ifp;
551	struct letmd tmd;
552	struct mbuf *m;
553	int bix, enq, len, rp;
554
555	LE_LOCK_ASSERT(sc, MA_OWNED);
556
557	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
558	    IFF_DRV_RUNNING)
559		return;
560
561	bix = sc->sc_last_td;
562	enq = 0;
563
564	for (; sc->sc_no_td < sc->sc_ntbuf &&
565	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
566		rp = LE_TMDADDR(sc, bix);
567		(*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
568
569		if (LE_LE32TOH(tmd.tmd1) & LE_T1_OWN) {
570			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
571			if_printf(ifp,
572			    "missing buffer, no_td = %d, last_td = %d\n",
573			    sc->sc_no_td, sc->sc_last_td);
574		}
575
576		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
577		if (m == 0)
578			break;
579
580		/*
581		 * If BPF is listening on this interface, let it see the packet
582		 * before we commit it to the wire.
583		 */
584		BPF_MTAP(ifp, m);
585
586		/*
587		 * Copy the mbuf chain into the transmit buffer.
588		 */
589		len = lance_put(sc, LE_TBUFADDR(sc, bix), m);
590
591#ifdef LEDEBUG
592		if (len > ETHERMTU + ETHER_HDR_LEN)
593			if_printf(ifp, "packet length %d\n", len);
594#endif
595
596		/*
597		 * Init transmit registers, and set transmit start flag.
598		 */
599		tmd.tmd1 = LE_HTOLE32(LE_T1_OWN | LE_T1_STP | LE_T1_ENP |
600		    LE_T1_ONES | (-len & 0xfff));
601		tmd.tmd2 = 0;
602		tmd.tmd3 = 0;
603
604		(*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
605
606#ifdef LEDEBUG
607		if (sc->sc_flags & LE_DEBUG)
608			am79900_xmit_print(sc, bix);
609#endif
610
611		(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
612		enq++;
613
614		if (++bix == sc->sc_ntbuf)
615			bix = 0;
616
617		if (++sc->sc_no_td == sc->sc_ntbuf) {
618			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
619			break;
620		}
621	}
622
623	sc->sc_last_td = bix;
624
625	if (enq > 0)
626		sc->sc_wdog_timer = 5;
627}
628
629#ifdef LEDEBUG
630static void
631am79900_recv_print(struct lance_softc *sc, int no)
632{
633	struct ifnet *ifp = sc->sc_ifp;
634	struct ether_header eh;
635	struct lermd rmd;
636	uint16_t len;
637
638	(*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
639	len = LE_LE32TOH(rmd.rmd2) & 0xfff;
640	if_printf(ifp, "receive buffer %d, len = %d\n", no, len);
641	if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
642	if_printf(ifp, "adr %08x, flags/blen %08x\n", LE_LE32TOH(rmd.rmd0),
643	    LE_LE32TOH(rmd.rmd1));
644	if (len - ETHER_CRC_LEN >= sizeof(eh)) {
645		(*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
646		if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
647		printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
648		    ntohs(eh.ether_type));
649	}
650}
651
652static void
653am79900_xmit_print(struct lance_softc *sc, int no)
654{
655	struct ifnet *ifp = sc->sc_ifp;
656	struct ether_header eh;
657	struct letmd tmd;
658	uint16_t len;
659
660	(*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
661	len = -(LE_LE32TOH(tmd.tmd1) & 0xfff);
662	if_printf(ifp, "transmit buffer %d, len = %d\n", no, len);
663	if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
664	if_printf(ifp, "adr %08x, flags/blen %08x\n", LE_LE32TOH(tmd.tmd0),
665	    LE_LE32TOH(tmd.tmd1));
666	if (len >= sizeof(eh)) {
667		(*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
668		if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
669		printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
670		    ntohs(eh.ether_type));
671	}
672}
673#endif /* LEDEBUG */
674