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