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.
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 __FBSDID("$FreeBSD$");
70 #include <sys/param.h>
72 #include <sys/endian.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
77 #include <sys/mutex.h>
78 #include <sys/socket.h>
79 #include <sys/sockio.h>
81 #include <net/ethernet.h>
83 #include <net/if_var.h>
84 #include <net/if_arp.h>
85 #include <net/if_dl.h>
86 #include <net/if_media.h>
87 #include <net/if_types.h>
88 #include <net/if_vlan_var.h>
90 #include <machine/bus.h>
92 #include <dev/le/lancereg.h>
93 #include <dev/le/lancevar.h>
95 devclass_t le_devclass;
97 static void lance_start(struct ifnet *);
98 static void lance_stop(struct lance_softc *);
99 static void lance_init(void *);
100 static void lance_watchdog(void *s);
101 static int lance_mediachange(struct ifnet *);
102 static void lance_mediastatus(struct ifnet *, struct ifmediareq *);
103 static int lance_ioctl(struct ifnet *, u_long, caddr_t);
106 lance_config(struct lance_softc *sc, const char* name, int unit)
111 if (LE_LOCK_INITIALIZED(sc) == 0)
114 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
118 callout_init_mtx(&sc->sc_wdog_ch, &sc->sc_mtx, 0);
120 /* Initialize ifnet structure. */
122 if_initname(ifp, name, unit);
123 ifp->if_start = lance_start;
124 ifp->if_ioctl = lance_ioctl;
125 ifp->if_init = lance_init;
126 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
127 #ifdef LANCE_REVC_BUG
128 ifp->if_flags &= ~IFF_MULTICAST;
130 ifp->if_baudrate = IF_Mbps(10);
131 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
132 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
133 IFQ_SET_READY(&ifp->if_snd);
135 /* Initialize ifmedia structures. */
136 ifmedia_init(&sc->sc_media, 0, lance_mediachange, lance_mediastatus);
137 if (sc->sc_supmedia != NULL) {
138 for (i = 0; i < sc->sc_nsupmedia; i++)
139 ifmedia_add(&sc->sc_media, sc->sc_supmedia[i], 0, NULL);
140 ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
142 ifmedia_add(&sc->sc_media,
143 IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0), 0, NULL);
144 ifmedia_set(&sc->sc_media,
145 IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0));
148 switch (sc->sc_memsize) {
174 /* weird memory size; cope with it */
175 nbuf = sc->sc_memsize / LEBLEN;
176 sc->sc_ntbuf = nbuf / 5;
177 sc->sc_nrbuf = nbuf - sc->sc_ntbuf;
180 if_printf(ifp, "%d receive buffers, %d transmit buffers\n",
181 sc->sc_nrbuf, sc->sc_ntbuf);
183 /* Make sure the chip is stopped. */
192 lance_attach(struct lance_softc *sc)
194 struct ifnet *ifp = sc->sc_ifp;
196 /* Attach the interface. */
197 ether_ifattach(ifp, sc->sc_enaddr);
199 /* Claim 802.1q capability. */
200 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
201 ifp->if_capabilities |= IFCAP_VLAN_MTU;
202 ifp->if_capenable |= IFCAP_VLAN_MTU;
206 lance_detach(struct lance_softc *sc)
208 struct ifnet *ifp = sc->sc_ifp;
213 callout_drain(&sc->sc_wdog_ch);
219 lance_suspend(struct lance_softc *sc)
228 lance_resume(struct lance_softc *sc)
232 if (sc->sc_ifp->if_flags & IFF_UP)
233 lance_init_locked(sc);
238 lance_start(struct ifnet *ifp)
240 struct lance_softc *sc = ifp->if_softc;
243 (*sc->sc_start_locked)(sc);
248 lance_stop(struct lance_softc *sc)
250 struct ifnet *ifp = sc->sc_ifp;
252 LE_LOCK_ASSERT(sc, MA_OWNED);
255 * Mark the interface down and cancel the watchdog timer.
257 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
258 callout_stop(&sc->sc_wdog_ch);
259 sc->sc_wdog_timer = 0;
261 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
265 lance_init(void *xsc)
267 struct lance_softc *sc = (struct lance_softc *)xsc;
270 lance_init_locked(sc);
275 * Initialization of interface; set up initialization block
276 * and transmit/receive descriptor rings.
279 lance_init_locked(struct lance_softc *sc)
281 struct ifnet *ifp = sc->sc_ifp;
285 LE_LOCK_ASSERT(sc, MA_OWNED);
287 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
290 /* Newer LANCE chips have a reset register. */
292 (*sc->sc_hwreset)(sc);
294 /* Set the correct byte swapping mode, etc. */
295 (*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
297 /* Set the current media. This may require the chip to be stopped. */
298 if (sc->sc_mediachange)
299 (void)(*sc->sc_mediachange)(sc);
302 * Update our private copy of the Ethernet address.
303 * We NEED the copy so we can ensure its alignment!
305 memcpy(sc->sc_enaddr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
307 /* Set up LANCE init block. */
308 (*sc->sc_meminit)(sc);
310 /* Give LANCE the physical address of its init block. */
311 a = sc->sc_addr + LE_INITADDR(sc);
312 (*sc->sc_wrcsr)(sc, LE_CSR1, a & 0xffff);
313 (*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
315 /* Try to initialize the LANCE. */
317 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
319 /* Wait for initialization to finish. */
320 for (timo = 100000; timo; timo--)
321 if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
324 if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
325 /* Start the LANCE. */
326 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT);
327 ifp->if_drv_flags |= IFF_DRV_RUNNING;
328 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
329 sc->sc_wdog_timer = 0;
330 callout_reset(&sc->sc_wdog_ch, hz, lance_watchdog, sc);
331 (*sc->sc_start_locked)(sc);
333 if_printf(ifp, "controller failed to initialize\n");
336 (*sc->sc_hwinit)(sc);
340 * Routine to copy from mbuf chain to transmit buffer in
341 * network buffer memory.
344 lance_put(struct lance_softc *sc, int boff, struct mbuf *m)
349 LE_LOCK_ASSERT(sc, MA_OWNED);
358 (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
364 if (tlen < LEMINSIZE) {
365 (*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
372 * Pull data off an interface.
373 * Len is length of data, with local net header stripped.
374 * We copy the data into mbufs. When full cluster sized units are present
375 * we copy into clusters.
378 lance_get(struct lance_softc *sc, int boff, int totlen)
380 struct ifnet *ifp = sc->sc_ifp;
381 struct mbuf *m, *m0, *newm;
385 if (totlen <= ETHER_HDR_LEN || totlen > LEBLEN - ETHER_CRC_LEN) {
387 if_printf(ifp, "invalid packet size %d; dropping\n", totlen);
392 MGETHDR(m0, M_NOWAIT, MT_DATA);
395 m0->m_pkthdr.rcvif = ifp;
396 m0->m_pkthdr.len = totlen;
401 if (totlen >= MINCLSIZE) {
402 if (!(MCLGET(m, M_NOWAIT)))
409 ALIGN(m->m_data + ETHER_HDR_LEN) - ETHER_HDR_LEN;
410 len -= newdata - m->m_data;
414 m->m_len = len = min(totlen, len);
415 (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
420 MGET(newm, M_NOWAIT, MT_DATA);
424 m = m->m_next = newm;
436 lance_watchdog(void *xsc)
438 struct lance_softc *sc = (struct lance_softc *)xsc;
439 struct ifnet *ifp = sc->sc_ifp;
441 LE_LOCK_ASSERT(sc, MA_OWNED);
443 if (sc->sc_wdog_timer == 0 || --sc->sc_wdog_timer != 0) {
444 callout_reset(&sc->sc_wdog_ch, hz, lance_watchdog, sc);
448 if_printf(ifp, "device timeout\n");
449 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
450 lance_init_locked(sc);
454 lance_mediachange(struct ifnet *ifp)
456 struct lance_softc *sc = ifp->if_softc;
458 if (sc->sc_mediachange) {
460 * For setting the port in LE_CSR15 the PCnet chips must
461 * be powered down or stopped and unlike documented may
462 * not take effect without an initialization. So don't
463 * invoke (*sc_mediachange) directly here but go through
464 * lance_init_locked().
468 lance_init_locked(sc);
469 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
470 (*sc->sc_start_locked)(sc);
477 lance_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
479 struct lance_softc *sc = ifp->if_softc;
482 if (!(ifp->if_flags & IFF_UP)) {
487 ifmr->ifm_status = IFM_AVALID;
488 if (sc->sc_flags & LE_CARRIER)
489 ifmr->ifm_status |= IFM_ACTIVE;
491 if (sc->sc_mediastatus)
492 (*sc->sc_mediastatus)(sc, ifmr);
497 * Process an ioctl request.
500 lance_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
502 struct lance_softc *sc = ifp->if_softc;
503 struct ifreq *ifr = (struct ifreq *)data;
509 if (ifp->if_flags & IFF_PROMISC) {
510 if (!(sc->sc_flags & LE_PROMISC)) {
511 sc->sc_flags |= LE_PROMISC;
512 lance_init_locked(sc);
514 } else if (sc->sc_flags & LE_PROMISC) {
515 sc->sc_flags &= ~LE_PROMISC;
516 lance_init_locked(sc);
519 if ((ifp->if_flags & IFF_ALLMULTI) &&
520 !(sc->sc_flags & LE_ALLMULTI)) {
521 sc->sc_flags |= LE_ALLMULTI;
522 lance_init_locked(sc);
523 } else if (!(ifp->if_flags & IFF_ALLMULTI) &&
524 (sc->sc_flags & LE_ALLMULTI)) {
525 sc->sc_flags &= ~LE_ALLMULTI;
526 lance_init_locked(sc);
529 if (!(ifp->if_flags & IFF_UP) &&
530 ifp->if_drv_flags & IFF_DRV_RUNNING) {
532 * If interface is marked down and it is running, then
536 } else if (ifp->if_flags & IFF_UP &&
537 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
539 * If interface is marked up and it is stopped, then
542 lance_init_locked(sc);
545 if (ifp->if_flags & IFF_DEBUG)
546 sc->sc_flags |= LE_DEBUG;
548 sc->sc_flags &= ~LE_DEBUG;
556 * Multicast list has changed; set the hardware filter
560 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
561 lance_init_locked(sc);
567 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
571 error = ether_ioctl(ifp, cmd, data);
579 * Set up the logical address filter.
582 lance_setladrf(struct lance_softc *sc, uint16_t *af)
584 struct ifnet *ifp = sc->sc_ifp;
585 struct ifmultiaddr *ifma;
589 * Set up multicast address filter by passing all multicast addresses
590 * through a crc generator, and then using the high order 6 bits as an
591 * index into the 64 bit logical address filter. The high order bit
592 * selects the word, while the rest of the bits select the bit within
596 if (ifp->if_flags & IFF_PROMISC || sc->sc_flags & LE_ALLMULTI) {
597 af[0] = af[1] = af[2] = af[3] = 0xffff;
601 af[0] = af[1] = af[2] = af[3] = 0x0000;
603 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
604 if (ifma->ifma_addr->sa_family != AF_LINK)
607 crc = ether_crc32_le(LLADDR((struct sockaddr_dl *)
608 ifma->ifma_addr), ETHER_ADDR_LEN);
610 /* Just want the 6 most significant bits. */
613 /* Set the corresponding bit in the filter. */
614 af[crc >> 4] |= LE_HTOLE16(1 << (crc & 0xf));
616 if_maddr_runlock(ifp);
620 * Routines for accessing the transmit and receive buffers.
621 * The various CPU and adapter configurations supported by this
622 * driver require three different access methods for buffers
624 * (1) contig (contiguous data; no padding),
625 * (2) gap2 (two bytes of data followed by two bytes of padding),
626 * (3) gap16 (16 bytes of data followed by 16 bytes of padding).
630 * contig: contiguous data with no padding.
632 * Buffers may have any alignment.
636 lance_copytobuf_contig(struct lance_softc *sc, void *from, int boff, int len)
638 volatile caddr_t buf = sc->sc_mem;
641 * Just call memcpy() to do the work.
643 memcpy(buf + boff, from, len);
647 lance_copyfrombuf_contig(struct lance_softc *sc, void *to, int boff, int len)
649 volatile caddr_t buf = sc->sc_mem;
652 * Just call memcpy() to do the work.
654 memcpy(to, buf + boff, len);
658 lance_zerobuf_contig(struct lance_softc *sc, int boff, int len)
660 volatile caddr_t buf = sc->sc_mem;
663 * Just let memset() do the work
665 memset(buf + boff, 0, len);
670 * Examples only; duplicate these and tweak (if necessary) in
671 * machine-specific front-ends.
675 * gap2: two bytes of data followed by two bytes of pad.
677 * Buffers must be 4-byte aligned. The code doesn't worry about
678 * doing an extra byte.
682 lance_copytobuf_gap2(struct lance_softc *sc, void *fromv, int boff, int len)
684 volatile caddr_t buf = sc->sc_mem;
685 caddr_t from = fromv;
686 volatile uint16_t *bptr;
689 /* Handle unaligned first byte. */
690 bptr = ((volatile uint16_t *)buf) + (boff - 1);
691 *bptr = (*from++ << 8) | (*bptr & 0xff);
695 bptr = ((volatile uint16_t *)buf) + boff;
697 *bptr = (from[1] << 8) | (from[0] & 0xff);
703 *bptr = (uint16_t)*from;
707 lance_copyfrombuf_gap2(struct lance_softc *sc, void *tov, int boff, int len)
709 volatile caddr_t buf = sc->sc_mem;
711 volatile uint16_t *bptr;
715 /* Handle unaligned first byte. */
716 bptr = ((volatile uint16_t *)buf) + (boff - 1);
717 *to++ = (*bptr >> 8) & 0xff;
721 bptr = ((volatile uint16_t *)buf) + boff;
725 *to++ = (tmp >> 8) & 0xff;
734 lance_zerobuf_gap2(struct lance_softc *sc, int boff, int len)
736 volatile caddr_t buf = sc->sc_mem;
737 volatile uint16_t *bptr;
739 if ((unsigned)boff & 0x1) {
740 bptr = ((volatile uint16_t *)buf) + (boff - 1);
745 bptr = ((volatile uint16_t *)buf) + boff;
754 * gap16: 16 bytes of data followed by 16 bytes of pad.
756 * Buffers must be 32-byte aligned.
760 lance_copytobuf_gap16(struct lance_softc *sc, void *fromv, int boff, int len)
762 volatile caddr_t buf = sc->sc_mem;
763 caddr_t bptr, from = fromv;
766 bptr = buf + ((boff << 1) & ~0x1f);
768 xfer = min(len, 16 - boff);
770 memcpy(bptr + boff, from, xfer);
780 lance_copyfrombuf_gap16(struct lance_softc *sc, void *tov, int boff, int len)
782 volatile caddr_t buf = sc->sc_mem;
783 caddr_t bptr, to = tov;
786 bptr = buf + ((boff << 1) & ~0x1f);
788 xfer = min(len, 16 - boff);
790 memcpy(to, bptr + boff, xfer);
800 lance_zerobuf_gap16(struct lance_softc *sc, int boff, int len)
802 volatile caddr_t buf = sc->sc_mem;
806 bptr = buf + ((boff << 1) & ~0x1f);
808 xfer = min(len, 16 - boff);
810 memset(bptr + boff, 0, xfer);
817 #endif /* Example only */