Home | History | Annotate | Line # | Download | only in dev
am79c950.c revision 1.6
      1 /*	$NetBSD: am79c950.c,v 1.6 1998/09/03 14:06:06 tsubai Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 David Huang <khym (at) bga.com>
      5  * All rights reserved.
      6  *
      7  * Portions of this code are based on code by Denton Gentry <denny1 (at) home.com>,
      8  * Charles M. Hannum, Yanagisawa Takeshi <yanagisw (at) aa.ap.titech.ac.jp>, and
      9  * Jason R. Thorpe.
     10  *
     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
     18  *
     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.
     29  *
     30  */
     31 
     32 /*
     33  * Driver for the AMD Am79C940 (MACE) ethernet chip, used for onboard
     34  * ethernet on the Centris/Quadra 660av and Quadra 840av.
     35  */
     36 #include "opt_inet.h"
     37 #include "opt_ccitt.h"
     38 #include "opt_llc.h"
     39 #include "opt_ns.h"
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/mbuf.h>
     44 #include <sys/buf.h>
     45 #include <sys/protosw.h>
     46 #include <sys/socket.h>
     47 #include <sys/syslog.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/errno.h>
     50 #include <sys/device.h>
     51 
     52 #include <net/if.h>
     53 #include <net/if_dl.h>
     54 #include <net/if_ether.h>
     55 #include <net/if_media.h>
     56 
     57 #ifdef INET
     58 #include <netinet/in.h>
     59 #include <netinet/if_inarp.h>
     60 #include <netinet/in_systm.h>
     61 #include <netinet/in_var.h>
     62 #include <netinet/ip.h>
     63 #endif
     64 
     65 #ifdef NS
     66 #include <netns/ns.h>
     67 #include <netns/ns_if.h>
     68 #endif
     69 
     70 #if defined(CCITT) && defined(LLC)
     71 #include <sys/socketvar.h>
     72 #include <netccitt/x25.h>
     73 #include <netccitt/pk.h>
     74 #include <netccitt/pk_var.h>
     75 #include <netccitt/pk_extern.h>
     76 #endif
     77 
     78 #include <vm/vm.h>
     79 
     80 #include "bpfilter.h"
     81 #if NBPFILTER > 0
     82 #include <net/bpf.h>
     83 #include <net/bpfdesc.h>
     84 #endif
     85 
     86 #include <machine/pio.h>
     87 #include <machine/bus.h>
     88 
     89 #include <macppc/dev/am79c950reg.h>
     90 #include <macppc/dev/if_mcvar.h>
     91 
     92 hide void	mcwatchdog __P((struct ifnet *));
     93 hide int	mcinit __P((struct mc_softc *sc));
     94 hide int	mcstop __P((struct mc_softc *sc));
     95 hide int	mcioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
     96 hide void	mcstart __P((struct ifnet *ifp));
     97 hide void	mcreset __P((struct mc_softc *sc));
     98 
     99 integrate u_int	maceput __P((struct mc_softc *sc, struct mbuf *m0));
    100 integrate void	mc_tint __P((struct mc_softc *sc));
    101 integrate void	mace_read __P((struct mc_softc *, caddr_t, int));
    102 integrate struct mbuf *mace_get __P((struct mc_softc *, caddr_t, int));
    103 static void mace_calcladrf __P((struct ethercom *ac, u_int8_t *af));
    104 static inline u_int16_t ether_cmp __P((void *, void *));
    105 static int mc_mediachange __P((struct ifnet *));
    106 static void mc_mediastatus __P((struct ifnet *, struct ifmediareq *));
    107 
    108 /*
    109  * Compare two Ether/802 addresses for equality, inlined and
    110  * unrolled for speed.  Use this like bcmp().
    111  *
    112  * XXX: Add <machine/inlines.h> for stuff like this?
    113  * XXX: or maybe add it to libkern.h instead?
    114  *
    115  * "I'd love to have an inline assembler version of this."
    116  * XXX: Who wanted that? mycroft?  I wrote one, but this
    117  * version in C is as good as hand-coded assembly. -gwr
    118  *
    119  * Please do NOT tweak this without looking at the actual
    120  * assembly code generated before and after your tweaks!
    121  */
    122 static inline u_int16_t
    123 ether_cmp(one, two)
    124 	void *one, *two;
    125 {
    126 	register u_int16_t *a = (u_short *) one;
    127 	register u_int16_t *b = (u_short *) two;
    128 	register u_int16_t diff;
    129 
    130 #ifdef	m68k
    131 	/*
    132 	 * The post-increment-pointer form produces the best
    133 	 * machine code for m68k.  This was carefully tuned
    134 	 * so it compiles to just 8 short (2-byte) op-codes!
    135 	 */
    136 	diff  = *a++ - *b++;
    137 	diff |= *a++ - *b++;
    138 	diff |= *a++ - *b++;
    139 #else
    140 	/*
    141 	 * Most modern CPUs do better with a single expresion.
    142 	 * Note that short-cut evaluation is NOT helpful here,
    143 	 * because it just makes the code longer, not faster!
    144 	 */
    145 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
    146 #endif
    147 
    148 	return (diff);
    149 }
    150 
    151 #define ETHER_CMP	ether_cmp
    152 
    153 /*
    154  * Interface exists: make available by filling in network interface
    155  * record.  System will initialize the interface when it is ready
    156  * to accept packets.
    157  */
    158 int
    159 mcsetup(sc, lladdr)
    160 	struct mc_softc	*sc;
    161 	u_int8_t *lladdr;
    162 {
    163 	struct ifnet *ifp = &sc->sc_if;
    164 
    165 	/* reset the chip and disable all interrupts */
    166 	NIC_PUT(sc, MACE_BIUCC, SWRST);
    167 	DELAY(100);
    168 	NIC_PUT(sc, MACE_IMR, ~0);
    169 
    170 	bcopy(lladdr, sc->sc_enaddr, ETHER_ADDR_LEN);
    171 	printf(": address %s\n", ether_sprintf(lladdr));
    172 
    173 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    174 	ifp->if_softc = sc;
    175 	ifp->if_ioctl = mcioctl;
    176 	ifp->if_start = mcstart;
    177 	ifp->if_flags =
    178 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    179 	ifp->if_watchdog = mcwatchdog;
    180 
    181 #if NBPFILTER > 0
    182 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    183 #endif
    184 
    185 	/* initialize ifmedia structures */
    186 	ifmedia_init(&sc->sc_media, 0, mc_mediachange, mc_mediastatus);
    187 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    188 	ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    189 
    190 	if_attach(ifp);
    191 	ether_ifattach(ifp, lladdr);
    192 
    193 	return (0);
    194 }
    195 
    196 hide int
    197 mcioctl(ifp, cmd, data)
    198 	struct ifnet *ifp;
    199 	u_long cmd;
    200 	caddr_t data;
    201 {
    202 	struct mc_softc *sc = ifp->if_softc;
    203 	struct ifaddr *ifa;
    204 	struct ifreq *ifr;
    205 
    206 	int	s = splnet(), err = 0;
    207 	int	temp;
    208 
    209 	switch (cmd) {
    210 
    211 	case SIOCSIFADDR:
    212 		ifa = (struct ifaddr *)data;
    213 		ifp->if_flags |= IFF_UP;
    214 		switch (ifa->ifa_addr->sa_family) {
    215 #ifdef INET
    216 		case AF_INET:
    217 			mcinit(sc);
    218 			arp_ifinit(ifp, ifa);
    219 			break;
    220 #endif
    221 #ifdef NS
    222 		case AF_NS:
    223 		    {
    224 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    225 
    226 			if (ns_nullhost(*ina))
    227 				ina->x_host =
    228 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    229 			else {
    230 				bcopy(ina->x_host.c_host,
    231 				    LLADDR(ifp->if_sadl),
    232 				    sizeof(sc->sc_enaddr));
    233 			}
    234 			/* Set new address. */
    235 			mcinit(sc);
    236 			break;
    237 		    }
    238 #endif
    239 		default:
    240 			mcinit(sc);
    241 			break;
    242 		}
    243 		break;
    244 
    245 	case SIOCSIFFLAGS:
    246 		if ((ifp->if_flags & IFF_UP) == 0 &&
    247 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    248 			/*
    249 			 * If interface is marked down and it is running,
    250 			 * then stop it.
    251 			 */
    252 			mcstop(sc);
    253 			ifp->if_flags &= ~IFF_RUNNING;
    254 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    255 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    256 			/*
    257 			 * If interface is marked up and it is stopped,
    258 			 * then start it.
    259 			 */
    260 			(void)mcinit(sc);
    261 		} else {
    262 			/*
    263 			 * reset the interface to pick up any other changes
    264 			 * in flags
    265 			 */
    266 			temp = ifp->if_flags & IFF_UP;
    267 			mcreset(sc);
    268 			ifp->if_flags |= temp;
    269 			mcstart(ifp);
    270 		}
    271 		break;
    272 
    273 	case SIOCADDMULTI:
    274 	case SIOCDELMULTI:
    275 		ifr = (struct ifreq *) data;
    276 		err = (cmd == SIOCADDMULTI) ?
    277 		    ether_addmulti(ifr, &sc->sc_ethercom) :
    278 		    ether_delmulti(ifr, &sc->sc_ethercom);
    279 
    280 		if (err == ENETRESET) {
    281 			/*
    282 			 * Multicast list has changed; set the hardware
    283 			 * filter accordingly. But remember UP flag!
    284 			 */
    285 			temp = ifp->if_flags & IFF_UP;
    286 			mcreset(sc);
    287 			ifp->if_flags |= temp;
    288 			err = 0;
    289 		}
    290 		break;
    291 
    292 	case SIOCGIFMEDIA:
    293 	case SIOCSIFMEDIA:
    294 		ifr = (struct ifreq *) data;
    295 		err = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    296 		break;
    297 
    298 	default:
    299 		err = EINVAL;
    300 	}
    301 	splx(s);
    302 	return (err);
    303 }
    304 
    305 /*
    306  * Encapsulate a packet of type family for the local net.
    307  */
    308 hide void
    309 mcstart(ifp)
    310 	struct ifnet *ifp;
    311 {
    312 	struct mc_softc	*sc = ifp->if_softc;
    313 	struct mbuf	*m;
    314 
    315 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    316 		return;
    317 
    318 	while (1) {
    319 		if (ifp->if_flags & IFF_OACTIVE)
    320 			return;
    321 
    322 		IF_DEQUEUE(&ifp->if_snd, m);
    323 		if (m == 0)
    324 			return;
    325 
    326 #if NBPFILTER > 0
    327 		/*
    328 		 * If bpf is listening on this interface, let it
    329 		 * see the packet before we commit it to the wire.
    330 		 */
    331 		if (ifp->if_bpf)
    332 			bpf_mtap(ifp->if_bpf, m);
    333 #endif
    334 
    335 		/*
    336 		 * Copy the mbuf chain into the transmit buffer.
    337 		 */
    338 		ifp->if_flags |= IFF_OACTIVE;
    339 		maceput(sc, m);
    340 
    341 		ifp->if_opackets++;		/* # of pkts */
    342 	}
    343 }
    344 
    345 /*
    346  * reset and restart the MACE.  Called in case of fatal
    347  * hardware/software errors.
    348  */
    349 hide void
    350 mcreset(sc)
    351 	struct mc_softc *sc;
    352 {
    353 	mcstop(sc);
    354 	mcinit(sc);
    355 }
    356 
    357 hide int
    358 mcinit(sc)
    359 	struct mc_softc *sc;
    360 {
    361 	int s;
    362 	u_int8_t maccc, ladrf[8];
    363 
    364 	if (sc->sc_if.if_flags & IFF_RUNNING)
    365 		/* already running */
    366 		return (0);
    367 
    368 	s = splnet();
    369 
    370 	NIC_PUT(sc, MACE_BIUCC, sc->sc_biucc);
    371 	NIC_PUT(sc, MACE_FIFOCC, sc->sc_fifocc);
    372 	NIC_PUT(sc, MACE_IMR, ~0); /* disable all interrupts */
    373 	NIC_PUT(sc, MACE_PLSCC, sc->sc_plscc);
    374 
    375 	NIC_PUT(sc, MACE_UTR, RTRD); /* disable reserved test registers */
    376 
    377 	/* set MAC address */
    378 	NIC_PUT(sc, MACE_IAC, ADDRCHG);
    379 	while (NIC_GET(sc, MACE_IAC) & ADDRCHG)
    380 		;
    381 	NIC_PUT(sc, MACE_IAC, PHYADDR);
    382 	bus_space_write_multi_1(sc->sc_regt, sc->sc_regh, MACE_REG(MACE_PADR),
    383 	    sc->sc_enaddr, ETHER_ADDR_LEN);
    384 
    385 	/* set logical address filter */
    386 	mace_calcladrf(&sc->sc_ethercom, ladrf);
    387 
    388 	NIC_PUT(sc, MACE_IAC, ADDRCHG);
    389 	while (NIC_GET(sc, MACE_IAC) & ADDRCHG)
    390 		;
    391 	NIC_PUT(sc, MACE_IAC, LOGADDR);
    392 	bus_space_write_multi_1(sc->sc_regt, sc->sc_regh, MACE_REG(MACE_LADRF),
    393 	    ladrf, 8);
    394 
    395 	NIC_PUT(sc, MACE_XMTFC, APADXMT);
    396 	/*
    397 	 * No need to autostrip padding on receive... Ethernet frames
    398 	 * don't have a length field, unlike 802.3 frames, so the MACE
    399 	 * can't figure out the length of the packet anyways.
    400 	 */
    401 	NIC_PUT(sc, MACE_RCVFC, 0);
    402 
    403 	maccc = ENXMT | ENRCV;
    404 	if (sc->sc_if.if_flags & IFF_PROMISC)
    405 		maccc |= PROM;
    406 
    407 	NIC_PUT(sc, MACE_MACCC, maccc);
    408 
    409 	if (sc->sc_bus_init)
    410 		(*sc->sc_bus_init)(sc);
    411 
    412 	/*
    413 	 * Enable all interrupts except receive, since we use the DMA
    414 	 * completion interrupt for that.
    415 	 */
    416 	NIC_PUT(sc, MACE_IMR, RCVINTM);
    417 
    418 	/* flag interface as "running" */
    419 	sc->sc_if.if_flags |= IFF_RUNNING;
    420 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    421 
    422 	splx(s);
    423 	return (0);
    424 }
    425 
    426 /*
    427  * close down an interface and free its buffers
    428  * Called on final close of device, or if mcinit() fails
    429  * part way through.
    430  */
    431 hide int
    432 mcstop(sc)
    433 	struct mc_softc *sc;
    434 {
    435 	int	s = splnet();
    436 
    437 	NIC_PUT(sc, MACE_BIUCC, SWRST);
    438 	DELAY(100);
    439 
    440 	sc->sc_if.if_timer = 0;
    441 	sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_UP);
    442 
    443 	splx(s);
    444 	return (0);
    445 }
    446 
    447 /*
    448  * Called if any Tx packets remain unsent after 5 seconds,
    449  * In all cases we just reset the chip, and any retransmission
    450  * will be handled by higher level protocol timeouts.
    451  */
    452 hide void
    453 mcwatchdog(ifp)
    454 	struct ifnet *ifp;
    455 {
    456 	struct mc_softc *sc = ifp->if_softc;
    457 	int temp;
    458 
    459 	printf("mcwatchdog: resetting chip\n");
    460 	temp = ifp->if_flags & IFF_UP;
    461 	mcreset(sc);
    462 	ifp->if_flags |= temp;
    463 }
    464 
    465 /*
    466  * stuff packet into MACE (at splnet)
    467  */
    468 integrate u_int
    469 maceput(sc, m)
    470 	struct mc_softc *sc;
    471 	struct mbuf *m;
    472 {
    473 	struct mbuf *n;
    474 	u_int len, totlen = 0;
    475 	u_char *buff;
    476 
    477 	buff = sc->sc_txbuf;
    478 
    479 	for (; m; m = n) {
    480 		u_char *data = mtod(m, u_char *);
    481 		len = m->m_len;
    482 		totlen += len;
    483 		bcopy(data, buff, len);
    484 		buff += len;
    485 		MFREE(m, n);
    486 	}
    487 
    488 	if (totlen > NBPG)
    489 		panic("%s: maceput: packet overflow", sc->sc_dev.dv_xname);
    490 
    491 #if 0
    492 	if (totlen < ETHERMIN + sizeof(struct ether_header)) {
    493 		int pad = ETHERMIN + sizeof(struct ether_header) - totlen;
    494 		bzero(sc->sc_txbuf + totlen, pad);
    495 		totlen = ETHERMIN + sizeof(struct ether_header);
    496 	}
    497 #endif
    498 
    499 	(*sc->sc_putpacket)(sc, totlen);
    500 
    501 	sc->sc_if.if_timer = 5;	/* 5 seconds to watch for failing to transmit */
    502 	return (totlen);
    503 }
    504 
    505 int
    506 mcintr(arg)
    507 	void *arg;
    508 {
    509 	struct mc_softc *sc = arg;
    510 	u_int8_t ir;
    511 
    512 	ir = NIC_GET(sc, MACE_IR) & ~NIC_GET(sc, MACE_IMR);
    513 	if (ir == 0)
    514 		return 0;
    515 
    516 	if (ir & JAB) {
    517 #ifdef MCDEBUG
    518 		printf("%s: jabber error\n", sc->sc_dev.dv_xname);
    519 #endif
    520 		sc->sc_if.if_oerrors++;
    521 	}
    522 
    523 	if (ir & BABL) {
    524 #ifdef MCDEBUG
    525 		printf("%s: babble\n", sc->sc_dev.dv_xname);
    526 #endif
    527 		sc->sc_if.if_oerrors++;
    528 	}
    529 
    530 	if (ir & CERR) {
    531 		printf("%s: collision error\n", sc->sc_dev.dv_xname);
    532 		sc->sc_if.if_collisions++;
    533 	}
    534 
    535 	/*
    536 	 * Pretend we have carrier; if we don't this will be cleared
    537 	 * shortly.
    538 	 */
    539 	sc->sc_havecarrier = 1;
    540 
    541 	if (ir & XMTINT)
    542 		mc_tint(sc);
    543 
    544 	if (ir & RCVINT)
    545 		mc_rint(sc);
    546 
    547 	return 1;
    548 }
    549 
    550 integrate void
    551 mc_tint(sc)
    552 	struct mc_softc *sc;
    553 {
    554 	u_int8_t xmtrc, xmtfs;
    555 
    556 	xmtrc = NIC_GET(sc, MACE_XMTRC);
    557 	xmtfs = NIC_GET(sc, MACE_XMTFS);
    558 
    559 	if ((xmtfs & XMTSV) == 0)
    560 		return;
    561 
    562 	if (xmtfs & UFLO) {
    563 		printf("%s: underflow\n", sc->sc_dev.dv_xname);
    564 		mcreset(sc);
    565 		return;
    566 	}
    567 
    568 	if (xmtfs & LCOL) {
    569 		printf("%s: late collision\n", sc->sc_dev.dv_xname);
    570 		sc->sc_if.if_oerrors++;
    571 		sc->sc_if.if_collisions++;
    572 	}
    573 
    574 	if (xmtfs & MORE)
    575 		/* Real number is unknown. */
    576 		sc->sc_if.if_collisions += 2;
    577 	else if (xmtfs & ONE)
    578 		sc->sc_if.if_collisions++;
    579 	else if (xmtfs & RTRY) {
    580 		sc->sc_if.if_collisions += 16;
    581 		sc->sc_if.if_oerrors++;
    582 	}
    583 
    584 	if (xmtfs & LCAR) {
    585 		sc->sc_havecarrier = 0;
    586 		printf("%s: lost carrier\n", sc->sc_dev.dv_xname);
    587 		sc->sc_if.if_oerrors++;
    588 	}
    589 
    590 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    591 	sc->sc_if.if_timer = 0;
    592 	mcstart(&sc->sc_if);
    593 }
    594 
    595 void
    596 mc_rint(sc)
    597 	struct mc_softc *sc;
    598 {
    599 #define	rxf	sc->sc_rxframe
    600 	u_int len;
    601 
    602 	len = (rxf.rx_rcvcnt | ((rxf.rx_rcvsts & 0xf) << 8)) - 4;
    603 
    604 #ifdef MCDEBUG
    605 	if (rxf.rx_rcvsts & 0xf0)
    606 		printf("%s: rcvcnt %02x rcvsts %02x rntpc 0x%02x rcvcc 0x%02x\n",
    607 		    sc->sc_dev.dv_xname, rxf.rx_rcvcnt, rxf.rx_rcvsts,
    608 		    rxf.rx_rntpc, rxf.rx_rcvcc);
    609 #endif
    610 
    611 	if (rxf.rx_rcvsts & OFLO) {
    612 		printf("%s: receive FIFO overflow\n", sc->sc_dev.dv_xname);
    613 		sc->sc_if.if_ierrors++;
    614 		return;
    615 	}
    616 
    617 	if (rxf.rx_rcvsts & CLSN)
    618 		sc->sc_if.if_collisions++;
    619 
    620 	if (rxf.rx_rcvsts & FRAM) {
    621 #ifdef MCDEBUG
    622 		printf("%s: framing error\n", sc->sc_dev.dv_xname);
    623 #endif
    624 		sc->sc_if.if_ierrors++;
    625 		return;
    626 	}
    627 
    628 	if (rxf.rx_rcvsts & FCS) {
    629 #ifdef MCDEBUG
    630 		printf("%s: frame control checksum error\n", sc->sc_dev.dv_xname);
    631 #endif
    632 		sc->sc_if.if_ierrors++;
    633 		return;
    634 	}
    635 
    636 	mace_read(sc, rxf.rx_frame, len);
    637 #undef	rxf
    638 }
    639 
    640 integrate void
    641 mace_read(sc, pkt, len)
    642 	struct mc_softc *sc;
    643 	caddr_t pkt;
    644 	int len;
    645 {
    646 	struct ifnet *ifp = &sc->sc_if;
    647 	struct ether_header *eh = (struct ether_header *)pkt;
    648 	struct mbuf *m;
    649 
    650 	if (len <= sizeof(struct ether_header) ||
    651 	    len > ETHERMTU + sizeof(struct ether_header)) {
    652 #ifdef MCDEBUG
    653 		printf("%s: invalid packet size %d; dropping\n",
    654 		    sc->sc_dev.dv_xname, len);
    655 #endif
    656 		ifp->if_ierrors++;
    657 		return;
    658 	}
    659 
    660 #if NBPFILTER > 0
    661 	/*
    662 	 * Check if there's a bpf filter listening on this interface.
    663 	 * If so, hand off the raw packet to enet, then discard things
    664 	 * not destined for us (but be sure to keep broadcast/multicast).
    665 	 */
    666 	if (ifp->if_bpf) {
    667 		bpf_tap(ifp->if_bpf, pkt, len);
    668 		if ((ifp->if_flags & IFF_PROMISC) != 0 &&
    669 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    670 		    ETHER_CMP(eh->ether_dhost, sc->sc_enaddr))
    671 			return;
    672 	}
    673 #endif
    674 	m = mace_get(sc, pkt, len);
    675 	if (m == NULL) {
    676 		ifp->if_ierrors++;
    677 		return;
    678 	}
    679 
    680 	ifp->if_ipackets++;
    681 
    682 	/* Pass the packet up, with the ether header sort-of removed. */
    683 	m_adj(m, sizeof(struct ether_header));
    684 	ether_input(ifp, eh, m);
    685 }
    686 
    687 /*
    688  * Pull data off an interface.
    689  * Len is length of data, with local net header stripped.
    690  * We copy the data into mbufs.  When full cluster sized units are present
    691  * we copy into clusters.
    692  */
    693 integrate struct mbuf *
    694 mace_get(sc, pkt, totlen)
    695 	struct mc_softc *sc;
    696 	caddr_t pkt;
    697 	int totlen;
    698 {
    699 	register struct mbuf *m;
    700 	struct mbuf *top, **mp;
    701 	int len;
    702 
    703 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    704 	if (m == 0)
    705 		return (0);
    706 	m->m_pkthdr.rcvif = &sc->sc_if;
    707 	m->m_pkthdr.len = totlen;
    708 	len = MHLEN;
    709 	top = 0;
    710 	mp = &top;
    711 
    712 	while (totlen > 0) {
    713 		if (top) {
    714 			MGET(m, M_DONTWAIT, MT_DATA);
    715 			if (m == 0) {
    716 				m_freem(top);
    717 				return 0;
    718 			}
    719 			len = MLEN;
    720 		}
    721 		if (totlen >= MINCLSIZE) {
    722 			MCLGET(m, M_DONTWAIT);
    723 			if ((m->m_flags & M_EXT) == 0) {
    724 				m_free(m);
    725 				m_freem(top);
    726 				return 0;
    727 			}
    728 			len = MCLBYTES;
    729 		}
    730 		m->m_len = len = min(totlen, len);
    731 		bcopy(pkt, mtod(m, caddr_t), len);
    732 		pkt += len;
    733 		totlen -= len;
    734 		*mp = m;
    735 		mp = &m->m_next;
    736 	}
    737 
    738 	return (top);
    739 }
    740 
    741 /*
    742  * Go through the list of multicast addresses and calculate the logical
    743  * address filter.
    744  */
    745 void
    746 mace_calcladrf(ac, af)
    747 	struct ethercom *ac;
    748 	u_int8_t *af;
    749 {
    750 	struct ifnet *ifp = &ac->ec_if;
    751 	struct ether_multi *enm;
    752 	register u_char *cp, c;
    753 	register u_int32_t crc;
    754 	register int i, len;
    755 	struct ether_multistep step;
    756 
    757 	/*
    758 	 * Set up multicast address filter by passing all multicast addresses
    759 	 * through a crc generator, and then using the high order 6 bits as an
    760 	 * index into the 64 bit logical address filter.  The high order bit
    761 	 * selects the word, while the rest of the bits select the bit within
    762 	 * the word.
    763 	 */
    764 
    765 	*((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0;
    766 
    767 	ETHER_FIRST_MULTI(step, ac, enm);
    768 	while (enm != NULL) {
    769 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    770 			/*
    771 			 * We must listen to a range of multicast addresses.
    772 			 * For now, just accept all multicasts, rather than
    773 			 * trying to set only those filter bits needed to match
    774 			 * the range.  (At this time, the only use of address
    775 			 * ranges is for IP multicast routing, for which the
    776 			 * range is big enough to require all bits set.)
    777 			 */
    778 			goto allmulti;
    779 		}
    780 
    781 		cp = enm->enm_addrlo;
    782 		crc = 0xffffffff;
    783 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
    784 			c = *cp++;
    785 			for (i = 8; --i >= 0;) {
    786 				if ((crc & 0x01) ^ (c & 0x01)) {
    787 					crc >>= 1;
    788 					crc ^= 0xedb88320;
    789 				} else
    790 					crc >>= 1;
    791 				c >>= 1;
    792 			}
    793 		}
    794 		/* Just want the 6 most significant bits. */
    795 		crc >>= 26;
    796 
    797 		/* Set the corresponding bit in the filter. */
    798 		af[crc >> 3] |= 1 << (crc & 7);
    799 
    800 		ETHER_NEXT_MULTI(step, enm);
    801 	}
    802 	ifp->if_flags &= ~IFF_ALLMULTI;
    803 	return;
    804 
    805 allmulti:
    806 	ifp->if_flags |= IFF_ALLMULTI;
    807 	*((u_int32_t *)af) = *((u_int32_t *)af + 1) = 0xffffffff;
    808 }
    809 
    810 int
    811 mc_mediachange(ifp)
    812 	struct ifnet *ifp;
    813 {
    814 	return EINVAL;
    815 }
    816 
    817 void
    818 mc_mediastatus(ifp, ifmr)
    819 	struct ifnet *ifp;
    820 	struct ifmediareq *ifmr;
    821 {
    822 	struct mc_softc *sc = ifp->if_softc;
    823 
    824 	if ((ifp->if_flags & IFF_UP) == 0)
    825 		return;
    826 
    827 	if (sc->sc_havecarrier)
    828 		ifmr->ifm_status |= IFM_ACTIVE;
    829 }
    830