1 /* $NetBSD: am7990.c,v 1.68 2005/12/11 12:21:25 christos Exp $ */
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center.
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
74 #include <sys/cdefs.h>
75 __FBSDID("$FreeBSD$");
77 #include <sys/param.h>
79 #include <sys/endian.h>
82 #include <sys/mutex.h>
83 #include <sys/socket.h>
86 #include <net/ethernet.h>
88 #include <net/if_arp.h>
89 #include <net/if_dl.h>
90 #include <net/if_media.h>
91 #include <net/if_var.h>
93 #include <machine/bus.h>
95 #include <dev/le/lancereg.h>
96 #include <dev/le/lancevar.h>
97 #include <dev/le/am7990reg.h>
98 #include <dev/le/am7990var.h>
100 static void am7990_meminit(struct lance_softc
*);
101 static void am7990_rint(struct lance_softc
*);
102 static void am7990_tint(struct lance_softc
*);
103 static void am7990_start_locked(struct lance_softc
*sc
);
106 static void am7990_recv_print(struct lance_softc
*, int);
107 static void am7990_xmit_print(struct lance_softc
*, int);
111 am7990_config(struct am7990_softc
*sc
, const char* name
, int unit
)
115 sc
->lsc
.sc_meminit
= am7990_meminit
;
116 sc
->lsc
.sc_start_locked
= am7990_start_locked
;
118 error
= lance_config(&sc
->lsc
, name
, unit
);
123 sc
->lsc
.sc_initaddr
= mem
;
124 mem
+= sizeof(struct leinit
);
125 sc
->lsc
.sc_rmdaddr
= mem
;
126 mem
+= sizeof(struct lermd
) * sc
->lsc
.sc_nrbuf
;
127 sc
->lsc
.sc_tmdaddr
= mem
;
128 mem
+= sizeof(struct letmd
) * sc
->lsc
.sc_ntbuf
;
129 sc
->lsc
.sc_rbufaddr
= mem
;
130 mem
+= LEBLEN
* sc
->lsc
.sc_nrbuf
;
131 sc
->lsc
.sc_tbufaddr
= mem
;
132 mem
+= LEBLEN
* sc
->lsc
.sc_ntbuf
;
134 if (mem
> sc
->lsc
.sc_memsize
)
135 panic("%s: memsize", __func__
);
137 lance_attach(&sc
->lsc
);
143 am7990_detach(struct am7990_softc
*sc
)
146 lance_detach(&sc
->lsc
);
150 * Set up the initialization block and the descriptor rings.
153 am7990_meminit(struct lance_softc
*sc
)
155 struct ifnet
*ifp
= sc
->sc_ifp
;
162 LE_LOCK_ASSERT(sc
, MA_OWNED
);
164 if (ifp
->if_flags
& IFF_PROMISC
)
165 init
.init_mode
= LE_MODE_NORMAL
| LE_MODE_PROM
;
167 init
.init_mode
= LE_MODE_NORMAL
;
169 init
.init_padr
[0] = (sc
->sc_enaddr
[1] << 8) | sc
->sc_enaddr
[0];
170 init
.init_padr
[1] = (sc
->sc_enaddr
[3] << 8) | sc
->sc_enaddr
[2];
171 init
.init_padr
[2] = (sc
->sc_enaddr
[5] << 8) | sc
->sc_enaddr
[4];
172 lance_setladrf(sc
, init
.init_ladrf
);
175 sc
->sc_first_td
= sc
->sc_last_td
= sc
->sc_no_td
= 0;
177 a
= sc
->sc_addr
+ LE_RMDADDR(sc
, 0);
179 init
.init_rlen
= (a
>> 16) | ((ffs(sc
->sc_nrbuf
) - 1) << 13);
181 a
= sc
->sc_addr
+ LE_TMDADDR(sc
, 0);
183 init
.init_tlen
= (a
>> 16) | ((ffs(sc
->sc_ntbuf
) - 1) << 13);
185 (*sc
->sc_copytodesc
)(sc
, &init
, LE_INITADDR(sc
), sizeof(init
));
188 * Set up receive ring descriptors.
190 for (bix
= 0; bix
< sc
->sc_nrbuf
; bix
++) {
191 a
= sc
->sc_addr
+ LE_RBUFADDR(sc
, bix
);
193 rmd
.rmd1_hadr
= a
>> 16;
194 rmd
.rmd1_bits
= LE_R1_OWN
;
195 rmd
.rmd2
= -LEBLEN
| LE_XMD2_ONES
;
197 (*sc
->sc_copytodesc
)(sc
, &rmd
, LE_RMDADDR(sc
, bix
),
202 * Set up transmit ring descriptors.
204 for (bix
= 0; bix
< sc
->sc_ntbuf
; bix
++) {
205 a
= sc
->sc_addr
+ LE_TBUFADDR(sc
, bix
);
207 tmd
.tmd1_hadr
= a
>> 16;
209 tmd
.tmd2
= LE_XMD2_ONES
;
211 (*sc
->sc_copytodesc
)(sc
, &tmd
, LE_TMDADDR(sc
, bix
),
217 am7990_rint(struct lance_softc
*sc
)
219 struct ifnet
*ifp
= sc
->sc_ifp
;
223 #if defined(LANCE_REVC_BUG)
224 struct ether_header
*eh
;
225 /* Make sure this is short-aligned, for ether_cmp(). */
226 static uint16_t bcast_enaddr
[3] = { ~0, ~0, ~0 };
229 bix
= sc
->sc_last_rd
;
231 /* Process all buffers with valid data. */
233 rp
= LE_RMDADDR(sc
, bix
);
234 (*sc
->sc_copyfromdesc
)(sc
, &rmd
, rp
, sizeof(rmd
));
236 if (rmd
.rmd1_bits
& LE_R1_OWN
)
240 if ((rmd
.rmd1_bits
& (LE_R1_ERR
| LE_R1_STP
| LE_R1_ENP
)) !=
241 (LE_R1_STP
| LE_R1_ENP
)) {
242 if (rmd
.rmd1_bits
& LE_R1_ERR
) {
244 if (rmd
.rmd1_bits
& LE_R1_ENP
) {
245 if ((rmd
.rmd1_bits
& LE_R1_OFLO
) == 0) {
246 if (rmd
.rmd1_bits
& LE_R1_FRAM
)
249 if (rmd
.rmd1_bits
& LE_R1_CRC
)
254 if (rmd
.rmd1_bits
& LE_R1_OFLO
)
255 if_printf(ifp
, "overflow\n");
257 if (rmd
.rmd1_bits
& LE_R1_BUFF
)
259 "receive buffer error\n");
260 } else if ((rmd
.rmd1_bits
& (LE_R1_STP
| LE_R1_ENP
)) !=
261 (LE_R1_STP
| LE_R1_ENP
))
262 if_printf(ifp
, "dropping chained buffer\n");
265 if (sc
->sc_flags
& LE_DEBUG
)
266 am7990_recv_print(sc
, bix
);
268 /* Pull the packet off the interface. */
269 m
= lance_get(sc
, LE_RBUFADDR(sc
, bix
),
270 (int)rmd
.rmd3
- ETHER_CRC_LEN
);
273 rmd
.rmd1_bits
= LE_R1_OWN
;
274 rmd
.rmd2
= -LEBLEN
| LE_XMD2_ONES
;
276 (*sc
->sc_copytodesc
)(sc
, &rmd
, rp
, sizeof(rmd
));
278 if (++bix
== sc
->sc_nrbuf
)
284 #ifdef LANCE_REVC_BUG
286 * The old LANCE (Rev. C) chips have a bug which
287 * causes garbage to be inserted in front of the
288 * received packet. The workaround is to ignore
289 * packets with an invalid destination address
290 * (garbage will usually not match).
291 * Of course, this precludes multicast support...
293 eh
= mtod(m
, struct ether_header
*);
294 if (ether_cmp(eh
->ether_dhost
, sc
->sc_enaddr
) &&
295 ether_cmp(eh
->ether_dhost
, bcast_enaddr
)) {
301 /* Pass the packet up. */
303 (*ifp
->if_input
)(ifp
, m
);
309 sc
->sc_last_rd
= bix
;
313 am7990_tint(struct lance_softc
*sc
)
315 struct ifnet
*ifp
= sc
->sc_ifp
;
319 bix
= sc
->sc_first_td
;
322 if (sc
->sc_no_td
<= 0)
325 (*sc
->sc_copyfromdesc
)(sc
, &tmd
, LE_TMDADDR(sc
, bix
),
329 if (sc
->sc_flags
& LE_DEBUG
)
330 if_printf(ifp
, "trans tmd: "
331 "ladr %04x, hadr %02x, flags %02x, "
332 "bcnt %04x, mcnt %04x\n",
333 tmd
.tmd0
, tmd
.tmd1_hadr
, tmd
.tmd1_bits
,
337 if (tmd
.tmd1_bits
& LE_T1_OWN
)
340 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
342 if (tmd
.tmd1_bits
& LE_T1_ERR
) {
343 if (tmd
.tmd3
& LE_T3_BUFF
)
344 if_printf(ifp
, "transmit buffer error\n");
345 else if (tmd
.tmd3
& LE_T3_UFLO
)
346 if_printf(ifp
, "underflow\n");
347 if (tmd
.tmd3
& (LE_T3_BUFF
| LE_T3_UFLO
)) {
348 lance_init_locked(sc
);
351 if (tmd
.tmd3
& LE_T3_LCAR
) {
352 if (sc
->sc_flags
& LE_CARRIER
)
353 if_link_state_change(ifp
,
355 sc
->sc_flags
&= ~LE_CARRIER
;
356 if (sc
->sc_nocarrier
)
357 (*sc
->sc_nocarrier
)(sc
);
359 if_printf(ifp
, "lost carrier\n");
361 if (tmd
.tmd3
& LE_T3_LCOL
)
362 ifp
->if_collisions
++;
363 if (tmd
.tmd3
& LE_T3_RTRY
) {
365 if_printf(ifp
, "excessive collisions, tdr %d\n",
366 tmd
.tmd3
& LE_T3_TDR_MASK
);
368 ifp
->if_collisions
+= 16;
372 if (tmd
.tmd1_bits
& LE_T1_ONE
)
373 ifp
->if_collisions
++;
374 else if (tmd
.tmd1_bits
& LE_T1_MORE
)
375 /* Real number is unknown. */
376 ifp
->if_collisions
+= 2;
380 if (++bix
== sc
->sc_ntbuf
)
386 sc
->sc_first_td
= bix
;
388 sc
->sc_wdog_timer
= sc
->sc_no_td
> 0 ? 5 : 0;
392 * Controller interrupt
395 am7990_intr(void *arg
)
397 struct lance_softc
*sc
= arg
;
398 struct ifnet
*ifp
= sc
->sc_ifp
;
403 if (sc
->sc_hwintr
&& (*sc
->sc_hwintr
)(sc
) == -1) {
405 lance_init_locked(sc
);
410 isr
= (*sc
->sc_rdcsr
)(sc
, LE_CSR0
);
411 #if defined(LEDEBUG) && LEDEBUG > 1
412 if (sc
->sc_flags
& LE_DEBUG
)
413 if_printf(ifp
, "%s: entering with isr=%04x\n", __func__
, isr
);
415 if ((isr
& LE_C0_INTR
) == 0) {
421 * Clear interrupt source flags and turn off interrupts. If we
422 * don't clear these flags before processing their sources we
423 * could completely miss some interrupt events as the NIC can
424 * change these flags while we're in this handler. We toggle
425 * the interrupt enable bit in order to keep receiving them
426 * (some chips work without this, some don't).
428 (*sc
->sc_wrcsr
)(sc
, LE_CSR0
, isr
& ~(LE_C0_INEA
| LE_C0_TDMD
|
429 LE_C0_STOP
| LE_C0_STRT
| LE_C0_INIT
));
431 if (isr
& LE_C0_ERR
) {
432 if (isr
& LE_C0_BABL
) {
434 if_printf(ifp
, "babble\n");
439 if (isr
& LE_C0_CERR
) {
440 if_printf(ifp
, "collision error\n");
441 ifp
->if_collisions
++;
444 if (isr
& LE_C0_MISS
) {
446 if_printf(ifp
, "missed packet\n");
450 if (isr
& LE_C0_MERR
) {
451 if_printf(ifp
, "memory error\n");
452 lance_init_locked(sc
);
458 if ((isr
& LE_C0_RXON
) == 0) {
459 if_printf(ifp
, "receiver disabled\n");
461 lance_init_locked(sc
);
465 if ((isr
& LE_C0_TXON
) == 0) {
466 if_printf(ifp
, "transmitter disabled\n");
468 lance_init_locked(sc
);
474 * Pretend we have carrier; if we don't this will be cleared shortly.
476 if (!(sc
->sc_flags
& LE_CARRIER
))
477 if_link_state_change(ifp
, LINK_STATE_UP
);
478 sc
->sc_flags
|= LE_CARRIER
;
480 if (isr
& LE_C0_RINT
)
482 if (isr
& LE_C0_TINT
)
485 /* Enable interrupts again. */
486 (*sc
->sc_wrcsr
)(sc
, LE_CSR0
, LE_C0_INEA
);
488 if (!IFQ_DRV_IS_EMPTY(&ifp
->if_snd
))
489 am7990_start_locked(sc
);
495 * Set up output on interface.
496 * Get another datagram to send off of the interface queue, and map it to the
497 * interface before starting the output.
500 am7990_start_locked(struct lance_softc
*sc
)
502 struct ifnet
*ifp
= sc
->sc_ifp
;
505 int bix
, enq
, len
, rp
;
507 LE_LOCK_ASSERT(sc
, MA_OWNED
);
509 if ((ifp
->if_drv_flags
& (IFF_DRV_RUNNING
| IFF_DRV_OACTIVE
)) !=
513 bix
= sc
->sc_last_td
;
516 for (; sc
->sc_no_td
< sc
->sc_ntbuf
&&
517 !IFQ_DRV_IS_EMPTY(&ifp
->if_snd
);) {
518 rp
= LE_TMDADDR(sc
, bix
);
519 (*sc
->sc_copyfromdesc
)(sc
, &tmd
, rp
, sizeof(tmd
));
521 if (tmd
.tmd1_bits
& LE_T1_OWN
) {
522 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
524 "missing buffer, no_td = %d, last_td = %d\n",
525 sc
->sc_no_td
, sc
->sc_last_td
);
528 IFQ_DRV_DEQUEUE(&ifp
->if_snd
, m
);
533 * If BPF is listening on this interface, let it see the packet
534 * before we commit it to the wire.
539 * Copy the mbuf chain into the transmit buffer.
541 len
= lance_put(sc
, LE_TBUFADDR(sc
, bix
), m
);
544 if (len
> ETHERMTU
+ ETHER_HDR_LEN
)
545 if_printf(ifp
, "packet length %d\n", len
);
549 * Init transmit registers, and set transmit start flag.
551 tmd
.tmd1_bits
= LE_T1_OWN
| LE_T1_STP
| LE_T1_ENP
;
552 tmd
.tmd2
= -len
| LE_XMD2_ONES
;
555 (*sc
->sc_copytodesc
)(sc
, &tmd
, rp
, sizeof(tmd
));
558 if (sc
->sc_flags
& LE_DEBUG
)
559 am7990_xmit_print(sc
, bix
);
562 (*sc
->sc_wrcsr
)(sc
, LE_CSR0
, LE_C0_INEA
| LE_C0_TDMD
);
565 if (++bix
== sc
->sc_ntbuf
)
568 if (++sc
->sc_no_td
== sc
->sc_ntbuf
) {
569 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
574 sc
->sc_last_td
= bix
;
577 sc
->sc_wdog_timer
= 5;
582 am7990_recv_print(struct lance_softc
*sc
, int no
)
584 struct ifnet
*ifp
= sc
->sc_ifp
;
585 struct ether_header eh
;
589 (*sc
->sc_copyfromdesc
)(sc
, &rmd
, LE_RMDADDR(sc
, no
), sizeof(rmd
));
591 if_printf(ifp
, "receive buffer %d, len = %d\n", no
, len
);
592 if_printf(ifp
, "status %04x\n", (*sc
->sc_rdcsr
)(sc
, LE_CSR0
));
594 "ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
595 rmd
.rmd0
, rmd
.rmd1_hadr
, rmd
.rmd1_bits
, rmd
.rmd2
, rmd
.rmd3
);
596 if (len
- ETHER_CRC_LEN
>= sizeof(eh
)) {
597 (*sc
->sc_copyfrombuf
)(sc
, &eh
, LE_RBUFADDR(sc
, no
), sizeof(eh
));
598 if_printf(ifp
, "dst %s", ether_sprintf(eh
.ether_dhost
));
599 printf(" src %s type %04x\n", ether_sprintf(eh
.ether_shost
),
600 ntohs(eh
.ether_type
));
605 am7990_xmit_print(struct lance_softc
*sc
, int no
)
607 struct ifnet
*ifp
= sc
->sc_ifp
;
608 struct ether_header eh
;
612 (*sc
->sc_copyfromdesc
)(sc
, &tmd
, LE_TMDADDR(sc
, no
), sizeof(tmd
));
614 if_printf(ifp
, "transmit buffer %d, len = %d\n", no
, len
);
615 if_printf(ifp
, "status %04x\n", (*sc
->sc_rdcsr
)(sc
, LE_CSR0
));
617 "ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
618 tmd
.tmd0
, tmd
.tmd1_hadr
, tmd
.tmd1_bits
, tmd
.tmd2
, tmd
.tmd3
);
619 if (len
>= sizeof(eh
)) {
620 (*sc
->sc_copyfrombuf
)(sc
, &eh
, LE_TBUFADDR(sc
, no
), sizeof(eh
));
621 if_printf(ifp
, "dst %s", ether_sprintf(eh
.ether_dhost
));
622 printf(" src %s type %04x\n", ether_sprintf(eh
.ether_shost
),
623 ntohs(eh
.ether_type
));