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 /* Initialize ifmedia structures. */
130 ifmedia_init(&sc->sc_media, 0, lance_mediachange, lance_mediastatus);
131 if (sc->sc_supmedia != NULL) {
132 for (i = 0; i < sc->sc_nsupmedia; i++)
133 ifmedia_add(&sc->sc_media, sc->sc_supmedia[i], 0, NULL);
134 ifmedia_set(&sc->sc_media, sc->sc_defaultmedia);
136 ifmedia_add(&sc->sc_media,
137 IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0), 0, NULL);
138 ifmedia_set(&sc->sc_media,
139 IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, 0));
142 switch (sc->sc_memsize) {
168 /* weird memory size; cope with it */
169 nbuf = sc->sc_memsize / LEBLEN;
170 sc->sc_ntbuf = nbuf / 5;
171 sc->sc_nrbuf = nbuf - sc->sc_ntbuf;
174 if_printf(ifp, "%d receive buffers, %d transmit buffers\n",
175 sc->sc_nrbuf, sc->sc_ntbuf);
177 /* Make sure the chip is stopped. */
184 lance_attach(struct lance_softc *sc)
186 struct ifnet *ifp = sc->ifp;
188 /* Attach the interface. */
189 ether_ifattach(ifp, sc->sc_enaddr, NULL);
191 /* Claim 802.1q capability. */
192 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
193 ifp->if_capabilities |= IFCAP_VLAN_MTU;
194 ifp->if_capenable |= IFCAP_VLAN_MTU;
198 lance_suspend(struct lance_softc *sc)
201 lwkt_serialize_enter(sc->ifp->if_serializer);
203 lwkt_serialize_exit(sc->ifp->if_serializer);
207 lance_resume(struct lance_softc *sc)
210 lwkt_serialize_enter(sc->ifp->if_serializer);
211 if (sc->ifp->if_flags & IFF_UP)
212 lance_init_locked(sc);
213 lwkt_serialize_exit(sc->ifp->if_serializer);
217 lance_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
219 struct lance_softc *sc = ifp->if_softc;
221 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
222 (*sc->sc_start_locked)(sc);
226 lance_stop(struct lance_softc *sc)
228 struct ifnet *ifp = sc->ifp;
231 * Mark the interface down and cancel the watchdog timer.
233 ifp->if_flags &= ~IFF_RUNNING;
234 ifq_clr_oactive(&ifp->if_snd);
237 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
241 lance_init(void *xsc)
243 struct lance_softc *sc = (struct lance_softc *)xsc;
246 lance_init_locked(sc);
251 * Initialization of interface; set up initialization block
252 * and transmit/receive descriptor rings.
255 lance_init_locked(struct lance_softc *sc)
257 struct ifnet *ifp = sc->ifp;
261 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP);
264 /* Newer LANCE chips have a reset register. */
266 (*sc->sc_hwreset)(sc);
268 /* Set the correct byte swapping mode, etc. */
269 (*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3);
272 * Update our private copy of the Ethernet address.
273 * We NEED the copy so we can ensure its alignment!
275 memcpy(sc->sc_enaddr, IF_LLADDR(ifp), ETHER_ADDR_LEN);
277 /* Set up LANCE init block. */
278 (*sc->sc_meminit)(sc);
280 /* Give LANCE the physical address of its init block. */
281 a = sc->sc_addr + LE_INITADDR(sc);
282 (*sc->sc_wrcsr)(sc, LE_CSR1, a & 0xffff);
283 (*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16);
285 /* Try to initialize the LANCE. */
287 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT);
289 /* Wait for initialization to finish. */
290 for (timo = 100000; timo; timo--)
291 if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON)
294 /* Set the current media. */
295 if (sc->sc_mediachange)
296 (*sc->sc_mediachange)(sc);
298 if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) {
299 /* Start the LANCE. */
300 (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT);
301 ifp->if_flags |= IFF_RUNNING;
302 ifq_clr_oactive(&ifp->if_snd);
306 if_printf(ifp, "controller failed to initialize\n");
309 (*sc->sc_hwinit)(sc);
313 * Routine to copy from mbuf chain to transmit buffer in
314 * network buffer memory.
317 lance_put(struct lance_softc *sc, int boff, struct mbuf *m)
321 for (; m; m = m_free(m)) {
324 (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, m->m_len);
328 if (tlen < LEMINSIZE) {
329 (*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
336 * Pull data off an interface.
337 * Len is length of data, with local net header stripped.
338 * We copy the data into mbufs. When full cluster sized units are present
339 * we copy into clusters.
342 lance_get(struct lance_softc *sc, int boff, int totlen)
344 struct ifnet *ifp = sc->ifp;
345 struct mbuf *m0 = NULL, *newm;
346 struct mbuf *m = NULL;
349 if (totlen <= ETHER_HDR_LEN || totlen > LEBLEN - ETHER_CRC_LEN) {
351 if_printf(ifp, "invalid packet size %d; dropping\n", totlen);
357 newm = m_getl(totlen, MB_DONTWAIT, MT_DATA,
358 m0 == NULL ? M_PKTHDR : 0, &mlen);
366 m0->m_pkthdr.rcvif = ifp;
367 m0->m_pkthdr.len = totlen;
369 ALIGN(m0->m_data + ETHER_HDR_LEN) - ETHER_HDR_LEN;
370 mlen -= newdata - m0->m_data;
371 m0->m_data = newdata;
377 m->m_len = min(totlen, mlen);
378 (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, m->m_len);
381 } while (totlen > 0);
391 lance_watchdog(struct ifnet *ifp)
393 struct lance_softc *sc = ifp->if_softc;
395 if_printf(ifp, "device timeout\n");
397 lance_init_locked(sc);
401 lance_mediachange(struct ifnet *ifp)
403 struct lance_softc *sc = ifp->if_softc;
406 if (sc->sc_mediachange) {
407 error = (*sc->sc_mediachange)(sc);
414 lance_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
416 struct lance_softc *sc = ifp->if_softc;
418 if (!(ifp->if_flags & IFF_UP)) {
422 ifmr->ifm_status = IFM_AVALID;
423 if (sc->sc_flags & LE_CARRIER)
424 ifmr->ifm_status |= IFM_ACTIVE;
426 if (sc->sc_mediastatus)
427 (*sc->sc_mediastatus)(sc, ifmr);
431 * Process an ioctl request.
434 lance_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
436 struct lance_softc *sc = ifp->if_softc;
437 struct ifreq *ifr = (struct ifreq *)data;
444 if (ifp->if_flags & IFF_PROMISC) {
445 if (!(sc->sc_flags & LE_PROMISC)) {
446 sc->sc_flags |= LE_PROMISC;
447 lance_init_locked(sc);
449 } else if (sc->sc_flags & LE_PROMISC) {
450 sc->sc_flags &= ~LE_PROMISC;
451 lance_init_locked(sc);
454 if ((ifp->if_flags & IFF_ALLMULTI) &&
455 !(sc->sc_flags & LE_ALLMULTI)) {
456 sc->sc_flags |= LE_ALLMULTI;
457 lance_init_locked(sc);
458 } else if (!(ifp->if_flags & IFF_ALLMULTI) &&
459 (sc->sc_flags & LE_ALLMULTI)) {
460 sc->sc_flags &= ~LE_ALLMULTI;
461 lance_init_locked(sc);
464 if (!(ifp->if_flags & IFF_UP) &&
465 ifp->if_flags & IFF_RUNNING) {
467 * If interface is marked down and it is running, then
471 } else if (ifp->if_flags & IFF_UP &&
472 !(ifp->if_flags & IFF_RUNNING)) {
474 * If interface is marked up and it is stopped, then
477 lance_init_locked(sc);
480 if (ifp->if_flags & IFF_DEBUG)
481 sc->sc_flags |= LE_DEBUG;
483 sc->sc_flags &= ~LE_DEBUG;
490 * Multicast list has changed; set the hardware filter
493 if (ifp->if_flags & IFF_RUNNING)
494 lance_init_locked(sc);
499 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
503 error = ether_ioctl(ifp, cmd, data);
513 * Set up the logical address filter.
516 lance_setladrf(struct lance_softc *sc, uint16_t *af)
518 struct ifnet *ifp = sc->ifp;
519 struct ifmultiaddr *ifma;
523 * Set up multicast address filter by passing all multicast addresses
524 * through a crc generator, and then using the high order 6 bits as an
525 * index into the 64 bit logical address filter. The high order bit
526 * selects the word, while the rest of the bits select the bit within
530 if (ifp->if_flags & IFF_PROMISC || sc->sc_flags & LE_ALLMULTI) {
531 af[0] = af[1] = af[2] = af[3] = 0xffff;
535 af[0] = af[1] = af[2] = af[3] = 0x0000;
537 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
538 if (ifma->ifma_addr->sa_family != AF_LINK)
541 crc = ether_crc32_le(LLADDR((struct sockaddr_dl *)
542 ifma->ifma_addr), ETHER_ADDR_LEN);
544 /* Just want the 6 most significant bits. */
547 /* Set the corresponding bit in the filter. */
548 af[crc >> 4] |= LE_HTOLE16(1 << (crc & 0xf));
553 * Routines for accessing the transmit and receive buffers.
554 * The various CPU and adapter configurations supported by this
555 * driver require three different access methods for buffers
557 * (1) contig (contiguous data; no padding),
558 * (2) gap2 (two bytes of data followed by two bytes of padding),
559 * (3) gap16 (16 bytes of data followed by 16 bytes of padding).
563 * contig: contiguous data with no padding.
565 * Buffers may have any alignment.
569 lance_copytobuf_contig(struct lance_softc *sc, void *from, int boff, int len)
571 volatile caddr_t buf = sc->sc_mem;
574 * Just call memcpy() to do the work.
576 memcpy(buf + boff, from, len);
580 lance_copyfrombuf_contig(struct lance_softc *sc, void *to, int boff, int len)
582 volatile caddr_t buf = sc->sc_mem;
585 * Just call memcpy() to do the work.
587 memcpy(to, buf + boff, len);
591 lance_zerobuf_contig(struct lance_softc *sc, int boff, int len)
593 volatile caddr_t buf = sc->sc_mem;
596 * Just let memset() do the work
598 memset(buf + boff, 0, len);
603 * Examples only; duplicate these and tweak (if necessary) in
604 * machine-specific front-ends.
608 * gap2: two bytes of data followed by two bytes of pad.
610 * Buffers must be 4-byte aligned. The code doesn't worry about
611 * doing an extra byte.
615 lance_copytobuf_gap2(struct lance_softc *sc, void *fromv, int boff, int len)
617 volatile caddr_t buf = sc->sc_mem;
618 caddr_t from = fromv;
619 volatile uint16_t *bptr;
622 /* Handle unaligned first byte. */
623 bptr = ((volatile uint16_t *)buf) + (boff - 1);
624 *bptr = (*from++ << 8) | (*bptr & 0xff);
628 bptr = ((volatile uint16_t *)buf) + boff;
630 *bptr = (from[1] << 8) | (from[0] & 0xff);
636 *bptr = (uint16_t)*from;
640 lance_copyfrombuf_gap2(struct lance_softc *sc, void *tov, int boff, int len)
642 volatile caddr_t buf = sc->sc_mem;
644 volatile uint16_t *bptr;
648 /* Handle unaligned first byte. */
649 bptr = ((volatile uint16_t *)buf) + (boff - 1);
650 *to++ = (*bptr >> 8) & 0xff;
654 bptr = ((volatile uint16_t *)buf) + boff;
658 *to++ = (tmp >> 8) & 0xff;
667 lance_zerobuf_gap2(struct lance_softc *sc, int boff, int len)
669 volatile caddr_t buf = sc->sc_mem;
670 volatile uint16_t *bptr;
672 if ((unsigned)boff & 0x1) {
673 bptr = ((volatile uint16_t *)buf) + (boff - 1);
678 bptr = ((volatile uint16_t *)buf) + boff;
687 * gap16: 16 bytes of data followed by 16 bytes of pad.
689 * Buffers must be 32-byte aligned.
693 lance_copytobuf_gap16(struct lance_softc *sc, void *fromv, int boff, int len)
695 volatile caddr_t buf = sc->sc_mem;
696 caddr_t bptr, from = fromv;
699 bptr = buf + ((boff << 1) & ~0x1f);
701 xfer = min(len, 16 - boff);
703 memcpy(bptr + boff, from, xfer);
713 lance_copyfrombuf_gap16(struct lance_softc *sc, void *tov, int boff, int len)
715 volatile caddr_t buf = sc->sc_mem;
716 caddr_t bptr, to = tov;
719 bptr = buf + ((boff << 1) & ~0x1f);
721 xfer = min(len, 16 - boff);
723 memcpy(to, bptr + boff, xfer);
733 lance_zerobuf_gap16(struct lance_softc *sc, int boff, int len)
735 volatile caddr_t buf = sc->sc_mem;
739 bptr = buf + ((boff << 1) & ~0x1f);
741 xfer = min(len, 16 - boff);
743 memset(bptr + boff, 0, xfer);
750 #endif /* Example only */