1 /* $NetBSD: if_mc.c,v 1.35 2008/11/07 00:20:01 dyoung Exp $ */
4 * Copyright (c) 1997 David Huang <khym@azeotrope.org>
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: if_mc.c,v 1.35 2008/11/07 00:20:01 dyoung Exp $");
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/syslog.h>
50 #include <sys/ioctl.h>
51 #include <sys/errno.h>
52 #include <sys/device.h>
54 #include <uvm/uvm_extern.h>
57 #include <net/if_dl.h>
58 #include <net/if_ether.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>
73 #include <net/bpfdesc.h>
76 #include <machine/bus.h>
77 #include <mac68k/dev/if_mcreg.h>
78 #include <mac68k/dev/if_mcvar.h>
80 hide
void mcwatchdog(struct ifnet
*);
81 hide
int mcinit(struct mc_softc
*);
82 hide
int mcstop(struct mc_softc
*);
83 hide
int mcioctl(struct ifnet
*, u_long
, void *);
84 hide
void mcstart(struct ifnet
*);
85 hide
void mcreset(struct mc_softc
*);
87 integrate u_int
maceput(struct mc_softc
*, struct mbuf
*);
88 integrate
void mc_tint(struct mc_softc
*);
89 integrate
void mace_read(struct mc_softc
*, void *, int);
90 integrate
struct mbuf
*mace_get(struct mc_softc
*, void *, int);
91 static void mace_calcladrf(struct ethercom
*, u_int8_t
*);
92 static inline u_int16_t
ether_cmp(void *, void *);
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 u_int16_t
*a
= (u_short
*) one
;
113 u_int16_t
*b
= (u_short
*) two
;
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
;
166 ether_ifattach(ifp
, lladdr
);
172 mcioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
174 struct mc_softc
*sc
= ifp
->if_softc
;
177 int s
= splnet(), err
= 0;
182 ifa
= (struct ifaddr
*)data
;
183 ifp
->if_flags
|= IFF_UP
;
185 switch (ifa
->ifa_addr
->sa_family
) {
188 arp_ifinit(ifp
, ifa
);
197 if ((err
= ifioctl_common(ifp
, cmd
, data
)) != 0)
199 /* XXX see the comment in ed_ioctl() about code re-use */
200 if ((ifp
->if_flags
& IFF_UP
) == 0 &&
201 (ifp
->if_flags
& IFF_RUNNING
) != 0) {
203 * If interface is marked down and it is running,
207 ifp
->if_flags
&= ~IFF_RUNNING
;
208 } else if ((ifp
->if_flags
& IFF_UP
) != 0 &&
209 (ifp
->if_flags
& IFF_RUNNING
) == 0) {
211 * If interface is marked up and it is stopped,
217 * reset the interface to pick up any other changes
227 if ((err
= ether_ioctl(ifp
, cmd
, data
)) == ENETRESET
) {
229 * Multicast list has changed; set the hardware
230 * filter accordingly. But remember UP flag!
232 if (ifp
->if_flags
& IFF_RUNNING
)
238 err
= ether_ioctl(ifp
, cmd
, data
);
245 * Encapsulate a packet of type family for the local net.
248 mcstart(struct ifnet
*ifp
)
250 struct mc_softc
*sc
= ifp
->if_softc
;
253 if ((ifp
->if_flags
& (IFF_RUNNING
| IFF_OACTIVE
)) != IFF_RUNNING
)
257 if (ifp
->if_flags
& IFF_OACTIVE
)
260 IF_DEQUEUE(&ifp
->if_snd
, m
);
266 * If bpf is listening on this interface, let it
267 * see the packet before we commit it to the wire.
270 bpf_mtap(ifp
->if_bpf
, m
);
274 * Copy the mbuf chain into the transmit buffer.
276 ifp
->if_flags
|= IFF_OACTIVE
;
279 ifp
->if_opackets
++; /* # of pkts */
284 * reset and restart the MACE. Called in case of fatal
285 * hardware/software errors.
288 mcreset(struct mc_softc
*sc
)
295 mcinit(struct mc_softc
*sc
)
298 u_int8_t maccc
, ladrf
[8];
300 if (sc
->sc_if
.if_flags
& IFF_RUNNING
)
301 /* already running */
306 NIC_PUT(sc
, MACE_BIUCC
, sc
->sc_biucc
);
307 NIC_PUT(sc
, MACE_FIFOCC
, sc
->sc_fifocc
);
308 NIC_PUT(sc
, MACE_IMR
, ~0); /* disable all interrupts */
309 NIC_PUT(sc
, MACE_PLSCC
, sc
->sc_plscc
);
311 NIC_PUT(sc
, MACE_UTR
, RTRD
); /* disable reserved test registers */
313 /* set MAC address */
314 NIC_PUT(sc
, MACE_IAC
, ADDRCHG
);
315 while (NIC_GET(sc
, MACE_IAC
) & ADDRCHG
)
317 NIC_PUT(sc
, MACE_IAC
, PHYADDR
);
318 bus_space_write_multi_1(sc
->sc_regt
, sc
->sc_regh
, MACE_REG(MACE_PADR
),
319 sc
->sc_enaddr
, ETHER_ADDR_LEN
);
321 /* set logical address filter */
322 mace_calcladrf(&sc
->sc_ethercom
, ladrf
);
324 NIC_PUT(sc
, MACE_IAC
, ADDRCHG
);
325 while (NIC_GET(sc
, MACE_IAC
) & ADDRCHG
)
327 NIC_PUT(sc
, MACE_IAC
, LOGADDR
);
328 bus_space_write_multi_1(sc
->sc_regt
, sc
->sc_regh
, MACE_REG(MACE_LADRF
),
331 NIC_PUT(sc
, MACE_XMTFC
, APADXMT
);
333 * No need to autostrip padding on receive... Ethernet frames
334 * don't have a length field, unlike 802.3 frames, so the MACE
335 * can't figure out the length of the packet anyways.
337 NIC_PUT(sc
, MACE_RCVFC
, 0);
339 maccc
= ENXMT
| ENRCV
;
340 if (sc
->sc_if
.if_flags
& IFF_PROMISC
)
343 NIC_PUT(sc
, MACE_MACCC
, maccc
);
346 (*sc
->sc_bus_init
)(sc
);
349 * Enable all interrupts except receive, since we use the DMA
350 * completion interrupt for that.
352 NIC_PUT(sc
, MACE_IMR
, RCVINTM
);
354 /* flag interface as "running" */
355 sc
->sc_if
.if_flags
|= IFF_RUNNING
;
356 sc
->sc_if
.if_flags
&= ~IFF_OACTIVE
;
363 * close down an interface and free its buffers
364 * Called on final close of device, or if mcinit() fails
368 mcstop(struct mc_softc
*sc
)
374 NIC_PUT(sc
, MACE_BIUCC
, SWRST
);
377 sc
->sc_if
.if_timer
= 0;
378 sc
->sc_if
.if_flags
&= ~IFF_RUNNING
;
385 * Called if any Tx packets remain unsent after 5 seconds,
386 * In all cases we just reset the chip, and any retransmission
387 * will be handled by higher level protocol timeouts.
390 mcwatchdog(struct ifnet
*ifp
)
392 struct mc_softc
*sc
= ifp
->if_softc
;
394 printf("mcwatchdog: resetting chip\n");
399 * stuff packet into MACE (at splnet)
402 maceput(struct mc_softc
*sc
, struct mbuf
*m
)
405 u_int len
, totlen
= 0;
408 buff
= (u_char
*)sc
->sc_txbuf
+ (sc
->sc_txset
== 0 ? 0 : 0x800);
411 u_char
*data
= mtod(m
, u_char
*);
414 memcpy(buff
, data
, len
);
419 if (totlen
> PAGE_SIZE
)
420 panic("%s: maceput: packet overflow", sc
->sc_dev
.dv_xname
);
423 if (totlen
< ETHERMIN
+ sizeof(struct ether_header
)) {
424 int pad
= ETHERMIN
+ sizeof(struct ether_header
) - totlen
;
425 memset(sc
->sc_txbuf
+ totlen
, 0, pad
);
426 totlen
= ETHERMIN
+ sizeof(struct ether_header
);
430 (*sc
->sc_putpacket
)(sc
, totlen
);
432 sc
->sc_if
.if_timer
= 5; /* 5 seconds to watch for failing to transmit */
439 struct mc_softc
*sc
= arg
;
442 ir
= NIC_GET(sc
, MACE_IR
) & ~NIC_GET(sc
, MACE_IMR
);
445 printf("%s: jabber error\n", sc
->sc_dev
.dv_xname
);
447 sc
->sc_if
.if_oerrors
++;
452 printf("%s: babble\n", sc
->sc_dev
.dv_xname
);
454 sc
->sc_if
.if_oerrors
++;
459 printf("%s: collision error\n", sc
->sc_dev
.dv_xname
);
461 sc
->sc_if
.if_collisions
++;
465 * Pretend we have carrier; if we don't this will be cleared
468 sc
->sc_havecarrier
= 1;
478 mc_tint(struct mc_softc
*sc
)
480 u_int8_t xmtrc
, xmtfs
;
482 xmtrc
= NIC_GET(sc
, MACE_XMTRC
);
483 xmtfs
= NIC_GET(sc
, MACE_XMTFS
);
485 if ((xmtfs
& XMTSV
) == 0)
489 printf("%s: underflow\n", sc
->sc_dev
.dv_xname
);
495 printf("%s: late collision\n", sc
->sc_dev
.dv_xname
);
496 sc
->sc_if
.if_oerrors
++;
497 sc
->sc_if
.if_collisions
++;
501 /* Real number is unknown. */
502 sc
->sc_if
.if_collisions
+= 2;
503 else if (xmtfs
& ONE
)
504 sc
->sc_if
.if_collisions
++;
505 else if (xmtfs
& RTRY
) {
506 printf("%s: excessive collisions\n", sc
->sc_dev
.dv_xname
);
507 sc
->sc_if
.if_collisions
+= 16;
508 sc
->sc_if
.if_oerrors
++;
512 sc
->sc_havecarrier
= 0;
513 printf("%s: lost carrier\n", sc
->sc_dev
.dv_xname
);
514 sc
->sc_if
.if_oerrors
++;
517 sc
->sc_if
.if_flags
&= ~IFF_OACTIVE
;
518 sc
->sc_if
.if_timer
= 0;
523 mc_rint(struct mc_softc
*sc
)
525 #define rxf sc->sc_rxframe
528 len
= (rxf
.rx_rcvcnt
| ((rxf
.rx_rcvsts
& 0xf) << 8)) - 4;
531 if (rxf
.rx_rcvsts
& 0xf0)
532 printf("%s: rcvcnt %02x rcvsts %02x rntpc 0x%02x rcvcc 0x%02x\n",
533 sc
->sc_dev
.dv_xname
, rxf
.rx_rcvcnt
, rxf
.rx_rcvsts
,
534 rxf
.rx_rntpc
, rxf
.rx_rcvcc
);
537 if (rxf
.rx_rcvsts
& OFLO
) {
538 printf("%s: receive FIFO overflow\n", sc
->sc_dev
.dv_xname
);
539 sc
->sc_if
.if_ierrors
++;
543 if (rxf
.rx_rcvsts
& CLSN
)
544 sc
->sc_if
.if_collisions
++;
546 if (rxf
.rx_rcvsts
& FRAM
) {
548 printf("%s: framing error\n", sc
->sc_dev
.dv_xname
);
550 sc
->sc_if
.if_ierrors
++;
554 if (rxf
.rx_rcvsts
& FCS
) {
556 printf("%s: frame control checksum error\n", sc
->sc_dev
.dv_xname
);
558 sc
->sc_if
.if_ierrors
++;
562 mace_read(sc
, rxf
.rx_frame
, len
);
567 mace_read(struct mc_softc
*sc
, void *pkt
, int len
)
569 struct ifnet
*ifp
= &sc
->sc_if
;
572 if (len
<= sizeof(struct ether_header
) ||
573 len
> ETHERMTU
+ sizeof(struct ether_header
)) {
575 printf("%s: invalid packet size %d; dropping\n",
576 sc
->sc_dev
.dv_xname
, len
);
582 m
= mace_get(sc
, pkt
, len
);
591 /* Pass the packet to any BPF listeners. */
593 bpf_mtap(ifp
->if_bpf
, m
);
596 /* Pass the packet up. */
597 (*ifp
->if_input
)(ifp
, m
);
601 * Pull data off an interface.
602 * Len is length of data, with local net header stripped.
603 * We copy the data into mbufs. When full cluster sized units are present
604 * we copy into clusters.
606 integrate
struct mbuf
*
607 mace_get(struct mc_softc
*sc
, void *pkt
, int totlen
)
610 struct mbuf
*top
, **mp
;
613 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
616 m
->m_pkthdr
.rcvif
= &sc
->sc_if
;
617 m
->m_pkthdr
.len
= totlen
;
624 MGET(m
, M_DONTWAIT
, MT_DATA
);
631 if (totlen
>= MINCLSIZE
) {
632 MCLGET(m
, M_DONTWAIT
);
633 if ((m
->m_flags
& M_EXT
) == 0) {
640 m
->m_len
= len
= min(totlen
, len
);
641 memcpy(mtod(m
, void *), pkt
, len
);
642 pkt
= (char*)pkt
+ len
;
652 * Go through the list of multicast addresses and calculate the logical
656 mace_calcladrf(struct ethercom
*ac
, u_int8_t
*af
)
658 struct ifnet
*ifp
= &ac
->ec_if
;
659 struct ether_multi
*enm
;
662 static const u_int32_t crctab
[] = {
663 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
664 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
665 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
666 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
669 struct ether_multistep step
;
672 * Set up multicast address filter by passing all multicast addresses
673 * through a crc generator, and then using the high order 6 bits as an
674 * index into the 64 bit logical address filter. The high order bit
675 * selects the word, while the rest of the bits select the bit within
679 *((u_int32_t
*)af
) = *((u_int32_t
*)af
+ 1) = 0;
680 ETHER_FIRST_MULTI(step
, ac
, enm
);
681 while (enm
!= NULL
) {
682 if (ETHER_CMP(enm
->enm_addrlo
, enm
->enm_addrhi
)) {
684 * We must listen to a range of multicast addresses.
685 * For now, just accept all multicasts, rather than
686 * trying to set only those filter bits needed to match
687 * the range. (At this time, the only use of address
688 * ranges is for IP multicast routing, for which the
689 * range is big enough to require all bits set.)
694 cp
= enm
->enm_addrlo
;
696 for (len
= sizeof(enm
->enm_addrlo
); --len
>= 0;) {
698 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
699 crc
= (crc
>> 4) ^ crctab
[crc
& 0xf];
701 /* Just want the 6 most significant bits. */
704 /* Set the corresponding bit in the filter. */
705 af
[crc
>> 3] |= 1 << (crc
& 7);
707 ETHER_NEXT_MULTI(step
, enm
);
709 ifp
->if_flags
&= ~IFF_ALLMULTI
;
713 ifp
->if_flags
|= IFF_ALLMULTI
;
714 *((u_int32_t
*)af
) = *((u_int32_t
*)af
+ 1) = 0xffffffff;
717 static u_char bbr4
[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
718 #define bbr(v) ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
721 mc_get_enaddr(bus_space_tag_t t
, bus_space_handle_t h
, bus_size_t o
,
728 * The XOR of the 8 bytes of the ROM must be 0xff for it to be
731 for (i
= 0, csum
= 0; i
< 8; i
++) {
732 b
= bus_space_read_1(t
, h
, o
+16*i
);
733 if (i
< ETHER_ADDR_LEN
)