1 /* $NetBSD: am79c950.c,v 1.26 2009/03/14 15:36:09 dsl Exp $ */
4 * Copyright (c) 1997 David Huang <khym@bga.com>
7 * Portions of this code are based on code by Denton Gentry <denny1@home.com>,
8 * Charles M. Hannum, Yanagisawa Takeshi <yanagisw@aa.ap.titech.ac.jp>, and
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. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Driver for the AMD Am79C940 (MACE) ethernet chip, used for onboard
34 * ethernet on the Centris/Quadra 660av and Quadra 840av.
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: am79c950.c,v 1.26 2009/03/14 15:36:09 dsl Exp $");
42 #include <sys/param.h>
43 #include <sys/systm.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/syslog.h>
49 #include <sys/ioctl.h>
50 #include <sys/errno.h>
51 #include <sys/device.h>
53 #include <uvm/uvm_extern.h>
56 #include <net/if_dl.h>
57 #include <net/if_ether.h>
58 #include <net/if_media.h>
61 #include <netinet/in.h>
62 #include <netinet/if_inarp.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip.h>
71 #include <net/bpfdesc.h>
74 #include <machine/bus.h>
76 #include <macppc/dev/am79c950reg.h>
77 #include <macppc/dev/if_mcvar.h>
79 hide
void mcwatchdog(struct ifnet
*);
80 hide
int mcinit(struct mc_softc
*sc
);
81 hide
int mcstop(struct mc_softc
*sc
);
82 hide
int mcioctl(struct ifnet
*ifp
, u_long cmd
, void *data
);
83 hide
void mcstart(struct ifnet
*ifp
);
84 hide
void mcreset(struct mc_softc
*sc
);
86 integrate u_int
maceput(struct mc_softc
*sc
, struct mbuf
*m0
);
87 integrate
void mc_tint(struct mc_softc
*sc
);
88 integrate
void mace_read(struct mc_softc
*, uint8_t *, int);
89 integrate
struct mbuf
*mace_get(struct mc_softc
*, uint8_t *, int);
90 static void mace_calcladrf(struct ethercom
*ac
, u_int8_t
*af
);
91 static inline u_int16_t
ether_cmp(void *, void *);
92 static int mc_mediachange(struct ifnet
*);
93 static void mc_mediastatus(struct ifnet
*, struct ifmediareq
*);
96 * Compare two Ether/802 addresses for equality, inlined and
97 * unrolled for speed. Use this like memcmp().
99 * XXX: Add <machine/inlines.h> for stuff like this?
100 * XXX: or maybe add it to libkern.h instead?
102 * "I'd love to have an inline assembler version of this."
103 * XXX: Who wanted that? mycroft? I wrote one, but this
104 * version in C is as good as hand-coded assembly. -gwr
106 * Please do NOT tweak this without looking at the actual
107 * assembly code generated before and after your tweaks!
109 static inline u_int16_t
110 ether_cmp(void *one
, void *two
)
112 register u_int16_t
*a
= (u_short
*) one
;
113 register u_int16_t
*b
= (u_short
*) two
;
114 register u_int16_t diff
;
118 * The post-increment-pointer form produces the best
119 * machine code for m68k. This was carefully tuned
120 * so it compiles to just 8 short (2-byte) op-codes!
127 * Most modern CPUs do better with a single expresion.
128 * Note that short-cut evaluation is NOT helpful here,
129 * because it just makes the code longer, not faster!
131 diff
= (a
[0] - b
[0]) | (a
[1] - b
[1]) | (a
[2] - b
[2]);
137 #define ETHER_CMP ether_cmp
140 * Interface exists: make available by filling in network interface
141 * record. System will initialize the interface when it is ready
145 mcsetup(struct mc_softc
*sc
, u_int8_t
*lladdr
)
147 struct ifnet
*ifp
= &sc
->sc_if
;
149 /* reset the chip and disable all interrupts */
150 NIC_PUT(sc
, MACE_BIUCC
, SWRST
);
152 NIC_PUT(sc
, MACE_IMR
, ~0);
154 memcpy(sc
->sc_enaddr
, lladdr
, ETHER_ADDR_LEN
);
155 printf(": address %s\n", ether_sprintf(lladdr
));
157 memcpy(ifp
->if_xname
, sc
->sc_dev
.dv_xname
, IFNAMSIZ
);
159 ifp
->if_ioctl
= mcioctl
;
160 ifp
->if_start
= mcstart
;
162 IFF_BROADCAST
| IFF_SIMPLEX
| IFF_NOTRAILERS
| IFF_MULTICAST
;
163 ifp
->if_watchdog
= mcwatchdog
;
165 /* initialize ifmedia structures */
166 ifmedia_init(&sc
->sc_media
, 0, mc_mediachange
, mc_mediastatus
);
167 ifmedia_add(&sc
->sc_media
, IFM_ETHER
|IFM_MANUAL
, 0, NULL
);
168 ifmedia_set(&sc
->sc_media
, IFM_ETHER
|IFM_MANUAL
);
171 ether_ifattach(ifp
, lladdr
);
177 mcioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
179 struct mc_softc
*sc
= ifp
->if_softc
;
183 int s
= splnet(), err
= 0;
188 ifa
= (struct ifaddr
*)data
;
189 ifp
->if_flags
|= IFF_UP
;
191 switch (ifa
->ifa_addr
->sa_family
) {
194 arp_ifinit(ifp
, ifa
);
203 if ((err
= ifioctl_common(ifp
, cmd
, data
)) != 0)
205 /* XXX see the comment in ed_ioctl() about code re-use */
206 if ((ifp
->if_flags
& IFF_UP
) == 0 &&
207 (ifp
->if_flags
& IFF_RUNNING
) != 0) {
209 * If interface is marked down and it is running,
213 ifp
->if_flags
&= ~IFF_RUNNING
;
214 } else if ((ifp
->if_flags
& IFF_UP
) != 0 &&
215 (ifp
->if_flags
& IFF_RUNNING
) == 0) {
217 * If interface is marked up and it is stopped,
223 * reset the interface to pick up any other changes
233 if ((err
= ether_ioctl(ifp
, cmd
, data
)) == ENETRESET
) {
235 * Multicast list has changed; set the hardware
236 * filter accordingly. But remember UP flag!
238 if (ifp
->if_flags
& IFF_RUNNING
)
246 ifr
= (struct ifreq
*) data
;
247 err
= ifmedia_ioctl(ifp
, ifr
, &sc
->sc_media
, cmd
);
251 err
= ether_ioctl(ifp
, cmd
, data
);
259 * Encapsulate a packet of type family for the local net.
262 mcstart(struct ifnet
*ifp
)
264 struct mc_softc
*sc
= ifp
->if_softc
;
267 if ((ifp
->if_flags
& (IFF_RUNNING
| IFF_OACTIVE
)) != IFF_RUNNING
)
271 if (ifp
->if_flags
& IFF_OACTIVE
)
274 IF_DEQUEUE(&ifp
->if_snd
, m
);
280 * If bpf is listening on this interface, let it
281 * see the packet before we commit it to the wire.
284 bpf_mtap(ifp
->if_bpf
, m
);
288 * Copy the mbuf chain into the transmit buffer.
290 ifp
->if_flags
|= IFF_OACTIVE
;
293 ifp
->if_opackets
++; /* # of pkts */
298 * reset and restart the MACE. Called in case of fatal
299 * hardware/software errors.
302 mcreset(struct mc_softc
*sc
)
309 mcinit(struct mc_softc
*sc
)
312 u_int8_t maccc
, ladrf
[8];
314 if (sc
->sc_if
.if_flags
& IFF_RUNNING
)
315 /* already running */
320 NIC_PUT(sc
, MACE_BIUCC
, sc
->sc_biucc
);
321 NIC_PUT(sc
, MACE_FIFOCC
, sc
->sc_fifocc
);
322 NIC_PUT(sc
, MACE_IMR
, ~0); /* disable all interrupts */
323 NIC_PUT(sc
, MACE_PLSCC
, sc
->sc_plscc
);
325 NIC_PUT(sc
, MACE_UTR
, RTRD
); /* disable reserved test registers */
327 /* set MAC address */
328 NIC_PUT(sc
, MACE_IAC
, ADDRCHG
);
329 while (NIC_GET(sc
, MACE_IAC
) & ADDRCHG
)
331 NIC_PUT(sc
, MACE_IAC
, PHYADDR
);
332 bus_space_write_multi_1(sc
->sc_regt
, sc
->sc_regh
, MACE_REG(MACE_PADR
),
333 sc
->sc_enaddr
, ETHER_ADDR_LEN
);
335 /* set logical address filter */
336 mace_calcladrf(&sc
->sc_ethercom
, ladrf
);
338 NIC_PUT(sc
, MACE_IAC
, ADDRCHG
);
339 while (NIC_GET(sc
, MACE_IAC
) & ADDRCHG
)
341 NIC_PUT(sc
, MACE_IAC
, LOGADDR
);
342 bus_space_write_multi_1(sc
->sc_regt
, sc
->sc_regh
, MACE_REG(MACE_LADRF
),
345 NIC_PUT(sc
, MACE_XMTFC
, APADXMT
);
347 * No need to autostrip padding on receive... Ethernet frames
348 * don't have a length field, unlike 802.3 frames, so the MACE
349 * can't figure out the length of the packet anyways.
351 NIC_PUT(sc
, MACE_RCVFC
, 0);
353 maccc
= ENXMT
| ENRCV
;
354 if (sc
->sc_if
.if_flags
& IFF_PROMISC
)
357 NIC_PUT(sc
, MACE_MACCC
, maccc
);
360 (*sc
->sc_bus_init
)(sc
);
363 * Enable all interrupts except receive, since we use the DMA
364 * completion interrupt for that.
366 NIC_PUT(sc
, MACE_IMR
, RCVINTM
);
368 /* flag interface as "running" */
369 sc
->sc_if
.if_flags
|= IFF_RUNNING
;
370 sc
->sc_if
.if_flags
&= ~IFF_OACTIVE
;
377 * close down an interface and free its buffers
378 * Called on final close of device, or if mcinit() fails
382 mcstop(struct mc_softc
*sc
)
386 NIC_PUT(sc
, MACE_BIUCC
, SWRST
);
389 sc
->sc_if
.if_timer
= 0;
390 sc
->sc_if
.if_flags
&= ~IFF_RUNNING
;
397 * Called if any Tx packets remain unsent after 5 seconds,
398 * In all cases we just reset the chip, and any retransmission
399 * will be handled by higher level protocol timeouts.
402 mcwatchdog(struct ifnet
*ifp
)
404 struct mc_softc
*sc
= ifp
->if_softc
;
406 printf("mcwatchdog: resetting chip\n");
411 * stuff packet into MACE (at splnet)
414 maceput(struct mc_softc
*sc
, struct mbuf
*m
)
417 u_int len
, totlen
= 0;
423 u_char
*data
= mtod(m
, u_char
*);
426 memcpy(buff
, data
, len
);
431 if (totlen
> PAGE_SIZE
)
432 panic("%s: maceput: packet overflow", sc
->sc_dev
.dv_xname
);
435 if (totlen
< ETHERMIN
+ sizeof(struct ether_header
)) {
436 int pad
= ETHERMIN
+ sizeof(struct ether_header
) - totlen
;
437 memset(sc
->sc_txbuf
+ totlen
, 0, pad
);
438 totlen
= ETHERMIN
+ sizeof(struct ether_header
);
442 (*sc
->sc_putpacket
)(sc
, totlen
);
444 sc
->sc_if
.if_timer
= 5; /* 5 seconds to watch for failing to transmit */
451 struct mc_softc
*sc
= arg
;
454 ir
= NIC_GET(sc
, MACE_IR
) & ~NIC_GET(sc
, MACE_IMR
);
460 printf("%s: jabber error\n", sc
->sc_dev
.dv_xname
);
462 sc
->sc_if
.if_oerrors
++;
467 printf("%s: babble\n", sc
->sc_dev
.dv_xname
);
469 sc
->sc_if
.if_oerrors
++;
473 printf("%s: collision error\n", sc
->sc_dev
.dv_xname
);
474 sc
->sc_if
.if_collisions
++;
478 * Pretend we have carrier; if we don't this will be cleared
481 sc
->sc_havecarrier
= 1;
493 mc_tint(struct mc_softc
*sc
)
495 u_int8_t xmtrc
, xmtfs
;
497 xmtrc
= NIC_GET(sc
, MACE_XMTRC
);
498 xmtfs
= NIC_GET(sc
, MACE_XMTFS
);
500 if ((xmtfs
& XMTSV
) == 0)
504 printf("%s: underflow\n", sc
->sc_dev
.dv_xname
);
510 printf("%s: late collision\n", sc
->sc_dev
.dv_xname
);
511 sc
->sc_if
.if_oerrors
++;
512 sc
->sc_if
.if_collisions
++;
516 /* Real number is unknown. */
517 sc
->sc_if
.if_collisions
+= 2;
518 else if (xmtfs
& ONE
)
519 sc
->sc_if
.if_collisions
++;
520 else if (xmtfs
& RTRY
) {
521 sc
->sc_if
.if_collisions
+= 16;
522 sc
->sc_if
.if_oerrors
++;
526 sc
->sc_havecarrier
= 0;
527 printf("%s: lost carrier\n", sc
->sc_dev
.dv_xname
);
528 sc
->sc_if
.if_oerrors
++;
531 sc
->sc_if
.if_flags
&= ~IFF_OACTIVE
;
532 sc
->sc_if
.if_timer
= 0;
537 mc_rint(struct mc_softc
*sc
)
539 #define rxf sc->sc_rxframe
542 len
= (rxf
.rx_rcvcnt
| ((rxf
.rx_rcvsts
& 0xf) << 8)) - 4;
545 if (rxf
.rx_rcvsts
& 0xf0)
546 printf("%s: rcvcnt %02x rcvsts %02x rntpc 0x%02x rcvcc 0x%02x\n",
547 sc
->sc_dev
.dv_xname
, rxf
.rx_rcvcnt
, rxf
.rx_rcvsts
,
548 rxf
.rx_rntpc
, rxf
.rx_rcvcc
);
551 if (rxf
.rx_rcvsts
& OFLO
) {
552 printf("%s: receive FIFO overflow\n", sc
->sc_dev
.dv_xname
);
553 sc
->sc_if
.if_ierrors
++;
557 if (rxf
.rx_rcvsts
& CLSN
)
558 sc
->sc_if
.if_collisions
++;
560 if (rxf
.rx_rcvsts
& FRAM
) {
562 printf("%s: framing error\n", sc
->sc_dev
.dv_xname
);
564 sc
->sc_if
.if_ierrors
++;
568 if (rxf
.rx_rcvsts
& FCS
) {
570 printf("%s: frame control checksum error\n", sc
->sc_dev
.dv_xname
);
572 sc
->sc_if
.if_ierrors
++;
576 mace_read(sc
, rxf
.rx_frame
, len
);
581 mace_read(struct mc_softc
*sc
, uint8_t *pkt
, int len
)
583 struct ifnet
*ifp
= &sc
->sc_if
;
586 if (len
<= sizeof(struct ether_header
) ||
587 len
> ETHERMTU
+ sizeof(struct ether_header
)) {
589 printf("%s: invalid packet size %d; dropping\n",
590 sc
->sc_dev
.dv_xname
, len
);
596 m
= mace_get(sc
, pkt
, len
);
605 /* Pass this up to any BPF listeners. */
607 bpf_mtap(ifp
->if_bpf
, m
);
610 /* Pass the packet up. */
611 (*ifp
->if_input
)(ifp
, m
);
615 * Pull data off an interface.
616 * Len is length of data, with local net header stripped.
617 * We copy the data into mbufs. When full cluster sized units are present
618 * we copy into clusters.
620 integrate
struct mbuf
*
621 mace_get(struct mc_softc
*sc
, uint8_t *pkt
, int totlen
)
623 register struct mbuf
*m
;
624 struct mbuf
*top
, **mp
;
627 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
630 m
->m_pkthdr
.rcvif
= &sc
->sc_if
;
631 m
->m_pkthdr
.len
= totlen
;
638 MGET(m
, M_DONTWAIT
, MT_DATA
);
645 if (totlen
>= MINCLSIZE
) {
646 MCLGET(m
, M_DONTWAIT
);
647 if ((m
->m_flags
& M_EXT
) == 0) {
654 m
->m_len
= len
= min(totlen
, len
);
655 memcpy(mtod(m
, void *), pkt
, len
);
666 * Go through the list of multicast addresses and calculate the logical
670 mace_calcladrf(struct ethercom
*ac
, u_int8_t
*af
)
672 struct ifnet
*ifp
= &ac
->ec_if
;
673 struct ether_multi
*enm
;
674 register u_char
*cp
, c
;
675 register u_int32_t crc
;
677 struct ether_multistep step
;
680 * Set up multicast address filter by passing all multicast addresses
681 * through a crc generator, and then using the high order 6 bits as an
682 * index into the 64 bit logical address filter. The high order bit
683 * selects the word, while the rest of the bits select the bit within
687 *((u_int32_t
*)af
) = *((u_int32_t
*)af
+ 1) = 0;
689 ETHER_FIRST_MULTI(step
, ac
, enm
);
690 while (enm
!= NULL
) {
691 if (ETHER_CMP(enm
->enm_addrlo
, enm
->enm_addrhi
)) {
693 * We must listen to a range of multicast addresses.
694 * For now, just accept all multicasts, rather than
695 * trying to set only those filter bits needed to match
696 * the range. (At this time, the only use of address
697 * ranges is for IP multicast routing, for which the
698 * range is big enough to require all bits set.)
703 cp
= enm
->enm_addrlo
;
705 for (len
= sizeof(enm
->enm_addrlo
); --len
>= 0;) {
707 for (i
= 8; --i
>= 0;) {
708 if ((crc
& 0x01) ^ (c
& 0x01)) {
716 /* Just want the 6 most significant bits. */
719 /* Set the corresponding bit in the filter. */
720 af
[crc
>> 3] |= 1 << (crc
& 7);
722 ETHER_NEXT_MULTI(step
, enm
);
724 ifp
->if_flags
&= ~IFF_ALLMULTI
;
728 ifp
->if_flags
|= IFF_ALLMULTI
;
729 *((u_int32_t
*)af
) = *((u_int32_t
*)af
+ 1) = 0xffffffff;
733 mc_mediachange(struct ifnet
*ifp
)
739 mc_mediastatus(struct ifnet
*ifp
, struct ifmediareq
*ifmr
)
741 struct mc_softc
*sc
= ifp
->if_softc
;
743 if ((ifp
->if_flags
& IFF_UP
) == 0)
746 if (sc
->sc_havecarrier
)
747 ifmr
->ifm_status
|= IFM_ACTIVE
;