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