1 /* $NetBSD: lance.c,v 1.42 2008/11/07 00:20:02 dyoung Exp $ */
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1992, 1993
35 * The Regents of the University of California. All rights reserved.
37 * This code is derived from software contributed to Berkeley by
38 * Ralph Campbell and Rick Macklem.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: lance.c,v 1.42 2008/11/07 00:20:02 dyoung Exp $");
73 #include <sys/param.h>
74 #include <sys/systm.h>
76 #include <sys/syslog.h>
77 #include <sys/socket.h>
78 #include <sys/device.h>
79 #include <sys/malloc.h>
80 #include <sys/ioctl.h>
81 #include <sys/errno.h>
87 #include <net/if_dl.h>
88 #include <net/if_ether.h>
89 #include <net/if_media.h>
94 #include <net/bpfdesc.h>
97 #include <dev/ic/lancereg.h>
98 #include <dev/ic/lancevar.h>
100 #if defined(_KERNEL_OPT)
108 #define integrate static inline
112 integrate
struct mbuf
*lance_get(struct lance_softc
*, int, int);
114 hide
bool lance_shutdown(device_t
, int);
116 int lance_mediachange(struct ifnet
*);
117 void lance_mediastatus(struct ifnet
*, struct ifmediareq
*);
119 static inline u_int16_t
ether_cmp(void *, void *);
121 void lance_stop(struct ifnet
*, int);
122 int lance_ioctl(struct ifnet
*, u_long
, void *);
123 void lance_watchdog(struct ifnet
*);
126 * Compare two Ether/802 addresses for equality, inlined and
127 * unrolled for speed. Use this like memcmp().
129 * XXX: Add <machine/inlines.h> for stuff like this?
130 * XXX: or maybe add it to libkern.h instead?
132 * "I'd love to have an inline assembler version of this."
133 * XXX: Who wanted that? mycroft? I wrote one, but this
134 * version in C is as good as hand-coded assembly. -gwr
136 * Please do NOT tweak this without looking at the actual
137 * assembly code generated before and after your tweaks!
139 static inline uint16_t
140 ether_cmp(void *one
, void *two
)
142 uint16_t *a
= (uint16_t *)one
;
143 uint16_t *b
= (uint16_t *)two
;
148 * The post-increment-pointer form produces the best
149 * machine code for m68k. This was carefully tuned
150 * so it compiles to just 8 short (2-byte) op-codes!
157 * Most modern CPUs do better with a single expresion.
158 * Note that short-cut evaluation is NOT helpful here,
159 * because it just makes the code longer, not faster!
161 diff
= (a
[0] - b
[0]) | (a
[1] - b
[1]) | (a
[2] - b
[2]);
167 #define ETHER_CMP ether_cmp
169 #ifdef LANCE_REVC_BUG
170 /* Make sure this is short-aligned, for ether_cmp(). */
171 static uint16_t bcast_enaddr
[3] = { ~0, ~0, ~0 };
175 lance_config(struct lance_softc
*sc
)
178 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
180 /* Initialize ifnet structure. */
181 strcpy(ifp
->if_xname
, device_xname(sc
->sc_dev
));
183 ifp
->if_start
= sc
->sc_start
;
184 ifp
->if_ioctl
= lance_ioctl
;
185 ifp
->if_watchdog
= lance_watchdog
;
186 ifp
->if_init
= lance_init
;
187 ifp
->if_stop
= lance_stop
;
189 IFF_BROADCAST
| IFF_SIMPLEX
| IFF_NOTRAILERS
| IFF_MULTICAST
;
190 #ifdef LANCE_REVC_BUG
191 ifp
->if_flags
&= ~IFF_MULTICAST
;
193 IFQ_SET_READY(&ifp
->if_snd
);
195 /* Initialize ifmedia structures. */
196 ifmedia_init(&sc
->sc_media
, 0, lance_mediachange
, lance_mediastatus
);
197 if (sc
->sc_supmedia
!= NULL
) {
198 for (i
= 0; i
< sc
->sc_nsupmedia
; i
++)
199 ifmedia_add(&sc
->sc_media
, sc
->sc_supmedia
[i
],
201 ifmedia_set(&sc
->sc_media
, sc
->sc_defaultmedia
);
203 ifmedia_add(&sc
->sc_media
, IFM_ETHER
|IFM_MANUAL
, 0, NULL
);
204 ifmedia_set(&sc
->sc_media
, IFM_ETHER
|IFM_MANUAL
);
207 switch (sc
->sc_memsize
) {
233 /* weird memory size; cope with it */
234 nbuf
= sc
->sc_memsize
/ LEBLEN
;
235 sc
->sc_ntbuf
= nbuf
/ 5;
236 sc
->sc_nrbuf
= nbuf
- sc
->sc_ntbuf
;
239 aprint_normal(": address %s\n", ether_sprintf(sc
->sc_enaddr
));
240 aprint_normal_dev(sc
->sc_dev
,
241 "%d receive buffers, %d transmit buffers\n",
242 sc
->sc_nrbuf
, sc
->sc_ntbuf
);
244 /* Make sure the chip is stopped. */
247 /* claim 802.1q capability */
248 sc
->sc_ethercom
.ec_capabilities
|= ETHERCAP_VLAN_MTU
;
249 /* Attach the interface. */
251 ether_ifattach(ifp
, sc
->sc_enaddr
);
253 if (pmf_device_register1(sc
->sc_dev
, NULL
, NULL
, lance_shutdown
))
254 pmf_class_network_register(sc
->sc_dev
, ifp
);
256 aprint_error_dev(sc
->sc_dev
,
257 "couldn't establish power handler\n");
259 sc
->sc_rbufaddr
= malloc(sc
->sc_nrbuf
* sizeof(int), M_DEVBUF
,
261 sc
->sc_tbufaddr
= malloc(sc
->sc_ntbuf
* sizeof(int), M_DEVBUF
,
265 rnd_attach_source(&sc
->rnd_source
, device_xname(sc
->sc_dev
),
271 lance_reset(struct lance_softc
*sc
)
276 lance_init(&sc
->sc_ethercom
.ec_if
);
281 lance_stop(struct ifnet
*ifp
, int disable
)
283 struct lance_softc
*sc
= ifp
->if_softc
;
285 (*sc
->sc_wrcsr
)(sc
, LE_CSR0
, LE_C0_STOP
);
289 * Initialization of interface; set up initialization block
290 * and transmit/receive descriptor rings.
293 lance_init(struct ifnet
*ifp
)
295 struct lance_softc
*sc
= ifp
->if_softc
;
299 (*sc
->sc_wrcsr
)(sc
, LE_CSR0
, LE_C0_STOP
);
302 /* Newer LANCE chips have a reset register */
304 (*sc
->sc_hwreset
)(sc
);
306 /* Set the correct byte swapping mode, etc. */
307 (*sc
->sc_wrcsr
)(sc
, LE_CSR3
, sc
->sc_conf3
);
309 /* Set up LANCE init block. */
310 (*sc
->sc_meminit
)(sc
);
312 /* Give LANCE the physical address of its init block. */
313 a
= sc
->sc_addr
+ LE_INITADDR(sc
);
314 (*sc
->sc_wrcsr
)(sc
, LE_CSR1
, a
);
315 (*sc
->sc_wrcsr
)(sc
, LE_CSR2
, a
>> 16);
317 /* Try to initialize the LANCE. */
319 (*sc
->sc_wrcsr
)(sc
, LE_CSR0
, LE_C0_INIT
);
321 /* Wait for initialization to finish. */
322 for (timo
= 100000; timo
; timo
--)
323 if ((*sc
->sc_rdcsr
)(sc
, LE_CSR0
) & LE_C0_IDON
)
326 if ((*sc
->sc_rdcsr
)(sc
, LE_CSR0
) & LE_C0_IDON
) {
327 /* Start the LANCE. */
328 (*sc
->sc_wrcsr
)(sc
, LE_CSR0
, LE_C0_INEA
| LE_C0_STRT
);
329 ifp
->if_flags
|= IFF_RUNNING
;
330 ifp
->if_flags
&= ~IFF_OACTIVE
;
332 (*sc
->sc_start
)(ifp
);
334 printf("%s: controller failed to initialize\n",
335 device_xname(sc
->sc_dev
));
337 (*sc
->sc_hwinit
)(sc
);
343 * Routine to copy from mbuf chain to transmit buffer in
344 * network buffer memory.
347 lance_put(struct lance_softc
*sc
, int boff
, struct mbuf
*m
)
358 (*sc
->sc_copytobuf
)(sc
, mtod(m
, void *), boff
, len
);
363 if (tlen
< LEMINSIZE
) {
364 (*sc
->sc_zerobuf
)(sc
, boff
, LEMINSIZE
- tlen
);
371 * Pull data off an interface.
372 * Len is length of data, with local net header stripped.
373 * We copy the data into mbufs. When full cluster sized units are present
374 * we copy into clusters.
376 integrate
struct mbuf
*
377 lance_get(struct lance_softc
*sc
, int boff
, int totlen
)
379 struct mbuf
*m
, *m0
, *newm
;
382 MGETHDR(m0
, M_DONTWAIT
, MT_DATA
);
385 m0
->m_pkthdr
.rcvif
= &sc
->sc_ethercom
.ec_if
;
386 m0
->m_pkthdr
.len
= totlen
;
391 if (totlen
>= MINCLSIZE
) {
392 MCLGET(m
, M_DONTWAIT
);
393 if ((m
->m_flags
& M_EXT
) == 0)
399 char *newdata
= (char *)
400 ALIGN(m
->m_data
+ sizeof(struct ether_header
)) -
401 sizeof(struct ether_header
);
402 len
-= newdata
- m
->m_data
;
406 m
->m_len
= len
= min(totlen
, len
);
407 (*sc
->sc_copyfrombuf
)(sc
, mtod(m
, void *), boff
, len
);
412 MGET(newm
, M_DONTWAIT
, MT_DATA
);
416 m
= m
->m_next
= newm
;
428 * Pass a packet to the higher levels.
431 lance_read(struct lance_softc
*sc
, int boff
, int len
)
434 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
435 struct ether_header
*eh
;
437 if (len
<= sizeof(struct ether_header
) ||
438 len
> ((sc
->sc_ethercom
.ec_capenable
& ETHERCAP_VLAN_MTU
) ?
439 ETHER_VLAN_ENCAP_LEN
+ ETHERMTU
+ sizeof(struct ether_header
) :
440 ETHERMTU
+ sizeof(struct ether_header
))) {
442 printf("%s: invalid packet size %d; dropping\n",
443 device_xname(sc
->sc_dev
), len
);
449 /* Pull packet off interface. */
450 m
= lance_get(sc
, boff
, len
);
458 eh
= mtod(m
, struct ether_header
*);
460 #ifdef LANCE_REVC_BUG
462 * The old LANCE (Rev. C) chips have a bug which causes
463 * garbage to be inserted in front of the received packet.
464 * The work-around is to ignore packets with an invalid
465 * destination address (garbage will usually not match).
466 * Of course, this precludes multicast support...
468 if (ETHER_CMP(eh
->ether_dhost
, sc
->sc_enaddr
) &&
469 ETHER_CMP(eh
->ether_dhost
, bcast_enaddr
)) {
476 * Some lance device does not present IFF_SIMPLEX behavior on multicast
477 * packets. Make sure to drop it if it is from ourselves.
479 if (!ETHER_CMP(eh
->ether_shost
, sc
->sc_enaddr
)) {
486 * Check if there's a BPF listener on this interface.
487 * If so, hand off the raw packet to BPF.
490 bpf_mtap(ifp
->if_bpf
, m
);
493 /* Pass the packet up. */
494 (*ifp
->if_input
)(ifp
, m
);
500 lance_watchdog(struct ifnet
*ifp
)
502 struct lance_softc
*sc
= ifp
->if_softc
;
504 log(LOG_ERR
, "%s: device timeout\n", device_xname(sc
->sc_dev
));
511 lance_mediachange(struct ifnet
*ifp
)
513 struct lance_softc
*sc
= ifp
->if_softc
;
515 if (sc
->sc_mediachange
)
516 return ((*sc
->sc_mediachange
)(sc
));
521 lance_mediastatus(struct ifnet
*ifp
, struct ifmediareq
*ifmr
)
523 struct lance_softc
*sc
= ifp
->if_softc
;
525 if ((ifp
->if_flags
& IFF_UP
) == 0)
528 ifmr
->ifm_status
= IFM_AVALID
;
529 if (sc
->sc_havecarrier
)
530 ifmr
->ifm_status
|= IFM_ACTIVE
;
532 if (sc
->sc_mediastatus
)
533 (*sc
->sc_mediastatus
)(sc
, ifmr
);
537 * Process an ioctl request.
540 lance_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
542 struct lance_softc
*sc
= ifp
->if_softc
;
543 struct ifreq
*ifr
= (struct ifreq
*)data
;
551 error
= ifmedia_ioctl(ifp
, ifr
, &sc
->sc_media
, cmd
);
554 if ((error
= ether_ioctl(ifp
, cmd
, data
)) != ENETRESET
)
557 if (cmd
!= SIOCADDMULTI
&& cmd
!= SIOCDELMULTI
)
559 if (ifp
->if_flags
& IFF_RUNNING
) {
561 * Multicast list has changed; set the hardware filter
575 lance_shutdown(device_t self
, int howto
)
577 struct lance_softc
*sc
= device_private(self
);
578 struct ifnet
*ifp
= &sc
->sc_ethercom
.ec_if
;
586 * Set up the logical address filter.
589 lance_setladrf(struct ethercom
*ac
, uint16_t *af
)
591 struct ifnet
*ifp
= &ac
->ec_if
;
592 struct ether_multi
*enm
;
594 struct ether_multistep step
;
597 * Set up multicast address filter by passing all multicast addresses
598 * through a crc generator, and then using the high order 6 bits as an
599 * index into the 64 bit logical address filter. The high order bit
600 * selects the word, while the rest of the bits select the bit within
604 if (ifp
->if_flags
& IFF_PROMISC
)
607 af
[0] = af
[1] = af
[2] = af
[3] = 0x0000;
608 ETHER_FIRST_MULTI(step
, ac
, enm
);
609 while (enm
!= NULL
) {
610 if (ETHER_CMP(enm
->enm_addrlo
, enm
->enm_addrhi
)) {
612 * We must listen to a range of multicast addresses.
613 * For now, just accept all multicasts, rather than
614 * trying to set only those filter bits needed to match
615 * the range. (At this time, the only use of address
616 * ranges is for IP multicast routing, for which the
617 * range is big enough to require all bits set.)
622 crc
= ether_crc32_le(enm
->enm_addrlo
, ETHER_ADDR_LEN
);
624 /* Just want the 6 most significant bits. */
627 /* Set the corresponding bit in the filter. */
628 af
[crc
>> 4] |= 1 << (crc
& 0xf);
630 ETHER_NEXT_MULTI(step
, enm
);
632 ifp
->if_flags
&= ~IFF_ALLMULTI
;
636 ifp
->if_flags
|= IFF_ALLMULTI
;
637 af
[0] = af
[1] = af
[2] = af
[3] = 0xffff;
641 * Routines for accessing the transmit and receive buffers.
642 * The various CPU and adapter configurations supported by this
643 * driver require three different access methods for buffers
645 * (1) contig (contiguous data; no padding),
646 * (2) gap2 (two bytes of data followed by two bytes of padding),
647 * (3) gap16 (16 bytes of data followed by 16 bytes of padding).
651 * contig: contiguous data with no padding.
653 * Buffers may have any alignment.
657 lance_copytobuf_contig(struct lance_softc
*sc
, void *from
, int boff
, int len
)
659 uint8_t *buf
= sc
->sc_mem
;
662 * Just call memcpy() to do the work.
664 memcpy(buf
+ boff
, from
, len
);
668 lance_copyfrombuf_contig(struct lance_softc
*sc
, void *to
, int boff
, int len
)
670 uint8_t *buf
= sc
->sc_mem
;
673 * Just call memcpy() to do the work.
675 memcpy(to
, buf
+ boff
, len
);
679 lance_zerobuf_contig(struct lance_softc
*sc
, int boff
, int len
)
681 uint8_t *buf
= sc
->sc_mem
;
684 * Just let memset() do the work
686 memset(buf
+ boff
, 0, len
);
691 * Examples only; duplicate these and tweak (if necessary) in
692 * machine-specific front-ends.
696 * gap2: two bytes of data followed by two bytes of pad.
698 * Buffers must be 4-byte aligned. The code doesn't worry about
699 * doing an extra byte.
703 lance_copytobuf_gap2(struct lance_softc
*sc
, void *fromv
, int boff
, int len
)
705 volatile void *buf
= sc
->sc_mem
;
707 volatile uint16_t *bptr
;
710 /* handle unaligned first byte */
711 bptr
= ((volatile uint16_t *)buf
) + (boff
- 1);
712 *bptr
= (*from
++ << 8) | (*bptr
& 0xff);
716 bptr
= ((volatile uint16_t *)buf
) + boff
;
718 *bptr
= (from
[1] << 8) | (from
[0] & 0xff);
724 *bptr
= (uint16_t)*from
;
728 lance_copyfrombuf_gap2(struct lance_softc
*sc
, void *tov
, int boff
, int len
)
730 volatile void *buf
= sc
->sc_mem
;
732 volatile uint16_t *bptr
;
736 /* handle unaligned first byte */
737 bptr
= ((volatile uint16_t *)buf
) + (boff
- 1);
738 *to
++ = (*bptr
>> 8) & 0xff;
742 bptr
= ((volatile uint16_t *)buf
) + boff
;
746 *to
++ = (tmp
>> 8) & 0xff;
755 lance_zerobuf_gap2(struct lance_softc
*sc
, int boff
, int len
)
757 volatile void *buf
= sc
->sc_mem
;
758 volatile uint16_t *bptr
;
760 if ((unsigned int)boff
& 0x1) {
761 bptr
= ((volatile uint16_t *)buf
) + (boff
- 1);
766 bptr
= ((volatile uint16_t *)buf
) + boff
;
775 * gap16: 16 bytes of data followed by 16 bytes of pad.
777 * Buffers must be 32-byte aligned.
781 lance_copytobuf_gap16(struct lance_softc
*sc
, void *fromv
, int boff
, int len
)
783 volatile uint8_t *buf
= sc
->sc_mem
;
788 bptr
= buf
+ ((boff
<< 1) & ~0x1f);
790 xfer
= min(len
, 16 - boff
);
792 memcpy(bptr
+ boff
, from
, xfer
);
802 lance_copyfrombuf_gap16(struct lance_softc
*sc
, void *tov
, int boff
, int len
)
804 volatile uint8_t *buf
= sc
->sc_mem
;
809 bptr
= buf
+ ((boff
<< 1) & ~0x1f);
811 xfer
= min(len
, 16 - boff
);
813 memcpy(to
, bptr
+ boff
, xfer
);
823 lance_zerobuf_gap16(struct lance_softc
*sc
, int boff
, int len
)
825 volatile uint8_t *buf
= sc
->sc_mem
;
829 bptr
= buf
+ ((boff
<< 1) & ~0x1f);
831 xfer
= min(len
, 16 - boff
);
833 memset(bptr
+ boff
, 0, xfer
);
840 #endif /* Example only */