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