1 /* $NetBSD: am79900.c,v 1.17 2005/12/24 20:27:29 perry Exp $ */
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
40 * Copyright (c) 1992, 1993
41 * The Regents of the University of California. All rights reserved.
43 * This code is derived from software contributed to Berkeley by
44 * Ralph Campbell and Rick Macklem.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
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.
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
70 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
75 * Matthias Drochner. All rights reserved.
76 * Copyright (c) 1995 Charles M. Hannum. All rights reserved.
78 * This code is derived from software contributed to Berkeley by
79 * Ralph Campbell and Rick Macklem.
81 * Redistribution and use in source and binary forms, with or without
82 * modification, are permitted provided that the following conditions
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.
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
109 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
112 #include <sys/cdefs.h>
113 __FBSDID("$FreeBSD$");
115 #include <sys/param.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>
124 #include <net/ethernet.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>
131 #include <machine/bus.h>
133 #include <dev/le/lancereg.h>
134 #include <dev/le/lancevar.h>
135 #include <dev/le/am79900reg.h>
136 #include <dev/le/am79900var.h>
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);
144 static void am79900_recv_print(struct lance_softc *, int);
145 static void am79900_xmit_print(struct lance_softc *, int);
149 am79900_config(struct am79900_softc *sc, const char* name, int unit)
153 sc->lsc.sc_meminit = am79900_meminit;
154 sc->lsc.sc_start_locked = am79900_start_locked;
156 error = lance_config(&sc->lsc, name, unit);
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;
172 if (mem > sc->lsc.sc_memsize)
173 panic("%s: memsize", __func__);
175 lance_attach(&sc->lsc);
181 am79900_detach(struct am79900_softc *sc)
184 lance_detach(&sc->lsc);
188 * Set up the initialization block and the descriptor rings.
191 am79900_meminit(struct lance_softc *sc)
193 struct ifnet *ifp = sc->sc_ifp;
200 LE_LOCK_ASSERT(sc, MA_OWNED);
202 if (ifp->if_flags & IFF_PROMISC)
203 init.init_mode = LE_HTOLE32(LE_MODE_NORMAL | LE_MODE_PROM);
205 init.init_mode = LE_HTOLE32(LE_MODE_NORMAL);
207 init.init_mode |= LE_HTOLE32(((ffs(sc->sc_ntbuf) - 1) << 28) |
208 ((ffs(sc->sc_nrbuf) - 1) << 20));
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);
218 sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
220 a = sc->sc_addr + LE_RMDADDR(sc, 0);
221 init.init_rdra = LE_HTOLE32(a);
223 a = sc->sc_addr + LE_TMDADDR(sc, 0);
224 init.init_tdra = LE_HTOLE32(a);
226 (*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
229 * Set up receive ring descriptors.
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 |
238 (*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
243 * Set up transmit ring descriptors.
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);
251 (*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
257 am79900_rint(struct lance_softc *sc)
259 struct ifnet *ifp = sc->sc_ifp;
264 #if defined(__i386__) && !defined(PC98)
265 struct ether_header *eh;
268 bix = sc->sc_last_rd;
270 /* Process all buffers with valid data. */
272 rp = LE_RMDADDR(sc, bix);
273 (*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
275 rmd1 = LE_LE32TOH(rmd.rmd1);
276 if (rmd1 & LE_R1_OWN)
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) {
284 if (rmd1 & LE_R1_ENP) {
285 if ((rmd1 & LE_R1_OFLO) == 0) {
286 if (rmd1 & LE_R1_FRAM)
289 if (rmd1 & LE_R1_CRC)
294 if (rmd1 & LE_R1_OFLO)
295 if_printf(ifp, "overflow\n");
297 if (rmd1 & LE_R1_BUFF)
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");
305 if (sc->sc_flags & LE_DEBUG)
306 am79900_recv_print(sc, bix);
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);
313 rmd.rmd1 = LE_HTOLE32(LE_R1_OWN | LE_R1_ONES |
317 (*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
319 if (++bix == sc->sc_nrbuf)
325 #if defined(__i386__) && !defined(PC98)
327 * The VMware LANCE does not present IFF_SIMPLEX
328 * behavior on multicast packets. Thus drop the
329 * packet if it is from ourselves.
331 eh = mtod(m, struct ether_header *);
332 if (!ether_cmp(eh->ether_shost, sc->sc_enaddr)) {
338 /* Pass the packet up. */
340 (*ifp->if_input)(ifp, m);
346 sc->sc_last_rd = bix;
350 am79900_tint(struct lance_softc *sc)
352 struct ifnet *ifp = sc->sc_ifp;
357 bix = sc->sc_first_td;
360 if (sc->sc_no_td <= 0)
363 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
366 tmd1 = LE_LE32TOH(tmd.tmd1);
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);
375 if (tmd1 & LE_T1_OWN)
378 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
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);
390 if (tmd2 & LE_T2_LCAR) {
391 if (sc->sc_flags & LE_CARRIER)
392 if_link_state_change(ifp,
394 sc->sc_flags &= ~LE_CARRIER;
395 if (sc->sc_nocarrier)
396 (*sc->sc_nocarrier)(sc);
398 if_printf(ifp, "lost carrier\n");
400 if (tmd2 & LE_T2_LCOL)
401 ifp->if_collisions++;
402 if (tmd2 & LE_T2_RTRY) {
404 if_printf(ifp, "excessive collisions\n");
406 ifp->if_collisions += 16;
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;
418 if (++bix == sc->sc_ntbuf)
424 sc->sc_first_td = bix;
426 sc->sc_wdog_timer = sc->sc_no_td > 0 ? 5 : 0;
430 * Controller interrupt
433 am79900_intr(void *arg)
435 struct lance_softc *sc = arg;
436 struct ifnet *ifp = sc->sc_ifp;
441 if (sc->sc_hwintr && (*sc->sc_hwintr)(sc) == -1) {
443 lance_init_locked(sc);
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);
453 if ((isr & LE_C0_INTR) == 0) {
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).
466 (*sc->sc_wrcsr)(sc, LE_CSR0, isr & ~(LE_C0_INEA | LE_C0_TDMD |
467 LE_C0_STOP | LE_C0_STRT | LE_C0_INIT));
469 if (isr & LE_C0_ERR) {
470 if (isr & LE_C0_BABL) {
472 if_printf(ifp, "babble\n");
477 if (isr & LE_C0_CERR) {
478 if_printf(ifp, "collision error\n");
479 ifp->if_collisions++;
482 if (isr & LE_C0_MISS) {
484 if_printf(ifp, "missed packet\n");
488 if (isr & LE_C0_MERR) {
489 if_printf(ifp, "memory error\n");
490 lance_init_locked(sc);
496 if ((isr & LE_C0_RXON) == 0) {
497 if_printf(ifp, "receiver disabled\n");
499 lance_init_locked(sc);
503 if ((isr & LE_C0_TXON) == 0) {
504 if_printf(ifp, "transmitter disabled\n");
506 lance_init_locked(sc);
512 * Pretend we have carrier; if we don't this will be cleared shortly.
514 if (!(sc->sc_flags & LE_CARRIER))
515 if_link_state_change(ifp, LINK_STATE_UP);
516 sc->sc_flags |= LE_CARRIER;
518 if (isr & LE_C0_RINT)
520 if (isr & LE_C0_TINT)
523 /* Enable interrupts again. */
524 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA);
526 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
527 am79900_start_locked(sc);
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.
538 am79900_start_locked(struct lance_softc *sc)
540 struct ifnet *ifp = sc->sc_ifp;
543 int bix, enq, len, rp;
545 LE_LOCK_ASSERT(sc, MA_OWNED);
547 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
551 bix = sc->sc_last_td;
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));
559 if (LE_LE32TOH(tmd.tmd1) & LE_T1_OWN) {
560 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
562 "missing buffer, no_td = %d, last_td = %d\n",
563 sc->sc_no_td, sc->sc_last_td);
566 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
571 * If BPF is listening on this interface, let it see the packet
572 * before we commit it to the wire.
577 * Copy the mbuf chain into the transmit buffer.
579 len = lance_put(sc, LE_TBUFADDR(sc, bix), m);
582 if (len > ETHERMTU + ETHER_HDR_LEN)
583 if_printf(ifp, "packet length %d\n", len);
587 * Init transmit registers, and set transmit start flag.
589 tmd.tmd1 = LE_HTOLE32(LE_T1_OWN | LE_T1_STP | LE_T1_ENP |
590 LE_T1_ONES | (-len & 0xfff));
594 (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
597 if (sc->sc_flags & LE_DEBUG)
598 am79900_xmit_print(sc, bix);
601 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
604 if (++bix == sc->sc_ntbuf)
607 if (++sc->sc_no_td == sc->sc_ntbuf) {
608 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
613 sc->sc_last_td = bix;
616 sc->sc_wdog_timer = 5;
621 am79900_recv_print(struct lance_softc *sc, int no)
623 struct ifnet *ifp = sc->sc_ifp;
624 struct ether_header eh;
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));
643 am79900_xmit_print(struct lance_softc *sc, int no)
645 struct ifnet *ifp = sc->sc_ifp;
646 struct ether_header eh;
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));