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 lance_config(&sc->lsc, name, unit);
119 sc->lsc.sc_initaddr = mem;
120 mem += sizeof(struct leinit);
121 sc->lsc.sc_rmdaddr = mem;
122 mem += sizeof(struct lermd) * sc->lsc.sc_nrbuf;
123 sc->lsc.sc_tmdaddr = mem;
124 mem += sizeof(struct letmd) * sc->lsc.sc_ntbuf;
125 sc->lsc.sc_rbufaddr = mem;
126 mem += LEBLEN * sc->lsc.sc_nrbuf;
127 sc->lsc.sc_tbufaddr = mem;
128 mem += LEBLEN * sc->lsc.sc_ntbuf;
130 if (mem > sc->lsc.sc_memsize)
131 panic("%s: memsize", __func__);
137 am7990_detach(struct am7990_softc *sc)
140 ether_ifdetach(sc->lsc.ifp);
144 * Set up the initialization block and the descriptor rings.
147 am7990_meminit(struct lance_softc *sc)
149 struct ifnet *ifp = sc->ifp;
156 if (ifp->if_flags & IFF_PROMISC)
157 init.init_mode = LE_MODE_NORMAL | LE_MODE_PROM;
159 init.init_mode = LE_MODE_NORMAL;
161 init.init_padr[0] = (sc->sc_enaddr[1] << 8) | sc->sc_enaddr[0];
162 init.init_padr[1] = (sc->sc_enaddr[3] << 8) | sc->sc_enaddr[2];
163 init.init_padr[2] = (sc->sc_enaddr[5] << 8) | sc->sc_enaddr[4];
164 lance_setladrf(sc, init.init_ladrf);
167 sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
169 a = sc->sc_addr + LE_RMDADDR(sc, 0);
171 init.init_rlen = (a >> 16) | ((ffs(sc->sc_nrbuf) - 1) << 13);
173 a = sc->sc_addr + LE_TMDADDR(sc, 0);
175 init.init_tlen = (a >> 16) | ((ffs(sc->sc_ntbuf) - 1) << 13);
177 (*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
180 * Set up receive ring descriptors.
182 for (bix = 0; bix < sc->sc_nrbuf; bix++) {
183 a = sc->sc_addr + LE_RBUFADDR(sc, bix);
185 rmd.rmd1_hadr = a >> 16;
186 rmd.rmd1_bits = LE_R1_OWN;
187 rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
189 (*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
194 * Set up transmit ring descriptors.
196 for (bix = 0; bix < sc->sc_ntbuf; bix++) {
197 a = sc->sc_addr + LE_TBUFADDR(sc, bix);
199 tmd.tmd1_hadr = a >> 16;
201 tmd.tmd2 = LE_XMD2_ONES;
203 (*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
209 am7990_rint(struct lance_softc *sc)
211 struct ifnet *ifp = sc->ifp;
215 #if defined(LANCE_REVC_BUG)
216 struct ether_header *eh;
217 /* Make sure this is short-aligned, for ether_cmp(). */
218 static uint16_t bcast_enaddr[3] = { ~0, ~0, ~0 };
221 bix = sc->sc_last_rd;
223 /* Process all buffers with valid data. */
225 rp = LE_RMDADDR(sc, bix);
226 (*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
228 if (rmd.rmd1_bits & LE_R1_OWN)
232 if ((rmd.rmd1_bits & (LE_R1_ERR | LE_R1_STP | LE_R1_ENP)) !=
233 (LE_R1_STP | LE_R1_ENP)) {
234 if (rmd.rmd1_bits & LE_R1_ERR) {
236 if (rmd.rmd1_bits & LE_R1_ENP) {
237 if ((rmd.rmd1_bits & LE_R1_OFLO) == 0) {
238 if (rmd.rmd1_bits & LE_R1_FRAM)
241 if (rmd.rmd1_bits & LE_R1_CRC)
246 if (rmd.rmd1_bits & LE_R1_OFLO)
247 if_printf(ifp, "overflow\n");
249 if (rmd.rmd1_bits & LE_R1_BUFF)
251 "receive buffer error\n");
252 } else if ((rmd.rmd1_bits & (LE_R1_STP | LE_R1_ENP)) !=
253 (LE_R1_STP | LE_R1_ENP))
254 if_printf(ifp, "dropping chained buffer\n");
257 if (sc->sc_flags & LE_DEBUG)
258 am7990_recv_print(sc, bix);
260 /* Pull the packet off the interface. */
261 m = lance_get(sc, LE_RBUFADDR(sc, bix),
262 (int)rmd.rmd3 - ETHER_CRC_LEN);
265 rmd.rmd1_bits = LE_R1_OWN;
266 rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
268 (*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
270 if (++bix == sc->sc_nrbuf)
274 IFNET_STAT_INC(ifp, ipackets, 1);
276 #ifdef LANCE_REVC_BUG
278 * The old LANCE (Rev. C) chips have a bug which
279 * causes garbage to be inserted in front of the
280 * received packet. The workaround is to ignore
281 * packets with an invalid destination address
282 * (garbage will usually not match).
283 * Of course, this precludes multicast support...
285 eh = mtod(m, struct ether_header *);
286 if (memcmp(eh->ether_dhost, sc->sc_enaddr,
288 memcmp(eh->ether_dhost, etherbroadcastaddr,
295 /* Pass the packet up. */
296 (*ifp->if_input)(ifp, m);
298 IFNET_STAT_INC(ifp, ierrors, 1);
301 sc->sc_last_rd = bix;
305 am7990_tint(struct lance_softc *sc)
307 struct ifnet *ifp = sc->ifp;
311 bix = sc->sc_first_td;
314 if (sc->sc_no_td <= 0)
317 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
321 if (sc->sc_flags & LE_DEBUG)
322 if_printf(ifp, "trans tmd: "
323 "ladr %04x, hadr %02x, flags %02x, "
324 "bcnt %04x, mcnt %04x\n",
325 tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits,
329 if (tmd.tmd1_bits & LE_T1_OWN)
332 ifq_clr_oactive(&ifp->if_snd);
334 if (tmd.tmd1_bits & LE_T1_ERR) {
335 if (tmd.tmd3 & LE_T3_BUFF)
336 if_printf(ifp, "transmit buffer error\n");
337 else if (tmd.tmd3 & LE_T3_UFLO)
338 if_printf(ifp, "underflow\n");
339 if (tmd.tmd3 & (LE_T3_BUFF | LE_T3_UFLO)) {
340 lance_init_locked(sc);
343 if (tmd.tmd3 & LE_T3_LCAR) {
344 if (sc->sc_flags & LE_CARRIER) {
345 ifp->if_link_state = LINK_STATE_DOWN;
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 IFNET_STAT_INC(ifp, collisions, 1);
356 if (tmd.tmd3 & LE_T3_RTRY) {
358 if_printf(ifp, "excessive collisions, tdr %d\n",
359 tmd.tmd3 & LE_T3_TDR_MASK);
361 IFNET_STAT_INC(ifp, collisions, 16);
363 IFNET_STAT_INC(ifp, oerrors, 1);
365 if (tmd.tmd1_bits & LE_T1_ONE)
366 IFNET_STAT_INC(ifp, collisions, 1);
367 else if (tmd.tmd1_bits & LE_T1_MORE)
368 /* Real number is unknown. */
369 IFNET_STAT_INC(ifp, collisions, 2);
370 IFNET_STAT_INC(ifp, opackets, 1);
373 if (++bix == sc->sc_ntbuf)
379 sc->sc_first_td = bix;
381 ifp->if_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->ifp;
394 if (sc->sc_hwintr && (*sc->sc_hwintr)(sc) == -1) {
395 IFNET_STAT_INC(ifp, ierrors, 1);
396 lance_init_locked(sc);
400 isr = (*sc->sc_rdcsr)(sc, LE_CSR0);
401 #if defined(LEDEBUG) && LEDEBUG > 1
402 if (sc->sc_flags & LE_DEBUG)
403 if_printf(ifp, "%s: entering with isr=%04x\n", __func__, isr);
405 if ((isr & LE_C0_INTR) == 0) {
410 * Clear interrupt source flags and turn off interrupts. If we
411 * don't clear these flags before processing their sources we
412 * could completely miss some interrupt events as the NIC can
413 * change these flags while we're in this handler. We turn off
414 * interrupts so we don't get another RX interrupt while still
415 * processing the previous one in ifp->if_input() with the
416 * driver lock dropped.
418 (*sc->sc_wrcsr)(sc, LE_CSR0, isr & ~(LE_C0_INEA | LE_C0_TDMD |
419 LE_C0_STOP | LE_C0_STRT | LE_C0_INIT));
421 if (isr & LE_C0_ERR) {
422 if (isr & LE_C0_BABL) {
424 if_printf(ifp, "babble\n");
426 IFNET_STAT_INC(ifp, oerrors, 1);
429 if (isr & LE_C0_CERR) {
430 if_printf(ifp, "collision error\n");
431 IFNET_STAT_INC(ifp, collisions, 1);
434 if (isr & LE_C0_MISS) {
436 if_printf(ifp, "missed packet\n");
438 IFNET_STAT_INC(ifp, ierrors, 1);
440 if (isr & LE_C0_MERR) {
441 if_printf(ifp, "memory error\n");
442 lance_init_locked(sc);
447 if ((isr & LE_C0_RXON) == 0) {
448 if_printf(ifp, "receiver disabled\n");
449 IFNET_STAT_INC(ifp, ierrors, 1);
450 lance_init_locked(sc);
453 if ((isr & LE_C0_TXON) == 0) {
454 if_printf(ifp, "transmitter disabled\n");
455 IFNET_STAT_INC(ifp, oerrors, 1);
456 lance_init_locked(sc);
461 * Pretend we have carrier; if we don't this will be cleared shortly.
463 if (!(sc->sc_flags & LE_CARRIER)) {
464 ifp->if_link_state = LINK_STATE_UP;
465 if_link_state_change(ifp);
467 sc->sc_flags |= LE_CARRIER;
469 if (isr & LE_C0_RINT)
471 if (isr & LE_C0_TINT)
474 /* Enable interrupts again. */
475 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA);
477 if (!ifq_is_empty(&ifp->if_snd))
482 * Set up output on interface.
483 * Get another datagram to send off of the interface queue, and map it to the
484 * interface before starting the output.
487 am7990_start_locked(struct lance_softc *sc)
489 struct ifnet *ifp = sc->ifp;
492 int bix, enq, len, rp;
494 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
497 bix = sc->sc_last_td;
500 for (; sc->sc_no_td < sc->sc_ntbuf &&
501 !ifq_is_empty(&ifp->if_snd);) {
502 rp = LE_TMDADDR(sc, bix);
503 (*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
505 if (tmd.tmd1_bits & LE_T1_OWN) {
506 ifq_set_oactive(&ifp->if_snd);
508 "missing buffer, no_td = %d, last_td = %d\n",
509 sc->sc_no_td, sc->sc_last_td);
512 m = ifq_dequeue(&ifp->if_snd);
517 * If BPF is listening on this interface, let it see the
518 * packet before we commit it to the wire.
523 * Copy the mbuf chain into the transmit buffer.
525 len = lance_put(sc, LE_TBUFADDR(sc, bix), m);
528 if (len > ETHERMTU + ETHER_HDR_LEN)
529 if_printf(ifp, "packet length %d\n", len);
533 * Init transmit registers, and set transmit start flag.
535 tmd.tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
536 tmd.tmd2 = -len | LE_XMD2_ONES;
539 (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
542 if (sc->sc_flags & LE_DEBUG)
543 am7990_xmit_print(sc, bix);
546 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
549 if (++bix == sc->sc_ntbuf)
552 if (++sc->sc_no_td == sc->sc_ntbuf) {
553 ifq_set_oactive(&ifp->if_snd);
558 sc->sc_last_td = bix;
566 am7990_recv_print(struct lance_softc *sc, int no)
568 struct ifnet *ifp = sc->ifp;
569 struct ether_header eh;
573 (*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
575 if_printf(ifp, "receive buffer %d, len = %d\n", no, len);
576 if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
578 "ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
579 rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits, rmd.rmd2, rmd.rmd3);
580 if (len - ETHER_CRC_LEN >= sizeof(eh)) {
581 (*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
582 if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
583 kprintf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
584 ntohs(eh.ether_type));
589 am7990_xmit_print(struct lance_softc *sc, int no)
591 struct ifnet *ifp = sc->ifp;
592 struct ether_header eh;
596 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
598 if_printf(ifp, "transmit buffer %d, len = %d\n", no, len);
599 if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
601 "ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
602 tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits, tmd.tmd2, tmd.tmd3);
603 if (len >= sizeof(eh)) {
604 (*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
605 if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
606 kprintf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
607 ntohs(eh.ether_type));