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