1 /* $NetBSD: am7990.c,v 1.68 2005/12/11 12:21:25 christos Exp $ */
2 /* $FreeBSD: src/sys/dev/le/am7990.c,v 1.3 2006/05/16 21:04:01 marius Exp $ */
3 /* $DragonFly: src/sys/dev/netif/lnc/am7990.c,v 1.5 2008/05/14 11:59:20 sephe Exp $ */
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.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
43 * Copyright (c) 1992, 1993
44 * The Regents of the University of California. All rights reserved.
46 * This code is derived from software contributed to Berkeley by
47 * Ralph Campbell and Rick Macklem.
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
76 #include <sys/param.h>
78 #include <sys/endian.h>
82 #include <sys/socket.h>
85 #include <net/ethernet.h>
87 #include <net/if_arp.h>
88 #include <net/if_dl.h>
89 #include <net/if_media.h>
90 #include <net/if_var.h>
91 #include <net/ifq_var.h>
93 #include <dev/netif/lnc/lancereg.h>
94 #include <dev/netif/lnc/lancevar.h>
95 #include <dev/netif/lnc/am7990reg.h>
96 #include <dev/netif/lnc/am7990var.h>
98 static void am7990_meminit(struct lance_softc *);
99 static void am7990_rint(struct lance_softc *);
100 static void am7990_tint(struct lance_softc *);
101 static void am7990_start_locked(struct lance_softc *sc);
104 static void am7990_recv_print(struct lance_softc *, int);
105 static void am7990_xmit_print(struct lance_softc *, int);
109 am7990_config(struct am7990_softc *sc, const char* name, int unit)
113 sc->lsc.sc_meminit = am7990_meminit;
114 sc->lsc.sc_start_locked = am7990_start_locked;
116 error = lance_config(&sc->lsc, name, unit);
121 sc->lsc.sc_initaddr = mem;
122 mem += sizeof(struct leinit);
123 sc->lsc.sc_rmdaddr = mem;
124 mem += sizeof(struct lermd) * sc->lsc.sc_nrbuf;
125 sc->lsc.sc_tmdaddr = mem;
126 mem += sizeof(struct letmd) * sc->lsc.sc_ntbuf;
127 sc->lsc.sc_rbufaddr = mem;
128 mem += LEBLEN * sc->lsc.sc_nrbuf;
129 sc->lsc.sc_tbufaddr = mem;
130 mem += LEBLEN * sc->lsc.sc_ntbuf;
132 if (mem > sc->lsc.sc_memsize)
133 panic("%s: memsize", __func__);
135 lance_attach(&sc->lsc);
141 am7990_detach(struct am7990_softc *sc)
144 ether_ifdetach(sc->lsc.ifp);
148 * Set up the initialization block and the descriptor rings.
151 am7990_meminit(struct lance_softc *sc)
153 struct ifnet *ifp = sc->ifp;
160 if (ifp->if_flags & IFF_PROMISC)
161 init.init_mode = LE_MODE_NORMAL | LE_MODE_PROM;
163 init.init_mode = LE_MODE_NORMAL;
165 init.init_padr[0] = (sc->sc_enaddr[1] << 8) | sc->sc_enaddr[0];
166 init.init_padr[1] = (sc->sc_enaddr[3] << 8) | sc->sc_enaddr[2];
167 init.init_padr[2] = (sc->sc_enaddr[5] << 8) | sc->sc_enaddr[4];
168 lance_setladrf(sc, init.init_ladrf);
171 sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
173 a = sc->sc_addr + LE_RMDADDR(sc, 0);
175 init.init_rlen = (a >> 16) | ((ffs(sc->sc_nrbuf) - 1) << 13);
177 a = sc->sc_addr + LE_TMDADDR(sc, 0);
179 init.init_tlen = (a >> 16) | ((ffs(sc->sc_ntbuf) - 1) << 13);
181 (*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
184 * Set up receive ring descriptors.
186 for (bix = 0; bix < sc->sc_nrbuf; bix++) {
187 a = sc->sc_addr + LE_RBUFADDR(sc, bix);
189 rmd.rmd1_hadr = a >> 16;
190 rmd.rmd1_bits = LE_R1_OWN;
191 rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
193 (*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
198 * Set up transmit ring descriptors.
200 for (bix = 0; bix < sc->sc_ntbuf; bix++) {
201 a = sc->sc_addr + LE_TBUFADDR(sc, bix);
203 tmd.tmd1_hadr = a >> 16;
205 tmd.tmd2 = LE_XMD2_ONES;
207 (*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
213 am7990_rint(struct lance_softc *sc)
215 struct ifnet *ifp = sc->ifp;
219 #if defined(LANCE_REVC_BUG)
220 struct ether_header *eh;
221 /* Make sure this is short-aligned, for ether_cmp(). */
222 static uint16_t bcast_enaddr[3] = { ~0, ~0, ~0 };
225 bix = sc->sc_last_rd;
227 /* Process all buffers with valid data. */
229 rp = LE_RMDADDR(sc, bix);
230 (*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
232 if (rmd.rmd1_bits & LE_R1_OWN)
236 if ((rmd.rmd1_bits & (LE_R1_ERR | LE_R1_STP | LE_R1_ENP)) !=
237 (LE_R1_STP | LE_R1_ENP)) {
238 if (rmd.rmd1_bits & LE_R1_ERR) {
240 if (rmd.rmd1_bits & LE_R1_ENP) {
241 if ((rmd.rmd1_bits & LE_R1_OFLO) == 0) {
242 if (rmd.rmd1_bits & LE_R1_FRAM)
245 if (rmd.rmd1_bits & LE_R1_CRC)
250 if (rmd.rmd1_bits & LE_R1_OFLO)
251 if_printf(ifp, "overflow\n");
253 if (rmd.rmd1_bits & LE_R1_BUFF)
255 "receive buffer error\n");
256 } else if ((rmd.rmd1_bits & (LE_R1_STP | LE_R1_ENP)) !=
257 (LE_R1_STP | LE_R1_ENP))
258 if_printf(ifp, "dropping chained buffer\n");
261 if (sc->sc_flags & LE_DEBUG)
262 am7990_recv_print(sc, bix);
264 /* Pull the packet off the interface. */
265 m = lance_get(sc, LE_RBUFADDR(sc, bix),
266 (int)rmd.rmd3 - ETHER_CRC_LEN);
269 rmd.rmd1_bits = LE_R1_OWN;
270 rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
272 (*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
274 if (++bix == sc->sc_nrbuf)
280 #ifdef LANCE_REVC_BUG
282 * The old LANCE (Rev. C) chips have a bug which
283 * causes garbage to be inserted in front of the
284 * received packet. The workaround is to ignore
285 * packets with an invalid destination address
286 * (garbage will usually not match).
287 * Of course, this precludes multicast support...
289 eh = mtod(m, struct ether_header *);
290 if (memcmp(eh->ether_dhost, sc->sc_enaddr,
292 memcmp(eh->ether_dhost, etherbroadcastaddr,
299 /* Pass the packet up. */
300 (*ifp->if_input)(ifp, m);
305 sc->sc_last_rd = bix;
309 am7990_tint(struct lance_softc *sc)
311 struct ifnet *ifp = sc->ifp;
315 bix = sc->sc_first_td;
318 if (sc->sc_no_td <= 0)
321 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
325 if (sc->sc_flags & LE_DEBUG)
326 if_printf(ifp, "trans tmd: "
327 "ladr %04x, hadr %02x, flags %02x, "
328 "bcnt %04x, mcnt %04x\n",
329 tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits,
333 if (tmd.tmd1_bits & LE_T1_OWN)
336 ifp->if_flags &= ~IFF_OACTIVE;
338 if (tmd.tmd1_bits & LE_T1_ERR) {
339 if (tmd.tmd3 & LE_T3_BUFF)
340 if_printf(ifp, "transmit buffer error\n");
341 else if (tmd.tmd3 & LE_T3_UFLO)
342 if_printf(ifp, "underflow\n");
343 if (tmd.tmd3 & (LE_T3_BUFF | LE_T3_UFLO)) {
344 lance_init_locked(sc);
347 if (tmd.tmd3 & LE_T3_LCAR) {
348 if (sc->sc_flags & LE_CARRIER) {
349 ifp->if_link_state = LINK_STATE_DOWN;
350 if_link_state_change(ifp);
352 sc->sc_flags &= ~LE_CARRIER;
353 if (sc->sc_nocarrier)
354 (*sc->sc_nocarrier)(sc);
356 if_printf(ifp, "lost carrier\n");
358 if (tmd.tmd3 & LE_T3_LCOL)
359 ifp->if_collisions++;
360 if (tmd.tmd3 & LE_T3_RTRY) {
362 if_printf(ifp, "excessive collisions, tdr %d\n",
363 tmd.tmd3 & LE_T3_TDR_MASK);
365 ifp->if_collisions += 16;
369 if (tmd.tmd1_bits & LE_T1_ONE)
370 ifp->if_collisions++;
371 else if (tmd.tmd1_bits & LE_T1_MORE)
372 /* Real number is unknown. */
373 ifp->if_collisions += 2;
377 if (++bix == sc->sc_ntbuf)
383 sc->sc_first_td = bix;
385 ifp->if_timer = sc->sc_no_td > 0 ? 5 : 0;
389 * Controller interrupt
392 am7990_intr(void *arg)
394 struct lance_softc *sc = arg;
395 struct ifnet *ifp = sc->ifp;
398 if (sc->sc_hwintr && (*sc->sc_hwintr)(sc) == -1) {
400 lance_init_locked(sc);
404 isr = (*sc->sc_rdcsr)(sc, LE_CSR0);
405 #if defined(LEDEBUG) && LEDEBUG > 1
406 if (sc->sc_flags & LE_DEBUG)
407 if_printf(ifp, "%s: entering with isr=%04x\n", __func__, isr);
409 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 turn off
418 * interrupts so we don't get another RX interrupt while still
419 * processing the previous one in ifp->if_input() with the
420 * driver lock dropped.
422 (*sc->sc_wrcsr)(sc, LE_CSR0, isr & ~(LE_C0_INEA | LE_C0_TDMD |
423 LE_C0_STOP | LE_C0_STRT | LE_C0_INIT));
425 if (isr & LE_C0_ERR) {
426 if (isr & LE_C0_BABL) {
428 if_printf(ifp, "babble\n");
433 if (isr & LE_C0_CERR) {
434 if_printf(ifp, "collision error\n");
435 ifp->if_collisions++;
438 if (isr & LE_C0_MISS) {
440 if_printf(ifp, "missed packet\n");
444 if (isr & LE_C0_MERR) {
445 if_printf(ifp, "memory error\n");
446 lance_init_locked(sc);
451 if ((isr & LE_C0_RXON) == 0) {
452 if_printf(ifp, "receiver disabled\n");
454 lance_init_locked(sc);
457 if ((isr & LE_C0_TXON) == 0) {
458 if_printf(ifp, "transmitter disabled\n");
460 lance_init_locked(sc);
465 * Pretend we have carrier; if we don't this will be cleared shortly.
467 if (!(sc->sc_flags & LE_CARRIER)) {
468 ifp->if_link_state = LINK_STATE_UP;
469 if_link_state_change(ifp);
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_is_empty(&ifp->if_snd))
486 * Set up output on interface.
487 * Get another datagram to send off of the interface queue, and map it to the
488 * interface before starting the output.
491 am7990_start_locked(struct lance_softc *sc)
493 struct ifnet *ifp = sc->ifp;
496 int bix, enq, len, rp;
498 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
502 bix = sc->sc_last_td;
505 for (; sc->sc_no_td < sc->sc_ntbuf &&
506 !ifq_is_empty(&ifp->if_snd);) {
507 rp = LE_TMDADDR(sc, bix);
508 (*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
510 if (tmd.tmd1_bits & LE_T1_OWN) {
511 ifp->if_flags |= IFF_OACTIVE;
513 "missing buffer, no_td = %d, last_td = %d\n",
514 sc->sc_no_td, sc->sc_last_td);
517 m = ifq_dequeue(&ifp->if_snd, NULL);
522 * If BPF is listening on this interface, let it see the
523 * packet before we commit it to the wire.
528 * Copy the mbuf chain into the transmit buffer.
530 len = lance_put(sc, LE_TBUFADDR(sc, bix), m);
533 if (len > ETHERMTU + ETHER_HDR_LEN)
534 if_printf(ifp, "packet length %d\n", len);
538 * Init transmit registers, and set transmit start flag.
540 tmd.tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
541 tmd.tmd2 = -len | LE_XMD2_ONES;
544 (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
547 if (sc->sc_flags & LE_DEBUG)
548 am7990_xmit_print(sc, bix);
551 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
554 if (++bix == sc->sc_ntbuf)
557 if (++sc->sc_no_td == sc->sc_ntbuf) {
558 ifp->if_flags |= IFF_OACTIVE;
563 sc->sc_last_td = bix;
571 am7990_recv_print(struct lance_softc *sc, int no)
573 struct ifnet *ifp = sc->ifp;
574 struct ether_header eh;
578 (*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
580 if_printf(ifp, "receive buffer %d, len = %d\n", no, len);
581 if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
583 "ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
584 rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits, rmd.rmd2, rmd.rmd3);
585 if (len - ETHER_CRC_LEN >= sizeof(eh)) {
586 (*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
587 if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
588 kprintf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
589 ntohs(eh.ether_type));
594 am7990_xmit_print(struct lance_softc *sc, int no)
596 struct ifnet *ifp = sc->ifp;
597 struct ether_header eh;
601 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
603 if_printf(ifp, "transmit buffer %d, len = %d\n", no, len);
604 if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
606 "ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
607 tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits, tmd.tmd2, tmd.tmd3);
608 if (len >= sizeof(eh)) {
609 (*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
610 if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
611 kprintf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
612 ntohs(eh.ether_type));