xref: /netbsd-src/sys/dev/ic/lance.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: lance.c,v 1.25 2001/07/07 16:13:48 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, NASA Ames Research Center.
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. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*-
41  * Copyright (c) 1992, 1993
42  *	The Regents of the University of California.  All rights reserved.
43  *
44  * This code is derived from software contributed to Berkeley by
45  * Ralph Campbell and Rick Macklem.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. All advertising materials mentioning features or use of this software
56  *    must display the following acknowledgement:
57  *	This product includes software developed by the University of
58  *	California, Berkeley and its contributors.
59  * 4. Neither the name of the University nor the names of its contributors
60  *    may be used to endorse or promote products derived from this software
61  *    without specific prior written permission.
62  *
63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73  * SUCH DAMAGE.
74  *
75  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
76  */
77 
78 #include "opt_ccitt.h"
79 #include "opt_llc.h"
80 #include "bpfilter.h"
81 #include "rnd.h"
82 
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/mbuf.h>
86 #include <sys/syslog.h>
87 #include <sys/socket.h>
88 #include <sys/device.h>
89 #include <sys/malloc.h>
90 #include <sys/ioctl.h>
91 #include <sys/errno.h>
92 #if NRND > 0
93 #include <sys/rnd.h>
94 #endif
95 
96 #include <net/if.h>
97 #include <net/if_dl.h>
98 #include <net/if_ether.h>
99 #include <net/if_media.h>
100 
101 #if defined(CCITT) && defined(LLC)
102 #include <sys/socketvar.h>
103 #include <netccitt/x25.h>
104 #include <netccitt/pk.h>
105 #include <netccitt/pk_var.h>
106 #include <netccitt/pk_extern.h>
107 #endif
108 
109 #if NBPFILTER > 0
110 #include <net/bpf.h>
111 #include <net/bpfdesc.h>
112 #endif
113 
114 #include <dev/ic/lancereg.h>
115 #include <dev/ic/lancevar.h>
116 
117 #if defined(_KERNEL_OPT)
118 #include "opt_ddb.h"
119 #endif
120 
121 #ifdef DDB
122 #define	integrate
123 #define hide
124 #else
125 #define	integrate	static __inline
126 #define hide		static
127 #endif
128 
129 integrate struct mbuf *lance_get __P((struct lance_softc *, int, int));
130 
131 hide void lance_shutdown __P((void *));
132 
133 int lance_mediachange __P((struct ifnet *));
134 void lance_mediastatus __P((struct ifnet *, struct ifmediareq *));
135 
136 static inline u_int16_t ether_cmp __P((void *, void *));
137 
138 void lance_stop __P((struct ifnet *, int));
139 int lance_ioctl __P((struct ifnet *, u_long, caddr_t));
140 void lance_watchdog __P((struct ifnet *));
141 
142 /*
143  * Compare two Ether/802 addresses for equality, inlined and
144  * unrolled for speed.  Use this like memcmp().
145  *
146  * XXX: Add <machine/inlines.h> for stuff like this?
147  * XXX: or maybe add it to libkern.h instead?
148  *
149  * "I'd love to have an inline assembler version of this."
150  * XXX: Who wanted that? mycroft?  I wrote one, but this
151  * version in C is as good as hand-coded assembly. -gwr
152  *
153  * Please do NOT tweak this without looking at the actual
154  * assembly code generated before and after your tweaks!
155  */
156 static inline u_int16_t
157 ether_cmp(one, two)
158 	void *one, *two;
159 {
160 	u_int16_t *a = (u_short *) one;
161 	u_int16_t *b = (u_short *) two;
162 	u_int16_t diff;
163 
164 #ifdef	m68k
165 	/*
166 	 * The post-increment-pointer form produces the best
167 	 * machine code for m68k.  This was carefully tuned
168 	 * so it compiles to just 8 short (2-byte) op-codes!
169 	 */
170 	diff  = *a++ - *b++;
171 	diff |= *a++ - *b++;
172 	diff |= *a++ - *b++;
173 #else
174 	/*
175 	 * Most modern CPUs do better with a single expresion.
176 	 * Note that short-cut evaluation is NOT helpful here,
177 	 * because it just makes the code longer, not faster!
178 	 */
179 	diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]);
180 #endif
181 
182 	return (diff);
183 }
184 
185 #define ETHER_CMP	ether_cmp
186 
187 #ifdef LANCE_REVC_BUG
188 /* Make sure this is short-aligned, for ether_cmp(). */
189 static u_int16_t bcast_enaddr[3] = { ~0, ~0, ~0 };
190 #endif
191 
192 void
193 lance_config(sc)
194 	struct lance_softc *sc;
195 {
196 	int i, nbuf;
197 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
198 
199 	/* Initialize ifnet structure. */
200 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
201 	ifp->if_softc = sc;
202 	ifp->if_start = sc->sc_start;
203 	ifp->if_ioctl = lance_ioctl;
204 	ifp->if_watchdog = lance_watchdog;
205 	ifp->if_init = lance_init;
206 	ifp->if_stop = lance_stop;
207 	ifp->if_flags =
208 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
209 #ifdef LANCE_REVC_BUG
210 	ifp->if_flags &= ~IFF_MULTICAST;
211 #endif
212 	IFQ_SET_READY(&ifp->if_snd);
213 
214 	/* Initialize ifmedia structures. */
215 	ifmedia_init(&sc->sc_media, 0, lance_mediachange, lance_mediastatus);
216 	if (sc->sc_supmedia != NULL) {
217 		for (i = 0; i < sc->sc_nsupmedia; i++)
218 			ifmedia_add(&sc->sc_media, sc->sc_supmedia[i],
219 			   0, NULL);
220 		ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
221 	} else {
222 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
223 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
224 	}
225 
226 	switch (sc->sc_memsize) {
227 	case 8192:
228 		sc->sc_nrbuf = 4;
229 		sc->sc_ntbuf = 1;
230 		break;
231 	case 16384:
232 		sc->sc_nrbuf = 8;
233 		sc->sc_ntbuf = 2;
234 		break;
235 	case 32768:
236 		sc->sc_nrbuf = 16;
237 		sc->sc_ntbuf = 4;
238 		break;
239 	case 65536:
240 		sc->sc_nrbuf = 32;
241 		sc->sc_ntbuf = 8;
242 		break;
243 	case 131072:
244 		sc->sc_nrbuf = 64;
245 		sc->sc_ntbuf = 16;
246 		break;
247 	case 262144:
248 		sc->sc_nrbuf = 128;
249 		sc->sc_ntbuf = 32;
250 		break;
251 	default:
252 		/* weird memory size; cope with it */
253 		nbuf = sc->sc_memsize / LEBLEN;
254 		sc->sc_ntbuf = nbuf / 5;
255 		sc->sc_nrbuf = nbuf - sc->sc_ntbuf;
256 	}
257 
258 	printf(": address %s\n", ether_sprintf(sc->sc_enaddr));
259 	printf("%s: %d receive buffers, %d transmit buffers\n",
260 	    sc->sc_dev.dv_xname, sc->sc_nrbuf, sc->sc_ntbuf);
261 
262 	/* Make sure the chip is stopped. */
263 	lance_stop(ifp, 0);
264 
265 	/* claim 802.1q capability */
266 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
267 	/* Attach the interface. */
268 	if_attach(ifp);
269 	ether_ifattach(ifp, sc->sc_enaddr);
270 
271 	sc->sc_sh = shutdownhook_establish(lance_shutdown, ifp);
272 	if (sc->sc_sh == NULL)
273 		panic("lance_config: can't establish shutdownhook");
274 	sc->sc_rbufaddr = malloc(sc->sc_nrbuf * sizeof(int), M_DEVBUF,
275 					M_WAITOK);
276 	sc->sc_tbufaddr = malloc(sc->sc_ntbuf * sizeof(int), M_DEVBUF,
277 					M_WAITOK);
278 
279 #if NRND > 0
280 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
281 			  RND_TYPE_NET, 0);
282 #endif
283 }
284 
285 void
286 lance_reset(sc)
287 	struct lance_softc *sc;
288 {
289 	int s;
290 
291 	s = splnet();
292 	lance_init(&sc->sc_ethercom.ec_if);
293 	splx(s);
294 }
295 
296 void
297 lance_stop(ifp, disable)
298 	struct ifnet *ifp;
299 	int disable;
300 {
301 	struct lance_softc *sc = ifp->if_softc;
302 
303 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
304 }
305 
306 /*
307  * Initialization of interface; set up initialization block
308  * and transmit/receive descriptor rings.
309  */
310 int
311 lance_init(ifp)
312 	struct ifnet *ifp;
313 {
314 	struct lance_softc *sc = ifp->if_softc;
315 	int timo;
316 	u_long a;
317 
318 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
319 	DELAY(100);
320 
321 	/* Newer LANCE chips have a reset register */
322 	if (sc->sc_hwreset)
323 		(*sc->sc_hwreset)(sc);
324 
325 	/* Set the correct byte swapping mode, etc. */
326 	(*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
327 
328 	/* Set up LANCE init block. */
329 	(*sc->sc_meminit)(sc);
330 
331 	/* Give LANCE the physical address of its init block. */
332 	a = sc->sc_addr + LE_INITADDR(sc);
333 	(*sc->sc_wrcsr)(sc, LE_CSR1, a);
334 	(*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
335 
336 	/* Try to initialize the LANCE. */
337 	DELAY(100);
338 	(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
339 
340 	/* Wait for initialization to finish. */
341 	for (timo = 100000; timo; timo--)
342 		if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
343 			break;
344 
345 	if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
346 		/* Start the LANCE. */
347 		(*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT |
348 		    LE_C0_IDON);
349 		ifp->if_flags |= IFF_RUNNING;
350 		ifp->if_flags &= ~IFF_OACTIVE;
351 		ifp->if_timer = 0;
352 		(*sc->sc_start)(ifp);
353 	} else
354 		printf("%s: controller failed to initialize\n",
355 			sc->sc_dev.dv_xname);
356 	if (sc->sc_hwinit)
357 		(*sc->sc_hwinit)(sc);
358 
359 	return (0);
360 }
361 
362 /*
363  * Routine to copy from mbuf chain to transmit buffer in
364  * network buffer memory.
365  */
366 int
367 lance_put(sc, boff, m)
368 	struct lance_softc *sc;
369 	int boff;
370 	struct mbuf *m;
371 {
372 	struct mbuf *n;
373 	int len, tlen = 0;
374 
375 	for (; m; m = n) {
376 		len = m->m_len;
377 		if (len == 0) {
378 			MFREE(m, n);
379 			continue;
380 		}
381 		(*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
382 		boff += len;
383 		tlen += len;
384 		MFREE(m, n);
385 	}
386 	if (tlen < LEMINSIZE) {
387 		(*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
388 		tlen = LEMINSIZE;
389 	}
390 	return (tlen);
391 }
392 
393 /*
394  * Pull data off an interface.
395  * Len is length of data, with local net header stripped.
396  * We copy the data into mbufs.  When full cluster sized units are present
397  * we copy into clusters.
398  */
399 integrate struct mbuf *
400 lance_get(sc, boff, totlen)
401 	struct lance_softc *sc;
402 	int boff, totlen;
403 {
404 	struct mbuf *m, *m0, *newm;
405 	int len;
406 
407 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
408 	if (m0 == 0)
409 		return (0);
410 	m0->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
411 	m0->m_pkthdr.len = totlen;
412 	len = MHLEN;
413 	m = m0;
414 
415 	while (totlen > 0) {
416 		if (totlen >= MINCLSIZE) {
417 			MCLGET(m, M_DONTWAIT);
418 			if ((m->m_flags & M_EXT) == 0)
419 				goto bad;
420 			len = MCLBYTES;
421 		}
422 
423 		if (m == m0) {
424 			caddr_t newdata = (caddr_t)
425 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
426 			    sizeof(struct ether_header);
427 			len -= newdata - m->m_data;
428 			m->m_data = newdata;
429 		}
430 
431 		m->m_len = len = min(totlen, len);
432 		(*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
433 		boff += len;
434 
435 		totlen -= len;
436 		if (totlen > 0) {
437 			MGET(newm, M_DONTWAIT, MT_DATA);
438 			if (newm == 0)
439 				goto bad;
440 			len = MLEN;
441 			m = m->m_next = newm;
442 		}
443 	}
444 
445 	return (m0);
446 
447 bad:
448 	m_freem(m0);
449 	return (0);
450 }
451 
452 /*
453  * Pass a packet to the higher levels.
454  */
455 void
456 lance_read(sc, boff, len)
457 	struct lance_softc *sc;
458 	int boff, len;
459 {
460 	struct mbuf *m;
461 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
462 #ifdef LANCE_REVC_BUG
463 	struct ether_header *eh;
464 #endif
465 
466 	if (len <= sizeof(struct ether_header) ||
467 	    len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
468 		ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
469 		ETHERMTU + sizeof(struct ether_header))) {
470 #ifdef LEDEBUG
471 		printf("%s: invalid packet size %d; dropping\n",
472 		    sc->sc_dev.dv_xname, len);
473 #endif
474 		ifp->if_ierrors++;
475 		return;
476 	}
477 
478 	/* Pull packet off interface. */
479 	m = lance_get(sc, boff, len);
480 	if (m == 0) {
481 		ifp->if_ierrors++;
482 		return;
483 	}
484 
485 	ifp->if_ipackets++;
486 
487 #if NBPFILTER > 0
488 	/*
489 	 * Check if there's a BPF listener on this interface.
490 	 * If so, hand off the raw packet to BPF.
491 	 */
492 	if (ifp->if_bpf)
493 		bpf_mtap(ifp->if_bpf, m);
494 #endif
495 
496 #ifdef LANCE_REVC_BUG
497 	/*
498 	 * The old LANCE (Rev. C) chips have a bug which causes
499 	 * garbage to be inserted in front of the received packet.
500 	 * The work-around is to ignore packets with an invalid
501 	 * destination address (garbage will usually not match).
502 	 * Of course, this precludes multicast support...
503 	 */
504 	eh = mtod(m, struct ether_header *);
505 	if (ETHER_CMP(eh->ether_dhost, sc->sc_enaddr) &&
506 	    ETHER_CMP(eh->ether_dhost, bcast_enaddr)) {
507 		m_freem(m);
508 		return;
509 	}
510 #endif
511 
512 	/* Pass the packet up. */
513 	(*ifp->if_input)(ifp, m);
514 }
515 
516 #undef	ifp
517 
518 void
519 lance_watchdog(ifp)
520 	struct ifnet *ifp;
521 {
522 	struct lance_softc *sc = ifp->if_softc;
523 
524 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
525 	++ifp->if_oerrors;
526 
527 	lance_reset(sc);
528 }
529 
530 int
531 lance_mediachange(ifp)
532 	struct ifnet *ifp;
533 {
534 	struct lance_softc *sc = ifp->if_softc;
535 
536 	if (sc->sc_mediachange)
537 		return ((*sc->sc_mediachange)(sc));
538 	return (0);
539 }
540 
541 void
542 lance_mediastatus(ifp, ifmr)
543 	struct ifnet *ifp;
544 	struct ifmediareq *ifmr;
545 {
546 	struct lance_softc *sc = ifp->if_softc;
547 
548 	if ((ifp->if_flags & IFF_UP) == 0)
549 		return;
550 
551 	ifmr->ifm_status = IFM_AVALID;
552 	if (sc->sc_havecarrier)
553 		ifmr->ifm_status |= IFM_ACTIVE;
554 
555 	if (sc->sc_mediastatus)
556 		(*sc->sc_mediastatus)(sc, ifmr);
557 }
558 
559 /*
560  * Process an ioctl request.
561  */
562 int
563 lance_ioctl(ifp, cmd, data)
564 	struct ifnet *ifp;
565 	u_long cmd;
566 	caddr_t data;
567 {
568 	struct lance_softc *sc = ifp->if_softc;
569 	struct ifreq *ifr = (struct ifreq *)data;
570 	int s, error = 0;
571 
572 	s = splnet();
573 
574 	switch (cmd) {
575 
576 	case SIOCSIFADDR:
577 	case SIOCSIFFLAGS:
578 		error = ether_ioctl(ifp, cmd, data);
579 		break;
580 
581 #if defined(CCITT) && defined(LLC)
582 	case SIOCSIFCONF_X25:
583 	    {
584 		struct ifaddr *ifa = (struct ifaddr *) data;
585 
586 		ifp->if_flags |= IFF_UP;
587 		ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
588 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
589 		if (error == 0)
590 			lance_init(&sc->sc_ethercom.ec_if);
591 		break;
592 	    }
593 #endif /* CCITT && LLC */
594 
595 	case SIOCADDMULTI:
596 	case SIOCDELMULTI:
597 		error = (cmd == SIOCADDMULTI) ?
598 		    ether_addmulti(ifr, &sc->sc_ethercom) :
599 		    ether_delmulti(ifr, &sc->sc_ethercom);
600 
601 		if (error == ENETRESET) {
602 			/*
603 			 * Multicast list has changed; set the hardware filter
604 			 * accordingly.
605 			 */
606 			lance_reset(sc);
607 			error = 0;
608 		}
609 		break;
610 
611 	case SIOCGIFMEDIA:
612 	case SIOCSIFMEDIA:
613 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
614 		break;
615 
616 	default:
617 		error = EINVAL;
618 		break;
619 	}
620 
621 	splx(s);
622 	return (error);
623 }
624 
625 hide void
626 lance_shutdown(arg)
627 	void *arg;
628 {
629 
630 	lance_stop((struct ifnet *)arg, 0);
631 }
632 
633 /*
634  * Set up the logical address filter.
635  */
636 void
637 lance_setladrf(ac, af)
638 	struct ethercom *ac;
639 	u_int16_t *af;
640 {
641 	struct ifnet *ifp = &ac->ec_if;
642 	struct ether_multi *enm;
643 	u_int32_t crc;
644 	struct ether_multistep step;
645 
646 	/*
647 	 * Set up multicast address filter by passing all multicast addresses
648 	 * through a crc generator, and then using the high order 6 bits as an
649 	 * index into the 64 bit logical address filter.  The high order bit
650 	 * selects the word, while the rest of the bits select the bit within
651 	 * the word.
652 	 */
653 
654 	if (ifp->if_flags & IFF_PROMISC)
655 		goto allmulti;
656 
657 	af[0] = af[1] = af[2] = af[3] = 0x0000;
658 	ETHER_FIRST_MULTI(step, ac, enm);
659 	while (enm != NULL) {
660 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
661 			/*
662 			 * We must listen to a range of multicast addresses.
663 			 * For now, just accept all multicasts, rather than
664 			 * trying to set only those filter bits needed to match
665 			 * the range.  (At this time, the only use of address
666 			 * ranges is for IP multicast routing, for which the
667 			 * range is big enough to require all bits set.)
668 			 */
669 			goto allmulti;
670 		}
671 
672 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
673 
674 		/* Just want the 6 most significant bits. */
675 		crc >>= 26;
676 
677 		/* Set the corresponding bit in the filter. */
678 		af[crc >> 4] |= 1 << (crc & 0xf);
679 
680 		ETHER_NEXT_MULTI(step, enm);
681 	}
682 	ifp->if_flags &= ~IFF_ALLMULTI;
683 	return;
684 
685 allmulti:
686 	ifp->if_flags |= IFF_ALLMULTI;
687 	af[0] = af[1] = af[2] = af[3] = 0xffff;
688 }
689 
690 /*
691  * Routines for accessing the transmit and receive buffers.
692  * The various CPU and adapter configurations supported by this
693  * driver require three different access methods for buffers
694  * and descriptors:
695  *	(1) contig (contiguous data; no padding),
696  *	(2) gap2 (two bytes of data followed by two bytes of padding),
697  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
698  */
699 
700 /*
701  * contig: contiguous data with no padding.
702  *
703  * Buffers may have any alignment.
704  */
705 
706 void
707 lance_copytobuf_contig(sc, from, boff, len)
708 	struct lance_softc *sc;
709 	void *from;
710 	int boff, len;
711 {
712 	volatile caddr_t buf = sc->sc_mem;
713 
714 	/*
715 	 * Just call memcpy() to do the work.
716 	 */
717 	memcpy(buf + boff, from, len);
718 }
719 
720 void
721 lance_copyfrombuf_contig(sc, to, boff, len)
722 	struct lance_softc *sc;
723 	void *to;
724 	int boff, len;
725 {
726 	volatile caddr_t buf = sc->sc_mem;
727 
728 	/*
729 	 * Just call memcpy() to do the work.
730 	 */
731 	memcpy(to, buf + boff, len);
732 }
733 
734 void
735 lance_zerobuf_contig(sc, boff, len)
736 	struct lance_softc *sc;
737 	int boff, len;
738 {
739 	volatile caddr_t buf = sc->sc_mem;
740 
741 	/*
742 	 * Just let memset() do the work
743 	 */
744 	memset(buf + boff, 0, len);
745 }
746 
747 #if 0
748 /*
749  * Examples only; duplicate these and tweak (if necessary) in
750  * machine-specific front-ends.
751  */
752 
753 /*
754  * gap2: two bytes of data followed by two bytes of pad.
755  *
756  * Buffers must be 4-byte aligned.  The code doesn't worry about
757  * doing an extra byte.
758  */
759 
760 void
761 lance_copytobuf_gap2(sc, fromv, boff, len)
762 	struct lance_softc *sc;
763 	void *fromv;
764 	int boff;
765 	int len;
766 {
767 	volatile caddr_t buf = sc->sc_mem;
768 	caddr_t from = fromv;
769 	volatile u_int16_t *bptr;
770 
771 	if (boff & 0x1) {
772 		/* handle unaligned first byte */
773 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
774 		*bptr = (*from++ << 8) | (*bptr & 0xff);
775 		bptr += 2;
776 		len--;
777 	} else
778 		bptr = ((volatile u_int16_t *)buf) + boff;
779 	while (len > 1) {
780 		*bptr = (from[1] << 8) | (from[0] & 0xff);
781 		bptr += 2;
782 		from += 2;
783 		len -= 2;
784 	}
785 	if (len == 1)
786 		*bptr = (u_int16_t)*from;
787 }
788 
789 void
790 lance_copyfrombuf_gap2(sc, tov, boff, len)
791 	struct lance_softc *sc;
792 	void *tov;
793 	int boff, len;
794 {
795 	volatile caddr_t buf = sc->sc_mem;
796 	caddr_t to = tov;
797 	volatile u_int16_t *bptr;
798 	u_int16_t tmp;
799 
800 	if (boff & 0x1) {
801 		/* handle unaligned first byte */
802 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
803 		*to++ = (*bptr >> 8) & 0xff;
804 		bptr += 2;
805 		len--;
806 	} else
807 		bptr = ((volatile u_int16_t *)buf) + boff;
808 	while (len > 1) {
809 		tmp = *bptr;
810 		*to++ = tmp & 0xff;
811 		*to++ = (tmp >> 8) & 0xff;
812 		bptr += 2;
813 		len -= 2;
814 	}
815 	if (len == 1)
816 		*to = *bptr & 0xff;
817 }
818 
819 void
820 lance_zerobuf_gap2(sc, boff, len)
821 	struct lance_softc *sc;
822 	int boff, len;
823 {
824 	volatile caddr_t buf = sc->sc_mem;
825 	volatile u_int16_t *bptr;
826 
827 	if ((unsigned)boff & 0x1) {
828 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
829 		*bptr &= 0xff;
830 		bptr += 2;
831 		len--;
832 	} else
833 		bptr = ((volatile u_int16_t *)buf) + boff;
834 	while (len > 0) {
835 		*bptr = 0;
836 		bptr += 2;
837 		len -= 2;
838 	}
839 }
840 
841 /*
842  * gap16: 16 bytes of data followed by 16 bytes of pad.
843  *
844  * Buffers must be 32-byte aligned.
845  */
846 
847 void
848 lance_copytobuf_gap16(sc, fromv, boff, len)
849 	struct lance_softc *sc;
850 	void *fromv;
851 	int boff;
852 	int len;
853 {
854 	volatile caddr_t buf = sc->sc_mem;
855 	caddr_t from = fromv;
856 	caddr_t bptr;
857 	int xfer;
858 
859 	bptr = buf + ((boff << 1) & ~0x1f);
860 	boff &= 0xf;
861 	xfer = min(len, 16 - boff);
862 	while (len > 0) {
863 		memcpy(bptr + boff, from, xfer);
864 		from += xfer;
865 		bptr += 32;
866 		boff = 0;
867 		len -= xfer;
868 		xfer = min(len, 16);
869 	}
870 }
871 
872 void
873 lance_copyfrombuf_gap16(sc, tov, boff, len)
874 	struct lance_softc *sc;
875 	void *tov;
876 	int boff, len;
877 {
878 	volatile caddr_t buf = sc->sc_mem;
879 	caddr_t to = tov;
880 	caddr_t bptr;
881 	int xfer;
882 
883 	bptr = buf + ((boff << 1) & ~0x1f);
884 	boff &= 0xf;
885 	xfer = min(len, 16 - boff);
886 	while (len > 0) {
887 		memcpy(to, bptr + boff, xfer);
888 		to += xfer;
889 		bptr += 32;
890 		boff = 0;
891 		len -= xfer;
892 		xfer = min(len, 16);
893 	}
894 }
895 
896 void
897 lance_zerobuf_gap16(sc, boff, len)
898 	struct lance_softc *sc;
899 	int boff, len;
900 {
901 	volatile caddr_t buf = sc->sc_mem;
902 	caddr_t bptr;
903 	int xfer;
904 
905 	bptr = buf + ((boff << 1) & ~0x1f);
906 	boff &= 0xf;
907 	xfer = min(len, 16 - boff);
908 	while (len > 0) {
909 		memset(bptr + boff, 0, xfer);
910 		bptr += 32;
911 		boff = 0;
912 		len -= xfer;
913 		xfer = min(len, 16);
914 	}
915 }
916 #endif /* Example only */
917