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