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