xref: /freebsd/sys/dev/le/am79900.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 
138 static void	am79900_meminit(struct lance_softc *);
139 static void	am79900_rint(struct lance_softc *);
140 static void	am79900_tint(struct lance_softc *);
141 static void	am79900_start_locked(struct lance_softc *sc);
142 
143 #ifdef LEDEBUG
144 static void	am79900_recv_print(struct lance_softc *, int);
145 static void	am79900_xmit_print(struct lance_softc *, int);
146 #endif
147 
148 int
149 am79900_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 
180 void
181 am79900_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  */
190 static void
191 am79900_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 
256 static inline void
257 am79900_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 
349 static inline void
350 am79900_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  */
432 void
433 am79900_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 	isr = (*sc->sc_rdcsr)(sc, LE_CSR0);
449 #if defined(LEDEBUG) && LEDEBUG > 1
450 	if (sc->sc_flags & LE_DEBUG)
451 		if_printf(ifp, "%s: entering with isr=%04x\n", __func__, isr);
452 #endif
453 	if ((isr & LE_C0_INTR) == 0) {
454 		LE_UNLOCK(sc);
455 		return;
456 	}
457 
458 	/*
459 	 * Clear interrupt source flags and turn off interrupts. If we
460 	 * don't clear these flags before processing their sources we
461 	 * could completely miss some interrupt events as the NIC can
462 	 * change these flags while we're in this handler. We toggle
463 	 * the interrupt enable bit in order to keep receiving them
464 	 * (some chips work without this, some don't).
465 	 */
466 	(*sc->sc_wrcsr)(sc, LE_CSR0, isr & ~(LE_C0_INEA | LE_C0_TDMD |
467 	    LE_C0_STOP | LE_C0_STRT | LE_C0_INIT));
468 
469 	if (isr & LE_C0_ERR) {
470 		if (isr & LE_C0_BABL) {
471 #ifdef LEDEBUG
472 			if_printf(ifp, "babble\n");
473 #endif
474 			ifp->if_oerrors++;
475 		}
476 #if 0
477 		if (isr & LE_C0_CERR) {
478 			if_printf(ifp, "collision error\n");
479 			ifp->if_collisions++;
480 		}
481 #endif
482 		if (isr & LE_C0_MISS) {
483 #ifdef LEDEBUG
484 			if_printf(ifp, "missed packet\n");
485 #endif
486 			ifp->if_ierrors++;
487 		}
488 		if (isr & LE_C0_MERR) {
489 			if_printf(ifp, "memory error\n");
490 			lance_init_locked(sc);
491 			LE_UNLOCK(sc);
492 			return;
493 		}
494 	}
495 
496 	if ((isr & LE_C0_RXON) == 0) {
497 		if_printf(ifp, "receiver disabled\n");
498 		ifp->if_ierrors++;
499 		lance_init_locked(sc);
500 		LE_UNLOCK(sc);
501 		return;
502 	}
503 	if ((isr & LE_C0_TXON) == 0) {
504 		if_printf(ifp, "transmitter disabled\n");
505 		ifp->if_oerrors++;
506 		lance_init_locked(sc);
507 		LE_UNLOCK(sc);
508 		return;
509 	}
510 
511 	/*
512 	 * Pretend we have carrier; if we don't this will be cleared shortly.
513 	 */
514 	if (!(sc->sc_flags & LE_CARRIER))
515 		if_link_state_change(ifp, LINK_STATE_UP);
516 	sc->sc_flags |= LE_CARRIER;
517 
518 	if (isr & LE_C0_RINT)
519 		am79900_rint(sc);
520 	if (isr & LE_C0_TINT)
521 		am79900_tint(sc);
522 
523 	/* Enable interrupts again. */
524 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA);
525 
526 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
527 		am79900_start_locked(sc);
528 
529 	LE_UNLOCK(sc);
530 }
531 
532 /*
533  * Set up output on interface.
534  * Get another datagram to send off of the interface queue, and map it to the
535  * interface before starting the output.
536  */
537 static void
538 am79900_start_locked(struct lance_softc *sc)
539 {
540 	struct ifnet *ifp = sc->sc_ifp;
541 	struct letmd tmd;
542 	struct mbuf *m;
543 	int bix, enq, len, rp;
544 
545 	LE_LOCK_ASSERT(sc, MA_OWNED);
546 
547 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
548 	    IFF_DRV_RUNNING)
549 		return;
550 
551 	bix = sc->sc_last_td;
552 	enq = 0;
553 
554 	for (; sc->sc_no_td < sc->sc_ntbuf &&
555 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
556 		rp = LE_TMDADDR(sc, bix);
557 		(*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
558 
559 		if (LE_LE32TOH(tmd.tmd1) & LE_T1_OWN) {
560 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
561 			if_printf(ifp,
562 			    "missing buffer, no_td = %d, last_td = %d\n",
563 			    sc->sc_no_td, sc->sc_last_td);
564 		}
565 
566 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
567 		if (m == 0)
568 			break;
569 
570 		/*
571 		 * If BPF is listening on this interface, let it see the packet
572 		 * before we commit it to the wire.
573 		 */
574 		BPF_MTAP(ifp, m);
575 
576 		/*
577 		 * Copy the mbuf chain into the transmit buffer.
578 		 */
579 		len = lance_put(sc, LE_TBUFADDR(sc, bix), m);
580 
581 #ifdef LEDEBUG
582 		if (len > ETHERMTU + ETHER_HDR_LEN)
583 			if_printf(ifp, "packet length %d\n", len);
584 #endif
585 
586 		/*
587 		 * Init transmit registers, and set transmit start flag.
588 		 */
589 		tmd.tmd1 = LE_HTOLE32(LE_T1_OWN | LE_T1_STP | LE_T1_ENP |
590 		    LE_T1_ONES | (-len & 0xfff));
591 		tmd.tmd2 = 0;
592 		tmd.tmd3 = 0;
593 
594 		(*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
595 
596 #ifdef LEDEBUG
597 		if (sc->sc_flags & LE_DEBUG)
598 			am79900_xmit_print(sc, bix);
599 #endif
600 
601 		(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
602 		enq++;
603 
604 		if (++bix == sc->sc_ntbuf)
605 			bix = 0;
606 
607 		if (++sc->sc_no_td == sc->sc_ntbuf) {
608 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
609 			break;
610 		}
611 	}
612 
613 	sc->sc_last_td = bix;
614 
615 	if (enq > 0)
616 		sc->sc_wdog_timer = 5;
617 }
618 
619 #ifdef LEDEBUG
620 static void
621 am79900_recv_print(struct lance_softc *sc, int no)
622 {
623 	struct ifnet *ifp = sc->sc_ifp;
624 	struct ether_header eh;
625 	struct lermd rmd;
626 	uint16_t len;
627 
628 	(*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
629 	len = LE_LE32TOH(rmd.rmd2) & 0xfff;
630 	if_printf(ifp, "receive buffer %d, len = %d\n", no, len);
631 	if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
632 	if_printf(ifp, "adr %08x, flags/blen %08x\n", LE_LE32TOH(rmd.rmd0),
633 	    LE_LE32TOH(rmd.rmd1));
634 	if (len - ETHER_CRC_LEN >= sizeof(eh)) {
635 		(*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
636 		if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
637 		printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
638 		    ntohs(eh.ether_type));
639 	}
640 }
641 
642 static void
643 am79900_xmit_print(struct lance_softc *sc, int no)
644 {
645 	struct ifnet *ifp = sc->sc_ifp;
646 	struct ether_header eh;
647 	struct letmd tmd;
648 	uint16_t len;
649 
650 	(*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
651 	len = -(LE_LE32TOH(tmd.tmd1) & 0xfff);
652 	if_printf(ifp, "transmit buffer %d, len = %d\n", no, len);
653 	if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
654 	if_printf(ifp, "adr %08x, flags/blen %08x\n", LE_LE32TOH(tmd.tmd0),
655 	    LE_LE32TOH(tmd.tmd1));
656 	if (len >= sizeof(eh)) {
657 		(*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
658 		if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
659 		printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
660 		    ntohs(eh.ether_type));
661 	}
662 }
663 #endif /* LEDEBUG */
664