1 /* $NetBSD: lance.c,v 1.34 2005/12/24 20:27:30 perry 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.
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.
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.
41 * Copyright (c) 1992, 1993
42 * The Regents of the University of California. All rights reserved.
44 * This code is derived from software contributed to Berkeley by
45 * Ralph Campbell and Rick Macklem.
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
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.
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
71 * @(#)if_le.c 8.2 (Berkeley) 11/16/93
74 #include <sys/cdefs.h>
75 __FBSDID("$FreeBSD$");
77 #include <sys/param.h>
79 #include <sys/endian.h>
81 #include <sys/kernel.h>
83 #include <sys/mutex.h>
84 #include <sys/socket.h>
85 #include <sys/sockio.h>
87 #include <net/ethernet.h>
89 #include <net/if_arp.h>
90 #include <net/if_dl.h>
91 #include <net/if_media.h>
92 #include <net/if_types.h>
93 #include <net/if_vlan_var.h>
95 #include <machine/bus.h>
97 #include <dev/le/lancereg.h>
98 #include <dev/le/lancevar.h>
100 devclass_t le_devclass;
102 static void lance_start(struct ifnet *);
103 static void lance_stop(struct lance_softc *);
104 static void lance_init(void *);
105 static void lance_watchdog(void *s);
106 static int lance_mediachange(struct ifnet *);
107 static void lance_mediastatus(struct ifnet *, struct ifmediareq *);
108 static int lance_ioctl(struct ifnet *, u_long, caddr_t);
111 lance_config(struct lance_softc *sc, const char* name, int unit)
116 if (LE_LOCK_INITIALIZED(sc) == 0)
119 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
123 callout_init_mtx(&sc->sc_wdog_ch, &sc->sc_mtx, 0);
125 /* Initialize ifnet structure. */
127 if_initname(ifp, name, unit);
128 ifp->if_start = lance_start;
129 ifp->if_ioctl = lance_ioctl;
130 ifp->if_init = lance_init;
131 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
132 #ifdef LANCE_REVC_BUG
133 ifp->if_flags &= ~IFF_MULTICAST;
135 ifp->if_baudrate = IF_Mbps(10);
136 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
137 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
138 IFQ_SET_READY(&ifp->if_snd);
140 /* Initialize ifmedia structures. */
141 ifmedia_init(&sc->sc_media, 0, lance_mediachange, lance_mediastatus);
142 if (sc->sc_supmedia != NULL) {
143 for (i = 0; i < sc->sc_nsupmedia; i++)
144 ifmedia_add(&sc->sc_media, sc->sc_supmedia[i], 0, NULL);
145 ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
147 ifmedia_add(&sc->sc_media,
148 IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0), 0, NULL);
149 ifmedia_set(&sc->sc_media,
150 IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0));
153 switch (sc->sc_memsize) {
179 /* weird memory size; cope with it */
180 nbuf = sc->sc_memsize / LEBLEN;
181 sc->sc_ntbuf = nbuf / 5;
182 sc->sc_nrbuf = nbuf - sc->sc_ntbuf;
185 if_printf(ifp, "%d receive buffers, %d transmit buffers\n",
186 sc->sc_nrbuf, sc->sc_ntbuf);
188 /* Make sure the chip is stopped. */
197 lance_attach(struct lance_softc *sc)
199 struct ifnet *ifp = sc->sc_ifp;
201 /* Attach the interface. */
202 ether_ifattach(ifp, sc->sc_enaddr);
204 /* Claim 802.1q capability. */
205 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
206 ifp->if_capabilities |= IFCAP_VLAN_MTU;
207 ifp->if_capenable |= IFCAP_VLAN_MTU;
211 lance_detach(struct lance_softc *sc)
213 struct ifnet *ifp = sc->sc_ifp;
218 callout_drain(&sc->sc_wdog_ch);
224 lance_suspend(struct lance_softc *sc)
233 lance_resume(struct lance_softc *sc)
237 if (sc->sc_ifp->if_flags & IFF_UP)
238 lance_init_locked(sc);
243 lance_start(struct ifnet *ifp)
245 struct lance_softc *sc = ifp->if_softc;
248 (*sc->sc_start_locked)(sc);
253 lance_stop(struct lance_softc *sc)
255 struct ifnet *ifp = sc->sc_ifp;
257 LE_LOCK_ASSERT(sc, MA_OWNED);
260 * Mark the interface down and cancel the watchdog timer.
262 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
263 callout_stop(&sc->sc_wdog_ch);
264 sc->sc_wdog_timer = 0;
266 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
270 lance_init(void *xsc)
272 struct lance_softc *sc = (struct lance_softc *)xsc;
275 lance_init_locked(sc);
280 * Initialization of interface; set up initialization block
281 * and transmit/receive descriptor rings.
284 lance_init_locked(struct lance_softc *sc)
286 struct ifnet *ifp = sc->sc_ifp;
290 LE_LOCK_ASSERT(sc, MA_OWNED);
292 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
295 /* Newer LANCE chips have a reset register. */
297 (*sc->sc_hwreset)(sc);
299 /* Set the correct byte swapping mode, etc. */
300 (*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
302 /* Set the current media. This may require the chip to be stopped. */
303 if (sc->sc_mediachange)
304 (void)(*sc->sc_mediachange)(sc);
307 * Update our private copy of the Ethernet address.
308 * We NEED the copy so we can ensure its alignment!
310 memcpy(sc->sc_enaddr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
312 /* Set up LANCE init block. */
313 (*sc->sc_meminit)(sc);
315 /* Give LANCE the physical address of its init block. */
316 a = sc->sc_addr + LE_INITADDR(sc);
317 (*sc->sc_wrcsr)(sc, LE_CSR1, a & 0xffff);
318 (*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
320 /* Try to initialize the LANCE. */
322 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
324 /* Wait for initialization to finish. */
325 for (timo = 100000; timo; timo--)
326 if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
329 if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
330 /* Start the LANCE. */
331 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT);
332 ifp->if_drv_flags |= IFF_DRV_RUNNING;
333 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
334 sc->sc_wdog_timer = 0;
335 callout_reset(&sc->sc_wdog_ch, hz, lance_watchdog, sc);
336 (*sc->sc_start_locked)(sc);
338 if_printf(ifp, "controller failed to initialize\n");
341 (*sc->sc_hwinit)(sc);
345 * Routine to copy from mbuf chain to transmit buffer in
346 * network buffer memory.
349 lance_put(struct lance_softc *sc, int boff, struct mbuf *m)
354 LE_LOCK_ASSERT(sc, MA_OWNED);
363 (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
369 if (tlen < LEMINSIZE) {
370 (*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
377 * Pull data off an interface.
378 * Len is length of data, with local net header stripped.
379 * We copy the data into mbufs. When full cluster sized units are present
380 * we copy into clusters.
383 lance_get(struct lance_softc *sc, int boff, int totlen)
385 struct ifnet *ifp = sc->sc_ifp;
386 struct mbuf *m, *m0, *newm;
390 if (totlen <= ETHER_HDR_LEN || totlen > LEBLEN - ETHER_CRC_LEN) {
392 if_printf(ifp, "invalid packet size %d; dropping\n", totlen);
397 MGETHDR(m0, M_NOWAIT, MT_DATA);
400 m0->m_pkthdr.rcvif = ifp;
401 m0->m_pkthdr.len = totlen;
406 if (totlen >= MINCLSIZE) {
408 if ((m->m_flags & M_EXT) == 0)
415 ALIGN(m->m_data + ETHER_HDR_LEN) - ETHER_HDR_LEN;
416 len -= newdata - m->m_data;
420 m->m_len = len = min(totlen, len);
421 (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
426 MGET(newm, M_NOWAIT, MT_DATA);
430 m = m->m_next = newm;
442 lance_watchdog(void *xsc)
444 struct lance_softc *sc = (struct lance_softc *)xsc;
445 struct ifnet *ifp = sc->sc_ifp;
447 LE_LOCK_ASSERT(sc, MA_OWNED);
449 if (sc->sc_wdog_timer == 0 || --sc->sc_wdog_timer != 0) {
450 callout_reset(&sc->sc_wdog_ch, hz, lance_watchdog, sc);
454 if_printf(ifp, "device timeout\n");
456 lance_init_locked(sc);
460 lance_mediachange(struct ifnet *ifp)
462 struct lance_softc *sc = ifp->if_softc;
464 if (sc->sc_mediachange) {
466 * For setting the port in LE_CSR15 the PCnet chips must
467 * be powered down or stopped and unlike documented may
468 * not take effect without an initialization. So don't
469 * invoke (*sc_mediachange) directly here but go through
470 * lance_init_locked().
474 lance_init_locked(sc);
475 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
476 (*sc->sc_start_locked)(sc);
483 lance_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
485 struct lance_softc *sc = ifp->if_softc;
488 if (!(ifp->if_flags & IFF_UP)) {
493 ifmr->ifm_status = IFM_AVALID;
494 if (sc->sc_flags & LE_CARRIER)
495 ifmr->ifm_status |= IFM_ACTIVE;
497 if (sc->sc_mediastatus)
498 (*sc->sc_mediastatus)(sc, ifmr);
503 * Process an ioctl request.
506 lance_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
508 struct lance_softc *sc = ifp->if_softc;
509 struct ifreq *ifr = (struct ifreq *)data;
515 if (ifp->if_flags & IFF_PROMISC) {
516 if (!(sc->sc_flags & LE_PROMISC)) {
517 sc->sc_flags |= LE_PROMISC;
518 lance_init_locked(sc);
520 } else if (sc->sc_flags & LE_PROMISC) {
521 sc->sc_flags &= ~LE_PROMISC;
522 lance_init_locked(sc);
525 if ((ifp->if_flags & IFF_ALLMULTI) &&
526 !(sc->sc_flags & LE_ALLMULTI)) {
527 sc->sc_flags |= LE_ALLMULTI;
528 lance_init_locked(sc);
529 } else if (!(ifp->if_flags & IFF_ALLMULTI) &&
530 (sc->sc_flags & LE_ALLMULTI)) {
531 sc->sc_flags &= ~LE_ALLMULTI;
532 lance_init_locked(sc);
535 if (!(ifp->if_flags & IFF_UP) &&
536 ifp->if_drv_flags & IFF_DRV_RUNNING) {
538 * If interface is marked down and it is running, then
542 } else if (ifp->if_flags & IFF_UP &&
543 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
545 * If interface is marked up and it is stopped, then
548 lance_init_locked(sc);
551 if (ifp->if_flags & IFF_DEBUG)
552 sc->sc_flags |= LE_DEBUG;
554 sc->sc_flags &= ~LE_DEBUG;
562 * Multicast list has changed; set the hardware filter
566 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
567 lance_init_locked(sc);
573 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
577 error = ether_ioctl(ifp, cmd, data);
585 * Set up the logical address filter.
588 lance_setladrf(struct lance_softc *sc, uint16_t *af)
590 struct ifnet *ifp = sc->sc_ifp;
591 struct ifmultiaddr *ifma;
595 * Set up multicast address filter by passing all multicast addresses
596 * through a crc generator, and then using the high order 6 bits as an
597 * index into the 64 bit logical address filter. The high order bit
598 * selects the word, while the rest of the bits select the bit within
602 if (ifp->if_flags & IFF_PROMISC || sc->sc_flags & LE_ALLMULTI) {
603 af[0] = af[1] = af[2] = af[3] = 0xffff;
607 af[0] = af[1] = af[2] = af[3] = 0x0000;
609 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
610 if (ifma->ifma_addr->sa_family != AF_LINK)
613 crc = ether_crc32_le(LLADDR((struct sockaddr_dl *)
614 ifma->ifma_addr), ETHER_ADDR_LEN);
616 /* Just want the 6 most significant bits. */
619 /* Set the corresponding bit in the filter. */
620 af[crc >> 4] |= LE_HTOLE16(1 << (crc & 0xf));
622 if_maddr_runlock(ifp);
626 * Routines for accessing the transmit and receive buffers.
627 * The various CPU and adapter configurations supported by this
628 * driver require three different access methods for buffers
630 * (1) contig (contiguous data; no padding),
631 * (2) gap2 (two bytes of data followed by two bytes of padding),
632 * (3) gap16 (16 bytes of data followed by 16 bytes of padding).
636 * contig: contiguous data with no padding.
638 * Buffers may have any alignment.
642 lance_copytobuf_contig(struct lance_softc *sc, void *from, int boff, int len)
644 volatile caddr_t buf = sc->sc_mem;
647 * Just call memcpy() to do the work.
649 memcpy(buf + boff, from, len);
653 lance_copyfrombuf_contig(struct lance_softc *sc, void *to, int boff, int len)
655 volatile caddr_t buf = sc->sc_mem;
658 * Just call memcpy() to do the work.
660 memcpy(to, buf + boff, len);
664 lance_zerobuf_contig(struct lance_softc *sc, int boff, int len)
666 volatile caddr_t buf = sc->sc_mem;
669 * Just let memset() do the work
671 memset(buf + boff, 0, len);
676 * Examples only; duplicate these and tweak (if necessary) in
677 * machine-specific front-ends.
681 * gap2: two bytes of data followed by two bytes of pad.
683 * Buffers must be 4-byte aligned. The code doesn't worry about
684 * doing an extra byte.
688 lance_copytobuf_gap2(struct lance_softc *sc, void *fromv, int boff, int len)
690 volatile caddr_t buf = sc->sc_mem;
691 caddr_t from = fromv;
692 volatile uint16_t *bptr;
695 /* Handle unaligned first byte. */
696 bptr = ((volatile uint16_t *)buf) + (boff - 1);
697 *bptr = (*from++ << 8) | (*bptr & 0xff);
701 bptr = ((volatile uint16_t *)buf) + boff;
703 *bptr = (from[1] << 8) | (from[0] & 0xff);
709 *bptr = (uint16_t)*from;
713 lance_copyfrombuf_gap2(struct lance_softc *sc, void *tov, int boff, int len)
715 volatile caddr_t buf = sc->sc_mem;
717 volatile uint16_t *bptr;
721 /* Handle unaligned first byte. */
722 bptr = ((volatile uint16_t *)buf) + (boff - 1);
723 *to++ = (*bptr >> 8) & 0xff;
727 bptr = ((volatile uint16_t *)buf) + boff;
731 *to++ = (tmp >> 8) & 0xff;
740 lance_zerobuf_gap2(struct lance_softc *sc, int boff, int len)
742 volatile caddr_t buf = sc->sc_mem;
743 volatile uint16_t *bptr;
745 if ((unsigned)boff & 0x1) {
746 bptr = ((volatile uint16_t *)buf) + (boff - 1);
751 bptr = ((volatile uint16_t *)buf) + boff;
760 * gap16: 16 bytes of data followed by 16 bytes of pad.
762 * Buffers must be 32-byte aligned.
766 lance_copytobuf_gap16(struct lance_softc *sc, void *fromv, int boff, int len)
768 volatile caddr_t buf = sc->sc_mem;
769 caddr_t bptr, from = fromv;
772 bptr = buf + ((boff << 1) & ~0x1f);
774 xfer = min(len, 16 - boff);
776 memcpy(bptr + boff, from, xfer);
786 lance_copyfrombuf_gap16(struct lance_softc *sc, void *tov, int boff, int len)
788 volatile caddr_t buf = sc->sc_mem;
789 caddr_t bptr, to = tov;
792 bptr = buf + ((boff << 1) & ~0x1f);
794 xfer = min(len, 16 - boff);
796 memcpy(to, bptr + boff, xfer);
806 lance_zerobuf_gap16(struct lance_softc *sc, int boff, int len)
808 volatile caddr_t buf = sc->sc_mem;
812 bptr = buf + ((boff << 1) & ~0x1f);
814 xfer = min(len, 16 - boff);
816 memset(bptr + boff, 0, xfer);
823 #endif /* Example only */