1 /* $NetBSD: am7990.c,v 1.68 2005/12/11 12:21:25 christos Exp $ */
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1992, 1993
35 * The Regents of the University of California. All rights reserved.
37 * This code is derived from software contributed to Berkeley by
38 * Ralph Campbell and Rick Macklem.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
70 #include <sys/param.h>
72 #include <sys/endian.h>
75 #include <sys/mutex.h>
76 #include <sys/socket.h>
79 #include <net/ethernet.h>
81 #include <net/if_arp.h>
82 #include <net/if_dl.h>
83 #include <net/if_media.h>
84 #include <net/if_var.h>
86 #include <machine/bus.h>
88 #include <dev/le/lancereg.h>
89 #include <dev/le/lancevar.h>
90 #include <dev/le/am7990reg.h>
91 #include <dev/le/am7990var.h>
93 static void am7990_meminit(struct lance_softc *);
94 static void am7990_rint(struct lance_softc *);
95 static void am7990_tint(struct lance_softc *);
96 static void am7990_start_locked(struct lance_softc *sc);
99 static void am7990_recv_print(struct lance_softc *, int);
100 static void am7990_xmit_print(struct lance_softc *, int);
104 am7990_config(struct am7990_softc *sc, const char* name, int unit)
108 sc->lsc.sc_meminit = am7990_meminit;
109 sc->lsc.sc_start_locked = am7990_start_locked;
111 error = lance_config(&sc->lsc, name, unit);
116 sc->lsc.sc_initaddr = mem;
117 mem += sizeof(struct leinit);
118 sc->lsc.sc_rmdaddr = mem;
119 mem += sizeof(struct lermd) * sc->lsc.sc_nrbuf;
120 sc->lsc.sc_tmdaddr = mem;
121 mem += sizeof(struct letmd) * sc->lsc.sc_ntbuf;
122 sc->lsc.sc_rbufaddr = mem;
123 mem += LEBLEN * sc->lsc.sc_nrbuf;
124 sc->lsc.sc_tbufaddr = mem;
125 mem += LEBLEN * sc->lsc.sc_ntbuf;
127 if (mem > sc->lsc.sc_memsize)
128 panic("%s: memsize", __func__);
130 lance_attach(&sc->lsc);
136 am7990_detach(struct am7990_softc *sc)
139 lance_detach(&sc->lsc);
143 * Set up the initialization block and the descriptor rings.
146 am7990_meminit(struct lance_softc *sc)
148 struct ifnet *ifp = sc->sc_ifp;
155 LE_LOCK_ASSERT(sc, MA_OWNED);
157 if (ifp->if_flags & IFF_PROMISC)
158 init.init_mode = LE_MODE_NORMAL | LE_MODE_PROM;
160 init.init_mode = LE_MODE_NORMAL;
162 init.init_padr[0] = (sc->sc_enaddr[1] << 8) | sc->sc_enaddr[0];
163 init.init_padr[1] = (sc->sc_enaddr[3] << 8) | sc->sc_enaddr[2];
164 init.init_padr[2] = (sc->sc_enaddr[5] << 8) | sc->sc_enaddr[4];
165 lance_setladrf(sc, init.init_ladrf);
168 sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
170 a = sc->sc_addr + LE_RMDADDR(sc, 0);
172 init.init_rlen = (a >> 16) | ((ffs(sc->sc_nrbuf) - 1) << 13);
174 a = sc->sc_addr + LE_TMDADDR(sc, 0);
176 init.init_tlen = (a >> 16) | ((ffs(sc->sc_ntbuf) - 1) << 13);
178 (*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
181 * Set up receive ring descriptors.
183 for (bix = 0; bix < sc->sc_nrbuf; bix++) {
184 a = sc->sc_addr + LE_RBUFADDR(sc, bix);
186 rmd.rmd1_hadr = a >> 16;
187 rmd.rmd1_bits = LE_R1_OWN;
188 rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
190 (*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
195 * Set up transmit ring descriptors.
197 for (bix = 0; bix < sc->sc_ntbuf; bix++) {
198 a = sc->sc_addr + LE_TBUFADDR(sc, bix);
200 tmd.tmd1_hadr = a >> 16;
202 tmd.tmd2 = LE_XMD2_ONES;
204 (*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
210 am7990_rint(struct lance_softc *sc)
212 struct ifnet *ifp = sc->sc_ifp;
216 #if defined(LANCE_REVC_BUG)
217 struct ether_header *eh;
218 /* Make sure this is short-aligned, for ether_cmp(). */
219 static uint16_t bcast_enaddr[3] = { ~0, ~0, ~0 };
222 bix = sc->sc_last_rd;
224 /* Process all buffers with valid data. */
226 rp = LE_RMDADDR(sc, bix);
227 (*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
229 if (rmd.rmd1_bits & LE_R1_OWN)
233 if ((rmd.rmd1_bits & (LE_R1_ERR | LE_R1_STP | LE_R1_ENP)) !=
234 (LE_R1_STP | LE_R1_ENP)) {
235 if (rmd.rmd1_bits & LE_R1_ERR) {
237 if (rmd.rmd1_bits & LE_R1_ENP) {
238 if ((rmd.rmd1_bits & LE_R1_OFLO) == 0) {
239 if (rmd.rmd1_bits & LE_R1_FRAM)
242 if (rmd.rmd1_bits & LE_R1_CRC)
247 if (rmd.rmd1_bits & LE_R1_OFLO)
248 if_printf(ifp, "overflow\n");
250 if (rmd.rmd1_bits & LE_R1_BUFF)
252 "receive buffer error\n");
253 } else if ((rmd.rmd1_bits & (LE_R1_STP | LE_R1_ENP)) !=
254 (LE_R1_STP | LE_R1_ENP))
255 if_printf(ifp, "dropping chained buffer\n");
258 if (sc->sc_flags & LE_DEBUG)
259 am7990_recv_print(sc, bix);
261 /* Pull the packet off the interface. */
262 m = lance_get(sc, LE_RBUFADDR(sc, bix),
263 (int)rmd.rmd3 - ETHER_CRC_LEN);
266 rmd.rmd1_bits = LE_R1_OWN;
267 rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
269 (*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
271 if (++bix == sc->sc_nrbuf)
277 #ifdef LANCE_REVC_BUG
279 * The old LANCE (Rev. C) chips have a bug which
280 * causes garbage to be inserted in front of the
281 * received packet. The workaround is to ignore
282 * packets with an invalid destination address
283 * (garbage will usually not match).
284 * Of course, this precludes multicast support...
286 eh = mtod(m, struct ether_header *);
287 if (ether_cmp(eh->ether_dhost, sc->sc_enaddr) &&
288 ether_cmp(eh->ether_dhost, bcast_enaddr)) {
294 /* Pass the packet up. */
296 (*ifp->if_input)(ifp, m);
302 sc->sc_last_rd = bix;
306 am7990_tint(struct lance_softc *sc)
308 struct ifnet *ifp = sc->sc_ifp;
312 bix = sc->sc_first_td;
315 if (sc->sc_no_td <= 0)
318 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
322 if (sc->sc_flags & LE_DEBUG)
323 if_printf(ifp, "trans tmd: "
324 "ladr %04x, hadr %02x, flags %02x, "
325 "bcnt %04x, mcnt %04x\n",
326 tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits,
330 if (tmd.tmd1_bits & LE_T1_OWN)
333 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
335 if (tmd.tmd1_bits & LE_T1_ERR) {
336 if (tmd.tmd3 & LE_T3_BUFF)
337 if_printf(ifp, "transmit buffer error\n");
338 else if (tmd.tmd3 & LE_T3_UFLO)
339 if_printf(ifp, "underflow\n");
340 if (tmd.tmd3 & (LE_T3_BUFF | LE_T3_UFLO)) {
341 lance_init_locked(sc);
344 if (tmd.tmd3 & LE_T3_LCAR) {
345 if (sc->sc_flags & LE_CARRIER)
346 if_link_state_change(ifp,
348 sc->sc_flags &= ~LE_CARRIER;
349 if (sc->sc_nocarrier)
350 (*sc->sc_nocarrier)(sc);
352 if_printf(ifp, "lost carrier\n");
354 if (tmd.tmd3 & LE_T3_LCOL)
355 ifp->if_collisions++;
356 if (tmd.tmd3 & LE_T3_RTRY) {
358 if_printf(ifp, "excessive collisions, tdr %d\n",
359 tmd.tmd3 & LE_T3_TDR_MASK);
361 ifp->if_collisions += 16;
365 if (tmd.tmd1_bits & LE_T1_ONE)
366 ifp->if_collisions++;
367 else if (tmd.tmd1_bits & LE_T1_MORE)
368 /* Real number is unknown. */
369 ifp->if_collisions += 2;
373 if (++bix == sc->sc_ntbuf)
379 sc->sc_first_td = bix;
381 sc->sc_wdog_timer = sc->sc_no_td > 0 ? 5 : 0;
385 * Controller interrupt
388 am7990_intr(void *arg)
390 struct lance_softc *sc = arg;
391 struct ifnet *ifp = sc->sc_ifp;
396 if (sc->sc_hwintr && (*sc->sc_hwintr)(sc) == -1) {
398 lance_init_locked(sc);
403 isr = (*sc->sc_rdcsr)(sc, LE_CSR0);
404 #if defined(LEDEBUG) && LEDEBUG > 1
405 if (sc->sc_flags & LE_DEBUG)
406 if_printf(ifp, "%s: entering with isr=%04x\n", __func__, isr);
408 if ((isr & LE_C0_INTR) == 0) {
414 * Clear interrupt source flags and turn off interrupts. If we
415 * don't clear these flags before processing their sources we
416 * could completely miss some interrupt events as the NIC can
417 * change these flags while we're in this handler. We toggle
418 * the interrupt enable bit in order to keep receiving them
419 * (some chips work without this, some don't).
421 (*sc->sc_wrcsr)(sc, LE_CSR0, isr & ~(LE_C0_INEA | LE_C0_TDMD |
422 LE_C0_STOP | LE_C0_STRT | LE_C0_INIT));
424 if (isr & LE_C0_ERR) {
425 if (isr & LE_C0_BABL) {
427 if_printf(ifp, "babble\n");
432 if (isr & LE_C0_CERR) {
433 if_printf(ifp, "collision error\n");
434 ifp->if_collisions++;
437 if (isr & LE_C0_MISS) {
439 if_printf(ifp, "missed packet\n");
443 if (isr & LE_C0_MERR) {
444 if_printf(ifp, "memory error\n");
445 lance_init_locked(sc);
451 if ((isr & LE_C0_RXON) == 0) {
452 if_printf(ifp, "receiver disabled\n");
454 lance_init_locked(sc);
458 if ((isr & LE_C0_TXON) == 0) {
459 if_printf(ifp, "transmitter disabled\n");
461 lance_init_locked(sc);
467 * Pretend we have carrier; if we don't this will be cleared shortly.
469 if (!(sc->sc_flags & LE_CARRIER))
470 if_link_state_change(ifp, LINK_STATE_UP);
471 sc->sc_flags |= LE_CARRIER;
473 if (isr & LE_C0_RINT)
475 if (isr & LE_C0_TINT)
478 /* Enable interrupts again. */
479 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA);
481 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
482 am7990_start_locked(sc);
488 * Set up output on interface.
489 * Get another datagram to send off of the interface queue, and map it to the
490 * interface before starting the output.
493 am7990_start_locked(struct lance_softc *sc)
495 struct ifnet *ifp = sc->sc_ifp;
498 int bix, enq, len, rp;
500 LE_LOCK_ASSERT(sc, MA_OWNED);
502 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
506 bix = sc->sc_last_td;
509 for (; sc->sc_no_td < sc->sc_ntbuf &&
510 !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
511 rp = LE_TMDADDR(sc, bix);
512 (*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
514 if (tmd.tmd1_bits & LE_T1_OWN) {
515 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
517 "missing buffer, no_td = %d, last_td = %d\n",
518 sc->sc_no_td, sc->sc_last_td);
521 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
526 * If BPF is listening on this interface, let it see the packet
527 * before we commit it to the wire.
532 * Copy the mbuf chain into the transmit buffer.
534 len = lance_put(sc, LE_TBUFADDR(sc, bix), m);
537 if (len > ETHERMTU + ETHER_HDR_LEN)
538 if_printf(ifp, "packet length %d\n", len);
542 * Init transmit registers, and set transmit start flag.
544 tmd.tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
545 tmd.tmd2 = -len | LE_XMD2_ONES;
548 (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
551 if (sc->sc_flags & LE_DEBUG)
552 am7990_xmit_print(sc, bix);
555 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
558 if (++bix == sc->sc_ntbuf)
561 if (++sc->sc_no_td == sc->sc_ntbuf) {
562 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
567 sc->sc_last_td = bix;
570 sc->sc_wdog_timer = 5;
575 am7990_recv_print(struct lance_softc *sc, int no)
577 struct ifnet *ifp = sc->sc_ifp;
578 struct ether_header eh;
582 (*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
584 if_printf(ifp, "receive buffer %d, len = %d\n", no, len);
585 if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
587 "ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
588 rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits, rmd.rmd2, rmd.rmd3);
589 if (len - ETHER_CRC_LEN >= sizeof(eh)) {
590 (*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
591 if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
592 printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
593 ntohs(eh.ether_type));
598 am7990_xmit_print(struct lance_softc *sc, int no)
600 struct ifnet *ifp = sc->sc_ifp;
601 struct ether_header eh;
605 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
607 if_printf(ifp, "transmit buffer %d, len = %d\n", no, len);
608 if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
610 "ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
611 tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits, tmd.tmd2, tmd.tmd3);
612 if (len >= sizeof(eh)) {
613 (*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
614 if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
615 printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
616 ntohs(eh.ether_type));