lance.c revision 1.3 1 /* $NetBSD: lance.c,v 1.3 1998/08/15 10:51:18 mycroft 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 register u_int16_t *a = (u_short *) one;
176 register u_int16_t *b = (u_short *) two;
177 register 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 /* Attach the interface. */
243 if_attach(ifp);
244 ether_ifattach(ifp, sc->sc_enaddr);
245
246 #if NBPFILTER > 0
247 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
248 #endif
249
250 switch (sc->sc_memsize) {
251 case 8192:
252 sc->sc_nrbuf = 4;
253 sc->sc_ntbuf = 1;
254 break;
255 case 16384:
256 sc->sc_nrbuf = 8;
257 sc->sc_ntbuf = 2;
258 break;
259 case 32768:
260 sc->sc_nrbuf = 16;
261 sc->sc_ntbuf = 4;
262 break;
263 case 65536:
264 sc->sc_nrbuf = 32;
265 sc->sc_ntbuf = 8;
266 break;
267 case 131072:
268 sc->sc_nrbuf = 64;
269 sc->sc_ntbuf = 16;
270 break;
271 default:
272 panic("lance_config: weird memory size");
273 }
274
275 printf(": address %s\n", ether_sprintf(sc->sc_enaddr));
276 printf("%s: %d receive buffers, %d transmit buffers\n",
277 sc->sc_dev.dv_xname, sc->sc_nrbuf, sc->sc_ntbuf);
278
279 sc->sc_sh = shutdownhook_establish(lance_shutdown, sc);
280 if (sc->sc_sh == NULL)
281 panic("lance_config: can't establish shutdownhook");
282 sc->sc_rbufaddr = malloc(sc->sc_nrbuf * sizeof(int), M_DEVBUF,
283 M_WAITOK);
284 sc->sc_tbufaddr = malloc(sc->sc_ntbuf * sizeof(int), M_DEVBUF,
285 M_WAITOK);
286
287 #if NRND > 0
288 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
289 RND_TYPE_NET);
290 #endif
291 }
292
293 void
294 lance_reset(sc)
295 struct lance_softc *sc;
296 {
297 int s;
298
299 s = splnet();
300 lance_init(sc);
301 splx(s);
302 }
303
304 void
305 lance_stop(sc)
306 struct lance_softc *sc;
307 {
308
309 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
310 }
311
312 /*
313 * Initialization of interface; set up initialization block
314 * and transmit/receive descriptor rings.
315 */
316 void
317 lance_init(sc)
318 register struct lance_softc *sc;
319 {
320 register int timo;
321 u_long a;
322
323 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
324 DELAY(100);
325
326 /* Newer LANCE chips have a reset register */
327 if (sc->sc_hwreset)
328 (*sc->sc_hwreset)(sc);
329
330 /* Set the correct byte swapping mode, etc. */
331 (*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
332
333 /* Set up LANCE init block. */
334 (*sc->sc_meminit)(sc);
335
336 /* Give LANCE the physical address of its init block. */
337 a = sc->sc_addr + LE_INITADDR(sc);
338 (*sc->sc_wrcsr)(sc, LE_CSR1, a);
339 (*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
340
341 /* Try to initialize the LANCE. */
342 DELAY(100);
343 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
344
345 /* Wait for initialization to finish. */
346 for (timo = 100000; timo; timo--)
347 if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
348 break;
349
350 if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
351 /* Start the LANCE. */
352 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT |
353 LE_C0_IDON);
354 ifp->if_flags |= IFF_RUNNING;
355 ifp->if_flags &= ~IFF_OACTIVE;
356 ifp->if_timer = 0;
357 (*sc->sc_start)(ifp);
358 } else
359 printf("%s: controller failed to initialize\n",
360 sc->sc_dev.dv_xname);
361 if (sc->sc_hwinit)
362 (*sc->sc_hwinit)(sc);
363 }
364
365 /*
366 * Routine to copy from mbuf chain to transmit buffer in
367 * network buffer memory.
368 */
369 int
370 lance_put(sc, boff, m)
371 struct lance_softc *sc;
372 int boff;
373 register struct mbuf *m;
374 {
375 register struct mbuf *n;
376 register int len, tlen = 0;
377
378 for (; m; m = n) {
379 len = m->m_len;
380 if (len == 0) {
381 MFREE(m, n);
382 continue;
383 }
384 (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
385 boff += len;
386 tlen += len;
387 MFREE(m, n);
388 }
389 if (tlen < LEMINSIZE) {
390 (*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
391 tlen = LEMINSIZE;
392 }
393 return (tlen);
394 }
395
396 /*
397 * Pull data off an interface.
398 * Len is length of data, with local net header stripped.
399 * We copy the data into mbufs. When full cluster sized units are present
400 * we copy into clusters.
401 */
402 integrate struct mbuf *
403 lance_get(sc, boff, totlen)
404 struct lance_softc *sc;
405 int boff, totlen;
406 {
407 register struct mbuf *m;
408 struct mbuf *top, **mp;
409 int len;
410
411 MGETHDR(m, M_DONTWAIT, MT_DATA);
412 if (m == 0)
413 return (0);
414 m->m_pkthdr.rcvif = ifp;
415 m->m_pkthdr.len = totlen;
416 len = MHLEN;
417 top = 0;
418 mp = ⊤
419
420 while (totlen > 0) {
421 if (top) {
422 MGET(m, M_DONTWAIT, MT_DATA);
423 if (m == 0) {
424 m_freem(top);
425 return 0;
426 }
427 len = MLEN;
428 }
429 if (totlen >= MINCLSIZE) {
430 MCLGET(m, M_DONTWAIT);
431 if ((m->m_flags & M_EXT) == 0) {
432 m_free(m);
433 m_freem(top);
434 return 0;
435 }
436 len = MCLBYTES;
437 }
438 if (!top) {
439 register int pad =
440 ALIGN(sizeof(struct ether_header)) -
441 sizeof(struct ether_header);
442 m->m_data += pad;
443 len -= pad;
444 }
445 m->m_len = len = min(totlen, len);
446 (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
447 boff += len;
448 totlen -= len;
449 *mp = m;
450 mp = &m->m_next;
451 }
452
453 return (top);
454 }
455
456 /*
457 * Pass a packet to the higher levels.
458 */
459 void
460 lance_read(sc, boff, len)
461 register struct lance_softc *sc;
462 int boff, len;
463 {
464 struct mbuf *m;
465 struct ether_header *eh;
466
467 if (len <= sizeof(struct ether_header) ||
468 len > ETHERMTU + sizeof(struct ether_header)) {
469 #ifdef LEDEBUG
470 printf("%s: invalid packet size %d; dropping\n",
471 sc->sc_dev.dv_xname, len);
472 #endif
473 ifp->if_ierrors++;
474 return;
475 }
476
477 /* Pull packet off interface. */
478 m = lance_get(sc, boff, len);
479 if (m == 0) {
480 ifp->if_ierrors++;
481 return;
482 }
483
484 ifp->if_ipackets++;
485
486 /* We assume that the header fit entirely in one mbuf. */
487 eh = mtod(m, struct ether_header *);
488
489 #if NBPFILTER > 0
490 /*
491 * Check if there's a BPF listener on this interface.
492 * If so, hand off the raw packet to BPF.
493 */
494 if (ifp->if_bpf) {
495 bpf_mtap(ifp->if_bpf, m);
496
497 #ifndef LANCE_REVC_BUG
498 /*
499 * Note that the interface cannot be in promiscuous mode if
500 * there are no BPF listeners. And if we are in promiscuous
501 * mode, we have to check if this packet is really ours.
502 */
503 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
504 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
505 ETHER_CMP(eh->ether_dhost, sc->sc_enaddr)) {
506 m_freem(m);
507 return;
508 }
509 #endif
510 }
511 #endif
512
513 #ifdef LANCE_REVC_BUG
514 /*
515 * The old LANCE (Rev. C) chips have a bug which causes
516 * garbage to be inserted in front of the received packet.
517 * The work-around is to ignore packets with an invalid
518 * destination address (garbage will usually not match).
519 * Of course, this precludes multicast support...
520 */
521 if (ETHER_CMP(eh->ether_dhost, sc->sc_enaddr) &&
522 ETHER_CMP(eh->ether_dhost, bcast_enaddr)) {
523 m_freem(m);
524 return;
525 }
526 #endif
527
528 /* Pass the packet up, with the ether header sort-of removed. */
529 m_adj(m, sizeof(struct ether_header));
530 ether_input(ifp, eh, m);
531 }
532
533 #undef ifp
534
535 void
536 lance_watchdog(ifp)
537 struct ifnet *ifp;
538 {
539 struct lance_softc *sc = ifp->if_softc;
540
541 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
542 ++ifp->if_oerrors;
543
544 lance_reset(sc);
545 }
546
547 int
548 lance_mediachange(ifp)
549 struct ifnet *ifp;
550 {
551 struct lance_softc *sc = ifp->if_softc;
552
553 if (sc->sc_mediachange)
554 return ((*sc->sc_mediachange)(sc));
555 return (EINVAL);
556 }
557
558 void
559 lance_mediastatus(ifp, ifmr)
560 struct ifnet *ifp;
561 struct ifmediareq *ifmr;
562 {
563 struct lance_softc *sc = ifp->if_softc;
564
565 if ((ifp->if_flags & IFF_UP) == 0)
566 return;
567
568 ifmr->ifm_status = IFM_AVALID;
569 if (sc->sc_havecarrier)
570 ifmr->ifm_status |= IFM_ACTIVE;
571
572 if (sc->sc_mediastatus)
573 (*sc->sc_mediastatus)(sc, ifmr);
574 }
575
576 /*
577 * Process an ioctl request.
578 */
579 int
580 lance_ioctl(ifp, cmd, data)
581 register struct ifnet *ifp;
582 u_long cmd;
583 caddr_t data;
584 {
585 register struct lance_softc *sc = ifp->if_softc;
586 struct ifaddr *ifa = (struct ifaddr *)data;
587 struct ifreq *ifr = (struct ifreq *)data;
588 int s, error = 0;
589
590 s = splnet();
591
592 switch (cmd) {
593
594 case SIOCSIFADDR:
595 ifp->if_flags |= IFF_UP;
596
597 switch (ifa->ifa_addr->sa_family) {
598 #ifdef INET
599 case AF_INET:
600 lance_init(sc);
601 arp_ifinit(ifp, ifa);
602 break;
603 #endif
604 #ifdef NS
605 case AF_NS:
606 {
607 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
608
609 if (ns_nullhost(*ina))
610 ina->x_host =
611 *(union ns_host *)LLADDR(ifp->if_sadl);
612 else {
613 bcopy(ina->x_host.c_host,
614 LLADDR(ifp->if_sadl),
615 sizeof(sc->sc_enaddr));
616 }
617 /* Set new address. */
618 lance_init(sc);
619 break;
620 }
621 #endif
622 default:
623 lance_init(sc);
624 break;
625 }
626 break;
627
628 #if defined(CCITT) && defined(LLC)
629 case SIOCSIFCONF_X25:
630 ifp->if_flags |= IFF_UP;
631 ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
632 error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
633 if (error == 0)
634 lance_init(sc);
635 break;
636 #endif /* CCITT && LLC */
637
638 case SIOCSIFFLAGS:
639 if ((ifp->if_flags & IFF_UP) == 0 &&
640 (ifp->if_flags & IFF_RUNNING) != 0) {
641 /*
642 * If interface is marked down and it is running, then
643 * stop it.
644 */
645 lance_stop(sc);
646 ifp->if_flags &= ~IFF_RUNNING;
647 } else if ((ifp->if_flags & IFF_UP) != 0 &&
648 (ifp->if_flags & IFF_RUNNING) == 0) {
649 /*
650 * If interface is marked up and it is stopped, then
651 * start it.
652 */
653 lance_init(sc);
654 } else {
655 /*
656 * Reset the interface to pick up changes in any other
657 * flags that affect hardware registers.
658 */
659 /*lance_stop(sc);*/
660 lance_init(sc);
661 }
662 #ifdef LEDEBUG
663 if (ifp->if_flags & IFF_DEBUG)
664 sc->sc_debug = 1;
665 else
666 sc->sc_debug = 0;
667 #endif
668 break;
669
670 case SIOCADDMULTI:
671 case SIOCDELMULTI:
672 error = (cmd == SIOCADDMULTI) ?
673 ether_addmulti(ifr, &sc->sc_ethercom) :
674 ether_delmulti(ifr, &sc->sc_ethercom);
675
676 if (error == ENETRESET) {
677 /*
678 * Multicast list has changed; set the hardware filter
679 * accordingly.
680 */
681 lance_reset(sc);
682 error = 0;
683 }
684 break;
685
686 case SIOCGIFMEDIA:
687 case SIOCSIFMEDIA:
688 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
689 break;
690
691 default:
692 error = EINVAL;
693 break;
694 }
695
696 splx(s);
697 return (error);
698 }
699
700 hide void
701 lance_shutdown(arg)
702 void *arg;
703 {
704
705 lance_stop((struct lance_softc *)arg);
706 }
707
708 /*
709 * Set up the logical address filter.
710 */
711 void
712 lance_setladrf(ac, af)
713 struct ethercom *ac;
714 u_int16_t *af;
715 {
716 struct ifnet *ifp = &ac->ec_if;
717 struct ether_multi *enm;
718 register u_char *cp;
719 register u_int32_t crc;
720 static const u_int32_t crctab[] = {
721 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
722 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
723 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
724 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
725 };
726 register int len;
727 struct ether_multistep step;
728
729 /*
730 * Set up multicast address filter by passing all multicast addresses
731 * through a crc generator, and then using the high order 6 bits as an
732 * index into the 64 bit logical address filter. The high order bit
733 * selects the word, while the rest of the bits select the bit within
734 * the word.
735 */
736
737 if (ifp->if_flags & IFF_PROMISC)
738 goto allmulti;
739
740 af[0] = af[1] = af[2] = af[3] = 0x0000;
741 ETHER_FIRST_MULTI(step, ac, enm);
742 while (enm != NULL) {
743 if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
744 /*
745 * We must listen to a range of multicast addresses.
746 * For now, just accept all multicasts, rather than
747 * trying to set only those filter bits needed to match
748 * the range. (At this time, the only use of address
749 * ranges is for IP multicast routing, for which the
750 * range is big enough to require all bits set.)
751 */
752 goto allmulti;
753 }
754
755 cp = enm->enm_addrlo;
756 crc = 0xffffffff;
757 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
758 crc ^= *cp++;
759 crc = (crc >> 4) ^ crctab[crc & 0xf];
760 crc = (crc >> 4) ^ crctab[crc & 0xf];
761 }
762 /* Just want the 6 most significant bits. */
763 crc >>= 26;
764
765 /* Set the corresponding bit in the filter. */
766 af[crc >> 4] |= 1 << (crc & 0xf);
767
768 ETHER_NEXT_MULTI(step, enm);
769 }
770 ifp->if_flags &= ~IFF_ALLMULTI;
771 return;
772
773 allmulti:
774 ifp->if_flags |= IFF_ALLMULTI;
775 af[0] = af[1] = af[2] = af[3] = 0xffff;
776 }
777
778 /*
779 * Routines for accessing the transmit and receive buffers.
780 * The various CPU and adapter configurations supported by this
781 * driver require three different access methods for buffers
782 * and descriptors:
783 * (1) contig (contiguous data; no padding),
784 * (2) gap2 (two bytes of data followed by two bytes of padding),
785 * (3) gap16 (16 bytes of data followed by 16 bytes of padding).
786 */
787
788 /*
789 * contig: contiguous data with no padding.
790 *
791 * Buffers may have any alignment.
792 */
793
794 void
795 lance_copytobuf_contig(sc, from, boff, len)
796 struct lance_softc *sc;
797 void *from;
798 int boff, len;
799 {
800 volatile caddr_t buf = sc->sc_mem;
801
802 /*
803 * Just call bcopy() to do the work.
804 */
805 bcopy(from, buf + boff, len);
806 }
807
808 void
809 lance_copyfrombuf_contig(sc, to, boff, len)
810 struct lance_softc *sc;
811 void *to;
812 int boff, len;
813 {
814 volatile caddr_t buf = sc->sc_mem;
815
816 /*
817 * Just call bcopy() to do the work.
818 */
819 bcopy(buf + boff, to, len);
820 }
821
822 void
823 lance_zerobuf_contig(sc, boff, len)
824 struct lance_softc *sc;
825 int boff, len;
826 {
827 volatile caddr_t buf = sc->sc_mem;
828
829 /*
830 * Just let bzero() do the work
831 */
832 bzero(buf + boff, len);
833 }
834
835 #if 0
836 /*
837 * Examples only; duplicate these and tweak (if necessary) in
838 * machine-specific front-ends.
839 */
840
841 /*
842 * gap2: two bytes of data followed by two bytes of pad.
843 *
844 * Buffers must be 4-byte aligned. The code doesn't worry about
845 * doing an extra byte.
846 */
847
848 void
849 lance_copytobuf_gap2(sc, fromv, boff, len)
850 struct lance_softc *sc;
851 void *fromv;
852 int boff;
853 register int len;
854 {
855 volatile caddr_t buf = sc->sc_mem;
856 register caddr_t from = fromv;
857 register volatile u_int16_t *bptr;
858
859 if (boff & 0x1) {
860 /* handle unaligned first byte */
861 bptr = ((volatile u_int16_t *)buf) + (boff - 1);
862 *bptr = (*from++ << 8) | (*bptr & 0xff);
863 bptr += 2;
864 len--;
865 } else
866 bptr = ((volatile u_int16_t *)buf) + boff;
867 while (len > 1) {
868 *bptr = (from[1] << 8) | (from[0] & 0xff);
869 bptr += 2;
870 from += 2;
871 len -= 2;
872 }
873 if (len == 1)
874 *bptr = (u_int16_t)*from;
875 }
876
877 void
878 lance_copyfrombuf_gap2(sc, tov, boff, len)
879 struct lance_softc *sc;
880 void *tov;
881 int boff, len;
882 {
883 volatile caddr_t buf = sc->sc_mem;
884 register caddr_t to = tov;
885 register volatile u_int16_t *bptr;
886 register u_int16_t tmp;
887
888 if (boff & 0x1) {
889 /* handle unaligned first byte */
890 bptr = ((volatile u_int16_t *)buf) + (boff - 1);
891 *to++ = (*bptr >> 8) & 0xff;
892 bptr += 2;
893 len--;
894 } else
895 bptr = ((volatile u_int16_t *)buf) + boff;
896 while (len > 1) {
897 tmp = *bptr;
898 *to++ = tmp & 0xff;
899 *to++ = (tmp >> 8) & 0xff;
900 bptr += 2;
901 len -= 2;
902 }
903 if (len == 1)
904 *to = *bptr & 0xff;
905 }
906
907 void
908 lance_zerobuf_gap2(sc, boff, len)
909 struct lance_softc *sc;
910 int boff, len;
911 {
912 volatile caddr_t buf = sc->sc_mem;
913 register volatile u_int16_t *bptr;
914
915 if ((unsigned)boff & 0x1) {
916 bptr = ((volatile u_int16_t *)buf) + (boff - 1);
917 *bptr &= 0xff;
918 bptr += 2;
919 len--;
920 } else
921 bptr = ((volatile u_int16_t *)buf) + boff;
922 while (len > 0) {
923 *bptr = 0;
924 bptr += 2;
925 len -= 2;
926 }
927 }
928
929 /*
930 * gap16: 16 bytes of data followed by 16 bytes of pad.
931 *
932 * Buffers must be 32-byte aligned.
933 */
934
935 void
936 lance_copytobuf_gap16(sc, fromv, boff, len)
937 struct lance_softc *sc;
938 void *fromv;
939 int boff;
940 register int len;
941 {
942 volatile caddr_t buf = sc->sc_mem;
943 register caddr_t from = fromv;
944 register caddr_t bptr;
945 register int xfer;
946
947 bptr = buf + ((boff << 1) & ~0x1f);
948 boff &= 0xf;
949 xfer = min(len, 16 - boff);
950 while (len > 0) {
951 bcopy(from, bptr + boff, xfer);
952 from += xfer;
953 bptr += 32;
954 boff = 0;
955 len -= xfer;
956 xfer = min(len, 16);
957 }
958 }
959
960 void
961 lance_copyfrombuf_gap16(sc, tov, boff, len)
962 struct lance_softc *sc;
963 void *tov;
964 int boff, len;
965 {
966 volatile caddr_t buf = sc->sc_mem;
967 register caddr_t to = tov;
968 register caddr_t bptr;
969 register int xfer;
970
971 bptr = buf + ((boff << 1) & ~0x1f);
972 boff &= 0xf;
973 xfer = min(len, 16 - boff);
974 while (len > 0) {
975 bcopy(bptr + boff, to, xfer);
976 to += xfer;
977 bptr += 32;
978 boff = 0;
979 len -= xfer;
980 xfer = min(len, 16);
981 }
982 }
983
984 void
985 lance_zerobuf_gap16(sc, boff, len)
986 struct lance_softc *sc;
987 int boff, len;
988 {
989 volatile caddr_t buf = sc->sc_mem;
990 register caddr_t bptr;
991 register int xfer;
992
993 bptr = buf + ((boff << 1) & ~0x1f);
994 boff &= 0xf;
995 xfer = min(len, 16 - boff);
996 while (len > 0) {
997 bzero(bptr + boff, xfer);
998 bptr += 32;
999 boff = 0;
1000 len -= xfer;
1001 xfer = min(len, 16);
1002 }
1003 }
1004 #endif /* Example only */
1005