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