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