1 /* $NetBSD: am79900.c,v 1.17 2005/12/24 20:27:29 perry Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD AND BSD-3-Clause
6 * Copyright (c) 1997 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
35 * Copyright (c) 1992, 1993
36 * The Regents of the University of California. All rights reserved.
38 * This code is derived from software contributed to Berkeley by
39 * Ralph Campbell and Rick Macklem.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
70 * Matthias Drochner. All rights reserved.
71 * Copyright (c) 1995 Charles M. Hannum. All rights reserved.
73 * This code is derived from software contributed to Berkeley by
74 * Ralph Campbell and Rick Macklem.
76 * Redistribution and use in source and binary forms, with or without
77 * modification, are permitted provided that the following conditions
79 * 1. Redistributions of source code must retain the above copyright
80 * notice, this list of conditions and the following disclaimer.
81 * 2. Redistributions in binary form must reproduce the above copyright
82 * notice, this list of conditions and the following disclaimer in the
83 * documentation and/or other materials provided with the distribution.
84 * 3. All advertising materials mentioning features or use of this software
85 * must display the following acknowledgement:
86 * This product includes software developed by the University of
87 * California, Berkeley and its contributors.
88 * 4. Neither the name of the University nor the names of its contributors
89 * may be used to endorse or promote products derived from this software
90 * without specific prior written permission.
92 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
93 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
95 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
96 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
97 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
98 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
100 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
101 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
107 #include <sys/cdefs.h>
108 __FBSDID("$FreeBSD$");
110 #include <sys/param.h>
112 #include <sys/endian.h>
113 #include <sys/lock.h>
114 #include <sys/mbuf.h>
115 #include <sys/mutex.h>
116 #include <sys/socket.h>
119 #include <net/ethernet.h>
121 #include <net/if_arp.h>
122 #include <net/if_dl.h>
123 #include <net/if_media.h>
124 #include <net/if_var.h>
126 #include <machine/bus.h>
128 #include <dev/le/lancereg.h>
129 #include <dev/le/lancevar.h>
130 #include <dev/le/am79900reg.h>
131 #include <dev/le/am79900var.h>
133 static void am79900_meminit(struct lance_softc *);
134 static void am79900_rint(struct lance_softc *);
135 static void am79900_tint(struct lance_softc *);
136 static void am79900_start_locked(struct lance_softc *sc);
139 static void am79900_recv_print(struct lance_softc *, int);
140 static void am79900_xmit_print(struct lance_softc *, int);
144 am79900_config(struct am79900_softc *sc, const char* name, int unit)
148 sc->lsc.sc_meminit = am79900_meminit;
149 sc->lsc.sc_start_locked = am79900_start_locked;
151 error = lance_config(&sc->lsc, name, unit);
156 sc->lsc.sc_initaddr = mem;
157 mem += sizeof(struct leinit);
158 sc->lsc.sc_rmdaddr = mem;
159 mem += sizeof(struct lermd) * sc->lsc.sc_nrbuf;
160 sc->lsc.sc_tmdaddr = mem;
161 mem += sizeof(struct letmd) * sc->lsc.sc_ntbuf;
162 sc->lsc.sc_rbufaddr = mem;
163 mem += LEBLEN * sc->lsc.sc_nrbuf;
164 sc->lsc.sc_tbufaddr = mem;
165 mem += LEBLEN * sc->lsc.sc_ntbuf;
167 if (mem > sc->lsc.sc_memsize)
168 panic("%s: memsize", __func__);
170 lance_attach(&sc->lsc);
176 am79900_detach(struct am79900_softc *sc)
179 lance_detach(&sc->lsc);
183 * Set up the initialization block and the descriptor rings.
186 am79900_meminit(struct lance_softc *sc)
188 struct ifnet *ifp = sc->sc_ifp;
195 LE_LOCK_ASSERT(sc, MA_OWNED);
197 if (ifp->if_flags & IFF_PROMISC)
198 init.init_mode = LE_HTOLE32(LE_MODE_NORMAL | LE_MODE_PROM);
200 init.init_mode = LE_HTOLE32(LE_MODE_NORMAL);
202 init.init_mode |= LE_HTOLE32(((ffs(sc->sc_ntbuf) - 1) << 28) |
203 ((ffs(sc->sc_nrbuf) - 1) << 20));
205 init.init_padr[0] = LE_HTOLE32(sc->sc_enaddr[0] |
206 (sc->sc_enaddr[1] << 8) | (sc->sc_enaddr[2] << 16) |
207 (sc->sc_enaddr[3] << 24));
208 init.init_padr[1] = LE_HTOLE32(sc->sc_enaddr[4] |
209 (sc->sc_enaddr[5] << 8));
210 lance_setladrf(sc, init.init_ladrf);
213 sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
215 a = sc->sc_addr + LE_RMDADDR(sc, 0);
216 init.init_rdra = LE_HTOLE32(a);
218 a = sc->sc_addr + LE_TMDADDR(sc, 0);
219 init.init_tdra = LE_HTOLE32(a);
221 (*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
224 * Set up receive ring descriptors.
226 for (bix = 0; bix < sc->sc_nrbuf; bix++) {
227 a = sc->sc_addr + LE_RBUFADDR(sc, bix);
228 rmd.rmd0 = LE_HTOLE32(a);
229 rmd.rmd1 = LE_HTOLE32(LE_R1_OWN | LE_R1_ONES |
233 (*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
238 * Set up transmit ring descriptors.
240 for (bix = 0; bix < sc->sc_ntbuf; bix++) {
241 a = sc->sc_addr + LE_TBUFADDR(sc, bix);
242 tmd.tmd0 = LE_HTOLE32(a);
243 tmd.tmd1 = LE_HTOLE32(LE_T1_ONES);
246 (*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
252 am79900_rint(struct lance_softc *sc)
254 struct ifnet *ifp = sc->sc_ifp;
259 #if defined(__i386__)
260 struct ether_header *eh;
263 bix = sc->sc_last_rd;
265 /* Process all buffers with valid data. */
267 rp = LE_RMDADDR(sc, bix);
268 (*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
270 rmd1 = LE_LE32TOH(rmd.rmd1);
271 if (rmd1 & LE_R1_OWN)
275 if ((rmd1 & (LE_R1_ERR | LE_R1_STP | LE_R1_ENP)) !=
276 (LE_R1_STP | LE_R1_ENP)){
277 if (rmd1 & LE_R1_ERR) {
279 if (rmd1 & LE_R1_ENP) {
280 if ((rmd1 & LE_R1_OFLO) == 0) {
281 if (rmd1 & LE_R1_FRAM)
284 if (rmd1 & LE_R1_CRC)
289 if (rmd1 & LE_R1_OFLO)
290 if_printf(ifp, "overflow\n");
292 if (rmd1 & LE_R1_BUFF)
294 "receive buffer error\n");
295 } else if ((rmd1 & (LE_R1_STP | LE_R1_ENP)) !=
296 (LE_R1_STP | LE_R1_ENP))
297 if_printf(ifp, "dropping chained buffer\n");
300 if (sc->sc_flags & LE_DEBUG)
301 am79900_recv_print(sc, bix);
303 /* Pull the packet off the interface. */
304 m = lance_get(sc, LE_RBUFADDR(sc, bix),
305 (LE_LE32TOH(rmd.rmd2) & 0xfff) - ETHER_CRC_LEN);
308 rmd.rmd1 = LE_HTOLE32(LE_R1_OWN | LE_R1_ONES |
312 (*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
314 if (++bix == sc->sc_nrbuf)
318 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
320 #if defined(__i386__)
322 * The VMware LANCE does not present IFF_SIMPLEX
323 * behavior on multicast packets. Thus drop the
324 * packet if it is from ourselves.
326 eh = mtod(m, struct ether_header *);
327 if (!ether_cmp(eh->ether_shost, sc->sc_enaddr)) {
333 /* Pass the packet up. */
335 (*ifp->if_input)(ifp, m);
338 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
341 sc->sc_last_rd = bix;
345 am79900_tint(struct lance_softc *sc)
347 struct ifnet *ifp = sc->sc_ifp;
352 bix = sc->sc_first_td;
355 if (sc->sc_no_td <= 0)
358 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
361 tmd1 = LE_LE32TOH(tmd.tmd1);
364 if (sc->sc_flags & LE_DEBUG)
365 if_printf(ifp, "trans tmd: "
366 "adr %08x, flags/blen %08x\n",
367 LE_LE32TOH(tmd.tmd0), tmd1);
370 if (tmd1 & LE_T1_OWN)
373 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
375 if (tmd1 & LE_T1_ERR) {
376 tmd2 = LE_LE32TOH(tmd.tmd2);
377 if (tmd2 & LE_T2_BUFF)
378 if_printf(ifp, "transmit buffer error\n");
379 else if (tmd2 & LE_T2_UFLO)
380 if_printf(ifp, "underflow\n");
381 if (tmd2 & (LE_T2_BUFF | LE_T2_UFLO)) {
382 lance_init_locked(sc);
385 if (tmd2 & LE_T2_LCAR) {
386 if (sc->sc_flags & LE_CARRIER)
387 if_link_state_change(ifp,
389 sc->sc_flags &= ~LE_CARRIER;
390 if (sc->sc_nocarrier)
391 (*sc->sc_nocarrier)(sc);
393 if_printf(ifp, "lost carrier\n");
395 if (tmd2 & LE_T2_LCOL)
396 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
397 if (tmd2 & LE_T2_RTRY) {
399 if_printf(ifp, "excessive collisions\n");
401 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 16);
403 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
405 if (tmd1 & LE_T1_ONE)
406 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
407 else if (tmd1 & LE_T1_MORE)
408 /* Real number is unknown. */
409 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 2);
410 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
413 if (++bix == sc->sc_ntbuf)
419 sc->sc_first_td = bix;
421 sc->sc_wdog_timer = sc->sc_no_td > 0 ? 5 : 0;
425 * Controller interrupt
428 am79900_intr(void *arg)
430 struct lance_softc *sc = arg;
431 struct ifnet *ifp = sc->sc_ifp;
436 if (sc->sc_hwintr && (*sc->sc_hwintr)(sc) == -1) {
437 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
438 lance_init_locked(sc);
443 isr = (*sc->sc_rdcsr)(sc, LE_CSR0);
444 #if defined(LEDEBUG) && LEDEBUG > 1
445 if (sc->sc_flags & LE_DEBUG)
446 if_printf(ifp, "%s: entering with isr=%04x\n", __func__, isr);
448 if ((isr & LE_C0_INTR) == 0) {
454 * Clear interrupt source flags and turn off interrupts. If we
455 * don't clear these flags before processing their sources we
456 * could completely miss some interrupt events as the NIC can
457 * change these flags while we're in this handler. We toggle
458 * the interrupt enable bit in order to keep receiving them
459 * (some chips work without this, some don't).
461 (*sc->sc_wrcsr)(sc, LE_CSR0, isr & ~(LE_C0_INEA | LE_C0_TDMD |
462 LE_C0_STOP | LE_C0_STRT | LE_C0_INIT));
464 if (isr & LE_C0_ERR) {
465 if (isr & LE_C0_BABL) {
467 if_printf(ifp, "babble\n");
469 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
472 if (isr & LE_C0_CERR) {
473 if_printf(ifp, "collision error\n");
474 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
477 if (isr & LE_C0_MISS) {
479 if_printf(ifp, "missed packet\n");
481 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
483 if (isr & LE_C0_MERR) {
484 if_printf(ifp, "memory error\n");
485 lance_init_locked(sc);
491 if ((isr & LE_C0_RXON) == 0) {
492 if_printf(ifp, "receiver disabled\n");
493 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
494 lance_init_locked(sc);
498 if ((isr & LE_C0_TXON) == 0) {
499 if_printf(ifp, "transmitter disabled\n");
500 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
501 lance_init_locked(sc);
507 * Pretend we have carrier; if we don't this will be cleared shortly.
509 if (!(sc->sc_flags & LE_CARRIER))
510 if_link_state_change(ifp, LINK_STATE_UP);
511 sc->sc_flags |= LE_CARRIER;
513 if (isr & LE_C0_RINT)
515 if (isr & LE_C0_TINT)
518 /* Enable interrupts again. */
519 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA);
521 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
522 am79900_start_locked(sc);
528 * Set up output on interface.
529 * Get another datagram to send off of the interface queue, and map it to the
530 * interface before starting the output.
533 am79900_start_locked(struct lance_softc *sc)
535 struct ifnet *ifp = sc->sc_ifp;
538 int bix, enq, len, rp;
540 LE_LOCK_ASSERT(sc, MA_OWNED);
542 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
546 bix = sc->sc_last_td;
549 for (; sc->sc_no_td < sc->sc_ntbuf &&
550 !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
551 rp = LE_TMDADDR(sc, bix);
552 (*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
554 if (LE_LE32TOH(tmd.tmd1) & LE_T1_OWN) {
555 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
557 "missing buffer, no_td = %d, last_td = %d\n",
558 sc->sc_no_td, sc->sc_last_td);
561 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
566 * If BPF is listening on this interface, let it see the packet
567 * before we commit it to the wire.
572 * Copy the mbuf chain into the transmit buffer.
574 len = lance_put(sc, LE_TBUFADDR(sc, bix), m);
577 if (len > ETHERMTU + ETHER_HDR_LEN)
578 if_printf(ifp, "packet length %d\n", len);
582 * Init transmit registers, and set transmit start flag.
584 tmd.tmd1 = LE_HTOLE32(LE_T1_OWN | LE_T1_STP | LE_T1_ENP |
585 LE_T1_ONES | (-len & 0xfff));
589 (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
592 if (sc->sc_flags & LE_DEBUG)
593 am79900_xmit_print(sc, bix);
596 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
599 if (++bix == sc->sc_ntbuf)
602 if (++sc->sc_no_td == sc->sc_ntbuf) {
603 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
608 sc->sc_last_td = bix;
611 sc->sc_wdog_timer = 5;
616 am79900_recv_print(struct lance_softc *sc, int no)
618 struct ifnet *ifp = sc->sc_ifp;
619 struct ether_header eh;
623 (*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
624 len = LE_LE32TOH(rmd.rmd2) & 0xfff;
625 if_printf(ifp, "receive buffer %d, len = %d\n", no, len);
626 if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
627 if_printf(ifp, "adr %08x, flags/blen %08x\n", LE_LE32TOH(rmd.rmd0),
628 LE_LE32TOH(rmd.rmd1));
629 if (len - ETHER_CRC_LEN >= sizeof(eh)) {
630 (*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
631 if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
632 printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
633 ntohs(eh.ether_type));
638 am79900_xmit_print(struct lance_softc *sc, int no)
640 struct ifnet *ifp = sc->sc_ifp;
641 struct ether_header eh;
645 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
646 len = -(LE_LE32TOH(tmd.tmd1) & 0xfff);
647 if_printf(ifp, "transmit buffer %d, len = %d\n", no, len);
648 if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
649 if_printf(ifp, "adr %08x, flags/blen %08x\n", LE_LE32TOH(tmd.tmd0),
650 LE_LE32TOH(tmd.tmd1));
651 if (len >= sizeof(eh)) {
652 (*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
653 if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
654 printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
655 ntohs(eh.ether_type));