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