From 9a59784f709e36e324e207dd446910af0a264f36 Mon Sep 17 00:00:00 2001 From: drochner Date: Tue, 21 Jul 1998 17:26:45 +0000 Subject: [PATCH] Split the LANCE driver into a part specific to the classic chip with 24-bit DMA addresses, and an independant part. Implement support for the 32-bit mode in the newer (79c9xx) chips. --- sys/conf/files | 8 +- sys/dev/ic/am7990.c | 952 ++---------------------------------- sys/dev/ic/am79900.c | 570 ++++++++++++++++++++++ sys/dev/ic/am79900reg.h | 112 +++++ sys/dev/ic/am79900var.h | 47 ++ sys/dev/ic/am7990reg.h | 66 +-- sys/dev/ic/am7990var.h | 137 +----- sys/dev/ic/lance.c | 1005 +++++++++++++++++++++++++++++++++++++++ sys/dev/ic/lancereg.h | 104 ++++ sys/dev/ic/lancevar.h | 190 ++++++++ 10 files changed, 2086 insertions(+), 1105 deletions(-) create mode 100644 sys/dev/ic/am79900.c create mode 100644 sys/dev/ic/am79900reg.h create mode 100644 sys/dev/ic/am79900var.h create mode 100644 sys/dev/ic/lance.c create mode 100644 sys/dev/ic/lancereg.h create mode 100644 sys/dev/ic/lancevar.h diff --git a/sys/conf/files b/sys/conf/files index 1ef704f9d742..66c0f9267a85 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1,4 +1,4 @@ -# $NetBSD: files,v 1.224 1998/07/21 17:00:07 drochner Exp $ +# $NetBSD: files,v 1.225 1998/07/21 17:26:45 drochner Exp $ # @(#)files.newconf 7.5 (Berkeley) 5/10/93 @@ -184,7 +184,11 @@ file dev/ic/lemac.c lc # LANCE and PCnet Ethernet controllers device le: arp, ether, ifnet -file dev/ic/am7990.c le +define le24 +define le32 +file dev/ic/am7990.c le24 +file dev/ic/am79900.c le32 +file dev/ic/lance.c le24 | le32 # 8390-family Ethernet controllers define dp8390nic diff --git a/sys/dev/ic/am7990.c b/sys/dev/ic/am7990.c index be90edd78403..1c5c68eaf8fc 100644 --- a/sys/dev/ic/am7990.c +++ b/sys/dev/ic/am7990.c @@ -1,4 +1,4 @@ -/* $NetBSD: am7990.c,v 1.50 1998/07/07 14:13:51 drochner Exp $ */ +/* $NetBSD: am7990.c,v 1.51 1998/07/21 17:26:45 drochner Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -76,11 +76,6 @@ * @(#)if_le.c 8.2 (Berkeley) 11/16/93 */ -#include "opt_ddb.h" -#include "opt_inet.h" -#include "opt_ccitt.h" -#include "opt_llc.h" -#include "opt_ns.h" #include "bpfilter.h" #include "rnd.h" @@ -102,215 +97,67 @@ #include #include -#ifdef INET -#include -#include -#include -#include -#include -#endif - -#ifdef NS -#include -#include -#endif - -#if defined(CCITT) && defined(LLC) -#include -#include -#include -#include -#include -#endif - #if NBPFILTER > 0 #include #include #endif +#include +#include #include #include -#ifdef LEDEBUG -void am7990_recv_print __P((struct am7990_softc *, int)); -void am7990_xmit_print __P((struct am7990_softc *, int)); +void am7990_meminit __P((struct lance_softc *)); +void am7990_start __P((struct ifnet *)); + +#if defined(_KERNEL) && !defined(_LKM) +#include "opt_ddb.h" #endif -integrate void am7990_rint __P((struct am7990_softc *)); -integrate void am7990_tint __P((struct am7990_softc *)); +#ifdef DDB +#define integrate +#define hide +#else +#define integrate static __inline +#define hide static +#endif -integrate int am7990_put __P((struct am7990_softc *, int, struct mbuf *)); -integrate struct mbuf *am7990_get __P((struct am7990_softc *, int, int)); -integrate void am7990_read __P((struct am7990_softc *, int, int)); +integrate void am7990_rint __P((struct lance_softc *)); +integrate void am7990_tint __P((struct lance_softc *)); -hide void am7990_shutdown __P((void *)); - -int am7990_mediachange __P((struct ifnet *)); -void am7990_mediastatus __P((struct ifnet *, struct ifmediareq *)); +#ifdef LEDEBUG +void am7990_recv_print __P((struct lance_softc *, int)); +void am7990_xmit_print __P((struct lance_softc *, int)); +#endif #define ifp (&sc->sc_ethercom.ec_if) -static inline u_int16_t ether_cmp __P((void *, void *)); - -/* - * Compare two Ether/802 addresses for equality, inlined and - * unrolled for speed. Use this like bcmp(). - * - * XXX: Add for stuff like this? - * XXX: or maybe add it to libkern.h instead? - * - * "I'd love to have an inline assembler version of this." - * XXX: Who wanted that? mycroft? I wrote one, but this - * version in C is as good as hand-coded assembly. -gwr - * - * Please do NOT tweak this without looking at the actual - * assembly code generated before and after your tweaks! - */ -static inline u_int16_t -ether_cmp(one, two) - void *one, *two; -{ - register u_int16_t *a = (u_short *) one; - register u_int16_t *b = (u_short *) two; - register u_int16_t diff; - -#ifdef m68k - /* - * The post-increment-pointer form produces the best - * machine code for m68k. This was carefully tuned - * so it compiles to just 8 short (2-byte) op-codes! - */ - diff = *a++ - *b++; - diff |= *a++ - *b++; - diff |= *a++ - *b++; -#else - /* - * Most modern CPUs do better with a single expresion. - * Note that short-cut evaluation is NOT helpful here, - * because it just makes the code longer, not faster! - */ - diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]); -#endif - - return (diff); -} - -#define ETHER_CMP ether_cmp - -#ifdef LANCE_REVC_BUG -/* Make sure this is short-aligned, for ether_cmp(). */ -static u_int16_t bcast_enaddr[3] = { ~0, ~0, ~0 }; -#endif - void am7990_config(sc) struct am7990_softc *sc; { int mem, i; - /* Make sure the chip is stopped. */ - am7990_stop(sc); + sc->lsc.sc_meminit = am7990_meminit; + sc->lsc.sc_start = am7990_start; - /* Initialize ifnet structure. */ - bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ); - ifp->if_softc = sc; - ifp->if_start = am7990_start; - ifp->if_ioctl = am7990_ioctl; - ifp->if_watchdog = am7990_watchdog; - ifp->if_flags = - IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST; -#ifdef LANCE_REVC_BUG - ifp->if_flags &= ~IFF_MULTICAST; -#endif - - /* Initialize ifmedia structures. */ - ifmedia_init(&sc->sc_media, 0, am7990_mediachange, am7990_mediastatus); - if (sc->sc_supmedia != NULL) { - for (i = 0; i < sc->sc_nsupmedia; i++) - ifmedia_add(&sc->sc_media, sc->sc_supmedia[i], - 0, NULL); - ifmedia_set(&sc->sc_media, sc->sc_defaultmedia); - } else { - ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); - ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL); - } - - /* Attach the interface. */ - if_attach(ifp); - ether_ifattach(ifp, sc->sc_enaddr); - -#if NBPFILTER > 0 - bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header)); -#endif - - switch (sc->sc_memsize) { - case 8192: - sc->sc_nrbuf = 4; - sc->sc_ntbuf = 1; - break; - case 16384: - sc->sc_nrbuf = 8; - sc->sc_ntbuf = 2; - break; - case 32768: - sc->sc_nrbuf = 16; - sc->sc_ntbuf = 4; - break; - case 65536: - sc->sc_nrbuf = 32; - sc->sc_ntbuf = 8; - break; - case 131072: - sc->sc_nrbuf = 64; - sc->sc_ntbuf = 16; - break; - default: - panic("am7990_config: weird memory size"); - } - - printf(": address %s\n", ether_sprintf(sc->sc_enaddr)); - printf("%s: %d receive buffers, %d transmit buffers\n", - sc->sc_dev.dv_xname, sc->sc_nrbuf, sc->sc_ntbuf); - - sc->sc_sh = shutdownhook_establish(am7990_shutdown, sc); - if (sc->sc_sh == NULL) - panic("am7990_config: can't establish shutdownhook"); - sc->sc_rbufaddr = malloc(sc->sc_nrbuf * sizeof(int), M_DEVBUF, - M_WAITOK); - sc->sc_tbufaddr = malloc(sc->sc_ntbuf * sizeof(int), M_DEVBUF, - M_WAITOK); + lance_config(&sc->lsc); mem = 0; - sc->sc_initaddr = mem; + sc->lsc.sc_initaddr = mem; mem += sizeof(struct leinit); - sc->sc_rmdaddr = mem; - mem += sizeof(struct lermd) * sc->sc_nrbuf; - sc->sc_tmdaddr = mem; - mem += sizeof(struct letmd) * sc->sc_ntbuf; - for (i = 0; i < sc->sc_nrbuf; i++, mem += LEBLEN) - sc->sc_rbufaddr[i] = mem; - for (i = 0; i < sc->sc_ntbuf; i++, mem += LEBLEN) - sc->sc_tbufaddr[i] = mem; + sc->lsc.sc_rmdaddr = mem; + mem += sizeof(struct lermd) * sc->lsc.sc_nrbuf; + sc->lsc.sc_tmdaddr = mem; + mem += sizeof(struct letmd) * sc->lsc.sc_ntbuf; + for (i = 0; i < sc->lsc.sc_nrbuf; i++, mem += LEBLEN) + sc->lsc.sc_rbufaddr[i] = mem; + for (i = 0; i < sc->lsc.sc_ntbuf; i++, mem += LEBLEN) + sc->lsc.sc_tbufaddr[i] = mem; #ifdef notyet if (mem > ...) panic(...); #endif - -#if NRND > 0 - rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname, - RND_TYPE_NET); -#endif -} - -void -am7990_reset(sc) - struct am7990_softc *sc; -{ - int s; - - s = splimp(); - am7990_init(sc); - splx(s); } /* @@ -318,7 +165,7 @@ am7990_reset(sc) */ void am7990_meminit(sc) - register struct am7990_softc *sc; + register struct lance_softc *sc; { u_long a; int bix; @@ -346,7 +193,7 @@ am7990_meminit(sc) init.init_padr[0] = (myaddr[1] << 8) | myaddr[0]; init.init_padr[1] = (myaddr[3] << 8) | myaddr[2]; init.init_padr[2] = (myaddr[5] << 8) | myaddr[4]; - am7990_setladrf(&sc->sc_ethercom, init.init_ladrf); + lance_setladrf(&sc->sc_ethercom, init.init_ladrf); sc->sc_last_rd = 0; sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0; @@ -390,238 +237,9 @@ am7990_meminit(sc) } } -void -am7990_stop(sc) - struct am7990_softc *sc; -{ - - (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP); -} - -/* - * Initialization of interface; set up initialization block - * and transmit/receive descriptor rings. - */ -void -am7990_init(sc) - register struct am7990_softc *sc; -{ - register int timo; - u_long a; - - (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP); - DELAY(100); - - /* Newer LANCE chips have a reset register */ - if (sc->sc_hwreset) - (*sc->sc_hwreset)(sc); - - /* Set the correct byte swapping mode, etc. */ - (*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3); - - /* Set up LANCE init block. */ - am7990_meminit(sc); - - /* Give LANCE the physical address of its init block. */ - a = sc->sc_addr + LE_INITADDR(sc); - (*sc->sc_wrcsr)(sc, LE_CSR1, a); - (*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16); - - /* Try to initialize the LANCE. */ - DELAY(100); - (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT); - - /* Wait for initialization to finish. */ - for (timo = 100000; timo; timo--) - if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) - break; - - if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) { - /* Start the LANCE. */ - (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT | - LE_C0_IDON); - ifp->if_flags |= IFF_RUNNING; - ifp->if_flags &= ~IFF_OACTIVE; - ifp->if_timer = 0; - am7990_start(ifp); - } else - printf("%s: controller failed to initialize\n", - sc->sc_dev.dv_xname); - if (sc->sc_hwinit) - (*sc->sc_hwinit)(sc); -} - -/* - * Routine to copy from mbuf chain to transmit buffer in - * network buffer memory. - */ -integrate int -am7990_put(sc, boff, m) - struct am7990_softc *sc; - int boff; - register struct mbuf *m; -{ - register struct mbuf *n; - register int len, tlen = 0; - - for (; m; m = n) { - len = m->m_len; - if (len == 0) { - MFREE(m, n); - continue; - } - (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len); - boff += len; - tlen += len; - MFREE(m, n); - } - if (tlen < LEMINSIZE) { - (*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen); - tlen = LEMINSIZE; - } - return (tlen); -} - -/* - * Pull data off an interface. - * Len is length of data, with local net header stripped. - * We copy the data into mbufs. When full cluster sized units are present - * we copy into clusters. - */ -integrate struct mbuf * -am7990_get(sc, boff, totlen) - struct am7990_softc *sc; - int boff, totlen; -{ - register struct mbuf *m; - struct mbuf *top, **mp; - int len; - - MGETHDR(m, M_DONTWAIT, MT_DATA); - if (m == 0) - return (0); - m->m_pkthdr.rcvif = ifp; - m->m_pkthdr.len = totlen; - len = MHLEN; - top = 0; - mp = ⊤ - - while (totlen > 0) { - if (top) { - MGET(m, M_DONTWAIT, MT_DATA); - if (m == 0) { - m_freem(top); - return 0; - } - len = MLEN; - } - if (totlen >= MINCLSIZE) { - MCLGET(m, M_DONTWAIT); - if ((m->m_flags & M_EXT) == 0) { - m_free(m); - m_freem(top); - return 0; - } - len = MCLBYTES; - } - if (!top) { - register int pad = - ALIGN(sizeof(struct ether_header)) - - sizeof(struct ether_header); - m->m_data += pad; - len -= pad; - } - m->m_len = len = min(totlen, len); - (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len); - boff += len; - totlen -= len; - *mp = m; - mp = &m->m_next; - } - - return (top); -} - -/* - * Pass a packet to the higher levels. - */ -integrate void -am7990_read(sc, boff, len) - register struct am7990_softc *sc; - int boff, len; -{ - struct mbuf *m; - struct ether_header *eh; - - if (len <= sizeof(struct ether_header) || - len > ETHERMTU + sizeof(struct ether_header)) { -#ifdef LEDEBUG - printf("%s: invalid packet size %d; dropping\n", - sc->sc_dev.dv_xname, len); -#endif - ifp->if_ierrors++; - return; - } - - /* Pull packet off interface. */ - m = am7990_get(sc, boff, len); - if (m == 0) { - ifp->if_ierrors++; - return; - } - - ifp->if_ipackets++; - - /* We assume that the header fit entirely in one mbuf. */ - eh = mtod(m, struct ether_header *); - -#if NBPFILTER > 0 - /* - * Check if there's a BPF listener on this interface. - * If so, hand off the raw packet to BPF. - */ - if (ifp->if_bpf) { - bpf_mtap(ifp->if_bpf, m); - -#ifndef LANCE_REVC_BUG - /* - * Note that the interface cannot be in promiscuous mode if - * there are no BPF listeners. And if we are in promiscuous - * mode, we have to check if this packet is really ours. - */ - if ((ifp->if_flags & IFF_PROMISC) != 0 && - (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */ - ETHER_CMP(eh->ether_dhost, sc->sc_enaddr)) { - m_freem(m); - return; - } -#endif - } -#endif - -#ifdef LANCE_REVC_BUG - /* - * The old LANCE (Rev. C) chips have a bug which causes - * garbage to be inserted in front of the received packet. - * The work-around is to ignore packets with an invalid - * destination address (garbage will usually not match). - * Of course, this precludes multicast support... - */ - if (ETHER_CMP(eh->ether_dhost, sc->sc_enaddr) && - ETHER_CMP(eh->ether_dhost, bcast_enaddr)) { - m_freem(m); - return; - } -#endif - - /* Pass the packet up, with the ether header sort-of removed. */ - m_adj(m, sizeof(struct ether_header)); - ether_input(ifp, eh, m); -} - integrate void am7990_rint(sc) - struct am7990_softc *sc; + struct lance_softc *sc; { register int bix; int rp; @@ -668,8 +286,8 @@ am7990_rint(sc) if (sc->sc_debug) am7990_recv_print(sc, sc->sc_last_rd); #endif - am7990_read(sc, LE_RBUFADDR(sc, bix), - (int)rmd.rmd3 - 4); + lance_read(sc, LE_RBUFADDR(sc, bix), + (int)rmd.rmd3 - 4); } rmd.rmd1_bits = LE_R1_OWN; @@ -696,7 +314,7 @@ am7990_rint(sc) integrate void am7990_tint(sc) - register struct am7990_softc *sc; + register struct lance_softc *sc; { register int bix; struct letmd tmd; @@ -731,7 +349,7 @@ am7990_tint(sc) else if (tmd.tmd3 & LE_T3_UFLO) printf("%s: underflow\n", sc->sc_dev.dv_xname); if (tmd.tmd3 & (LE_T3_BUFF | LE_T3_UFLO)) { - am7990_reset(sc); + lance_reset((struct lance_softc *)sc); return; } if (tmd.tmd3 & LE_T3_LCAR) { @@ -781,7 +399,7 @@ int am7990_intr(arg) register void *arg; { - register struct am7990_softc *sc = arg; + register struct lance_softc *sc = arg; register u_int16_t isr; isr = (*sc->sc_rdcsr)(sc, LE_CSR0) | sc->sc_saved_csr0; @@ -818,7 +436,7 @@ am7990_intr(arg) } if (isr & LE_C0_MERR) { printf("%s: memory error\n", sc->sc_dev.dv_xname); - am7990_reset(sc); + lance_reset(sc); return (1); } } @@ -826,13 +444,13 @@ am7990_intr(arg) if ((isr & LE_C0_RXON) == 0) { printf("%s: receiver disabled\n", sc->sc_dev.dv_xname); ifp->if_ierrors++; - am7990_reset(sc); + lance_reset(sc); return (1); } if ((isr & LE_C0_TXON) == 0) { printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname); ifp->if_oerrors++; - am7990_reset(sc); + lance_reset(sc); return (1); } @@ -854,48 +472,7 @@ am7990_intr(arg) return (1); } -#undef ifp - -void -am7990_watchdog(ifp) - struct ifnet *ifp; -{ - struct am7990_softc *sc = ifp->if_softc; - - log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname); - ++ifp->if_oerrors; - - am7990_reset(sc); -} - -int -am7990_mediachange(ifp) - struct ifnet *ifp; -{ - struct am7990_softc *sc = ifp->if_softc; - - if (sc->sc_mediachange) - return ((*sc->sc_mediachange)(sc)); - return (EINVAL); -} - -void -am7990_mediastatus(ifp, ifmr) - struct ifnet *ifp; - struct ifmediareq *ifmr; -{ - struct am7990_softc *sc = ifp->if_softc; - - if ((ifp->if_flags & IFF_UP) == 0) - return; - - ifmr->ifm_status = IFM_AVALID; - if (sc->sc_havecarrier) - ifmr->ifm_status |= IFM_ACTIVE; - - if (sc->sc_mediastatus) - (*sc->sc_mediastatus)(sc, ifmr); -} +#undef ifp /* * Setup output on interface. @@ -907,7 +484,7 @@ void am7990_start(ifp) register struct ifnet *ifp; { - register struct am7990_softc *sc = ifp->if_softc; + register struct lance_softc *sc = ifp->if_softc; register int bix; register struct mbuf *m; struct letmd tmd; @@ -945,7 +522,7 @@ am7990_start(ifp) /* * Copy the mbuf chain into the transmit buffer. */ - len = am7990_put(sc, LE_TBUFADDR(sc, bix), m); + len = lance_put(sc, LE_TBUFADDR(sc, bix), m); #ifdef LEDEBUG if (len > ETHERMTU + sizeof(struct ether_header)) @@ -983,142 +560,10 @@ am7990_start(ifp) sc->sc_last_td = bix; } -/* - * Process an ioctl request. - */ -int -am7990_ioctl(ifp, cmd, data) - register struct ifnet *ifp; - u_long cmd; - caddr_t data; -{ - register struct am7990_softc *sc = ifp->if_softc; - struct ifaddr *ifa = (struct ifaddr *)data; - struct ifreq *ifr = (struct ifreq *)data; - int s, error = 0; - - s = splimp(); - - switch (cmd) { - - case SIOCSIFADDR: - ifp->if_flags |= IFF_UP; - - switch (ifa->ifa_addr->sa_family) { -#ifdef INET - case AF_INET: - am7990_init(sc); - arp_ifinit(ifp, ifa); - break; -#endif -#ifdef NS - case AF_NS: - { - register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr; - - if (ns_nullhost(*ina)) - ina->x_host = - *(union ns_host *)LLADDR(ifp->if_sadl); - else { - bcopy(ina->x_host.c_host, - LLADDR(ifp->if_sadl), - sizeof(sc->sc_enaddr)); - } - /* Set new address. */ - am7990_init(sc); - break; - } -#endif - default: - am7990_init(sc); - break; - } - break; - -#if defined(CCITT) && defined(LLC) - case SIOCSIFCONF_X25: - ifp->if_flags |= IFF_UP; - ifa->ifa_rtrequest = cons_rtrequest; /* XXX */ - error = x25_llcglue(PRC_IFUP, ifa->ifa_addr); - if (error == 0) - am7990_init(sc); - break; -#endif /* CCITT && LLC */ - - case SIOCSIFFLAGS: - if ((ifp->if_flags & IFF_UP) == 0 && - (ifp->if_flags & IFF_RUNNING) != 0) { - /* - * If interface is marked down and it is running, then - * stop it. - */ - am7990_stop(sc); - ifp->if_flags &= ~IFF_RUNNING; - } else if ((ifp->if_flags & IFF_UP) != 0 && - (ifp->if_flags & IFF_RUNNING) == 0) { - /* - * If interface is marked up and it is stopped, then - * start it. - */ - am7990_init(sc); - } else { - /* - * Reset the interface to pick up changes in any other - * flags that affect hardware registers. - */ - /*am7990_stop(sc);*/ - am7990_init(sc); - } -#ifdef LEDEBUG - if (ifp->if_flags & IFF_DEBUG) - sc->sc_debug = 1; - else - sc->sc_debug = 0; -#endif - break; - - case SIOCADDMULTI: - case SIOCDELMULTI: - error = (cmd == SIOCADDMULTI) ? - ether_addmulti(ifr, &sc->sc_ethercom) : - ether_delmulti(ifr, &sc->sc_ethercom); - - if (error == ENETRESET) { - /* - * Multicast list has changed; set the hardware filter - * accordingly. - */ - am7990_reset(sc); - error = 0; - } - break; - - case SIOCGIFMEDIA: - case SIOCSIFMEDIA: - error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); - break; - - default: - error = EINVAL; - break; - } - - splx(s); - return (error); -} - -hide void -am7990_shutdown(arg) - void *arg; -{ - - am7990_stop((struct am7990_softc *)arg); -} - #ifdef LEDEBUG void am7990_recv_print(sc, no) - struct am7990_softc *sc; + struct lance_softc *sc; int no; { struct lermd rmd; @@ -1145,7 +590,7 @@ am7990_recv_print(sc, no) void am7990_xmit_print(sc, no) - struct am7990_softc *sc; + struct lance_softc *sc; int no; { struct letmd tmd; @@ -1170,302 +615,3 @@ am7990_xmit_print(sc, no) } } #endif /* LEDEBUG */ - -/* - * Set up the logical address filter. - */ -void -am7990_setladrf(ac, af) - struct ethercom *ac; - u_int16_t *af; -{ - struct ifnet *ifp = &ac->ec_if; - struct ether_multi *enm; - register u_char *cp; - register u_int32_t crc; - static const u_int32_t crctab[] = { - 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, - 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, - 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, - 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c - }; - register int len; - struct ether_multistep step; - - /* - * Set up multicast address filter by passing all multicast addresses - * through a crc generator, and then using the high order 6 bits as an - * index into the 64 bit logical address filter. The high order bit - * selects the word, while the rest of the bits select the bit within - * the word. - */ - - if (ifp->if_flags & IFF_PROMISC) - goto allmulti; - - af[0] = af[1] = af[2] = af[3] = 0x0000; - ETHER_FIRST_MULTI(step, ac, enm); - while (enm != NULL) { - if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) { - /* - * We must listen to a range of multicast addresses. - * For now, just accept all multicasts, rather than - * trying to set only those filter bits needed to match - * the range. (At this time, the only use of address - * ranges is for IP multicast routing, for which the - * range is big enough to require all bits set.) - */ - goto allmulti; - } - - cp = enm->enm_addrlo; - crc = 0xffffffff; - for (len = sizeof(enm->enm_addrlo); --len >= 0;) { - crc ^= *cp++; - crc = (crc >> 4) ^ crctab[crc & 0xf]; - crc = (crc >> 4) ^ crctab[crc & 0xf]; - } - /* Just want the 6 most significant bits. */ - crc >>= 26; - - /* Set the corresponding bit in the filter. */ - af[crc >> 4] |= 1 << (crc & 0xf); - - ETHER_NEXT_MULTI(step, enm); - } - ifp->if_flags &= ~IFF_ALLMULTI; - return; - -allmulti: - ifp->if_flags |= IFF_ALLMULTI; - af[0] = af[1] = af[2] = af[3] = 0xffff; -} - - -/* - * Routines for accessing the transmit and receive buffers. - * The various CPU and adapter configurations supported by this - * driver require three different access methods for buffers - * and descriptors: - * (1) contig (contiguous data; no padding), - * (2) gap2 (two bytes of data followed by two bytes of padding), - * (3) gap16 (16 bytes of data followed by 16 bytes of padding). - */ - -/* - * contig: contiguous data with no padding. - * - * Buffers may have any alignment. - */ - -void -am7990_copytobuf_contig(sc, from, boff, len) - struct am7990_softc *sc; - void *from; - int boff, len; -{ - volatile caddr_t buf = sc->sc_mem; - - /* - * Just call bcopy() to do the work. - */ - bcopy(from, buf + boff, len); -} - -void -am7990_copyfrombuf_contig(sc, to, boff, len) - struct am7990_softc *sc; - void *to; - int boff, len; -{ - volatile caddr_t buf = sc->sc_mem; - - /* - * Just call bcopy() to do the work. - */ - bcopy(buf + boff, to, len); -} - -void -am7990_zerobuf_contig(sc, boff, len) - struct am7990_softc *sc; - int boff, len; -{ - volatile caddr_t buf = sc->sc_mem; - - /* - * Just let bzero() do the work - */ - bzero(buf + boff, len); -} - -#if 0 -/* - * Examples only; duplicate these and tweak (if necessary) in - * machine-specific front-ends. - */ - -/* - * gap2: two bytes of data followed by two bytes of pad. - * - * Buffers must be 4-byte aligned. The code doesn't worry about - * doing an extra byte. - */ - -void -am7990_copytobuf_gap2(sc, fromv, boff, len) - struct am7990_softc *sc; - void *fromv; - int boff; - register int len; -{ - volatile caddr_t buf = sc->sc_mem; - register caddr_t from = fromv; - register volatile u_int16_t *bptr; - - if (boff & 0x1) { - /* handle unaligned first byte */ - bptr = ((volatile u_int16_t *)buf) + (boff - 1); - *bptr = (*from++ << 8) | (*bptr & 0xff); - bptr += 2; - len--; - } else - bptr = ((volatile u_int16_t *)buf) + boff; - while (len > 1) { - *bptr = (from[1] << 8) | (from[0] & 0xff); - bptr += 2; - from += 2; - len -= 2; - } - if (len == 1) - *bptr = (u_int16_t)*from; -} - -void -am7990_copyfrombuf_gap2(sc, tov, boff, len) - struct am7990_softc *sc; - void *tov; - int boff, len; -{ - volatile caddr_t buf = sc->sc_mem; - register caddr_t to = tov; - register volatile u_int16_t *bptr; - register u_int16_t tmp; - - if (boff & 0x1) { - /* handle unaligned first byte */ - bptr = ((volatile u_int16_t *)buf) + (boff - 1); - *to++ = (*bptr >> 8) & 0xff; - bptr += 2; - len--; - } else - bptr = ((volatile u_int16_t *)buf) + boff; - while (len > 1) { - tmp = *bptr; - *to++ = tmp & 0xff; - *to++ = (tmp >> 8) & 0xff; - bptr += 2; - len -= 2; - } - if (len == 1) - *to = *bptr & 0xff; -} - -void -am7990_zerobuf_gap2(sc, boff, len) - struct am7990_softc *sc; - int boff, len; -{ - volatile caddr_t buf = sc->sc_mem; - register volatile u_int16_t *bptr; - - if ((unsigned)boff & 0x1) { - bptr = ((volatile u_int16_t *)buf) + (boff - 1); - *bptr &= 0xff; - bptr += 2; - len--; - } else - bptr = ((volatile u_int16_t *)buf) + boff; - while (len > 0) { - *bptr = 0; - bptr += 2; - len -= 2; - } -} - -/* - * gap16: 16 bytes of data followed by 16 bytes of pad. - * - * Buffers must be 32-byte aligned. - */ - -void -am7990_copytobuf_gap16(sc, fromv, boff, len) - struct am7990_softc *sc; - void *fromv; - int boff; - register int len; -{ - volatile caddr_t buf = sc->sc_mem; - register caddr_t from = fromv; - register caddr_t bptr; - register int xfer; - - bptr = buf + ((boff << 1) & ~0x1f); - boff &= 0xf; - xfer = min(len, 16 - boff); - while (len > 0) { - bcopy(from, bptr + boff, xfer); - from += xfer; - bptr += 32; - boff = 0; - len -= xfer; - xfer = min(len, 16); - } -} - -void -am7990_copyfrombuf_gap16(sc, tov, boff, len) - struct am7990_softc *sc; - void *tov; - int boff, len; -{ - volatile caddr_t buf = sc->sc_mem; - register caddr_t to = tov; - register caddr_t bptr; - register int xfer; - - bptr = buf + ((boff << 1) & ~0x1f); - boff &= 0xf; - xfer = min(len, 16 - boff); - while (len > 0) { - bcopy(bptr + boff, to, xfer); - to += xfer; - bptr += 32; - boff = 0; - len -= xfer; - xfer = min(len, 16); - } -} - -void -am7990_zerobuf_gap16(sc, boff, len) - struct am7990_softc *sc; - int boff, len; -{ - volatile caddr_t buf = sc->sc_mem; - register caddr_t bptr; - register int xfer; - - bptr = buf + ((boff << 1) & ~0x1f); - boff &= 0xf; - xfer = min(len, 16 - boff); - while (len > 0) { - bzero(bptr + boff, xfer); - bptr += 32; - boff = 0; - len -= xfer; - xfer = min(len, 16); - } -} -#endif /* Example only */ diff --git a/sys/dev/ic/am79900.c b/sys/dev/ic/am79900.c new file mode 100644 index 000000000000..f2d2ce1ef3f1 --- /dev/null +++ b/sys/dev/ic/am79900.c @@ -0,0 +1,570 @@ +/* $NetBSD: am79900.c,v 1.1 1998/07/21 17:26:46 drochner Exp $ */ + +/*- + * Copyright (c) 1998 + * Matthias Drochner. All rights reserved. + * Copyright (c) 1997 Jason R. Thorpe. All rights reserved. + * Copyright (c) 1995 Charles M. Hannum. All rights reserved. + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell and Rick Macklem. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)if_le.c 8.2 (Berkeley) 11/16/93 + */ + +#include "bpfilter.h" +#include "rnd.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if NRND > 0 +#include +#endif + +#include +#include +#include +#include + +#if NBPFILTER > 0 +#include +#include +#endif + +#include +#include +#include +#include + +void am79900_meminit __P((struct lance_softc *)); +void am79900_start __P((struct ifnet *)); + +#if defined(_KERNEL) && !defined(_LKM) +#include "opt_ddb.h" +#endif + +#ifdef DDB +#define integrate +#define hide +#else +#define integrate static __inline +#define hide static +#endif + +integrate void am79900_rint __P((struct lance_softc *)); +integrate void am79900_tint __P((struct lance_softc *)); + +#ifdef LEDEBUG +void am79900_recv_print __P((struct lance_softc *, int)); +void am79900_xmit_print __P((struct lance_softc *, int)); +#endif + +#define ifp (&sc->sc_ethercom.ec_if) + +void +am79900_config(sc) + struct am79900_softc *sc; +{ + int mem, i; + + sc->lsc.sc_meminit = am79900_meminit; + sc->lsc.sc_start = am79900_start; + + lance_config(&sc->lsc); + + mem = 0; + sc->lsc.sc_initaddr = mem; + mem += sizeof(struct leinit); + sc->lsc.sc_rmdaddr = mem; + mem += sizeof(struct lermd) * sc->lsc.sc_nrbuf; + sc->lsc.sc_tmdaddr = mem; + mem += sizeof(struct letmd) * sc->lsc.sc_ntbuf; + for (i = 0; i < sc->lsc.sc_nrbuf; i++, mem += LEBLEN) + sc->lsc.sc_rbufaddr[i] = mem; + for (i = 0; i < sc->lsc.sc_ntbuf; i++, mem += LEBLEN) + sc->lsc.sc_tbufaddr[i] = mem; + + if (mem > sc->lsc.sc_memsize) + panic("%s: memsize", sc->lsc.sc_dev.dv_xname); +} + +/* + * Set up the initialization block and the descriptor rings. + */ +void +am79900_meminit(sc) + register struct lance_softc *sc; +{ + u_long a; + int bix; + struct leinit init; + struct lermd rmd; + struct letmd tmd; + u_int8_t *myaddr; + +#if NBPFILTER > 0 + if (ifp->if_flags & IFF_PROMISC) + init.init_mode = LE_MODE_NORMAL | LE_MODE_PROM; + else +#endif + init.init_mode = LE_MODE_NORMAL; + if (sc->sc_initmodemedia == 1) + init.init_mode |= LE_MODE_PSEL0; + + init.init_mode |= ((ffs(sc->sc_ntbuf) - 1) << 28) + | ((ffs(sc->sc_nrbuf) - 1) << 20); + + /* + * Update our private copy of the Ethernet address. + * We NEED the copy so we can ensure its alignment! + */ + bcopy(LLADDR(ifp->if_sadl), sc->sc_enaddr, 6); + myaddr = sc->sc_enaddr; + + init.init_padr[0] = myaddr[0] | (myaddr[1] << 8) + | (myaddr[2] << 16) | (myaddr[3] << 24); + init.init_padr[1] = myaddr[4] | (myaddr[5] << 8); + lance_setladrf(&sc->sc_ethercom, init.init_ladrf); + + sc->sc_last_rd = 0; + sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0; + + a = sc->sc_addr + LE_RMDADDR(sc, 0); + init.init_rdra = a; + + a = sc->sc_addr + LE_TMDADDR(sc, 0); + init.init_tdra = a; + + (*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init)); + + /* + * Set up receive ring descriptors. + */ + for (bix = 0; bix < sc->sc_nrbuf; bix++) { + a = sc->sc_addr + LE_RBUFADDR(sc, bix); + rmd.rmd0 = a; + rmd.rmd1 = LE_R1_OWN | LE_R1_ONES | (-LEBLEN & 0xfff); + rmd.rmd2 = 0; + rmd.rmd3 = 0; + (*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix), + sizeof(rmd)); + } + + /* + * Set up transmit ring descriptors. + */ + for (bix = 0; bix < sc->sc_ntbuf; bix++) { + a = sc->sc_addr + LE_TBUFADDR(sc, bix); + tmd.tmd0 = a; + tmd.tmd1 = LE_T1_ONES; + tmd.tmd2 = 0; + tmd.tmd3 = 0; + (*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix), + sizeof(tmd)); + } +} + +integrate void +am79900_rint(sc) + struct lance_softc *sc; +{ + register int bix; + int rp; + struct lermd rmd; + + bix = sc->sc_last_rd; + + /* Process all buffers with valid data. */ + for (;;) { + rp = LE_RMDADDR(sc, bix); + (*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd)); + + if (rmd.rmd1 & LE_R1_OWN) + break; + + if (rmd.rmd1 & LE_R1_ERR) { + if (rmd.rmd1 & LE_R1_ENP) { +#ifdef LEDEBUG + if ((rmd.rmd1 & LE_R1_OFLO) == 0) { + if (rmd.rmd1 & LE_R1_FRAM) + printf("%s: framing error\n", + sc->sc_dev.dv_xname); + if (rmd.rmd1 & LE_R1_CRC) + printf("%s: crc mismatch\n", + sc->sc_dev.dv_xname); + } +#endif + } else { + if (rmd.rmd1 & LE_R1_OFLO) + printf("%s: overflow\n", + sc->sc_dev.dv_xname); + } + if (rmd.rmd1 & LE_R1_BUFF) + printf("%s: receive buffer error\n", + sc->sc_dev.dv_xname); + ifp->if_ierrors++; + } else if ((rmd.rmd1 & (LE_R1_STP | LE_R1_ENP)) != + (LE_R1_STP | LE_R1_ENP)) { + printf("%s: dropping chained buffer\n", + sc->sc_dev.dv_xname); + ifp->if_ierrors++; + } else { +#ifdef LEDEBUG + if (sc->sc_debug) + am79900_recv_print(sc, sc->sc_last_rd); +#endif + lance_read(sc, LE_RBUFADDR(sc, bix), + (rmd.rmd2 & 0xfff) - 4); + } + + rmd.rmd1 = LE_R1_OWN | LE_R1_ONES | (-LEBLEN & 0xfff); + rmd.rmd2 = 0; + rmd.rmd3 = 0; + (*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd)); + +#ifdef LEDEBUG + if (sc->sc_debug) + printf("sc->sc_last_rd = %x, rmd: " + "adr %08x, flags/blen %08x\n", + sc->sc_last_rd, + rmd.rmd0, rmd.rmd1); +#endif + + if (++bix == sc->sc_nrbuf) + bix = 0; + } + + sc->sc_last_rd = bix; +} + +integrate void +am79900_tint(sc) + register struct lance_softc *sc; +{ + register int bix; + struct letmd tmd; + + bix = sc->sc_first_td; + + for (;;) { + if (sc->sc_no_td <= 0) + break; + + (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix), + sizeof(tmd)); + +#ifdef LEDEBUG + if (sc->sc_debug) + printf("trans tmd: " + "adr %08x, flags/blen %08x\n", + tmd.tmd0, tmd.tmd1); +#endif + + if (tmd.tmd1 & LE_T1_OWN) + break; + + ifp->if_flags &= ~IFF_OACTIVE; + + if (tmd.tmd1 & LE_T1_ERR) { + if (tmd.tmd2 & LE_T2_BUFF) + printf("%s: transmit buffer error\n", + sc->sc_dev.dv_xname); + else if (tmd.tmd2 & LE_T2_UFLO) + printf("%s: underflow\n", sc->sc_dev.dv_xname); + if (tmd.tmd2 & (LE_T2_BUFF | LE_T2_UFLO)) { + lance_reset(sc); + return; + } + if (tmd.tmd2 & LE_T2_LCAR) { + sc->sc_havecarrier = 0; + if (sc->sc_nocarrier) + (*sc->sc_nocarrier)(sc); + else + printf("%s: lost carrier\n", + sc->sc_dev.dv_xname); + } + if (tmd.tmd2 & LE_T2_LCOL) + ifp->if_collisions++; + if (tmd.tmd2 & LE_T2_RTRY) { + printf("%s: excessive collisions\n", + sc->sc_dev.dv_xname); + ifp->if_collisions += 16; + } + ifp->if_oerrors++; + } else { + if (tmd.tmd1 & LE_T1_ONE) + ifp->if_collisions++; + else if (tmd.tmd1 & LE_T1_MORE) + /* Real number is unknown. */ + ifp->if_collisions += 2; + ifp->if_opackets++; + } + + if (++bix == sc->sc_ntbuf) + bix = 0; + + --sc->sc_no_td; + } + + sc->sc_first_td = bix; + + am79900_start(ifp); + + if (sc->sc_no_td == 0) + ifp->if_timer = 0; +} + +/* + * Controller interrupt. + */ +int +am79900_intr(arg) + register void *arg; +{ + register struct lance_softc *sc = arg; + register u_int16_t isr; + + isr = (*sc->sc_rdcsr)(sc, LE_CSR0) | sc->sc_saved_csr0; + sc->sc_saved_csr0 = 0; +#ifdef LEDEBUG + if (sc->sc_debug) + printf("%s: am79900_intr entering with isr=%04x\n", + sc->sc_dev.dv_xname, isr); +#endif + if ((isr & LE_C0_INTR) == 0) + return (0); + + (*sc->sc_wrcsr)(sc, LE_CSR0, + isr & (LE_C0_INEA | LE_C0_BABL | LE_C0_MISS | LE_C0_MERR | + LE_C0_RINT | LE_C0_TINT | LE_C0_IDON)); + if (isr & LE_C0_ERR) { + if (isr & LE_C0_BABL) { +#ifdef LEDEBUG + printf("%s: babble\n", sc->sc_dev.dv_xname); +#endif + ifp->if_oerrors++; + } +#if 0 + if (isr & LE_C0_CERR) { + printf("%s: collision error\n", sc->sc_dev.dv_xname); + ifp->if_collisions++; + } +#endif + if (isr & LE_C0_MISS) { +#ifdef LEDEBUG + printf("%s: missed packet\n", sc->sc_dev.dv_xname); +#endif + ifp->if_ierrors++; + } + if (isr & LE_C0_MERR) { + printf("%s: memory error\n", sc->sc_dev.dv_xname); + lance_reset(sc); + return (1); + } + } + + if ((isr & LE_C0_RXON) == 0) { + printf("%s: receiver disabled\n", sc->sc_dev.dv_xname); + ifp->if_ierrors++; + lance_reset(sc); + return (1); + } + if ((isr & LE_C0_TXON) == 0) { + printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname); + ifp->if_oerrors++; + lance_reset(sc); + return (1); + } + + /* + * Pretend we have carrier; if we don't this will be cleared + * shortly. + */ + sc->sc_havecarrier = 1; + + if (isr & LE_C0_RINT) + am79900_rint(sc); + if (isr & LE_C0_TINT) + am79900_tint(sc); + + return (1); +} + +#undef ifp + +/* + * Setup output on interface. + * Get another datagram to send off of the interface queue, and map it to the + * interface before starting the output. + * Called only at splimp or interrupt level. + */ +void +am79900_start(ifp) + register struct ifnet *ifp; +{ + register struct lance_softc *sc = ifp->if_softc; + register int bix; + register struct mbuf *m; + struct letmd tmd; + int rp; + int len; + + if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) + return; + + bix = sc->sc_last_td; + + for (;;) { + rp = LE_TMDADDR(sc, bix); + (*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd)); + + if (tmd.tmd1 & LE_T1_OWN) { + ifp->if_flags |= IFF_OACTIVE; + printf("missing buffer, no_td = %d, last_td = %d\n", + sc->sc_no_td, sc->sc_last_td); + } + + IF_DEQUEUE(&ifp->if_snd, m); + if (m == 0) + break; + +#if NBPFILTER > 0 + /* + * If BPF is listening on this interface, let it see the packet + * before we commit it to the wire. + */ + if (ifp->if_bpf) + bpf_mtap(ifp->if_bpf, m); +#endif + + /* + * Copy the mbuf chain into the transmit buffer. + */ + len = lance_put(sc, LE_TBUFADDR(sc, bix), m); + +#ifdef LEDEBUG + if (len > ETHERMTU + sizeof(struct ether_header)) + printf("packet length %d\n", len); +#endif + + ifp->if_timer = 5; + + /* + * Init transmit registers, and set transmit start flag. + */ + tmd.tmd1 = LE_T1_OWN | LE_T1_STP | LE_T1_ENP | LE_T1_ONES | (-len & 0xfff); + tmd.tmd2 = 0; + tmd.tmd3 = 0; + + (*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd)); + +#ifdef LEDEBUG + if (sc->sc_debug) + am79900_xmit_print(sc, sc->sc_last_td); +#endif + + (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD); + + if (++bix == sc->sc_ntbuf) + bix = 0; + + if (++sc->sc_no_td == sc->sc_ntbuf) { + ifp->if_flags |= IFF_OACTIVE; + break; + } + + } + + sc->sc_last_td = bix; +} + +#ifdef LEDEBUG +void +am79900_recv_print(sc, no) + struct lance_softc *sc; + int no; +{ + struct lermd rmd; + u_int16_t len; + struct ether_header eh; + + (*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd)); + len = (rmd.rmd2 & 0xfff) - 4; + printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no, + len); + printf("%s: status %04x\n", sc->sc_dev.dv_xname, + (*sc->sc_rdcsr)(sc, LE_CSR0)); + printf("%s: adr %08x, flags/blen %08x\n", + sc->sc_dev.dv_xname, rmd.rmd0, rmd.rmd1); + if (len >= sizeof(eh)) { + (*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh)); + printf("%s: dst %s", sc->sc_dev.dv_xname, + ether_sprintf(eh.ether_dhost)); + printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost), + ntohs(eh.ether_type)); + } +} + +void +am79900_xmit_print(sc, no) + struct lance_softc *sc; + int no; +{ + struct letmd tmd; + u_int16_t len; + struct ether_header eh; + + (*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd)); + len = -(tmd.tmd1 & 0xfff); + printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no, + len); + printf("%s: status %04x\n", sc->sc_dev.dv_xname, + (*sc->sc_rdcsr)(sc, LE_CSR0)); + printf("%s: adr %08x, flags/blen %08x\n", + sc->sc_dev.dv_xname, tmd.tmd0, tmd.tmd1); + if (len >= sizeof(eh)) { + (*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh)); + printf("%s: dst %s", sc->sc_dev.dv_xname, + ether_sprintf(eh.ether_dhost)); + printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost), + ntohs(eh.ether_type)); + } +} +#endif /* LEDEBUG */ diff --git a/sys/dev/ic/am79900reg.h b/sys/dev/ic/am79900reg.h new file mode 100644 index 000000000000..d1af482e58dd --- /dev/null +++ b/sys/dev/ic/am79900reg.h @@ -0,0 +1,112 @@ +/* $NetBSD: am79900reg.h,v 1.1 1998/07/21 17:26:46 drochner Exp $ */ + +/*- + * Copyright (c) 1995 Charles M. Hannum. All rights reserved. + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell and Rick Macklem. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)if_lereg.h 8.1 (Berkeley) 6/10/93 + */ + +/* + * Receive message descriptor + */ +struct lermd { + u_int32_t rmd0; + u_int32_t rmd1; + u_int32_t rmd2; + int32_t rmd3; +}; + +/* + * Transmit message descriptor + */ +struct letmd { + u_int32_t tmd0; + u_int32_t tmd1; + u_int32_t tmd2; + int32_t tmd3; +}; + +/* + * Initialization block + */ +struct leinit { + u_int32_t init_mode; /* +0x0000 */ + u_int32_t init_padr[2]; /* +0x0002 */ + u_int16_t init_ladrf[4]; /* +0x0008 */ + u_int32_t init_rdra; /* +0x0010 */ + u_int32_t init_tdra; /* +0x0014 */ + int32_t pad; /* Pad to 16 shorts */ +}; + +/* Receive message descriptor 1 (rmd1_bits) */ +#define LE_R1_OWN (1<<31) /* LANCE owns the packet */ +#define LE_R1_ERR (1<<30) /* error summary */ +#define LE_R1_FRAM (1<<29) /* framing error */ +#define LE_R1_OFLO (1<<28) /* overflow error */ +#define LE_R1_CRC (1<<27) /* CRC error */ +#define LE_R1_BUFF (1<<26) /* buffer error */ +#define LE_R1_STP (1<<25) /* start of packet */ +#define LE_R1_ENP (1<<24) /* end of packet */ +#define LE_R1_ONES (0xf<<12) /* end of packet */ + +#define LE_R1_BITS \ + "\20\40OWN\37ERR\36FRAM\35OFLO\34CRC\33BUFF\32STP\31ENP" + +/* Transmit message descriptor 1 (tmd1_bits) */ +#define LE_T1_OWN (1<<31) /* LANCE owns the packet */ +#define LE_T1_ERR (1<<30) /* error summary */ +#define LE_T1_MORE (1<<28) /* multiple collisions */ +#define LE_T1_ONE (1<<27) /* single collision */ +#define LE_T1_DEF (1<<26) /* defferred transmit */ +#define LE_T1_STP (1<<25) /* start of packet */ +#define LE_T1_ENP (1<<24) /* end of packet */ +#define LE_T1_ONES (0xf<<12) /* end of packet */ + +#define LE_T1_BITS \ + "\20\40OWN\37ERR\36RES\35MORE\34ONE\33DEF\32STP\31ENP" + +/* Transmit message descriptor 3 (tmd3) */ +#define LE_T2_BUFF (1<<31) /* buffer error */ +#define LE_T2_UFLO (1<<30) /* underflow error */ +#define LE_T2_LCOL (1<<28) /* late collision */ +#define LE_T2_LCAR (1<<27) /* loss of carrier */ +#define LE_T2_RTRY (1<<26) /* retry error */ +#if 0 +#define LE_T3_TDR_MASK 0x03ff /* time domain reflectometry counter */ +#endif + +#define LE_T3_BITS \ + "\12\40BUFF\37UFLO\35LCOL\34LCAR\33RTRY" diff --git a/sys/dev/ic/am79900var.h b/sys/dev/ic/am79900var.h new file mode 100644 index 000000000000..0ef996bdd184 --- /dev/null +++ b/sys/dev/ic/am79900var.h @@ -0,0 +1,47 @@ +/* $NetBSD: am79900var.h,v 1.1 1998/07/21 17:26:46 drochner Exp $ */ + +/* + * Copyright (c) 1997 Jason R. Thorpe. All rights reserved. + * Copyright (c) 1995 Charles M. Hannum. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Charles M. Hannum. + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Ethernet software status per device. + * + * NOTE: this structure MUST be the first element in machine-dependent + * le_softc structures! This is designed SPECIFICALLY to make it possible + * to simply cast a "void *" to "struct le_softc *" or to + * "struct am79900_softc *". Among other things, this saves a lot of hair + * in the interrupt handlers. + */ +struct am79900_softc { + struct lance_softc lsc; +}; + +void am79900_config __P((struct am79900_softc *)); +int am79900_intr __P((void *)); diff --git a/sys/dev/ic/am7990reg.h b/sys/dev/ic/am7990reg.h index 34cf36008d2f..ae1cf7c889e3 100644 --- a/sys/dev/ic/am7990reg.h +++ b/sys/dev/ic/am7990reg.h @@ -1,4 +1,4 @@ -/* $NetBSD: am7990reg.h,v 1.4 1997/03/27 21:01:49 veego Exp $ */ +/* $NetBSD: am7990reg.h,v 1.5 1998/07/21 17:26:45 drochner Exp $ */ /*- * Copyright (c) 1995 Charles M. Hannum. All rights reserved. @@ -39,9 +39,6 @@ * @(#)if_lereg.h 8.1 (Berkeley) 6/10/93 */ -#define LEBLEN 1536 /* ETHERMTU + header + CRC */ -#define LEMINSIZE 60 /* should be 64 if mode DTCR is set */ - /* * Receive message descriptor */ @@ -88,67 +85,6 @@ struct leinit { int16_t pad0[4]; /* Pad to 16 shorts */ }; -#define LE_INITADDR(sc) (sc->sc_initaddr) -#define LE_RMDADDR(sc, bix) (sc->sc_rmdaddr + sizeof(struct lermd) * (bix)) -#define LE_TMDADDR(sc, bix) (sc->sc_tmdaddr + sizeof(struct letmd) * (bix)) -#define LE_RBUFADDR(sc, bix) (sc->sc_rbufaddr[bix]) -#define LE_TBUFADDR(sc, bix) (sc->sc_tbufaddr[bix]) - -/* register addresses */ -#define LE_CSR0 0x0000 /* Control and status register */ -#define LE_CSR1 0x0001 /* low address of init block */ -#define LE_CSR2 0x0002 /* high address of init block */ -#define LE_CSR3 0x0003 /* Bus master and control */ - -/* Control and status register 0 (csr0) */ -#define LE_C0_ERR 0x8000 /* error summary */ -#define LE_C0_BABL 0x4000 /* transmitter timeout error */ -#define LE_C0_CERR 0x2000 /* collision */ -#define LE_C0_MISS 0x1000 /* missed a packet */ -#define LE_C0_MERR 0x0800 /* memory error */ -#define LE_C0_RINT 0x0400 /* receiver interrupt */ -#define LE_C0_TINT 0x0200 /* transmitter interrupt */ -#define LE_C0_IDON 0x0100 /* initalization done */ -#define LE_C0_INTR 0x0080 /* interrupt condition */ -#define LE_C0_INEA 0x0040 /* interrupt enable */ -#define LE_C0_RXON 0x0020 /* receiver on */ -#define LE_C0_TXON 0x0010 /* transmitter on */ -#define LE_C0_TDMD 0x0008 /* transmit demand */ -#define LE_C0_STOP 0x0004 /* disable all external activity */ -#define LE_C0_STRT 0x0002 /* enable external activity */ -#define LE_C0_INIT 0x0001 /* begin initalization */ - -#define LE_C0_BITS \ - "\20\20ERR\17BABL\16CERR\15MISS\14MERR\13RINT\ -\12TINT\11IDON\10INTR\07INEA\06RXON\05TXON\04TDMD\03STOP\02STRT\01INIT" - -/* Control and status register 3 (csr3) */ -#define LE_C3_BSWP 0x0004 /* byte swap */ -#define LE_C3_ACON 0x0002 /* ALE control, eh? */ -#define LE_C3_BCON 0x0001 /* byte control */ - -/* Initialzation block (mode) */ -#define LE_MODE_PROM 0x8000 /* promiscuous mode */ -/* 0x7f80 reserved, must be zero */ -/* 0x4000 - 0x0080 are not available on LANCE 7990 */ -#define LE_MODE_DRCVBC 0x4000 /* disable receive brodcast */ -#define LE_MODE_DRCVPA 0x2000 /* disable physical address detection */ -#define LE_MODE_DLNKTST 0x1000 /* disable link status */ -#define LE_MODE_DAPC 0x0800 /* disable automatic polarity correction */ -#define LE_MODE_MENDECL 0x0400 /* MENDEC loopback mode */ -#define LE_MODE_LRTTSEL 0x0200 /* lower receice threshold / - transmit mode selection */ -#define LE_MODE_PSEL1 0x0100 /* port selection bit1 */ -#define LE_MODE_PSEL0 0x0080 /* port selection bit0 */ -#define LE_MODE_INTL 0x0040 /* internal loopback */ -#define LE_MODE_DRTY 0x0020 /* disable retry */ -#define LE_MODE_COLL 0x0010 /* force a collision */ -#define LE_MODE_DTCR 0x0008 /* disable transmit CRC */ -#define LE_MODE_LOOP 0x0004 /* loopback mode */ -#define LE_MODE_DTX 0x0002 /* disable transmitter */ -#define LE_MODE_DRX 0x0001 /* disable receiver */ -#define LE_MODE_NORMAL 0 /* none of the above */ - /* Receive message descriptor 1 (rmd1_bits) */ #define LE_R1_OWN 0x80 /* LANCE owns the packet */ #define LE_R1_ERR 0x40 /* error summary */ diff --git a/sys/dev/ic/am7990var.h b/sys/dev/ic/am7990var.h index 43d9b5e860cb..51ed296f3af2 100644 --- a/sys/dev/ic/am7990var.h +++ b/sys/dev/ic/am7990var.h @@ -1,4 +1,4 @@ -/* $NetBSD: am7990var.h,v 1.19 1998/07/04 22:18:49 jonathan Exp $ */ +/* $NetBSD: am7990var.h,v 1.20 1998/07/21 17:26:45 drochner Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -66,31 +66,9 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "rnd.h" - -#if NRND > 0 -#include -#endif - -#if defined(_KERNEL) && !defined(_LKM) -#include "opt_ddb.h" -#endif - -#ifdef DDB -#define integrate -#define hide -#else -#define integrate static __inline -#define hide static -#endif - /* * Ethernet software status per device. * - * Each interface is referenced by a network interface structure, - * ethercom.ec_if, which the routing code uses to locate the interface. - * This structure contains the output queue for the interface, its address, ... - * * NOTE: this structure MUST be the first element in machine-dependent * le_softc structures! This is designed SPECIFICALLY to make it possible * to simply cast a "void *" to "struct le_softc *" or to @@ -98,119 +76,8 @@ * in the interrupt handlers. */ struct am7990_softc { - struct device sc_dev; /* base device glue */ - struct ethercom sc_ethercom; /* Ethernet common part */ - struct ifmedia sc_media; /* our supported media */ - - /* - * Memory functions: - * - * copy to/from descriptor - * copy to/from buffer - * zero bytes in buffer - */ - void (*sc_copytodesc) - __P((struct am7990_softc *, void *, int, int)); - void (*sc_copyfromdesc) - __P((struct am7990_softc *, void *, int, int)); - void (*sc_copytobuf) - __P((struct am7990_softc *, void *, int, int)); - void (*sc_copyfrombuf) - __P((struct am7990_softc *, void *, int, int)); - void (*sc_zerobuf) - __P((struct am7990_softc *, int, int)); - - /* - * Machine-dependent functions: - * - * read/write CSR - * hardware reset hook - may be NULL - * hardware init hook - may be NULL - * no carrier hook - may be NULL - * media change hook - may be NULL - */ - u_int16_t (*sc_rdcsr) - __P((struct am7990_softc *, u_int16_t)); - void (*sc_wrcsr) - __P((struct am7990_softc *, u_int16_t, u_int16_t)); - void (*sc_hwreset) __P((struct am7990_softc *)); - void (*sc_hwinit) __P((struct am7990_softc *)); - void (*sc_nocarrier) __P((struct am7990_softc *)); - int (*sc_mediachange) __P((struct am7990_softc *)); - void (*sc_mediastatus) __P((struct am7990_softc *, - struct ifmediareq *)); - - /* - * Media-supported by this interface. If this is NULL, - * the only supported media is assumed to be "manual". - */ - int *sc_supmedia; - int sc_nsupmedia; - int sc_defaultmedia; - - /* PCnet bit to use software selection of a port */ - int sc_initmodemedia; - - int sc_havecarrier; /* carrier status */ - - void *sc_sh; /* shutdownhook cookie */ - - u_int16_t sc_conf3; /* CSR3 value */ - u_int16_t sc_saved_csr0;/* Value of csr0 at time of interrupt */ - - void *sc_mem; /* base address of RAM -- CPU's view */ - u_long sc_addr; /* base address of RAM -- LANCE's view */ - - u_long sc_memsize; /* size of RAM */ - - int sc_nrbuf; /* number of receive buffers */ - int sc_ntbuf; /* number of transmit buffers */ - int sc_last_rd; - int sc_first_td, sc_last_td, sc_no_td; - - int sc_initaddr; - int sc_rmdaddr; - int sc_tmdaddr; - int *sc_rbufaddr; - int *sc_tbufaddr; - -#ifdef LEDEBUG - int sc_debug; -#endif - u_int8_t sc_enaddr[6]; - u_int8_t sc_pad[2]; -#if NRND > 0 - rndsource_element_t rnd_source; -#endif + struct lance_softc lsc; }; void am7990_config __P((struct am7990_softc *)); -void am7990_init __P((struct am7990_softc *)); -int am7990_ioctl __P((struct ifnet *, u_long, caddr_t)); -void am7990_meminit __P((struct am7990_softc *)); -void am7990_reset __P((struct am7990_softc *)); -void am7990_setladrf __P((struct ethercom *, u_int16_t *)); -void am7990_start __P((struct ifnet *)); -void am7990_stop __P((struct am7990_softc *)); -void am7990_watchdog __P((struct ifnet *)); int am7990_intr __P((void *)); - -/* - * The following functions are only useful on certain cpu/bus - * combinations. They should be written in assembly language for - * maximum efficiency, but machine-independent versions are provided - * for drivers that have not yet been optimized. - */ -void am7990_copytobuf_contig __P((struct am7990_softc *, void *, int, int)); -void am7990_copyfrombuf_contig __P((struct am7990_softc *, void *, int, int)); -void am7990_zerobuf_contig __P((struct am7990_softc *, int, int)); - -#if 0 /* Example only - see am7990.c */ -void am7990_copytobuf_gap2 __P((struct am7990_softc *, void *, int, int)); -void am7990_copyfrombuf_gap2 __P((struct am7990_softc *, void *, int, int)); -void am7990_zerobuf_gap2 __P((struct am7990_softc *, int, int)); - -void am7990_copytobuf_gap16 __P((struct am7990_softc *, void *, int, int)); -void am7990_copyfrombuf_gap16 __P((struct am7990_softc *, void *, int, int)); -void am7990_zerobuf_gap16 __P((struct am7990_softc *, int, int)); -#endif /* Example only */ diff --git a/sys/dev/ic/lance.c b/sys/dev/ic/lance.c new file mode 100644 index 000000000000..fd13d9ab1bf1 --- /dev/null +++ b/sys/dev/ic/lance.c @@ -0,0 +1,1005 @@ +/* $NetBSD: lance.c,v 1.1 1998/07/21 17:26:46 drochner Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/*- + * Copyright (c) 1995 Charles M. Hannum. All rights reserved. + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell and Rick Macklem. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)if_le.c 8.2 (Berkeley) 11/16/93 + */ + +#include "opt_inet.h" +#include "opt_ccitt.h" +#include "opt_llc.h" +#include "opt_ns.h" +#include "bpfilter.h" +#include "rnd.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if NRND > 0 +#include +#endif + +#include +#include +#include +#include + +#ifdef INET +#include +#include +#include +#include +#include +#endif + +#ifdef NS +#include +#include +#endif + +#if defined(CCITT) && defined(LLC) +#include +#include +#include +#include +#include +#endif + +#if NBPFILTER > 0 +#include +#include +#endif + +#include +#include + +#if defined(_KERNEL) && !defined(_LKM) +#include "opt_ddb.h" +#endif + +#ifdef DDB +#define integrate +#define hide +#else +#define integrate static __inline +#define hide static +#endif + +integrate struct mbuf *lance_get __P((struct lance_softc *, int, int)); + +hide void lance_shutdown __P((void *)); + +int lance_mediachange __P((struct ifnet *)); +void lance_mediastatus __P((struct ifnet *, struct ifmediareq *)); + +static inline u_int16_t ether_cmp __P((void *, void *)); + +void lance_stop __P((struct lance_softc *)); +int lance_ioctl __P((struct ifnet *, u_long, caddr_t)); +void lance_watchdog __P((struct ifnet *)); + +/* + * Compare two Ether/802 addresses for equality, inlined and + * unrolled for speed. Use this like bcmp(). + * + * XXX: Add for stuff like this? + * XXX: or maybe add it to libkern.h instead? + * + * "I'd love to have an inline assembler version of this." + * XXX: Who wanted that? mycroft? I wrote one, but this + * version in C is as good as hand-coded assembly. -gwr + * + * Please do NOT tweak this without looking at the actual + * assembly code generated before and after your tweaks! + */ +static inline u_int16_t +ether_cmp(one, two) + void *one, *two; +{ + register u_int16_t *a = (u_short *) one; + register u_int16_t *b = (u_short *) two; + register u_int16_t diff; + +#ifdef m68k + /* + * The post-increment-pointer form produces the best + * machine code for m68k. This was carefully tuned + * so it compiles to just 8 short (2-byte) op-codes! + */ + diff = *a++ - *b++; + diff |= *a++ - *b++; + diff |= *a++ - *b++; +#else + /* + * Most modern CPUs do better with a single expresion. + * Note that short-cut evaluation is NOT helpful here, + * because it just makes the code longer, not faster! + */ + diff = (a[0] - b[0]) | (a[1] - b[1]) | (a[2] - b[2]); +#endif + + return (diff); +} + +#define ETHER_CMP ether_cmp + +#ifdef LANCE_REVC_BUG +/* Make sure this is short-aligned, for ether_cmp(). */ +static u_int16_t bcast_enaddr[3] = { ~0, ~0, ~0 }; +#endif + +#define ifp (&sc->sc_ethercom.ec_if) + +void +lance_config(sc) + struct lance_softc *sc; +{ + int i; + + /* Make sure the chip is stopped. */ + lance_stop(sc); + + /* Initialize ifnet structure. */ + bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ); + ifp->if_softc = sc; + ifp->if_start = sc->sc_start; + ifp->if_ioctl = lance_ioctl; + ifp->if_watchdog = lance_watchdog; + ifp->if_flags = + IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST; +#ifdef LANCE_REVC_BUG + ifp->if_flags &= ~IFF_MULTICAST; +#endif + + /* Initialize ifmedia structures. */ + ifmedia_init(&sc->sc_media, 0, lance_mediachange, lance_mediastatus); + if (sc->sc_supmedia != NULL) { + for (i = 0; i < sc->sc_nsupmedia; i++) + ifmedia_add(&sc->sc_media, sc->sc_supmedia[i], + 0, NULL); + ifmedia_set(&sc->sc_media, sc->sc_defaultmedia); + } else { + ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); + ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL); + } + + /* Attach the interface. */ + if_attach(ifp); + ether_ifattach(ifp, sc->sc_enaddr); + +#if NBPFILTER > 0 + bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header)); +#endif + + switch (sc->sc_memsize) { + case 8192: + sc->sc_nrbuf = 4; + sc->sc_ntbuf = 1; + break; + case 16384: + sc->sc_nrbuf = 8; + sc->sc_ntbuf = 2; + break; + case 32768: + sc->sc_nrbuf = 16; + sc->sc_ntbuf = 4; + break; + case 65536: + sc->sc_nrbuf = 32; + sc->sc_ntbuf = 8; + break; + case 131072: + sc->sc_nrbuf = 64; + sc->sc_ntbuf = 16; + break; + default: + panic("lance_config: weird memory size"); + } + + printf(": address %s\n", ether_sprintf(sc->sc_enaddr)); + printf("%s: %d receive buffers, %d transmit buffers\n", + sc->sc_dev.dv_xname, sc->sc_nrbuf, sc->sc_ntbuf); + + sc->sc_sh = shutdownhook_establish(lance_shutdown, sc); + if (sc->sc_sh == NULL) + panic("lance_config: can't establish shutdownhook"); + sc->sc_rbufaddr = malloc(sc->sc_nrbuf * sizeof(int), M_DEVBUF, + M_WAITOK); + sc->sc_tbufaddr = malloc(sc->sc_ntbuf * sizeof(int), M_DEVBUF, + M_WAITOK); + +#if NRND > 0 + rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname, + RND_TYPE_NET); +#endif +} + +void +lance_reset(sc) + struct lance_softc *sc; +{ + int s; + + s = splimp(); + lance_init(sc); + splx(s); +} + +void +lance_stop(sc) + struct lance_softc *sc; +{ + + (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP); +} + +/* + * Initialization of interface; set up initialization block + * and transmit/receive descriptor rings. + */ +void +lance_init(sc) + register struct lance_softc *sc; +{ + register int timo; + u_long a; + + (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_STOP); + DELAY(100); + + /* Newer LANCE chips have a reset register */ + if (sc->sc_hwreset) + (*sc->sc_hwreset)(sc); + + /* Set the correct byte swapping mode, etc. */ + (*sc->sc_wrcsr)(sc, LE_CSR3, sc->sc_conf3); + + /* Set up LANCE init block. */ + (*sc->sc_meminit)(sc); + + /* Give LANCE the physical address of its init block. */ + a = sc->sc_addr + LE_INITADDR(sc); + (*sc->sc_wrcsr)(sc, LE_CSR1, a); + (*sc->sc_wrcsr)(sc, LE_CSR2, a >> 16); + + /* Try to initialize the LANCE. */ + DELAY(100); + (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INIT); + + /* Wait for initialization to finish. */ + for (timo = 100000; timo; timo--) + if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) + break; + + if ((*sc->sc_rdcsr)(sc, LE_CSR0) & LE_C0_IDON) { + /* Start the LANCE. */ + (*sc->sc_wrcsr)(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT | + LE_C0_IDON); + ifp->if_flags |= IFF_RUNNING; + ifp->if_flags &= ~IFF_OACTIVE; + ifp->if_timer = 0; + (*sc->sc_start)(ifp); + } else + printf("%s: controller failed to initialize\n", + sc->sc_dev.dv_xname); + if (sc->sc_hwinit) + (*sc->sc_hwinit)(sc); +} + +/* + * Routine to copy from mbuf chain to transmit buffer in + * network buffer memory. + */ +int +lance_put(sc, boff, m) + struct lance_softc *sc; + int boff; + register struct mbuf *m; +{ + register struct mbuf *n; + register int len, tlen = 0; + + for (; m; m = n) { + len = m->m_len; + if (len == 0) { + MFREE(m, n); + continue; + } + (*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len); + boff += len; + tlen += len; + MFREE(m, n); + } + if (tlen < LEMINSIZE) { + (*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen); + tlen = LEMINSIZE; + } + return (tlen); +} + +/* + * Pull data off an interface. + * Len is length of data, with local net header stripped. + * We copy the data into mbufs. When full cluster sized units are present + * we copy into clusters. + */ +integrate struct mbuf * +lance_get(sc, boff, totlen) + struct lance_softc *sc; + int boff, totlen; +{ + register struct mbuf *m; + struct mbuf *top, **mp; + int len; + + MGETHDR(m, M_DONTWAIT, MT_DATA); + if (m == 0) + return (0); + m->m_pkthdr.rcvif = ifp; + m->m_pkthdr.len = totlen; + len = MHLEN; + top = 0; + mp = ⊤ + + while (totlen > 0) { + if (top) { + MGET(m, M_DONTWAIT, MT_DATA); + if (m == 0) { + m_freem(top); + return 0; + } + len = MLEN; + } + if (totlen >= MINCLSIZE) { + MCLGET(m, M_DONTWAIT); + if ((m->m_flags & M_EXT) == 0) { + m_free(m); + m_freem(top); + return 0; + } + len = MCLBYTES; + } + if (!top) { + register int pad = + ALIGN(sizeof(struct ether_header)) - + sizeof(struct ether_header); + m->m_data += pad; + len -= pad; + } + m->m_len = len = min(totlen, len); + (*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len); + boff += len; + totlen -= len; + *mp = m; + mp = &m->m_next; + } + + return (top); +} + +/* + * Pass a packet to the higher levels. + */ +void +lance_read(sc, boff, len) + register struct lance_softc *sc; + int boff, len; +{ + struct mbuf *m; + struct ether_header *eh; + + if (len <= sizeof(struct ether_header) || + len > ETHERMTU + sizeof(struct ether_header)) { +#ifdef LEDEBUG + printf("%s: invalid packet size %d; dropping\n", + sc->sc_dev.dv_xname, len); +#endif + ifp->if_ierrors++; + return; + } + + /* Pull packet off interface. */ + m = lance_get(sc, boff, len); + if (m == 0) { + ifp->if_ierrors++; + return; + } + + ifp->if_ipackets++; + + /* We assume that the header fit entirely in one mbuf. */ + eh = mtod(m, struct ether_header *); + +#if NBPFILTER > 0 + /* + * Check if there's a BPF listener on this interface. + * If so, hand off the raw packet to BPF. + */ + if (ifp->if_bpf) { + bpf_mtap(ifp->if_bpf, m); + +#ifndef LANCE_REVC_BUG + /* + * Note that the interface cannot be in promiscuous mode if + * there are no BPF listeners. And if we are in promiscuous + * mode, we have to check if this packet is really ours. + */ + if ((ifp->if_flags & IFF_PROMISC) != 0 && + (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */ + ETHER_CMP(eh->ether_dhost, sc->sc_enaddr)) { + m_freem(m); + return; + } +#endif + } +#endif + +#ifdef LANCE_REVC_BUG + /* + * The old LANCE (Rev. C) chips have a bug which causes + * garbage to be inserted in front of the received packet. + * The work-around is to ignore packets with an invalid + * destination address (garbage will usually not match). + * Of course, this precludes multicast support... + */ + if (ETHER_CMP(eh->ether_dhost, sc->sc_enaddr) && + ETHER_CMP(eh->ether_dhost, bcast_enaddr)) { + m_freem(m); + return; + } +#endif + + /* Pass the packet up, with the ether header sort-of removed. */ + m_adj(m, sizeof(struct ether_header)); + ether_input(ifp, eh, m); +} + +#undef ifp + +void +lance_watchdog(ifp) + struct ifnet *ifp; +{ + struct lance_softc *sc = ifp->if_softc; + + log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname); + ++ifp->if_oerrors; + + lance_reset(sc); +} + +int +lance_mediachange(ifp) + struct ifnet *ifp; +{ + struct lance_softc *sc = ifp->if_softc; + + if (sc->sc_mediachange) + return ((*sc->sc_mediachange)(sc)); + return (EINVAL); +} + +void +lance_mediastatus(ifp, ifmr) + struct ifnet *ifp; + struct ifmediareq *ifmr; +{ + struct lance_softc *sc = ifp->if_softc; + + if ((ifp->if_flags & IFF_UP) == 0) + return; + + ifmr->ifm_status = IFM_AVALID; + if (sc->sc_havecarrier) + ifmr->ifm_status |= IFM_ACTIVE; + + if (sc->sc_mediastatus) + (*sc->sc_mediastatus)(sc, ifmr); +} + +/* + * Process an ioctl request. + */ +int +lance_ioctl(ifp, cmd, data) + register struct ifnet *ifp; + u_long cmd; + caddr_t data; +{ + register struct lance_softc *sc = ifp->if_softc; + struct ifaddr *ifa = (struct ifaddr *)data; + struct ifreq *ifr = (struct ifreq *)data; + int s, error = 0; + + s = splimp(); + + switch (cmd) { + + case SIOCSIFADDR: + ifp->if_flags |= IFF_UP; + + switch (ifa->ifa_addr->sa_family) { +#ifdef INET + case AF_INET: + lance_init(sc); + arp_ifinit(ifp, ifa); + break; +#endif +#ifdef NS + case AF_NS: + { + register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr; + + if (ns_nullhost(*ina)) + ina->x_host = + *(union ns_host *)LLADDR(ifp->if_sadl); + else { + bcopy(ina->x_host.c_host, + LLADDR(ifp->if_sadl), + sizeof(sc->sc_enaddr)); + } + /* Set new address. */ + lance_init(sc); + break; + } +#endif + default: + lance_init(sc); + break; + } + break; + +#if defined(CCITT) && defined(LLC) + case SIOCSIFCONF_X25: + ifp->if_flags |= IFF_UP; + ifa->ifa_rtrequest = cons_rtrequest; /* XXX */ + error = x25_llcglue(PRC_IFUP, ifa->ifa_addr); + if (error == 0) + lance_init(sc); + break; +#endif /* CCITT && LLC */ + + case SIOCSIFFLAGS: + if ((ifp->if_flags & IFF_UP) == 0 && + (ifp->if_flags & IFF_RUNNING) != 0) { + /* + * If interface is marked down and it is running, then + * stop it. + */ + lance_stop(sc); + ifp->if_flags &= ~IFF_RUNNING; + } else if ((ifp->if_flags & IFF_UP) != 0 && + (ifp->if_flags & IFF_RUNNING) == 0) { + /* + * If interface is marked up and it is stopped, then + * start it. + */ + lance_init(sc); + } else { + /* + * Reset the interface to pick up changes in any other + * flags that affect hardware registers. + */ + /*lance_stop(sc);*/ + lance_init(sc); + } +#ifdef LEDEBUG + if (ifp->if_flags & IFF_DEBUG) + sc->sc_debug = 1; + else + sc->sc_debug = 0; +#endif + break; + + case SIOCADDMULTI: + case SIOCDELMULTI: + error = (cmd == SIOCADDMULTI) ? + ether_addmulti(ifr, &sc->sc_ethercom) : + ether_delmulti(ifr, &sc->sc_ethercom); + + if (error == ENETRESET) { + /* + * Multicast list has changed; set the hardware filter + * accordingly. + */ + lance_reset(sc); + error = 0; + } + break; + + case SIOCGIFMEDIA: + case SIOCSIFMEDIA: + error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); + break; + + default: + error = EINVAL; + break; + } + + splx(s); + return (error); +} + +hide void +lance_shutdown(arg) + void *arg; +{ + + lance_stop((struct lance_softc *)arg); +} + +/* + * Set up the logical address filter. + */ +void +lance_setladrf(ac, af) + struct ethercom *ac; + u_int16_t *af; +{ + struct ifnet *ifp = &ac->ec_if; + struct ether_multi *enm; + register u_char *cp; + register u_int32_t crc; + static const u_int32_t crctab[] = { + 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, + 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, + 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, + 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c + }; + register int len; + struct ether_multistep step; + + /* + * Set up multicast address filter by passing all multicast addresses + * through a crc generator, and then using the high order 6 bits as an + * index into the 64 bit logical address filter. The high order bit + * selects the word, while the rest of the bits select the bit within + * the word. + */ + + if (ifp->if_flags & IFF_PROMISC) + goto allmulti; + + af[0] = af[1] = af[2] = af[3] = 0x0000; + ETHER_FIRST_MULTI(step, ac, enm); + while (enm != NULL) { + if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) { + /* + * We must listen to a range of multicast addresses. + * For now, just accept all multicasts, rather than + * trying to set only those filter bits needed to match + * the range. (At this time, the only use of address + * ranges is for IP multicast routing, for which the + * range is big enough to require all bits set.) + */ + goto allmulti; + } + + cp = enm->enm_addrlo; + crc = 0xffffffff; + for (len = sizeof(enm->enm_addrlo); --len >= 0;) { + crc ^= *cp++; + crc = (crc >> 4) ^ crctab[crc & 0xf]; + crc = (crc >> 4) ^ crctab[crc & 0xf]; + } + /* Just want the 6 most significant bits. */ + crc >>= 26; + + /* Set the corresponding bit in the filter. */ + af[crc >> 4] |= 1 << (crc & 0xf); + + ETHER_NEXT_MULTI(step, enm); + } + ifp->if_flags &= ~IFF_ALLMULTI; + return; + +allmulti: + ifp->if_flags |= IFF_ALLMULTI; + af[0] = af[1] = af[2] = af[3] = 0xffff; +} + +/* + * Routines for accessing the transmit and receive buffers. + * The various CPU and adapter configurations supported by this + * driver require three different access methods for buffers + * and descriptors: + * (1) contig (contiguous data; no padding), + * (2) gap2 (two bytes of data followed by two bytes of padding), + * (3) gap16 (16 bytes of data followed by 16 bytes of padding). + */ + +/* + * contig: contiguous data with no padding. + * + * Buffers may have any alignment. + */ + +void +lance_copytobuf_contig(sc, from, boff, len) + struct lance_softc *sc; + void *from; + int boff, len; +{ + volatile caddr_t buf = sc->sc_mem; + + /* + * Just call bcopy() to do the work. + */ + bcopy(from, buf + boff, len); +} + +void +lance_copyfrombuf_contig(sc, to, boff, len) + struct lance_softc *sc; + void *to; + int boff, len; +{ + volatile caddr_t buf = sc->sc_mem; + + /* + * Just call bcopy() to do the work. + */ + bcopy(buf + boff, to, len); +} + +void +lance_zerobuf_contig(sc, boff, len) + struct lance_softc *sc; + int boff, len; +{ + volatile caddr_t buf = sc->sc_mem; + + /* + * Just let bzero() do the work + */ + bzero(buf + boff, len); +} + +#if 0 +/* + * Examples only; duplicate these and tweak (if necessary) in + * machine-specific front-ends. + */ + +/* + * gap2: two bytes of data followed by two bytes of pad. + * + * Buffers must be 4-byte aligned. The code doesn't worry about + * doing an extra byte. + */ + +void +lance_copytobuf_gap2(sc, fromv, boff, len) + struct lance_softc *sc; + void *fromv; + int boff; + register int len; +{ + volatile caddr_t buf = sc->sc_mem; + register caddr_t from = fromv; + register volatile u_int16_t *bptr; + + if (boff & 0x1) { + /* handle unaligned first byte */ + bptr = ((volatile u_int16_t *)buf) + (boff - 1); + *bptr = (*from++ << 8) | (*bptr & 0xff); + bptr += 2; + len--; + } else + bptr = ((volatile u_int16_t *)buf) + boff; + while (len > 1) { + *bptr = (from[1] << 8) | (from[0] & 0xff); + bptr += 2; + from += 2; + len -= 2; + } + if (len == 1) + *bptr = (u_int16_t)*from; +} + +void +lance_copyfrombuf_gap2(sc, tov, boff, len) + struct lance_softc *sc; + void *tov; + int boff, len; +{ + volatile caddr_t buf = sc->sc_mem; + register caddr_t to = tov; + register volatile u_int16_t *bptr; + register u_int16_t tmp; + + if (boff & 0x1) { + /* handle unaligned first byte */ + bptr = ((volatile u_int16_t *)buf) + (boff - 1); + *to++ = (*bptr >> 8) & 0xff; + bptr += 2; + len--; + } else + bptr = ((volatile u_int16_t *)buf) + boff; + while (len > 1) { + tmp = *bptr; + *to++ = tmp & 0xff; + *to++ = (tmp >> 8) & 0xff; + bptr += 2; + len -= 2; + } + if (len == 1) + *to = *bptr & 0xff; +} + +void +lance_zerobuf_gap2(sc, boff, len) + struct lance_softc *sc; + int boff, len; +{ + volatile caddr_t buf = sc->sc_mem; + register volatile u_int16_t *bptr; + + if ((unsigned)boff & 0x1) { + bptr = ((volatile u_int16_t *)buf) + (boff - 1); + *bptr &= 0xff; + bptr += 2; + len--; + } else + bptr = ((volatile u_int16_t *)buf) + boff; + while (len > 0) { + *bptr = 0; + bptr += 2; + len -= 2; + } +} + +/* + * gap16: 16 bytes of data followed by 16 bytes of pad. + * + * Buffers must be 32-byte aligned. + */ + +void +lance_copytobuf_gap16(sc, fromv, boff, len) + struct lance_softc *sc; + void *fromv; + int boff; + register int len; +{ + volatile caddr_t buf = sc->sc_mem; + register caddr_t from = fromv; + register caddr_t bptr; + register int xfer; + + bptr = buf + ((boff << 1) & ~0x1f); + boff &= 0xf; + xfer = min(len, 16 - boff); + while (len > 0) { + bcopy(from, bptr + boff, xfer); + from += xfer; + bptr += 32; + boff = 0; + len -= xfer; + xfer = min(len, 16); + } +} + +void +lance_copyfrombuf_gap16(sc, tov, boff, len) + struct lance_softc *sc; + void *tov; + int boff, len; +{ + volatile caddr_t buf = sc->sc_mem; + register caddr_t to = tov; + register caddr_t bptr; + register int xfer; + + bptr = buf + ((boff << 1) & ~0x1f); + boff &= 0xf; + xfer = min(len, 16 - boff); + while (len > 0) { + bcopy(bptr + boff, to, xfer); + to += xfer; + bptr += 32; + boff = 0; + len -= xfer; + xfer = min(len, 16); + } +} + +void +lance_zerobuf_gap16(sc, boff, len) + struct lance_softc *sc; + int boff, len; +{ + volatile caddr_t buf = sc->sc_mem; + register caddr_t bptr; + register int xfer; + + bptr = buf + ((boff << 1) & ~0x1f); + boff &= 0xf; + xfer = min(len, 16 - boff); + while (len > 0) { + bzero(bptr + boff, xfer); + bptr += 32; + boff = 0; + len -= xfer; + xfer = min(len, 16); + } +} +#endif /* Example only */ diff --git a/sys/dev/ic/lancereg.h b/sys/dev/ic/lancereg.h new file mode 100644 index 000000000000..a2a16b0ec54b --- /dev/null +++ b/sys/dev/ic/lancereg.h @@ -0,0 +1,104 @@ +/* $NetBSD: lancereg.h,v 1.1 1998/07/21 17:26:46 drochner Exp $ */ + +/*- + * Copyright (c) 1995 Charles M. Hannum. All rights reserved. + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell and Rick Macklem. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)if_lereg.h 8.1 (Berkeley) 6/10/93 + */ + +#define LEBLEN 1536 /* ETHERMTU + header + CRC */ +#define LEMINSIZE 60 /* should be 64 if mode DTCR is set */ + +#define LE_INITADDR(sc) (sc->sc_initaddr) +#define LE_RMDADDR(sc, bix) (sc->sc_rmdaddr + sizeof(struct lermd) * (bix)) +#define LE_TMDADDR(sc, bix) (sc->sc_tmdaddr + sizeof(struct letmd) * (bix)) +#define LE_RBUFADDR(sc, bix) (sc->sc_rbufaddr[bix]) +#define LE_TBUFADDR(sc, bix) (sc->sc_tbufaddr[bix]) + +/* register addresses */ +#define LE_CSR0 0x0000 /* Control and status register */ +#define LE_CSR1 0x0001 /* low address of init block */ +#define LE_CSR2 0x0002 /* high address of init block */ +#define LE_CSR3 0x0003 /* Bus master and control */ + +/* Control and status register 0 (csr0) */ +#define LE_C0_ERR 0x8000 /* error summary */ +#define LE_C0_BABL 0x4000 /* transmitter timeout error */ +#define LE_C0_CERR 0x2000 /* collision */ +#define LE_C0_MISS 0x1000 /* missed a packet */ +#define LE_C0_MERR 0x0800 /* memory error */ +#define LE_C0_RINT 0x0400 /* receiver interrupt */ +#define LE_C0_TINT 0x0200 /* transmitter interrupt */ +#define LE_C0_IDON 0x0100 /* initalization done */ +#define LE_C0_INTR 0x0080 /* interrupt condition */ +#define LE_C0_INEA 0x0040 /* interrupt enable */ +#define LE_C0_RXON 0x0020 /* receiver on */ +#define LE_C0_TXON 0x0010 /* transmitter on */ +#define LE_C0_TDMD 0x0008 /* transmit demand */ +#define LE_C0_STOP 0x0004 /* disable all external activity */ +#define LE_C0_STRT 0x0002 /* enable external activity */ +#define LE_C0_INIT 0x0001 /* begin initalization */ + +#define LE_C0_BITS \ + "\20\20ERR\17BABL\16CERR\15MISS\14MERR\13RINT\ +\12TINT\11IDON\10INTR\07INEA\06RXON\05TXON\04TDMD\03STOP\02STRT\01INIT" + +/* Control and status register 3 (csr3) */ +#define LE_C3_BSWP 0x0004 /* byte swap */ +#define LE_C3_ACON 0x0002 /* ALE control, eh? */ +#define LE_C3_BCON 0x0001 /* byte control */ + +/* Initialzation block (mode) */ +#define LE_MODE_PROM 0x8000 /* promiscuous mode */ +/* 0x7f80 reserved, must be zero */ +/* 0x4000 - 0x0080 are not available on LANCE 7990 */ +#define LE_MODE_DRCVBC 0x4000 /* disable receive brodcast */ +#define LE_MODE_DRCVPA 0x2000 /* disable physical address detection */ +#define LE_MODE_DLNKTST 0x1000 /* disable link status */ +#define LE_MODE_DAPC 0x0800 /* disable automatic polarity correction */ +#define LE_MODE_MENDECL 0x0400 /* MENDEC loopback mode */ +#define LE_MODE_LRTTSEL 0x0200 /* lower receice threshold / + transmit mode selection */ +#define LE_MODE_PSEL1 0x0100 /* port selection bit1 */ +#define LE_MODE_PSEL0 0x0080 /* port selection bit0 */ +#define LE_MODE_INTL 0x0040 /* internal loopback */ +#define LE_MODE_DRTY 0x0020 /* disable retry */ +#define LE_MODE_COLL 0x0010 /* force a collision */ +#define LE_MODE_DTCR 0x0008 /* disable transmit CRC */ +#define LE_MODE_LOOP 0x0004 /* loopback mode */ +#define LE_MODE_DTX 0x0002 /* disable transmitter */ +#define LE_MODE_DRX 0x0001 /* disable receiver */ +#define LE_MODE_NORMAL 0 /* none of the above */ diff --git a/sys/dev/ic/lancevar.h b/sys/dev/ic/lancevar.h new file mode 100644 index 000000000000..3fbe7b0b983f --- /dev/null +++ b/sys/dev/ic/lancevar.h @@ -0,0 +1,190 @@ +/* $NetBSD: lancevar.h,v 1.1 1998/07/21 17:26:46 drochner Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Copyright (c) 1995 Charles M. Hannum. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Charles M. Hannum. + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "rnd.h" + +#if NRND > 0 +#include +#endif + +struct lance_softc { + struct device sc_dev; /* base device glue */ + struct ethercom sc_ethercom; /* Ethernet common part */ + struct ifmedia sc_media; /* our supported media */ + + /* + * Memory functions: + * + * copy to/from descriptor + * copy to/from buffer + * zero bytes in buffer + */ + void (*sc_copytodesc) + __P((struct lance_softc *, void *, int, int)); + void (*sc_copyfromdesc) + __P((struct lance_softc *, void *, int, int)); + void (*sc_copytobuf) + __P((struct lance_softc *, void *, int, int)); + void (*sc_copyfrombuf) + __P((struct lance_softc *, void *, int, int)); + void (*sc_zerobuf) + __P((struct lance_softc *, int, int)); + + /* + * Machine-dependent functions: + * + * read/write CSR + * hardware reset hook - may be NULL + * hardware init hook - may be NULL + * no carrier hook - may be NULL + * media change hook - may be NULL + */ + u_int16_t (*sc_rdcsr) + __P((struct lance_softc *, u_int16_t)); + void (*sc_wrcsr) + __P((struct lance_softc *, u_int16_t, u_int16_t)); + void (*sc_hwreset) __P((struct lance_softc *)); + void (*sc_hwinit) __P((struct lance_softc *)); + void (*sc_nocarrier) __P((struct lance_softc *)); + int (*sc_mediachange) __P((struct lance_softc *)); + void (*sc_mediastatus) __P((struct lance_softc *, + struct ifmediareq *)); + + /* + * Media-supported by this interface. If this is NULL, + * the only supported media is assumed to be "manual". + */ + int *sc_supmedia; + int sc_nsupmedia; + int sc_defaultmedia; + + /* PCnet bit to use software selection of a port */ + int sc_initmodemedia; + + int sc_havecarrier; /* carrier status */ + + void *sc_sh; /* shutdownhook cookie */ + + u_int16_t sc_conf3; /* CSR3 value */ + u_int16_t sc_saved_csr0;/* Value of csr0 at time of interrupt */ + + void *sc_mem; /* base address of RAM -- CPU's view */ + u_long sc_addr; /* base address of RAM -- LANCE's view */ + + u_long sc_memsize; /* size of RAM */ + + int sc_nrbuf; /* number of receive buffers */ + int sc_ntbuf; /* number of transmit buffers */ + int sc_last_rd; + int sc_first_td, sc_last_td, sc_no_td; + + int sc_initaddr; + int sc_rmdaddr; + int sc_tmdaddr; + int *sc_rbufaddr; + int *sc_tbufaddr; + +#ifdef LEDEBUG + int sc_debug; +#endif + u_int8_t sc_enaddr[6]; + u_int8_t sc_pad[2]; +#if NRND > 0 + rndsource_element_t rnd_source; +#endif + + void (*sc_meminit) __P((struct lance_softc *)); + void (*sc_start) __P((struct ifnet *)); +}; + +void lance_config __P((struct lance_softc *)); +void lance_reset __P((struct lance_softc *)); +void lance_init __P((struct lance_softc *)); +int lance_put __P((struct lance_softc *, int, struct mbuf *)); +void lance_read __P((struct lance_softc *, int, int)); +void lance_setladrf __P((struct ethercom *, u_int16_t *)); + +/* + * The following functions are only useful on certain cpu/bus + * combinations. They should be written in assembly language for + * maximum efficiency, but machine-independent versions are provided + * for drivers that have not yet been optimized. + */ +void lance_copytobuf_contig __P((struct lance_softc *, void *, int, int)); +void lance_copyfrombuf_contig __P((struct lance_softc *, void *, int, int)); +void lance_zerobuf_contig __P((struct lance_softc *, int, int)); + +#if 0 /* Example only - see lance.c */ +void lance_copytobuf_gap2 __P((struct lance_softc *, void *, int, int)); +void lance_copyfrombuf_gap2 __P((struct lance_softc *, void *, int, int)); +void lance_zerobuf_gap2 __P((struct lance_softc *, int, int)); + +void lance_copytobuf_gap16 __P((struct lance_softc *, void *, int, int)); +void lance_copyfrombuf_gap16 __P((struct lance_softc *, void *, int, int)); +void lance_zerobuf_gap16 __P((struct lance_softc *, int, int)); +#endif /* Example only */