xref: /netbsd-src/sys/dev/ic/lance.c (revision b8c616269f5ebf18ab2e35cb8099d683130a177c)
1 /*	$NetBSD: lance.c,v 1.28 2003/01/31 17:40:05 pk 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.28 2003/01/31 17:40:05 pk 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 		ifp->if_flags |= IFF_RUNNING;
352 		ifp->if_flags &= ~IFF_OACTIVE;
353 		ifp->if_timer = 0;
354 		(*sc->sc_start)(ifp);
355 	} else
356 		printf("%s: controller failed to initialize\n",
357 			sc->sc_dev.dv_xname);
358 	if (sc->sc_hwinit)
359 		(*sc->sc_hwinit)(sc);
360 
361 	return (0);
362 }
363 
364 /*
365  * Routine to copy from mbuf chain to transmit buffer in
366  * network buffer memory.
367  */
368 int
369 lance_put(sc, boff, m)
370 	struct lance_softc *sc;
371 	int boff;
372 	struct mbuf *m;
373 {
374 	struct mbuf *n;
375 	int len, tlen = 0;
376 
377 	for (; m; m = n) {
378 		len = m->m_len;
379 		if (len == 0) {
380 			MFREE(m, n);
381 			continue;
382 		}
383 		(*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
384 		boff += len;
385 		tlen += len;
386 		MFREE(m, n);
387 	}
388 	if (tlen < LEMINSIZE) {
389 		(*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
390 		tlen = LEMINSIZE;
391 	}
392 	return (tlen);
393 }
394 
395 /*
396  * Pull data off an interface.
397  * Len is length of data, with local net header stripped.
398  * We copy the data into mbufs.  When full cluster sized units are present
399  * we copy into clusters.
400  */
401 integrate struct mbuf *
402 lance_get(sc, boff, totlen)
403 	struct lance_softc *sc;
404 	int boff, totlen;
405 {
406 	struct mbuf *m, *m0, *newm;
407 	int len;
408 
409 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
410 	if (m0 == 0)
411 		return (0);
412 	m0->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
413 	m0->m_pkthdr.len = totlen;
414 	len = MHLEN;
415 	m = m0;
416 
417 	while (totlen > 0) {
418 		if (totlen >= MINCLSIZE) {
419 			MCLGET(m, M_DONTWAIT);
420 			if ((m->m_flags & M_EXT) == 0)
421 				goto bad;
422 			len = MCLBYTES;
423 		}
424 
425 		if (m == m0) {
426 			caddr_t newdata = (caddr_t)
427 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
428 			    sizeof(struct ether_header);
429 			len -= newdata - m->m_data;
430 			m->m_data = newdata;
431 		}
432 
433 		m->m_len = len = min(totlen, len);
434 		(*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
435 		boff += len;
436 
437 		totlen -= len;
438 		if (totlen > 0) {
439 			MGET(newm, M_DONTWAIT, MT_DATA);
440 			if (newm == 0)
441 				goto bad;
442 			len = MLEN;
443 			m = m->m_next = newm;
444 		}
445 	}
446 
447 	return (m0);
448 
449 bad:
450 	m_freem(m0);
451 	return (0);
452 }
453 
454 /*
455  * Pass a packet to the higher levels.
456  */
457 void
458 lance_read(sc, boff, len)
459 	struct lance_softc *sc;
460 	int boff, len;
461 {
462 	struct mbuf *m;
463 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
464 	struct ether_header *eh;
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 	eh = mtod(m, struct ether_header *);
488 
489 #ifdef LANCE_REVC_BUG
490 	/*
491 	 * The old LANCE (Rev. C) chips have a bug which causes
492 	 * garbage to be inserted in front of the received packet.
493 	 * The work-around is to ignore packets with an invalid
494 	 * destination address (garbage will usually not match).
495 	 * Of course, this precludes multicast support...
496 	 */
497 	if (ETHER_CMP(eh->ether_dhost, sc->sc_enaddr) &&
498 	    ETHER_CMP(eh->ether_dhost, bcast_enaddr)) {
499 		m_freem(m);
500 		return;
501 	}
502 #endif
503 
504 	/*
505 	 * Some lance device does not present IFF_SIMPLEX behavior on multicast
506 	 * packets.  Make sure to drop it if it is from ourselves.
507 	 */
508 	if (!ETHER_CMP(eh->ether_shost, sc->sc_enaddr)) {
509 		m_freem(m);
510 		return;
511 	}
512 
513 #if NBPFILTER > 0
514 	/*
515 	 * Check if there's a BPF listener on this interface.
516 	 * If so, hand off the raw packet to BPF.
517 	 */
518 	if (ifp->if_bpf)
519 		bpf_mtap(ifp->if_bpf, m);
520 #endif
521 
522 	/* Pass the packet up. */
523 	(*ifp->if_input)(ifp, m);
524 }
525 
526 #undef	ifp
527 
528 void
529 lance_watchdog(ifp)
530 	struct ifnet *ifp;
531 {
532 	struct lance_softc *sc = ifp->if_softc;
533 
534 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
535 	++ifp->if_oerrors;
536 
537 	lance_reset(sc);
538 }
539 
540 int
541 lance_mediachange(ifp)
542 	struct ifnet *ifp;
543 {
544 	struct lance_softc *sc = ifp->if_softc;
545 
546 	if (sc->sc_mediachange)
547 		return ((*sc->sc_mediachange)(sc));
548 	return (0);
549 }
550 
551 void
552 lance_mediastatus(ifp, ifmr)
553 	struct ifnet *ifp;
554 	struct ifmediareq *ifmr;
555 {
556 	struct lance_softc *sc = ifp->if_softc;
557 
558 	if ((ifp->if_flags & IFF_UP) == 0)
559 		return;
560 
561 	ifmr->ifm_status = IFM_AVALID;
562 	if (sc->sc_havecarrier)
563 		ifmr->ifm_status |= IFM_ACTIVE;
564 
565 	if (sc->sc_mediastatus)
566 		(*sc->sc_mediastatus)(sc, ifmr);
567 }
568 
569 /*
570  * Process an ioctl request.
571  */
572 int
573 lance_ioctl(ifp, cmd, data)
574 	struct ifnet *ifp;
575 	u_long cmd;
576 	caddr_t data;
577 {
578 	struct lance_softc *sc = ifp->if_softc;
579 	struct ifreq *ifr = (struct ifreq *)data;
580 	int s, error = 0;
581 
582 	s = splnet();
583 
584 	switch (cmd) {
585 
586 	case SIOCSIFADDR:
587 	case SIOCSIFFLAGS:
588 		error = ether_ioctl(ifp, cmd, data);
589 		break;
590 
591 #if defined(CCITT) && defined(LLC)
592 	case SIOCSIFCONF_X25:
593 	    {
594 		struct ifaddr *ifa = (struct ifaddr *) data;
595 
596 		ifp->if_flags |= IFF_UP;
597 		ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
598 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
599 		if (error == 0)
600 			lance_init(&sc->sc_ethercom.ec_if);
601 		break;
602 	    }
603 #endif /* CCITT && LLC */
604 
605 	case SIOCADDMULTI:
606 	case SIOCDELMULTI:
607 		error = (cmd == SIOCADDMULTI) ?
608 		    ether_addmulti(ifr, &sc->sc_ethercom) :
609 		    ether_delmulti(ifr, &sc->sc_ethercom);
610 
611 		if (error == ENETRESET) {
612 			/*
613 			 * Multicast list has changed; set the hardware filter
614 			 * accordingly.
615 			 */
616 			lance_reset(sc);
617 			error = 0;
618 		}
619 		break;
620 
621 	case SIOCGIFMEDIA:
622 	case SIOCSIFMEDIA:
623 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
624 		break;
625 
626 	default:
627 		error = EINVAL;
628 		break;
629 	}
630 
631 	splx(s);
632 	return (error);
633 }
634 
635 hide void
636 lance_shutdown(arg)
637 	void *arg;
638 {
639 
640 	lance_stop((struct ifnet *)arg, 0);
641 }
642 
643 /*
644  * Set up the logical address filter.
645  */
646 void
647 lance_setladrf(ac, af)
648 	struct ethercom *ac;
649 	u_int16_t *af;
650 {
651 	struct ifnet *ifp = &ac->ec_if;
652 	struct ether_multi *enm;
653 	u_int32_t crc;
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 	if (ifp->if_flags & IFF_PROMISC)
665 		goto allmulti;
666 
667 	af[0] = af[1] = af[2] = af[3] = 0x0000;
668 	ETHER_FIRST_MULTI(step, ac, enm);
669 	while (enm != NULL) {
670 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
671 			/*
672 			 * We must listen to a range of multicast addresses.
673 			 * For now, just accept all multicasts, rather than
674 			 * trying to set only those filter bits needed to match
675 			 * the range.  (At this time, the only use of address
676 			 * ranges is for IP multicast routing, for which the
677 			 * range is big enough to require all bits set.)
678 			 */
679 			goto allmulti;
680 		}
681 
682 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
683 
684 		/* Just want the 6 most significant bits. */
685 		crc >>= 26;
686 
687 		/* Set the corresponding bit in the filter. */
688 		af[crc >> 4] |= 1 << (crc & 0xf);
689 
690 		ETHER_NEXT_MULTI(step, enm);
691 	}
692 	ifp->if_flags &= ~IFF_ALLMULTI;
693 	return;
694 
695 allmulti:
696 	ifp->if_flags |= IFF_ALLMULTI;
697 	af[0] = af[1] = af[2] = af[3] = 0xffff;
698 }
699 
700 /*
701  * Routines for accessing the transmit and receive buffers.
702  * The various CPU and adapter configurations supported by this
703  * driver require three different access methods for buffers
704  * and descriptors:
705  *	(1) contig (contiguous data; no padding),
706  *	(2) gap2 (two bytes of data followed by two bytes of padding),
707  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
708  */
709 
710 /*
711  * contig: contiguous data with no padding.
712  *
713  * Buffers may have any alignment.
714  */
715 
716 void
717 lance_copytobuf_contig(sc, from, boff, len)
718 	struct lance_softc *sc;
719 	void *from;
720 	int boff, len;
721 {
722 	volatile caddr_t buf = sc->sc_mem;
723 
724 	/*
725 	 * Just call memcpy() to do the work.
726 	 */
727 	memcpy(buf + boff, from, len);
728 }
729 
730 void
731 lance_copyfrombuf_contig(sc, to, boff, len)
732 	struct lance_softc *sc;
733 	void *to;
734 	int boff, len;
735 {
736 	volatile caddr_t buf = sc->sc_mem;
737 
738 	/*
739 	 * Just call memcpy() to do the work.
740 	 */
741 	memcpy(to, buf + boff, len);
742 }
743 
744 void
745 lance_zerobuf_contig(sc, boff, len)
746 	struct lance_softc *sc;
747 	int boff, len;
748 {
749 	volatile caddr_t buf = sc->sc_mem;
750 
751 	/*
752 	 * Just let memset() do the work
753 	 */
754 	memset(buf + boff, 0, len);
755 }
756 
757 #if 0
758 /*
759  * Examples only; duplicate these and tweak (if necessary) in
760  * machine-specific front-ends.
761  */
762 
763 /*
764  * gap2: two bytes of data followed by two bytes of pad.
765  *
766  * Buffers must be 4-byte aligned.  The code doesn't worry about
767  * doing an extra byte.
768  */
769 
770 void
771 lance_copytobuf_gap2(sc, fromv, boff, len)
772 	struct lance_softc *sc;
773 	void *fromv;
774 	int boff;
775 	int len;
776 {
777 	volatile caddr_t buf = sc->sc_mem;
778 	caddr_t from = fromv;
779 	volatile u_int16_t *bptr;
780 
781 	if (boff & 0x1) {
782 		/* handle unaligned first byte */
783 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
784 		*bptr = (*from++ << 8) | (*bptr & 0xff);
785 		bptr += 2;
786 		len--;
787 	} else
788 		bptr = ((volatile u_int16_t *)buf) + boff;
789 	while (len > 1) {
790 		*bptr = (from[1] << 8) | (from[0] & 0xff);
791 		bptr += 2;
792 		from += 2;
793 		len -= 2;
794 	}
795 	if (len == 1)
796 		*bptr = (u_int16_t)*from;
797 }
798 
799 void
800 lance_copyfrombuf_gap2(sc, tov, boff, len)
801 	struct lance_softc *sc;
802 	void *tov;
803 	int boff, len;
804 {
805 	volatile caddr_t buf = sc->sc_mem;
806 	caddr_t to = tov;
807 	volatile u_int16_t *bptr;
808 	u_int16_t tmp;
809 
810 	if (boff & 0x1) {
811 		/* handle unaligned first byte */
812 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
813 		*to++ = (*bptr >> 8) & 0xff;
814 		bptr += 2;
815 		len--;
816 	} else
817 		bptr = ((volatile u_int16_t *)buf) + boff;
818 	while (len > 1) {
819 		tmp = *bptr;
820 		*to++ = tmp & 0xff;
821 		*to++ = (tmp >> 8) & 0xff;
822 		bptr += 2;
823 		len -= 2;
824 	}
825 	if (len == 1)
826 		*to = *bptr & 0xff;
827 }
828 
829 void
830 lance_zerobuf_gap2(sc, boff, len)
831 	struct lance_softc *sc;
832 	int boff, len;
833 {
834 	volatile caddr_t buf = sc->sc_mem;
835 	volatile u_int16_t *bptr;
836 
837 	if ((unsigned)boff & 0x1) {
838 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
839 		*bptr &= 0xff;
840 		bptr += 2;
841 		len--;
842 	} else
843 		bptr = ((volatile u_int16_t *)buf) + boff;
844 	while (len > 0) {
845 		*bptr = 0;
846 		bptr += 2;
847 		len -= 2;
848 	}
849 }
850 
851 /*
852  * gap16: 16 bytes of data followed by 16 bytes of pad.
853  *
854  * Buffers must be 32-byte aligned.
855  */
856 
857 void
858 lance_copytobuf_gap16(sc, fromv, boff, len)
859 	struct lance_softc *sc;
860 	void *fromv;
861 	int boff;
862 	int len;
863 {
864 	volatile caddr_t buf = sc->sc_mem;
865 	caddr_t from = fromv;
866 	caddr_t bptr;
867 	int xfer;
868 
869 	bptr = buf + ((boff << 1) & ~0x1f);
870 	boff &= 0xf;
871 	xfer = min(len, 16 - boff);
872 	while (len > 0) {
873 		memcpy(bptr + boff, from, xfer);
874 		from += xfer;
875 		bptr += 32;
876 		boff = 0;
877 		len -= xfer;
878 		xfer = min(len, 16);
879 	}
880 }
881 
882 void
883 lance_copyfrombuf_gap16(sc, tov, boff, len)
884 	struct lance_softc *sc;
885 	void *tov;
886 	int boff, len;
887 {
888 	volatile caddr_t buf = sc->sc_mem;
889 	caddr_t to = tov;
890 	caddr_t bptr;
891 	int xfer;
892 
893 	bptr = buf + ((boff << 1) & ~0x1f);
894 	boff &= 0xf;
895 	xfer = min(len, 16 - boff);
896 	while (len > 0) {
897 		memcpy(to, bptr + boff, xfer);
898 		to += xfer;
899 		bptr += 32;
900 		boff = 0;
901 		len -= xfer;
902 		xfer = min(len, 16);
903 	}
904 }
905 
906 void
907 lance_zerobuf_gap16(sc, boff, len)
908 	struct lance_softc *sc;
909 	int boff, len;
910 {
911 	volatile caddr_t buf = sc->sc_mem;
912 	caddr_t bptr;
913 	int xfer;
914 
915 	bptr = buf + ((boff << 1) & ~0x1f);
916 	boff &= 0xf;
917 	xfer = min(len, 16 - boff);
918 	while (len > 0) {
919 		memset(bptr + boff, 0, xfer);
920 		bptr += 32;
921 		boff = 0;
922 		len -= xfer;
923 		xfer = min(len, 16);
924 	}
925 }
926 #endif /* Example only */
927