Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / mac68k / dev / if_mc.c
blob63e821b325bece721db6c86df82d383f7cef45d4
1 /* $NetBSD: if_mc.c,v 1.35 2008/11/07 00:20:01 dyoung Exp $ */
3 /*-
4 * Copyright (c) 1997 David Huang <khym@azeotrope.org>
5 * All rights reserved.
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
9 * Jason R. Thorpe.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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 $");
40 #include "opt_ddb.h"
41 #include "opt_inet.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/buf.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>
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_ether.h>
60 #ifdef INET
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>
66 #endif
70 #include "bpfilter.h"
71 #if NBPFILTER > 0
72 #include <net/bpf.h>
73 #include <net/bpfdesc.h>
74 #endif
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;
114 u_int16_t diff;
116 #ifdef m68k
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!
122 diff = *a++ - *b++;
123 diff |= *a++ - *b++;
124 diff |= *a++ - *b++;
125 #else
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]);
132 #endif
134 return (diff);
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
142 * to accept packets.
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);
151 DELAY(100);
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);
158 ifp->if_softc = sc;
159 ifp->if_ioctl = mcioctl;
160 ifp->if_start = mcstart;
161 ifp->if_flags =
162 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
163 ifp->if_watchdog = mcwatchdog;
165 if_attach(ifp);
166 ether_ifattach(ifp, lladdr);
168 return (0);
171 hide int
172 mcioctl(struct ifnet *ifp, u_long cmd, void *data)
174 struct mc_softc *sc = ifp->if_softc;
175 struct ifaddr *ifa;
177 int s = splnet(), err = 0;
179 switch (cmd) {
181 case SIOCINITIFADDR:
182 ifa = (struct ifaddr *)data;
183 ifp->if_flags |= IFF_UP;
184 mcinit(sc);
185 switch (ifa->ifa_addr->sa_family) {
186 #ifdef INET
187 case AF_INET:
188 arp_ifinit(ifp, ifa);
189 break;
190 #endif
191 default:
192 break;
194 break;
196 case SIOCSIFFLAGS:
197 if ((err = ifioctl_common(ifp, cmd, data)) != 0)
198 break;
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,
204 * then stop it.
206 mcstop(sc);
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,
212 * then start it.
214 (void)mcinit(sc);
215 } else {
217 * reset the interface to pick up any other changes
218 * in flags
220 mcreset(sc);
221 mcstart(ifp);
223 break;
225 case SIOCADDMULTI:
226 case SIOCDELMULTI:
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)
233 mcreset(sc);
234 err = 0;
236 break;
237 default:
238 err = ether_ioctl(ifp, cmd, data);
240 splx(s);
241 return (err);
245 * Encapsulate a packet of type family for the local net.
247 hide void
248 mcstart(struct ifnet *ifp)
250 struct mc_softc *sc = ifp->if_softc;
251 struct mbuf *m;
253 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
254 return;
256 while (1) {
257 if (ifp->if_flags & IFF_OACTIVE)
258 return;
260 IF_DEQUEUE(&ifp->if_snd, m);
261 if (m == 0)
262 return;
264 #if NBPFILTER > 0
266 * If bpf is listening on this interface, let it
267 * see the packet before we commit it to the wire.
269 if (ifp->if_bpf)
270 bpf_mtap(ifp->if_bpf, m);
271 #endif
274 * Copy the mbuf chain into the transmit buffer.
276 ifp->if_flags |= IFF_OACTIVE;
277 maceput(sc, m);
279 ifp->if_opackets++; /* # of pkts */
284 * reset and restart the MACE. Called in case of fatal
285 * hardware/software errors.
287 hide void
288 mcreset(struct mc_softc *sc)
290 mcstop(sc);
291 mcinit(sc);
294 hide int
295 mcinit(struct mc_softc *sc)
297 int s;
298 u_int8_t maccc, ladrf[8];
300 if (sc->sc_if.if_flags & IFF_RUNNING)
301 /* already running */
302 return (0);
304 s = splnet();
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),
329 ladrf, 8);
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)
341 maccc |= PROM;
343 NIC_PUT(sc, MACE_MACCC, maccc);
345 if (sc->sc_bus_init)
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;
358 splx(s);
359 return (0);
363 * close down an interface and free its buffers
364 * Called on final close of device, or if mcinit() fails
365 * part way through.
367 hide int
368 mcstop(struct mc_softc *sc)
370 int s;
372 s = splnet();
374 NIC_PUT(sc, MACE_BIUCC, SWRST);
375 DELAY(100);
377 sc->sc_if.if_timer = 0;
378 sc->sc_if.if_flags &= ~IFF_RUNNING;
380 splx(s);
381 return (0);
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.
389 hide void
390 mcwatchdog(struct ifnet *ifp)
392 struct mc_softc *sc = ifp->if_softc;
394 printf("mcwatchdog: resetting chip\n");
395 mcreset(sc);
399 * stuff packet into MACE (at splnet)
401 integrate u_int
402 maceput(struct mc_softc *sc, struct mbuf *m)
404 struct mbuf *n;
405 u_int len, totlen = 0;
406 u_char *buff;
408 buff = (u_char*)sc->sc_txbuf + (sc->sc_txset == 0 ? 0 : 0x800);
410 for (; m; m = n) {
411 u_char *data = mtod(m, u_char *);
412 len = m->m_len;
413 totlen += len;
414 memcpy(buff, data, len);
415 buff += len;
416 MFREE(m, n);
419 if (totlen > PAGE_SIZE)
420 panic("%s: maceput: packet overflow", sc->sc_dev.dv_xname);
422 #if 0
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);
428 #endif
430 (*sc->sc_putpacket)(sc, totlen);
432 sc->sc_if.if_timer = 5; /* 5 seconds to watch for failing to transmit */
433 return (totlen);
436 void
437 mcintr(void *arg)
439 struct mc_softc *sc = arg;
440 u_int8_t ir;
442 ir = NIC_GET(sc, MACE_IR) & ~NIC_GET(sc, MACE_IMR);
443 if (ir & JAB) {
444 #ifdef MCDEBUG
445 printf("%s: jabber error\n", sc->sc_dev.dv_xname);
446 #endif
447 sc->sc_if.if_oerrors++;
450 if (ir & BABL) {
451 #ifdef MCDEBUG
452 printf("%s: babble\n", sc->sc_dev.dv_xname);
453 #endif
454 sc->sc_if.if_oerrors++;
457 if (ir & CERR) {
458 #ifdef MCDEBUG
459 printf("%s: collision error\n", sc->sc_dev.dv_xname);
460 #endif
461 sc->sc_if.if_collisions++;
465 * Pretend we have carrier; if we don't this will be cleared
466 * shortly.
468 sc->sc_havecarrier = 1;
470 if (ir & XMTINT)
471 mc_tint(sc);
473 if (ir & RCVINT)
474 mc_rint(sc);
477 integrate void
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)
486 return;
488 if (xmtfs & UFLO) {
489 printf("%s: underflow\n", sc->sc_dev.dv_xname);
490 mcreset(sc);
491 return;
494 if (xmtfs & LCOL) {
495 printf("%s: late collision\n", sc->sc_dev.dv_xname);
496 sc->sc_if.if_oerrors++;
497 sc->sc_if.if_collisions++;
500 if (xmtfs & MORE)
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++;
511 if (xmtfs & LCAR) {
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;
519 mcstart(&sc->sc_if);
522 void
523 mc_rint(struct mc_softc *sc)
525 #define rxf sc->sc_rxframe
526 u_int len;
528 len = (rxf.rx_rcvcnt | ((rxf.rx_rcvsts & 0xf) << 8)) - 4;
530 #ifdef MCDEBUG
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);
535 #endif
537 if (rxf.rx_rcvsts & OFLO) {
538 printf("%s: receive FIFO overflow\n", sc->sc_dev.dv_xname);
539 sc->sc_if.if_ierrors++;
540 return;
543 if (rxf.rx_rcvsts & CLSN)
544 sc->sc_if.if_collisions++;
546 if (rxf.rx_rcvsts & FRAM) {
547 #ifdef MCDEBUG
548 printf("%s: framing error\n", sc->sc_dev.dv_xname);
549 #endif
550 sc->sc_if.if_ierrors++;
551 return;
554 if (rxf.rx_rcvsts & FCS) {
555 #ifdef MCDEBUG
556 printf("%s: frame control checksum error\n", sc->sc_dev.dv_xname);
557 #endif
558 sc->sc_if.if_ierrors++;
559 return;
562 mace_read(sc, rxf.rx_frame, len);
563 #undef rxf
566 integrate void
567 mace_read(struct mc_softc *sc, void *pkt, int len)
569 struct ifnet *ifp = &sc->sc_if;
570 struct mbuf *m;
572 if (len <= sizeof(struct ether_header) ||
573 len > ETHERMTU + sizeof(struct ether_header)) {
574 #ifdef MCDEBUG
575 printf("%s: invalid packet size %d; dropping\n",
576 sc->sc_dev.dv_xname, len);
577 #endif
578 ifp->if_ierrors++;
579 return;
582 m = mace_get(sc, pkt, len);
583 if (m == NULL) {
584 ifp->if_ierrors++;
585 return;
588 ifp->if_ipackets++;
590 #if NBPFILTER > 0
591 /* Pass the packet to any BPF listeners. */
592 if (ifp->if_bpf)
593 bpf_mtap(ifp->if_bpf, m);
594 #endif
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)
609 struct mbuf *m;
610 struct mbuf *top, **mp;
611 int len;
613 MGETHDR(m, M_DONTWAIT, MT_DATA);
614 if (m == 0)
615 return (0);
616 m->m_pkthdr.rcvif = &sc->sc_if;
617 m->m_pkthdr.len = totlen;
618 len = MHLEN;
619 top = 0;
620 mp = &top;
622 while (totlen > 0) {
623 if (top) {
624 MGET(m, M_DONTWAIT, MT_DATA);
625 if (m == 0) {
626 m_freem(top);
627 return 0;
629 len = MLEN;
631 if (totlen >= MINCLSIZE) {
632 MCLGET(m, M_DONTWAIT);
633 if ((m->m_flags & M_EXT) == 0) {
634 m_free(m);
635 m_freem(top);
636 return 0;
638 len = MCLBYTES;
640 m->m_len = len = min(totlen, len);
641 memcpy(mtod(m, void *), pkt, len);
642 pkt = (char*)pkt + len;
643 totlen -= len;
644 *mp = m;
645 mp = &m->m_next;
648 return (top);
652 * Go through the list of multicast addresses and calculate the logical
653 * address filter.
655 void
656 mace_calcladrf(struct ethercom *ac, u_int8_t *af)
658 struct ifnet *ifp = &ac->ec_if;
659 struct ether_multi *enm;
660 u_char *cp;
661 u_int32_t crc;
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
668 int len;
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
676 * the word.
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.)
691 goto allmulti;
694 cp = enm->enm_addrlo;
695 crc = 0xffffffff;
696 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
697 crc ^= *cp++;
698 crc = (crc >> 4) ^ crctab[crc & 0xf];
699 crc = (crc >> 4) ^ crctab[crc & 0xf];
701 /* Just want the 6 most significant bits. */
702 crc >>= 26;
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;
710 return;
712 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])
720 u_char
721 mc_get_enaddr(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
722 u_char *dst)
724 int i;
725 u_char b, csum;
728 * The XOR of the 8 bytes of the ROM must be 0xff for it to be
729 * valid
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)
734 dst[i] = bbr(b);
735 csum ^= b;
738 return csum;