1 /* $NetBSD: am79900.c,v 1.17 2005/12/24 20:27:29 perry Exp $ */
2 /* $FreeBSD: src/sys/dev/le/am79900.c,v 1.3 2006/05/16 21:04:01 marius Exp $ */
5 * Copyright (c) 1997 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
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.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
41 * Copyright (c) 1992, 1993
42 * The Regents of the University of California. All rights reserved.
44 * This code is derived from software contributed to Berkeley by
45 * Ralph Campbell and Rick Macklem.
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
76 * Matthias Drochner. All rights reserved.
77 * Copyright (c) 1995 Charles M. Hannum. All rights reserved.
79 * This code is derived from software contributed to Berkeley by
80 * Ralph Campbell and Rick Macklem.
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
85 * 1. Redistributions of source code must retain the above copyright
86 * notice, this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright
88 * notice, this list of conditions and the following disclaimer in the
89 * documentation and/or other materials provided with the distribution.
90 * 3. All advertising materials mentioning features or use of this software
91 * must display the following acknowledgement:
92 * This product includes software developed by the University of
93 * California, Berkeley and its contributors.
94 * 4. Neither the name of the University nor the names of its contributors
95 * may be used to endorse or promote products derived from this software
96 * without specific prior written permission.
98 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
99 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
110 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
113 #include <sys/param.h>
115 #include <sys/endian.h>
116 #include <sys/lock.h>
117 #include <sys/mbuf.h>
118 #include <sys/socket.h>
122 #include <net/ethernet.h>
124 #include <net/if_arp.h>
125 #include <net/if_dl.h>
126 #include <net/if_media.h>
127 #include <net/if_var.h>
128 #include <net/ifq_var.h>
130 #include <dev/netif/lnc/lancereg.h>
131 #include <dev/netif/lnc/lancevar.h>
132 #include <dev/netif/lnc/am79900reg.h>
133 #include <dev/netif/lnc/am79900var.h>
135 static void am79900_meminit(struct lance_softc *);
136 static void am79900_rint(struct lance_softc *);
137 static void am79900_tint(struct lance_softc *);
138 static void am79900_start_locked(struct lance_softc *sc);
141 static void am79900_recv_print(struct lance_softc *, int);
142 static void am79900_xmit_print(struct lance_softc *, int);
146 am79900_config(struct am79900_softc *sc, const char* name, int unit)
150 sc->lsc.sc_meminit = am79900_meminit;
151 sc->lsc.sc_start_locked = am79900_start_locked;
153 error = lance_config(&sc->lsc, name, unit);
158 sc->lsc.sc_initaddr = mem;
159 mem += sizeof(struct leinit);
160 sc->lsc.sc_rmdaddr = mem;
161 mem += sizeof(struct lermd) * sc->lsc.sc_nrbuf;
162 sc->lsc.sc_tmdaddr = mem;
163 mem += sizeof(struct letmd) * sc->lsc.sc_ntbuf;
164 sc->lsc.sc_rbufaddr = mem;
165 mem += LEBLEN * sc->lsc.sc_nrbuf;
166 sc->lsc.sc_tbufaddr = mem;
167 mem += LEBLEN * sc->lsc.sc_ntbuf;
169 if (mem > sc->lsc.sc_memsize)
170 panic("%s: memsize", __func__);
172 lance_attach(&sc->lsc);
178 am79900_detach(struct am79900_softc *sc)
181 ether_ifdetach(sc->lsc.ifp);
185 * Set up the initialization block and the descriptor rings.
188 am79900_meminit(struct lance_softc *sc)
190 struct ifnet *ifp = sc->ifp;
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->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)
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 (memcmp(eh->ether_shost, sc->sc_enaddr,
328 ETHER_ADDR_LEN) == 0) {
334 /* Pass the packet up. */
335 (*ifp->if_input)(ifp, m);
340 sc->sc_last_rd = bix;
344 am79900_tint(struct lance_softc *sc)
346 struct ifnet *ifp = sc->ifp;
351 bix = sc->sc_first_td;
354 if (sc->sc_no_td <= 0)
357 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
360 tmd1 = LE_LE32TOH(tmd.tmd1);
363 if (sc->sc_flags & LE_DEBUG)
364 if_printf(ifp, "trans tmd: "
365 "adr %08x, flags/blen %08x\n",
366 LE_LE32TOH(tmd.tmd0), tmd1);
369 if (tmd1 & LE_T1_OWN)
372 ifp->if_flags &= ~IFF_OACTIVE;
374 if (tmd1 & LE_T1_ERR) {
375 tmd2 = LE_LE32TOH(tmd.tmd2);
376 if (tmd2 & LE_T2_BUFF)
377 if_printf(ifp, "transmit buffer error\n");
378 else if (tmd2 & LE_T2_UFLO)
379 if_printf(ifp, "underflow\n");
380 if (tmd2 & (LE_T2_BUFF | LE_T2_UFLO)) {
381 lance_init_locked(sc);
384 if (tmd2 & LE_T2_LCAR) {
385 if (sc->sc_flags & LE_CARRIER) {
386 ifp->if_link_state = LINK_STATE_DOWN;
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 ifp->if_collisions++;
397 if (tmd2 & LE_T2_RTRY) {
399 if_printf(ifp, "excessive collisions\n");
401 ifp->if_collisions += 16;
405 if (tmd1 & LE_T1_ONE)
406 ifp->if_collisions++;
407 else if (tmd1 & LE_T1_MORE)
408 /* Real number is unknown. */
409 ifp->if_collisions += 2;
413 if (++bix == sc->sc_ntbuf)
419 sc->sc_first_td = bix;
421 ifp->if_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->ifp;
434 if (sc->sc_hwintr && (*sc->sc_hwintr)(sc) == -1) {
436 lance_init_locked(sc);
440 isr = (*sc->sc_rdcsr)(sc, LE_CSR0);
441 #if defined(LEDEBUG) && LEDEBUG > 1
442 if (sc->sc_flags & LE_DEBUG)
443 if_printf(ifp, "%s: entering with isr=%04x\n", __func__, isr);
445 if ((isr & LE_C0_INTR) == 0) {
450 * Clear interrupt source flags and turn off interrupts. If we
451 * don't clear these flags before processing their sources we
452 * could completely miss some interrupt events as the NIC can
453 * change these flags while we're in this handler. We turn off
454 * interrupts so we don't get another RX interrupt while still
455 * processing the previous one in ifp->if_input() with the
456 * driver lock dropped.
458 (*sc->sc_wrcsr)(sc, LE_CSR0, isr & ~(LE_C0_INEA | LE_C0_TDMD |
459 LE_C0_STOP | LE_C0_STRT | LE_C0_INIT));
461 if (isr & LE_C0_ERR) {
462 if (isr & LE_C0_BABL) {
464 if_printf(ifp, "babble\n");
469 if (isr & LE_C0_CERR) {
470 if_printf(ifp, "collision error\n");
471 ifp->if_collisions++;
474 if (isr & LE_C0_MISS) {
476 if_printf(ifp, "missed packet\n");
480 if (isr & LE_C0_MERR) {
481 if_printf(ifp, "memory error\n");
482 lance_init_locked(sc);
487 if ((isr & LE_C0_RXON) == 0) {
488 if_printf(ifp, "receiver disabled\n");
490 lance_init_locked(sc);
493 if ((isr & LE_C0_TXON) == 0) {
494 if_printf(ifp, "transmitter disabled\n");
496 lance_init_locked(sc);
501 * Pretend we have carrier; if we don't this will be cleared shortly.
503 if (!(sc->sc_flags & LE_CARRIER)) {
504 ifp->if_link_state = LINK_STATE_UP;
505 if_link_state_change(ifp);
507 sc->sc_flags |= LE_CARRIER;
509 if (isr & LE_C0_RINT)
511 if (isr & LE_C0_TINT)
514 /* Enable interrupts again. */
515 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA);
517 if (!ifq_is_empty(&ifp->if_snd))
522 * Set up output on interface.
523 * Get another datagram to send off of the interface queue, and map it to the
524 * interface before starting the output.
527 am79900_start_locked(struct lance_softc *sc)
529 struct ifnet *ifp = sc->ifp;
532 int bix, enq, len, rp;
534 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
538 bix = sc->sc_last_td;
541 for (; sc->sc_no_td < sc->sc_ntbuf &&
542 !ifq_is_empty(&ifp->if_snd);) {
543 rp = LE_TMDADDR(sc, bix);
544 (*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
546 if (LE_LE32TOH(tmd.tmd1) & LE_T1_OWN) {
547 ifp->if_flags |= IFF_OACTIVE;
549 "missing buffer, no_td = %d, last_td = %d\n",
550 sc->sc_no_td, sc->sc_last_td);
553 m = ifq_dequeue(&ifp->if_snd, NULL);
558 * If BPF is listening on this interface, let it see the packet
559 * before we commit it to the wire.
564 * Copy the mbuf chain into the transmit buffer.
566 len = lance_put(sc, LE_TBUFADDR(sc, bix), m);
569 if (len > ETHERMTU + ETHER_HDR_LEN)
570 if_printf(ifp, "packet length %d\n", len);
574 * Init transmit registers, and set transmit start flag.
576 tmd.tmd1 = LE_HTOLE32(LE_T1_OWN | LE_T1_STP | LE_T1_ENP |
577 LE_T1_ONES | (-len & 0xfff));
581 (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
584 if (sc->sc_flags & LE_DEBUG)
585 am79900_xmit_print(sc, bix);
588 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
591 if (++bix == sc->sc_ntbuf)
594 if (++sc->sc_no_td == sc->sc_ntbuf) {
595 ifp->if_flags |= IFF_OACTIVE;
600 sc->sc_last_td = bix;
608 am79900_recv_print(struct lance_softc *sc, int no)
610 struct ifnet *ifp = sc->ifp;
611 struct ether_header eh;
615 (*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
616 len = LE_LE32TOH(rmd.rmd2) & 0xfff;
617 if_printf(ifp, "receive buffer %d, len = %d\n", no, len);
618 if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
619 if_printf(ifp, "adr %08x, flags/blen %08x\n", LE_LE32TOH(rmd.rmd0),
620 LE_LE32TOH(rmd.rmd1));
621 if (len - ETHER_CRC_LEN >= sizeof(eh)) {
622 (*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
623 if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
624 kprintf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
625 ntohs(eh.ether_type));
630 am79900_xmit_print(struct lance_softc *sc, int no)
632 struct ifnet *ifp = sc->ifp;
633 struct ether_header eh;
637 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
638 len = -(LE_LE32TOH(tmd.tmd1) & 0xfff);
639 if_printf(ifp, "transmit buffer %d, len = %d\n", no, len);
640 if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
641 if_printf(ifp, "adr %08x, flags/blen %08x\n", LE_LE32TOH(tmd.tmd0),
642 LE_LE32TOH(tmd.tmd1));
643 if (len >= sizeof(eh)) {
644 (*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
645 if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
646 kprintf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
647 ntohs(eh.ether_type));