1 /* $NetBSD: am7990.c,v 1.68 2005/12/11 12:21:25 christos Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD AND BSD-3-Clause
6 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
11 * Simulation Facility, NASA Ames Research Center.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
36 * Copyright (c) 1992, 1993
37 * The Regents of the University of California. All rights reserved.
39 * This code is derived from software contributed to Berkeley by
40 * Ralph Campbell and Rick Macklem.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
69 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
72 #include <sys/param.h>
74 #include <sys/endian.h>
77 #include <sys/mutex.h>
78 #include <sys/socket.h>
81 #include <net/ethernet.h>
83 #include <net/if_arp.h>
84 #include <net/if_dl.h>
85 #include <net/if_media.h>
86 #include <net/if_var.h>
88 #include <machine/bus.h>
90 #include <dev/le/lancereg.h>
91 #include <dev/le/lancevar.h>
92 #include <dev/le/am7990reg.h>
93 #include <dev/le/am7990var.h>
95 static void am7990_meminit(struct lance_softc *);
96 static void am7990_rint(struct lance_softc *);
97 static void am7990_tint(struct lance_softc *);
98 static void am7990_start_locked(struct lance_softc *sc);
101 static void am7990_recv_print(struct lance_softc *, int);
102 static void am7990_xmit_print(struct lance_softc *, int);
106 am7990_config(struct am7990_softc *sc, const char* name, int unit)
110 sc->lsc.sc_meminit = am7990_meminit;
111 sc->lsc.sc_start_locked = am7990_start_locked;
113 error = lance_config(&sc->lsc, name, unit);
118 sc->lsc.sc_initaddr = mem;
119 mem += sizeof(struct leinit);
120 sc->lsc.sc_rmdaddr = mem;
121 mem += sizeof(struct lermd) * sc->lsc.sc_nrbuf;
122 sc->lsc.sc_tmdaddr = mem;
123 mem += sizeof(struct letmd) * sc->lsc.sc_ntbuf;
124 sc->lsc.sc_rbufaddr = mem;
125 mem += LEBLEN * sc->lsc.sc_nrbuf;
126 sc->lsc.sc_tbufaddr = mem;
127 mem += LEBLEN * sc->lsc.sc_ntbuf;
129 if (mem > sc->lsc.sc_memsize)
130 panic("%s: memsize", __func__);
132 lance_attach(&sc->lsc);
138 am7990_detach(struct am7990_softc *sc)
141 lance_detach(&sc->lsc);
145 * Set up the initialization block and the descriptor rings.
148 am7990_meminit(struct lance_softc *sc)
150 struct ifnet *ifp = sc->sc_ifp;
157 LE_LOCK_ASSERT(sc, MA_OWNED);
159 if (ifp->if_flags & IFF_PROMISC)
160 init.init_mode = LE_MODE_NORMAL | LE_MODE_PROM;
162 init.init_mode = LE_MODE_NORMAL;
164 init.init_padr[0] = (sc->sc_enaddr[1] << 8) | sc->sc_enaddr[0];
165 init.init_padr[1] = (sc->sc_enaddr[3] << 8) | sc->sc_enaddr[2];
166 init.init_padr[2] = (sc->sc_enaddr[5] << 8) | sc->sc_enaddr[4];
167 lance_setladrf(sc, init.init_ladrf);
170 sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
172 a = sc->sc_addr + LE_RMDADDR(sc, 0);
174 init.init_rlen = (a >> 16) | ((ffs(sc->sc_nrbuf) - 1) << 13);
176 a = sc->sc_addr + LE_TMDADDR(sc, 0);
178 init.init_tlen = (a >> 16) | ((ffs(sc->sc_ntbuf) - 1) << 13);
180 (*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
183 * Set up receive ring descriptors.
185 for (bix = 0; bix < sc->sc_nrbuf; bix++) {
186 a = sc->sc_addr + LE_RBUFADDR(sc, bix);
188 rmd.rmd1_hadr = a >> 16;
189 rmd.rmd1_bits = LE_R1_OWN;
190 rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
192 (*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
197 * Set up transmit ring descriptors.
199 for (bix = 0; bix < sc->sc_ntbuf; bix++) {
200 a = sc->sc_addr + LE_TBUFADDR(sc, bix);
202 tmd.tmd1_hadr = a >> 16;
204 tmd.tmd2 = LE_XMD2_ONES;
206 (*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
212 am7990_rint(struct lance_softc *sc)
214 struct ifnet *ifp = sc->sc_ifp;
218 #if defined(LANCE_REVC_BUG)
219 struct ether_header *eh;
220 /* Make sure this is short-aligned, for ether_cmp(). */
221 static uint16_t bcast_enaddr[3] = { ~0, ~0, ~0 };
224 bix = sc->sc_last_rd;
226 /* Process all buffers with valid data. */
228 rp = LE_RMDADDR(sc, bix);
229 (*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
231 if (rmd.rmd1_bits & LE_R1_OWN)
235 if ((rmd.rmd1_bits & (LE_R1_ERR | LE_R1_STP | LE_R1_ENP)) !=
236 (LE_R1_STP | LE_R1_ENP)) {
237 if (rmd.rmd1_bits & LE_R1_ERR) {
239 if (rmd.rmd1_bits & LE_R1_ENP) {
240 if ((rmd.rmd1_bits & LE_R1_OFLO) == 0) {
241 if (rmd.rmd1_bits & LE_R1_FRAM)
244 if (rmd.rmd1_bits & LE_R1_CRC)
249 if (rmd.rmd1_bits & LE_R1_OFLO)
250 if_printf(ifp, "overflow\n");
252 if (rmd.rmd1_bits & LE_R1_BUFF)
254 "receive buffer error\n");
255 } else if ((rmd.rmd1_bits & (LE_R1_STP | LE_R1_ENP)) !=
256 (LE_R1_STP | LE_R1_ENP))
257 if_printf(ifp, "dropping chained buffer\n");
260 if (sc->sc_flags & LE_DEBUG)
261 am7990_recv_print(sc, bix);
263 /* Pull the packet off the interface. */
264 m = lance_get(sc, LE_RBUFADDR(sc, bix),
265 (int)rmd.rmd3 - ETHER_CRC_LEN);
268 rmd.rmd1_bits = LE_R1_OWN;
269 rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
271 (*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
273 if (++bix == sc->sc_nrbuf)
277 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
279 #ifdef LANCE_REVC_BUG
281 * The old LANCE (Rev. C) chips have a bug which
282 * causes garbage to be inserted in front of the
283 * received packet. The workaround is to ignore
284 * packets with an invalid destination address
285 * (garbage will usually not match).
286 * Of course, this precludes multicast support...
288 eh = mtod(m, struct ether_header *);
289 if (ether_cmp(eh->ether_dhost, sc->sc_enaddr) &&
290 ether_cmp(eh->ether_dhost, bcast_enaddr)) {
296 /* Pass the packet up. */
298 (*ifp->if_input)(ifp, m);
301 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
304 sc->sc_last_rd = bix;
308 am7990_tint(struct lance_softc *sc)
310 struct ifnet *ifp = sc->sc_ifp;
314 bix = sc->sc_first_td;
317 if (sc->sc_no_td <= 0)
320 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
324 if (sc->sc_flags & LE_DEBUG)
325 if_printf(ifp, "trans tmd: "
326 "ladr %04x, hadr %02x, flags %02x, "
327 "bcnt %04x, mcnt %04x\n",
328 tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits,
332 if (tmd.tmd1_bits & LE_T1_OWN)
335 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
337 if (tmd.tmd1_bits & LE_T1_ERR) {
338 if (tmd.tmd3 & LE_T3_BUFF)
339 if_printf(ifp, "transmit buffer error\n");
340 else if (tmd.tmd3 & LE_T3_UFLO)
341 if_printf(ifp, "underflow\n");
342 if (tmd.tmd3 & (LE_T3_BUFF | LE_T3_UFLO)) {
343 lance_init_locked(sc);
346 if (tmd.tmd3 & LE_T3_LCAR) {
347 if (sc->sc_flags & LE_CARRIER)
348 if_link_state_change(ifp,
350 sc->sc_flags &= ~LE_CARRIER;
351 if (sc->sc_nocarrier)
352 (*sc->sc_nocarrier)(sc);
354 if_printf(ifp, "lost carrier\n");
356 if (tmd.tmd3 & LE_T3_LCOL)
357 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
358 if (tmd.tmd3 & LE_T3_RTRY) {
360 if_printf(ifp, "excessive collisions, tdr %d\n",
361 tmd.tmd3 & LE_T3_TDR_MASK);
363 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 16);
365 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
367 if (tmd.tmd1_bits & LE_T1_ONE)
368 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
369 else if (tmd.tmd1_bits & LE_T1_MORE)
370 /* Real number is unknown. */
371 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 2);
372 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
375 if (++bix == sc->sc_ntbuf)
381 sc->sc_first_td = bix;
383 sc->sc_wdog_timer = sc->sc_no_td > 0 ? 5 : 0;
387 * Controller interrupt
390 am7990_intr(void *arg)
392 struct lance_softc *sc = arg;
393 struct ifnet *ifp = sc->sc_ifp;
398 if (sc->sc_hwintr && (*sc->sc_hwintr)(sc) == -1) {
399 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
400 lance_init_locked(sc);
405 isr = (*sc->sc_rdcsr)(sc, LE_CSR0);
406 #if defined(LEDEBUG) && LEDEBUG > 1
407 if (sc->sc_flags & LE_DEBUG)
408 if_printf(ifp, "%s: entering with isr=%04x\n", __func__, isr);
410 if ((isr & LE_C0_INTR) == 0) {
416 * Clear interrupt source flags and turn off interrupts. If we
417 * don't clear these flags before processing their sources we
418 * could completely miss some interrupt events as the NIC can
419 * change these flags while we're in this handler. We toggle
420 * the interrupt enable bit in order to keep receiving them
421 * (some chips work without this, some don't).
423 (*sc->sc_wrcsr)(sc, LE_CSR0, isr & ~(LE_C0_INEA | LE_C0_TDMD |
424 LE_C0_STOP | LE_C0_STRT | LE_C0_INIT));
426 if (isr & LE_C0_ERR) {
427 if (isr & LE_C0_BABL) {
429 if_printf(ifp, "babble\n");
431 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
434 if (isr & LE_C0_CERR) {
435 if_printf(ifp, "collision error\n");
436 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
439 if (isr & LE_C0_MISS) {
441 if_printf(ifp, "missed packet\n");
443 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
445 if (isr & LE_C0_MERR) {
446 if_printf(ifp, "memory error\n");
447 lance_init_locked(sc);
453 if ((isr & LE_C0_RXON) == 0) {
454 if_printf(ifp, "receiver disabled\n");
455 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
456 lance_init_locked(sc);
460 if ((isr & LE_C0_TXON) == 0) {
461 if_printf(ifp, "transmitter disabled\n");
462 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
463 lance_init_locked(sc);
469 * Pretend we have carrier; if we don't this will be cleared shortly.
471 if (!(sc->sc_flags & LE_CARRIER))
472 if_link_state_change(ifp, LINK_STATE_UP);
473 sc->sc_flags |= LE_CARRIER;
475 if (isr & LE_C0_RINT)
477 if (isr & LE_C0_TINT)
480 /* Enable interrupts again. */
481 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA);
483 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
484 am7990_start_locked(sc);
490 * Set up output on interface.
491 * Get another datagram to send off of the interface queue, and map it to the
492 * interface before starting the output.
495 am7990_start_locked(struct lance_softc *sc)
497 struct ifnet *ifp = sc->sc_ifp;
500 int bix, enq, len, rp;
502 LE_LOCK_ASSERT(sc, MA_OWNED);
504 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
508 bix = sc->sc_last_td;
511 for (; sc->sc_no_td < sc->sc_ntbuf &&
512 !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
513 rp = LE_TMDADDR(sc, bix);
514 (*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
516 if (tmd.tmd1_bits & LE_T1_OWN) {
517 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
519 "missing buffer, no_td = %d, last_td = %d\n",
520 sc->sc_no_td, sc->sc_last_td);
523 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
528 * If BPF is listening on this interface, let it see the packet
529 * before we commit it to the wire.
534 * Copy the mbuf chain into the transmit buffer.
536 len = lance_put(sc, LE_TBUFADDR(sc, bix), m);
539 if (len > ETHERMTU + ETHER_HDR_LEN)
540 if_printf(ifp, "packet length %d\n", len);
544 * Init transmit registers, and set transmit start flag.
546 tmd.tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
547 tmd.tmd2 = -len | LE_XMD2_ONES;
550 (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
553 if (sc->sc_flags & LE_DEBUG)
554 am7990_xmit_print(sc, bix);
557 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
560 if (++bix == sc->sc_ntbuf)
563 if (++sc->sc_no_td == sc->sc_ntbuf) {
564 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
569 sc->sc_last_td = bix;
572 sc->sc_wdog_timer = 5;
577 am7990_recv_print(struct lance_softc *sc, int no)
579 struct ifnet *ifp = sc->sc_ifp;
580 struct ether_header eh;
584 (*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
586 if_printf(ifp, "receive buffer %d, len = %d\n", no, len);
587 if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
589 "ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
590 rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits, rmd.rmd2, rmd.rmd3);
591 if (len - ETHER_CRC_LEN >= sizeof(eh)) {
592 (*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
593 if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
594 printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
595 ntohs(eh.ether_type));
600 am7990_xmit_print(struct lance_softc *sc, int no)
602 struct ifnet *ifp = sc->sc_ifp;
603 struct ether_header eh;
607 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
609 if_printf(ifp, "transmit buffer %d, len = %d\n", no, len);
610 if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
612 "ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
613 tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits, tmd.tmd2, tmd.tmd3);
614 if (len >= sizeof(eh)) {
615 (*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
616 if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
617 printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
618 ntohs(eh.ether_type));