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