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>
121 #include <net/ethernet.h>
123 #include <net/if_arp.h>
124 #include <net/if_dl.h>
125 #include <net/if_media.h>
126 #include <net/if_var.h>
127 #include <net/ifq_var.h>
129 #include <dev/netif/lnc/lancereg.h>
130 #include <dev/netif/lnc/lancevar.h>
131 #include <dev/netif/lnc/am79900reg.h>
132 #include <dev/netif/lnc/am79900var.h>
134 static void am79900_meminit(struct lance_softc *);
135 static void am79900_rint(struct lance_softc *);
136 static void am79900_tint(struct lance_softc *);
137 static void am79900_start_locked(struct lance_softc *sc);
140 static void am79900_recv_print(struct lance_softc *, int);
141 static void am79900_xmit_print(struct lance_softc *, int);
145 am79900_config(struct am79900_softc *sc, const char* name, int unit)
149 sc->lsc.sc_meminit = am79900_meminit;
150 sc->lsc.sc_start_locked = am79900_start_locked;
152 lance_config(&sc->lsc, name, unit);
155 sc->lsc.sc_initaddr = mem;
156 mem += sizeof(struct leinit);
157 sc->lsc.sc_rmdaddr = mem;
158 mem += sizeof(struct lermd) * sc->lsc.sc_nrbuf;
159 sc->lsc.sc_tmdaddr = mem;
160 mem += sizeof(struct letmd) * sc->lsc.sc_ntbuf;
161 sc->lsc.sc_rbufaddr = mem;
162 mem += LEBLEN * sc->lsc.sc_nrbuf;
163 sc->lsc.sc_tbufaddr = mem;
164 mem += LEBLEN * sc->lsc.sc_ntbuf;
166 if (mem > sc->lsc.sc_memsize)
167 panic("%s: memsize", __func__);
173 am79900_detach(struct am79900_softc *sc)
176 ether_ifdetach(sc->lsc.ifp);
180 * Set up the initialization block and the descriptor rings.
183 am79900_meminit(struct lance_softc *sc)
185 struct ifnet *ifp = sc->ifp;
192 if (ifp->if_flags & IFF_PROMISC)
193 init.init_mode = LE_HTOLE32(LE_MODE_NORMAL | LE_MODE_PROM);
195 init.init_mode = LE_HTOLE32(LE_MODE_NORMAL);
197 init.init_mode |= LE_HTOLE32(((ffs(sc->sc_ntbuf) - 1) << 28) |
198 ((ffs(sc->sc_nrbuf) - 1) << 20));
200 init.init_padr[0] = LE_HTOLE32(sc->sc_enaddr[0] |
201 (sc->sc_enaddr[1] << 8) | (sc->sc_enaddr[2] << 16) |
202 (sc->sc_enaddr[3] << 24));
203 init.init_padr[1] = LE_HTOLE32(sc->sc_enaddr[4] |
204 (sc->sc_enaddr[5] << 8));
205 lance_setladrf(sc, init.init_ladrf);
208 sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
210 a = sc->sc_addr + LE_RMDADDR(sc, 0);
211 init.init_rdra = LE_HTOLE32(a);
213 a = sc->sc_addr + LE_TMDADDR(sc, 0);
214 init.init_tdra = LE_HTOLE32(a);
216 (*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
219 * Set up receive ring descriptors.
221 for (bix = 0; bix < sc->sc_nrbuf; bix++) {
222 a = sc->sc_addr + LE_RBUFADDR(sc, bix);
223 rmd.rmd0 = LE_HTOLE32(a);
224 rmd.rmd1 = LE_HTOLE32(LE_R1_OWN | LE_R1_ONES |
228 (*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
233 * Set up transmit ring descriptors.
235 for (bix = 0; bix < sc->sc_ntbuf; bix++) {
236 a = sc->sc_addr + LE_TBUFADDR(sc, bix);
237 tmd.tmd0 = LE_HTOLE32(a);
238 tmd.tmd1 = LE_HTOLE32(LE_T1_ONES);
241 (*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
247 am79900_rint(struct lance_softc *sc)
249 struct ifnet *ifp = sc->ifp;
254 #if defined(__x86_64__)
255 struct ether_header *eh;
258 bix = sc->sc_last_rd;
260 /* Process all buffers with valid data. */
262 rp = LE_RMDADDR(sc, bix);
263 (*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
265 rmd1 = LE_LE32TOH(rmd.rmd1);
266 if (rmd1 & LE_R1_OWN)
270 if ((rmd1 & (LE_R1_ERR | LE_R1_STP | LE_R1_ENP)) !=
271 (LE_R1_STP | LE_R1_ENP)){
272 if (rmd1 & LE_R1_ERR) {
274 if (rmd1 & LE_R1_ENP) {
275 if ((rmd1 & LE_R1_OFLO) == 0) {
276 if (rmd1 & LE_R1_FRAM)
279 if (rmd1 & LE_R1_CRC)
284 if (rmd1 & LE_R1_OFLO)
285 if_printf(ifp, "overflow\n");
287 if (rmd1 & LE_R1_BUFF)
289 "receive buffer error\n");
290 } else if ((rmd1 & (LE_R1_STP | LE_R1_ENP)) !=
291 (LE_R1_STP | LE_R1_ENP))
292 if_printf(ifp, "dropping chained buffer\n");
295 if (sc->sc_flags & LE_DEBUG)
296 am79900_recv_print(sc, bix);
298 /* Pull the packet off the interface. */
299 m = lance_get(sc, LE_RBUFADDR(sc, bix),
300 (LE_LE32TOH(rmd.rmd2) & 0xfff) - ETHER_CRC_LEN);
303 rmd.rmd1 = LE_HTOLE32(LE_R1_OWN | LE_R1_ONES |
307 (*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
309 if (++bix == sc->sc_nrbuf)
313 IFNET_STAT_INC(ifp, ipackets, 1);
317 * The VMware LANCE does not present IFF_SIMPLEX
318 * behavior on multicast packets. Thus drop the
319 * packet if it is from ourselves.
321 eh = mtod(m, struct ether_header *);
322 if (memcmp(eh->ether_shost, sc->sc_enaddr,
323 ETHER_ADDR_LEN) == 0) {
329 /* Pass the packet up. */
330 ifp->if_input(ifp, m, NULL, -1);
332 IFNET_STAT_INC(ifp, ierrors, 1);
335 sc->sc_last_rd = bix;
339 am79900_tint(struct lance_softc *sc)
341 struct ifnet *ifp = sc->ifp;
346 bix = sc->sc_first_td;
349 if (sc->sc_no_td <= 0)
352 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
355 tmd1 = LE_LE32TOH(tmd.tmd1);
358 if (sc->sc_flags & LE_DEBUG)
359 if_printf(ifp, "trans tmd: "
360 "adr %08x, flags/blen %08x\n",
361 LE_LE32TOH(tmd.tmd0), tmd1);
364 if (tmd1 & LE_T1_OWN)
367 ifq_clr_oactive(&ifp->if_snd);
369 if (tmd1 & LE_T1_ERR) {
370 tmd2 = LE_LE32TOH(tmd.tmd2);
371 if (tmd2 & LE_T2_BUFF)
372 if_printf(ifp, "transmit buffer error\n");
373 else if (tmd2 & LE_T2_UFLO)
374 if_printf(ifp, "underflow\n");
375 if (tmd2 & (LE_T2_BUFF | LE_T2_UFLO)) {
376 lance_init_locked(sc);
379 if (tmd2 & LE_T2_LCAR) {
380 if (sc->sc_flags & LE_CARRIER) {
381 ifp->if_link_state = LINK_STATE_DOWN;
382 if_link_state_change(ifp);
384 sc->sc_flags &= ~LE_CARRIER;
385 if (sc->sc_nocarrier)
386 (*sc->sc_nocarrier)(sc);
388 if_printf(ifp, "lost carrier\n");
390 if (tmd2 & LE_T2_LCOL)
391 IFNET_STAT_INC(ifp, collisions, 1);
392 if (tmd2 & LE_T2_RTRY) {
394 if_printf(ifp, "excessive collisions\n");
396 IFNET_STAT_INC(ifp, collisions, 16);
398 IFNET_STAT_INC(ifp, oerrors, 1);
400 if (tmd1 & LE_T1_ONE)
401 IFNET_STAT_INC(ifp, collisions, 1);
402 else if (tmd1 & LE_T1_MORE)
403 /* Real number is unknown. */
404 IFNET_STAT_INC(ifp, collisions, 2);
405 IFNET_STAT_INC(ifp, opackets, 1);
408 if (++bix == sc->sc_ntbuf)
414 sc->sc_first_td = bix;
416 ifp->if_timer = sc->sc_no_td > 0 ? 5 : 0;
420 * Controller interrupt
423 am79900_intr(void *arg)
425 struct lance_softc *sc = arg;
426 struct ifnet *ifp = sc->ifp;
429 if (sc->sc_hwintr && (*sc->sc_hwintr)(sc) == -1) {
430 IFNET_STAT_INC(ifp, ierrors, 1);
431 lance_init_locked(sc);
435 isr = (*sc->sc_rdcsr)(sc, LE_CSR0);
436 #if defined(LEDEBUG) && LEDEBUG > 1
437 if (sc->sc_flags & LE_DEBUG)
438 if_printf(ifp, "%s: entering with isr=%04x\n", __func__, isr);
440 if ((isr & LE_C0_INTR) == 0) {
445 * Clear interrupt source flags and turn off interrupts. If we
446 * don't clear these flags before processing their sources we
447 * could completely miss some interrupt events as the NIC can
448 * change these flags while we're in this handler. We turn off
449 * interrupts so we don't get another RX interrupt while still
450 * processing the previous one in ifp->if_input() with the
451 * driver lock dropped.
453 (*sc->sc_wrcsr)(sc, LE_CSR0, isr & ~(LE_C0_INEA | LE_C0_TDMD |
454 LE_C0_STOP | LE_C0_STRT | LE_C0_INIT));
456 if (isr & LE_C0_ERR) {
457 if (isr & LE_C0_BABL) {
459 if_printf(ifp, "babble\n");
461 IFNET_STAT_INC(ifp, oerrors, 1);
464 if (isr & LE_C0_CERR) {
465 if_printf(ifp, "collision error\n");
466 ifp->if_collisions++;
469 if (isr & LE_C0_MISS) {
471 if_printf(ifp, "missed packet\n");
473 IFNET_STAT_INC(ifp, ierrors, 1);
475 if (isr & LE_C0_MERR) {
476 if_printf(ifp, "memory error\n");
477 lance_init_locked(sc);
482 if ((isr & LE_C0_RXON) == 0) {
483 if_printf(ifp, "receiver disabled\n");
484 IFNET_STAT_INC(ifp, ierrors, 1);
485 lance_init_locked(sc);
488 if ((isr & LE_C0_TXON) == 0) {
489 if_printf(ifp, "transmitter disabled\n");
490 IFNET_STAT_INC(ifp, oerrors, 1);
491 lance_init_locked(sc);
496 * Pretend we have carrier; if we don't this will be cleared shortly.
498 if (!(sc->sc_flags & LE_CARRIER)) {
499 ifp->if_link_state = LINK_STATE_UP;
500 if_link_state_change(ifp);
502 sc->sc_flags |= LE_CARRIER;
504 if (isr & LE_C0_RINT)
506 if (isr & LE_C0_TINT)
509 /* Enable interrupts again. */
510 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA);
512 if (!ifq_is_empty(&ifp->if_snd))
517 * Set up output on interface.
518 * Get another datagram to send off of the interface queue, and map it to the
519 * interface before starting the output.
522 am79900_start_locked(struct lance_softc *sc)
524 struct ifnet *ifp = sc->ifp;
527 int bix, enq, len, rp;
529 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
532 bix = sc->sc_last_td;
535 for (; sc->sc_no_td < sc->sc_ntbuf &&
536 !ifq_is_empty(&ifp->if_snd);) {
537 rp = LE_TMDADDR(sc, bix);
538 (*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
540 if (LE_LE32TOH(tmd.tmd1) & LE_T1_OWN) {
541 ifq_set_oactive(&ifp->if_snd);
543 "missing buffer, no_td = %d, last_td = %d\n",
544 sc->sc_no_td, sc->sc_last_td);
547 m = ifq_dequeue(&ifp->if_snd);
552 * If BPF is listening on this interface, let it see the packet
553 * before we commit it to the wire.
558 * Copy the mbuf chain into the transmit buffer.
560 len = lance_put(sc, LE_TBUFADDR(sc, bix), m);
563 if (len > ETHERMTU + ETHER_HDR_LEN)
564 if_printf(ifp, "packet length %d\n", len);
568 * Init transmit registers, and set transmit start flag.
570 tmd.tmd1 = LE_HTOLE32(LE_T1_OWN | LE_T1_STP | LE_T1_ENP |
571 LE_T1_ONES | (-len & 0xfff));
575 (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
578 if (sc->sc_flags & LE_DEBUG)
579 am79900_xmit_print(sc, bix);
582 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
585 if (++bix == sc->sc_ntbuf)
588 if (++sc->sc_no_td == sc->sc_ntbuf) {
589 ifq_set_oactive(&ifp->if_snd);
594 sc->sc_last_td = bix;
602 am79900_recv_print(struct lance_softc *sc, int no)
604 struct ifnet *ifp = sc->ifp;
605 struct ether_header eh;
609 (*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
610 len = LE_LE32TOH(rmd.rmd2) & 0xfff;
611 if_printf(ifp, "receive buffer %d, len = %d\n", no, len);
612 if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
613 if_printf(ifp, "adr %08x, flags/blen %08x\n", LE_LE32TOH(rmd.rmd0),
614 LE_LE32TOH(rmd.rmd1));
615 if (len - ETHER_CRC_LEN >= sizeof(eh)) {
616 (*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
617 if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
618 kprintf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
619 ntohs(eh.ether_type));
624 am79900_xmit_print(struct lance_softc *sc, int no)
626 struct ifnet *ifp = sc->ifp;
627 struct ether_header eh;
631 (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
632 len = -(LE_LE32TOH(tmd.tmd1) & 0xfff);
633 if_printf(ifp, "transmit buffer %d, len = %d\n", no, len);
634 if_printf(ifp, "status %04x\n", (*sc->sc_rdcsr)(sc, LE_CSR0));
635 if_printf(ifp, "adr %08x, flags/blen %08x\n", LE_LE32TOH(tmd.tmd0),
636 LE_LE32TOH(tmd.tmd1));
637 if (len >= sizeof(eh)) {
638 (*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
639 if_printf(ifp, "dst %s", ether_sprintf(eh.ether_dhost));
640 kprintf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
641 ntohs(eh.ether_type));