1 /* $NetBSD: lance.c,v 1.34 2005/12/24 20:27:30 perry Exp $ */
2 /* $FreeBSD: src/sys/dev/le/lance.c,v 1.2 2006/05/16 21:04:01 marius Exp $ */
3 /* $DragonFly: src/sys/dev/netif/lnc/lance.c,v 1.7 2008/05/14 11:59:20 sephe Exp $ */
7 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
12 * Simulation Facility, NASA Ames Research Center.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
44 * Copyright (c) 1992, 1993
45 * The Regents of the University of California. All rights reserved.
47 * This code is derived from software contributed to Berkeley by
48 * Ralph Campbell and Rick Macklem.
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
53 * 1. Redistributions of source code must retain the above copyright
54 * notice, this list of conditions and the following disclaimer.
55 * 2. Redistributions in binary form must reproduce the above copyright
56 * notice, this list of conditions and the following disclaimer in the
57 * documentation and/or other materials provided with the distribution.
58 * 3. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
77 #include <sys/param.h>
79 #include <sys/endian.h>
82 #include <sys/socket.h>
83 #include <sys/sockio.h>
85 #include <net/ethernet.h>
87 #include <net/ifq_var.h>
88 #include <net/if_arp.h>
89 #include <net/if_dl.h>
90 #include <net/if_media.h>
91 #include <net/if_types.h>
92 #include <net/vlan/if_vlan_var.h>
94 #include <dev/netif/lnc/lancereg.h>
95 #include <dev/netif/lnc/lancevar.h>
97 devclass_t le_devclass;
99 static void lance_start(struct ifnet *, struct ifaltq_subque *);
100 static void lance_init(void *);
101 static void lance_watchdog(struct ifnet *);
102 static int lance_mediachange(struct ifnet *);
103 static void lance_mediastatus(struct ifnet *, struct ifmediareq *);
104 static int lance_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
107 lance_config(struct lance_softc *sc, const char* name, int unit)
112 ifp = sc->ifp = &sc->sc_if;
114 /* Initialize ifnet structure. */
116 if_initname(ifp, name, unit);
117 ifp->if_start = lance_start;
118 ifp->if_ioctl = lance_ioctl;
119 ifp->if_watchdog = lance_watchdog;
120 ifp->if_init = lance_init;
121 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
122 #ifdef LANCE_REVC_BUG
123 ifp->if_flags &= ~IFF_MULTICAST;
125 ifp->if_baudrate = IF_Mbps(10);
126 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
127 ifq_set_ready(&ifp->if_snd);
129 /* Attach the interface. */
130 ether_ifattach(ifp, sc->sc_enaddr, NULL);
132 /* Claim 802.1q capability. */
133 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
134 ifp->if_capabilities |= IFCAP_VLAN_MTU;
135 ifp->if_capenable |= IFCAP_VLAN_MTU;
137 /* Initialize ifmedia structures. */
138 ifmedia_init(&sc->sc_media, 0, lance_mediachange, lance_mediastatus);
139 if (sc->sc_supmedia != NULL) {
140 for (i = 0; i < sc->sc_nsupmedia; i++)
141 ifmedia_add(&sc->sc_media, sc->sc_supmedia[i], 0, NULL);
142 ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
144 ifmedia_add(&sc->sc_media,
145 IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0), 0, NULL);
146 ifmedia_set(&sc->sc_media,
147 IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0));
150 switch (sc->sc_memsize) {
176 /* weird memory size; cope with it */
177 nbuf = sc->sc_memsize / LEBLEN;
178 sc->sc_ntbuf = nbuf / 5;
179 sc->sc_nrbuf = nbuf - sc->sc_ntbuf;
182 if_printf(ifp, "%d receive buffers, %d transmit buffers\n",
183 sc->sc_nrbuf, sc->sc_ntbuf);
185 /* Make sure the chip is stopped. */
190 lance_suspend(struct lance_softc *sc)
193 lwkt_serialize_enter(sc->ifp->if_serializer);
195 lwkt_serialize_exit(sc->ifp->if_serializer);
199 lance_resume(struct lance_softc *sc)
202 lwkt_serialize_enter(sc->ifp->if_serializer);
203 if (sc->ifp->if_flags & IFF_UP)
204 lance_init_locked(sc);
205 lwkt_serialize_exit(sc->ifp->if_serializer);
209 lance_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
211 struct lance_softc *sc = ifp->if_softc;
213 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
214 (*sc->sc_start_locked)(sc);
218 lance_stop(struct lance_softc *sc)
220 struct ifnet *ifp = sc->ifp;
223 * Mark the interface down and cancel the watchdog timer.
225 ifp->if_flags &= ~IFF_RUNNING;
226 ifq_clr_oactive(&ifp->if_snd);
229 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
233 lance_init(void *xsc)
235 struct lance_softc *sc = (struct lance_softc *)xsc;
238 lance_init_locked(sc);
243 * Initialization of interface; set up initialization block
244 * and transmit/receive descriptor rings.
247 lance_init_locked(struct lance_softc *sc)
249 struct ifnet *ifp = sc->ifp;
253 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
256 /* Newer LANCE chips have a reset register. */
258 (*sc->sc_hwreset)(sc);
260 /* Set the correct byte swapping mode, etc. */
261 (*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
264 * Update our private copy of the Ethernet address.
265 * We NEED the copy so we can ensure its alignment!
267 memcpy(sc->sc_enaddr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
269 /* Set up LANCE init block. */
270 (*sc->sc_meminit)(sc);
272 /* Give LANCE the physical address of its init block. */
273 a = sc->sc_addr + LE_INITADDR(sc);
274 (*sc->sc_wrcsr)(sc, LE_CSR1, a & 0xffff);
275 (*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
277 /* Try to initialize the LANCE. */
279 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
281 /* Wait for initialization to finish. */
282 for (timo = 100000; timo; timo--)
283 if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
286 /* Set the current media. */
287 if (sc->sc_mediachange)
288 (*sc->sc_mediachange)(sc);
290 if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
291 /* Start the LANCE. */
292 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT);
293 ifp->if_flags |= IFF_RUNNING;
294 ifq_clr_oactive(&ifp->if_snd);
298 if_printf(ifp, "controller failed to initialize\n");
301 (*sc->sc_hwinit)(sc);
305 * Routine to copy from mbuf chain to transmit buffer in
306 * network buffer memory.
309 lance_put(struct lance_softc *sc, int boff, struct mbuf *m)
313 for (; m; m = m_free(m)) {
316 (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, m->m_len);
320 if (tlen < LEMINSIZE) {
321 (*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
328 * Pull data off an interface.
329 * Len is length of data, with local net header stripped.
330 * We copy the data into mbufs. When full cluster sized units are present
331 * we copy into clusters.
334 lance_get(struct lance_softc *sc, int boff, int totlen)
336 struct ifnet *ifp = sc->ifp;
337 struct mbuf *m0 = NULL, *newm;
338 struct mbuf *m = NULL;
341 if (totlen <= ETHER_HDR_LEN || totlen > LEBLEN - ETHER_CRC_LEN) {
343 if_printf(ifp, "invalid packet size %d; dropping\n", totlen);
349 newm = m_getl(totlen, MB_DONTWAIT, MT_DATA,
350 m0 == NULL ? M_PKTHDR : 0, &mlen);
358 m0->m_pkthdr.rcvif = ifp;
359 m0->m_pkthdr.len = totlen;
361 ALIGN(m0->m_data + ETHER_HDR_LEN) - ETHER_HDR_LEN;
362 mlen -= newdata - m0->m_data;
363 m0->m_data = newdata;
369 m->m_len = min(totlen, mlen);
370 (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, m->m_len);
373 } while (totlen > 0);
383 lance_watchdog(struct ifnet *ifp)
385 struct lance_softc *sc = ifp->if_softc;
387 if_printf(ifp, "device timeout\n");
388 IFNET_STAT_INC(ifp, oerrors, 1);
389 lance_init_locked(sc);
393 lance_mediachange(struct ifnet *ifp)
395 struct lance_softc *sc = ifp->if_softc;
398 if (sc->sc_mediachange) {
399 error = (*sc->sc_mediachange)(sc);
406 lance_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
408 struct lance_softc *sc = ifp->if_softc;
410 if (!(ifp->if_flags & IFF_UP)) {
414 ifmr->ifm_status = IFM_AVALID;
415 if (sc->sc_flags & LE_CARRIER)
416 ifmr->ifm_status |= IFM_ACTIVE;
418 if (sc->sc_mediastatus)
419 (*sc->sc_mediastatus)(sc, ifmr);
423 * Process an ioctl request.
426 lance_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
428 struct lance_softc *sc = ifp->if_softc;
429 struct ifreq *ifr = (struct ifreq *)data;
436 if (ifp->if_flags & IFF_PROMISC) {
437 if (!(sc->sc_flags & LE_PROMISC)) {
438 sc->sc_flags |= LE_PROMISC;
439 lance_init_locked(sc);
441 } else if (sc->sc_flags & LE_PROMISC) {
442 sc->sc_flags &= ~LE_PROMISC;
443 lance_init_locked(sc);
446 if ((ifp->if_flags & IFF_ALLMULTI) &&
447 !(sc->sc_flags & LE_ALLMULTI)) {
448 sc->sc_flags |= LE_ALLMULTI;
449 lance_init_locked(sc);
450 } else if (!(ifp->if_flags & IFF_ALLMULTI) &&
451 (sc->sc_flags & LE_ALLMULTI)) {
452 sc->sc_flags &= ~LE_ALLMULTI;
453 lance_init_locked(sc);
456 if (!(ifp->if_flags & IFF_UP) &&
457 ifp->if_flags & IFF_RUNNING) {
459 * If interface is marked down and it is running, then
463 } else if (ifp->if_flags & IFF_UP &&
464 !(ifp->if_flags & IFF_RUNNING)) {
466 * If interface is marked up and it is stopped, then
469 lance_init_locked(sc);
472 if (ifp->if_flags & IFF_DEBUG)
473 sc->sc_flags |= LE_DEBUG;
475 sc->sc_flags &= ~LE_DEBUG;
482 * Multicast list has changed; set the hardware filter
485 if (ifp->if_flags & IFF_RUNNING)
486 lance_init_locked(sc);
491 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
495 error = ether_ioctl(ifp, cmd, data);
505 * Set up the logical address filter.
508 lance_setladrf(struct lance_softc *sc, uint16_t *af)
510 struct ifnet *ifp = sc->ifp;
511 struct ifmultiaddr *ifma;
515 * Set up multicast address filter by passing all multicast addresses
516 * through a crc generator, and then using the high order 6 bits as an
517 * index into the 64 bit logical address filter. The high order bit
518 * selects the word, while the rest of the bits select the bit within
522 if (ifp->if_flags & IFF_PROMISC || sc->sc_flags & LE_ALLMULTI) {
523 af[0] = af[1] = af[2] = af[3] = 0xffff;
527 af[0] = af[1] = af[2] = af[3] = 0x0000;
529 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
530 if (ifma->ifma_addr->sa_family != AF_LINK)
533 crc = ether_crc32_le(LLADDR((struct sockaddr_dl *)
534 ifma->ifma_addr), ETHER_ADDR_LEN);
536 /* Just want the 6 most significant bits. */
539 /* Set the corresponding bit in the filter. */
540 af[crc >> 4] |= LE_HTOLE16(1 << (crc & 0xf));
545 * Routines for accessing the transmit and receive buffers.
546 * The various CPU and adapter configurations supported by this
547 * driver require three different access methods for buffers
549 * (1) contig (contiguous data; no padding),
550 * (2) gap2 (two bytes of data followed by two bytes of padding),
551 * (3) gap16 (16 bytes of data followed by 16 bytes of padding).
555 * contig: contiguous data with no padding.
557 * Buffers may have any alignment.
561 lance_copytobuf_contig(struct lance_softc *sc, void *from, int boff, int len)
563 volatile caddr_t buf = sc->sc_mem;
566 * Just call memcpy() to do the work.
568 memcpy(buf + boff, from, len);
572 lance_copyfrombuf_contig(struct lance_softc *sc, void *to, int boff, int len)
574 volatile caddr_t buf = sc->sc_mem;
577 * Just call memcpy() to do the work.
579 memcpy(to, buf + boff, len);
583 lance_zerobuf_contig(struct lance_softc *sc, int boff, int len)
585 volatile caddr_t buf = sc->sc_mem;
588 * Just let memset() do the work
590 memset(buf + boff, 0, len);
595 * Examples only; duplicate these and tweak (if necessary) in
596 * machine-specific front-ends.
600 * gap2: two bytes of data followed by two bytes of pad.
602 * Buffers must be 4-byte aligned. The code doesn't worry about
603 * doing an extra byte.
607 lance_copytobuf_gap2(struct lance_softc *sc, void *fromv, int boff, int len)
609 volatile caddr_t buf = sc->sc_mem;
610 caddr_t from = fromv;
611 volatile uint16_t *bptr;
614 /* Handle unaligned first byte. */
615 bptr = ((volatile uint16_t *)buf) + (boff - 1);
616 *bptr = (*from++ << 8) | (*bptr & 0xff);
620 bptr = ((volatile uint16_t *)buf) + boff;
622 *bptr = (from[1] << 8) | (from[0] & 0xff);
628 *bptr = (uint16_t)*from;
632 lance_copyfrombuf_gap2(struct lance_softc *sc, void *tov, int boff, int len)
634 volatile caddr_t buf = sc->sc_mem;
636 volatile uint16_t *bptr;
640 /* Handle unaligned first byte. */
641 bptr = ((volatile uint16_t *)buf) + (boff - 1);
642 *to++ = (*bptr >> 8) & 0xff;
646 bptr = ((volatile uint16_t *)buf) + boff;
650 *to++ = (tmp >> 8) & 0xff;
659 lance_zerobuf_gap2(struct lance_softc *sc, int boff, int len)
661 volatile caddr_t buf = sc->sc_mem;
662 volatile uint16_t *bptr;
664 if ((unsigned)boff & 0x1) {
665 bptr = ((volatile uint16_t *)buf) + (boff - 1);
670 bptr = ((volatile uint16_t *)buf) + boff;
679 * gap16: 16 bytes of data followed by 16 bytes of pad.
681 * Buffers must be 32-byte aligned.
685 lance_copytobuf_gap16(struct lance_softc *sc, void *fromv, int boff, int len)
687 volatile caddr_t buf = sc->sc_mem;
688 caddr_t bptr, from = fromv;
691 bptr = buf + ((boff << 1) & ~0x1f);
693 xfer = min(len, 16 - boff);
695 memcpy(bptr + boff, from, xfer);
705 lance_copyfrombuf_gap16(struct lance_softc *sc, void *tov, int boff, int len)
707 volatile caddr_t buf = sc->sc_mem;
708 caddr_t bptr, to = tov;
711 bptr = buf + ((boff << 1) & ~0x1f);
713 xfer = min(len, 16 - boff);
715 memcpy(to, bptr + boff, xfer);
725 lance_zerobuf_gap16(struct lance_softc *sc, int boff, int len)
727 volatile caddr_t buf = sc->sc_mem;
731 bptr = buf + ((boff << 1) & ~0x1f);
733 xfer = min(len, 16 - boff);
735 memset(bptr + boff, 0, xfer);
742 #endif /* Example only */